forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-aed45836b2e4092bb'

Browse files at this commit

3530c57c256085faf5106f8b2786532154cf45b0Ahmet Kaan GümüşParent: d14444579a9f49
crates/engine/src/project.rsmodified+6 -15
@@ -103,7 +103,7 @@ use crate::color::{ColorKey, MAX_COLOR_KEYS, evaluate_color};
103 use crate::transform::{103 use crate::transform::{
104 KeyEase, Keyed, MAX_TRANSFORM_KEYS, OpacityKey, TransformKey, TransformParams, ease_shape,104 KeyEase, Keyed, MAX_TRANSFORM_KEYS, OpacityKey, TransformKey, TransformParams, ease_shape,
105 evaluate as keyed_transform, evaluate_opacity, keys_after_still_head, keys_within,105 evaluate as keyed_transform, evaluate_opacity, keys_after_still_head, keys_within,
106- sample,106+ with_cut_key,
107 };107 };
108 108
109 /// How far down a lane's own volume goes ([`Project::set_lane_gain_db`]).109 /// How far down a lane's own volume goes ([`Project::set_lane_gain_db`]).
@@ -1228,21 +1228,12 @@ fn rekey_trimmed(c: &mut Clip, old: &Clip, edge: Edge, still: bool) {
1228 /// half keeps the keys before the cut and the right half the keys after it,1228 /// half keeps the keys before the cut and the right half the keys after it,
1229 /// and both get a key at `cut` holding the placement the whole animation gives1229 /// and both get a key at `cut` holding the placement the whole animation gives
1230 /// there, so each half still moves exactly as the clip did over its own frames.1230 /// there, so each half still moves exactly as the clip did over its own frames.
1231-/// The cut key takes the easing of the segment it sits in. Empty in, empty out.1231+/// A cut inside an eased segment splits its ease between the two halves
1232+/// ([`with_cut_key`]), so the motion keeps its exact shape. Empty in, empty out.
1232 pub fn split_transform_keys<K: Keyed>(keys: &[K], cut: u32) -> (Vec<K>, Vec<K>) {1233 pub fn split_transform_keys<K: Keyed>(keys: &[K], cut: u32) -> (Vec<K>, Vec<K>) {
1233- let Some(at) = sample(keys, f64::from(cut)) else {1234+ let list = with_cut_key(keys, cut);
1234- return (Vec::new(), Vec::new());1235+ let left: Vec<K> = list.iter().copied().filter(|k| k.at() <= cut).collect();
1235- };1236+ let right: Vec<K> = list.iter().copied().filter(|k| k.at() >= cut).collect();
1236- let ease = keys
1237- .iter()
1238- .rev()
1239- .find(|k| k.at() <= cut)
1240- .map_or(KeyEase::Linear, |k| k.ease());
1241- let boundary = at.placed(cut, ease);
1242- let mut left: Vec<K> = keys.iter().copied().filter(|k| k.at() < cut).collect();
1243- left.push(boundary);
1244- let mut right = vec![boundary];
1245- right.extend(keys.iter().copied().filter(|k| k.at() > cut));
1246 (left, right)1237 (left, right)
1247 }1238 }
1248 1239
crates/engine/src/transform.rsmodified+143 -35
@@ -275,36 +275,39 @@ pub fn ease_shape(ease: KeyEase, t: f32) -> f32 {
275 }275 }
276 }276 }
277 277
278-/// The height of a custom curve where its horizontal position is `t`. The278+/// One coordinate of the cubic from `0` to `1` whose inner control points are
279-/// curve's parameter is found by a few Newton steps, with bisection as the279+/// `p1` and `p2`, at curve parameter `s`.
280-/// fallback when the slope is too flat for Newton.280+fn cubic_coord(p1: f32, p2: f32, s: f32) -> f32 {
281-fn bezier_height(b: Bezier, t: f32) -> f32 {281+ let u = 1.0 - s;
282+ 3.0 * u * u * s * p1 + 3.0 * u * s * s * p2 + s * s * s
283+}
284+
285+/// The slope of [`cubic_coord`] with respect to `s`.
286+fn cubic_slope(p1: f32, p2: f32, s: f32) -> f32 {
287+ let u = 1.0 - s;
288+ 3.0 * u * u * p1 + 6.0 * u * s * (p2 - p1) + 3.0 * s * s * (1.0 - p2)
289+}
290+
291+/// The curve parameter `s` at which a cubic with horizontal inner controls
292+/// `x1` and `x2` is at horizontal position `t` (`0` at `t <= 0`, `1` at
293+/// `t >= 1`). A few Newton steps find it, with bisection as the fallback when
294+/// the slope is too flat for Newton.
295+fn curve_param(x1: f32, x2: f32, t: f32) -> f32 {
282 if t <= 0.0 {296 if t <= 0.0 {
283 return 0.0;297 return 0.0;
284 }298 }
285 if t >= 1.0 {299 if t >= 1.0 {
286 return 1.0;300 return 1.0;
287 }301 }
288- let [x1, y1, x2, y2] = b.points();
289- // One coordinate of the cubic from 0 to 1 with control points `p1` and `p2`, at parameter `s`.
290- let coord = |p1: f32, p2: f32, s: f32| {
291- let u = 1.0 - s;
292- 3.0 * u * u * s * p1 + 3.0 * u * s * s * p2 + s * s * s
293- };
294- // The slope of `coord` with respect to `s`.
295- let slope = |p1: f32, p2: f32, s: f32| {
296- let u = 1.0 - s;
297- 3.0 * u * u * p1 + 6.0 * u * s * (p2 - p1) + 3.0 * s * s * (1.0 - p2)
298- };
299 let mut s = t;302 let mut s = t;
300 let mut found = false;303 let mut found = false;
301 for _ in 0..8 {304 for _ in 0..8 {
302- let err = coord(x1, x2, s) - t;305+ let err = cubic_coord(x1, x2, s) - t;
303 if err.abs() < 1e-6 {306 if err.abs() < 1e-6 {
304 found = true;307 found = true;
305 break;308 break;
306 }309 }
307- let d = slope(x1, x2, s);310+ let d = cubic_slope(x1, x2, s);
308 if d.abs() < 1e-6 {311 if d.abs() < 1e-6 {
309 break;312 break;
310 }313 }
@@ -314,14 +317,80 @@ fn bezier_height(b: Bezier, t: f32) -> f32 {
314 let (mut lo, mut hi) = (0.0f32, 1.0f32);317 let (mut lo, mut hi) = (0.0f32, 1.0f32);
315 for _ in 0..32 {318 for _ in 0..32 {
316 s = 0.5 * (lo + hi);319 s = 0.5 * (lo + hi);
317- if coord(x1, x2, s) < t {320+ if cubic_coord(x1, x2, s) < t {
318 lo = s;321 lo = s;
319 } else {322 } else {
320 hi = s;323 hi = s;
321 }324 }
322 }325 }
323 }326 }
324- coord(y1, y2, s)327+ s
328+}
329+
330+/// The height of a custom curve where its horizontal position is `t`.
331+fn bezier_height(b: Bezier, t: f32) -> f32 {
332+ if t <= 0.0 {
333+ return 0.0;
334+ }
335+ if t >= 1.0 {
336+ return 1.0;
337+ }
338+ let [x1, y1, x2, y2] = b.points();
339+ cubic_coord(y1, y2, curve_param(x1, x2, t))
340+}
341+
342+/// The eases of the two halves of a segment eased by `ease` when it is cut
343+/// at fraction `t` of its way. The left half runs from the segment's start to
344+/// the cut and the right half from the cut to its end, and each half moves
345+/// exactly as the whole segment did over its own frames. Linear and hold are
346+/// their own halves. Any other ease is split as the cubic bezier it is: de
347+/// Casteljau at the parameter where its horizontal position is `t`, then each
348+/// half normalised back to the unit square. Control points are rounded to
349+/// thousandths by [`Bezier::new`], so each half matches the original to about
350+/// `1e-3` of the segment's span. A half whose value does not change takes
351+/// [`KeyEase::Linear`], since every ease moves it the same way. A `t` at or
352+/// beyond an end leaves that empty half [`KeyEase::Linear`] too.
353+pub fn split_ease(ease: KeyEase, t: f32) -> (KeyEase, KeyEase) {
354+ let [x1, y1, x2, y2] = match ease {
355+ KeyEase::Linear | KeyEase::Hold => return (ease, ease),
356+ KeyEase::Ease => [1.0 / 3.0, 0.0, 2.0 / 3.0, 1.0],
357+ KeyEase::EaseIn => [1.0 / 3.0, 0.0, 2.0 / 3.0, 0.0],
358+ KeyEase::EaseOut => [1.0 / 3.0, 1.0, 2.0 / 3.0, 1.0],
359+ KeyEase::Bezier(b) => b.points(),
360+ };
361+ if t <= 0.0 {
362+ return (KeyEase::Linear, ease);
363+ }
364+ if t >= 1.0 {
365+ return (ease, KeyEase::Linear);
366+ }
367+ // A point of the segment, de Casteljau style, between `a` and `b` at `u`.
368+ let mix = |a: (f32, f32), b: (f32, f32), u: f32| (a.0 + (b.0 - a.0) * u, a.1 + (b.1 - a.1) * u);
369+ let s = curve_param(x1, x2, t);
370+ let q0 = mix((0.0, 0.0), (x1, y1), s);
371+ let q1 = mix((x1, y1), (x2, y2), s);
372+ let q2 = mix((x2, y2), (1.0, 1.0), s);
373+ let r0 = mix(q0, q1, s);
374+ let r1 = mix(q1, q2, s);
375+ let yt = mix(r0, r1, s).1;
376+ const FLAT: f32 = 1e-6;
377+ let left = if yt <= FLAT {
378+ KeyEase::Linear
379+ } else {
380+ let c = |p: (f32, f32)| (p.0 / t, p.1 / yt);
381+ let (c1, c2) = (c(q0), c(r0));
382+ Bezier::new(c1.0.clamp(0.0, 1.0), c1.1.clamp(0.0, 1.0), c2.0.clamp(0.0, 1.0), c2.1.clamp(0.0, 1.0))
383+ .map_or(KeyEase::Linear, KeyEase::Bezier)
384+ };
385+ let right = if 1.0 - yt <= FLAT {
386+ KeyEase::Linear
387+ } else {
388+ let c = |p: (f32, f32)| ((p.0 - t) / (1.0 - t), (p.1 - yt) / (1.0 - yt));
389+ let (c1, c2) = (c(r1), c(q2));
390+ Bezier::new(c1.0.clamp(0.0, 1.0), c1.1.clamp(0.0, 1.0), c2.0.clamp(0.0, 1.0), c2.1.clamp(0.0, 1.0))
391+ .map_or(KeyEase::Linear, KeyEase::Bezier)
392+ };
393+ (left, right)
325 }394 }
326 395
327 /// One point of a clip's transform animation ([`crate::project::Clip::transform_keys`]):396 /// One point of a clip's transform animation ([`crate::project::Clip::transform_keys`]):
@@ -464,29 +533,50 @@ pub fn evaluate_opacity(base: f32, keys: &[OpacityKey], src: f64) -> f32 {
464 /// there is a key at the window's edge holding the placement the whole533 /// there is a key at the window's edge holding the placement the whole
465 /// animation gives there, so the visible part moves exactly as it did. An534 /// animation gives there, so the visible part moves exactly as it did. An
466 /// edge with nothing dropped gets no key: the animation already holds there.535 /// edge with nothing dropped gets no key: the animation already holds there.
467-/// Empty in, empty out.536+/// An edge key cuts the eased segment it sits in, so the kept part of that
537+/// segment keeps its exact shape (see [`with_cut_key`]). Empty in, empty out.
468 pub fn keys_within<K: Keyed>(keys: &[K], lo: u32, hi: u32) -> Vec<K> {538 pub fn keys_within<K: Keyed>(keys: &[K], lo: u32, hi: u32) -> Vec<K> {
469 if keys.is_empty() || lo > hi {539 if keys.is_empty() || lo > hi {
470 return Vec::new();540 return Vec::new();
471 }541 }
472- // The easing of the segment the edge sits in, as a split takes it (`split_transform_keys`).542+ let mut list = keys.to_vec();
473- let edge_key = |src: u32| {
474- sample(keys, f64::from(src)).expect("keyed, so a value").placed(
475- src,
476- keys.iter()
477- .rev()
478- .find(|k| k.at() <= src)
479- .map_or(KeyEase::Linear, |k| k.ease()),
480- )
481- };
482- let mut out = Vec::new();
483 if keys.iter().any(|k| k.at() < lo) && !keys.iter().any(|k| k.at() == lo) {543 if keys.iter().any(|k| k.at() < lo) && !keys.iter().any(|k| k.at() == lo) {
484- out.push(edge_key(lo));544+ list = with_cut_key(&list, lo);
485 }545 }
486- out.extend(keys.iter().copied().filter(|k| (lo..=hi).contains(&k.at())));
487 if hi > lo && keys.iter().any(|k| k.at() > hi) && !keys.iter().any(|k| k.at() == hi) {546 if hi > lo && keys.iter().any(|k| k.at() > hi) && !keys.iter().any(|k| k.at() == hi) {
488- out.push(edge_key(hi));547+ list = with_cut_key(&list, hi);
489 }548 }
549+ list.into_iter().filter(|k| (lo..=hi).contains(&k.at())).collect()
550+}
551+
552+/// `keys` with a key at source frame `cut` holding the value the animation
553+/// gives there, so a cut through the animation loses nothing. Cut inside an
554+/// eased segment, the key takes the right half of that segment's ease and the
555+/// earlier key takes the left half ([`split_ease`]), so each side keeps the
556+/// exact shape the whole segment had. Cut outside the segments, the key takes
557+/// the ease of the key before it, as a plain hold would. `keys` unchanged if a
558+/// key already sits at `cut`. Empty in, empty out.
559+pub fn with_cut_key<K: Keyed>(keys: &[K], cut: u32) -> Vec<K> {
560+ let Some(at) = sample(keys, f64::from(cut)) else {
561+ return Vec::new();
562+ };
563+ let i = keys.partition_point(|k| k.at() < cut);
564+ if keys.get(i).is_some_and(|k| k.at() == cut) {
565+ return keys.to_vec();
566+ }
567+ let mut out = keys.to_vec();
568+ let ease = if i > 0 && i < keys.len() {
569+ let (a, b) = (keys[i - 1], keys[i]);
570+ let t = ((f64::from(cut) - f64::from(a.at())) / f64::from(b.at() - a.at())) as f32;
571+ let (left, right) = split_ease(a.ease(), t);
572+ out[i - 1] = a.placed(a.at(), left);
573+ right
574+ } else if i > 0 {
575+ keys[i - 1].ease()
576+ } else {
577+ KeyEase::Linear
578+ };
579+ out.insert(i, at.placed(cut, ease));
490 out580 out
491 }581 }
492 582
@@ -904,11 +994,29 @@ mod tests {
904 let keys = [ok(0, 0.0, KeyEase::EaseIn), ok(100, 1.0, KeyEase::Linear)];994 let keys = [ok(0, 0.0, KeyEase::EaseIn), ok(100, 1.0, KeyEase::Linear)];
905 let within = keys_within(&keys, 20, 80);995 let within = keys_within(&keys, 20, 80);
906 assert_eq!(within.iter().map(|k| k.at).collect::<Vec<_>>(), vec![20, 80]);996 assert_eq!(within.iter().map(|k| k.at).collect::<Vec<_>>(), vec![20, 80]);
907- assert_eq!(within[0].ease, KeyEase::EaseIn, "an edge key keeps the segment's ease");997+ assert!(
998+ matches!(within[0].ease, KeyEase::Bezier(_)),
999+ "an edge key takes the right half of the cut segment's ease"
1000+ );
908 let want = evaluate_opacity(1.0, &keys, 20.0);1001 let want = evaluate_opacity(1.0, &keys, 20.0);
909 assert!((within[0].opacity - want).abs() < 1e-7);1002 assert!((within[0].opacity - want).abs() < 1e-7);
910 }1003 }
911 1004
1005+ #[test]
1006+ fn a_cut_through_an_ease_in_keeps_the_motion() {
1007+ use crate::project::split_transform_keys;
1008+ let ok = |at, opacity, ease| OpacityKey { at, opacity, ease };
1009+ // An ease-in from 0 to 100 over frames 0..100, cut at frame 40.
1010+ let keys = [ok(0, 0.0, KeyEase::EaseIn), ok(100, 100.0, KeyEase::Linear)];
1011+ let (left, right) = split_transform_keys(&keys, 40);
1012+ for f in [20.0, 40.0, 70.0] {
1013+ let half = if f <= 40.0 { &left } else { &right };
1014+ let got = sample(half, f).expect("keyed").opacity;
1015+ let want = sample(&keys, f).expect("keyed").opacity;
1016+ assert!((got - want).abs() < 0.5, "frame {f}: {got} against {want}");
1017+ }
1018+ }
1019+
912 #[test]1020 #[test]
913 fn corner_offset_matches_the_documented_examples() {1021 fn corner_offset_matches_the_documented_examples() {
914 // 16:9 on 16:9 at scale 0.3: half-width 0.15, half-height 0.15. The 0.031022 // 16:9 on 16:9 at scale 0.3: half-width 0.15, half-height 0.15. The 0.03