From e4b46d6a337f0653f5101cec306dcebe2edeb157 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 09:17:58 -0800 Subject: [PATCH] =?UTF-8?q?craft-pbr:=20keep=20n=C2=B7h=20at=20most=201=20?= =?UTF-8?q?in=20pbr=5Fdirect,=20as=20try=5Fdirect=20does?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pbr_direct now takes n·h as clamp(dot(n, h), 0, 1), matching try_direct. With n·h slightly over 1 after rounding, the GGX inner term shrinks and D rises above its peak. Its comment also names the 1e-4 floor of the specular denominator. The README's pbr_direct row lists the floors and the differences from try_direct, and the changes from EffectCraft's shader say that it clamped roughness to 0.03 to 1 and read the other material values unchecked. --- crates/pbr/README.md | 10 ++++++---- crates/pbr/src/shade.wgsl | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/pbr/README.md b/crates/pbr/README.md index 8b760c85eb..1cf69ebd20 100644 --- a/crates/pbr/README.md +++ b/crates/pbr/README.md @@ -129,7 +129,7 @@ The crate does not depend on wgpu or naga in its build. |------|------|-------| | `Material::try_new` | `pbr_material_clamped` | Clamps to the ranges the Rust constructors check, including `try_with_weights`. | | `Material::f0` | `pbr_f0` | Same formula. | -| `Material::try_direct` | `pbr_direct` | Same terms, floors and zero cases; a zero-length direction gives 0 instead of an error. | +| `Material::try_direct` | `pbr_direct` | Same floors: roughness at least 0.032, `n·v` at least `1e-4`, `n·h` at most 1 and `4 (n·v) (n·l)` at least `1e-4`. A light at or below the surface or a view straight opposite the light gives 0 in both. A zero-length direction gives 0 instead of an error, material values are clamped instead of checked, and `D` is evaluated in `f32` by `pbr_ggx_distribution`. | | `fresnel_schlick` | `pbr_fresnel_schlick` | Same formula. | | `smith_schlick_ggx` | `pbr_smith_schlick_ggx` | Same formula. | | `ggx_distribution` | `pbr_ggx_distribution` | Same `α` floor, in `f32`; `(n·h)² (α² − 1) + 1` is computed as `(1 − n·h)(1 + n·h) + (n·h)² α²`. | @@ -149,10 +149,12 @@ The functions come from EffectCraft's `crates/gpu/src/shaders/advanced3d.wgsl` ( `f4d45dfe`), which ports `shade.rs`. Changes from that shader: - The direct roughness floor is 0.032 instead of 0.03, a view straight opposite the light gives 0 - instead of a fallback half vector, and the environment BRDF keeps `scale` and `bias` at least 0 - with a sum of at most 1, as in the Rust functions (see Origin). + instead of a fallback half vector, `n·h` is taken as at most 1 as well as at least 0, and the + environment BRDF keeps `scale` and `bias` at least 0 with a sum of at most 1, as in the Rust + functions (see Origin). - A zero-length direction gives 0. EffectCraft's `norm3` replaced it with `(0, 0, −1)`. -- Material values are clamped to the Rust ranges. EffectCraft read them unchecked. +- Material values are clamped to the Rust ranges. EffectCraft clamped roughness to 0.03 to 1 and + read base color, metallic, specular and diffuse unchecked. - `D` computes its inner term as `(1 − n·h)(1 + n·h) + (n·h)² α²` with an `α` floor of 0.001 and a denominator of at least `f32::MIN_POSITIVE`. EffectCraft computed `(n·h)² (α² − 1) + 1`, which loses precision in `f32` near `n·h = 1` for small `α`, with a denominator of at least diff --git a/crates/pbr/src/shade.wgsl b/crates/pbr/src/shade.wgsl index aa27e0cfaa..01a3a20a92 100644 --- a/crates/pbr/src/shade.wgsl +++ b/crates/pbr/src/shade.wgsl @@ -94,7 +94,8 @@ fn pbr_smith_schlick_ggx(n_dot_v: f32, n_dot_l: f32, roughness: f32) -> f32 { // normal, the direction to the viewer and the direction to the light; they are normalized here. // The material is clamped with pbr_material_clamped and roughness is shaded as at least // PBR_MIN_DIRECT_ROUGHNESS. A light at or below the surface, a view straight opposite the light -// and a zero-length direction give 0. n·v is taken as at least 1e-4. +// and a zero-length direction give 0. n·v is taken as at least 1e-4, n·h as at most 1, and the +// specular denominator 4 (n·v) (n·l) as at least 1e-4. fn pbr_direct(material: PbrMaterial, n_in: vec3, v_in: vec3, l_in: vec3) -> vec3 { let m = pbr_material_clamped(material); let n = pbr_unit(n_in); @@ -109,7 +110,7 @@ fn pbr_direct(material: PbrMaterial, n_in: vec3, v_in: vec3, l_in: vec return vec3(0.0); } let n_v = max(dot(n, v), PBR_MIN_N_DOT_V); - let n_h = max(dot(n, h), 0.0); + let n_h = clamp(dot(n, h), 0.0, 1.0); let v_h = max(dot(v, h), 0.0); let r = max(m.roughness, PBR_MIN_DIRECT_ROUGHNESS); let f = pbr_fresnel_schlick(pbr_f0(m), v_h);