[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / graphics / shaders / shadow-map-shader.vert
1
2 #define ADD_EXTRA_SKINNING_ATTRIBUTES
3 #define ADD_EXTRA_WEIGHTS
4
5 precision highp float;
6
7 INPUT vec3 aPosition;
8 INPUT vec2 aTexCoord;
9 INPUT vec4 aVertexColor;
10
11 #ifdef SL_VERSION_LOW
12 INPUT float aVertexID;
13 #endif
14
15 #ifdef SKINNING
16 INPUT vec4 aJoints0;
17 INPUT vec4 aWeights0;
18 ADD_EXTRA_SKINNING_ATTRIBUTES;
19 #endif
20
21 #ifdef MORPH
22 uniform highp sampler2D sBlendShapeGeometry;
23 #ifdef SL_VERSION_LOW
24 uniform int uBlendShapeGeometryWidth;
25 uniform int uBlendShapeGeometryHeight;
26 #endif
27 #endif
28
29 OUTPUT mediump vec2 vUV;
30 OUTPUT lowp vec4 vColor;
31
32 uniform highp mat4 uViewMatrix;
33 uniform highp mat4 uModelMatrix;
34 uniform highp mat4 uProjection;
35
36 #ifdef SKINNING
37
38 #ifdef SL_VERSION_LOW
39 #define MAX_BONES 80
40 uniform mat4 uBone[MAX_BONES];
41 #else
42 #define MAX_BONES 256
43 layout(std140) uniform Bones
44 {
45   mat4 uBone[MAX_BONES];
46 };
47 #endif
48
49 uniform mediump vec3 uYDirection;
50 #endif
51
52 #ifdef MORPH
53 #define MAX_BLEND_SHAPE_NUMBER 256
54 uniform int uNumberOfBlendShapes;                                         ///< Total number of blend shapes loaded.
55 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
56 #ifdef MORPH_VERSION_2_0
57 uniform highp float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
58 #else
59 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
60 #endif
61 uniform highp int uBlendShapeComponentSize;                               ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
62 #endif
63
64 uniform highp mat4 uShadowLightViewProjectionMatrix;
65
66 void main()
67 {
68   highp vec4 position = vec4(aPosition, 1.0);
69
70 #ifdef MORPH
71
72 #ifdef SL_VERSION_LOW
73   int width = uBlendShapeGeometryWidth;
74 #else
75   int width = textureSize( sBlendShapeGeometry, 0 ).x;
76 #endif
77
78   highp int blendShapeBufferOffset = 0;
79
80 #ifdef SL_VERSION_LOW
81   highp float blendShapeWidth = float(uBlendShapeGeometryWidth);
82   highp float blendShapeHeight = float(uBlendShapeGeometryHeight);
83   highp float invertBlendShapeWidth = 1.0 / blendShapeWidth;
84   highp float invertBlendShapeHeight = 1.0 / blendShapeHeight;
85 #endif
86
87   for( int index = 0; index < uNumberOfBlendShapes; ++index )
88   {
89     highp vec3 diff = vec3(0.0);
90     highp int vertexId = 0;
91     highp int x = 0;
92     highp int y = 0;
93     highp float weight = clamp(uBlendShapeWeight[index], 0.0, 1.0);
94
95 #ifdef MORPH_POSITION
96     // Calculate the index to retrieve the geometry from the texture.
97 #ifdef SL_VERSION_LOW
98     vertexId = int(floor(aVertexID + 0.5)) + blendShapeBufferOffset;
99     y = vertexId / width;
100     x = vertexId - y * width;
101 #else
102     vertexId = gl_VertexID + blendShapeBufferOffset;
103     x = vertexId % width;
104     y = vertexId / width;
105 #endif
106
107     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
108     if( 0.0 != uBlendShapeWeight[index] )
109     {
110 #ifdef MORPH_VERSION_2_0
111        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
112 #else
113        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
114 #endif
115
116 #ifdef SL_VERSION_LOW
117       highp float floatX = float(x) + 0.5;
118       highp float floatY = float(y) + 0.5;
119       diff = weight * unnormalizeFactor * ( texture2D( sBlendShapeGeometry, vec2(floatX * invertBlendShapeWidth, floatY * invertBlendShapeHeight) ).xyz - 0.5 );
120 #else
121       diff = weight * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
122 #endif
123     }
124
125     position.xyz += diff;
126
127     blendShapeBufferOffset += uBlendShapeComponentSize;
128 #endif
129
130 #ifdef MORPH_NORMAL
131     blendShapeBufferOffset += uBlendShapeComponentSize;
132 #endif
133
134 #ifdef MORPH_TANGENT
135     blendShapeBufferOffset += uBlendShapeComponentSize;
136 #endif
137   }
138
139 #endif
140
141 #ifdef SKINNING
142   highp mat4 bone = uBone[int(aJoints0.x)] * aWeights0.x +
143     uBone[int(aJoints0.y)] * aWeights0.y +
144     uBone[int(aJoints0.z)] * aWeights0.z +
145     uBone[int(aJoints0.w)] * aWeights0.w;
146
147   ADD_EXTRA_WEIGHTS;
148
149   position = bone * position;
150   highp vec4 positionW = position;
151 #else
152   highp vec4 positionW = uModelMatrix * position;
153 #endif
154
155   // To synchronize View-Projection matrix with pbr shader
156   gl_Position = uShadowLightViewProjectionMatrix * positionW;
157
158 #ifdef FLIP_V
159   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
160 #else
161   vUV = aTexCoord;
162 #endif
163
164   vColor = aVertexColor;
165 }