Debian Codegraph @

shaders/src/shading_model_subsurface.fs

raw
1 /**
2 * Evalutes lit materials with the subsurface shading model. This model is a
3 * combination of a BRDF (the same used in shading_model_standard.fs, refer to that
4 * file for more information) and of an approximated BTDF to simulate subsurface
5 * scattering. The BTDF itself is not physically based and does not represent a
6 * correct interpretation of transmission events.
7 */
8 vec3 surfaceShading(const PixelParams pixel, const Light light, float occlusion) {
9 vec3 h = normalize(shading_view + light.l);
10
11 float NoL = light.NoL;
12 float NoH = saturate(dot(shading_normal, h));
13 float LoH = saturate(dot(light.l, h));
14
15 vec3 Fr = vec3(0.0);
16 if (NoL > 0.0) {
17 // specular BRDF
18 float D = distribution(pixel.roughness, NoH, h);
19 float V = visibility(pixel.roughness, shading_NoV, NoL);
20 vec3 F = fresnel(pixel.f0, LoH);
21 Fr = (D * V) * F * pixel.energyCompensation;
22 }
23
24 // diffuse BRDF
25 vec3 Fd = pixel.diffuseColor * diffuse(pixel.roughness, shading_NoV, NoL, LoH);
26
27 // NoL does not apply to transmitted light
28 vec3 color = (Fd + Fr) * (NoL * occlusion);
29
30 // subsurface scattering
31 // Use a spherical gaussian approximation of pow() for forwardScattering
32 // We could include distortion by adding shading_normal * distortion to light.l
33 float scatterVoH = saturate(dot(shading_view, -light.l));
34 float forwardScatter = exp2(scatterVoH * pixel.subsurfacePower - pixel.subsurfacePower);
35 float backScatter = saturate(NoL * pixel.thickness + (1.0 - pixel.thickness)) * 0.5;
36 float subsurface = mix(backScatter, 1.0, forwardScatter) * (1.0 - pixel.thickness);
37 color += pixel.subsurfaceColor * (subsurface * Fd_Lambert());
38
39 // TODO: apply occlusion to the transmitted light
40 return (color * light.colorIntensity.rgb) * (light.colorIntensity.w * light.attenuation);
41 }