Fix transform issue of skinned mesh
[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 #endif
45
46 #ifdef MORPH
47 #define MAX_BLEND_SHAPE_NUMBER 128
48 uniform float uNumberOfBlendShapes;                                 ///< Total number of blend shapes loaded.
49 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
50 #ifdef MORPH_VERSION_2_0
51 uniform float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
52 #else
53 uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
54 #endif
55 uniform float uBlendShapeComponentSize;                             ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
56 #endif
57
58 void main()
59 {
60   highp vec4 position = vec4(aPosition, 1.0);
61   highp vec3 normal = aNormal;
62   highp vec3 tangent = aTangent.xyz;
63
64 #ifdef MORPH
65   int width = textureSize( sBlendShapeGeometry, 0 ).x;
66
67   int blendShapeBufferOffset = 0;
68   int blendShapeComponentSize = int(uBlendShapeComponentSize);
69   int numberOfBlendShapes = int(uNumberOfBlendShapes);
70
71   for( int index = 0; index < numberOfBlendShapes; ++index )
72   {
73     highp vec3 diff = vec3(0.0);
74
75 #ifdef MORPH_POSITION
76     // Calculate the index to retrieve the geometry from the texture.
77     int vertexId = gl_VertexID + blendShapeBufferOffset;
78     int x = vertexId % width;
79     int y = vertexId / width;
80
81     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
82     if( 0.0 != uBlendShapeWeight[index] )
83     {
84 #ifdef MORPH_VERSION_2_0
85        float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
86 #else
87        float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
88 #endif
89
90       diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
91     }
92
93     position.xyz += diff;
94
95     blendShapeBufferOffset += blendShapeComponentSize;
96 #endif
97
98 #ifdef MORPH_NORMAL
99     // Calculate the index to retrieve the normal from the texture.
100     vertexId = gl_VertexID + int(blendShapeBufferOffset);
101     x = vertexId % width;
102     y = vertexId / width;
103
104     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
105     if( 0.0 != uBlendShapeWeight[index] )
106     {
107       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
108     }
109
110     normal += diff.xyz;
111
112     blendShapeBufferOffset += blendShapeComponentSize;
113 #endif
114
115 #ifdef MORPH_TANGENT
116     // Calculate the index to retrieve the tangent from the texture.
117     vertexId = gl_VertexID + blendShapeBufferOffset;
118     x = vertexId % width;
119     y = vertexId / width;
120
121     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
122     if( 0.0 != uBlendShapeWeight[index] )
123     {
124       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
125     }
126
127     tangent += diff.xyz;
128
129     blendShapeBufferOffset += blendShapeComponentSize;
130 #endif
131   }
132
133 #endif
134
135 #ifdef SKINNING
136   highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
137     uBone[int(aJoints.y)] * aWeights.y +
138     uBone[int(aJoints.z)] * aWeights.z +
139     uBone[int(aJoints.w)] * aWeights.w;
140   position = bone * position;
141   normal = (bone * vec4(normal, 0.0)).xyz;
142   tangent = (bone * vec4(tangent, 0.0)).xyz;
143   normal = normalize(normal);
144   tangent = normalize(tangent);
145
146   highp vec4 positionW = position;
147 #else
148   highp vec4 positionW = uModelMatrix * position;
149 #endif
150   highp vec4 positionV = uViewMatrix * positionW;
151
152   vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
153
154   vec3 bitangent = cross(normal, tangent);
155 #ifdef VEC4_TANGENT
156   bitangent *= aTangent.w;
157 #endif
158   vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
159
160 #ifdef FLIP_V
161   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
162 #else
163   vUV = aTexCoord;
164 #endif
165
166   vColor = aVertexColor;
167
168   gl_Position = uProjection * positionV;
169 }