4 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
5 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
7 #define MORPH defined(MORPH_POSITION) || defined(MORPH_NORMAL) || defined(MORPH_TANGENT)
10 precision highp float;
12 precision mediump float;
28 uniform highp sampler2D sBlendShapeGeometry;
34 out highp vec3 vPositionToCamera;
36 uniform highp mat4 uViewMatrix;
37 uniform mat3 uNormalMatrix;
38 uniform mat4 uModelMatrix;
39 uniform mat4 uProjection;
45 uniform mat4 uBone[MAX_BONES];
46 uniform mediump vec3 uYDirection;
50 #define MAX_BLEND_SHAPE_NUMBER 128
51 uniform int uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
52 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
53 #ifdef MORPH_VERSION_2_0
54 uniform highp float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
56 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
58 uniform highp int uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
63 highp vec4 position = vec4(aPosition, 1.0);
64 highp vec3 normal = aNormal;
65 highp vec3 tangent = aTangent.xyz;
68 int width = textureSize( sBlendShapeGeometry, 0 ).x;
70 highp int blendShapeBufferOffset = 0;
72 for( int index = 0; index < uNumberOfBlendShapes; ++index )
74 highp vec3 diff = vec3(0.0);
75 highp int vertexId = 0;
80 // Calculate the index to retrieve the geometry from the texture.
81 vertexId = gl_VertexID + blendShapeBufferOffset;
85 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
86 if( 0.0 != uBlendShapeWeight[index] )
88 #ifdef MORPH_VERSION_2_0
89 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
91 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
94 diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
99 blendShapeBufferOffset += uBlendShapeComponentSize;
103 // Calculate the index to retrieve the normal from the texture.
104 vertexId = gl_VertexID + blendShapeBufferOffset;
105 x = vertexId % width;
106 y = vertexId / width;
108 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
109 if( 0.0 != uBlendShapeWeight[index] )
111 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
116 blendShapeBufferOffset += uBlendShapeComponentSize;
120 // Calculate the index to retrieve the tangent from the texture.
121 vertexId = gl_VertexID + blendShapeBufferOffset;
122 x = vertexId % width;
123 y = vertexId / width;
125 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
126 if( 0.0 != uBlendShapeWeight[index] )
128 diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
133 blendShapeBufferOffset += uBlendShapeComponentSize;
140 highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
141 uBone[int(aJoints.y)] * aWeights.y +
142 uBone[int(aJoints.z)] * aWeights.z +
143 uBone[int(aJoints.w)] * aWeights.w;
144 position = bone * position;
145 normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
146 tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
148 highp vec4 positionW = position;
150 highp vec4 positionW = uModelMatrix * position;
152 highp vec4 positionV = uViewMatrix * positionW;
154 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
156 normal = normalize(normal);
157 tangent = normalize(tangent);
158 vec3 bitangent = cross(normal, tangent);
160 bitangent *= aTangent.w;
162 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
165 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
170 vColor = aVertexColor;
172 gl_Position = uProjection * positionV;