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