diff --git a/crates/model/src/comp.rs b/crates/model/src/comp.rs index 7631a924bb977ef215dfadcda8fb2d13cefab3ff..43067aa838c4fbb0c8c26058088a9eb612d8b7f6 100644 --- a/crates/model/src/comp.rs +++ b/crates/model/src/comp.rs @@ -668,6 +668,34 @@ impl Comp { } } + // Apply cyclic [0..6] endpoint continuity on LUTBezier splines attached to HueCurves nodes. + const HUE_CURVE_PORTS: &[&str] = &[ + "Hue", + "Sat", + "Lum", + "Red", + "Green", + "Blue", + "RedSuppress", + "GreenSuppress", + "BlueSuppress", + ]; + let mut hue_spline_names: HashSet = HashSet::new(); + for n in &nodes { + if n.kind == "HueCurves" { + for &port in HUE_CURVE_PORTS { + if let Some(sp_name) = n.connected_node(port) { + hue_spline_names.insert(sp_name.to_owned()); + } + } + } + } + for sp_name in hue_spline_names { + if let Some(sp) = splines.get_mut(&sp_name) { + sp.apply_hue_curves_cyclic_ends(); + } + } + // Infer comp dimensions from first Background/TextPlus if Prefs omitted Width/Height if width == 1920 && height == 1080 { for n in &nodes { @@ -2199,7 +2227,12 @@ fn sync_spline_table(spline_name: &str, spline: &BezierSpline, tbl: &mut LuaTabl if existing.color != spline.color { tbl.set(LuaKey::Str("SplineColor".to_owned()), spline_color_lua(spline.color)); } - if existing.keyframes != spline.keyframes { + let unchanged_cyclic = tbl.tag.as_deref() == Some("LUTBezier") && { + let mut normalized = existing.clone(); + normalized.apply_hue_curves_cyclic_ends(); + normalized.keyframes == spline.keyframes + }; + if existing.keyframes != spline.keyframes && !unchanged_cyclic { let new_kfs = LuaValue::Table(build_keyframes_table(spline)); if tbl.tag.as_deref() == Some("LUTBezier") && tbl.get_table("KeyColorSplines").is_some() { if let Some(kcs) = tbl.get_table_mut("KeyColorSplines") { @@ -3301,4 +3334,143 @@ mod expression_modifier_tests { assert_eq!(comp.eval_text(node, "StyledText", 10.0, "", &expr), "first line"); assert_eq!(comp.eval_text(node, "StyledText", 25.0, "", &expr), "second line"); } + + #[test] + fn source_op_inherits_all_color_corrector_params_and_respects_blend_override() { + // Mirrors repro_seed_110: N4 inherits Wheel*/TintMode/*RGBGain/PreDividePostMultiply + // from N2 while keeping its own `Input` wire and `Blend = 0.75` override. + let src = r#"Composition { + Tools = ordered() { + S0 = Loader {}, + S1 = DaySky {}, + N2 = ColorCorrector { + Inputs = { + Input = Input { SourceOp = "S0", Source = "Output", }, + WheelHue1 = Input { Value = -0.235, }, + WheelSaturation1 = Input { Value = 0.65, }, + WheelTintAngle1 = Input { Value = 0.323, }, + WheelTintLength1 = Input { Value = 0.67, }, + TintMode = Input { Value = 0, }, + MasterRGBGain = Input { Value = 0.65, }, + ShadowsRGBGain = Input { Value = 0.84, }, + HighlightsRGBGain = Input { Value = 0.75, }, + PreDividePostMultiply = Input { Value = 1, }, + Blend = Input { Value = 1.0, }, + }, + }, + N4 = ColorCorrector { + SourceOp = "N2", + Inputs = { + Input = Input { SourceOp = "S1", Source = "Output", }, + Blend = Input { Value = 0.75, }, + }, + }, + N5 = ColorCorrector { + SourceOp = "N2", + Inputs = { + Input = Input { SourceOp = "S1", Source = "Output", }, + }, + }, + }, + }"#; + let comp = Comp::from_fusion_str(src).expect("parse seed 110 style SourceOp"); + let expr = ExprEngine::new(); + let n4 = comp.node(comp.find_node("N4").unwrap()); + assert_eq!(n4.connected_node("Input"), Some("S1")); + assert!((comp.eval_number(n4, "Blend", 1.0, 1.0, &expr) - 0.75).abs() < 1e-6); + assert!((comp.eval_number(n4, "WheelHue1", 1.0, 0.0, &expr) - (-0.235)).abs() < 1e-6); + assert!((comp.eval_number(n4, "WheelSaturation1", 1.0, 1.0, &expr) - 0.65).abs() < 1e-6); + assert!((comp.eval_number(n4, "WheelTintAngle1", 1.0, 0.0, &expr) - 0.323).abs() < 1e-6); + assert!((comp.eval_number(n4, "WheelTintLength1", 1.0, 0.0, &expr) - 0.67).abs() < 1e-6); + assert!((comp.eval_number(n4, "TintMode", 1.0, 2.0, &expr) - 0.0).abs() < 1e-6); + assert!((comp.eval_number(n4, "MasterRGBGain", 1.0, 1.0, &expr) - 0.65).abs() < 1e-6); + assert!((comp.eval_number(n4, "ShadowsRGBGain", 1.0, 1.0, &expr) - 0.84).abs() < 1e-6); + assert!((comp.eval_number(n4, "HighlightsRGBGain", 1.0, 1.0, &expr) - 0.75).abs() < 1e-6); + assert!((comp.eval_number(n4, "PreDividePostMultiply", 1.0, 0.0, &expr) - 1.0).abs() < 1e-6); + + let n5 = comp.node(comp.find_node("N5").unwrap()); + assert!((comp.eval_number(n5, "Blend", 1.0, 0.0, &expr) - 1.0).abs() < 1e-6); + } + + #[test] + fn hue_curves_lut_bezier_applies_cyclic_ends_and_roundtrips_cleanly() { + let src = r#"Composition { + Tools = ordered() { + Bg = Background {}, + HC_RSup = LUTBezier { + SmoothEndsTransition = true, + KeyColorSplines = { + [0] = { + [0] = { 0.5 }, + [2] = { 1.0 }, + [6] = { 0.5 }, + } + }, + }, + CC_Red = LUTBezier { + SmoothEndsTransition = true, + KeyColorSplines = { + [0] = { + [0.0] = { 0.0 }, + [0.5] = { 0.8 }, + [1.0] = { 0.2 }, + } + }, + }, + HC1 = HueCurves { + Inputs = { + Input = Input { SourceOp = "Bg", Source = "Output", }, + RedSuppress = Input { SourceOp = "HC_RSup", Source = "Value", }, + }, + }, + CC1 = ColorCurves { + Inputs = { + Input = Input { SourceOp = "Bg", Source = "Output", }, + Red = Input { SourceOp = "CC_Red", Source = "Value", }, + }, + }, + }, + }"#; + let comp = Comp::from_fusion_str(src).expect("parse HueCurves + ColorCurves"); + let rsup = comp.splines.get("HC_RSup").expect("HC_RSup"); + assert!(rsup.keyframes[2].lh.is_some()); + assert!((rsup.eval(5.0) - 0.4140625).abs() < 1e-3); + + // ColorCurves [0..1] must NOT get cyclic HueCurves wrapping even with SmoothEndsTransition = true. + let cc_red = comp.splines.get("CC_Red").expect("CC_Red"); + assert!((cc_red.eval(1.0) - 0.2).abs() < 1e-6); + assert!((cc_red.eval(0.75) - 0.5).abs() < 1e-6); + + // Lossless roundtrip keeps KeyColorSplines untouched when unedited. + let saved = comp.to_fusion_string(); + assert!(!saved.contains("LH =")); + let reloaded = Comp::from_fusion_str(&saved).expect("reload"); + assert!((reloaded.splines["HC_RSup"].eval(5.0) - rsup.eval(5.0)).abs() < 1e-9); + } + + #[test] + fn comp_eval_number_clamps_loop_spline_before_first_keyframe() { + let src = r#"Composition { + Tools = ordered() { + CG = ColorGain { + Inputs = { + HighHue = Input { SourceOp = "ColorGain1HighHue", Source = "Value", }, + }, + }, + ColorGain1HighHue = BezierSpline { + KeyFrames = { + [4129] = { 0, RH = { 4137.66666666667, 0.333333333333333 }, Flags = { Linear = true, Loop = true } }, + [4155] = { 1, LH = { 4146.33333333333, 0.666666666666667 }, Flags = { Linear = true, Loop = true } }, + }, + }, + }, + }"#; + let comp = Comp::from_fusion_str(src).expect("parse underwater loop spline"); + let expr = ExprEngine::new(); + let cg = comp.node(comp.find_node("CG").unwrap()); + assert!((comp.eval_number(cg, "HighHue", 3933.0, 0.0, &expr) - 0.0).abs() < 1e-9); + assert!((comp.eval_number(cg, "HighHue", 4142.0, 0.0, &expr) - 0.5).abs() < 1e-9); + assert!((comp.eval_number(cg, "HighHue", 4155.0, 0.0, &expr) - 0.0).abs() < 1e-9); + assert!((comp.eval_number(cg, "HighHue", 4168.0, 0.0, &expr) - 0.5).abs() < 1e-9); + } } diff --git a/crates/model/src/spline.rs b/crates/model/src/spline.rs index 056e04fd30fa9a7df2f469aa325e5bc109e000a2..38cbd6d4c32683407a7b9a301d41fa19320b5b7d 100644 --- a/crates/model/src/spline.rs +++ b/crates/model/src/spline.rs @@ -132,30 +132,33 @@ impl BezierSpline { let first = &kfs[0]; let last = &kfs[kfs.len() - 1]; - // Handle loop / relative-loop (`LoopRel`) / ping-pong if set on first or last keyframe + // Fusion never loops backwards before the first keyframe (`Loop` / `LoopRel` / `PingPong` all clamp). + if t <= first.time { + return first.value; + } + + // In Fusion, `Loop` / `LoopRel` requires at least two loop-flagged keyframes (`loop_start < loop_end`). + // The loop repeats `[loop_start, loop_end]` for `t >= loop_end.time` up to the next keyframe (if any). + let is_loop_key = |k: &Keyframe| k.flags.loop_after || k.flags.loop_rel; + let loop_pair = kfs.iter().position(is_loop_key).and_then(|s| { + let e = kfs.iter().rposition(is_loop_key)?; + (s < e).then_some((s, e)) + }); + let mut t = t; let mut value_offset = 0.0_f64; - let span = last.time - first.time; - let dv_cycle = last.value - first.value; - let do_loop_after = last.flags.loop_after || last.flags.loop_rel || last.flags.ping_pong; - let do_loop_before = first.flags.loop_after || first.flags.loop_rel || first.flags.ping_pong; - - if span > 1e-9 && ((t > last.time && do_loop_after) || (t < first.time && do_loop_before)) { - let offset = t - first.time; - let cycle = (offset / span).floor(); - let rem = offset - cycle * span; - if (t > last.time && last.flags.ping_pong) || (t < first.time && first.flags.ping_pong) { - t = if (cycle as i64) & 1 == 0 { - first.time + rem - } else { - last.time - rem - }; - } else { - t = first.time + rem; - if (t > last.time || offset >= span) && (last.flags.loop_rel || first.flags.loop_rel) { - value_offset = cycle * dv_cycle; - } else if offset < 0.0 && (first.flags.loop_rel || last.flags.loop_rel) { - value_offset = cycle * dv_cycle; + if let Some((ls_idx, le_idx)) = loop_pair { + let ls = &kfs[ls_idx]; + let le = &kfs[le_idx]; + let next_after_time = kfs.get(le_idx + 1).map(|k| k.time); + let span = le.time - ls.time; + if span > 1e-9 && t >= le.time && next_after_time.is_none_or(|nt| t < nt) { + let offset = t - ls.time; + let cycle = (offset / span).floor(); + let rem = offset - cycle * span; + t = ls.time + rem; + if ls.flags.loop_rel || le.flags.loop_rel { + value_offset = cycle * (le.value - ls.value); } } } @@ -174,6 +177,46 @@ impl BezierSpline { eval_segment(k0, k1, t) + value_offset } + /// Applies Fusion `HueCurves` cyclic `[0..6]` endpoint continuity when tangent handles are omitted. + pub fn apply_hue_curves_cyclic_ends(&mut self) { + let kfs = &mut self.keyframes; + if kfs.len() < 2 { + return; + } + let first_t = kfs[0].time; + let last_t = kfs[kfs.len() - 1].time; + if (first_t - 0.0).abs() > 1e-6 || (last_t - 6.0).abs() > 1e-6 { + return; + } + let n = kfs.len(); + let first_v = kfs[0].value; + let last = &kfs[n - 1]; + if last.flags.linear || kfs[0].flags.step_out || last.flags.step_in { + if last.flags.linear && (last.value - first_v).abs() > 1e-9 { + kfs[n - 1].value = first_v; + } + return; + } + if kfs[0].rh.is_some() || last.lh.is_some() { + return; + } + let dt0 = kfs[1].time - first_t; + let dv0 = kfs[1].value - first_v; + let rh0 = [first_t + dt0 / 3.0, first_v + dv0 / 3.0]; + // Preserve the 2-keyframe initial chord slope in `RH` before snapping `last.value`. + if n == 2 { + kfs[0].rh = Some(rh0); + } + let dt_last = last_t - kfs[n - 2].time; + let dx_last = dt_last / 3.0; + let slope0 = if dt0.abs() > 1e-12 { dv0 / dt0 } else { 0.0 }; + kfs[n - 1].value = first_v; + kfs[n - 1].lh = Some([ + last_t - dx_last, + first_v - slope0 * dx_last, + ]); + } + /// Returns the effective cubic Bezier control points `[p0, p1, p2, p3]` for segment `idx -> idx+1`. pub fn segment_points(&self, idx: usize) -> Option<[[f64; 2]; 4]> { let k0 = self.keyframes.get(idx)?; @@ -489,3 +532,163 @@ pub fn read_vec2(t: &LuaTable) -> Option<[f64; 2]> { let y = t.get_index(2)?.as_f64()?; Some([x, y]) } + +#[cfg(test)] +mod tests { + use super::*; + + fn kf(time: f64, value: f64, flags: KeyFlags) -> Keyframe { + Keyframe { + time, + value, + lh: None, + rh: None, + flags, + handle_mode: if flags.linear { HandleMode::Vector } else { HandleMode::Free }, + } + } + + fn spline(keyframes: Vec) -> BezierSpline { + BezierSpline { + name: "test".to_owned(), + color: [255, 255, 255], + keyframes, + } + } + + #[test] + fn loop_clamps_before_first_keyframe_and_loops_after_last() { + let loop_flags = KeyFlags { linear: true, loop_after: true, ..Default::default() }; + let sp = spline(vec![ + kf(4129.0, 0.0, loop_flags), + kf(4155.0, 1.0, loop_flags), + ]); + // Pre-first keyframe must clamp to first.value (54_underwater ColorGain1HighHue @ 3933). + assert!((sp.eval(3933.0) - 0.0).abs() < 1e-9); + assert!((sp.eval(4129.0) - 0.0).abs() < 1e-9); + assert!((sp.eval(4142.0) - 0.5).abs() < 1e-9); + // At and after last.time, Loop wraps to first.time. + assert!((sp.eval(4155.0) - 0.0).abs() < 1e-9); + assert!((sp.eval(4168.0) - 0.5).abs() < 1e-9); + assert!((sp.eval(4181.0) - 0.0).abs() < 1e-9); + } + + #[test] + fn loop_requires_both_endpoints_flagged() { + let lin = KeyFlags { linear: true, ..Default::default() }; + let loop_f = KeyFlags { linear: true, loop_after: true, ..Default::default() }; + + let first_only = spline(vec![kf(10.0, 0.2, loop_f), kf(20.0, 0.8, lin)]); + assert!((first_only.eval(0.0) - 0.2).abs() < 1e-9); + assert!((first_only.eval(25.0) - 0.8).abs() < 1e-9); + + let last_only = spline(vec![kf(10.0, 0.2, lin), kf(20.0, 0.8, loop_f)]); + assert!((last_only.eval(0.0) - 0.2).abs() < 1e-9); + assert!((last_only.eval(25.0) - 0.8).abs() < 1e-9); + } + + #[test] + fn loop_rel_clamps_before_first_and_accumulates_after_last() { + let lin = KeyFlags { linear: true, ..Default::default() }; + let rel_both = KeyFlags { linear: true, loop_after: true, loop_rel: true, ..Default::default() }; + let sp = spline(vec![ + kf(10.0, 0.4, rel_both), + kf(20.0, 0.6, rel_both), + ]); + assert!((sp.eval(0.0) - 0.4).abs() < 1e-9); + assert!((sp.eval(15.0) - 0.5).abs() < 1e-9); + assert!((sp.eval(20.0) - 0.6).abs() < 1e-9); + assert!((sp.eval(25.0) - 0.7).abs() < 1e-9); + assert!((sp.eval(35.0) - 0.9).abs() < 1e-9); + + // LoopRel without Loop flag still loops relatively when both endpoints are marked. + let rel_only = KeyFlags { linear: true, loop_rel: true, ..Default::default() }; + let sp2 = spline(vec![kf(10.0, 0.4, rel_only), kf(20.0, 0.6, rel_only)]); + assert!((sp2.eval(0.0) - 0.4).abs() < 1e-9); + assert!((sp2.eval(25.0) - 0.7).abs() < 1e-9); + + // Single-endpoint LoopRel does not loop. + let single = spline(vec![kf(10.0, 0.4, lin), kf(20.0, 0.6, rel_both)]); + assert!((single.eval(25.0) - 0.6).abs() < 1e-9); + } + + #[test] + fn ping_pong_flag_semantics_match_fusion_21() { + let pp = KeyFlags { linear: true, ping_pong: true, ..Default::default() }; + let sp_pp = spline(vec![kf(10.0, 0.2, pp), kf(20.0, 0.8, pp)]); + assert!((sp_pp.eval(0.0) - 0.2).abs() < 1e-9); + assert!((sp_pp.eval(15.0) - 0.5).abs() < 1e-9); + assert!((sp_pp.eval(25.0) - 0.8).abs() < 1e-9); + + let loop_pp = KeyFlags { linear: true, loop_after: true, ping_pong: true, ..Default::default() }; + let sp_lpp = spline(vec![kf(10.0, 0.2, loop_pp), kf(20.0, 0.8, loop_pp)]); + assert!((sp_lpp.eval(0.0) - 0.2).abs() < 1e-9); + assert!((sp_lpp.eval(20.0) - 0.2).abs() < 1e-9); + assert!((sp_lpp.eval(25.0) - 0.5).abs() < 1e-9); + } + + #[test] + fn subrange_loop_repeats_between_flagged_keyframes_until_next_key() { + let lin = KeyFlags { linear: true, ..Default::default() }; + let lp = KeyFlags { linear: true, loop_after: true, ..Default::default() }; + let sp = spline(vec![ + kf(10.0, 0.2, lp), + kf(15.0, 0.7, lp), + kf(25.0, 0.9, lin), + ]); + assert!((sp.eval(5.0) - 0.2).abs() < 1e-9); + assert!((sp.eval(13.0) - 0.5).abs() < 1e-9); + assert!((sp.eval(15.0) - 0.2).abs() < 1e-9); + assert!((sp.eval(18.0) - 0.5).abs() < 1e-9); + assert!((sp.eval(23.0) - 0.5).abs() < 1e-9); + assert!((sp.eval(25.0) - 0.9).abs() < 1e-9); + assert!((sp.eval(27.0) - 0.9).abs() < 1e-9); + + let tail_loop = spline(vec![ + kf(10.0, 0.2, lin), + kf(15.0, 0.9, lp), + kf(20.0, 0.5, lp), + ]); + assert!((tail_loop.eval(0.0) - 0.2).abs() < 1e-9); + assert!((tail_loop.eval(13.0) - 0.62).abs() < 1e-9); + assert!((tail_loop.eval(20.0) - 0.9).abs() < 1e-9); + assert!((tail_loop.eval(23.0) - 0.66).abs() < 1e-9); + assert!((tail_loop.eval(33.0) - 0.66).abs() < 1e-9); + } + + #[test] + fn apply_hue_curves_cyclic_ends_wraps_last_handle_and_preserves_explicit_handles() { + // [0]=0.5, [2]=1.0, [6]=0.5 (HueCurves RedSuppress curve): + // slope0 = (1.0 - 0.5)/2 = +0.25, dt_last = 4 -> dx_last = 4/3, dy = 1/3 -> lh = (14/3, 1/6) + let smooth = KeyFlags::default(); + let mut rsup = spline(vec![ + kf(0.0, 0.5, smooth), + kf(2.0, 1.0, smooth), + kf(6.0, 0.5, smooth), + ]); + rsup.apply_hue_curves_cyclic_ends(); + let last_lh = rsup.keyframes[2].lh.expect("cyclic last.lh"); + assert!((last_lh[0] - (6.0 - 4.0 / 3.0)).abs() < 1e-9); + assert!((last_lh[1] - (0.5 - 1.0 / 3.0)).abs() < 1e-9); + // At h=5.0 (u=0.75 on [2..6]), cubic evaluates to ~0.414 instead of linear 0.625. + assert!((rsup.eval(5.0) - 0.4140625).abs() < 1e-3, "got {}", rsup.eval(5.0)); + + // 2-keyframe cyclic curve [0]=0.5, [6]=1.0 snaps last.value to 0.5 and mirrors chord RH->LH. + let mut two = spline(vec![kf(0.0, 0.5, smooth), kf(6.0, 1.0, smooth)]); + two.apply_hue_curves_cyclic_ends(); + assert!((two.eval(0.0) - 0.5).abs() < 1e-9); + assert!((two.eval(1.5) - 0.546875).abs() < 1e-6); + assert!((two.eval(3.0) - 0.5).abs() < 1e-9); + assert!((two.eval(4.5) - 0.453125).abs() < 1e-6); + assert!((two.eval(6.0) - 0.5).abs() < 1e-9); + + // Explicit LH/RH handles must not be overwritten. + let mut explicit = spline(vec![ + Keyframe { rh: Some([0.5, 0.9]), ..kf(0.0, 0.5, smooth) }, + Keyframe { lh: Some([5.5, 0.8]), ..kf(6.0, 0.5, smooth) }, + ]); + explicit.apply_hue_curves_cyclic_ends(); + assert_eq!(explicit.keyframes[0].rh, Some([0.5, 0.9])); + assert_eq!(explicit.keyframes[1].lh, Some([5.5, 0.8])); + } +}