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