| 1 | //! Fusion `Paint` / `PaintMask`: `PolylineStroke` and `Multistroke` chains are CPU-rasterized |
| 2 | //! into one RGBA layer that the GPU then composites (`Paint`) or post-processes as a mask (`PaintMask`). |
| 3 | |
| 4 | use super::masks::MaskPost; |
| 5 | use super::*; |
| 6 | use model::{LuaTable, LuaValue, PolyPoint}; |
| 7 | |
| 8 | /// Fusion stroke coordinates are centered on the image, Y-up, and normalized per axis. |
| 9 | struct Stroke { |
| 10 | path: Vec<[f32; 2]>, |
| 11 | write_on: [f32; 2], |
| 12 | /// Brush diameter as a fraction of image width. |
| 13 | size: f32, |
| 14 | softness: f32, |
| 15 | color: [f32; 3], |
| 16 | opacity: f32, |
| 17 | erase: bool, |
| 18 | } |
| 19 | |
| 20 | impl Stroke { |
| 21 | fn hash_into(&self, out: &mut Vec<f32>) { |
| 22 | out.extend_from_slice(&[self.write_on[0], self.write_on[1], self.size, self.softness, self.opacity]); |
| 23 | out.extend_from_slice(&self.color); |
| 24 | out.push(if self.erase { 1.0 } else { 0.0 }); |
| 25 | out.push(self.path.len() as f32); |
| 26 | out.extend(self.path.iter().flatten()); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | const BEZIER_STEPS: usize = 24; |
| 31 | |
| 32 | fn flatten_polyline(points: &[PolyPoint]) -> Vec<[f32; 2]> { |
| 33 | let mut out: Vec<[f32; 2]> = points.first().map(|p| [p.x as f32, p.y as f32]).into_iter().collect(); |
| 34 | for pair in points.windows(2) { |
| 35 | let (a, b) = (&pair[0], &pair[1]); |
| 36 | if a.linear && b.linear { |
| 37 | out.push([b.x as f32, b.y as f32]); |
| 38 | continue; |
| 39 | } |
| 40 | let p = [[a.x, a.y], [a.x + a.rx, a.y + a.ry], [b.x + b.lx, b.y + b.ly], [b.x, b.y]]; |
| 41 | for k in 1..=BEZIER_STEPS { |
| 42 | let t = k as f64 / BEZIER_STEPS as f64; |
| 43 | let u = 1.0 - t; |
| 44 | let w = [u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t]; |
| 45 | out.push([0, 1].map(|i| (0..4).map(|j| w[j] * p[j][i]).sum::<f64>() as f32)); |
| 46 | } |
| 47 | } |
| 48 | out |
| 49 | } |
| 50 | |
| 51 | fn fuid(v: &LuaValue) -> Option<&str> { |
| 52 | v.as_table()?.get_index(1)?.as_str() |
| 53 | } |
| 54 | |
| 55 | /// Walks `head`'s `Paint` input chain and returns the strokes visible at `time`, oldest first. |
| 56 | fn collect_strokes(comp: &Comp, head: &Node, time: f64, expr: &ExprEngine) -> Vec<Stroke> { |
| 57 | let mut chain: Vec<&Node> = Vec::new(); |
| 58 | let mut next = head.connected_node("Paint"); |
| 59 | while let Some(n) = next.and_then(|s| comp.find_node(s)).map(|id| comp.node(id)) { |
| 60 | if chain.iter().any(|c| c.id == n.id) { |
| 61 | break; |
| 62 | } |
| 63 | chain.push(n); |
| 64 | next = n.connected_node("Paint"); |
| 65 | } |
| 66 | |
| 67 | let mut strokes = Vec::new(); |
| 68 | for node in chain.into_iter().rev().filter(|n| !n.bypassed) { |
| 69 | match node.kind.as_str() { |
| 70 | "PolylineStroke" => { |
| 71 | let path = flatten_polyline(&Engine::polyline_points(comp, node, time)); |
| 72 | strokes.extend(stroke_style(comp, node, time, expr, None).map(|mut s| { |
| 73 | s.write_on = [ |
| 74 | comp.eval_number(node, "WriteOnStart", time, 0.0, expr) as f32, |
| 75 | comp.eval_number(node, "WriteOnEnd", time, 1.0, expr) as f32, |
| 76 | ]; |
| 77 | s.path = path; |
| 78 | s |
| 79 | })); |
| 80 | } |
| 81 | "Multistroke" => { |
| 82 | let Some(list) = node.raw.get_table("Strokes") else { continue }; |
| 83 | for st in list.positional_values().filter_map(LuaValue::as_table) { |
| 84 | let params = st.get_table("Params"); |
| 85 | let Some(mut s) = stroke_style(comp, node, time, expr, params) else { continue }; |
| 86 | let start = st.get_f64("Time").unwrap_or(0.0); |
| 87 | let duration = st |
| 88 | .get_f64("Frames") |
| 89 | .unwrap_or_else(|| stroke_number(comp, node, time, expr, params, "Duration", 1.0)); |
| 90 | if time < start || time >= start + duration { |
| 91 | continue; |
| 92 | } |
| 93 | s.path = st |
| 94 | .positional_values() |
| 95 | .filter_map(LuaValue::as_table) |
| 96 | .map(|p| [p.get_f64("X").unwrap_or(0.0) as f32, p.get_f64("Y").unwrap_or(0.0) as f32]) |
| 97 | .collect(); |
| 98 | strokes.push(s); |
| 99 | } |
| 100 | } |
| 101 | _ => {} |
| 102 | } |
| 103 | } |
| 104 | strokes.retain(|s| !s.path.is_empty()); |
| 105 | strokes |
| 106 | } |
| 107 | |
| 108 | /// Per-stroke `Params` override the node's inputs of the same name. |
| 109 | fn stroke_number( |
| 110 | comp: &Comp, |
| 111 | node: &Node, |
| 112 | time: f64, |
| 113 | expr: &ExprEngine, |
| 114 | params: Option<&LuaTable>, |
| 115 | key: &str, |
| 116 | default: f64, |
| 117 | ) -> f64 { |
| 118 | params |
| 119 | .and_then(|p| p.get_table(key)) |
| 120 | .and_then(|t| t.get_f64("Value")) |
| 121 | .unwrap_or_else(|| comp.eval_number(node, key, time, default, expr)) |
| 122 | } |
| 123 | |
| 124 | /// Brush and apply-mode controls; `None` for apply modes this engine does not rasterize (e.g. `Clone`). |
| 125 | fn stroke_style( |
| 126 | comp: &Comp, |
| 127 | node: &Node, |
| 128 | time: f64, |
| 129 | expr: &ExprEngine, |
| 130 | params: Option<&LuaTable>, |
| 131 | ) -> Option<Stroke> { |
| 132 | let num = |key: &str, default: f64| stroke_number(comp, node, time, expr, params, key, default) as f32; |
| 133 | let mode = params |
| 134 | .and_then(|p| p.get_table("ApplyMode")?.get("Value")) |
| 135 | .and_then(fuid) |
| 136 | .or_else(|| node.static_str("ApplyMode")) |
| 137 | .or_else(|| node.raw.get_table("ApplyModes")?.get_index(1)?.as_str()) |
| 138 | .unwrap_or("PaintApplyColor"); |
| 139 | let erase = match mode { |
| 140 | "PaintApplyColor" | "PaintApplyMerge" => false, |
| 141 | "PaintApplyErase" => true, |
| 142 | _ => return None, |
| 143 | }; |
| 144 | let brush = node |
| 145 | .static_str("BrushShape") |
| 146 | .or_else(|| node.raw.get_table("Brushes")?.get_index(1)?.as_str()) |
| 147 | .unwrap_or("SoftBrush"); |
| 148 | let size = stroke_number( |
| 149 | comp, |
| 150 | node, |
| 151 | time, |
| 152 | expr, |
| 153 | params, |
| 154 | &format!("{brush}.Size"), |
| 155 | stroke_number(comp, node, time, expr, params, "Size", 0.01), |
| 156 | ) as f32; |
| 157 | let softness = if brush == "SoftBrush" { |
| 158 | num("SoftBrush.Softness", 0.5).clamp(0.0, 1.0) |
| 159 | } else { |
| 160 | 0.0 |
| 161 | }; |
| 162 | Some(Stroke { |
| 163 | path: Vec::new(), |
| 164 | write_on: [0.0, 1.0], |
| 165 | size, |
| 166 | softness, |
| 167 | color: [ |
| 168 | num("PaintApplyColor.Red", 1.0), |
| 169 | num("PaintApplyColor.Green", 1.0), |
| 170 | num("PaintApplyColor.Blue", 1.0), |
| 171 | ], |
| 172 | opacity: num(&format!("{mode}.Opacity"), 1.0).clamp(0.0, 1.0), |
| 173 | erase, |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | /// Brush stamp positions along `path` (pixels) between the `write_on` fractions of its arclength. |
| 178 | fn stamp_positions(path: &[[f32; 2]], write_on: [f32; 2], spacing: f32) -> Vec<[f32; 2]> { |
| 179 | const MAX_STAMPS_PER_STROKE: usize = 65_536; |
| 180 | let Some(&first) = path.first() else { return Vec::new() }; |
| 181 | let [start, end] = write_on.map(|v| v.clamp(0.0, 1.0)); |
| 182 | if !start.is_finite() || !end.is_finite() || end <= start { |
| 183 | return Vec::new(); |
| 184 | } |
| 185 | let mut cum = vec![0.0f32]; |
| 186 | for w in path.windows(2) { |
| 187 | cum.push(cum.last().unwrap() + (w[1][0] - w[0][0]).hypot(w[1][1] - w[0][1])); |
| 188 | } |
| 189 | let total = *cum.last().unwrap(); |
| 190 | if !total.is_finite() { |
| 191 | return Vec::new(); |
| 192 | } |
| 193 | if total < 1e-4 { |
| 194 | return if first[0].is_finite() && first[1].is_finite() { vec![first] } else { Vec::new() }; |
| 195 | } |
| 196 | let (d0, d1) = (start * total, end * total); |
| 197 | let span = d1 - d0; |
| 198 | if !span.is_finite() || span <= 1e-4 { |
| 199 | return Vec::new(); |
| 200 | } |
| 201 | let n = ((span / spacing.max(1e-4)).round().max(1.0) as usize).min(MAX_STAMPS_PER_STROKE); |
| 202 | let step = span / n as f32; |
| 203 | (0..=n) |
| 204 | .map(|k| { |
| 205 | let d = (d0 + k as f32 * step).min(d1); |
| 206 | let i = cum.partition_point(|&c| c < d).clamp(1, path.len() - 1); |
| 207 | let t = (d - cum[i - 1]) / (cum[i] - cum[i - 1]).max(1e-6); |
| 208 | [0, 1].map(|a| path[i - 1][a] + (path[i][a] - path[i - 1][a]) * t) |
| 209 | }) |
| 210 | .collect() |
| 211 | } |
| 212 | |
| 213 | /// Coverage of a round brush of radius `r` at distance `d` from its center (calibrated to Fusion 21.1 SoftBrush/CircleBrush). |
| 214 | fn brush_coverage(d: f32, r: f32, softness: f32) -> f32 { |
| 215 | if softness <= 0.0 { |
| 216 | return (r + 0.5 - d).clamp(0.0, 1.0); |
| 217 | } |
| 218 | let u = (d / r.max(1e-5)).clamp(0.0, 1.0); |
| 219 | if u >= 1.0 { |
| 220 | return 0.0; |
| 221 | } |
| 222 | let p = 1.75 / softness.max(1e-3); |
| 223 | (1.0 - u.powf(p)).max(0.0).powf(1.85) |
| 224 | } |
| 225 | |
| 226 | /// Rasterizes `strokes` in order into a premultiplied RGBA float layer, top row first. |
| 227 | fn render_strokes(strokes: &[Stroke], [w, h]: [u32; 2]) -> Vec<[f32; 4]> { |
| 228 | let mut layer = vec![[0.0f32; 4]; (w * h) as usize]; |
| 229 | for s in strokes { |
| 230 | let r = (s.size * w as f32 * 0.5).max(0.5); |
| 231 | let px: Vec<[f32; 2]> = s.path.iter().map(|p| [(0.5 + p[0]) * w as f32, (0.5 - p[1]) * h as f32]).collect(); |
| 232 | // Fusion default stamp spacing is ~0.205 * diameter (~0.41 * radius). |
| 233 | let stamps = stamp_positions(&px, s.write_on, (r * 0.41).max(0.5)); |
| 234 | let bounds = stamps.iter().fold([f32::MAX, f32::MAX, f32::MIN, f32::MIN], |b, p| { |
| 235 | [b[0].min(p[0]), b[1].min(p[1]), b[2].max(p[0]), b[3].max(p[1])] |
| 236 | }); |
| 237 | let x0 = (bounds[0] - r - 1.0).floor().max(0.0) as u32; |
| 238 | let y0 = (bounds[1] - r - 1.0).floor().max(0.0) as u32; |
| 239 | let x1 = ((bounds[2] + r + 1.0).ceil().max(0.0) as u32).min(w); |
| 240 | let y1 = ((bounds[3] + r + 1.0).ceil().max(0.0) as u32).min(h); |
| 241 | if stamps.is_empty() || x0 >= x1 || y0 >= y1 { |
| 242 | continue; |
| 243 | } |
| 244 | let bw = (x1 - x0) as usize; |
| 245 | let mut cov = vec![0.0f32; bw * (y1 - y0) as usize]; |
| 246 | for c in &stamps { |
| 247 | let (sx0, sx1) = (((c[0] - r - 1.0).floor().max(x0 as f32)) as u32, ((c[0] + r + 1.0).ceil().min(x1 as f32)) as u32); |
| 248 | let (sy0, sy1) = (((c[1] - r - 1.0).floor().max(y0 as f32)) as u32, ((c[1] + r + 1.0).ceil().min(y1 as f32)) as u32); |
| 249 | for y in sy0..sy1 { |
| 250 | for x in sx0..sx1 { |
| 251 | let d = (x as f32 + 0.5 - c[0]).hypot(y as f32 + 0.5 - c[1]); |
| 252 | let a_stamp = brush_coverage(d, r, s.softness) * s.opacity; |
| 253 | if a_stamp > 0.0 { |
| 254 | let v = &mut cov[(y - y0) as usize * bw + (x - x0) as usize]; |
| 255 | *v = a_stamp + *v * (1.0 - a_stamp); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | for y in y0..y1 { |
| 261 | for x in x0..x1 { |
| 262 | let a = cov[(y - y0) as usize * bw + (x - x0) as usize]; |
| 263 | let px = &mut layer[(y * w + x) as usize]; |
| 264 | if s.erase { |
| 265 | px.iter_mut().for_each(|c| *c *= 1.0 - a); |
| 266 | } else { |
| 267 | let color = [s.color[0], s.color[1], s.color[2], 1.0]; |
| 268 | for (c, k) in px.iter_mut().zip(color) { |
| 269 | *c = k * a + *c * (1.0 - a); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | layer |
| 276 | } |
| 277 | |
| 278 | fn to_rgba8(layer: &[[f32; 4]], map: impl Fn([f32; 4]) -> [f32; 4]) -> Vec<u8> { |
| 279 | layer.iter().flat_map(|&p| map(p).map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)).collect() |
| 280 | } |
| 281 | |
| 282 | impl Engine { |
| 283 | #[allow(clippy::too_many_arguments)] |
| 284 | pub(super) fn eval_paint( |
| 285 | &mut self, |
| 286 | comp: &Comp, |
| 287 | node: &Node, |
| 288 | time: f64, |
| 289 | frame_i: i64, |
| 290 | comp_dims: [u32; 2], |
| 291 | enc: &mut wgpu::CommandEncoder, |
| 292 | visiting: &mut HashSet<(NodeId, i64)>, |
| 293 | stats: &mut EvalStats, |
| 294 | ) -> CachedTexture { |
| 295 | let src = match node.connected_node("Input").and_then(|s| comp.find_node(s)) { |
| 296 | Some(id) => self.eval_node(comp, id, time, enc, visiting, stats), |
| 297 | None => self.alloc_tex(comp_dims, BitDepth::Int8, 0), |
| 298 | }; |
| 299 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 300 | let strokes = collect_strokes(comp, node, time, &self.expr); |
| 301 | if blend <= 1e-6 || strokes.is_empty() { |
| 302 | return src; |
| 303 | } |
| 304 | let src = self.crop_to_canvas(enc, src); |
| 305 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 306 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 307 | |
| 308 | let mut params = vec![blend]; |
| 309 | strokes.iter().for_each(|s| s.hash_into(&mut params)); |
| 310 | let mut u = UniformParams::default(); |
| 311 | u.p[0] = [0.5, 0.5, 1.0, 0.0]; |
| 312 | u.p[1] = [blend, 1.0, 0.0, 0.0]; |
| 313 | u.p[2] = [0.0, 0.0, 1.0, 0.0]; |
| 314 | u.p[3] = [ |
| 315 | if mask.is_some() { 1.0 } else { 0.0 }, |
| 316 | if mask_inv { 1.0 } else { 0.0 }, |
| 317 | src.size[0] as f32, |
| 318 | src.size[1] as f32, |
| 319 | ]; |
| 320 | u.p[6] = [0.0, 1.0, 0.0, 0.0]; |
| 321 | let key = self.make_key(node, "Paint", src.size, &[src.content_hash, mask_hash], &params); |
| 322 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 323 | stats.cache_hits += 1; |
| 324 | return hit; |
| 325 | } |
| 326 | stats.cache_misses += 1; |
| 327 | stats.nodes_evaluated += 1; |
| 328 | |
| 329 | let pixels = to_rgba8(&render_strokes(&strokes, src.size), |p| p); |
| 330 | let layer = create_texture(&self.device, src.size, 0); |
| 331 | self.queue.write_texture( |
| 332 | layer.texture.as_image_copy(), |
| 333 | &pixels, |
| 334 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(src.size[0] * 4), rows_per_image: Some(src.size[1]) }, |
| 335 | wgpu::Extent3d { width: src.size[0], height: src.size[1], depth_or_array_layers: 1 }, |
| 336 | ); |
| 337 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 338 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 339 | self.dispatch_op(enc, ShaderOp::Merge, &src.view, &layer.view, mv, &dst, &u); |
| 340 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 341 | dst |
| 342 | } |
| 343 | |
| 344 | #[allow(clippy::too_many_arguments)] |
| 345 | pub(super) fn eval_paint_mask( |
| 346 | &mut self, |
| 347 | comp: &Comp, |
| 348 | node: &Node, |
| 349 | time: f64, |
| 350 | frame_i: i64, |
| 351 | comp_dims: [u32; 2], |
| 352 | enc: &mut wgpu::CommandEncoder, |
| 353 | visiting: &mut HashSet<(NodeId, i64)>, |
| 354 | stats: &mut EvalStats, |
| 355 | ) -> CachedTexture { |
| 356 | let raw_size = self.explicit_mask_size(comp, node, time, comp_dims); |
| 357 | let max_dim = self.device.limits().max_texture_dimension_2d.max(1); |
| 358 | let (size, tex_scale) = crate::cache::clamp_dims_to_limit(raw_size[0], raw_size[1], max_dim); |
| 359 | let soft = comp.eval_number(node, "SoftEdge", time, 0.0, &self.expr).max(0.0) as f32; |
| 360 | let invert = comp.eval_number(node, "Invert", time, 0.0, &self.expr) > 0.5; |
| 361 | let level = comp.eval_number(node, "Level", time, 1.0, &self.expr) as f32; |
| 362 | let is_box = Self::mask_filter_is_box(node); |
| 363 | let clamp_edges = Self::mask_clamp_edges(node); |
| 364 | let paint_mode = Self::paint_mode_id(node); |
| 365 | let (prev_mask, _) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats); |
| 366 | let prev_hash = prev_mask.as_ref().map_or(0, |m| m.content_hash); |
| 367 | let strokes = collect_strokes(comp, node, time, &self.expr); |
| 368 | |
| 369 | let mut params = vec![soft, invert as u8 as f32, level, paint_mode, is_box as u8 as f32, clamp_edges]; |
| 370 | strokes.iter().for_each(|s| s.hash_into(&mut params)); |
| 371 | let key = self.make_key(node, "PaintMask", size, &[prev_hash], &params); |
| 372 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 373 | stats.cache_hits += 1; |
| 374 | return hit; |
| 375 | } |
| 376 | stats.cache_misses += 1; |
| 377 | stats.nodes_evaluated += 1; |
| 378 | |
| 379 | let pixels = to_rgba8(&render_strokes(&strokes, size), |p| [p[3]; 4]); |
| 380 | let post = MaskPost { |
| 381 | soft_px: soft * size[0] as f32, |
| 382 | is_box, |
| 383 | clamp_edges, |
| 384 | paint_mode, |
| 385 | invert, |
| 386 | level, |
| 387 | prev: prev_mask.as_ref(), |
| 388 | }; |
| 389 | let mut tex = self.upload_cpu_mask(enc, size, key, &pixels, Some(post)); |
| 390 | tex.tex_scale = tex_scale; |
| 391 | self.cache.insert(key, tex.clone(), node.id, frame_i); |
| 392 | tex |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | #[cfg(test)] |
| 397 | mod tests { |
| 398 | use super::*; |
| 399 | use crate::Engine; |
| 400 | |
| 401 | const POLYLINE: &str = "Polyline = Input { Value = Polyline { Points = { |
| 402 | { Linear = true, X = -0.25, Y = 0.25, RX = 0.1, RY = 0 }, |
| 403 | { Linear = true, X = 0.25, Y = 0.25, LX = -0.1, LY = 0 }, |
| 404 | } }, },"; |
| 405 | |
| 406 | fn comp_src(tools: &str) -> String { |
| 407 | format!( |
| 408 | r#"Composition {{ |
| 409 | CurrentTime = 0, RenderRange = {{ 0, 10 }}, GlobalRange = {{ 0, 10 }}, |
| 410 | Tools = ordered() {{ |
| 411 | BG = Background {{ Inputs = {{ |
| 412 | Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, |
| 413 | TopLeftAlpha = Input {{ Value = 1.0, }}, |
| 414 | }}, }}, |
| 415 | {tools} |
| 416 | }} |
| 417 | }}"# |
| 418 | ) |
| 419 | } |
| 420 | |
| 421 | fn stroke(path: &[[f32; 2]], softness: f32, erase: bool) -> Stroke { |
| 422 | Stroke { |
| 423 | path: path.to_vec(), |
| 424 | write_on: [0.0, 1.0], |
| 425 | size: 0.2, |
| 426 | softness, |
| 427 | color: [1.0, 0.5, 0.0], |
| 428 | opacity: 1.0, |
| 429 | erase, |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | fn alpha_at(layer: &[[f32; 4]], x: u32, y: u32) -> f32 { |
| 434 | layer[(y * 64 + x) as usize][3] |
| 435 | } |
| 436 | |
| 437 | #[test] |
| 438 | fn write_on_trims_stamps_by_arclength() { |
| 439 | let path = [[0.0, 10.0], [100.0, 10.0]]; |
| 440 | let half = stamp_positions(&path, [0.0, 0.5], 5.0); |
| 441 | assert_eq!(half.first(), Some(&[0.0, 10.0])); |
| 442 | assert!((half.last().unwrap()[0] - 50.0).abs() < 1e-3); |
| 443 | assert!(stamp_positions(&path, [0.5, 0.5], 5.0).is_empty()); |
| 444 | assert_eq!(stamp_positions(&[[3.0, 4.0], [3.0, 4.0]], [0.0, 1.0], 5.0), vec![[3.0, 4.0]]); |
| 445 | } |
| 446 | |
| 447 | #[test] |
| 448 | fn soft_brush_falls_off_and_hard_brush_is_flat() { |
| 449 | let dot = [[0.0, 0.0]]; |
| 450 | let hard = render_strokes(&[stroke(&dot, 0.0, false)], [64, 64]); |
| 451 | let soft = render_strokes(&[stroke(&dot, 1.0, false)], [64, 64]); |
| 452 | // radius = 0.2 * 64 / 2 = 6.4px around (32, 32) |
| 453 | assert_eq!(alpha_at(&hard, 32, 32), 1.0); |
| 454 | assert_eq!(alpha_at(&hard, 35, 32), 1.0); |
| 455 | assert_eq!(alpha_at(&hard, 40, 32), 0.0); |
| 456 | assert!(alpha_at(&soft, 32, 32) > 0.95); |
| 457 | let mid = alpha_at(&soft, 35, 32); |
| 458 | assert!(mid > 0.0 && mid < 1.0, "soft falloff {mid}"); |
| 459 | assert!(alpha_at(&soft, 37, 32) < mid); |
| 460 | assert_eq!(hard[(32 * 64 + 32) as usize], [1.0, 0.5, 0.0, 1.0]); |
| 461 | } |
| 462 | |
| 463 | #[test] |
| 464 | fn erase_removes_earlier_paint_only() { |
| 465 | let dot = [[0.0, 0.0]]; |
| 466 | let layer = render_strokes(&[stroke(&dot, 0.0, false), stroke(&[[0.5, 0.0]], 0.0, true)], [64, 64]); |
| 467 | assert_eq!(alpha_at(&layer, 32, 32), 1.0); |
| 468 | let erased = render_strokes(&[stroke(&dot, 0.0, false), stroke(&dot, 0.0, true)], [64, 64]); |
| 469 | assert_eq!(erased[(32 * 64 + 32) as usize], [0.0; 4]); |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn multistroke_is_visible_only_within_its_duration() { |
| 474 | let src = comp_src( |
| 475 | r#"Stroke = Multistroke { |
| 476 | Brushes = { "CircleBrush" }, |
| 477 | ApplyModes = { "PaintApplyColor", "PaintApplyErase" }, |
| 478 | Inputs = { BrushShape = Input { Value = FuID { "CircleBrush" }, }, }, |
| 479 | Strokes = { |
| 480 | { Time = 4, { X = 0, Y = 0, T = 0 }, { X = 0.1, Y = 0, T = 5 } }, |
| 481 | { Time = 6, Params = { Duration = { Value = 3 }, ApplyMode = { Value = FuID { "PaintApplyErase" } } }, |
| 482 | { X = 0, Y = 0, T = 0 } }, |
| 483 | { Time = 6, Params = { ApplyMode = { Value = FuID { "PaintApplyClone" } } }, { X = 0, Y = 0, T = 0 } }, |
| 484 | }, |
| 485 | }, |
| 486 | Paint1 = Paint { Inputs = { |
| 487 | Input = Input { SourceOp = "BG", Source = "Output", }, |
| 488 | Paint = Input { SourceOp = "Stroke", Source = "Out", }, |
| 489 | }, },"#, |
| 490 | ); |
| 491 | let comp = Comp::from_fusion_str(&src).expect("parse"); |
| 492 | let expr = ExprEngine::new(); |
| 493 | let paint = comp.node(comp.find_node("Paint1").unwrap()); |
| 494 | let count = |t: f64| collect_strokes(&comp, paint, t, &expr).iter().map(|s| s.erase).collect::<Vec<_>>(); |
| 495 | assert_eq!(count(3.0), Vec::<bool>::new()); |
| 496 | assert_eq!(count(4.0), vec![false]); |
| 497 | assert_eq!(count(5.0), Vec::<bool>::new()); |
| 498 | assert_eq!(count(7.0), vec![true]); |
| 499 | assert_eq!(count(9.0), Vec::<bool>::new()); |
| 500 | } |
| 501 | |
| 502 | #[test] |
| 503 | fn paint_composites_polyline_write_on_over_input() { |
| 504 | let src = comp_src(&format!( |
| 505 | r#"Stroke = PolylineStroke {{ |
| 506 | Brushes = {{ "CircleBrush" }}, ApplyModes = {{ "PaintApplyColor" }}, |
| 507 | Inputs = {{ |
| 508 | BrushShape = Input {{ Value = FuID {{ "CircleBrush" }}, }}, |
| 509 | ["CircleBrush.Size"] = Input {{ Value = 0.1, }}, |
| 510 | WriteOnEnd = Input {{ Value = 0.5, }}, |
| 511 | {POLYLINE} |
| 512 | }}, |
| 513 | }}, |
| 514 | Paint1 = Paint {{ Inputs = {{ |
| 515 | Input = Input {{ SourceOp = "BG", Source = "Output", }}, |
| 516 | Paint = Input {{ SourceOp = "Stroke", Source = "Out", }}, |
| 517 | }}, }}, |
| 518 | Mask1 = PaintMask {{ Inputs = {{ |
| 519 | MaskWidth = Input {{ Value = 64, }}, MaskHeight = Input {{ Value = 64, }}, |
| 520 | Paint = Input {{ SourceOp = "Stroke", Source = "Out", }}, |
| 521 | }}, }}, |
| 522 | Aux1 = Dimension.CopyAux {{ Inputs = {{ |
| 523 | Input = Input {{ SourceOp = "BG", Source = "Output", }}, |
| 524 | }}, }},"# |
| 525 | )); |
| 526 | let comp = Comp::from_fusion_str(&src).expect("parse"); |
| 527 | let Ok(mut eng) = Engine::new_headless() else { return }; |
| 528 | let mut read = |name: &str| { |
| 529 | let (tex, stats) = eng.evaluate(&comp, comp.find_node(name).unwrap(), 0.0); |
| 530 | assert!(stats.unsupported_nodes.is_empty(), "{:?}", stats.unsupported_nodes); |
| 531 | eng.readback_rgba(&tex).expect("readback") |
| 532 | }; |
| 533 | let px = |img: &[u8], x: usize, y: usize| <[u8; 4]>::try_from(&img[(y * 64 + x) * 4..][..4]).unwrap(); |
| 534 | |
| 535 | let painted = read("Paint1"); |
| 536 | assert_eq!(px(&painted, 20, 16), [255; 4], "written half is white"); |
| 537 | assert_eq!(px(&painted, 44, 16), [0, 0, 0, 255], "un-written half stays input"); |
| 538 | assert_eq!(px(&painted, 20, 48), [0, 0, 0, 255], "away from the stroke stays input"); |
| 539 | |
| 540 | let mask = read("Mask1"); |
| 541 | assert_eq!(px(&mask, 20, 16), [255; 4]); |
| 542 | assert_eq!(px(&mask, 44, 16), [0; 4]); |
| 543 | |
| 544 | assert_eq!(read("Aux1"), read("BG"), "CopyAux passes RGBA through"); |
| 545 | } |
| 546 | } |