[dali_2.3.13] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / graphics / shaders / default-physically-based-shader.frag
1
2 // Original Code
3 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-frag.glsl
4 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
5 //
6 // This fragment shader defines a reference implementation for Physically Based Shading of
7 // a microfacet surface material defined by a glTF model.For the DamagedHelmet.gltf and its Assets
8 //
9 // References:
10 // [1] Real Shading in Unreal Engine 4
11 //     http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
12 // [2] Physically Based Shading at Disney
13 //     http://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
14 // [3] README.md - Environment Maps
15 //     https://github.com/KhronosGroup/glTF-Sample-Viewer/#environment-maps
16 // [4] \"An Inexpensive BRDF Model for Physically based Rendering\" by Christophe Schlick
17 //     https://www.cs.virginia.edu/~jdl/bib/appearance/analytic%20models/schlick94b.pdf
18
19 #ifdef HIGHP
20 precision highp float;
21 #else
22 precision mediump float;
23 #endif
24
25 #ifdef GLTF_CHANNELS
26 #define METALLIC b
27 #define ROUGHNESS g
28 #else //GLTF_CHANNELS
29 #define METALLIC r
30 #define ROUGHNESS a
31 #endif //GLTF_CHANNELS
32
33 uniform lowp vec4 uColor; // Color from SceneGraph
34 uniform lowp vec4 uColorFactor; // Color from material
35 uniform lowp float uMetallicFactor;
36 uniform lowp float uRoughnessFactor;
37 uniform lowp float uDielectricSpecular;
38
39 #ifdef THREE_TEX
40 #ifdef BASECOLOR_TEX
41 uniform sampler2D sAlbedoAlpha;
42 uniform float uBaseColorTextureTransformAvailable;
43 uniform mat3 uBaseColorTextureTransform;
44 #endif // BASECOLOR_TEX
45 #ifdef METALLIC_ROUGHNESS_TEX
46 uniform sampler2D sMetalRoughness;
47 uniform float uMetalRoughnessTextureTransformAvailable;
48 uniform mat3 uMetalRoughnessTextureTransform;
49 #endif // METALLIC_ROUGHNESS_TEX
50 #ifdef NORMAL_TEX
51 uniform sampler2D sNormal;
52 uniform float uNormalTextureTransformAvailable;
53 uniform mat3 uNormalTextureTransform;
54 uniform float uNormalScale;
55 #endif // NORMAL_TEX
56 #else // THREE_TEX
57 uniform sampler2D sAlbedoMetal;
58 uniform float uBaseColorTextureTransformAvailable;
59 uniform mat3 uBaseColorTextureTransform;
60 uniform sampler2D sNormalRoughness;
61 uniform float uNormalRoughnessTextureTransformAvailable;
62 uniform mat3 uNormalRoughnessTextureTransform;
63 #endif
64
65
66
67 #ifdef OCCLUSION
68 uniform sampler2D sOcclusion;
69 uniform float uOcclusionTextureTransformAvailable;
70 uniform mat3 uOcclusionTextureTransform;
71 uniform float uOcclusionStrength;
72 #endif
73
74 #ifdef EMISSIVE_TEXTURE
75 uniform sampler2D sEmissive;
76 uniform float uEmissiveTextureTransformAvailable;
77 uniform mat3 uEmissiveTextureTransform;
78 #endif
79 uniform vec3 uEmissiveFactor;
80
81 uniform float uSpecularFactor;
82 uniform vec3  uSpecularColorFactor;
83 #ifdef MATERIAL_SPECULAR_TEXTURE
84 uniform sampler2D sSpecular;
85 #endif
86 #ifdef MATERIAL_SPECULAR_COLOR_TEXTURE
87 uniform sampler2D sSpecularColor;
88 #endif
89
90 // For Light (Currently Directional Only)
91 #define MAX_LIGHTS 5
92 uniform mediump int uLightCount;
93 uniform mediump vec3 uLightDirection[MAX_LIGHTS];
94 uniform mediump vec3 uLightColor[MAX_LIGHTS];
95
96 // For Shadow Map
97 uniform lowp int uIsShadowEnabled;
98 uniform sampler2D sShadowMap;
99 #ifdef GLSL_VERSION_1_0
100 uniform int uShadowMapWidth;
101 uniform int uShadowMapHeight;
102 #endif
103 INPUT highp vec3 positionFromLightView;
104
105 //// For IBL
106 uniform sampler2D sbrdfLUT;
107 uniform samplerCube sDiffuseEnvSampler;
108 uniform samplerCube sSpecularEnvSampler;
109 uniform float uIblIntensity;
110 uniform mediump vec3 uYDirection;
111 uniform float uMaxLOD;
112
113 // For Alpha Mode.
114 uniform lowp float uOpaque;
115 uniform lowp float uMask;
116 uniform lowp float uAlphaThreshold;
117
118 // TODO: Multiple texture coordinate will be supported.
119 INPUT mediump vec2 vUV;
120 INPUT lowp mat3 vTBN;
121 INPUT lowp vec4 vColor;
122 INPUT highp vec3 vPositionToCamera;
123
124 const float c_MinRoughness = 0.04;
125 const float M_PI = 3.141592653589793;
126
127 // These properties can be used for circular sampling for PCF
128
129 // Percentage Closer Filtering to mitigate the banding artifacts.
130 const int kPcfSampleCount = 9;
131
132 const float kPi = 3.141592653589;
133 const float kInvSampleCount = 1.0 / float(kPcfSampleCount);
134 const float kPcfTheta = 2.0 * kPi * kInvSampleCount;
135 const float kSinPcfTheta = sin(kPcfTheta);
136 const float kCosPcfTheta = cos(kPcfTheta);
137
138 uniform lowp int uEnableShadowSoftFiltering;
139 uniform mediump float uShadowIntensity;
140 uniform highp float uShadowBias;
141
142 vec3 linear(vec3 color)
143 {
144   return pow(color, vec3(2.2));
145 }
146
147 vec2 computeTextureTransform(vec2 texCoord, mat3 textureTransform)
148 {
149     return vec2(textureTransform * vec3(texCoord, 1.0));
150 }
151
152 void main()
153 {
154   // Metallic and Roughness material properties are packed together
155   // In glTF, these factors can be specified by fixed scalar values
156   // or from a metallic-roughness map
157   // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
158   // This layout intentionally reserves the 'r' channel for (optional) occlusion map data
159   lowp float metallic = uMetallicFactor;
160   lowp float perceptualRoughness = uRoughnessFactor;
161   // If there isn't normal texture, use surface normal
162   highp vec3 n = normalize(vTBN[2].xyz);
163
164 #ifdef THREE_TEX
165   // The albedo may be defined from a base texture or a flat color
166 #ifdef BASECOLOR_TEX
167   mediump vec2 baseColorTexCoords = mix(vUV, computeTextureTransform(vUV, uBaseColorTextureTransform), uBaseColorTextureTransformAvailable);
168   lowp vec4 baseColor = TEXTURE(sAlbedoAlpha, baseColorTexCoords);
169   baseColor = vColor * vec4(linear(baseColor.rgb), baseColor.w) * uColorFactor;
170 #else // BASECOLOR_TEX
171   lowp vec4 baseColor = vColor * uColorFactor;
172 #endif // BASECOLOR_TEX
173
174 #ifdef METALLIC_ROUGHNESS_TEX
175   mediump vec2 metalRoughnessTexCoords = mix(vUV, computeTextureTransform(vUV, uMetalRoughnessTextureTransform), uMetalRoughnessTextureTransformAvailable);
176   lowp vec4 metrou = TEXTURE(sMetalRoughness, metalRoughnessTexCoords);
177   metallic = metrou.METALLIC * metallic;
178   perceptualRoughness = metrou.ROUGHNESS * perceptualRoughness;
179 #endif // METALLIC_ROUGHNESS_TEX
180
181 #ifdef NORMAL_TEX
182   mediump vec2 normalTexCoords = mix(vUV, computeTextureTransform(vUV, uNormalTextureTransform), uNormalTextureTransformAvailable);
183   n = TEXTURE(sNormal, normalTexCoords).rgb;
184   n = normalize(vTBN * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
185 #endif // NORMAL_TEX
186 #else // THREE_TEX
187   mediump vec2 baseColorTexCoords = mix(vUV, computeTextureTransform(vUV, uBaseColorTextureTransform), uBaseColorTextureTransformAvailable);
188   vec4 albedoMetal = TEXTURE(sAlbedoMetal, baseColorTexCoords);
189   lowp vec4 baseColor = vec4(linear(albedoMetal.rgb), 1.0) * vColor * uColorFactor;
190
191   metallic = albedoMetal.METALLIC * metallic;
192
193   mediump vec2 normalRoughnessTexCoords = mix(vUV, computeTextureTransform(vUV, uNormalRoughnessTextureTransform), uNormalRoughnessTextureTransformAvailable);
194   vec4 normalRoughness = TEXTURE(sNormalRoughness, normalRoughnessTexCoords);
195   perceptualRoughness = normalRoughness.ROUGHNESS * perceptualRoughness;
196
197   n = normalRoughness.rgb;
198   n = normalize(vTBN * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
199 #endif // THREE_TEX
200
201   // The value of uOpaque and uMask can be 0.0 or 1.0.
202   // If uMask is 1.0, a Pixel that has bigger alpha than uAlphaThreashold becomes fully opaque,
203   // and, a pixel that has smaller alpha than uAlphaThreashold becomes fully transparent.
204   // If uOpaque is 1.0, alpha value of final color is 1.0;
205   // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#_material_alphamode
206   if(uMask > 0.5 && baseColor.a < uAlphaThreshold)
207   {
208     discard;
209   }
210   baseColor.a = mix(baseColor.a, 1.0, uOpaque);
211
212   metallic = clamp(metallic, 0.0, 1.0);
213   // Roughness is authored as perceptual roughness; as is convention,
214   // convert to material roughness by squaring the perceptual roughness [2].
215   perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);
216
217   // Material ior
218   lowp vec3 f0 = vec3(uDielectricSpecular);
219
220   // Material Specular
221   float specularWeight = 1.0;
222   vec4 materialSpecularTexture = vec4(1.0);
223 #ifdef MATERIAL_SPECULAR_TEXTURE
224   materialSpecularTexture.a = TEXTURE(sSpecular, vUV).a;
225 #endif
226 #ifdef MATERIAL_SPECULAR_COLOR_TEXTURE
227   materialSpecularTexture.rgb = TEXTURE(sSpecularColor, vUV).rgb;
228 #endif
229   specularWeight = uSpecularFactor * materialSpecularTexture.a;
230   f0 = min(f0 * uSpecularColorFactor * materialSpecularTexture.rgb, vec3(1.0));
231   f0 = mix(f0, baseColor.rgb, metallic);
232
233   mediump vec3 v = normalize(vPositionToCamera); // Vector from surface point to camera
234   mediump float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);
235   mediump vec3 reflection = -normalize(reflect(v, n));
236   lowp vec3 brdf = TEXTURE(sbrdfLUT, vec2(NdotV, 1.0 - perceptualRoughness)).rgb;
237   vec3 Fr = max(vec3(1.0 - perceptualRoughness), f0) - f0;
238   vec3 k_S = f0 + Fr * pow(1.0 - NdotV, 5.0);
239   vec3 FssEss = specularWeight * (k_S * brdf.x + brdf.y);
240
241   // Specular Light
242   // uMaxLOD that means mipmap level of specular texture is used for bluring of reflection of specular following roughness.
243   float lod = perceptualRoughness * (uMaxLOD - 1.0);
244 #ifdef GLSL_VERSION_1_0
245   // glsl 1.0 doesn't support textureLod. Let we just use textureCube instead.
246   lowp vec3 specularLight = linear(textureCube(sSpecularEnvSampler, reflection * uYDirection).rgb);
247 #else
248   lowp vec3 specularLight = linear(textureLod(sSpecularEnvSampler, reflection * uYDirection, lod).rgb);
249 #endif
250   lowp vec3 specular = specularLight * FssEss;
251
252   // Diffuse Light
253   lowp vec3 diffuseColor = mix(baseColor.rgb, vec3(0), metallic);
254 #ifdef GLSL_VERSION_1_0
255   lowp vec3 irradiance = linear(textureCube(sDiffuseEnvSampler, n * uYDirection).rgb);
256 #else
257   lowp vec3 irradiance = linear(TEXTURE(sDiffuseEnvSampler, n * uYDirection).rgb);
258 #endif
259   float Ems = (1.0 - (brdf.x + brdf.y));
260   vec3 F_avg = specularWeight * (f0 + (1.0 - f0) / 21.0);
261   vec3 FmsEms = Ems * FssEss * F_avg / (1.0 - F_avg * Ems);
262   vec3 k_D = diffuseColor * (1.0 - FssEss + FmsEms);
263   lowp vec3 diffuse = (FmsEms + k_D) * irradiance;
264
265   lowp vec3 color = (diffuse + specular) * uIblIntensity;
266
267   // Punctual Light
268   if(uLightCount > 0)
269   {
270     // Compute reflectance.
271     highp float reflectance = max(max(f0.r, f0.g), f0.b);
272     highp float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
273     highp float r = perceptualRoughness * perceptualRoughness;
274     highp float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));
275     highp float roughnessSq = r * r;
276     highp vec3 diffuseColorPunctual = baseColor.rgb * (vec3(1.0) - f0);
277     diffuseColorPunctual *= ( 1.0 - metallic );
278
279     for(int i = 0; i < uLightCount; ++i)
280     {
281       highp vec3 l = normalize(-uLightDirection[i]); // Vector from surface point to light
282       highp vec3 h = normalize(l+v);               // Half vector between both l and v
283       highp float VdotH = dot(v, h);
284       highp vec3 specularReflection = f0 + (reflectance90 - f0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0);
285
286       highp float NdotL = clamp(dot(n, l), 0.001, 1.0);
287       highp float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
288       highp float geometricOcclusion = attenuationL * attenuationV;
289
290       highp float NdotH = dot(n, h);
291       highp float f = (NdotH * roughnessSq - NdotH) * NdotH + 1.0;
292       highp float microfacetDistribution = roughnessSq / (M_PI * f * f);;
293
294       // Calculation of analytical lighting contribution
295       highp vec3 diffuseContrib = ( 1.0 - specularReflection ) * ( diffuseColorPunctual / M_PI );
296       highp vec3 specContrib = specularReflection * geometricOcclusion * microfacetDistribution / ( 4.0 * NdotL * NdotV );
297
298       // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
299       color += NdotL * uLightColor[i] * (diffuseContrib + specContrib);
300     }
301   }
302
303   if(float(uIsShadowEnabled) * uShadowIntensity > 0.0)
304   {
305     mediump float exposureFactor = 0.0;
306     if(uEnableShadowSoftFiltering > 0)
307     {
308 #ifdef GLSL_VERSION_1_0
309       ivec2 texSize = ivec2(uShadowMapWidth, uShadowMapHeight);
310 #else
311       ivec2 texSize = textureSize(sShadowMap, 0);
312 #endif
313       mediump vec2 texelSize = vec2(1.0) / vec2(texSize.x, texSize.y);
314       mediump vec2 pcfSample = vec2(1.0, 0.0);
315       for (int i = 0; i < kPcfSampleCount; ++i)
316       {
317         pcfSample = vec2(kCosPcfTheta * pcfSample.x - kSinPcfTheta * pcfSample.y,
318                          kSinPcfTheta * pcfSample.x + kCosPcfTheta * pcfSample.y);
319         lowp float depthValue = TEXTURE(sShadowMap, positionFromLightView.xy + pcfSample * texelSize).r;
320         exposureFactor += (depthValue < positionFromLightView.z - uShadowBias) ? 0.0 : 1.0;
321       }
322       exposureFactor *= kInvSampleCount;
323     }
324     else
325     {
326       mediump float depthValue = TEXTURE(sShadowMap, positionFromLightView.xy).r;
327       exposureFactor           = (depthValue < positionFromLightView.z - uShadowBias) ? 0.0 : 1.0;
328     }
329     color *= (1.0 - (1.0 - exposureFactor) * uShadowIntensity);
330   }
331
332 #ifdef OCCLUSION
333   mediump vec2 occlusionTexCoords = mix(vUV, computeTextureTransform(vUV, uOcclusionTextureTransform), uOcclusionTextureTransformAvailable);
334   lowp float ao = TEXTURE(sOcclusion, occlusionTexCoords).r;
335   color = mix(color, color * ao, uOcclusionStrength);
336 #endif // OCCLUSION
337
338 #ifdef EMISSIVE_TEXTURE
339   mediump vec2 emissiveTexCoords = mix(vUV, computeTextureTransform(vUV, uEmissiveTextureTransform), uEmissiveTextureTransformAvailable);
340   lowp vec3 emissive = linear(TEXTURE(sEmissive, emissiveTexCoords).rgb) * uEmissiveFactor;
341 #else
342   lowp vec3 emissive = uEmissiveFactor;
343 #endif // EMISSIVE_TEXTURE
344   color += emissive;
345
346   OUT_COLOR = vec4(pow(color, vec3(1.0 / 2.2)), baseColor.a) * uColor;
347 }