4 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
5 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
10 precision mediump float;
23 #ifdef COLOR_ATTRIBUTE
25 uniform lowp float uHasVertexColor;
29 uniform highp sampler2D sBlendShapeGeometry;
34 #ifdef COLOR_ATTRIBUTE
37 out highp vec3 vPositionToCamera;
39 uniform highp mat4 uViewMatrix;
40 uniform mat3 uNormalMatrix;
41 uniform mat4 uModelMatrix;
42 uniform mat4 uProjection;
48 uniform mat4 uBone[MAX_BONES];
52 #define MAX_BLEND_SHAPE_NUMBER 128
53 uniform float uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
54 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
55 #ifdef MORPH_VERSION_2_0
56 uniform float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
58 uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
60 uniform float uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
65 highp vec4 position = vec4(aPosition, 1.0);
66 highp vec3 normal = aNormal;
67 highp vec3 tangent = aTangent.xyz;
70 int width = textureSize( sBlendShapeGeometry, 0 ).x;
72 int blendShapeBufferOffset = 0;
73 int blendShapeComponentSize = int(uBlendShapeComponentSize);
74 int numberOfBlendShapes = int(uNumberOfBlendShapes);
76 for( int index = 0; index < numberOfBlendShapes; ++index )
78 highp vec3 diff = vec3(0.0);
81 // Calculate the index to retrieve the geometry from the texture.
82 int vertexId = gl_VertexID + blendShapeBufferOffset;
83 int x = vertexId % width;
84 int y = vertexId / width;
86 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
87 if( 0.0 != uBlendShapeWeight[index] )
89 #ifdef MORPH_VERSION_2_0
90 float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
92 float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
95 diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
100 blendShapeBufferOffset += blendShapeComponentSize;
104 // Calculate the index to retrieve the normal from the texture.
105 vertexId = gl_VertexID + int(blendShapeBufferOffset);
106 x = vertexId % width;
107 y = vertexId / width;
109 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
110 if( 0.0 != uBlendShapeWeight[index] )
112 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
117 blendShapeBufferOffset += blendShapeComponentSize;
121 // Calculate the index to retrieve the tangent from the texture.
122 vertexId = gl_VertexID + blendShapeBufferOffset;
123 x = vertexId % width;
124 y = vertexId / width;
126 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
127 if( 0.0 != uBlendShapeWeight[index] )
129 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
134 blendShapeBufferOffset += blendShapeComponentSize;
141 highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
142 uBone[int(aJoints.y)] * aWeights.y +
143 uBone[int(aJoints.z)] * aWeights.z +
144 uBone[int(aJoints.w)] * aWeights.w;
145 position = bone * position;
146 normal = (bone * vec4(normal, 0.0)).xyz;
147 tangent = (bone * vec4(tangent, 0.0)).xyz;
150 highp vec4 positionW = uModelMatrix * position;
151 highp vec4 positionV = uViewMatrix * positionW;
153 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
155 vec3 bitangent = cross(normal, tangent);
157 bitangent *= aTangent.w;
159 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
162 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
167 #ifdef COLOR_ATTRIBUTE
168 vColor = mix(vec4(1.0f), aVertexColor, uHasVertexColor);
171 gl_Position = uProjection * positionV;