// https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
// Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
+#define MORPH defined(MORPH_POSITION) || defined(MORPH_NORMAL) || defined(MORPH_TANGENT)
+
#ifdef HIGHP
precision highp float;
#else
#ifdef SKINNING
in vec4 aJoints;
in vec4 aWeights;
- #define MAX_BONES 64
+ #define MAX_BONES 80
uniform mat4 uBone[MAX_BONES];
+ uniform mediump vec3 uYDirection;
#endif
#ifdef MORPH
#define MAX_BLEND_SHAPE_NUMBER 128
-uniform float uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
+uniform int uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
#ifdef MORPH_VERSION_2_0
-uniform float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
+uniform highp float uBlendShapeUnnormalizeFactor; ///< Factor used to unnormalize the geometry of the blend shape.
#else
-uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
+uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
#endif
-uniform float uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
+uniform highp int uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
#endif
+// Shadow
+uniform lowp int uIsShadowEnabled;
+uniform highp mat4 uShadowLightViewProjectionMatrix;
+out highp vec3 positionFromLightView;
+
void main()
{
highp vec4 position = vec4(aPosition, 1.0);
#ifdef MORPH
int width = textureSize( sBlendShapeGeometry, 0 ).x;
- int blendShapeBufferOffset = 0;
- int blendShapeComponentSize = int(uBlendShapeComponentSize);
- int numberOfBlendShapes = int(uNumberOfBlendShapes);
+ highp int blendShapeBufferOffset = 0;
- for( int index = 0; index < numberOfBlendShapes; ++index )
+ for( int index = 0; index < uNumberOfBlendShapes; ++index )
{
highp vec3 diff = vec3(0.0);
+ highp int vertexId = 0;
+ highp int x = 0;
+ highp int y = 0;
#ifdef MORPH_POSITION
// Calculate the index to retrieve the geometry from the texture.
- int vertexId = gl_VertexID + blendShapeBufferOffset;
- int x = vertexId % width;
- int y = vertexId / width;
+ vertexId = gl_VertexID + blendShapeBufferOffset;
+ x = vertexId % width;
+ y = vertexId / width;
// Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
if( 0.0 != uBlendShapeWeight[index] )
{
#ifdef MORPH_VERSION_2_0
- float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
+ highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
#else
- float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
+ highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
#endif
diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
position.xyz += diff;
- blendShapeBufferOffset += blendShapeComponentSize;
+ blendShapeBufferOffset += uBlendShapeComponentSize;
#endif
#ifdef MORPH_NORMAL
// Calculate the index to retrieve the normal from the texture.
- vertexId = gl_VertexID + int(blendShapeBufferOffset);
+ vertexId = gl_VertexID + blendShapeBufferOffset;
x = vertexId % width;
y = vertexId / width;
normal += diff.xyz;
- blendShapeBufferOffset += blendShapeComponentSize;
+ blendShapeBufferOffset += uBlendShapeComponentSize;
#endif
#ifdef MORPH_TANGENT
tangent += diff.xyz;
- blendShapeBufferOffset += blendShapeComponentSize;
+ blendShapeBufferOffset += uBlendShapeComponentSize;
#endif
}
uBone[int(aJoints.z)] * aWeights.z +
uBone[int(aJoints.w)] * aWeights.w;
position = bone * position;
- normal = (bone * vec4(normal, 0.0)).xyz;
- tangent = (bone * vec4(tangent, 0.0)).xyz;
- normal = normalize(normal);
- tangent = normalize(tangent);
+ normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
+ tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
highp vec4 positionW = position;
#else
vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
+ normal = normalize(normal);
+ tangent = normalize(tangent);
vec3 bitangent = cross(normal, tangent);
#ifdef VEC4_TANGENT
bitangent *= aTangent.w;
vColor = aVertexColor;
+ positionFromLightView = vec3(1.0);
+ if(uIsShadowEnabled > 0)
+ {
+ highp vec4 positionInLightView = uShadowLightViewProjectionMatrix * positionW;
+ positionFromLightView = ((positionInLightView.xyz / positionInLightView.w) * 0.5) + vec3(0.5);
+ }
+
gl_Position = uProjection * positionV;
}