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 int 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 int 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;
70 for( int index = 0; index < uNumberOfBlendShapes; ++index )
72 highp vec3 diff = vec3(0.0);
73 highp int vertexId = 0;
78 // Calculate the index to retrieve the geometry from the texture.
79 vertexId = gl_VertexID + blendShapeBufferOffset;
83 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
84 if( 0.0 != uBlendShapeWeight[index] )
86 #ifdef MORPH_VERSION_2_0
87 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
89 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
92 diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
97 blendShapeBufferOffset += uBlendShapeComponentSize;
101 // Calculate the index to retrieve the normal from the texture.
102 vertexId = gl_VertexID + blendShapeBufferOffset;
103 x = vertexId % width;
104 y = vertexId / width;
106 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
107 if( 0.0 != uBlendShapeWeight[index] )
109 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
114 blendShapeBufferOffset += uBlendShapeComponentSize;
118 // Calculate the index to retrieve the tangent from the texture.
119 vertexId = gl_VertexID + blendShapeBufferOffset;
120 x = vertexId % width;
121 y = vertexId / width;
123 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
124 if( 0.0 != uBlendShapeWeight[index] )
126 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
131 blendShapeBufferOffset += uBlendShapeComponentSize;
138 highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
139 uBone[int(aJoints.y)] * aWeights.y +
140 uBone[int(aJoints.z)] * aWeights.z +
141 uBone[int(aJoints.w)] * aWeights.w;
142 position = bone * position;
143 normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
144 tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
146 highp vec4 positionW = position;
148 highp vec4 positionW = uModelMatrix * position;
150 highp vec4 positionV = uViewMatrix * positionW;
152 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
154 normal = normalize(normal);
155 tangent = normalize(tangent);
156 vec3 bitangent = cross(normal, tangent);
158 bitangent *= aTangent.w;
160 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
163 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
168 vColor = aVertexColor;
170 gl_Position = uProjection * positionV;