4 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
5 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
10 precision mediump float;
26 uniform highp sampler2D sBlendShapeGeometry;
32 out highp vec3 vPositionToCamera;
34 uniform highp mat4 uViewMatrix;
35 uniform mat3 uNormalMatrix;
36 uniform mat4 uModelMatrix;
37 uniform mat4 uProjection;
43 uniform mat4 uBone[MAX_BONES];
44 uniform mediump vec3 uYDirection;
48 #define MAX_BLEND_SHAPE_NUMBER 128
49 uniform float uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
50 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
51 #ifdef MORPH_VERSION_2_0
52 uniform highp float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
54 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
56 uniform highp float uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
61 highp vec4 position = vec4(aPosition, 1.0);
62 highp vec3 normal = aNormal;
63 highp vec3 tangent = aTangent.xyz;
66 int width = textureSize( sBlendShapeGeometry, 0 ).x;
68 highp int blendShapeBufferOffset = 0;
69 highp int blendShapeComponentSize = int(uBlendShapeComponentSize);
70 int numberOfBlendShapes = int(uNumberOfBlendShapes);
72 for( int index = 0; index < numberOfBlendShapes; ++index )
74 highp vec3 diff = vec3(0.0);
75 highp int vertexId = 0;
80 // Calculate the index to retrieve the geometry from the texture.
81 vertexId = gl_VertexID + blendShapeBufferOffset;
85 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
86 if( 0.0 != uBlendShapeWeight[index] )
88 #ifdef MORPH_VERSION_2_0
89 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
91 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
94 diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
99 blendShapeBufferOffset += blendShapeComponentSize;
103 // Calculate the index to retrieve the normal from the texture.
104 vertexId = gl_VertexID + int(blendShapeBufferOffset);
105 x = vertexId % width;
106 y = vertexId / width;
108 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
109 if( 0.0 != uBlendShapeWeight[index] )
111 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
116 blendShapeBufferOffset += blendShapeComponentSize;
120 // Calculate the index to retrieve the tangent from the texture.
121 vertexId = gl_VertexID + blendShapeBufferOffset;
122 x = vertexId % width;
123 y = vertexId / width;
125 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
126 if( 0.0 != uBlendShapeWeight[index] )
128 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
133 blendShapeBufferOffset += blendShapeComponentSize;
140 highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
141 uBone[int(aJoints.y)] * aWeights.y +
142 uBone[int(aJoints.z)] * aWeights.z +
143 uBone[int(aJoints.w)] * aWeights.w;
144 position = bone * position;
145 normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
146 tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
148 highp vec4 positionW = position;
150 highp vec4 positionW = uModelMatrix * position;
152 highp vec4 positionV = uViewMatrix * positionW;
154 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
156 normal = normalize(normal);
157 tangent = normalize(tangent);
158 vec3 bitangent = cross(normal, tangent);
160 bitangent *= aTangent.w;
162 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
165 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
170 vColor = aVertexColor;
172 gl_Position = uProjection * positionV;