Compute min/max value if min/max is not defined.
[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 highp 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 float 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 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 float 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   int blendShapeBufferOffset = 0;
69   int blendShapeComponentSize = int(uBlendShapeComponentSize);
70   int numberOfBlendShapes = int(uNumberOfBlendShapes);
71
72   for( int index = 0; index < numberOfBlendShapes; ++index )
73   {
74     highp vec3 diff = vec3(0.0);
75
76 #ifdef MORPH_POSITION
77     // Calculate the index to retrieve the geometry from the texture.
78     int vertexId = gl_VertexID + blendShapeBufferOffset;
79     int x = vertexId % width;
80     int y = vertexId / width;
81
82     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
83     if( 0.0 != uBlendShapeWeight[index] )
84     {
85 #ifdef MORPH_VERSION_2_0
86        float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
87 #else
88        float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
89 #endif
90
91       diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
92     }
93
94     position.xyz += diff;
95
96     blendShapeBufferOffset += blendShapeComponentSize;
97 #endif
98
99 #ifdef MORPH_NORMAL
100     // Calculate the index to retrieve the normal from the texture.
101     vertexId = gl_VertexID + int(blendShapeBufferOffset);
102     x = vertexId % width;
103     y = vertexId / width;
104
105     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
106     if( 0.0 != uBlendShapeWeight[index] )
107     {
108       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
109     }
110
111     normal += diff.xyz;
112
113     blendShapeBufferOffset += blendShapeComponentSize;
114 #endif
115
116 #ifdef MORPH_TANGENT
117     // Calculate the index to retrieve the tangent from the texture.
118     vertexId = gl_VertexID + blendShapeBufferOffset;
119     x = vertexId % width;
120     y = vertexId / width;
121
122     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
123     if( 0.0 != uBlendShapeWeight[index] )
124     {
125       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
126     }
127
128     tangent += diff.xyz;
129
130     blendShapeBufferOffset += blendShapeComponentSize;
131 #endif
132   }
133
134 #endif
135
136 #ifdef SKINNING
137   highp mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
138     uBone[int(aJoints.y)] * aWeights.y +
139     uBone[int(aJoints.z)] * aWeights.z +
140     uBone[int(aJoints.w)] * aWeights.w;
141   position = bone * position;
142   normal = (bone * vec4(normal, 0.0)).xyz;
143   tangent = (bone * vec4(tangent, 0.0)).xyz;
144 #endif
145
146   highp vec4 positionW = uModelMatrix * position;
147   highp vec4 positionV = uViewMatrix * positionW;
148
149   vPositionToCamera = transpose(mat3(uViewMatrix)) * -vec3(positionV.xyz / positionV.w);
150
151   vec3 bitangent = cross(normal, tangent);
152 #ifdef VEC4_TANGENT
153   bitangent *= aTangent.w;
154 #endif
155   vTBN = mat3(uModelMatrix) * mat3(tangent, bitangent, normal);
156
157 #ifdef FLIP_V
158   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
159 #else
160   vUV = aTexCoord;
161 #endif
162
163   vColor = mix(vec4(1.0f), aVertexColor, uHasVertexColor);
164
165   gl_Position = uProjection * positionV;
166 }