Anchor the Motion Blur node's shear at the content's local origin instead of its bounds (#5046)

Anchor Motion Blur's shear at the content's local origin
This commit is contained in:
Keavon Chambers authored and GitHub committed 2026-11-29 17:04:43 -08:00
1 parent addba860d3
commit c75ebbd143
6 files changed
+63 -11

No files matched your search

@@ -213,11 +213,11 @@ impl EffectOperation {
}
}
/// Whether this operation takes the content's frame, which a motion blur measures its shears from, unless it changes nothing.
/// Whether this operation takes the content's frame, which effects relative to its extent measure from, unless it changes nothing.
pub fn takes_frame(&self) -> bool {
match self {
Self::MotionBlur { .. } => !self.changes_nothing(),
Self::RadialBlur { .. }
Self::MotionBlur { .. }
| Self::RadialBlur { .. }
| Self::DropShadow { .. }
| Self::InnerShadow { .. }
| Self::OuterGlow { .. }
@@ -10,7 +10,7 @@ use raster_types::parallel_rows::fill_rows;
pub const MAX_MOTION_BLUR_SAMPLES: f64 = 1024.;
/// For PSD interop, a motion blur shears the image so its line runs along whichever axis it's closer to (the major axis), averages a box along that axis, and shears back.
/// Each shear interpolates across the minor axis with [`keys_cubic`], measured from the frame's edge as PSD measures from the image's.
/// Each shear interpolates across the minor axis with [`keys_cubic`], measured from the content's origin, which for an image is the corner PSD measures from.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MotionBlurShear {
/// Whether the line is closer to vertical, so the shears move columns of pixels across rows rather than rows across columns.
@@ -7144,7 +7144,7 @@ mod tests {
}
}
// A motion blur measures its shears from the frame, unlike a shadow or a radial blur (centered from the content's origin), and a blur that changes nothing is left out
// A shadow takes no frame, nor do a motion blur and a radial blur, which measure from the content's origin
let motion_blur = |distance: f64| graphic_types::EffectOperation::MotionBlur { angle: 0., distance, gamma: false };
let radial_blur = graphic_types::EffectOperation::RadialBlur {
method: graphic_types::RadialBlurMethod::Spin,
@@ -7155,7 +7155,7 @@ mod tests {
let shadow = drop_shadow(180., 3., 2.);
let cases = [
(vec![shadow.clone()], false),
(vec![shadow.clone(), motion_blur(4.)], true),
(vec![shadow.clone(), motion_blur(4.)], false),
(vec![shadow.clone(), motion_blur(0.)], false),
(vec![shadow, radial_blur], false),
];
@@ -7174,7 +7174,7 @@ mod tests {
use graphic_types::raster_types::Image;
use graphic_types::{Effect, stamp_effect};
// An image with a motion blur, which takes its frame, turned after the blur was stamped and then flattened onto a line by its parent
// An image with a motion blur turned after the blur was stamped and then flattened onto a line by its parent
let stamped = DAffine2::from_scale(DVec2::new(120., 80.));
let turned = DAffine2::from_angle(0.1) * stamped;
let flattened = DAffine2::from_scale(DVec2::new(1., 0.));
@@ -240,7 +240,8 @@ impl EffectPasses {
fill: f32,
opacity: f32,
effect_to_pixels: DAffine2,
frame: Option<EffectFrame>,
// Used by the frame-taking effects in the next PRs
#[allow(unused_variables)] frame: Option<EffectFrame>,
paints: &[wgpu::Texture],
capture: Option<EffectCapture>,
) -> Vec<(wgpu::Texture, BlendMode)> {
@@ -718,8 +719,8 @@ impl EffectPasses {
});
}
EffectOperation::MotionBlur { angle, distance, gamma } => {
let anchor = frame.map_or(DVec2::ZERO, |frame| frame.to_pixels.translation);
let Some(shear) = MotionBlurShear::new(angle, distance, lengths_to_pixels, anchor) else {
// Anchored at the effect space's origin, which is the content's, so the shear moves with it
let Some(shear) = MotionBlurShear::new(angle, distance, lengths_to_pixels, effect_to_pixels.translation) else {
continue;
};
@@ -5113,6 +5113,57 @@ fn long_motion_blurs_spread_their_samples_alike_on_the_cpu_and_gpu() {
}
}
#[test]
fn skewed_motion_blurs_move_with_the_contents_origin() {
use core_types::ATTR_TRANSFORM;
use core_types::list::{ATTR_APPEARANCE, List};
use glam::{DMat2, DVec2, IVec2};
use graphic_types::{Appearance, Coverage, Effect, EffectOperation, Graphic, Vector, stamp_effect};
use rendering::{Render, RenderParams};
use vello_gpu::kurbo::Shape;
// A square around its origin, smeared along a slanted line, then skewed so its effects render along its skewed axes
let mut square = Vector::item_from_bezpath(&Rect::new(-16., -16., 16., 16.).to_path(0.1));
square.set_attribute(
ATTR_APPEARANCE,
Appearance::new_single(Coverage::new_fill(), Graphic::ColorList(List::new_from_element(core_types::Color::WHITE))),
);
stamp_effect(
&mut square,
Effect::new(&EffectOperation::MotionBlur {
angle: 30.,
distance: 12.,
gamma: false,
}),
);
// Stretching at most twofold, the skew moves those texels by whole texels when the square moves by whole pixels
let skew = DAffine2::from_mat2(DMat2::from_cols(DVec2::X, DVec2::new(1.5, 1.)));
let size = UVec2::new(256, 160);
let draw_at = |origin: DVec2| {
let graphic = Graphic::Vector(Box::new(square.clone().with_attribute(ATTR_TRANSFORM, DAffine2::from_translation(origin) * skew)));
render(size, |scene| graphic.render_to_vello(scene, DAffine2::IDENTITY, &RenderParams::default()))
};
let origin = DVec2::new(100., 70.);
let shift = IVec2::new(7, 3);
let (Some(before), Some(after)) = (draw_at(origin), draw_at(origin + shift.as_dvec2())) else {
return;
};
let smeared = before.iter().filter(|pixel| (8..248).contains(&pixel[3])).count();
assert!(smeared > 500, "the blur smears only {smeared} pixels");
// Its shears are measured from its origin, so the smear moves along with it
let mut worst = 0;
for y in 0..size.y - shift.y as u32 {
for x in 0..size.x - shift.x as u32 {
let (expected, actual) = (pixel(&before, size, x, y), pixel(&after, size, x + shift.x as u32, y + shift.y as u32));
worst = worst.max(expected.iter().zip(actual).map(|(&expected, actual)| expected.abs_diff(actual)).max().unwrap_or_default());
}
}
assert!(worst <= 1, "moved by {shift}, the smear differs by up to {worst} levels");
}
#[test]
fn radial_blurs_reach_as_far_inside_faded_groups_and_from_out_of_view() {
use core_types::list::{ATTR_APPEARANCE, ATTR_EFFECTS, Item, List};
@@ -42,7 +42,7 @@ fn clipped(color: vec4<f32>) -> vec4<f32> {
}
// A motion blur's passes measure texels along the major axis (x) and across it (y), which are the texture's y and x when `values.z` is 1.
// The line moves `values.x` across the minor axis per texel along the major axis, less the fraction `values.y` that measures it from the frame's edge, matching the CPU's `MotionBlurShear`.
// The line moves `values.x` across the minor axis per texel along the major axis, less the fraction `values.y` that measures it from the content's origin, matching the CPU's `MotionBlurShear`.
fn motion_axes(position: vec2<f32>) -> vec2<i32> {
let texel = vec2<i32>(position);
if parameters.values.z > 0.5 {