mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
craft-pbr: GGX D is 0 where n·h is 0 or less
Section B.3.2 writes D with the factor chi+(n·h). ggx_distribution and ggx_distribution_between squared n·h without it, so a half vector below the surface, such as h = -n, gave the peak 1 / (pi alpha²). Both now return 0 where n·h is 0 or less. A test checks both functions at n·h of 0, -0.3 and -1 and at perpendicular and back-facing half vectors.
This commit is contained in:
1 parent
9ae6324d9b
commit
dc829475eb
2 files changed
+24
-5
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user