Three.js shader内置矩阵注入
common matrices (公共矩阵)
three.js
在WebGLRender
中setProgram
函数注入
p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix );
p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix );
p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
// = object.matrixWorld
uniform mat4 modelMatrix;// = camera.matrixWorldInverse * object.matrixWorld
uniform mat4 modelViewMatrix;// = camera.projectionMatrix
uniform mat4 projectionMatrix;// = camera.matrixWorldInverse
uniform mat4 viewMatrix;// = inverse transpose of modelViewMatrix
uniform mat3 normalMatrix;// = camera position in world space
uniform vec3 cameraPosition;
// default vertex attributes provided by BufferGeometry
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4( position, 1.0 );
#ifdef USE_TANGENTattribute vec4 tangent;
#endif
#if defined( USE_COLOR_ALPHA )// vertex color attribute with alphaattribute vec4 color;
#elif defined( USE_COLOR )// vertex color attributeattribute vec3 color;
#endif
#ifdef USE_MORPHTARGETSattribute vec3 morphTarget0;attribute vec3 morphTarget1;attribute vec3 morphTarget2;attribute vec3 morphTarget3;#ifdef USE_MORPHNORMALSattribute vec3 morphNormal0;attribute vec3 morphNormal1;attribute vec3 morphNormal2;attribute vec3 morphNormal3;#elseattribute vec3 morphTarget4;attribute vec3 morphTarget5;attribute vec3 morphTarget6;attribute vec3 morphTarget7;#endif
#endif
#ifdef USE_SKINNINGattribute vec4 skinIndex;attribute vec4 skinWeight;
#endif
#ifdef USE_INSTANCING// Note that modelViewMatrix is not set when rendering an instanced model,// but can be calculated from viewMatrix * modelMatrix.//// Basic Usage:// gl_Position = projectionMatrix * viewMatrix * modelMatrix * instanceMatrix * vec4(position, 1.0);attribute mat4 instanceMatrix;
#endif
uniform mat4 viewMatrix;
uniform vec3 cameraPosition;