[Tizen] Fix skinning issue
[platform/core/uifw/dali-demo.git] / resources / shaders / dli_pbr.vsh
1 #version 300 es
2
3 #ifdef HIGHP
4   precision highp float;
5 #else
6   precision mediump float;
7 #endif
8
9 in vec3 aPosition;
10 in vec2 aTexCoord;
11 in vec3 aNormal;
12 in vec3 aTangent;
13
14 #ifdef MORPH
15   uniform sampler2D sBlendShapeGeometry;
16 #endif
17
18 out vec2 vUV;
19 out vec3 vNormal;
20 out vec3 vTangent;
21 out vec3 vViewVec;
22
23
24 uniform highp mat4 uMvpMatrix;
25 uniform highp mat4 uViewMatrix;
26 uniform mat3 uNormalMatrix;
27 uniform mat4 uModelMatrix;
28 uniform mat4 uModelView;
29 uniform mat4 uProjection;
30
31 #ifdef SKINNING
32   in vec4 aJoints;
33   in vec4 aWeights;
34   #define MAX_BONES 64
35   uniform mat4 uBone[MAX_BONES];
36 #endif
37
38 #ifdef MORPH
39 #define MAX_BLEND_SHAPE_NUMBER 128
40 uniform int uNumberOfBlendShapes;                                   ///< Total number of blend shapes loaded.
41 uniform float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
42 #ifdef MORPH_VERSION_2_0
43 uniform float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
44 #else
45 uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
46 #endif
47 uniform int uBlendShapeComponentSize;                               ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
48 #endif
49
50 void main()
51 {
52   vec4 position = vec4(aPosition, 1.0);
53   vec3 normal = aNormal;
54   vec3 tangent = aTangent;
55
56 #ifdef MORPH
57   int width = textureSize( sBlendShapeGeometry, 0 ).x;
58
59   int blendShapeBufferOffset = 0;
60   for( int index = 0; index < uNumberOfBlendShapes; ++index )
61   {
62 #ifdef MORPH_POSITION
63     // Calculate the index to retrieve the geometry from the texture.
64     int vertexId = gl_VertexID + blendShapeBufferOffset;
65     int x = vertexId % width;
66     int y = vertexId / width;
67
68     vec3 diff = vec3(0.0);
69     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
70     if( 0.0 != uBlendShapeWeight[index] )
71     {
72 #ifdef MORPH_VERSION_2_0
73        float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
74 #else
75        float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
76 #endif
77
78       diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
79     }
80
81     position.xyz += diff;
82
83     blendShapeBufferOffset += uBlendShapeComponentSize;
84 #endif
85
86 #ifdef MORPH_NORMAL
87     // Calculate the index to retrieve the normal from the texture.
88     vertexId = gl_VertexID + blendShapeBufferOffset;
89     x = vertexId % width;
90     y = vertexId / width;
91
92     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
93     if( 0.0 != uBlendShapeWeight[index] )
94     {
95       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
96     }
97
98     normal += diff.xyz;
99
100     blendShapeBufferOffset += uBlendShapeComponentSize;
101 #endif
102
103 #ifdef MORPH_TANGENT
104     // Calculate the index to retrieve the tangent from the texture.
105     vertexId = gl_VertexID + blendShapeBufferOffset;
106     x = vertexId % width;
107     y = vertexId / width;
108
109     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
110     if( 0.0 != uBlendShapeWeight[index] )
111     {
112       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
113     }
114
115     tangent += diff.xyz;
116
117     blendShapeBufferOffset += uBlendShapeComponentSize;
118 #endif
119   }
120
121 #endif
122
123 #ifdef SKINNING
124   mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
125     uBone[int(aJoints.y)] * aWeights.y +
126     uBone[int(aJoints.z)] * aWeights.z +
127     uBone[int(aJoints.w)] * aWeights.w;
128   position = bone * position;
129   normal = (bone * vec4(normal, 0.0)).xyz;
130   tangent = (bone * vec4(tangent, 0.0)).xyz;
131
132   normal = normalize(normal);
133   tangent = normalize(tangent);
134   vec4 vPosition = position;
135 #else
136   vec4 vPosition = uModelMatrix * position;
137 #endif
138
139   vNormal = normalize(uNormalMatrix * normal);
140
141   vTangent = normalize(uNormalMatrix * tangent);
142
143
144   vec4 viewPosition = uViewMatrix * vPosition;
145   gl_Position = uProjection * viewPosition;
146
147 #ifdef FLIP_V
148   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
149 #else
150   vUV = aTexCoord;
151 #endif
152
153   vViewVec = viewPosition.xyz;
154 }