Fix the 3D model rendering issue after the KHR_texture_transform extension support
[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 #define MORPH defined(MORPH_POSITION) || defined(MORPH_NORMAL) || defined(MORPH_TANGENT)
8
9 // These lines in the shader may be replaced with actual definitions by the model loader,
10 // if they are needed. Note, some shader compilers have problems with spurious ";", so
11 // the macro invocations don't have a trailing ";". The replacement strings in the model
12 // loader will provide it instead.
13 #define ADD_EXTRA_SKINNING_ATTRIBUTES
14 #define ADD_EXTRA_WEIGHTS
15
16 #ifdef HIGHP
17   precision highp float;
18 #else
19   precision mediump float;
20 #endif
21
22 in vec3 aPosition;
23 in vec2 aTexCoord;
24 in vec3 aNormal;
25
26 #ifdef VEC4_TANGENT
27 in vec4 aTangent;
28 #else
29 in vec3 aTangent;
30 #endif
31
32 in vec4 aVertexColor;
33
34 #ifdef SKINNING
35 in vec4 aJoints0;
36 in vec4 aWeights0;
37 ADD_EXTRA_SKINNING_ATTRIBUTES
38 #endif
39
40 #ifdef MORPH
41   uniform highp sampler2D sBlendShapeGeometry;
42 #endif
43
44 out mediump vec2 vUV;
45 out lowp mat3 vTBN;
46 out lowp vec4 vColor;
47 out highp vec3 vPositionToCamera;
48
49 uniform highp mat4 uViewMatrix;
50 uniform mat3 uNormalMatrix;
51 uniform mat4 uModelMatrix;
52 uniform mat4 uProjection;
53
54 #ifdef SKINNING
55
56 #define MAX_BONES 256
57 layout(std140) uniform Bones
58 {
59   mat4 uBone[MAX_BONES];
60 };
61
62 uniform mediump vec3 uYDirection;
63 #endif
64
65 #ifdef MORPH
66 #define MAX_BLEND_SHAPE_NUMBER 256
67 uniform int uNumberOfBlendShapes;                                         ///< Total number of blend shapes loaded.
68 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
69 #ifdef MORPH_VERSION_2_0
70 uniform highp float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
71 #else
72 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
73 #endif
74 uniform highp int uBlendShapeComponentSize;                               ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
75 #endif
76
77 // Shadow
78 uniform lowp int uIsShadowEnabled;
79 uniform highp mat4 uShadowLightViewProjectionMatrix;
80 out highp vec3 positionFromLightView;
81
82 void main()
83 {
84   highp vec4 position = vec4(aPosition, 1.0);
85   highp vec3 normal = aNormal;
86   highp vec3 tangent = aTangent.xyz;
87
88 #ifdef MORPH
89   int width = textureSize( sBlendShapeGeometry, 0 ).x;
90
91   highp int blendShapeBufferOffset = 0;
92
93   for( int index = 0; index < uNumberOfBlendShapes; ++index )
94   {
95     highp vec3 diff = vec3(0.0);
96     highp int vertexId = 0;
97     highp int x = 0;
98     highp int y = 0;
99     highp float weight = clamp(uBlendShapeWeight[index], 0.0, 1.0);
100
101 #ifdef MORPH_POSITION
102     // Calculate the index to retrieve the geometry from the texture.
103     vertexId = gl_VertexID + blendShapeBufferOffset;
104     x = vertexId % width;
105     y = vertexId / width;
106
107     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
108     if(0.0 != weight)
109     {
110 #ifdef MORPH_VERSION_2_0
111        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
112 #else
113        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
114 #endif
115
116       diff = weight * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
117     }
118
119     position.xyz += diff;
120
121     blendShapeBufferOffset += uBlendShapeComponentSize;
122 #endif
123
124 #ifdef MORPH_NORMAL
125     // Calculate the index to retrieve the normal from the texture.
126     vertexId = gl_VertexID + blendShapeBufferOffset;
127     x = vertexId % width;
128     y = vertexId / width;
129
130     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
131     if(0.0 != weight)
132     {
133       diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
134     }
135
136     normal += diff.xyz;
137
138     blendShapeBufferOffset += uBlendShapeComponentSize;
139 #endif
140
141 #ifdef MORPH_TANGENT
142     // Calculate the index to retrieve the tangent from the texture.
143     vertexId = gl_VertexID + blendShapeBufferOffset;
144     x = vertexId % width;
145     y = vertexId / width;
146
147     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
148     if(0.0 != weight)
149     {
150       diff = weight * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
151     }
152
153     tangent += diff.xyz;
154
155     blendShapeBufferOffset += uBlendShapeComponentSize;
156 #endif
157   }
158
159 #endif
160
161
162
163
164 #ifdef SKINNING
165   highp mat4 bone =
166     uBone[int(aJoints0.x)] * aWeights0.x +
167     uBone[int(aJoints0.y)] * aWeights0.y +
168     uBone[int(aJoints0.z)] * aWeights0.z +
169     uBone[int(aJoints0.w)] * aWeights0.w;
170
171   ADD_EXTRA_WEIGHTS
172
173   position = bone * position;
174   normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
175   tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
176
177   highp vec4 positionW = position;
178 #else
179   highp vec4 positionW = uModelMatrix * position;
180 #endif
181
182   highp vec4 positionV = uViewMatrix * positionW;
183
184   vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
185
186   normal = normalize(normal);
187   tangent = normalize(tangent);
188   vec3 bitangent = cross(normal, tangent);
189 #ifdef VEC4_TANGENT
190   bitangent *= aTangent.w;
191 #endif
192   vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
193
194 #ifdef FLIP_V
195   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
196 #else
197   vUV = aTexCoord;
198 #endif
199
200   vColor = aVertexColor;
201
202   positionFromLightView = vec3(1.0);
203   if(uIsShadowEnabled > 0)
204   {
205     highp vec4 positionInLightView = uShadowLightViewProjectionMatrix * positionW;
206     positionFromLightView = ((positionInLightView.xyz / positionInLightView.w) * 0.5) + vec3(0.5);
207   }
208
209   gl_Position = uProjection * positionV;
210 }