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.
62 uniform lowp int uIsShadowEnabled;
63 uniform highp mat4 uShadowLightViewProjectionMatrix;
64 out highp vec3 positionFromLightView;
68 highp vec4 position = vec4(aPosition, 1.0);
69 highp vec3 normal = aNormal;
70 highp vec3 tangent = aTangent.xyz;
73 int width = textureSize( sBlendShapeGeometry, 0 ).x;
75 highp int blendShapeBufferOffset = 0;
77 for( int index = 0; index < uNumberOfBlendShapes; ++index )
79 highp vec3 diff = vec3(0.0);
80 highp int vertexId = 0;
83 highp float weight = clamp(uBlendShapeWeight[index], 0.0, 1.0);
86 // Calculate the index to retrieve the geometry from the texture.
87 vertexId = gl_VertexID + blendShapeBufferOffset;
91 // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
94 #ifdef MORPH_VERSION_2_0
95 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
97 highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
100 diff = weight * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
103 position.xyz += diff;
105 blendShapeBufferOffset += uBlendShapeComponentSize;
109 // Calculate the index to retrieve the normal from the texture.
110 vertexId = gl_VertexID + blendShapeBufferOffset;
111 x = vertexId % width;
112 y = vertexId / width;
114 // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
117 diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
122 blendShapeBufferOffset += uBlendShapeComponentSize;
126 // Calculate the index to retrieve the tangent from the texture.
127 vertexId = gl_VertexID + blendShapeBufferOffset;
128 x = vertexId % width;
129 y = vertexId / width;
131 // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
134 diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
139 blendShapeBufferOffset += uBlendShapeComponentSize;
146 highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
147 uBone[int(aJoints.y)] * aWeights.y +
148 uBone[int(aJoints.z)] * aWeights.z +
149 uBone[int(aJoints.w)] * aWeights.w;
150 position = bone * position;
151 normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
152 tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
154 highp vec4 positionW = position;
156 highp vec4 positionW = uModelMatrix * position;
158 highp vec4 positionV = uViewMatrix * positionW;
160 vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
162 normal = normalize(normal);
163 tangent = normalize(tangent);
164 vec3 bitangent = cross(normal, tangent);
166 bitangent *= aTangent.w;
168 vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
171 vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
176 vColor = aVertexColor;
178 positionFromLightView = vec3(1.0);
179 if(uIsShadowEnabled > 0)
181 highp vec4 positionInLightView = uShadowLightViewProjectionMatrix * positionW;
182 positionFromLightView = ((positionInLightView.xyz / positionInLightView.w) * 0.5) + vec3(0.5);
185 gl_Position = uProjection * positionV;