From ef28c018468cfcddb608dfe4c2cb27190bdb931f Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 5 Oct 2026 02:43:09 -0400 Subject: [PATCH] paint: Tilt Scale, Brush Projection, speed spacing, section locks, mixer settings in the brush Shape Dynamics gains Tilt Scale (pen tilt squashes the tip height when the size control is Pen Tilt) and Brush Projection (tilt foreshortens the tip along the tilt direction, barrel rotation turns it). Spacing gets Photoshop's checkbox: off spaces dabs by stroke time, so speed sets the spacing. Section locks keep a section when another preset is picked. Transfer gains Wetness/Mix jitter, and the brush carries the Mixer Brush options (Wet, Load, Mix, Flow, Sample All Layers). Co-Authored-By: Claude Opus 5.5 --- crates/paint/src/brush.rs | 82 ++++++++++++++- crates/paint/src/brush_tests.rs | 175 ++++++++++++++++++++++++++++++++ crates/paint/src/dynamics.rs | 91 +++++++++++++++-- crates/paint/src/lib.rs | 16 ++- crates/paint/src/mixer.rs | 38 ++++++- crates/paint/src/presets.rs | 2 +- crates/paint/src/render.rs | 15 ++- crates/paint/src/rng.rs | 2 + 8 files changed, 404 insertions(+), 17 deletions(-) diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rs index aca8475702..01ffc756b3 100644 --- a/crates/paint/src/brush.rs +++ b/crates/paint/src/brush.rs @@ -10,6 +10,7 @@ use photocraft_color::BlendMode; use serde::{Deserialize, Serialize}; +use crate::mixer::MixerSettings; use crate::tile::GrayTile; /// What drives a dynamic parameter (Photoshop's "Control" pop-ups). @@ -80,12 +81,19 @@ pub struct ShapeDynamics { pub enabled: bool, /// Size jitter / control; `minimum` is Photoshop's Minimum Diameter. pub size: Dynamic, + /// Tilt Scale, 0..2 (0–200 %): with the size control on Pen Tilt, how much a tilted pen + /// squashes the tip's height (before the tip angle is applied). At full tilt the height is + /// scaled by `1 − tiltScale / 2`, so 0 leaves the tip alone and 200 % flattens it. + pub tilt_scale: f32, /// Angle jitter (fraction of ±180°) / control. pub angle: Dynamic, /// Roundness jitter / control; `minimum` is Minimum Roundness. pub roundness: Dynamic, pub flip_x_jitter: bool, pub flip_y_jitter: bool, + /// Brush Projection: the pen's tilt and barrel rotation are applied to the tip as a + /// projection (the tip is foreshortened along the tilt direction and turns with the pen). + pub brush_projection: bool, } /// Scattering. @@ -254,13 +262,36 @@ impl Default for ColorDynamics { } } -/// Transfer: per-dab opacity ceiling and flow. +/// Transfer: per-dab opacity ceiling and flow, plus the Mixer Brush's wetness and mix. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] pub struct Transfer { pub enabled: bool, pub opacity: Dynamic, pub flow: Dynamic, + /// Wetness Jitter / control (Mixer Brush only): scales the Wet setting per dab. + pub wetness: Dynamic, + /// Mix Jitter / control (Mixer Brush only): scales the Mix setting per dab. + pub mix: Dynamic, +} + +/// The sections a lock can hold (the lock icons in Photoshop's Brush Settings list): a locked +/// section keeps its settings when another brush preset is picked. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct SectionLocks { + pub shape_dynamics: bool, + pub scattering: bool, + pub texture: bool, + pub dual_brush: bool, + pub color_dynamics: bool, + pub transfer: bool, + pub pose: bool, + pub noise: bool, + pub wet_edges: bool, + pub build_up: bool, + pub smoothing: bool, + pub protect_texture: bool, } /// Brush Pose: stylus values used instead of (override) or for missing device data. @@ -327,6 +358,9 @@ pub struct BrushSettings { pub hardness: f32, /// Spacing between dabs as a fraction of the diameter. pub spacing: f32, + /// Photoshop's Spacing checkbox. Off: the pointer's speed sets the spacing (one dab every + /// [`crate::dynamics::SPEED_SPACING_MS`] of stroke time; without timestamps, one per input point). + pub spacing_enabled: bool, /// Maximum coverage for the whole stroke. pub opacity: f32, /// Per-dab coverage. @@ -374,6 +408,10 @@ pub struct BrushSettings { pub smoothing: Smoothing, /// Keep the current texture (pattern + scale) when switching to another textured preset. pub protect_texture: bool, + /// Sections whose settings stay when another preset is picked (tool state, like Photoshop's locks). + pub locks: SectionLocks, + /// Mixer Brush options (Wet, Load, Mix, Flow, Sample All Layers) used by `paint.mixerBrush`. + pub mixer: MixerSettings, /// Random seed for jitters (commands derive it from the stroke when not given), so strokes replay. pub seed: u64, } @@ -384,6 +422,7 @@ impl Default for BrushSettings { size: 20.0, hardness: 1.0, spacing: 0.1, + spacing_enabled: true, opacity: 1.0, flow: 1.0, pressure_size: true, @@ -411,6 +450,8 @@ impl Default for BrushSettings { build_up_rate: 20.0, smoothing: Smoothing::default(), protect_texture: false, + locks: SectionLocks::default(), + mixer: MixerSettings::default(), seed: 0, } } @@ -418,10 +459,47 @@ impl Default for BrushSettings { impl BrushSettings { /// This brush (a preset) picked while `current` is the tool's brush: Smoothing is a tool - /// option, so it stays the tool's (Photoshop), and a protected texture stays too. + /// option, so it stays the tool's (Photoshop), a protected texture stays too, and so does every + /// section `current` has locked (the locks themselves are tool state and carry over). pub fn picked_over(self, current: &BrushSettings) -> Self { let mut b = self.with_protected_texture(current); b.smoothing = current.smoothing.clone(); + let l = ¤t.locks; + if l.shape_dynamics { + b.shape_dynamics = current.shape_dynamics.clone(); + } + if l.scattering { + b.scattering = current.scattering.clone(); + } + if l.texture { + b.texture = current.texture.clone(); + } + if l.dual_brush { + b.dual_brush = current.dual_brush.clone(); + } + if l.color_dynamics { + b.color_dynamics = current.color_dynamics.clone(); + } + if l.transfer { + b.transfer = current.transfer.clone(); + } + if l.pose { + b.pose = current.pose.clone(); + } + if l.noise { + b.noise = current.noise; + } + if l.wet_edges { + b.wet_edges = current.wet_edges; + } + if l.build_up { + b.build_up = current.build_up; + b.build_up_rate = current.build_up_rate; + } + if l.protect_texture { + b.protect_texture = current.protect_texture; + } + b.locks = current.locks.clone(); b } diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs index f79fda87f9..dc35ee5189 100644 --- a/crates/paint/src/brush_tests.rs +++ b/crates/paint/src/brush_tests.rs @@ -232,6 +232,7 @@ fn jitter_is_deterministic_per_seed() { roundness: Dynamic { jitter: 1.0, minimum: 0.2, ..Default::default() }, flip_x_jitter: true, flip_y_jitter: true, + ..Default::default() }, scattering: Scattering { enabled: true, scatter: Dynamic::jitter(2.0), both_axes: true, count: 3, count_jitter: Dynamic::jitter(1.0) }, ..brush() @@ -638,3 +639,177 @@ fn tail_preview_shows_the_stroke_as_finishing_it_would() { r.push(&pts); assert!(r.tail_preview().is_none()); } + +// ---------- Tilt Scale, Brush Projection, speed spacing, locks, mixer transfer ---------- + +/// Coverage extent (pixels above half coverage) of a rasterised dab along x and y through its centre. +fn extents(b: &BrushSettings, d: &Dab) -> (usize, usize) { + let r = raster(b, d); + let (cx, cy) = (d.center.x.floor() as i32, d.center.y.floor() as i32); + let w = (r.0.x0..r.0.x1).filter(|&x| at(&r, x, cy) > 0.5).count(); + let h = (r.0.y0..r.0.y1).filter(|&y| at(&r, cx, y) > 0.5).count(); + (w, h) +} + +fn pen(x: f64, y: f64, tilt_x: f32, tilt_y: f32, rotation: f32) -> StrokePoint { + StrokePoint { tilt_x, tilt_y, rotation, ..StrokePoint::new(x, y, 1.0) } +} + +#[test] +fn tilt_scale_squashes_the_tip_height_with_pen_tilt() { + // Size control on Pen Tilt with a 100 % minimum, so only Tilt Scale changes the shape. + let mk = |scale: f32| BrushSettings { + size: 40.0, + shape_dynamics: ShapeDynamics { + enabled: true, + size: Dynamic { control: Control::PenTilt, minimum: 1.0, ..Default::default() }, + tilt_scale: scale, + ..Default::default() + }, + ..brush() + }; + let upright = [pen(50.0, 50.0, 0.0, 0.0, 0.0)]; + let tilted = [pen(50.0, 50.0, 45.0, 0.0, 0.0)]; + // 0 % (the default) and an upright pen leave the tip round. + let d = dabs_of(&mk(0.0), &tilted)[0]; + assert_eq!(d.roundness, 1.0); + let d = dabs_of(&mk(2.0), &upright)[0]; + assert_eq!(d.roundness, 1.0); + // 100 % at 45° tilt: height × (1 − 0.5 × 0.5) = 0.75; 200 %: 0.5. + let d = dabs_of(&mk(1.0), &tilted)[0]; + assert!((d.roundness - 0.75).abs() < 1e-4, "{}", d.roundness); + let d2 = dabs_of(&mk(2.0), &tilted)[0]; + assert!((d2.roundness - 0.5).abs() < 1e-4, "{}", d2.roundness); + let (w, h) = extents(&mk(2.0), &d2); + assert!(w >= 38 && (h as i32 - 20).abs() <= 2, "{w}×{h}"); + // Only with the size control on Pen Tilt (Photoshop greys the slider otherwise). + let mut b = mk(2.0); + b.shape_dynamics.size.control = Control::PenPressure; + assert_eq!(dabs_of(&b, &tilted)[0].roundness, 1.0); + // Rendered strokes differ. + let a = paint(&mk(0.0), &[pen(20.0, 40.0, 60.0, 0.0, 0.0), pen(120.0, 40.0, 60.0, 0.0, 0.0)], 0, 0); + let c = paint(&mk(2.0), &[pen(20.0, 40.0, 60.0, 0.0, 0.0), pen(120.0, 40.0, 60.0, 0.0, 0.0)], 0, 0); + let area = Rect::new(0, 0, 160, 80); + assert!(alpha_sum(&c, area) < alpha_sum(&a, area) * 0.75, "{} vs {}", alpha_sum(&c, area), alpha_sum(&a, area)); +} + +#[test] +fn brush_projection_foreshortens_along_tilt_and_turns_with_rotation() { + let mk = |on: bool| BrushSettings { size: 40.0, shape_dynamics: ShapeDynamics { enabled: true, brush_projection: on, ..Default::default() }, ..brush() }; + // Tilted 60° towards +x: the tip is foreshortened to cos 60° = ½ along x. + let pts = [pen(50.0, 50.0, 60.0, 0.0, 0.0)]; + let d = dabs_of(&mk(true), &pts)[0]; + assert!((d.proj_scale - 0.5).abs() < 1e-4 && d.proj_angle.abs() < 1e-5, "{d:?}"); + let (w, h) = extents(&mk(true), &d); + assert!((w as i32 - 20).abs() <= 2 && h >= 38, "{w}×{h}"); + // Tilted along y instead: foreshortened vertically. + let d = dabs_of(&mk(true), &[pen(50.0, 50.0, 0.0, 60.0, 0.0)])[0]; + let (w, h) = extents(&mk(true), &d); + assert!(w >= 38 && (h as i32 - 20).abs() <= 2, "{w}×{h}"); + // Off: the tilt doesn't touch the tip. + let d = dabs_of(&mk(false), &pts)[0]; + assert_eq!((d.proj_scale, d.angle), (1.0, 0.0)); + assert_eq!(extents(&mk(false), &d), (40, 40)); + // Barrel rotation turns the tip (an elliptical one shows it). + let mut b = mk(true); + b.roundness = 0.5; + let d = dabs_of(&b, &[pen(50.0, 50.0, 0.0, 0.0, 90.0)])[0]; + assert!((d.angle.to_degrees() - 90.0).abs() < 1e-3); + let (w, h) = extents(&b, &d); + assert!(w < h, "{w}×{h}"); + // Dual-brush dabs are never projected. + let ctx = BrushContext::new(&mk(true)); + let dd = Dab { proj_scale: 0.3, ..Dab::round(Point::new(50.0, 50.0), 20.0, 1.0) }; + let (r, mut a, mut c) = (ctx.dab_rect(&dd, true), Vec::new(), Vec::new()); + ctx.rasterize(&dd, true, r, &mut a); + ctx.rasterize(&Dab { proj_scale: 1.0, ..dd }, true, r, &mut c); + assert_eq!(a, c); +} + +#[test] +fn spacing_off_spaces_dabs_by_pointer_speed() { + let mut b = BrushSettings { size: 20.0, spacing_enabled: false, ..brush() }; + let timed = |ms: f64| vec![StrokePoint { time: 0.0, ..StrokePoint::new(0.0, 10.0, 1.0) }, StrokePoint { time: ms, ..StrokePoint::new(100.0, 10.0, 1.0) }]; + // 100 px in 400 ms: a dab every 8 ms = every 2 px; in 40 ms: every 20 px. + let slow = dabs_of(&b, &timed(400.0)).len(); + let fast = dabs_of(&b, &timed(40.0)).len(); + assert_eq!((slow, fast), (51, 6)); + // Never denser than half a pixel, however slow. + assert!(dabs_of(&b, &timed(1.0e9)).len() <= 201); + // No timestamps: one dab per input point. + let pts: Vec = (0..7).map(|i| StrokePoint::new(f64::from(i) * 15.0, 10.0, 1.0)).collect(); + assert_eq!(dabs_of(&b, &pts).len(), 7); + // Checked: fixed spacing (25 % of 20 px = 5 px) whatever the speed. + b.spacing_enabled = true; + assert_eq!(dabs_of(&b, &timed(400.0)).len(), dabs_of(&b, &timed(40.0)).len()); + // Chunked input gives the same dabs. + b.spacing_enabled = false; + let pts: Vec = (0..20).map(|i| StrokePoint { time: f64::from(i) * 13.0, ..StrokePoint::new(f64::from(i) * 7.0, 10.0, 1.0) }).collect(); + let one = dabs_of(&b, &pts); + let mut g = crate::dynamics::DabGenerator::new(&b, 1.0); + let (mut out, mut dual) = (Vec::new(), Vec::new()); + for c in pts.chunks(3) { + g.push(c, &mut out, &mut dual); + } + g.finish(&mut out, &mut dual); + assert_eq!(one, out); +} + +#[test] +fn locked_sections_survive_picking_a_preset() { + let presets = crate::presets::builtin(); + let chalk = crate::presets::find(&presets, "chalk").unwrap().brush.clone(); + let mut cur = BrushSettings { + scattering: Scattering { enabled: true, count: 5, ..Default::default() }, + transfer: Transfer { enabled: true, wetness: Dynamic::jitter(0.4), ..Default::default() }, + noise: true, + ..brush() + }; + // Unlocked: the preset's sections win. + let next = chalk.clone().picked_over(&cur); + assert_eq!(next.scattering, chalk.scattering); + assert_eq!(next.noise, chalk.noise); + // Locked: the current sections stay, and so do the locks. + cur.locks = SectionLocks { scattering: true, transfer: true, noise: true, ..Default::default() }; + let next = chalk.clone().picked_over(&cur); + assert_eq!(next.scattering, cur.scattering); + assert_eq!(next.transfer, cur.transfer); + assert!(next.noise); + assert_eq!(next.locks, cur.locks); + // Unlocked sections still come from the preset. + assert_eq!(next.texture, chalk.texture); + assert_eq!(next.tip, chalk.tip); +} + +#[test] +fn transfer_wetness_and_mix_vary_per_dab() { + let b = BrushSettings { + transfer: Transfer { + enabled: true, + wetness: Dynamic::controlled(Control::PenPressure), + mix: Dynamic { jitter: 1.0, minimum: 0.25, ..Default::default() }, + ..Default::default() + }, + ..brush() + }; + let pts = [StrokePoint::new(0.0, 0.0, 0.3), StrokePoint::new(60.0, 0.0, 0.3)]; + let d = dabs_of(&b, &pts); + assert!(d.iter().all(|x| (x.wet - 0.3).abs() < 1e-5)); + assert!(d.iter().all(|x| (0.25..=1.0).contains(&x.mix)) && d.iter().any(|x| x.mix < 0.9)); + // Transfer off: as set. + let d = dabs_of(&brush(), &pts); + assert!(d.iter().all(|x| x.wet == 1.0 && x.mix == 1.0)); + // New fields round-trip and old JSON loads with neutral values. + let full = BrushSettings { + spacing_enabled: false, + shape_dynamics: ShapeDynamics { tilt_scale: 1.5, brush_projection: true, ..Default::default() }, + locks: SectionLocks { texture: true, ..Default::default() }, + mixer: crate::mixer::MixerSettings { wet: 0.9, sample_all_layers: true, ..Default::default() }, + ..b + }; + let back: BrushSettings = serde_json::from_value(serde_json::to_value(&full).unwrap()).unwrap(); + assert_eq!(back, full); + let old: BrushSettings = serde_json::from_str(r#"{"size": 12, "shapeDynamics": {"enabled": true}, "transfer": {"enabled": true}}"#).unwrap(); + assert!(old.spacing_enabled && !old.shape_dynamics.brush_projection && old.shape_dynamics.tilt_scale == 0.0 && old.locks == SectionLocks::default()); + assert_eq!(old.mixer, crate::mixer::MixerSettings::default()); +} diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs index 68a98bacb5..64caebeafd 100644 --- a/crates/paint/src/dynamics.rs +++ b/crates/paint/src/dynamics.rs @@ -13,6 +13,13 @@ use crate::{Dab, StrokePoint}; /// Maximum pulled-string length at 100 % smoothing, in screen pixels. pub const MAX_STRING_PX: f64 = 100.0; +/// With Spacing unchecked, one dab per this many milliseconds of stroke time, so faster strokes +/// space their dabs further apart (Photoshop: "the speed of the cursor determines the spacing"). +pub const SPEED_SPACING_MS: f64 = 8.0; + +/// Densest speed spacing, in pixels between dabs (a slow, long stroke never floods the buffer). +const MIN_SPEED_STEP: f64 = 0.5; + #[inline] fn lerp_pt(a: &StrokePoint, b: &StrokePoint, f: f64) -> StrokePoint { let ff = f as f32; @@ -147,11 +154,32 @@ pub struct PathWalker { /// Airbrush interval in ms (None = off). interval: Option, time_acc: f64, + /// Speed spacing (Spacing unchecked): one step per this many ms of stroke time. + speed: Option, + speed_acc: f64, } impl PathWalker { pub fn new(build_up_interval_ms: Option) -> Self { - Self { last: None, next_at: 0.0, pending_first: None, initial_dir: None, dir: 0.0, step: 0, interval: build_up_interval_ms, time_acc: 0.0 } + Self { + last: None, + next_at: 0.0, + pending_first: None, + initial_dir: None, + dir: 0.0, + step: 0, + interval: build_up_interval_ms, + time_acc: 0.0, + speed: None, + speed_acc: 0.0, + } + } + + /// Space steps by stroke time instead of distance ([`SPEED_SPACING_MS`]): faster movement + /// spreads them out. Segments without timestamps get one step at their end point. + pub fn speed_spacing(mut self, interval_ms: f64) -> Self { + self.speed = Some(interval_ms.max(0.1)); + self } fn emit(&mut self, point: StrokePoint, out: &mut impl FnMut(StepInput)) { @@ -179,12 +207,36 @@ impl PathWalker { self.emit(f, out); } if len > 1e-9 { - while self.next_at <= len + 1e-9 { - let q = lerp_pt(&a, &p, (self.next_at / len).min(1.0)); - self.emit(q, out); - self.next_at += step_len(&q).max(0.25); + match self.speed { + None => { + while self.next_at <= len + 1e-9 { + let q = lerp_pt(&a, &p, (self.next_at / len).min(1.0)); + self.emit(q, out); + self.next_at += step_len(&q).max(0.25); + } + self.next_at -= len; + } + Some(iv) => { + let dt = p.time - a.time; + if dt > 0.0 && dt.is_finite() { + self.speed_acc += dt; + let max_n = (len / MIN_SPEED_STEP).ceil().max(1.0) as usize; + let mut n = 0; + while self.speed_acc >= iv && n < max_n { + self.speed_acc -= iv; + let f = ((dt - self.speed_acc) / dt).clamp(0.0, 1.0); + self.emit(lerp_pt(&a, &p, f), out); + n += 1; + } + if n == max_n { + self.speed_acc = self.speed_acc.rem_euclid(iv); + } + } else { + // No timestamps: one dab per input point. + self.emit(p, out); + } + } } - self.next_at -= len; } if let Some(iv) = self.interval { let dt = p.time - a.time; @@ -379,6 +431,22 @@ impl DabBuilder { fy = !fy; } } + // Tilt Scale and Brush Projection (Shape Dynamics). + let (mut proj_angle, mut proj_scale) = (0.0, 1.0); + if sd.enabled { + let tilt = s.point.tilt_x.hypot(s.point.tilt_y).clamp(0.0, 90.0); + if sd.size.control == Control::PenTilt && sd.tilt_scale > 0.0 { + let k = 1.0 - sd.tilt_scale.clamp(0.0, 2.0) * 0.5 * tilt / 90.0; + roundness = (roundness * k).clamp(0.01, 1.0); + } + if sd.brush_projection { + angle += s.point.rotation; + if tilt > 0.0 { + proj_angle = (-s.point.tilt_y).atan2(s.point.tilt_x); + proj_scale = tilt.to_radians().cos().max(0.05); + } + } + } let radius = (diameter / 2.0).max(0.5); // Scatter. let mut c = Point::new(s.point.x, s.point.y); @@ -403,6 +471,7 @@ impl DabBuilder { opacity = scalar(&tr.opacity, s, r(stream::OPACITY)); flow *= scalar(&tr.flow, s, r(stream::FLOW)); } + let (wet, mix) = if tr.enabled { (scalar(&tr.wetness, s, r(stream::WET)), scalar(&tr.mix, s, r(stream::MIX))) } else { (1.0, 1.0) }; if b.pressure_opacity && !(tr.enabled && tr.opacity.control != Control::Off) { opacity *= s.point.pressure.clamp(0.0, 1.0); } @@ -428,6 +497,10 @@ impl DabBuilder { color, depth, index: i, + proj_angle, + proj_scale, + wet, + mix, }); } } @@ -479,6 +552,10 @@ impl DualBuilder { color: [0.0, 0.0, 0.0, 1.0], depth: 1.0, index: i, + proj_angle: 0.0, + proj_scale: 1.0, + wet: 1.0, + mix: 1.0, }); } } @@ -519,7 +596,7 @@ impl DabGenerator { let dual_on = brush.dual_brush.enabled; Self { smoother: Smoother::new(&brush.smoothing, zoom), - walker: PathWalker::new(interval), + walker: if brush.spacing_enabled { PathWalker::new(interval) } else { PathWalker::new(interval).speed_spacing(SPEED_SPACING_MS) }, dual_walker: dual_on.then(|| PathWalker::new(None)), builder: DabBuilder::new(brush), dual: dual_on.then(|| DualBuilder::new(brush)), diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs index b1208240c4..96e98facf8 100644 --- a/crates/paint/src/lib.rs +++ b/crates/paint/src/lib.rs @@ -31,9 +31,10 @@ pub mod rng; pub mod tile; pub use brush::{ - BrushPreset, BrushSettings, ColorDynamics, Control, DualBrush, Dynamic, MaskMode, Pattern, PatternStyle, Pose, Scattering, ShapeDynamics, Smoothing, - Texture, TipShape, Transfer, + BrushPreset, BrushSettings, ColorDynamics, Control, DualBrush, Dynamic, MaskMode, Pattern, PatternStyle, Pose, Scattering, SectionLocks, ShapeDynamics, + Smoothing, Texture, TipShape, Transfer, }; +pub use mixer::MixerSettings; pub use render::{BrushContext, StrokeRenderer, render_stroke}; pub use tile::GrayTile; @@ -94,6 +95,13 @@ pub struct Dab { pub depth: f32, /// Position in the stroke (0 = first). pub index: u64, + /// Brush Projection: direction (radians, counter-clockwise, y up) along which the tip is + /// foreshortened, and the factor (1 = no projection). + pub proj_angle: f32, + pub proj_scale: f32, + /// Mixer Brush multipliers of Wet and Mix (Transfer › Wetness/Mix Jitter), 1 = as set. + pub wet: f32, + pub mix: f32, } impl Dab { @@ -111,6 +119,10 @@ impl Dab { color: [0.0, 0.0, 0.0, 1.0], depth: 1.0, index: 0, + proj_angle: 0.0, + proj_scale: 1.0, + wet: 1.0, + mix: 1.0, } } } diff --git a/crates/paint/src/mixer.rs b/crates/paint/src/mixer.rs index dcbc1acf52..afb8092cd2 100644 --- a/crates/paint/src/mixer.rs +++ b/crates/paint/src/mixer.rs @@ -24,11 +24,13 @@ pub struct MixerSettings { pub load: f32, pub mix: f32, pub flow: f32, + /// Pick up colour from all visible layers instead of the target layer only. + pub sample_all_layers: bool, } impl Default for MixerSettings { fn default() -> Self { - Self { wet: 0.5, load: 0.5, mix: 0.5, flow: 1.0 } + Self { wet: 0.5, load: 0.5, mix: 0.5, flow: 1.0, sample_all_layers: false } } } @@ -87,6 +89,8 @@ pub fn apply_mixer_stroke( let mut acc = vec![0.0f32; n]; let mut paint = vec![0.0f32; n]; let dmg = apply_dab_stroke(target, &st, selection, lock_transparency, 0, |work, fp| { + // Transfer › Wetness / Mix Jitter scale the settings per dab. + let (wet, mix) = (wet * fp.dab.wet.clamp(0.0, 1.0), mix * fp.dab.mix.clamp(0.0, 1.0)); // Sample the canvas under the dab. acc.iter_mut().for_each(|v| *v = 0.0); let (mut wsum, mut asum) = (0.0f32, 0.0f32); @@ -165,7 +169,7 @@ mod tests { let mut s = Surface::new(PixelFormat::RGBA8); let mut st = MixerState::default(); st.load([1.0, 0.0, 0.0, 1.0]); - apply_mixer_stroke(&mut s, None, &stroke(10.0), &MixerSettings { wet: 0.0, load, mix: 0.0, flow: 1.0 }, &mut st, None, false); + apply_mixer_stroke(&mut s, None, &stroke(10.0), &MixerSettings { wet: 0.0, load, mix: 0.0, flow: 1.0, ..Default::default() }, &mut st, None, false); let (start, end) = (s.rgba(10, 10)[3], s.rgba(190, 10)[3]); assert!(start > 0.95, "{start}"); if expect_dry { @@ -186,7 +190,15 @@ mod tests { s.fill_rect(Rect::new(100, 0, 200, 20), &[1.0, 1.0, 1.0, 1.0]); let mut st = MixerState::default(); st.load([1.0, 0.0, 0.0, 1.0]); - apply_mixer_stroke(&mut s, None, &stroke(10.0), &MixerSettings { wet: 1.0, load: 0.3, mix: 0.9, flow: 1.0 }, &mut st, None, false); + apply_mixer_stroke( + &mut s, + None, + &stroke(10.0), + &MixerSettings { wet: 1.0, load: 0.3, mix: 0.9, flow: 1.0, ..Default::default() }, + &mut st, + None, + false, + ); // Blue is dragged into the white half. let c = s.rgba(115, 10); assert!(c[2] > c[1] + 0.05 && c[1] < 0.95, "{c:?}"); @@ -205,4 +217,24 @@ mod tests { assert!(!dmg.is_empty()); assert_eq!(s.rgba(50, 10)[3], 0.0); } + + #[test] + fn transfer_wetness_control_scales_pickup_per_dab() { + // Wetness on Pen Pressure: no pressure means a dry brush (no pickup), full pressure picks up. + let run = |pressure: f32| { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 200, 20), &[0.0, 0.0, 1.0, 1.0]); + let mut st = MixerState::default(); + st.load([1.0, 0.0, 0.0, 1.0]); + let mut k = stroke(10.0); + k.brush.transfer = crate::Transfer { enabled: true, wetness: crate::Dynamic::controlled(crate::Control::PenPressure), ..Default::default() }; + for p in &mut k.points { + p.pressure = pressure; + } + apply_mixer_stroke(&mut s, None, &k, &MixerSettings { wet: 1.0, load: 1.0, mix: 1.0, flow: 1.0, ..Default::default() }, &mut st, None, false); + st.pickup + }; + assert!(run(0.0).is_none()); + assert!(run(1.0).is_some_and(|p| p[2] > 0.2)); + } } diff --git a/crates/paint/src/presets.rs b/crates/paint/src/presets.rs index e987348d53..c5d9143e34 100644 --- a/crates/paint/src/presets.rs +++ b/crates/paint/src/presets.rs @@ -27,7 +27,7 @@ fn size_dyn(size: Dynamic) -> ShapeDynamics { } fn transfer(opacity: Dynamic, flow: Dynamic) -> Transfer { - Transfer { enabled: true, opacity, flow } + Transfer { enabled: true, opacity, flow, ..Default::default() } } fn paper(seed: u32, depth: f32) -> Texture { diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs index 3cee715d59..cf33a1ef5f 100644 --- a/crates/paint/src/render.rs +++ b/crates/paint/src/render.rs @@ -144,6 +144,12 @@ impl BrushContext { let (cx, cy) = (d.center.x as f32, d.center.y as f32); let (sn, cs) = d.angle.sin_cos(); let (fx, fy) = (if d.flip_x { -1.0 } else { 1.0 }, if d.flip_y { -1.0 } else { 1.0 }); + // Brush Projection: stretch the sampling coordinates along the tilt direction, which + // foreshortens the tip there by `proj_scale`. + let proj = (!dual && d.proj_scale < 0.999).then(|| { + let (ps, pc) = d.proj_angle.sin_cos(); + (pc, ps, 1.0 / d.proj_scale.max(0.05) - 1.0) + }); let r = d.radius; let ro = d.roundness.max(0.5 / r).min(1.0); let rm = (r * ro).max(0.5); @@ -163,8 +169,13 @@ impl BrushContext { for xx in 0..w { let x = rect.x0 + xx as i32; let dx = x as f32 + 0.5 - cx; - // To y-up, rotate by -angle, flip. - let (ux, uy) = (dx, -dy); + // To y-up, project, rotate by -angle, flip. + let (mut ux, mut uy) = (dx, -dy); + if let Some((ax, ay, k)) = proj { + let t = (ux * ax + uy * ay) * k; + ux += t * ax; + uy += t * ay; + } let u = (ux * cs + uy * sn) * fx; let v = (-ux * sn + uy * cs) * fy; let (mut val, rn) = match mips { diff --git a/crates/paint/src/rng.rs b/crates/paint/src/rng.rs index 9d053653a1..3dfe5fd809 100644 --- a/crates/paint/src/rng.rs +++ b/crates/paint/src/rng.rs @@ -53,6 +53,8 @@ pub mod stream { pub const DUAL_X: u64 = 16; pub const DUAL_Y: u64 = 17; pub const DUAL_FLIP: u64 = 18; + pub const WET: u64 = 19; + pub const MIX: u64 = 20; } /// Seed derived from arbitrary bytes (FNV-1a then mixed), for commands without an explicit seed.