From 8c6fcf64f8318568430a0011590c5d8a952ce3bb Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Thu, 5 Nov 2026 07:16:49 -0500 Subject: [PATCH] fix(nodes): scale the coarse levels of Normal to Height correctly The multigrid solve divided the slope divergence by the level's scale where it must multiply (one coarse pixel spans `scale` full-size pixels), so the coarse levels contributed almost nothing and the broad shape was lost. A Gaussian bump round-tripped through Height to Normal now correlates 0.99+ at 256 and 1024 px (0.86 at 256 before). Co-Authored-By: Claude Sonnet 5.5 --- crates/nodes/src/filters.rs | 9 ++++-- crates/nodes/src/filters.test.rs | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 crates/nodes/src/filters.test.rs diff --git a/crates/nodes/src/filters.rs b/crates/nodes/src/filters.rs index 84a6645e..b207513d 100644 --- a/crates/nodes/src/filters.rs +++ b/crates/nodes/src/filters.rs @@ -841,8 +841,9 @@ fn normal_to_height(ctx: &Ctx) -> CookResult { cur = Image::generate(sw, sh, Kind::Gray, |x, y| { let (x, y) = (x as i64, y as i64); let nb = prev.get(x - 1, y, true)[0] + prev.get(x + 1, y, true)[0] + prev.get(x, y - 1, true)[0] + prev.get(x, y + 1, true)[0]; - // Divergence of the gradient field (pixels of this level). - let div = (gxs.get(x + 1, y, true)[0] - gxs.get(x - 1, y, true)[0] + gys.get(x, y + 1, true)[0] - gys.get(x, y - 1, true)[0]) * 0.5 / scale; + // Divergence of the slope field in pixels of this level: the slopes are per + // full-size pixel and one pixel here spans `scale` of them, so multiply. + let div = (gxs.get(x + 1, y, true)[0] - gxs.get(x - 1, y, true)[0] + gys.get(x, y + 1, true)[0] - gys.get(x, y - 1, true)[0]) * 0.5 * scale; [(nb - div) * 0.25, 0.0, 0.0, 0.0] }); } @@ -949,3 +950,7 @@ fn skew(ctx: &Ctx) -> CookResult { if vert { img.sample(u, v + (u - 0.5) * k, ctx.tile) } else { img.sample(u + (v - 0.5) * k, v, ctx.tile) } })]) } + +#[cfg(test)] +#[path = "filters.test.rs"] +mod tests; diff --git a/crates/nodes/src/filters.test.rs b/crates/nodes/src/filters.test.rs new file mode 100644 index 00000000..9a0eafac --- /dev/null +++ b/crates/nodes/src/filters.test.rs @@ -0,0 +1,47 @@ +//! Round-trip test for Height to Normal -> Normal to Height on a smooth analytic bump. + +use std::collections::BTreeMap; + +use materialcraft_core::Value; +use materialcraft_image::{Image, Kind}; + +use crate::{Ctx, find, resolve_params}; + +fn run(id: &str, input: &Image, size: u32, over: &[(&str, Value)]) -> Image { + let def = find(id).unwrap(); + let over: BTreeMap = over.iter().map(|(k, v)| (k.to_string(), v.clone())).collect(); + let params = resolve_params(def, &over); + let no_res = |_: &str| None; + let vars = BTreeMap::new(); + let inputs = [Some(input.clone())]; + let ctx = Ctx { w: size, h: size, seed: 1, tile: true, inputs: &inputs, params: ¶ms, resource: &no_res, vars: &vars }; + (def.cook)(&ctx).unwrap().remove(0) +} + +fn pearson(a: &Image, b: &Image) -> f32 { + let n = (a.w * a.h) as usize; + let (ma, mb) = (a.data.iter().take(n).sum::() / n as f32, b.data.iter().take(n).sum::() / n as f32); + let (mut sab, mut saa, mut sbb) = (0.0f32, 0.0f32, 0.0f32); + for i in 0..n { + let (da, db) = (a.data[i] - ma, b.data[i] - mb); + sab += da * db; + saa += da * da; + sbb += db * db; + } + sab / (saa * sbb).sqrt().max(1e-9) +} + +#[test] +fn normal_to_height_recovers_the_broad_shape_of_a_bump() { + for size in [256u32, 1024] { + // A wide wrap-around Gaussian bump: nearly all of its energy is at coarse scales. + let bump = Image::generate_uv(size, size, Kind::Gray, |u, v| { + let (dx, dy) = (u - 0.5, v - 0.5); + [(-(dx * dx + dy * dy) / (2.0 * 0.12 * 0.12)).exp(), 0.0, 0.0, 0.0] + }); + let normal = run("height_to_normal", &bump, size, &[("intensity", Value::Float(2.0))]); + let back = run("normal_to_height", &normal, size, &[]); + let c = pearson(&bump, &back); + assert!(c > 0.99, "size {size}: correlation {c}"); + } +}