diff --git a/crates/engine/src/eval/shapes_warp.rs b/crates/engine/src/eval/shapes_warp.rs index 0a2b8fd5c9ffaa097f89cd7dd61e427092c77393..7688d5f8f73b9cd2edc2776117a8f913427705f1 100644 --- a/crates/engine/src/eval/shapes_warp.rs +++ b/crates/engine/src/eval/shapes_warp.rs @@ -92,6 +92,7 @@ enum ShapeTree { Group(Vec), Boolean { op: ShapeBoolOp, + color: [f32; 4], a: Box, b: Box, }, @@ -145,9 +146,12 @@ impl ShapeTree { ch.hash_into(h); } } - ShapeTree::Boolean { op, a, b } => { + ShapeTree::Boolean { op, color, a, b } => { 2u8.hash(h); (*op as u8).hash(h); + for c in color { + c.to_bits().hash(h); + } a.hash_into(h); b.hash_into(h); } @@ -332,6 +336,11 @@ fn collect_shape_tree( } "sBoolean" => { let op = parse_bool_op(comp, node, time, expr); + let r = comp.eval_number(node, "Red", time, 1.0, expr) as f32; + let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32; + let b_col = comp.eval_number(node, "Blue", time, 1.0, expr) as f32; + let a_col = comp.eval_number(node, "Alpha", time, 1.0, expr) as f32; + let color = [r, g, b_col, a_col]; let a = node .connected_node("Input1") .or_else(|| node.connected_node("Input")) @@ -348,6 +357,7 @@ fn collect_shape_tree( match (a, b) { (Some(ta), Some(tb)) => Some(ShapeTree::Boolean { op, + color, a: Box::new(ta), b: Box::new(tb), }), @@ -395,8 +405,7 @@ fn build_primitive_commands(kind: ShapePrimKind, xform: Affine2D, dims: [u32; 2] return cmds; } for k in 0..n { - let theta = std::f32::consts::FRAC_PI_2 - + (k as f32) * std::f32::consts::TAU / (n as f32); + let theta = (k as f32) * std::f32::consts::TAU / (n as f32); let v = to_px([rx * theta.cos(), ry * theta.sin()]); if k == 0 { cmds.push(zeno::Command::MoveTo(v)); @@ -566,13 +575,14 @@ fn rasterize_shape_tree_f32( rasterize_shape_tree_f32(ch, dims, dst, cov_buf, scratch); } } - ShapeTree::Boolean { op, a, b } => { + ShapeTree::Boolean { op, color, a, b } => { let len = dst.len(); let mut buf_a = vec![0.0f32; len]; let mut buf_b = vec![0.0f32; len]; rasterize_shape_tree_f32(a, dims, &mut buf_a, cov_buf, scratch); rasterize_shape_tree_f32(b, dims, &mut buf_b, cov_buf, scratch); let mut comb = vec![0.0f32; len]; + let ca = color[3].clamp(0.0, 1.0); for ((ca_p, cb_p), out_p) in buf_a .chunks_exact(4) .zip(buf_b.chunks_exact(4)) @@ -580,48 +590,17 @@ fn rasterize_shape_tree_f32( { let aa = ca_p[3].clamp(0.0, 1.0); let ba = cb_p[3].clamp(0.0, 1.0); - let unpre_a = if aa > 1e-5 { - [ca_p[0] / aa, ca_p[1] / aa, ca_p[2] / aa] - } else { - [0.0; 3] + let oa = match op { + ShapeBoolOp::Union => (aa + ba - aa * ba).clamp(0.0, 1.0), + ShapeBoolOp::Intersect => (aa * ba).clamp(0.0, 1.0), + ShapeBoolOp::Subtract => (aa * (1.0 - ba)).clamp(0.0, 1.0), + ShapeBoolOp::Xor => (aa * (1.0 - ba) + ba * (1.0 - aa)).clamp(0.0, 1.0), }; - let unpre_b = if ba > 1e-5 { - [cb_p[0] / ba, cb_p[1] / ba, cb_p[2] / ba] - } else { - [0.0; 3] - }; - match op { - ShapeBoolOp::Union => { - let inv_b = 1.0 - ba; - out_p[0] = cb_p[0] + ca_p[0] * inv_b; - out_p[1] = cb_p[1] + ca_p[1] * inv_b; - out_p[2] = cb_p[2] + ca_p[2] * inv_b; - out_p[3] = (ba + aa * inv_b).clamp(0.0, 1.0); - } - ShapeBoolOp::Intersect => { - let oa = aa * ba; - out_p[0] = unpre_a[0] * oa; - out_p[1] = unpre_a[1] * oa; - out_p[2] = unpre_a[2] * oa; - out_p[3] = oa; - } - ShapeBoolOp::Subtract => { - let oa = aa * (1.0 - ba); - out_p[0] = unpre_a[0] * oa; - out_p[1] = unpre_a[1] * oa; - out_p[2] = unpre_a[2] * oa; - out_p[3] = oa; - } - ShapeBoolOp::Xor => { - let wa = aa * (1.0 - ba); - let wb = ba * (1.0 - aa); - let oa = (wa + wb).clamp(0.0, 1.0); - out_p[0] = unpre_a[0] * wa + unpre_b[0] * wb; - out_p[1] = unpre_a[1] * wa + unpre_b[1] * wb; - out_p[2] = unpre_a[2] * wa + unpre_b[2] * wb; - out_p[3] = oa; - } - } + let sa = oa * ca; + out_p[0] = color[0] * sa; + out_p[1] = color[1] * sa; + out_p[2] = color[2] * sa; + out_p[3] = sa; } composite_over(dst, &comb); } @@ -668,9 +647,8 @@ impl Engine { }; let src = self.eval_node(comp, src_id, time, enc, visiting, stats); let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; - let strength = comp.eval_number(node, "Strength", time, 1.0, &self.expr) as f32; let amp = comp.eval_number(node, "Amplitude", time, 0.1, &self.expr) as f32; - if blend <= 1e-6 || (strength * amp).abs() <= 1e-7 { + if blend <= 1e-6 || amp.abs() <= 1e-7 { return src; } let src = self.crop_to_canvas(enc, src); @@ -684,7 +662,7 @@ impl Engine { let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); let mut u = UniformParams::default(); - u.p[0] = [center[0] as f32, center[1] as f32, strength, amp]; + u.p[0] = [center[0] as f32, center[1] as f32, 0.0, amp]; u.p[1] = [freq, aspect, phase, dampening]; u.p[2] = [shape, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }]; @@ -1054,4 +1032,183 @@ mod tests { // Top-left corner (0, 0) should be transparent assert_eq!(&px[0..4], &[0, 0, 0, 0], "corner should be transparent"); } + + #[test] + fn test_drip_shapes_and_strength_independence() { + let make_comp = |strength: f64, shape: i32| { + format!( + r#"Composition {{ + CurrentTime = 0, + RenderRange = {{ 0, 1 }}, + GlobalRange = {{ 0, 1 }}, + Tools = ordered() {{ + BG1 = Background {{ + Inputs = {{ + Width = Input {{ Value = 128, }}, + Height = Input {{ Value = 128, }}, + Type = Input {{ Value = FuID {{ "Horizontal" }}, }}, + TopLeftRed = Input {{ Value = 1.0, }}, + TopRightBlue = Input {{ Value = 1.0, }}, + }}, + }}, + Drip1 = Drip {{ + Inputs = {{ + Input = Input {{ SourceOp = "BG1", Source = "Output", }}, + Center = Input {{ Value = {{ 0.4, 0.6 }}, }}, + Strength = Input {{ Value = {strength}, }}, + Amplitude = Input {{ Value = 0.08, }}, + Frequency = Input {{ Value = 4.0, }}, + Phase = Input {{ Value = 0.75, }}, + Shape = Input {{ Value = {shape}, }}, + }}, + }}, + }}, + }}"# + ) + }; + let Ok(mut eng) = Engine::new_headless() else { + return; + }; + let comp_a = Comp::from_fusion_str(&make_comp(0.25, 1)).unwrap(); + let comp_b = Comp::from_fusion_str(&make_comp(1.0, 1)).unwrap(); + let (tex_a, _) = eng.evaluate(&comp_a, comp_a.find_node("Drip1").unwrap(), 0.0); + let (tex_b, _) = eng.evaluate(&comp_b, comp_b.find_node("Drip1").unwrap(), 0.0); + let px_a = eng.readback_rgba(&tex_a).unwrap(); + let px_b = eng.readback_rgba(&tex_b).unwrap(); + assert_eq!(px_a, px_b, "Fusion Drip ignores Strength and uses Amplitude directly"); + + // Center pixel (0.4*128=51, (1-0.6)*128=51) must remain smooth (raw_rel=0 -> disp=0) + let (bg_tex, _) = eng.evaluate(&comp_a, comp_a.find_node("BG1").unwrap(), 0.0); + let bg_px = eng.readback_rgba(&bg_tex).unwrap(); + let diff_sum: u64 = bg_px.iter().zip(px_a.iter()).map(|(&a, &b)| a.abs_diff(b) as u64).sum(); + assert!(diff_sum > 5_000, "Square Drip should displace pixels, got {diff_sum}"); + } + + #[test] + fn test_dent_type0_signed_inversion() { + let src = r#"Composition { + CurrentTime = 0, + RenderRange = { 0, 1 }, + GlobalRange = { 0, 1 }, + Tools = ordered() { + BG1 = Background { + Inputs = { + Width = Input { Value = 128, }, + Height = Input { Value = 128, }, + Type = Input { Value = FuID { "Horizontal" }, }, + TopLeftRed = Input { Value = 1.0, }, + TopLeftGreen = Input { Value = 0.0, }, + TopLeftBlue = Input { Value = 0.0, }, + TopRightRed = Input { Value = 0.0, }, + TopRightGreen = Input { Value = 1.0, }, + TopRightBlue = Input { Value = 0.0, }, + }, + }, + Dent1 = Dent { + Inputs = { + Input = Input { SourceOp = "BG1", Source = "Output", }, + Center = Input { Value = { 0.5, 0.5 }, }, + Size = Input { Value = 0.65, }, + Strength = Input { Value = 0.75, }, + Type = Input { Value = 0, }, + }, + }, + }, + }"#; + let comp = Comp::from_fusion_str(src).unwrap(); + let Ok(mut eng) = Engine::new_headless() else { + return; + }; + let (tex, _) = eng.evaluate(&comp, comp.find_node("Dent1").unwrap(), 0.0); + let px = eng.readback_rgba(&tex).unwrap(); + // Just right of center (x=72, r~0.066): new_r = r - 0.75*(0.325 - r) < 0, + // so it samples the LEFT (red-dominant) side! + let right_near = (64 * 128 + 72) * 4; + assert!( + px[right_near] > px[right_near + 1] + 40, + "Dent Type=0 should invert radially near center: {:?}", + &px[right_near..right_near + 4] + ); + } + + #[test] + fn test_s_boolean_uses_own_color_and_subtract_xor() { + let src = r#"Composition { + CurrentTime = 0, + RenderRange = { 0, 1 }, + GlobalRange = { 0, 1 }, + Tools = ordered() { + NGon1 = sNGon { + Inputs = { + Sides = Input { Value = 5, }, + Width = Input { Value = 0.5, }, + Height = Input { Value = 0.5, }, + Red = Input { Value = 1.0, }, + Green = Input { Value = 0.0, }, + Blue = Input { Value = 0.0, }, + }, + }, + Rect1 = sRectangle { + Inputs = { + Width = Input { Value = 0.35, }, + Height = Input { Value = 0.35, }, + ["Translate.X"] = Input { Value = 0.18, }, + Red = Input { Value = 0.0, }, + Green = Input { Value = 1.0, }, + Blue = Input { Value = 0.0, }, + }, + }, + BoolSub = sBoolean { + Inputs = { + Operation = Input { Value = FuID { "Subtract" }, }, + Input1 = Input { SourceOp = "Rect1", Source = "Output", }, + Input2 = Input { SourceOp = "NGon1", Source = "Output", }, + }, + }, + BoolXor = sBoolean { + Inputs = { + Operation = Input { Value = FuID { "Xor" }, }, + Input1 = Input { SourceOp = "NGon1", Source = "Output", }, + Input2 = Input { SourceOp = "Rect1", Source = "Output", }, + }, + }, + RSub = sRender { + Inputs = { + Width = Input { Value = 128, }, + Height = Input { Value = 128, }, + Input = Input { SourceOp = "BoolSub", Source = "Output", }, + }, + }, + RXor = sRender { + Inputs = { + Width = Input { Value = 128, }, + Height = Input { Value = 128, }, + Input = Input { SourceOp = "BoolXor", Source = "Output", }, + }, + }, + }, + }"#; + let comp = Comp::from_fusion_str(src).unwrap(); + let Ok(mut eng) = Engine::new_headless() else { + return; + }; + let (tex_sub, _) = eng.evaluate(&comp, comp.find_node("RSub").unwrap(), 0.0); + let (tex_xor, _) = eng.evaluate(&comp, comp.find_node("RXor").unwrap(), 0.0); + let px_sub = eng.readback_rgba(&tex_sub).unwrap(); + let px_xor = eng.readback_rgba(&tex_xor).unwrap(); + + // Far right of Rect (x=0.30 -> px=102, y=64): outside NGon (max x=0.25), inside Rect -> white! + let rect_only = (64 * 128 + 102) * 4; + assert_eq!(&px_sub[rect_only..rect_only + 4], &[255, 255, 255, 255]); + assert_eq!(&px_xor[rect_only..rect_only + 4], &[255, 255, 255, 255]); + + // Overlap region (x=0.10 -> px=77, y=64): subtracted/xored out -> transparent! + let overlap = (64 * 128 + 77) * 4; + assert_eq!(&px_sub[overlap..overlap + 4], &[0, 0, 0, 0]); + assert_eq!(&px_xor[overlap..overlap + 4], &[0, 0, 0, 0]); + + // Center (0,0 -> px=64, y=64): inside NGon, outside Rect (starts at 0.005) -> white in Xor! + let ngon_only = (64 * 128 + 54) * 4; + assert_eq!(&px_xor[ngon_only..ngon_only + 4], &[255, 255, 255, 255]); + } } diff --git a/crates/engine/src/eval/transform.rs b/crates/engine/src/eval/transform.rs index d9384405bfd424df03fef629394755f34f7e9ea9..db5595baf5efe70a3390673f96ed9e4b89aba163 100644 --- a/crates/engine/src/eval/transform.rs +++ b/crates/engine/src/eval/transform.rs @@ -867,6 +867,7 @@ impl Engine { if blend <= 1e-6 { return src; } + let src = self.crop_to_canvas(enc, src); let center = comp.eval_point(node, "Center", time, [0.5, 0.5], &self.expr); let pivot = comp.eval_point(node, "Pivot", time, [0.5, 0.5], &self.expr); let z_pivot = comp.eval_number(node, "ZPivot", time, 0.0, &self.expr); @@ -874,31 +875,48 @@ impl Engine { let rx = comp.eval_number(node, "XRotation", time, 0.0, &self.expr).to_radians(); let ry = comp.eval_number(node, "YRotation", time, 0.0, &self.expr).to_radians(); let rz = comp.eval_number(node, "ZRotation", time, 0.0, &self.expr).to_radians(); + let rot_order = node.static_str("RotationOrder").unwrap_or("ZYX"); let alpha_mode = comp.eval_number(node, "AlphaMode", time, 0.0, &self.expr).round() as i32; - let aspect = src.size[0] as f64 / src.size[1].max(1) as f64; + let aspect = src.canvas_size[0] as f64 / src.canvas_size[1].max(1) as f64; - // Fusion DVE rotates card corners around Pivot in ZYX order (Rz -> Ry -> Rx), then adds Center offset before perspective + // Fusion DVE rotates card corners around Pivot (default ZYX: Rz -> Ry -> Rx), + // scales (Center - 0.5) by ZMove so 2D screen shift at the card plane stays exact, + // and projects from Pivot with perspective 1 / (ZMove + z + ZPivot). let (sx, cx) = rx.sin_cos(); let (sy, cy) = ry.sin_cos(); let (sz, cz) = rz.sin_cos(); let project = |u_c: f64, v_c: f64| -> [f64; 2] { - let x0 = u_c - pivot[0]; - let y0 = (v_c - pivot[1]) / aspect; - let z0 = -z_pivot; - // 1. Rz (+Z CCW) - let x1 = x0 * cz - y0 * sz; - let y1 = x0 * sz + y0 * cz; - // 2. Ry (+Y tilts right away +z) - let x2 = x1 * cy - z0 * sy; - let z2 = x1 * sy + z0 * cy; - // 3. Rx (+X tilts bottom away +z) - let y3 = y1 * cx + z2 * sx; - let z3 = -y1 * sx + z2 * cx; - let z_cam = (z_move + z3 + z_pivot).max(0.05); + let mut x = u_c - pivot[0]; + let mut y = (v_c - pivot[1]) / aspect; + let mut z = -z_pivot; + for b in rot_order.bytes() { + match b { + b'X' | b'x' => { + let ny = y * cx + z * sx; + let nz = -y * sx + z * cx; + y = ny; + z = nz; + } + b'Y' | b'y' => { + let nx = x * cy - z * sy; + let nz = x * sy + z * cy; + x = nx; + z = nz; + } + b'Z' | b'z' => { + let nx = x * cz - y * sz; + let ny = x * sz + y * cz; + x = nx; + y = ny; + } + _ => {} + } + } + let z_cam = (z_move + z + z_pivot).max(0.05); let inv_z = 1.0 / z_cam; [ - pivot[0] + (x2 + (center[0] - 0.5)) * inv_z, - pivot[1] + ((y3 + (center[1] - 0.5) / aspect) * aspect) * inv_z, + pivot[0] + (x + (center[0] - 0.5) * z_move) * inv_z, + pivot[1] + ((y + (center[1] - 0.5) / aspect * z_move) * aspect) * inv_z, ] }; let bl = project(0.0, 0.0); @@ -906,7 +924,41 @@ impl Engine { let tr = project(1.0, 1.0); let tl = project(0.0, 1.0); let h_inv = Self::quad_inverse_homography(bl, br, tr, tl); - let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); + + // Expand DoD when projected card corners extend beyond canvas (so downstream Merge preserves edges) + let cw = src.canvas_size[0].max(1) as f64; + let ch = src.canvas_size[1].max(1) as f64; + let mut min_x = cw; + let mut max_x = 0.0_f64; + let mut min_y = ch; + let mut max_y = 0.0_f64; + for c in [bl, br, tr, tl] { + let px = c[0] * cw; + let py = (1.0 - c[1]) * ch; + min_x = min_x.min(px); + max_x = max_x.max(px); + min_y = min_y.min(py); + max_y = max_y.max(py); + } + let pad = 2_i32; + let max_pad = 1024_i32; + let x0 = if min_x < -0.25 { (min_x.floor() as i32 - pad).max(-max_pad) } else { 0 }; + let y0 = if min_y < -0.25 { (min_y.floor() as i32 - pad).max(-max_pad) } else { 0 }; + let x1 = if max_x > cw + 0.25 { + (max_x.ceil() as i32 + pad).min(src.canvas_size[0] as i32 + max_pad) + } else { + src.canvas_size[0] as i32 + }; + let y1 = if max_y > ch + 0.25 { + (max_y.ceil() as i32 + pad).min(src.canvas_size[1] as i32 + max_pad) + } else { + src.canvas_size[1] as i32 + }; + let dst_origin = [x0, y0]; + let dst_size = [(x1 - x0).max(1) as u32, (y1 - y0).max(1) as u32]; + let has_dst_dod = dst_origin != [0, 0] || dst_size != src.canvas_size; + + let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.canvas_size), enc, visiting, stats); let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); let mut u = UniformParams::default(); @@ -914,14 +966,22 @@ impl Engine { u.p[1] = [h_inv[4], h_inv[5], h_inv[6], h_inv[7]]; u.p[2] = [h_inv[8], blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }]; u.p[3] = [if alpha_mode == 1 { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0]; - let key = self.make_key(node, "DVE", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..4])); + if has_dst_dod { + u.p[4] = [ + dst_origin[0] as f32, + dst_origin[1] as f32, + src.canvas_size[0] as f32, + src.canvas_size[1] as f32, + ]; + } + let key = self.make_key(node, "DVE", dst_size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5])); if let Some(hit) = self.cache.get(key, node.id, frame_i) { stats.cache_hits += 1; return hit; } stats.cache_misses += 1; stats.nodes_evaluated += 1; - let dst = self.alloc_tex_dod(src.size, src.origin, src.canvas_size, src.depth, key); + let dst = self.alloc_tex_dod(dst_size, dst_origin, src.canvas_size, src.depth, key); let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); self.dispatch_op(enc, ShaderOp::Homography, &src.view, &self.dummy.view, mv, &dst, &u); self.cache.insert(key, dst.clone(), node.id, frame_i); @@ -1442,4 +1502,67 @@ mod tests { "cropped FG Mirror phase should reflect across FG bounds x=4 (far={out_far_mirrored:?} bluer than near={out_near_left_fg:?})" ); } + + #[test] + fn test_dve_pivot_center_zmove_and_dod_expansion() { + // 64x48 foreground transformed by DVE (ZMove=0.75 zooms card larger than 64x48) + // and merged over 128x128 black background must preserve pixels outside 64x48 via DoD! + let src = r#"Composition { + CurrentTime = 0, + RenderRange = { 0, 1 }, + GlobalRange = { 0, 1 }, + Tools = ordered() { + BG = Background { + Inputs = { + Width = Input { Value = 128, }, + Height = Input { Value = 128, }, + TopLeftAlpha = Input { Value = 1.0, }, + }, + }, + FG = Background { + Inputs = { + Width = Input { Value = 64, }, + Height = Input { Value = 48, }, + TopLeftRed = Input { Value = 1.0, }, + TopLeftGreen = Input { Value = 0.5, }, + TopLeftBlue = Input { Value = 0.0, }, + TopLeftAlpha = Input { Value = 1.0, }, + }, + }, + DVE1 = DVE { + Inputs = { + Input = Input { SourceOp = "FG", Source = "Output", }, + XRotation = Input { Value = 15.0, }, + YRotation = Input { Value = -25.0, }, + ZRotation = Input { Value = 10.0, }, + ZMove = Input { Value = 0.75, }, + Pivot = Input { Value = { 0.3, 0.6 }, }, + Center = Input { Value = { 0.45, 0.55 }, }, + }, + }, + Out = Merge { + Inputs = { + Background = Input { SourceOp = "BG", Source = "Output", }, + Foreground = Input { SourceOp = "DVE1", Source = "Output", }, + }, + }, + }, + }"#; + let comp = Comp::from_fusion_str(src).unwrap(); + let Ok(mut eng) = Engine::new_headless() else { + return; + }; + let out_id = comp.find_node("Out").unwrap(); + let (out_tex, stats) = eng.evaluate(&comp, out_id, 0.0); + assert!(stats.unsupported_nodes.is_empty()); + let px = eng.readback_rgba(&out_tex).unwrap(); + // Outside original 64x48 centered box (x in 32..96, y in 40..88) at x=110, y=64 + // must still show orange card due to ZMove=0.75 + YRotation=-25 DoD expansion! + let expanded_right = (64 * 128 + 110) * 4; + assert!( + px[expanded_right] > 200 && px[expanded_right + 1] > 100, + "expected expanded orange DVE region outside 64x48 at x=110, got {:?}", + &px[expanded_right..expanded_right + 4] + ); + } } diff --git a/crates/engine/src/shaders/archive_fx.wgsl b/crates/engine/src/shaders/archive_fx.wgsl index f91f82d1deeb48caec27793a3ed2e9cc71912c52..64690d5ac837c085541e82c23941a26f3a65eda5 100644 --- a/crates/engine/src/shaders/archive_fx.wgsl +++ b/crates/engine/src/shaders/archive_fx.wgsl @@ -5,7 +5,7 @@ // 1. Drip (concentric ripple warp) // p[0] = (center.x, center.y, strength, amplitude) // p[1] = (frequency, aspect, phase, dampening) -// p[2] = (shape: 0=Circle, 1=Square, 2=Horizontal, 3=Vertical, blend, has_mask, mask_invert) +// p[2] = (shape: 0=Circle, 1=Square, 2=Random, 3=Horizontal, 4=Vertical, blend, has_mask, mask_invert) @compute @workgroup_size(8, 8) fn drip_op(@builtin(global_invocation_id) gid: vec3) { let dims = textureDimensions(dst_tex); @@ -15,7 +15,6 @@ fn drip_op(@builtin(global_invocation_id) gid: vec3) { let orig = textureLoad(src_a, vec2(gid.xy), 0); let center = u.p[0].xy; - let strength = u.p[0].z; let amp = u.p[0].w; let freq = u.p[1].x; let drip_aspect = max(u.p[1].y, 1e-4); @@ -24,37 +23,35 @@ fn drip_op(@builtin(global_invocation_id) gid: vec3) { let shape = i32(u.p[2].x + 0.5); let blend = u.p[2].y; - let rel = vec2(fu.x - center.x, (fu.y - center.y) / (canvas_aspect * drip_aspect)); - var dist: f32 = 0.0; - var dir: vec2 = vec2(0.0); + let raw_rel = fu - center; + let rel = vec2(raw_rel.x / drip_aspect, raw_rel.y / canvas_aspect); - if (shape == 1) { - // Square ripples - dist = max(abs(rel.x), abs(rel.y)); - let rl = length(rel); - dir = select(vec2(0.0), rel / rl, rl > 1e-5); - } else if (shape == 2) { - // Horizontal wave bands - dist = abs(rel.y); - dir = vec2(0.0, select(-1.0, 1.0, rel.y >= 0.0)); - } else if (shape == 3) { - // Vertical wave bands - dist = abs(rel.x); - dir = vec2(select(-1.0, 1.0, rel.x >= 0.0), 0.0); - } else if (shape == 4) { - // Diamond / cross ripples - dist = abs(rel.x) + abs(rel.y); - let rl = length(rel); - dir = select(vec2(0.0), rel / rl, rl > 1e-5); + var src_fu = fu; + if (shape == 2) { + // Random jitter shape in Fusion Drip + let jx = (fg_hash(vec2(gid.xy), 13) - 0.5) * amp * 0.25; + let jy = (fg_hash(vec2(gid.xy), 29) - 0.5) * amp * 0.25; + src_fu = fu + vec2(jx, jy); } else { - // Circle ripples (shape == 0 and default) - dist = length(rel); - dir = select(vec2(0.0), rel / dist, dist > 1e-5); - } + var dist: f32 = 0.0; + if (shape == 1) { + // Square ripples + dist = max(abs(rel.x), abs(rel.y)); + } else if (shape == 3) { + // Horizontal wave bands + dist = abs(rel.y); + } else if (shape == 4) { + // Vertical wave bands + dist = abs(rel.x); + } else { + // Circle ripples (shape == 0 and default), normalized to half-diagonal 1/sqrt(2) + dist = length(rel) * 0.70710678118; + } - let falloff = exp(-dist * dampening * 2.0); - let disp = sin(dist * freq * 6.28318530718 + phase) * amp * strength * falloff; - let src_fu = fu - dir * disp * vec2(1.0, canvas_aspect); + let falloff = exp(-dist * dampening * 22.0); + let disp = sin((dist * freq - phase) * 6.28318530718) * amp * falloff; + src_fu = fu + raw_rel * disp; + } let warped = sample_edges(src_a, src_fu, 2); let m = read_mask(gid.xy, dims, u.p[2].z, u.p[2].w) * blend; @@ -86,25 +83,22 @@ fn dent_op(@builtin(global_invocation_id) gid: vec3) { let t = clamp(r / radius, 0.0, 1.0); var new_r = r; if (dent_type == 0) { - // Dent: smooth quadratic bulge/indent - let w = (1.0 - t) * (1.0 - t); - new_r = r * max(0.0, 1.0 - strength * w); + // Dent: linear radial displacement (signed across center) + new_r = r - strength * (radius - r); } else if (dent_type == 1) { - // Sine: sinusoidal radial displacement - let w = sin(t * 3.14159265359); - new_r = r * max(0.0, 1.0 - strength * w); + // Type 1: pi-scaled linear radial displacement + new_r = r - 3.14159265359 * strength * (radius - r); } else if (dent_type == 2) { // Pinch: power-law radial pinch/expand let p = select(1.0 / max(1.0 - strength, 0.05), 1.0 + strength, strength >= 0.0); new_r = radius * pow(t, p); } else if (dent_type == 3) { - // Punch: spherical dome punch - let w = sqrt(max(0.0, 1.0 - t * t)); - new_r = r * max(0.0, 1.0 - strength * w); + // Type 3: sqrt(3) * sqrt(t) * (1 - t) radial profile + new_r = r - strength * radius * 1.73205080757 * sqrt(t) * (1.0 - t); } else { - // Extended Fusion Dent modes (4..5): smooth quartic falloff - let w = (1.0 - t * t) * (1.0 - t * t); - new_r = r * max(0.0, 1.0 - strength * w); + // Type 4: (2 / pi) * cos^2(t * pi / 2) radial profile + let c = cos(t * 1.57079632679); + new_r = r - strength * radius * 0.63661977237 * c * c; } let scale = new_r / r; let src_fu = center + rel_w * scale * vec2(1.0, aspect); diff --git a/crates/engine/src/shaders/transform.wgsl b/crates/engine/src/shaders/transform.wgsl index 054a2da921e6589c7e4a956e7a8ea0c148711695..394af9ec66a74f99be88270ee0146b1d6cd296ab 100644 --- a/crates/engine/src/shaders/transform.wgsl +++ b/crates/engine/src/shaders/transform.wgsl @@ -237,13 +237,26 @@ fn xf_chroma_op(@builtin(global_invocation_id) gid: vec3) { // p[1] = (h11, h12, h20, h21) // p[2] = (h22, blend, has_mask, mask_invert) // p[3] = (ignore_alpha, 0, 0, 0) +// p[4] = (dst_dod_ox, dst_dod_oy, canvas_w, canvas_h) // ============================================================================ @compute @workgroup_size(8, 8) fn homography_op(@builtin(global_invocation_id) gid: vec3) { let dims = textureDimensions(dst_tex); if (gid.x >= dims.x || gid.y >= dims.y) { return; } - let fu = pix_to_fu(gid.xy, dims); - let orig = textureLoad(src_a, vec2(gid.xy), 0); + let has_dod = u.p[4].z > 0.5 && u.p[4].w > 0.5; + let fu = select( + pix_to_fu(gid.xy, dims), + vec2( + (f32(gid.x) + 0.5 + u.p[4].x) / u.p[4].z, + 1.0 - (f32(gid.y) + 0.5 + u.p[4].y) / u.p[4].w, + ), + has_dod, + ); + let orig = select( + textureLoad(src_a, vec2(gid.xy), 0), + sample_edges(src_a, fu, 0), + has_dod, + ); let sx = u.p[0].x * fu.x + u.p[0].y * fu.y + u.p[0].z; let sy = u.p[0].w * fu.x + u.p[1].x * fu.y + u.p[1].y; @@ -259,7 +272,11 @@ fn homography_op(@builtin(global_invocation_id) gid: vec3) { } } } - let m = read_mask(gid.xy, dims, u.p[2].z, u.p[2].w) * u.p[2].y; + let m = select( + read_mask(gid.xy, dims, u.p[2].z, u.p[2].w), + read_mask_uv(fu_to_uv(fu), u.p[2].z, u.p[2].w), + has_dod, + ) * u.p[2].y; let out_col = mix(orig, warped, clamp(m, 0.0, 1.0)); store_dst(vec2(gid.xy), out_col); }