Fix normal vector error when we use skin
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / graphics / shaders / default-physically-based-shader.vert
1 #version 300 es
2
3 // Original Code
4 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-vert.glsl
5 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
6
7 #ifdef HIGHP
8   precision highp float;
9 #else
10   precision mediump float;
11 #endif
12
13 in vec3 aPosition;
14 in vec2 aTexCoord;
15 in vec3 aNormal;
16
17 #ifdef VEC4_TANGENT
18 in vec4 aTangent;
19 #else
20 in vec3 aTangent;
21 #endif
22
23 in vec4 aVertexColor;
24
25 #ifdef MORPH
26   uniform highp sampler2D sBlendShapeGeometry;
27 #endif
28
29 out mediump vec2 vUV;
30 out lowp mat3 vTBN;
31 out lowp vec4 vColor;
32 out highp vec3 vPositionToCamera;
33
34 uniform highp mat4 uViewMatrix;
35 uniform mat3 uNormalMatrix;
36 uniform mat4 uModelMatrix;
37 uniform mat4 uProjection;
38
39 #ifdef SKINNING
40   in vec4 aJoints;
41   in vec4 aWeights;
42   #define MAX_BONES 64
43   uniform mat4 uBone[MAX_BONES];
44   uniform mediump vec3 uYDirection;
45 #endif
46
47 #ifdef MORPH
48 #define MAX_BLEND_SHAPE_NUMBER 128
49 uniform float uNumberOfBlendShapes;                                       ///< Total number of blend shapes loaded.
50 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
51 #ifdef MORPH_VERSION_2_0
52 uniform highp float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
53 #else
54 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
55 #endif
56 uniform highp float uBlendShapeComponentSize;                             ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
57 #endif
58
59 void main()
60 {
61   highp vec4 position = vec4(aPosition, 1.0);
62   highp vec3 normal = aNormal;
63   highp vec3 tangent = aTangent.xyz;
64
65 #ifdef MORPH
66   int width = textureSize( sBlendShapeGeometry, 0 ).x;
67
68   highp int blendShapeBufferOffset = 0;
69   highp int blendShapeComponentSize = int(uBlendShapeComponentSize);
70   int numberOfBlendShapes = int(uNumberOfBlendShapes);
71
72   for( int index = 0; index < numberOfBlendShapes; ++index )
73   {
74     highp vec3 diff = vec3(0.0);
75     highp int vertexId = 0;
76     highp int x = 0;
77     highp int y = 0;
78
79 #ifdef MORPH_POSITION
80     // Calculate the index to retrieve the geometry from the texture.
81     vertexId = gl_VertexID + blendShapeBufferOffset;
82     x = vertexId % width;
83     y = vertexId / width;
84
85     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
86     if( 0.0 != uBlendShapeWeight[index] )
87     {
88 #ifdef MORPH_VERSION_2_0
89        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
90 #else
91        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
92 #endif
93
94       diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
95     }
96
97     position.xyz += diff;
98
99     blendShapeBufferOffset += blendShapeComponentSize;
100 #endif
101
102 #ifdef MORPH_NORMAL
103     // Calculate the index to retrieve the normal from the texture.
104     vertexId = gl_VertexID + int(blendShapeBufferOffset);
105     x = vertexId % width;
106     y = vertexId / width;
107
108     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
109     if( 0.0 != uBlendShapeWeight[index] )
110     {
111       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
112     }
113
114     normal += diff.xyz;
115
116     blendShapeBufferOffset += blendShapeComponentSize;
117 #endif
118
119 #ifdef MORPH_TANGENT
120     // Calculate the index to retrieve the tangent from the texture.
121     vertexId = gl_VertexID + blendShapeBufferOffset;
122     x = vertexId % width;
123     y = vertexId / width;
124
125     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
126     if( 0.0 != uBlendShapeWeight[index] )
127     {
128       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
129     }
130
131     tangent += diff.xyz;
132
133     blendShapeBufferOffset += blendShapeComponentSize;
134 #endif
135   }
136
137 #endif
138
139 #ifdef SKINNING
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;
147
148   highp vec4 positionW = position;
149 #else
150   highp vec4 positionW = uModelMatrix * position;
151 #endif
152   highp vec4 positionV = uViewMatrix * positionW;
153
154   vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
155
156   normal = normalize(normal);
157   tangent = normalize(tangent);
158   vec3 bitangent = cross(normal, tangent);
159 #ifdef VEC4_TANGENT
160   bitangent *= aTangent.w;
161 #endif
162   vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
163
164 #ifdef FLIP_V
165   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
166 #else
167   vUV = aTexCoord;
168 #endif
169
170   vColor = aVertexColor;
171
172   gl_Position = uProjection * positionV;
173 }