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