diff --git a/crates/pbr/src/prefilter.rs b/crates/pbr/src/prefilter.rs index 500ba292fb..34975409be 100644 --- a/crates/pbr/src/prefilter.rs +++ b/crates/pbr/src/prefilter.rs @@ -55,10 +55,14 @@ pub fn hammersley(i: u32, n: u32) -> Option<[f32; 2]> { } /// The GGX normal distribution `D` at `n_dot_h` for perceptual `roughness`, with -/// `α = max(roughness², 0.001)`: `α² / (π ((n·h)² (α² − 1) + 1)²)`, as glTF 2.0 Appendix B writes -/// it in section B.3.2. Over the hemisphere, `D(h) (n·h)` integrates to 1. +/// `α = max(roughness², 0.001)`: `α² χ⁺(n·h) / (π ((n·h)² (α² − 1) + 1)²)`, as glTF 2.0 +/// Appendix B writes it in section B.3.2, so it is 0 where `n·h` is 0 or less. Over the +/// hemisphere, `D(h) (n·h)` integrates to 1. /// It is evaluated in `f64`, so it keeps its peak `1 / (π α²)` at `n·h = 1` for the smallest `α`. pub fn ggx_distribution(n_dot_h: f32, roughness: f32) -> f32 { + if n_dot_h <= 0.0 { + return 0.0; + } let a = f64::from((roughness * roughness).max(1e-3)); let c2 = f64::from(n_dot_h) * f64::from(n_dot_h); let a2 = a * a; @@ -70,6 +74,7 @@ pub fn ggx_distribution(n_dot_h: f32, roughness: f32) -> f32 { /// section B.3.2 for unit vectors, with `(n·h)²` and `|n × h|²` taken from the vectors in `f64`. /// Near `n·h = 1` this is exact, where an `n·h` rounded to `f32` and passed to /// [`ggx_distribution`] can be one step below 1 and give a `D` about 20% low at the smallest `α`. +/// It is 0 where `n·h` is 0 or less. /// /// Returns [`PbrError::BadOption`] when `roughness` is not in `0..=1`, and /// [`PbrError::BadDirection`] when a vector is not finite or has zero length. @@ -85,6 +90,9 @@ pub fn ggx_distribution_between(n: [f32; 3], h: [f32; 3], roughness: f32) -> Res return Err(PbrError::BadDirection); } let c = dot(n, h); + if c <= 0.0 { + return Ok(0.0); + } let x = [n[1] * h[2] - n[2] * h[1], n[2] * h[0] - n[0] * h[2], n[0] * h[1] - n[1] * h[0]]; let a = f64::from((roughness * roughness).max(1e-3)); let a2 = a * a; diff --git a/crates/pbr/tests/it/numeric.rs b/crates/pbr/tests/it/numeric.rs index c0dbed614d..fe3623ed56 100644 --- a/crates/pbr/tests/it/numeric.rs +++ b/crates/pbr/tests/it/numeric.rs @@ -2,8 +2,7 @@ //! kept, and the SH projection matches analytic irradiance. use craft_pbr::{ - EnvImage, Environment, Limits, PbrError, PrefilterOptions, dir_of, ggx_distribution, ggx_distribution_between, irradiance, try_prefilter, - try_sh9, + EnvImage, Environment, Limits, PbrError, PrefilterOptions, dir_of, ggx_distribution, ggx_distribution_between, irradiance, try_prefilter, try_sh9, }; use crate::{energy, env_from, max_rel_diff, opts}; @@ -64,6 +63,18 @@ fn ggx_projected_area_integrates_to_one() { } } +#[test] +fn ggx_is_zero_where_the_half_vector_is_not_above_the_surface() { + for roughness in [0.0, 0.5, 1.0] { + for n_dot_h in [0.0, -0.3, -1.0] { + assert_eq!(ggx_distribution(n_dot_h, roughness), 0.0, "roughness {roughness} at {n_dot_h}"); + } + for h in [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.6, -0.8, 0.0]] { + assert_eq!(ggx_distribution_between([0.0, 1.0, 0.0], h, roughness), Ok(0.0), "roughness {roughness} at {h:?}"); + } + } +} + /// `v` normalized in `f32`, divided by its largest component and then multiplied by the /// reciprocal of its length, as the crate normalizes directions. fn unit_f32(v: [f32; 3]) -> [f32; 3] { @@ -85,7 +96,7 @@ fn ggx_between_vectors_keeps_its_peak_off_axis() { assert!((d / peak - 1.0).abs() < 1e-5, "{n:?} {h:?}: {d} for {peak}"); } // Away from the peak it is the scalar form at the same angle. - for (roughness, angle) in [(0.0f32, 0.05f32), (0.3, 0.4), (1.0, 1.2), (0.5, std::f32::consts::FRAC_PI_2)] { + for (roughness, angle) in [(0.0f32, 0.05f32), (0.3, 0.4), (1.0, 1.2), (0.5, 1.5)] { let h = [angle.sin(), 0.0, angle.cos()]; let d = ggx_distribution_between([0.0, 0.0, 1.0], h, roughness).unwrap(); let want = ggx_distribution(angle.cos(), roughness);