Use default PBR shader to the scene-loader
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / internal / graphics / shaders / default-physically-based-shader.vert
1 #version 300 es
2
3 #ifdef HIGHP
4   precision highp float;
5 #else
6   precision mediump float;
7 #endif
8
9 in vec3 aPosition;
10 in vec2 aTexCoord;
11 in vec3 aNormal;
12 in vec3 aTangent;
13
14 #ifdef MORPH
15   uniform sampler2D sBlendShapeGeometry;
16 #endif
17
18 out vec2 vUV;
19 out vec3 vNormal;
20 out vec3 vTangent;
21 out vec3 vViewVec;
22
23 uniform highp mat4 uMvpMatrix;
24 uniform highp mat4 uViewMatrix;
25 uniform mat3 uNormalMatrix;
26 uniform mat4 uModelMatrix;
27 uniform mat4 uModelView;
28 uniform mat4 uProjection;
29
30 #ifdef SKINNING
31   in vec4 aJoints;
32   in vec4 aWeights;
33   #define MAX_BONES 64
34   uniform mat4 uBone[MAX_BONES];
35 #endif
36
37 #ifdef MORPH
38 #define MAX_BLEND_SHAPE_NUMBER 128
39 uniform int uNumberOfBlendShapes;                                   ///< Total number of blend shapes loaded.
40 uniform float uBlendShapeWeight[MAX_BLEND_SHAPE_NUMBER];            ///< The weight of each blend shape.
41 #ifdef MORPH_VERSION_2_0
42 uniform float uBlendShapeUnnormalizeFactor;                         ///< Factor used to unnormalize the geometry of the blend shape.
43 #else
44 uniform float uBlendShapeUnnormalizeFactor[MAX_BLEND_SHAPE_NUMBER]; ///< Factor used to unnormalize the geometry of the blend shape.
45 #endif
46 uniform int uBlendShapeComponentSize;                               ///< The size in the texture of either the vertices, normals or tangents. Used to calculate the offset to address them.
47 #endif
48
49 void main()
50 {
51   vec4 position = vec4(aPosition, 1.0);
52   vec3 normal = aNormal;
53   vec3 tangent = aTangent;
54
55 #ifdef MORPH
56   int width = textureSize( sBlendShapeGeometry, 0 ).x;
57
58   int blendShapeBufferOffset = 0;
59   for( int index = 0; index < uNumberOfBlendShapes; ++index )
60   {
61 #ifdef MORPH_POSITION
62     // Calculate the index to retrieve the geometry from the texture.
63     int vertexId = gl_VertexID + blendShapeBufferOffset;
64     int x = vertexId % width;
65     int y = vertexId / width;
66
67     vec3 diff = vec3(0.0);
68     // Retrieves the blend shape geometry from the texture, unnormalizes it and multiply by the weight.
69     if( 0.0 != uBlendShapeWeight[index] )
70     {
71 #ifdef MORPH_VERSION_2_0
72        float unnormalizeFactor = uBlendShapeUnnormalizeFactor;
73 #else
74        float unnormalizeFactor = uBlendShapeUnnormalizeFactor[index];
75 #endif
76
77       diff = uBlendShapeWeight[index] * unnormalizeFactor * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
78     }
79
80     position.xyz += diff;
81
82     blendShapeBufferOffset += uBlendShapeComponentSize;
83 #endif
84
85 #ifdef MORPH_NORMAL
86     // Calculate the index to retrieve the normal from the texture.
87     vertexId = gl_VertexID + blendShapeBufferOffset;
88     x = vertexId % width;
89     y = vertexId / width;
90
91     // Retrieves the blend shape normal from the texture, unnormalizes it and multiply by the weight.
92     if( 0.0 != uBlendShapeWeight[index] )
93     {
94       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
95     }
96
97     normal += diff.xyz;
98
99     blendShapeBufferOffset += uBlendShapeComponentSize;
100 #endif
101
102 #ifdef MORPH_TANGENT
103     // Calculate the index to retrieve the tangent from the texture.
104     vertexId = gl_VertexID + blendShapeBufferOffset;
105     x = vertexId % width;
106     y = vertexId / width;
107
108     // Retrieves the blend shape tangent from the texture, unnormalizes it and multiply by the weight.
109     if( 0.0 != uBlendShapeWeight[index] )
110     {
111       diff = uBlendShapeWeight[index] * 2.0 * ( texelFetch( sBlendShapeGeometry, ivec2(x, y), 0 ).xyz - 0.5 );
112     }
113
114     tangent += diff.xyz;
115
116     blendShapeBufferOffset += uBlendShapeComponentSize;
117 #endif
118   }
119
120 #endif
121
122 #ifdef SKINNING
123   mat4 bone = uBone[int(aJoints.x)] * aWeights.x +
124     uBone[int(aJoints.y)] * aWeights.y +
125     uBone[int(aJoints.z)] * aWeights.z +
126     uBone[int(aJoints.w)] * aWeights.w;
127   position = bone * position;
128   normal = (bone * vec4(normal, 0.0)).xyz;
129   tangent = (bone * vec4(tangent, 0.0)).xyz;
130 #endif
131
132   vec4 vPosition = uModelMatrix * position;
133
134   vNormal = normalize(uNormalMatrix * normal);
135
136   vTangent = normalize(uNormalMatrix * tangent);
137
138
139   vec4 viewPosition = uViewMatrix * vPosition;
140   gl_Position = uProjection * viewPosition;
141
142 #ifdef FLIP_V
143   vUV = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
144 #else
145   vUV = aTexCoord;
146 #endif
147
148   vViewVec = viewPosition.xyz;
149 }