From d428d41c8703f6c43ea8d6dbf1f6f05bbda52882 Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Thu, 5 Nov 2026 00:10:05 -0500 Subject: [PATCH] fix(nodes): Normal Blend no longer returns NaN for a base normal with z = -1 Co-Authored-By: Claude Sonnet 5.5 --- crates/nodes/src/filters.rs | 8 ++++++++ crates/nodes/src/filters.test.rs | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 crates/nodes/src/filters.test.rs diff --git a/crates/nodes/src/filters.rs b/crates/nodes/src/filters.rs index 84a6645..e2867a8 100644 --- a/crates/nodes/src/filters.rs +++ b/crates/nodes/src/filters.rs @@ -9,6 +9,10 @@ use crate::atomic::{height_to_normal, levels_fn, transform_sample}; use crate::catalog::{CookResult, Ctx, NodeDef, PortKind, node}; use crate::ops; +#[cfg(test)] +#[path = "filters.test.rs"] +mod tests; + const ADJ: &str = "Filters/Adjustments"; const BLENDING: &str = "Filters/Blending"; const BLURS: &str = "Filters/Blurs"; @@ -790,6 +794,10 @@ fn normal_blend(ctx: &Ctx) -> CookResult { // Reoriented normal mapping (BarrĂ©-Brisebois & Hill). let t = [b[0], b[1], b[2] + 1.0]; let u = [-d[0], -d[1], d[2]]; + // A base normal pointing straight away (z = -1) has no reorientation; keep it. + if t[2].abs() < 1e-4 { + return pack(b, 1.0); + } let dot = t[0] * u[0] + t[1] * u[1] + t[2] * u[2]; let r = [t[0] * dot / t[2] - u[0], t[1] * dot / t[2] - u[1], t[2] * dot / t[2] - u[2]]; pack(r, 1.0) diff --git a/crates/nodes/src/filters.test.rs b/crates/nodes/src/filters.test.rs new file mode 100644 index 0000000..f1d9e3d --- /dev/null +++ b/crates/nodes/src/filters.test.rs @@ -0,0 +1,24 @@ +#[cfg(test)] +mod normal_blend_tests { + use std::collections::BTreeMap; + + use materialcraft_image::{Image, Kind}; + + use crate::catalog::Ctx; + use crate::catalog::{find, resolve_params}; + + /// A base normal pointing straight away from the viewer (blue = 0) used to divide by zero. + #[test] + fn normal_blend_with_a_black_base_stays_finite() { + let def = find("normal_blend").expect("normal_blend exists"); + let params = resolve_params(def, &BTreeMap::new()); + let no_res = |_: &str| None; + let vars = BTreeMap::new(); + let black = Image::filled(8, 8, Kind::Color, [0.0, 0.0, 0.0, 1.0]); + let bump = Image::filled(8, 8, Kind::Color, [0.7, 0.4, 0.6, 1.0]); + let inputs = vec![Some(black), Some(bump), None]; + let ctx = Ctx { w: 8, h: 8, seed: 1, tile: true, inputs: &inputs, params: ¶ms, resource: &no_res, vars: &vars }; + let out = (def.cook)(&ctx).expect("cooks"); + assert!(out[0].data.iter().all(|v| v.is_finite()), "NaN or Inf in the blended normal"); + } +}