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;
38 uniform lowp float uHasVertexColor;
44 uniform mat4 uBone[MAX_BONES];
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 float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
54 uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
56 uniform 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 int blendShapeBufferOffset = 0;
69 int blendShapeComponentSize = int(uBlendShapeComponentSize);
70 int numberOfBlendShapes = int(uNumberOfBlendShapes);
72 for( int index = 0; index < numberOfBlendShapes; ++index )
74 highp vec3 diff = vec3(0.0);
77 // Calculate the index to retrieve the geometry from the texture.
78 int vertexId = gl_VertexID + blendShapeBufferOffset;
79 int x = vertexId % width;
80 int y = vertexId / width;
82 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
83 if( 0.0 != uBlendShapeWeight[index] )
85 #ifdef MORPH_VERSION_2_0
86 float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
88 float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
91 diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
96 blendShapeBufferOffset += blendShapeComponentSize;
100 // Calculate the index to retrieve the normal from the texture.
101 vertexId = gl_VertexID + int(blendShapeBufferOffset);
102 x = vertexId % width;
103 y = vertexId / width;
105 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
106 if( 0.0 != uBlendShapeWeight[index] )
108 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
113 blendShapeBufferOffset += blendShapeComponentSize;
117 // Calculate the index to retrieve the tangent from the texture.
118 vertexId = gl_VertexID + blendShapeBufferOffset;
119 x = vertexId % width;
120 y = vertexId / width;
122 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
123 if( 0.0 != uBlendShapeWeight[index] )
125 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
130 blendShapeBufferOffset += blendShapeComponentSize;
137 highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
138 uBone[int(aJoints.y)] * aWeights.y +
139 uBone[int(aJoints.z)] * aWeights.z +
140 uBone[int(aJoints.w)] * aWeights.w;
141 position = bone * position;
142 normal = (bone * vec4(normal, 0.0)).xyz;
143 tangent = (bone * vec4(tangent, 0.0)).xyz;
146 highp vec4 positionW = uModelMatrix * position;
147 highp vec4 positionV = uViewMatrix * positionW;
149 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
151 vec3 bitangent = cross(normal, tangent);
153 bitangent *= aTangent.w;
155 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
158 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
163 vColor = mix(vec4(1.0f), aVertexColor, uHasVertexColor);
165 gl_Position = uProjection * positionV;