Merge changes I776588c1,I7292a2fb into devel/master
[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 #ifdef HIGHP
10   precision highp float;
11 #else
12   precision mediump float;
13 #endif
14
15 in vec3 aPosition;
16 in vec2 aTexCoord;
17 in vec3 aNormal;
18
19 #ifdef VEC4_TANGENT
20 in vec4 aTangent;
21 #else
22 in vec3 aTangent;
23 #endif
24
25 in vec4 aVertexColor;
26
27 #ifdef MORPH
28   uniform highp sampler2D sBlendShapeGeometry;
29 #endif
30
31 out mediump vec2 vUV;
32 out lowp mat3 vTBN;
33 out lowp vec4 vColor;
34 out highp vec3 vPositionToCamera;
35
36 uniform highp mat4 uViewMatrix;
37 uniform mat3 uNormalMatrix;
38 uniform mat4 uModelMatrix;
39 uniform mat4 uProjection;
40
41 #ifdef SKINNING
42   in vec4 aJoints;
43   in vec4 aWeights;
44   #define MAX_BONES 80
45   uniform mat4 uBone[MAX_BONES];
46   uniform mediump vec3 uYDirection;
47 #endif
48
49 #ifdef MORPH
50 #define MAX_BLEND_SHAPE_NUMBER 128
51 uniform int uNumberOfBlendShapes;                                         ///< Total number of blend shapes loaded.
52 uniform highp float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
53 #ifdef MORPH_VERSION_2_0
54 uniform highp float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
55 #else
56 uniform highp float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
57 #endif
58 uniform highp int uBlendShapeComponentSize;                               ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
59 #endif
60
61 // Shadow
62 uniform lowp int uIsShadowEnabled;
63 uniform highp mat4 uShadowLightViewProjectionMatrix;
64 out highp vec3 positionFromLightView;
65
66 void main()
67 {
68   highp vec4 position = vec4(aPosition, 1.0);
69   highp vec3 normal = aNormal;
70   highp vec3 tangent = aTangent.xyz;
71
72 #ifdef MORPH
73   int width = textureSize( sBlendShapeGeometry, 0 ).x;
74
75   highp int blendShapeBufferOffset = 0;
76
77   for( int index = 0; index < uNumberOfBlendShapes; ++index )
78   {
79     highp vec3 diff = vec3(0.0);
80     highp int vertexId = 0;
81     highp int x = 0;
82     highp int y = 0;
83
84 #ifdef MORPH_POSITION
85     // Calculate the index to retrieve the geometry from the texture.
86     vertexId = gl_VertexID + blendShapeBufferOffset;
87     x = vertexId % width;
88     y = vertexId / width;
89
90     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
91     if( 0.0 != uBlendShapeWeight[index] )
92     {
93 #ifdef MORPH_VERSION_2_0
94        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
95 #else
96        highp float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
97 #endif
98
99       diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
100     }
101
102     position.xyz += diff;
103
104     blendShapeBufferOffset += uBlendShapeComponentSize;
105 #endif
106
107 #ifdef MORPH_NORMAL
108     // Calculate the index to retrieve the normal from the texture.
109     vertexId = gl_VertexID + blendShapeBufferOffset;
110     x = vertexId % width;
111     y = vertexId / width;
112
113     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
114     if( 0.0 != uBlendShapeWeight[index] )
115     {
116       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
117     }
118
119     normal += diff.xyz;
120
121     blendShapeBufferOffset += uBlendShapeComponentSize;
122 #endif
123
124 #ifdef MORPH_TANGENT
125     // Calculate the index to retrieve the tangent from the texture.
126     vertexId = gl_VertexID + blendShapeBufferOffset;
127     x = vertexId % width;
128     y = vertexId / width;
129
130     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
131     if( 0.0 != uBlendShapeWeight[index] )
132     {
133       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
134     }
135
136     tangent += diff.xyz;
137
138     blendShapeBufferOffset += uBlendShapeComponentSize;
139 #endif
140   }
141
142 #endif
143
144 #ifdef SKINNING
145   highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
146     uBone[int(aJoints.y)] * aWeights.y +
147     uBone[int(aJoints.z)] * aWeights.z +
148     uBone[int(aJoints.w)] * aWeights.w;
149   position = bone * position;
150   normal = uYDirection * (bone * vec4(normal, 0.0)).xyz;
151   tangent = uYDirection * (bone * vec4(tangent, 0.0)).xyz;
152
153   highp vec4 positionW = position;
154 #else
155   highp vec4 positionW = uModelMatrix * position;
156 #endif
157   highp vec4 positionV = uViewMatrix * positionW;
158
159   vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
160
161   normal = normalize(normal);
162   tangent = normalize(tangent);
163   vec3 bitangent = cross(normal, tangent);
164 #ifdef VEC4_TANGENT
165   bitangent *= aTangent.w;
166 #endif
167   vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
168
169 #ifdef FLIP_V
170   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
171 #else
172   vUV = aTexCoord;
173 #endif
174
175   vColor = aVertexColor;
176
177   positionFromLightView = vec3(1.0);
178   if(uIsShadowEnabled > 0)
179   {
180     highp vec4 positionInLightView = uShadowLightViewProjectionMatrix * positionW;
181     positionFromLightView = ((positionInLightView.xyz / positionInLightView.w) * 0.5) + vec3(0.5);
182   }
183
184   gl_Position = uProjection * positionV;
185 }