Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge upstream main into fix/1010-orientation-followup

Kristian Myram7e6d76406b65
diff --cc crates/engine/src/commands/layer.rs
index c112a4912,ca8aa865d..123a81471
--- a/crates/engine/src/commands/layer.rs
+++ b/crates/engine/src/commands/layer.rs
@@@ -770,155 -769,11 +770,156 @@@ fn euler_zyx(r: [[f64; 4]; 4]) -> [f64
let cy = r[0][0].hypot(r[1][0]);
let y = (-r[2][0]).atan2(cy);
let (x, z) = if cy > 1e-9 { (r[2][1].atan2(r[2][2]), r[1][0].atan2(r[0][0])) } else { ((-r[1][2]).atan2(r[1][1]), 0.0) };
- Some(([x.to_degrees(), y.to_degrees(), z.to_degrees()], factor))
+ [x.to_degrees(), y.to_degrees(), z.to_degrees()]
+}
+
+/// The equivalent ZYX Euler triple nearest `reference`: each axis moved by whole turns, or the
+/// `(x + 180, 180 - y, z + 180)` form when that is nearer.
+fn nearest_euler(euler: [f64; 3], reference: [f64; 3]) -> [f64; 3] {
+ let near = |v: [f64; 3]| -> [f64; 3] { std::array::from_fn(|i| v[i] + ((reference[i] - v[i]) / 360.0).round() * 360.0) };
+ let natural = near(euler);
+ let flipped = near([euler[0] + 180.0, 180.0 - euler[1], euler[2] + 180.0]);
+ let distance = |v: [f64; 3]| v.iter().zip(reference.iter()).map(|(a, b)| (a - b).powi(2)).sum::<f64>();
+ if distance(flipped) < distance(natural) { flipped } else { natural }
+}
+
+/// The Euler Orientation of `correction * Orientation(o)`, in the form nearest `reference`.
+fn reexpress_euler(correction: effectcraft_geom::Mat4, o: [f64; 3], reference: [f64; 3]) -> [f64; 3] {
+ let world = correction * effectcraft_geom::Mat4::orientation(effectcraft_geom::Vec3::from(o));
+ nearest_euler(euler_zyx(world.0), reference)
+}
+
+/// Largest angle (degrees) a baked Orientation may drift from the exact rotation between its keys.
+const BAKE_TOLERANCE_DEGREES: f64 = 0.05;
+/// How many times a segment may be halved while baking, and the most keys a property may end up with.
+const BAKE_MAX_DEPTH: u32 = 10;
+const BAKE_MAX_KEYS: usize = 4096;
+
+/// The angle (degrees) between two rotations.
+fn rotation_angle(a: effectcraft_geom::Mat4, b: effectcraft_geom::Mat4) -> f64 {
+ let trace: f64 = (0..3).flat_map(|i| (0..3).map(move |j| (i, j))).map(|(i, j)| a.0[i][j] * b.0[i][j]).sum();
+ ((trace - 1.0) / 2.0).clamp(-1.0, 1.0).acos().to_degrees()
+}
+
+/// Adds keys between `(ta, ea)` and `(tb, eb)` (already converted) wherever the straight Euler blend
+/// between them drifts from `correction * Orientation(authored)` by more than the tolerance.
+fn bake_between(
+ correction: effectcraft_geom::Mat4,
+ authored: &effectcraft_project::Property,
+ (ta, ea): (Tick, [f64; 3]),
+ (tb, eb): (Tick, [f64; 3]),
+ depth: u32,
+ out: &mut Vec<effectcraft_keyframe::Keyframe>,
+) {
+ use effectcraft_geom::{Mat4, Vec3};
+ if depth == 0 || tb.0 - ta.0 < 2 || out.len() >= BAKE_MAX_KEYS {
+ return;
+ }
+ let tm = Tick(ta.0 + (tb.0 - ta.0) / 2);
+ let blend = [(ea[0] + eb[0]) / 2.0, (ea[1] + eb[1]) / 2.0, (ea[2] + eb[2]) / 2.0];
+ let exact = correction * Mat4::orientation(Vec3::from(authored.value_at(tm).as_vec3()));
+ if rotation_angle(Mat4::orientation(Vec3::from(blend)), exact) <= BAKE_TOLERANCE_DEGREES {
+ return;
+ }
+ let em = nearest_euler(euler_zyx(exact.0), blend);
+ bake_between(correction, authored, (ta, ea), (tm, em), depth - 1, out);
+ out.push(effectcraft_keyframe::Keyframe::new(tm, KV::Vec3(em)));
+ bake_between(correction, authored, (tm, em), (tb, eb), depth - 1, out);
+}
+
+/// Re-expresses Orientation as `correction * Orientation`. A Z-only correction shifts Z exactly in the
+/// value and every key. For any other correction a static value is converted directly; keys are
+/// converted, and keys are added between them wherever the Euler blend between two keys would drift
+/// from the exact rotation (the segments become linear, with the original easing baked in).
+fn reexpress_orientation(correction: effectcraft_geom::Mat4, pr: &mut effectcraft_project::Property) {
+ if let Some(turn) = z_only_turn(correction) {
+ // No turn (an identity correction) leaves every authored value as it is.
+ if turn.abs() <= 1e-12 {
+ return;
+ }
+ let shift = |value: &KV| {
+ let [x, y, z] = value.as_vec3();
+ KV::Vec3([x, y, z + turn])
+ };
+ pr.value = shift(&pr.value);
+ for key in &mut pr.keys {
+ key.value = shift(&key.value);
+ }
+ return;
+ }
+ let value = pr.value.as_vec3();
+ pr.value = KV::Vec3(reexpress_euler(correction, value, value));
+ if pr.keys.is_empty() {
+ return;
+ }
+ let authored = pr.clone();
+ // Each key keeps the authored change from the previous key on top of its converted value.
+ let mut converted: Vec<[f64; 3]> = vec![];
+ let mut prev: Option<([f64; 3], [f64; 3])> = None;
+ for key in &authored.keys {
+ let o = key.value.as_vec3();
+ let reference = match prev {
+ Some((before, after)) => [after[0] + o[0] - before[0], after[1] + o[1] - before[1], after[2] + o[2] - before[2]],
+ None => o,
+ };
+ let n = reexpress_euler(correction, o, reference);
+ converted.push(n);
+ prev = Some((o, n));
+ }
+ use effectcraft_keyframe::Interp;
+ let mut keys: Vec<effectcraft_keyframe::Keyframe> = vec![];
+ let mut after_baked_segment = false;
+ for (i, key) in authored.keys.iter().enumerate() {
+ let mut k = key.clone();
+ k.value = KV::Vec3(converted[i]);
+ if after_baked_segment {
+ k.in_interp = Interp::Linear;
+ k.in_ease.clear();
+ k.auto_bezier = false;
+ k.continuous = false;
+ }
+ after_baked_segment = false;
+ let segment = authored.keys.get(i + 1).filter(|_| key.out_interp != Interp::Hold);
+ let mut between = vec![];
+ if let Some(next) = segment {
+ bake_between(correction, &authored, (key.time, converted[i]), (next.time, converted[i + 1]), BAKE_MAX_DEPTH, &mut between);
+ // An eased segment, or one that gained keys, becomes a run of straight blends.
+ if !between.is_empty() || key.out_interp != Interp::Linear || next.in_interp != Interp::Linear {
+ k.out_interp = Interp::Linear;
+ k.out_ease.clear();
+ k.auto_bezier = false;
+ k.continuous = false;
+ after_baked_segment = true;
+ }
+ }
+ keys.push(k);
+ keys.extend(between);
+ }
+ pr.keys = keys;
+}
+
+/// Correction for a two-node rig's Orientation: after a uniform parent change its aim is
+/// `rotation * aim`, so the roll is re-expressed against that aim. `None` for a degenerate aim.
+fn aim_correction(rotation: effectcraft_geom::Mat4, aim: [f64; 3]) -> Option<effectcraft_geom::Mat4> {
+ use effectcraft_geom::vec3;
+ use effectcraft_render::three_d::camera::basis_rotation;
+ let [x, y, z] = aim;
+ let length = (x * x + y * y + z * z).sqrt();
+ if !length.is_finite() || length <= 1e-9 {
+ return None;
+ }
+ let dir = vec3(x / length, y / length, z / length);
+ let turned = rotation.apply_vec(dir);
+ // A view along the up axis has no roll to keep, and its basis is undefined.
+ if 1.0 - dir.y * dir.y < 1e-12 || 1.0 - turned.y * turned.y < 1e-12 {
+ return None;
+ }
+ let up = vec3(0.0, 1.0, 0.0);
+ Some(basis_rotation(turned, up).transpose() * rotation * basis_rotation(dir, up))
}
- /// A parent change retains 2D transforms and the world-space anchor of 3D AV layers. Position is
+ /// A parent change retains 2D transforms and the world-space anchor of 3D layers (Position,
+ /// and Point of Interest for two-node cameras and lights). Position is
/// re-expressed in the new parent's space (`None`: the composition), evaluated at the current
/// time.
pub(crate) fn parent_fixes(s: &Session, cid: ItemId, ids: &[LayerId], parent: Option<LayerId>) -> Vec<ParentFix> {