4 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
5 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
7 #define MORPH defined(MORPH_POSITION) || defined(MORPH_NORMAL) || defined(MORPH_TANGENT)
9 // These lines in the shader may be replaced with actual definitions by the model loader,
10 // if they are needed. Note, some shader compilers have problems with spurious ";", so
11 // the macro invocations don't have a trailing ";". The replacement strings in the model
12 // loader will provide it instead.
13 #define ADD_EXTRA_SKINNING_ATTRIBUTES
14 #define ADD_EXTRA_WEIGHTS
17 precision highp float;
19 precision mediump float;
37 ADD_EXTRA_SKINNING_ATTRIBUTES
41 uniform highp sampler2D sBlendShapeGeometry;
47 out highp vec3 vPositionToCamera;
49 uniform highp mat4 uViewMatrix;
50 uniform mat3 uNormalMatrix;
51 uniform mat4 uModelMatrix;
52 uniform mat4 uProjection;
57 layout(std140) uniform Bones
59 mat4 uBone[MAX_BONES];
62 uniform mediump vec3 uYDirection;
66 #define MAX_BLEND_SHAPE_NUMBER 128
67 uniform int uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
68 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
69 #ifdef MORPH_VERSION_2_0
70 uniform highp float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
72 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
74 uniform highp int uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
78 uniform lowp int uIsShadowEnabled;
79 uniform highp mat4 uShadowLightViewProjectionMatrix;
80 out highp vec3 positionFromLightView;
84 highp vec4 position = vec4(aPosition, 1.0);
85 highp vec3 normal = aNormal;
86 highp vec3 tangent = aTangent.xyz;
89 int width = textureSize( sBlendShapeGeometry, 0 ).x;
91 highp int blendShapeBufferOffset = 0;
93 for( int index = 0; index < uNumberOfBlendShapes; ++index )
95 highp vec3 diff = vec3(0.0);
96 highp int vertexId = 0;
99 highp float weight = clamp(uBlendShapeWeight[index], 0.0, 1.0);
101 #ifdef MORPH_POSITION
102 // Calculate the index to retrieve the geometry from the texture.
103 vertexId = gl_VertexID + blendShapeBufferOffset;
104 x = vertexId % width;
105 y = vertexId / width;
107 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
110 #ifdef MORPH_VERSION_2_0
111 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
113 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
116 diff = weight * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
119 position.xyz += diff;
121 blendShapeBufferOffset += uBlendShapeComponentSize;
125 // Calculate the index to retrieve the normal from the texture.
126 vertexId = gl_VertexID + blendShapeBufferOffset;
127 x = vertexId % width;
128 y = vertexId / width;
130 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
133 diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
138 blendShapeBufferOffset += uBlendShapeComponentSize;
142 // Calculate the index to retrieve the tangent from the texture.
143 vertexId = gl_VertexID + blendShapeBufferOffset;
144 x = vertexId % width;
145 y = vertexId / width;
147 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
150 diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
155 blendShapeBufferOffset += uBlendShapeComponentSize;
166 uBone[int(aJoints0.x)] * aWeights0.x +
167 uBone[int(aJoints0.y)] * aWeights0.y +
168 uBone[int(aJoints0.z)] * aWeights0.z +
169 uBone[int(aJoints0.w)] * aWeights0.w;
173 position = bone * position;
174 normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
175 tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
177 highp vec4 positionW = position;
179 highp vec4 positionW = uModelMatrix * position;
182 highp vec4 positionV = uViewMatrix * positionW;
184 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
186 normal = normalize(normal);
187 tangent = normalize(tangent);
188 vec3 bitangent = cross(normal, tangent);
190 bitangent *= aTangent.w;
192 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
195 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
200 vColor = aVertexColor;
202 positionFromLightView = vec3(1.0);
203 if(uIsShadowEnabled > 0)
205 highp vec4 positionInLightView = uShadowLightViewProjectionMatrix * positionW;
206 positionFromLightView = ((positionInLightView.xyz / positionInLightView.w) * 0.5) + vec3(0.5);
209 gl_Position = uProjection * positionV;