Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Fix MorphShape inaccuracy on complex paths #14089

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 76 additions & 47 deletions core/src/display_object/morph_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ruffle_render::backend::ShapeHandle;
use ruffle_render::commands::CommandHandler;
use std::cell::{Ref, RefCell, RefMut};
use std::sync::Arc;
use swf::{Fixed16, Fixed8, PointDelta, Twips};
use swf::{Fixed16, Fixed8, Twips};

#[derive(Clone, Collect, Copy)]
#[collect(no_drop)]
Expand Down Expand Up @@ -343,7 +343,14 @@ impl MorphShapeStatic {
continue;
}
_ => {
shape.push(lerp_edges(s, e, a, b));
shape.push(lerp_edges(
Point::new(start_x, start_y),
Point::new(end_x, end_y),
s,
e,
a,
b,
));
Self::update_pos(&mut start_x, &mut start_y, s);
Self::update_pos(&mut end_x, &mut end_y, e);
start = start_iter.next();
Expand Down Expand Up @@ -418,6 +425,13 @@ fn lerp_twips(start: Twips, end: Twips, a: f32, b: f32) -> Twips {
Twips::new((start.get() as f32 * a + end.get() as f32 * b).round() as i32)
}

fn lerp_point_twips(start: Point<Twips>, end: Point<Twips>, a: f32, b: f32) -> Point<Twips> {
Point::new(
lerp_twips(start.x, end.x, a, b),
lerp_twips(start.y, end.y, a, b),
)
}

fn lerp_fill(start: &swf::FillStyle, end: &swf::FillStyle, a: f32, b: f32) -> swf::FillStyle {
use swf::FillStyle;
match (start, end) {
Expand Down Expand Up @@ -483,81 +497,96 @@ fn lerp_fill(start: &swf::FillStyle, end: &swf::FillStyle, a: f32, b: f32) -> sw
}

fn lerp_edges(
start_pen: Point<Twips>,
end_pen: Point<Twips>,
start: &swf::ShapeRecord,
end: &swf::ShapeRecord,
a: f32,
b: f32,
) -> swf::ShapeRecord {
use swf::ShapeRecord;
let pen = lerp_point_twips(start_pen, end_pen, a, b);
match (start, end) {
(ShapeRecord::StraightEdge { delta: start }, ShapeRecord::StraightEdge { delta: end }) => {
(
ShapeRecord::StraightEdge { delta: start_delta },
ShapeRecord::StraightEdge { delta: end_delta },
) => {
let start_anchor = start_pen + *start_delta;
let end_anchor = end_pen + *end_delta;

let anchor = lerp_point_twips(start_anchor, end_anchor, a, b);

ShapeRecord::StraightEdge {
delta: PointDelta::new(
lerp_twips(start.dx, end.dx, a, b),
lerp_twips(start.dy, end.dy, a, b),
),
delta: anchor - pen,
}
}

(
ShapeRecord::CurvedEdge {
control_delta: start_control,
anchor_delta: start_anchor,
control_delta: start_control_delta,
anchor_delta: start_anchor_delta,
},
ShapeRecord::CurvedEdge {
control_delta: end_control,
anchor_delta: end_anchor,
control_delta: end_control_delta,
anchor_delta: end_anchor_delta,
},
) => ShapeRecord::CurvedEdge {
control_delta: PointDelta::new(
lerp_twips(start_control.dx, end_control.dx, a, b),
lerp_twips(start_control.dy, end_control.dy, a, b),
),
anchor_delta: PointDelta::new(
lerp_twips(start_anchor.dx, end_anchor.dx, a, b),
lerp_twips(start_anchor.dy, end_anchor.dy, a, b),
),
},
) => {
let start_control = start_pen + *start_control_delta;
let start_anchor = start_control + *start_anchor_delta;

let end_control = end_pen + *end_control_delta;
let end_anchor = end_control + *end_anchor_delta;

let control = lerp_point_twips(start_control, end_control, a, b);
let anchor = lerp_point_twips(start_anchor, end_anchor, a, b);

ShapeRecord::CurvedEdge {
control_delta: control - pen,
anchor_delta: anchor - control,
}
}

(
ShapeRecord::StraightEdge { delta: start },
ShapeRecord::StraightEdge { delta: start_delta },
ShapeRecord::CurvedEdge {
control_delta: end_control,
anchor_delta: end_anchor,
control_delta: end_control_delta,
anchor_delta: end_anchor_delta,
},
) => {
let start_control = *start / 2;
let start_anchor = start_control;
let start_control = start_pen + *start_delta / 2;
let start_anchor = start_pen + *start_delta;

let end_control = end_pen + *end_control_delta;
let end_anchor = end_control + *end_anchor_delta;

let control = lerp_point_twips(start_control, end_control, a, b);
let anchor = lerp_point_twips(start_anchor, end_anchor, a, b);

ShapeRecord::CurvedEdge {
control_delta: PointDelta::new(
lerp_twips(start_control.dx, end_control.dx, a, b),
lerp_twips(start_control.dy, end_control.dy, a, b),
),
anchor_delta: PointDelta::new(
lerp_twips(start_anchor.dx, end_anchor.dx, a, b),
lerp_twips(start_anchor.dy, end_anchor.dy, a, b),
),
control_delta: control - pen,
anchor_delta: anchor - control,
}
}

(
ShapeRecord::CurvedEdge {
control_delta: start_control,
anchor_delta: start_anchor,
control_delta: start_control_delta,
anchor_delta: start_anchor_delta,
},
ShapeRecord::StraightEdge { delta: end },
ShapeRecord::StraightEdge { delta: end_delta },
) => {
let end_control = *end / 2;
let end_anchor = end_control;
let start_control = start_pen + *start_control_delta;
let start_anchor = start_control + *start_anchor_delta;

let end_control = end_pen + *end_delta / 2;
let end_anchor = end_pen + *end_delta;

let control = lerp_point_twips(start_control, end_control, a, b);
let anchor = lerp_point_twips(start_anchor, end_anchor, a, b);

ShapeRecord::CurvedEdge {
control_delta: PointDelta::new(
lerp_twips(start_control.dx, end_control.dx, a, b),
lerp_twips(start_control.dy, end_control.dy, a, b),
),
anchor_delta: PointDelta::new(
lerp_twips(start_anchor.dx, end_anchor.dx, a, b),
lerp_twips(start_anchor.dy, end_anchor.dy, a, b),
),
control_delta: control - pen,
anchor_delta: anchor - control,
}
}
_ => unreachable!("{:?} {:?}", start, end),
Expand Down
50 changes: 50 additions & 0 deletions tests/tests/swfs/avm1/movieclip_hittest_shapeflag/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,53 @@ false
// _level0.clip.hitTest(400, 400, true)
false

// morph complex shape
// _level0.clip.hitTest(360, 300, true)
false
// _level0.clip.hitTest(380, 300, true)
false
// _level0.clip.hitTest(400, 300, true)
false
// _level0.clip.hitTest(420, 300, true)
true
// _level0.clip.hitTest(440, 300, true)
true
// _level0.clip.hitTest(460, 300, true)
true
// _level0.clip.hitTest(360, 320, true)
false
// _level0.clip.hitTest(380, 320, true)
true
// _level0.clip.hitTest(400, 320, true)
true
// _level0.clip.hitTest(420, 320, true)
true
// _level0.clip.hitTest(440, 320, true)
true
// _level0.clip.hitTest(460, 320, true)
true
// _level0.clip.hitTest(360, 340, true)
true
// _level0.clip.hitTest(380, 340, true)
true
// _level0.clip.hitTest(400, 340, true)
true
// _level0.clip.hitTest(420, 340, true)
true
// _level0.clip.hitTest(440, 340, true)
true
// _level0.clip.hitTest(460, 340, true)
true
// _level0.clip.hitTest(360, 360, true)
true
// _level0.clip.hitTest(380, 360, true)
true
// _level0.clip.hitTest(400, 360, true)
true
// _level0.clip.hitTest(420, 360, true)
true
// _level0.clip.hitTest(440, 360, true)
true
// _level0.clip.hitTest(460, 360, true)
true

Binary file modified tests/tests/swfs/avm1/movieclip_hittest_shapeflag/test.fla
Binary file not shown.
Binary file modified tests/tests/swfs/avm1/movieclip_hittest_shapeflag/test.swf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
num_frames = 12
num_frames = 13