Make shader version be top of the code
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / 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 sampler2D sBlendShapeGeometry;
27 #endif
28
29 out 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 uniform lowp float uHasVertexColor;
39
40 #ifdef SKINNING
41   in vec4 aJoints;
42   in vec4 aWeights;
43   #define MAX_BONES 64
44   uniform mat4 uBone[MAX_BONES];
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 float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
51 #ifdef MORPH_VERSION_2_0
52 uniform float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
53 #else
54 uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
55 #endif
56 uniform 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   vec4 position = vec4(aPosition, 1.0);
62   vec3 normal = aNormal;
63   vec3 tangent = aTangent.xyz;
64
65 #ifdef MORPH
66   int width = textureSize( sBlendShapeGeometry, 0 ).x;
67
68   int blendShapeBufferOffset = 0;
69   for( int index = 0; index < uNumberOfBlendShapes; ++index )
70   {
71 #ifdef MORPH_POSITION
72     // Calculate the index to retrieve the geometry from the texture.
73     int vertexId = gl_VertexID + blendShapeBufferOffset;
74     int x = vertexId % width;
75     int y = vertexId / width;
76
77     vec3 diff = vec3(0.0);
78     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
79     if( 0.0 != uBlendShapeWeight[index] )
80     {
81 #ifdef MORPH_VERSION_2_0
82        float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
83 #else
84        float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
85 #endif
86
87       diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
88     }
89
90     position.xyz += diff;
91
92     blendShapeBufferOffset += uBlendShapeComponentSize;
93 #endif
94
95 #ifdef MORPH_NORMAL
96     // Calculate the index to retrieve the normal from the texture.
97     vertexId = gl_VertexID + blendShapeBufferOffset;
98     x = vertexId % width;
99     y = vertexId / width;
100
101     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
102     if( 0.0 != uBlendShapeWeight[index] )
103     {
104       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
105     }
106
107     normal += diff.xyz;
108
109     blendShapeBufferOffset += uBlendShapeComponentSize;
110 #endif
111
112 #ifdef MORPH_TANGENT
113     // Calculate the index to retrieve the tangent from the texture.
114     vertexId = gl_VertexID + blendShapeBufferOffset;
115     x = vertexId % width;
116     y = vertexId / width;
117
118     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
119     if( 0.0 != uBlendShapeWeight[index] )
120     {
121       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
122     }
123
124     tangent += diff.xyz;
125
126     blendShapeBufferOffset += uBlendShapeComponentSize;
127 #endif
128   }
129
130 #endif
131
132 #ifdef SKINNING
133   mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
134     uBone[int(aJoints.y)] * aWeights.y +
135     uBone[int(aJoints.z)] * aWeights.z +
136     uBone[int(aJoints.w)] * aWeights.w;
137   position = bone * position;
138   normal = (bone * vec4(normal, 0.0)).xyz;
139   tangent = (bone * vec4(tangent, 0.0)).xyz;
140 #endif
141
142   vec4 positionW = uModelMatrix * position;
143   vec4 positionV = uViewMatrix * positionW;
144
145   vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
146
147   lowp vec3 bitangent = cross(normal, tangent);
148 #ifdef VEC4_TANGENT
149   bitangent *= aTangent.w;
150 #endif
151   vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
152
153 #ifdef FLIP_V
154   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
155 #else
156   vUV = aTexCoord;
157 #endif
158
159   vColor = mix(vec4(1.0f), aVertexColor, uHasVertexColor);
160
161   gl_Position = uProjection * positionV;
162 }