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