authoravatarclover caruso<git@paperclover.net>2026-12-01 00:33:44-08:00
committeravatarclover caruso<git@paperclover.net>2026-12-01 01:05:25-08:00
logd0c875aa773e5d28656a545f701099d66106098c
tree012a0e509b5e5791dbc1aab7e04728be218cfcfe
parent408403efb4edc35a8b564399b38ae75bcd036b0f
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

FastNoise: exact 3D simplex lattice recovered from Float32 probes

Replace the improved-Perlin approximation with Fusion's lattice: Gustavson-style 3D simplex noise, kernel 16.642 * (0.5196152 - r^2)^4, hash perm[k + perm[j + perm[i]]] & 15 into a 16-entry edge-gradient table. Octave i samples 2^i * (x, y, Seethe) + 50.3 * i at weight 0.5^i; Seethe is the z coordinate. Coordinates are frame-width units measured from the pixel corner (y up), Angle rotates clockwise before the per-axis scale. Falling noise nodes now agree with Fusion to one 8-bit LSB. MAE vs the previous shader: falling_2360 4.23 -> 3.07, falling_2400 0.95 -> 0.58, falling_2199 0.50 -> 0.45, violet_4000 0.66 -> 0.21; falling_2380 1.02 -> 1.25. Assisted-by: claude-sonnet-5.5

2 files changed, 73 insertions(+), 76 deletions(-)

