Files
texturecraft/crates/paint/src/particles.rs
T
bflatastic afdea4a6dd Radial symmetry for brushes
Strokes can repeat around an axis through the mesh center, 2 to 32 copies, on their own or with
the X/Y/Z mirrors (each mirror is spread too).

- Symmetry gains radial (copies) and axis (X/Y/Z, default Y), serde defaults; sanitized clamps
- symmetry copies are marked and paint where symmetry puts them: the camera visibility and
  back-face tests apply to the stroke itself only, so copies on the far side of the model paint
  (mirror Z included); they still only paint surfaces facing the copy
- tool.brush {symmetryRadial, symmetryAxis}, shown in its result as radialSymmetry; Properties >
  Lazy mouse and symmetry: Radial copies slider and axis; translations in all eight languages
- tests: copies a quarter turn apart, with mirrors, clamping; an engine stroke paints the back of
  a sphere through radial symmetry
2026-11-01 15:04:23 +07:00

113 lines
4.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Particle brush: each stamp emits a few particles that slide down the surface under gravity
//! (world −Y) and fall off overhangs, painting a small dab per simulation step. Deterministic:
//! particles are seeded by the stamp seed.
use texturecraft_geom::glam::Vec3;
use texturecraft_geom::{Surface, surface_at};
use texturecraft_texel::noise::pcg3d;
use crate::brush::Particles;
use crate::stroke::{PickCtx, Stamp};
/// Particle dab radius relative to the brush stamp.
const PARTICLE_SIZE: f32 = 0.35;
/// The surface hit by a ray, if any within `tmax`.
fn cast(ctx: &PickCtx, o: Vec3, d: Vec3, tmax: f32) -> Option<Surface> {
let hit = ctx.bvh.closest(o, d, tmax)?;
surface_at(ctx.mesh, hit.tri, hit.u, hit.v)
}
/// Project `p` onto the surface along the normal `n` (within `reach`).
fn snap(ctx: &PickCtx, p: Vec3, n: Vec3, reach: f32) -> Option<Surface> {
cast(ctx, p + n * reach, -n, reach * 2.0)
}
/// Particle dabs for one brush stamp.
pub fn emit(ctx: &PickCtx, p: &Particles, s: &Stamp) -> Vec<Stamp> {
let size = s.radius * PARTICLE_SIZE;
let step = size * p.gravity;
let fall = s.radius * 20.0;
let unit = |v: u32| v as f32 / u32::MAX as f32;
let mut out = Vec::new();
for k in 0..p.count {
let r = pcg3d(s.seed, k, 0xD057);
let (a, d) = (unit(r[0]) * std::f32::consts::TAU, unit(r[1]).sqrt());
let start = s.center + (s.tangent * a.cos() + s.bitangent * a.sin()) * d * s.radius;
let Some(mut at) = snap(ctx, start, s.normal, s.radius) else { continue };
for life in 0..p.lifetime {
let tangent = at.nrm.any_orthonormal_vector();
out.push(Stamp {
center: at.pos,
normal: at.nrm,
tangent,
bitangent: at.nrm.cross(tangent),
radius: size,
flow: s.flow * (1.0 - life as f32 / p.lifetime as f32),
seed: s.seed.wrapping_mul(17).wrapping_add(k),
..*s
});
if step <= 0.0 {
break;
}
let slide = Vec3::NEG_Y - at.nrm * at.nrm.dot(Vec3::NEG_Y);
let next = if slide.length() > 0.05 {
// Slide downhill; past an edge the particle drops onto what lies below.
let to = at.pos + slide.normalize() * step;
snap(ctx, to, at.nrm, step).or_else(|| cast(ctx, to + at.nrm * step, Vec3::NEG_Y, fall))
} else if at.nrm.y < 0.0 {
// Hanging under a ceiling: fall straight down.
cast(ctx, at.pos + at.nrm * step, Vec3::NEG_Y, fall)
} else {
None // resting on flat ground
};
match next {
Some(h) => at = h,
None => break,
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use texturecraft_geom::glam::Vec2;
use texturecraft_geom::{Bvh, OrbitCamera, primitives};
#[test]
fn particles_slide_down_a_wall_deterministically() {
// A vertical wall: the plane rotated to face +Z.
let mut m = primitives::plane(2.0, 8);
for p in &mut m.positions {
*p = Vec3::new(p.x, -p.z, 0.0);
}
for n in &mut m.normals {
*n = Vec3::Z;
}
let bvh = Bvh::new(&m);
let cam = OrbitCamera::default();
let ctx = PickCtx { mesh: &m, bvh: &bvh, camera: &cam, viewport: Vec2::new(100.0, 100.0) };
let s = Stamp {
center: Vec3::ZERO,
normal: Vec3::Z,
tangent: Vec3::X,
bitangent: Vec3::Y,
radius: 0.1,
flow: 1.0,
hardness: 0.5,
depth: 1.0,
seed: 3,
copy: false,
};
let p = Particles { enabled: true, count: 4, lifetime: 20, gravity: 1.0 };
let a = emit(&ctx, &p, &s);
assert_eq!(a, emit(&ctx, &p, &s));
assert_eq!(a.len(), 80);
let lowest = a.iter().map(|d| d.center.y).fold(f32::MAX, f32::min);
assert!(lowest < -0.4, "{lowest}");
assert!(a.iter().all(|d| d.center.z.abs() < 1e-3));
}
}