From d104f7015fa3524c589e8b7a79f1da43cba856e3 Mon Sep 17 00:00:00 2001 From: Matthias Schlich Date: Tue, 20 Oct 2026 22:49:04 +0200 Subject: [PATCH] fix(filters): Clouds and Fibers move with their smart object Their noise was sampled in document coordinates, so moving a smart object re-rendered a different texture with the same params. The pattern is now anchored at the filter's bounds: the object for a smart filter, the canvas on a layer. Fibers, like Clouds, takes the canvas as its bounds even with a selection, which only masks it, so layer results stay as before (CodeRabbit review on #6160). --- crates/algo/src/lib.rs | 6 +++--- crates/algo/src/render.rs | 7 +++++-- crates/engine/src/filters.rs | 4 ++-- crates/engine/src/smart_cmds/tests.rs | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs index f2b061cf75..35c195f399 100644 --- a/crates/algo/src/lib.rs +++ b/crates/algo/src/lib.rs @@ -617,10 +617,10 @@ impl FilterParams { matches!(self, FilterParams::Fibers { .. } | FilterParams::Clouds { difference: false, .. }) } - /// Whether the filter sizes its pattern by the canvas even with a selection (Clouds): its - /// bounds are then the canvas, and the selection only masks the result. + /// Whether the filter places and sizes its pattern by the canvas even with a selection + /// (Clouds, Fibers): its bounds are then the canvas, and the selection only masks the result. pub fn sizes_by_canvas(&self) -> bool { - matches!(self, FilterParams::Clouds { .. }) + matches!(self, FilterParams::Clouds { .. } | FilterParams::Fibers { .. }) } /// Human-readable name. diff --git a/crates/algo/src/render.rs b/crates/algo/src/render.rs index 3c0aa6dcda..2a1867dac8 100644 --- a/crates/algo/src/render.rs +++ b/crates/algo/src/render.rs @@ -47,7 +47,8 @@ fn clouds_value(x: f32, y: f32, base: f32, seed: u32) -> f32 { } /// Clouds between the foreground and background colours; Difference Clouds subtracts them from -/// the pixels (alpha kept). The cloud size is a quarter of the bounds' longer side. +/// the pixels (alpha kept). The cloud size is a quarter of the bounds' longer side, and the +/// pattern is anchored at the bounds' corner (so it moves with a smart object). pub(crate) fn clouds(src: &Image, out: Rect, ctx: &Ctx, seed: u32, difference: bool, colours: ([f32; 4], [f32; 4])) -> Vec { let n = src.ch; let (fg, bg) = colours; @@ -55,6 +56,7 @@ pub(crate) fn clouds(src: &Image, out: Rect, ctx: &Ctx, seed: u32, difference: b let mut res = src.crop(out); for (i, px) in res.chunks_exact_mut(n).enumerate() { let (x, y) = xy(out, i); + let (x, y) = (x - ctx.bounds.x0, y - ctx.bounds.y0); let t = clouds_value(x as f32, y as f32, base, seed); let c: [f32; 3] = std::array::from_fn(|k| fg[k] + (bg[k] - fg[k]) * t); if difference { @@ -81,8 +83,9 @@ pub(crate) fn fibers(src: &Image, out: Rect, ctx: &Ctx, variance: f32, strength: let fy = fx / (2.0 + st * 1.5) * (0.5 + var / 32.0); let mut res = src.crop(out); for (i, px) in res.chunks_exact_mut(n).enumerate() { + // Anchored at the bounds' corner, so the fibres move with a smart object. let (x, y) = xy(out, i); - let (xf, yf) = (x as f32, y as f32); + let (xf, yf) = ((x - ctx.bounds.x0) as f32, (y - ctx.bounds.y0) as f32); let mut sum = 0.0; let mut amp = 1.0; let mut norm = 0.0; diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs index 9eebebe301..bb7550706f 100644 --- a/crates/engine/src/filters.rs +++ b/crates/engine/src/filters.rs @@ -262,8 +262,8 @@ pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result }; let selection = doc.selection.clone(); let sel_bounds = selection.as_ref().map(photocraft_raster::Surface::content_bounds); - // Distortions centre on the selection when there is one, else the canvas. Clouds size - // their pattern by the canvas either way; the selection only masks them. + // Distortions centre on the selection when there is one, else the canvas. Clouds and + // Fibers place and size their pattern by the canvas either way; the selection masks them. let doc_bounds = doc.bounds(); let bounds = if fp.sizes_by_canvas() { doc_bounds } else { sel_bounds.filter(|b| !b.is_empty()).unwrap_or(doc_bounds) }; // Neighbourhood filters repeat edge pixels at the canvas edge (or past it, where the diff --git a/crates/engine/src/smart_cmds/tests.rs b/crates/engine/src/smart_cmds/tests.rs index c1c06909f3..76ca1aa899 100644 --- a/crates/engine/src/smart_cmds/tests.rs +++ b/crates/engine/src/smart_cmds/tests.rs @@ -1197,3 +1197,25 @@ fn lens_flare_centres_on_the_smart_object() { let (x, y) = (core.iter().map(|p| p.0).sum::() / core.len() as i32, core.iter().map(|p| p.1).sum::() / core.len() as i32); assert!((x - 48).abs() <= 1 && (y - 36).abs() <= 1, "flare at {x},{y}, the object's centre is 48,36"); } + +/// Moving a smart object moves its Clouds and Fibers with it: the pattern is anchored at the +/// object, not at the canvas, so re-rendering after the move doesn't change the texture. +#[test] +fn clouds_and_fibers_move_with_the_smart_object() { + for (command, params) in [("filter.render.clouds", json!({"seed": 3})), ("filter.render.fibers", json!({"seed": 3}))] { + let mut s = session(8); + paint(&mut s); + let id = convert(&mut s); + s.execute(command, params).unwrap(); + let before = flat(&s); + s.execute("edit.transform", json!({"layer": id, "matrix": [1, 0, 0, 1, 8, 6]})).unwrap(); + let after = flat(&s); + // Inside the object (painted from (-6, 5) to (40, 38)) both before and after the move. + for y in 8..30 { + for x in 2..30 { + let (a, b) = (before[(y * W + x) as usize], after[((y + 6) * W + x + 8) as usize]); + assert!((0..4).all(|c| (a[c] - b[c]).abs() < 1e-3), "{command} at {x},{y}: {a:?} vs {b:?}"); + } + } + } +}