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];
47 #define MAX_BLEND_SHAPE_NUMBER 128
48 uniform float uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
49 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
50 #ifdef MORPH_VERSION_2_0
51 uniform float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
53 uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
55 uniform float uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
60 highp vec4 position = vec4(aPosition, 1.0);
61 highp vec3 normal = aNormal;
62 highp vec3 tangent = aTangent.xyz;
65 int width = textureSize( sBlendShapeGeometry, 0 ).x;
67 int blendShapeBufferOffset = 0;
68 int blendShapeComponentSize = int(uBlendShapeComponentSize);
69 int numberOfBlendShapes = int(uNumberOfBlendShapes);
71 for( int index = 0; index < numberOfBlendShapes; ++index )
73 highp vec3 diff = vec3(0.0);
76 // Calculate the index to retrieve the geometry from the texture.
77 int vertexId = gl_VertexID + blendShapeBufferOffset;
78 int x = vertexId % width;
79 int y = vertexId / width;
81 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
82 if( 0.0 != uBlendShapeWeight[index] )
84 #ifdef MORPH_VERSION_2_0
85 float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
87 float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
90 diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
95 blendShapeBufferOffset += blendShapeComponentSize;
99 // Calculate the index to retrieve the normal from the texture.
100 vertexId = gl_VertexID + int(blendShapeBufferOffset);
101 x = vertexId % width;
102 y = vertexId / width;
104 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
105 if( 0.0 != uBlendShapeWeight[index] )
107 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
112 blendShapeBufferOffset += blendShapeComponentSize;
116 // Calculate the index to retrieve the tangent from the texture.
117 vertexId = gl_VertexID + blendShapeBufferOffset;
118 x = vertexId % width;
119 y = vertexId / width;
121 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
122 if( 0.0 != uBlendShapeWeight[index] )
124 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
129 blendShapeBufferOffset += blendShapeComponentSize;
136 highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
137 uBone[int(aJoints.y)] * aWeights.y +
138 uBone[int(aJoints.z)] * aWeights.z +
139 uBone[int(aJoints.w)] * aWeights.w;
140 position = bone * position;
141 normal = (bone * vec4(normal, 0.0)).xyz;
142 tangent = (bone * vec4(tangent, 0.0)).xyz;
143 normal = normalize(normal);
144 tangent = normalize(tangent);
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 vec3 bitangent = cross(normal, tangent);
156 bitangent *= aTangent.w;
158 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
161 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
166 vColor = aVertexColor;
168 gl_Position = uProjection * positionV;