diff options
| author | yum <yum.food.vr@gmail.com> | 2025-02-28 15:17:43 -0800 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2025-02-28 15:17:43 -0800 |
| commit | eb1529d4401cc4d385ef8bc806a186890999fed6 (patch) | |
| tree | f73f77b9f6b2b676f80d3071c591b53985655018 /yum_brdf.cginc | |
| parent | 646482b01a552fddfff33e31aeb9275644d08336 (diff) | |
add basic clearcoat lobe
Diffstat (limited to 'yum_brdf.cginc')
| -rw-r--r-- | yum_brdf.cginc | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/yum_brdf.cginc b/yum_brdf.cginc index 52bc495..ab1730b 100644 --- a/yum_brdf.cginc +++ b/yum_brdf.cginc @@ -44,6 +44,22 @@ float3 specularLobe(YumPbr pbr, float f0, return (D * V) * F * PI;
}
+#if defined(_CLEARCOAT)
+// Add a clearcoat specular lobe function
+float3 clearcoatLobe(float roughness, float f0,
+ float3 h, float LoH, float NoH_mesh, float NoV_mesh, float NoL_mesh)
+{
+ const float F = F_Schlick(f0, LoH);
+
+ // Normal distribution function with clearcoat roughness
+ float D = D_GGX(roughness, NoH_mesh, h);
+ // Geometric shadowing
+ float V = V_SmithGGXCorrelated_Fast(roughness, NoV_mesh, NoL_mesh);
+
+ return (D * V) * F * PI;
+}
+#endif // _CLEARCOAT
+
float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) {
const float3 h = normalize(light.view_dir + light.dir);
const float LoH = saturate(dot(light.dir, h));
@@ -73,13 +89,49 @@ float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) { const float3 E = specularDFG(dfg, f0);
const float3 energy_compensation = energyCompensation(dfg, f0);
+#if defined(_CLEARCOAT)
+ // Clearcoat parameters
+ const float clearcoat_strength = _Clearcoat_Strength;
+ const float clearcoat_roughness = max(0.089, _Clearcoat_Roughness);
+ const float clearcoat_perceptual_roughness = sqrt(clearcoat_roughness);
+
+ // IoR of 1.5 -> reflectance of 0.04
+ const float clearcoat_f0 = 0.04;
+
+ // Use cc normal for clearcoat instead of detail normal
+ const float3 cc_normal = i.normal;
+ const float NoV_cc = max(1E-4, dot(cc_normal, light.view_dir));
+ const float NoH_cc = saturate(dot(cc_normal, h));
+ const float NoL_cc = saturate(dot(cc_normal, light.dir));
+#if defined(_WRAPPED_LIGHTING)
+ const float NoL_cc_wrapped = saturate((NoL_cc + light.wrapped) / (1.0 + light.wrapped));
+#else
+ const float NoL_cc_wrapped = NoL_cc;
+#endif
+
+ // Calculate clearcoat DFG terms with cc normal
+ const float3 clearcoat_dfg = PrefilteredDFG_LUT(clearcoat_perceptual_roughness, NoV_cc);
+ const float clearcoat_E = specularDFG(clearcoat_dfg, clearcoat_f0);
+ const float clearcoat_energy_compensation = energyCompensation(clearcoat_dfg, clearcoat_f0);
+#endif // _CLEARCOAT
+
float3 direct;
{
+ // Base layer
float3 Fd = diffuseLobe(pbr.albedo, pbr.roughness_perceptual, LoH, NoL,
NoV, f90);
Fd *= (1.0 - pbr.metallic) * light.attenuation;
float3 Fr = specularLobe(pbr, f0, h, LoH, NoH, NoV, NoL_wrapped_s);
+
+#if defined(_CLEARCOAT)
+ float Fcr = clearcoatLobe(clearcoat_roughness, clearcoat_f0, h, LoH, NoH_cc, NoV_cc, NoL_cc_wrapped);
+ Fcr *= clearcoat_strength * clearcoat_energy_compensation;
+ float clearcoat_factor = 1.0 - clearcoat_strength * F_Schlick(clearcoat_f0, NoV_cc);
+ float3 color = (Fd * NoL_wrapped_d + Fr * energy_compensation * NoL_wrapped_s) * clearcoat_factor +
+ Fcr * NoL_cc_wrapped;
+#else
float3 color = Fd * NoL_wrapped_d + Fr * energy_compensation * NoL_wrapped_s;
+#endif // _CLEARCOAT
direct = color * light.direct;
}
@@ -88,7 +140,14 @@ float4 YumBRDF(v2f i, const YumLighting light, YumPbr pbr) { {
float3 Fr = E * light.specular * energy_compensation;
float3 Fd = pbr.albedo * light.diffuse * (1.0 - E) * (1.0 - pbr.metallic) * pbr.ao;
+
+#if defined(_CLEARCOAT)
+ float Fcr = clearcoat_E * light.specular * clearcoat_energy_compensation * clearcoat_strength;
+ float clearcoat_factor = 1.0 - clearcoat_strength * F_Schlick(clearcoat_f0, NoV_cc);
+ indirect = (Fr + Fd) * clearcoat_factor + Fcr;
+#else
indirect = Fr + Fd;
+#endif // _CLEARCOAT
}
return float4(direct + indirect, pbr.albedo.a);
|
