diff --git a/crates/pbr/README.md b/crates/pbr/README.md index 511641038a..8b760c85eb 100644 --- a/crates/pbr/README.md +++ b/crates/pbr/README.md @@ -57,7 +57,11 @@ It has no UI, does no file or network I/O and has no `unsafe` code. angles. - Roughness is shaded as at least `MIN_DIRECT_ROUGHNESS` (0.032, `α` = 0.001024). - `n·v` is taken as at least `1e-4`, so a view at or below the surface sees it as at a - grazing angle. A light at or below the surface, or a view straight opposite the light, gives + grazing angle. `n·h` is taken as at most 1, so rounding cannot raise `D` above its peak. + - The specular denominator `4 (n·v) (n·l)` is taken as at least `1e-4`, as in EffectCraft. + Where the product is smaller, at grazing angles, the specular lobe is below + `F D G / (4 (n·v) (n·l))`: about 4% of it with `n·v` and `n·l` both 0.001. + - A light at or below the surface, or a view straight opposite the light, gives 0. Directions are normalized; one that is not finite or has zero length gives `PbrError::BadDirection`. - A white metal reflects at most the light it receives. As section B.3.6.3 notes, the Fresnel @@ -196,6 +200,8 @@ The BRDF, the environment BRDF and the default environment come from EffectCraft - Roughness is shaded as at least 0.032 instead of 0.03, so `α` stays above the 0.001 floor of `ggx_distribution`. - A view straight opposite the light gives 0 instead of using a fallback half vector. +- `n·h` is taken as at most 1. EffectCraft took it only as at least 0, so rounding after + normalization could put `D` above its peak. - The environment BRDF keeps `scale` and `bias` at least 0 with a sum of at most 1. The fit gives a negative scale for smooth surfaces at grazing angles (-0.044 with a bias of 1.044 at `n·v` = 0 and roughness 0), which made `f0 × scale + bias` greater than 1 and the diffuse diff --git a/crates/pbr/src/brdf.rs b/crates/pbr/src/brdf.rs index be2547268f..6aa059853f 100644 --- a/crates/pbr/src/brdf.rs +++ b/crates/pbr/src/brdf.rs @@ -107,7 +107,9 @@ impl Material { /// lights, where section B.3.2 writes the separable Smith form. Roughness is shaded as at /// least [`MIN_DIRECT_ROUGHNESS`]. A light at or below the surface gives 0, and so does a /// view straight opposite the light, which has no half vector. `n·v` is taken as at least - /// `1e-4`, so a view at or below the surface sees the surface as at a grazing angle. + /// `1e-4`, so a view at or below the surface sees the surface as at a grazing angle. `n·h` is + /// taken as at most 1. The specular denominator `4 (n·v) (n·l)` is taken as at least `1e-4`, + /// as in EffectCraft, so where it is smaller the specular lobe is below the expression above. /// /// Returns [`PbrError::BadDirection`] when a direction is not finite or has zero length. pub fn try_direct(&self, n: [f32; 3], v: [f32; 3], l: [f32; 3]) -> Result<[f32; 3], PbrError> { @@ -120,7 +122,7 @@ impl Material { } let Some(h) = unit((v + l).to_array()) else { return Ok([0.0; 3]) }; let n_v = n.dot(v).max(MIN_N_DOT_V); - let n_h = n.dot(h).max(0.0); + let n_h = n.dot(h).clamp(0.0, 1.0); let v_h = v.dot(h).max(0.0); let r = self.roughness.max(MIN_DIRECT_ROUGHNESS); let f = fresnel_schlick(self.f0(), v_h); diff --git a/crates/pbr/tests/it/brdf.rs b/crates/pbr/tests/it/brdf.rs index 6c3f71fdf9..f7bac7f27c 100644 --- a/crates/pbr/tests/it/brdf.rs +++ b/crates/pbr/tests/it/brdf.rs @@ -362,6 +362,37 @@ fn roughness_is_clamped_at_the_documented_floor() { assert!(at(MIN_DIRECT_ROUGHNESS + 0.01)[0] < floor[0], "a rougher surface has a lower peak"); } +/// `n·h` is taken as at most 1, so when rounding after normalization gives a dot product slightly +/// over 1, the highlight is not above the head-on peak. +#[test] +fn the_highlight_is_never_above_the_head_on_peak() { + let m = material([1.0; 3], 1.0, 0.0); + let head_on = m.try_direct([0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0]).unwrap()[0]; + for d in [[1.0, 1.0, 0.5], [0.3, -0.7, 0.2], [2.0, 3.0, 7.0], [-1.0, 0.1, 0.9], [0.6, 0.6, 0.6]] { + let got = m.try_direct(d, d, d).unwrap()[0]; + assert!(got <= head_on * (1.0 + 1e-4), "{d:?}: {got} vs {head_on}"); + } +} + +/// The specular lobe divides by `4 (n·v) (n·l)` taken as at least `1e-4`, as EffectCraft does, +/// so at grazing angles it is lower than the expression without that floor. +#[test] +fn the_specular_denominator_is_floored_at_grazing_angles() { + let n = [0.0, 0.0, 1.0]; + let v = norm([1.0, 0.0, 0.001]); + let r = 0.5f32; + // A white metal: F is 1 and the diffuse lobe is 0. + let got = material([1.0; 3], 1.0, r).try_direct(n, v, v).unwrap()[0]; + let nv = dot(n, v); + let k = (r + 1.0).powi(2) / 8.0; + let g = (nv / (nv * (1.0 - k) + k)).powi(2); + let d = ggx_distribution(nv, r); + let floored = d * g / (4.0 * nv * nv).max(1e-4) * nv * PI; + let unfloored = d * g / (4.0 * nv * nv) * nv * PI; + assert!((got - floored).abs() <= 1e-3 * floored, "{got} vs {floored}"); + assert!(got < 0.05 * unfloored, "{got} vs {unfloored}"); +} + #[test] fn the_environment_brdf_keeps_reflectance_between_0_and_1() { for i in 0..=60 {