crates/engine/src/eval/io.rs+19-21
......@@ -1513,27 +1513,25 @@ Composition {{
15131513 }
15141514 }
15151515
1516 fn mean_and_std(px: &[[f32; 4]]) -> (f32, f32) {
1517 let mean = px.iter().map(|p| p[0]).sum::<f32>() / px.len() as f32;
1518 (mean, (px.iter().map(|p| (p[0] - mean).powi(2)).sum::<f32>() / px.len() as f32).sqrt())
1519 }
1520
1521 #[test]
1522 fn fast_noise_single_octave_matches_fusion_distribution_and_bounds() {
1523 // Fusion 21.1 float probe (Detail 1, 2048px, XScale 24): std 0.104, range 0.2425..0.7575.
1524 let Some(px) = render_generator("FastNoise", 1024, 1024, "Detail = Input { Value = 1, }, XScale = Input { Value = 12, },") else { return };
1525 let (_, std) = mean_and_std(&px);
1526 assert!((0.098..0.110).contains(&std), "std {std}");
1527 let (lo, hi) = px.iter().fold((1.0f32, 0.0f32), |(lo, hi), p| (lo.min(p[0]), hi.max(p[0])));
1528 assert!(lo >= 0.2425 - 0.01 && hi <= 0.7575 + 0.01, "octave must stay inside Fusion's bound, got {lo}..{hi}");
1529 }
1530
1516 /// Fusion 21.1 Float32 probes (R at x, y) of the simplex lattice, octave offsets, Seethe, Angle, Center and YScale.
15311517 #[test]
1532 fn fast_noise_discontinuous_detail10_matches_falling_stats() {
1533 // Fusion 21.1 probe of 20_falling's noise (Detail 10, XScale 10, Discontinuous, 1080p): mean 0.336, std 0.140.
1534 let Some(px) = render_generator("FastNoise", 1920, 1080, "Detail = Input { Value = 10, }, XScale = Input { Value = 10, }, Discontinuous = Input { Value = 1, }, Seethe = Input { Value = 29820, },") else { return };
1535 let (mean, std) = mean_and_std(&px);
1536 assert!((0.32..0.35).contains(&mean), "mean {mean}");
1537 assert!((0.125..0.150).contains(&std), "std {std}");
1518 fn fast_noise_matches_fusion_float_probes() {
1519 let cases: [(u32, u32, &str, [(usize, usize, f32); 6]); 4] = [
1520 (512, 512, "Detail = Input { Value = 2, }, XScale = Input { Value = 4, }, Seethe = Input { Value = 1, },",
1521 [(37, 41, 0.4457), (200, 150, 0.5710), (311, 17, 0.4191), (480, 333, 0.5275), (90, 300, 0.4076), (255, 255, 0.2991)]),
1522 (640, 360, "Detail = Input { Value = 3, }, XScale = Input { Value = 4, }, Discontinuous = Input { Value = 1, },",
1523 [(37, 41, 0.2150), (200, 150, 0.4042), (311, 17, 0.1601), (480, 333, 0.1774), (90, 300, 0.1933), (255, 255, 0.1025)]),
1524 (640, 360, "Detail = Input { Value = 1, }, XScale = Input { Value = 4, }, Angle = Input { Value = 30, }, Center = Input { Value = { 0.3, 0.6 }, },",
1525 [(37, 41, 0.4068), (200, 150, 0.5251), (311, 17, 0.4916), (480, 333, 0.4710), (90, 300, 0.5406), (255, 255, 0.7552)]),
1526 (640, 360, "Detail = Input { Value = 1, }, XScale = Input { Value = 4, }, LockXY = Input { Value = 0, }, YScale = Input { Value = 7, },",
1527 [(37, 41, 0.3507), (200, 150, 0.2588), (311, 17, 0.5995), (480, 333, 0.5001), (90, 300, 0.5444), (255, 255, 0.5119)]),
1528 ];
1529 for (w, h, inputs, probes) in cases {
1530 let Some(px) = render_generator("FastNoise", w, h, inputs) else { return };
1531 for (x, y, want) in probes {
1532 let got = px[y * w as usize + x][0];
1533 assert!((got - want).abs() <= 1.5 / 255.0, "{inputs} at ({x}, {y}): got {got:.4}, want {want:.4}");
1534 }
1535 }
15381536 }
15391537}
crates/engine/src/shaders/keyers_gen.wgsl+54-55
......@@ -24,7 +24,7 @@ fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) {
2424 store_dst(vec2<i32>(gid.xy), out_col);
2525}
2626// ============================================================================
27// 8. FastNoise (3D Perlin FBM with Seethe, Angle, Discontinuous & gradient LUT)
27// 8. FastNoise (3D simplex FBM with Seethe, Angle, Discontinuous & gradient LUT)
2828// p[0] = (x_scale, y_scale, detail, contrast)
2929// p[1] = (brightness, seethe, discontinuous, center_x)
3030// p[2] = (center_y, angle_rad, inverted, type) with type 2 = Gradient, else Two Color
......@@ -49,59 +49,60 @@ var<private> PERM: array<u32, 256> = array<u32, 256>(
4949 222u, 114u, 67u, 29u, 24u, 72u, 243u, 141u, 128u, 195u, 78u, 66u, 215u, 61u, 156u, 180u
5050);
5151
52fn perm(i: u32) -> u32 {
53 return PERM[i & 255u];
54}
52const GRAD16: array<vec3<f32>, 16> = array<vec3<f32>, 16>(
53 vec3<f32>(0.0, 1.0, 1.0), vec3<f32>(0.0, 1.0, -1.0), vec3<f32>(0.0, -1.0, 1.0), vec3<f32>(0.0, -1.0, -1.0),
54 vec3<f32>(1.0, 0.0, 1.0), vec3<f32>(1.0, 0.0, -1.0), vec3<f32>(-1.0, 0.0, 1.0), vec3<f32>(-1.0, 0.0, -1.0),
55 vec3<f32>(1.0, 1.0, 0.0), vec3<f32>(1.0, -1.0, 0.0), vec3<f32>(-1.0, 1.0, 0.0), vec3<f32>(-1.0, -1.0, 0.0),
56 vec3<f32>(1.0, 0.0, -1.0), vec3<f32>(-1.0, 0.0, -1.0), vec3<f32>(0.0, -1.0, 1.0), vec3<f32>(0.0, 1.0, 1.0),
57);
5558
56fn grad3(h: u32, f: vec3<f32>) -> f32 {
57 let u_v = select(f.y, f.x, h < 8u);
58 let v_v = select(select(f.z, f.x, h == 12u || h == 14u), f.y, h < 4u);
59 return select(-u_v, u_v, (h & 1u) == 0u) + select(-v_v, v_v, (h & 2u) == 0u);
59fn simplex_corner(ijk: vec3<i32>, d: vec3<f32>) -> f32 {
60 let t = SIMPLEX_RADIUS2 - dot(d, d);
61 if (t <= 0.0) { return 0.0; }
62 let c = ijk & vec3<i32>(255);
63 let h = PERM[(u32(c.z) + PERM[(u32(c.y) + PERM[u32(c.x)]) & 255u]) & 255u] & 15u;
64 let t2 = t * t;
65 return t2 * t2 * dot(GRAD16[h], d);
6066}
6167
62// Improved-Perlin lattice noise (quintic fade, 16 edge gradients).
63fn noise3d(p: vec3<f32>) -> f32 {
64 let fl = floor(p);
65 let f = p - fl;
66 let c = vec3<u32>(vec3<i32>(fl) & vec3<i32>(255));
67 let u_f = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
68 let a = perm(c.x) + c.y;
69 let b = perm(c.x + 1u) + c.y;
70 let aa = perm(a) + c.z;
71 let ab = perm(a + 1u) + c.z;
72 let ba = perm(b) + c.z;
73 let bb = perm(b + 1u) + c.z;
74 let n000 = grad3(perm(aa) & 15u, f);
75 let n100 = grad3(perm(ba) & 15u, f - vec3<f32>(1.0, 0.0, 0.0));
76 let n010 = grad3(perm(ab) & 15u, f - vec3<f32>(0.0, 1.0, 0.0));
77 let n110 = grad3(perm(bb) & 15u, f - vec3<f32>(1.0, 1.0, 0.0));
78 let n001 = grad3(perm(aa + 1u) & 15u, f - vec3<f32>(0.0, 0.0, 1.0));
79 let n101 = grad3(perm(ba + 1u) & 15u, f - vec3<f32>(1.0, 0.0, 1.0));
80 let n011 = grad3(perm(ab + 1u) & 15u, f - vec3<f32>(0.0, 1.0, 1.0));
81 let n111 = grad3(perm(bb + 1u) & 15u, f - vec3<f32>(1.0, 1.0, 1.0));
82 let nxy0 = mix(mix(n000, n100, u_f.x), mix(n010, n110, u_f.x), u_f.y);
83 let nxy1 = mix(mix(n001, n101, u_f.x), mix(n011, n111, u_f.x), u_f.y);
84 return mix(nxy0, nxy1, u_f.z);
68// Fusion's lattice: Gustavson-style 3D simplex noise with kernel (0.5196 - r^2)^4 and a 16-entry
69// gradient table, recovered from Float32 probes (residual 2e-7).
70fn simplex3d(p: vec3<f32>) -> f32 {
71 let cell = floor(p + (p.x + p.y + p.z) / 3.0);
72 let ijk = vec3<i32>(cell);
73 let d0 = p - (cell - (cell.x + cell.y + cell.z) / 6.0);
74 var a = vec3<i32>(0);
75 var b = vec3<i32>(0);
76 if (d0.x >= d0.y) {
77 if (d0.y >= d0.z) { a = vec3<i32>(1, 0, 0); b = vec3<i32>(1, 1, 0); }
78 else if (d0.x >= d0.z) { a = vec3<i32>(1, 0, 0); b = vec3<i32>(1, 0, 1); }
79 else { a = vec3<i32>(0, 0, 1); b = vec3<i32>(1, 0, 1); }
80 } else {
81 if (d0.y < d0.z) { a = vec3<i32>(0, 0, 1); b = vec3<i32>(0, 1, 1); }
82 else if (d0.x < d0.z) { a = vec3<i32>(0, 1, 0); b = vec3<i32>(0, 1, 1); }
83 else { a = vec3<i32>(0, 1, 0); b = vec3<i32>(1, 1, 0); }
84 }
85 let g = 1.0 / 6.0;
86 return SIMPLEX_GAIN * (
87 simplex_corner(ijk, d0)
88 + simplex_corner(ijk + a, d0 - vec3<f32>(a) + g)
89 + simplex_corner(ijk + b, d0 - vec3<f32>(b) + 2.0 * g)
90 + simplex_corner(ijk + vec3<i32>(1), d0 - 1.0 + 3.0 * g)
91 );
8592}
8693
87// Measured against Fusion 21.1 float32 probes: one octave spans 0.5 +/- 0.2575 (std 0.104),
88// later octaves are 2x frequency at half amplitude (own offsets) and sum unnormalized,
89// fractional Detail weights the last octave, Discontinuous sums 2|n| per octave, Contrast
90// pivots on 0.5 (on 0 when Discontinuous), Inverted runs before Brightness, Brightness adds
91// half its value, and Gradient + Discontinuous shifts the lookup by 0.25. Fusion's lattice
92// hash is not recovered: the noise only matches its distribution, and NOISE_FREQ 1.0 trades
93// Fusion's ~1.8x finer spectrum for lower per-pixel MAE against its unmatched phases.
94const NOISE_GAIN: f32 = 0.376;
95const NOISE_FREQ: f32 = 1.0;
96const NOISE_KNEE: f32 = 0.22;
97const NOISE_SOFT: f32 = 0.0375;
94// Octave i samples the lattice at 2^i * (x, y, seethe) + 50.3 * i with weight 0.5^i; a fractional
95// Detail scales the last octave. Discontinuous sums 2|n| per octave, Contrast pivots on 0.5 (on 0
96// when Discontinuous), Inverted runs before Brightness, Brightness adds half its value, and
97// Gradient + Discontinuous shifts the lookup by 0.25.
98const SIMPLEX_RADIUS2: f32 = 0.5196152;
99const SIMPLEX_GAIN: f32 = 16.642;
100const OCTAVE_OFFSET: f32 = 50.3;
98101
99102@compute @workgroup_size(8, 8)
100103fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
101104 let dims = textureDimensions(dst_tex);
102105 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
103 let aspect = f32(dims.x) / max(f32(dims.y), 1.0);
104 let fu = pix_to_fu(gid.xy, dims);
105106 let center = vec2<f32>(u.p[1].w, u.p[2].x);
106107 let scale = u.p[0].xy;
107108 let detail = clamp(u.p[0].z, 0.0, 10.0);
......@@ -112,14 +113,13 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
112113 let angle = u.p[2].y;
113114 let gradient = u.p[2].w > 1.5;
114115
115 // Frame-width isotropic units
116 var rel = (fu - center) * vec2<f32>(1.0, 1.0 / aspect);
117 if (abs(angle) > 1e-5) {
118 let ca = cos(angle);
119 let sa = sin(angle);
120 rel = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca);
121 }
122 let p = rel * scale * NOISE_FREQ;
116 // Frame-width units, y up, measured from the pixel corner.
117 let w = f32(dims.x);
118 let h = f32(dims.y);
119 let rel = vec2<f32>(f32(gid.x) / w, (h - 1.0 - f32(gid.y)) / w) - center * vec2<f32>(1.0, h / w);
120 let ca = cos(angle);
121 let sa = sin(angle);
122 let p = vec2<f32>(rel.x * ca + rel.y * sa, rel.y * ca - rel.x * sa) * scale;
123123
124124 let octaves = i32(ceil(detail));
125125 var sum = 0.0;
......@@ -127,9 +127,8 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
127127 var freq = 1.0;
128128 for (var i = 0; i < octaves; i = i + 1) {
129129 let weight = select(amp, amp * fract(detail), i == octaves - 1 && fract(detail) > 1e-4);
130 let raw = noise3d(vec3<f32>(p * freq + f32(i) * vec2<f32>(31.7, 17.3), seethe + 0.37 + f32(i) * 1.73)) * NOISE_GAIN;
131 let over = max(abs(raw) - NOISE_KNEE, 0.0);
132 let n = sign(raw) * (min(abs(raw), NOISE_KNEE) + NOISE_SOFT * tanh(over / NOISE_SOFT));
130 let off = OCTAVE_OFFSET * f32(i);
131 let n = simplex3d(vec3<f32>(p * freq + off, seethe * freq + off));
133132 sum = sum + weight * select(n, 2.0 * abs(n), disc);
134133 freq = freq * 2.0;
135134 amp = amp * 0.5;