3 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
4 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
6 #define MORPH defined(MORPH_POSITION) || defined(MORPH_NORMAL) || defined(MORPH_TANGENT)
8 // These lines in the shader may be replaced with actual definitions by the model loader,
9 // if they are needed. Note, some shader compilers have problems with spurious ";", so
10 // the macro invocations don't have a trailing ";". The replacement strings in the model
11 // loader will provide it instead.
12 #define ADD_EXTRA_SKINNING_ATTRIBUTES
13 #define ADD_EXTRA_WEIGHTS
16 precision highp float;
18 precision mediump float;
31 INPUT vec4 aVertexColor;
34 INPUT float aVertexID;
40 ADD_EXTRA_SKINNING_ATTRIBUTES
44 uniform highp sampler2D sBlendShapeGeometry;
46 uniform int uBlendShapeGeometryWidth;
47 uniform int uBlendShapeGeometryHeight;
51 OUTPUT mediump vec2 vUV;
52 OUTPUT lowp mat3 vTBN;
53 OUTPUT lowp vec4 vColor;
54 OUTPUT highp vec3 vPositionToCamera;
56 uniform highp mat4 uViewMatrix;
57 uniform mat3 uNormalMatrix;
58 uniform mat4 uModelMatrix;
59 uniform mat4 uProjection;
65 uniform mat4 uBone[MAX_BONES];
68 layout(std140) uniform Bones
70 mat4 uBone[MAX_BONES];
74 uniform mediump vec3 uYDirection;
78 #define MAX_BLEND_SHAPE_NUMBER 256
79 uniform int uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
80 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
81 #ifdef MORPH_VERSION_2_0
82 uniform highp float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
84 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
86 uniform highp int uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
90 uniform lowp int uIsShadowEnabled;
91 uniform highp mat4 uShadowLightViewProjectionMatrix;
92 OUTPUT highp vec3 positionFromLightView;
96 highp vec4 position = vec4(aPosition, 1.0);
97 highp vec3 normal = aNormal;
98 highp vec3 tangent = aTangent.xyz;
101 #ifdef SL_VERSION_LOW
102 int width = uBlendShapeGeometryWidth;
104 int width = textureSize( sBlendShapeGeometry, 0 ).x;
107 highp int blendShapeBufferOffset = 0;
109 #ifdef SL_VERSION_LOW
110 highp float blendShapeWidth = float(uBlendShapeGeometryWidth);
111 highp float blendShapeHeight = float(uBlendShapeGeometryHeight);
112 highp float invertBlendShapeWidth = 1.0 / blendShapeWidth;
113 highp float invertBlendShapeHeight = 1.0 / blendShapeHeight;
116 for( int index = 0; index < uNumberOfBlendShapes; ++index )
118 highp vec3 diff = vec3(0.0);
119 highp int vertexId = 0;
122 highp float weight = clamp(uBlendShapeWeight[index], 0.0, 1.0);
124 #ifdef MORPH_POSITION
125 // Calculate the index to retrieve the geometry from the texture.
126 #ifdef SL_VERSION_LOW
127 vertexId = int(floor(aVertexID + 0.5)) + blendShapeBufferOffset;
128 y = vertexId / width;
129 x = vertexId - y * width;
131 vertexId = gl_VertexID + blendShapeBufferOffset;
132 x = vertexId % width;
133 y = vertexId / width;
136 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
139 #ifdef MORPH_VERSION_2_0
140 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
142 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
145 #ifdef SL_VERSION_LOW
146 highp float floatX = float(x) + 0.5;
147 highp float floatY = float(y) + 0.5;
148 diff = weight * unnormalizeFactor * ( texture2D( sBlendShapeGeometry, vec2(floatX * invertBlendShapeWidth, floatY * invertBlendShapeHeight) ).xyz - 0.5 );
150 diff = weight * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
154 position.xyz += diff;
156 blendShapeBufferOffset += uBlendShapeComponentSize;
160 // Calculate the index to retrieve the normal from the texture.
161 #ifdef SL_VERSION_LOW
162 vertexId = int(floor(aVertexID + 0.5)) + blendShapeBufferOffset;
163 y = vertexId / width;
164 x = vertexId - y * width;
166 vertexId = gl_VertexID + blendShapeBufferOffset;
167 x = vertexId % width;
168 y = vertexId / width;
171 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
174 #ifdef SL_VERSION_LOW
175 highp float floatX = float(x) + 0.5;
176 highp float floatY = float(y) + 0.5;
177 diff = weight * 2.0 * ( texture2D( sBlendShapeGeometry, vec2(floatX * invertBlendShapeWidth, floatY * invertBlendShapeHeight) ).xyz - 0.5 );
179 diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
185 blendShapeBufferOffset += uBlendShapeComponentSize;
189 // Calculate the index to retrieve the tangent from the texture.
190 #ifdef SL_VERSION_LOW
191 vertexId = int(floor(aVertexID + 0.5)) + blendShapeBufferOffset;
192 y = vertexId / width;
193 x = vertexId - y * width;
195 vertexId = gl_VertexID + blendShapeBufferOffset;
196 x = vertexId % width;
197 y = vertexId / width;
200 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
203 #ifdef SL_VERSION_LOW
204 highp float floatX = float(x) + 0.5;
205 highp float floatY = float(y) + 0.5;
206 diff = weight * 2.0 * ( texture2D( sBlendShapeGeometry, vec2(floatX * invertBlendShapeWidth, floatY * invertBlendShapeHeight) ).xyz - 0.5 );
208 diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
214 blendShapeBufferOffset += uBlendShapeComponentSize;
225 uBone[int(aJoints0.x)] * aWeights0.x +
226 uBone[int(aJoints0.y)] * aWeights0.y +
227 uBone[int(aJoints0.z)] * aWeights0.z +
228 uBone[int(aJoints0.w)] * aWeights0.w;
232 position = bone * position;
233 normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
234 tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
236 highp vec4 positionW = position;
238 highp vec4 positionW = uModelMatrix * position;
241 highp vec4 positionV = uViewMatrix * positionW;
243 #ifdef SL_VERSION_LOW
244 highp vec3 i0 = uViewMatrix[0].xyz;
245 highp vec3 i1 = uViewMatrix[1].xyz;
246 highp vec3 i2 = uViewMatrix[2].xyz;
248 vPositionToCamera = mat3(vec3(i0.x, i1.x, i2.x), vec3(i0.y, i1.y, i2.y), vec3(i0.z, i1.z, i2.z)) * -vec3(positionV.xyz / positionV.w);
250 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
253 normal = normalize(normal);
254 tangent = normalize(tangent);
255 vec3 bitangent = cross(normal, tangent);
257 bitangent *= aTangent.w;
259 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
262 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
267 vColor = aVertexColor;
269 positionFromLightView = vec3(1.0);
270 if(uIsShadowEnabled > 0)
272 highp vec4 positionInLightView = uShadowLightViewProjectionMatrix * positionW;
273 positionFromLightView = ((positionInLightView.xyz / positionInLightView.w) * 0.5) + vec3(0.5);
276 gl_Position = uProjection * positionV;