From 98279937bc8bd8e864b21d3eb34cbe5f5e49642a Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Thu, 5 Nov 2026 07:05:29 -0500 Subject: [PATCH] fix(nodes): make Anisotropic Noise, Grunge Map 3 and Leaks tile Each used one noise period for both axes while the cell counts differed per axis, leaving a seam. Add noise::value_xy (separate period per axis) and use whole-number cell counts. The noises_tile test now checks the top/bottom edge too, compares against the mean neighbour difference, and covers the three nodes. Co-Authored-By: Claude Sonnet 5.5 --- crates/nodes/src/generators.rs | 14 ++++++--- crates/nodes/src/generators.test.rs | 48 +++++++++++++++++++++++++++++ crates/nodes/src/lib.rs | 18 ++++++++++- crates/nodes/src/noise.rs | 8 ++++- crates/nodes/src/weathering.rs | 8 +++-- crates/nodes/src/weathering.test.rs | 34 ++++++++++++++++++++ 6 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 crates/nodes/src/generators.test.rs create mode 100644 crates/nodes/src/weathering.test.rs diff --git a/crates/nodes/src/generators.rs b/crates/nodes/src/generators.rs index 700a6a0..77c8204 100644 --- a/crates/nodes/src/generators.rs +++ b/crates/nodes/src/generators.rs @@ -3,7 +3,7 @@ use materialcraft_image::{Image, Kind}; use crate::catalog::{CookResult, Ctx, NodeDef, node}; -use crate::noise::{self, cellular, fbm, perlin, rand2, ridged, value}; +use crate::noise::{self, cellular, fbm, perlin, rand2, ridged, value, value_xy}; use crate::ops; pub const NOISES: &str = "Texture generators/Noises"; @@ -173,8 +173,8 @@ fn anisotropic(ctx: &Ctx) -> CookResult { let smooth = ctx.f("smoothness"); let s = ctx.seed; let base = ctx.gen_gray(|u, v| { - let a = value(u * sx, v * sy, sy as i32, s); - let b = value(u * sx * 2.0, v * sy, sy as i32, s ^ 77) * 0.5; + let a = value_xy(u * sx, v * sy, sx as i32, sy as i32, s); + let b = value_xy(u * sx * 2.0, v * sy, (sx * 2.0) as i32, sy as i32, s ^ 77) * 0.5; (a * 0.75 + b * 0.25 * (1.0 - smooth) + 0.125 * smooth).clamp(0.0, 1.0) }); Ok(vec![if smooth > 0.0 { ops::gaussian(&base, smooth * 2.0, ctx.tile) } else { base }]) @@ -313,7 +313,9 @@ fn grunge2(ctx: &Ctx) -> CookResult { fn grunge3(ctx: &Ctx) -> CookResult { grunge(ctx, |u, v, sc, s| { - let streak = value(u * sc * 8.0, v * sc * 0.5, (sc * 8.0) as i32, s); + // Whole cell counts per axis (and the matching periods) so both edges tile. + let cy = (sc * 0.5).round().max(1.0); + let streak = value_xy(u * sc * 8.0, v * cy, (sc * 8.0) as i32, cy as i32, s); let a = fbm(u, v, sc, 6, 0.55, s ^ 2); streak * 0.5 + a * 0.5 }) @@ -410,3 +412,7 @@ fn white_noise(ctx: &Ctx) -> CookResult { Image::gray(ctx.w, ctx.h, |x, y| rand2(x as i32, y as i32, s)) }]) } + +#[cfg(test)] +#[path = "generators.test.rs"] +mod tests; diff --git a/crates/nodes/src/generators.test.rs b/crates/nodes/src/generators.test.rs new file mode 100644 index 0000000..adc0238 --- /dev/null +++ b/crates/nodes/src/generators.test.rs @@ -0,0 +1,48 @@ +//! Tiling tests for generators whose cell counts differ per axis. + +use std::collections::BTreeMap; + +use materialcraft_core::Value; +use materialcraft_image::Image; + +use crate::{Ctx, find, resolve_params}; + +fn cook(id: &str, scale: f32, extra: &[(&str, Value)]) -> Image { + let def = find(id).unwrap(); + let mut over: BTreeMap = extra.iter().map(|(k, v)| (k.to_string(), v.clone())).collect(); + over.insert("scale".into(), Value::Float(scale)); + let params = resolve_params(def, &over); + let no_res = |_: &str| None; + let vars = BTreeMap::new(); + let inputs = vec![None; def.inputs.len()]; + let ctx = Ctx { w: 64, h: 64, seed: 5, tile: true, inputs: &inputs, params: ¶ms, resource: &no_res, vars: &vars }; + (def.cook)(&ctx).unwrap().remove(0) +} + +/// Wrap jump along both axes versus the typical neighbour difference. +fn seams(img: &Image) -> (f32, f32, f32, f32) { + let (mut hs, mut hi, mut vs, mut vi) = (0.0f32, 0.0f32, 0.0f32, 0.0f32); + for k in 0..64 { + hs += (img.get(63, k, false)[0] - img.get(0, k, false)[0]).abs(); + hi += (img.get(31, k, false)[0] - img.get(32, k, false)[0]).abs(); + vs += (img.get(k, 63, false)[0] - img.get(k, 0, false)[0]).abs(); + vi += (img.get(k, 31, false)[0] - img.get(k, 32, false)[0]).abs(); + } + (hs, hi, vs, vi) +} + +#[test] +fn grunge_map_3_tiles_at_odd_scales() { + for scale in [3.0, 5.0, 7.0] { + let (hs, hi, vs, vi) = seams(&cook("grunge_map_3", scale, &[])); + assert!(hs <= hi * 3.0 + 0.5, "scale {scale}: horizontal seam {hs} vs {hi}"); + assert!(vs <= vi * 3.0 + 0.5, "scale {scale}: vertical seam {vs} vs {vi}"); + } +} + +#[test] +fn anisotropic_noise_tiles_with_different_axis_scales() { + let (hs, hi, vs, vi) = seams(&cook("anisotropic_noise", 8.0, &[("scale_y", Value::Float(3.0))])); + assert!(hs <= hi * 3.0 + 0.5, "horizontal seam {hs} vs {hi}"); + assert!(vs <= vi * 3.0 + 0.5, "vertical seam {vs} vs {vi}"); +} diff --git a/crates/nodes/src/lib.rs b/crates/nodes/src/lib.rs index d51c3d9..c2446f4 100644 --- a/crates/nodes/src/lib.rs +++ b/crates/nodes/src/lib.rs @@ -115,17 +115,33 @@ mod tests { "scratches_generator", "rust", "moss", + "anisotropic_noise", + "grunge_map_3", + "leaks", ] { let def = find(id).unwrap(); let img = &cook(def, &[], 64).unwrap()[0]; // The jump across the wrap is no larger than typical neighbouring differences. let mut seam = 0.0f32; let mut inner = 0.0f32; + // (Interior differences are averaged over every column pair: sparse images can be + // blank at the middle.) for y in 0..64 { seam += (img.get(63, y, false)[0] - img.get(0, y, false)[0]).abs(); - inner += (img.get(31, y, false)[0] - img.get(32, y, false)[0]).abs(); + for x in 0..63 { + inner += (img.get(x, y, false)[0] - img.get(x + 1, y, false)[0]).abs() / 63.0; + } } assert!(seam <= inner * 3.0 + 0.5, "{id}: seam {seam} vs inner {inner}"); + // Same across the top and bottom edges. + let (mut vseam, mut vinner) = (0.0f32, 0.0f32); + for x in 0..64 { + vseam += (img.get(x, 63, false)[0] - img.get(x, 0, false)[0]).abs(); + for y in 0..63 { + vinner += (img.get(x, y, false)[0] - img.get(x, y + 1, false)[0]).abs() / 63.0; + } + } + assert!(vseam <= vinner * 3.0 + 0.5, "{id}: vertical seam {vseam} vs inner {vinner}"); } } diff --git a/crates/nodes/src/noise.rs b/crates/nodes/src/noise.rs index 95cd36d..d1a4af5 100644 --- a/crates/nodes/src/noise.rs +++ b/crates/nodes/src/noise.rs @@ -42,10 +42,16 @@ fn wrapi(i: i32, p: i32) -> i32 { /// Value noise in 0..1, lattice period `p` cells (coordinates already scaled to cells). pub fn value(x: f32, y: f32, p: i32, seed: u32) -> f32 { + value_xy(x, y, p, p, seed) +} + +/// Value noise in 0..1 with a separate lattice period per axis (`px`, `py` cells), so a noise +/// stretched differently along x and y still tiles. +pub fn value_xy(x: f32, y: f32, px: i32, py: i32, seed: u32) -> f32 { let (x0, y0) = (x.floor(), y.floor()); let (tx, ty) = (fade(x - x0), fade(y - y0)); let (ix, iy) = (x0 as i32, y0 as i32); - let r = |i: i32, j: i32| rand2(wrapi(i, p), wrapi(j, p), seed); + let r = |i: i32, j: i32| rand2(wrapi(i, px), wrapi(j, py), seed); let a = r(ix, iy) + (r(ix + 1, iy) - r(ix, iy)) * tx; let b = r(ix, iy + 1) + (r(ix + 1, iy + 1) - r(ix, iy + 1)) * tx; a + (b - a) * ty diff --git a/crates/nodes/src/weathering.rs b/crates/nodes/src/weathering.rs index 4c1cfa4..6692543 100644 --- a/crates/nodes/src/weathering.rs +++ b/crates/nodes/src/weathering.rs @@ -9,7 +9,7 @@ use rayon::prelude::*; use crate::catalog::{CookResult, Ctx, NodeDef, PortKind, node}; use crate::fx_map::{BLENDS, Inst, splat}; use crate::generators::NOISES; -use crate::noise::{cellular, fbm, rand1, value}; +use crate::noise::{cellular, fbm, rand1, value, value_xy}; use crate::noises_more::smoothstep; use crate::ops; use crate::patterns::PATTERNS; @@ -267,7 +267,7 @@ fn leaks(ctx: &Ctx) -> CookResult { let run = Image::gray(ctx.w, ctx.h, |x, y| cols.get(x as usize).and_then(|c| c.get(y as usize)).copied().unwrap_or(0.0)); Ok(vec![ctx.gen_gray(|u, v| { let dx = (fbm(u, v, 8.0, 3, 0.5, seed ^ 22) - 0.5) * wob * 0.04; - run.sample_gray(u + dx, v, ctx.tile) * (0.75 + 0.25 * value(u * streaks * 2.0, v * 3.0, (streaks * 2.0) as i32, seed ^ 25)) + run.sample_gray(u + dx, v, ctx.tile) * (0.75 + 0.25 * value_xy(u * streaks * 2.0, v * 3.0, (streaks * 2.0) as i32, 3, seed ^ 25)) })]) } @@ -294,3 +294,7 @@ fn edge_damage(ctx: &Ctx) -> CookResult { }); Ok(vec![out, mask]) } + +#[cfg(test)] +#[path = "weathering.test.rs"] +mod tests; diff --git a/crates/nodes/src/weathering.test.rs b/crates/nodes/src/weathering.test.rs new file mode 100644 index 0000000..7381c1e --- /dev/null +++ b/crates/nodes/src/weathering.test.rs @@ -0,0 +1,34 @@ +//! Tiling tests for the weathering nodes. + +use std::collections::BTreeMap; + +use crate::{Ctx, find, resolve_params}; + +/// Mean absolute difference across the wrap versus across every interior neighbour pair, per axis. +fn wrap_vs_interior(id: &str) -> [(f32, f32); 2] { + let def = find(id).unwrap(); + let params = resolve_params(def, &BTreeMap::new()); + let no_res = |_: &str| None; + let vars = BTreeMap::new(); + let inputs = vec![Some(materialcraft_image::Image::filled(64, 64, materialcraft_image::Kind::Gray, [1.0; 4])); def.inputs.len()]; + let ctx = Ctx { w: 64, h: 64, seed: 9, tile: true, inputs: &inputs, params: ¶ms, resource: &no_res, vars: &vars }; + let img = (def.cook)(&ctx).unwrap().remove(0); + let g = |x: i64, y: i64| img.get(x, y, false)[0]; + let (mut hw, mut hi, mut vw, mut vi) = (0.0f32, 0.0f32, 0.0f32, 0.0f32); + for k in 0..64 { + hw += (g(63, k) - g(0, k)).abs(); + vw += (g(k, 63) - g(k, 0)).abs(); + for j in 0..63 { + hi += (g(j, k) - g(j + 1, k)).abs() / 63.0; + vi += (g(k, j) - g(k, j + 1)).abs() / 63.0; + } + } + [(hw, hi), (vw, vi)] +} + +#[test] +fn leaks_tile_in_both_directions() { + for (axis, (wrap, interior)) in wrap_vs_interior("leaks").into_iter().enumerate() { + assert!(wrap <= interior * 3.0 + 0.5, "axis {axis}: wrap {wrap} vs interior {interior}"); + } +}