From 1c657979d0bafe0a3b16f2deb919abf443041d0e Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Mon, 2 Nov 2026 22:07:14 +0100 Subject: [PATCH 1/2] Apply brush symmetry to filled closed paths Fixes #176 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/engine/src/path_tool.rs | 46 ++++++++++++++++++++------------ crates/engine/tests/path_tool.rs | 11 ++++++++ crates/paint/src/brush.rs | 23 ++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/crates/engine/src/path_tool.rs b/crates/engine/src/path_tool.rs index 0b2903e0..4e130848 100644 --- a/crates/engine/src/path_tool.rs +++ b/crates/engine/src/path_tool.rs @@ -463,26 +463,38 @@ impl Session { let view_proj = self.camera.view_proj(viewport.x.max(1.0) / viewport.y.max(1.0)); let inv = view_proj.inverse(); let (cull, mesh, bvh) = (self.tool.brush.backface_culling, &self.mesh, &self.bvh); + // Symmetry copies: a texel is painted when its mirror image (or radial copy) lies under + // the lasso on the surface the camera sees. + let (lo, hi) = mesh.bounds(); + let center = (lo + hi) * 0.5; + let inverses: Vec<_> = self.tool.brush.sanitized().symmetry.transforms().iter().map(|m| m.inverse()).collect(); fill_coverage(&mut canvas, &gbuf, |i, _| { let Some(s) = gbuf.surface(mesh, i) else { return 0.0 }; - let c = view_proj * s.pos.extend(1.0); - if c.w <= 1e-6 { - return 0.0; + let mut best = 0.0f32; + for m in &inverses { + let (pos, nrm) = (center + *m * (s.pos - center), *m * s.nrm); + let c = view_proj * pos.extend(1.0); + if c.w <= 1e-6 { + continue; + } + let ndc = Vec2::new(c.x, c.y) / c.w; + let a = lasso.coverage(to_pixels(ndc, viewport)); + if a <= best { + continue; + } + // Exact visibility (a depth buffer flickers on grazing surfaces): nothing between + // the near plane and the texel. + let o = inv * ndc.extend(0.0).extend(1.0); + let origin = o.truncate() / o.w; + let to = pos - origin; + let d = to.length(); + let dir = to / d.max(1e-9); + let hidden = (cull && nrm.dot(dir) >= 0.0) || bvh.closest(origin, dir, d * 0.999).is_some(); + if !hidden { + best = a; + } } - let ndc = Vec2::new(c.x, c.y) / c.w; - let a = lasso.coverage(to_pixels(ndc, viewport)); - if a <= 0.0 { - return 0.0; - } - // Exact visibility (a depth buffer flickers on grazing surfaces): nothing between - // the near plane and the texel. - let o = inv * ndc.extend(0.0).extend(1.0); - let origin = o.truncate() / o.w; - let to = s.pos - origin; - let d = to.length(); - let dir = to / d.max(1e-9); - let hidden = (cull && s.nrm.dot(dir) >= 0.0) || bvh.closest(origin, dir, d * 0.999).is_some(); - if hidden { 0.0 } else { a } + best }); } let tiles = canvas.dirty.len(); diff --git a/crates/engine/tests/path_tool.rs b/crates/engine/tests/path_tool.rs index 80736fc8..b663bbc2 100644 --- a/crates/engine/tests/path_tool.rs +++ b/crates/engine/tests/path_tool.rs @@ -88,6 +88,17 @@ fn closed_filled_path_fills_the_inside_only() { assert!(execute(&mut s, "tool.path", &json!({"uvPoints": [[0.1, 0.1], [0.2, 0.2]], "closed": true, "fill": true, "commit": true})).is_err()); } +#[test] +fn closed_filled_path_is_symmetrized() { + let mut s = session(primitives::sphere(1.0, 48, 24)); + execute(&mut s, "tool.brush", &json!({"symmetryX": true})).unwrap(); + let square = json!([[230, 160], [270, 160], [270, 240], [230, 240]]); + execute(&mut s, "tool.path", &json!({"points": square, "closed": true, "fill": true, "commit": true})).unwrap(); + assert!(red_at(&s, 250.0, 200.0), "the drawn side"); + assert!(red_at(&s, 150.0, 200.0), "the mirrored side"); + assert!(!red_at(&s, 200.0, 200.0), "between the two"); +} + /// A 2×2 wall facing the camera (+Z): v grows downward (world −Y). fn wall() -> Mesh { let mut m = primitives::plane(2.0, 8); diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rs index 7bcd4334..04b2e330 100644 --- a/crates/paint/src/brush.rs +++ b/crates/paint/src/brush.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use texturecraft_compose::ImageRes; +use texturecraft_geom::glam::{Mat3, Vec3}; use texturecraft_texel::noise; use crate::alphas::AlphaImage; @@ -157,6 +158,28 @@ impl Symmetry { pub fn any(&self) -> bool { self.x || self.y || self.z || self.radial > 1 } + + /// Linear maps (through the mesh center) taking a point to each of its symmetry copies, + /// the identity first. Mirrors come first, then radial rotations of the point and its mirrors. + pub fn transforms(&self) -> Vec { + let mut out = vec![Mat3::IDENTITY]; + for mask in 1u8..8 { + let (mx, my, mz) = (mask & 1 != 0, mask & 2 != 0, mask & 4 != 0); + if (mx && !self.x) || (my && !self.y) || (mz && !self.z) { + continue; + } + out.push(Mat3::from_diagonal(Vec3::new(if mx { -1.0 } else { 1.0 }, if my { -1.0 } else { 1.0 }, if mz { -1.0 } else { 1.0 }))); + } + if self.radial > 1 { + let axis = [Vec3::X, Vec3::Y, Vec3::Z].get(self.axis as usize).copied().unwrap_or(Vec3::Y); + let base = out.clone(); + for k in 1..self.radial { + let r = Mat3::from_axis_angle(axis, f32::from(k) * std::f32::consts::TAU / f32::from(self.radial)); + out.extend(base.iter().map(|m| r * *m)); + } + } + out + } } /// Particle brush: every stamp emits particles that slide down the surface under gravity From 4e7f6f0114210b8135d1e68fcb30cc4a9038ebe9 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Tue, 3 Nov 2026 00:57:31 -0500 Subject: [PATCH 2/2] Fix path fill symmetry culling --- crates/engine/src/path_tool.rs | 37 ++++++++++++++++++++++---------- crates/engine/tests/path_tool.rs | 29 +++++++++++++++++++++++++ crates/paint/src/brush.rs | 29 ++++++++++++++++++------- crates/paint/src/stroke.rs | 23 +++++++++++--------- 4 files changed, 89 insertions(+), 29 deletions(-) diff --git a/crates/engine/src/path_tool.rs b/crates/engine/src/path_tool.rs index 4e130848..150a103e 100644 --- a/crates/engine/src/path_tool.rs +++ b/crates/engine/src/path_tool.rs @@ -11,8 +11,8 @@ use std::collections::{BTreeMap, BTreeSet}; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use texturecraft_core::{Channel, Effect, EffectKind, LayerId, Mask, MaskBackground, PathShape}; -use texturecraft_geom::OrbitCamera; use texturecraft_geom::glam::{Vec2, Vec3}; +use texturecraft_geom::{OrbitCamera, surface_at}; use texturecraft_paint::{BrushSettings, Lasso, fill_coverage, spline}; use crate::commands::{CommandSpec, always, bad, boolp, has_layer, u64p}; @@ -25,6 +25,9 @@ pub const MAX_PATH_POINTS: usize = 4096; /// Curve sampling step in viewport pixels (3D view) and texture units (2D view). const STEP_PX: f32 = 2.0; const STEP_UV: f32 = 0.001; +const MIN_PROJECTIVE_W: f32 = 1e-6; +// Allow ray hits to differ slightly from transformed texel positions +const RAY_HIT_RELATIVE_TOLERANCE: f32 = 0.001; /// The curve being edited with the Path tool. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] @@ -463,18 +466,20 @@ impl Session { let view_proj = self.camera.view_proj(viewport.x.max(1.0) / viewport.y.max(1.0)); let inv = view_proj.inverse(); let (cull, mesh, bvh) = (self.tool.brush.backface_culling, &self.mesh, &self.bvh); - // Symmetry copies: a texel is painted when its mirror image (or radial copy) lies under - // the lasso on the surface the camera sees. + + // A visible source inside the lasso can paint each symmetry copy let (lo, hi) = mesh.bounds(); let center = (lo + hi) * 0.5; let inverses: Vec<_> = self.tool.brush.sanitized().symmetry.transforms().iter().map(|m| m.inverse()).collect(); + fill_coverage(&mut canvas, &gbuf, |i, _| { let Some(s) = gbuf.surface(mesh, i) else { return 0.0 }; let mut best = 0.0f32; + for m in &inverses { - let (pos, nrm) = (center + *m * (s.pos - center), *m * s.nrm); + let pos = center + *m * (s.pos - center); let c = view_proj * pos.extend(1.0); - if c.w <= 1e-6 { + if c.w <= MIN_PROJECTIVE_W { continue; } let ndc = Vec2::new(c.x, c.y) / c.w; @@ -482,18 +487,28 @@ impl Session { if a <= best { continue; } - // Exact visibility (a depth buffer flickers on grazing surfaces): nothing between - // the near plane and the texel. + + // Use the source surface normal; a copy may face away from the camera let o = inv * ndc.extend(0.0).extend(1.0); let origin = o.truncate() / o.w; let to = pos - origin; let d = to.length(); - let dir = to / d.max(1e-9); - let hidden = (cull && nrm.dot(dir) >= 0.0) || bvh.closest(origin, dir, d * 0.999).is_some(); - if !hidden { - best = a; + let Some(dir) = to.try_normalize() else { continue }; + let Some(hit) = + bvh.closest(origin, dir, d * (1.0 + RAY_HIT_RELATIVE_TOLERANCE)).filter(|hit| hit.t >= d * (1.0 - RAY_HIT_RELATIVE_TOLERANCE)) + else { + continue; + }; + + let Some(source) = surface_at(mesh, hit.tri, hit.u, hit.v) else { continue }; + + if cull && source.nrm.dot(dir) >= 0.0 { + continue; } + + best = a; } + best }); } diff --git a/crates/engine/tests/path_tool.rs b/crates/engine/tests/path_tool.rs index b663bbc2..b053e979 100644 --- a/crates/engine/tests/path_tool.rs +++ b/crates/engine/tests/path_tool.rs @@ -92,13 +92,42 @@ fn closed_filled_path_fills_the_inside_only() { fn closed_filled_path_is_symmetrized() { let mut s = session(primitives::sphere(1.0, 48, 24)); execute(&mut s, "tool.brush", &json!({"symmetryX": true})).unwrap(); + let square = json!([[230, 160], [270, 160], [270, 240], [230, 240]]); execute(&mut s, "tool.path", &json!({"points": square, "closed": true, "fill": true, "commit": true})).unwrap(); + assert!(red_at(&s, 250.0, 200.0), "the drawn side"); assert!(red_at(&s, 150.0, 200.0), "the mirrored side"); assert!(!red_at(&s, 200.0, 200.0), "between the two"); } +#[test] +fn closed_filled_path_copies_from_visible_source_to_backfacing_destination() { + let mut mesh = wall(); + for (position, normal) in mesh.positions.iter().zip(&mut mesh.normals) { + *normal = if position.x < 0.0 { Vec3::NEG_Z } else { Vec3::Z }; + } + + let mut s = session(mesh.clone()); + execute(&mut s, "tool.brush", &json!({"symmetryX": true})).unwrap(); + + let square = json!([[230, 170], [270, 170], [270, 230], [230, 230]]); + execute(&mut s, "tool.path", &json!({"points": square, "closed": true, "fill": true, "commit": true})).unwrap(); + + assert!(red_at(&s, 250.0, 200.0), "visible source"); + assert!(red_at(&s, 150.0, 200.0), "backfacing symmetry copy"); + assert!(!red_at(&s, 200.0, 200.0), "outside the lasso and its copy"); + + let mut s = session(mesh); + execute(&mut s, "tool.brush", &json!({"symmetryX": true})).unwrap(); + + let square = json!([[130, 170], [170, 170], [170, 230], [130, 230]]); + execute(&mut s, "tool.path", &json!({"points": square, "closed": true, "fill": true, "commit": true})).unwrap(); + + assert!(!red_at(&s, 150.0, 200.0), "backfacing source"); + assert!(!red_at(&s, 250.0, 200.0), "no copy from a hidden source"); +} + /// A 2×2 wall facing the camera (+Z): v grows downward (world −Y). fn wall() -> Mesh { let mut m = primitives::plane(2.0, 8); diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rs index 04b2e330..61313d57 100644 --- a/crates/paint/src/brush.rs +++ b/crates/paint/src/brush.rs @@ -159,25 +159,38 @@ impl Symmetry { self.x || self.y || self.z || self.radial > 1 } + /// Mirror scales in stroke order, including identity, without allocating + pub(crate) fn mirror_signs(&self) -> impl Iterator { + [ + Vec3::ONE, + Vec3::new(-1.0, 1.0, 1.0), + Vec3::new(1.0, -1.0, 1.0), + Vec3::new(-1.0, -1.0, 1.0), + Vec3::new(1.0, 1.0, -1.0), + Vec3::new(-1.0, 1.0, -1.0), + Vec3::new(1.0, -1.0, -1.0), + Vec3::NEG_ONE, + ] + .into_iter() + .filter(|sign| (sign.x > 0.0 || self.x) && (sign.y > 0.0 || self.y) && (sign.z > 0.0 || self.z)) + } + /// Linear maps (through the mesh center) taking a point to each of its symmetry copies, /// the identity first. Mirrors come first, then radial rotations of the point and its mirrors. pub fn transforms(&self) -> Vec { - let mut out = vec![Mat3::IDENTITY]; - for mask in 1u8..8 { - let (mx, my, mz) = (mask & 1 != 0, mask & 2 != 0, mask & 4 != 0); - if (mx && !self.x) || (my && !self.y) || (mz && !self.z) { - continue; - } - out.push(Mat3::from_diagonal(Vec3::new(if mx { -1.0 } else { 1.0 }, if my { -1.0 } else { 1.0 }, if mz { -1.0 } else { 1.0 }))); - } + let mut out: Vec = self.mirror_signs().map(Mat3::from_diagonal).collect(); + if self.radial > 1 { let axis = [Vec3::X, Vec3::Y, Vec3::Z].get(self.axis as usize).copied().unwrap_or(Vec3::Y); let base = out.clone(); + out.reserve(base.len().saturating_mul(usize::from(self.radial - 1))); + for k in 1..self.radial { let r = Mat3::from_axis_angle(axis, f32::from(k) * std::f32::consts::TAU / f32::from(self.radial)); out.extend(base.iter().map(|m| r * *m)); } } + out } } diff --git a/crates/paint/src/stroke.rs b/crates/paint/src/stroke.rs index 58330b3e..6aacb737 100644 --- a/crates/paint/src/stroke.rs +++ b/crates/paint/src/stroke.rs @@ -202,21 +202,18 @@ impl Stroke { if !sym.any() { return Vec::new(); } + let mut out = Vec::new(); - for mask in 1u8..8 { - let (mx, my, mz) = (mask & 1 != 0, mask & 2 != 0, mask & 4 != 0); - if (mx && !sym.x) || (my && !sym.y) || (mz && !sym.z) { - continue; - } - let f = Vec3::new(if mx { -1.0 } else { 1.0 }, if my { -1.0 } else { 1.0 }, if mz { -1.0 } else { 1.0 }); - let m = |v: Vec3| v * f; - let normal = m(s.normal); - let tangent = m(s.tangent); - out.push(Stamp { center: self.center + m(s.center - self.center), normal, tangent, bitangent: normal.cross(tangent), copy: true, ..*s }); + for sign in sym.mirror_signs().skip(1) { + let normal = s.normal * sign; + let tangent = s.tangent * sign; + out.push(Stamp { center: self.center + (s.center - self.center) * sign, normal, tangent, bitangent: normal.cross(tangent), copy: true, ..*s }); } + if sym.radial > 1 { let axis = [Vec3::X, Vec3::Y, Vec3::Z].get(sym.axis as usize).copied().unwrap_or(Vec3::Y); let base: Vec = std::iter::once(*s).chain(out.iter().copied()).collect(); + for k in 1..sym.radial { let q = Quat::from_axis_angle(axis, f32::from(k) * std::f32::consts::TAU / f32::from(sym.radial)); out.extend(base.iter().map(|b| Stamp { @@ -229,6 +226,7 @@ impl Stroke { })); } } + out } @@ -291,6 +289,11 @@ mod tests { let out = s.add(&ctx, Vec2::new(250.0, 200.0), 1.0); assert_eq!(out.len(), 2); assert!((out[0].center.x + out[1].center.x).abs() < 1e-4); + + st.symmetry.y = true; + st.symmetry.z = true; + let mut s = Stroke::new(&st, &m); + assert_eq!(s.add(&ctx, Vec2::new(250.0, 180.0), 1.0).len(), 8); } #[test]