#version 300 es
+// Original Code
+// https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
+// Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
+
#ifdef HIGHP
precision highp float;
#else
in vec3 aPosition;
in vec2 aTexCoord;
in vec3 aNormal;
+
+#ifdef VEC4_TANGENT
+in vec4 aTangent;
+#else
in vec3 aTangent;
+#endif
+
+in vec4 aVertexColor;
#ifdef MORPH
- uniform sampler2D sBlendShapeGeometry;
+ uniform highp sampler2D sBlendShapeGeometry;
#endif
out vec2 vUV;
-out vec3 vNormal;
-out vec3 vTangent;
-out vec3 vViewVec;
+out lowp mat3 vTBN;
+out lowp vec4 vColor;
+out highp vec3 vPositionToCamera;
-uniform highp mat4 uMvpMatrix;
uniform highp mat4 uViewMatrix;
uniform mat3 uNormalMatrix;
uniform mat4 uModelMatrix;
-uniform mat4 uModelView;
uniform mat4 uProjection;
+uniform lowp float uHasVertexColor;
#ifdef SKINNING
in vec4 aJoints;
#ifdef MORPH
#define MAX_BLEND_SHAPE_NUMBER 128
-uniform int uNumberOfBlendShapes; ///< Total number of blend shapes loaded.
-uniform float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER]; ///< The weight of each blend shape.
+uniform float 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.
#else
uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
#endif
-uniform int uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
+uniform float uBlendShapeComponentSize; ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
#endif
void main()
{
- vec4 position = vec4(aPosition, 1.0);
- vec3 normal = aNormal;
- vec3 tangent = aTangent;
+ highp vec4 position = vec4(aPosition, 1.0);
+ highp vec3 normal = aNormal;
+ highp vec3 tangent = aTangent.xyz;
#ifdef MORPH
int width = textureSize( sBlendShapeGeometry, 0 ).x;
int blendShapeBufferOffset = 0;
- for( int index = 0; index < uNumberOfBlendShapes; ++index )
+ int blendShapeComponentSize = int(uBlendShapeComponentSize);
+ int numberOfBlendShapes = int(uNumberOfBlendShapes);
+
+ for( int index = 0; index < numberOfBlendShapes; ++index )
{
+ highp vec3 diff = vec3(0.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;
- vec3 diff = vec3(0.0);
// Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
if( 0.0 != uBlendShapeWeight[index] )
{
position.xyz += diff;
- blendShapeBufferOffset += uBlendShapeComponentSize;
+ blendShapeBufferOffset += blendShapeComponentSize;
#endif
#ifdef MORPH_NORMAL
// Calculate the index to retrieve the normal from the texture.
- vertexId = gl_VertexID + blendShapeBufferOffset;
+ vertexId = gl_VertexID + int(blendShapeBufferOffset);
x = vertexId % width;
y = vertexId / width;
normal += diff.xyz;
- blendShapeBufferOffset += uBlendShapeComponentSize;
+ blendShapeBufferOffset += blendShapeComponentSize;
#endif
#ifdef MORPH_TANGENT
tangent += diff.xyz;
- blendShapeBufferOffset += uBlendShapeComponentSize;
+ blendShapeBufferOffset += blendShapeComponentSize;
#endif
}
#endif
#ifdef SKINNING
- mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
+ highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
uBone[int(aJoints.y)] * aWeights.y +
uBone[int(aJoints.z)] * aWeights.z +
uBone[int(aJoints.w)] * aWeights.w;
tangent = (bone * vec4(tangent, 0.0)).xyz;
#endif
- vec4 vPosition = uModelMatrix * position;
-
- vNormal = normalize(uNormalMatrix * normal);
+ highp vec4 positionW = uModelMatrix * position;
+ highp vec4 positionV = uViewMatrix * positionW;
- vTangent = normalize(uNormalMatrix * tangent);
+ vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
-
- vec4 viewPosition = uViewMatrix * vPosition;
- gl_Position = uProjection * viewPosition;
+ vec3 bitangent = cross(normal, tangent);
+#ifdef VEC4_TANGENT
+ bitangent *= aTangent.w;
+#endif
+ vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
#ifdef FLIP_V
vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
vUV = aTexCoord;
#endif
- vViewVec = viewPosition.xyz;
+ vColor = mix(vec4(1.0f), aVertexColor, uHasVertexColor);
+
+ gl_Position = uProjection * positionV;
}