FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Basic 3D: Distance to Image keeps its perspective at every render scale (fixes #1920)
Marco Arnulfoca35e1bb9db3
diff --git a/crates/render/src/adjustment_tests.rs b/crates/render/src/adjustment_tests.rsindex 54baf13e9..f81cc7b4e 100644--- a/crates/render/src/adjustment_tests.rs+++ b/crates/render/src/adjustment_tests.rs@@ -322,3 +322,44 @@ fn a_transition_with_an_adjustment_layer_is_not_planned_as_layers() {// halfway (frame 24): half blue, half the inverted red (cyan)assert!(near(at(&s, 24), [0.0, 0.5, 1.0], 1e-3), "{:?}", at(&s, 24));}++/// The picture's bounds (in sequence pixels) and the share of the frame it covers.+fn coverage(img: &Image, scale: f32) -> ([f32; 4], f32) {+ let (mut b, mut sum) = ([f32::MAX, f32::MAX, f32::MIN, f32::MIN], 0.0);+ for y in 0..img.h {+ for x in 0..img.w {+ let a = img.get(x, y)[3];+ sum += a;+ if a > 0.5 {+ let (sx, sy) = (x as f32 / scale, y as f32 / scale);+ b = [b[0].min(sx), b[1].min(sy), b[2].max(sx), b[3].max(sy)];+ }+ }+ }+ (b, sum / (img.w * img.h) as f32)+}++/// Basic 3D's Distance to Image is in sequence pixels: zooming the Program monitor out (a lower+/// render scale) shrinks the picture, it does not push the plane further away (#1920).+#[test]+fn basic_3d_keeps_its_perspective_at_every_render_scale() {+ let mut s = scene(2);+ let red = matte(&mut s.p, &mut s.map, [1.0, 0.0, 0.0, 1.0]);+ place(&mut s.p, s.seq, 0, red);+ let adj = adjustment(&mut s.p, s.seq, 1);+ fx(&mut s, adj, "basic_3d");+ let b3 = clip(&mut s, adj).effects.iter_mut().find(|e| e.effect == "basic_3d").unwrap();+ for (id, v) in [("swivel", -12.0), ("tilt", -15.0), ("distance", 15.0)] {+ b3.params.get_mut(id).unwrap().value = ParamValue::Float(v);+ }+ let at = |scale: f32| coverage(&render_sequence(&s.p, s.seq, R.tick_of(12), RenderOptions { scale, ..Default::default() }, &s.map), scale);+ let (full, full_cov) = at(1.0);+ assert!(full_cov < 0.9, "the plane is turned and moved away: {full_cov}");+ for scale in [0.5, 0.25] {+ let (b, cov) = at(scale);+ assert!((cov - full_cov).abs() < 0.02, "at {scale}: covers {cov}, {full_cov} at full resolution");+ for k in 0..4 {+ assert!((b[k] - full[k]).abs() < 6.0, "at {scale}: bounds {b:?}, {full:?} at full resolution");+ }+ }+}diff --git a/crates/render/src/effects.rs b/crates/render/src/effects.rsindex ce6a6a1af..d5dba3787 100644--- a/crates/render/src/effects.rs+++ b/crates/render/src/effects.rs@@ -277,7 +277,8 @@ pub fn apply(img: &mut Image, e: &EffectInstance, cx: &FxCtx) {"basic_3d" => {let sw = (f(e, "swivel", cx) as f64).to_radians();let tl = (f(e, "tilt", cx) as f64).to_radians();- let dist = f(e, "distance", cx) as f64;+ // authored in full-resolution pixels; the focal length follows the working image (#1920)+ let dist = f(e, "distance", cx) as f64 * cx.px_scale as f64;let (w, h) = (img.w as f64, img.h as f64);let focal = w.max(h) * 1.2;// inverse perspective mapping: ray through (x,y) intersected with rotated plane