diff --git a/crates/engine/src/tests/essential_more.rs b/crates/engine/src/tests/essential_more.rs index ea4aa42e5..c87d36eec 100644 --- a/crates/engine/src/tests/essential_more.rs +++ b/crates/engine/src/tests/essential_more.rs @@ -65,7 +65,7 @@ fn font_and_scale_controls_override_per_instance() { let mut v = vec![]; g.walk("", &mut |_, p| { if let Some(c) = essential::control_of(&p.match_id) { - v.push(essential::Override { control: c, value: p.value.clone() }); + v.push(essential::Override { control: c, value: p.value.clone(), keys: vec![] }); } }); v @@ -104,6 +104,164 @@ fn open_in_essential_graphics_sets_the_primary_comp() { assert_eq!(s.execute("essential.list", json!({})).unwrap()["name"], "A"); } +#[test] +fn instance_keyframes_reach_the_comp_so_expressions_can_look_ahead() { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Inner", "width": 32, "height": 32, "frameRate": 30, "duration": 2})).unwrap(); + let inner = s.active_comp_id().unwrap(); + let text = s.execute("layer.newText", json!({"text": "Hi"})).unwrap()["layer"].as_u64().unwrap(); + let ctl = s.execute("essential.addProperty", json!({"layer": text, "path": "transform/opacity"})).unwrap()["controls"][0].as_u64().unwrap(); + let keys = vec![ + effectcraft_keyframe::Keyframe::new(effectcraft_time::Tick::ZERO, KV::Scalar(0.0)), + effectcraft_keyframe::Keyframe::new(effectcraft_time::Tick::from_seconds_f64(1.0), KV::Scalar(100.0)), + ]; + let over = [essential::Override { control: ctl, value: KV::Scalar(25.0), keys: keys.clone() }]; + let p = essential::with_overrides(&s.project, inner, &over).unwrap(); + let pr = p.comp(inner).unwrap().layer(effectcraft_project::LayerId(text)).unwrap().props.prop("transform/opacity").unwrap(); + assert_eq!(pr.keys, keys); + assert_eq!(pr.value, KV::Scalar(25.0)); +} + +#[test] +fn keys_set_on_an_instance_are_passed_to_the_nested_comp_render() { + use effectcraft_project::LayerId; + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Inner", "width": 32, "height": 32, "frameRate": 30, "duration": 2})).unwrap(); + let inner = s.active_comp_id().unwrap(); + let solid = s.execute("layer.newSolid", json!({"name": "BG", "color": "#ffffff"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("essential.addProperty", json!({"layer": solid, "path": "transform/opacity"})).unwrap(); + s.execute("comp.new", json!({"name": "Scene", "width": 32, "height": 32, "frameRate": 30, "duration": 2})).unwrap(); + let scene = s.active_comp_id().unwrap(); + let inst = s.execute("layer.addItem", json!({"item": inner.0, "time": 0.0})).unwrap()["layer"].as_u64().unwrap(); + let path = s.execute("essential.instance", json!({"layer": inst})).unwrap()["controls"][0]["path"].as_str().unwrap().to_string(); + s.execute("prop.addKey", json!({"layer": inst, "path": path, "time": 0.0, "value": 0.0})).unwrap(); + s.execute("prop.addKey", json!({"layer": inst, "path": path, "time": 1.0, "value": 100.0})).unwrap(); + // At 0.25 s the nested comp's opacity carries the instance's keys, so it reads the instance's + // value half a second ahead too (valueAtTime), not only the current one. + let nested_opacity = |s: &Session| { + let comp = s.project.comp(scene).unwrap(); + let layer = comp.layer(LayerId(inst)).unwrap(); + let ctx = effectcraft_render::EvalCtx::new(&s.project, scene, comp, Tick::from_seconds_f64(0.25)); + let over = effectcraft_render::essential_overrides(&ctx, layer); + let p = essential::with_overrides(&s.project, inner, &over).unwrap(); + p.comp(inner).unwrap().layer(LayerId(solid)).unwrap().props.prop("transform/opacity").unwrap().clone() + }; + let pr = nested_opacity(&s); + let ahead = s.project.comp(scene).unwrap().layer(LayerId(inst)).unwrap().props.prop(&path).unwrap().value_at(Tick::from_seconds_f64(0.75)); + assert_eq!(pr.keys.len(), 2); + assert_eq!(pr.value_at(Tick::from_seconds_f64(0.75)), ahead); + assert_ne!(pr.value_at(Tick::from_seconds_f64(0.75)), pr.value_at(Tick::from_seconds_f64(0.25))); + // With Time Remap the instance's layer time is not the nested comp's time: only the value. + s.execute("layer.enableTimeRemap", json!({"layers": [inst]})).unwrap(); + assert!(nested_opacity(&s).keys.is_empty()); +} + +/// Inner comp with two opacity-exposed solids (the second linked to the first's control), an +/// instance of it in Scene keyed 0 -> 100 over 0..1 s. `timing` sets each solid's start (s) and +/// stretch (%). Returns the pieces the checks below need. +fn keyed_instance(timing: [(f64, f64); 2]) -> (Session, effectcraft_project::ItemId, effectcraft_project::ItemId, [u64; 2], u64) { + use effectcraft_project::LayerId; + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Inner", "width": 32, "height": 32, "frameRate": 30, "duration": 4})).unwrap(); + let inner = s.active_comp_id().unwrap(); + let a = s.execute("layer.newSolid", json!({"name": "A", "color": "#ffffff"})).unwrap()["layer"].as_u64().unwrap(); + let ctl = s.execute("essential.addProperty", json!({"layer": a, "path": "transform/opacity"})).unwrap()["controls"][0].as_u64().unwrap(); + let b = s.execute("layer.newSolid", json!({"name": "B", "color": "#ffffff"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("essential.linkProperty", json!({"control": ctl, "layer": b, "path": "transform/opacity"})).unwrap(); + for (id, (start, stretch)) in [a, b].into_iter().zip(timing) { + let l = std::sync::Arc::make_mut(&mut s.project).comp_mut(inner).unwrap().layer_mut(LayerId(id)).unwrap(); + l.start_time = Tick::from_seconds_f64(start); + l.stretch = stretch; + } + s.execute("comp.new", json!({"name": "Scene", "width": 32, "height": 32, "frameRate": 30, "duration": 4})).unwrap(); + let scene = s.active_comp_id().unwrap(); + let inst = s.execute("layer.addItem", json!({"item": inner.0, "time": 0.0})).unwrap()["layer"].as_u64().unwrap(); + let path = s.execute("essential.instance", json!({"layer": inst})).unwrap()["controls"][0]["path"].as_str().unwrap().to_string(); + s.execute("prop.addKey", json!({"layer": inst, "path": path, "time": 0.0, "value": 0.0})).unwrap(); + s.execute("prop.addKey", json!({"layer": inst, "path": path, "time": 1.0, "value": 100.0})).unwrap(); + (s, scene, inner, [a, b], inst) +} + +/// Opacity of `solid` inside Inner as the instance renders it at nested time `t`, and the +/// overridden property itself (its keys are what `numKeys` / `valueAtTime` read). +fn rendered( + s: &Session, + scene: effectcraft_project::ItemId, + inner: effectcraft_project::ItemId, + inst: u64, + solid: u64, + t: f64, +) -> (f64, effectcraft_project::Property) { + use effectcraft_project::LayerId; + let comp = s.project.comp(scene).unwrap(); + let layer = comp.layer(LayerId(inst)).unwrap(); + let ctx = effectcraft_render::EvalCtx::new(&s.project, scene, comp, Tick::from_seconds_f64(t)); + let over = effectcraft_render::essential_overrides(&ctx, layer); + let p = essential::with_overrides(&s.project, inner, &over).unwrap(); + let ic = p.comp(inner).unwrap(); + let il = ic.layer(LayerId(solid)).unwrap(); + let pr = il.props.prop("transform/opacity").unwrap().clone(); + let ictx = effectcraft_render::EvalCtx::new(&p, inner, ic, Tick::from_seconds_f64(t)); + (ictx.value(il, &pr).as_f64(), pr) +} + +#[test] +fn instance_keys_follow_a_nested_layer_that_starts_late() { + let (s, scene, inner, [a, _], inst) = keyed_instance([(0.5, 100.0), (0.0, 100.0)]); + // The instance reads 75 at 0.75 s; the layer starting at 0.5 s must too, not 25. + let (v, pr) = rendered(&s, scene, inner, inst, a, 0.75); + assert!((v - 75.0).abs() < 1e-6, "rendered {v}"); + assert_eq!(pr.keys.len(), 2); + // Layer time: the 0 s and 1 s keys sit at -0.5 s and 0.5 s. + assert_eq!(pr.keys[0].time, Tick::from_seconds_f64(-0.5)); + assert_eq!(pr.keys[1].time, Tick::from_seconds_f64(0.5)); + // What valueAtTime reads: layer time 0.5 s is the end key, -0.5 s the start key. + assert_eq!(pr.value_at(Tick::from_seconds_f64(0.5)), KV::Scalar(100.0)); + assert_eq!(pr.value_at(Tick::from_seconds_f64(-0.5)), KV::Scalar(0.0)); +} + +#[test] +fn instance_keys_follow_a_stretched_nested_layer() { + let (s, scene, inner, [a, _], inst) = keyed_instance([(0.0, 200.0), (0.0, 100.0)]); + // Stretched to 200 %: nested time 0.5 s is layer time 0.25 s. + let (v, pr) = rendered(&s, scene, inner, inst, a, 0.5); + assert!((v - 50.0).abs() < 1e-6, "rendered {v}"); + assert_eq!(pr.keys.len(), 2); + assert_eq!(pr.keys[1].time, Tick::from_seconds_f64(0.5)); + assert_eq!(pr.value_at(Tick::from_seconds_f64(0.5)), KV::Scalar(100.0)); + // Offset and stretch together: starts at 0.25 s, 50 %: nested 0.5 s is layer time 0.5 s. + let (s, scene, inner, [a, _], inst) = keyed_instance([(0.25, 50.0), (0.0, 100.0)]); + let (v, pr) = rendered(&s, scene, inner, inst, a, 0.5); + assert!((v - 50.0).abs() < 1e-6, "rendered {v}"); + assert_eq!(pr.keys[0].time, Tick::from_seconds_f64(-0.5)); + assert_eq!(pr.keys[1].time, Tick::from_seconds_f64(1.5)); +} + +#[test] +fn one_control_with_targets_of_different_timing_stays_in_step() { + let (s, scene, inner, [a, b], inst) = keyed_instance([(0.5, 100.0), (0.25, 200.0)]); + for t in [0.2, 0.6, 0.75, 0.9] { + let (va, pa) = rendered(&s, scene, inner, inst, a, t); + let (vb, pb) = rendered(&s, scene, inner, inst, b, t); + let want = t * 100.0; + assert!((va - want).abs() < 1e-6, "A at {t}: {va}"); + assert!((vb - want).abs() < 1e-6, "B at {t}: {vb}"); + // Each target carries the keys in its own time. + assert_ne!(pa.keys[0].time, pb.keys[0].time); + assert_ne!(pa.keys[1].time, pb.keys[1].time); + } +} + +#[test] +fn instance_keys_stay_out_of_a_nested_comp_that_preserves_its_frame_rate() { + let (mut s, scene, inner, [a, _], inst) = keyed_instance([(0.0, 100.0), (0.0, 100.0)]); + std::sync::Arc::make_mut(&mut s.project).comp_mut(inner).unwrap().preserve_frame_rate = true; + // The nested time is floored to the nested comp's frames, so only the instance's value goes in. + let (v, pr) = rendered(&s, scene, inner, inst, a, 0.25); + assert!((v - 25.0).abs() < 1e-6, "rendered {v}"); + assert!(pr.keys.is_empty()); +} + /// #1747: through a collapsed precomp, a Text override and a Font override of the same Source /// Text both apply: the Font merges into the overridden text, as `essential::with_overrides` /// does on the flattened path, instead of one entry replacing the other. @@ -133,3 +291,50 @@ fn collapsed_instance_merges_text_and_font_overrides() { o => panic!("{o:?}"), } } + +#[test] +fn instance_keys_stay_out_of_a_layer_that_blends_frames() { + let (mut s, scene, inner, [a, _], inst) = keyed_instance([(0.0, 100.0), (0.0, 100.0)]); + { + let comp = std::sync::Arc::make_mut(&mut s.project).comp_mut(scene).unwrap(); + comp.enable_frame_blending = true; + comp.layer_mut(effectcraft_project::LayerId(inst)).unwrap().switches.frame_blend = effectcraft_project::FrameBlend::FrameMix; + } + // Frame Mix renders the nested comp at two other times, so only the instance's current value goes in. + let (v, pr) = rendered(&s, scene, inner, inst, a, 0.25); + assert!((v - 25.0).abs() < 1e-6, "rendered {v}"); + assert!(pr.keys.is_empty()); +} + +/// An override from a collapsed parent reaches the nested instance as a value only: the keys the +/// nested instance property carries itself must not be copied onto its own target. +#[test] +fn collapsed_parent_override_replaces_the_nested_instance_keys() { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Inner", "width": 32, "height": 32, "frameRate": 30, "duration": 4})).unwrap(); + let a = s.execute("layer.newSolid", json!({"name": "A", "color": "#ffffff"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("essential.addProperty", json!({"layer": a, "path": "transform/opacity"})).unwrap(); + s.execute("comp.new", json!({"name": "Mid", "width": 32, "height": 32, "frameRate": 30, "duration": 4})).unwrap(); + let mid = s.active_comp_id().unwrap(); + let inner = s.project.find_by_name("Inner").unwrap().id; + let nested = s.execute("layer.addItem", json!({"item": inner.0, "time": 0.0})).unwrap()["layer"].as_u64().unwrap(); + let path = s.execute("essential.instance", json!({"layer": nested})).unwrap()["controls"][0]["path"].as_str().unwrap().to_string(); + s.execute("prop.addKey", json!({"layer": nested, "path": path, "time": 0.0, "value": 0.0})).unwrap(); + s.execute("prop.addKey", json!({"layer": nested, "path": path, "time": 1.0, "value": 100.0})).unwrap(); + let ctl = s.execute("essential.addProperty", json!({"layer": nested, "path": path})).unwrap()["controls"][0].as_u64().unwrap(); + s.execute("comp.new", json!({"name": "Top", "width": 32, "height": 32, "frameRate": 30, "duration": 4})).unwrap(); + let top = s.active_comp_id().unwrap(); + let inst = s.execute("layer.addItem", json!({"item": mid.0, "time": 0.0})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.setSwitch", json!({"layers": [inst], "switch": "collapse", "value": true})).unwrap(); + s.execute("essential.set", json!({"layer": inst, "control": ctl, "value": 80.0})).unwrap(); + let r = effectcraft_render::Renderer::new(&s.project, &*s.footage, crate::render::RenderOpts::default()); + let ctx = effectcraft_render::EvalCtx::new(&s.project, top, s.project.comp(top).unwrap(), Tick::from_seconds_f64(0.25)); + let layer = ctx.comp.layer(effectcraft_project::LayerId(inst)).unwrap(); + let collapsed = r.collapsed(&ctx, layer).expect("the instance collapses"); + let (_, nctx) = r.collapse_into(&ctx, layer, &collapsed, 1.0).expect("a nested context"); + let nl = nctx.comp.layer(effectcraft_project::LayerId(nested)).unwrap(); + let over = effectcraft_render::essential_overrides(&nctx, nl); + assert_eq!(over.len(), 1); + assert!((over[0].value.as_f64() - 80.0).abs() < 1e-6, "value {:?}", over[0].value); + assert!(over[0].keys.is_empty(), "the nested instance's own keys are stale"); +} diff --git a/crates/project/src/essential.rs b/crates/project/src/essential.rs index 3d851f902..cc3b12209 100644 --- a/crates/project/src/essential.rs +++ b/crates/project/src/essential.rs @@ -12,10 +12,11 @@ //! [`sync_project`] keeps the groups in step with the controls (and refreshes the values of //! controls that are not overridden), [`with_overrides`] builds the project an instance renders. +use effectcraft_time::Tick; use serde::{Deserialize, Serialize}; use crate::props::{GroupKind, Node, ParamUi, PropGroup, Property, Uid}; -use crate::{ItemId, ItemKind, Layer, LayerId, LayerSource, Project, Value}; +use crate::{ItemId, ItemKind, Keyframe, Layer, LayerId, LayerSource, Project, Value}; /// Match id of the Essential Properties group on precomp layers. pub const GROUP: &str = "essential"; @@ -647,15 +648,40 @@ pub fn sync_project(before: &Project, after: &mut Project) { after.next_id = after.next_id.max(next); } +/// Keys given in comp time, in the time of a layer that starts at `start` and is stretched to +/// `stretch` percent. Empty when the stretch is not a usable factor. +fn keys_in_layer_time(keys: &[Keyframe], start: Tick, stretch: f64) -> Vec { + if stretch.is_finite() && stretch > 0.0 { keys.iter().map(|k| key_in_layer_time(k, start, stretch)).collect() } else { vec![] } +} + +/// A key given in comp time, moved into the layer time of a layer that starts at `start` and is +/// stretched to `stretch` percent (the inverse of `Layer::comp_time`). Ease speeds are per +/// second, so they scale with the time axis; influences are fractions of the segment and stay. +fn key_in_layer_time(k: &Keyframe, start: Tick, stretch: f64) -> Keyframe { + let d = k.time - start; + let mut k = k.clone(); + k.time = if (stretch - 100.0).abs() < 1e-9 { d } else { Tick((d.0 as f64 * 100.0 / stretch) as i64) }; + if (stretch - 100.0).abs() >= 1e-9 { + for e in k.in_ease.iter_mut().chain(k.out_ease.iter_mut()) { + e.speed *= stretch / 100.0; + } + } + k +} + /// One instance override: the control and the value it takes in this instance. #[derive(Clone, Debug, PartialEq)] pub struct Override { pub control: u64, pub value: Value, + /// The instance property's keyframes, in the nested comp's time (`with_overrides` moves them + /// into each target layer's time). Empty when the property is not keyed (or its keys can't + /// be mapped); expressions in the comp then see only `value`. + pub keys: Vec, } /// The project as an instance of `comp` renders it: each overridden control's source property -/// set to the instance value (keyframes dropped; its expression, if any, still applies) and +/// set to the instance value and keyframes (its expression, if any, still applies) and /// replaced media swapped in. `None` when nothing applies. pub fn with_overrides(project: &Project, comp: ItemId, overrides: &[Override]) -> Option { let eg = project.comp(comp)?.essential.clone()?; @@ -682,8 +708,13 @@ pub fn with_overrides(project: &Project, comp: ItemId, overrides: &[Override]) - } EgKind::Property { .. } => { for (layer, prop) in ctl.kind.targets() { - if let Some(pr) = c.layer_mut(layer).and_then(|l| l.props.find_mut(prop)) { - pr.keys.clear(); + if let Some(l) = c.layer_mut(layer) { + let (start, stretch) = (l.start_time, l.stretch); + let Some(pr) = l.props.find_mut(prop) else { continue }; + // The keys are in nested comp time; the property reads them in this + // layer's own time (start offset, stretch). Without a usable mapping + // only the value is set. + pr.keys = keys_in_layer_time(&o.keys, start, stretch); pr.value = o.value.clone(); any = true; } diff --git a/crates/render/src/fx_host.rs b/crates/render/src/fx_host.rs index 3a6f1a34c..6e8a0d42d 100644 --- a/crates/render/src/fx_host.rs +++ b/crates/render/src/fx_host.rs @@ -1,4 +1,5 @@ -use super::{Arc, Buf, EffectHost, EvalCtx, ItemKind, Layer, LayerPixels, LayerSource, Params, Renderer, Tick, three_d}; +use super::frames::frame_blend_mode; +use super::{Arc, Buf, EffectHost, EvalCtx, FrameBlend, ItemKind, Layer, LayerPixels, LayerSource, Params, Renderer, Tick, three_d}; /// Layer parameters and audio for effects (see [`effectcraft_effects::EffectHost`]). What it /// hands out is in effect space (see [`effect_bounds`]): the other layer's for its pixels, the @@ -183,17 +184,37 @@ fn effect_param<'a>(layer: &'a Layer, effect_index: usize, id: &str) -> Option<& None } +/// Whether an instance's keys are in the nested comp's time, so they can go into the comp as they +/// are: not with Time Remap, Responsive Design - Time (a stretched precomp with protected +/// regions), a nested comp that preserves its frame rate (its time is floored to its own frames) +/// or a layer with Frame Blending (it renders the nested comp at two other times and mixes them, +/// so keys would pull the next frame's value in early). +fn keys_in_nested_time(ctx: &EvalCtx, layer: &Layer) -> bool { + if layer.props.get("timeRemap").is_some() || frame_blend_mode(ctx, layer) != FrameBlend::Off { + return false; + } + let LayerSource::Comp { item } = layer.source else { return true }; + let Some(nc) = ctx.project.comp(item) else { return true }; + let responsive = (layer.stretch - 100.0).abs() > 1e-9 && !super::eval::protected_regions(nc).is_empty(); + !responsive && !nc.preserve_frame_rate +} + /// The Essential Properties overrides of a precomp layer, evaluated at the context time. pub fn essential_overrides(ctx: &EvalCtx, layer: &Layer) -> Vec { use effectcraft_project::essential; let over = essential::overridden(layer); let Some(g) = essential::group(layer).filter(|_| !over.is_empty()) else { return vec![] }; + let same_time = keys_in_nested_time(ctx, layer); let mut out = vec![]; g.walk("", &mut |_, p| { if over.contains(&p.uid) && let Some(control) = essential::control_of(&p.match_id) { - out.push(essential::Override { control, value: ctx.value(layer, p) }); + // An expression on the instance property replaces its keys, and so does an override + // from a collapsed parent (only its value reaches here, the keys are the stale own ones). + let from_parent = ctx.property_overrides.iter().any(|o| o.layer == layer.id && o.prop == p.uid); + let keys = if same_time && !from_parent && !p.has_expression() { p.keys.clone() } else { vec![] }; + out.push(essential::Override { control, value: ctx.value(layer, p), keys }); } }); out