mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-20 06:21:05 +01:00
fix(nodes): Normal Blend no longer returns NaN for a base normal with z = -1
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
91bb53f07a
commit
d428d41c87
2 files changed
+32
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user