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