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 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 int uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
50 uniform 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 int uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
61 vec4 position = vec4(aPosition, 1.0);
62 vec3 normal = aNormal;
63 vec3 tangent = aTangent.xyz;
66 int width = textureSize( sBlendShapeGeometry, 0 ).x;
68 int blendShapeBufferOffset = 0;
69 for( int index = 0; index < uNumberOfBlendShapes; ++index )
72 // Calculate the index to retrieve the geometry from the texture.
73 int vertexId = gl_VertexID + blendShapeBufferOffset;
74 int x = vertexId % width;
75 int y = vertexId / width;
77 vec3 diff = vec3(0.0);
78 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
79 if( 0.0 != uBlendShapeWeight[index] )
81 #ifdef MORPH_VERSION_2_0
82 float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
84 float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
87 diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
92 blendShapeBufferOffset += uBlendShapeComponentSize;
96 // Calculate the index to retrieve the normal from the texture.
97 vertexId = gl_VertexID + blendShapeBufferOffset;
101 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
102 if( 0.0 != uBlendShapeWeight[index] )
104 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
109 blendShapeBufferOffset += uBlendShapeComponentSize;
113 // Calculate the index to retrieve the tangent from the texture.
114 vertexId = gl_VertexID + blendShapeBufferOffset;
115 x = vertexId % width;
116 y = vertexId / width;
118 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
119 if( 0.0 != uBlendShapeWeight[index] )
121 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
126 blendShapeBufferOffset += uBlendShapeComponentSize;
133 mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
134 uBone[int(aJoints.y)] * aWeights.y +
135 uBone[int(aJoints.z)] * aWeights.z +
136 uBone[int(aJoints.w)] * aWeights.w;
137 position = bone * position;
138 normal = (bone * vec4(normal, 0.0)).xyz;
139 tangent = (bone * vec4(tangent, 0.0)).xyz;
142 vec4 positionW = uModelMatrix * position;
143 vec4 positionV = uViewMatrix * positionW;
145 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
147 lowp vec3 bitangent = cross(normal, tangent);
149 bitangent *= aTangent.w;
151 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
154 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
159 vColor = mix(vec4(1.0f), aVertexColor, uHasVertexColor);
161 gl_Position = uProjection * positionV;