Debian Codegraph @

shaders/src/shading_model_cloth.fs

raw
1 /**
2 * Evaluates lit materials with the cloth shading model. Similar to the standard
3 * model, the cloth shading model is based on a Cook-Torrance microfacet model.
4 * Its distribution and visibility terms are however very different to take into
5 * account the softer apperance of many types of cloth. Some highly reflecting
6 * fabrics like satin or leather should use the standard model instead.
7 *
8 * This shading model optionally models subsurface scattering events. The
9 * computation of these events is not physically based but can add necessary
10 * details to a material.
11 */
12 vec3 surfaceShading(const PixelParams pixel, const Light light, float occlusion) {
13 vec3 h = normalize(shading_view + light.l);
14 float NoL = light.NoL;
15 float NoH = saturate(dot(shading_normal, h));
16 float LoH = saturate(dot(light.l, h));
17
18 // specular BRDF
19 float D = distributionCloth(pixel.roughness, NoH);
20 float V = visibilityCloth(shading_NoV, NoL);
21 vec3 F = pixel.f0;
22 // Ignore pixel.energyCompensation since we use a different BRDF here
23 vec3 Fr = (D * V) * F;
24
25 // diffuse BRDF
26 float diffuse = diffuse(pixel.roughness, shading_NoV, NoL, LoH);
27 #if defined(MATERIAL_HAS_SUBSURFACE_COLOR)
28 // Energy conservative wrap diffuse to simulate subsurface scattering
29 diffuse *= Fd_Wrap(dot(shading_normal, light.l), 0.5);
30 #endif
31
32 // We do not multiply the diffuse term by the Fresnel term as discussed in
33 // Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
34 // The effect is fairly subtle and not deemed worth the cost for mobile
35 vec3 Fd = diffuse * pixel.diffuseColor;
36
37 #if defined(MATERIAL_HAS_SUBSURFACE_COLOR)
38 // Cheap subsurface scatter
39 Fd *= saturate(pixel.subsurfaceColor + NoL);
40 // We need to apply NoL separately to the specular lobe since we already took
41 // it into account in the diffuse lobe
42 vec3 color = Fd + Fr * NoL;
43 color *= light.colorIntensity.rgb * (light.colorIntensity.w * light.attenuation * occlusion);
44 #else
45 vec3 color = Fd + Fr;
46 color *= light.colorIntensity.rgb * (light.colorIntensity.w * light.attenuation * NoL * occlusion);
47 #endif
48
49 return color;
50 }