Skip to content

Instantly share code, notes, and snippets.

@spite
Last active June 9, 2018 09:59
Show Gist options
  • Save spite/9110247 to your computer and use it in GitHub Desktop.
Save spite/9110247 to your computer and use it in GitHub Desktop.
Pass Inverse of ModelView Matrix to Vertex Shader
/*
uniform types: https://github.com/mrdoob/three.js/wiki/Uniforms-types
THREE.Matrix4: http://threejs.org/docs/#Reference/Math/Matrix4
https://github.com/mrdoob/three.js/issues/1188
*/
/*
on Init();
Add this uniform to your uniforms
myModelViewMatrixInverse: { type: 'm4', value: new THREE.Matrix4() }
*/
material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
eyeGeoL = new THREE.Mesh(geometry, material );
/*
on Render();
camera.matrixWorldInverse * object.matrixWorld => modelViewMatrix
the order might be wrong, I never get it right at first try
*/
var m = new THREE.Matrix4();
m.copy( eyeGeoL.matrixWorld );
m.multiply( camera.matrixWorldInverse );
var i = new THREE.Matrix4().getInverse( m );
eyeGeoL.material.uniforms.myModelViewMatrixInverse.copy( i );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment