Debian Codegraph @

shaders/src/light_indirect.fs

raw
1 //------------------------------------------------------------------------------
2 // Image based lighting configuration
3 //------------------------------------------------------------------------------
4
5 // Number of spherical harmonics bands (1, 2 or 3)
6 #define SPHERICAL_HARMONICS_BANDS 3
7
8 // IBL integration algorithm
9 #define IBL_INTEGRATION_PREFILTERED_CUBEMAP 0
10 #define IBL_INTEGRATION_IMPORTANCE_SAMPLING 1
11
12 #define IBL_INTEGRATION IBL_INTEGRATION_PREFILTERED_CUBEMAP
13
14 #define IBL_INTEGRATION_IMPORTANCE_SAMPLING_COUNT 64
15
16 //------------------------------------------------------------------------------
17 // IBL utilities
18 //------------------------------------------------------------------------------
19
20 vec3 decodeDataForIBL(const vec4 data) {
21 return data.rgb;
22 }
23
24 //------------------------------------------------------------------------------
25 // IBL prefiltered DFG term implementations
26 //------------------------------------------------------------------------------
27
28 vec3 PrefilteredDFG_LUT(float lod, float NoV) {
29 // coord = sqrt(linear_roughness), which is the mapping used by cmgen.
30 return textureLod(light_iblDFG, vec2(NoV, lod), 0.0).rgb;
31 }
32
33 //------------------------------------------------------------------------------
34 // IBL environment BRDF dispatch
35 //------------------------------------------------------------------------------
36
37 vec3 prefilteredDFG(float perceptualRoughness, float NoV) {
38 // PrefilteredDFG_LUT() takes a LOD, which is sqrt(roughness) = perceptualRoughness
39 return PrefilteredDFG_LUT(perceptualRoughness, NoV);
40 }
41
42 //------------------------------------------------------------------------------
43 // IBL irradiance implementations
44 //------------------------------------------------------------------------------
45
46 vec3 Irradiance_SphericalHarmonics(const vec3 n) {
47 return max(
48 frameUniforms.iblSH[0]
49 #if SPHERICAL_HARMONICS_BANDS >= 2
50 + frameUniforms.iblSH[1] * (n.y)
51 + frameUniforms.iblSH[2] * (n.z)
52 + frameUniforms.iblSH[3] * (n.x)
53 #endif
54 #if SPHERICAL_HARMONICS_BANDS >= 3
55 + frameUniforms.iblSH[4] * (n.y * n.x)
56 + frameUniforms.iblSH[5] * (n.y * n.z)
57 + frameUniforms.iblSH[6] * (3.0 * n.z * n.z - 1.0)
58 + frameUniforms.iblSH[7] * (n.z * n.x)
59 + frameUniforms.iblSH[8] * (n.x * n.x - n.y * n.y)
60 #endif
61 , 0.0);
62 }
63
64 vec3 Irradiance_RoughnessOne(const vec3 n) {
65 // note: lod used is always integer, hopefully the hardware skips tri-linear filtering
66 return decodeDataForIBL(textureLod(light_iblSpecular, n, frameUniforms.iblRoughnessOneLevel));
67 }
68
69 //------------------------------------------------------------------------------
70 // IBL irradiance dispatch
71 //------------------------------------------------------------------------------
72
73 vec3 diffuseIrradiance(const vec3 n) {
74 if (frameUniforms.iblSH[0].x == 65504.0) {
75 #if FILAMENT_QUALITY < FILAMENT_QUALITY_HIGH
76 return Irradiance_RoughnessOne(n);
77 #else
78 ivec2 s = textureSize(light_iblSpecular, int(frameUniforms.iblRoughnessOneLevel));
79 float du = 1.0 / float(s.x);
80 float dv = 1.0 / float(s.y);
81 vec3 m0 = normalize(cross(n, vec3(0.0, 1.0, 0.0)));
82 vec3 m1 = cross(m0, n);
83 vec3 m0du = m0 * du;
84 vec3 m1dv = m1 * dv;
85 vec3 c;
86 c = Irradiance_RoughnessOne(n - m0du - m1dv);
87 c += Irradiance_RoughnessOne(n + m0du - m1dv);
88 c += Irradiance_RoughnessOne(n + m0du + m1dv);
89 c += Irradiance_RoughnessOne(n - m0du + m1dv);
90 return c * 0.25;
91 #endif
92 } else {
93 return Irradiance_SphericalHarmonics(n);
94 }
95 }
96
97 //------------------------------------------------------------------------------
98 // IBL specular
99 //------------------------------------------------------------------------------
100
101 float perceptualRoughnessToLod(float perceptualRoughness) {
102 // The mapping below is a quadratic fit for log2(perceptualRoughness)+iblRoughnessOneLevel when
103 // iblRoughnessOneLevel is 4. We found empirically that this mapping works very well for
104 // a 256 cubemap with 5 levels used. But also scales well for other iblRoughnessOneLevel values.
105 return frameUniforms.iblRoughnessOneLevel * perceptualRoughness * (2.0 - perceptualRoughness);
106 }
107
108 vec3 prefilteredRadiance(const vec3 r, float perceptualRoughness) {
109 float lod = perceptualRoughnessToLod(perceptualRoughness);
110 return decodeDataForIBL(textureLod(light_iblSpecular, r, lod));
111 }
112
113 vec3 prefilteredRadiance(const vec3 r, float roughness, float offset) {
114 float lod = frameUniforms.iblRoughnessOneLevel * roughness;
115 return decodeDataForIBL(textureLod(light_iblSpecular, r, lod + offset));
116 }
117
118 vec3 getSpecularDominantDirection(const vec3 n, const vec3 r, float roughness) {
119 return mix(r, n, roughness * roughness);
120 }
121
122 vec3 specularDFG(const PixelParams pixel) {
123 #if defined(SHADING_MODEL_CLOTH)
124 return pixel.f0 * pixel.dfg.z;
125 #else
126 return mix(pixel.dfg.xxx, pixel.dfg.yyy, pixel.f0);
127 #endif
128 }
129
130 /**
131 * Returns the reflected vector at the current shading point. The reflected vector
132 * return by this function might be different from shading_reflected:
133 * - For anisotropic material, we bend the reflection vector to simulate
134 * anisotropic indirect lighting
135 * - The reflected vector may be modified to point towards the dominant specular
136 * direction to match reference renderings when the roughness increases
137 */
138
139 vec3 getReflectedVector(const PixelParams pixel, const vec3 v, const vec3 n) {
140 #if defined(MATERIAL_HAS_ANISOTROPY)
141 vec3 anisotropyDirection = pixel.anisotropy >= 0.0 ? pixel.anisotropicB : pixel.anisotropicT;
142 vec3 anisotropicTangent = cross(anisotropyDirection, v);
143 vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection);
144 float bendFactor = abs(pixel.anisotropy) * saturate(5.0 * pixel.perceptualRoughness);
145 vec3 bentNormal = normalize(mix(n, anisotropicNormal, bendFactor));
146
147 vec3 r = reflect(-v, bentNormal);
148 #else
149 vec3 r = reflect(-v, n);
150 #endif
151 return r;
152 }
153
154 vec3 getReflectedVector(const PixelParams pixel, const vec3 n) {
155 #if defined(MATERIAL_HAS_ANISOTROPY)
156 vec3 r = getReflectedVector(pixel, shading_view, n);
157 #else
158 vec3 r = shading_reflected;
159 #endif
160 return getSpecularDominantDirection(n, r, pixel.roughness);
161 }
162
163 //------------------------------------------------------------------------------
164 // Prefiltered importance sampling
165 //------------------------------------------------------------------------------
166
167 #if IBL_INTEGRATION == IBL_INTEGRATION_IMPORTANCE_SAMPLING
168 vec2 hammersley(uint index) {
169 const uint numSamples = uint(IBL_INTEGRATION_IMPORTANCE_SAMPLING_COUNT);
170 const float invNumSamples = 1.0 / float(numSamples);
171 const float tof = 0.5 / float(0x80000000U);
172 uint bits = index;
173 bits = (bits << 16u) | (bits >> 16u);
174 bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
175 bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
176 bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
177 bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
178 return vec2(float(index) * invNumSamples, float(bits) * tof);
179 }
180
181 vec3 importanceSamplingNdfDggx(vec2 u, float roughness) {
182 // Importance sampling D_GGX
183 float a2 = roughness * roughness;
184 float phi = 2.0 * PI * u.x;
185 float cosTheta2 = (1.0 - u.y) / (1.0 + (a2 - 1.0) * u.y);
186 float cosTheta = sqrt(cosTheta2);
187 float sinTheta = sqrt(1.0 - cosTheta2);
188 return vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
189 }
190
191 vec3 hemisphereCosSample(vec2 u) {
192 float phi = 2.0f * PI * u.x;
193 float cosTheta2 = 1.0 - u.y;
194 float cosTheta = sqrt(cosTheta2);
195 float sinTheta = sqrt(1.0 - cosTheta2);
196 return vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
197 }
198
199 vec3 importanceSamplingVNdfDggx(vec2 u, float roughness, vec3 v) {
200 // See: "A Simpler and Exact Sampling Routine for the GGX Distribution of Visible Normals", Eric Heitz
201 float alpha = roughness;
202
203 // stretch view
204 v = normalize(vec3(alpha * v.x, alpha * v.y, v.z));
205
206 // orthonormal basis
207 vec3 up = abs(v.z) < 0.9999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
208 vec3 t = normalize(cross(up, v));
209 vec3 b = cross(t, v);
210
211 // sample point with polar coordinates (r, phi)
212 float a = 1.0 / (1.0 + v.z);
213 float r = sqrt(u.x);
214 float phi = (u.y < a) ? u.y / a * PI : PI + (u.y - a) / (1.0 - a) * PI;
215 float p1 = r * cos(phi);
216 float p2 = r * sin(phi) * ((u.y < a) ? 1.0 : v.z);
217
218 // compute normal
219 vec3 h = p1 * t + p2 * b + sqrt(max(0.0, 1.0 - p1*p1 - p2*p2)) * v;
220
221 // unstretch
222 h = normalize(vec3(alpha * h.x, alpha * h.y, max(0.0, h.z)));
223 return h;
224 }
225
226 float prefilteredImportanceSampling(float ipdf, float omegaP) {
227 // See: "Real-time Shading with Filtered Importance Sampling", Jaroslav Krivanek
228 // Prefiltering doesn't work with anisotropy
229 const float numSamples = float(IBL_INTEGRATION_IMPORTANCE_SAMPLING_COUNT);
230 const float invNumSamples = 1.0 / float(numSamples);
231 const float K = 4.0;
232 float omegaS = invNumSamples * ipdf;
233 float mipLevel = log2(K * omegaS / omegaP) * 0.5; // log4
234 return mipLevel;
235 }
236
237 vec3 isEvaluateSpecularIBL(const PixelParams pixel, vec3 n, vec3 v, float NoV) {
238 const uint numSamples = uint(IBL_INTEGRATION_IMPORTANCE_SAMPLING_COUNT);
239 const float invNumSamples = 1.0 / float(numSamples);
240 const vec3 up = vec3(0.0, 0.0, 1.0);
241
242 // TODO: for a true anisotropic BRDF, we need a real tangent space
243 // tangent space
244 mat3 T;
245 T[0] = normalize(cross(up, n));
246 T[1] = cross(n, T[0]);
247 T[2] = n;
248
249 // Random rotation around N per pixel
250 const vec3 m = vec3(0.06711056, 0.00583715, 52.9829189);
251 float a = 2.0 * PI * fract(m.z * fract(dot(gl_FragCoord.xy, m.xy)));
252 float c = cos(a);
253 float s = sin(a);
254 mat3 R;
255 R[0] = vec3( c, s, 0);
256 R[1] = vec3(-s, c, 0);
257 R[2] = vec3( 0, 0, 1);
258 T *= R;
259
260 float roughness = pixel.roughness;
261 float dim = float(textureSize(light_iblSpecular, 0).x);
262 float omegaP = (4.0 * PI) / (6.0 * dim * dim);
263
264 vec3 indirectSpecular = vec3(0.0);
265 for (uint i = 0u; i < numSamples; i++) {
266 vec2 u = hammersley(i);
267 vec3 h = T * importanceSamplingNdfDggx(u, roughness);
268
269 // Since anisotropy doesn't work with prefiltering, we use the same "faux" anisotropy
270 // we do when we use the prefiltered cubemap
271 vec3 l = getReflectedVector(pixel, v, h);
272
273 // Compute this sample's contribution to the brdf
274 float NoL = saturate(dot(n, l));
275 if (NoL > 0.0) {
276 float NoH = dot(n, h);
277 float LoH = saturate(dot(l, h));
278
279 // PDF inverse (we must use D_GGX() here, which is used to generate samples)
280 float ipdf = (4.0 * LoH) / (D_GGX(roughness, NoH, h) * NoH);
281 float mipLevel = prefilteredImportanceSampling(ipdf, omegaP);
282 vec3 L = decodeDataForIBL(textureLod(light_iblSpecular, l, mipLevel));
283
284 float D = distribution(roughness, NoH, h);
285 float V = visibility(roughness, NoV, NoL);
286 vec3 F = fresnel(pixel.f0, LoH);
287 vec3 Fr = F * (D * V * NoL * ipdf * invNumSamples);
288
289 indirectSpecular += (Fr * L);
290 }
291 }
292
293 return indirectSpecular;
294 }
295
296 vec3 isEvaluateDiffuseIBL(const PixelParams pixel, vec3 n, vec3 v) {
297 const uint numSamples = uint(IBL_INTEGRATION_IMPORTANCE_SAMPLING_COUNT);
298 const float invNumSamples = 1.0 / float(numSamples);
299 const vec3 up = vec3(0.0, 0.0, 1.0);
300
301 // TODO: for a true anisotropic BRDF, we need a real tangent space
302 // tangent space
303 mat3 T;
304 T[0] = normalize(cross(up, n));
305 T[1] = cross(n, T[0]);
306 T[2] = n;
307
308 // Random rotation around N per pixel
309 const vec3 m = vec3(0.06711056, 0.00583715, 52.9829189);
310 float a = 2.0 * PI * fract(m.z * fract(dot(gl_FragCoord.xy, m.xy)));
311 float c = cos(a);
312 float s = sin(a);
313 mat3 R;
314 R[0] = vec3( c, s, 0);
315 R[1] = vec3(-s, c, 0);
316 R[2] = vec3( 0, 0, 1);
317 T *= R;
318
319 float dim = float(textureSize(light_iblSpecular, 0).x);
320 float omegaP = (4.0 * PI) / (6.0 * dim * dim);
321
322 vec3 indirectDiffuse = vec3(0.0);
323 for (uint i = 0u; i < numSamples; i++) {
324 vec2 u = hammersley(i);
325 vec3 h = T * hemisphereCosSample(u);
326
327 // Since anisotropy doesn't work with prefiltering, we use the same "faux" anisotropy
328 // we do when we use the prefiltered cubemap
329 vec3 l = getReflectedVector(pixel, v, h);
330
331 // Compute this sample's contribution to the brdf
332 float NoL = saturate(dot(n, l));
333 if (NoL > 0.0) {
334 // PDF inverse (we must use D_GGX() here, which is used to generate samples)
335 float ipdf = PI / NoL;
336 // we have to bias the mipLevel (+1) to help with very strong highlights
337 float mipLevel = prefilteredImportanceSampling(ipdf, omegaP) + 1.0;
338 vec3 L = decodeDataForIBL(textureLod(light_iblSpecular, l, mipLevel));
339 indirectDiffuse += L;
340 }
341 }
342
343 return indirectDiffuse * invNumSamples; // we bake 1/PI here, which cancels out
344 }
345
346 void isEvaluateClearCoatIBL(const PixelParams pixel, float specularAO, inout vec3 Fd, inout vec3 Fr) {
347 #if defined(MATERIAL_HAS_CLEAR_COAT)
348 #if defined(MATERIAL_HAS_NORMAL) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL)
349 // We want to use the geometric normal for the clear coat layer
350 float clearCoatNoV = clampNoV(dot(shading_clearCoatNormal, shading_view));
351 vec3 clearCoatNormal = shading_clearCoatNormal;
352 #else
353 float clearCoatNoV = shading_NoV;
354 vec3 clearCoatNormal = shading_normal;
355 #endif
356 // The clear coat layer assumes an IOR of 1.5 (4% reflectance)
357 float Fc = F_Schlick(0.04, 1.0, clearCoatNoV) * pixel.clearCoat;
358 float attenuation = 1.0 - Fc;
359 Fd *= attenuation;
360 Fr *= attenuation;
361
362 PixelParams p;
363 p.perceptualRoughness = pixel.clearCoatPerceptualRoughness;
364 p.f0 = vec3(0.04);
365 p.roughness = perceptualRoughnessToRoughness(p.perceptualRoughness);
366 #if defined(MATERIAL_HAS_ANISOTROPY)
367 p.anisotropy = 0.0;
368 #endif
369
370 vec3 clearCoatLobe = isEvaluateSpecularIBL(p, clearCoatNormal, shading_view, clearCoatNoV);
371 Fr += clearCoatLobe * (specularAO * pixel.clearCoat);
372 #endif
373 }
374 #endif
375
376 //------------------------------------------------------------------------------
377 // IBL evaluation
378 //------------------------------------------------------------------------------
379
380 void evaluateClothIndirectDiffuseBRDF(const PixelParams pixel, inout float diffuse) {
381 #if defined(SHADING_MODEL_CLOTH)
382 #if defined(MATERIAL_HAS_SUBSURFACE_COLOR)
383 // Simulate subsurface scattering with a wrap diffuse term
384 diffuse *= Fd_Wrap(shading_NoV, 0.5);
385 #endif
386 #endif
387 }
388
389 void evaluateSheenIBL(const PixelParams pixel, float specularAO, inout vec3 Fd, inout vec3 Fr) {
390 #if !defined(SHADING_MODEL_CLOTH) && !defined(SHADING_MODEL_SUBSURFACE)
391 #if defined(MATERIAL_HAS_SHEEN_COLOR)
392 // Albedo scaling of the base layer before we layer sheen on top
393 Fd *= pixel.sheenScaling;
394 Fr *= pixel.sheenScaling;
395
396 vec3 reflectance = pixel.sheenDFG * pixel.sheenColor;
397 Fr += reflectance * prefilteredRadiance(shading_reflected, pixel.sheenPerceptualRoughness);
398 #endif
399 #endif
400 }
401
402 void evaluateClearCoatIBL(const PixelParams pixel, float specularAO, inout vec3 Fd, inout vec3 Fr) {
403 #if IBL_INTEGRATION == IBL_INTEGRATION_IMPORTANCE_SAMPLING
404 isEvaluateClearCoatIBL(pixel, specularAO, Fd, Fr);
405 return;
406 #endif
407
408 #if defined(MATERIAL_HAS_CLEAR_COAT)
409 #if defined(MATERIAL_HAS_NORMAL) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL)
410 // We want to use the geometric normal for the clear coat layer
411 float clearCoatNoV = clampNoV(dot(shading_clearCoatNormal, shading_view));
412 vec3 clearCoatR = reflect(-shading_view, shading_clearCoatNormal);
413 #else
414 float clearCoatNoV = shading_NoV;
415 vec3 clearCoatR = shading_reflected;
416 #endif
417 // The clear coat layer assumes an IOR of 1.5 (4% reflectance)
418 float Fc = F_Schlick(0.04, 1.0, clearCoatNoV) * pixel.clearCoat;
419 float attenuation = 1.0 - Fc;
420 Fd *= attenuation;
421 Fr *= attenuation;
422 Fr += prefilteredRadiance(clearCoatR, pixel.clearCoatPerceptualRoughness) * (specularAO * Fc);
423 #endif
424 }
425
426 void evaluateSubsurfaceIBL(const PixelParams pixel, const vec3 diffuseIrradiance,
427 inout vec3 Fd, inout vec3 Fr) {
428 #if defined(SHADING_MODEL_SUBSURFACE)
429 vec3 viewIndependent = diffuseIrradiance;
430 vec3 viewDependent = prefilteredRadiance(-shading_view, pixel.roughness, 1.0 + pixel.thickness);
431 float attenuation = (1.0 - pixel.thickness) / (2.0 * PI);
432 Fd += pixel.subsurfaceColor * (viewIndependent + viewDependent) * attenuation;
433 #elif defined(SHADING_MODEL_CLOTH) && defined(MATERIAL_HAS_SUBSURFACE_COLOR)
434 Fd *= saturate(pixel.subsurfaceColor + shading_NoV);
435 #endif
436 }
437
438 #if defined(HAS_REFRACTION)
439
440 struct Refraction {
441 vec3 position;
442 vec3 direction;
443 float d;
444 };
445
446 void refractionSolidSphere(const PixelParams pixel,
447 const vec3 n, vec3 r, out Refraction ray) {
448 r = refract(r, n, pixel.etaIR);
449 float NoR = dot(n, r);
450 float d = pixel.thickness * -NoR;
451 ray.position = vec3(shading_position + r * d);
452 ray.d = d;
453 vec3 n1 = normalize(NoR * r - n * 0.5);
454 ray.direction = refract(r, n1, pixel.etaRI);
455 }
456
457 void refractionSolidBox(const PixelParams pixel,
458 const vec3 n, vec3 r, out Refraction ray) {
459 vec3 rr = refract(r, n, pixel.etaIR);
460 float NoR = dot(n, rr);
461 float d = pixel.thickness / max(-NoR, 0.001);
462 ray.position = vec3(shading_position + rr * d);
463 ray.direction = r;
464 ray.d = d;
465 #if REFRACTION_MODE == REFRACTION_MODE_CUBEMAP
466 // fudge direction vector, so we see the offset due to the thickness of the object
467 float envDistance = 10.0; // this should come from a ubo
468 ray.direction = normalize((ray.position - shading_position) + ray.direction * envDistance);
469 #endif
470 }
471
472 void refractionThinSphere(const PixelParams pixel,
473 const vec3 n, vec3 r, out Refraction ray) {
474 float d = 0.0;
475 #if defined(MATERIAL_HAS_MICRO_THICKNESS)
476 // note: we need the refracted ray to calculate the distance traveled
477 // we could use shading_NoV, but we would lose the dependency on ior.
478 vec3 rr = refract(r, n, pixel.etaIR);
479 float NoR = dot(n, rr);
480 d = pixel.uThickness / max(-NoR, 0.001);
481 ray.position = vec3(shading_position + rr * d);
482 #else
483 ray.position = vec3(shading_position);
484 #endif
485 ray.direction = r;
486 ray.d = d;
487 }
488
489 void applyRefraction(const PixelParams pixel,
490 const vec3 n0, vec3 E, vec3 Fd, vec3 Fr,
491 inout vec3 color) {
492
493 Refraction ray;
494
495 #if REFRACTION_TYPE == REFRACTION_TYPE_SOLID
496 refractionSolidSphere(pixel, n0, -shading_view, ray);
497 #elif REFRACTION_TYPE == REFRACTION_TYPE_THIN
498 refractionThinSphere(pixel, n0, -shading_view, ray);
499 #else
500 #error invalid REFRACTION_TYPE
501 #endif
502
503 /* compute transmission T */
504 #if defined(MATERIAL_HAS_ABSORPTION)
505 #if defined(MATERIAL_HAS_THICKNESS) || defined(MATERIAL_HAS_MICRO_THICKNESS)
506 vec3 T = min(vec3(1.0), exp(-pixel.absorption * ray.d));
507 #else
508 vec3 T = 1.0 - pixel.absorption;
509 #endif
510 #endif
511
512 float perceptualRoughness = pixel.perceptualRoughnessUnclamped;
513 #if REFRACTION_TYPE == REFRACTION_TYPE_THIN
514 // Roughness remaping for thin layers, see Burley 2012, "Physically-Based Shading at Disney"
515 perceptualRoughness = saturate((0.65 * pixel.etaRI - 0.35) * perceptualRoughness);
516
517 // For thin surfaces, the light will bounce off at the second interface in the direction of
518 // the reflection, effectively adding to the specular, but this process will repeat itself.
519 // Each time the ray exits the surface on the front side after the first bounce,
520 // it's multiplied by E^2, and we get: E + E(1-E)^2 + E^3(1-E)^2 + ...
521 // This infinite series converges and is easy to simplify.
522 // Note: we calculate these bounces only on a single component,
523 // since it's a fairly subtle effect.
524 E *= 1.0 + pixel.transmission * (1.0 - E.g) / (1.0 + E.g);
525 #endif
526
527 /* sample the cubemap or screen-space */
528 #if REFRACTION_MODE == REFRACTION_MODE_CUBEMAP
529 // when reading from the cubemap, we are not pre-exposed so we apply iblLuminance
530 // which is not the case when we'll read from the screen-space buffer
531 vec3 Ft = prefilteredRadiance(ray.direction, perceptualRoughness) * frameUniforms.iblLuminance;
532 #else
533 // compute the point where the ray exits the medium, if needed
534 vec4 p = vec4(frameUniforms.clipFromWorldMatrix * vec4(ray.position, 1.0));
535 p.xy = uvToRenderTargetUV(p.xy * (0.5 / p.w) + 0.5);
536
537 // perceptualRoughness to LOD
538 // Empirical factor to compensate for the gaussian approximation of Dggx, chosen so
539 // cubemap and screen-space modes match at perceptualRoughness 0.125
540 float tweakedPerceptualRoughness = perceptualRoughness * 1.74;
541 float lod = max(0.0, 2.0 * log2(tweakedPerceptualRoughness) + frameUniforms.refractionLodOffset);
542
543 vec3 Ft = textureLod(light_ssr, p.xy, lod).rgb;
544 #endif
545
546 /* fresnel from the first interface */
547 Ft *= 1.0 - E;
548
549 /* apply absorption */
550 #if defined(MATERIAL_HAS_ABSORPTION)
551 Ft *= T;
552 #endif
553
554 Fr *= frameUniforms.iblLuminance;
555 Fd *= frameUniforms.iblLuminance;
556 color.rgb += Fr + mix(Fd, Ft, pixel.transmission);
557 }
558 #endif
559
560 void combineDiffuseAndSpecular(const PixelParams pixel,
561 const vec3 n, const vec3 E, const vec3 Fd, const vec3 Fr,
562 inout vec3 color) {
563 #if defined(HAS_REFRACTION)
564 applyRefraction(pixel, n, E, Fd, Fr, color);
565 #else
566 color.rgb += (Fd + Fr) * frameUniforms.iblLuminance;
567 #endif
568 }
569
570 void evaluateIBL(const MaterialInputs material, const PixelParams pixel, inout vec3 color) {
571 float ssao = evaluateSSAO();
572 float diffuseAO = min(material.ambientOcclusion, ssao);
573 float specularAO = computeSpecularAO(shading_NoV, diffuseAO, pixel.roughness);
574
575 // specular layer
576 vec3 Fr;
577 #if IBL_INTEGRATION == IBL_INTEGRATION_PREFILTERED_CUBEMAP
578 vec3 E = specularDFG(pixel);
579 vec3 r = getReflectedVector(pixel, shading_normal);
580 Fr = E * prefilteredRadiance(r, pixel.perceptualRoughness);
581 #elif IBL_INTEGRATION == IBL_INTEGRATION_IMPORTANCE_SAMPLING
582 vec3 E = vec3(0.0); // TODO: fix for importance sampling
583 Fr = isEvaluateSpecularIBL(pixel, shading_normal, shading_view, shading_NoV);
584 #endif
585 Fr *= singleBounceAO(specularAO) * pixel.energyCompensation;
586
587 // diffuse layer
588 float diffuseBRDF = singleBounceAO(diffuseAO); // Fd_Lambert() is baked in the SH below
589 evaluateClothIndirectDiffuseBRDF(pixel, diffuseBRDF);
590
591 #if defined(MATERIAL_HAS_BENT_NORMAL)
592 vec3 diffuseNormal = shading_bentNormal;
593 #else
594 vec3 diffuseNormal = shading_normal;
595 #endif
596
597 #if IBL_INTEGRATION == IBL_INTEGRATION_PREFILTERED_CUBEMAP
598 vec3 diffuseIrradiance = diffuseIrradiance(diffuseNormal);
599 #elif IBL_INTEGRATION == IBL_INTEGRATION_IMPORTANCE_SAMPLING
600 vec3 diffuseIrradiance = isEvaluateDiffuseIBL(pixel, diffuseNormal, shading_view);
601 #endif
602 vec3 Fd = pixel.diffuseColor * diffuseIrradiance * (1.0 - E) * diffuseBRDF;
603
604 // sheen layer
605 evaluateSheenIBL(pixel, specularAO, Fd, Fr);
606
607 // clear coat layer
608 evaluateClearCoatIBL(pixel, specularAO, Fd, Fr);
609
610 // subsurface layer
611 evaluateSubsurfaceIBL(pixel, diffuseIrradiance, Fd, Fr);
612
613 // extra ambient occlusion term
614 multiBounceAO(diffuseAO, pixel.diffuseColor, Fd);
615 multiBounceSpecularAO(specularAO, pixel.f0, Fr);
616
617 // Note: iblLuminance is already premultiplied by the exposure
618 combineDiffuseAndSpecular(pixel, shading_normal, E, Fd, Fr, color);
619 }