Merge "Fix webp&gif issue" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / internal / graphics / shaders / default-physically-based-shader.frag
1 #version 300 es
2
3 #ifdef HIGHP
4   precision highp float;
5 #else
6   precision mediump float;
7 #endif
8
9 #ifdef THREE_TEX
10 #ifdef GLTF_CHANNELS
11 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#pbrmetallicroughnessmetallicroughnesstexture
12 #define METALLIC b
13 #define ROUGHNESS g
14 #else //GLTF_CHANNELS
15 #define METALLIC r
16 #define ROUGHNESS a
17 #endif //GLTF_CHANNELS
18 #endif //THREE_TEX
19
20 #ifdef THREE_TEX
21   uniform sampler2D sAlbedoAlpha;
22   uniform sampler2D sMetalRoughness;
23   uniform sampler2D sNormal;
24
25 #ifdef ALPHA_TEST
26   uniform float uAlphaThreshold;
27 #endif //ALPHA_TEST
28
29 #else
30   uniform sampler2D sAlbedoMetal;
31   uniform sampler2D sNormalRoughness;
32 #endif
33
34 #ifdef OCCLUSION
35   uniform sampler2D sOcclusion;
36   uniform float uOcclusionStrength;
37 #endif
38
39 #ifdef EMISSIVE
40   uniform sampler2D sEmissive;
41   uniform vec3 uEmissiveFactor;
42 #endif
43
44 uniform samplerCube sDiffuse;
45 uniform samplerCube sSpecular;
46
47 // Number of mip map levels in the texture
48 uniform float uMaxLOD;
49
50 // Transformation matrix of the cubemap texture
51 uniform mat4 uCubeMatrix;
52
53 uniform vec4 uColor;
54 uniform float uMetallicFactor;
55 uniform float uRoughnessFactor;
56
57 //IBL Light intensity
58 uniform float uIblIntensity;
59
60 // TODO: Multiple texture coordinate will be supported.
61 in vec2 vUV;
62 in vec3 vNormal;
63 in vec3 vTangent;
64 in vec3 vViewVec;
65
66 out vec4 FragColor;
67
68 // Functions for BRDF calculation come from
69 // https://www.unrealengine.com/blog/physically-based-shading-on-mobile
70 // Based on the paper by Dimitar Lazarov
71 // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
72 vec3 EnvBRDFApprox( vec3 SpecularColor, float Roughness, float NoV )
73 {
74   const vec4 c0 = vec4( -1.0, -0.0275, -0.572, 0.022 );
75   const vec4 c1 = vec4( 1.0, 0.0425, 1.04, -0.04 );
76   vec4 r = Roughness * c0 + c1;
77   float a004 = min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y;
78   vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
79
80   return SpecularColor * AB.x + AB.y;
81 }
82
83 void main()
84 {
85   // We get information from the maps (albedo, normal map, roughness, metalness
86   // I access the maps in the order they will be used
87 #ifdef THREE_TEX
88   vec4 albedoAlpha = texture(sAlbedoAlpha, vUV.st);
89   float alpha = albedoAlpha.a;
90 #ifdef ALPHA_TEST
91   if (alpha <= uAlphaThreshold)
92   {
93     discard;
94   }
95 #endif  //ALPHA_TEST
96   vec3 albedoColor = albedoAlpha.rgb * uColor.rgb;
97
98   vec4 metalRoughness = texture(sMetalRoughness, vUV.st);
99   float metallic = metalRoughness.METALLIC * uMetallicFactor;
100   float roughness = metalRoughness.ROUGHNESS * uRoughnessFactor;
101
102   vec3 normalMap = texture(sNormal, vUV.st).rgb;
103 #else  //THREE_TEX
104   vec4 albedoMetal = texture(sAlbedoMetal, vUV.st);
105   vec3 albedoColor = albedoMetal.rgb * uColor.rgb;
106   float metallic = albedoMetal.a * uMetallicFactor;
107
108   vec4 normalRoughness = texture(sNormalRoughness, vUV.st);
109   vec3 normalMap = normalRoughness.rgb;
110   float roughness = normalRoughness.a * uRoughnessFactor;
111 #endif
112   //Normalize vectors
113   vec3 normal = normalize(vNormal);
114   vec3 tangent = normalize(vTangent);
115
116   // NOTE: normal and tangent have to be orthogonal for the result of the cross()
117   // product to be a unit vector. We might find that we need to normalize().
118   vec3 bitangent = cross(normal, tangent);
119
120   vec3 viewVec = normalize(vViewVec);
121
122   // Create Inverse Local to world matrix
123   mat3 vInvTBN = mat3(tangent, bitangent, normal);
124
125   // Get normal map info in world space
126   normalMap = normalize(normalMap - 0.5);
127   vec3 newNormal = vInvTBN * normalMap.rgb;
128
129   // Calculate normal dot view vector
130   float NoV = max(dot(newNormal, -viewVec), 0.0);
131
132   // Reflect vector
133   vec3 reflectionVec = reflect(viewVec, newNormal);
134
135   //transform it now to environment coordinates (used when the environment rotates)
136   vec3 reflecCube = (uCubeMatrix * vec4( reflectionVec, 0.0 ) ).xyz;
137   reflecCube = normalize( reflecCube );
138
139   //transform it now to environment coordinates
140   vec3 normalCube = ( uCubeMatrix * vec4( newNormal, 0.0 ) ).xyz;
141   normalCube = normalize( normalCube );
142
143   // Get irradiance from diffuse cubemap
144   vec3 irradiance = texture( sDiffuse, normalCube ).rgb;
145
146   // Access reflection color using roughness value
147   float finalLod = mix( 0.0, uMaxLOD - 2.0, roughness);
148   vec3 reflectionColor = textureLod(sSpecular, reflecCube, finalLod).rgb;
149
150   // We are supposed to be using DielectricColor (0.04) of a plastic (almost everything)
151   // http://blog.selfshadow.com/publications/s2014-shading-course/hoffman/s2014_pbs_physics_math_slides.pdf
152   // however that seems to prevent achieving very dark tones (i.e. get dark gray blacks).
153   vec3 DiffuseColor = albedoColor - albedoColor * metallic;  // 1 mad
154   vec3 SpecularColor = mix( vec3(0.04), albedoColor, metallic); // 2 mad
155
156   // Calculate specular color using Magic Function (takes original roughness and normal dot view).
157   vec3 specColor =  reflectionColor.rgb * EnvBRDFApprox(SpecularColor, roughness, NoV );
158
159   // Multiply the result by albedo texture and do energy conservation
160   vec3 diffuseColor = irradiance * DiffuseColor;
161
162   // Final color is the sum of the diffuse and specular term
163   vec3 finalColor = diffuseColor + specColor;
164
165   finalColor = sqrt( finalColor ) * uIblIntensity;
166
167
168 #ifdef OCCLUSION
169   float ao = texture(sOcclusion, vUV.st).r;
170   finalColor = mix( finalColor, finalColor * ao, uOcclusionStrength );
171 #endif
172
173 #ifdef EMISSIVE
174   vec3 emissive = texture( sEmissive, vUV.st ).rgb * uEmissiveFactor;
175   finalColor += emissive;
176 #endif
177
178 #ifdef THREE_TEX
179   FragColor = vec4( finalColor, alpha );
180 #else //THREE_TEX
181   FragColor = vec4( finalColor, 1.0 );
182 #endif //THREE_TEX
183 }