mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-21 06:50:43 +01:00
Merge pull request #181 from Silvertree2010/fix/176-bug-paths-with-fill-closed-path
Apply brush symmetry to filled closed paths
This commit is contained in:
4 files changed
+134
-28
No files matched your search
@@ -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,26 +466,50 @@ 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);
|
||||
|
||||
// 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 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 = center + *m * (s.pos - center);
|
||||
let c = view_proj * pos.extend(1.0);
|
||||
if c.w <= MIN_PROJECTIVE_W {
|
||||
continue;
|
||||
}
|
||||
let ndc = Vec2::new(c.x, c.y) / c.w;
|
||||
let a = lasso.coverage(to_pixels(ndc, viewport));
|
||||
if a <= best {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 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 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;
|
||||
}
|
||||
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();
|
||||
|
||||
@@ -88,6 +88,46 @@ 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");
|
||||
}
|
||||
|
||||
#[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);
|
||||
|
||||
@@ -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,41 @@ impl Symmetry {
|
||||
pub fn any(&self) -> bool {
|
||||
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<Item = Vec3> {
|
||||
[
|
||||
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<Mat3> {
|
||||
let mut out: Vec<Mat3> = 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
|
||||
}
|
||||
}
|
||||
|
||||
/// Particle brush: every stamp emits particles that slide down the surface under gravity
|
||||
|
||||
+13
-10
@@ -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<Stamp> = 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]
|
||||
|
||||
Reference in new issue
Block a user