Debian Codegraph @

shaders/src/shading_model_standard.fs

raw
1 #if defined(MATERIAL_HAS_SHEEN_COLOR)
2 vec3 sheenLobe(const PixelParams pixel, float NoV, float NoL, float NoH) {
3 float D = distributionCloth(pixel.sheenRoughness, NoH);
4 float V = visibilityCloth(NoV, NoL);
5
6 return (D * V) * pixel.sheenColor;
7 }
8 #endif
9
10 #if defined(MATERIAL_HAS_CLEAR_COAT)
11 float clearCoatLobe(const PixelParams pixel, const vec3 h, float NoH, float LoH, out float Fcc) {
12 #if defined(MATERIAL_HAS_NORMAL) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL)
13 // If the material has a normal map, we want to use the geometric normal
14 // instead to avoid applying the normal map details to the clear coat layer
15 float clearCoatNoH = saturate(dot(shading_clearCoatNormal, h));
16 #else
17 float clearCoatNoH = NoH;
18 #endif
19
20 // clear coat specular lobe
21 float D = distributionClearCoat(pixel.clearCoatRoughness, clearCoatNoH, h);
22 float V = visibilityClearCoat(LoH);
23 float F = F_Schlick(0.04, 1.0, LoH) * pixel.clearCoat; // fix IOR to 1.5
24
25 Fcc = F;
26 return D * V * F;
27 }
28 #endif
29
30 #if defined(MATERIAL_HAS_ANISOTROPY)
31 vec3 anisotropicLobe(const PixelParams pixel, const Light light, const vec3 h,
32 float NoV, float NoL, float NoH, float LoH) {
33
34 vec3 l = light.l;
35 vec3 t = pixel.anisotropicT;
36 vec3 b = pixel.anisotropicB;
37 vec3 v = shading_view;
38
39 float ToV = dot(t, v);
40 float BoV = dot(b, v);
41 float ToL = dot(t, l);
42 float BoL = dot(b, l);
43 float ToH = dot(t, h);
44 float BoH = dot(b, h);
45
46 // Anisotropic parameters: at and ab are the roughness along the tangent and bitangent
47 // to simplify materials, we derive them from a single roughness parameter
48 // Kulla 2017, "Revisiting Physically Based Shading at Imageworks"
49 float at = max(pixel.roughness * (1.0 + pixel.anisotropy), MIN_ROUGHNESS);
50 float ab = max(pixel.roughness * (1.0 - pixel.anisotropy), MIN_ROUGHNESS);
51
52 // specular anisotropic BRDF
53 float D = distributionAnisotropic(at, ab, ToH, BoH, NoH);
54 float V = visibilityAnisotropic(pixel.roughness, at, ab, ToV, BoV, ToL, BoL, NoV, NoL);
55 vec3 F = fresnel(pixel.f0, LoH);
56
57 return (D * V) * F;
58 }
59 #endif
60
61 vec3 isotropicLobe(const PixelParams pixel, const Light light, const vec3 h,
62 float NoV, float NoL, float NoH, float LoH) {
63
64 float D = distribution(pixel.roughness, NoH, h);
65 float V = visibility(pixel.roughness, NoV, NoL);
66 vec3 F = fresnel(pixel.f0, LoH);
67
68 return (D * V) * F;
69 }
70
71 vec3 specularLobe(const PixelParams pixel, const Light light, const vec3 h,
72 float NoV, float NoL, float NoH, float LoH) {
73 #if defined(MATERIAL_HAS_ANISOTROPY)
74 return anisotropicLobe(pixel, light, h, NoV, NoL, NoH, LoH);
75 #else
76 return isotropicLobe(pixel, light, h, NoV, NoL, NoH, LoH);
77 #endif
78 }
79
80 vec3 diffuseLobe(const PixelParams pixel, float NoV, float NoL, float LoH) {
81 return pixel.diffuseColor * diffuse(pixel.roughness, NoV, NoL, LoH);
82 }
83
84 /**
85 * Evaluates lit materials with the standard shading model. This model comprises
86 * of 2 BRDFs: an optional clear coat BRDF, and a regular surface BRDF.
87 *
88 * Surface BRDF
89 * The surface BRDF uses a diffuse lobe and a specular lobe to render both
90 * dielectrics and conductors. The specular lobe is based on the Cook-Torrance
91 * micro-facet model (see brdf.fs for more details). In addition, the specular
92 * can be either isotropic or anisotropic.
93 *
94 * Clear coat BRDF
95 * The clear coat BRDF simulates a transparent, absorbing dielectric layer on
96 * top of the surface. Its IOR is set to 1.5 (polyutherane) to simplify
97 * our computations. This BRDF only contains a specular lobe and while based
98 * on the Cook-Torrance microfacet model, it uses cheaper terms than the surface
99 * BRDF's specular lobe (see brdf.fs).
100 */
101 vec3 surfaceShading(const PixelParams pixel, const Light light, float occlusion) {
102 vec3 h = normalize(shading_view + light.l);
103
104 float NoV = shading_NoV;
105 float NoL = saturate(light.NoL);
106 float NoH = saturate(dot(shading_normal, h));
107 float LoH = saturate(dot(light.l, h));
108
109 vec3 Fr = specularLobe(pixel, light, h, NoV, NoL, NoH, LoH);
110 vec3 Fd = diffuseLobe(pixel, NoV, NoL, LoH);
111 #if defined(HAS_REFRACTION)
112 Fd *= (1.0 - pixel.transmission);
113 #endif
114
115 // TODO: attenuate the diffuse lobe to avoid energy gain
116
117 // The energy compensation term is used to counteract the darkening effect
118 // at high roughness
119 vec3 color = Fd + Fr * pixel.energyCompensation;
120
121 #if defined(MATERIAL_HAS_SHEEN_COLOR)
122 color *= pixel.sheenScaling;
123 color += sheenLobe(pixel, NoV, NoL, NoH);
124 #endif
125
126 #if defined(MATERIAL_HAS_CLEAR_COAT)
127 float Fcc;
128 float clearCoat = clearCoatLobe(pixel, h, NoH, LoH, Fcc);
129 float attenuation = 1.0 - Fcc;
130
131 #if defined(MATERIAL_HAS_NORMAL) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL)
132 color *= attenuation * NoL;
133
134 // If the material has a normal map, we want to use the geometric normal
135 // instead to avoid applying the normal map details to the clear coat layer
136 float clearCoatNoL = saturate(dot(shading_clearCoatNormal, light.l));
137 color += clearCoat * clearCoatNoL;
138
139 // Early exit to avoid the extra multiplication by NoL
140 return (color * light.colorIntensity.rgb) *
141 (light.colorIntensity.w * light.attenuation * occlusion);
142 #else
143 color *= attenuation;
144 color += clearCoat;
145 #endif
146 #endif
147
148 return (color * light.colorIntensity.rgb) *
149 (light.colorIntensity.w * light.attenuation * NoL * occlusion);
150 }