From 97cf7e2c02336bfb7135e3b651c34d1a615bb724 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 20:39:38 -0400 Subject: [PATCH 01/23] M9.11: Audio effects (Compressor, Distortion, Gate) Co-Authored-By: Claude Opus 5.5 --- crates/effects/src/audio_fx.rs | 241 +++++++++++++++++++++++++++- crates/render/src/tests_audio_fx.rs | 12 +- 2 files changed, 249 insertions(+), 4 deletions(-) diff --git a/crates/effects/src/audio_fx.rs b/crates/effects/src/audio_fx.rs index 855fd10e..bd80519a 100644 --- a/crates/effects/src/audio_fx.rs +++ b/crates/effects/src/audio_fx.rs @@ -1,5 +1,5 @@ -//! Audio effects (Effect > Audio): Backwards, Bass & Treble, Delay, Flange & Chorus, -//! High-Low Pass, Modulator, Parametric EQ, Reverb, Stereo Mixer and Tone. +//! Audio effects (Effect > Audio): Backwards, Bass & Treble, Compressor, Delay, Distortion, +//! Flange & Chorus, Gate, High-Low Pass, Modulator, Parametric EQ, Reverb, Stereo Mixer and Tone. //! //! They are ordinary effect instances on a layer (so their parameters animate and show in //! Effect Controls), pass video through unchanged, and are applied to the layer's audio by the @@ -12,7 +12,10 @@ //! DSP is textbook: second-order sections use the public RBJ "Audio EQ Cookbook" formulas; //! Reverb is a Schroeder/Moorer network (parallel low-pass feedback combs into series //! all-passes, as in the published Freeverb description); chorus/flange/vibrato are modulated -//! fractional delay lines. +//! fractional delay lines. Compressor and Gate are feed-forward dynamics processors on a +//! stereo-linked peak envelope (one-pole attack/release smoothing, the textbook quadratic soft-knee +//! gain computer); Distortion is a static waveshaper (soft clip, hard clip, fold-back, bit crush) +//! followed by a one-pole tone filter. use std::f64::consts::PI; @@ -60,6 +63,20 @@ pub fn specs() -> Vec { p("treble", "Treble", num(0.0), slider(-100.0, 100.0, -100.0, 100.0, 2)), ], ), + spec( + "ec.audio.compressor", + "Compressor", + vec![ + p("threshold", "Threshold (dB)", num(-20.0), slider(-60.0, 0.0, -60.0, 0.0, 2)), + p("ratio", "Ratio", num(4.0), slider(1.0, 30.0, 1.0, 20.0, 2)), + p("attack", "Attack (ms)", num(10.0), slider(0.0, 500.0, 0.0, 200.0, 2)), + p("release", "Release (ms)", num(100.0), slider(1.0, 5000.0, 1.0, 1000.0, 2)), + p("knee", "Knee (dB)", num(0.0), slider(0.0, 24.0, 0.0, 24.0, 2)), + p("makeupGain", "Makeup Gain (dB)", num(0.0), slider(-24.0, 36.0, 0.0, 24.0, 2)), + pp("dryOut", "Dry Out", pct(0.0)), + pp("wetOut", "Wet Out", pct(100.0)), + ], + ), spec( "ec.audio.delay", "Delay", @@ -71,6 +88,19 @@ pub fn specs() -> Vec { pp("wetOut", "Wet Out", pct(75.0)), ], ), + spec( + "ec.audio.distortion", + "Distortion", + vec![ + p("distortionType", "Distortion Type", Value::Enum(0), popup(&["Soft Clip", "Hard Clip", "Fold Back", "Bit Crush"])), + p("drive", "Drive (dB)", num(12.0), slider(0.0, 48.0, 0.0, 48.0, 2)), + p("bitDepth", "Bit Depth", num(8.0), slider(1.0, 16.0, 1.0, 16.0, 0)), + p("tone", "Tone (Hz)", num(8000.0), slider(200.0, 20000.0, 200.0, 20000.0, 1)), + p("outputGain", "Output Gain (dB)", num(-6.0), slider(-48.0, 12.0, -24.0, 12.0, 2)), + pp("dryOut", "Dry Out", pct(0.0)), + pp("wetOut", "Wet Out", pct(100.0)), + ], + ), spec( "ec.audio.flangechorus", "Flange & Chorus", @@ -86,6 +116,17 @@ pub fn specs() -> Vec { pp("wetOut", "Wet Out", pct(50.0)), ], ), + spec( + "ec.audio.gate", + "Gate", + vec![ + p("threshold", "Threshold (dB)", num(-40.0), slider(-90.0, 0.0, -80.0, 0.0, 2)), + p("attack", "Attack (ms)", num(1.0), slider(0.0, 500.0, 0.0, 100.0, 2)), + p("hold", "Hold (ms)", num(50.0), slider(0.0, 5000.0, 0.0, 500.0, 2)), + p("release", "Release (ms)", num(100.0), slider(1.0, 5000.0, 1.0, 1000.0, 2)), + p("range", "Range (dB)", num(-80.0), slider(-120.0, 0.0, -90.0, 0.0, 2)), + ], + ), spec( "ec.audio.highlowpass", "High-Low Pass", @@ -167,6 +208,14 @@ pub fn preroll(id: &str, params: &Params) -> f64 { match id { "ec.audio.basstreble" | "ec.audio.highlowpass" | "ec.audio.parametriceq" => 0.05, "ec.audio.flangechorus" | "ec.audio.modulator" => 0.06, + "ec.audio.distortion" => 0.01, + // The envelope settles within a few attack/release time constants. + "ec.audio.compressor" | "ec.audio.gate" => { + let rel = params.f("release").max(1.0) / 1000.0; + let att = params.f("attack").max(0.0) / 1000.0; + let hold = if id == "ec.audio.gate" { params.f("hold").max(0.0) / 1000.0 } else { 0.0 }; + (9.0 * rel + 9.0 * att + hold + 0.01).min(10.0) + } "ec.audio.delay" => { let d = params.f("delayTime").max(1.0) / 1000.0; let fb = (params.f("feedback") / 100.0).clamp(0.0, 0.99); @@ -231,6 +280,9 @@ pub fn process(id: &str, params: &Params, buf: &mut [f32], rate: u32, start: f64 } } "ec.audio.delay" => delay(params, buf, sr), + "ec.audio.compressor" => compressor(params, buf, sr), + "ec.audio.distortion" => distortion(params, buf, sr), + "ec.audio.gate" => gate(params, buf, sr), "ec.audio.flangechorus" => flange_chorus(params, buf, sr, start), "ec.audio.modulator" => modulator(params, buf, sr, start), "ec.audio.reverb" => reverb(params, buf, sr), @@ -362,6 +414,126 @@ fn delay(params: &Params, buf: &mut [f32], sr: f64) { } } +#[inline] +fn db_to_lin(db: f64) -> f64 { + 10f64.powf(db / 20.0) +} + +#[inline] +fn lin_to_db(v: f64) -> f64 { + 20.0 * v.max(1e-12).log10() +} + +/// One-pole smoothing coefficient for a time constant of `ms` milliseconds. +fn smoothing(ms: f64, sr: f64) -> f64 { + if ms <= 0.0 { 0.0 } else { (-1.0 / (ms / 1000.0 * sr)).exp() } +} + +/// Static compressor curve: gain change (dB, ≤ 0) for an input level `x` dB (quadratic soft +/// knee of width `knee` around the threshold). +pub fn compressor_gain_db(x: f64, threshold: f64, ratio: f64, knee: f64) -> f64 { + let r = ratio.max(1.0); + let over = x - threshold; + let y = if knee > 0.0 && 2.0 * over.abs() <= knee { + x + (1.0 / r - 1.0) * (over + knee / 2.0).powi(2) / (2.0 * knee) + } else if over > 0.0 { + threshold + over / r + } else { + x + }; + y - x +} + +fn compressor(params: &Params, buf: &mut [f32], sr: f64) { + let th = params.f("threshold"); + let ratio = params.f("ratio").max(1.0); + let knee = params.f("knee").max(0.0); + let att = smoothing(params.f("attack"), sr); + let rel = smoothing(params.f("release").max(0.001), sr); + let makeup = params.f("makeupGain"); + let dry = (params.f("dryOut") / 100.0) as f32; + let wet = (params.f("wetOut") / 100.0) as f32; + // Smoothed gain change in dB (≤ 0): attack while the reduction grows, release as it shrinks. + let mut g = 0.0f64; + for f in buf.chunks_exact_mut(2) { + let peak = f[0].abs().max(f[1].abs()) as f64; + let target = compressor_gain_db(lin_to_db(peak), th, ratio, knee); + let k = if target < g { att } else { rel }; + g = target + (g - target) * k; + let lin = db_to_lin(g + makeup) as f32; + for v in f.iter_mut() { + *v = *v * dry + *v * lin * wet; + } + } +} + +fn gate(params: &Params, buf: &mut [f32], sr: f64) { + let th = db_to_lin(params.f("threshold")); + let att = smoothing(params.f("attack"), sr); + let rel = smoothing(params.f("release").max(0.001), sr); + let hold = (params.f("hold").max(0.0) / 1000.0 * sr) as usize; + let floor = db_to_lin(params.f("range").min(0.0)); + // Openness 0..1 (closed..open), mapped to floor..1. + let mut g = 0.0f64; + let mut held = 0usize; + for f in buf.chunks_exact_mut(2) { + let peak = f[0].abs().max(f[1].abs()) as f64; + let open = if peak >= th { + held = hold; + true + } else if held > 0 { + held -= 1; + true + } else { + false + }; + let target = if open { 1.0 } else { 0.0 }; + let k = if target > g { att } else { rel }; + g = target + (g - target) * k; + let lin = (floor + (1.0 - floor) * g) as f32; + f[0] *= lin; + f[1] *= lin; + } +} + +/// The Distortion effect's waveshaper for one sample (`drive` linear gain, `bits` for Bit Crush). +pub fn distort_sample(kind: u32, x: f32, drive: f32, bits: f32) -> f32 { + let v = x * drive; + match kind { + 1 => v.clamp(-1.0, 1.0), + 2 => { + // Fold back: reflect at ±1 (a triangle function of the input). + let t = (v + 1.0).rem_euclid(4.0); + if t < 2.0 { t - 1.0 } else { 3.0 - t } + } + 3 => { + let q = 2f32.powf(bits.clamp(1.0, 16.0) - 1.0); + (v.clamp(-1.0, 1.0) * q).round() / q + } + _ => v.tanh(), + } +} + +fn distortion(params: &Params, buf: &mut [f32], sr: f64) { + let kind = params.e("distortionType"); + let drive = db_to_lin(params.f("drive")) as f32; + let bits = params.f("bitDepth") as f32; + let out = db_to_lin(params.f("outputGain")) as f32; + let dry = (params.f("dryOut") / 100.0) as f32; + let wet = (params.f("wetOut") / 100.0) as f32; + let fc = params.f("tone").clamp(20.0, sr * 0.49); + let a = (-2.0 * PI * fc / sr).exp() as f32; + let mut lp = [0.0f32; 2]; + for f in buf.chunks_exact_mut(2) { + for ch in 0..2 { + let x = f[ch]; + let y = distort_sample(kind, x, drive, bits); + lp[ch] = y + (lp[ch] - y) * a; + f[ch] = x * dry + lp[ch] * out * wet; + } + } +} + fn lfo(kind: u32, phase: f64) -> f64 { let ph = phase.rem_euclid(1.0); match kind { @@ -546,7 +718,10 @@ mod tests { for id in [ "ec.audio.backwards", "ec.audio.basstreble", + "ec.audio.compressor", "ec.audio.delay", + "ec.audio.distortion", + "ec.audio.gate", "ec.audio.flangechorus", "ec.audio.highlowpass", "ec.audio.modulator", @@ -679,4 +854,64 @@ mod tests { assert_eq!(b, vec![2.0, 1.0]); assert!(reverses("ec.audio.backwards")); } + + #[test] + fn compressor_reduces_gain_above_threshold() { + // Static curve: 10 dB over a −20 dB threshold at 4:1 comes out 7.5 dB lower. + assert!((compressor_gain_db(-10.0, -20.0, 4.0, 0.0) + 7.5).abs() < 1e-9); + assert_eq!(compressor_gain_db(-30.0, -20.0, 4.0, 0.0), 0.0); + // The soft knee meets the hard curve at its edges. + let k = 6.0; + assert!(compressor_gain_db(-23.0, -20.0, 4.0, k).abs() < 1e-9); + assert!((compressor_gain_db(-17.0, -20.0, 4.0, k) - compressor_gain_db(-17.0, -20.0, 4.0, 0.0)).abs() < 1e-9); + let sr = 48000.0; + let mut p = defaults("ec.audio.compressor"); + p.values.insert("attack".into(), num(1.0)); + let mut loud = sine(1000.0, sr, 9600); + process("ec.audio.compressor", &p, &mut loud, 48000, 0.0); + // A full-scale sine (peak 0 dB) is pulled down by ~15 dB once the envelope settles. + let peak = loud[18000..].iter().fold(0.0f32, |m, v| m.max(v.abs())); + let want = db_to_lin(compressor_gain_db(0.0, -20.0, 4.0, 0.0)) as f32; + assert!((peak - want).abs() < 0.05, "{peak} vs {want}"); + // Quiet input passes untouched. + let mut quiet: Vec = sine(1000.0, sr, 4800).iter().map(|v| v * 0.01).collect(); + let orig = quiet.clone(); + process("ec.audio.compressor", &p, &mut quiet, 48000, 0.0); + assert!(quiet.iter().zip(&orig).all(|(a, b)| (a - b).abs() < 1e-6)); + } + + #[test] + fn gate_mutes_below_threshold() { + let sr = 48000.0; + let p = defaults("ec.audio.gate"); + // A −60 dB signal under the −40 dB threshold is attenuated by the −80 dB range. + let mut quiet: Vec = sine(500.0, sr, 9600).iter().map(|v| v * 0.001).collect(); + process("ec.audio.gate", &p, &mut quiet, 48000, 0.0); + assert!(rms(&quiet, 4800) < 1e-6, "{}", rms(&quiet, 4800)); + // A loud signal opens the gate. + let mut loud: Vec = sine(500.0, sr, 9600).iter().map(|v| v * 0.5).collect(); + let r0 = rms(&loud, 4800); + process("ec.audio.gate", &p, &mut loud, 48000, 0.0); + assert!((rms(&loud, 4800) - r0).abs() < 0.01); + // Deterministic. + let mut a = sine(300.0, sr, 2000); + let mut b = a.clone(); + process("ec.audio.gate", &p, &mut a, 48000, 0.0); + process("ec.audio.gate", &p, &mut b, 48000, 0.0); + assert_eq!(a, b); + } + + #[test] + fn distortion_shapes_and_bounds() { + assert!((distort_sample(0, 0.5, 1.0, 8.0) - 0.5f32.tanh()).abs() < 1e-6); + assert_eq!(distort_sample(1, 0.8, 2.0, 8.0), 1.0); + assert!((distort_sample(2, 0.7, 2.0, 8.0) - 0.6).abs() < 1e-5); + assert_eq!(distort_sample(3, 0.3, 1.0, 2.0), 0.5); + let p = defaults("ec.audio.distortion"); + let mut b = sine(200.0, 48000.0, 4800); + process("ec.audio.distortion", &p, &mut b, 48000, 0.0); + assert!(b.iter().all(|v| v.abs() <= 1.0)); + assert!(rms(&b, 100) > 0.1); + assert!(preroll("ec.audio.compressor", &defaults("ec.audio.compressor")) > 0.4); + } } diff --git a/crates/render/src/tests_audio_fx.rs b/crates/render/src/tests_audio_fx.rs index dfd49ccc..a249955f 100644 --- a/crates/render/src/tests_audio_fx.rs +++ b/crates/render/src/tests_audio_fx.rs @@ -174,7 +174,17 @@ fn blocks_match_one_long_mix() { let x = ((t * 12345.678).sin() * 43758.5453).fract() as f32; (x, -x) } - for id in ["ec.audio.reverb", "ec.audio.basstreble", "ec.audio.flangechorus", "ec.audio.modulator", "ec.audio.delay", "ec.audio.parametriceq"] { + for id in [ + "ec.audio.reverb", + "ec.audio.basstreble", + "ec.audio.flangechorus", + "ec.audio.modulator", + "ec.audio.delay", + "ec.audio.parametriceq", + "ec.audio.compressor", + "ec.audio.gate", + "ec.audio.distortion", + ] { let vals: Vec<(&str, Value)> = match id { "ec.audio.basstreble" => vec![("bass", Value::Scalar(50.0))], "ec.audio.parametriceq" => vec![("band1BoostCut", Value::Scalar(9.0))], From fe815888672b77ef44f9bc4d7c3b7a078408da72 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 20:46:53 -0400 Subject: [PATCH 02/23] M9.11: OCIO transforms, Color Stabilizer and Color Profile Converter Built-in minimal OCIO config (ACES2065-1, ACEScg, ACEScct, sRGB, Rec.709, Rec.2020, Display P3, linear variants, XYZ) from published primaries, transfer functions and Bradford adaptation; CDL v1.2 maths; .cube/.3dl/.csp and .cc/.ccc/.cdl file transforms with nearest/trilinear/tetrahedral interpolation and inverse; displays/views/looks. Co-Authored-By: Claude Opus 5.5 --- crates/effects/src/lib.rs | 6 + crates/effects/src/ocio.rs | 1281 +++++++++++++++++++++++++++++++++ crates/effects/src/utility.rs | 55 +- crates/render/src/lib.rs | 11 +- 4 files changed, 1349 insertions(+), 4 deletions(-) create mode 100644 crates/effects/src/ocio.rs diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 2e29061a..4bb658c3 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -27,6 +27,7 @@ mod misc; mod noise; mod noise2; mod obsolete; +mod ocio; pub mod paint; mod perspective; mod perspective2; @@ -245,6 +246,10 @@ pub struct EffectEnv<'a> { pub frame_rate: f64, /// Index of the running effect in its layer's stack (for [`EffectHost::self_at`]). pub effect_index: usize, + /// The project's working colour space (`None` = unmanaged, treated as sRGB). + pub working_space: Option, + /// Working-space pixels are linear light (Linearize Working Space). + pub working_linear: bool, } /// What an effect gets to render with. @@ -348,6 +353,7 @@ pub fn registry() -> &'static [EffectSpec] { v.extend(noise2::specs()); v.extend(color3::specs()); v.extend(obsolete::specs()); + v.extend(ocio::specs()); v.extend(time_fx::specs()); v.extend(audio_fx::specs()); v.extend(paint::specs()); diff --git a/crates/effects/src/ocio.rs b/crates/effects/src/ocio.rs new file mode 100644 index 00000000..d868a8f9 --- /dev/null +++ b/crates/effects/src/ocio.rs @@ -0,0 +1,1281 @@ +//! OCIO-style colour transforms (Color Correction ▸ OCIO CDL / Color Space / Display / File / Look +//! Transform), Color Profile Converter (Utility) and Color Stabilizer (Color Correction). +//! +//! The OCIO effects run on a **built-in minimal configuration** written from published +//! standards, not from OpenColorIO's source: +//! +//! * colour spaces are defined by primaries, white point and transfer function: ACES2065-1 (AP0) +//! and ACEScg / ACEScct (AP1) from SMPTE ST 2065-1 and Academy S-2014-004 / S-2016-001, sRGB +//! (IEC 61966-2-1), Rec. 709 / Rec. 2020 with the BT.1886 gamma 2.4 EOTF, Display P3 (SMPTE +//! EG 432-1 primaries, D65, sRGB curve), their linear variants and CIE XYZ D65; +//! * the reference space is CIE XYZ D65; spaces with another white point (the ACES white, +//! x 0.32168 y 0.33767) are adapted with the Bradford chromatic adaptation transform, as in the +//! published ACES configurations; +//! * displays (sRGB, Rec.1886 Rec.709, Display P3, Rec.2100-PQ via SMPTE ST 2084) and views +//! (Standard = clip-free encode, Tone Mapped = extended Reinhard roll-off, Raw); +//! * looks are ASC CDLs applied in ACEScct; +//! * CDL maths follows the ASC CDL v1.2 specification (slope, offset, power, then saturation with +//! Rec. 709 luma weights); the "No Clamp" style mirrors power around zero; +//! * File Transform reads `.cube` (Adobe/Resolve), `.3dl` (Autodesk Lustre/Flame integer +//! lattices), `.csp` (cineSpace, with its per-channel pre-LUT shaper) and ASC `.cc` / `.ccc` / +//! `.cdl` XML, with nearest, trilinear or tetrahedral interpolation and an inverse direction +//! (exact for 1D/CDL, iterative for 3D lattices). + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, OnceLock}; + +use effectcraft_color::ColorSpace; +use effectcraft_color::space::{invert, mul, mul_vec, rgb_to_xyz}; +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use rayon::prelude::*; + +use crate::util::unpremul; +use crate::utility::{Lut, parse_cube}; +use crate::{Buf, EffectCtx, EffectSpec, num, p, popup, slider}; + +type M3 = [[f64; 3]; 3]; + +const D65: [f64; 2] = [0.3127, 0.3290]; +const ACES_WHITE: [f64; 2] = [0.32168, 0.33767]; +const AP0: [[f64; 2]; 3] = [[0.7347, 0.2653], [0.0, 1.0], [0.0001, -0.0770]]; +const AP1: [[f64; 2]; 3] = [[0.713, 0.293], [0.165, 0.830], [0.128, 0.044]]; +const REC709: [[f64; 2]; 3] = [[0.640, 0.330], [0.300, 0.600], [0.150, 0.060]]; +const REC2020: [[f64; 2]; 3] = [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]]; +const P3: [[f64; 2]; 3] = [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060]]; +const IDENTITY: M3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; + +/// Transfer functions (encoded ↔ linear). +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Tf { + Linear, + Srgb, + Gamma(f64), + AcesCct, + /// SMPTE ST 2084 with 1.0 = 100 cd/m². + Pq, +} + +impl Tf { + pub fn decode(self, v: f64) -> f64 { + match self { + Tf::Linear => v, + Tf::Srgb => { + let a = v.abs(); + let l = if a <= 0.04045 { a / 12.92 } else { ((a + 0.055) / 1.055).powf(2.4) }; + l.copysign(v) + } + Tf::Gamma(g) => v.abs().powf(g).copysign(v), + Tf::AcesCct => { + if v <= 0.155_251_141_552_511 { + (v - 0.072_905_534_195_835_5) / 10.540_237_741_654_5 + } else if v < (65504f64.log2() + 9.72) / 17.52 { + 2f64.powf(v * 17.52 - 9.72) + } else { + 65504.0 + } + } + Tf::Pq => { + let (m1, m2, c1, c2, c3) = pq_consts(); + let e = v.max(0.0).powf(1.0 / m2); + let l = ((e - c1).max(0.0) / (c2 - c3 * e)).powf(1.0 / m1); + l * 100.0 + } + } + } + pub fn encode(self, v: f64) -> f64 { + match self { + Tf::Linear => v, + Tf::Srgb => { + let a = v.abs(); + let e = if a <= 0.003_130_8 { a * 12.92 } else { 1.055 * a.powf(1.0 / 2.4) - 0.055 }; + e.copysign(v) + } + Tf::Gamma(g) => v.abs().powf(1.0 / g).copysign(v), + Tf::AcesCct => { + if v <= 0.007_812_5 { + 10.540_237_741_654_5 * v + 0.072_905_534_195_835_5 + } else { + (v.log2() + 9.72) / 17.52 + } + } + Tf::Pq => { + let (m1, m2, c1, c2, c3) = pq_consts(); + let y = (v / 100.0).max(0.0).powf(m1); + ((c1 + c2 * y) / (1.0 + c3 * y)).powf(m2) + } + } + } +} + +fn pq_consts() -> (f64, f64, f64, f64, f64) { + (2610.0 / 16384.0, 2523.0 / 4096.0 * 128.0, 3424.0 / 4096.0, 2413.0 / 4096.0 * 32.0, 2392.0 / 4096.0 * 32.0) +} + +/// A colour space of the built-in config. +#[derive(Clone, Copy, Debug)] +pub struct Space { + pub name: &'static str, + /// Primaries and white (None = CIE XYZ itself). + pub prims: Option<([[f64; 2]; 3], [f64; 2])>, + pub tf: Tf, + /// Passes data through untouched. + pub raw: bool, +} + +const fn sp(name: &'static str, prims: [[f64; 2]; 3], white: [f64; 2], tf: Tf) -> Space { + Space { name, prims: Some((prims, white)), tf, raw: false } +} + +/// The built-in configuration's colour spaces, in popup order. +pub const SPACES: &[Space] = &[ + sp("ACES2065-1", AP0, ACES_WHITE, Tf::Linear), + sp("ACEScg", AP1, ACES_WHITE, Tf::Linear), + sp("ACEScct", AP1, ACES_WHITE, Tf::AcesCct), + sp("Linear Rec.709 (sRGB)", REC709, D65, Tf::Linear), + sp("sRGB", REC709, D65, Tf::Srgb), + sp("Gamma 2.4 Rec.709", REC709, D65, Tf::Gamma(2.4)), + sp("Linear Rec.2020", REC2020, D65, Tf::Linear), + sp("Gamma 2.4 Rec.2020", REC2020, D65, Tf::Gamma(2.4)), + sp("Linear P3-D65", P3, D65, Tf::Linear), + sp("Display P3", P3, D65, Tf::Srgb), + Space { name: "CIE-XYZ-D65", prims: None, tf: Tf::Linear, raw: false }, + Space { name: "Raw", prims: None, tf: Tf::Linear, raw: true }, +]; + +pub fn space_names() -> Vec<&'static str> { + SPACES.iter().map(|s| s.name).collect() +} + +pub fn space_by_name(n: &str) -> Option<&'static Space> { + SPACES.iter().find(|s| s.name.eq_ignore_ascii_case(n)) +} + +/// Bradford chromatic adaptation from white `src` to white `dst` (xy). +pub fn bradford(src: [f64; 2], dst: [f64; 2]) -> M3 { + if src == dst { + return IDENTITY; + } + const MB: M3 = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; + let xyz = |c: [f64; 2]| [c[0] / c[1], 1.0, (1.0 - c[0] - c[1]) / c[1]]; + let s = mul_vec(&MB, xyz(src)); + let d = mul_vec(&MB, xyz(dst)); + let diag = [[d[0] / s[0], 0.0, 0.0], [0.0, d[1] / s[1], 0.0], [0.0, 0.0, d[2] / s[2]]]; + mul(&invert(&MB), &mul(&diag, &MB)) +} + +impl Space { + /// Linear RGB → XYZ D65 (Bradford-adapted when `adapt`, else the space's own white). + pub fn to_ref(self, adapt: bool) -> M3 { + match self.prims { + None => IDENTITY, + Some((p, w)) => { + let m = rgb_to_xyz(p, w); + if adapt { mul(&bradford(w, D65), &m) } else { m } + } + } + } +} + +/// Linear-RGB matrix from space `a` to space `b`. +pub fn matrix(a: &Space, b: &Space) -> M3 { + mul(&invert(&b.to_ref(true)), &a.to_ref(true)) +} + +/// A compiled colour-space conversion. +#[derive(Clone, Copy, Debug)] +pub struct Xform { + dec: Tf, + m: [[f32; 3]; 3], + ident: bool, + enc: Tf, +} + +impl Xform { + pub fn new(a: &Space, b: &Space) -> Xform { + if a.raw || b.raw { + return Xform { dec: Tf::Linear, m: IDENTITY.map(|r| r.map(|v| v as f32)), ident: true, enc: Tf::Linear }; + } + let m = matrix(a, b); + let ident = (0..3).all(|i| (0..3).all(|j| (m[i][j] - IDENTITY[i][j]).abs() < 1e-12)); + Xform { dec: a.tf, m: m.map(|r| r.map(|v| v as f32)), ident, enc: b.tf } + } + #[inline] + pub fn apply(&self, c: [f32; 3]) -> [f32; 3] { + let l = c.map(|v| self.dec.decode(v as f64) as f32); + let l = if self.ident { l } else { [0, 1, 2].map(|i| self.m[i][0] * l[0] + self.m[i][1] * l[1] + self.m[i][2] * l[2]) }; + l.map(|v| self.enc.encode(v as f64) as f32) + } +} + +// ---------------------------------------------------------------- ASC CDL + +/// An ASC Colour Decision List correction. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Cdl { + pub slope: [f64; 3], + pub offset: [f64; 3], + pub power: [f64; 3], + pub sat: f64, +} + +impl Default for Cdl { + fn default() -> Self { + Cdl { slope: [1.0; 3], offset: [0.0; 3], power: [1.0; 3], sat: 1.0 } + } +} + +const LUMA: [f64; 3] = [0.2126, 0.7152, 0.0722]; + +impl Cdl { + /// Forward CDL. `clamp` = the v1.2 style (results clamped to 0..1 after SOP and saturation). + pub fn apply(&self, c: [f64; 3], clamp: bool) -> [f64; 3] { + let mut o = [0.0; 3]; + for i in 0..3 { + let v = c[i] * self.slope[i] + self.offset[i]; + o[i] = if clamp { + v.clamp(0.0, 1.0).powf(self.power[i]) + } else if v >= 0.0 { + v.powf(self.power[i]) + } else { + v + }; + } + let l = o[0] * LUMA[0] + o[1] * LUMA[1] + o[2] * LUMA[2]; + let o = o.map(|v| l + self.sat * (v - l)); + if clamp { o.map(|v| v.clamp(0.0, 1.0)) } else { o } + } + /// Exact inverse of [`Cdl::apply`] (inside the clamped range). + pub fn invert(&self, c: [f64; 3], clamp: bool) -> [f64; 3] { + let c = if clamp { c.map(|v| v.clamp(0.0, 1.0)) } else { c }; + let l = c[0] * LUMA[0] + c[1] * LUMA[1] + c[2] * LUMA[2]; + let s = if self.sat.abs() < 1e-9 { 1e-9 } else { self.sat }; + let o = c.map(|v| l + (v - l) / s); + let mut r = [0.0; 3]; + for i in 0..3 { + let v = if clamp { o[i].clamp(0.0, 1.0) } else { o[i] }; + let pw = self.power[i].max(1e-9); + let u = if v >= 0.0 { v.powf(1.0 / pw) } else { v }; + let sl = if self.slope[i].abs() < 1e-9 { 1e-9 } else { self.slope[i] }; + r[i] = (u - self.offset[i]) / sl; + } + if clamp { r.map(|v| v.clamp(0.0, 1.0)) } else { r } + } +} + +fn tag<'a>(xml: &'a str, name: &str) -> Option<&'a str> { + let open = xml.find(&format!("<{name}"))?; + let gt = open + xml[open..].find('>')?; + if xml[..gt].ends_with('/') { + return Some(""); + } + let close = xml[gt..].find(&format!(""))? + gt; + Some(&xml[gt + 1..close]) +} + +fn nums3(s: &str) -> Option<[f64; 3]> { + let v: Vec = s.split_whitespace().filter_map(|t| t.parse().ok()).collect(); + (v.len() >= 3).then(|| [v[0], v[1], v[2]]) +} + +fn parse_cc(block: &str) -> Cdl { + let mut c = Cdl::default(); + if let Some(s) = tag(block, "Slope").and_then(nums3) { + c.slope = s; + } + if let Some(s) = tag(block, "Offset").and_then(nums3) { + c.offset = s; + } + if let Some(s) = tag(block, "Power").and_then(nums3) { + c.power = s; + } + if let Some(s) = tag(block, "Saturation").and_then(|t| t.trim().parse().ok()) { + c.sat = s; + } + c +} + +/// Parse ASC CDL XML (`.cc`, `.ccc`, `.cdl`): every `` with its `id`. +pub fn parse_cdl_xml(xml: &str) -> Vec<(String, Cdl)> { + let mut out = vec![]; + let mut rest = xml; + while let Some(i) = rest.find("').unwrap_or(after.len()); + let head = &after[..head_end]; + let id = head + .find("id=") + .and_then(|k| { + let q = head[k + 3..].chars().next()?; + let s = &head[k + 4..]; + s.find(q).map(|e| s[..e].to_string()) + }) + .unwrap_or_default(); + let end = after.find("").map(|e| e + "".len()).unwrap_or(after.len()); + out.push((id, parse_cc(&after[..end]))); + rest = &after[end..]; + } + out +} + +// ---------------------------------------------------------------- LUT files + +/// A loaded transform file. +#[derive(Clone, Debug)] +pub enum FileXform { + /// A lattice/1D LUT with an optional per-channel piecewise-linear shaper (cineSpace pre-LUT). + Lut { lut: Lut, shaper: Option<[Vec<(f32, f32)>; 3]> }, + /// CDL corrections by id (in file order). + Cdl(Vec<(String, Cdl)>), +} + +/// Parse an Autodesk `.3dl` lattice (integers, blue fastest; an optional input-mesh line). +pub fn parse_3dl(text: &str) -> Option { + let mut mesh: Option> = None; + let mut rows: Vec<[f64; 3]> = vec![]; + let mut out_bits: Option = None; + for line in text.lines() { + let l = line.trim(); + if l.is_empty() || l.starts_with('#') || l.starts_with('<') { + continue; + } + if l.starts_with(|c: char| c.is_ascii_alphabetic()) { + // "Mesh " (Lustre header); other keywords are ignored. + let t: Vec<&str> = l.split_whitespace().collect(); + if t[0].eq_ignore_ascii_case("mesh") && t.len() >= 3 { + out_bits = t[2].parse().ok(); + } + continue; + } + let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).collect(); + if v.len() > 3 && mesh.is_none() && rows.is_empty() { + mesh = Some(v); + } else if v.len() == 3 { + rows.push([v[0], v[1], v[2]]); + } else { + return None; + } + } + let is_cube = |k: usize| { + let n = (k as f64).cbrt().round() as usize; + n * n * n == k + }; + // A 3-entry input mesh (3³ lattice) looks like a data row: tell them apart by the count. + if mesh.is_none() && !is_cube(rows.len()) && rows.len() == 28 { + mesh = Some(rows.remove(0).to_vec()); + } + let n = match &mesh { + Some(m) => m.len(), + None => (rows.len() as f64).cbrt().round() as usize, + }; + if n < 2 || rows.len() != n * n * n { + return None; + } + let maxv = rows.iter().flat_map(|r| r.iter()).fold(0.0f64, |m, &v| m.max(v)); + let out_max = match out_bits { + Some(b) if b > 0 && b < 32 => (1u64 << b) as f64 - 1.0, + _ => [255.0, 1023.0, 4095.0, 65535.0].into_iter().find(|&m| maxv <= m).unwrap_or(maxv.max(1.0)), + }; + // .3dl rows: red slowest, blue fastest. Our lattice is red fastest. + let mut d3 = vec![[0.0f32; 3]; n * n * n]; + for (k, r) in rows.iter().enumerate() { + let (ri, gi, bi) = (k / (n * n), (k / n) % n, k % n); + d3[ri + gi * n + bi * n * n] = r.map(|v| (v / out_max) as f32); + } + Some(Lut { n1: 0, d1: vec![], n3: n, d3, dmin: [0.0; 3], dmax: [1.0; 3] }) +} + +/// Parse a cineSpace `.csp` LUT (1D or 3D, with its three pre-LUT shapers). +pub fn parse_csp(text: &str) -> Option<(Lut, [Vec<(f32, f32)>; 3])> { + let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty()); + if !lines.next()?.starts_with("CSPLUTV100") { + return None; + } + let kind = lines.next()?; + let mut lines = lines.peekable(); + if lines.peek().is_some_and(|l| l.starts_with("BEGIN METADATA")) { + for l in lines.by_ref() { + if l.starts_with("END METADATA") { + break; + } + } + } + let nums = |l: &str| -> Option> { l.split_whitespace().map(|t| t.parse().ok()).collect() }; + let mut shaper: [Vec<(f32, f32)>; 3] = Default::default(); + for ch in shaper.iter_mut() { + let n: usize = lines.next()?.parse().ok()?; + let ins = nums(lines.next()?)?; + let outs = nums(lines.next()?)?; + if ins.len() != n || outs.len() != n || n < 2 { + return None; + } + *ch = ins.into_iter().zip(outs).collect(); + } + if kind.starts_with("3D") { + let sz = nums(lines.next()?)?; + if sz.len() != 3 || sz[0] != sz[1] || sz[1] != sz[2] { + return None; + } + let n = sz[0] as usize; + let mut d3 = Vec::with_capacity(n * n * n); + for _ in 0..n * n * n { + let v = nums(lines.next()?)?; + if v.len() != 3 { + return None; + } + d3.push([v[0], v[1], v[2]]); + } + (n >= 2).then_some((Lut { n1: 0, d1: vec![], n3: n, d3, dmin: [0.0; 3], dmax: [1.0; 3] }, shaper)) + } else { + let n: usize = lines.next()?.parse().ok()?; + let mut d1 = Vec::with_capacity(n); + for _ in 0..n { + let v = nums(lines.next()?)?; + if v.len() != 3 { + return None; + } + d1.push([v[0], v[1], v[2]]); + } + (n >= 2).then_some((Lut { n1: n, d1, n3: 0, d3: vec![], dmin: [0.0; 3], dmax: [1.0; 3] }, shaper)) + } +} + +fn shape(curve: &[(f32, f32)], x: f32) -> f32 { + if curve.len() < 2 { + return x; + } + let i = curve.partition_point(|p| p.0 < x).clamp(1, curve.len() - 1); + let (a, b) = (curve[i - 1], curve[i]); + let t = ((x - a.0) / (b.0 - a.0).max(1e-12)).clamp(0.0, 1.0); + a.1 + (b.1 - a.1) * t +} + +fn shape_inv(curve: &[(f32, f32)], y: f32) -> f32 { + let flipped: Vec<(f32, f32)> = curve.iter().map(|&(a, b)| (b, a)).collect(); + shape(&flipped, y) +} + +/// Parse transform text by its format (detected from `ext` or the content). +pub fn parse_file(text: &str, ext: &str) -> Option { + let ext = ext.to_ascii_lowercase(); + if ext == "cc" || ext == "ccc" || ext == "cdl" || text.contains(">>>; + +/// Load (and cache) a transform from a file path or inline content. +pub fn load_file(src: &str) -> Option> { + let src_t = src.trim(); + if src_t.is_empty() { + return None; + } + static CACHE: OnceLock = OnceLock::new(); + let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new())); + if let Ok(m) = cache.lock() + && let Some(v) = m.get(src) + { + return v.clone(); + } + let inline = src.contains('\n'); + let parsed = if inline { + parse_file(src, "") + } else { + let ext = std::path::Path::new(src_t).extension().and_then(|e| e.to_str()).unwrap_or(""); + std::fs::read_to_string(src_t).ok().and_then(|t| parse_file(&t, ext)) + } + .map(Arc::new); + if let Ok(mut m) = cache.lock() { + m.insert(src.to_string(), parsed.clone()); + } + parsed +} + +impl FileXform { + pub fn apply(&self, c: [f32; 3], interp: u32, inverse: bool, ccc_id: &str) -> [f32; 3] { + match self { + FileXform::Cdl(list) => { + let cdl = list.iter().find(|(id, _)| !ccc_id.is_empty() && id == ccc_id).or(list.first()).map(|(_, c)| *c).unwrap_or_default(); + let c64 = c.map(|v| v as f64); + let o = if inverse { cdl.invert(c64, true) } else { cdl.apply(c64, true) }; + o.map(|v| v as f32) + } + FileXform::Lut { lut, shaper } => { + let fwd = |c: [f32; 3]| { + let c = match shaper { + Some(s) => [shape(&s[0], c[0]), shape(&s[1], c[1]), shape(&s[2], c[2])], + None => c, + }; + lut.apply_interp(c, interp) + }; + if !inverse { + return fwd(c); + } + if lut.n3 == 0 { + // 1D: invert each channel exactly through the (monotone) tables. + let mut x = c; + if lut.n1 > 1 { + x = [0, 1, 2].map(|i| { + let curve: Vec<(f32, f32)> = + (0..lut.n1).map(|k| (lut.d1[k][i], lut.dmin[i] + (lut.dmax[i] - lut.dmin[i]) * k as f32 / (lut.n1 - 1) as f32)).collect(); + shape(&curve, c[i]) + }); + } + if let Some(s) = shaper { + x = [shape_inv(&s[0], x[0]), shape_inv(&s[1], x[1]), shape_inv(&s[2], x[2])]; + } + return x; + } + // 3D: damped fixed-point (Newton with an identity Jacobian) from the target. + let mut x = c; + for _ in 0..40 { + let y = fwd(x); + let e = [c[0] - y[0], c[1] - y[1], c[2] - y[2]]; + if e.iter().all(|v| v.abs() < 1e-6) { + break; + } + x = [x[0] + e[0], x[1] + e[1], x[2] + e[2]]; + } + x + } + } + } +} + +// ---------------------------------------------------------------- displays, views and looks + +/// Displays of the built-in config: (name, display primaries/white, encoding). +pub const DISPLAYS: &[(&str, [[f64; 2]; 3], Tf)] = + &[("sRGB", REC709, Tf::Srgb), ("Rec.1886 Rec.709", REC709, Tf::Gamma(2.4)), ("Display P3", P3, Tf::Srgb), ("Rec.2100-PQ", REC2020, Tf::Pq)]; + +pub const VIEWS: &[&str] = &["Standard", "Tone Mapped", "Raw"]; + +/// Built-in looks: CDLs applied in ACEScct. +pub fn looks() -> Vec<(&'static str, Cdl)> { + let grey = 0.413_588_402_492_442_1; // ACEScct of 0.18 + let c = |slope: [f64; 3], offset: [f64; 3], sat: f64| Cdl { slope, offset, power: [1.0; 3], sat }; + vec![ + ("Warm", c([1.04, 1.0, 0.94], [0.0; 3], 1.0)), + ("Cool", c([0.95, 1.0, 1.06], [0.0; 3], 1.0)), + ("High Contrast", c([1.25; 3], [grey * (1.0 - 1.25); 3], 1.0)), + ("Low Contrast", c([0.8; 3], [grey * (1.0 - 0.8); 3], 1.0)), + ("Desaturated", c([1.0; 3], [0.0; 3], 0.6)), + ("Vivid", c([1.0; 3], [0.0; 3], 1.3)), + ] +} + +/// Extended Reinhard roll-off (white point 16): maps 0..∞ into 0..1, ≈ identity near 0. +fn tonemap(v: f64) -> f64 { + let w2 = 256.0; + if v <= 0.0 { v } else { v * (1.0 + v / w2) / (1.0 + v) } +} + +fn tonemap_inv(y: f64) -> f64 { + if y <= 0.0 { + return y; + } + // Solve v² / w² + v (1 − y) − y = 0. + let (a, b, c) = (1.0 / 256.0, 1.0 - y, -y); + (-b + (b * b - 4.0 * a * c).sqrt()) / (2.0 * a) +} + +/// Display transform of an input-space colour (`inverse`: display code values → input space). +pub fn display_xform(input: &Space, display: usize, view: usize, inverse: bool) -> impl Fn([f32; 3]) -> [f32; 3] + Sync { + let (_, prims, tf) = DISPLAYS[display.min(DISPLAYS.len() - 1)]; + let disp = Space { name: "display", prims: Some((prims, D65)), tf: Tf::Linear, raw: false }; + let raw = view == 2 || input.raw; + let m_in = Xform::new(&Space { tf: Tf::Linear, ..*input }, &disp); + let m_out = Xform::new(&disp, &Space { tf: Tf::Linear, ..*input }); + let in_tf = input.tf; + move |c: [f32; 3]| { + if raw { + return c; + } + if !inverse { + let lin = c.map(|v| in_tf.decode(v as f64) as f32); + let d = m_in.apply(lin); + d.map(|v| { + let v = v as f64; + let v = if view == 1 { tonemap(v) } else { v }; + tf.encode(v) as f32 + }) + } else { + let d = c.map(|v| { + let l = tf.decode(v as f64); + (if view == 1 { tonemap_inv(l) } else { l }) as f32 + }); + m_out.apply(d).map(|v| in_tf.encode(v as f64) as f32) + } + } +} + +// ---------------------------------------------------------------- effects + +fn spec(id: &'static str, name: &'static str, cat: &'static str, params: Vec, render: crate::RenderFn) -> EffectSpec { + EffectSpec { id, name, category: cat, params, render, gpu: false, float: true } +} + +fn space_param(id: &'static str, name: &'static str, default: u32) -> crate::ParamSpec { + p(id, name, Value::Enum(default), popup(&space_names())) +} + +fn direction() -> crate::ParamSpec { + p("direction", "Direction", Value::Enum(0), popup(&["Forward", "Inverse"])) +} + +fn map_px(b: &mut Buf, f: impl Fn([f32; 3]) -> [f32; 3] + Sync) { + b.img.data.par_iter_mut().for_each(|px| { + let (c, a) = unpremul(*px); + if a <= 0.0 { + return; + } + let o = f(c); + *px = [o[0] * a, o[1] * a, o[2] * a, a]; + }); +} + +fn space_at(i: u32) -> &'static Space { + &SPACES[(i as usize).min(SPACES.len() - 1)] +} + +fn ocio_cst(ctx: &EffectCtx, mut b: Buf) -> Buf { + let (a, z) = (space_at(ctx.params.e("source")), space_at(ctx.params.e("destination"))); + let (a, z) = if ctx.params.e("direction") == 1 { (z, a) } else { (a, z) }; + if a.name == z.name { + return b; + } + let x = Xform::new(a, z); + map_px(&mut b, |c| x.apply(c)); + b +} + +fn cdl_from_params(pr: &crate::Params) -> Cdl { + Cdl { + slope: [pr.f("slopeRed"), pr.f("slopeGreen"), pr.f("slopeBlue")], + offset: [pr.f("offsetRed"), pr.f("offsetGreen"), pr.f("offsetBlue")], + power: [pr.f("powerRed").max(0.0), pr.f("powerGreen").max(0.0), pr.f("powerBlue").max(0.0)], + sat: pr.f("saturation").max(0.0), + } +} + +fn ocio_cdl(ctx: &EffectCtx, mut b: Buf) -> Buf { + let cdl = cdl_from_params(ctx.params); + if cdl == Cdl::default() { + return b; + } + let clamp = ctx.params.e("style") == 0; + let inv = ctx.params.e("direction") == 1; + map_px(&mut b, |c| { + let c = c.map(|v| v as f64); + let o = if inv { cdl.invert(c, clamp) } else { cdl.apply(c, clamp) }; + o.map(|v| v as f32) + }); + b +} + +fn ocio_display(ctx: &EffectCtx, mut b: Buf) -> Buf { + let input = space_at(ctx.params.e("source")); + let f = display_xform(input, ctx.params.e("display") as usize, ctx.params.e("view") as usize, ctx.params.e("direction") == 1); + map_px(&mut b, f); + b +} + +fn ocio_file(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(x) = load_file(ctx.params.s("file")) else { return b }; + let interp = match ctx.params.e("interpolation") { + 0 => 0, + 1 => 1, + _ => 2, + }; + let inv = ctx.params.e("direction") == 1; + let id = ctx.params.s("cccId").to_string(); + map_px(&mut b, |c| x.apply(c, interp, inv, &id)); + b +} + +/// Parse a looks string: comma/space separated names, `-Name` = inverse. +pub fn parse_looks(s: &str) -> Vec<(Cdl, bool)> { + let all = looks(); + s.split([',', ':']) + .map(str::trim) + .filter(|t| !t.is_empty()) + .filter_map(|t| { + let (inv, n) = match t.strip_prefix('-') { + Some(r) => (true, r.trim()), + None => (false, t.trim_start_matches('+').trim()), + }; + all.iter().find(|(name, _)| name.eq_ignore_ascii_case(n)).map(|(_, c)| (*c, inv)) + }) + .collect() +} + +fn ocio_look(ctx: &EffectCtx, mut b: Buf) -> Buf { + let mut list = parse_looks(ctx.params.s("looks")); + let pick = ctx.params.e("look"); + if pick > 0 + && let Some((_, c)) = looks().get(pick as usize - 1) + { + list.insert(0, (*c, false)); + } + if list.is_empty() { + return b; + } + let (src, dst) = (space_at(ctx.params.e("source")), space_at(ctx.params.e("destination"))); + let inv = ctx.params.e("direction") == 1; + let cct = space_by_name("ACEScct").expect("built-in"); + let (a, z) = if inv { (dst, src) } else { (src, dst) }; + let to_p = Xform::new(a, cct); + let from_p = Xform::new(cct, z); + if inv { + list.reverse(); + } + map_px(&mut b, |c| { + let mut v = to_p.apply(c).map(|x| x as f64); + for (cdl, li) in &list { + v = if *li != inv { cdl.invert(v, false) } else { cdl.apply(v, false) }; + } + from_p.apply(v.map(|x| x as f32)) + }); + b +} + +// ---- Color Profile Converter (Utility) + +pub const PROFILES: &[&str] = &["Project Working Space", "sRGB IEC61966-2.1", "HDTV (Rec. 709)", "Rec. 2020", "Display P3", "ACEScg", "ACES2065-1"]; + +fn profile(ctx: &EffectCtx, i: u32) -> (Space, bool) { + let ws = |cs: ColorSpace| match cs { + ColorSpace::Srgb => sp("sRGB", REC709, D65, Tf::Srgb), + ColorSpace::Rec709 => sp("Rec.709", REC709, D65, Tf::Gamma(2.4)), + ColorSpace::Rec2020 => sp("Rec.2020", REC2020, D65, Tf::Gamma(2.4)), + ColorSpace::DisplayP3 => sp("Display P3", P3, D65, Tf::Srgb), + }; + match i { + 0 => (ws(ctx.env.working_space.unwrap_or(ColorSpace::Srgb)), ctx.env.working_linear), + 1 => (ws(ColorSpace::Srgb), false), + 2 => (ws(ColorSpace::Rec709), false), + 3 => (ws(ColorSpace::Rec2020), false), + 4 => (ws(ColorSpace::DisplayP3), false), + 5 => (*space_by_name("ACEScg").expect("built-in"), true), + _ => (*space_by_name("ACES2065-1").expect("built-in"), true), + } +} + +/// Gamut compression for the perceptual intent: pull out-of-gamut (negative) colours toward +/// their luminance just enough to bring every channel to ≥ 0, rolling off smoothly. +fn compress_gamut(c: [f64; 3], luma: [f64; 3]) -> [f64; 3] { + let l = c[0] * luma[0] + c[1] * luma[1] + c[2] * luma[2]; + if l <= 0.0 { + return c.map(|v| v.max(0.0)); + } + let mn = c.iter().cloned().fold(f64::INFINITY, f64::min); + if mn >= 0.0 { + return c; + } + // Fraction of the way toward grey that reaches zero on the most negative channel. + let t = (l / (l - mn)).clamp(0.0, 1.0); + c.map(|v| l + (v - l) * t) +} + +fn profile_converter(ctx: &EffectCtx, mut b: Buf) -> Buf { + let (inp, in_lin_default) = profile(ctx, ctx.params.e("inputProfile")); + let (out, out_lin_default) = profile(ctx, ctx.params.e("outputProfile")); + let in_lin = ctx.params.b("linearizeInputProfile") || in_lin_default && ctx.params.e("inputProfile") == 0; + let out_lin = ctx.params.b("linearizeOutputProfile") || out_lin_default && ctx.params.e("outputProfile") == 0; + let intent = ctx.params.e("intent"); + let dec = if in_lin { Tf::Linear } else { inp.tf }; + let enc = if out_lin { Tf::Linear } else { out.tf }; + // Absolute colorimetric keeps the source white (no adaptation); the others adapt. + let m = if intent == 3 { mul(&invert(&out.to_ref(false)), &inp.to_ref(false)) } else { matrix(&inp, &out) }; + let identity = dec == enc && (0..3).all(|i| (0..3).all(|j| (m[i][j] - IDENTITY[i][j]).abs() < 1e-9)); + if identity { + return b; + } + let out_luma = out.to_ref(true)[1]; + map_px(&mut b, |c| { + let l = c.map(|v| dec.decode(v as f64)); + let mut o = mul_vec(&m, l); + match intent { + 0 => o = compress_gamut(o, out_luma), + 2 => o = o.map(|v| v.max(0.0)), + _ => {} + } + o.map(|v| enc.encode(v) as f32) + }); + b +} + +// ---- Color Stabilizer + +/// Mean straight colour of a square (half-size `r` layer pixels) around layer point `pt`. +fn sample_mean(b: &Buf, pt: [f64; 2], r: f64) -> [f64; 3] { + let (cx, cy) = b.to_px(pt); + let rr = (r * b.scale).max(0.5); + let (x0, x1) = ((cx - rr).floor().max(0.0) as i64, ((cx + rr).ceil() as i64).min(b.img.width as i64)); + let (y0, y1) = ((cy - rr).floor().max(0.0) as i64, ((cy + rr).ceil() as i64).min(b.img.height as i64)); + let mut acc = [0.0f64; 4]; + for y in y0..y1 { + for x in x0..x1 { + let p = b.img.get(x, y); + for k in 0..4 { + acc[k] += p[k] as f64; + } + } + } + if acc[3] <= 1e-9 { + return [0.0; 3]; + } + [acc[0] / acc[3], acc[1] / acc[3], acc[2] / acc[3]] +} + +/// Monotone piecewise-quadratic curve through (0,0), three samples and (1,1) (linear between +/// them is enough for a stabiliser and stays monotone). +fn curve3(xs: [f64; 3], ys: [f64; 3], v: f64) -> f64 { + let mut pts = vec![(0.0, 0.0)]; + let mut pairs: Vec<(f64, f64)> = xs.iter().cloned().zip(ys.iter().cloned()).collect(); + pairs.sort_by(|a, b| a.0.total_cmp(&b.0)); + for (x, y) in pairs { + if x > pts.last().expect("non-empty").0 + 1e-4 && x < 1.0 - 1e-4 { + pts.push((x, y)); + } + } + pts.push((1.0, 1.0)); + let i = pts.partition_point(|p| p.0 < v).clamp(1, pts.len() - 1); + let (a, c) = (pts[i - 1], pts[i]); + a.1 + (c.1 - a.1) * (v - a.0) / (c.0 - a.0).max(1e-9) +} + +fn color_stabilizer(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(host) = ctx.env.host else { return b }; + let Some(reference) = host.self_at(ctx.params.f("referenceFrame"), ctx.env.effect_index) else { return b }; + let r = ctx.params.f("sampleSize").max(0.5); + let pts = [ctx.params.v2("blackPoint"), ctx.params.v2("midPoint"), ctx.params.v2("whitePoint")]; + let cur = pts.map(|pt| sample_mean(&b, pt, r)); + let refv = pts.map(|pt| sample_mean(&reference, pt, r)); + let mode = ctx.params.e("stabilize"); + map_px(&mut b, |c| { + [0, 1, 2].map(|k| { + let v = c[k] as f64; + let o = match mode { + // Brightness: one sample (black point) — offset each channel. + 0 => v + refv[0][k] - cur[0][k], + // Levels: black and white samples — linear map per channel. + 1 => { + let (c0, c1, r0, r1) = (cur[0][k], cur[2][k], refv[0][k], refv[2][k]); + if (c1 - c0).abs() < 1e-6 { v + r0 - c0 } else { r0 + (v - c0) * (r1 - r0) / (c1 - c0) } + } + // Curves: three samples — a curve through them per channel. + _ => curve3([cur[0][k], cur[1][k], cur[2][k]], [refv[0][k], refv[1][k], refv[2][k]], v), + }; + o as f32 + }) + }); + b +} + +fn sl(lo: f64, hi: f64, smin: f64, smax: f64) -> ParamUi { + slider(lo, hi, smin, smax, 3) +} + +pub fn specs() -> Vec { + let cc = "Color Correction"; + let srgb = SPACES.iter().position(|s| s.name == "sRGB").unwrap_or(0) as u32; + let lin709 = SPACES.iter().position(|s| s.name == "Linear Rec.709 (sRGB)").unwrap_or(0) as u32; + let acescg = SPACES.iter().position(|s| s.name == "ACEScg").unwrap_or(0) as u32; + let mut look_opts = vec!["None"]; + look_opts.extend(looks().iter().map(|(n, _)| *n)); + vec![ + spec( + "ec.color.ociocdl", + "OCIO CDL Transform", + cc, + vec![ + p("slopeRed", "Slope Red", num(1.0), sl(0.0, 100.0, 0.0, 4.0)), + p("slopeGreen", "Slope Green", num(1.0), sl(0.0, 100.0, 0.0, 4.0)), + p("slopeBlue", "Slope Blue", num(1.0), sl(0.0, 100.0, 0.0, 4.0)), + p("offsetRed", "Offset Red", num(0.0), sl(-100.0, 100.0, -1.0, 1.0)), + p("offsetGreen", "Offset Green", num(0.0), sl(-100.0, 100.0, -1.0, 1.0)), + p("offsetBlue", "Offset Blue", num(0.0), sl(-100.0, 100.0, -1.0, 1.0)), + p("powerRed", "Power Red", num(1.0), sl(0.0, 100.0, 0.0, 4.0)), + p("powerGreen", "Power Green", num(1.0), sl(0.0, 100.0, 0.0, 4.0)), + p("powerBlue", "Power Blue", num(1.0), sl(0.0, 100.0, 0.0, 4.0)), + p("saturation", "Saturation", num(1.0), sl(0.0, 100.0, 0.0, 4.0)), + p("style", "Style", Value::Enum(0), popup(&["ASC CDL v1.2", "No Clamp"])), + direction(), + ], + ocio_cdl, + ), + spec( + "ec.color.ociocolorspace", + "OCIO Color Space Transform", + cc, + vec![ + p("config", "Configuration", Value::Enum(0), popup(&["Built-in (EffectCraft minimal)"])), + space_param("source", "Source", lin709), + space_param("destination", "Destination", srgb), + direction(), + ], + ocio_cst, + ), + spec( + "ec.color.ociodisplay", + "OCIO Display Transform", + cc, + vec![ + p("config", "Configuration", Value::Enum(0), popup(&["Built-in (EffectCraft minimal)"])), + space_param("source", "Source", lin709), + p("display", "Display", Value::Enum(0), popup(&DISPLAYS.iter().map(|d| d.0).collect::>())), + p("view", "View", Value::Enum(0), popup(VIEWS)), + direction(), + ], + ocio_display, + ), + // `file`: a path to a .cube/.3dl/.csp/.cc/.ccc/.cdl file, or the file's text itself. + spec( + "ec.color.ociofile", + "OCIO File Transform", + cc, + vec![ + p("file", "File", Value::Str(String::new()), ParamUi::Text), + p("cccId", "CCC ID", Value::Str(String::new()), ParamUi::Text), + p("interpolation", "Interpolation", Value::Enum(3), popup(&["Nearest", "Linear", "Tetrahedral", "Best"])), + direction(), + ], + ocio_file, + ), + spec( + "ec.color.ociolook", + "OCIO Look Transform", + cc, + vec![ + p("config", "Configuration", Value::Enum(0), popup(&["Built-in (EffectCraft minimal)"])), + space_param("source", "Source", acescg), + space_param("destination", "Destination", acescg), + p("look", "Look", Value::Enum(0), popup(&look_opts)), + p("looks", "Looks", Value::Str(String::new()), ParamUi::Text), + direction(), + ], + ocio_look, + ), + spec( + "ec.color.colorstabilizer", + "Color Stabilizer", + cc, + vec![ + p("stabilize", "Stabilize", Value::Enum(0), popup(&["Brightness", "Levels", "Curves"])), + p("referenceFrame", "Reference Frame (s)", num(0.0), slider(-100000.0, 100000.0, 0.0, 60.0, 2)), + p("blackPoint", "Black Point", Value::Vec2([0.25, 0.75]), ParamUi::Point), + p("midPoint", "Mid Point", Value::Vec2([0.5, 0.5]), ParamUi::Point), + p("whitePoint", "White Point", Value::Vec2([0.75, 0.25]), ParamUi::Point), + p("sampleSize", "Sample Size", num(5.0), slider(1.0, 100.0, 1.0, 50.0, 0)), + ], + color_stabilizer, + ), + spec( + "ec.utility.colorprofileconverter", + "Color Profile Converter", + "Utility", + vec![ + p("inputProfile", "Input Profile", Value::Enum(0), popup(PROFILES)), + p("linearizeInputProfile", "Linearize Input Profile", Value::Bool(false), ParamUi::Checkbox), + p("outputProfile", "Output Profile", Value::Enum(1), popup(PROFILES)), + p("linearizeOutputProfile", "Linearize Output Profile", Value::Bool(false), ParamUi::Checkbox), + p("intent", "Intent", Value::Enum(1), popup(&["Perceptual", "Relative Colorimetric", "Saturation", "Absolute Colorimetric"])), + ], + profile_converter, + ), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, EffectHost, Image, LayerPixels, run_fx}; + + fn close(a: M3, b: M3, tol: f64) { + for i in 0..3 { + for j in 0..3 { + assert!((a[i][j] - b[i][j]).abs() < tol, "{a:?} vs {b:?}"); + } + } + } + + #[test] + fn aces_matrices_match_published_values() { + let ap0 = space_by_name("ACES2065-1").unwrap(); + let ap1 = space_by_name("ACEScg").unwrap(); + // Academy S-2014-004 AP0 → AP1. + close( + matrix(ap0, ap1), + [ + [1.451_439_316_1, -0.236_510_746_9, -0.214_928_569_3], + [-0.076_553_773_4, 1.176_229_699_8, -0.099_675_926_4], + [0.008_316_148_4, -0.006_032_449_8, 0.997_716_301_4], + ], + 2e-4, + ); + // ACEScg → linear Rec.709 with Bradford adaptation (published ACES config value). + let lin = space_by_name("Linear Rec.709 (sRGB)").unwrap(); + close(matrix(ap1, lin), [[1.705_05, -0.621_79, -0.083_26], [-0.130_26, 1.140_80, -0.010_55], [-0.024_00, -0.128_97, 1.152_97]], 2e-3); + // White maps to white. + let w = mul_vec(&matrix(ap1, lin), [1.0, 1.0, 1.0]); + assert!(w.iter().all(|v| (v - 1.0).abs() < 1e-3), "{w:?}"); + } + + #[test] + fn transfer_functions_round_trip() { + for tf in [Tf::Srgb, Tf::Gamma(2.4), Tf::AcesCct, Tf::Pq] { + for v in [0.0, 0.001, 0.18, 0.5, 1.0, 4.0] { + let r = tf.decode(tf.encode(v)); + assert!((r - v).abs() < 1e-6 * v.max(1.0), "{tf:?} {v} {r}"); + } + } + // ACEScct of 18 % grey (S-2016-001). + assert!((Tf::AcesCct.encode(0.18) - 0.413_588).abs() < 1e-5); + // PQ: 100 nits (1.0) encodes to ≈ 0.508. + assert!((Tf::Pq.encode(1.0) - 0.508).abs() < 1e-3); + } + + #[test] + fn color_space_transform_effect_round_trips() { + let img = Image::filled(4, 4, [0.2, 0.5, 0.7, 1.0]); + let srgb = SPACES.iter().position(|s| s.name == "sRGB").unwrap() as u32; + let cg = SPACES.iter().position(|s| s.name == "ACEScg").unwrap() as u32; + let a = run_fx("ec.color.ociocolorspace", &[("source", Value::Enum(srgb)), ("destination", Value::Enum(cg))], img.clone(), 0.0, EffectEnv::default()); + assert_ne!(a.img.data, img.data); + let b = run_fx("ec.color.ociocolorspace", &[("source", Value::Enum(cg)), ("destination", Value::Enum(srgb))], a.img.clone(), 0.0, EffectEnv::default()); + let c = run_fx( + "ec.color.ociocolorspace", + &[("source", Value::Enum(srgb)), ("destination", Value::Enum(cg)), ("direction", Value::Enum(1))], + a.img, + 0.0, + EffectEnv::default(), + ); + for (x, y) in b.img.data.iter().zip(&img.data).chain(c.img.data.iter().zip(&img.data)) { + assert!((0..4).all(|k| (x[k] - y[k]).abs() < 1e-4), "{x:?} {y:?}"); + } + } + + #[test] + fn cdl_math() { + let cdl = Cdl { slope: [1.2, 1.0, 0.8], offset: [0.05, 0.0, -0.02], power: [1.1, 1.0, 0.9], sat: 0.8 }; + let c = [0.3, 0.5, 0.7]; + // Hand computation of the v1.2 formula. + let sop = [(0.3f64 * 1.2 + 0.05).powf(1.1), 0.5, (0.7f64 * 0.8 - 0.02).powf(0.9)]; + let l = sop[0] * 0.2126 + sop[1] * 0.7152 + sop[2] * 0.0722; + let want = sop.map(|v| l + 0.8 * (v - l)); + let got = cdl.apply(c, true); + for k in 0..3 { + assert!((got[k] - want[k]).abs() < 1e-12); + } + let back = cdl.invert(got, true); + for k in 0..3 { + assert!((back[k] - c[k]).abs() < 1e-9); + } + // v1.2 clamps, No Clamp keeps negatives. + let neg = Cdl { offset: [-0.5; 3], ..Cdl::default() }; + assert_eq!(neg.apply([0.2; 3], true), [0.0; 3]); + assert!((neg.apply([0.2; 3], false)[0] + 0.3).abs() < 1e-12); + let img = Image::filled(2, 2, [0.3, 0.5, 0.7, 1.0]); + let out = run_fx("ec.color.ociocdl", &[("slopeRed", num(1.2)), ("offsetRed", num(0.05)), ("powerRed", num(1.1))], img, 0.0, EffectEnv::default()); + let r = (0.3f64 * 1.2 + 0.05).powf(1.1); + assert!((out.img.get(0, 0)[0] as f64 - r).abs() < 1e-5); + } + + fn identity_lattice(n: usize, f: impl Fn([f32; 3]) -> [f32; 3]) -> Vec<[f32; 3]> { + let mut v = vec![]; + for b in 0..n { + for g in 0..n { + for r in 0..n { + let s = (n - 1) as f32; + v.push(f([r as f32 / s, g as f32 / s, b as f32 / s])); + } + } + } + v + } + + #[test] + fn lut_files_parse_and_interpolate() { + // .cube with a non-linear lattice: trilinear between lattice points. + let f = |c: [f32; 3]| [c[0] * c[0], c[1], 1.0 - c[2]]; + let mut cube = String::from("LUT_3D_SIZE 3\n"); + for r in identity_lattice(3, f) { + cube += &format!("{} {} {}\n", r[0], r[1], r[2]); + } + let Some(FileXform::Lut { lut, .. }) = parse_file(&cube, "cube") else { panic!() }; + // Trilinear between 0.5 (0.25) and 1.0 (1.0) at 0.75 → 0.625. + let o = lut.apply_interp([0.75, 0.3, 0.2], 1); + assert!((o[0] - 0.625).abs() < 1e-6 && (o[1] - 0.3).abs() < 1e-6 && (o[2] - 0.8).abs() < 1e-6, "{o:?}"); + // Nearest snaps. + assert_eq!(lut.apply_interp([0.74, 0.0, 0.0], 0)[0], 0.25); + // .3dl (blue fastest, 10-bit integers, mesh line). + let mut tdl = String::from("0 511 1023\n"); + for r in 0..3 { + for g in 0..3 { + for b in 0..3 { + let v = [r, g, b].map(|i| i as f64 * 511.5); + tdl += &format!("{} {} {}\n", v[0].round(), v[1].round(), (1023.0 - v[2]).round()); + } + } + } + let Some(FileXform::Lut { lut, .. }) = parse_file(&tdl, "3dl") else { panic!() }; + let o = lut.apply_interp([0.25, 0.5, 0.25], 1); + assert!((o[0] - 0.25).abs() < 2e-3 && (o[1] - 0.5).abs() < 2e-3 && (o[2] - 0.75).abs() < 2e-3, "{o:?}"); + // .csp with a squaring pre-LUT on red and an identity 2³ lattice. + let mut csp = String::from("CSPLUTV100\n3D\n\n3\n0 0.5 1\n0 0.25 1\n2\n0 1\n0 1\n2\n0 1\n0 1\n\n2 2 2\n"); + for r in identity_lattice(2, |c| c) { + csp += &format!("{} {} {}\n", r[0], r[1], r[2]); + } + let x = parse_file(&csp, "csp").unwrap(); + let o = x.apply([0.5, 0.4, 0.6], 2, false, ""); + assert!((o[0] - 0.25).abs() < 1e-6 && (o[1] - 0.4).abs() < 1e-6, "{o:?}"); + let inv = x.apply(o, 2, true, ""); + assert!((inv[0] - 0.5).abs() < 1e-4, "{inv:?}"); + // CDL collection: pick by id. + let ccc = r#"2 2 20 0 01 1 1 + 0.5 0.5 0.51"#; + let x = parse_file(ccc, "ccc").unwrap(); + assert!((x.apply([0.4; 3], 2, false, "b")[0] - 0.2).abs() < 1e-6); + assert!((x.apply([0.4; 3], 2, false, "a")[0] - 0.8).abs() < 1e-6); + assert!((x.apply([0.4; 3], 2, false, "")[0] - 0.8).abs() < 1e-6); + // Effect with inline content; inverse of a 3D lattice. + let img = Image::filled(2, 2, [0.6, 0.3, 0.2, 1.0]); + let out = run_fx("ec.color.ociofile", &[("file", Value::Str(cube.clone()))], img.clone(), 0.0, EffectEnv::default()); + let back = run_fx("ec.color.ociofile", &[("file", Value::Str(cube)), ("direction", Value::Enum(1))], out.img, 0.0, EffectEnv::default()); + assert!((back.img.get(0, 0)[0] - 0.6).abs() < 1e-3, "{:?}", back.img.get(0, 0)); + assert!(parse_file("garbage", "3dl").is_none()); + } + + #[test] + fn display_and_look_transforms() { + // Linear 0.18 through the sRGB display / Standard view = sRGB encoding of 0.18. + let img = Image::filled(2, 2, [0.18, 0.18, 0.18, 1.0]); + let out = run_fx("ec.color.ociodisplay", &[], img.clone(), 0.0, EffectEnv::default()); + assert!((out.img.get(0, 0)[0] as f64 - Tf::Srgb.encode(0.18)).abs() < 1e-5); + let back = run_fx("ec.color.ociodisplay", &[("direction", Value::Enum(1))], out.img, 0.0, EffectEnv::default()); + assert!((back.img.get(0, 0)[0] - 0.18).abs() < 1e-5); + // Tone mapped view keeps highlights below 1. + let hot = Image::filled(1, 1, [8.0, 8.0, 8.0, 1.0]); + let tm = run_fx("ec.color.ociodisplay", &[("view", Value::Enum(1))], hot, 0.0, EffectEnv::default()); + assert!(tm.img.get(0, 0)[0] < 1.0); + assert!((tonemap_inv(tonemap(3.0)) - 3.0).abs() < 1e-9); + // Looks: Warm raises red over blue; its inverse undoes it. + let grey = Image::filled(1, 1, [0.18, 0.18, 0.18, 1.0]); + let w = run_fx("ec.color.ociolook", &[("looks", Value::Str("Warm".into()))], grey.clone(), 0.0, EffectEnv::default()); + let px = w.img.get(0, 0); + assert!(px[0] > px[2]); + let u = run_fx("ec.color.ociolook", &[("looks", Value::Str("-Warm".into()))], w.img, 0.0, EffectEnv::default()); + assert!((u.img.get(0, 0)[0] - 0.18).abs() < 1e-4 && (u.img.get(0, 0)[2] - 0.18).abs() < 1e-4); + assert_eq!(parse_looks("Warm, -Cool, nope").len(), 2); + } + + #[test] + fn profile_converter_converts_and_round_trips() { + let img = Image::filled(2, 2, [0.8, 0.4, 0.1, 1.0]); + // sRGB → Rec. 2020 (relative), then back. + let a = run_fx( + "ec.utility.colorprofileconverter", + &[("inputProfile", Value::Enum(1)), ("outputProfile", Value::Enum(3))], + img.clone(), + 0.0, + EffectEnv::default(), + ); + assert_ne!(a.img.data, img.data); + let b = run_fx( + "ec.utility.colorprofileconverter", + &[("inputProfile", Value::Enum(3)), ("outputProfile", Value::Enum(1))], + a.img, + 0.0, + EffectEnv::default(), + ); + assert!((0..3).all(|k| (b.img.get(0, 0)[k] - img.get(0, 0)[k]).abs() < 1e-4)); + // Same profile both sides: identity. Working space defaults to sRGB. + let c = run_fx( + "ec.utility.colorprofileconverter", + &[("inputProfile", Value::Enum(0)), ("outputProfile", Value::Enum(1))], + img.clone(), + 0.0, + EffectEnv::default(), + ); + assert_eq!(c.img.data, img.data); + // Perceptual pulls out-of-gamut colours inside: saturated Rec.2020 green into sRGB. + let g = Image::filled(1, 1, [0.0, 1.0, 0.0, 1.0]); + let rel = run_fx( + "ec.utility.colorprofileconverter", + &[("inputProfile", Value::Enum(3)), ("outputProfile", Value::Enum(1))], + g.clone(), + 0.0, + EffectEnv::default(), + ); + assert!(rel.img.get(0, 0)[0] < 0.0); + let per = run_fx( + "ec.utility.colorprofileconverter", + &[("inputProfile", Value::Enum(3)), ("outputProfile", Value::Enum(1)), ("intent", Value::Enum(0))], + g, + 0.0, + EffectEnv::default(), + ); + assert!(per.img.get(0, 0).iter().all(|v| *v >= -1e-6)); + } + + struct RefHost(Image); + impl EffectHost for RefHost { + fn layer(&self, _: u64, _: bool) -> Option { + None + } + fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option> { + None + } + fn self_at(&self, _: f64, _: usize) -> Option { + Some(Buf { img: self.0.clone(), offset: [0.0; 2], scale: 1.0 }) + } + } + + #[test] + fn color_stabilizer_matches_the_reference_frame() { + let mk = |k: f32, o: f32| { + let mut img = Image::new(40, 40); + for y in 0..40 { + for x in 0..40 { + let v = (x as f32 / 40.0) * k + o; + img.set(x, y, [v, v * 0.9, v * 0.8, 1.0]); + } + } + img + }; + let reference = mk(1.0, 0.0); + let flicker = mk(0.8, 0.1); + let host = RefHost(reference.clone()); + let env = EffectEnv { host: Some(&host), ..Default::default() }; + let pts = [("blackPoint", Value::Vec2([5.0, 20.0])), ("midPoint", Value::Vec2([20.0, 20.0])), ("whitePoint", Value::Vec2([35.0, 20.0]))]; + let mut vals: Vec<(&str, Value)> = pts.to_vec(); + vals.push(("stabilize", Value::Enum(1))); + vals.push(("sampleSize", num(1.0))); + let out = run_fx("ec.color.colorstabilizer", &vals, flicker.clone(), 0.0, env); + for x in [5i64, 20, 35] { + let (o, r) = (out.img.get(x, 20), reference.get(x, 20)); + assert!((o[0] - r[0]).abs() < 0.01, "x {x}: {o:?} vs {r:?}"); + } + // Deterministic, and a no-op without a host. + let again = run_fx("ec.color.colorstabilizer", &vals, flicker.clone(), 0.0, env); + assert_eq!(out.img.data, again.img.data); + let none = run_fx("ec.color.colorstabilizer", &vals, flicker.clone(), 0.0, EffectEnv::default()); + assert_eq!(none.img.data, flicker.data); + // Brightness mode corrects an offset at the black point. + let lifted = mk(1.0, 0.1); + let mut vals2: Vec<(&str, Value)> = pts.to_vec(); + vals2.push(("sampleSize", num(1.0))); + let b = run_fx("ec.color.colorstabilizer", &vals2, lifted, 0.0, env); + assert!((b.img.get(5, 20)[0] - reference.get(5, 20)[0]).abs() < 0.01); + } +} diff --git a/crates/effects/src/utility.rs b/crates/effects/src/utility.rs index 49b7ed36..7689050d 100644 --- a/crates/effects/src/utility.rs +++ b/crates/effects/src/utility.rs @@ -153,7 +153,7 @@ fn cineon(ctx: &EffectCtx, mut b: Buf) -> Buf { // ---- Colour LUTs (.cube) ---- #[derive(Clone, Debug, PartialEq)] -pub(crate) struct Lut { +pub struct Lut { pub n1: usize, pub d1: Vec<[f32; 3]>, pub n3: usize, @@ -163,7 +163,7 @@ pub(crate) struct Lut { } /// Parse `.cube` text (TITLE, LUT_1D_SIZE, LUT_3D_SIZE, DOMAIN_MIN/MAX, data red-fastest). -pub(crate) fn parse_cube(text: &str) -> Option { +pub fn parse_cube(text: &str) -> Option { let mut lut = Lut { n1: 0, d1: vec![], n3: 0, d3: vec![], dmin: [0.0; 3], dmax: [1.0; 3] }; let mut rows: Vec<[f32; 3]> = Vec::new(); let three = |it: &mut std::str::SplitWhitespace| -> Option<[f32; 3]> { @@ -238,6 +238,57 @@ impl Lut { } c } + /// Apply with a choice of 3D interpolation: 0 nearest, 1 trilinear, otherwise tetrahedral. + pub fn apply_interp(&self, c: [f32; 3], interp: u32) -> [f32; 3] { + let mut c = c; + if self.n1 > 1 { + let t = self.norm(c); + c = [0, 1, 2].map(|i| { + let x = t[i] * (self.n1 - 1) as f32; + if interp == 0 { + return self.d1[(x.round() as usize).min(self.n1 - 1)][i]; + } + let i0 = (x.floor() as usize).min(self.n1 - 2); + let f = x - i0 as f32; + self.d1[i0][i] + (self.d1[i0 + 1][i] - self.d1[i0][i]) * f + }); + } + if self.n3 > 1 { + let t = self.norm(c); + c = match interp { + 0 => self.nearest(t), + 1 => self.trilinear(t), + _ => self.tetra(t), + }; + } + c + } + fn nearest(&self, t: [f32; 3]) -> [f32; 3] { + let n = self.n3; + let s = (n - 1) as f32; + let i = |x: f32| ((x * s).round() as usize).min(n - 1); + self.d3[i(t[0]) + i(t[1]) * n + i(t[2]) * n * n] + } + /// Trilinear interpolation on the lattice (red fastest). + pub fn trilinear(&self, t: [f32; 3]) -> [f32; 3] { + let n = self.n3; + let s = (n - 1) as f32; + let idx = |x: f32| { + let v = x * s; + let i = (v.floor() as usize).min(n - 2); + (i, v - i as f32) + }; + let (r, fr) = idx(t[0]); + let (g, fg) = idx(t[1]); + let (b, fb) = idx(t[2]); + let at = |dr: usize, dg: usize, db: usize| self.d3[(r + dr) + (g + dg) * n + (b + db) * n * n]; + let l = |a: [f32; 3], b: [f32; 3], f: f32| [a[0] + (b[0] - a[0]) * f, a[1] + (b[1] - a[1]) * f, a[2] + (b[2] - a[2]) * f]; + let c00 = l(at(0, 0, 0), at(1, 0, 0), fr); + let c10 = l(at(0, 1, 0), at(1, 1, 0), fr); + let c01 = l(at(0, 0, 1), at(1, 0, 1), fr); + let c11 = l(at(0, 1, 1), at(1, 1, 1), fr); + l(l(c00, c10, fg), l(c01, c11, fg), fb) + } fn tetra(&self, t: [f32; 3]) -> [f32; 3] { let n = self.n3; let s = (n - 1) as f32; diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 14a81575..bd42d31a 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -527,8 +527,15 @@ impl<'a> Renderer<'a> { let layer_size = if size.0 == 0 { [ctx.comp.width as f64, ctx.comp.height as f64] } else { [size.0 as f64, size.1 as f64] }; let mask_shapes = masks::shapes(ctx, layer); let host = FxHost { r: self, ctx, layer, index: Default::default() }; - let env = - EffectEnv { masks: &mask_shapes, host: Some(&host), comp_time: ctx.time.seconds(), frame_rate: ctx.comp.frame_rate.as_f64(), effect_index: 0 }; + let env = EffectEnv { + masks: &mask_shapes, + host: Some(&host), + comp_time: ctx.time.seconds(), + frame_rate: ctx.comp.frame_rate.as_f64(), + effect_index: 0, + working_space: self.pipe.space, + working_linear: self.pipe.linear, + }; // Video effects in stack order (index, group, spec); disabled and audio effects skipped. let stack: Vec<(usize, &effectcraft_project::PropGroup, &'static effectcraft_effects::EffectSpec)> = fx .groups() From d9fe03a9653ba78301960611fff35150887969f3 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 20:54:26 -0400 Subject: [PATCH 03/23] M9.11: Immersive Video effects (all 12 VR effects) Seam-aware equirectangular processing (wrap-around, pole continuation, latitude-scaled filters), per-eye stereo layouts, VR Converter between equirectangular, cube maps (4:3, 3:2, 6:1), sphere map and 2D. Co-Authored-By: Claude Opus 5.5 --- crates/effects/src/lib.rs | 3 + crates/effects/src/vr.rs | 1188 +++++++++++++++++++++++++++++++++++++ 2 files changed, 1191 insertions(+) create mode 100644 crates/effects/src/vr.rs diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 4bb658c3..14a43464 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -43,6 +43,7 @@ mod transition2; pub mod util; mod utility; mod utility2; +mod vr; use std::collections::HashMap; use std::sync::OnceLock; @@ -354,6 +355,7 @@ pub fn registry() -> &'static [EffectSpec] { v.extend(color3::specs()); v.extend(obsolete::specs()); v.extend(ocio::specs()); + v.extend(vr::specs()); v.extend(time_fx::specs()); v.extend(audio_fx::specs()); v.extend(paint::specs()); @@ -469,6 +471,7 @@ pub const TIME_DEPENDENT: &[&str] = &[ "ec.time.ccforcemotionblur", "ec.time.ccwidetime", "ec.time.pixelmotionblur", + "ec.vr.digitalglitch", ]; /// See [`TIME_DEPENDENT`]. diff --git a/crates/effects/src/vr.rs b/crates/effects/src/vr.rs new file mode 100644 index 00000000..ba7e6d9d --- /dev/null +++ b/crates/effects/src/vr.rs @@ -0,0 +1,1188 @@ +//! Immersive Video effects (VR Blur, VR Chromatic Aberrations, VR Color Gradients, VR Converter, +//! VR De-Noise, VR Digital Glitch, VR Fractal Noise, VR Glow, VR Plane to Sphere, VR Rotate +//! Sphere, VR Sharpen, VR Sphere To Plane). +//! +//! All of them treat the layer as a 360° equirectangular frame (longitude across, latitude down; +//! the front, +Z, at the centre) and are **seam-aware**: filters wrap around the left/right edge, +//! continue across the poles (a column continues on the opposite meridian), and spatial filters +//! widen their horizontal footprint by 1/cos(latitude) so a blur is the same size on the sphere +//! everywhere. Stereo footage (Frame Layout: over/under or side by side) is processed per eye. +//! +//! VR Converter maps between equirectangular, cube maps (4:3 cross, 3:2 and 6:1 strips), an +//! angular fisheye ("sphere map") and a rectilinear 2D view, through a common view direction. + +use std::f64::consts::PI; + +use effectcraft_color::{BlendMode, blend_pixel, luminance}; +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use effectcraft_raster::{Image, Px}; +use rayon::prelude::*; + +use crate::generate::value_noise; +use crate::util::{hash1, unpremul}; +use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider}; + +type V3 = [f64; 3]; + +fn spec(id: &'static str, name: &'static str, params: Vec, render: crate::RenderFn) -> EffectSpec { + EffectSpec { id, name, category: "Immersive Video", params, render, gpu: false, float: true } +} + +pub const FRAME_LAYOUTS: [&str; 3] = ["Monoscopic", "Stereo - Over/Under", "Stereo - Side by Side"]; + +fn layout_param() -> crate::ParamSpec { + p("frameLayout", "Frame Layout", Value::Enum(0), popup(&FRAME_LAYOUTS)) +} + +/// Eye rectangles (x, y, w, h) of a frame. +fn eyes(w: u32, h: u32, layout: u32) -> Vec<(u32, u32, u32, u32)> { + match layout { + 1 if h >= 2 => vec![(0, 0, w, h / 2), (0, h / 2, w, h - h / 2)], + 2 if w >= 2 => vec![(0, 0, w / 2, h), (w / 2, 0, w - w / 2, h)], + _ => vec![(0, 0, w, h)], + } +} + +/// Run `f` on every eye of `img` and reassemble. +fn per_eye(img: &Image, layout: u32, f: impl Fn(&Image, usize) -> Image + Sync) -> Image { + let rects = eyes(img.width, img.height, layout); + if rects.len() == 1 { + return f(img, 0); + } + let outs: Vec = rects.par_iter().enumerate().map(|(i, &(x, y, w, h))| f(&img.crop(x as i64, y as i64, w, h), i)).collect(); + let mut out = Image::new(img.width, img.height); + for (&(x0, y0, w, _), o) in rects.iter().zip(&outs) { + for (yy, row) in o.data.chunks(w.max(1) as usize).enumerate() { + let start = out.idx(x0, y0 + yy as u32); + out.data[start..start + row.len()].copy_from_slice(row); + } + } + out +} + +// ---------------------------------------------------------------- sphere geometry + +#[inline] +fn norm(v: V3) -> V3 { + let l = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt().max(1e-300); + [v[0] / l, v[1] / l, v[2] / l] +} + +#[inline] +fn dot(a: V3, b: V3) -> f64 { + a[0] * b[0] + a[1] * b[1] + a[2] * b[2] +} + +/// Direction (y up, +z front) of an equirectangular position in pixels. +#[inline] +pub fn equi_dir(x: f64, y: f64, w: f64, h: f64) -> V3 { + let lon = (x / w - 0.5) * 2.0 * PI; + let lat = (0.5 - y / h) * PI; + [lat.cos() * lon.sin(), lat.sin(), lat.cos() * lon.cos()] +} + +/// Equirectangular pixel position of a direction. +#[inline] +pub fn equi_px(d: V3, w: f64, h: f64) -> (f64, f64) { + let lon = d[0].atan2(d[2]); + let lat = d[1].clamp(-1.0, 1.0).asin(); + ((lon / (2.0 * PI) + 0.5) * w, (0.5 - lat / PI) * h) +} + +/// Bilinear sample of an equirectangular image: wraps horizontally, continues over the poles. +pub fn sample_equi(img: &Image, x: f64, y: f64) -> Px { + let (w, h) = (img.width as i64, img.height as i64); + if w == 0 || h == 0 { + return [0.0; 4]; + } + let fx = x - 0.5; + let fy = y - 0.5; + let (x0, y0) = (fx.floor(), fy.floor()); + let (tx, ty) = ((fx - x0) as f32, (fy - y0) as f32); + let at = |xi: i64, yi: i64| -> Px { + let (mut xi, mut yi) = (xi, yi); + if yi < 0 { + yi = -1 - yi; + xi += w / 2; + } else if yi >= h { + yi = 2 * h - 1 - yi; + xi += w / 2; + } + img.data[(yi.clamp(0, h - 1) * w + xi.rem_euclid(w)) as usize] + }; + let (x0, y0) = (x0 as i64, y0 as i64); + let (a, b, c, d) = (at(x0, y0), at(x0 + 1, y0), at(x0, y0 + 1), at(x0 + 1, y0 + 1)); + let mut o = [0.0f32; 4]; + for k in 0..4 { + let top = a[k] + (b[k] - a[k]) * tx; + let bot = c[k] + (d[k] - c[k]) * tx; + o[k] = top + (bot - top) * ty; + } + o +} + +/// Rotation matrix (row-major) for tilt (about X), pan (about Y) and roll (about Z), degrees: +/// R = Ry(pan) · Rx(tilt) · Rz(roll). +pub fn rotation(tilt: f64, pan: f64, roll: f64) -> [[f64; 3]; 3] { + let (a, b, c) = (tilt.to_radians(), pan.to_radians(), roll.to_radians()); + let rx = [[1.0, 0.0, 0.0], [0.0, a.cos(), -a.sin()], [0.0, a.sin(), a.cos()]]; + let ry = [[b.cos(), 0.0, b.sin()], [0.0, 1.0, 0.0], [-b.sin(), 0.0, b.cos()]]; + let rz = [[c.cos(), -c.sin(), 0.0], [c.sin(), c.cos(), 0.0], [0.0, 0.0, 1.0]]; + mm(&ry, &mm(&rx, &rz)) +} + +fn mm(a: &[[f64; 3]; 3], b: &[[f64; 3]; 3]) -> [[f64; 3]; 3] { + let mut o = [[0.0; 3]; 3]; + for i in 0..3 { + for j in 0..3 { + o[i][j] = (0..3).map(|k| a[i][k] * b[k][j]).sum(); + } + } + o +} + +#[inline] +fn mv(m: &[[f64; 3]; 3], v: V3) -> V3 { + [dot(m[0], v), dot(m[1], v), dot(m[2], v)] +} + +#[inline] +fn mtv(m: &[[f64; 3]; 3], v: V3) -> V3 { + [m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2], m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2], m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2]] +} + +// ---------------------------------------------------------------- projections (VR Converter) + +pub const PROJECTIONS: [&str; 6] = ["Equirectangular", "Cube-map 4:3", "Cube-map 3:2", "Cube-map 6:1", "Sphere Map", "2D Rectilinear"]; + +/// Cube faces: (forward, right, up). +const FACES: [(V3, V3, V3); 6] = [ + ([1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]), // +X right + ([-1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0]), // -X left + ([0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, -1.0]), // +Y top + ([0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]), // -Y bottom + ([0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]), // +Z front + ([0.0, 0.0, -1.0], [-1.0, 0.0, 0.0], [0.0, 1.0, 0.0]), // -Z back +]; + +/// Grid size and the (column, row) of each face (+X, −X, +Y, −Y, +Z, −Z) for a cube layout. +fn cube_grid(kind: u32) -> ((u32, u32), [(u32, u32); 6]) { + match kind { + // Horizontal cross: top over the front; left, front, right, back across the middle. + 1 => ((4, 3), [(2, 1), (0, 1), (1, 0), (1, 2), (1, 1), (3, 1)]), + // 3 × 2: right, left, top / bottom, front, back. + 2 => ((3, 2), [(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1)]), + // 6 × 1 strip in face order. + _ => ((6, 1), [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0)]), + } +} + +/// A projection: frame position (pixels) ↔ view direction. +#[derive(Clone, Copy, Debug)] +pub struct Proj { + pub kind: u32, + /// Horizontal field of view in degrees (sphere map and 2D only). + pub fov: f64, +} + +impl Proj { + /// Direction seen at frame position (x, y) of a `w`×`h` frame (None outside the image). + pub fn dir(&self, x: f64, y: f64, w: f64, h: f64) -> Option { + match self.kind { + 0 => Some(equi_dir(x, y, w, h)), + 1..=3 => { + let ((gw, gh), cells) = cube_grid(self.kind); + let (cw, ch) = (w / gw as f64, h / gh as f64); + let (cx, cy) = ((x / cw).floor(), (y / ch).floor()); + let f = cells.iter().position(|&(c, r)| c as f64 == cx && r as f64 == cy)?; + let a = (x - cx * cw) / cw * 2.0 - 1.0; + let b = (y - cy * ch) / ch * 2.0 - 1.0; + let (fw, r, u) = FACES[f]; + Some(norm([fw[0] + a * r[0] - b * u[0], fw[1] + a * r[1] - b * u[1], fw[2] + a * r[2] - b * u[2]])) + } + 4 => { + // Angular fisheye centred on +Z; radius ∝ angle, `fov` across the image width. + let half = (self.fov.clamp(1.0, 360.0) / 2.0).to_radians(); + let s = w.min(h) / 2.0; + let (u, v) = ((x - w / 2.0) / s, (h / 2.0 - y) / s); + let r = (u * u + v * v).sqrt(); + if r > 1.0 { + return None; + } + let th = r * half; + let (cu, cv) = if r > 1e-12 { (u / r, v / r) } else { (0.0, 0.0) }; + Some([th.sin() * cu, th.sin() * cv, th.cos()]) + } + _ => { + let t = (self.fov.clamp(1.0, 179.0) / 2.0).to_radians().tan(); + let u = (x / w * 2.0 - 1.0) * t; + let v = (1.0 - y / h * 2.0) * t * h / w; + Some(norm([u, v, 1.0])) + } + } + } + + /// Frame position (pixels) of direction `d`, and for cube maps the face rectangle to clamp + /// samples into. None when the direction is not visible. + pub fn pos(&self, d: V3, w: f64, h: f64) -> Option<(f64, f64, Option<[f64; 4]>)> { + match self.kind { + 0 => { + let (x, y) = equi_px(d, w, h); + Some((x, y, None)) + } + 1..=3 => { + let f = (0..6).max_by(|&i, &j| dot(d, FACES[i].0).total_cmp(&dot(d, FACES[j].0)))?; + let (fw, r, u) = FACES[f]; + let k = dot(d, fw); + let (a, b) = (dot(d, r) / k, -dot(d, u) / k); + let ((gw, gh), cells) = cube_grid(self.kind); + let (cw, ch) = (w / gw as f64, h / gh as f64); + let (c, rr) = cells[f]; + let (x0, y0) = (c as f64 * cw, rr as f64 * ch); + Some((x0 + (a + 1.0) * 0.5 * cw, y0 + (b + 1.0) * 0.5 * ch, Some([x0, y0, x0 + cw, y0 + ch]))) + } + 4 => { + let half = (self.fov.clamp(1.0, 360.0) / 2.0).to_radians(); + let th = d[2].clamp(-1.0, 1.0).acos(); + if th > half + 1e-9 { + return None; + } + let s = w.min(h) / 2.0; + let r = th / half; + let l = (d[0] * d[0] + d[1] * d[1]).sqrt(); + let (cu, cv) = if l > 1e-12 { (d[0] / l, d[1] / l) } else { (0.0, 0.0) }; + Some((w / 2.0 + cu * r * s, h / 2.0 - cv * r * s, None)) + } + _ => { + if d[2] <= 1e-9 { + return None; + } + let t = (self.fov.clamp(1.0, 179.0) / 2.0).to_radians().tan(); + let u = d[0] / d[2] / t; + let v = d[1] / d[2] / (t * h / w); + if u.abs() > 1.0 || v.abs() > 1.0 { + return None; + } + Some(((u + 1.0) * 0.5 * w, (1.0 - v) * 0.5 * h, None)) + } + } + } + + /// Sample image `img` (in this projection) in direction `d`. + pub fn sample(&self, img: &Image, d: V3) -> Px { + let (w, h) = (img.width as f64, img.height as f64); + match self.pos(d, w, h) { + None => [0.0; 4], + Some((x, y, None)) => { + if self.kind == 0 { + sample_equi(img, x, y) + } else { + img.sample_bilinear_clamped(x, y) + } + } + Some((x, y, Some(r))) => { + // Stay inside the face so neighbouring faces don't bleed in. + let x = x.clamp(r[0] + 0.5, r[2] - 0.5); + let y = y.clamp(r[1] + 0.5, r[3] - 0.5); + img.sample_bilinear_clamped(x, y) + } + } + } +} + +/// Re-project `src` (in `from`) into a `w`×`h` frame in `to`, rotating the view by `rot` +/// (output direction → source direction = rotᵀ · d). +pub fn convert(src: &Image, from: Proj, to: Proj, w: u32, h: u32, rot: Option<&[[f64; 3]; 3]>) -> Image { + let mut out = Image::new(w, h); + let (fw, fh) = (w as f64, h as f64); + out.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + *px = match to.dir(x as f64 + 0.5, y as f64 + 0.5, fw, fh) { + Some(d) => { + let d = match rot { + Some(m) => mtv(m, d), + None => d, + }; + from.sample(src, d) + } + None => [0.0; 4], + }; + } + }); + out +} + +// ---------------------------------------------------------------- seam-aware filters + +/// Horizontal box mean with wrap-around, radius `r` (pixels, ≤ w/2) on one row. +fn box_row_wrap(src: &[Px], dst: &mut [Px], r: usize) { + let n = src.len(); + if n == 0 { + return; + } + if r == 0 { + dst.copy_from_slice(src); + return; + } + let r = r.min((n - 1) / 2); + if r == 0 { + dst.copy_from_slice(src); + return; + } + let k = (2 * r + 1) as f64; + let mut acc = [0.0f64; 4]; + for i in 0..=2 * r { + let p = src[(i + n - r) % n]; + for c in 0..4 { + acc[c] += p[c] as f64; + } + } + for x in 0..n { + for c in 0..4 { + dst[x][c] = (acc[c] / k) as f32; + } + let add = src[(x + r + 1) % n]; + let sub = src[(x + n - r) % n]; + for c in 0..4 { + acc[c] += add[c] as f64 - sub[c] as f64; + } + } +} + +/// Vertical box mean with pole continuation, radius `r`, over all columns. +fn box_cols_pole(img: &Image, r: usize) -> Image { + let (w, h) = (img.width as usize, img.height as usize); + if r == 0 || h == 0 { + return img.clone(); + } + let at = |x: usize, y: i64| -> Px { + let (mut x, mut y) = (x, y); + if y < 0 { + y = -1 - y; + x = (x + w / 2) % w; + } else if y >= h as i64 { + y = 2 * h as i64 - 1 - y; + x = (x + w / 2) % w; + } + img.data[y.clamp(0, h as i64 - 1) as usize * w + x] + }; + let k = (2 * r + 1) as f64; + let cols: Vec> = (0..w) + .into_par_iter() + .map(|x| { + let mut out = vec![[0.0f32; 4]; h]; + let mut acc = [0.0f64; 4]; + for j in -(r as i64)..=(r as i64) { + let p = at(x, j); + for c in 0..4 { + acc[c] += p[c] as f64; + } + } + for y in 0..h { + for c in 0..4 { + out[y][c] = (acc[c] / k) as f32; + } + let add = at(x, y as i64 + r as i64 + 1); + let sub = at(x, y as i64 - r as i64); + for c in 0..4 { + acc[c] += add[c] as f64 - sub[c] as f64; + } + } + out + }) + .collect(); + let mut out = Image::new(img.width, img.height); + out.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + *px = cols[x][y]; + } + }); + out +} + +/// Gaussian-like blur of an equirectangular image with standard deviation `sigma` pixels at +/// the equator (3 box passes per axis), wrapping at the seam and continuing over the poles. +pub fn sphere_blur(img: &Image, sigma: f64) -> Image { + if sigma < 0.1 || img.is_empty() { + return img.clone(); + } + let (w, h) = (img.width as usize, img.height as usize); + let radii = crate::util::box_radii(sigma, 3); + let mut cur = img.clone(); + let mut tmp = Image::new(img.width, img.height); + for &r in &radii { + tmp.rows_mut().for_each(|(y, row)| { + let lat = (0.5 - (y as f64 + 0.5) / h as f64) * PI; + let rr = ((r as f64) / lat.cos().max(1e-3)).round().min((w / 2) as f64) as usize; + box_row_wrap(&cur.data[y * w..(y + 1) * w], row, rr); + }); + std::mem::swap(&mut cur, &mut tmp); + } + for &r in &radii { + cur = box_cols_pole(&cur, r); + } + cur +} + +// ---------------------------------------------------------------- effects + +fn layout(ctx: &EffectCtx) -> u32 { + ctx.params.e("frameLayout") +} + +fn vr_blur(ctx: &EffectCtx, mut b: Buf) -> Buf { + let s = ctx.params.f("blurriness").max(0.0) * b.scale * 0.5; + if s < 0.1 { + return b; + } + b.img = per_eye(&b.img, layout(ctx), |e, _| sphere_blur(e, s)); + b +} + +fn vr_sharpen(ctx: &EffectCtx, mut b: Buf) -> Buf { + let amt = ctx.params.f("sharpenAmount") as f32 / 100.0; + if amt == 0.0 { + return b; + } + let s = (1.5 * b.scale).max(0.5); + b.img = per_eye(&b.img, layout(ctx), |e, _| { + let bl = sphere_blur(e, s); + let mut o = e.clone(); + o.data.par_iter_mut().zip(bl.data.par_iter()).for_each(|(p, q)| { + for c in 0..3 { + p[c] = (p[c] + (p[c] - q[c]) * amt).clamp(0.0, p[3].max(0.0) * 4.0); + } + }); + o + }); + b +} + +fn vr_denoise(ctx: &EffectCtx, mut b: Buf) -> Buf { + let level = ctx.params.f("noiseLevel").max(0.0) / 100.0; + if level <= 0.0 { + return b; + } + let r = ((1.0 + level * 4.0) * b.scale).max(1.0) as usize; + let eps = (level * level * 0.02) as f32; + let detail = (ctx.params.f("detail") / 100.0).clamp(0.0, 1.0) as f32; + b.img = per_eye(&b.img, layout(ctx), |e, _| { + // Guided filter (self-guided, per channel) on a frame padded with wrapped / pole-continued + // pixels so the seam and poles are filtered like any other place. + let pad = r * 2 + 1; + let padded = wrap_pad(e, pad); + let guide = crate::util::Plane::luma(&padded); + let ch = crate::util::split(&padded); + let f: Vec = ch.par_iter().map(|c| crate::util::guided_filter(&guide, c, r, eps.max(1e-6))).collect(); + let joined = crate::util::join(&[f[0].clone(), f[1].clone(), f[2].clone(), f[3].clone()]); + let mut o = joined.crop(pad as i64, pad as i64, e.width, e.height); + o.data.par_iter_mut().zip(e.data.par_iter()).for_each(|(p, s)| { + for c in 0..4 { + p[c] = p[c] + (s[c] - p[c]) * detail; + } + p[3] = p[3].clamp(0.0, 1.0); + for c in 0..3 { + p[c] = p[c].max(0.0); + } + }); + o + }); + b +} + +/// Pad an equirectangular image by `pad` pixels: wrapped columns, pole-continued rows. +pub fn wrap_pad(img: &Image, pad: usize) -> Image { + let (w, h) = (img.width as i64, img.height as i64); + let p = pad as i64; + let mut out = Image::new((w + 2 * p) as u32, (h + 2 * p) as u32); + out.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let (mut sx, mut sy) = (x as i64 - p, y as i64 - p); + if sy < 0 { + sy = -1 - sy; + sx += w / 2; + } else if sy >= h { + sy = 2 * h - 1 - sy; + sx += w / 2; + } + *px = img.data[(sy.clamp(0, h - 1) * w + sx.rem_euclid(w.max(1))) as usize]; + } + }); + out +} + +fn vr_glow(ctx: &EffectCtx, mut b: Buf) -> Buf { + let th = ctx.params.f("luminanceThreshold") as f32 / 100.0; + let radius = ctx.params.f("glowRadius").max(0.0) * b.scale; + let bright = ctx.params.f("glowBrightness") as f32 / 100.0; + let sat = ctx.params.f("glowSaturation") as f32 / 100.0; + let tint = ctx.params.b("useTintColor"); + let tc = ctx.params.color("tintColor"); + if bright <= 0.0 { + return b; + } + b.img = per_eye(&b.img, layout(ctx), |e, _| { + let mut hi = e.clone(); + hi.data.par_iter_mut().for_each(|p| { + let (c, a) = unpremul(*p); + let l = luminance(c[0], c[1], c[2]); + let k = if l > th { ((l - th) / (1.0 - th).max(1e-3)).min(4.0) } else { 0.0 }; + let mut c = c.map(|v| v * k); + let lc = luminance(c[0], c[1], c[2]); + c = c.map(|v| lc + (v - lc) * sat); + if tint { + c = [lc * tc[0], lc * tc[1], lc * tc[2]]; + } + *p = [c[0] * a, c[1] * a, c[2] * a, a * k.min(1.0)]; + }); + let g = sphere_blur(&hi, (radius * 0.5).max(0.5)); + let mut o = e.clone(); + o.data.par_iter_mut().zip(g.data.par_iter()).for_each(|(p, q)| { + for c in 0..3 { + p[c] += q[c] * bright; + } + p[3] = (p[3] + q[3] * bright).min(1.0); + }); + o + }); + b +} + +fn rot_params(ctx: &EffectCtx) -> [[f64; 3]; 3] { + rotation(ctx.params.f("tilt"), ctx.params.f("pan"), ctx.params.f("roll")) +} + +fn vr_rotate(ctx: &EffectCtx, mut b: Buf) -> Buf { + let (t, pn, r) = (ctx.params.f("tilt"), ctx.params.f("pan"), ctx.params.f("roll")); + if t == 0.0 && pn == 0.0 && r == 0.0 { + return b; + } + let m = rot_params(ctx); + let eq = Proj { kind: 0, fov: 360.0 }; + b.img = per_eye(&b.img, layout(ctx), |e, _| convert(e, eq, eq, e.width, e.height, Some(&m))); + b +} + +fn vr_converter(ctx: &EffectCtx, mut b: Buf) -> Buf { + let from = Proj { kind: ctx.params.e("sourceProjection"), fov: ctx.params.f("sourceHorizontalFov") }; + let to = Proj { kind: ctx.params.e("targetProjection"), fov: ctx.params.f("targetHorizontalFov") }; + let (t, pn, r) = (ctx.params.f("tilt"), ctx.params.f("pan"), ctx.params.f("roll")); + if from.kind == to.kind && (from.kind <= 3 || from.fov == to.fov) && t == 0.0 && pn == 0.0 && r == 0.0 { + return b; + } + let m = rot_params(ctx); + b.img = per_eye(&b.img, layout(ctx), |e, _| convert(e, from, to, e.width, e.height, Some(&m))); + b +} + +fn vr_sphere_to_plane(ctx: &EffectCtx, mut b: Buf) -> Buf { + let fov = ctx.params.f("horizontalFov"); + let m = rot_params(ctx); + let eq = Proj { kind: 0, fov: 360.0 }; + let flat = Proj { kind: 5, fov }; + b.img = per_eye(&b.img, layout(ctx), |e, _| convert(e, eq, flat, e.width, e.height, Some(&m))); + b +} + +fn vr_plane_to_sphere(ctx: &EffectCtx, mut b: Buf) -> Buf { + // The layer is a flat picture placed on the sphere: horizontal FOV = 90° × scale. + let fov = (90.0 * ctx.params.f("scale") / 100.0).clamp(1.0, 179.0); + let m = rot_params(ctx); + let feather = (ctx.params.f("feather") / 100.0).clamp(0.0, 1.0); + let flat = Proj { kind: 5, fov }; + let eq = Proj { kind: 0, fov: 360.0 }; + let src = b.img.clone(); + let mut out = convert(&src, flat, eq, src.width, src.height, Some(&m)); + if feather > 0.0 { + let (w, h) = (src.width as f64, src.height as f64); + let t = (fov / 2.0).to_radians().tan(); + out.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let d = mtv(&m, equi_dir(x as f64 + 0.5, y as f64 + 0.5, w, h)); + if d[2] <= 0.0 { + continue; + } + let u = (d[0] / d[2] / t).abs(); + let v = (d[1] / d[2] / (t * h / w)).abs(); + let edge = 1.0 - u.max(v); + let k = (edge / feather).clamp(0.0, 1.0) as f32; + for c in px.iter_mut() { + *c *= k; + } + } + }); + } + b.img = out; + b +} + +/// Angle (radians) between two directions. +#[inline] +fn angle(a: V3, b: V3) -> f64 { + dot(a, b).clamp(-1.0, 1.0).acos() +} + +fn vr_chromatic(ctx: &EffectCtx, mut b: Buf) -> Buf { + let k = [ctx.params.f("aberrationRed"), ctx.params.f("aberrationGreen"), ctx.params.f("aberrationBlue")].map(|v| v / 100.0 * 0.1); + if k.iter().all(|v| *v == 0.0) { + return b; + } + let center = mv(&rotation(ctx.params.f("centerTilt"), ctx.params.f("centerPan"), 0.0), [0.0, 0.0, 1.0]); + let falloff = (ctx.params.f("falloff") / 100.0).clamp(0.0, 1.0); + b.img = per_eye(&b.img, layout(ctx), |e, _| { + let (w, h) = (e.width as f64, e.height as f64); + let mut o = Image::new(e.width, e.height); + o.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let d = equi_dir(x as f64 + 0.5, y as f64 + 0.5, w, h); + let th = angle(d, center); + // Axis perpendicular to centre and d: rotate d about it toward/away from centre. + let ax = [center[1] * d[2] - center[2] * d[1], center[2] * d[0] - center[0] * d[2], center[0] * d[1] - center[1] * d[0]]; + let al = dot(ax, ax).sqrt(); + let weight = (th / PI).powf(1.0 + falloff * 3.0); + let mut out = [0.0f32; 4]; + for c in 0..3 { + let s = if al < 1e-9 { + e.data[(y * e.width as usize) + x] + } else { + let new_th = th * (1.0 - k[c] * weight); + // Direction at angle new_th from the centre in the plane of (centre, d). + let perp = norm([d[0] - center[0] * th.cos(), d[1] - center[1] * th.cos(), d[2] - center[2] * th.cos()]); + let nd = [ + center[0] * new_th.cos() + perp[0] * new_th.sin(), + center[1] * new_th.cos() + perp[1] * new_th.sin(), + center[2] * new_th.cos() + perp[2] * new_th.sin(), + ]; + let (sx, sy) = equi_px(nd, w, h); + sample_equi(e, sx, sy) + }; + out[c] = s[c]; + out[3] = out[3].max(s[3]); + } + *px = out; + } + }); + o + }); + b +} + +const BLEND_OPTS: [&str; 6] = ["None", "Normal", "Add", "Multiply", "Screen", "Overlay"]; + +fn blend_mode(i: u32) -> Option { + match i { + 0 => None, + 2 => Some(BlendMode::Add), + 3 => Some(BlendMode::Multiply), + 4 => Some(BlendMode::Screen), + 5 => Some(BlendMode::Overlay), + _ => Some(BlendMode::Normal), + } +} + +/// Composite a generated eye image over the original with a blend mode and opacity (mode None +/// replaces the layer). +fn composite(orig: &Image, layer: &Image, mode: u32, opacity: f32) -> Image { + let mut o = orig.clone(); + o.data.par_iter_mut().zip(layer.data.par_iter()).for_each(|(d, s)| { + let s = [s[0] * opacity, s[1] * opacity, s[2] * opacity, s[3] * opacity]; + *d = match blend_mode(mode) { + None => s, + Some(m) => { + let mut r = blend_pixel(m, *d, s, 0.5); + // Keep the layer's own coverage: generated pixels show only where it is opaque. + let a = d[3]; + if a < 1.0 { + let k = a / r[3].max(1e-6); + if k < 1.0 { + r = r.map(|v| v * k); + } + } + r + } + }; + }); + o +} + +fn vr_gradients(ctx: &EffectCtx, mut b: Buf) -> Buf { + let pr = ctx.params; + let blend = pr.f("blend").max(0.1); + let opacity = (pr.f("opacity") / 100.0) as f32; + let mode = pr.e("blendingMode"); + let (lw, lh) = (ctx.layer_size[0].max(1.0), ctx.layer_size[1].max(1.0)); + let pts: Vec<(V3, [f32; 4])> = (1..=5) + .filter(|i| pr.b(&format!("enablePoint{i}"))) + .map(|i| { + let p = pr.v2(&format!("point{i}")); + (equi_dir(p[0], p[1], lw, lh), pr.color(&format!("color{i}"))) + }) + .collect(); + if pts.is_empty() { + return b; + } + b.img = per_eye(&b.img, layout(ctx), |e, _| { + let (w, h) = (e.width as f64, e.height as f64); + let mut g = Image::new(e.width, e.height); + g.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let d = equi_dir(x as f64 + 0.5, y as f64 + 0.5, w, h); + // Inverse-distance weighting on great-circle distance. + let mut acc = [0.0f64; 3]; + let mut ws = 0.0; + let mut exact = None; + for (pd, c) in &pts { + let a = angle(d, *pd); + if a < 1e-6 { + exact = Some(*c); + break; + } + let wgt = 1.0 / a.powf(blend); + for k in 0..3 { + acc[k] += c[k] as f64 * wgt; + } + ws += wgt; + } + let c = exact.map(|c| [c[0], c[1], c[2]]).unwrap_or([(acc[0] / ws) as f32, (acc[1] / ws) as f32, (acc[2] / ws) as f32]); + *px = [c[0], c[1], c[2], 1.0]; + } + }); + composite(e, &g, mode, opacity) + }); + b +} + +fn vr_fractal(ctx: &EffectCtx, mut b: Buf) -> Buf { + let pr = ctx.params; + let kind = pr.e("fractalType"); + let invert = pr.b("invert"); + let contrast = pr.f("contrast") as f32 / 100.0; + let brightness = pr.f("brightness") as f32 / 100.0; + let scale = (pr.f("scale") / 100.0).max(0.01) as f32; + let octaves = pr.f("complexity").clamp(1.0, 20.0); + let evo = (pr.f("evolution") / 360.0) as f32; + let opacity = (pr.f("opacity") / 100.0) as f32; + let mode = pr.e("blendingMode"); + let seed = pr.f("randomSeed") as u32; + let base = 4.0 / scale; + b.img = per_eye(&b.img, layout(ctx), |e, _| { + let (w, h) = (e.width as f64, e.height as f64); + let mut g = Image::new(e.width, e.height); + g.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let d = equi_dir(x as f64 + 0.5, y as f64 + 0.5, w, h); + let mut v = 0.0f32; + let mut amp = 1.0f32; + let mut freq = base; + let mut norm = 0.0f32; + let n_oct = octaves.ceil() as usize; + for o in 0..n_oct { + let wgt = if o + 1 == n_oct && octaves.fract() > 0.0 { octaves.fract() as f32 } else { 1.0 }; + let q = [d[0] as f32 * freq + evo * 0.37, d[1] as f32 * freq + evo * 0.71, d[2] as f32 * freq + evo * 0.59]; + let n = value_noise(q[0] + o as f32 * 17.3, q[1], q[2], seed.wrapping_add(o as u32)); + let n = match kind { + 1 => (n * 2.0 - 1.0).abs(), + 2 => 1.0 - (n * 2.0 - 1.0).abs(), + _ => n, + }; + v += n * amp * wgt; + norm += amp * wgt; + amp *= 0.5; + freq *= 2.0; + } + let mut v = v / norm.max(1e-6); + v = (v - 0.5) * contrast + 0.5 + brightness; + if invert { + v = 1.0 - v; + } + let v = v.clamp(0.0, 1.0); + *px = [v, v, v, 1.0]; + } + }); + composite(e, &g, mode, opacity) + }); + b +} + +fn vr_glitch(ctx: &EffectCtx, mut b: Buf) -> Buf { + let pr = ctx.params; + let amp = (pr.f("masterAmplitude") / 100.0).max(0.0); + if amp <= 0.0 { + return b; + } + let rate = pr.f("distortionRate").max(0.0); + let geo = pr.f("geometricDistortion") / 100.0 * amp; + let complexity = pr.f("distortionComplexity").clamp(1.0, 100.0); + let rgb = pr.f("colorDistortion") / 100.0 * amp; + let scan = (pr.f("scanlines") / 100.0 * amp) as f32; + let seed = (pr.f("randomSeed") as u32) ^ ctx.seed; + // The glitch pattern changes `rate` times a second (held between changes). + let slot = if rate > 0.0 { (ctx.time * rate).floor() as i64 } else { 0 } as u32; + b.img = per_eye(&b.img, layout(ctx), |e, eye| { + let (w, h) = (e.width as f64, e.height as f64); + let bands = complexity.round() as u32; + let mut o = Image::new(e.width, e.height); + o.rows_mut().for_each(|(y, row)| { + let band = ((y as f64 / h) * bands as f64) as u32; + let r0 = hash1(band, slot, seed ^ (eye as u32 * 977)); + let on = r0 < 0.35; + let shift = if on { (hash1(band, slot + 1, seed) as f64 * 2.0 - 1.0) * geo * w * 0.15 } else { 0.0 }; + let split = if on { rgb * w * 0.01 * (1.0 + hash1(band, slot + 2, seed) as f64) } else { 0.0 }; + let dark = if y % 2 == 0 { 1.0 - scan * 0.5 } else { 1.0 }; + for (x, px) in row.iter_mut().enumerate() { + let sx = x as f64 + 0.5 - shift; + let sy = y as f64 + 0.5; + // Horizontal shifts wrap around the seam (the frame is a full turn). + let g = sample_equi(e, sx, sy); + let r = sample_equi(e, sx - split, sy); + let bb = sample_equi(e, sx + split, sy); + *px = [r[0] * dark, g[1] * dark, bb[2] * dark, g[3].max(r[3]).max(bb[3])]; + } + }); + o + }); + b +} + +fn ang(id: &'static str, name: &'static str) -> crate::ParamSpec { + p(id, name, num(0.0), ParamUi::Angle) +} + +pub fn specs() -> Vec { + let pct = |d: f64| (num(d), slider(0.0, 100.0, 0.0, 100.0, 1)); + let mut grad = vec![layout_param()]; + let defaults = [ + ([0.25, 0.5], [1.0, 0.0, 0.0]), + ([0.5, 0.25], [1.0, 1.0, 0.0]), + ([0.75, 0.5], [0.0, 0.0, 1.0]), + ([0.5, 0.75], [0.0, 1.0, 0.0]), + ([0.0, 0.5], [1.0, 0.0, 1.0]), + ]; + const IDS: [[&str; 6]; 5] = [ + ["enablePoint1", "Enable Point 1", "point1", "Point 1", "color1", "Color 1"], + ["enablePoint2", "Enable Point 2", "point2", "Point 2", "color2", "Color 2"], + ["enablePoint3", "Enable Point 3", "point3", "Point 3", "color3", "Color 3"], + ["enablePoint4", "Enable Point 4", "point4", "Point 4", "color4", "Color 4"], + ["enablePoint5", "Enable Point 5", "point5", "Point 5", "color5", "Color 5"], + ]; + for (i, (ids, (pt, c))) in IDS.iter().zip(defaults).enumerate() { + grad.push(p(ids[0], ids[1], Value::Bool(i < 4), ParamUi::Checkbox)); + grad.push(p(ids[2], ids[3], Value::Vec2(pt), ParamUi::Point)); + grad.push(p(ids[4], ids[5], col(c[0], c[1], c[2]), ParamUi::Color)); + } + grad.push(p("blend", "Blend", num(2.0), slider(0.1, 10.0, 0.5, 6.0, 2))); + let (v, u) = pct(100.0); + grad.push(p("opacity", "Opacity", v, u)); + grad.push(p("blendingMode", "Blending Mode", Value::Enum(0), popup(&BLEND_OPTS))); + vec![ + spec("ec.vr.blur", "VR Blur", vec![layout_param(), p("blurriness", "Blurriness", num(10.0), slider(0.0, 1000.0, 0.0, 100.0, 1))], vr_blur), + spec( + "ec.vr.chromaticaberrations", + "VR Chromatic Aberrations", + vec![ + layout_param(), + ang("centerTilt", "Center Tilt"), + ang("centerPan", "Center Pan"), + p("aberrationRed", "Aberration (Red)", num(10.0), slider(-100.0, 100.0, -100.0, 100.0, 1)), + p("aberrationGreen", "Aberration (Green)", num(0.0), slider(-100.0, 100.0, -100.0, 100.0, 1)), + p("aberrationBlue", "Aberration (Blue)", num(-10.0), slider(-100.0, 100.0, -100.0, 100.0, 1)), + p("falloff", "Falloff", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + ], + vr_chromatic, + ), + spec("ec.vr.colorgradients", "VR Color Gradients", grad, vr_gradients), + spec( + "ec.vr.converter", + "VR Converter", + vec![ + layout_param(), + p("sourceProjection", "Source Projection", Value::Enum(0), popup(&PROJECTIONS)), + p("sourceHorizontalFov", "Source Horizontal FOV", num(90.0), slider(1.0, 360.0, 1.0, 360.0, 1)), + p("targetProjection", "Target Projection", Value::Enum(2), popup(&PROJECTIONS)), + p("targetHorizontalFov", "Target Horizontal FOV", num(90.0), slider(1.0, 360.0, 1.0, 360.0, 1)), + ang("tilt", "Tilt (X Axis)"), + ang("pan", "Pan (Y Axis)"), + ang("roll", "Roll (Z Axis)"), + ], + vr_converter, + ), + spec( + "ec.vr.denoise", + "VR De-Noise", + vec![ + layout_param(), + p("noiseLevel", "Noise Level", num(20.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("detail", "Detail", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + ], + vr_denoise, + ), + spec( + "ec.vr.digitalglitch", + "VR Digital Glitch", + vec![ + layout_param(), + p("masterAmplitude", "Master Amplitude", num(100.0), slider(0.0, 500.0, 0.0, 200.0, 1)), + p("distortionRate", "Distortion Rate", num(5.0), slider(0.0, 60.0, 0.0, 30.0, 2)), + p("distortionComplexity", "Distortion Complexity", num(16.0), slider(1.0, 100.0, 1.0, 64.0, 0)), + p("geometricDistortion", "Geometric Distortion", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("colorDistortion", "Color Distortion", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("scanlines", "Scanlines", num(20.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("randomSeed", "Random Seed", num(0.0), slider(0.0, 10000.0, 0.0, 1000.0, 0)), + ], + vr_glitch, + ), + spec( + "ec.vr.fractalnoise", + "VR Fractal Noise", + vec![ + layout_param(), + p("fractalType", "Fractal Type", Value::Enum(0), popup(&["Basic", "Turbulent Smooth", "Turbulent Sharp"])), + p("invert", "Invert", Value::Bool(false), ParamUi::Checkbox), + p("contrast", "Contrast", num(100.0), slider(0.0, 10000.0, 0.0, 400.0, 1)), + p("brightness", "Brightness", num(0.0), slider(-10000.0, 10000.0, -200.0, 200.0, 1)), + p("scale", "Scale", num(100.0), slider(1.0, 10000.0, 10.0, 600.0, 1)), + p("complexity", "Complexity", num(6.0), slider(1.0, 20.0, 1.0, 20.0, 1)), + ang("evolution", "Evolution"), + p("randomSeed", "Random Seed", num(0.0), slider(0.0, 10000.0, 0.0, 1000.0, 0)), + p("opacity", "Opacity", num(100.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("blendingMode", "Blending Mode", Value::Enum(0), popup(&BLEND_OPTS)), + ], + vr_fractal, + ), + spec( + "ec.vr.glow", + "VR Glow", + vec![ + layout_param(), + p("luminanceThreshold", "Luminance Threshold", num(60.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("glowRadius", "Glow Radius", num(20.0), slider(0.0, 1000.0, 0.0, 200.0, 1)), + p("glowBrightness", "Glow Brightness", num(100.0), slider(0.0, 1000.0, 0.0, 400.0, 1)), + p("glowSaturation", "Glow Saturation", num(100.0), slider(0.0, 400.0, 0.0, 200.0, 1)), + p("useTintColor", "Use Tint Color", Value::Bool(false), ParamUi::Checkbox), + p("tintColor", "Tint Color", col(1.0, 0.8, 0.5), ParamUi::Color), + ], + vr_glow, + ), + spec( + "ec.vr.planetosphere", + "VR Plane to Sphere", + vec![ + layout_param(), + p("scale", "Scale", num(100.0), slider(1.0, 198.0, 1.0, 198.0, 1)), + ang("tilt", "Tilt (X Axis)"), + ang("pan", "Pan (Y Axis)"), + ang("roll", "Roll (Z Axis)"), + p("feather", "Feather", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + ], + vr_plane_to_sphere, + ), + spec( + "ec.vr.rotatesphere", + "VR Rotate Sphere", + vec![layout_param(), ang("tilt", "Tilt (X Axis)"), ang("pan", "Pan (Y Axis)"), ang("roll", "Roll (Z Axis)")], + vr_rotate, + ), + spec( + "ec.vr.sharpen", + "VR Sharpen", + vec![layout_param(), p("sharpenAmount", "Sharpen Amount", num(20.0), slider(0.0, 500.0, 0.0, 100.0, 1))], + vr_sharpen, + ), + spec( + "ec.vr.spheretoplane", + "VR Sphere To Plane", + vec![ + layout_param(), + p("horizontalFov", "Horizontal FOV", num(90.0), slider(1.0, 179.0, 10.0, 170.0, 1)), + ang("tilt", "Tilt (X Axis)"), + ang("pan", "Pan (Y Axis)"), + ang("roll", "Roll (Z Axis)"), + ], + vr_sphere_to_plane, + ), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, run_fx}; + + /// A smooth function of the view direction rendered as an equirectangular image. + fn sphere_img(w: u32, h: u32) -> Image { + let mut img = Image::new(w, h); + for y in 0..h { + for x in 0..w { + let d = equi_dir(x as f64 + 0.5, y as f64 + 0.5, w as f64, h as f64); + img.set(x, y, [(0.5 + 0.4 * d[0]) as f32, (0.5 + 0.4 * d[1]) as f32, (0.5 + 0.4 * d[2]) as f32, 1.0]); + } + } + img + } + + #[test] + fn projections_are_inverse() { + for kind in 0..6u32 { + let pr = Proj { kind, fov: if kind == 4 { 360.0 } else { 100.0 } }; + let (w, h) = (240.0, 120.0); + for (x, y) in [(10.5, 20.5), (100.5, 60.5), (230.5, 110.5), (130.5, 33.5)] { + let Some(d) = pr.dir(x, y, w, h) else { continue }; + let (px, py, _) = pr.pos(d, w, h).unwrap(); + assert!((px - x).abs() < 1e-6 && (py - y).abs() < 1e-6, "kind {kind}: ({x},{y}) → ({px},{py})"); + } + } + } + + #[test] + fn converter_round_trips_equirect_through_cube_maps() { + let img = sphere_img(128, 64); + for target in [1u32, 2, 3] { + let cube = run_fx("ec.vr.converter", &[("targetProjection", Value::Enum(target))], img.clone(), 0.0, EffectEnv::default()); + let back = run_fx( + "ec.vr.converter", + &[("sourceProjection", Value::Enum(target)), ("targetProjection", Value::Enum(0))], + cube.img.clone(), + 0.0, + EffectEnv::default(), + ); + let mut worst = 0.0f32; + let mut sum = 0.0f32; + for (a, b) in back.img.data.iter().zip(&img.data) { + let e = (0..3).map(|k| (a[k] - b[k]).abs()).fold(0.0, f32::max); + worst = worst.max(e); + sum += e; + } + let mean = sum / img.data.len() as f32; + // The 6:1 strip squeezes each face into a narrow cell, so its seams are coarser. + let tol = if target == 3 { 0.25 } else { 0.08 }; + assert!(mean < 0.01 && worst < tol, "target {target}: mean {mean} worst {worst}"); + // Cube faces carry the expected directions: the front face centre looks at +Z. + let pr = Proj { kind: target, fov: 90.0 }; + let (x, y, _) = pr.pos([0.0, 0.0, 1.0], 128.0, 64.0).unwrap(); + let px = cube.img.sample_bilinear_clamped(x, y); + assert!((px[2] - 0.9).abs() < 0.02, "{px:?}"); + } + } + + #[test] + fn rotate_sphere_pans_and_round_trips() { + let img = sphere_img(96, 48); + let a = run_fx("ec.vr.rotatesphere", &[("pan", num(90.0))], img.clone(), 0.0, EffectEnv::default()); + let b = run_fx("ec.vr.rotatesphere", &[("pan", num(-90.0))], a.img.clone(), 0.0, EffectEnv::default()); + let err: f32 = b.img.data.iter().zip(&img.data).map(|(p, q)| (p[0] - q[0]).abs()).sum::() / img.data.len() as f32; + assert!(err < 0.01, "{err}"); + // A 90° pan is a horizontal shift by a quarter turn. + let (x, y) = (10u32, 24u32); + let shifted = img.get(((x + 24) % 96) as i64, y as i64); + let got = a.img.get(x as i64, y as i64); + let alt = img.get(((x + 72) % 96) as i64, y as i64); + assert!((got[0] - shifted[0]).abs() < 0.02 || (got[0] - alt[0]).abs() < 0.02); + } + + #[test] + fn blur_is_seamless() { + // A frame whose left and right edges differ: after blurring, the two edge columns agree + // (they are neighbours on the sphere). + let mut img = Image::new(64, 32); + for y in 0..32 { + for x in 0..64 { + let v = if x < 4 { + 1.0 + } else if x >= 60 { + 0.0 + } else { + 0.5 + }; + img.set(x, y, [v, v, v, 1.0]); + } + } + let out = run_fx("ec.vr.blur", &[("blurriness", num(8.0))], img.clone(), 0.0, EffectEnv::default()); + let (l, r) = (out.img.get(0, 16)[0], out.img.get(63, 16)[0]); + assert!((l - r).abs() < 0.2, "{l} {r}"); + assert!(r > 0.2 && l < 0.85); + // Pole rows are uniform (every pixel of the top row is the same point). + let top: Vec = (0..64).map(|x| out.img.get(x, 0)[0]).collect(); + let spread = top.iter().cloned().fold(f32::MIN, f32::max) - top.iter().cloned().fold(f32::MAX, f32::min); + assert!(spread < 0.1, "{spread}"); + // Constant images stay constant. + let flat = Image::filled(32, 16, [0.3, 0.3, 0.3, 1.0]); + let o = run_fx("ec.vr.blur", &[], flat, 0.0, EffectEnv::default()); + assert!(o.img.data.iter().all(|p| (p[0] - 0.3).abs() < 1e-4)); + } + + #[test] + fn stereo_layouts_process_each_eye() { + let mut img = Image::new(32, 32); + for y in 0..32 { + for x in 0..32 { + let v = if y < 16 { 0.2 } else { 0.8 }; + img.set(x, y, [v, v, v, 1.0]); + } + } + // Over/under: blurring each eye keeps the eyes separate. + let o = run_fx("ec.vr.blur", &[("frameLayout", Value::Enum(1)), ("blurriness", num(10.0))], img.clone(), 0.0, EffectEnv::default()); + assert!((o.img.get(5, 15)[0] - 0.2).abs() < 1e-4 && (o.img.get(5, 16)[0] - 0.8).abs() < 1e-4); + let mono = run_fx("ec.vr.blur", &[("blurriness", num(10.0))], img, 0.0, EffectEnv::default()); + assert!((mono.img.get(5, 15)[0] - 0.2).abs() > 0.01); + } + + #[test] + fn sphere_to_plane_and_back() { + let img = sphere_img(128, 64); + // The centre of a 2D view looking forward sees +Z (blue 0.9). + let v = run_fx("ec.vr.spheretoplane", &[], img.clone(), 0.0, EffectEnv::default()); + let c = v.img.get(64, 32); + assert!((c[2] - 0.9).abs() < 0.02, "{c:?}"); + // Looking up (tilt 90°): +Y (green 0.9). + let up = run_fx("ec.vr.spheretoplane", &[("tilt", num(-90.0))], img.clone(), 0.0, EffectEnv::default()); + let u = up.img.get(64, 32); + let dn = run_fx("ec.vr.spheretoplane", &[("tilt", num(90.0))], img, 0.0, EffectEnv::default()); + let d = dn.img.get(64, 32); + assert!((u[1] - 0.9).abs() < 0.03 || (d[1] - 0.9).abs() < 0.03, "{u:?} {d:?}"); + // Plane to sphere puts the picture in front, transparent behind. + let pic = Image::filled(64, 32, [1.0, 0.0, 0.0, 1.0]); + let s = run_fx("ec.vr.planetosphere", &[], pic, 0.0, EffectEnv::default()); + assert!(s.img.get(32, 16)[3] > 0.99 && s.img.get(1, 16)[3] < 0.01); + } + + #[test] + fn generators_and_filters_are_deterministic_and_seamless() { + let img = sphere_img(64, 32); + for (id, vals) in [ + ("ec.vr.fractalnoise", vec![]), + ("ec.vr.colorgradients", vec![]), + ("ec.vr.glow", vec![("luminanceThreshold", num(50.0))]), + ("ec.vr.sharpen", vec![]), + ("ec.vr.denoise", vec![]), + ("ec.vr.chromaticaberrations", vec![]), + ("ec.vr.digitalglitch", vec![]), + ] { + let a = run_fx(id, &vals, img.clone(), 0.5, EffectEnv::default()); + let b = run_fx(id, &vals, img.clone(), 0.5, EffectEnv::default()); + assert_eq!(a.img.data, b.img.data, "{id}"); + assert!(a.img.data.iter().all(|p| p.iter().all(|v| v.is_finite())), "{id}"); + } + // Fractal noise is continuous across the seam: the first and last columns are close. + let n = run_fx("ec.vr.fractalnoise", &[], img.clone(), 0.0, EffectEnv::default()); + let mut worst = 0.0f32; + for y in 0..32 { + worst = worst.max((n.img.get(0, y)[0] - n.img.get(63, y)[0]).abs()); + } + assert!(worst < 0.15, "{worst}"); + // Gradients reach the point colour at the point. + let pts = [ + ("point1", Value::Vec2([16.0, 16.0])), + ("point2", Value::Vec2([32.0, 8.0])), + ("point3", Value::Vec2([48.0, 16.0])), + ("point4", Value::Vec2([32.0, 24.0])), + ]; + let g = run_fx("ec.vr.colorgradients", &pts, img.clone(), 0.0, EffectEnv::default()); + let px = g.img.get(16, 16); + assert!(px[0] > 0.8 && px[1] < 0.3, "{px:?}"); + // Glitch changes over time. + let g0 = run_fx("ec.vr.digitalglitch", &[], img.clone(), 0.0, EffectEnv::default()); + let g1 = run_fx("ec.vr.digitalglitch", &[], img.clone(), 3.3, EffectEnv::default()); + assert_ne!(g0.img.data, g1.img.data); + } +} From 83c7e14b944a44329db30bc269b091ff0cf83151 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:04:47 -0400 Subject: [PATCH 04/23] M7.4: model import New crate effectcraft-model (L2): glTF 2.0 (.gltf/.glb, data URIs, external buffers and images, PBR metallic-roughness with base/MR/normal/occlusion/emissive textures, node hierarchy, skins, step/linear/cubic animations) and OBJ/MTL import, parametric primitives, extruded/bevelled outline meshes and polygon triangulation with holes. Project: FootageKind::Model, LayerSource::Model/Primitive, Layer::environment, Geometry and Material Options builders, Environment light Source. Media probes and loads models (FootageSource::model). Engine: layer.newModel, layer.new3dPrimitive/newPrimitive, comp.renderer, layer.environment, material.set/reset/duplicateAssign/revealSource (stubs replaced); File > Import and layer.addItem pick up models. Hand-written fixtures with attribution sidecars. Co-Authored-By: Claude Opus 5.5 --- ATTRIBUTION.md | 4 + Cargo.lock | 14 + Cargo.toml | 1 + crates/engine/Cargo.toml | 1 + crates/engine/src/commands/comp.rs | 1 + crates/engine/src/commands/file.rs | 2 +- crates/engine/src/commands/layer.rs | 9 +- crates/engine/src/commands/mod.rs | 2 + crates/engine/src/commands/model3d.rs | 397 ++++++++++ crates/engine/src/commands/stubs.rs | 5 - crates/engine/src/lib.rs | 2 + crates/engine/src/tests_model3d.rs | 194 +++++ crates/lottie/src/export.rs | 4 + crates/media/Cargo.toml | 1 + crates/media/src/lib.rs | 10 +- crates/media/src/pool.rs | 35 +- crates/media/src/probe.rs | 39 + crates/model/Cargo.toml | 17 + crates/model/src/extrude.rs | 301 ++++++++ crates/model/src/gltf.rs | 519 +++++++++++++ crates/model/src/lib.rs | 691 ++++++++++++++++++ crates/model/src/obj.rs | 262 +++++++ crates/model/src/prim.rs | 235 ++++++ crates/model/src/tests.rs | 318 ++++++++ crates/model/src/triangulate.rs | 187 +++++ crates/model/tests/fixtures/cube.mtl | 11 + .../model/tests/fixtures/cube.mtl.attribution | 7 + crates/model/tests/fixtures/cube.obj | 29 + .../model/tests/fixtures/cube.obj.attribution | 7 + crates/model/tests/fixtures/quad.glb | Bin 0 -> 2028 bytes .../model/tests/fixtures/quad.glb.attribution | 7 + crates/model/tests/fixtures/quad.gltf | 210 ++++++ .../tests/fixtures/quad.gltf.attribution | 7 + crates/project/src/build.rs | 100 ++- crates/project/src/lib.rs | 53 +- crates/render/Cargo.toml | 1 + crates/render/src/lib.rs | 4 + crates/ui-egui/src/menus.rs | 2 +- crates/ui-egui/src/panels/project.rs | 1 + crates/ui-egui/src/panels/timeline.rs | 1 + docs/architecture.md | 1 + xtask/src/main.rs | 1 + 42 files changed, 3676 insertions(+), 17 deletions(-) create mode 100644 crates/engine/src/commands/model3d.rs create mode 100644 crates/engine/src/tests_model3d.rs create mode 100644 crates/model/Cargo.toml create mode 100644 crates/model/src/extrude.rs create mode 100644 crates/model/src/gltf.rs create mode 100644 crates/model/src/lib.rs create mode 100644 crates/model/src/obj.rs create mode 100644 crates/model/src/prim.rs create mode 100644 crates/model/src/tests.rs create mode 100644 crates/model/src/triangulate.rs create mode 100644 crates/model/tests/fixtures/cube.mtl create mode 100644 crates/model/tests/fixtures/cube.mtl.attribution create mode 100644 crates/model/tests/fixtures/cube.obj create mode 100644 crates/model/tests/fixtures/cube.obj.attribution create mode 100644 crates/model/tests/fixtures/quad.glb create mode 100644 crates/model/tests/fixtures/quad.glb.attribution create mode 100644 crates/model/tests/fixtures/quad.gltf create mode 100644 crates/model/tests/fixtures/quad.gltf.attribution diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 9caf1c27..f89c0e87 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -36,6 +36,10 @@ iconography, images or other Adobe assets; open licences only; every asset attri | `crates/lottie/tests/fixtures/shapes-gradient.json` | EffectCraft contributors | Original work: hand-written Lottie test fixture (star, rectangle, gradient fill, dashed stroke, trim paths) | MIT OR Apache-2.0 | | `crates/lottie/tests/fixtures/precomp-matte-text.json` | EffectCraft contributors | Original work: hand-written Lottie test fixture (precomp, track matte, parented text) | MIT OR Apache-2.0 | | `crates/lottie/tests/fixtures/legacy-masks.json` | EffectCraft contributors | Original work: hand-written Lottie test fixture (legacy keyframes, masks) | MIT OR Apache-2.0 | +| `crates/model/tests/fixtures/quad.gltf` | EffectCraft contributors | Original work: glTF 2.0 test fixture (textured quad, node hierarchy, translation animation; data URIs) | MIT OR Apache-2.0 | +| `crates/model/tests/fixtures/quad.glb` | EffectCraft contributors | Original work: binary glTF test fixture (the same quad with its texture in the BIN chunk) | MIT OR Apache-2.0 | +| `crates/model/tests/fixtures/cube.obj` | EffectCraft contributors | Original work: Wavefront OBJ test fixture (unit cube, two materials) | MIT OR Apache-2.0 | +| `crates/model/tests/fixtures/cube.mtl` | EffectCraft contributors | Original work: MTL test fixture (materials of cube.obj) | MIT OR Apache-2.0 | ## Screenshots diff --git a/Cargo.lock b/Cargo.lock index 635fb0f5..fc3f4a02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1195,6 +1195,7 @@ dependencies = [ "effectcraft-geom", "effectcraft-keyframe", "effectcraft-lottie", + "effectcraft-model", "effectcraft-path", "effectcraft-project", "effectcraft-raster", @@ -1322,6 +1323,7 @@ dependencies = [ name = "effectcraft-media" version = "0.1.0" dependencies = [ + "effectcraft-model", "effectcraft-project", "effectcraft-raster", "effectcraft-render", @@ -1337,6 +1339,17 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "effectcraft-model" +version = "0.1.0" +dependencies = [ + "effectcraft-geom", + "image", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "effectcraft-path" version = "0.1.0" @@ -1379,6 +1392,7 @@ dependencies = [ "effectcraft-effects", "effectcraft-geom", "effectcraft-keyframe", + "effectcraft-model", "effectcraft-path", "effectcraft-project", "effectcraft-raster", diff --git a/Cargo.toml b/Cargo.toml index a1941cf4..a5294834 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ effectcraft-project = { path = "crates/project" } effectcraft-text = { path = "crates/text" } effectcraft-effects = { path = "crates/effects" } effectcraft-track = { path = "crates/track" } +effectcraft-model = { path = "crates/model" } effectcraft-render = { path = "crates/render" } effectcraft-gpu = { path = "crates/gpu" } effectcraft-media = { path = "crates/media" } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 132bdcac..a60242bb 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -21,6 +21,7 @@ effectcraft-text = { workspace = true } effectcraft-effects = { workspace = true } effectcraft-render = { workspace = true } effectcraft-track = { workspace = true } +effectcraft-model = { workspace = true } effectcraft-lottie = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/engine/src/commands/comp.rs b/crates/engine/src/commands/comp.rs index 5ebd491e..a33fd574 100644 --- a/crates/engine/src/commands/comp.rs +++ b/crates/engine/src/commands/comp.rs @@ -125,6 +125,7 @@ fn settings(s: &mut Session, p: &Value) -> Result { } let c = proj.comp_mut(cid).ok_or(EngineError::NoComp)?; apply_settings(c, p); + super::model3d::sync_geometry_options(proj, cid); Ok(()) })?; let c = s.project.comp(cid).ok_or(EngineError::NoComp)?; diff --git a/crates/engine/src/commands/file.rs b/crates/engine/src/commands/file.rs index a85a391f..11e8a387 100644 --- a/crates/engine/src/commands/file.rs +++ b/crates/engine/src/commands/file.rs @@ -110,7 +110,7 @@ fn import(s: &mut Session, p: &Value) -> Result { let label = match f.kind { FootageKind::Still | FootageKind::Sequence => Label::Lavender, FootageKind::Audio => Label::SeaFoam, - FootageKind::Video => Label::Aqua, + FootageKind::Video | FootageKind::Model => Label::Aqua, }; let id = proj.add_item(&name, label, None, ItemKind::Footage(f)); ids.push(id.0); diff --git a/crates/engine/src/commands/layer.rs b/crates/engine/src/commands/layer.rs index 3dea6880..ac728200 100644 --- a/crates/engine/src/commands/layer.rs +++ b/crates/engine/src/commands/layer.rs @@ -24,7 +24,11 @@ pub(crate) fn color_p(p: &Value, k: &str) -> Option<[f32; 3]> { } /// Insert a new layer above the selection (or at the top) and select it. -pub(crate) fn insert_layer(proj: &mut Project, st: &mut crate::EditorState, cid: ItemId, layer: Layer) -> Result { +pub(crate) fn insert_layer(proj: &mut Project, st: &mut crate::EditorState, cid: ItemId, mut layer: Layer) -> Result { + // Text and shape layers in Advanced 3D comps get Geometry Options (extrusion, bevels). + if proj.comp(cid).is_some_and(|c| c.renderer == effectcraft_project::Renderer::Advanced3D) { + super::model3d::add_geometry_options(&mut proj.next_id, &mut layer); + } let comp = proj.comp_mut(cid).ok_or(EngineError::NoComp)?; let at = st.selected_layers.first().and_then(|id| comp.layers.iter().position(|l| l.id == *id)).unwrap_or(0); let id = layer.id; @@ -235,6 +239,9 @@ fn add_item(s: &mut Session, p: &Value) -> Result { let d = (s.prefs.import.still_footage == "seconds").then(|| Tick::from_seconds_f64(s.prefs.import.still_seconds)); (LayerSource::Footage { item }, (f.width, f.height), d) } + ItemKind::Footage(f) if f.kind == effectcraft_project::FootageKind::Model => { + return super::model3d::new_model(s, &serde_json::json!({"comp": cid.0, "item": item.0, "time": f_p(p, "time")})); + } ItemKind::Footage(f) => (LayerSource::Footage { item }, (f.width, f.height), Some(f.duration)), ItemKind::Solid(so) => (LayerSource::Solid { item }, (so.width, so.height), None), ItemKind::Folder => return Err(bad("layer.addItem", "folders can't be layers")), diff --git a/crates/engine/src/commands/mod.rs b/crates/engine/src/commands/mod.rs index 26db85c9..519bce63 100644 --- a/crates/engine/src/commands/mod.rs +++ b/crates/engine/src/commands/mod.rs @@ -20,6 +20,7 @@ mod link; mod lottie; mod markers; mod mask; +pub(crate) mod model3d; pub mod paint; mod paths; mod project_items; @@ -99,6 +100,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(layer::specs()); v.extend(styles::specs()); v.extend(three_d::specs()); + v.extend(model3d::specs()); v.extend(text_anim::specs()); v.extend(text_edit::specs()); v.extend(layer_time::specs()); diff --git a/crates/engine/src/commands/model3d.rs b/crates/engine/src/commands/model3d.rs new file mode 100644 index 00000000..1ad9cc32 --- /dev/null +++ b/crates/engine/src/commands/model3d.rs @@ -0,0 +1,397 @@ +//! Advanced 3D: model layers (File ▸ Import of glTF/OBJ, Layer ▸ New ▸ 3D primitives), the +//! comp renderer switch, Layer ▸ Environment Layer, Geometry Options for extruded text and +//! shapes, and Layer ▸ Material commands. + +use effectcraft_keyframe::Value as KV; +use effectcraft_project::build::{self, Ids}; +use effectcraft_project::{FootageKind, ItemId, ItemKind, Layer, LayerSource, Node, PrimitiveKind, PropGroup, Renderer}; +use effectcraft_time::Tick; +use serde_json::{Value, json}; + +use super::layer::{color_p, insert_layer}; +use super::{CommandSpec, b_p, bad, comp_id, f_p, has_comp, has_layers, layer_mut, layer_p, layers_p, str_p}; +use crate::{EngineError, Result, Session, cmd}; + +// ---------------------------------------------------------------- geometry options + +/// Add Geometry Options (Bevel Style, Bevel Depth, Hole Bevel Depth, Extrusion Depth) to a text +/// or shape layer that has none. +pub(crate) fn add_geometry_options(next_id: &mut u64, l: &mut Layer) { + if !matches!(l.source, LayerSource::Text | LayerSource::Shape) || l.props.sub("geometryOptions").is_some() { + return; + } + let g = build::extrusion_geometry_options(&mut Ids(next_id)); + // After Transform, like After Effects. + let at = l.props.children.iter().position(|c| c.match_id() == "transform").map_or(l.props.children.len(), |i| i + 1); + l.props.children.insert(at, g.into()); +} + +/// Advanced 3D comps: give every text and shape layer Geometry Options. +pub(crate) fn sync_geometry_options(proj: &mut effectcraft_project::Project, cid: ItemId) { + if proj.comp(cid).is_none_or(|c| c.renderer != Renderer::Advanced3D) { + return; + } + let mut next = proj.next_id; + if let Some(c) = proj.comp_mut(cid) { + for l in &mut c.layers { + add_geometry_options(&mut next, l); + } + } + proj.next_id = next; +} + +fn renderer_p(p: &Value, cmd: &str) -> Result { + let v = str_p(p, "renderer").or_else(|| str_p(p, "value")).ok_or_else(|| bad(cmd, "missing `renderer` (advanced3d|classic3d)"))?; + match v.to_ascii_lowercase().replace([' ', '_', '-'], "").as_str() { + "advanced3d" | "advanced" => Ok(Renderer::Advanced3D), + "classic3d" | "classic" => Ok(Renderer::Classic3D), + _ => Err(bad(cmd, format!("renderer: advanced3d|classic3d, not `{v}`"))), + } +} + +fn comp_renderer(s: &mut Session, p: &Value) -> Result { + let cid = comp_id(s, p)?; + if p.get("renderer").is_none() && p.get("value").is_none() { + let r = s.project.comp(cid).ok_or(EngineError::NoComp)?.renderer; + return Ok(json!({"renderer": renderer_id(r)})); + } + let r = renderer_p(p, "comp.renderer")?; + s.edit("Change Renderer", None, |proj, _| { + proj.comp_mut(cid).ok_or(EngineError::NoComp)?.renderer = r; + sync_geometry_options(proj, cid); + Ok(()) + })?; + Ok(json!({"renderer": renderer_id(r)})) +} + +fn renderer_id(r: Renderer) -> &'static str { + match r { + Renderer::Advanced3D => "advanced3d", + Renderer::Classic3D => "classic3d", + } +} + +// ---------------------------------------------------------------- model layers + +/// Set a property's value at comp time `t` (adds a key when it is animated). +fn set_at(l: &mut Layer, path: &str, t: Tick, v: KV) -> bool { + let lt = l.layer_time(t); + match l.props.prop_mut(path) { + Some(pr) => { + pr.set_value_at(lt, v); + true + } + None => false, + } +} + +/// New 3D model layer from a model footage item (`item`) or a file (`path`, imported first). +pub(crate) fn new_model(s: &mut Session, p: &Value) -> Result { + let cid = comp_id(s, p)?; + let item = match (p.get("item"), str_p(p, "path")) { + (Some(Value::Number(n)), _) => ItemId(n.as_u64().unwrap_or(0)), + (Some(Value::String(name)), _) => s.project.find_by_name(name).map(|i| i.id).ok_or_else(|| bad("layer.newModel", format!("no item `{name}`")))?, + (_, Some(path)) => { + let r = s.execute("file.import", json!({"paths": [path]}))?; + let id = r.get("items").and_then(|a| a.get(0)).and_then(Value::as_u64); + let err = r.get("errors").and_then(|e| e.get(0)).and_then(Value::as_str).unwrap_or("import failed").to_string(); + ItemId(id.ok_or_else(|| bad("layer.newModel", err))?) + } + _ => *s.state.project_selection.first().ok_or_else(|| bad("layer.newModel", "missing `item` or `path`"))?, + }; + let it = s.project.item(item).ok_or_else(|| bad("layer.newModel", "no such item"))?.clone(); + let ItemKind::Footage(f) = &it.kind else { return Err(bad("layer.newModel", format!("`{}` is not a 3D model", it.name))) }; + if f.kind != FootageKind::Model { + return Err(bad("layer.newModel", format!("`{}` is not a 3D model", it.name))); + } + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone(); + // Fit the model to half the comp height, and list its animation clips. + let model = s.footage.model(item, f); + let (scale, clips) = match &model { + Some(m) => { + let ext = m.bounds().map_or(1.0, |(lo, hi)| (0..3).map(|k| hi[k] - lo[k]).fold(0.0f64, f64::max)); + let scale = if ext > 1e-9 { comp.height as f64 * 0.5 / ext } else { 100.0 }; + let clips = m.animations.iter().enumerate().map(|(i, a)| if a.name.is_empty() { format!("Animation {}", i + 1) } else { a.name.clone() }).collect(); + (scale, clips) + } + None => (100.0, vec![]), + }; + let name = str_p(p, "name").unwrap_or(&it.name).to_string(); + let fr = comp.frame_rate; + let start = fr.snap_nearest(f_p(p, "time").map(Tick::from_seconds_f64).unwrap_or(Tick::ZERO)); + let id = s.edit("New 3D Model Layer", None, |proj, st| { + let mut l = build::layer(proj, &comp, &name, LayerSource::Model { item }, (comp.width, comp.height), None); + let g = build::model_geometry_options(&mut Ids(&mut proj.next_id), scale, &clips); + if let Some(slot) = l.props.children.iter_mut().find(|c| c.match_id() == "geometryOptions") { + *slot = g.into(); + } + l.start_time = start; + l.in_point = start; + l.out_point = comp.duration.max(start + fr.frame_duration()); + insert_layer(proj, st, cid, l) + })?; + Ok(json!({"layer": id.0, "item": item.0})) +} + +fn kind_p(p: &Value, cmd: &str) -> Result { + let k = str_p(p, "kind").or_else(|| str_p(p, "type")).ok_or_else(|| bad(cmd, "missing `kind` (cube|sphere|plane|torus|cone|cylinder)"))?; + PrimitiveKind::parse(k).ok_or_else(|| bad(cmd, format!("kind: cube|sphere|plane|torus|cone|cylinder, not `{k}`"))) +} + +/// Parse Material Options parameters onto a layer at comp time `t`. +fn apply_material(l: &mut Layer, p: &Value, t: Tick, cmd: &str) -> Result { + let mut n = 0; + let tri = |v: &Value| -> Option { + match v { + Value::Bool(b) => Some(*b as u32), + Value::Number(x) => x.as_u64().map(|x| x.min(2) as u32), + Value::String(s) => match s.to_ascii_lowercase().as_str() { + "off" | "false" => Some(0), + "on" | "true" => Some(1), + "only" => Some(2), + _ => None, + }, + _ => None, + } + }; + for k in ["castsShadows", "acceptsShadows"] { + if let Some(v) = p.get(k) { + let e = tri(v).ok_or_else(|| bad(cmd, format!("{k}: off|on|only")))?; + n += set_at(l, &format!("materialOptions/{k}"), t, KV::Enum(e)) as usize; + } + } + for k in ["acceptsLights", "appearsInReflections"] { + if let Some(v) = b_p(p, k) { + n += set_at(l, &format!("materialOptions/{k}"), t, KV::Bool(v)) as usize; + } + } + for k in ["lightTransmission", "ambient", "diffuse", "specularIntensity", "specularShininess", "metal", "metallic", "roughness"] { + if let Some(v) = f_p(p, k) { + n += set_at(l, &format!("materialOptions/{k}"), t, KV::Scalar(v)) as usize; + } + } + for k in ["baseColor", "emissive"] { + if let Some(c) = color_p(p, k) { + n += set_at(l, &format!("materialOptions/{k}"), t, KV::Color([c[0] as f64, c[1] as f64, c[2] as f64, 1.0])) as usize; + } + } + Ok(n) +} + +fn new_primitive(s: &mut Session, p: &Value) -> Result { + let cid = comp_id(s, p)?; + let kind = kind_p(p, "layer.newPrimitive")?; + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone(); + let name = str_p(p, "name").map(str::to_string).unwrap_or_else(|| kind.label().to_string()); + let t = super::time_p(s, p, Some(&comp)); + let id = s.edit(&format!("New {}", kind.label()), None, |proj, st| { + let mut l = build::layer(proj, &comp, &name, LayerSource::Primitive { kind }, (comp.width, comp.height), None); + for k in ["width", "height", "depth", "radius", "tubeRadius", "segments", "rings"] { + if let Some(v) = f_p(p, k) { + set_at(&mut l, &format!("geometryOptions/{k}"), t, KV::Scalar(v.max(0.0))); + } + } + if let Some(pos) = p.get("position").and_then(Value::as_array) { + let g = |i: usize| pos.get(i).and_then(Value::as_f64).unwrap_or(0.0); + set_at(&mut l, "transform/position", t, KV::Vec3([g(0), g(1), g(2)])); + } + apply_material(&mut l, p, t, "layer.newPrimitive")?; + insert_layer(proj, st, cid, l) + })?; + // Primitives are drawn by the Advanced 3D renderer. + let advanced = s.project.comp(cid).is_some_and(|c| c.renderer == Renderer::Advanced3D); + Ok(json!({"layer": id.0, "kind": kind.label(), "advanced3d": advanced})) +} + +// ---------------------------------------------------------------- environment layer + +fn environment(s: &mut Session, p: &Value) -> Result { + let (cid, ids) = layers_p(s, p)?; + if ids.is_empty() { + return Err(bad("layer.environment", "select a footage or composition layer")); + } + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; + for id in &ids { + let l = comp.layer(*id).ok_or(EngineError::NoComp)?; + if !l.source.is_av() || l.source.is_model() { + return Err(bad("layer.environment", format!("`{}` can't be an environment layer (use a footage, comp or solid layer)", l.name))); + } + } + let on = b_p(p, "on").unwrap_or_else(|| !comp.layer(ids[0]).is_some_and(|l| l.environment)); + s.edit("Environment Layer", None, |proj, _| { + for id in &ids { + let l = layer_mut(proj, cid, *id)?; + l.environment = on; + if on { + l.switches.three_d = true; + } + } + Ok(()) + })?; + Ok(json!({"environment": on, "layers": ids.iter().map(|l| l.0).collect::>()})) +} + +// ---------------------------------------------------------------- materials + +fn material_set(s: &mut Session, p: &Value) -> Result { + let (cid, ids) = layers_p(s, p)?; + if ids.is_empty() { + return Err(bad("material.set", "no `layers` given and no layer selected")); + } + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone(); + let t = super::time_p(s, p, Some(&comp)); + let n = s.edit("Material Options", None, |proj, _| { + let mut n = 0; + for id in &ids { + n += apply_material(layer_mut(proj, cid, *id)?, p, t, "material.set")?; + } + Ok(n) + })?; + if n == 0 { + return Err(bad( + "material.set", + "no Material Options property matched (castsShadows, acceptsShadows, acceptsLights, baseColor, metallic, roughness, …)", + )); + } + Ok(json!({"set": n})) +} + +/// Fresh Material Options for a layer. +fn default_material(next: &mut u64, l: &Layer) -> Option { + let ids = &mut Ids(next); + match l.source { + LayerSource::Primitive { .. } => Some(build::model_material_options(ids, true)), + LayerSource::Model { .. } => Some(build::model_material_options(ids, false)), + _ if l.source.is_av() => Some(build::material_options(ids)), + _ => None, + } +} + +fn replace_material(l: &mut Layer, g: PropGroup) { + match l.props.children.iter_mut().find(|c| c.match_id() == "materialOptions") { + Some(slot) => *slot = Node::Group(g), + None => l.props.children.push(g.into()), + } +} + +fn material_reset(s: &mut Session, p: &Value) -> Result { + let (cid, ids) = layers_p(s, p)?; + if ids.is_empty() { + return Err(bad("material.reset", "no `layers` given and no layer selected")); + } + s.edit("Reset Material", None, |proj, _| { + let mut next = proj.next_id; + for id in &ids { + let l = layer_mut(proj, cid, *id)?; + if let Some(g) = default_material(&mut next, l) { + replace_material(l, g); + } + } + proj.next_id = next; + Ok(()) + })?; + Ok(json!({"layers": ids.iter().map(|l| l.0).collect::>()})) +} + +/// Duplicate and Assign Material: the first layer's Material Options are copied onto the other +/// selected layers of the same kind. +fn material_duplicate(s: &mut Session, p: &Value) -> Result { + let (cid, ids) = layers_p(s, p)?; + if ids.len() < 2 { + return Err(bad("material.duplicateAssign", "select the source layer first, then the layers to assign its material to")); + } + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; + let src = comp + .layer(ids[0]) + .and_then(|l| l.props.sub("materialOptions")) + .cloned() + .ok_or_else(|| bad("material.duplicateAssign", "the first layer has no material"))?; + let n = s.edit("Duplicate and Assign Material", None, |proj, _| { + let mut n = 0; + for id in &ids[1..] { + let l = layer_mut(proj, cid, *id)?; + let Some(dst) = l.props.sub("materialOptions") else { continue }; + // Property sets differ (models, primitives, cards): copy the values they share. + let mut g = dst.clone(); + for c in g.children.iter_mut() { + if let Node::Prop(d) = c + && let Some(sv) = src.get(&d.match_id) + { + d.value = sv.value.clone(); + d.keys = sv.keys.clone(); + d.expr = sv.expr.clone(); + } + } + replace_material(l, g); + n += 1; + } + Ok(n) + })?; + Ok(json!({"assigned": n})) +} + +fn material_reveal(s: &mut Session, p: &Value) -> Result { + let (cid, lid) = layer_p(s, p, "material.revealSource")?; + let l = s.project.comp(cid).and_then(|c| c.layer(lid)).ok_or(EngineError::NoComp)?; + let LayerSource::Model { item } = l.source else { return Err(bad("material.revealSource", "the layer's material is its own (not from a model file)")) }; + s.state.project_selection = vec![item]; + Ok(json!({"item": item.0})) +} + +fn has_model_or_layers(s: &Session) -> std::result::Result<(), String> { + has_layers(s) +} + +pub fn specs() -> Vec { + vec![ + cmd!( + "layer.newModel", + "3D Model Layer", + [], + None, + "{item?: id|name (a 3D model footage item) | path?: .gltf|.glb|.obj file, name?, time? (s)}", + has_comp, + new_model + ), + cmd!( + "layer.new3dPrimitive", + "3D Primitive", + [], + None, + "{kind: cube|sphere|plane|torus|cone|cylinder, name?, width?, height?, depth?, radius?, tubeRadius?, segments?, rings?, position? [x,y,z], baseColor?, metallic?, roughness?, emissive?, castsShadows?, acceptsShadows?, acceptsLights?}", + has_comp, + new_primitive + ), + cmd!( + "layer.newPrimitive", + "New 3D Primitive", + [], + None, + "{kind: cube|sphere|plane|torus|cone|cylinder, name?, width?, height?, depth?, radius?, tubeRadius?, segments?, rings?, position? [x,y,z], baseColor?, metallic?, roughness?, emissive?, castsShadows?, acceptsShadows?, acceptsLights?}", + has_comp, + new_primitive + ), + cmd!("comp.renderer", "Renderer", [], None, "{renderer?: advanced3d|classic3d (omit to read), value?}", has_comp, comp_renderer), + cmd!("layer.environment", "Environment Layer", ["Layer"], None, "{layers?, on?}", has_layers, environment), + cmd!( + "material.set", + "Material Options", + [], + None, + "{layers?, castsShadows?: off|on|only, acceptsShadows?: off|on|only, acceptsLights?, appearsInReflections?, lightTransmission?, ambient?, diffuse?, specularIntensity?, specularShininess?, metal?, baseColor?, metallic?, roughness?, emissive?, time?}", + has_layers, + material_set + ), + cmd!("material.revealSource", "Reveal Material Source in Project", ["Layer", "Material"], None, "{layer?}", has_layers, material_reveal), + cmd!("material.reset", "Reset Material", ["Layer", "Material"], None, "{layers?}", has_model_or_layers, material_reset), + cmd!( + "material.duplicateAssign", + "Duplicate and Assign Material", + ["Layer", "Material"], + None, + "{layers? (first = source)}", + has_layers, + material_duplicate + ), + ] +} diff --git a/crates/engine/src/commands/stubs.rs b/crates/engine/src/commands/stubs.rs index f7868d9c..01880a53 100644 --- a/crates/engine/src/commands/stubs.rs +++ b/crates/engine/src/commands/stubs.rs @@ -29,11 +29,9 @@ pub fn specs() -> Vec { stub!("keys.selectLabelGroup", "Select Keyframe Label Group", [], "{scope}"), // Layer ▸ New (3D / content-aware fill). stub!("layer.newContentAwareFill", "Content-Aware Fill Layer...", ["Layer", "New"], "{}"), - stub!("layer.new3dPrimitive", "3D Primitive", [], "{kind: cube|sphere|plane|torus|cone|cylinder}"), // Mask options not in the mask model yet; pen / vertex editing (timeline milestone). // Time (timeline milestone). stub!("layer.alignVideoToData", "Align Video to Data", ["Layer", "Time"], "{}"), - stub!("layer.environment", "Environment Layer", ["Layer"], "{}"), stub!("layer.create", "Create", [], "{op}"), stub!("layer.autoTrace", "Auto-trace...", ["Layer"], "{}"), stub!("layer.sceneEditDetection", "Scene Edit Detection...", ["Layer"], "{}"), @@ -47,9 +45,6 @@ pub fn specs() -> Vec { stub!("light.fromModel", "Create Lights from 3D Model", ["Layer", "Light"], "{}"), stub!("light.controlWithCamera", "Control Light with Camera", ["Layer", "Light"], "{}"), stub!("light.environmentBackground", "Create Environment Light Background Layer", ["Layer", "Light"], "{}"), - stub!("material.revealSource", "Reveal Material Source in Project", ["Layer", "Material"], "{}"), - stub!("material.reset", "Reset Material", ["Layer", "Material"], "{}"), - stub!("material.duplicateAssign", "Duplicate and Assign Material", ["Layer", "Material"], "{}"), stub!("view.3d.default", "Default", ["View", "Switch 3D View"], "{}"), stub!("view.splitLockedViewer", "Split with New Locked Viewer", ["View"], "{}"), // Color management. diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index f64ce817..eb234ea8 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -556,6 +556,8 @@ mod tests_markers; #[cfg(test)] mod tests_menu_cmds; #[cfg(test)] +mod tests_model3d; +#[cfg(test)] mod tests_project_items; #[cfg(test)] mod tests_settings; diff --git a/crates/engine/src/tests_model3d.rs b/crates/engine/src/tests_model3d.rs new file mode 100644 index 00000000..51f9173d --- /dev/null +++ b/crates/engine/src/tests_model3d.rs @@ -0,0 +1,194 @@ +//! Advanced 3D commands: model import → model layers, primitives, the renderer switch with +//! Geometry Options, Environment Layer and Material commands (undo/redo, serde). + +use std::sync::Arc; + +use effectcraft_keyframe::Value as KV; +use effectcraft_project::{Footage, FootageKind, ItemId, LayerId, LayerSource, PrimitiveKind, Project, Renderer}; +use effectcraft_render::FootageSource; +use effectcraft_time::{FrameRate, Tick}; +use serde_json::json; + +use crate::{Importer, Session}; + +fn fixture(name: &str) -> String { + format!("{}/../model/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR")) +} + +/// Loads models from disk (what `effectcraft-media` does in the app). +struct DiskModels; + +impl FootageSource for DiskModels { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn model(&self, _: ItemId, f: &Footage) -> Option> { + let dir = std::path::Path::new(&f.path).parent()?.to_path_buf(); + let bytes = std::fs::read(&f.path).ok()?; + effectcraft_model::load(&f.path, &bytes, &|u| std::fs::read(dir.join(u)).ok()).ok().map(Arc::new) + } +} + +struct ModelImporter; + +impl Importer for ModelImporter { + fn probe(&self, path: &str) -> Result { + effectcraft_model::format_of(path).ok_or("not a model")?; + Ok(Footage { + path: path.into(), + kind: FootageKind::Model, + width: 0, + height: 0, + pixel_aspect: 1.0, + frame_rate: FrameRate::FPS_30, + native_rate: None, + duration: Tick::ZERO, + has_video: false, + has_audio: false, + alpha: Default::default(), + premul_color: [0.0; 3], + loop_count: 1, + codec: "glTF".into(), + missing: false, + sequence: vec![], + color_profile: None, + }) + } +} + +fn session() -> Session { + let mut s = Session { footage: Arc::new(DiskModels), importer: Some(Arc::new(ModelImporter)), ..Default::default() }; + s.execute("comp.new", json!({"name": "C", "width": 640, "height": 360, "duration": 2})).unwrap(); + s +} + +fn layer(s: &Session, id: u64) -> effectcraft_project::Layer { + s.active_comp().unwrap().layer(LayerId(id)).unwrap().clone() +} + +#[test] +fn import_gltf_and_add_model_layer() { + let mut s = session(); + let r = s.execute("file.import", json!({"paths": [fixture("quad.gltf")]})).unwrap(); + let item = r["items"][0].as_u64().unwrap(); + assert_eq!(s.project.item(ItemId(item)).unwrap().type_name(), "3D Model"); + // Adding the item to the comp makes a model layer (3D, fitted, with the clip popup). + let id = s.execute("layer.addItem", json!({"item": item})).unwrap()["layer"].as_u64().unwrap(); + let l = layer(&s, id); + assert_eq!(l.source, LayerSource::Model { item: ItemId(item) }); + assert!(l.switches.three_d); + assert!(l.props.sub("masks").is_none() && l.props.sub("materialOptions").is_some()); + // Bounds 2 units tall → half the comp height. + let scale = l.props.prop("geometryOptions/unitScale").unwrap().value.as_f64(); + assert!((scale - 90.0).abs() < 1e-6, "{scale}"); + let anim = l.props.prop("geometryOptions/animation").unwrap(); + assert_eq!(anim.value, KV::Enum(1)); + match &anim.ui { + effectcraft_project::ParamUi::Popup { options } => assert_eq!(options, &vec!["None".to_string(), "Move".to_string()]), + u => panic!("{u:?}"), + } + s.undo(); + assert!(s.active_comp().unwrap().layers.is_empty()); + s.redo(); + assert_eq!(s.active_comp().unwrap().layers.len(), 1); + // layer.newModel imports and places in one step. + let r = s.execute("layer.newModel", json!({"path": fixture("cube.obj")})).unwrap(); + assert!(r["layer"].as_u64().is_some()); + assert!(s.execute("layer.newModel", json!({"path": "/nonexistent/thing.png"})).is_err()); +} + +#[test] +fn primitives_commands() { + let mut s = session(); + for k in PrimitiveKind::ALL { + let r = s.execute("layer.new3dPrimitive", json!({"kind": k.label().to_lowercase()})).unwrap(); + let l = layer(&s, r["layer"].as_u64().unwrap()); + assert_eq!(l.source, LayerSource::Primitive { kind: k }); + assert_eq!(l.name, k.label()); + assert!(l.switches.three_d); + assert!(l.props.sub("geometryOptions").is_some()); + } + let r = s + .execute("layer.newPrimitive", json!({"kind": "sphere", "radius": 80, "segments": 12, "baseColor": [1, 0, 0], "metallic": 100, "castsShadows": "on"})) + .unwrap(); + let l = layer(&s, r["layer"].as_u64().unwrap()); + assert_eq!(l.props.prop("geometryOptions/radius").unwrap().value.as_f64(), 80.0); + assert_eq!(l.props.prop("materialOptions/metallic").unwrap().value.as_f64(), 100.0); + assert_eq!(l.props.prop("materialOptions/castsShadows").unwrap().value, KV::Enum(1)); + assert!(s.execute("layer.newPrimitive", json!({"kind": "teapot"})).is_err()); + let n = s.active_comp().unwrap().layers.len(); + s.undo(); + assert_eq!(s.active_comp().unwrap().layers.len(), n - 1); + // Menu entries point at the real command now. + assert!(s.is_enabled("layer.new3dPrimitive")); +} + +#[test] +fn renderer_switch_adds_geometry_options() { + let mut s = session(); + let t = s.execute("layer.newText", json!({"text": "Hi"})).unwrap()["layer"].as_u64().unwrap(); + assert!(layer(&s, t).props.sub("geometryOptions").is_none()); + s.execute("comp.renderer", json!({"renderer": "advanced3d"})).unwrap(); + assert_eq!(s.active_comp().unwrap().renderer, Renderer::Advanced3D); + let g = layer(&s, t); + let g = g.props.sub("geometryOptions").unwrap(); + let ids: Vec<&str> = g.children.iter().map(|c| c.match_id()).collect(); + assert_eq!(ids, vec!["bevelStyle", "bevelDepth", "holeBevelDepth", "extrusionDepth"]); + // New shape layers in the Advanced 3D comp get them too. + let sh = s.execute("layer.newShape", json!({})).unwrap()["layer"].as_u64().unwrap(); + assert!(layer(&s, sh).props.sub("geometryOptions").is_some()); + assert_eq!(s.execute("comp.renderer", json!({})).unwrap()["renderer"], "advanced3d"); + s.undo(); + s.undo(); + assert_eq!(s.active_comp().unwrap().renderer, Renderer::Classic3D); + assert!(layer(&s, t).props.sub("geometryOptions").is_none()); + s.redo(); + assert_eq!(s.active_comp().unwrap().renderer, Renderer::Advanced3D); + assert!(s.execute("comp.renderer", json!({"renderer": "raytraced"})).is_err()); + // Extrusion values are ordinary properties. + s.execute("prop.set", json!({"layer": t, "path": "geometryOptions/extrusionDepth", "value": 40})).unwrap(); + assert_eq!(layer(&s, t).props.prop("geometryOptions/extrusionDepth").unwrap().value.as_f64(), 40.0); +} + +#[test] +fn environment_layer_toggle() { + let mut s = session(); + let id = s.execute("layer.newSolid", json!({"width": 64, "height": 32})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.environment", json!({"layer": id})).unwrap(); + assert!(layer(&s, id).environment && layer(&s, id).switches.three_d); + s.execute("layer.environment", json!({"layer": id})).unwrap(); + assert!(!layer(&s, id).environment); + s.undo(); + assert!(layer(&s, id).environment); + let cam = s.execute("layer.newCamera", json!({})).unwrap()["layer"].as_u64().unwrap(); + assert!(s.execute("layer.environment", json!({"layer": cam})).is_err()); + // An Environment light sources a layer. + let li = s.execute("layer.newLight", json!({"kind": "Environment"})).unwrap()["layer"].as_u64().unwrap(); + assert!(layer(&s, li).props.prop("lightOptions/source").is_some()); +} + +#[test] +fn material_commands_and_serde() { + let mut s = session(); + let a = s.execute("layer.newPrimitive", json!({"kind": "cube"})).unwrap()["layer"].as_u64().unwrap(); + let b = s.execute("layer.newPrimitive", json!({"kind": "torus"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("material.set", json!({"layers": [a], "baseColor": "#3366ff", "roughness": 20, "acceptsLights": false})).unwrap(); + assert_eq!(layer(&s, a).props.prop("materialOptions/roughness").unwrap().value.as_f64(), 20.0); + assert!(s.execute("material.set", json!({"layers": [a], "castsShadows": "sometimes"})).is_err()); + s.execute("material.duplicateAssign", json!({"layers": [a, b]})).unwrap(); + assert_eq!(layer(&s, b).props.prop("materialOptions/roughness").unwrap().value.as_f64(), 20.0); + assert_eq!(layer(&s, b).props.prop("materialOptions/acceptsLights").unwrap().value, KV::Bool(false)); + s.execute("material.reset", json!({"layers": [a]})).unwrap(); + assert_eq!(layer(&s, a).props.prop("materialOptions/roughness").unwrap().value.as_f64(), 50.0); + s.undo(); + assert_eq!(layer(&s, a).props.prop("materialOptions/roughness").unwrap().value.as_f64(), 20.0); + assert!(s.execute("material.revealSource", json!({"layer": a})).is_err()); + // The project (model and primitive sources, environment flag) round-trips through JSON. + let r = s.execute("file.import", json!({"paths": [fixture("quad.glb")]})).unwrap(); + let item = r["items"][0].as_u64().unwrap(); + let m = s.execute("layer.addItem", json!({"item": item})).unwrap()["layer"].as_u64().unwrap(); + assert_eq!(s.execute("material.revealSource", json!({"layer": m})).unwrap()["item"], item); + let json = s.project.to_json(); + let back = Project::from_json(&json).unwrap(); + assert_eq!(&back, &*s.project); +} diff --git a/crates/lottie/src/export.rs b/crates/lottie/src/export.rs index fa5447d8..eeb6e273 100644 --- a/crates/lottie/src/export.rs +++ b/crates/lottie/src/export.rs @@ -400,6 +400,10 @@ impl Doc<'_> { self.ex.warn(format!("{name}: cameras and lights are not exported (3D; exported as a hidden null to keep parenting)")); hidden = true; } + LayerSource::Model { .. } | LayerSource::Primitive { .. } => { + self.ex.warn(format!("{name}: 3D model layers are not part of Lottie (layer exported as a hidden null)")); + hidden = true; + } } o.insert("ty".into(), json!(ty)); if let Some(p) = l.parent.and_then(|p| comp.index_of(p)) { diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml index 89992569..010a6414 100644 --- a/crates/media/Cargo.toml +++ b/crates/media/Cargo.toml @@ -14,6 +14,7 @@ effectcraft-time = { workspace = true } effectcraft-project = { workspace = true } effectcraft-raster = { workspace = true } effectcraft-render = { workspace = true } +effectcraft-model = { workspace = true } filmcraft-codecs = { workspace = true } filmcraft-media = { workspace = true } filmcraft-frame = { workspace = true } diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index 29546f7e..fcc872d1 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -17,7 +17,7 @@ mod pool; mod probe; pub use pool::{DEFAULT_BUDGET, MediaPool, PoolStats}; -pub use probe::{DEFAULT_SEQUENCE_RATE, probe, probe_bytes, probe_single, sequence_files}; +pub use probe::{DEFAULT_SEQUENCE_RATE, probe, probe_bytes, probe_model, probe_single, sequence_files}; /// Errors from probing or decoding footage. #[derive(Debug, thiserror::Error)] @@ -50,10 +50,16 @@ pub const AUDIO_EXTENSIONS: &[&str] = filmcraft_media::AUDIO_EXTENSIONS; /// File extensions recognised as movies. pub const VIDEO_EXTENSIONS: &[&str] = &["mp4", "m4v", "mov", "mkv", "webm"]; +/// File extensions recognised as 3D models (Advanced 3D model layers). +pub const MODEL_EXTENSIONS: &[&str] = &["gltf", "glb", "obj"]; + /// Whether `path` has an extension we can import. pub fn is_importable(path: &std::path::Path) -> bool { let ext = ext_of(path); - STILL_EXTENSIONS.contains(&ext.as_str()) || AUDIO_EXTENSIONS.contains(&ext.as_str()) || VIDEO_EXTENSIONS.contains(&ext.as_str()) + MODEL_EXTENSIONS.contains(&ext.as_str()) + || STILL_EXTENSIONS.contains(&ext.as_str()) + || AUDIO_EXTENSIONS.contains(&ext.as_str()) + || VIDEO_EXTENSIONS.contains(&ext.as_str()) } pub(crate) fn ext_of(path: &std::path::Path) -> String { diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index b4cc51ef..a7898fc6 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -122,6 +122,8 @@ struct Inner { sources: Mutex, Option>>, /// In-memory files registered with `add_bytes` (web builds, tests). files: Mutex>>, + /// Parsed 3D models by path (`None`: failed to load; not retried until `forget`). + models: Mutex>>>, cache: Mutex, done: Condvar, budget: AtomicU64, @@ -183,6 +185,7 @@ impl MediaPool { inner: Arc::new(Inner { sources: Mutex::default(), files: Mutex::default(), + models: Mutex::default(), cache: Mutex::default(), done: Condvar::new(), budget: AtomicU64::new(bytes as u64), @@ -234,6 +237,7 @@ impl MediaPool { /// Forget a file (after it changed on disk, or to retry a file that failed to open). pub fn forget(&self, path: &str) { lock(&self.inner.sources).remove(path); + lock(&self.inner.models).remove(path); let mut c = lock(&self.inner.cache); let keys: Vec = c.lru.map.keys().filter(|k| &*k.path == path).cloned().collect(); for k in keys { @@ -305,6 +309,29 @@ impl Inner { self.budget.load(Ordering::Relaxed) as usize } + /// A 3D model file and its sibling resources (buffers, textures, MTL files), parsed once. + fn model(&self, path: &str) -> Option> { + if let Some(m) = lock(&self.models).get(path) { + return m.clone(); + } + let loaded = self.read(path).map_err(|e| e.to_string()).and_then(|bytes| { + let dir = std::path::Path::new(path).parent().map(|d| d.to_path_buf()).unwrap_or_default(); + let resolve = |uri: &str| -> Option> { + let p = dir.join(uri); + self.read(&p.to_string_lossy()).ok().map(|b| b.to_vec()) + }; + effectcraft_model::load(path, &bytes, &resolve).map_err(|e| e.to_string()) + }); + let m = match loaded { + Ok(m) => Some(Arc::new(m)), + Err(e) => { + log::warn!("media: cannot load model {path}: {e}"); + None + } + }; + lock(&self.models).entry(path.to_string()).or_insert(m).clone() + } + fn read(&self, path: &str) -> Result> { if let Some(b) = lock(&self.files).get(path) { return Ok(b.clone()); @@ -359,7 +386,7 @@ impl Inner { let i = Self::frame_index(footage.frame_rate, t, footage.sequence.len() as i64, footage.loop_count); Loc { key: key(&footage.sequence[i as usize], 0), media_t: None } } - FootageKind::Still | FootageKind::Sequence => Loc { key: key(&footage.path, 0), media_t: None }, + FootageKind::Still | FootageKind::Sequence | FootageKind::Model => Loc { key: key(&footage.path, 0), media_t: None }, FootageKind::Video | FootageKind::Audio => { let rate = footage.frame_rate; let n = Self::frame_count(rate, footage.duration); @@ -451,6 +478,12 @@ impl Inner { } impl FootageSource for MediaPool { + fn model(&self, _item: ItemId, footage: &Footage) -> Option> { + if footage.missing || footage.kind != FootageKind::Model { + return None; + } + self.inner.model(&footage.path) + } fn set_cache_budget(&self, bytes: usize) { self.set_budget(bytes); } diff --git a/crates/media/src/probe.rs b/crates/media/src/probe.rs index 4e1c5611..e97544db 100644 --- a/crates/media/src/probe.rs +++ b/crates/media/src/probe.rs @@ -28,11 +28,50 @@ pub fn probe(path: impl AsRef) -> Result { pub fn probe_single(path: impl AsRef) -> Result { let path = path.as_ref(); let bytes = std::fs::read(path).map_err(|e| MediaError::Io(format!("{}: {e}", path.display())))?; + if crate::MODEL_EXTENSIONS.contains(&ext_of(path).as_str()) { + let dir = path.parent().map(Path::to_path_buf).unwrap_or_default(); + return probe_model(&path.to_string_lossy(), &bytes, &|uri| std::fs::read(dir.join(uri)).ok()); + } probe_bytes(&path.to_string_lossy(), bytes.into()) } +/// Probe a 3D model (glTF 2.0 / OBJ): a [`FootageKind::Model`] item whose duration is its +/// longest animation. `resolve` reads sibling resources. +pub fn probe_model(path: &str, bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { + let m = effectcraft_model::load(path, bytes, resolve).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; + let s = m.summary(); + let dur = s.animations.iter().map(|a| a.1).fold(0.0f64, f64::max); + let codec = match effectcraft_model::format_of(path) { + Some("obj") => "OBJ", + Some("glb") => "GLB", + _ => "glTF", + }; + Ok(Footage { + path: path.to_string(), + kind: FootageKind::Model, + width: 0, + height: 0, + pixel_aspect: 1.0, + frame_rate: DEFAULT_SEQUENCE_RATE, + native_rate: None, + duration: Tick::from_seconds_f64(dur), + has_video: false, + has_audio: false, + alpha: AlphaMode::Straight, + premul_color: [0.0; 3], + loop_count: 1, + codec: format!("{codec} ({} meshes, {} triangles)", s.meshes, s.triangles), + missing: false, + sequence: Vec::new(), + color_profile: None, + }) +} + /// Probe in-memory file contents; `path` names the footage (and is stored as `Footage::path`). pub fn probe_bytes(path: &str, bytes: Arc<[u8]>) -> Result { + if crate::MODEL_EXTENSIONS.contains(&ext_of(Path::new(path)).as_str()) { + return probe_model(path, &bytes, &|_| None); + } if let Ok(fmt) = image::guess_format(&bytes) { return probe_still_bytes(path, &bytes, fmt); } diff --git a/crates/model/Cargo.toml b/crates/model/Cargo.toml new file mode 100644 index 00000000..20a1fde0 --- /dev/null +++ b/crates/model/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "effectcraft-model" +description = "3D models: glTF 2.0 (.gltf/.glb) and Wavefront OBJ/MTL import, PBR metallic-roughness materials, node hierarchy, skinning and animation, parametric primitives, and extruded/bevelled outline meshes" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] +effectcraft-geom = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +image = { workspace = true } diff --git a/crates/model/src/extrude.rs b/crates/model/src/extrude.rs new file mode 100644 index 00000000..344c67cb --- /dev/null +++ b/crates/model/src/extrude.rs @@ -0,0 +1,301 @@ +//! Extruded, bevelled meshes from 2D outlines (Advanced 3D text and shape layers' Geometry +//! Options: Bevel Style, Bevel Depth, Hole Bevel Depth, Extrusion Depth). +//! +//! Input outlines are closed polylines in layer space (x right, y down). The mesh is in layer +//! space too: the front cap lies on the layer plane (z = 0, facing the camera, −z), the back cap +//! at z = depth. Each contour gets a ring of vertices per profile step (inset from the outline +//! by the bevel, at a depth along the profile); consecutive rings are joined by quads, the caps +//! are triangulated from the innermost front/back rings with holes. Caps and walls share +//! positions exactly, so the mesh is watertight (welded by position). + +use serde::{Deserialize, Serialize}; + +use crate::Primitive; +use crate::triangulate::{signed_area, triangulate}; + +type P = [f64; 2]; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub enum BevelStyle { + #[default] + None, + Angular, + Concave, + Convex, +} + +impl BevelStyle { + pub const ALL: [BevelStyle; 4] = [BevelStyle::None, BevelStyle::Angular, BevelStyle::Concave, BevelStyle::Convex]; + pub fn label(self) -> &'static str { + match self { + BevelStyle::None => "None", + BevelStyle::Angular => "Angular", + BevelStyle::Concave => "Concave", + BevelStyle::Convex => "Convex", + } + } + pub fn from_index(i: u32) -> BevelStyle { + BevelStyle::ALL.get(i as usize).copied().unwrap_or_default() + } + /// (inset fraction 1 → 0, depth fraction 0 → 1) along the profile at `t` in 0..1. + fn profile(self, t: f64) -> (f64, f64) { + let th = t * std::f64::consts::FRAC_PI_2; + match self { + BevelStyle::None | BevelStyle::Angular => (1.0 - t, t), + // Quarter circle bulging out towards the outline corner. + BevelStyle::Convex => (1.0 - th.sin(), 1.0 - th.cos()), + // Quarter circle scooped in. + BevelStyle::Concave => (th.cos(), th.sin()), + } + } + fn steps(self) -> usize { + match self { + BevelStyle::None => 0, + BevelStyle::Angular => 1, + _ => 6, + } + } +} + +/// Geometry Options of an extruded layer. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ExtrudeParams { + pub bevel: BevelStyle, + /// Pixels. + pub bevel_depth: f64, + /// Hole bevels as a fraction of the bevel depth (0..1). + pub hole_bevel: f64, + /// Pixels (> 0). + pub depth: f64, +} + +impl Default for ExtrudeParams { + fn default() -> Self { + ExtrudeParams { bevel: BevelStyle::None, bevel_depth: 2.0, hole_bevel: 1.0, depth: 50.0 } + } +} + +/// An outer contour with its holes. +#[derive(Clone, Debug, PartialEq)] +pub struct Outline { + pub outer: Vec

, + pub holes: Vec>, +} + +fn point_in(p: P, c: &[P]) -> bool { + let n = c.len(); + let mut inside = false; + let mut j = n - 1; + for i in 0..n { + let (a, b) = (c[i], c[j]); + if (a[1] > p[1]) != (b[1] > p[1]) && p[0] < (b[0] - a[0]) * (p[1] - a[1]) / (b[1] - a[1]) + a[0] { + inside = !inside; + } + j = i; + } + inside +} + +/// Group closed contours into outlines by nesting depth (even depth = outer, odd = hole of the +/// innermost containing outer contour). Degenerate contours are dropped. +pub fn group_contours(contours: Vec>) -> Vec { + let cs: Vec> = contours + .into_iter() + .map(|mut c| { + c.dedup(); + while c.len() > 1 && c.first() == c.last() { + c.pop(); + } + c + }) + .filter(|c| c.len() >= 3 && signed_area(c).abs() > 1e-9) + .collect(); + let n = cs.len(); + let area: Vec = cs.iter().map(|c| signed_area(c).abs()).collect(); + // Containers of each contour (by a vertex inside; nested contours do not cross). + let parents: Vec> = (0..n) + .map(|i| { + let probe = cs[i][0]; + (0..n).filter(|&j| j != i && area[j] > area[i] && point_in(probe, &cs[j])).collect() + }) + .collect(); + let mut outlines: Vec = vec![]; + let mut slot = vec![usize::MAX; n]; + let mut order: Vec = (0..n).collect(); + order.sort_by_key(|&i| parents[i].len()); + for i in order { + let depth = parents[i].len(); + if depth.is_multiple_of(2) { + slot[i] = outlines.len(); + outlines.push(Outline { outer: cs[i].clone(), holes: vec![] }); + } else if let Some(&p) = parents[i].iter().filter(|&&j| parents[j].len() == depth - 1).min_by(|&&a, &&b| area[a].total_cmp(&area[b])) + && slot[p] != usize::MAX + { + outlines[slot[p]].holes.push(cs[i].clone()); + } + } + outlines +} + +struct Contour { + pts: Vec

, + /// Outward unit normal of edge i (pts[i] → pts[i+1]). + edge_n: Vec

, + /// Miter offset direction per vertex (outward, length ≥ 1). + miter: Vec

, + /// Smooth-shaded vertex (shallow corner). + smooth: Vec, + bevel: f64, +} + +fn contour(mut pts: Vec

, outer: bool, bevel: f64) -> Contour { + // Outer counter-clockwise (in x/y), holes clockwise: the outward normal is (dy, −dx). + let ccw = signed_area(&pts) > 0.0; + if ccw != outer { + pts.reverse(); + } + let n = pts.len(); + let edge_n: Vec

= (0..n) + .map(|i| { + let (a, b) = (pts[i], pts[(i + 1) % n]); + let (dx, dy) = (b[0] - a[0], b[1] - a[1]); + let l = dx.hypot(dy).max(1e-12); + [dy / l, -dx / l] + }) + .collect(); + let mut miter = vec![[0.0; 2]; n]; + let mut smooth = vec![false; n]; + for i in 0..n { + let (n0, n1) = (edge_n[(i + n - 1) % n], edge_n[i]); + let d = n0[0] * n1[0] + n0[1] * n1[1]; + let s = [n0[0] + n1[0], n0[1] + n1[1]]; + let k = 1.0 + d; + miter[i] = if k < 0.25 { + // Very sharp corner: limit the miter (offset along the bisector, length 2.8). + let l = s[0].hypot(s[1]).max(1e-12); + [s[0] / l * 2.8, s[1] / l * 2.8] + } else { + [s[0] / k, s[1] / k] + }; + smooth[i] = d > 0.82; // < ~35° turn + } + Contour { pts, edge_n, miter, smooth, bevel } +} + +/// Rings of a contour: (inset, z) from the front cap to the back cap. +fn rings(style: BevelStyle, bevel: f64, depth: f64) -> Vec<(f64, f64)> { + let b = bevel.clamp(0.0, depth * 0.5); + let steps = if b > 1e-9 { style.steps() } else { 0 }; + if steps == 0 { + return vec![(0.0, 0.0), (0.0, depth)]; + } + let front: Vec<(f64, f64)> = (0..=steps) + .map(|k| { + let (f, g) = style.profile(k as f64 / steps as f64); + (b * f, b * g) + }) + .collect(); + let mut out = front.clone(); + let back: Vec<(f64, f64)> = front.iter().rev().map(|&(i, z)| (i, depth - z)).collect(); + // Skip a duplicate ring where the walls have zero height (bevel = depth / 2). + let start = if (back[0].1 - out[out.len() - 1].1).abs() < 1e-9 { 1 } else { 0 }; + out.extend_from_slice(&back[start..]); + out +} + +/// Build the extruded mesh of outlines (one primitive, material 0). +pub fn extrude(outlines: &[Outline], p: &ExtrudeParams) -> Primitive { + let mut prim = Primitive { material: Some(0), ..Default::default() }; + let depth = p.depth.max(1e-3); + let push = |prim: &mut Primitive, pos: [f64; 3], n: [f64; 3]| -> u32 { + prim.positions.push([pos[0] as f32, pos[1] as f32, pos[2] as f32]); + let l = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt().max(1e-12); + prim.normals.push([(n[0] / l) as f32, (n[1] / l) as f32, (n[2] / l) as f32]); + prim.uvs.push([0.0, 0.0]); + (prim.positions.len() - 1) as u32 + }; + for o in outlines { + let mut cs = vec![contour(o.outer.clone(), true, p.bevel_depth)]; + for h in &o.holes { + cs.push(contour(h.clone(), false, p.bevel_depth * p.hole_bevel.clamp(0.0, 1.0))); + } + // Positions per contour per ring. + let mut ring_pos: Vec>> = vec![]; + let mut ring_par: Vec> = vec![]; + for c in &cs { + let rs = rings(p.bevel, c.bevel, depth); + let pos: Vec> = + rs.iter().map(|&(inset, z)| c.pts.iter().zip(&c.miter).map(|(q, m)| [q[0] - m[0] * inset, q[1] - m[1] * inset, z]).collect()).collect(); + ring_pos.push(pos); + ring_par.push(rs); + } + // Walls and bevels. + for (ci, c) in cs.iter().enumerate() { + let rs = &ring_par[ci]; + let n = c.pts.len(); + let curved = matches!(p.bevel, BevelStyle::Concave | BevelStyle::Convex); + // Profile normal (outward weight, z weight) of segment k. + let seg_n = |k: usize| -> (f64, f64) { + let (i0, z0) = rs[k]; + let (i1, z1) = rs[k + 1]; + let (di, dz) = (i1 - i0, z1 - z0); + let l = di.hypot(dz).max(1e-12); + (dz / l, di / l) + }; + for k in 0..rs.len() - 1 { + let is_wall = (rs[k].0 - rs[k + 1].0).abs() < 1e-12; + let (a0, a1) = (seg_n(k), seg_n(k)); + // Curved bevels: average with the neighbouring bevel segments at shared rings. + let blend = |k2: Option, base: (f64, f64)| -> (f64, f64) { + match k2 { + Some(k2) if curved && !is_wall && k2 < rs.len() - 1 && (rs[k2].0 - rs[k2 + 1].0).abs() >= 1e-12 => { + let o = seg_n(k2); + (base.0 + o.0, base.1 + o.1) + } + _ => base, + } + }; + let pa = blend(k.checked_sub(1), a0); + let pb = blend(Some(k + 1), a1); + for i in 0..n { + let j = (i + 1) % n; + let en = c.edge_n[i]; + let vn = |v: usize| -> P { + if c.smooth[v] { + let m = c.miter[v]; + let l = m[0].hypot(m[1]).max(1e-12); + [m[0] / l, m[1] / l] + } else { + en + } + }; + let (ni, nj) = (vn(i), vn(j)); + let nrm = |xy: P, pr: (f64, f64)| [xy[0] * pr.0, xy[1] * pr.0, pr.1]; + let ai = push(&mut prim, ring_pos[ci][k][i], nrm(ni, pa)); + let aj = push(&mut prim, ring_pos[ci][k][j], nrm(nj, pa)); + let bj = push(&mut prim, ring_pos[ci][k + 1][j], nrm(nj, pb)); + let bi = push(&mut prim, ring_pos[ci][k + 1][i], nrm(ni, pb)); + prim.indices.extend([ai, aj, bj, ai, bj, bi]); + } + } + } + // Caps: triangulate in x/y (counter-clockwise → +z normal: the back cap; the front cap + // is reversed to face the camera). + let outer2: Vec

= ring_pos[0][0].iter().map(|q| [q[0], q[1]]).collect(); + let holes2: Vec> = ring_pos[1..].iter().map(|r| r[0].iter().map(|q| [q[0], q[1]]).collect()).collect(); + let tris = triangulate(&outer2, &holes2); + let flat: Vec<(usize, usize)> = (0..cs.len()).flat_map(|ci| (0..cs[ci].pts.len()).map(move |i| (ci, i))).collect(); + for (front, nz) in [(true, -1.0), (false, 1.0)] { + let base = prim.positions.len() as u32; + for &(ci, i) in &flat { + let r = if front { 0 } else { ring_pos[ci].len() - 1 }; + push(&mut prim, ring_pos[ci][r][i], [0.0, 0.0, nz]); + } + for t in &tris { + let (a, b, c) = (base + t[0] as u32, base + t[1] as u32, base + t[2] as u32); + if front { prim.indices.extend([a, c, b]) } else { prim.indices.extend([a, b, c]) } + } + } + } + prim +} diff --git a/crates/model/src/gltf.rs b/crates/model/src/gltf.rs new file mode 100644 index 00000000..375884bc --- /dev/null +++ b/crates/model/src/gltf.rs @@ -0,0 +1,519 @@ +//! glTF 2.0 import, written from the Khronos glTF 2.0 specification. +//! +//! Supports `.gltf` (JSON with external `.bin`/image files or `data:` URIs) and `.glb` (binary +//! container: a JSON chunk and a BIN chunk). Imports the default scene's node hierarchy (TRS or +//! matrix), triangle primitives (`TRIANGLES`, `TRIANGLE_STRIP`, `TRIANGLE_FAN`) with POSITION, +//! NORMAL, TANGENT, TEXCOORD_0, JOINTS_0 and WEIGHTS_0, PBR metallic-roughness materials +//! (base colour, metallic-roughness, normal, occlusion and emissive textures; alpha modes; +//! double-sided; `KHR_materials_emissive_strength`, `KHR_materials_unlit`), embedded or +//! external PNG/JPEG images with sampler wrap modes, skins and animations (step, linear and +//! cubic-spline translation/rotation/scale). Sparse accessors, morph targets, cameras and +//! lights are ignored. + +use std::sync::Arc; + +use serde_json::Value; + +use crate::{ + AlphaMode, Animation, Channel, ChannelPath, Interp, Mat4, Material, Mesh, Model, ModelError, Node, Primitive, Result, Skin, TexRef, Texture, Wrap, perr, +}; + +const GLB_MAGIC: u32 = 0x4654_6C67; +const CHUNK_JSON: u32 = 0x4E4F_534A; +const CHUNK_BIN: u32 = 0x004E_4942; + +/// Split a GLB container into its JSON text and BIN chunk. +pub fn split_glb(b: &[u8]) -> Result<(&[u8], Option<&[u8]>)> { + let u32_at = |o: usize| -> Option { b.get(o..o + 4).map(|s| u32::from_le_bytes([s[0], s[1], s[2], s[3]])) }; + if u32_at(0) != Some(GLB_MAGIC) { + return Err(perr("not a GLB file")); + } + if u32_at(4) != Some(2) { + return Err(ModelError::Unsupported(format!("GLB version {}", u32_at(4).unwrap_or(0)))); + } + let total = (u32_at(8).unwrap_or(0) as usize).min(b.len()); + let mut o = 12; + let mut json = None; + let mut bin = None; + while o + 8 <= total { + let len = u32_at(o).unwrap_or(0) as usize; + let ty = u32_at(o + 4).unwrap_or(0); + let data = b.get(o + 8..o + 8 + len).ok_or_else(|| perr("GLB chunk runs past the end of the file"))?; + match ty { + CHUNK_JSON if json.is_none() => json = Some(data), + CHUNK_BIN if bin.is_none() => bin = Some(data), + _ => {} + } + o += 8 + len; + } + Ok((json.ok_or_else(|| perr("GLB without a JSON chunk"))?, bin)) +} + +/// Decode standard base64 (with or without padding; whitespace ignored). +pub fn base64_decode(s: &str) -> Option> { + let mut out = Vec::with_capacity(s.len() * 3 / 4); + let mut acc = 0u32; + let mut bits = 0; + for c in s.bytes() { + let v = match c { + b'A'..=b'Z' => c - b'A', + b'a'..=b'z' => c - b'a' + 26, + b'0'..=b'9' => c - b'0' + 52, + b'+' | b'-' => 62, + b'/' | b'_' => 63, + b'=' => break, + b' ' | b'\n' | b'\r' | b'\t' => continue, + _ => return None, + } as u32; + acc = (acc << 6) | v; + bits += 6; + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + Some(out) +} + +/// Percent-decode a relative URI. +pub fn uri_decode(s: &str) -> String { + let b = s.as_bytes(); + let mut out = Vec::with_capacity(b.len()); + let mut i = 0; + while i < b.len() { + if b[i] == b'%' + && i + 2 < b.len() + && let Ok(v) = u8::from_str_radix(std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or("zz"), 16) + { + out.push(v); + i += 3; + continue; + } + out.push(b[i]); + i += 1; + } + String::from_utf8_lossy(&out).into_owned() +} + +/// Bytes of a `data:` URI or an external resource. +fn load_uri(uri: &str, resolve: &dyn Fn(&str) -> Option>) -> Result> { + if let Some(rest) = uri.strip_prefix("data:") { + let (meta, data) = rest.split_once(',').ok_or_else(|| perr("malformed data URI"))?; + return if meta.ends_with(";base64") { base64_decode(data).ok_or_else(|| perr("bad base64 in data URI")) } else { Ok(uri_decode(data).into_bytes()) }; + } + let name = uri_decode(uri); + resolve(&name).ok_or(ModelError::Missing(name)) +} + +struct Doc<'a> { + j: &'a Value, + buffers: Vec>, +} + +fn arr<'v>(v: &'v Value, k: &str) -> &'v [Value] { + v.get(k).and_then(Value::as_array).map(Vec::as_slice).unwrap_or(&[]) +} +fn idx(v: &Value, k: &str) -> Option { + v.get(k).and_then(Value::as_u64).map(|x| x as usize) +} +fn num(v: &Value, k: &str, d: f64) -> f64 { + v.get(k).and_then(Value::as_f64).unwrap_or(d) +} +fn nums(v: &Value, k: &str, d: [f64; N]) -> [f64; N] { + let mut out = d; + if let Some(a) = v.get(k).and_then(Value::as_array) { + for (i, x) in a.iter().take(N).enumerate() { + out[i] = x.as_f64().unwrap_or(d[i]); + } + } + out +} + +impl Doc<'_> { + /// An accessor as `f32` rows of `n` components (normalised integers mapped to 0..1 / -1..1). + fn floats(&self, a: usize) -> Result<(Vec, usize)> { + let acc = arr(self.j, "accessors").get(a).ok_or_else(|| perr(format!("no accessor {a}")))?; + let n = match acc.get("type").and_then(Value::as_str).unwrap_or("") { + "SCALAR" => 1, + "VEC2" => 2, + "VEC3" => 3, + "VEC4" => 4, + "MAT2" => 4, + "MAT3" => 9, + "MAT4" => 16, + t => return Err(perr(format!("accessor type `{t}`"))), + }; + let count = idx(acc, "count").unwrap_or(0); + let ct = idx(acc, "componentType").unwrap_or(5126); + let norm = acc.get("normalized").and_then(Value::as_bool).unwrap_or(false); + let csize = match ct { + 5120 | 5121 => 1, + 5122 | 5123 => 2, + 5125 | 5126 => 4, + _ => return Err(perr(format!("componentType {ct}"))), + }; + let mut out = vec![0.0f32; count * n]; + let Some(bv) = idx(acc, "bufferView") else { + // No buffer view: all zeros (sparse substitution is not supported). + return Ok((out, n)); + }; + let view = arr(self.j, "bufferViews").get(bv).ok_or_else(|| perr(format!("no bufferView {bv}")))?; + let buf = self.buffers.get(idx(view, "buffer").unwrap_or(0)).ok_or_else(|| perr("bufferView buffer out of range"))?; + let base = idx(view, "byteOffset").unwrap_or(0) + idx(acc, "byteOffset").unwrap_or(0); + let elem = csize * n; + // Matrices of 1/2-byte components pad columns to 4 bytes; ignored (rare). + let stride = idx(view, "byteStride").filter(|s| *s > 0).unwrap_or(elem); + for i in 0..count { + for c in 0..n { + let o = base + i * stride + c * csize; + let s = buf.get(o..o + csize).ok_or_else(|| perr("accessor reads past its buffer"))?; + out[i * n + c] = match ct { + 5126 => f32::from_le_bytes([s[0], s[1], s[2], s[3]]), + 5125 => u32::from_le_bytes([s[0], s[1], s[2], s[3]]) as f32, + 5123 => { + let v = u16::from_le_bytes([s[0], s[1]]) as f32; + if norm { v / 65535.0 } else { v } + } + 5122 => { + let v = i16::from_le_bytes([s[0], s[1]]) as f32; + if norm { (v / 32767.0).max(-1.0) } else { v } + } + 5121 => { + let v = s[0] as f32; + if norm { v / 255.0 } else { v } + } + _ => { + let v = s[0] as i8 as f32; + if norm { (v / 127.0).max(-1.0) } else { v } + } + }; + } + } + Ok((out, n)) + } + + /// Integer accessor (indices, joints) read exactly. + fn ints(&self, a: usize) -> Result> { + let acc = arr(self.j, "accessors").get(a).ok_or_else(|| perr(format!("no accessor {a}")))?; + let ct = idx(acc, "componentType").unwrap_or(5125); + if ct == 5126 { + return Ok(self.floats(a)?.0.into_iter().map(|v| v as u32).collect()); + } + let n = match acc.get("type").and_then(Value::as_str).unwrap_or("SCALAR") { + "VEC4" => 4, + "VEC3" => 3, + "VEC2" => 2, + _ => 1, + }; + let count = idx(acc, "count").unwrap_or(0); + let csize = match ct { + 5120 | 5121 => 1, + 5122 | 5123 => 2, + _ => 4, + }; + let Some(bv) = idx(acc, "bufferView") else { return Ok(vec![0; count * n]) }; + let view = arr(self.j, "bufferViews").get(bv).ok_or_else(|| perr(format!("no bufferView {bv}")))?; + let buf = self.buffers.get(idx(view, "buffer").unwrap_or(0)).ok_or_else(|| perr("bufferView buffer out of range"))?; + let base = idx(view, "byteOffset").unwrap_or(0) + idx(acc, "byteOffset").unwrap_or(0); + let stride = idx(view, "byteStride").filter(|s| *s > 0).unwrap_or(csize * n); + let mut out = Vec::with_capacity(count * n); + for i in 0..count { + for c in 0..n { + let o = base + i * stride + c * csize; + let s = buf.get(o..o + csize).ok_or_else(|| perr("accessor reads past its buffer"))?; + out.push(match csize { + 1 => s[0] as u32, + 2 => u16::from_le_bytes([s[0], s[1]]) as u32, + _ => u32::from_le_bytes([s[0], s[1], s[2], s[3]]), + }); + } + } + Ok(out) + } +} + +/// Parse a `.gltf` (JSON) or `.glb` file. +pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { + let (json, bin) = if bytes.starts_with(b"glTF") { split_glb(bytes)? } else { (bytes, None) }; + let j: Value = serde_json::from_slice(json).map_err(|e| perr(format!("glTF JSON: {e}")))?; + let ver = j.get("asset").and_then(|a| a.get("version")).and_then(Value::as_str).unwrap_or(""); + if !ver.starts_with('2') { + return Err(ModelError::Unsupported(format!("glTF version `{ver}` (2.0 required)"))); + } + for ext in arr(&j, "extensionsRequired") { + let e = ext.as_str().unwrap_or(""); + if !matches!(e, "KHR_materials_unlit" | "KHR_materials_emissive_strength") { + return Err(ModelError::Unsupported(format!("required extension {e}"))); + } + } + let mut buffers = vec![]; + for (i, b) in arr(&j, "buffers").iter().enumerate() { + let data = match b.get("uri").and_then(Value::as_str) { + Some(u) => load_uri(u, resolve)?, + None if i == 0 => bin.map(<[u8]>::to_vec).ok_or_else(|| perr("buffer 0 has no uri and the file has no BIN chunk"))?, + None => vec![], + }; + buffers.push(data); + } + let d = Doc { j: &j, buffers }; + + // Images → textures (decoded once per image; samplers set the wrap modes). + let mut images: Vec>> = vec![]; + for im in arr(&j, "images") { + let data = if let Some(u) = im.get("uri").and_then(Value::as_str) { + load_uri(u, resolve).ok() + } else if let Some(bv) = idx(im, "bufferView") { + arr(&j, "bufferViews").get(bv).and_then(|v| { + let b = d.buffers.get(idx(v, "buffer").unwrap_or(0))?; + let o = idx(v, "byteOffset").unwrap_or(0); + b.get(o..o + idx(v, "byteLength").unwrap_or(0)).map(<[u8]>::to_vec) + }) + } else { + None + }; + images.push(data.and_then(|b| Texture::decode(&b).ok()).map(Arc::new)); + } + let wrap = |v: u64| match v { + 33071 => Wrap::Clamp, + 33648 => Wrap::Mirror, + _ => Wrap::Repeat, + }; + let mut textures: Vec> = vec![]; + let mut tex_map: Vec> = vec![]; + for t in arr(&j, "textures") { + let Some(img) = idx(t, "source").and_then(|s| images.get(s).cloned().flatten()) else { + tex_map.push(None); + continue; + }; + let samp = idx(t, "sampler").and_then(|s| arr(&j, "samplers").get(s)); + let ws = samp.and_then(|s| s.get("wrapS")).and_then(Value::as_u64).unwrap_or(10497); + let wt = samp.and_then(|s| s.get("wrapT")).and_then(Value::as_u64).unwrap_or(10497); + let w = [wrap(ws), wrap(wt)]; + let tex = if img.wrap == w { img } else { Arc::new(Texture { wrap: w, ..(*img).clone() }) }; + tex_map.push(Some(textures.len())); + textures.push(tex); + } + let tref = |v: Option<&Value>| -> Option { + let v = v?; + let t = tex_map.get(idx(v, "index")?).copied().flatten()?; + Some(TexRef { texture: t, uv_set: idx(v, "texCoord").unwrap_or(0) as u32 }) + }; + + let mut materials = vec![]; + for m in arr(&j, "materials") { + let pbr = m.get("pbrMetallicRoughness").cloned().unwrap_or(Value::Null); + let bc = nums(&pbr, "baseColorFactor", [1.0; 4]); + let ext = m.get("extensions"); + let strength = ext.and_then(|e| e.get("KHR_materials_emissive_strength")).map_or(1.0, |e| num(e, "emissiveStrength", 1.0)); + let em = nums(m, "emissiveFactor", [0.0; 3]); + materials.push(Material { + name: m.get("name").and_then(Value::as_str).unwrap_or("").to_string(), + base_color: bc.map(|v| v as f32), + base_color_tex: tref(pbr.get("baseColorTexture")), + metallic: num(&pbr, "metallicFactor", 1.0) as f32, + roughness: num(&pbr, "roughnessFactor", 1.0) as f32, + metallic_roughness_tex: tref(pbr.get("metallicRoughnessTexture")), + normal_tex: tref(m.get("normalTexture")), + normal_scale: m.get("normalTexture").map_or(1.0, |n| num(n, "scale", 1.0)) as f32, + occlusion_tex: tref(m.get("occlusionTexture")), + occlusion_strength: m.get("occlusionTexture").map_or(1.0, |n| num(n, "strength", 1.0)) as f32, + emissive: [(em[0] * strength) as f32, (em[1] * strength) as f32, (em[2] * strength) as f32], + emissive_tex: tref(m.get("emissiveTexture")), + alpha_mode: match m.get("alphaMode").and_then(Value::as_str) { + Some("MASK") => AlphaMode::Mask, + Some("BLEND") => AlphaMode::Blend, + _ => AlphaMode::Opaque, + }, + alpha_cutoff: num(m, "alphaCutoff", 0.5) as f32, + double_sided: m.get("doubleSided").and_then(Value::as_bool).unwrap_or(false), + unlit: ext.and_then(|e| e.get("KHR_materials_unlit")).is_some(), + }); + } + + let mut meshes = vec![]; + for m in arr(&j, "meshes") { + let mut mesh = Mesh { name: m.get("name").and_then(Value::as_str).unwrap_or("").to_string(), primitives: vec![] }; + for p in arr(m, "primitives") { + if let Some(prim) = primitive(&d, p, &materials)? { + mesh.primitives.push(prim); + } + } + meshes.push(mesh); + } + + let mut nodes = vec![]; + for n in arr(&j, "nodes") { + let matrix = n.get("matrix").and_then(Value::as_array).filter(|a| a.len() == 16).map(|a| { + // Column-major in the file. + let v: Vec = a.iter().map(|x| x.as_f64().unwrap_or(0.0)).collect(); + let mut m = [[0.0; 4]; 4]; + for c in 0..4 { + for r in 0..4 { + m[r][c] = v[c * 4 + r]; + } + } + Mat4(m) + }); + nodes.push(Node { + name: n.get("name").and_then(Value::as_str).unwrap_or("").to_string(), + translation: nums(n, "translation", [0.0; 3]), + rotation: nums(n, "rotation", [0.0, 0.0, 0.0, 1.0]), + scale: nums(n, "scale", [1.0; 3]), + matrix, + mesh: idx(n, "mesh").filter(|&m| m < meshes.len()), + skin: idx(n, "skin"), + children: arr(n, "children").iter().filter_map(Value::as_u64).map(|c| c as usize).collect(), + }); + } + + let scene = idx(&j, "scene").unwrap_or(0); + let roots: Vec = match arr(&j, "scenes").get(scene) { + Some(s) => arr(s, "nodes").iter().filter_map(Value::as_u64).map(|c| c as usize).collect(), + None => { + // No scene: every node that is nobody's child. + let mut child = vec![false; nodes.len()]; + for n in &nodes { + for &c in &n.children { + if c < child.len() { + child[c] = true; + } + } + } + (0..nodes.len()).filter(|&i| !child[i]).collect() + } + }; + + let mut skins = vec![]; + for s in arr(&j, "skins") { + let joints: Vec = arr(s, "joints").iter().filter_map(Value::as_u64).map(|c| c as usize).collect(); + let inverse_bind = match idx(s, "inverseBindMatrices") { + Some(a) => { + let (f, _) = d.floats(a)?; + f.chunks_exact(16) + .map(|v| { + let mut m = [[0.0; 4]; 4]; + for c in 0..4 { + for r in 0..4 { + m[r][c] = v[c * 4 + r] as f64; + } + } + Mat4(m) + }) + .collect() + } + None => vec![Mat4::IDENTITY; joints.len()], + }; + skins.push(Skin { joints, inverse_bind }); + } + + let mut animations = vec![]; + for a in arr(&j, "animations") { + let samplers = arr(a, "samplers"); + let mut anim = Animation { name: a.get("name").and_then(Value::as_str).unwrap_or("").to_string(), channels: vec![] }; + for c in arr(a, "channels") { + let Some(t) = c.get("target") else { continue }; + let Some(node) = idx(t, "node") else { continue }; + let path = match t.get("path").and_then(Value::as_str) { + Some("translation") => ChannelPath::Translation, + Some("rotation") => ChannelPath::Rotation, + Some("scale") => ChannelPath::Scale, + _ => continue, + }; + let Some(s) = idx(c, "sampler").and_then(|s| samplers.get(s)) else { continue }; + let interp = match s.get("interpolation").and_then(Value::as_str) { + Some("STEP") => Interp::Step, + Some("CUBICSPLINE") => Interp::CubicSpline, + _ => Interp::Linear, + }; + let (Some(input), Some(output)) = (idx(s, "input"), idx(s, "output")) else { continue }; + let (times, _) = d.floats(input)?; + let (values, _) = d.floats(output)?; + anim.channels.push(Channel { node, path, interp, times, values }); + } + animations.push(anim); + } + + Ok(Model { meshes, nodes, roots, materials, textures, skins, animations }) +} + +fn primitive(d: &Doc, p: &Value, materials: &[Material]) -> Result> { + let mode = idx(p, "mode").unwrap_or(4); + if !matches!(mode, 4..=6) { + return Ok(None); + } + let attrs = p.get("attributes").ok_or_else(|| perr("primitive without attributes"))?; + let Some(pa) = idx(attrs, "POSITION") else { return Ok(None) }; + let (pos, _) = d.floats(pa)?; + let positions: Vec<[f32; 3]> = pos.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect(); + let n = positions.len(); + let vec3s = |a: Option| -> Result> { + Ok(match a { + Some(a) => d.floats(a)?.0.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect(), + None => vec![], + }) + }; + let mut normals = vec3s(idx(attrs, "NORMAL"))?; + let uvs: Vec<[f32; 2]> = match idx(attrs, "TEXCOORD_0") { + Some(a) => d.floats(a)?.0.chunks_exact(2).map(|c| [c[0], c[1]]).collect(), + None => vec![], + }; + let tangents: Vec<[f32; 4]> = match idx(attrs, "TANGENT") { + Some(a) => d.floats(a)?.0.chunks_exact(4).map(|c| [c[0], c[1], c[2], c[3]]).collect(), + None => vec![], + }; + let joints: Vec<[u16; 4]> = match idx(attrs, "JOINTS_0") { + Some(a) => d.ints(a)?.chunks_exact(4).map(|c| [c[0] as u16, c[1] as u16, c[2] as u16, c[3] as u16]).collect(), + None => vec![], + }; + let weights: Vec<[f32; 4]> = match idx(attrs, "WEIGHTS_0") { + Some(a) => d.floats(a)?.0.chunks_exact(4).map(|c| [c[0], c[1], c[2], c[3]]).collect(), + None => vec![], + }; + let raw: Vec = match idx(p, "indices") { + Some(a) => d.ints(a)?, + None => (0..n as u32).collect(), + }; + if raw.iter().any(|&i| i as usize >= n) { + return Err(perr("index out of range")); + } + let mut indices = Vec::with_capacity(raw.len()); + match mode { + 5 => { + for i in 2..raw.len() { + let (a, b, c) = (raw[i - 2], raw[i - 1], raw[i]); + if i % 2 == 0 { indices.extend([a, b, c]) } else { indices.extend([b, a, c]) } + } + } + 6 => { + for i in 2..raw.len() { + indices.extend([raw[0], raw[i - 1], raw[i]]); + } + } + _ => indices.extend(raw.chunks_exact(3).flatten()), + } + if normals.len() != n { + normals.clear(); + } + let material = idx(p, "material").filter(|&m| m < materials.len()); + let skinned = joints.len() == n && weights.len() == n; + let mut prim = Primitive { + positions, + normals, + uvs: if uvs.len() == n { uvs } else { vec![] }, + tangents: if tangents.len() == n { tangents } else { vec![] }, + joints: if skinned { joints } else { vec![] }, + weights: if skinned { weights } else { vec![] }, + indices, + material, + }; + if prim.normals.is_empty() { + prim.flat_normals(); + prim.tangents.clear(); + } + let needs_tangents = material.is_some_and(|m| materials[m].normal_tex.is_some()); + if needs_tangents && prim.tangents.is_empty() { + prim.compute_tangents(); + } + Ok(Some(prim)) +} diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs new file mode 100644 index 00000000..290e5500 --- /dev/null +++ b/crates/model/src/lib.rs @@ -0,0 +1,691 @@ +//! 3D models for the Advanced 3D renderer. +//! +//! - [`gltf`]: glTF 2.0 import (`.gltf` with external or embedded buffers, `.glb`), written from +//! the Khronos glTF 2.0 specification: meshes, PBR metallic-roughness materials (base colour, +//! metallic-roughness, normal, occlusion and emissive maps), the node hierarchy, skins and +//! animations. +//! - [`obj`]: Wavefront OBJ geometry with MTL materials (mapped onto the metallic-roughness model). +//! - [`prim`]: parametric primitives (cube, sphere, plane, torus, cone, cylinder). +//! - [`extrude`]: extruded, bevelled meshes from 2D outlines (text and shape layers), with +//! [`triangulate`] for the caps. +//! +//! Models are in their own units with +Y up (glTF's convention); the renderer maps them into +//! After Effects' Y-down pixel space. + +pub mod extrude; +pub mod gltf; +pub mod obj; +pub mod prim; +pub mod triangulate; + +use std::sync::Arc; + +pub use effectcraft_geom::{Mat4, Vec3, vec3}; + +#[derive(Debug, thiserror::Error)] +pub enum ModelError { + #[error("{0}")] + Parse(String), + #[error("missing resource `{0}`")] + Missing(String), + #[error("unsupported: {0}")] + Unsupported(String), +} + +pub type Result = std::result::Result; + +pub(crate) fn perr(s: impl Into) -> ModelError { + ModelError::Parse(s.into()) +} + +/// Texture coordinate wrapping (glTF sampler wrap modes). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Wrap { + #[default] + Repeat, + Clamp, + Mirror, +} + +/// A decoded texture: straight RGBA, 0..1, as stored in the file (colour textures are still +/// sRGB-encoded; the renderer linearises them). +#[derive(Clone, Debug, PartialEq)] +pub struct Texture { + pub width: u32, + pub height: u32, + pub data: Vec<[f32; 4]>, + pub wrap: [Wrap; 2], +} + +impl Texture { + pub fn solid(c: [f32; 4]) -> Texture { + Texture { width: 1, height: 1, data: vec![c], wrap: [Wrap::Repeat; 2] } + } + /// Decode a PNG or JPEG. + pub fn decode(bytes: &[u8]) -> Result { + let img = image::load_from_memory(bytes).map_err(|e| perr(format!("image: {e}")))?.to_rgba32f(); + let (w, h) = (img.width(), img.height()); + let data = img.pixels().map(|p| p.0).collect(); + Ok(Texture { width: w, height: h, data, wrap: [Wrap::Repeat; 2] }) + } +} + +/// How a material's alpha is used (glTF `alphaMode`). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum AlphaMode { + #[default] + Opaque, + Mask, + Blend, +} + +/// A texture reference of a material. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct TexRef { + pub texture: usize, + /// TEXCOORD set (only set 0 is imported; others fall back to set 0). + pub uv_set: u32, +} + +/// A metallic-roughness PBR material (glTF 2.0 core material model). +#[derive(Clone, Debug, PartialEq)] +pub struct Material { + pub name: String, + /// Linear RGBA factor. + pub base_color: [f32; 4], + pub base_color_tex: Option, + pub metallic: f32, + pub roughness: f32, + /// Roughness in G, metalness in B. + pub metallic_roughness_tex: Option, + pub normal_tex: Option, + pub normal_scale: f32, + /// Occlusion in R. + pub occlusion_tex: Option, + pub occlusion_strength: f32, + /// Linear emissive factor (× KHR_materials_emissive_strength). + pub emissive: [f32; 3], + pub emissive_tex: Option, + pub alpha_mode: AlphaMode, + pub alpha_cutoff: f32, + pub double_sided: bool, + /// KHR_materials_unlit. + pub unlit: bool, +} + +impl Default for Material { + fn default() -> Self { + Material { + name: String::new(), + base_color: [1.0; 4], + base_color_tex: None, + metallic: 1.0, + roughness: 1.0, + metallic_roughness_tex: None, + normal_tex: None, + normal_scale: 1.0, + occlusion_tex: None, + occlusion_strength: 1.0, + emissive: [0.0; 3], + emissive_tex: None, + alpha_mode: AlphaMode::Opaque, + alpha_cutoff: 0.5, + double_sided: false, + unlit: false, + } + } +} + +impl Material { + /// A plain dielectric material of a colour (primitives, OBJ without MTL). + pub fn plain(base: [f32; 4], metallic: f32, roughness: f32) -> Material { + Material { base_color: base, metallic, roughness, ..Material::default() } + } +} + +/// Triangles with one material. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Primitive { + pub positions: Vec<[f32; 3]>, + pub normals: Vec<[f32; 3]>, + /// TEXCOORD_0 (empty when the primitive has none). + pub uvs: Vec<[f32; 2]>, + /// Tangents (xyz, w = bitangent sign); empty when not needed. + pub tangents: Vec<[f32; 4]>, + /// JOINTS_0 / WEIGHTS_0 (skinned meshes; empty otherwise). + pub joints: Vec<[u16; 4]>, + pub weights: Vec<[f32; 4]>, + /// Triangle list. + pub indices: Vec, + pub material: Option, +} + +impl Primitive { + pub fn triangle_count(&self) -> usize { + self.indices.len() / 3 + } + /// Smooth vertex normals from the triangles (area weighted), for meshes without normals. + pub fn compute_normals(&mut self) { + let mut n = vec![[0.0f32; 3]; self.positions.len()]; + for t in self.indices.chunks_exact(3) { + let (a, b, c) = (self.positions[t[0] as usize], self.positions[t[1] as usize], self.positions[t[2] as usize]); + let f = cross(sub(b, a), sub(c, a)); + for &i in t { + let v = &mut n[i as usize]; + v[0] += f[0]; + v[1] += f[1]; + v[2] += f[2]; + } + } + self.normals = n.into_iter().map(|v| normalize(v).unwrap_or([0.0, 0.0, 1.0])).collect(); + } + /// Split every triangle into its own vertices with a face normal (glTF: flat normals when a + /// primitive has none). + pub fn flat_normals(&mut self) { + let mut p = Primitive { material: self.material, ..Default::default() }; + for t in self.indices.chunks_exact(3) { + let (a, b, c) = (self.positions[t[0] as usize], self.positions[t[1] as usize], self.positions[t[2] as usize]); + let f = normalize(cross(sub(b, a), sub(c, a))).unwrap_or([0.0, 0.0, 1.0]); + for &i in t { + let i = i as usize; + p.indices.push(p.positions.len() as u32); + p.positions.push(self.positions[i]); + p.normals.push(f); + if !self.uvs.is_empty() { + p.uvs.push(self.uvs[i]); + } + if !self.joints.is_empty() { + p.joints.push(self.joints[i]); + p.weights.push(self.weights[i]); + } + } + } + *self = p; + } + /// Per-vertex tangents from the UV layout (accumulated per triangle, Gram-Schmidt + /// orthogonalised against the normal, handedness in w). + pub fn compute_tangents(&mut self) { + let n = self.positions.len(); + if self.uvs.len() != n || self.normals.len() != n { + return; + } + let mut tan = vec![[0.0f32; 3]; n]; + let mut bit = vec![[0.0f32; 3]; n]; + for t in self.indices.chunks_exact(3) { + let (i0, i1, i2) = (t[0] as usize, t[1] as usize, t[2] as usize); + let (p0, p1, p2) = (self.positions[i0], self.positions[i1], self.positions[i2]); + let (w0, w1, w2) = (self.uvs[i0], self.uvs[i1], self.uvs[i2]); + let e1 = sub(p1, p0); + let e2 = sub(p2, p0); + let (du1, dv1, du2, dv2) = (w1[0] - w0[0], w1[1] - w0[1], w2[0] - w0[0], w2[1] - w0[1]); + let det = du1 * dv2 - du2 * dv1; + if det.abs() < 1e-12 { + continue; + } + let r = 1.0 / det; + let sdir = [(e1[0] * dv2 - e2[0] * dv1) * r, (e1[1] * dv2 - e2[1] * dv1) * r, (e1[2] * dv2 - e2[2] * dv1) * r]; + let tdir = [(e2[0] * du1 - e1[0] * du2) * r, (e2[1] * du1 - e1[1] * du2) * r, (e2[2] * du1 - e1[2] * du2) * r]; + for i in [i0, i1, i2] { + for k in 0..3 { + tan[i][k] += sdir[k]; + bit[i][k] += tdir[k]; + } + } + } + self.tangents = (0..n) + .map(|i| { + let nn = self.normals[i]; + let t = tan[i]; + let d = dot(nn, t); + let o = [t[0] - nn[0] * d, t[1] - nn[1] * d, t[2] - nn[2] * d]; + let o = normalize(o).unwrap_or_else(|| any_perpendicular(nn)); + let w = if dot(cross(nn, o), bit[i]) < 0.0 { -1.0 } else { 1.0 }; + [o[0], o[1], o[2], w] + }) + .collect(); + } + /// Axis-aligned bounds of the positions. + pub fn bounds(&self) -> Option<([f32; 3], [f32; 3])> { + let mut it = self.positions.iter(); + let f = *it.next()?; + let (mut lo, mut hi) = (f, f); + for p in it { + for k in 0..3 { + lo[k] = lo[k].min(p[k]); + hi[k] = hi[k].max(p[k]); + } + } + Some((lo, hi)) + } +} + +pub(crate) fn sub(a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + [a[0] - b[0], a[1] - b[1], a[2] - b[2]] +} +pub(crate) fn cross(a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]] +} +pub(crate) fn dot(a: [f32; 3], b: [f32; 3]) -> f32 { + a[0] * b[0] + a[1] * b[1] + a[2] * b[2] +} +pub(crate) fn normalize(a: [f32; 3]) -> Option<[f32; 3]> { + let l = dot(a, a).sqrt(); + (l > 1e-20 && l.is_finite()).then(|| [a[0] / l, a[1] / l, a[2] / l]) +} +fn any_perpendicular(n: [f32; 3]) -> [f32; 3] { + let a = if n[0].abs() < 0.9 { [1.0, 0.0, 0.0] } else { [0.0, 1.0, 0.0] }; + normalize(cross(n, a)).unwrap_or([1.0, 0.0, 0.0]) +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Mesh { + pub name: String, + pub primitives: Vec, +} + +/// A node of the scene hierarchy: local transform (TRS or a matrix), an optional mesh and skin. +#[derive(Clone, Debug, PartialEq)] +pub struct Node { + pub name: String, + pub translation: [f64; 3], + /// Unit quaternion (x, y, z, w). + pub rotation: [f64; 4], + pub scale: [f64; 3], + /// A fixed local matrix (nodes given by `matrix` are not animated). + pub matrix: Option, + pub mesh: Option, + pub skin: Option, + pub children: Vec, +} + +impl Default for Node { + fn default() -> Self { + Node { + name: String::new(), + translation: [0.0; 3], + rotation: [0.0, 0.0, 0.0, 1.0], + scale: [1.0; 3], + matrix: None, + mesh: None, + skin: None, + children: vec![], + } + } +} + +impl Node { + pub fn local(&self) -> Mat4 { + self.matrix.unwrap_or_else(|| trs(self.translation, self.rotation, self.scale)) + } +} + +/// Translation × rotation (quaternion x, y, z, w) × scale. +pub fn trs(t: [f64; 3], q: [f64; 4], s: [f64; 3]) -> Mat4 { + let r = quat_matrix(q); + let mut m = r; + for i in 0..3 { + for j in 0..3 { + m.0[i][j] *= s[j]; + } + m.0[i][3] = t[i]; + } + m +} + +/// Rotation matrix of a quaternion (x, y, z, w); normalised first. +pub fn quat_matrix(q: [f64; 4]) -> Mat4 { + let l = (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]).sqrt(); + let [x, y, z, w] = if l > 1e-12 { [q[0] / l, q[1] / l, q[2] / l, q[3] / l] } else { [0.0, 0.0, 0.0, 1.0] }; + Mat4([ + [1.0 - 2.0 * (y * y + z * z), 2.0 * (x * y - z * w), 2.0 * (x * z + y * w), 0.0], + [2.0 * (x * y + z * w), 1.0 - 2.0 * (x * x + z * z), 2.0 * (y * z - x * w), 0.0], + [2.0 * (x * z - y * w), 2.0 * (y * z + x * w), 1.0 - 2.0 * (x * x + y * y), 0.0], + [0.0, 0.0, 0.0, 1.0], + ]) +} + +/// Spherical linear interpolation of unit quaternions (shortest path). +pub fn slerp(a: [f64; 4], b: [f64; 4], t: f64) -> [f64; 4] { + let mut d = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; + let mut b = b; + if d < 0.0 { + d = -d; + b = [-b[0], -b[1], -b[2], -b[3]]; + } + let (ka, kb) = if d > 0.9995 { + (1.0 - t, t) + } else { + let th = d.clamp(-1.0, 1.0).acos(); + let s = th.sin(); + (((1.0 - t) * th).sin() / s, (t * th).sin() / s) + }; + let q = [a[0] * ka + b[0] * kb, a[1] * ka + b[1] * kb, a[2] * ka + b[2] * kb, a[3] * ka + b[3] * kb]; + let l = (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]).sqrt().max(1e-12); + [q[0] / l, q[1] / l, q[2] / l, q[3] / l] +} + +/// A skin: joint nodes and their inverse bind matrices. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Skin { + pub joints: Vec, + pub inverse_bind: Vec, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ChannelPath { + Translation, + Rotation, + Scale, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Interp { + Step, + Linear, + CubicSpline, +} + +/// One animated node property. +#[derive(Clone, Debug, PartialEq)] +pub struct Channel { + pub node: usize, + pub path: ChannelPath, + pub interp: Interp, + /// Key times (seconds). + pub times: Vec, + /// Values, `width` floats per key (cubic spline: in-tangent, value, out-tangent per key). + pub values: Vec, +} + +impl Channel { + fn width(&self) -> usize { + match self.path { + ChannelPath::Rotation => 4, + _ => 3, + } + } + /// The value at time `t` (seconds), clamped to the key range. + pub fn sample(&self, t: f64) -> Option<[f64; 4]> { + let n = self.times.len(); + let w = self.width(); + let stride = if self.interp == Interp::CubicSpline { 3 * w } else { w }; + let off = if self.interp == Interp::CubicSpline { w } else { 0 }; + if n == 0 || self.values.len() < n * stride { + return None; + } + let get = |k: usize, o: usize| -> [f64; 4] { + let mut v = [0.0; 4]; + for i in 0..w { + v[i] = self.values[k * stride + o + i] as f64; + } + v + }; + let t0 = self.times[0] as f64; + if t <= t0 || n == 1 { + return Some(get(0, off)); + } + if t >= self.times[n - 1] as f64 { + return Some(get(n - 1, off)); + } + let k = self.times.partition_point(|&x| (x as f64) <= t).saturating_sub(1).min(n - 2); + let (ta, tb) = (self.times[k] as f64, self.times[k + 1] as f64); + let dt = (tb - ta).max(1e-9); + let u = ((t - ta) / dt).clamp(0.0, 1.0); + let (a, b) = (get(k, off), get(k + 1, off)); + let v = match self.interp { + Interp::Step => a, + Interp::Linear => { + if self.path == ChannelPath::Rotation { + slerp(a, b, u) + } else { + let mut v = [0.0; 4]; + for i in 0..w { + v[i] = a[i] + (b[i] - a[i]) * u; + } + v + } + } + Interp::CubicSpline => { + // Hermite spline with tangents scaled by the key interval (glTF Appendix C). + let out_a = get(k, 2 * w); + let in_b = get(k + 1, 0); + let (u2, u3) = (u * u, u * u * u); + let h00 = 2.0 * u3 - 3.0 * u2 + 1.0; + let h10 = u3 - 2.0 * u2 + u; + let h01 = -2.0 * u3 + 3.0 * u2; + let h11 = u3 - u2; + let mut v = [0.0; 4]; + for i in 0..w { + v[i] = h00 * a[i] + h10 * dt * out_a[i] + h01 * b[i] + h11 * dt * in_b[i]; + } + if self.path == ChannelPath::Rotation { + let l = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]).sqrt().max(1e-12); + for c in v.iter_mut() { + *c /= l; + } + } + v + } + }; + Some(v) + } +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Animation { + pub name: String, + pub channels: Vec, +} + +impl Animation { + /// Length in seconds (the last key time of any channel). + pub fn duration(&self) -> f64 { + self.channels.iter().filter_map(|c| c.times.last()).fold(0.0f64, |a, &b| a.max(b as f64)) + } +} + +/// An imported model (one glTF scene, one OBJ file, or a primitive). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Model { + pub meshes: Vec, + pub nodes: Vec, + /// Root nodes of the displayed scene. + pub roots: Vec, + pub materials: Vec, + pub textures: Vec>, + pub skins: Vec, + pub animations: Vec, +} + +/// One mesh placed in model space by [`Model::instances`]. +#[derive(Clone, Debug)] +pub struct Instance<'m> { + pub mesh: &'m Mesh, + /// Node → model space. + pub world: Mat4, + /// Skinned meshes: joint matrices (model space, `joint_world × inverse_bind`). + pub joints: Option>, +} + +impl Model { + /// A model of one mesh at the root. + pub fn single(mesh: Mesh, materials: Vec) -> Model { + Model { meshes: vec![mesh], nodes: vec![Node { mesh: Some(0), ..Node::default() }], roots: vec![0], materials, ..Model::default() } + } + + /// Local node matrices at animation `clip` time `t` (seconds); `None` = rest pose. + pub fn local_pose(&self, clip: Option, t: f64) -> Vec { + let mut trs_v: Vec<([f64; 3], [f64; 4], [f64; 3])> = self.nodes.iter().map(|n| (n.translation, n.rotation, n.scale)).collect(); + if let Some(a) = clip.and_then(|c| self.animations.get(c)) { + for ch in &a.channels { + let (Some(v), Some(e)) = (ch.sample(t), trs_v.get_mut(ch.node)) else { continue }; + match ch.path { + ChannelPath::Translation => e.0 = [v[0], v[1], v[2]], + ChannelPath::Rotation => e.1 = v, + ChannelPath::Scale => e.2 = [v[0], v[1], v[2]], + } + } + } + self.nodes.iter().zip(trs_v).map(|(n, (t, r, s))| n.matrix.unwrap_or_else(|| trs(t, r, s))).collect() + } + + /// Model-space matrices of every node (hierarchy applied) for a local pose. + pub fn world_pose(&self, local: &[Mat4]) -> Vec { + let mut world = vec![Mat4::IDENTITY; self.nodes.len()]; + let mut seen = vec![false; self.nodes.len()]; + let mut stack: Vec<(usize, Mat4)> = self.roots.iter().map(|&r| (r, Mat4::IDENTITY)).collect(); + while let Some((i, parent)) = stack.pop() { + if i >= self.nodes.len() || seen[i] { + continue; + } + seen[i] = true; + let w = parent * local[i]; + world[i] = w; + for &c in &self.nodes[i].children { + stack.push((c, w)); + } + } + world + } + + /// The meshes to draw at animation `clip` time `t`. + pub fn instances(&self, clip: Option, t: f64) -> Vec> { + let local = self.local_pose(clip, t); + let world = self.world_pose(&local); + let mut seen = vec![false; self.nodes.len()]; + let mut stack: Vec = self.roots.clone(); + let mut out = vec![]; + while let Some(i) = stack.pop() { + if i >= self.nodes.len() || seen[i] { + continue; + } + seen[i] = true; + let n = &self.nodes[i]; + if let Some(m) = n.mesh.and_then(|m| self.meshes.get(m)) { + let joints = n.skin.and_then(|s| self.skins.get(s)).map(|s| { + s.joints + .iter() + .enumerate() + .map(|(k, &j)| world.get(j).copied().unwrap_or(Mat4::IDENTITY) * s.inverse_bind.get(k).copied().unwrap_or(Mat4::IDENTITY)) + .collect() + }); + // Skinned vertices are placed by their joints alone (glTF: the node transform of + // a skinned mesh is ignored). + let w = if joints.is_some() { Mat4::IDENTITY } else { world[i] }; + out.push(Instance { mesh: m, world: w, joints }); + } + stack.extend(n.children.iter().rev()); + } + out + } + + /// Model-space bounds of the rest pose (None for an empty model). + pub fn bounds(&self) -> Option<([f64; 3], [f64; 3])> { + let mut lo = [f64::INFINITY; 3]; + let mut hi = [f64::NEG_INFINITY; 3]; + for inst in self.instances(None, 0.0) { + for p in &inst.mesh.primitives { + for (k, v) in p.positions.iter().enumerate() { + let q = skin_point(&inst, p, k, *v); + for c in 0..3 { + lo[c] = lo[c].min(q[c]); + hi[c] = hi[c].max(q[c]); + } + } + } + } + lo[0].is_finite().then_some((lo, hi)) + } + + pub fn vertex_count(&self) -> usize { + self.meshes.iter().flat_map(|m| &m.primitives).map(|p| p.positions.len()).sum() + } + pub fn triangle_count(&self) -> usize { + self.meshes.iter().flat_map(|m| &m.primitives).map(|p| p.triangle_count()).sum() + } +} + +/// The skinning (or node) matrix of vertex `k` of an instance's primitive. +pub fn vertex_matrix(inst: &Instance, p: &Primitive, k: usize) -> Mat4 { + let Some(j) = &inst.joints else { return inst.world }; + let (Some(ji), Some(wi)) = (p.joints.get(k), p.weights.get(k)) else { return inst.world }; + let mut m = [[0.0; 4]; 4]; + let mut total = 0.0; + for c in 0..4 { + let w = wi[c] as f64; + if w == 0.0 { + continue; + } + let Some(jm) = j.get(ji[c] as usize) else { continue }; + total += w; + for r in 0..4 { + for q in 0..4 { + m[r][q] += jm.0[r][q] * w; + } + } + } + if total <= 1e-9 { inst.world } else { Mat4(m) } +} + +/// A vertex position of an instance in model space. +pub fn skin_point(inst: &Instance, p: &Primitive, k: usize, v: [f32; 3]) -> [f64; 3] { + let q = vertex_matrix(inst, p, k).apply(vec3(v[0] as f64, v[1] as f64, v[2] as f64)); + [q.x, q.y, q.z] +} + +/// Which loader handles a file name (`gltf`, `glb`, `obj`), by extension. +pub fn format_of(path: &str) -> Option<&'static str> { + let ext = path.rsplit('.').next()?.to_ascii_lowercase(); + match ext.as_str() { + "gltf" => Some("gltf"), + "glb" => Some("glb"), + "obj" => Some("obj"), + _ => None, + } +} + +/// Load a model from its main file's bytes; `resolve(uri)` reads sibling resources (buffers, +/// textures, MTL files) by their relative URI. +pub fn load(path: &str, bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { + match format_of(path) { + Some("obj") => obj::parse(bytes, resolve), + Some(_) => gltf::parse(bytes, resolve), + None if bytes.starts_with(b"glTF") => gltf::parse(bytes, resolve), + None => Err(ModelError::Unsupported(format!("{path}: not a glTF or OBJ file"))), + } +} + +/// Summary for footage metadata (Project panel, Interpret Footage). +#[derive(Clone, Debug, Default, PartialEq, serde::Serialize)] +pub struct Summary { + pub meshes: usize, + pub vertices: usize, + pub triangles: usize, + pub materials: usize, + pub textures: usize, + pub animations: Vec<(String, f64)>, + pub skinned: bool, + /// Rest-pose bounds (model units). + pub min: [f64; 3], + pub max: [f64; 3], +} + +impl Model { + pub fn summary(&self) -> Summary { + let (min, max) = self.bounds().unwrap_or(([0.0; 3], [0.0; 3])); + Summary { + meshes: self.meshes.len(), + vertices: self.vertex_count(), + triangles: self.triangle_count(), + materials: self.materials.len(), + textures: self.textures.len(), + animations: self.animations.iter().map(|a| (a.name.clone(), a.duration())).collect(), + skinned: !self.skins.is_empty(), + min, + max, + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/model/src/obj.rs b/crates/model/src/obj.rs new file mode 100644 index 00000000..e6727e73 --- /dev/null +++ b/crates/model/src/obj.rs @@ -0,0 +1,262 @@ +//! Wavefront OBJ geometry and MTL materials. +//! +//! OBJ: `v` (optional vertex colours ignored), `vt`, `vn`, `f` with `v`, `v/vt`, `v//vn`, +//! `v/vt/vn` references (negative = relative), polygons fan-triangulated, `o`/`g` groups, `s` +//! smoothing (normals computed per smoothing state when a face has none), `usemtl`, `mtllib`. +//! +//! MTL materials map onto metallic-roughness: `Kd` → base colour, `d`/`Tr` → alpha, `Ke` → +//! emissive, `Pr`/`Pm` (the PBR extension) → roughness/metallic, otherwise roughness from the +//! Phong exponent `Ns` (`sqrt(2 / (Ns + 2))`) and metallic 0; `map_Kd`, `map_Ke`, +//! `map_Bump`/`bump`/`norm` load base colour, emissive and normal textures. + +use std::collections::HashMap; +use std::sync::Arc; + +use crate::{AlphaMode, Material, Mesh, Model, Primitive, Result, TexRef, Texture, perr}; + +/// Parse MTL text into named materials (textures appended to `textures`). +pub fn parse_mtl(text: &str, resolve: &dyn Fn(&str) -> Option>, textures: &mut Vec>) -> Vec { + let mut out: Vec = vec![]; + let mut loaded: HashMap> = HashMap::new(); + let mut tex = |name: &str, textures: &mut Vec>| -> Option { + // Options (`-bm 1 file.png`): the file name is the last token. + let file = name.split_whitespace().last()?.to_string(); + let t = *loaded.entry(file.clone()).or_insert_with(|| { + let b = resolve(&file)?; + let t = Texture::decode(&b).ok()?; + textures.push(Arc::new(t)); + Some(textures.len() - 1) + }); + t.map(|texture| TexRef { texture, uv_set: 0 }) + }; + let mut ns_seen = false; + let mut pr_seen = false; + for line in text.lines() { + let line = line.trim(); + let (key, rest) = line.split_once(char::is_whitespace).map(|(k, r)| (k, r.trim())).unwrap_or((line, "")); + let f3 = || -> [f32; 3] { + let v: Vec = rest.split_whitespace().filter_map(|x| x.parse().ok()).collect(); + match v.len() { + 0 => [0.0; 3], + 1 | 2 => [v[0]; 3], + _ => [v[0], v[1], v[2]], + } + }; + let f1 = || rest.split_whitespace().next().and_then(|x| x.parse::().ok()); + if key == "newmtl" { + if let Some(m) = out.last_mut() { + finish(m, ns_seen, pr_seen); + } + ns_seen = false; + pr_seen = false; + out.push(Material { name: rest.to_string(), metallic: 0.0, roughness: 0.5, ..Material::default() }); + continue; + } + let Some(m) = out.last_mut() else { continue }; + match key { + "Kd" => { + let c = f3(); + m.base_color = [srgb_to_linear(c[0]), srgb_to_linear(c[1]), srgb_to_linear(c[2]), m.base_color[3]]; + } + "Ke" => m.emissive = f3().map(srgb_to_linear), + "d" => m.base_color[3] = f1().unwrap_or(1.0), + "Tr" => m.base_color[3] = 1.0 - f1().unwrap_or(0.0), + "Ns" => { + let ns = f1().unwrap_or(0.0).max(0.0); + if !pr_seen { + m.roughness = (2.0 / (ns + 2.0)).sqrt(); + } + ns_seen = true; + } + "Pr" => { + m.roughness = f1().unwrap_or(0.5).clamp(0.0, 1.0); + pr_seen = true; + } + "Pm" => m.metallic = f1().unwrap_or(0.0).clamp(0.0, 1.0), + "map_Kd" => m.base_color_tex = tex(rest, textures), + "map_Ke" => { + m.emissive_tex = tex(rest, textures); + if m.emissive == [0.0; 3] { + m.emissive = [1.0; 3]; + } + } + "map_Bump" | "map_bump" | "bump" | "norm" => m.normal_tex = tex(rest, textures), + _ => {} + } + } + if let Some(m) = out.last_mut() { + finish(m, ns_seen, pr_seen); + } + out +} + +fn finish(m: &mut Material, _ns: bool, _pr: bool) { + if m.base_color[3] < 0.999 { + m.alpha_mode = AlphaMode::Blend; + } +} + +/// sRGB-encoded component → linear (MTL colours are display colours). +pub fn srgb_to_linear(c: f32) -> f32 { + if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +struct Key { + v: u32, + t: u32, + n: u32, + /// Smoothing state (faces without normals get computed normals per state). + s: u32, +} + +/// Parse OBJ text (and its MTL libraries through `resolve`). +pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { + let text = String::from_utf8_lossy(bytes); + let mut v: Vec<[f32; 3]> = vec![]; + let mut vt: Vec<[f32; 2]> = vec![]; + let mut vn: Vec<[f32; 3]> = vec![]; + let mut materials: Vec = vec![]; + let mut textures: Vec> = vec![]; + // One primitive per material, in order of first use. + let mut prims: Vec<(Option, Primitive, HashMap, Vec)> = vec![]; + let mut current: Option = None; + let mut smooth = 0u32; + let mut flat_id = 1u32 << 31; + let mut faces = 0usize; + // Join `\` line continuations. + let joined = text.replace("\\\r\n", " ").replace("\\\n", " "); + for (ln, line) in joined.lines().enumerate() { + let line = line.split('#').next().unwrap_or("").trim(); + if line.is_empty() { + continue; + } + let mut it = line.split_whitespace(); + let key = it.next().unwrap_or(""); + let rest: Vec<&str> = it.collect(); + let fl = |i: usize| rest.get(i).and_then(|x| x.parse::().ok()); + match key { + "v" => v.push([fl(0).unwrap_or(0.0), fl(1).unwrap_or(0.0), fl(2).unwrap_or(0.0)]), + "vt" => vt.push([fl(0).unwrap_or(0.0), fl(1).unwrap_or(0.0)]), + "vn" => vn.push([fl(0).unwrap_or(0.0), fl(1).unwrap_or(0.0), fl(2).unwrap_or(1.0)]), + "s" => { + smooth = match rest.first().copied() { + None | Some("off") | Some("0") => 0, + Some(x) => x.parse().unwrap_or(1).max(1), + } + } + "mtllib" => { + let name = line["mtllib".len()..].trim(); + for file in std::iter::once(name).chain(name.split_whitespace()) { + if let Some(b) = resolve(file) { + let mut ms = parse_mtl(&String::from_utf8_lossy(&b), resolve, &mut textures); + materials.append(&mut ms); + break; + } + } + } + "usemtl" => { + let name = line["usemtl".len()..].trim(); + current = materials.iter().position(|m| m.name == name); + if current.is_none() { + // Unknown material: a named grey default. + materials.push(Material { name: name.to_string(), metallic: 0.0, roughness: 0.5, base_color: [0.8, 0.8, 0.8, 1.0], ..Material::default() }); + current = Some(materials.len() - 1); + } + } + "f" => { + if rest.len() < 3 { + continue; + } + faces += 1; + let resolve_i = |s: &str, len: usize| -> Option { + if s.is_empty() { + return None; + } + let i: i64 = s.parse().ok()?; + let k = if i < 0 { len as i64 + i } else { i - 1 }; + (k >= 0 && (k as usize) < len).then_some(k as u32) + }; + let mut refs = vec![]; + for r in &rest { + let mut parts = r.split('/'); + let vi = resolve_i(parts.next().unwrap_or(""), v.len()).ok_or_else(|| perr(format!("line {}: bad vertex index `{r}`", ln + 1)))?; + let ti = parts.next().and_then(|s| resolve_i(s, vt.len())); + let ni = parts.next().and_then(|s| resolve_i(s, vn.len())); + refs.push((vi, ti, ni)); + } + let has_n = refs.iter().all(|r| r.2.is_some()); + // Faces without normals: shared within a smoothing group, unique otherwise. + let s = if has_n { + 0 + } else if smooth == 0 { + flat_id += 1; + flat_id + } else { + smooth + }; + let slot = match prims.iter().position(|p| p.0 == current) { + Some(i) => i, + None => { + prims.push((current, Primitive { material: current, ..Default::default() }, HashMap::new(), vec![])); + prims.len() - 1 + } + }; + let (_, prim, map, need) = &mut prims[slot]; + let mut ids = vec![]; + for (vi, ti, ni) in refs { + let k = Key { v: vi, t: ti.map_or(u32::MAX, |x| x), n: ni.map_or(u32::MAX, |x| x), s }; + let id = *map.entry(k).or_insert_with(|| { + prim.positions.push(v[vi as usize]); + prim.uvs.push(ti.map_or([0.0, 0.0], |t| vt[t as usize])); + prim.normals.push(ni.map_or([0.0; 3], |n| vn[n as usize])); + need.push(ni.is_none()); + (prim.positions.len() - 1) as u32 + }); + ids.push(id); + } + for i in 2..ids.len() { + prim.indices.extend([ids[0], ids[i - 1], ids[i]]); + } + } + _ => {} + } + } + if faces == 0 { + return Err(perr("OBJ file without faces")); + } + let any_uv = !vt.is_empty(); + let mut mesh = Mesh { name: String::new(), primitives: vec![] }; + for (_, mut p, _, need) in prims { + if need.iter().any(|&b| b) { + // Fill missing normals from the triangles (area weighted), keep given ones. + let given = p.normals.clone(); + p.compute_normals(); + for (i, n) in p.normals.iter_mut().enumerate() { + if !need[i] { + *n = given[i]; + } + } + } + if !any_uv { + p.uvs.clear(); + } else { + // OBJ's v axis points up the image; textures are stored top row first. + for uv in p.uvs.iter_mut() { + uv[1] = 1.0 - uv[1]; + } + } + if p.material.is_some_and(|m| materials[m].normal_tex.is_some()) { + p.compute_tangents(); + } + mesh.primitives.push(p); + } + Ok(Model::single(mesh, materials).with_textures(textures)) +} + +impl Model { + fn with_textures(mut self, t: Vec>) -> Model { + self.textures = t; + self + } +} diff --git a/crates/model/src/prim.rs b/crates/model/src/prim.rs new file mode 100644 index 00000000..f04873df --- /dev/null +++ b/crates/model/src/prim.rs @@ -0,0 +1,235 @@ +//! Parametric primitives (Layer ▸ New ▸ Cube/Sphere/Plane/Torus/Cone/Cylinder). +//! +//! Meshes are built in model space (+Y up, counter-clockwise front faces, like glTF), centred +//! on the origin, in pixels, with normals, UVs (0..1 across each face/around each surface) and +//! tangents. + +use std::f32::consts::{PI, TAU}; + +use serde::{Deserialize, Serialize}; + +use crate::{Material, Mesh, Model, Primitive}; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum PrimitiveKind { + #[default] + Cube, + Sphere, + Plane, + Torus, + Cone, + Cylinder, +} + +impl PrimitiveKind { + pub const ALL: [PrimitiveKind; 6] = + [PrimitiveKind::Cube, PrimitiveKind::Sphere, PrimitiveKind::Plane, PrimitiveKind::Torus, PrimitiveKind::Cone, PrimitiveKind::Cylinder]; + pub fn label(self) -> &'static str { + match self { + PrimitiveKind::Cube => "Cube", + PrimitiveKind::Sphere => "Sphere", + PrimitiveKind::Plane => "Plane", + PrimitiveKind::Torus => "Torus", + PrimitiveKind::Cone => "Cone", + PrimitiveKind::Cylinder => "Cylinder", + } + } + pub fn parse(s: &str) -> Option { + PrimitiveKind::ALL.into_iter().find(|k| k.label().eq_ignore_ascii_case(s.trim())) + } +} + +/// Primitive dimensions (pixels) and tessellation. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct PrimParams { + /// Width, height, depth (cube, plane: width × height). + pub size: [f32; 3], + /// Sphere/cone/cylinder radius; torus ring radius. + pub radius: f32, + /// Torus tube radius. + pub tube: f32, + /// Cone/cylinder height. + pub height: f32, + /// Segments around. + pub segments: u32, + /// Sphere rings / torus tube sides. + pub rings: u32, +} + +impl Default for PrimParams { + fn default() -> Self { + PrimParams { size: [300.0; 3], radius: 150.0, tube: 50.0, height: 300.0, segments: 48, rings: 24 } + } +} + +struct B { + p: Primitive, +} + +impl B { + fn new() -> B { + B { p: Primitive { material: Some(0), ..Default::default() } } + } + fn v(&mut self, pos: [f32; 3], n: [f32; 3], uv: [f32; 2]) -> u32 { + self.p.positions.push(pos); + self.p.normals.push(n); + self.p.uvs.push(uv); + (self.p.positions.len() - 1) as u32 + } + fn tri(&mut self, a: u32, b: u32, c: u32) { + self.p.indices.extend([a, b, c]); + } + fn quad(&mut self, a: u32, b: u32, c: u32, d: u32) { + // a-b-c-d counter-clockwise. + self.tri(a, b, c); + self.tri(a, c, d); + } +} + +/// The mesh of a primitive. +pub fn mesh(kind: PrimitiveKind, pr: &PrimParams) -> Primitive { + let seg = pr.segments.clamp(3, 512); + let rings = pr.rings.clamp(2, 512); + let mut b = B::new(); + match kind { + PrimitiveKind::Cube => { + let [w, h, d] = pr.size.map(|v| v.abs() * 0.5); + // (normal, u axis, v axis) per face; v runs down the texture. + let faces: [([f32; 3], [f32; 3], [f32; 3]); 6] = [ + ([0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, -1.0, 0.0]), + ([0.0, 0.0, -1.0], [-1.0, 0.0, 0.0], [0.0, -1.0, 0.0]), + ([1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, -1.0, 0.0]), + ([-1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, -1.0, 0.0]), + ([0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]), + ([0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, -1.0]), + ]; + let half = [w, h, d]; + for (n, u, v) in faces { + let c = |su: f32, sv: f32| -> [f32; 3] { [0, 1, 2].map(|k| (n[k] + u[k] * su + v[k] * sv) * half[k]) }; + let i0 = b.v(c(-1.0, -1.0), n, [0.0, 0.0]); + let i1 = b.v(c(1.0, -1.0), n, [1.0, 0.0]); + let i2 = b.v(c(1.0, 1.0), n, [1.0, 1.0]); + let i3 = b.v(c(-1.0, 1.0), n, [0.0, 1.0]); + // u × v points along -n for these axes (v down): wind so the face is CCW from outside. + b.quad(i0, i3, i2, i1); + } + } + PrimitiveKind::Plane => { + let (w, h) = (pr.size[0].abs() * 0.5, pr.size[1].abs() * 0.5); + let n = [0.0, 0.0, 1.0]; + let i0 = b.v([-w, h, 0.0], n, [0.0, 0.0]); + let i1 = b.v([w, h, 0.0], n, [1.0, 0.0]); + let i2 = b.v([w, -h, 0.0], n, [1.0, 1.0]); + let i3 = b.v([-w, -h, 0.0], n, [0.0, 1.0]); + b.quad(i0, i3, i2, i1); + } + PrimitiveKind::Sphere => { + let r = pr.radius.abs(); + for j in 0..=rings { + let th = PI * j as f32 / rings as f32; + // Exact poles and seam (welded positions). + let (st, ct) = if j == 0 { + (0.0, 1.0) + } else if j == rings { + (0.0, -1.0) + } else { + th.sin_cos() + }; + for i in 0..=seg { + let ph = TAU * (i % seg) as f32 / seg as f32; + let n = [st * ph.sin(), ct, st * ph.cos()]; + b.v([n[0] * r, n[1] * r, n[2] * r], n, [i as f32 / seg as f32, j as f32 / rings as f32]); + } + } + let row = seg + 1; + for j in 0..rings { + for i in 0..seg { + let a = j * row + i; + let (b0, c, d) = (a + row, a + row + 1, a + 1); + if j != 0 { + b.tri(a, b0, d); + } + if j != rings - 1 { + b.tri(d, b0, c); + } + } + } + } + PrimitiveKind::Torus => { + let (rr, tr) = (pr.radius.abs(), pr.tube.abs()); + for j in 0..=rings { + let v = TAU * (j % rings) as f32 / rings as f32; + for i in 0..=seg { + let u = TAU * (i % seg) as f32 / seg as f32; + // Ring in the XZ plane. + let (cu, su, cv, sv) = (u.cos(), u.sin(), v.cos(), v.sin()); + let n = [cv * su, sv, cv * cu]; + let p = [(rr + tr * cv) * su, tr * sv, (rr + tr * cv) * cu]; + b.v(p, n, [i as f32 / seg as f32, j as f32 / rings as f32]); + } + } + let row = seg + 1; + for j in 0..rings { + for i in 0..seg { + let a = j * row + i; + b.quad(a, a + 1, a + row + 1, a + row); + } + } + } + PrimitiveKind::Cylinder | PrimitiveKind::Cone => { + let r = pr.radius.abs(); + let h = pr.height.abs() * 0.5; + let top_r = if kind == PrimitiveKind::Cone { 0.0 } else { r }; + // Side normal tilt for the cone (slope). + let (ny, nk) = { + let s = (r - top_r) / (2.0 * h).max(1e-6); + let l = (1.0 + s * s).sqrt(); + (s / l, 1.0 / l) + }; + let base = b.p.positions.len() as u32; + for i in 0..=seg { + let a = TAU * (i % seg) as f32 / seg as f32; + let (s, c) = a.sin_cos(); + let n = [s * nk, ny, c * nk]; + let u = i as f32 / seg as f32; + b.v([s * top_r, h, c * top_r], n, [u, 0.0]); + b.v([s * r, -h, c * r], n, [u, 1.0]); + } + for i in 0..seg { + let t0 = base + 2 * i; + let (b0, t1, b1) = (t0 + 1, t0 + 2, t0 + 3); + if kind == PrimitiveKind::Cone { + b.tri(t0, b0, b1); + } else { + b.quad(t0, b0, b1, t1); + } + } + let caps: &[(f32, f32)] = if kind == PrimitiveKind::Cone { &[(-1.0, r)] } else { &[(1.0, r), (-1.0, r)] }; + for &(side, rad) in caps { + let n = [0.0, side, 0.0]; + let c = b.v([0.0, h * side, 0.0], n, [0.5, 0.5]); + let first = b.p.positions.len() as u32; + for i in 0..=seg { + let a = TAU * (i % seg) as f32 / seg as f32; + let (s, co) = a.sin_cos(); + b.v([s * rad, h * side, co * rad], n, [0.5 + 0.5 * s, 0.5 - 0.5 * co * side]); + } + for i in 0..seg { + let (p0, p1) = (first + i, first + i + 1); + if side > 0.0 { b.tri(c, p0, p1) } else { b.tri(c, p1, p0) } + } + } + } + } + b.p.compute_tangents(); + b.p +} + +/// A primitive as a one-mesh model with one material. +pub fn model(kind: PrimitiveKind, pr: &PrimParams, material: Material) -> Model { + let mut material = material; + if kind == PrimitiveKind::Plane { + material.double_sided = true; + } + Model::single(Mesh { name: kind.label().to_string(), primitives: vec![mesh(kind, pr)] }, vec![material]) +} diff --git a/crates/model/src/tests.rs b/crates/model/src/tests.rs new file mode 100644 index 00000000..31040b7c --- /dev/null +++ b/crates/model/src/tests.rs @@ -0,0 +1,318 @@ +use std::collections::HashMap; + +use super::extrude::{BevelStyle, ExtrudeParams, Outline, extrude, group_contours}; +use super::prim::{PrimParams, PrimitiveKind, mesh}; +use super::triangulate::{signed_area, triangulate}; +use super::*; + +fn fixture(name: &str) -> Vec { + std::fs::read(format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR"))).unwrap() +} + +fn resolver(name: &str) -> Option> { + std::fs::read(format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR"))).ok() +} + +fn check_quad(m: &Model) { + assert_eq!(m.nodes.len(), 2); + assert_eq!(m.roots, vec![0]); + assert_eq!(m.nodes[0].children, vec![1]); + assert_eq!(m.meshes.len(), 1); + let p = &m.meshes[0].primitives[0]; + assert_eq!(p.positions.len(), 4); + assert_eq!(p.indices, vec![0, 1, 2, 0, 2, 3]); + assert_eq!(p.uvs[0], [0.0, 1.0]); + assert_eq!(p.normals[2], [0.0, 0.0, 1.0]); + let mat = &m.materials[0]; + assert_eq!(mat.name, "Painted"); + assert_eq!(mat.base_color, [1.0, 0.5, 0.25, 1.0]); + assert!((mat.metallic - 0.25).abs() < 1e-6 && (mat.roughness - 0.75).abs() < 1e-6); + assert_eq!(mat.emissive, [0.1, 0.2, 0.3]); + assert!(mat.double_sided); + let t = &m.textures[mat.base_color_tex.unwrap().texture]; + assert_eq!((t.width, t.height), (2, 2)); + assert_eq!(t.data[0], [1.0, 0.0, 0.0, 1.0]); + assert_eq!(t.data[3], [1.0, 1.0, 1.0, 1.0]); + assert_eq!(t.wrap, [Wrap::Clamp, Wrap::Repeat]); + // Hierarchy: scale 2 under a +1 Y translation. + let (lo, hi) = m.bounds().unwrap(); + assert!((lo[0] + 1.0).abs() < 1e-9 && (hi[0] - 1.0).abs() < 1e-9); + assert!((lo[1] - 0.0).abs() < 1e-9 && (hi[1] - 2.0).abs() < 1e-9); + // Animation: root translation (0,1,0) → (2,1,0) over 1 s. + assert_eq!(m.animations.len(), 1); + assert_eq!(m.animations[0].name, "Move"); + assert!((m.animations[0].duration() - 1.0).abs() < 1e-9); + let inst = m.instances(Some(0), 0.5); + assert_eq!(inst.len(), 1); + let c = inst[0].world.apply(vec3(0.0, 0.0, 0.0)); + assert!((c.x - 1.0).abs() < 1e-6 && (c.y - 1.0).abs() < 1e-6, "{c:?}"); +} + +#[test] +fn gltf_with_data_uris() { + let m = gltf::parse(&fixture("quad.gltf"), &|_| None).unwrap(); + check_quad(&m); +} + +#[test] +fn glb_matches_gltf() { + let m = load("quad.glb", &fixture("quad.glb"), &resolver).unwrap(); + check_quad(&m); + let g = gltf::parse(&fixture("quad.gltf"), &|_| None).unwrap(); + assert_eq!(m.meshes, g.meshes); + assert_eq!(m.materials, g.materials); +} + +#[test] +fn gltf_errors_are_reported() { + assert!(gltf::parse(b"{\"asset\":{\"version\":\"1.0\"}}", &|_| None).is_err()); + assert!(gltf::parse(b"not json", &|_| None).is_err()); + let missing = br#"{"asset":{"version":"2.0"},"buffers":[{"uri":"nowhere.bin","byteLength":4}]}"#; + assert!(matches!(gltf::parse(missing, &|_| None), Err(ModelError::Missing(_)))); + assert!(gltf::split_glb(b"glTF\x01\0\0\0").is_err()); +} + +#[test] +fn base64_and_uri_decoding() { + assert_eq!(gltf::base64_decode("aGVsbG8=").unwrap(), b"hello"); + assert_eq!(gltf::base64_decode("aGVsbG8").unwrap(), b"hello"); + assert_eq!(gltf::uri_decode("my%20file.bin"), "my file.bin"); +} + +#[test] +fn obj_with_mtl() { + let m = load("cube.obj", &fixture("cube.obj"), &resolver).unwrap(); + assert_eq!(m.materials.len(), 2); + let red = &m.materials[0]; + assert_eq!(red.name, "Red"); + assert_eq!(red.base_color, [1.0, 0.0, 0.0, 1.0]); + assert!((red.roughness - (2.0f32 / 100.0).sqrt()).abs() < 1e-6); + assert_eq!(red.metallic, 0.0); + let gold = &m.materials[1]; + assert_eq!((gold.metallic, gold.roughness), (1.0, 0.3)); + let prims = &m.meshes[0].primitives; + assert_eq!(prims.len(), 2); + assert_eq!(prims[0].material, Some(0)); + assert_eq!(prims.iter().map(|p| p.triangle_count()).sum::(), 12); + // Four unique (v, vt, vn) corners per face. + assert_eq!(m.vertex_count(), 24); + // Every face is wound counter-clockwise from outside (face normal agrees with vn). + for p in prims { + for t in p.indices.chunks(3) { + let (a, b, c) = (p.positions[t[0] as usize], p.positions[t[1] as usize], p.positions[t[2] as usize]); + let f = cross(sub(b, a), sub(c, a)); + assert!(dot(f, p.normals[t[0] as usize]) > 0.0); + } + } + let (lo, hi) = m.bounds().unwrap(); + assert_eq!((lo, hi), ([-0.5; 3], [0.5; 3])); + // V is flipped to the image's top-down rows. + assert_eq!(prims[0].uvs[0], [0.0, 1.0]); +} + +#[test] +fn obj_without_normals_gets_them() { + let src = b"v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n"; + let m = obj::parse(src, &|_| None).unwrap(); + let p = &m.meshes[0].primitives[0]; + assert_eq!(p.normals, vec![[0.0, 0.0, 1.0]; 3]); + assert!(obj::parse(b"v 0 0 0\n", &|_| None).is_err()); +} + +#[test] +fn channel_interpolation_modes() { + let mut c = Channel { node: 0, path: ChannelPath::Translation, interp: Interp::Step, times: vec![0.0, 1.0], values: vec![0.0, 0.0, 0.0, 10.0, 0.0, 0.0] }; + assert_eq!(c.sample(0.7).unwrap()[0], 0.0); + c.interp = Interp::Linear; + assert!((c.sample(0.7).unwrap()[0] - 7.0).abs() < 1e-9); + assert_eq!(c.sample(5.0).unwrap()[0], 10.0); + // Cubic spline with zero tangents: smoothstep. + c.interp = Interp::CubicSpline; + c.values = vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0]; + assert!((c.sample(0.5).unwrap()[0] - 5.0).abs() < 1e-9); + assert!((c.sample(0.25).unwrap()[0] - 10.0 * (3.0 * 0.0625 - 2.0 * 0.015625)).abs() < 1e-9); + // Rotation: slerp keeps unit length, half way around Z. + let r = + Channel { node: 0, path: ChannelPath::Rotation, interp: Interp::Linear, times: vec![0.0, 1.0], values: vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0] }; + let q = r.sample(0.5).unwrap(); + assert!((q[2] - (0.5f64).sqrt()).abs() < 1e-9 && (q[3] - (0.5f64).sqrt()).abs() < 1e-9); +} + +#[test] +fn skinning_moves_vertices_with_joints() { + let mut p = mesh(PrimitiveKind::Plane, &PrimParams { size: [2.0, 2.0, 0.0], ..Default::default() }); + p.joints = vec![[0, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]]; + p.weights = vec![[1.0, 0.0, 0.0, 0.0]; 4]; + let mut m = Model::single(Mesh { name: "s".into(), primitives: vec![p] }, vec![Material::default()]); + m.nodes[0].skin = Some(0); + m.nodes.push(Node::default()); + m.nodes.push(Node { translation: [0.0, 0.0, 5.0], ..Node::default() }); + m.roots = vec![0, 1, 2]; + m.skins.push(Skin { joints: vec![1, 2], inverse_bind: vec![Mat4::IDENTITY; 2] }); + let inst = m.instances(None, 0.0); + let pr = &inst[0].mesh.primitives[0]; + assert_eq!(skin_point(&inst[0], pr, 0, pr.positions[0])[2], 0.0); + assert_eq!(skin_point(&inst[0], pr, 1, pr.positions[1])[2], 5.0); +} + +/// Edges of a mesh welded by position: (undirected edge → uses, directed edge → uses). +fn edge_stats(p: &Primitive) -> (HashMap<(u32, u32), usize>, HashMap<(u32, u32), usize>) { + let mut weld: HashMap<[u32; 3], u32> = HashMap::new(); + let ids: Vec = p + .positions + .iter() + .map(|q| { + let k = q.map(|v| (v + 0.0).to_bits()); + let n = weld.len() as u32; + *weld.entry(k).or_insert(n) + }) + .collect(); + let mut und = HashMap::new(); + let mut dir = HashMap::new(); + for t in p.indices.chunks(3) { + let v = [ids[t[0] as usize], ids[t[1] as usize], ids[t[2] as usize]]; + if v[0] == v[1] || v[1] == v[2] || v[0] == v[2] { + continue; + } + for k in 0..3 { + let (a, b) = (v[k], v[(k + 1) % 3]); + *und.entry((a.min(b), a.max(b))).or_insert(0) += 1; + *dir.entry((a, b)).or_insert(0) += 1; + } + } + (und, dir) +} + +fn assert_watertight(p: &Primitive) { + let (und, dir) = edge_stats(p); + for (e, n) in &und { + assert_eq!(*n, 2, "edge {e:?} used {n} times"); + } + for (e, n) in &dir { + assert_eq!(*n, 1, "directed edge {e:?} used {n} times (inconsistent winding)"); + } +} + +/// Signed volume (positive when faces wind outward). +fn volume(p: &Primitive) -> f64 { + p.indices + .chunks(3) + .map(|t| { + let [a, b, c] = [0, 1, 2].map(|k| p.positions[t[k] as usize].map(|v| v as f64)); + (a[0] * (b[1] * c[2] - b[2] * c[1]) - a[1] * (b[0] * c[2] - b[2] * c[0]) + a[2] * (b[0] * c[1] - b[1] * c[0])) / 6.0 + }) + .sum() +} + +/// Vertex normals agree with the faces they belong to. +fn assert_normals_outward(p: &Primitive) { + for t in p.indices.chunks(3) { + let (a, b, c) = (p.positions[t[0] as usize], p.positions[t[1] as usize], p.positions[t[2] as usize]); + let f = cross(sub(b, a), sub(c, a)); + if dot(f, f) < 1e-10 { + continue; + } + for &i in t { + let n = p.normals[i as usize]; + assert!((dot(n, n) - 1.0).abs() < 1e-3, "normal not unit"); + assert!(dot(f, n) > 0.0, "normal {n:?} faces away from triangle {f:?}"); + } + } +} + +#[test] +fn primitive_vertex_counts_and_normals() { + let pr = PrimParams { segments: 16, rings: 8, ..Default::default() }; + let cases = [ + (PrimitiveKind::Cube, 24, 36), + (PrimitiveKind::Plane, 4, 6), + (PrimitiveKind::Sphere, 17 * 9, 6 * 16 * 7), + (PrimitiveKind::Torus, 17 * 9, 6 * 16 * 8), + (PrimitiveKind::Cylinder, 2 * 17 + 2 * 18, 6 * 16 + 2 * 3 * 16), + (PrimitiveKind::Cone, 2 * 17 + 18, 3 * 16 + 3 * 16), + ]; + for (k, v, i) in cases { + let m = mesh(k, &pr); + assert_eq!((m.positions.len(), m.indices.len()), (v, i), "{k:?}"); + assert_eq!(m.normals.len(), v); + assert_eq!(m.uvs.len(), v); + assert_eq!(m.tangents.len(), v); + assert_normals_outward(&m); + if k != PrimitiveKind::Plane { + assert!(volume(&m) > 0.0, "{k:?} winds inward"); + assert_watertight(&m); + } + } + // Volumes approach the analytic ones. + let cube = mesh(PrimitiveKind::Cube, &PrimParams { size: [2.0, 3.0, 4.0], ..Default::default() }); + assert!((volume(&cube) - 24.0).abs() < 1e-4); + let s = mesh(PrimitiveKind::Sphere, &PrimParams { radius: 1.0, segments: 128, rings: 64, ..Default::default() }); + assert!((volume(&s) - 4.0 / 3.0 * std::f64::consts::PI).abs() < 0.01); +} + +#[test] +fn triangulation_covers_polygon_with_hole() { + let outer = vec![[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [5.0, 12.0], [0.0, 10.0]]; + let hole = vec![[3.0, 3.0], [3.0, 7.0], [7.0, 7.0], [7.0, 3.0]]; + let pts: Vec<[f64; 2]> = outer.iter().chain(hole.iter()).copied().collect(); + let tris = triangulate(&outer, std::slice::from_ref(&hole)); + let area: f64 = tris.iter().map(|t| signed_area(&[pts[t[0]], pts[t[1]], pts[t[2]]]) * 0.5).sum(); + let expect = signed_area(&outer).abs() * 0.5 - 16.0; + assert!((area - expect).abs() < 1e-9, "{area} vs {expect}"); + assert!(tris.iter().all(|t| signed_area(&[pts[t[0]], pts[t[1]], pts[t[2]]]) >= 0.0)); + // A concave "C". + let c = vec![[0.0, 0.0], [6.0, 0.0], [6.0, 2.0], [2.0, 2.0], [2.0, 4.0], [6.0, 4.0], [6.0, 6.0], [0.0, 6.0]]; + let tris = triangulate(&c, &[]); + let area: f64 = tris.iter().map(|t| signed_area(&[c[t[0]], c[t[1]], c[t[2]]]) * 0.5).sum(); + assert!((area - 28.0).abs() < 1e-9); +} + +fn ring_outline() -> Outline { + let circle = + |r: f64, n: usize| -> Vec<[f64; 2]> { (0..n).map(|i| (i as f64 / n as f64) * std::f64::consts::TAU).map(|a| [r * a.cos(), r * a.sin()]).collect() }; + Outline { outer: circle(100.0, 48), holes: vec![circle(50.0, 32)] } +} + +#[test] +fn extrusion_is_watertight_with_outward_normals() { + let square = Outline { outer: vec![[0.0, 0.0], [100.0, 0.0], [100.0, 100.0], [0.0, 100.0]], holes: vec![] }; + for bevel in BevelStyle::ALL { + for o in [square.clone(), ring_outline()] { + let p = ExtrudeParams { bevel, bevel_depth: 8.0, hole_bevel: 0.5, depth: 40.0 }; + let m = extrude(std::slice::from_ref(&o), &p); + assert_watertight(&m); + assert_normals_outward(&m); + assert!(volume(&m) > 0.0, "{bevel:?}: inward winding"); + let (lo, hi) = m.bounds().unwrap(); + assert!(lo[2].abs() < 1e-6 && (hi[2] - 40.0).abs() < 1e-4); + } + } + // Without a bevel the square block's volume is exact. + let m = extrude(&[square], &ExtrudeParams { bevel: BevelStyle::None, bevel_depth: 0.0, hole_bevel: 1.0, depth: 10.0 }); + assert!((volume(&m) - 100_000.0).abs() < 1e-2); + // Front cap faces the camera (−z), back cap away. + assert!(m.normals.iter().any(|n| n[2] < -0.99) && m.normals.iter().any(|n| n[2] > 0.99)); +} + +#[test] +fn bevels_shrink_the_caps() { + let square = Outline { outer: vec![[0.0, 0.0], [100.0, 0.0], [100.0, 100.0], [0.0, 100.0]], holes: vec![] }; + let m = extrude(std::slice::from_ref(&square), &ExtrudeParams { bevel: BevelStyle::Angular, bevel_depth: 10.0, hole_bevel: 1.0, depth: 50.0 }); + // Front cap vertices (z = 0) are inset by the bevel. + let front: Vec<_> = m.positions.iter().filter(|p| p[2] == 0.0).collect(); + assert!(front.iter().all(|p| p[0] >= 9.999 && p[0] <= 90.001)); + // The widest ring (the walls) still reaches the outline. + assert!(m.positions.iter().any(|p| p[0] == 0.0 && p[2] == 10.0)); + // Convex bevels have more profile steps than angular ones. + let c = extrude(std::slice::from_ref(&square), &ExtrudeParams { bevel: BevelStyle::Convex, bevel_depth: 10.0, hole_bevel: 1.0, depth: 50.0 }); + assert!(c.positions.len() > m.positions.len()); +} + +#[test] +fn contours_group_into_outlines_by_nesting() { + let sq = |x: f64, y: f64, s: f64| vec![[x, y], [x + s, y], [x + s, y + s], [x, y + s]]; + // "O" (outer + hole) next to an island inside another hole. + let g = group_contours(vec![sq(0.0, 0.0, 10.0), sq(2.0, 2.0, 6.0), sq(20.0, 0.0, 10.0), sq(22.0, 2.0, 6.0), sq(24.0, 4.0, 2.0)]); + assert_eq!(g.len(), 3); + assert_eq!(g.iter().map(|o| o.holes.len()).sum::(), 2); +} diff --git a/crates/model/src/triangulate.rs b/crates/model/src/triangulate.rs new file mode 100644 index 00000000..7505f5f9 --- /dev/null +++ b/crates/model/src/triangulate.rs @@ -0,0 +1,187 @@ +//! Polygon triangulation with holes: holes are bridged into the outer contour (each hole's +//! rightmost vertex joined to a visible outer vertex), then ears are clipped. + +type P = [f64; 2]; + +/// Twice the signed area (positive = counter-clockwise in a Y-up frame). +pub fn signed_area(c: &[P]) -> f64 { + let n = c.len(); + (0..n) + .map(|i| { + let (a, b) = (c[i], c[(i + 1) % n]); + a[0] * b[1] - b[0] * a[1] + }) + .sum() +} + +#[inline] +fn cross(o: P, a: P, b: P) -> f64 { + (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) +} + +fn in_triangle(p: P, a: P, b: P, c: P) -> bool { + let d1 = cross(a, b, p); + let d2 = cross(b, c, p); + let d3 = cross(c, a, p); + d1 >= 0.0 && d2 >= 0.0 && d3 >= 0.0 +} + +/// Triangulate an outer contour with holes. Contours are closed polylines (last point not +/// repeated); orientation is normalised internally. Returns triangles as indices into the +/// contours concatenated in order (`outer`, then `holes`), counter-clockwise (Y up). +pub fn triangulate(outer: &[P], holes: &[Vec

]) -> Vec<[usize; 3]> { + if outer.len() < 3 { + return vec![]; + } + let mut pts: Vec

= outer.to_vec(); + for h in holes { + pts.extend_from_slice(h); + } + // Ring of global indices, outer counter-clockwise. + let mut ring: Vec = (0..outer.len()).collect(); + if signed_area(outer) < 0.0 { + ring.reverse(); + } + // Holes clockwise, rightmost first. + let mut hs: Vec> = vec![]; + let mut off = outer.len(); + for h in holes { + let mut r: Vec = (off..off + h.len()).collect(); + off += h.len(); + if h.len() < 3 { + continue; + } + if signed_area(h) > 0.0 { + r.reverse(); + } + hs.push(r); + } + hs.sort_by(|a, b| { + let mx = |r: &Vec| r.iter().map(|&i| pts[i][0]).fold(f64::NEG_INFINITY, f64::max); + mx(b).total_cmp(&mx(a)) + }); + for h in hs { + bridge(&pts, &mut ring, &h); + } + ear_clip(&pts, ring) +} + +/// Join hole `h` into `ring` through a bridge from the hole's rightmost vertex. +fn bridge(pts: &[P], ring: &mut Vec, h: &[usize]) { + let (hk, &m) = h.iter().enumerate().max_by(|a, b| pts[*a.1][0].total_cmp(&pts[*b.1][0])).unwrap_or((0, &h[0])); + let mp = pts[m]; + // Nearest ring edge hit by the ray +x from M. + let n = ring.len(); + let mut best: Option<(f64, usize)> = None; + for i in 0..n { + let (a, b) = (pts[ring[i]], pts[ring[(i + 1) % n]]); + if (a[1] > mp[1]) == (b[1] > mp[1]) && a[1] != mp[1] && b[1] != mp[1] { + continue; + } + if a[1] == b[1] { + continue; + } + let t = (mp[1] - a[1]) / (b[1] - a[1]); + if !(0.0..=1.0).contains(&t) { + continue; + } + let x = a[0] + t * (b[0] - a[0]); + if x >= mp[0] && best.is_none_or(|(bx, _)| x < bx) { + best = Some((x, i)); + } + } + let Some((x, i)) = best else { return }; + let (a, b) = (ring[i], ring[(i + 1) % n]); + // Candidate: the edge endpoint with the larger x. + let mut cand = if pts[a][0] > pts[b][0] { (i, a) } else { ((i + 1) % n, b) }; + let ip = [x, mp[1]]; + // Reflex ring vertices inside triangle (M, I, P) block the view: take the one with the + // smallest angle to the ray (closest on ties). + let pp = pts[cand.1]; + let (t0, t1, t2) = if cross(mp, ip, pp) >= 0.0 { (mp, ip, pp) } else { (mp, pp, ip) }; + let mut best_ang = f64::INFINITY; + for k in 0..n { + let v = ring[k]; + if v == cand.1 { + continue; + } + let q = pts[v]; + let prev = pts[ring[(k + n - 1) % n]]; + let next = pts[ring[(k + 1) % n]]; + let reflex = cross(prev, q, next) <= 0.0; + if reflex && in_triangle(q, t0, t1, t2) { + let ang = (q[1] - mp[1]).abs().atan2(q[0] - mp[0]); + let d = (q[0] - mp[0]).hypot(q[1] - mp[1]); + let cd = (pts[cand.1][0] - mp[0]).hypot(pts[cand.1][1] - mp[1]); + if ang < best_ang || (ang == best_ang && d < cd) { + best_ang = ang; + cand = (k, v); + } + } + } + let (k, _) = cand; + // ring[..=k], hole from M around back to M, ring[k] again, rest. + let mut out = Vec::with_capacity(ring.len() + h.len() + 2); + out.extend_from_slice(&ring[..=k]); + for j in 0..=h.len() { + out.push(h[(hk + j) % h.len()]); + } + out.extend_from_slice(&ring[k..]); + *ring = out; +} + +fn ear_clip(pts: &[P], mut ring: Vec) -> Vec<[usize; 3]> { + let mut tris = vec![]; + let mut guard = 0usize; + let mut i = 0usize; + while ring.len() > 3 { + let n = ring.len(); + if guard > 2 * n { + // No ear found (degenerate/self-intersecting input): clip anyway to finish. + let (a, b, c) = (ring[(i + n - 1) % n], ring[i % n], ring[(i + 1) % n]); + tris.push([a, b, c]); + ring.remove(i % n); + guard = 0; + continue; + } + let ii = i % n; + let (a, b, c) = (ring[(ii + n - 1) % n], ring[ii], ring[(ii + 1) % n]); + let (pa, pb, pc) = (pts[a], pts[b], pts[c]); + let cr = cross(pa, pb, pc); + let scale = 1.0 + pa[0].abs() + pa[1].abs() + pc[0].abs() + pc[1].abs(); + // A straight-through vertex is clipped as a zero-area triangle (keeps every contour edge + // in the triangulation, so caps meet walls without T-junctions). + let straight = cr.abs() <= 1e-12 * scale * scale && (pa[0] - pb[0]) * (pc[0] - pb[0]) + (pa[1] - pb[1]) * (pc[1] - pb[1]) <= 0.0; + let convex = cr > 0.0 && !straight; + let mut ear = convex || straight; + if convex { + for &v in &ring { + if v == a || v == b || v == c { + continue; + } + let q = pts[v]; + // Bridged duplicates share positions with the ear's corners. + if q == pa || q == pb || q == pc { + continue; + } + if in_triangle(q, pa, pb, pc) { + ear = false; + break; + } + } + } + if ear { + tris.push([a, b, c]); + ring.remove(ii); + guard = 0; + i = if ii == 0 { 0 } else { ii - 1 }; + } else { + i = ii + 1; + guard += 1; + } + } + if ring.len() == 3 { + tris.push([ring[0], ring[1], ring[2]]); + } + tris +} diff --git a/crates/model/tests/fixtures/cube.mtl b/crates/model/tests/fixtures/cube.mtl new file mode 100644 index 00000000..9ea4c99c --- /dev/null +++ b/crates/model/tests/fixtures/cube.mtl @@ -0,0 +1,11 @@ +# EffectCraft test fixture: materials for cube.obj (original work). +newmtl Red +Kd 1.0 0.0 0.0 +Ns 98 +d 1.0 + +newmtl Gold +Kd 1.0 0.766 0.336 +Ke 0.0 0.0 0.0 +Pr 0.3 +Pm 1.0 diff --git a/crates/model/tests/fixtures/cube.mtl.attribution b/crates/model/tests/fixtures/cube.mtl.attribution new file mode 100644 index 00000000..7af62d2d --- /dev/null +++ b/crates/model/tests/fixtures/cube.mtl.attribution @@ -0,0 +1,7 @@ +asset: cube.mtl +title: MTL fixture: the two materials of cube.obj (Phong exponent and PBR extension keys) +author: EffectCraft contributors +source: original work +license: MIT OR Apache-2.0 +added: 2026-10-02 by EffectCraft agent (M7.4) +notes: Hand-written / script-generated test data for the model importer, made from the public Khronos glTF 2.0 specification and the Wavefront OBJ/MTL format description; not taken from any third-party file. The 2x2 texture is four solid colours. diff --git a/crates/model/tests/fixtures/cube.obj b/crates/model/tests/fixtures/cube.obj new file mode 100644 index 00000000..e50ef30c --- /dev/null +++ b/crates/model/tests/fixtures/cube.obj @@ -0,0 +1,29 @@ +# EffectCraft test fixture: a unit cube with texture coordinates and normals (original work). +mtllib cube.mtl +o Cube +v -0.5 -0.5 0.5 +v 0.5 -0.5 0.5 +v 0.5 0.5 0.5 +v -0.5 0.5 0.5 +v -0.5 -0.5 -0.5 +v 0.5 -0.5 -0.5 +v 0.5 0.5 -0.5 +v -0.5 0.5 -0.5 +vt 0 0 +vt 1 0 +vt 1 1 +vt 0 1 +vn 0 0 1 +vn 0 0 -1 +vn 1 0 0 +vn -1 0 0 +vn 0 1 0 +vn 0 -1 0 +usemtl Red +f 1/1/1 2/2/1 3/3/1 4/4/1 +f 6/1/2 5/2/2 8/3/2 7/4/2 +f 2/1/3 6/2/3 7/3/3 3/4/3 +usemtl Gold +f 5/1/4 1/2/4 4/3/4 8/4/4 +f 4/1/5 3/2/5 7/3/5 8/4/5 +f -4/1/6 -3/2/6 -7/3/6 -8/4/6 diff --git a/crates/model/tests/fixtures/cube.obj.attribution b/crates/model/tests/fixtures/cube.obj.attribution new file mode 100644 index 00000000..faf95fc0 --- /dev/null +++ b/crates/model/tests/fixtures/cube.obj.attribution @@ -0,0 +1,7 @@ +asset: cube.obj +title: Wavefront OBJ fixture: a unit cube with texture coordinates, normals and two materials +author: EffectCraft contributors +source: original work +license: MIT OR Apache-2.0 +added: 2026-10-02 by EffectCraft agent (M7.4) +notes: Hand-written / script-generated test data for the model importer, made from the public Khronos glTF 2.0 specification and the Wavefront OBJ/MTL format description; not taken from any third-party file. The 2x2 texture is four solid colours. diff --git a/crates/model/tests/fixtures/quad.glb b/crates/model/tests/fixtures/quad.glb new file mode 100644 index 0000000000000000000000000000000000000000..ab978546e3201331109e7eebb5503a071e2628df GIT binary patch literal 2028 zcmbVNJ#X7a7``Mx+8FiHspwMS&>;mFm!c%g){@$?k|?kxR}zc@g3+F+lXM7qM;wo~ z14BTPE&n0^AV9{f>DaAnenf_DO}D=9k&z8J?+uQrqr40{?7IG|ngj66E zX1EqC0L(lMoo!@Pq$M9RAq~gWqme$~h6RD}dn%dEB#&+E5sP_K1xi0+zHP)XWupd+ zcr~O3XbCgq!p0s^z3{WzuGbv8^f=(Qi&!CK&d)GasDg(gj)92<3R+OcU@$5WYpjw{ z!24nhX*Y~cIQG({uF~3Km1sX}J`1COgM5GlkV3jjtxh$I`ao61tpk;~8UU$ob-RwH zB4pF77o!A}r;h|brHxIfRfK%-JVL=(qaPzVr3hgX)~K4)e(#(r~WA9*j}p;?#lD~=$!i}(dg zd2aBs6#YWy|C?XSJ?d=5XugxS`J?BCcPA73Hif3*a-Yj7Dx$izl1rt!y$SD&1wogf zX#0P$+rUX4TR~=go+3*gEXNtdgIinp#bE=2OGS6SB~k9<+}+Nu#*6LM60 z)h)PjrO)u4+iiE?pM1y$&L0-$xW*saEarBDkQcf2b#pw=kIaRA+#>kRy-n^E{4Ra| zw21Zku=99*?b|glTyHsc5;zl3i!(eKy4hsW3Ly`90?8~^j4zyI~!S1+sI5YqnR W-rxVceZLN;q}}T5z1#im75NvFRaM9U literal 0 HcmV?d00001 diff --git a/crates/model/tests/fixtures/quad.glb.attribution b/crates/model/tests/fixtures/quad.glb.attribution new file mode 100644 index 00000000..c1b0e631 --- /dev/null +++ b/crates/model/tests/fixtures/quad.glb.attribution @@ -0,0 +1,7 @@ +asset: quad.glb +title: Binary glTF (GLB) fixture: the same textured, animated quad with its PNG texture in the BIN chunk +author: EffectCraft contributors +source: original work +license: MIT OR Apache-2.0 +added: 2026-10-02 by EffectCraft agent (M7.4) +notes: Hand-written / script-generated test data for the model importer, made from the public Khronos glTF 2.0 specification and the Wavefront OBJ/MTL format description; not taken from any third-party file. The 2x2 texture is four solid colours. diff --git a/crates/model/tests/fixtures/quad.gltf b/crates/model/tests/fixtures/quad.gltf new file mode 100644 index 00000000..82afef9b --- /dev/null +++ b/crates/model/tests/fixtures/quad.gltf @@ -0,0 +1,210 @@ +{ + "asset": { + "version": "2.0", + "generator": "EffectCraft fixture script (original work)" + }, + "scene": 0, + "scenes": [ + { + "nodes": [ + 0 + ] + } + ], + "nodes": [ + { + "name": "Root", + "translation": [ + 0.0, + 1.0, + 0.0 + ], + "children": [ + 1 + ] + }, + { + "name": "Quad", + "mesh": 0, + "scale": [ + 2.0, + 2.0, + 2.0 + ] + } + ], + "meshes": [ + { + "name": "Quad", + "primitives": [ + { + "attributes": { + "POSITION": 0, + "NORMAL": 1, + "TEXCOORD_0": 2 + }, + "indices": 3, + "material": 0 + } + ] + } + ], + "materials": [ + { + "name": "Painted", + "pbrMetallicRoughness": { + "baseColorFactor": [ + 1.0, + 0.5, + 0.25, + 1.0 + ], + "metallicFactor": 0.25, + "roughnessFactor": 0.75, + "baseColorTexture": { + "index": 0 + } + }, + "emissiveFactor": [ + 0.1, + 0.2, + 0.3 + ], + "doubleSided": true + } + ], + "textures": [ + { + "source": 0, + "sampler": 0 + } + ], + "samplers": [ + { + "wrapS": 33071, + "wrapT": 10497 + } + ], + "images": [ + { + "uri": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR4nGP4z8DwHwyBNBgAAEnICff5q7YNAAAAAElFTkSuQmCC" + } + ], + "animations": [ + { + "name": "Move", + "samplers": [ + { + "input": 4, + "output": 5, + "interpolation": "LINEAR" + } + ], + "channels": [ + { + "sampler": 0, + "target": { + "node": 0, + "path": "translation" + } + } + ] + } + ], + "accessors": [ + { + "bufferView": 0, + "componentType": 5126, + "count": 4, + "type": "VEC3", + "min": [ + -0.5, + -0.5, + 0.0 + ], + "max": [ + 0.5, + 0.5, + 0.0 + ] + }, + { + "bufferView": 1, + "componentType": 5126, + "count": 4, + "type": "VEC3" + }, + { + "bufferView": 2, + "componentType": 5126, + "count": 4, + "type": "VEC2" + }, + { + "bufferView": 3, + "componentType": 5123, + "count": 6, + "type": "SCALAR" + }, + { + "bufferView": 4, + "componentType": 5126, + "count": 2, + "type": "SCALAR", + "min": [ + 0.0 + ], + "max": [ + 1.0 + ] + }, + { + "bufferView": 5, + "componentType": 5126, + "count": 2, + "type": "VEC3" + } + ], + "bufferViews": [ + { + "buffer": 0, + "byteOffset": 0, + "byteLength": 48, + "target": 34962 + }, + { + "buffer": 0, + "byteOffset": 48, + "byteLength": 48, + "target": 34962 + }, + { + "buffer": 0, + "byteOffset": 96, + "byteLength": 32, + "target": 34962 + }, + { + "buffer": 0, + "byteOffset": 128, + "byteLength": 12, + "target": 34963 + }, + { + "buffer": 0, + "byteOffset": 140, + "byteLength": 8 + }, + { + "buffer": 0, + "byteOffset": 148, + "byteLength": 24 + } + ], + "buffers": [ + { + "byteLength": 172, + "uri": "data:application/octet-stream;base64,AAAAvwAAAL8AAAAAAAAAPwAAAL8AAAAAAAAAPwAAAD8AAAAAAAAAvwAAAD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAAAAAAIA/AAAAAAAAgD8AAIA/AACAPwAAgD8AAAAAAAAAAAAAAAAAAAEAAgAAAAIAAwAAAAAAAACAPwAAAAAAAIA/AAAAAAAAAEAAAIA/AAAAAA==" + } + ] +} diff --git a/crates/model/tests/fixtures/quad.gltf.attribution b/crates/model/tests/fixtures/quad.gltf.attribution new file mode 100644 index 00000000..1ccf03b7 --- /dev/null +++ b/crates/model/tests/fixtures/quad.gltf.attribution @@ -0,0 +1,7 @@ +asset: quad.gltf +title: glTF 2.0 fixture: a textured quad under a parent node with a translation animation (all data embedded as data URIs) +author: EffectCraft contributors +source: original work +license: MIT OR Apache-2.0 +added: 2026-10-02 by EffectCraft agent (M7.4) +notes: Hand-written / script-generated test data for the model importer, made from the public Khronos glTF 2.0 specification and the Wavefront OBJ/MTL format description; not taken from any third-party file. The 2x2 texture is four solid colours. diff --git a/crates/project/src/build.rs b/crates/project/src/build.rs index 12e384d6..a201879d 100644 --- a/crates/project/src/build.rs +++ b/crates/project/src/build.rs @@ -584,6 +584,13 @@ pub fn light_options(ids: &mut Ids, kind: LightKind) -> PropGroup { .with(ids.prop("coneAngle", "Cone Angle", Value::Scalar(90.0)).with_ui(ParamUi::Angle)) .with(ids.prop("coneFeather", "Cone Feather", Value::Scalar(50.0)).with_ui(ParamUi::Percent)); } + if kind == LightKind::Environment { + // The environment image: a layer (footage, comp…) mapped as an equirectangular panorama; + // none = the comp's Environment Layer. + return g + .with(ids.prop("source", "Source", Value::Layer(None)).with_ui(ParamUi::Layer)) + .with(ids.prop("rotation", "Environment Rotation", Value::Scalar(0.0)).with_ui(ParamUi::Angle)); + } if kind != LightKind::Ambient { g = g .with(ids.prop("falloff", "Falloff", Value::Enum(0)).with_ui(popup(&["None", "Smooth", "Inverse Square Clamped"]))) @@ -609,6 +616,70 @@ pub fn material_options(ids: &mut Ids) -> PropGroup { .with(ids.prop("metal", "Metal", Value::Scalar(100.0)).with_ui(ParamUi::Percent)) } +/// Material Options of a 3D model or primitive layer (Advanced 3D): shadows and lights, plus +/// the primitive's own metallic-roughness material (models keep their file's materials). +pub fn model_material_options(ids: &mut Ids, primitive: bool) -> PropGroup { + let mut g = ids + .group("materialOptions", "Material Options") + .with(ids.prop("castsShadows", "Casts Shadows", Value::Enum(0)).with_ui(popup(&["Off", "On", "Only"]))) + .with(ids.prop("acceptsShadows", "Accepts Shadows", Value::Enum(1)).with_ui(popup(&["Off", "On", "Only"]))) + .with(ids.prop("acceptsLights", "Accepts Lights", Value::Bool(true)).with_ui(ParamUi::Checkbox)) + .with(ids.prop("appearsInReflections", "Appears in Reflections", Value::Bool(true)).with_ui(ParamUi::Checkbox)); + if primitive { + g = g + .with(ids.prop("baseColor", "Base Color", Value::Color([0.8, 0.8, 0.8, 1.0])).with_ui(ParamUi::Color)) + .with(ids.prop("metallic", "Metallic", Value::Scalar(0.0)).with_ui(slider(0.0, 100.0, 0.0, 100.0, 1))) + .with(ids.prop("roughness", "Roughness", Value::Scalar(50.0)).with_ui(slider(0.0, 100.0, 0.0, 100.0, 1))) + .with(ids.prop("emissive", "Emissive", Value::Color([0.0, 0.0, 0.0, 1.0])).with_ui(ParamUi::Color)); + } + g +} + +/// Geometry Options of an imported model layer: model units → pixels, and the animation clip +/// played along layer time. +pub fn model_geometry_options(ids: &mut Ids, unit_scale: f64, clips: &[String]) -> PropGroup { + let mut opts: Vec<&str> = vec!["None"]; + opts.extend(clips.iter().map(String::as_str)); + ids.group("geometryOptions", "Geometry Options") + .with(ids.prop("unitScale", "Model Scale", Value::Scalar(unit_scale)).with_ui(slider(0.0, 1.0e6, 0.0, 1000.0, 2))) + .with(ids.prop("animation", "Animation", Value::Enum(if clips.is_empty() { 0 } else { 1 })).with_ui(popup(&opts))) + .with(ids.prop("loopAnimation", "Loop Animation", Value::Bool(true)).with_ui(ParamUi::Checkbox)) + .with(ids.prop("animationSpeed", "Animation Speed", Value::Scalar(100.0)).with_ui(ParamUi::Percent)) +} + +/// Geometry Options of a primitive layer (pixels). +pub fn primitive_geometry_options(ids: &mut Ids, kind: crate::PrimitiveKind) -> PropGroup { + use crate::PrimitiveKind as K; + let px = |ids: &mut Ids, m: &str, n: &str, v: f64| ids.prop(m, n, Value::Scalar(v)).with_ui(slider(0.0, 100_000.0, 0.0, 2000.0, 1)); + let count = |ids: &mut Ids, m: &str, n: &str, v: f64| ids.prop(m, n, Value::Scalar(v)).with_ui(slider(3.0, 512.0, 3.0, 128.0, 0)); + let mut g = ids.group("geometryOptions", "Geometry Options"); + let props = match kind { + K::Cube => vec![px(ids, "width", "Width", 300.0), px(ids, "height", "Height", 300.0), px(ids, "depth", "Depth", 300.0)], + K::Plane => vec![px(ids, "width", "Width", 400.0), px(ids, "height", "Height", 400.0)], + K::Sphere => vec![px(ids, "radius", "Radius", 150.0), count(ids, "segments", "Segments", 48.0), count(ids, "rings", "Rings", 24.0)], + K::Torus => vec![ + px(ids, "radius", "Radius", 150.0), + px(ids, "tubeRadius", "Tube Radius", 50.0), + count(ids, "segments", "Segments", 48.0), + count(ids, "rings", "Sides", 24.0), + ], + K::Cone | K::Cylinder => vec![px(ids, "radius", "Radius", 150.0), px(ids, "height", "Height", 300.0), count(ids, "segments", "Segments", 48.0)], + }; + for p in props { + g = g.with(p); + } + g +} + +/// Geometry Options of a text or shape layer in an Advanced 3D comp (extrusion and bevels). +pub fn extrusion_geometry_options(ids: &mut Ids) -> PropGroup { + ids.group("geometryOptions", "Geometry Options") + .with(ids.prop("bevelStyle", "Bevel Style", Value::Enum(0)).with_ui(popup(&["None", "Angular", "Concave", "Convex"]))) + .with(ids.prop("bevelDepth", "Bevel Depth", Value::Scalar(2.0)).with_ui(slider(0.0, 1000.0, 0.0, 100.0, 1))) + .with(ids.prop("holeBevelDepth", "Hole Bevel Depth", Value::Scalar(100.0)).with_ui(slider(0.0, 100.0, 0.0, 100.0, 1))) + .with(ids.prop("extrusionDepth", "Extrusion Depth", Value::Scalar(0.0)).with_ui(slider(0.0, 10_000.0, 0.0, 1000.0, 1))) +} + pub fn audio(ids: &mut Ids) -> PropGroup { ids.group("audio", "Audio").with(ids.prop("levels", "Audio Levels", Value::Vec2([0.0, 0.0]))) } @@ -629,6 +700,7 @@ pub fn default_label(src: &LayerSource, project: &Project) -> Label { LayerSource::Shape => Label::Blue, LayerSource::Null => Label::Red, LayerSource::Camera | LayerSource::Light { .. } => Label::Pink, + LayerSource::Model { .. } | LayerSource::Primitive { .. } => Label::Aqua, } } @@ -640,7 +712,12 @@ pub fn layer(project: &mut Project, comp: &Comp, name: &str, source: LayerSource let (w, h) = (size.0 as f64, size.1 as f64); let (cw, ch) = (comp.width as f64, comp.height as f64); let anchor = match source { - LayerSource::Text | LayerSource::Shape | LayerSource::Camera | LayerSource::Light { .. } => [0.0, 0.0], + LayerSource::Text + | LayerSource::Shape + | LayerSource::Camera + | LayerSource::Light { .. } + | LayerSource::Model { .. } + | LayerSource::Primitive { .. } => [0.0, 0.0], LayerSource::Null => [50.0, 50.0], _ => [w / 2.0, h / 2.0], }; @@ -654,7 +731,9 @@ pub fn layer(project: &mut Project, comp: &Comp, name: &str, source: LayerSource } _ => {} } - if source.is_av() { + if source.is_model() { + // Model layers have no masks or effects (as in After Effects). + } else if source.is_av() { root.children.push(masks(&mut ids).into()); root.children.push(effects(&mut ids).into()); } else if matches!(source, LayerSource::Null) { @@ -694,8 +773,17 @@ pub fn layer(project: &mut Project, comp: &Comp, name: &str, source: LayerSource // Nulls are 100×100. } root.children.push(std::mem::replace(&mut tr, PropGroup::new(0, "", "")).into()); - if source.is_av() { - root.children.push(material_options(&mut ids).into()); + match source { + LayerSource::Primitive { kind } => { + root.children.push(primitive_geometry_options(&mut ids, kind).into()); + root.children.push(model_material_options(&mut ids, true).into()); + } + LayerSource::Model { .. } => { + root.children.push(model_geometry_options(&mut ids, 1.0, &[]).into()); + root.children.push(model_material_options(&mut ids, false).into()); + } + _ if source.is_av() => root.children.push(material_options(&mut ids).into()), + _ => {} } } let has_audio = matches!(&source, LayerSource::Footage { item } if matches!(project.item(*item).map(|i| &i.kind), Some(crate::ItemKind::Footage(f)) if f.has_audio)) @@ -711,6 +799,7 @@ pub fn layer(project: &mut Project, comp: &Comp, name: &str, source: LayerSource } else { crate::AutoOrient::Off }; + let switches = Switches { three_d: source.is_model(), ..Switches::default() }; Layer { id, name: comp.unique_layer_name(name), @@ -721,13 +810,14 @@ pub fn layer(project: &mut Project, comp: &Comp, name: &str, source: LayerSource in_point: Tick::ZERO, out_point: out, stretch: 100.0, - switches: Switches::default(), + switches, blend_mode: BlendMode::Normal, preserve_transparency: false, track_matte: None, parent: None, markers: vec![], markers_locked: false, + environment: false, auto_orient, props: root, } diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index 1ed35c09..4a00c413 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -310,6 +310,36 @@ impl LightKind { } } +/// A parametric 3D primitive (Layer ▸ New ▸ Cube/Sphere/Plane/Torus/Cone/Cylinder). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum PrimitiveKind { + #[default] + Cube, + Sphere, + Plane, + Torus, + Cone, + Cylinder, +} + +impl PrimitiveKind { + pub const ALL: [PrimitiveKind; 6] = + [PrimitiveKind::Cube, PrimitiveKind::Sphere, PrimitiveKind::Plane, PrimitiveKind::Torus, PrimitiveKind::Cone, PrimitiveKind::Cylinder]; + pub fn label(self) -> &'static str { + match self { + PrimitiveKind::Cube => "Cube", + PrimitiveKind::Sphere => "Sphere", + PrimitiveKind::Plane => "Plane", + PrimitiveKind::Torus => "Torus", + PrimitiveKind::Cone => "Cone", + PrimitiveKind::Cylinder => "Cylinder", + } + } + pub fn parse(s: &str) -> Option { + PrimitiveKind::ALL.into_iter().find(|k| k.label().eq_ignore_ascii_case(s.trim())) + } +} + /// What a layer shows. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "type")] @@ -333,12 +363,20 @@ pub enum LayerSource { Light { kind: LightKind, }, + /// An imported 3D model (a footage item of kind [`FootageKind::Model`]; Advanced 3D). + Model { + item: ItemId, + }, + /// A parametric 3D primitive (Advanced 3D). + Primitive { + kind: PrimitiveKind, + }, } impl LayerSource { pub fn item(&self) -> Option { match self { - LayerSource::Footage { item } | LayerSource::Comp { item } | LayerSource::Solid { item } => Some(*item), + LayerSource::Footage { item } | LayerSource::Comp { item } | LayerSource::Solid { item } | LayerSource::Model { item } => Some(*item), _ => None, } } @@ -352,12 +390,18 @@ impl LayerSource { LayerSource::Null => "Null", LayerSource::Camera => "Camera", LayerSource::Light { .. } => "Light", + LayerSource::Model { .. } => "3D Model", + LayerSource::Primitive { .. } => "3D Primitive", } } /// Layers with pixels (cameras, lights and nulls have none). pub fn is_av(&self) -> bool { !matches!(self, LayerSource::Camera | LayerSource::Light { .. } | LayerSource::Null) } + /// A 3D model or primitive layer (meshes; drawn by the Advanced 3D renderer). + pub fn is_model(&self) -> bool { + matches!(self, LayerSource::Model { .. } | LayerSource::Primitive { .. }) + } } #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] @@ -489,6 +533,10 @@ pub struct Layer { pub markers_locked: bool, #[serde(default)] pub auto_orient: AutoOrient, + /// Layer ▸ Environment Layer: the layer is the environment (image-based light and + /// reflections) of Advanced 3D comps instead of being drawn. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub environment: bool, /// The property tree (Masks, Effects, Transform, Text, Contents, Camera/Light options…). pub props: PropGroup, } @@ -540,6 +588,8 @@ pub enum FootageKind { Still, Sequence, Audio, + /// A 3D model (glTF 2.0 / OBJ), placed as a model layer. + Model, } #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] @@ -625,6 +675,7 @@ impl Item { FootageKind::Still => "Image", FootageKind::Sequence => "Image Sequence", FootageKind::Audio => "Audio", + FootageKind::Model => "3D Model", }, ItemKind::Solid(_) => "Solid", } diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml index 4c8a9787..80e30455 100644 --- a/crates/render/Cargo.toml +++ b/crates/render/Cargo.toml @@ -19,6 +19,7 @@ effectcraft-raster = { workspace = true } effectcraft-path = { workspace = true } effectcraft-text = { workspace = true } effectcraft-effects = { workspace = true } +effectcraft-model = { workspace = true } kurbo = { workspace = true } rayon = { workspace = true } web-time = { workspace = true } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 14a81575..4ec70cfa 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -39,6 +39,10 @@ pub trait FootageSource: Send + Sync { fn audio(&self, _item: ItemId, _footage: &Footage, _t: Tick, _frames: usize, _rate: u32) -> Option> { None } + /// The parsed 3D model of a [`effectcraft_project::FootageKind::Model`] item (Advanced 3D). + fn model(&self, _item: ItemId, _footage: &Footage) -> Option> { + None + } /// Set the decoded-frame cache budget in bytes (Settings ▸ Memory & CPU); sources without /// a cache ignore it. fn set_cache_budget(&self, _bytes: usize) {} diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index da5eaec2..22b2bd40 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -716,7 +716,7 @@ fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option None, diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index 1824fec5..d13f3768 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -22,6 +22,7 @@ fn item_icon(it: &Item) -> Icon { ItemKind::Footage(f) => match f.kind { effectcraft_engine::project::FootageKind::Still | effectcraft_engine::project::FootageKind::Sequence => Icon::Image, effectcraft_engine::project::FootageKind::Audio => Icon::Audio, + effectcraft_engine::project::FootageKind::Model => Icon::Cube, _ => Icon::Footage, }, } diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 5b6c085f..c9a42698 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -433,6 +433,7 @@ fn layer_icon(l: &Layer, project: &effectcraft_engine::project::Project) -> Icon LayerSource::Null => Icon::Null, LayerSource::Camera => Icon::Camera, LayerSource::Light { .. } => Icon::Light, + LayerSource::Model { .. } | LayerSource::Primitive { .. } => Icon::Cube, LayerSource::Comp { .. } => Icon::Comp, LayerSource::Solid { .. } => { if l.switches.adjustment { diff --git a/docs/architecture.md b/docs/architecture.md index c3d478e3..7e2921e0 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -22,6 +22,7 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for | L2 | `project` | The document: items, compositions, layers, the property tree, render queue model, `.ecproj` serde | | L2 | `text` | Fonts, shaping, layout, per-glyph geometry, text animators and selectors | | L2 | `effects` | The effect registry (241 effects) and their CPU implementations | +| L2 | `model` | 3D models for Advanced 3D: glTF 2.0 (`.gltf`/`.glb`) and OBJ/MTL import (meshes, PBR metallic-roughness materials and textures, node hierarchy, skins, animations), parametric primitives, extruded/bevelled outline meshes and polygon triangulation | | L2 | `track` | Motion tracking: feature/search region point tracking (pyramid normalized cross-correlation, Lucas–Kanade sub-pixel refinement), confidence, homography/affine/similarity solves | | L3 | `render` | Evaluation and compositing: sources, masks, effects, transforms, 3D, motion blur, mattes, blending, layer cache, audio mixdown | | L3 | `media` | Footage decoding (FilmCraft's pure-Rust codecs), image sequences, frame cache | diff --git a/xtask/src/main.rs b/xtask/src/main.rs index ae38adf9..7e265297 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -31,6 +31,7 @@ const LAYERS: &[(&str, u8)] = &[ ("text", 2), ("effects", 2), ("track", 2), + ("model", 2), ("render", 3), ("media", 3), ("expr", 3), From 451f63cc0d706232be34db43f43b00c9695d79c7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:08:04 -0400 Subject: [PATCH 05/23] M9.11: Blur & Sharpen and Distort effects (CC Radial Blur, Camera-Shake Deblur, CC Flo Motion, Liquify, Rolling Shutter Repair) Liquify stores its strokes as effect data and replays them into a cached displacement mesh; liquify.stroke / liquify.clear commands for agents. Rolling Shutter Repair measures motion with effectcraft-track KLT (new effects -> track same-layer edge). Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/effects/Cargo.toml | 1 + crates/effects/src/blur3.rs | 338 ++++++++++++ crates/effects/src/distort4.rs | 719 ++++++++++++++++++++++++++ crates/effects/src/lib.rs | 6 + crates/engine/src/commands/liquify.rs | 156 ++++++ crates/engine/src/commands/mod.rs | 2 + crates/engine/src/tests_paint.rs | 23 + docs/architecture.md | 2 +- xtask/src/main.rs | 1 + 10 files changed, 1248 insertions(+), 1 deletion(-) create mode 100644 crates/effects/src/blur3.rs create mode 100644 crates/effects/src/distort4.rs create mode 100644 crates/engine/src/commands/liquify.rs diff --git a/Cargo.lock b/Cargo.lock index 635fb0f5..46f34e63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1182,6 +1182,7 @@ dependencies = [ "effectcraft-keyframe", "effectcraft-project", "effectcraft-raster", + "effectcraft-track", "rayon", "serde_json", ] diff --git a/crates/effects/Cargo.toml b/crates/effects/Cargo.toml index db74bf27..e321fa2f 100644 --- a/crates/effects/Cargo.toml +++ b/crates/effects/Cargo.toml @@ -15,6 +15,7 @@ effectcraft-color = { workspace = true } effectcraft-keyframe = { workspace = true } effectcraft-project = { workspace = true } effectcraft-raster = { workspace = true } +effectcraft-track = { workspace = true } rayon = { workspace = true } [dev-dependencies] diff --git a/crates/effects/src/blur3.rs b/crates/effects/src/blur3.rs new file mode 100644 index 00000000..0140ac99 --- /dev/null +++ b/crates/effects/src/blur3.rs @@ -0,0 +1,338 @@ +//! Blur & Sharpen: CC Radial Blur and Camera-Shake Deblur. +//! +//! * **CC Radial Blur** — zoom (straight, fading, centred both ways) and rotational (plain or +//! fading) blurs around a centre point, sampled along the ray / arc through each pixel with a +//! sample count set by Quality. +//! * **Camera-Shake Deblur** — frame-based restoration in the spirit of published video deblurring +//! by "lucky frames": the frame is split into patches; for each patch the sharpness (mean +//! squared gradient) is compared with the same patch in the neighbouring frames within Blur +//! Duration; when a neighbour is clearly sharper, its patch is aligned to the current frame by +//! a small translation search (sum of squared differences on luma) and substituted with +//! feathered patch blending. Deterministic; neighbours come from [`crate::EffectHost::self_at`]. + +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use effectcraft_raster::{Image, Px}; +use rayon::prelude::*; + +use crate::util::Plane; +use crate::{Buf, EffectCtx, EffectSpec, num, p, popup, slider}; + +fn spec(id: &'static str, name: &'static str, params: Vec, render: crate::RenderFn) -> EffectSpec { + EffectSpec { id, name, category: "Blur & Sharpen", params, render, gpu: false, float: true } +} + +// ---------------------------------------------------------------- CC Radial Blur + +pub const RADIAL_TYPES: [&str; 5] = ["Straight Zoom", "Fading Zoom", "Centered", "Rotate", "Rotate Fading"]; + +fn cc_radial_blur(ctx: &EffectCtx, mut b: Buf) -> Buf { + let amount = ctx.params.f("amount"); + if amount == 0.0 { + return b; + } + let kind = ctx.params.e("type"); + let quality = ctx.params.f("quality").clamp(1.0, 100.0); + let c = b.to_px(ctx.params.v2("center")); + let src = b.img.clone(); + // Zoom: fraction of the distance to the centre; rotate: degrees of arc. + let zoom = (amount / 100.0).clamp(-1.0, 1.0) * 0.5; + let arc = amount.to_radians() * 0.25; + b.img.rows_mut().for_each(|(y, row)| { + for (x, o) in row.iter_mut().enumerate() { + let (dx, dy) = (x as f64 + 0.5 - c.0, y as f64 + 0.5 - c.1); + let r = (dx * dx + dy * dy).sqrt(); + let span = if kind >= 3 { r * arc.abs() } else { r * zoom.abs() }; + let n = ((span * quality / 50.0).ceil() as usize).clamp(1, 256); + if n <= 1 { + *o = src.get(x as i64, y as i64); + continue; + } + let mut acc = [0.0f32; 4]; + let mut wsum = 0.0f32; + for i in 0..n { + let t = i as f64 / (n - 1) as f64; + let (sx, sy, w) = match kind { + // Toward the centre, equal weights. + 0 => { + let k = 1.0 - zoom * t; + (c.0 + dx * k, c.1 + dy * k, 1.0) + } + // Toward the centre, weights fading with distance along the streak. + 1 => { + let k = 1.0 - zoom * t; + (c.0 + dx * k, c.1 + dy * k, (1.0 - t) as f32 + 0.05) + } + // Both ways around the pixel. + 2 => { + let k = 1.0 + zoom * (t - 0.5); + (c.0 + dx * k, c.1 + dy * k, 1.0) + } + _ => { + let a = arc * (t - 0.5); + let (s, co) = a.sin_cos(); + let w = if kind == 4 { (1.0 - (2.0 * t - 1.0).abs()) as f32 + 0.05 } else { 1.0 }; + (c.0 + dx * co - dy * s, c.1 + dx * s + dy * co, w) + } + }; + let q = src.sample_bilinear(sx, sy); + for k in 0..4 { + acc[k] += q[k] * w; + } + wsum += w; + } + *o = acc.map(|v| v / wsum); + } + }); + b +} + +// ---------------------------------------------------------------- Camera-Shake Deblur + +/// Mean squared luma gradient over a rectangle (sharpness). +fn sharpness(l: &Plane, x0: usize, y0: usize, x1: usize, y1: usize) -> f64 { + let mut s = 0.0f64; + let mut n = 0usize; + for y in y0..y1.saturating_sub(1) { + for x in x0..x1.saturating_sub(1) { + let v = l.get(x, y); + let gx = l.get(x + 1, y) - v; + let gy = l.get(x, y + 1) - v; + s += (gx * gx + gy * gy) as f64; + n += 1; + } + } + if n == 0 { 0.0 } else { s / n as f64 } +} + +/// Best integer translation (dx, dy) within ±`r` aligning `b` to `a` on a patch (SSD). +fn align(a: &Plane, bp: &Plane, x0: usize, y0: usize, x1: usize, y1: usize, r: i64) -> (i64, i64) { + let mut best = (f64::INFINITY, 0i64, 0i64); + let step = ((x1 - x0).max(y1 - y0) / 24).max(1); + for dy in -r..=r { + for dx in -r..=r { + let mut s = 0.0f64; + for y in (y0..y1).step_by(step) { + for x in (x0..x1).step_by(step) { + let d = a.get(x, y) - bp.get_clamped(x as i64 + dx, y as i64 + dy); + s += (d * d) as f64; + } + } + // Prefer the smallest shift on ties. + let s = s * (1.0 + 1e-9 * (dx * dx + dy * dy) as f64); + if s < best.0 { + best = (s, dx, dy); + } + } + } + (best.1, best.2) +} + +fn camera_shake_deblur(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(host) = ctx.env.host else { return b }; + let dur = ctx.params.f("blurDuration").clamp(1.0, 10.0).round() as i64; + let search = (ctx.params.f("searchRange").max(0.0) * b.scale).round() as i64; + let patch = (ctx.params.f("patchSize").max(8.0) * b.scale).round().max(4.0) as usize; + let thresh = 1.0 + ctx.params.f("threshold").max(0.0) / 100.0; + let show = ctx.params.b("showBlurryFrames"); + let fps = ctx.fps(); + let (w, h) = (b.img.width as usize, b.img.height as usize); + if w == 0 || h == 0 { + return b; + } + // Neighbouring frames in the effect's own input, resampled into this buffer's grid. + let mut neigh: Vec<(Image, Plane)> = vec![]; + for k in (-dur..=dur).filter(|k| *k != 0) { + let Some(nb) = host.self_at(ctx.time + k as f64 / fps, ctx.env.effect_index) else { continue }; + let img = if nb.img.width == b.img.width && nb.img.height == b.img.height && nb.offset == b.offset && nb.scale == b.scale { + nb.img + } else { + let k = nb.scale / b.scale.max(1e-9); + crate::util::gen_image(b.img.width, b.img.height, |x, y| { + let lx = (x as f64 + 0.5 - b.offset[0]) * k + nb.offset[0]; + let ly = (y as f64 + 0.5 - b.offset[1]) * k + nb.offset[1]; + nb.img.sample_bilinear(lx, ly) + }) + }; + let l = Plane::luma(&img); + neigh.push((img, l)); + } + if neigh.is_empty() { + return b; + } + let cur_l = Plane::luma(&b.img); + let (nx, ny) = (w.div_ceil(patch), h.div_ceil(patch)); + // Per patch: (neighbour index, shift) of the substitute, or None to keep. + let choice: Vec> = (0..nx * ny) + .into_par_iter() + .map(|i| { + let (px, py) = (i % nx, i / nx); + let (x0, y0) = (px * patch, py * patch); + let (x1, y1) = ((x0 + patch).min(w), (y0 + patch).min(h)); + let s0 = sharpness(&cur_l, x0, y0, x1, y1); + let mut best: Option<(f64, usize)> = None; + for (k, (_, l)) in neigh.iter().enumerate() { + let s = sharpness(l, x0, y0, x1, y1); + if s > s0 * thresh && best.is_none_or(|(bs, _)| s > bs) { + best = Some((s, k)); + } + } + let (_, k) = best?; + let (dx, dy) = align(&cur_l, &neigh[k].1, x0, y0, x1, y1, search); + Some((k, dx, dy)) + }) + .collect(); + if choice.iter().all(Option::is_none) { + return b; + } + let src = b.img.clone(); + // Feathered blending between patch decisions: each pixel mixes the four nearest patch + // centres bilinearly. + let pick = |i: usize, x: usize, y: usize| -> Px { + match choice[i] { + None => src.data[y * w + x], + Some((k, dx, dy)) => neigh[k].0.get_clamped(x as i64 + dx, y as i64 + dy), + } + }; + b.img.rows_mut().for_each(|(y, row)| { + let fy = ((y as f64 + 0.5) / patch as f64 - 0.5).clamp(0.0, (ny - 1) as f64); + let (y0, ty) = (fy.floor() as usize, (fy - fy.floor()) as f32); + let y1 = (y0 + 1).min(ny - 1); + for (x, o) in row.iter_mut().enumerate() { + let fx = ((x as f64 + 0.5) / patch as f64 - 0.5).clamp(0.0, (nx - 1) as f64); + let (x0, tx) = (fx.floor() as usize, (fx - fx.floor()) as f32); + let x1 = (x0 + 1).min(nx - 1); + let ids = [y0 * nx + x0, y0 * nx + x1, y1 * nx + x0, y1 * nx + x1]; + if ids.iter().all(|&i| choice[i].is_none()) { + continue; + } + let ws = [(1.0 - tx) * (1.0 - ty), tx * (1.0 - ty), (1.0 - tx) * ty, tx * ty]; + let mut acc = [0.0f32; 4]; + for (i, wgt) in ids.iter().zip(ws) { + let q = pick(*i, x, y); + for c in 0..4 { + acc[c] += q[c] * wgt; + } + } + if show { + acc[0] = (acc[0] + 0.3 * acc[3]).min(acc[3].max(acc[0])); + } + *o = acc; + } + }); + b +} + +pub fn specs() -> Vec { + vec![ + spec( + "ec.blur.ccradial", + "CC Radial Blur", + vec![ + p("type", "Type", Value::Enum(1), popup(&RADIAL_TYPES)), + p("amount", "Amount", num(10.0), slider(-100.0, 100.0, -100.0, 100.0, 1)), + p("quality", "Quality", num(50.0), slider(1.0, 100.0, 1.0, 100.0, 1)), + p("center", "Center", Value::Vec2([0.5, 0.5]), ParamUi::Point), + ], + cc_radial_blur, + ), + spec( + "ec.blur.camerashakedeblur", + "Camera-Shake Deblur", + vec![ + p("blurDuration", "Blur Duration", num(2.0), slider(1.0, 10.0, 1.0, 10.0, 0)), + p("searchRange", "Search Range", num(8.0), slider(0.0, 64.0, 0.0, 32.0, 0)), + p("patchSize", "Patch Size", num(32.0), slider(8.0, 256.0, 8.0, 128.0, 0)), + p("threshold", "Threshold", num(20.0), slider(0.0, 500.0, 0.0, 100.0, 1)), + p("showBlurryFrames", "Show Blurry Frames", Value::Bool(false), ParamUi::Checkbox), + ], + camera_shake_deblur, + ), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, EffectHost, LayerPixels, run_fx}; + + fn checker(w: u32, h: u32, cell: u32) -> Image { + crate::util::gen_image(w, h, |x, y| { + let v = if ((x as u32 / cell) + (y as u32 / cell)).is_multiple_of(2) { 1.0 } else { 0.0 }; + [v, v, v, 1.0] + }) + } + + #[test] + fn cc_radial_blur_keeps_centre_and_smears_edges() { + let img = checker(64, 64, 4); + for kind in 0..5u32 { + let out = run_fx( + "ec.blur.ccradial", + &[("type", Value::Enum(kind)), ("amount", num(40.0)), ("center", Value::Vec2([32.0, 32.0]))], + img.clone(), + 0.0, + EffectEnv::default(), + ); + // Far from the centre the checker is smeared toward grey. + let edge = out.img.get(2, 2)[0]; + assert!(edge > 0.05 && edge < 0.95, "type {kind}: {edge}"); + let again = run_fx( + "ec.blur.ccradial", + &[("type", Value::Enum(kind)), ("amount", num(40.0)), ("center", Value::Vec2([32.0, 32.0]))], + img.clone(), + 0.0, + EffectEnv::default(), + ); + assert_eq!(out.img.data, again.img.data); + } + // Zero amount is identity; a constant image stays constant. + let flat = Image::filled(16, 16, [0.4, 0.4, 0.4, 1.0]); + let o = run_fx("ec.blur.ccradial", &[("amount", num(80.0))], flat.clone(), 0.0, EffectEnv::default()); + assert!(o.img.data.iter().all(|p| (p[0] - 0.4).abs() < 1e-5)); + let o = run_fx("ec.blur.ccradial", &[("amount", num(0.0))], img.clone(), 0.0, EffectEnv::default()); + assert_eq!(o.img.data, img.data); + } + + /// Frames: sharp checker at even frames, a blurred one at odd frames (shifted by 2 px). + struct Shaky; + impl EffectHost for Shaky { + fn layer(&self, _: u64, _: bool) -> Option { + None + } + fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option> { + None + } + fn self_at(&self, t: f64, _: usize) -> Option { + let f = (t * 30.0).round() as i64; + Some(Buf { img: frame(f), offset: [0.0; 2], scale: 1.0 }) + } + } + + fn frame(f: i64) -> Image { + let sharp = crate::util::gen_image(48, 48, |x, y| { + let v = if (((x + 2) / 6) + (y / 6)) % 2 == 0 { 1.0 } else { 0.0 }; + [v, v, v, 1.0] + }); + if f % 2 == 0 { sharp } else { effectcraft_raster::gaussian_blur(&sharp, 2.5, 2.5, true) } + } + + #[test] + fn camera_shake_deblur_substitutes_sharp_neighbours() { + let host = Shaky; + let env = EffectEnv { host: Some(&host), frame_rate: 30.0, ..Default::default() }; + let blurry = frame(1); + let out = run_fx("ec.blur.camerashakedeblur", &[("patchSize", num(16.0)), ("searchRange", num(3.0))], blurry.clone(), 1.0 / 30.0, env); + let sharp = frame(0); + let err = |a: &Image| a.data.iter().zip(&sharp.data).map(|(p, q)| (p[0] - q[0]).abs()).sum::() / a.data.len() as f32; + assert!(err(&out.img) < err(&blurry) * 0.5, "{} vs {}", err(&out.img), err(&blurry)); + // A sharp frame is left alone (its neighbours are blurrier). + let keep = run_fx("ec.blur.camerashakedeblur", &[], sharp.clone(), 0.0, env); + assert_eq!(keep.img.data, sharp.data); + // Deterministic; no host = pass-through. + let again = run_fx("ec.blur.camerashakedeblur", &[("patchSize", num(16.0)), ("searchRange", num(3.0))], blurry.clone(), 1.0 / 30.0, env); + assert_eq!(out.img.data, again.img.data); + let none = run_fx("ec.blur.camerashakedeblur", &[], blurry.clone(), 0.0, EffectEnv::default()); + assert_eq!(none.img.data, blurry.data); + } +} diff --git a/crates/effects/src/distort4.rs b/crates/effects/src/distort4.rs new file mode 100644 index 00000000..4e6b5040 --- /dev/null +++ b/crates/effects/src/distort4.rs @@ -0,0 +1,719 @@ +//! Distort: CC Flo Motion, Liquify and Rolling Shutter Repair. +//! +//! * **CC Flo Motion** — two knots that pull the image toward themselves (positive amount) or +//! push it away (negative), with a falloff exponent, optional tiling and supersampled +//! antialiasing. +//! * **Liquify** — brush strokes build a displacement mesh (a grid of layer-space offsets). The +//! strokes are the effect's data (the hidden `distortionMesh` text, one stroke per line, so it +//! animates with hold keyframes and is written by the `liquify.stroke` command); replaying +//! them is deterministic and cached. Tools: warp, turbulence, twirl clockwise / counter- +//! clockwise, pucker, bloat, shift pixels, reflection, clone, reconstruction and freeze / thaw. +//! Distortion Percentage scales the mesh and Distortion Mesh Offset moves it. +//! * **Rolling Shutter Repair** — every scanline of a CMOS frame is exposed a little later than +//! the previous one, so moving content shears. Motion is measured between the neighbouring +//! frames with pyramidal Lucas–Kanade (`effectcraft-track`) on a grid of points; each pixel is +//! then resampled from where it was at the frame's mid-exposure time: +//! `out(p) = in(p + v(p)·rate·(s(p) − ½))`, with `s` the scan position (0…1 along the scan +//! direction) and `v` the per-frame velocity (Warp: one smooth velocity per scanline band; +//! Pixel Motion: a dense interpolated field). + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, OnceLock}; + +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use effectcraft_raster::Image; +use rayon::prelude::*; + +use crate::util::{hash1, point_in_poly}; +use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider}; + +fn spec(id: &'static str, name: &'static str, params: Vec, render: crate::RenderFn) -> EffectSpec { + EffectSpec { id, name, category: "Distort", params, render, gpu: false, float: true } +} + +// ---------------------------------------------------------------- CC Flo Motion + +fn flo_motion(ctx: &EffectCtx, mut b: Buf) -> Buf { + let knots = [(ctx.params.v2("knot1"), ctx.params.f("amount1") / 100.0), (ctx.params.v2("knot2"), ctx.params.f("amount2") / 100.0)]; + if knots.iter().all(|k| k.1 == 0.0) { + return b; + } + let tile = ctx.params.b("tileEdges"); + let ss = match ctx.params.e("antialiasing") { + 0 => 1, + 1 => 2, + _ => 3, + }; + let falloff = ctx.params.f("falloff").max(0.01); + let (lw, lh) = (ctx.layer_size[0].max(1.0), ctx.layer_size[1].max(1.0)); + let radius = (lw * lw + lh * lh).sqrt() * 0.5; + let src = b.img.clone(); + let (scale, off) = (b.scale, b.offset); + let map = |lx: f64, ly: f64| -> (f64, f64) { + let (mut x, mut y) = (lx, ly); + for (k, a) in knots { + if a == 0.0 { + continue; + } + let (dx, dy) = (x - k[0], y - k[1]); + let r = (dx * dx + dy * dy).sqrt() / radius; + // Weight 1 at the knot, falling off with distance. + let w = (1.0 - r.min(1.0)).powf(falloff); + // Pull: sample closer to the knot (content flows out of it); push: farther. + let s = (1.0 - a.clamp(-4.0, 0.95) * w).max(0.02); + x = k[0] + dx * s; + y = k[1] + dy * s; + } + (x, y) + }; + b.img.rows_mut().for_each(|(y, row)| { + for (x, o) in row.iter_mut().enumerate() { + let mut acc = [0.0f32; 4]; + for sy in 0..ss { + for sx in 0..ss { + let px = x as f64 + (sx as f64 + 0.5) / ss as f64; + let py = y as f64 + (sy as f64 + 0.5) / ss as f64; + let (lx, ly) = map((px - off[0]) / scale, (py - off[1]) / scale); + let (lx, ly) = if tile { (lx.rem_euclid(lw), ly.rem_euclid(lh)) } else { (lx, ly) }; + let q = src.sample_bilinear(lx * scale + off[0], ly * scale + off[1]); + for c in 0..4 { + acc[c] += q[c]; + } + } + } + let n = (ss * ss) as f32; + *o = acc.map(|v| v / n); + } + }); + b +} + +// ---------------------------------------------------------------- Liquify + +/// Liquify tools (the stroke text uses these names). +pub const LIQUIFY_TOOLS: [&str; 12] = [ + "warp", + "turbulence", + "twirlClockwise", + "twirlCounterclockwise", + "pucker", + "bloat", + "shiftPixels", + "reflection", + "clone", + "reconstruction", + "freeze", + "thaw", +]; + +/// One recorded Liquify stroke. +#[derive(Clone, Debug, PartialEq)] +pub struct LiquifyStroke { + pub tool: usize, + /// Brush size (diameter) and pressure (0–100), turbulent jitter (0–100), layer pixels. + pub size: f64, + pub pressure: f64, + pub jitter: f64, + /// Clone offset (source − destination). + pub clone_offset: [f64; 2], + pub points: Vec<[f64; 2]>, +} + +impl LiquifyStroke { + /// Text form: `tool size pressure jitter cloneX cloneY x,y x,y …`. + pub fn to_line(&self) -> String { + let mut s = format!("{} {} {} {} {} {}", LIQUIFY_TOOLS[self.tool], self.size, self.pressure, self.jitter, self.clone_offset[0], self.clone_offset[1]); + for p in &self.points { + s.push_str(&format!(" {},{}", p[0], p[1])); + } + s + } + pub fn parse(line: &str) -> Option { + let mut it = line.split_whitespace(); + let name = it.next()?; + let tool = LIQUIFY_TOOLS.iter().position(|t| t.eq_ignore_ascii_case(name))?; + let mut f = || -> Option { it.next()?.parse().ok() }; + let (size, pressure, jitter, cx, cy) = (f()?, f()?, f()?, f()?, f()?); + let points: Vec<[f64; 2]> = it + .filter_map(|t| { + let (a, b) = t.split_once(',')?; + Some([a.parse().ok()?, b.parse().ok()?]) + }) + .collect(); + (!points.is_empty()).then_some(LiquifyStroke { tool, size, pressure, jitter, clone_offset: [cx, cy], points }) + } +} + +/// Parse the `distortionMesh` text into strokes (bad lines are skipped). +pub fn parse_strokes(s: &str) -> Vec { + s.lines().filter_map(LiquifyStroke::parse).collect() +} + +/// Displacement mesh: offsets (layer pixels) at grid nodes spaced `cell` apart. +#[derive(Clone, Debug, PartialEq)] +pub struct Mesh { + pub nx: usize, + pub ny: usize, + pub cell: f64, + pub d: Vec<[f64; 2]>, + pub freeze: Vec, +} + +impl Mesh { + fn new(w: f64, h: f64, cell: f64) -> Mesh { + let nx = (w / cell).ceil() as usize + 1; + let ny = (h / cell).ceil() as usize + 1; + Mesh { nx, ny, cell, d: vec![[0.0; 2]; nx * ny], freeze: vec![0.0; nx * ny] } + } + /// Bilinear displacement at a layer point (zero outside the mesh). + pub fn at(&self, x: f64, y: f64) -> [f64; 2] { + let fx = x / self.cell; + let fy = y / self.cell; + if fx < 0.0 || fy < 0.0 || fx > (self.nx - 1) as f64 || fy > (self.ny - 1) as f64 { + return [0.0; 2]; + } + let (x0, y0) = ((fx.floor() as usize).min(self.nx - 2), (fy.floor() as usize).min(self.ny - 2)); + let (x1, y1) = ((x0 + 1).min(self.nx - 1), (y0 + 1).min(self.ny - 1)); + let (tx, ty) = (fx - x0 as f64, fy - y0 as f64); + let g = |i: usize, j: usize| self.d[j * self.nx + i]; + let l = |a: [f64; 2], b: [f64; 2], t: f64| [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]; + l(l(g(x0, y0), g(x1, y0), tx), l(g(x0, y1), g(x1, y1), tx), ty) + } +} + +/// Replay strokes into a displacement mesh over a `w`×`h` layer. `frozen(x, y)` reports +/// mask-frozen points (Freeze Area Mask). +pub fn build_mesh(strokes: &[LiquifyStroke], w: f64, h: f64, frozen: &(dyn Fn(f64, f64) -> bool + Sync)) -> Mesh { + let cell = (w.max(h) / 128.0).clamp(2.0, 16.0); + let mut m = Mesh::new(w, h, cell); + for (i, f) in m.freeze.iter_mut().enumerate() { + let (x, y) = ((i % m.nx) as f64 * cell, (i / m.nx) as f64 * cell); + if frozen(x, y) { + *f = 1.0; + } + } + for (si, s) in strokes.iter().enumerate() { + let r = (s.size * 0.5).max(1.0); + let pr = (s.pressure / 100.0).clamp(0.0, 1.0); + // Dabs every r/4 along the stroke; each dab knows the local motion of the brush. + let mut dabs: Vec<([f64; 2], [f64; 2])> = vec![]; + if s.points.len() == 1 { + dabs.push((s.points[0], [0.0; 2])); + } + for seg in s.points.windows(2) { + let (a, b) = (seg[0], seg[1]); + let len = ((b[0] - a[0]).powi(2) + (b[1] - a[1]).powi(2)).sqrt(); + let n = ((len / (r * 0.25)).ceil() as usize).max(1); + let step = [(b[0] - a[0]) / n as f64, (b[1] - a[1]) / n as f64]; + for k in 1..=n { + dabs.push(([a[0] + step[0] * k as f64, a[1] + step[1] * k as f64], step)); + } + } + for (di, (c, mv)) in dabs.iter().enumerate() { + let (gx0, gx1) = (((c[0] - r) / cell).floor().max(0.0) as usize, (((c[0] + r) / cell).ceil() as usize).min(m.nx - 1)); + let (gy0, gy1) = (((c[1] - r) / cell).floor().max(0.0) as usize, (((c[1] + r) / cell).ceil() as usize).min(m.ny - 1)); + let prev = m.d.clone(); + for gy in gy0..=gy1 { + for gx in gx0..=gx1 { + let (x, y) = (gx as f64 * cell, gy as f64 * cell); + let (dx, dy) = (x - c[0], y - c[1]); + let dist = (dx * dx + dy * dy).sqrt(); + if dist >= r { + continue; + } + let i = gy * m.nx + gx; + // Smooth brush falloff. + let fall = { + let t = 1.0 - dist / r; + t * t * (3.0 - 2.0 * t) + }; + let tool = LIQUIFY_TOOLS[s.tool]; + if tool == "freeze" || tool == "thaw" { + let v = pr * fall; + m.freeze[i] = if tool == "freeze" { m.freeze[i].max(v) } else { (m.freeze[i] - v).max(0.0) }; + continue; + } + let w = pr * fall * (1.0 - m.freeze[i]) * 0.5; + if w <= 0.0 { + continue; + } + let d = &mut m.d[i]; + match tool { + "warp" => { + d[0] -= mv[0] * w * 2.0; + d[1] -= mv[1] * w * 2.0; + } + "turbulence" => { + let j = s.jitter / 100.0 * r * 0.1; + let seed = (si as u32).wrapping_mul(7919) ^ di as u32; + d[0] += (hash1(gx as u32, gy as u32, seed) as f64 * 2.0 - 1.0) * j * w; + d[1] += (hash1(gx as u32, gy as u32, seed ^ 0x55) as f64 * 2.0 - 1.0) * j * w; + d[0] -= mv[0] * w; + d[1] -= mv[1] * w; + } + "twirlClockwise" | "twirlCounterclockwise" => { + let sgn = if tool == "twirlClockwise" { -1.0 } else { 1.0 }; + let a = sgn * 0.08 * w; + let (sn, cs) = a.sin_cos(); + d[0] += dx * cs - dy * sn - dx; + d[1] += dx * sn + dy * cs - dy; + } + "pucker" => { + d[0] += dx * 0.05 * w; + d[1] += dy * 0.05 * w; + } + "bloat" => { + d[0] -= dx * 0.05 * w; + d[1] -= dy * 0.05 * w; + } + "shiftPixels" => { + // Perpendicular to the stroke (to its left). + d[0] -= mv[1] * w * 2.0; + d[1] += mv[0] * w * 2.0; + } + "reflection" => { + // Mirror across the stroke line through the dab. + let l = (mv[0] * mv[0] + mv[1] * mv[1]).sqrt(); + if l > 1e-9 { + let n = [-mv[1] / l, mv[0] / l]; + let k = dx * n[0] + dy * n[1]; + let tgt = [-2.0 * k * n[0], -2.0 * k * n[1]]; + d[0] += (tgt[0] - d[0]) * w; + d[1] += (tgt[1] - d[1]) * w; + } + } + "clone" => { + // Copy the distortion found at the clone source. + let sx = x + s.clone_offset[0]; + let sy = y + s.clone_offset[1]; + let fx = (sx / cell).round().clamp(0.0, (m.nx - 1) as f64) as usize; + let fy = (sy / cell).round().clamp(0.0, (m.ny - 1) as f64) as usize; + let src = prev[fy * m.nx + fx]; + let tgt = [src[0] + s.clone_offset[0], src[1] + s.clone_offset[1]]; + d[0] += (tgt[0] - d[0]) * w; + d[1] += (tgt[1] - d[1]) * w; + } + // Reconstruction: relax toward no distortion. + _ => { + d[0] *= 1.0 - w; + d[1] *= 1.0 - w; + } + } + } + } + } + } + m +} + +type MeshCache = Mutex>>; + +fn cached_mesh(ctx: &EffectCtx) -> Arc { + use std::hash::{Hash, Hasher}; + let text = ctx.params.s("distortionMesh"); + let mask = ctx.params.f("freezeAreaMask").round() as usize; + let poly = mask.checked_sub(1).and_then(|i| ctx.env.masks.get(i)).cloned(); + let mut h = std::collections::hash_map::DefaultHasher::new(); + text.hash(&mut h); + ctx.layer_size[0].to_bits().hash(&mut h); + ctx.layer_size[1].to_bits().hash(&mut h); + if let Some(p) = &poly { + for q in &p.points { + q[0].to_bits().hash(&mut h); + q[1].to_bits().hash(&mut h); + } + p.inverted.hash(&mut h); + } + let key = h.finish(); + static CACHE: OnceLock = OnceLock::new(); + let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new())); + if let Some(m) = cache.lock().ok().and_then(|c| c.get(&key).cloned()) { + return m; + } + let strokes = parse_strokes(text); + let frozen = |x: f64, y: f64| match &poly { + Some(p) => point_in_poly(&p.points, x, y) != p.inverted, + None => false, + }; + let m = Arc::new(build_mesh(&strokes, ctx.layer_size[0].max(1.0), ctx.layer_size[1].max(1.0), &frozen)); + if let Ok(mut c) = cache.lock() { + if c.len() > 32 { + c.clear(); + } + c.insert(key, m.clone()); + } + m +} + +fn liquify(ctx: &EffectCtx, mut b: Buf) -> Buf { + let pct = ctx.params.f("distortionPercentage") / 100.0; + let show_freeze = ctx.params.b("viewFreezeAreaMask"); + let show_mesh = ctx.params.b("viewMesh"); + if ctx.params.s("distortionMesh").trim().is_empty() && !show_freeze && !show_mesh { + return b; + } + let mesh = cached_mesh(ctx); + let off = ctx.params.v2("distortionMeshOffset"); + let mesh_color = ctx.params.color("meshColor"); + let src = b.img.clone(); + let (scale, o) = (b.scale, b.offset); + let line_w = 0.6 / scale.max(1e-6); + b.img.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let lx = (x as f64 + 0.5 - o[0]) / scale; + let ly = (y as f64 + 0.5 - o[1]) / scale; + let d = mesh.at(lx - off[0], ly - off[1]); + let (sx, sy) = (lx + d[0] * pct, ly + d[1] * pct); + let mut c = if d == [0.0; 2] { *px } else { src.sample_bilinear(sx * scale + o[0], sy * scale + o[1]) }; + if show_freeze { + let fx = ((lx - off[0]) / mesh.cell).round().clamp(0.0, (mesh.nx - 1) as f64) as usize; + let fy = ((ly - off[1]) / mesh.cell).round().clamp(0.0, (mesh.ny - 1) as f64) as usize; + let f = mesh.freeze[fy * mesh.nx + fx] as f32; + if f > 0.0 { + c[0] = c[0] * (1.0 - 0.5 * f) + 0.5 * f; + c[3] = c[3].max(0.5 * f); + } + } + if show_mesh { + // Grid lines every 4 mesh cells, displaced with the mesh. + let gs = mesh.cell * 4.0; + let (gx, gy) = ((lx + d[0] * pct - off[0]).rem_euclid(gs), (ly + d[1] * pct - off[1]).rem_euclid(gs)); + if gx < line_w || gy < line_w { + c = [mesh_color[0], mesh_color[1], mesh_color[2], 1.0]; + } + } + *px = c; + } + }); + b +} + +// ---------------------------------------------------------------- Rolling Shutter Repair + +/// Per-pixel velocity field (layer pixels per frame) on a coarse grid, from tracked points. +struct Flow { + nx: usize, + ny: usize, + cell: f64, + v: Vec<[f64; 2]>, +} + +impl Flow { + fn at(&self, x: f64, y: f64) -> [f64; 2] { + let fx = (x / self.cell - 0.5).clamp(0.0, (self.nx - 1) as f64); + let fy = (y / self.cell - 0.5).clamp(0.0, (self.ny - 1) as f64); + let (x0, y0) = (fx.floor() as usize, fy.floor() as usize); + let (x1, y1) = ((x0 + 1).min(self.nx - 1), (y0 + 1).min(self.ny - 1)); + let (tx, ty) = (fx - x0 as f64, fy - y0 as f64); + let g = |i: usize, j: usize| self.v[j * self.nx + i]; + let l = |a: [f64; 2], b: [f64; 2], t: f64| [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]; + l(l(g(x0, y0), g(x1, y0), tx), l(g(x0, y1), g(x1, y1), tx), ty) + } +} + +fn median(v: &mut [f64]) -> f64 { + v.sort_by(f64::total_cmp); + v[v.len() / 2] +} + +/// Measure motion around the current frame (`cur`) from `prev` to `next` (all in the same +/// buffer grid). `per_row`: Warp method (one velocity per scanline band). +fn measure(cur: &Image, prev: Option<&Image>, next: Option<&Image>, scale: f64, detailed: bool, per_row: bool) -> Option { + use effectcraft_track::klt::{GrayPyramid, LkOpts, analysis_factor, track}; + let factor = analysis_factor(cur.width, cur.height, if detailed { 640 } else { 320 }); + let pc = GrayPyramid::from_image(cur, [0.0; 2], factor, 4); + let (w, h) = (pc.width(), pc.height()); + let n = if detailed { 24 } else { 12 }; + let cell_w = w as f64 / n as f64; + let rows = ((n as f64) * h as f64 / w.max(1) as f64).round().max(2.0) as usize; + let cell_h = h as f64 / rows as f64; + let pts: Vec<[f64; 2]> = (0..rows).flat_map(|j| (0..n).map(move |i| [(i as f64 + 0.5) * cell_w, (j as f64 + 0.5) * cell_h])).collect(); + let o = LkOpts { radius: 4, fb_max: 1.0, ..Default::default() }; + let go = |other: Option<&Image>| other.map(|img| track(&pc, &GrayPyramid::from_image(img, [0.0; 2], factor, 4), &pts, None, &o)); + let (tn, tp) = (go(next), go(prev)); + // Velocity in plane pixels per frame. + let vel: Vec> = (0..pts.len()) + .map(|i| { + let p = pts[i]; + let fw = tn.as_ref().and_then(|t| t[i]).map(|q| [q[0] - p[0], q[1] - p[1]]); + let bw = tp.as_ref().and_then(|t| t[i]).map(|q| [p[0] - q[0], p[1] - q[1]]); + match (fw, bw) { + (Some(a), Some(b)) => Some([(a[0] + b[0]) * 0.5, (a[1] + b[1]) * 0.5]), + (a, b) => a.or(b), + } + }) + .collect(); + let valid: Vec<[f64; 2]> = vel.iter().flatten().copied().collect(); + if valid.is_empty() { + return None; + } + let mut vx: Vec = valid.iter().map(|v| v[0]).collect(); + let mut vy: Vec = valid.iter().map(|v| v[1]).collect(); + let global = [median(&mut vx), median(&mut vy)]; + let k = factor as f64 / scale.max(1e-9); + let mut v = vec![[0.0; 2]; n * rows]; + for j in 0..rows { + // Row (band) median for Warp; per point (outliers replaced) for Pixel Motion. + let band: Vec<[f64; 2]> = (0..n).filter_map(|i| vel[j * n + i]).collect(); + let row_med = if band.is_empty() { + global + } else { + let mut a: Vec = band.iter().map(|v| v[0]).collect(); + let mut b: Vec = band.iter().map(|v| v[1]).collect(); + [median(&mut a), median(&mut b)] + }; + for i in 0..n { + let s = if per_row { + row_med + } else { + match vel[j * n + i] { + Some(q) if (q[0] - row_med[0]).abs() + (q[1] - row_med[1]).abs() < 3.0 + 0.5 * (row_med[0].abs() + row_med[1].abs()) => q, + _ => row_med, + } + }; + v[j * n + i] = [s[0] * k, s[1] * k]; + } + } + // Cell size in layer pixels (grid is uniform in plane pixels; assume square plane pixels). + Some(Flow { nx: n, ny: rows, cell: cell_w * k, v }) +} + +fn same_grid(nb: Buf, b: &Buf) -> Image { + if nb.img.width == b.img.width && nb.img.height == b.img.height && nb.offset == b.offset && nb.scale == b.scale { + return nb.img; + } + let k = nb.scale / b.scale.max(1e-9); + crate::util::gen_image(b.img.width, b.img.height, |x, y| { + let lx = (x as f64 + 0.5 - b.offset[0]) * k + nb.offset[0]; + let ly = (y as f64 + 0.5 - b.offset[1]) * k + nb.offset[1]; + nb.img.sample_bilinear(lx, ly) + }) +} + +fn rolling_shutter(ctx: &EffectCtx, mut b: Buf) -> Buf { + let rate = ctx.params.f("rollingShutterRate") / 100.0; + if rate == 0.0 { + return b; + } + let Some(host) = ctx.env.host else { return b }; + let fps = ctx.fps(); + let prev = host.self_at(ctx.time - 1.0 / fps, ctx.env.effect_index).map(|nb| same_grid(nb, &b)); + let next = host.self_at(ctx.time + 1.0 / fps, ctx.env.effect_index).map(|nb| same_grid(nb, &b)); + if prev.is_none() && next.is_none() { + return b; + } + let per_row = ctx.params.e("method") == 0; + let Some(flow) = measure(&b.img, prev.as_ref(), next.as_ref(), 1.0, ctx.params.b("detailedAnalysis"), per_row) else { return b }; + let dir = ctx.params.e("scanDirection"); + let src = b.img.clone(); + let (w, h) = (b.img.width as f64, b.img.height as f64); + b.img.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let (fx, fy) = (x as f64 + 0.5, y as f64 + 0.5); + let s = match dir { + 1 => 1.0 - fy / h, + 2 => fx / w, + 3 => 1.0 - fx / w, + _ => fy / h, + }; + let tau = rate * (s - 0.5); + let v = flow.at(fx, fy); + *px = src.sample_bilinear_clamped(fx + v[0] * tau, fy + v[1] * tau); + } + }); + b +} + +pub fn specs() -> Vec { + vec![ + spec( + "ec.distort.ccflomotion", + "CC Flo Motion", + vec![ + p("knot1", "Knot 1", Value::Vec2([0.25, 0.5]), ParamUi::Point), + p("amount1", "Amount 1", num(50.0), slider(-400.0, 100.0, -100.0, 100.0, 1)), + p("knot2", "Knot 2", Value::Vec2([0.75, 0.5]), ParamUi::Point), + p("amount2", "Amount 2", num(-50.0), slider(-400.0, 100.0, -100.0, 100.0, 1)), + p("tileEdges", "Tile Edges", Value::Bool(false), ParamUi::Checkbox), + p("antialiasing", "Antialiasing", Value::Enum(1), popup(&["Low", "Medium", "High"])), + p("falloff", "Falloff", num(2.0), slider(0.01, 10.0, 0.1, 5.0, 2)), + ], + flo_motion, + ), + spec( + "ec.distort.liquify", + "Liquify", + vec![ + p("tool", "Tool", Value::Enum(0), popup(&LIQUIFY_TOOLS)), + p("brushSize", "Brush Size", num(64.0), slider(1.0, 600.0, 1.0, 600.0, 0)), + p("brushPressure", "Brush Pressure", num(50.0), slider(1.0, 100.0, 1.0, 100.0, 0)), + p("freezeAreaMask", "Freeze Area Mask", num(0.0), ParamUi::Mask), + p("turbulentJitter", "Turbulent Jitter", num(70.0), slider(1.0, 100.0, 1.0, 100.0, 0)), + p("cloneOffset", "Clone Offset", Value::Vec2([0.0, 0.0]), ParamUi::Hidden), + p("reconstructionMode", "Reconstruction Mode", Value::Enum(0), popup(&["Revert", "Displace", "Amplitwist", "Affine"])), + p("viewFreezeAreaMask", "View Freeze Area Mask", Value::Bool(false), ParamUi::Checkbox), + p("viewMesh", "View Mesh", Value::Bool(false), ParamUi::Checkbox), + p("meshSize", "Mesh Size", Value::Enum(1), popup(&["Small", "Medium", "Large"])), + p("meshColor", "Mesh Color", col(0.5, 0.5, 0.5), ParamUi::Color), + // The strokes (see `LiquifyStroke`); written by the `liquify.stroke` command. + p("distortionMesh", "Distortion Mesh", Value::Str(String::new()), ParamUi::Hidden), + p("distortionMeshOffset", "Distortion Mesh Offset", Value::Vec2([0.0, 0.0]), ParamUi::Hidden), + p("distortionPercentage", "Distortion Percentage", num(100.0), slider(0.0, 200.0, 0.0, 200.0, 1)), + p("distortionDisplayQuality", "Distortion Display Quality", Value::Enum(1), popup(&["Draft", "Smooth"])), + ], + liquify, + ), + spec( + "ec.distort.rollingshutterrepair", + "Rolling Shutter Repair", + vec![ + p("rollingShutterRate", "Rolling Shutter Rate", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("scanDirection", "Scan Direction", Value::Enum(0), popup(&["Top → Bottom", "Bottom → Top", "Left → Right", "Right → Left"])), + p("method", "Method", Value::Enum(0), popup(&["Warp", "Pixel Motion"])), + p("detailedAnalysis", "Detailed Analysis", Value::Bool(false), ParamUi::Checkbox), + p("pixelMotionDetail", "Pixel Motion Detail", num(20.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + ], + rolling_shutter, + ), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, EffectHost, LayerPixels, run_fx}; + + fn grid_img(w: u32, h: u32) -> Image { + crate::util::gen_image(w, h, |x, y| { + let v = if x % 8 == 0 || y % 8 == 0 { 1.0 } else { 0.2 }; + [v, v * 0.5, x as f32 / w as f32, 1.0] + }) + } + + #[test] + fn flo_motion_pulls_and_pushes() { + let img = grid_img(64, 64); + let out = run_fx( + "ec.distort.ccflomotion", + &[("knot1", Value::Vec2([32.0, 32.0])), ("amount1", num(80.0)), ("amount2", num(0.0))], + img.clone(), + 0.0, + EffectEnv::default(), + ); + // The knot itself is fixed; pixels near it are magnified (sampled closer to the knot). + assert!((out.img.get(32, 32)[2] - img.get(32, 32)[2]).abs() < 0.01); + assert!((out.img.get(40, 32)[2] - img.get(40, 32)[2]).abs() > 0.01); + assert!(out.img.get(40, 32)[2] < img.get(40, 32)[2]); + let none = run_fx("ec.distort.ccflomotion", &[("amount1", num(0.0)), ("amount2", num(0.0))], img.clone(), 0.0, EffectEnv::default()); + assert_eq!(none.img.data, img.data); + let again = run_fx( + "ec.distort.ccflomotion", + &[("knot1", Value::Vec2([32.0, 32.0])), ("amount1", num(80.0)), ("amount2", num(0.0))], + img, + 0.0, + EffectEnv::default(), + ); + assert_eq!(out.img.data, again.img.data); + } + + #[test] + fn liquify_strokes_round_trip_and_warp() { + let s = LiquifyStroke { tool: 0, size: 30.0, pressure: 80.0, jitter: 70.0, clone_offset: [0.0, 0.0], points: vec![[20.0, 32.0], [40.0, 32.0]] }; + assert_eq!(LiquifyStroke::parse(&s.to_line()), Some(s.clone())); + assert!(LiquifyStroke::parse("nope 1 2 3 4 5 1,2").is_none()); + let img = grid_img(64, 64); + let out = run_fx("ec.distort.liquify", &[("distortionMesh", Value::Str(s.to_line()))], img.clone(), 0.0, EffectEnv::default()); + assert_ne!(out.img.data, img.data); + // Warping to the right: a pixel right of the start now shows content from its left. + let at = |i: &Image| i.get(36, 32)[2]; + assert!(at(&out.img) < at(&img), "{} vs {}", at(&out.img), at(&img)); + // Far away, untouched. + assert_eq!(out.img.get(5, 5), img.get(5, 5)); + // Distortion Percentage 0 = identity; deterministic. + let zero = run_fx( + "ec.distort.liquify", + &[("distortionMesh", Value::Str(s.to_line())), ("distortionPercentage", num(0.0))], + img.clone(), + 0.0, + EffectEnv::default(), + ); + assert_eq!(zero.img.data, img.data); + let again = run_fx("ec.distort.liquify", &[("distortionMesh", Value::Str(s.to_line()))], img.clone(), 0.0, EffectEnv::default()); + assert_eq!(out.img.data, again.img.data); + // Reconstruction undoes the warp; freeze protects. + let mut rec = s.clone(); + rec.tool = 9; + rec.pressure = 100.0; + let lines = format!("{}\n{}\n{}\n{}", s.to_line(), rec.to_line(), rec.to_line(), rec.to_line()); + let m1 = build_mesh(&parse_strokes(&s.to_line()), 64.0, 64.0, &|_, _| false); + let m2 = build_mesh(&parse_strokes(&lines), 64.0, 64.0, &|_, _| false); + let mag = |m: &Mesh| m.d.iter().map(|d| d[0].abs() + d[1].abs()).sum::(); + assert!(mag(&m2) < mag(&m1) * 0.5); + let frozen = build_mesh(&parse_strokes(&s.to_line()), 64.0, 64.0, &|_, _| true); + assert_eq!(mag(&frozen), 0.0); + // Every tool changes (or deliberately keeps) the mesh without NaNs. + for t in 0..LIQUIFY_TOOLS.len() { + let st = LiquifyStroke { tool: t, clone_offset: [10.0, 0.0], ..s.clone() }; + let m = build_mesh(&[s.clone(), st], 64.0, 64.0, &|_, _| false); + assert!(m.d.iter().all(|d| d[0].is_finite() && d[1].is_finite()), "{}", LIQUIFY_TOOLS[t]); + } + } + + /// A textured frame panning right at 3 px per frame, captured with a rolling shutter: row + /// `y` is exposed `(y / h − ½)` frames late (rate 100 %). + fn texture(x: f64, y: f64) -> f32 { + let a = ((x * 0.31).sin() * (y * 0.23).cos() + (x * 0.07 + y * 0.11).sin()) as f32; + 0.5 + 0.25 * a + } + fn rs_frame(f: f64, shutter: bool) -> Image { + let (w, h) = (96u32, 64u32); + crate::util::gen_image(w, h, |x, y| { + let tau = if shutter { (y as f64 + 0.5) / h as f64 - 0.5 } else { 0.0 }; + let shift = 3.0 * (f + tau); + let v = texture(x as f64 + 0.5 - shift, y as f64 + 0.5); + [v, v, v, 1.0] + }) + } + struct Pan; + impl EffectHost for Pan { + fn layer(&self, _: u64, _: bool) -> Option { + None + } + fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option> { + None + } + fn self_at(&self, t: f64, _: usize) -> Option { + Some(Buf { img: rs_frame(t * 24.0, true), offset: [0.0; 2], scale: 1.0 }) + } + } + + #[test] + fn rolling_shutter_repair_unskews_a_pan() { + let host = Pan; + let env = EffectEnv { host: Some(&host), frame_rate: 24.0, ..Default::default() }; + let t = 5.0 / 24.0; + let skewed = rs_frame(5.0, true); + let truth = rs_frame(5.0, false); + let err = |a: &Image| { + let mut s = 0.0; + let mut n = 0.0; + for y in 4..60 { + for x in 12..84 { + s += (a.get(x, y)[0] - truth.get(x, y)[0]).abs(); + n += 1.0; + } + } + s / n + }; + for method in [0u32, 1] { + let out = run_fx("ec.distort.rollingshutterrepair", &[("rollingShutterRate", num(100.0)), ("method", Value::Enum(method))], skewed.clone(), t, env); + assert!(err(&out.img) < err(&skewed) * 0.4, "method {method}: {} vs {}", err(&out.img), err(&skewed)); + let again = + run_fx("ec.distort.rollingshutterrepair", &[("rollingShutterRate", num(100.0)), ("method", Value::Enum(method))], skewed.clone(), t, env); + assert_eq!(out.img.data, again.img.data); + } + let none = run_fx("ec.distort.rollingshutterrepair", &[], skewed.clone(), t, EffectEnv::default()); + assert_eq!(none.img.data, skewed.data); + } +} diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 14a43464..746500e6 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -8,6 +8,7 @@ pub mod audio_fx; mod blur2; +mod blur3; mod channel; mod channel2; mod color2; @@ -17,6 +18,7 @@ mod controls; mod distort; mod distort2; mod distort3; +pub mod distort4; mod generate; mod generate2; mod generate3; @@ -356,6 +358,8 @@ pub fn registry() -> &'static [EffectSpec] { v.extend(obsolete::specs()); v.extend(ocio::specs()); v.extend(vr::specs()); + v.extend(blur3::specs()); + v.extend(distort4::specs()); v.extend(time_fx::specs()); v.extend(audio_fx::specs()); v.extend(paint::specs()); @@ -472,6 +476,8 @@ pub const TIME_DEPENDENT: &[&str] = &[ "ec.time.ccwidetime", "ec.time.pixelmotionblur", "ec.vr.digitalglitch", + "ec.blur.camerashakedeblur", + "ec.distort.rollingshutterrepair", ]; /// See [`TIME_DEPENDENT`]. diff --git a/crates/engine/src/commands/liquify.rs b/crates/engine/src/commands/liquify.rs new file mode 100644 index 00000000..748c664c --- /dev/null +++ b/crates/engine/src/commands/liquify.rs @@ -0,0 +1,156 @@ +//! Liquify brush strokes (`liquify.stroke`, `liquify.clear`). +//! +//! A stroke is appended to the layer's Liquify effect (added when the layer has none) as one +//! line of its hidden Distortion Mesh data (see `effectcraft_effects::distort4`). Brush size, +//! pressure, turbulent jitter and clone offset default to the effect's own tool options. When +//! the Distortion Mesh is animated, the stroke goes into a keyframe at the current time (copying +//! the mesh in effect there), as After Effects records mesh keyframes. + +use effectcraft_effects::distort4::{LIQUIFY_TOOLS, LiquifyStroke, parse_strokes}; +use effectcraft_keyframe::{Keyframe, Value as KV}; +use effectcraft_project::build::Ids; +use effectcraft_project::{GroupKind, PropGroup, Uid}; +use serde_json::{Value, json}; + +use super::{CommandSpec, bad, f_p, has_comp, layer_mut, layer_p, str_p}; +use crate::{EngineError, Result, Session, cmd}; + +const ID: &str = "ec.distort.liquify"; + +fn is_liquify(g: &PropGroup) -> bool { + matches!(&g.kind, GroupKind::Effect { effect } if effect == ID) +} + +fn liquify_effect(fx: &mut PropGroup, ids: &mut Ids, layer_size: [f64; 2], want: Option) -> Result { + if let Some(u) = want { + return fx.groups().find(|g| g.uid == u && is_liquify(g)).map(|g| g.uid).ok_or_else(|| bad("liquify.stroke", "no such Liquify effect")); + } + if let Some(g) = fx.groups().filter(|g| is_liquify(g)).last() { + return Ok(g.uid); + } + let spec = effectcraft_effects::find(ID).ok_or_else(|| EngineError::Other("Liquify effect missing".into()))?; + let g = effectcraft_effects::instantiate(spec, ids, "Liquify", layer_size); + let uid = g.uid; + fx.children.push(g.into()); + Ok(uid) +} + +fn num_of(g: &PropGroup, id: &str, default: f64) -> f64 { + g.get(id).map(|p| p.value.as_f64()).unwrap_or(default) +} + +fn stroke(s: &mut Session, p: &Value) -> Result { + let cmd = "liquify.stroke"; + let (cid, lid) = layer_p(s, p, cmd)?; + let tool_name = str_p(p, "tool").unwrap_or("warp"); + let tool = LIQUIFY_TOOLS + .iter() + .position(|t| t.eq_ignore_ascii_case(tool_name)) + .ok_or_else(|| bad(cmd, format!("unknown tool `{tool_name}` ({})", LIQUIFY_TOOLS.join(" | "))))?; + let raw = p.get("points").and_then(Value::as_array).ok_or_else(|| bad(cmd, "missing `points` [[x, y], …] (layer space)"))?; + let mut points = vec![]; + for q in raw { + let a = q.as_array().ok_or_else(|| bad(cmd, "each point is [x, y]"))?; + let (Some(x), Some(y)) = (a.first().and_then(Value::as_f64), a.get(1).and_then(Value::as_f64)) else { + return Err(bad(cmd, "each point is [x, y]")); + }; + points.push([x, y]); + } + if points.is_empty() { + return Err(bad(cmd, "`points` is empty")); + } + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; + let t = comp.frame_rate.snap_nearest(super::time_p(s, p, Some(comp))); + let layer = comp.layer(lid).ok_or_else(|| bad(cmd, "no layer"))?; + if layer.effects().is_none() || !layer.source.is_av() { + return Err(bad(cmd, "Liquify works on footage, solid, text, shape and precomp layers")); + } + let lt = layer.layer_time(t); + let (w, h) = effectcraft_render::source_size(&s.project, layer); + let size = if w == 0 { [comp.width as f64, comp.height as f64] } else { [w as f64, h as f64] }; + let want = p.get("effect").and_then(Value::as_u64); + let (fx_uid, count) = s.edit("Liquify Stroke", None, |proj, _st| { + let mut next = proj.next_id; + let l = layer_mut(proj, cid, lid)?; + let fx = l.props.sub_mut("effects").ok_or_else(|| bad(cmd, "this layer has no effects"))?; + let mut ids = Ids(&mut next); + let fx_uid = liquify_effect(fx, &mut ids, size, want)?; + proj.next_id = next; + let l = layer_mut(proj, cid, lid)?; + let g = l.props.sub_mut("effects").and_then(|fx| fx.find_group_mut(fx_uid)).ok_or_else(|| bad(cmd, "Liquify effect gone"))?; + let co = g.get("cloneOffset").map(|p| p.value.as_vec2()).unwrap_or([0.0; 2]); + let st = LiquifyStroke { + tool, + size: f_p(p, "size").unwrap_or_else(|| num_of(g, "brushSize", 64.0)).max(1.0), + pressure: f_p(p, "pressure").unwrap_or_else(|| num_of(g, "brushPressure", 50.0)).clamp(1.0, 100.0), + jitter: f_p(p, "jitter").unwrap_or_else(|| num_of(g, "turbulentJitter", 70.0)).clamp(1.0, 100.0), + clone_offset: match p.get("cloneOffset").and_then(Value::as_array) { + Some(a) => [a.first().and_then(Value::as_f64).unwrap_or(0.0), a.get(1).and_then(Value::as_f64).unwrap_or(0.0)], + None => co, + }, + points: points.clone(), + }; + let prop = g.get_mut("distortionMesh").ok_or_else(|| bad(cmd, "Liquify effect has no Distortion Mesh"))?; + let append = |cur: &str| if cur.trim().is_empty() { st.to_line() } else { format!("{}\n{}", cur.trim_end(), st.to_line()) }; + let text = if prop.keys.is_empty() { + let cur = match &prop.value { + KV::Str(x) => x.clone(), + _ => String::new(), + }; + let v = append(&cur); + prop.value = KV::Str(v.clone()); + v + } else { + // Hold keyframes: the mesh in effect at `lt` is the last key at or before it. + let cur = prop.keys.iter().rfind(|k| k.time <= lt).or(prop.keys.first()).map(|k| k.value.clone()); + let cur = match cur { + Some(KV::Str(x)) => x, + _ => String::new(), + }; + let v = append(&cur); + match prop.keys.iter_mut().find(|k| k.time == lt) { + Some(k) => k.value = KV::Str(v.clone()), + None => { + prop.keys.push(Keyframe::new(lt, KV::Str(v.clone()))); + prop.keys.sort_by_key(|k| k.time); + } + } + v + }; + Ok((fx_uid, parse_strokes(&text).len())) + })?; + Ok(json!({"effect": fx_uid, "strokes": count})) +} + +fn clear(s: &mut Session, p: &Value) -> Result { + let cmd = "liquify.clear"; + let (cid, lid) = layer_p(s, p, cmd)?; + let want = p.get("effect").and_then(Value::as_u64); + s.edit("Clear Liquify Mesh", None, |proj, _| { + let l = layer_mut(proj, cid, lid)?; + let fx = l.props.sub_mut("effects").ok_or_else(|| bad(cmd, "no effects"))?; + let uid = + fx.groups().filter(|g| is_liquify(g) && want.is_none_or(|u| u == g.uid)).last().map(|g| g.uid).ok_or_else(|| bad(cmd, "no Liquify effect"))?; + let g = fx.find_group_mut(uid).ok_or_else(|| bad(cmd, "no Liquify effect"))?; + let prop = g.get_mut("distortionMesh").ok_or_else(|| bad(cmd, "no Distortion Mesh"))?; + prop.keys.clear(); + prop.value = KV::Str(String::new()); + Ok(()) + })?; + Ok(Value::Null) +} + +pub fn specs() -> Vec { + vec![ + cmd!( + "liquify.stroke", + "Liquify Stroke", + [], + None, + "{layer?, effect?: uid, tool?: warp|turbulence|twirlClockwise|twirlCounterclockwise|pucker|bloat|shiftPixels|reflection|clone|reconstruction|freeze|thaw, points: [[x,y],…] (layer space), size?, pressure? (1-100), jitter? (1-100), cloneOffset?: [dx,dy], time? (s) | frame?}", + has_comp, + stroke + ), + cmd!("liquify.clear", "Clear Liquify Mesh", [], None, "{layer?, effect?: uid}", has_comp, clear), + ] +} diff --git a/crates/engine/src/commands/mod.rs b/crates/engine/src/commands/mod.rs index 26db85c9..4f0910ba 100644 --- a/crates/engine/src/commands/mod.rs +++ b/crates/engine/src/commands/mod.rs @@ -17,6 +17,7 @@ mod layer; mod layer_menu; mod layer_time; mod link; +mod liquify; mod lottie; mod markers; mod mask; @@ -127,6 +128,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(track::specs()); v.extend(paint::specs()); v.extend(puppet::specs()); + v.extend(liquify::specs()); v.extend(settings::specs()); v.extend(stubs::specs()); v diff --git a/crates/engine/src/tests_paint.rs b/crates/engine/src/tests_paint.rs index e274f725..9926e04c 100644 --- a/crates/engine/src/tests_paint.rs +++ b/crates/engine/src/tests_paint.rs @@ -227,3 +227,26 @@ fn project_with_paint_and_puppet_round_trips() { let v: Value = serde_json::from_str(&j).unwrap(); assert!(v.to_string().contains("ec.paint.paint") && v.to_string().contains("ec.distort.puppet")); } + +#[test] +fn liquify_stroke_adds_the_effect_and_warps() { + let (mut s, id) = setup(); + // Layer (5, 30) is comp (55, 50). Before: opaque blue. + assert!(px(&s, 0.0, 55, 50)[3] > 0.99); + let r = s.execute("liquify.stroke", json!({"layer": id, "tool": "warp", "points": [[-20, 30], [10, 30]], "size": 40, "pressure": 100})).unwrap(); + assert_eq!(r["strokes"], 1); + let l = layer(&s, id); + let fx = l.effects().unwrap().groups().next().unwrap().clone(); + assert!(matches!(&fx.kind, effectcraft_project::GroupKind::Effect { effect } if effect == "ec.distort.liquify")); + // The warp pulls transparent pixels from outside the layer over its left edge. + assert!(px(&s, 0.0, 55, 50)[3] < 0.9, "{:?}", px(&s, 0.0, 55, 50)); + // A second stroke goes into the same effect. + let r2 = s.execute("liquify.stroke", json!({"layer": id, "tool": "reconstruction", "points": [[0, 30]], "size": 60, "pressure": 100})).unwrap(); + assert_eq!(r2["strokes"], 2); + assert_eq!(r2["effect"], r["effect"]); + assert!(s.execute("liquify.stroke", json!({"layer": id, "tool": "smudge", "points": [[0, 0]]})).is_err()); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + assert!(layer(&s, id).effects().unwrap().groups().next().is_none()); + assert!(px(&s, 0.0, 55, 50)[3] > 0.99); +} diff --git a/docs/architecture.md b/docs/architecture.md index c3d478e3..f7819a7f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -35,7 +35,7 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for | L5 | `automation` | The MCP server, headless or bridged to the running app | | L6 | apps `effectcraft`, `effectcraft-cli`, `effectcraft-web` | Desktop app; command-line tool (render, exec, get/set, MCP); the browser app (wasm32, [web.md](web.md)) | -Allowed same-layer edges: `path → keyframe, raster`, `text → path`, `effects → project, text, path`, +Allowed same-layer edges: `path → keyframe, raster`, `text → path`, `effects → project, text, path, track`, `media / expr / export / gpu → render`, `export → media`, `lottie → format`, `host → engine`. ## 2. Time diff --git a/xtask/src/main.rs b/xtask/src/main.rs index ae38adf9..76ff6fa3 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -55,6 +55,7 @@ const SAME_LAYER: &[(&str, &str)] = &[ ("effects", "project"), ("effects", "text"), ("effects", "path"), + ("effects", "track"), ("media", "render"), ("expr", "render"), ("export", "render"), From f95a600722208543ae88d5477d2f389a1a099664 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:11:50 -0400 Subject: [PATCH 06/23] M9.11: Key Light (Keylight 1.2-equivalent keyer) and mocha shape Key Light is registered under a generic name (Keylight is a vendor trademark); lookup() resolves "Keylight (1.2)" to it. mocha shape reads a documented JSON shape format (Mocha's own export format is not public). Co-Authored-By: Claude Opus 5.5 --- crates/effects/Cargo.toml | 3 +- crates/effects/src/keylight.rs | 455 ++++++++++++++++++++++++++++++ crates/effects/src/lib.rs | 10 +- crates/effects/src/mocha_shape.rs | 243 ++++++++++++++++ 4 files changed, 708 insertions(+), 3 deletions(-) create mode 100644 crates/effects/src/keylight.rs create mode 100644 crates/effects/src/mocha_shape.rs diff --git a/crates/effects/Cargo.toml b/crates/effects/Cargo.toml index e321fa2f..ff5d3ed7 100644 --- a/crates/effects/Cargo.toml +++ b/crates/effects/Cargo.toml @@ -17,6 +17,5 @@ effectcraft-project = { workspace = true } effectcraft-raster = { workspace = true } effectcraft-track = { workspace = true } rayon = { workspace = true } - -[dev-dependencies] serde_json = { workspace = true } + diff --git a/crates/effects/src/keylight.rs b/crates/effects/src/keylight.rs new file mode 100644 index 00000000..980a6169 --- /dev/null +++ b/crates/effects/src/keylight.rs @@ -0,0 +1,455 @@ +//! Key Light — a full screen-colour keyer with the controls of After Effects' bundled +//! "Keylight (1.2)" (that name is a trademark of its vendor, so the effect is registered as +//! **Key Light**; `lookup("Keylight (1.2)")` finds it through [`KEYLIGHT_ALIASES`]). +//! +//! The method is the textbook *screen difference* key, written from the public description of +//! the controls: +//! +//! * colours are first neutralised by the **Alpha Bias** / **Despill Bias** colours (each channel +//! scaled so the bias colour becomes grey — mid grey = no change); +//! * the **screen difference** of a pixel is its screen-primary channel minus a balance-weighted +//! mix of the other two (**Screen Balance**); the raw matte is +//! `1 − Screen Gain × diff(pixel) / diff(screen colour)`; +//! * the **Screen Matte** is then clipped (Clip Black / White, with Clip Rollback restoring edge +//! detail), despotted (morphological open/close: Despot Black / White), shrunk or grown and +//! softened, combined with the **Inside** / **Outside** masks and the source alpha; +//! * the foreground is the source with the screen colour removed in proportion to the +//! transparency (`(c − (1 − α)·S) / α`), then **despilled** (the primary is limited to the +//! balanced secondary), with **Replace Method** colouring areas whose alpha was raised by +//! clipping or the inside mask, and optional foreground and edge colour correction. +//! +//! Views: Source, Source Alpha, Corrected Source, Colour Correction Edges, Screen Matte, Inside +//! Mask, Outside Mask, Combined Matte, Status, Intermediate Result and Final Result. + +use effectcraft_color::luminance; +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use effectcraft_raster::{Image, gaussian_blur}; +use rayon::prelude::*; + +use crate::util::{Plane, gauss_plane, morph_frac, point_in_poly, premul, unpremul}; +use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider}; + +/// Other names the effect answers to in [`crate::lookup`]. +pub const KEYLIGHT_ALIASES: &[&str] = &["Keylight (1.2)", "Keylight"]; + +pub const VIEWS: [&str; 11] = [ + "Source", + "Source Alpha", + "Corrected Source", + "Colour Correction Edges", + "Screen Matte", + "Inside Mask", + "Outside Mask", + "Combined Matte", + "Status", + "Intermediate Result", + "Final Result", +]; + +const REPLACE: [&str; 4] = ["None", "Source", "Hard Colour", "Soft Colour"]; + +fn pct(d: f64) -> (Value, ParamUi) { + (num(d), slider(0.0, 100.0, 0.0, 100.0, 1)) +} + +fn mask_popup() -> ParamUi { + popup(&["None", "Mask 1", "Mask 2", "Mask 3", "Mask 4", "Mask 5", "Mask 6", "Mask 7", "Mask 8", "Mask 9", "Mask 10"]) +} + +/// Index of the screen's primary channel and the two others (larger-in-screen first). +fn primary(s: [f32; 3]) -> (usize, usize, usize) { + let pi = if s[1] >= s[0] && s[1] >= s[2] { + 1 + } else if s[2] >= s[0] { + 2 + } else { + 0 + }; + let (a, b) = match pi { + 0 => (1, 2), + 1 => (0, 2), + _ => (0, 1), + }; + if s[a] >= s[b] { (pi, a, b) } else { (pi, b, a) } +} + +/// Screen difference: primary minus the balance-weighted secondaries. +#[inline] +pub fn screen_diff(c: [f32; 3], pi: usize, o1: usize, o2: usize, bal: f32) -> f32 { + c[pi] - (bal * c[o1] + (1.0 - bal) * c[o2]) +} + +/// Scale channels so `bias` becomes neutral (grey keeps everything as is). +#[inline] +fn neutralise(c: [f32; 3], bias: [f32; 3]) -> [f32; 3] { + let l = luminance(bias[0], bias[1], bias[2]).max(1e-4); + [0, 1, 2].map(|i| c[i] * l / bias[i].max(1e-4)) +} + +fn mask_coverage(ctx: &EffectCtx, b: &Buf, idx: u32, softness: f64, invert: bool) -> Option { + let shape = ctx.env.masks.get((idx as usize).checked_sub(1)?)?; + let (w, h) = (b.img.width as usize, b.img.height as usize); + let mut pl = Plane::new(w, h); + let inv = 1.0 / b.scale.max(1e-9); + pl.data.par_chunks_mut(w.max(1)).enumerate().for_each(|(y, row)| { + for (x, v) in row.iter_mut().enumerate() { + let lx = (x as f64 + 0.5 - b.offset[0]) * inv; + let ly = (y as f64 + 0.5 - b.offset[1]) * inv; + *v = if point_in_poly(&shape.points, lx, ly) != shape.inverted { 1.0 } else { 0.0 }; + } + }); + let s = softness * b.scale; + if s > 0.0 { + pl = gauss_plane(&pl, s * 0.5, s * 0.5); + } + if invert { + pl = pl.map(|v| 1.0 - v); + } + Some(pl) +} + +/// Saturation / contrast / brightness correction of a straight colour. +fn correct(c: [f32; 3], sat: f32, contrast: f32, bright: f32) -> [f32; 3] { + let l = luminance(c[0], c[1], c[2]); + let c = c.map(|v| l + (v - l) * sat); + c.map(|v| ((v - 0.5) * contrast + 0.5) * bright) +} + +fn key_light(ctx: &EffectCtx, mut b: Buf) -> Buf { + let pr = ctx.params; + let view = pr.e("view"); + if view == 0 { + return b; + } + let sc = pr.color("screenColour"); + let screen = [sc[0], sc[1], sc[2]]; + let (pi, o1, o2) = primary(screen); + let gain = pr.f("screenGain") as f32 / 100.0; + let bal = pr.f("screenBalance") as f32 / 100.0; + let dbias = pr.color("despillBias"); + let abias = if pr.b("lockBiasesTogether") { dbias } else { pr.color("alphaBias") }; + let (dbias, abias) = ([dbias[0], dbias[1], dbias[2]], [abias[0], abias[1], abias[2]]); + let s_alpha = neutralise(screen, abias); + let sd = screen_diff(s_alpha, pi, o1, o2, bal).max(1e-3); + let cb = pr.f("clipBlack") as f32 / 100.0; + let cw = pr.f("clipWhite") as f32 / 100.0; + let rollback = pr.f("clipRollback") * b.scale; + let preblur = pr.f("screenPreblur") * b.scale; + let sg = pr.f("screenShrinkGrow") * b.scale; + let soft = pr.f("screenSoftness") * b.scale; + let despot_b = pr.f("screenDespotBlack") * b.scale; + let despot_w = pr.f("screenDespotWhite") * b.scale; + + let src = b.img.clone(); + let blurred; + let key_src: &Image = if preblur > 0.0 { + blurred = gaussian_blur(&src, preblur * 0.5, preblur * 0.5, true); + &blurred + } else { + &src + }; + // Raw screen matte (opacity of the foreground). + let raw = Plane::from_image(key_src, |px| { + let (c, _) = unpremul(px); + let d = screen_diff(neutralise(c, abias), pi, o1, o2, bal); + (1.0 - gain * d / sd).clamp(0.0, 1.0) + }); + let clip = |v: f32| ((v - cb) / (cw - cb).max(1e-4)).clamp(0.0, 1.0); + let mut m = raw.map(clip); + if rollback > 0.0 { + // Restore edge detail lost by clipping within `rollback` pixels of the clipped matte. + let grown = morph_frac(&m, rollback, true); + let shrunk = morph_frac(&m, rollback, false); + m = Plane { + w: m.w, + h: m.h, + data: (0..m.data.len()) + .into_par_iter() + .map(|i| if grown.data[i] > shrunk.data[i] + 1e-4 { m.data[i].max(raw.data[i]).min(grown.data[i]) } else { m.data[i] }) + .collect(), + }; + } + if despot_b > 0.0 { + // Fill small holes: close (dilate then erode). + m = morph_frac(&morph_frac(&m, despot_b, true), despot_b, false); + } + if despot_w > 0.0 { + // Remove small specks: open (erode then dilate). + m = morph_frac(&morph_frac(&m, despot_w, false), despot_w, true); + } + if sg < 0.0 { + m = morph_frac(&m, -sg, false); + } else if sg > 0.0 { + m = morph_frac(&m, sg, true); + } + if soft > 0.0 { + m = gauss_plane(&m, soft * 0.5, soft * 0.5); + } + let screen_matte = m.clone(); + let inside = mask_coverage(ctx, &b, pr.e("insideMask"), pr.f("insideMaskSoftness"), pr.b("invertInsideMask")); + let outside = mask_coverage(ctx, &b, pr.e("outsideMask"), pr.f("outsideMaskSoftness"), pr.b("invertOutsideMask")); + let src_alpha_mode = pr.e("sourceAlpha"); + let n = src.data.len(); + let combined: Vec = (0..n) + .into_par_iter() + .map(|i| { + let mut a = screen_matte.data[i]; + let sa = src.data[i][3].clamp(0.0, 1.0); + let ins = inside.as_ref().map_or(0.0, |p| p.data[i]); + let ins = if src_alpha_mode == 1 { ins.max(sa) } else { ins }; + a = a.max(ins); + if let Some(o) = &outside { + a *= 1.0 - o.data[i]; + } + if src_alpha_mode == 2 { + a *= sa; + } + a.clamp(0.0, 1.0) + }) + .collect(); + let edges: Option> = pr.b("enableEdgeColourCorrection").then(|| { + let grow = pr.f("edgeGrow") * b.scale; + let hard = (pr.f("edgeHardness") / 100.0) as f32; + let esoft = pr.f("edgeSoftness") * b.scale; + // Edge band: where the (grown) combined matte is partial. + let cm = Plane { w: m.w, h: m.h, data: combined.clone() }; + let band = cm.map(|a| if a > 1e-3 && a < 0.999 { 1.0 } else { 0.0 }); + let mut band = if grow > 0.0 { morph_frac(&band, grow, true) } else { band }; + if esoft > 0.0 { + band = gauss_plane(&band, esoft * 0.5, esoft * 0.5); + } + band.data.iter().map(|v| (v * (1.0 + hard * 4.0)).min(1.0)).collect() + }); + let replace = pr.e("replaceMethod"); + let rc = pr.color("replaceColour"); + let in_replace = pr.e("insideReplaceMethod"); + let in_rc = pr.color("insideReplaceColour"); + let fg_cc = pr.b("enableColourCorrection"); + let (fs, fc, fbr) = (pr.f("saturation") as f32 / 100.0, pr.f("contrast") as f32 / 100.0 + 1.0, pr.f("brightness") as f32 / 100.0 + 1.0); + let (es, ec, ebr) = (pr.f("edgeSaturation") as f32 / 100.0, pr.f("edgeContrast") as f32 / 100.0 + 1.0, pr.f("edgeBrightness") as f32 / 100.0 + 1.0); + let unpremultiply = pr.b("unpremultiplyResult"); + let crops = [pr.f("cropLeft"), pr.f("cropRight"), pr.f("cropTop"), pr.f("cropBottom")].map(|v| v / 100.0); + let (lw, lh) = (ctx.layer_size[0], ctx.layer_size[1]); + let w = b.img.width as usize; + let (scale, off) = (b.scale, b.offset); + b.img.data.par_iter_mut().enumerate().for_each(|(i, px)| { + let (c, _) = unpremul(src.data[i]); + let a = combined[i]; + let sm = screen_matte.data[i]; + // Source crops (layer fractions from each edge). + let (x, y) = ((i % w) as f64 + 0.5, (i / w) as f64 + 0.5); + let (lx, ly) = ((x - off[0]) / scale / lw.max(1e-9), (y - off[1]) / scale / lh.max(1e-9)); + let cropped = lx < crops[0] || lx > 1.0 - crops[1] || ly < crops[2] || ly > 1.0 - crops[3]; + // Screen removal on the raw (unclipped) transparency, then despill. + let ar = raw.data[i].max(1e-4); + let mut fg = [0, 1, 2].map(|k| ((c[k] - (1.0 - ar) * screen[k]) / ar).max(0.0)); + let nfg = neutralise(fg, dbias); + let spill = screen_diff(nfg, pi, o1, o2, bal).max(0.0); + let l0 = (luminance(dbias[0], dbias[1], dbias[2]) / dbias[pi].max(1e-4)).max(1e-4); + fg[pi] -= spill / l0; + let intermediate = fg; + // Replace colour where the alpha was raised beyond the raw screen matte. + let raised = (sm - raw.data[i]).max(0.0); + let raised_in = inside.as_ref().map_or(0.0, |p| (p.data[i] - sm).max(0.0)); + let apply_replace = |fg: [f32; 3], method: u32, rc: [f32; 4], amt: f32| -> [f32; 3] { + if amt <= 0.0 { + return fg; + } + match method { + 1 => [0, 1, 2].map(|k| fg[k] + (c[k] - fg[k]) * amt), + 2 => [rc[0], rc[1], rc[2]], + 3 => { + let l = luminance(c[0], c[1], c[2]) / luminance(rc[0], rc[1], rc[2]).max(1e-4); + [0, 1, 2].map(|k| fg[k] + (rc[k] * l - fg[k]) * amt) + } + _ => fg, + } + }; + fg = apply_replace(fg, replace, rc, raised); + fg = apply_replace(fg, in_replace, in_rc, raised_in); + if fg_cc { + fg = correct(fg, fs, fc, fbr); + } + let edge = edges.as_ref().map_or(0.0, |e| e[i]); + if edge > 0.0 { + let ce = correct(fg, es, ec, ebr); + fg = [0, 1, 2].map(|k| fg[k] + (ce[k] - fg[k]) * edge); + } + let grey = |v: f32| [v, v, v, 1.0]; + *px = match view { + 1 => grey(src.data[i][3]), + 2 => { + let mut cs = neutralise(c, dbias); + let s2 = screen_diff(cs, pi, o1, o2, bal).max(0.0); + cs[pi] -= s2; + if fg_cc { + cs = correct(cs, fs, fc, fbr); + } + [cs[0], cs[1], cs[2], 1.0] + } + 3 => grey(edge), + 4 => grey(sm), + 5 => grey(inside.as_ref().map_or(0.0, |p| p.data[i])), + 6 => grey(outside.as_ref().map_or(0.0, |p| p.data[i])), + 7 => grey(a), + 8 => { + let v = if a <= 1e-3 { + 0.0 + } else if a >= 0.999 { + 1.0 + } else { + 0.5 + }; + grey(v) + } + 9 => premul(intermediate, sm), + _ => { + if cropped { + [0.0; 4] + } else if unpremultiply { + premul(fg, a) + } else { + // Keylight's premultiplied output read as straight colour. + premul(fg.map(|v| v * a), a) + } + } + }; + }); + b +} + +pub fn specs() -> Vec { + let pp = |id: &'static str, name: &'static str, v: (Value, ParamUi)| p(id, name, v.0, v.1); + let cc = |id: &'static str, name: &'static str| p(id, name, num(0.0), slider(-100.0, 100.0, -100.0, 100.0, 1)); + vec![EffectSpec { + id: "ec.keying.keylight", + name: "Key Light", + category: "Keying", + params: vec![ + p("view", "View", Value::Enum(10), popup(&VIEWS)), + p("unpremultiplyResult", "Unpremultiply Result", Value::Bool(true), ParamUi::Checkbox), + p("screenColour", "Screen Colour", col(0.0, 1.0, 0.0), ParamUi::Color), + p("screenGain", "Screen Gain", num(100.0), slider(0.0, 200.0, 0.0, 200.0, 1)), + p("screenBalance", "Screen Balance", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("despillBias", "Despill Bias", col(0.5, 0.5, 0.5), ParamUi::Color), + p("alphaBias", "Alpha Bias", col(0.5, 0.5, 0.5), ParamUi::Color), + p("lockBiasesTogether", "Lock Biases Together", Value::Bool(true), ParamUi::Checkbox), + p("screenPreblur", "Screen Pre-blur", num(0.0), slider(0.0, 100.0, 0.0, 10.0, 1)), + // Screen Matte + pp("clipBlack", "Clip Black", pct(0.0)), + pp("clipWhite", "Clip White", pct(100.0)), + p("clipRollback", "Clip Rollback", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("screenShrinkGrow", "Screen Shrink/Grow", num(0.0), slider(-100.0, 100.0, -10.0, 10.0, 1)), + p("screenSoftness", "Screen Softness", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("screenDespotBlack", "Screen Despot Black", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("screenDespotWhite", "Screen Despot White", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("replaceMethod", "Replace Method", Value::Enum(3), popup(&REPLACE)), + p("replaceColour", "Replace Colour", col(0.5, 0.5, 0.5), ParamUi::Color), + // Inside Mask + p("insideMask", "Inside Mask", Value::Enum(0), mask_popup()), + p("insideMaskSoftness", "Inside Mask Softness", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("invertInsideMask", "Invert", Value::Bool(false), ParamUi::Checkbox), + p("insideReplaceMethod", "Inside Replace Method", Value::Enum(1), popup(&REPLACE)), + p("insideReplaceColour", "Inside Replace Colour", col(0.5, 0.5, 0.5), ParamUi::Color), + p("sourceAlpha", "Source Alpha", Value::Enum(2), popup(&["Ignore", "Add to Inside Mask", "Normal"])), + // Outside Mask + p("outsideMask", "Outside Mask", Value::Enum(0), mask_popup()), + p("outsideMaskSoftness", "Outside Mask Softness", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("invertOutsideMask", "Invert Outside Mask", Value::Bool(false), ParamUi::Checkbox), + // Foreground Colour Correction + p("enableColourCorrection", "Enable Colour Correction", Value::Bool(false), ParamUi::Checkbox), + p("saturation", "Saturation", num(100.0), slider(0.0, 400.0, 0.0, 200.0, 1)), + cc("contrast", "Contrast"), + cc("brightness", "Brightness"), + // Edge Colour Correction + p("enableEdgeColourCorrection", "Enable Edge Colour Correction", Value::Bool(false), ParamUi::Checkbox), + pp("edgeHardness", "Edge Hardness", pct(0.0)), + p("edgeSoftness", "Edge Softness", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("edgeGrow", "Edge Grow", num(1.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("edgeSaturation", "Edge Saturation", num(100.0), slider(0.0, 400.0, 0.0, 200.0, 1)), + cc("edgeContrast", "Edge Contrast"), + cc("edgeBrightness", "Edge Brightness"), + // Source Crops + pp("cropLeft", "Left", pct(0.0)), + pp("cropRight", "Right", pct(0.0)), + pp("cropTop", "Top", pct(0.0)), + pp("cropBottom", "Bottom", pct(0.0)), + ], + render: key_light, + gpu: false, + float: true, + }] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, MaskShape, run_fx}; + + /// Green screen with a red square (fg) and a half-transparent mix strip. + fn plate() -> Image { + crate::util::gen_image(40, 30, |x, y| { + if (10..20).contains(&x) && (10..20).contains(&y) { + [0.8, 0.2, 0.1, 1.0] + } else if (25..30).contains(&x) { + // 50 % red over green. + [0.4, 0.6, 0.05, 1.0] + } else { + [0.1, 0.9, 0.1, 1.0] + } + }) + } + + fn green() -> Value { + col(0.1, 0.9, 0.1) + } + + #[test] + fn keys_the_screen_and_keeps_the_foreground() { + let out = run_fx("ec.keying.keylight", &[("screenColour", green())], plate(), 0.0, EffectEnv::default()); + let bg = out.img.get(2, 2); + let fg = out.img.get(15, 15); + let mix = out.img.get(27, 5); + assert!(bg[3] < 0.02, "{bg:?}"); + assert!(fg[3] > 0.98 && (fg[0] - 0.8).abs() < 0.05, "{fg:?}"); + assert!(mix[3] > 0.3 && mix[3] < 0.7, "{mix:?}"); + // The recovered edge colour has the green removed (no spill). + let (c, _) = unpremul(mix); + assert!(c[1] <= c[0] + 0.05, "{c:?}"); + // Deterministic. + let again = run_fx("ec.keying.keylight", &[("screenColour", green())], plate(), 0.0, EffectEnv::default()); + assert_eq!(out.img.data, again.img.data); + } + + #[test] + fn clip_gain_views_and_masks() { + // Clip White 60 %: the half-transparent strip becomes solid. + let out = run_fx("ec.keying.keylight", &[("screenColour", green()), ("clipWhite", num(40.0))], plate(), 0.0, EffectEnv::default()); + assert!(out.img.get(27, 5)[3] > 0.99); + // Screen Matte and Status views are grey-scale and opaque. + let sm = run_fx("ec.keying.keylight", &[("screenColour", green()), ("view", Value::Enum(4))], plate(), 0.0, EffectEnv::default()); + assert!(sm.img.get(2, 2)[0] < 0.02 && sm.img.get(15, 15)[0] > 0.98 && sm.img.get(2, 2)[3] == 1.0); + let st = run_fx("ec.keying.keylight", &[("screenColour", green()), ("view", Value::Enum(8))], plate(), 0.0, EffectEnv::default()); + assert_eq!(st.img.get(27, 5)[0], 0.5); + // Source view is the identity. + let src = run_fx("ec.keying.keylight", &[("view", Value::Enum(0))], plate(), 0.0, EffectEnv::default()); + assert_eq!(src.img.data, plate().data); + // Inside mask forces opacity; outside mask forces transparency. + let sq = |x0: f64, y0: f64, x1: f64, y1: f64| MaskShape { + name: "m".into(), + points: vec![[x0, y0], [x1, y0], [x1, y1], [x0, y1]], + closed: true, + inverted: false, + }; + let masks = [sq(0.0, 0.0, 6.0, 6.0), sq(12.0, 12.0, 18.0, 18.0)]; + let env = EffectEnv { masks: &masks, ..Default::default() }; + let m = run_fx("ec.keying.keylight", &[("screenColour", green()), ("insideMask", Value::Enum(1)), ("outsideMask", Value::Enum(2))], plate(), 0.0, env); + assert!(m.img.get(2, 2)[3] > 0.99, "{:?}", m.img.get(2, 2)); + assert!(m.img.get(15, 15)[3] < 0.01); + // Screen gain above 100 % keys more of the strip. + let g = run_fx("ec.keying.keylight", &[("screenColour", green()), ("screenGain", num(150.0))], plate(), 0.0, EffectEnv::default()); + assert!(g.img.get(27, 5)[3] < out.img.get(27, 5)[3]); + assert_eq!(crate::lookup("Keylight (1.2)").map(|s| s.id), Some("ec.keying.keylight")); + } +} diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 746500e6..4a4c46ee 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -24,8 +24,10 @@ mod generate2; mod generate3; mod keying; mod keying2; +pub mod keylight; mod matte; mod misc; +pub mod mocha_shape; mod noise; mod noise2; mod obsolete; @@ -360,6 +362,8 @@ pub fn registry() -> &'static [EffectSpec] { v.extend(vr::specs()); v.extend(blur3::specs()); v.extend(distort4::specs()); + v.extend(keylight::specs()); + v.extend(mocha_shape::specs()); v.extend(time_fx::specs()); v.extend(audio_fx::specs()); v.extend(paint::specs()); @@ -380,7 +384,10 @@ pub fn find(id: &str) -> Option<&'static EffectSpec> { /// Find by id or (case-insensitive) display name. pub fn lookup(name_or_id: &str) -> Option<&'static EffectSpec> { - find(name_or_id).or_else(|| registry().iter().find(|s| s.name.eq_ignore_ascii_case(name_or_id))) + find(name_or_id).or_else(|| registry().iter().find(|s| s.name.eq_ignore_ascii_case(name_or_id))).or_else(|| { + // After Effects' third-party display names we register under a generic name. + keylight::KEYLIGHT_ALIASES.iter().any(|a| a.eq_ignore_ascii_case(name_or_id)).then(|| find("ec.keying.keylight")).flatten() + }) } /// A parameter's default value on a layer of `layer_size` (point defaults are fractions of the @@ -478,6 +485,7 @@ pub const TIME_DEPENDENT: &[&str] = &[ "ec.vr.digitalglitch", "ec.blur.camerashakedeblur", "ec.distort.rollingshutterrepair", + "ec.obsolete.mochashape", ]; /// See [`TIME_DEPENDENT`]. diff --git a/crates/effects/src/mocha_shape.rs b/crates/effects/src/mocha_shape.rs new file mode 100644 index 00000000..0fafce9e --- /dev/null +++ b/crates/effects/src/mocha_shape.rs @@ -0,0 +1,243 @@ +//! Obsolete ▸ mocha shape: renders planar-tracked roto shapes as a matte. +//! +//! Mocha's own clipboard / export formats are not publicly specified, so the effect reads shape +//! data in a small documented JSON format of ours (in the hidden `shapeData` parameter, either the +//! JSON itself or a path to a `.json` file), which converters or agents can produce: +//! +//! ```json +//! {"shapes": [{"name": "Shape 1", "closed": true, "inverted": false, "feather": 2.0, +//! "points": [[x, y], …], +//! "frames": [{"time": 0.0, "points": [[x, y], …]}, {"time": 1.0, "points": […]}]}]} +//! ``` +//! +//! Coordinates are layer pixels. `frames` (layer time in seconds, linearly interpolated between +//! keys with the same vertex count, held outside) override the static `points`. Shapes combine +//! with Blend Mode (add / subtract / intersect / difference) into a coverage matte that is used +//! per Render Type: Shape Cutout (layer × matte), Color Composite (shape colour over the layer) +//! or Color Shape Cutout (shape colour inside the matte only). + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, OnceLock}; + +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use rayon::prelude::*; + +use crate::util::{Plane, gauss_plane, point_in_poly}; +use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider}; + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct MochaShape { + pub name: String, + pub closed: bool, + pub inverted: bool, + pub feather: f64, + pub points: Vec<[f64; 2]>, + /// (layer time, points), sorted by time. + pub frames: Vec<(f64, Vec<[f64; 2]>)>, +} + +impl MochaShape { + /// Outline at layer time `t`. + pub fn at(&self, t: f64) -> Vec<[f64; 2]> { + if self.frames.is_empty() { + return self.points.clone(); + } + let i = self.frames.partition_point(|f| f.0 <= t); + if i == 0 { + return self.frames[0].1.clone(); + } + if i == self.frames.len() { + return self.frames[i - 1].1.clone(); + } + let (a, b) = (&self.frames[i - 1], &self.frames[i]); + if a.1.len() != b.1.len() { + return a.1.clone(); + } + let u = ((t - a.0) / (b.0 - a.0).max(1e-12)).clamp(0.0, 1.0); + a.1.iter().zip(&b.1).map(|(p, q)| [p[0] + (q[0] - p[0]) * u, p[1] + (q[1] - p[1]) * u]).collect() + } +} + +fn pts(v: &serde_json::Value) -> Vec<[f64; 2]> { + v.as_array() + .map(|a| { + a.iter() + .filter_map(|p| { + let p = p.as_array()?; + Some([p.first()?.as_f64()?, p.get(1)?.as_f64()?]) + }) + .collect() + }) + .unwrap_or_default() +} + +/// Parse shape data JSON (see the module docs). `None` when it is not valid. +pub fn parse_shapes(text: &str) -> Option> { + let v: serde_json::Value = serde_json::from_str(text).ok()?; + let list = v.get("shapes").and_then(|s| s.as_array()).or_else(|| v.as_array())?; + let mut out = vec![]; + for s in list { + let mut frames: Vec<(f64, Vec<[f64; 2]>)> = s + .get("frames") + .and_then(|f| f.as_array()) + .map(|a| a.iter().filter_map(|f| Some((f.get("time")?.as_f64()?, pts(f.get("points")?)))).collect()) + .unwrap_or_default(); + frames.sort_by(|a, b| a.0.total_cmp(&b.0)); + out.push(MochaShape { + name: s.get("name").and_then(|n| n.as_str()).unwrap_or("").to_string(), + closed: s.get("closed").and_then(|c| c.as_bool()).unwrap_or(true), + inverted: s.get("inverted").and_then(|c| c.as_bool()).unwrap_or(false), + feather: s.get("feather").and_then(|c| c.as_f64()).unwrap_or(0.0).max(0.0), + points: s.get("points").map(pts).unwrap_or_default(), + frames, + }); + } + Some(out) +} + +type ShapeCache = Mutex>>>>; + +fn load(src: &str) -> Option>> { + let s = src.trim(); + if s.is_empty() { + return None; + } + static CACHE: OnceLock = OnceLock::new(); + let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new())); + if let Some(v) = cache.lock().ok().and_then(|c| c.get(src).cloned()) { + return v; + } + let parsed = + if s.starts_with('{') || s.starts_with('[') { parse_shapes(s) } else { std::fs::read_to_string(s).ok().and_then(|t| parse_shapes(&t)) }.map(Arc::new); + if let Ok(mut c) = cache.lock() { + if c.len() > 64 { + c.clear(); + } + c.insert(src.to_string(), parsed.clone()); + } + parsed +} + +fn mocha_shape(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(shapes) = load(ctx.params.s("shapeData")) else { return b }; + let (w, h) = (b.img.width as usize, b.img.height as usize); + let blend = ctx.params.e("blendMode"); + let inv = 1.0 / b.scale.max(1e-9); + let mut matte: Option = None; + for s in shapes.iter() { + let outline = s.at(ctx.time); + if outline.len() < 3 || !s.closed { + continue; + } + let mut cov = Plane::new(w, h); + cov.data.par_chunks_mut(w.max(1)).enumerate().for_each(|(y, row)| { + for (x, v) in row.iter_mut().enumerate() { + let lx = (x as f64 + 0.5 - b.offset[0]) * inv; + let ly = (y as f64 + 0.5 - b.offset[1]) * inv; + *v = if point_in_poly(&outline, lx, ly) != s.inverted { 1.0 } else { 0.0 }; + } + }); + if s.feather > 0.0 { + let f = s.feather * b.scale * 0.5; + cov = gauss_plane(&cov, f, f); + } + matte = Some(match matte { + None => cov, + Some(m) => m.zip_map(&cov, |a, c| match blend { + 1 => a * (1.0 - c), + 2 => a * c, + 3 => (a - c).abs(), + _ => a + c - a * c, + }), + }); + } + let Some(mut m) = matte else { return b }; + if ctx.params.b("invert") { + m = m.map(|v| 1.0 - v); + } + let kind = ctx.params.e("renderType"); + let c = ctx.params.color("shapeColour"); + let op = (ctx.params.f("opacity") / 100.0) as f32; + let edge = ctx.params.b("renderEdgeWidth"); + b.img.data.par_iter_mut().zip(m.data.par_iter()).for_each(|(px, &k)| { + let k = if edge { (1.0 - (2.0 * k - 1.0).abs()).clamp(0.0, 1.0) } else { k.clamp(0.0, 1.0) }; + match kind { + 1 => { + let a = k * op; + let s = [c[0] * a, c[1] * a, c[2] * a, a]; + *px = [s[0] + px[0] * (1.0 - a), s[1] + px[1] * (1.0 - a), s[2] + px[2] * (1.0 - a), s[3] + px[3] * (1.0 - a)]; + } + 2 => { + let a = k * op; + *px = [c[0] * a, c[1] * a, c[2] * a, a]; + } + _ => { + let a = 1.0 - (1.0 - k) * op; + *px = px.map(|v| v * a); + } + } + }); + b +} + +pub fn specs() -> Vec { + vec![EffectSpec { + id: "ec.obsolete.mochashape", + name: "mocha shape", + category: "Obsolete", + params: vec![ + p("blendMode", "Blend mode", Value::Enum(0), popup(&["Add", "Subtract", "Intersect", "Difference"])), + p("invert", "Invert", Value::Bool(false), ParamUi::Checkbox), + p("renderEdgeWidth", "Render edge width", Value::Bool(false), ParamUi::Checkbox), + p("renderType", "Render type", Value::Enum(0), popup(&["Shape cutout", "Color composite", "Color shape cutout"])), + p("shapeColour", "Shape colour", col(1.0, 1.0, 1.0), ParamUi::Color), + p("opacity", "Opacity", num(100.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + // JSON shape data (see the module docs) or a path to a .json file. + p("shapeData", "Shape data", Value::Str(String::new()), ParamUi::Hidden), + ], + render: mocha_shape, + gpu: false, + float: true, + }] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, Image, run_fx}; + + const DATA: &str = r#"{"shapes":[{"name":"S","closed":true,"points":[[2,2],[10,2],[10,10],[2,10]], + "frames":[{"time":0,"points":[[2,2],[10,2],[10,10],[2,10]]},{"time":1,"points":[[12,2],[20,2],[20,10],[12,10]]}]}]}"#; + + #[test] + fn parses_and_interpolates() { + let s = parse_shapes(DATA).unwrap(); + assert_eq!(s.len(), 1); + assert_eq!(s[0].at(0.5)[0], [7.0, 2.0]); + assert_eq!(s[0].at(5.0)[0], [12.0, 2.0]); + assert!(parse_shapes("not json").is_none()); + } + + #[test] + fn cuts_out_the_shape_over_time() { + let img = Image::filled(24, 12, [1.0, 0.0, 0.0, 1.0]); + let a = run_fx("ec.obsolete.mochashape", &[("shapeData", Value::Str(DATA.into()))], img.clone(), 0.0, EffectEnv::default()); + assert_eq!(a.img.get(5, 5)[3], 1.0); + assert_eq!(a.img.get(15, 5)[3], 0.0); + let b = run_fx("ec.obsolete.mochashape", &[("shapeData", Value::Str(DATA.into()))], img.clone(), 1.0, EffectEnv::default()); + assert_eq!(b.img.get(5, 5)[3], 0.0); + assert_eq!(b.img.get(15, 5)[3], 1.0); + // Color shape cutout fills with the shape colour; no data = pass-through. + let c = run_fx( + "ec.obsolete.mochashape", + &[("shapeData", Value::Str(DATA.into())), ("renderType", Value::Enum(2)), ("shapeColour", col(0.0, 1.0, 0.0))], + img.clone(), + 0.0, + EffectEnv::default(), + ); + assert_eq!(c.img.get(5, 5), [0.0, 1.0, 0.0, 1.0]); + let none = run_fx("ec.obsolete.mochashape", &[], img.clone(), 0.0, EffectEnv::default()); + assert_eq!(none.img.data, img.data); + } +} From 7b815bcbb206e1a1f77b851cf76956eb0d97a624 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:18:33 -0400 Subject: [PATCH 07/23] M9.11: Simulation effects (Particle Playground, CC Hair) Particle Playground: cannon, grid, layer exploder, layer map, gravity, repel, wall and persistent property mapper; deterministic, stepped from t=0 through the checkpointed SimCache. Co-Authored-By: Claude Opus 5.5 --- crates/effects/src/lib.rs | 2 + crates/effects/src/sim3.rs | 716 +++++++++++++++++++++++++++++++++++++ 2 files changed, 718 insertions(+) create mode 100644 crates/effects/src/sim3.rs diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 4a4c46ee..2f6a463e 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -38,6 +38,7 @@ mod perspective2; pub mod puppet; mod sim; mod sim2; +mod sim3; mod stylize2; mod stylize3; mod textfx; @@ -364,6 +365,7 @@ pub fn registry() -> &'static [EffectSpec] { v.extend(distort4::specs()); v.extend(keylight::specs()); v.extend(mocha_shape::specs()); + v.extend(sim3::specs()); v.extend(time_fx::specs()); v.extend(audio_fx::specs()); v.extend(paint::specs()); diff --git a/crates/effects/src/sim3.rs b/crates/effects/src/sim3.rs new file mode 100644 index 00000000..66e9e11c --- /dev/null +++ b/crates/effects/src/sim3.rs @@ -0,0 +1,716 @@ +//! Simulation: Particle Playground and CC Hair. +//! +//! **Particle Playground** is a deterministic 2D particle system stepped from layer time 0 at +//! [`SPS`] steps per second through [`SimCache`] (seeking anywhere gives the same frame as +//! playing up to it). Producers: **Cannon** (a stream from a barrel with angle, radius, rate, +//! velocity and random spreads), **Grid** (particles placed on a grid at time 0) and **Layer +//! Exploder** (another layer broken into particles at time 0). Forces: **Gravity** (force, +//! random spread, direction), **Repel** (particles push each other apart within a radius), +//! **Wall** (a mask of the layer the particles bounce off) and the **Persistent Property +//! Mapper** (a map layer's red / green / blue drive particle properties at the particle's +//! position each step). **Layer Map** draws particles with the colours of another layer +//! sampled at each particle's birth position. Map layers are read at the frame being rendered +//! (their pixels are part of the cache key). Particles replace the layer's own pixels, as in +//! After Effects. +//! +//! **CC Hair** grows strands from the layer's opaque pixels (Density per 1 000 px²), each a +//! quadratic curve of Length that droops with Weight, optionally steered and scaled by a hairfall +//! map layer (luminance → length, horizontal/vertical gradient → lean), and shades them with a +//! Kajiya–Kay-style strand model (diffuse ∝ sin(tangent, light), specular from the half vector). + +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use effectcraft_raster::{Image, Px}; +use rayon::prelude::*; + +use crate::sim::{Acc, SPS, Shape, Sprite, combine, splat, steps_at}; +use crate::util::{SimCache, hash1, params_key, point_in_poly, unpremul}; +use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider}; + +fn spec(id: &'static str, name: &'static str, params: Vec, render: crate::RenderFn) -> EffectSpec { + EffectSpec { id, name, category: "Simulation", params, render, gpu: false, float: true } +} + +// ---------------------------------------------------------------- Particle Playground + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct PgParticle { + pub p: [f32; 2], + pub v: [f32; 2], + /// Layer position where the particle was born (Layer Map lookups). + pub origin: [f32; 2], + pub c: [f32; 4], + pub r: f32, + pub mass: f32, + pub friction: f32, + pub force: [f32; 2], + pub id: u32, +} + +#[derive(Clone, Debug, Default)] +pub struct PgState { + pub parts: Vec, + carry: f64, + next_id: u32, +} + +/// A map layer sampled in layer coordinates. +struct MapImg { + img: Image, + /// Layer point → pixel: (x · k₀ + o₀, y · k₁ + o₁). + k: [f64; 2], + o: [f64; 2], +} + +impl MapImg { + fn from(ctx: &EffectCtx, lp: crate::LayerPixels) -> MapImg { + // Fit the map layer to this layer (stretch), like AE's layer maps. + let (ls, os) = (ctx.layer_size, lp.size); + let sx = if ls[0] > 0.0 { os[0] / ls[0] } else { 1.0 }; + let sy = if ls[1] > 0.0 { os[1] / ls[1] } else { 1.0 }; + MapImg { img: lp.buf.img, k: [lp.buf.scale * sx, lp.buf.scale * sy], o: lp.buf.offset } + } + fn at(&self, x: f32, y: f32) -> Px { + let (c, a) = unpremul(self.img.sample_bilinear_clamped(x as f64 * self.k[0] + self.o[0], y as f64 * self.k[1] + self.o[1])); + [c[0], c[1], c[2], a] + } +} + +pub const MAP_TARGETS: [&str; 12] = + ["None", "Red", "Green", "Blue", "Scale", "Opacity", "X Velocity", "Y Velocity", "X Force", "Y Force", "Kinetic Friction", "Mass"]; + +struct Pg { + // Cannon + cannon: bool, + pos: [f32; 2], + barrel_angle: f32, + barrel_radius: f32, + rate: f64, + dir_spread: f32, + vel: f32, + vel_spread: f32, + color: [f32; 4], + radius: f32, + // Gravity + gravity: [f32; 2], + grav_spread: f32, + // Repel + repel: f32, + repel_radius: f32, + // Wall + wall: Option>, + // Property mapper + mapper: Option, + map_to: [(u32, f32, f32); 3], + seed: u32, + bounds: [f32; 4], +} + +impl Pg { + fn spawn(&self, id: u32) -> PgParticle { + let s = self.seed; + let h = |k: u32| hash1(id, k, s) * 2.0 - 1.0; + // AE angles: 0° = up, clockwise. + let a = (self.barrel_angle + h(1) * self.dir_spread * 0.5).to_radians(); + let d = [a.sin(), -a.cos()]; + let perp = [-d[1], d[0]]; + let off = h(2) * self.barrel_radius; + let speed = (self.vel + h(3) * self.vel_spread).max(0.0); + let p = [self.pos[0] + perp[0] * off, self.pos[1] + perp[1] * off]; + PgParticle { p, v: [d[0] * speed, d[1] * speed], origin: p, c: self.color, r: self.radius, mass: 1.0, friction: 0.0, force: [0.0; 2], id } + } + + fn step(&self, st: &mut PgState) { + let dt = (1.0 / SPS) as f32; + let s = self.seed; + // Property mapper (persistent): drive properties from the map at the particle position. + if let Some(m) = &self.mapper { + st.parts.par_iter_mut().for_each(|q| { + let px = m.at(q.p[0], q.p[1]); + for (ch, &(target, lo, hi)) in self.map_to.iter().enumerate() { + let v = lo + (hi - lo) * px[ch]; + match target { + 1 => q.c[0] = v, + 2 => q.c[1] = v, + 3 => q.c[2] = v, + 4 => q.r = self.radius * v.max(0.0), + 5 => q.c[3] = v.clamp(0.0, 1.0), + 6 => q.v[0] = v, + 7 => q.v[1] = v, + 8 => q.force[0] = v, + 9 => q.force[1] = v, + 10 => q.friction = v.clamp(0.0, 1.0), + 11 => q.mass = v.max(0.01), + _ => {} + } + } + }); + } + // Repel: spatial hash, symmetric pairwise push within the radius. + let mut push = vec![[0.0f32; 2]; st.parts.len()]; + if self.repel != 0.0 && self.repel_radius > 0.0 && st.parts.len() > 1 { + let cell = self.repel_radius; + let mut grid: std::collections::HashMap<(i32, i32), Vec> = std::collections::HashMap::new(); + for (i, q) in st.parts.iter().enumerate() { + grid.entry(((q.p[0] / cell).floor() as i32, (q.p[1] / cell).floor() as i32)).or_default().push(i); + } + let parts = &st.parts; + push.par_iter_mut().enumerate().for_each(|(i, f)| { + let q = parts[i]; + let (cx, cy) = ((q.p[0] / cell).floor() as i32, (q.p[1] / cell).floor() as i32); + for gy in cy - 1..=cy + 1 { + for gx in cx - 1..=cx + 1 { + let Some(list) = grid.get(&(gx, gy)) else { continue }; + for &j in list { + if j == i { + continue; + } + let o = parts[j]; + let (dx, dy) = (q.p[0] - o.p[0], q.p[1] - o.p[1]); + let d = (dx * dx + dy * dy).sqrt(); + if d < self.repel_radius && d > 1e-4 { + let k = self.repel * (1.0 - d / self.repel_radius) / d; + f[0] += dx * k; + f[1] += dy * k; + } + } + } + } + }); + } + let wall = self.wall.as_deref(); + st.parts.par_iter_mut().zip(push.par_iter()).for_each(|(q, f)| { + let g = if self.grav_spread > 0.0 { 1.0 + (hash1(q.id, 7, s) * 2.0 - 1.0) * self.grav_spread } else { 1.0 }; + let ax = self.gravity[0] * g + (q.force[0] + f[0]) / q.mass; + let ay = self.gravity[1] * g + (q.force[1] + f[1]) / q.mass; + let damp = 1.0 - q.friction * 0.1; + q.v = [(q.v[0] + ax * dt) * damp, (q.v[1] + ay * dt) * damp]; + let np = [q.p[0] + q.v[0] * dt, q.p[1] + q.v[1] * dt]; + if let Some(w) = wall { + let was = point_in_poly(w, q.p[0] as f64, q.p[1] as f64); + let now = point_in_poly(w, np[0] as f64, np[1] as f64); + if was != now { + // Bounce: reverse the velocity and stay on the same side. + q.v = [-q.v[0] * 0.9, -q.v[1] * 0.9]; + return; + } + } + q.p = np; + }); + let b = self.bounds; + st.parts.retain(|q| q.p[0] > b[0] && q.p[0] < b[2] && q.p[1] > b[1] && q.p[1] < b[3]); + if self.cannon { + st.carry += self.rate / SPS; + let n = st.carry.floor() as u32; + st.carry -= n as f64; + for _ in 0..n { + if st.parts.len() >= 60_000 { + break; + } + let id = st.next_id; + st.next_id = st.next_id.wrapping_add(1); + let mut q = self.spawn(id); + // Sub-step birth offset keeps streams smooth. + let frac = hash1(id, 9, s) * dt; + q.p = [q.p[0] + q.v[0] * frac, q.p[1] + q.v[1] * frac]; + st.parts.push(q); + } + } + } +} + +static PG_CACHE: SimCache = SimCache::new(6); + +fn image_key(img: &Image) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + img.width.hash(&mut h); + img.height.hash(&mut h); + for p in img.data.iter().step_by(7) { + for c in p { + c.to_bits().hash(&mut h); + } + } + h.finish() +} + +fn particle_playground(ctx: &EffectCtx, mut b: Buf) -> Buf { + let pr = ctx.params; + let (lw, lh) = (ctx.layer_size[0] as f32, ctx.layer_size[1] as f32); + let grav_dir = (pr.f("gravityDirection") as f32).to_radians(); + let gf = pr.f("gravityForce") as f32; + let wall = (pr.f("wallBoundary").round() as usize).checked_sub(1).and_then(|i| ctx.env.masks.get(i)).map(|m| m.points.clone()); + let mapper = if pr.b("mapperEnabled") { ctx.layer_param("useLayerAsMap", true).map(|lp| MapImg::from(ctx, lp)) } else { None }; + let pg = Pg { + cannon: pr.b("cannonEnabled"), + pos: { pr.v2("cannonPosition").map(|v| v as f32) }, + barrel_angle: pr.f("barrelAngle") as f32, + barrel_radius: pr.f("barrelRadius") as f32, + rate: pr.f("particlesPerSecond").max(0.0), + dir_spread: pr.f("directionRandomSpread") as f32, + vel: pr.f("velocity") as f32, + vel_spread: pr.f("velocityRandomSpread") as f32, + color: pr.color("cannonColor"), + radius: pr.f("cannonParticleRadius").max(0.0) as f32, + gravity: [grav_dir.sin() * gf, -grav_dir.cos() * gf], + grav_spread: (pr.f("gravityForceRandomSpread") / 100.0).max(0.0) as f32, + repel: pr.f("repelForce") as f32 * 100.0, + repel_radius: pr.f("repelForceRadius").max(0.0) as f32, + wall, + mapper, + map_to: [ + (pr.e("mapRedTo"), pr.f("redMin") as f32, pr.f("redMax") as f32), + (pr.e("mapGreenTo"), pr.f("greenMin") as f32, pr.f("greenMax") as f32), + (pr.e("mapBlueTo"), pr.f("blueMin") as f32, pr.f("blueMax") as f32), + ], + seed: (pr.f("randomSeed") as u32).wrapping_mul(0x9e37_79b9) ^ ctx.seed, + bounds: [-lw * 2.0, -lh * 2.0, lw * 3.0, lh * 3.0], + }; + // Initial particles: Grid and Layer Exploder. + let grid_on = pr.b("gridEnabled"); + let across = pr.f("particlesAcross").max(0.0).round() as u32; + let down = pr.f("particlesDown").max(0.0).round() as u32; + let gpos = pr.v2("gridPosition"); + let (gw, gh) = (pr.f("gridWidth"), pr.f("gridHeight")); + let gcol = pr.color("gridColor"); + let grad = pr.f("gridParticleRadius").max(0.0) as f32; + let exploder = if pr.b("exploderEnabled") { + let host = ctx.env.host; + let lid = pr.get("explodeLayer").and_then(Value::as_layer); + match (host, lid) { + (Some(h), Some(id)) => h.layer_at(id, ctx.env.comp_time - ctx.time, true).or_else(|| h.layer(id, true)), + _ => None, + } + } else { + None + }; + let ex_r = pr.f("radiusOfNewParticles").max(0.5) as f32; + let ex_disp = pr.f("velocityDispersion") as f32; + let layer_map = pr.b("layerMapEnabled").then(|| ctx.layer_param("layerMapLayer", true).map(|lp| MapImg::from(ctx, lp))).flatten(); + + let mut key = params_key(ctx, &Buf { img: Image::new(0, 0), offset: [0.0; 2], scale: 1.0 }, 0x7067); + if let Some(m) = &pg.mapper { + key ^= image_key(&m.img).rotate_left(7); + } + if let Some(e) = &exploder { + key ^= image_key(&e.buf.img).rotate_left(13); + } + if let Some(w) = &pg.wall { + key ^= w.iter().fold(0u64, |a, p| a.rotate_left(5) ^ p[0].to_bits() ^ p[1].to_bits().rotate_left(17)); + } + let seed = pg.seed; + let init = || { + let mut st = PgState::default(); + if grid_on && across > 0 && down > 0 { + for j in 0..down { + for i in 0..across { + let fx = if across > 1 { i as f64 / (across - 1) as f64 - 0.5 } else { 0.0 }; + let fy = if down > 1 { j as f64 / (down - 1) as f64 - 0.5 } else { 0.0 }; + let p = [(gpos[0] + fx * gw) as f32, (gpos[1] + fy * gh) as f32]; + let id = st.next_id; + st.next_id += 1; + st.parts.push(PgParticle { p, v: [0.0; 2], origin: p, c: gcol, r: grad, mass: 1.0, friction: 0.0, force: [0.0; 2], id }); + } + } + } + if let Some(e) = &exploder { + let m = MapImg::from(ctx, e.clone()); + let step = (ex_r * 2.0).max(1.0); + let mut y = step * 0.5; + while y < lh { + let mut x = step * 0.5; + while x < lw { + let c = m.at(x, y); + if c[3] > 0.05 { + let id = st.next_id; + st.next_id += 1; + let h = |k: u32| hash1(id, k, seed) * 2.0 - 1.0; + let (dx, dy) = (x - lw * 0.5, y - lh * 0.5); + let l = (dx * dx + dy * dy).sqrt().max(1.0); + let v = [dx / l * ex_disp + h(1) * ex_disp, dy / l * ex_disp + h(2) * ex_disp]; + st.parts.push(PgParticle { p: [x, y], v, origin: [x, y], c, r: ex_r, mass: 1.0, friction: 0.0, force: [0.0; 2], id }); + } + x += step; + } + y += step; + } + } + st + }; + let st = PG_CACHE.run(key, steps_at(ctx.time), init, |st, _| pg.step(st)); + let s = b.scale as f32; + let sprites: Vec = st + .parts + .iter() + .map(|q| { + let (x, y) = b.to_px([q.p[0] as f64, q.p[1] as f64]); + let c = match &layer_map { + Some(m) => { + let mc = m.at(q.origin[0], q.origin[1]); + [mc[0], mc[1], mc[2], mc[3] * q.c[3]] + } + None => q.c, + }; + Sprite::new(x as f32, y as f32, q.r * s, c, Shape::Disc) + }) + .collect(); + let fx = splat(b.img.width, b.img.height, &sprites, Acc::Over); + // Particles replace the layer's own pixels. + b.img = combine(&b.img, &fx, 5); + b +} + +// ---------------------------------------------------------------- CC Hair + +fn cc_hair(ctx: &EffectCtx, mut b: Buf) -> Buf { + let pr = ctx.params; + let len = pr.f("length").max(0.0) as f32; + let density = pr.f("density").max(0.0) as f32 / 100.0; + if len <= 0.0 || density <= 0.0 { + return b; + } + let thick = pr.f("thickness").max(0.05) as f32; + let weight = pr.f("weight") as f32; + let const_mass = pr.b("constantMass"); + let map = ctx.layer_param("mapLayer", true).map(|lp| MapImg::from(ctx, lp)); + let map_strength = (pr.f("mapStrength") / 100.0) as f32; + let map_soft = pr.f("mapSoftness").max(0.0) as f32; + let noise = (pr.f("addNoise") / 100.0) as f32; + let hc = pr.color("hairColor"); + let bright = (pr.f("brightness") / 100.0) as f32; + let opacity = (pr.f("opacity") / 100.0) as f32; + let inherit = (pr.f("colorInheritance") / 100.0) as f32; + let light_i = (pr.f("lightIntensity") / 100.0) as f32; + let ld = (pr.f("lightDirection") as f32).to_radians(); + let light = [ld.sin(), -ld.cos()]; + let (amb, dif, spe) = ((pr.f("ambient") / 100.0) as f32, (pr.f("diffuse") / 100.0) as f32, (pr.f("specular") / 100.0) as f32); + let rough = (pr.f("roughness") / 100.0).clamp(0.01, 1.0) as f32; + let seed = ctx.seed ^ (pr.f("randomSeed") as u32).wrapping_mul(0x85eb_ca6b); + let (lw, lh) = (ctx.layer_size[0] as f32, ctx.layer_size[1] as f32); + // Roots: a jittered grid over the layer, density per 1 000 px². + let count = ((lw * lh / 1000.0) * density * 10.0).min(200_000.0) as u32; + let src = b.img.clone(); + let segs = 6u32; + let strands: Vec> = (0..count) + .into_par_iter() + .filter_map(|i| { + let rx = hash1(i, 1, seed) * lw; + let ry = hash1(i, 2, seed) * lh; + let (bx, by) = b.to_px([rx as f64, ry as f64]); + let root = src.sample_bilinear(bx, by); + if root[3] < 0.5 { + return None; + } + let (rc, _) = unpremul(root); + let mut l = len * (0.75 + 0.5 * hash1(i, 3, seed)); + // Lean: random, plus the map's local gradient (hairfall map). + let mut lean = [(hash1(i, 4, seed) * 2.0 - 1.0) * 0.6, -1.0 + hash1(i, 5, seed) * 0.4]; + if let Some(m) = &map { + let lum = |x: f32, y: f32| { + let c = m.at(x, y); + (0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]) * c[3] + }; + let d = map_soft.max(1.0); + let (gx, gy) = (lum(rx + d, ry) - lum(rx - d, ry), lum(rx, ry + d) - lum(rx, ry - d)); + l *= 1.0 - map_strength + map_strength * lum(rx, ry); + lean = [lean[0] + gx * map_strength * 4.0, lean[1] + gy * map_strength * 4.0]; + if noise > 0.0 { + lean[0] += (hash1(i, 6, seed) * 2.0 - 1.0) * noise; + } + } + let ll = (lean[0] * lean[0] + lean[1] * lean[1]).sqrt().max(1e-4); + let dir = [lean[0] / ll, lean[1] / ll]; + // Droop: heavier (or thinner with Constant Mass off) hair bends more. + let droop = weight * if const_mass { 1.0 } else { 1.0 / thick.max(0.2) } * l; + let base = [rc[0] + (hc[0] - rc[0]) * (1.0 - inherit), rc[1] + (hc[1] - rc[1]) * (1.0 - inherit), rc[2] + (hc[2] - rc[2]) * (1.0 - inherit)]; + let pt = |s: f32| [rx + dir[0] * l * s, ry + dir[1] * l * s + droop * s * s]; + let mut out = Vec::with_capacity(segs as usize); + for k in 0..segs { + let (s0, s1) = (k as f32 / segs as f32, (k + 1) as f32 / segs as f32); + let (a, c) = (pt(s0), pt(s1)); + let t = { + let (tx, ty) = (c[0] - a[0], c[1] - a[1]); + let tl = (tx * tx + ty * ty).sqrt().max(1e-6); + [tx / tl, ty / tl] + }; + // Strand shading (Kajiya–Kay): diffuse ∝ sin(T, L), specular from T·L. + let tl = t[0] * light[0] + t[1] * light[1]; + let diff = (1.0 - tl * tl).max(0.0).sqrt(); + let spec = (1.0 - tl.abs()).powf(1.0 / rough) * spe; + let shade = (amb + dif * diff * light_i) * bright; + let tip = 1.0 - 0.6 * s1; + let c3 = [base[0] * shade + spec * light_i, base[1] * shade + spec * light_i, base[2] * shade + spec * light_i]; + let (pa, pc) = (b.to_px([a[0] as f64, a[1] as f64]), b.to_px([c[0] as f64, c[1] as f64])); + out.push(Sprite { + x: pa.0 as f32, + y: pa.1 as f32, + r: thick * 0.5 * b.scale as f32 * tip, + c: [c3[0], c3[1], c3[2], opacity], + shape: Shape::Line { dx: (pc.0 - pa.0) as f32, dy: (pc.1 - pa.1) as f32 }, + rot: 0.0, + }); + } + Some(out) + }) + .collect(); + let sprites: Vec = strands.into_iter().flatten().collect(); + let fx = splat(b.img.width, b.img.height, &sprites, Acc::Over); + b.img = combine(&b.img, &fx, 0); + b +} + +pub fn specs() -> Vec { + let pt = |x: f64, y: f64| Value::Vec2([x, y]); + let px = |d: f64, max: f64| (num(d), slider(0.0, max * 10.0, 0.0, max, 1)); + let sp = |id: &'static str, name: &'static str, v: (Value, ParamUi)| p(id, name, v.0, v.1); + let mut pg = vec![ + // Cannon + p("cannonEnabled", "Cannon", Value::Bool(true), ParamUi::Checkbox), + p("cannonPosition", "Position", pt(0.5, 0.9), ParamUi::Point), + p("barrelAngle", "Barrel Angle", num(0.0), ParamUi::Angle), + sp("barrelRadius", "Barrel Radius", px(0.0, 100.0)), + sp("particlesPerSecond", "Particles Per Second", px(60.0, 500.0)), + sp("directionRandomSpread", "Direction Random Spread", px(20.0, 360.0)), + sp("velocity", "Velocity", px(130.0, 1000.0)), + sp("velocityRandomSpread", "Velocity Random Spread", px(20.0, 500.0)), + p("cannonColor", "Color", col(1.0, 0.0, 0.0), ParamUi::Color), + sp("cannonParticleRadius", "Particle Radius", px(2.0, 50.0)), + // Grid + p("gridEnabled", "Grid", Value::Bool(true), ParamUi::Checkbox), + p("gridPosition", "Grid Position", pt(0.5, 0.5), ParamUi::Point), + sp("gridWidth", "Width", px(100.0, 2000.0)), + sp("gridHeight", "Height", px(100.0, 2000.0)), + sp("particlesAcross", "Particles Across", px(0.0, 100.0)), + sp("particlesDown", "Particles Down", px(0.0, 100.0)), + p("gridColor", "Grid Color", col(1.0, 1.0, 1.0), ParamUi::Color), + sp("gridParticleRadius", "Grid Particle Radius", px(2.0, 50.0)), + // Layer Exploder + p("exploderEnabled", "Layer Exploder", Value::Bool(true), ParamUi::Checkbox), + p("explodeLayer", "Explode Layer", Value::Layer(None), ParamUi::Layer), + sp("radiusOfNewParticles", "Radius of New Particles", px(2.0, 50.0)), + sp("velocityDispersion", "Velocity Dispersion", px(20.0, 500.0)), + // Layer Map + p("layerMapEnabled", "Layer Map", Value::Bool(true), ParamUi::Checkbox), + p("layerMapLayer", "Use Layer", Value::Layer(None), ParamUi::Layer), + // Gravity + sp("gravityForce", "Force", px(108.0, 1000.0)), + sp("gravityForceRandomSpread", "Force Random Spread", px(0.0, 100.0)), + p("gravityDirection", "Direction", num(180.0), ParamUi::Angle), + // Repel + p("repelForce", "Repel Force", num(0.0), slider(-100.0, 100.0, -10.0, 10.0, 2)), + sp("repelForceRadius", "Force Radius", px(0.0, 100.0)), + // Wall + p("wallBoundary", "Boundary", num(0.0), ParamUi::Mask), + // Persistent Property Mapper + p("mapperEnabled", "Persistent Property Mapper", Value::Bool(true), ParamUi::Checkbox), + p("useLayerAsMap", "Use Layer As Map", Value::Layer(None), ParamUi::Layer), + ]; + for (to, mn, mx, n_to, n_min, n_max) in [ + ("mapRedTo", "redMin", "redMax", "Map Red To", "Red Min", "Red Max"), + ("mapGreenTo", "greenMin", "greenMax", "Map Green To", "Green Min", "Green Max"), + ("mapBlueTo", "blueMin", "blueMax", "Map Blue To", "Blue Min", "Blue Max"), + ] { + pg.push(p(to, n_to, Value::Enum(0), popup(&MAP_TARGETS))); + pg.push(p(mn, n_min, num(0.0), slider(-10000.0, 10000.0, -100.0, 100.0, 2))); + pg.push(p(mx, n_max, num(1.0), slider(-10000.0, 10000.0, -100.0, 100.0, 2))); + } + pg.push(p("randomSeed", "Random Seed", num(0.0), slider(0.0, 10000.0, 0.0, 1000.0, 0))); + let pc = |d: f64| (num(d), slider(0.0, 100.0, 0.0, 100.0, 1)); + vec![ + spec("ec.sim.particleplayground", "Particle Playground", pg, particle_playground), + spec( + "ec.sim.cchair", + "CC Hair", + vec![ + sp("length", "Length", px(30.0, 200.0)), + p("thickness", "Thickness", num(1.0), slider(0.05, 20.0, 0.1, 5.0, 2)), + p("weight", "Weight", num(0.2), slider(-10.0, 10.0, -2.0, 2.0, 2)), + p("constantMass", "Constant Mass", Value::Bool(false), ParamUi::Checkbox), + sp("density", "Density", px(100.0, 1000.0)), + // Hairfall Map + sp("mapStrength", "Map Strength", pc(0.0)), + p("mapLayer", "Map Layer", Value::Layer(None), ParamUi::Layer), + sp("mapSoftness", "Map Softness", px(0.0, 100.0)), + sp("addNoise", "Add Noise", pc(0.0)), + // Hair Color + p("hairColor", "Color", col(0.35, 0.25, 0.15), ParamUi::Color), + sp("brightness", "Brightness", px(100.0, 400.0)), + sp("opacity", "Opacity", pc(100.0)), + sp("colorInheritance", "Color Inheritance", pc(0.0)), + // Light + sp("lightIntensity", "Light Intensity", px(100.0, 400.0)), + p("lightDirection", "Light Direction", num(-45.0), ParamUi::Angle), + // Shading + sp("ambient", "Ambient", pc(40.0)), + sp("diffuse", "Diffuse", pc(60.0)), + sp("specular", "Specular", pc(30.0)), + sp("roughness", "Roughness", pc(20.0)), + p("randomSeed", "Random Seed", num(0.0), slider(0.0, 10000.0, 0.0, 1000.0, 0)), + ], + cc_hair, + ), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, MaskShape, run_fx}; + + fn run(vals: &[(&str, Value)], t: f64) -> Image { + run_fx("ec.sim.particleplayground", vals, Image::filled(80, 60, [0.2, 0.2, 0.2, 1.0]), t, EffectEnv::default()).img + } + + fn cannon(t: f64) -> Image { + run(&[("cannonPosition", Value::Vec2([40.0, 50.0])), ("velocity", num(60.0)), ("gravityForce", num(30.0))], t) + } + + #[test] + fn cannon_stream_is_seek_consistent() { + let a = cannon(1.0); + // Particles replace the layer: some coverage, mostly transparent, red. + let cov: f32 = a.data.iter().map(|p| p[3]).sum(); + assert!(cov > 5.0, "{cov}"); + assert!(a.data.iter().any(|p| p[3] > 0.5 && p[0] > 0.5 && p[1] < 0.1)); + // Seek back and forth: the same frame every time. + let _ = cannon(2.0); + let b = cannon(1.0); + assert_eq!(a.data, b.data); + let c = cannon(0.5); + assert_ne!(a.data, c.data); + // Nothing has been fired at time 0. + let z = cannon(0.0); + assert!(z.data.iter().all(|p| p[3] == 0.0)); + } + + #[test] + fn grid_falls_with_gravity_and_bounces_off_a_wall() { + let grid = |t: f64, env: EffectEnv| { + run_fx( + "ec.sim.particleplayground", + &[ + ("cannonEnabled", Value::Bool(false)), + ("gridPosition", Value::Vec2([40.0, 10.0])), + ("gridWidth", num(40.0)), + ("gridHeight", num(0.0)), + ("particlesAcross", num(5.0)), + ("particlesDown", num(1.0)), + ("gravityForce", num(100.0)), + ("wallBoundary", num(1.0)), + ], + Image::new(80, 60), + t, + env, + ) + .img + }; + // Alpha-weighted mean height of the particles. + let row_of = |img: &Image| { + let (mut s, mut n) = (0.0f64, 0.0f64); + for y in 0..60 { + for x in 0..80 { + let a = img.get(x, y)[3] as f64; + s += (y as f64 + 0.5) * a; + n += a; + } + } + (s / n.max(1e-9)).round() as i64 + }; + let t0 = grid(0.0, EffectEnv::default()); + let t1 = grid(0.5, EffectEnv::default()); + assert_eq!(row_of(&t0), 10); + // ½ g t² = 12.5 px. + assert!((row_of(&t1) - 22).abs() <= 2, "{}", row_of(&t1)); + // A wall (mask 1) whose floor is at y = 15 keeps them above it. + let masks = [MaskShape { name: "w".into(), points: vec![[0.0, 0.0], [80.0, 0.0], [80.0, 15.0], [0.0, 15.0]], closed: true, inverted: false }]; + let env = EffectEnv { masks: &masks, ..Default::default() }; + let w = grid(1.5, env); + assert!(row_of(&w) <= 16, "{}", row_of(&w)); + } + + #[test] + fn repel_spreads_particles() { + let spread = |force: f64| { + let img = run( + &[ + ("cannonEnabled", Value::Bool(false)), + ("gridPosition", Value::Vec2([40.0, 30.0])), + ("gridWidth", num(6.0)), + ("gridHeight", num(6.0)), + ("particlesAcross", num(3.0)), + ("particlesDown", num(3.0)), + ("gravityForce", num(0.0)), + ("repelForce", num(force)), + ("repelForceRadius", num(20.0)), + ], + 0.5, + ); + let (mut x0, mut x1) = (80, 0); + for y in 0..60 { + for x in 0..80 { + if img.get(x, y)[3] > 0.3 { + x0 = x0.min(x); + x1 = x1.max(x); + } + } + } + x1 - x0 + }; + assert!(spread(0.2) > spread(0.0) + 4, "{} vs {}", spread(0.2), spread(0.0)); + } + + #[test] + fn property_mapper_and_layer_map_read_layers() { + // Without a host, the layer-based features are inert (deterministic output). + let a = run(&[("mapRedTo", Value::Enum(6)), ("redMin", num(-50.0)), ("redMax", num(50.0))], 0.7); + let b = run(&[("mapRedTo", Value::Enum(6)), ("redMin", num(-50.0)), ("redMax", num(50.0))], 0.7); + assert_eq!(a.data, b.data); + let st = Pg { + cannon: false, + pos: [0.0; 2], + barrel_angle: 0.0, + barrel_radius: 0.0, + rate: 0.0, + dir_spread: 0.0, + vel: 0.0, + vel_spread: 0.0, + color: [1.0; 4], + radius: 2.0, + gravity: [0.0; 2], + grav_spread: 0.0, + repel: 0.0, + repel_radius: 0.0, + wall: None, + mapper: Some(MapImg { img: Image::filled(4, 4, [1.0, 0.0, 0.5, 1.0]), k: [1.0; 2], o: [0.0; 2] }), + map_to: [(6, 0.0, 120.0), (0, 0.0, 1.0), (4, 0.0, 2.0)], + seed: 1, + bounds: [-1e6, -1e6, 1e6, 1e6], + }; + let mut s = PgState::default(); + s.parts.push(PgParticle { p: [1.0, 1.0], v: [0.0; 2], origin: [1.0, 1.0], c: [1.0; 4], r: 2.0, mass: 1.0, friction: 0.0, force: [0.0; 2], id: 0 }); + st.step(&mut s); + // Red 1 → X velocity 120 px/s; blue 0.5 → scale 1 (radius 2). + assert!((s.parts[0].v[0] - 120.0).abs() < 1e-3); + assert!((s.parts[0].r - 2.0).abs() < 1e-3); + } + + #[test] + fn cc_hair_grows_from_opaque_pixels() { + let mut img = Image::new(60, 60); + for y in 20..40 { + for x in 20..40 { + img.set(x, y, [0.8, 0.6, 0.4, 1.0]); + } + } + let out = run_fx("ec.sim.cchair", &[("length", num(12.0)), ("density", num(300.0))], img.clone(), 0.0, EffectEnv::default()); + // Hair reaches beyond the square (above it: hair leans up, then droops). + let outside: f32 = (0..60).flat_map(|x| (0..20).map(move |y| (x, y))).map(|(x, y)| out.img.get(x, y)[3]).sum(); + assert!(outside > 1.0, "{outside}"); + // Nothing grows from transparent areas far away. + assert_eq!(out.img.get(2, 58)[3], 0.0); + let again = run_fx("ec.sim.cchair", &[("length", num(12.0)), ("density", num(300.0))], img.clone(), 3.0, EffectEnv::default()); + assert_eq!(out.img.data, again.img.data); + let none = run_fx("ec.sim.cchair", &[("length", num(0.0))], img.clone(), 0.0, EffectEnv::default()); + assert_eq!(none.img.data, img.data); + } +} From dec091e57539cf1642025df10736ea4b0eb196e4 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:27:59 -0400 Subject: [PATCH 08/23] M7.5: advanced 3D renderer Advanced 3D comps render their 3D runs as one scene of world-space triangles: model and primitive layers (meshes, skinning, animation clips by layer time), extruded text/shape layers, and textured cards for every other layer with their Material Options. Shading is physically based in linear light: Cook-Torrance GGX + Lambert for parallel/spot/point lights (AE falloff and cone), ambient lights, image-based light from an Environment light's layer (equirectangular; cosine irradiance map + roughness mip chain + split-sum BRDF fit), and PCF shadow maps (spot/parallel/point cube faces). 2x2 SSAA, depth of field as a gather blur by circle of confusion, back to the working encoding. The software rasteriser (depth buffer, top-left rule, perspective-correct, opaque visibility then sorted transparent blending) is the reference and CI path; effectcraft-gpu implements Accelerator::raster_3d with a wgpu render pipeline (advanced3d.wgsl) and matches it within tolerance. Environment layers are hidden from both renderers. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/commands/model3d.rs | 2 +- crates/gpu/src/adv3d.rs | 318 +++++++++ crates/gpu/src/context.rs | 3 + crates/gpu/src/lib.rs | 10 + crates/gpu/src/shaders/advanced3d.wgsl | 429 ++++++++++++ crates/gpu/src/tests_adv3d.rs | 167 +++++ crates/host/src/lib.rs | 33 + crates/render/src/lib.rs | 8 +- crates/render/src/shapes.rs | 21 + crates/render/src/three_d/adv/mod.rs | 165 +++++ crates/render/src/three_d/adv/raster.rs | 492 +++++++++++++ crates/render/src/three_d/adv/scene.rs | 872 ++++++++++++++++++++++++ crates/render/src/three_d/adv/shade.rs | 408 +++++++++++ crates/render/src/three_d/adv/tests.rs | 314 +++++++++ crates/render/src/three_d/compose.rs | 6 + crates/render/src/three_d/light.rs | 13 +- crates/render/src/three_d/mod.rs | 1 + 17 files changed, 3259 insertions(+), 3 deletions(-) create mode 100644 crates/gpu/src/adv3d.rs create mode 100644 crates/gpu/src/shaders/advanced3d.wgsl create mode 100644 crates/gpu/src/tests_adv3d.rs create mode 100644 crates/render/src/three_d/adv/mod.rs create mode 100644 crates/render/src/three_d/adv/raster.rs create mode 100644 crates/render/src/three_d/adv/scene.rs create mode 100644 crates/render/src/three_d/adv/shade.rs create mode 100644 crates/render/src/three_d/adv/tests.rs diff --git a/crates/engine/src/commands/model3d.rs b/crates/engine/src/commands/model3d.rs index 1ad9cc32..5de5a2c2 100644 --- a/crates/engine/src/commands/model3d.rs +++ b/crates/engine/src/commands/model3d.rs @@ -349,7 +349,7 @@ pub fn specs() -> Vec { "3D Model Layer", [], None, - "{item?: id|name (a 3D model footage item) | path?: .gltf|.glb|.obj file, name?, time? (s)}", + "{item?: id or name of a 3D model footage item, path?: a .gltf, .glb or .obj file to import, name?, time? (s)}", has_comp, new_model ), diff --git a/crates/gpu/src/adv3d.rs b/crates/gpu/src/adv3d.rs new file mode 100644 index 00000000..6e428c84 --- /dev/null +++ b/crates/gpu/src/adv3d.rs @@ -0,0 +1,318 @@ +//! The Advanced 3D rasteriser on wgpu: a render pipeline with a reversed-Z depth buffer +//! (`Depth32Float`, Greater), the scene's materials, textures, lights and shadow maps in +//! storage buffers, and `advanced3d.wgsl`'s physically based fragment shader. Opaque triangles +//! are drawn with depth writes, then the sorted transparent ones blended over (premultiplied). +//! Colour (`Rgba16Float`) and camera depth (`R32Float`) are read back for the CPU's resolve, +//! depth of field and encoding. + +use effectcraft_render::three_d::adv::{Scene, Target}; +use wgpu::util::DeviceExt; + +use crate::context::{Enc, GpuContext}; + +/// Pipelines (built on first use). +pub(crate) struct Pipes { + bgl: wgpu::BindGroupLayout, + opaque: wgpu::RenderPipeline, + transparent: wgpu::RenderPipeline, +} + +const COLOR: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; +const DEPTH_OUT: wgpu::TextureFormat = wgpu::TextureFormat::R32Float; +const DEPTH: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; +const VERTEX_SIZE: u64 = 52; + +fn storage(binding: u32) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: None }, + count: None, + } +} + +impl Pipes { + fn new(device: &wgpu::Device) -> Pipes { + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("effectcraft advanced 3d"), + source: wgpu::ShaderSource::Wgsl(include_str!("shaders/advanced3d.wgsl").into()), + }); + let mut entries = vec![wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: false, min_binding_size: None }, + count: None, + }]; + entries.extend((1..=6).map(storage)); + let bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("advanced 3d"), entries: &entries }); + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("advanced 3d"), bind_group_layouts: &[Some(&bgl)], immediate_size: 0 }); + let attrs = [ + wgpu::VertexAttribute { format: wgpu::VertexFormat::Float32x3, offset: 0, shader_location: 0 }, + wgpu::VertexAttribute { format: wgpu::VertexFormat::Float32x3, offset: 12, shader_location: 1 }, + wgpu::VertexAttribute { format: wgpu::VertexFormat::Float32x2, offset: 24, shader_location: 2 }, + wgpu::VertexAttribute { format: wgpu::VertexFormat::Float32x4, offset: 32, shader_location: 3 }, + wgpu::VertexAttribute { format: wgpu::VertexFormat::Uint32, offset: 48, shader_location: 4 }, + ]; + let make = |transparent: bool| { + let blend = transparent.then_some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING); + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(if transparent { "advanced 3d transparent" } else { "advanced 3d opaque" }), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &module, + entry_point: Some("vs"), + compilation_options: Default::default(), + buffers: &[Some(wgpu::VertexBufferLayout { array_stride: VERTEX_SIZE, step_mode: wgpu::VertexStepMode::Vertex, attributes: &attrs })], + }, + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Ccw, + cull_mode: None, + ..Default::default() + }, + depth_stencil: Some(wgpu::DepthStencilState { + format: DEPTH, + depth_write_enabled: Some(!transparent), + depth_compare: Some(wgpu::CompareFunction::Greater), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("fs"), + compilation_options: Default::default(), + targets: &[ + Some(wgpu::ColorTargetState { format: COLOR, blend, write_mask: wgpu::ColorWrites::ALL }), + Some(wgpu::ColorTargetState { + format: DEPTH_OUT, + blend: None, + write_mask: if transparent { wgpu::ColorWrites::empty() } else { wgpu::ColorWrites::ALL }, + }), + ], + }), + multiview_mask: None, + cache: None, + }) + }; + Pipes { opaque: make(false), transparent: make(true), bgl } + } +} + +fn f32s(v: impl IntoIterator) -> Vec { + let mut b: Vec = v.into_iter().flat_map(f32::to_le_bytes).collect(); + while b.len() < 16 { + b.push(0); + } + b +} + +/// Pack the scene's arrays as the shader's storage buffers. +fn pack(s: &Scene) -> [Vec; 7] { + let mut globals: Vec = vec![]; + // Column-major clip matrix. + for c in 0..4 { + for r in 0..4 { + globals.push(s.clip[r][c]); + } + } + globals.extend(s.view[2]); + globals.extend([s.eye[0], s.eye[1], s.eye[2], s.ortho as u32 as f32]); + globals.extend([s.cam_fwd[0], s.cam_fwd[1], s.cam_fwd[2], s.lights.len() as f32]); + globals.extend([s.ambient[0], s.ambient[1], s.ambient[2], s.env.is_some() as u32 as f32]); + let e = s.env.unwrap_or(effectcraft_render::three_d::adv::EnvInfo { radiance: 0, mips: 1, irradiance: 0, intensity: 0.0, rotation: 0.0 }); + globals.extend([e.radiance as f32, e.mips as f32, e.irradiance as f32, e.intensity]); + let lit = !(s.lights.is_empty() && s.env.is_none() && s.ambient == [0.0; 3]); + globals.extend([e.rotation, lit as u32 as f32, 0.0, 0.0]); + let mats = s.materials.iter().flat_map(|m| { + let flags = (m.double_sided as u32) | (m.unlit as u32) << 1 | (m.accepts_lights as u32) << 2 | (m.receives_shadows as u32) << 3 | (m.alpha_mode << 8); + [ + m.base[0], + m.base[1], + m.base[2], + m.base[3], + m.emissive[0], + m.emissive[1], + m.emissive[2], + m.metallic, + m.roughness, + m.normal_scale, + m.occlusion_strength, + m.alpha_cutoff, + m.tex_base as f32, + m.tex_mr as f32, + m.tex_normal as f32, + m.tex_occlusion as f32, + m.tex_emissive as f32, + flags as f32, + m.diffuse_k, + m.specular_k, + m.ambient_k, + m.opacity, + 0.0, + 0.0, + ] + }); + let mut tex: Vec = s.textures.iter().flat_map(|t| [t.offset, t.width, t.height, t.wrap_u | t.wrap_v << 8]).flat_map(u32::to_le_bytes).collect(); + while tex.len() < 16 { + tex.push(0); + } + let texels = f32s(s.texels.iter().flatten().copied()); + let lights = s.lights.iter().flat_map(|l| { + [ + l.pos[0], + l.pos[1], + l.pos[2], + l.kind as f32, + l.dir[0], + l.dir[1], + l.dir[2], + l.cos_outer, + l.color[0], + l.color[1], + l.color[2], + l.cos_inner, + l.falloff as f32, + l.radius, + l.falloff_distance, + l.shadow as f32, + l.shadow_darkness, + 0.0, + 0.0, + 0.0, + ] + }); + let shadows = s.shadows.iter().flat_map(|m| { + let mut v: Vec = m.view.iter().flatten().copied().collect(); + v.extend([m.focal, m.ortho as u32 as f32, f32::from_bits(m.size), f32::from_bits(m.offset), m.bias, m.radius, 0.0, 0.0]); + v + }); + [f32s(globals), f32s(mats), tex, texels, f32s(lights), f32s(shadows), f32s(s.shadow_texels.iter().copied())] +} + +fn half_to_f32(h: u16) -> f32 { + let s = if h & 0x8000 != 0 { -1.0 } else { 1.0 }; + let e = ((h >> 10) & 0x1f) as i32; + let m = (h & 0x3ff) as f32; + match e { + 0 => s * m * 2f32.powi(-24), + 31 => { + if m == 0.0 { + s * f32::INFINITY + } else { + f32::NAN + } + } + _ => s * (1.0 + m / 1024.0) * 2f32.powi(e - 15), + } +} + +/// Rasterise a scene. `None` when the device can't (no readback, size or buffer limits). +pub(crate) fn render(g: &GpuContext, s: &Scene) -> Option { + if !g.can_readback() || !g.fits(s.width, s.height) || s.indices.is_empty() { + return None; + } + let limits = g.device.limits(); + let bufs = pack(s); + if bufs.iter().any(|b| b.len() as u64 > limits.max_storage_buffer_binding_size) { + return None; + } + let p = g.adv3d.get_or_init(|| Pipes::new(&g.device)); + let dev = &g.device; + let vb: Vec = s + .vertices + .iter() + .flat_map(|v| { + let mut b = Vec::with_capacity(VERTEX_SIZE as usize); + for x in v.pos.iter().chain(&v.normal).chain(&v.uv).chain(&v.tangent) { + b.extend_from_slice(&x.to_le_bytes()); + } + b.extend_from_slice(&v.material.to_le_bytes()); + b + }) + .collect(); + let vbuf = dev.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("adv3d vertices"), contents: &vb, usage: wgpu::BufferUsages::VERTEX }); + let ib: Vec = s.indices.iter().flat_map(|i| i.to_le_bytes()).collect(); + let ibuf = dev.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("adv3d indices"), contents: &ib, usage: wgpu::BufferUsages::INDEX }); + let ub = dev.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("adv3d globals"), contents: &bufs[0], usage: wgpu::BufferUsages::UNIFORM }); + let sbs: Vec = bufs[1..] + .iter() + .map(|b| dev.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("adv3d data"), contents: b, usage: wgpu::BufferUsages::STORAGE })) + .collect(); + let mut entries = vec![wgpu::BindGroupEntry { binding: 0, resource: ub.as_entire_binding() }]; + for (i, b) in sbs.iter().enumerate() { + entries.push(wgpu::BindGroupEntry { binding: i as u32 + 1, resource: b.as_entire_binding() }); + } + let bg = dev.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("adv3d"), layout: &p.bgl, entries: &entries }); + let tex = |format: wgpu::TextureFormat, usage: wgpu::TextureUsages| { + dev.create_texture(&wgpu::TextureDescriptor { + label: Some("adv3d target"), + size: wgpu::Extent3d { width: s.width, height: s.height, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) + }; + let rt = wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC; + let color = tex(COLOR, rt); + let zout = tex(DEPTH_OUT, rt); + let depth = tex(DEPTH, wgpu::TextureUsages::RENDER_ATTACHMENT); + let (cv, zv, dv) = (color.create_view(&Default::default()), zout.create_view(&Default::default()), depth.create_view(&Default::default())); + let mut enc = dev.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("adv3d") }); + { + let mut pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("adv3d"), + color_attachments: &[ + Some(wgpu::RenderPassColorAttachment { + view: &cv, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), store: wgpu::StoreOp::Store }, + }), + Some(wgpu::RenderPassColorAttachment { + view: &zv, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { r: -1.0, g: 0.0, b: 0.0, a: 0.0 }), store: wgpu::StoreOp::Store }, + }), + ], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &dv, + depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(0.0), store: wgpu::StoreOp::Store }), + stencil_ops: None, + }), + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_bind_group(0, &bg, &[]); + pass.set_vertex_buffer(0, vbuf.slice(..)); + pass.set_index_buffer(ibuf.slice(..), wgpu::IndexFormat::Uint32); + let n = s.indices.len() as u32; + if s.opaque_count > 0 { + pass.set_pipeline(&p.opaque); + pass.draw_indexed(0..s.opaque_count, 0, 0..1); + } + if n > s.opaque_count { + pass.set_pipeline(&p.transparent); + pass.draw_indexed(s.opaque_count..n, 0, 0..1); + } + } + g.queue.submit([enc.finish()]); + let mut e = Enc::new(g); + let cb = e.read_texture(&color, s.width, s.height, 8)?; + let zb = e.read_texture(&zout, s.width, s.height, 4)?; + let color = cb.chunks_exact(8).map(|c| [0, 1, 2, 3].map(|k| half_to_f32(u16::from_le_bytes([c[2 * k], c[2 * k + 1]])))).collect(); + let depth = zb + .chunks_exact(4) + .map(|c| { + let z = f32::from_le_bytes([c[0], c[1], c[2], c[3]]); + if z < 0.0 { f32::INFINITY } else { z } + }) + .collect(); + Some(Target { width: s.width, height: s.height, color, depth }) +} diff --git a/crates/gpu/src/context.rs b/crates/gpu/src/context.rs index 9f814377..1b141301 100644 --- a/crates/gpu/src/context.rs +++ b/crates/gpu/src/context.rs @@ -94,6 +94,8 @@ pub struct GpuContext { dummy_tex: wgpu::TextureView, dummy_buf: wgpu::Buffer, uploads: Mutex, + /// Advanced 3D render pipelines (built on first use). + pub(crate) adv3d: std::sync::OnceLock, } fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { @@ -225,6 +227,7 @@ impl GpuContext { dummy_tex: dummy.create_view(&Default::default()), dummy_buf, uploads: Mutex::new(Uploads::default()), + adv3d: std::sync::OnceLock::new(), }) } diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index e7b54ab2..c299cf5d 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -9,6 +9,9 @@ //! clamping and quantisation, and converts colour spaces. 3D runs, adjustment layers and //! wireframes run on the CPU between GPU steps (read back, draw, upload). //! +//! Advanced 3D comps rasterise on a render pipeline (`advanced3d.wgsl`: depth buffer, PBR, +//! image-based light, shadow maps), see [`Accelerator::raster_3d`]. +//! //! GPU effects ([`effectcraft_effects::GPU_EFFECTS`]) run as compute kernels with the CPU //! effect's exact steps (padding, box-blur radii, parameter conversions); chains of them are //! uploaded and read back once. @@ -18,6 +21,7 @@ //! skip readback entirely with [`Gpu::render_display`], which leaves an RGBA8 texture for //! egui-wgpu to draw. +mod adv3d; mod context; mod effects; mod ops; @@ -121,7 +125,13 @@ impl Accelerator for Gpu { fn effects(&self, chain: &[FxStep], buf: &Buf, levels: Option) -> Option { effects::run_chain(&mut Enc::new(&self.ctx), chain, buf, levels) } + + fn raster_3d(&self, scene: &effectcraft_render::three_d::adv::Scene) -> Option { + adv3d::render(&self.ctx, scene) + } } #[cfg(test)] mod tests; +#[cfg(test)] +mod tests_adv3d; diff --git a/crates/gpu/src/shaders/advanced3d.wgsl b/crates/gpu/src/shaders/advanced3d.wgsl new file mode 100644 index 00000000..9ae81ff5 --- /dev/null +++ b/crates/gpu/src/shaders/advanced3d.wgsl @@ -0,0 +1,429 @@ +// Advanced 3D: rasterised meshes with physically based shading. A step-for-step port of +// `effectcraft_render::three_d::adv::shade` (Cook–Torrance GGX + Lambert, image-based light +// from an equirectangular environment, PCF shadow maps); the CPU rasteriser is the reference. + +struct Globals { + clip: mat4x4, + // Camera-space depth row (x, y, z, w). + view_z: vec4, + // eye.xyz, ortho (0/1) + eye: vec4, + // cam_fwd.xyz, light count + fwd: vec4, + // ambient.rgb, environment present (0/1) + ambient: vec4, + // radiance first texture, mip count, irradiance texture, intensity + env: vec4, + // rotation, any lighting (0/1), -, - + env2: vec4, +}; + +@group(0) @binding(0) var g: Globals; +@group(0) @binding(1) var materials: array>; +@group(0) @binding(2) var texinfo: array>; +@group(0) @binding(3) var texels: array>; +@group(0) @binding(4) var lights: array>; +@group(0) @binding(5) var shadows: array>; +@group(0) @binding(6) var shadow_texels: array; + +const PI: f32 = 3.14159265358979; +const MAT_STRIDE: u32 = 6u; +const LIGHT_STRIDE: u32 = 5u; +const SHADOW_STRIDE: u32 = 5u; + +struct VIn { + @location(0) pos: vec3, + @location(1) normal: vec3, + @location(2) uv: vec2, + @location(3) tangent: vec4, + @location(4) material: u32, +}; + +struct VOut { + @builtin(position) clip: vec4, + @location(0) pos: vec3, + @location(1) normal: vec3, + @location(2) uv: vec2, + @location(3) tangent: vec4, + @location(4) @interpolate(flat) material: u32, +}; + +@vertex +fn vs(v: VIn) -> VOut { + var o: VOut; + o.clip = g.clip * vec4(v.pos, 1.0); + o.pos = v.pos; + o.normal = v.normal; + o.uv = v.uv; + o.tangent = v.tangent; + o.material = v.material; + return o; +} + +fn norm3(a: vec3) -> vec3 { + let l = sqrt(dot(a, a)); + if (l > 1e-20) { + return a / l; + } + return vec3(0.0, 0.0, -1.0); +} + +fn srgb_to_linear(c: f32) -> f32 { + if (c <= 0.04045) { + return c / 12.92; + } + return pow((c + 0.055) / 1.055, 2.4); +} + +fn wrap_i(i: i32, n: i32, mode: u32) -> i32 { + if (mode == 1u) { + return clamp(i, 0, n - 1); + } + if (mode == 2u) { + let p = 2 * n; + var m = i % p; + if (m < 0) { + m = m + p; + } + if (m >= n) { + return p - 1 - m; + } + return m; + } + var m = i % n; + if (m < 0) { + m = m + n; + } + return m; +} + +fn texel(t: vec4, x: i32, y: i32) -> vec4 { + let w = i32(t.y); + let h = i32(t.z); + let xi = wrap_i(x, w, t.w & 255u); + let yi = wrap_i(y, h, (t.w >> 8u) & 255u); + return texels[t.x + u32(yi * w + xi)]; +} + +fn sample_tex(i: i32, uv: vec2) -> vec4 { + let t = texinfo[u32(i)]; + if (t.y == 0u || t.z == 0u) { + return vec4(1.0); + } + let x = uv.x * f32(t.y) - 0.5; + let y = uv.y * f32(t.z) - 0.5; + let x0 = floor(x); + let y0 = floor(y); + let fx = x - x0; + let fy = y - y0; + let xi = i32(x0); + let yi = i32(y0); + let a = texel(t, xi, yi); + let b = texel(t, xi + 1, yi); + let c = texel(t, xi, yi + 1); + let d = texel(t, xi + 1, yi + 1); + let top = a + (b - a) * fx; + let bot = c + (d - c) * fx; + return top + (bot - top) * fy; +} + +fn d_ggx(n_h: f32, a: f32) -> f32 { + let a2 = a * a; + let d = n_h * n_h * (a2 - 1.0) + 1.0; + return a2 / max(PI * d * d, 1e-12); +} + +fn g1(x: f32, k: f32) -> f32 { + return x / (x * (1.0 - k) + k); +} + +fn brdf(base: vec3, metallic: f32, rough_in: f32, spec_k: f32, diff_k: f32, n: vec3, v: vec3, l: vec3) -> vec3 { + let n_l = dot(n, l); + if (n_l <= 0.0) { + return vec3(0.0); + } + let n_v = max(dot(n, v), 1e-4); + let h = norm3(v + l); + let n_h = max(dot(n, h), 0.0); + let v_h = max(dot(v, h), 0.0); + let r = clamp(rough_in, 0.03, 1.0); + let f0 = mix(vec3(0.04 * spec_k), base, metallic); + let fk = pow(clamp(1.0 - v_h, 0.0, 1.0), 5.0); + let f = f0 + (vec3(1.0) - f0) * fk; + let d = d_ggx(n_h, r * r); + let k = (r + 1.0) * (r + 1.0) / 8.0; + let gg = g1(n_v, k) * g1(n_l, k); + let spec = d * gg / max(4.0 * n_v * n_l, 1e-4); + let kd = (vec3(1.0) - f) * (1.0 - metallic); + return (kd * base / PI * diff_k + f * spec) * n_l * PI; +} + +fn env_brdf(n_v: f32, rough: f32) -> vec2 { + let c0 = vec4(-1.0, -0.0275, -0.572, 0.022); + let c1 = vec4(1.0, 0.0425, 1.04, -0.04); + let r = rough * c0 + c1; + let a004 = min(r.x * r.x, exp2(-9.28 * n_v)) * r.x + r.y; + return vec2(-1.04 * a004 + r.z, 1.04 * a004 + r.w); +} + +fn equirect_uv(d: vec3, rot: f32) -> vec2 { + let u = 0.5 + (atan2(d.x, d.z) + rot) / (2.0 * PI); + let v = acos(clamp(-d.y, -1.0, 1.0)) / PI; + return vec2(u - floor(u), v); +} + +fn shadow_factor(m: u32, p: vec3, n_l: f32) -> f32 { + let r0 = shadows[m]; + let r1 = shadows[m + 1u]; + let r2 = shadows[m + 2u]; + let info = shadows[m + 3u]; + let extra = shadows[m + 4u]; + let q = vec3(dot(r0.xyz, p) + r0.w, dot(r1.xyz, p) + r1.w, dot(r2.xyz, p) + r2.w); + let focal = info.x; + let ortho = info.y > 0.5; + let size = bitcast(info.z); + let offset = bitcast(info.w); + let c = f32(size) * 0.5; + var u: f32; + var v: f32; + if (ortho) { + u = q.x * focal + c; + v = q.y * focal + c; + } else { + if (q.z <= 1e-3) { + return 1.0; + } + u = q.x * focal / q.z + c; + v = q.y * focal / q.z + c; + } + let z = q.z; + let bias = extra.x * (1.0 + 2.0 * (1.0 - clamp(n_l, 0.0, 1.0))) + abs(z) * 0.0015; + let step = extra.y / 2.0; + let sz = i32(size); + var lit = 0.0; + for (var j = -2; j <= 2; j = j + 1) { + for (var i = -2; i <= 2; i = i + 1) { + let x = i32(floor(u + f32(i) * step)); + let y = i32(floor(v + f32(j) * step)); + if (x < 0 || y < 0 || x >= sz || y >= sz) { + lit = lit + 1.0; + continue; + } + let d = shadow_texels[offset + u32(y * sz + x)]; + if (z - bias <= d) { + lit = lit + 1.0; + } + } + } + return lit / 25.0; +} + +fn light_shadow(li: u32, kind: u32, lpos: vec3, shadow: i32, darkness: f32, p: vec3, n_l: f32) -> f32 { + if (shadow < 0) { + return 1.0; + } + var face = 0u; + if (kind == 2u) { + let d = p - lpos; + let a = abs(d); + if (a.x >= a.y && a.x >= a.z) { + face = select(1u, 0u, d.x > 0.0); + } else if (a.y >= a.z) { + face = select(3u, 2u, d.y > 0.0); + } else { + face = select(5u, 4u, d.z > 0.0); + } + } + let f = shadow_factor((u32(shadow) + face) * SHADOW_STRIDE, p, n_l); + return 1.0 - darkness * (1.0 - f); +} + +fn attenuation(kind: u32, falloff: u32, radius: f32, falloff_distance: f32, cos_inner: f32, cos_outer: f32, dir: vec3, to_l: vec3, dist: f32) -> f32 { + var k = 1.0; + if (falloff == 1u && kind != 0u) { + if (dist <= radius) { + k = 1.0; + } else if (falloff_distance <= 0.0) { + k = 0.0; + } else { + let t = clamp((dist - radius) / falloff_distance, 0.0, 1.0); + k = 1.0 - t * t * (3.0 - 2.0 * t); + } + } else if (falloff == 2u && kind != 0u) { + if (dist <= radius || (radius <= 0.0 && dist <= 1.0)) { + k = 1.0; + } else { + let q = max(radius, 1.0) / dist; + k = q * q; + } + } + if (kind == 1u) { + let cos_a = clamp(dot(-to_l, dir), -1.0, 1.0); + if (cos_a >= cos_inner) { + k = k * 1.0; + } else if (cos_a <= cos_outer) { + k = 0.0; + } else { + let a = acos(cos_a); + let ai = acos(cos_inner); + let ao = acos(cos_outer); + let t = clamp((a - ai) / max(ao - ai, 1e-6), 0.0, 1.0); + k = k * (1.0 - t * t * (3.0 - 2.0 * t)); + } + } + return k; +} + +struct FOut { + @location(0) color: vec4, + @location(1) depth: f32, +}; + +@fragment +fn fs(i: VOut, @builtin(front_facing) front: bool) -> FOut { + let mb = i.material * MAT_STRIDE; + let m0 = materials[mb]; + let m1 = materials[mb + 1u]; + let m2 = materials[mb + 2u]; + let m3 = materials[mb + 3u]; + let m4 = materials[mb + 4u]; + let m5 = materials[mb + 5u]; + let flags = u32(m4.y); + let double_sided = (flags & 1u) != 0u; + let unlit = (flags & 2u) != 0u; + let accepts_lights = (flags & 4u) != 0u; + let receives_shadows = (flags & 8u) != 0u; + let alpha_mode = (flags >> 8u) & 3u; + if (!front && !double_sided) { + discard; + } + var base = m0; + let tex_base = i32(m3.x); + if (tex_base >= 0) { + base = base * sample_tex(tex_base, i.uv); + } + let opacity = m5.y; + var alpha = base.a * opacity; + if (alpha_mode == 1u && alpha < m2.w) { + discard; + } + if (alpha <= 1e-5 && alpha_mode != 0u) { + discard; + } + if (alpha_mode == 0u) { + alpha = opacity; + } + let rgb = base.rgb; + var emissive = m1.xyz; + let tex_em = i32(m4.x); + if (tex_em >= 0) { + emissive = emissive * sample_tex(tex_em, i.uv).rgb; + } + var out: FOut; + out.depth = dot(g.view_z.xyz, i.pos) + g.view_z.w; + if (unlit || !accepts_lights || g.env2.y < 0.5) { + let c = rgb + emissive; + out.color = vec4(c * alpha, alpha); + return out; + } + var metallic = m1.w; + var rough = m2.x; + let tex_mr = i32(m3.y); + if (tex_mr >= 0) { + let t = sample_tex(tex_mr, i.uv); + rough = rough * t.y; + metallic = metallic * t.z; + } + var n = norm3(i.normal); + if (!front) { + n = -n; + } + let tex_n = i32(m3.z); + if (tex_n >= 0) { + let t = sample_tex(tex_n, i.uv); + let ns = m2.y; + let tn = vec3((t.x * 2.0 - 1.0) * ns, (t.y * 2.0 - 1.0) * ns, t.z * 2.0 - 1.0); + let tg = norm3(i.tangent.xyz - n * dot(n, i.tangent.xyz)); + let sign = select(-i.tangent.w, i.tangent.w, front); + let bt = cross(n, tg) * sign; + n = norm3(tg * tn.x + bt * tn.y + n * tn.z); + } + var ao = 1.0; + let tex_ao = i32(m3.w); + if (tex_ao >= 0) { + ao = 1.0 + m2.z * (sample_tex(tex_ao, i.uv).x - 1.0); + } + var v: vec3; + if (g.eye.w > 0.5) { + v = -g.fwd.xyz; + } else { + v = norm3(g.eye.xyz - i.pos); + } + let diff_k = m4.z; + let spec_k = m4.w; + let amb_k = m5.x; + var acc = vec3(0.0); + let nl_count = u32(g.fwd.w); + for (var li = 0u; li < nl_count; li = li + 1u) { + let lb = li * LIGHT_STRIDE; + let l0 = lights[lb]; + let l1 = lights[lb + 1u]; + let l2 = lights[lb + 2u]; + let l3 = lights[lb + 3u]; + let l4 = lights[lb + 4u]; + let kind = u32(l0.w); + var to_l: vec3; + var dist: f32; + if (kind == 0u) { + to_l = -l1.xyz; + dist = 1e30; + } else { + let d = l0.xyz - i.pos; + let len = sqrt(dot(d, d)); + if (len < 1e-6) { + to_l = vec3(0.0, 0.0, -1.0); + dist = 0.0; + } else { + to_l = d / len; + dist = len; + } + } + let k = attenuation(kind, u32(l3.x), l3.y, l3.z, l2.w, l1.w, l1.xyz, to_l, dist); + if (k <= 0.0) { + continue; + } + let n_l = dot(n, to_l); + if (n_l <= 0.0) { + continue; + } + var sh = 1.0; + if (receives_shadows) { + sh = light_shadow(li, kind, l0.xyz, i32(l3.w), l4.x, i.pos, n_l); + } + let b = brdf(rgb, metallic, rough, spec_k, diff_k, n, v, to_l); + acc = acc + b * l2.xyz * k * sh; + } + acc = acc + g.ambient.xyz * rgb * amb_k * ao; + if (g.ambient.w > 0.5) { + let n_v = clamp(dot(n, v), 1e-4, 1.0); + let r = n * (2.0 * dot(n, v)) - v; + let rot = g.env2.x; + let irr = sample_tex(i32(g.env.z), equirect_uv(n, rot)).rgb * g.env.w; + let mips = u32(g.env.y); + let lv = clamp(rough, 0.0, 1.0) * f32(max(mips, 1u) - 1u); + let lo = u32(floor(lv)); + let hi = min(lo + 1u, max(mips, 1u) - 1u); + let f = lv - f32(lo); + let ruv = equirect_uv(r, rot); + let sa = sample_tex(i32(g.env.x) + i32(lo), ruv).rgb; + let sb = sample_tex(i32(g.env.x) + i32(hi), ruv).rgb; + let spec = (sa + (sb - sa) * f) * g.env.w; + let f0 = mix(vec3(0.04 * spec_k), rgb, metallic); + let ab = env_brdf(n_v, rough); + let fs = f0 * ab.x + vec3(ab.y); + acc = acc + (irr * rgb * (vec3(1.0) - fs) * (1.0 - metallic) * diff_k + spec * fs) * ao; + } + let c = acc + emissive; + out.color = vec4(c * alpha, alpha); + return out; +} diff --git a/crates/gpu/src/tests_adv3d.rs b/crates/gpu/src/tests_adv3d.rs new file mode 100644 index 00000000..067e4c8a --- /dev/null +++ b/crates/gpu/src/tests_adv3d.rs @@ -0,0 +1,167 @@ +//! Advanced 3D: the GPU rasteriser against the CPU reference on the same scenes. +//! +//! Tolerances: the GPU's colour target is half float (relative error ≤ 1e-3) and its +//! rasteriser and interpolators differ from the CPU's in the last bits, so pixels on triangle +//! edges may be covered by the other neighbour. Interior pixels agree within 2/255; at most +//! 1.5 % of pixels (silhouettes) may differ more. Skipped without an adapter. + +use effectcraft_keyframe::Value; +use effectcraft_project::{Comp, ItemId, ItemKind, LayerSource, LightKind, PrimitiveKind, Project, Renderer as R3, Solid, build}; +use effectcraft_render::three_d::adv::{self, Scene}; +use effectcraft_render::{Accelerator, EvalCtx, NoFootage, RenderOpts, Renderer}; +use effectcraft_time::{FrameRate, Tick}; + +use crate::Gpu; + +fn gpu() -> Option { + let g = Gpu::headless(); + if g.is_none() { + eprintln!("effectcraft-gpu adv3d tests: no GPU adapter, skipping"); + } + g +} + +fn add(p: &mut Project, cid: ItemId, src: LayerSource, edit: impl FnOnce(&mut effectcraft_project::Layer)) { + let comp = p.comp(cid).unwrap().clone(); + let mut l = build::layer(p, &comp, "L", src, (comp.width, comp.height), None); + edit(&mut l); + p.comp_mut(cid).unwrap().layers.insert(0, l); +} + +fn set(l: &mut effectcraft_project::Layer, path: &str, v: Value) { + l.props.prop_mut(path).unwrap_or_else(|| panic!("{path}")).value = v; +} + +/// Primitives with metal/dielectric materials, a shadow-casting spot light and a point light, +/// an environment light, a semi-transparent textured card. +fn scene_project() -> (Project, ItemId) { + let mut p = Project::default(); + let mut c = Comp::new(240, 160, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + c.renderer = R3::Advanced3D; + let cid = p.add_item("C", Default::default(), None, ItemKind::Comp(c.into())); + add(&mut p, cid, LayerSource::Primitive { kind: PrimitiveKind::Plane }, |l| { + set(l, "geometryOptions/width", Value::Scalar(900.0)); + set(l, "geometryOptions/height", Value::Scalar(900.0)); + set(l, "transform/position", Value::Vec3([120.0, 130.0, 0.0])); + set(l, "transform/rotationX", Value::Scalar(90.0)); + }); + add(&mut p, cid, LayerSource::Primitive { kind: PrimitiveKind::Sphere }, |l| { + set(l, "geometryOptions/radius", Value::Scalar(35.0)); + set(l, "transform/position", Value::Vec3([80.0, 95.0, 0.0])); + set(l, "materialOptions/baseColor", Value::Color([0.9, 0.2, 0.2, 1.0])); + set(l, "materialOptions/roughness", Value::Scalar(35.0)); + set(l, "materialOptions/castsShadows", Value::Enum(1)); + }); + add(&mut p, cid, LayerSource::Primitive { kind: PrimitiveKind::Torus }, |l| { + set(l, "geometryOptions/radius", Value::Scalar(35.0)); + set(l, "geometryOptions/tubeRadius", Value::Scalar(12.0)); + set(l, "transform/position", Value::Vec3([165.0, 90.0, 0.0])); + set(l, "transform/rotationX", Value::Scalar(60.0)); + set(l, "materialOptions/metallic", Value::Scalar(100.0)); + set(l, "materialOptions/roughness", Value::Scalar(20.0)); + set(l, "materialOptions/castsShadows", Value::Enum(1)); + }); + let sid = p.add_item("S", Default::default(), None, ItemKind::Solid(Solid { color: [0.2, 0.6, 0.9], width: 60, height: 40, pixel_aspect: 1.0 })); + add(&mut p, cid, LayerSource::Solid { item: sid }, |l| { + l.switches.three_d = true; + set(l, "transform/opacity", Value::Scalar(60.0)); + set(l, "transform/position", Value::Vec3([120.0, 80.0, -60.0])); + set(l, "transform/rotationY", Value::Scalar(30.0)); + }); + let env = p.add_item("E", Default::default(), None, ItemKind::Solid(Solid { color: [0.6, 0.7, 1.0], width: 32, height: 16, pixel_aspect: 1.0 })); + add(&mut p, cid, LayerSource::Solid { item: env }, |l| { + l.environment = true; + l.switches.three_d = true; + }); + add(&mut p, cid, LayerSource::Light { kind: LightKind::Environment }, |l| set(l, "lightOptions/intensity", Value::Scalar(50.0))); + add(&mut p, cid, LayerSource::Light { kind: LightKind::Spot }, |l| { + set(l, "transform/position", Value::Vec3([40.0, -120.0, -200.0])); + set(l, "transform/poi", Value::Vec3([120.0, 100.0, 0.0])); + set(l, "lightOptions/castsShadows", Value::Bool(true)); + set(l, "lightOptions/coneAngle", Value::Scalar(110.0)); + set(l, "lightOptions/shadowDiffusion", Value::Scalar(4.0)); + }); + add(&mut p, cid, LayerSource::Light { kind: LightKind::Point }, |l| { + set(l, "transform/position", Value::Vec3([200.0, 20.0, -100.0])); + set(l, "lightOptions/intensity", Value::Scalar(40.0)); + set(l, "lightOptions/falloff", Value::Enum(1)); + }); + (p, cid) +} + +fn scene(p: &Project, cid: ItemId) -> Scene { + let r = Renderer::new(p, &NoFootage, RenderOpts::default()); + let ctx = EvalCtx { project: p, comp_id: cid, comp: p.comp(cid).unwrap(), time: Tick::ZERO, expr: None }; + let run: Vec<&effectcraft_project::Layer> = ctx.comp.layers.iter().rev().filter(|l| l.is_3d() && l.has_video()).collect(); + adv::scene_of(&r, &ctx, &run, (ctx.comp.width, ctx.comp.height)) +} + +fn compare(s: &Scene, g: &Gpu) { + let cpu = adv::raster::render(s); + let gt = g.raster_3d(s).expect("gpu raster"); + assert_eq!((gt.width, gt.height), (cpu.width, cpu.height)); + let n = cpu.color.len(); + let mut bad = 0; + let mut sum = 0.0f64; + for (a, b) in cpu.color.iter().zip(>.color) { + let d = (0..4).map(|k| (a[k] - b[k]).abs() / a[k].abs().max(1.0)).fold(0.0f32, f32::max); + sum += d as f64; + if d > 2.0 / 255.0 { + bad += 1; + } + } + let frac = bad as f64 / n as f64; + eprintln!("adv3d gpu vs cpu: {bad}/{n} pixels beyond 2/255 ({:.3} %), mean {:.5}", frac * 100.0, sum / n as f64); + assert!(frac < 0.015, "{:.3} % of pixels differ", frac * 100.0); + assert!(sum / (n as f64) < 0.002); + // Depth agrees where both see a surface. + let mut dz = 0; + for (a, b) in cpu.depth.iter().zip(>.depth) { + if a.is_finite() != b.is_finite() || (a.is_finite() && (a - b).abs() > 0.01 * a.abs().max(1.0)) { + dz += 1; + } + } + assert!((dz as f64) < 0.015 * n as f64, "{dz} depth mismatches"); +} + +#[test] +fn gpu_matches_cpu_lit_scene() { + let Some(g) = gpu() else { return }; + let (p, cid) = scene_project(); + let s = scene(&p, cid); + assert!(!s.shadows.is_empty() && s.env.is_some() && s.opaque_count < s.indices.len() as u32); + compare(&s, &g); +} + +#[test] +fn gpu_matches_cpu_unlit_and_full_frames() { + let Some(g) = gpu() else { return }; + let (mut p, cid) = scene_project(); + // Unlit: remove the lights. + p.comp_mut(cid).unwrap().layers.retain(|l| !l.is_light()); + compare(&scene(&p, cid), &g); + // Whole frames through the renderer with the GPU attached. + let (p, cid) = scene_project(); + let cpu = Renderer::new(&p, &NoFootage, RenderOpts::default()).comp_frame(cid, Tick::ZERO); + let mut r = Renderer::new(&p, &NoFootage, RenderOpts { backend: effectcraft_render::Backend::Gpu, ..Default::default() }); + r.accel = Some(&g); + let gimg = r.comp_frame(cid, Tick::ZERO); + let diff = cpu.data.iter().zip(&gimg.data).filter(|(a, b)| (0..4).any(|k| (a[k] - b[k]).abs() > 3.0 / 255.0)).count(); + assert!((diff as f64) < 0.02 * cpu.data.len() as f64, "{diff} pixels differ"); +} + +#[test] +fn gpu_raster_perf_smoke() { + let Some(g) = gpu() else { return }; + let (p, cid) = scene_project(); + let s = scene(&p, cid); + let t0 = std::time::Instant::now(); + let _ = adv::raster::render(&s); + let cpu = t0.elapsed(); + // First use compiles the pipelines. + let _ = g.raster_3d(&s); + let t0 = std::time::Instant::now(); + let _ = g.raster_3d(&s); + let gpu = t0.elapsed(); + eprintln!("adv3d 480x320 (2x SSAA of 240x160), {} triangles: cpu {:?}, gpu {:?}", s.indices.len() / 3, cpu, gpu); +} diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index 16199dfb..d8906a1a 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -74,6 +74,39 @@ mod tests { assert!(img.data.iter().any(|p| p[3] > 0.5)); } + /// File ▸ Import of glTF/GLB/OBJ models through the media layer, placed as model layers and + /// drawn by the Advanced 3D renderer. + #[test] + fn imported_models_render_in_advanced_3d() { + let fx = |n: &str| format!("{}/../model/tests/fixtures/{n}", env!("CARGO_MANIFEST_DIR")); + let mut s = super::session(); + s.execute("comp.new", json!({"name": "M", "width": 160, "height": 120, "renderer": "advanced3d"})).unwrap(); + let r = s.execute("file.import", json!({"paths": [fx("cube.obj"), fx("quad.glb"), fx("quad.gltf")]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let items: Vec = r["items"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect(); + assert_eq!(items.len(), 3); + let cube = s.execute("layer.addItem", json!({"item": items[0]})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.set", json!({"layer": cube, "path": "transform/rotationY", "value": 30})).unwrap(); + s.execute("prop.set", json!({"layer": cube, "path": "transform/rotationX", "value": 20})).unwrap(); + let cid = s.active_comp_id().unwrap(); + let opts = effectcraft_engine::render::RenderOpts::default(); + let img = s.render(cid, s.time(), opts); + let covered = img.data.iter().filter(|p| p[3] > 0.99).count(); + assert!(covered > 1000, "the cube covers part of the frame: {covered}"); + // Red faces (material Red) are visible. + assert!(img.data.iter().any(|p| p[3] > 0.99 && p[0] > 0.5 && p[1] < 0.2)); + // The textured binary glTF quad. + for _ in 0..3 { + s.execute("edit.undo", json!({})).unwrap(); + } + s.execute("layer.addItem", json!({"item": items[1]})).unwrap(); + let img = s.render(cid, s.time(), opts); + assert!(img.data.iter().filter(|p| p[3] > 0.99).count() > 1000); + // Classic 3D doesn't draw models. + s.execute("comp.renderer", json!({"renderer": "classic3d"})).unwrap(); + assert!(s.render(cid, s.time(), opts).data.iter().all(|p| p[3] == 0.0)); + } + /// The web app's path: outputs go to a sink (downloads), never to the file system. #[test] fn render_queue_exports_to_a_sink() { diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 4ec70cfa..d557357b 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -178,6 +178,12 @@ pub trait Accelerator: Send + Sync { /// Run a chain of supported effects on `buf`, clamping and quantising to `levels` after /// every effect when set (8/16 bpc). `None` = not handled. fn effects(&self, chain: &[FxStep], buf: &Buf, levels: Option) -> Option; + /// Rasterise and shade an Advanced 3D scene at its raster size (depth buffer, PBR, image + /// based light, shadow maps), with the CPU rasteriser's semantics + /// ([`three_d::adv::raster::render`]). `None` = not handled (the CPU renders it). + fn raster_3d(&self, _scene: &three_d::adv::Scene) -> Option { + None + } } #[derive(Clone, Copy, Debug)] @@ -1185,7 +1191,7 @@ impl<'a> Renderer<'a> { comp.layers .iter() .rev() - .filter(|l| l.is_active_at(t) && l.has_video() && (!any_solo || l.switches.solo) && (self.opts.guides || !l.switches.guide)) + .filter(|l| l.is_active_at(t) && l.has_video() && !l.environment && (!any_solo || l.switches.solo) && (self.opts.guides || !l.switches.guide)) .collect() } diff --git a/crates/render/src/shapes.rs b/crates/render/src/shapes.rs index 25eb6090..7cb59135 100644 --- a/crates/render/src/shapes.rs +++ b/crates/render/src/shapes.rs @@ -508,6 +508,27 @@ pub fn render(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64) -> Buf #[cfg(test)] mod tests; +/// The filled outlines of a shape layer in layer space with their straight colours (gradient +/// fills: the colour half way along), for Advanced 3D extrusion. Strokes are not extruded. +pub fn fill_outlines(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup) -> Vec<(Vec, [f32; 4])> { + let mut arena = Vec::new(); + let (draws, _) = collect(ctx, layer, contents, &mut arena); + draws + .iter() + .rev() + .filter(|d| d.opacity > 0.0) + .filter_map(|d| { + let c = match &d.paint { + Paint::Fill { color, .. } => *color, + Paint::Gradient { g, stroke: None, .. } => g.sample(0.5), + _ => return None, + }; + let paths: Vec = d.slots.iter().map(|&i| arena[i].clone()).filter(|p| !p.elements().is_empty()).collect(); + (!paths.is_empty()).then_some((paths, [c[0], c[1], c[2], c[3] * d.opacity])) + }) + .collect() +} + /// Layer-space bounds of a shape layer's painted contents (strokes included), without /// rasterising. pub fn content_bounds(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup) -> Option { diff --git a/crates/render/src/three_d/adv/mod.rs b/crates/render/src/three_d/adv/mod.rs new file mode 100644 index 00000000..04f23885 --- /dev/null +++ b/crates/render/src/three_d/adv/mod.rs @@ -0,0 +1,165 @@ +//! The Advanced 3D renderer (Composition Settings ▸ 3D Renderer ▸ Advanced 3D). +//! +//! A run of 3D layers becomes one [`Scene`] of world-space triangles (models, primitives, +//! extruded text and shapes, and textured cards for every other layer), lit physically +//! ([`shade`]) by the comp's lights, an Environment light's image and shadow maps. The scene +//! is rasterised with a depth buffer on the GPU when an [`crate::Accelerator`] implements +//! [`crate::Accelerator::raster_3d`], otherwise by the software rasteriser ([`raster`]), at 2×2 +//! supersampling. The resolve (box filter), depth of field (a gather blur by each pixel's +//! circle of confusion) and the conversion back to the working encoding run on the CPU for +//! both paths, and the result is composited over the layers below the run. + +pub mod raster; +pub mod scene; +pub mod shade; + +use effectcraft_project::Layer; +use rayon::prelude::*; + +pub use raster::Target; +pub use scene::{EnvInfo, Light, Material, Scene, ShadowMap, TexInfo, Vertex}; + +use super::camera::Dof; +use super::compose::camera_for; +use crate::{EvalCtx, Image, Renderer}; + +/// Box-filter a supersampled target down by `k`: (premultiplied colour, mean depth of the +/// covered subsamples, ∞ where none). +pub fn resolve(t: &Target, k: u32) -> (Image, Vec) { + let (w, h) = (t.width / k, t.height / k); + let mut img = Image::new(w, h); + let mut depth = vec![f32::INFINITY; (w * h) as usize]; + let n = (k * k) as f32; + img.data.par_chunks_mut(w as usize).zip(depth.par_chunks_mut(w as usize)).enumerate().for_each(|(y, (row, drow))| { + for x in 0..w as usize { + let mut acc = [0.0f32; 4]; + let (mut dz, mut dn) = (0.0f32, 0.0f32); + for j in 0..k as usize { + for i in 0..k as usize { + let s = (y * k as usize + j) * t.width as usize + x * k as usize + i; + let c = t.color[s]; + for q in 0..4 { + acc[q] += c[q]; + } + if t.depth[s].is_finite() { + dz += t.depth[s]; + dn += 1.0; + } + } + } + row[x] = acc.map(|v| v / n); + if dn > 0.0 { + drow[x] = dz / dn; + } + } + }); + (img, depth) +} + +/// Depth of field: a gather blur where each pixel collects the neighbours whose circle of +/// confusion (from their depth, in output pixels) reaches it; nearer in-focus surfaces are not +/// smeared by the blur of what is behind them. +pub fn depth_of_field(img: &Image, depth: &[f32], dof: &Dof, scale: f64) -> Image { + let (w, h) = (img.width as i64, img.height as i64); + let max_r = 48.0f32; + let radius: Vec = depth + .iter() + .map(|&z| { + let z = if z.is_finite() { z as f64 } else { 1.0e9 }; + ((dof.coc(z) * scale * 0.5) as f32).min(max_r) + }) + .collect(); + let rmax = radius.iter().fold(0.0f32, |a, &b| a.max(b)); + if rmax < 0.5 { + return img.clone(); + } + // Disk samples (golden-angle spiral) over the largest radius. + const N: usize = 64; + let golden = std::f32::consts::PI * (3.0 - 5.0f32.sqrt()); + let taps: Vec<(f32, f32, f32)> = (1..=N) + .map(|i| { + let r = (i as f32 / N as f32).sqrt() * rmax; + let a = i as f32 * golden; + (r * a.cos(), r * a.sin(), r) + }) + .collect(); + let mut out = Image::new(img.width, img.height); + out.data.par_chunks_mut(w as usize).enumerate().for_each(|(y, row)| { + for x in 0..w as usize { + let i = y * w as usize + x; + let (rp, zp) = (radius[i], depth[i]); + let cw = 1.0 / (std::f32::consts::PI * rp * rp).max(1.0); + let c = img.data[i]; + let mut acc = [c[0] * cw, c[1] * cw, c[2] * cw, c[3] * cw]; + let mut wsum = cw; + for &(dx, dy, d) in &taps { + let (sx, sy) = (x as i64 + dx.round() as i64, y as i64 + dy.round() as i64); + if sx < 0 || sy < 0 || sx >= w || sy >= h { + continue; + } + let j = (sy * w + sx) as usize; + // A sample behind this pixel blurs no further than this pixel's own circle. + let rq = if depth[j] > zp { radius[j].min(rp) } else { radius[j] }; + let cov = (rq - d + 0.5).clamp(0.0, 1.0); + if cov <= 0.0 { + continue; + } + // Each tap stands for an annulus of area ≈ π·rmax²/N. + let wq = cov / (std::f32::consts::PI * rq * rq).max(1.0) * (std::f32::consts::PI * rmax * rmax / N as f32); + let q = img.data[j]; + for k in 0..4 { + acc[k] += q[k] * wq; + } + wsum += wq; + } + row[x] = acc.map(|v| v / wsum); + } + }); + out +} + +/// Linear → sRGB-encoded premultiplied pixels (the working encoding of non-linear projects). +pub fn encode(img: &mut Image) { + img.data.par_iter_mut().for_each(|p| { + let a = p[3]; + if a > 1e-6 { + for c in 0..3 { + p[c] = shade::linear_to_srgb(p[c] / a) * a; + } + } + }); +} + +/// Render a scene on the CPU, resolved: (premultiplied image at output size, depth). +pub fn render_cpu(s: &Scene) -> (Image, Vec) { + resolve(&raster::render(s), s.ssaa.max(1)) +} + +/// Build the scene of a run (for tests and tools). +pub fn scene_of(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], out: (u32, u32)) -> Scene { + scene::build(r, ctx, run, out) +} + +/// Draw a run of 3D layers with the Advanced 3D renderer into `canvas`. +pub(crate) fn draw_run(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], canvas: &mut Image) { + let s = scene::build(r, ctx, run, (canvas.width, canvas.height)); + if s.indices.is_empty() { + return; + } + let target = r.active_accel().and_then(|a| a.raster_3d(&s)).filter(|t| t.width == s.width && t.height == s.height).unwrap_or_else(|| raster::render(&s)); + let (mut img, depth) = resolve(&target, s.ssaa.max(1)); + let cam = camera_for(r, ctx); + if let Some(dof) = cam.dof.filter(|_| !ctx.comp.draft_3d && !r.opts.draft) { + img = depth_of_field(&img, &depth, &dof, r.opts.scale); + } + if !s.linear_io { + encode(&mut img); + } + canvas.data.par_iter_mut().zip(img.data.par_iter()).for_each(|(d, s)| { + let k = 1.0 - s[3]; + *d = [s[0] + d[0] * k, s[1] + d[1] * k, s[2] + d[2] * k, s[3] + d[3] * k]; + }); +} + +#[cfg(test)] +mod tests; diff --git a/crates/render/src/three_d/adv/raster.rs b/crates/render/src/three_d/adv/raster.rs new file mode 100644 index 00000000..36cec108 --- /dev/null +++ b/crates/render/src/three_d/adv/raster.rs @@ -0,0 +1,492 @@ +//! The software rasteriser of the Advanced 3D renderer (the CPU reference and the fallback +//! when no GPU is available) and the shadow-map rasteriser shared by both paths. +//! +//! Triangles are clipped against the depth range (`0 ≤ z ≤ w`, as GPUs do), set up in screen +//! space and scanned per band of rows in parallel with edge functions (pixel centres, top-left +//! fill rule) and perspective-correct barycentrics. Opaque triangles resolve visibility first +//! (reversed depth: larger ndc z is nearer) and each pixel is shaded once; transparent triangles +//! (already sorted back to front) are then shaded per fragment and blended over, depth-tested +//! against the opaque surfaces. + +use rayon::prelude::*; + +use super::scene::{Scene, ShadowMap, Vertex}; +use super::shade::{Frag, sample, shade}; + +/// A rendered target at raster size. +pub struct Target { + pub width: u32, + pub height: u32, + /// Premultiplied linear RGBA. + pub color: Vec<[f32; 4]>, + /// Camera-space depth of the nearest opaque surface (∞ where empty). + pub depth: Vec, +} + +/// A clipped triangle in screen space; `w` holds, per corner, its weights over the original +/// triangle's three vertices. +#[derive(Clone, Copy)] +struct STri { + p: [[f64; 2]; 3], + z: [f32; 3], + inv_w: [f32; 3], + w: [[f32; 3]; 3], + tri: u32, + front: bool, + bbox: [i32; 4], +} + +#[inline] +fn mat_mul(m: &[[f32; 4]; 4], p: [f32; 3]) -> [f32; 4] { + let mut o = [0.0; 4]; + for r in 0..4 { + o[r] = m[r][0] * p[0] + m[r][1] * p[1] + m[r][2] * p[2] + m[r][3]; + } + o +} + +/// Clip a polygon (clip coords + original weights) against `0 ≤ z ≤ w`. +fn clip_poly(poly: Vec<([f32; 4], [f32; 3])>) -> Vec<([f32; 4], [f32; 3])> { + let mut p = poly; + for plane in 0..2 { + if p.is_empty() { + break; + } + let d = |c: &[f32; 4]| if plane == 0 { c[2] } else { c[3] - c[2] }; + let mut out = Vec::with_capacity(p.len() + 2); + for i in 0..p.len() { + let (a, b) = (p[i], p[(i + 1) % p.len()]); + let (da, db) = (d(&a.0), d(&b.0)); + if da >= 0.0 { + out.push(a); + } + if (da >= 0.0) != (db >= 0.0) { + let t = da / (da - db); + let mut c = [0.0; 4]; + let mut w = [0.0; 3]; + for k in 0..4 { + c[k] = a.0[k] + (b.0[k] - a.0[k]) * t; + } + for k in 0..3 { + w[k] = a.1[k] + (b.1[k] - a.1[k]) * t; + } + out.push((c, w)); + } + } + p = out; + } + p +} + +/// Set up the screen triangles of index range `range`. +fn setup(s: &Scene, clip: &[[f32; 4]], range: std::ops::Range) -> Vec { + let (w, h) = (s.width as f64, s.height as f64); + let mut out = vec![]; + for t in range.step_by(3) { + let ids = [s.indices[t] as usize, s.indices[t + 1] as usize, s.indices[t + 2] as usize]; + let mat = s.vertices[ids[0]].material as usize; + if !s.materials.get(mat).is_some_and(|m| m.visible) { + continue; + } + let poly = vec![(clip[ids[0]], [1.0, 0.0, 0.0]), (clip[ids[1]], [0.0, 1.0, 0.0]), (clip[ids[2]], [0.0, 0.0, 1.0])]; + let poly = clip_poly(poly); + if poly.len() < 3 { + continue; + } + let scr: Vec<([f64; 2], f32, f32, [f32; 3])> = poly + .iter() + .map(|(c, wt)| { + let iw = 1.0 / c[3]; + let (nx, ny) = (c[0] * iw, c[1] * iw); + ([(nx as f64 + 1.0) * 0.5 * w, (1.0 - ny as f64) * 0.5 * h], c[2] * iw, iw, *wt) + }) + .collect(); + // Facing from the polygon's screen-space winding (y down: negative = front). + let mut area = 0.0; + for i in 0..scr.len() { + let (a, b) = (scr[i].0, scr[(i + 1) % scr.len()].0); + area += a[0] * b[1] - b[0] * a[1]; + } + if area == 0.0 || !area.is_finite() { + continue; + } + let front = area < 0.0; + for k in 1..scr.len() - 1 { + let c = [scr[0], scr[k], scr[k + 1]]; + let mut x0 = f64::INFINITY; + let mut y0 = f64::INFINITY; + let mut x1 = f64::NEG_INFINITY; + let mut y1 = f64::NEG_INFINITY; + for v in &c { + x0 = x0.min(v.0[0]); + y0 = y0.min(v.0[1]); + x1 = x1.max(v.0[0]); + y1 = y1.max(v.0[1]); + } + let bbox = [ + (x0.floor() as i64).max(0) as i32, + (y0.floor() as i64).max(0) as i32, + (x1.ceil() as i64).min(s.width as i64) as i32, + (y1.ceil() as i64).min(s.height as i64) as i32, + ]; + if bbox[0] >= bbox[2] || bbox[1] >= bbox[3] { + continue; + } + out.push(STri { + p: [c[0].0, c[1].0, c[2].0], + z: [c[0].1, c[1].1, c[2].1], + inv_w: [c[0].2, c[1].2, c[2].2], + w: [c[0].3, c[1].3, c[2].3], + tri: (t / 3) as u32, + front, + bbox, + }); + } + } + out +} + +/// Coverage of a pixel centre: (ndc z, perspective-correct weights over the screen corners). +#[inline] +fn cover(t: &STri, px: f64, py: f64) -> Option<(f32, [f32; 3])> { + let [a, b, c] = t.p; + let area = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); + // Orient so the area is positive. + let (b, c, swap) = if area < 0.0 { (c, b, true) } else { (b, c, false) }; + let area = area.abs(); + let edge = |p: [f64; 2], q: [f64; 2]| -> f64 { (q[0] - p[0]) * (py - p[1]) - (q[1] - p[1]) * (px - p[0]) }; + let top_left = |p: [f64; 2], q: [f64; 2]| -> bool { + let (dx, dy) = (q[0] - p[0], q[1] - p[1]); + dy < 0.0 || (dy == 0.0 && dx > 0.0) + }; + let e0 = edge(b, c); // opposite a + let e1 = edge(c, a); // opposite b + let e2 = edge(a, b); // opposite c + let inside = |e: f64, p: [f64; 2], q: [f64; 2]| e > 0.0 || (e == 0.0 && top_left(p, q)); + if !(inside(e0, b, c) && inside(e1, c, a) && inside(e2, a, b)) { + return None; + } + let (l0, l1, l2) = ((e0 / area) as f32, (e1 / area) as f32, (e2 / area) as f32); + // Back to the stored corner order. + let l = if swap { [l0, l2, l1] } else { [l0, l1, l2] }; + let z = l[0] * t.z[0] + l[1] * t.z[1] + l[2] * t.z[2]; + let q = [l[0] * t.inv_w[0], l[1] * t.inv_w[1], l[2] * t.inv_w[2]]; + let sum = q[0] + q[1] + q[2]; + if sum <= 0.0 { + return None; + } + Some((z, [q[0] / sum, q[1] / sum, q[2] / sum])) +} + +/// Interpolate the original triangle's vertex attributes at screen-corner weights `l`. +fn frag(s: &Scene, t: &STri, l: [f32; 3]) -> Frag { + let base = t.tri as usize * 3; + let v: [&Vertex; 3] = [0, 1, 2].map(|k| &s.vertices[s.indices[base + k] as usize]); + // Weights over the original vertices. + let mut wv = [0.0f32; 3]; + for c in 0..3 { + for k in 0..3 { + wv[k] += l[c] * t.w[c][k]; + } + } + let mut f = Frag { pos: [0.0; 3], normal: [0.0; 3], uv: [0.0; 2], tangent: [0.0; 4], material: v[0].material, front: t.front }; + for k in 0..3 { + let x = wv[k]; + for i in 0..3 { + f.pos[i] += v[k].pos[i] * x; + f.normal[i] += v[k].normal[i] * x; + } + for i in 0..2 { + f.uv[i] += v[k].uv[i] * x; + } + for i in 0..4 { + f.tangent[i] += v[k].tangent[i] * x; + } + } + f.tangent[3] = v[0].tangent[3]; + f +} + +const BAND: i32 = 16; + +/// Rasterise and shade the scene. +pub fn render(s: &Scene) -> Target { + let (w, h) = (s.width as usize, s.height as usize); + let clip: Vec<[f32; 4]> = s.vertices.iter().map(|v| mat_mul(&s.clip, v.pos)).collect(); + let opaque = setup(s, &clip, 0..s.opaque_count as usize); + let trans = setup(s, &clip, s.opaque_count as usize..s.indices.len()); + let bands = (h as i32 + BAND - 1) / BAND; + let bin = |ts: &[STri]| -> Vec> { + let mut b = vec![vec![]; bands.max(0) as usize]; + for (i, t) in ts.iter().enumerate() { + for band in (t.bbox[1] / BAND)..((t.bbox[3] + BAND - 1) / BAND).min(bands) { + b[band as usize].push(i as u32); + } + } + b + }; + let ob = bin(&opaque); + let tb = bin(&trans); + let view = s.view; + let cam_z = |p: [f32; 3]| view[2][0] * p[0] + view[2][1] * p[1] + view[2][2] * p[2] + view[2][3]; + let mut color = vec![[0.0f32; 4]; w * h]; + let mut depth = vec![f32::INFINITY; w * h]; + color.par_chunks_mut(w * BAND as usize).zip(depth.par_chunks_mut(w * BAND as usize)).enumerate().for_each(|(band, (crow, drow))| { + let y0 = band as i32 * BAND; + let rows = crow.len() / w; + // Visibility: (screen tri, ndc z, weights). + let mut vis: Vec<(u32, f32, [f32; 3])> = vec![(u32::MAX, 0.0, [0.0; 3]); rows * w]; + for &ti in &ob[band] { + let t = &opaque[ti as usize]; + for y in t.bbox[1].max(y0)..t.bbox[3].min(y0 + rows as i32) { + let r = (y - y0) as usize * w; + for x in t.bbox[0]..t.bbox[2] { + if let Some((z, l)) = cover(t, x as f64 + 0.5, y as f64 + 0.5) { + let v = &mut vis[r + x as usize]; + if z > v.1 && z <= 1.0 { + *v = (ti, z, l); + } + } + } + } + } + // A discarded opaque fragment (alpha test, back face) lets the next nearer-than-nothing + // surface through only on a second look; keep it simple: resolve again without it. + for i in 0..rows * w { + let (ti, _, l) = vis[i]; + if ti == u32::MAX { + continue; + } + let t = &opaque[ti as usize]; + let f = frag(s, t, l); + match shade(s, &f) { + Some(c) => { + crow[i] = c; + drow[i] = cam_z(f.pos); + } + None => { + // Find the nearest surviving fragment under this pixel. + let (x, y) = ((i % w) as f64 + 0.5, (y0 as usize + i / w) as f64 + 0.5); + let mut best: Option<(f32, [f32; 4], f32)> = None; + for &tj in &ob[band] { + if tj == ti { + continue; + } + let t2 = &opaque[tj as usize]; + if (x as i32) < t2.bbox[0] || (x as i32) >= t2.bbox[2] || (y as i32) < t2.bbox[1] || (y as i32) >= t2.bbox[3] { + continue; + } + if let Some((z, l2)) = cover(t2, x, y) + && best.is_none_or(|b| z > b.0) + { + let f2 = frag(s, t2, l2); + if let Some(c) = shade(s, &f2) { + best = Some((z, c, cam_z(f2.pos))); + } + } + } + if let Some((z, c, d)) = best { + crow[i] = c; + drow[i] = d; + vis[i].1 = z; + } else { + vis[i].1 = 0.0; + } + } + } + } + // Transparent fragments in order, over the opaque result. + for &ti in &tb[band] { + let t = &trans[ti as usize]; + for y in t.bbox[1].max(y0)..t.bbox[3].min(y0 + rows as i32) { + let r = (y - y0) as usize * w; + for x in t.bbox[0]..t.bbox[2] { + let i = r + x as usize; + if let Some((z, l)) = cover(t, x as f64 + 0.5, y as f64 + 0.5) + && z > vis[i].1 + && z <= 1.0 + && let Some(c) = shade(s, &frag(s, t, l)) + { + let d = crow[i]; + let k = 1.0 - c[3]; + crow[i] = [c[0] + d[0] * k, c[1] + d[1] * k, c[2] + d[2] * k, c[3] + d[3] * k]; + } + } + } + } + }); + Target { width: s.width, height: s.height, color, depth } +} + +// ---------------------------------------------------------------- shadow maps + +fn basis(eye: [f64; 3], fwd: [f64; 3], down: [f64; 3]) -> [[f32; 4]; 3] { + use effectcraft_geom::vec3; + let m = crate::three_d::camera::basis_view(vec3(eye[0], eye[1], eye[2]), vec3(fwd[0], fwd[1], fwd[2]), vec3(down[0], down[1], down[2])); + [0, 1, 2].map(|r| m.0[r].map(|v| v as f32)) +} + +/// Rasterise the shadow-casting triangles' light-space depth into `m`'s texels. +fn shadow_raster(s: &Scene, m: &ShadowMap, casters: &[usize]) -> Vec { + let n = m.size as usize; + let mut d = vec![f32::INFINITY; n * n]; + let c = n as f32 * 0.5; + const NEAR: f32 = 1.0; + for &t in casters { + let ids = [s.indices[3 * t] as usize, s.indices[3 * t + 1] as usize, s.indices[3 * t + 2] as usize]; + let mat = &s.materials[s.vertices[ids[0]].material as usize]; + let alpha_tex = (mat.alpha_mode != 0 && mat.tex_base >= 0).then(|| &s.textures[mat.tex_base as usize]); + // Light space, clipped against the near plane (perspective). + let q: Vec<([f32; 3], [f32; 2])> = ids + .iter() + .map(|&i| { + let p = s.vertices[i].pos; + let v = &m.view; + ([0, 1, 2].map(|r| v[r][0] * p[0] + v[r][1] * p[1] + v[r][2] * p[2] + v[r][3]), s.vertices[i].uv) + }) + .collect(); + let mut poly = q; + if !m.ortho { + let mut out = vec![]; + for i in 0..poly.len() { + let (a, b) = (poly[i], poly[(i + 1) % poly.len()]); + let (da, db) = (a.0[2] - NEAR, b.0[2] - NEAR); + if da >= 0.0 { + out.push(a); + } + if (da >= 0.0) != (db >= 0.0) { + let k = da / (da - db); + let lerp = |x: f32, y: f32| x + (y - x) * k; + out.push(([lerp(a.0[0], b.0[0]), lerp(a.0[1], b.0[1]), lerp(a.0[2], b.0[2])], [lerp(a.1[0], b.1[0]), lerp(a.1[1], b.1[1])])); + } + } + poly = out; + } + if poly.len() < 3 { + continue; + } + // Screen: (x, y, 1/z or 1, depth) per corner. + let scr: Vec<([f64; 2], f32, f32, [f32; 2])> = poly + .iter() + .map(|(p, uv)| { + if m.ortho { + ([(p[0] * m.focal + c) as f64, (p[1] * m.focal + c) as f64], 1.0, p[2], *uv) + } else { + let iz = 1.0 / p[2]; + ([(p[0] * m.focal * iz + c) as f64, (p[1] * m.focal * iz + c) as f64], iz, p[2], *uv) + } + }) + .collect(); + for k in 1..scr.len() - 1 { + let tri = [scr[0], scr[k], scr[k + 1]]; + let (a, b, cc) = (tri[0].0, tri[1].0, tri[2].0); + let area = (b[0] - a[0]) * (cc[1] - a[1]) - (b[1] - a[1]) * (cc[0] - a[0]); + if area.abs() < 1e-12 { + continue; + } + let x0 = a[0].min(b[0]).min(cc[0]).floor().max(0.0) as usize; + let x1 = (a[0].max(b[0]).max(cc[0]).ceil().max(0.0) as usize).min(n); + let y0 = a[1].min(b[1]).min(cc[1]).floor().max(0.0) as usize; + let y1 = (a[1].max(b[1]).max(cc[1]).ceil().max(0.0) as usize).min(n); + for y in y0..y1 { + for x in x0..x1 { + let (px, py) = (x as f64 + 0.5, y as f64 + 0.5); + let e0 = ((cc[0] - b[0]) * (py - b[1]) - (cc[1] - b[1]) * (px - b[0])) / area; + let e1 = ((a[0] - cc[0]) * (py - cc[1]) - (a[1] - cc[1]) * (px - cc[0])) / area; + let e2 = 1.0 - e0 - e1; + if e0 < 0.0 || e1 < 0.0 || e2 < 0.0 { + continue; + } + let l = [e0 as f32, e1 as f32, e2 as f32]; + let (z, uv) = if m.ortho { + (l[0] * tri[0].2 + l[1] * tri[1].2 + l[2] * tri[2].2, [0, 1].map(|i| l[0] * tri[0].3[i] + l[1] * tri[1].3[i] + l[2] * tri[2].3[i])) + } else { + let iz = l[0] * tri[0].1 + l[1] * tri[1].1 + l[2] * tri[2].1; + let z = 1.0 / iz; + (z, [0, 1].map(|i| (l[0] * tri[0].3[i] * tri[0].1 + l[1] * tri[1].3[i] * tri[1].1 + l[2] * tri[2].3[i] * tri[2].1) * z)) + }; + let px = &mut d[y * n + x]; + if z >= *px { + continue; + } + if let Some(t) = alpha_tex + && sample(t, &s.texels, uv[0], uv[1])[3] * mat.base[3] * mat.opacity < 0.5 + { + continue; + } + *px = z; + } + } + } + } + d +} + +/// Build the shadow maps of every shadow-casting light (lights marked `shadow == 0`). +pub(crate) fn shadow_maps(s: &mut Scene, draft: bool) { + let casters: Vec = (0..s.indices.len() / 3) + .filter(|&t| s.materials.get(s.vertices[s.indices[3 * t] as usize].material as usize).is_some_and(|m| m.casts_shadows)) + .collect(); + if casters.is_empty() { + for l in &mut s.lights { + l.shadow = -1; + } + return; + } + // Bounding sphere of the scene (parallel lights' orthographic frustum). + let (mut lo, mut hi) = ([f64::INFINITY; 3], [f64::NEG_INFINITY; 3]); + for v in &s.vertices { + for k in 0..3 { + lo[k] = lo[k].min(v.pos[k] as f64); + hi[k] = hi[k].max(v.pos[k] as f64); + } + } + let centre = [0, 1, 2].map(|k| (lo[k] + hi[k]) * 0.5); + let radius = ((hi[0] - lo[0]).powi(2) + (hi[1] - lo[1]).powi(2) + (hi[2] - lo[2]).powi(2)).sqrt() * 0.5 + 1.0; + let size = if draft { 512 } else { 1024 }; + let mut maps: Vec = vec![]; + for li in 0..s.lights.len() { + if s.lights[li].shadow != 0 { + s.lights[li].shadow = -1; + continue; + } + let l = s.lights[li]; + let first = maps.len() as i32; + let soft = 1.0 + (l.shadow_diffusion * 0.5).min(24.0); + let pos = l.pos.map(|v| v as f64); + let dir = l.dir.map(|v| v as f64); + let hint = if dir[1].abs() < 0.9 { [0.0, 1.0, 0.0] } else { [0.0, 0.0, 1.0] }; + match l.kind { + 0 => { + let eye = [centre[0] - dir[0] * radius * 2.0, centre[1] - dir[1] * radius * 2.0, centre[2] - dir[2] * radius * 2.0]; + let focal = (size as f64 * 0.5 / radius) as f32; + maps.push(ShadowMap { view: basis(eye, dir, hint), focal, ortho: true, size, offset: 0, bias: 1.5 / focal, radius: soft }); + } + 1 => { + let half = (l.cos_outer.clamp(-1.0, 1.0).acos() as f64 + 0.05).min(1.45); + let focal = (size as f64 * 0.5 / half.tan()) as f32; + maps.push(ShadowMap { view: basis(pos, dir, hint), focal, ortho: false, size, offset: 0, bias: 1.0, radius: soft }); + } + _ => { + let fs = size / 2; + for (f, d) in [ + ([1.0, 0.0, 0.0], [0.0, 1.0, 0.0]), + ([-1.0, 0.0, 0.0], [0.0, 1.0, 0.0]), + ([0.0, 1.0, 0.0], [0.0, 0.0, 1.0]), + ([0.0, -1.0, 0.0], [0.0, 0.0, 1.0]), + ([0.0, 0.0, 1.0], [0.0, 1.0, 0.0]), + ([0.0, 0.0, -1.0], [0.0, 1.0, 0.0]), + ] { + maps.push(ShadowMap { view: basis(pos, f, d), focal: fs as f32 * 0.5, ortho: false, size: fs, offset: 0, bias: 1.0, radius: soft }); + } + } + } + s.lights[li].shadow = first; + } + let texels: Vec> = maps.par_iter().map(|m| shadow_raster(s, m, &casters)).collect(); + for (m, t) in maps.iter_mut().zip(texels) { + m.offset = s.shadow_texels.len() as u32; + s.shadow_texels.extend(t); + } + s.shadows = maps; +} diff --git a/crates/render/src/three_d/adv/scene.rs b/crates/render/src/three_d/adv/scene.rs new file mode 100644 index 00000000..1d3acc3c --- /dev/null +++ b/crates/render/src/three_d/adv/scene.rs @@ -0,0 +1,872 @@ +//! The Advanced 3D scene: everything a rasteriser needs, flattened into world-space triangles +//! and plain arrays (identical input for the CPU rasteriser and the GPU pipeline). +//! +//! - Model and primitive layers: their meshes (glTF/OBJ models are +Y up in model units and +//! are mapped into the layer by `Model Scale` with Y and Z flipped, a 180° turn about X). +//! - Extruded text and shape layers (Geometry Options ▸ Extrusion Depth > 0): bevelled meshes. +//! - Every other 3D layer (footage, solids, comps, flat text and shapes): a textured card of +//! its processed buffer, lit with its Material Options. +//! - Lights from the comp's light layers, the Environment light's image (prefiltered for image +//! based lighting), and shadow maps of the shadow-casting lights. +//! +//! Colours are linear light; textures and layer buffers are linearised from the sRGB curve +//! unless the project already blends in linear light. + +use std::collections::HashMap; +use std::sync::Arc; + +use effectcraft_effects::Buf; +use effectcraft_geom::{Mat4, Vec3, vec3}; +use effectcraft_model::{self as model, Primitive}; +use effectcraft_project::{ItemKind, Layer, LayerSource, LightKind, PrimitiveKind}; + +use super::shade::srgb_to_linear; +use crate::three_d::camera::{CameraState, NEAR}; +use crate::three_d::compose::camera_for; +use crate::three_d::light::{LightState, Material as ClassicMaterial}; +use crate::{EvalCtx, Renderer}; + +/// One vertex in world space. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +#[repr(C)] +pub struct Vertex { + pub pos: [f32; 3], + pub normal: [f32; 3], + pub uv: [f32; 2], + pub tangent: [f32; 4], + pub material: u32, +} + +/// A material, resolved for shading. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Material { + /// Linear RGBA factor. + pub base: [f32; 4], + pub metallic: f32, + pub roughness: f32, + pub emissive: [f32; 3], + pub normal_scale: f32, + pub occlusion_strength: f32, + /// 0 opaque, 1 mask, 2 blend. + pub alpha_mode: u32, + pub alpha_cutoff: f32, + /// Layer opacity (multiplies alpha). + pub opacity: f32, + /// Texture indices (−1 = none). + pub tex_base: i32, + pub tex_mr: i32, + pub tex_normal: i32, + pub tex_occlusion: i32, + pub tex_emissive: i32, + pub double_sided: bool, + pub unlit: bool, + pub accepts_lights: bool, + pub receives_shadows: bool, + pub casts_shadows: bool, + /// Drawn (false for "Casts Shadows: Only"). + pub visible: bool, + /// Material Options weights (cards: Diffuse, Specular Intensity / 50%, Ambient). + pub diffuse_k: f32, + pub specular_k: f32, + pub ambient_k: f32, +} + +impl Default for Material { + fn default() -> Self { + Material { + base: [1.0; 4], + metallic: 0.0, + roughness: 0.5, + emissive: [0.0; 3], + normal_scale: 1.0, + occlusion_strength: 1.0, + alpha_mode: 0, + alpha_cutoff: 0.5, + opacity: 1.0, + tex_base: -1, + tex_mr: -1, + tex_normal: -1, + tex_occlusion: -1, + tex_emissive: -1, + double_sided: false, + unlit: false, + accepts_lights: true, + receives_shadows: true, + casts_shadows: false, + visible: true, + diffuse_k: 1.0, + specular_k: 1.0, + ambient_k: 1.0, + } + } +} + +impl Material { + /// Drawn in the transparent (sorted, blended) pass. + pub fn transparent(&self) -> bool { + self.alpha_mode == 2 || self.opacity < 0.999 + } +} + +/// A texture in [`Scene::texels`]: linear straight RGBA. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct TexInfo { + pub offset: u32, + pub width: u32, + pub height: u32, + /// 0 repeat, 1 clamp, 2 mirror. + pub wrap_u: u32, + pub wrap_v: u32, +} + +/// A light (ambient lights are summed into [`Scene::ambient`]). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Light { + /// 0 parallel, 1 spot, 2 point. + pub kind: u32, + pub pos: [f32; 3], + /// Direction the light travels. + pub dir: [f32; 3], + /// Linear colour × intensity. + pub color: [f32; 3], + pub cos_inner: f32, + pub cos_outer: f32, + /// 0 none, 1 smooth, 2 inverse square clamped. + pub falloff: u32, + pub radius: f32, + pub falloff_distance: f32, + /// First shadow map (point lights use six), −1 = none. + pub shadow: i32, + pub shadow_darkness: f32, + /// Shadow Diffusion (pixels). + pub shadow_diffusion: f32, +} + +/// A depth map seen from a light: `view` maps world → light space (+z forward); pixels +/// `u = focal·x/z + size/2` (perspective) or `focal·x + size/2` (orthographic); texels store +/// the light-space depth of the nearest caster. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ShadowMap { + pub view: [[f32; 4]; 3], + pub focal: f32, + pub ortho: bool, + pub size: u32, + pub offset: u32, + /// Depth bias (world units). + pub bias: f32, + /// PCF spread (texels). + pub radius: f32, +} + +/// The environment image: radiance mip levels `radiance..radiance + mips` and an irradiance +/// map, in [`Scene::textures`]. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct EnvInfo { + pub radiance: u32, + pub mips: u32, + pub irradiance: u32, + pub intensity: f32, + /// Radians about the vertical axis. + pub rotation: f32, +} + +#[derive(Clone, Debug, Default)] +pub struct Scene { + /// Raster size (output × `ssaa`). + pub width: u32, + pub height: u32, + pub ssaa: u32, + /// World → clip (row-major; reversed depth: ndc z = near / camera z). + pub clip: [[f32; 4]; 4], + /// World → camera (row-major; camera z = depth for depth of field). + pub view: [[f32; 4]; 4], + pub eye: [f32; 3], + pub cam_fwd: [f32; 3], + pub ortho: bool, + pub vertices: Vec, + /// Triangles: the first `opaque_count` indices are opaque, the rest transparent, sorted + /// back to front. + pub indices: Vec, + pub opaque_count: u32, + pub materials: Vec, + pub textures: Vec, + pub texels: Vec<[f32; 4]>, + pub lights: Vec, + pub shadows: Vec, + pub shadow_texels: Vec, + pub env: Option, + pub ambient: [f32; 3], + /// Shading inputs and outputs are linear (no sRGB conversion needed). + pub linear_io: bool, +} + +/// Row-major f32 copy of a matrix. +pub fn m32(m: &Mat4) -> [[f32; 4]; 4] { + m.0.map(|r| r.map(|v| v as f32)) +} + +/// Builds a [`Scene`]: texture uploads deduplicated by source. +pub(crate) struct Builder { + pub s: Scene, + tex_keys: HashMap<(usize, bool), i32>, + /// Per triangle: transparent and its sort depth. + tri_depth: Vec<(bool, f32)>, + cam: CameraState, +} + +impl Builder { + fn texture_from(&mut self, w: u32, h: u32, wrap: [u32; 2], data: impl Iterator) -> i32 { + let offset = self.s.texels.len() as u32; + self.s.texels.extend(data); + self.s.textures.push(TexInfo { offset, width: w, height: h, wrap_u: wrap[0], wrap_v: wrap[1] }); + (self.s.textures.len() - 1) as i32 + } + + /// Upload a model texture (`srgb`: colour data to linearise). + fn model_texture(&mut self, t: &Arc, srgb: bool) -> i32 { + let key = (Arc::as_ptr(t) as usize, srgb); + if let Some(&i) = self.tex_keys.get(&key) { + return i; + } + let lin = srgb && !self.s.linear_io; + let wrap = t.wrap.map(|w| match w { + model::Wrap::Repeat => 0, + model::Wrap::Clamp => 1, + model::Wrap::Mirror => 2, + }); + let i = self.texture_from( + t.width, + t.height, + wrap, + t.data.iter().map(|p| if lin { [srgb_to_linear(p[0]), srgb_to_linear(p[1]), srgb_to_linear(p[2]), p[3]] } else { *p }), + ); + self.tex_keys.insert(key, i); + i + } + + /// Upload a premultiplied layer buffer as a linear straight-alpha texture (clamped edges). + fn buf_texture(&mut self, b: &Buf) -> (i32, bool) { + let lin = !self.s.linear_io; + let mut opaque = true; + let data: Vec<[f32; 4]> = b + .img + .data + .iter() + .map(|p| { + let a = p[3]; + if a < 0.999 { + opaque = false; + } + if a <= 1e-6 { + return [0.0; 4]; + } + let s = [p[0] / a, p[1] / a, p[2] / a]; + if lin { [srgb_to_linear(s[0]), srgb_to_linear(s[1]), srgb_to_linear(s[2]), a] } else { [s[0], s[1], s[2], a] } + }) + .collect(); + (self.texture_from(b.img.width, b.img.height, [1, 1], data.into_iter()), opaque) + } + + fn material(&mut self, m: Material) -> u32 { + self.s.materials.push(m); + (self.s.materials.len() - 1) as u32 + } + + /// Append a primitive with per-vertex matrices (model/layer space → world). + fn emit(&mut self, p: &Primitive, mat: u32, xf: &dyn Fn(usize) -> Mat4) { + if p.positions.is_empty() || p.indices.len() < 3 { + return; + } + let base = self.s.vertices.len() as u32; + let transparent = self.s.materials[mat as usize].transparent(); + let uniform = xf(usize::MAX); + let normal_of = |m: &Mat4| -> (Mat4, f64) { + let a = &m.0; + let det = a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1]) - a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0]) + + a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]); + let inv = Mat4([[a[0][0], a[0][1], a[0][2], 0.0], [a[1][0], a[1][1], a[1][2], 0.0], [a[2][0], a[2][1], a[2][2], 0.0], [0.0, 0.0, 0.0, 1.0]]) + .inverse() + .unwrap_or(Mat4::IDENTITY); + (inv.transpose(), det) + }; + let (un, udet) = normal_of(&uniform); + let mut flip_count = 0usize; + for k in 0..p.positions.len() { + let (m, n, det) = if p.joints.is_empty() { + (uniform, un, udet) + } else { + let m = xf(k); + let (n, d) = normal_of(&m); + (m, n, d) + }; + if det < 0.0 { + flip_count += 1; + } + let q = p.positions[k]; + let w = m.apply(vec3(q[0] as f64, q[1] as f64, q[2] as f64)); + let nn = p.normals.get(k).copied().unwrap_or([0.0, 0.0, 1.0]); + let wn = n.apply_vec(vec3(nn[0] as f64, nn[1] as f64, nn[2] as f64)).normalize(); + let t = p.tangents.get(k).copied().unwrap_or([1.0, 0.0, 0.0, 1.0]); + let wt = m.apply_vec(vec3(t[0] as f64, t[1] as f64, t[2] as f64)).normalize(); + self.s.vertices.push(Vertex { + pos: [w.x as f32, w.y as f32, w.z as f32], + normal: [wn.x as f32, wn.y as f32, wn.z as f32], + uv: p.uvs.get(k).copied().unwrap_or([0.0, 0.0]), + tangent: [wt.x as f32, wt.y as f32, wt.z as f32, if det < 0.0 { -t[3] } else { t[3] }], + material: mat, + }); + } + // Mirroring transforms reverse the winding. + let flip = flip_count * 2 > p.positions.len(); + for t in p.indices.chunks_exact(3) { + let (a, b, c) = (t[0] + base, t[1] + base, t[2] + base); + if flip { + self.s.indices.extend([a, c, b]) + } else { + self.s.indices.extend([a, b, c]) + } + let z = [a, b, c].iter().map(|&i| self.cam.depth(Vec3::from(self.s.vertices[i as usize].pos.map(|v| v as f64)))).sum::() / 3.0; + self.tri_depth.push((transparent, z as f32)); + } + } + + /// Opaque triangles first, then transparent ones back to front. + fn sort_triangles(&mut self) { + let tris: Vec<[u32; 3]> = self.s.indices.chunks_exact(3).map(|t| [t[0], t[1], t[2]]).collect(); + let mut order: Vec = (0..tris.len()).collect(); + order.sort_by(|&a, &b| { + let (ta, za) = self.tri_depth[a]; + let (tb, zb) = self.tri_depth[b]; + ta.cmp(&tb).then_with(|| if ta { zb.total_cmp(&za) } else { std::cmp::Ordering::Equal }) + }); + self.s.opaque_count = 3 * self.tri_depth.iter().filter(|t| !t.0).count() as u32; + self.s.indices = order.into_iter().flat_map(|i| tris[i]).collect(); + } +} + +/// The Material Options of a card (classic properties). +fn card_material(ctx: &EvalCtx, layer: &Layer) -> Material { + let c = ClassicMaterial::of(ctx, layer); + Material { + roughness: (1.0 - c.shininess).clamp(0.05, 1.0), + double_sided: true, + accepts_lights: c.accepts_lights, + receives_shadows: c.accepts_shadows != 0, + casts_shadows: c.casts_shadows != 0, + visible: c.casts_shadows != 2, + diffuse_k: c.diffuse, + specular_k: c.specular * 2.0, + ambient_k: c.ambient, + ..Material::default() + } +} + +/// Shadow/light switches and the primitive material of a model or primitive layer. +fn model_options(ctx: &EvalCtx, layer: &Layer, lin: bool) -> (Material, Option) { + let g = layer.props.sub("materialOptions"); + let e = |m: &str, d: u32| g.and_then(|g| g.get(m)).map_or(d, |p| ctx.value(layer, p).as_enum()); + let b = |m: &str| g.and_then(|g| g.get(m)).is_none_or(|p| ctx.value(layer, p).as_bool()); + let casts = e("castsShadows", 0); + let opts = Material { + accepts_lights: b("acceptsLights"), + receives_shadows: e("acceptsShadows", 1) != 0, + casts_shadows: casts != 0, + visible: casts != 2, + ..Material::default() + }; + let prim = match (layer.source.clone(), g) { + (LayerSource::Primitive { .. }, Some(g)) => { + let c = ctx.color(layer, g, "baseColor"); + let em = ctx.color(layer, g, "emissive"); + let l = |v: f32| if lin { v } else { srgb_to_linear(v) }; + let mut m = model::Material::plain( + [l(c[0]), l(c[1]), l(c[2]), 1.0], + (ctx.f(layer, g, "metallic", 0.0) / 100.0).clamp(0.0, 1.0) as f32, + (ctx.f(layer, g, "roughness", 50.0) / 100.0).clamp(0.0, 1.0) as f32, + ); + m.emissive = [l(em[0]), l(em[1]), l(em[2])]; + Some(m) + } + _ => None, + }; + (opts, prim) +} + +fn prim_kind(k: PrimitiveKind) -> model::prim::PrimitiveKind { + use model::prim::PrimitiveKind as M; + match k { + PrimitiveKind::Cube => M::Cube, + PrimitiveKind::Sphere => M::Sphere, + PrimitiveKind::Plane => M::Plane, + PrimitiveKind::Torus => M::Torus, + PrimitiveKind::Cone => M::Cone, + PrimitiveKind::Cylinder => M::Cylinder, + } +} + +/// The primitive model of a primitive layer at the context time. +pub fn primitive_model(ctx: &EvalCtx, layer: &Layer, kind: PrimitiveKind, mat: model::Material) -> model::Model { + let g = layer.props.sub("geometryOptions"); + let f = |m: &str, d: f64| g.map_or(d, |g| ctx.f(layer, g, m, d)); + let pr = model::prim::PrimParams { + size: [f("width", 300.0) as f32, f("height", 300.0) as f32, f("depth", 300.0) as f32], + radius: f("radius", 150.0) as f32, + tube: f("tubeRadius", 50.0) as f32, + height: f("height", 300.0) as f32, + segments: f("segments", 48.0).round().clamp(3.0, 512.0) as u32, + rings: f("rings", 24.0).round().clamp(2.0, 512.0) as u32, + }; + model::prim::model(prim_kind(kind), &pr, mat) +} + +/// Model space → layer space: model units to pixels, Y up → Y down (a 180° turn about X). +pub fn model_to_layer(unit_scale: f64) -> Mat4 { + Mat4::scale(vec3(unit_scale, -unit_scale, -unit_scale)) +} + +fn model_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, b: &mut Builder) { + let lin = b.s.linear_io; + let (opts, prim_mat) = model_options(ctx, layer, lin); + let opacity = (ctx.opacity(layer) as f32 * r.opacity_mul()).clamp(0.0, 1.0); + let geo = layer.props.sub("geometryOptions"); + let (mdl, unit, clip, t): (Arc, f64, Option, f64) = match &layer.source { + LayerSource::Primitive { kind } => (Arc::new(primitive_model(ctx, layer, *kind, prim_mat.unwrap_or_default())), 1.0, None, 0.0), + LayerSource::Model { item } => { + let Some(it) = r.project.item(*item) else { return }; + let ItemKind::Footage(f) = &it.kind else { return }; + let Some(m) = r.footage.model(*item, f) else { return }; + let unit = geo.map_or(1.0, |g| ctx.f(layer, g, "unitScale", 1.0)); + let clip = geo.map_or(0, |g| ctx.e(layer, g, "animation")) as usize; + let clip = clip.checked_sub(1).filter(|c| *c < m.animations.len()); + let speed = geo.map_or(100.0, |g| ctx.f(layer, g, "animationSpeed", 100.0)) / 100.0; + let mut t = layer.layer_time(ctx.time).seconds() * speed; + if let Some(c) = clip { + let d = m.animations[c].duration(); + let looping = geo.is_none_or(|g| ctx.b(layer, g, "loopAnimation")); + if looping && d > 0.0 { + t = t.rem_euclid(d); + } + } + (m, unit, clip, t) + } + _ => return, + }; + let world = ctx.world_matrix(layer) * model_to_layer(unit); + // Materials of the model (one default when it has none). + let mut mats: Vec = vec![]; + let defaults = [model::Material::plain([0.8, 0.8, 0.8, 1.0], 0.0, 0.5)]; + let src: &[model::Material] = if mdl.materials.is_empty() { &defaults } else { &mdl.materials }; + for mm in src { + let tex = |b: &mut Builder, t: Option, srgb: bool| -> i32 { + t.and_then(|t| mdl.textures.get(t.texture)).map_or(-1, |t| b.model_texture(t, srgb)) + }; + let m = Material { + base: mm.base_color, + metallic: mm.metallic, + roughness: mm.roughness, + emissive: mm.emissive, + normal_scale: mm.normal_scale, + occlusion_strength: mm.occlusion_strength, + alpha_mode: match mm.alpha_mode { + model::AlphaMode::Opaque => 0, + model::AlphaMode::Mask => 1, + model::AlphaMode::Blend => 2, + }, + alpha_cutoff: mm.alpha_cutoff, + opacity, + tex_base: tex(b, mm.base_color_tex, true), + tex_mr: tex(b, mm.metallic_roughness_tex, false), + tex_normal: tex(b, mm.normal_tex, false), + tex_occlusion: tex(b, mm.occlusion_tex, false), + tex_emissive: tex(b, mm.emissive_tex, true), + double_sided: mm.double_sided, + unlit: mm.unlit, + ..opts + }; + mats.push(b.material(m)); + } + for inst in mdl.instances(clip, t) { + for p in &inst.mesh.primitives { + let mat = mats[p.material.unwrap_or(0).min(mats.len() - 1)]; + b.emit(p, mat, &|k| if k == usize::MAX { world * inst.world } else { world * model::vertex_matrix(&inst, p, k) }); + } + } +} + +/// Closed polylines of a path (flattened to `tol` pixels). +pub fn contours(path: &kurbo::BezPath, tol: f64) -> Vec> { + let mut out: Vec> = vec![]; + let mut cur: Vec<[f64; 2]> = vec![]; + kurbo::flatten(path, tol, |el| match el { + kurbo::PathEl::MoveTo(p) => { + if cur.len() >= 3 { + out.push(std::mem::take(&mut cur)); + } + cur = vec![[p.x, p.y]]; + } + kurbo::PathEl::LineTo(p) => cur.push([p.x, p.y]), + kurbo::PathEl::ClosePath => { + if cur.len() >= 3 { + out.push(std::mem::take(&mut cur)); + } + cur.clear(); + } + _ => {} + }); + if cur.len() >= 3 { + out.push(cur); + } + out +} + +/// Geometry Options of a text/shape layer when it is extruded. +pub fn extrusion_params(ctx: &EvalCtx, layer: &Layer) -> Option { + let g = layer.props.sub("geometryOptions")?; + let depth = ctx.f(layer, g, "extrusionDepth", 0.0); + (depth > 0.0).then(|| model::extrude::ExtrudeParams { + bevel: model::extrude::BevelStyle::from_index(ctx.e(layer, g, "bevelStyle")), + bevel_depth: ctx.f(layer, g, "bevelDepth", 2.0).max(0.0), + hole_bevel: (ctx.f(layer, g, "holeBevelDepth", 100.0) / 100.0).clamp(0.0, 1.0), + depth, + }) +} + +/// Extruded meshes of a text or shape layer: (mesh in layer space, straight colour). +pub fn extruded_meshes(ctx: &EvalCtx, layer: &Layer, p: &model::extrude::ExtrudeParams) -> Vec<(Primitive, [f32; 4])> { + let mut groups: Vec<(Vec, [f32; 4])> = vec![]; + match layer.source { + LayerSource::Text => { + if let Some(tg) = crate::text::text_geom(ctx, layer) { + for g in tg.glyphs.iter().filter(|g| g.apply_fill && g.fill[3] > 0.0) { + groups.push((vec![g.path()], g.fill)); + } + } + } + LayerSource::Shape => { + if let Some(c) = layer.props.sub("contents") { + groups = crate::shapes::fill_outlines(ctx, layer, c); + } + } + _ => {} + } + groups + .into_iter() + .filter_map(|(paths, color)| { + let cs: Vec> = paths.iter().flat_map(|bp| contours(bp, 0.25)).collect(); + let outlines = model::extrude::group_contours(cs); + if outlines.is_empty() { + return None; + } + Some((model::extrude::extrude(&outlines, p), color)) + }) + .collect() +} + +fn extruded_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, p: &model::extrude::ExtrudeParams, b: &mut Builder) { + let base = card_material(ctx, layer); + let opacity = (ctx.opacity(layer) as f32 * r.opacity_mul()).clamp(0.0, 1.0); + let world = ctx.world_matrix(layer); + let lin = b.s.linear_io; + for (mesh, c) in extruded_meshes(ctx, layer, p) { + let l = |v: f32| if lin { v } else { srgb_to_linear(v) }; + let m = b.material(Material { + base: [l(c[0]), l(c[1]), l(c[2]), c[3]], + alpha_mode: if c[3] < 0.999 { 2 } else { 0 }, + opacity, + double_sided: false, + ..base + }); + b.emit(&mesh, m, &|_| world); + } +} + +fn card(b: &mut Builder, buf: &Buf, world: Mat4, mut mat: Material) { + if buf.img.width == 0 || buf.img.height == 0 { + return; + } + let (tex, opaque) = b.buf_texture(buf); + mat.tex_base = tex; + mat.alpha_mode = if opaque { 0 } else { 2 }; + let m = b.material(mat); + let s = buf.scale.max(1e-9); + let (x0, y0) = (-buf.offset[0] / s, -buf.offset[1] / s); + let (x1, y1) = ((buf.img.width as f64 - buf.offset[0]) / s, (buf.img.height as f64 - buf.offset[1]) / s); + // Faces the camera (−z) in layer space; uv (0,0) at the buffer's top-left texel corner. + let p = Primitive { + positions: vec![[x0 as f32, y0 as f32, 0.0], [x1 as f32, y0 as f32, 0.0], [x1 as f32, y1 as f32, 0.0], [x0 as f32, y1 as f32, 0.0]], + normals: vec![[0.0, 0.0, -1.0]; 4], + uvs: vec![[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]], + tangents: vec![[1.0, 0.0, 0.0, 1.0]; 4], + indices: vec![0, 2, 1, 0, 3, 2], + material: Some(0), + ..Default::default() + }; + b.emit(&p, m, &|_| world); +} + +fn card_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, b: &mut Builder) { + let mut mat = card_material(ctx, layer); + mat.opacity = (ctx.opacity(layer) as f32 * r.opacity_mul()).clamp(0.0, 1.0); + let world = ctx.world_matrix(layer); + if crate::text::per_char_3d(ctx, layer) { + for (buf, m) in crate::text::per_char_planes(ctx, layer, r.opts.scale) { + let buf = r.to_blend(Arc::new(buf), None); + card(b, &buf, world * m, mat); + } + return; + } + if let Some(buf) = r.blend_layer_buf(ctx, layer) { + card(b, &buf, world, mat); + } +} + +/// Evaluated lights: (scene lights, ambient sum, environment light (layer, intensity × colour, +/// rotation)). +fn lights(ctx: &EvalCtx, lin: bool) -> (Vec, [f32; 3], Option<(Option, [f32; 3], f32)>) { + let mut out = vec![]; + let mut ambient = [0.0f32; 3]; + let mut env = None; + let l = |v: f32| if lin { v } else { srgb_to_linear(v) }; + for layer in ctx.comp.layers.iter().filter(|l| l.is_light() && l.switches.video && l.is_active_at(ctx.time)) { + let LayerSource::Light { kind } = layer.source else { continue }; + let Some(g) = layer.props.sub("lightOptions") else { continue }; + let k = (ctx.f(layer, g, "intensity", 100.0) / 100.0) as f32; + let c = ctx.color(layer, g, "color"); + let color = [l(c[0]) * k, l(c[1]) * k, l(c[2]) * k]; + match kind { + LightKind::Ambient => { + for i in 0..3 { + ambient[i] += color[i]; + } + } + LightKind::Environment => { + if env.is_none() { + let src = g.get("source").and_then(|p| ctx.value(layer, p).as_layer()); + let rot = ctx.f(layer, g, "rotation", 0.0).to_radians() as f32; + env = Some((src, color, rot)); + } + } + _ => { + let Some(st) = LightState::from_layer(ctx, layer) else { continue }; + let f = |v: Vec3| [v.x as f32, v.y as f32, v.z as f32]; + out.push(Light { + kind: match kind { + LightKind::Parallel => 0, + LightKind::Spot => 1, + _ => 2, + }, + pos: f(st.pos), + dir: f(st.dir.normalize()), + color, + cos_inner: (st.cone_half * (1.0 - st.feather)).cos() as f32, + cos_outer: st.cone_half.cos() as f32, + falloff: st.falloff, + radius: st.radius as f32, + falloff_distance: st.falloff_distance as f32, + // Marked; `raster::shadow_maps` assigns the maps. + shadow: if st.casts_shadows { 0 } else { -1 }, + shadow_darkness: st.shadow_darkness as f32, + shadow_diffusion: st.shadow_diffusion as f32, + }); + } + } + } + (out, ambient, env) +} + +/// Box-downsample a straight RGBA image by 2 (odd sizes round up). +fn half(w: u32, h: u32, d: &[[f32; 4]]) -> (u32, u32, Vec<[f32; 4]>) { + let (nw, nh) = (w.div_ceil(2).max(1), h.div_ceil(2).max(1)); + let mut out = vec![[0.0f32; 4]; (nw * nh) as usize]; + for y in 0..nh { + for x in 0..nw { + let mut acc = [0.0f32; 4]; + let mut n = 0.0; + for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { + let (sx, sy) = (2 * x + dx, 2 * y + dy); + if sx < w && sy < h { + let p = d[(sy * w + sx) as usize]; + for k in 0..4 { + acc[k] += p[k]; + } + n += 1.0; + } + } + out[(y * nw + x) as usize] = acc.map(|v| v / n); + } + } + (nw, nh, out) +} + +/// Cosine-weighted irradiance (`w`×`h` equirectangular) of an equirectangular radiance image: +/// each texel is the cosine-weighted mean radiance over the hemisphere around its direction +/// (a constant environment gives the same constant). +pub fn irradiance_map(sw: u32, sh: u32, src: &[[f32; 4]], w: u32, h: u32) -> Vec<[f32; 4]> { + use super::shade::{dot, equirect_dir}; + // Source directions and solid-angle weights (∝ sin θ). + let dirs: Vec<([f32; 3], f32, [f32; 3])> = (0..sh) + .flat_map(|y| (0..sw).map(move |x| (x, y))) + .map(|(x, y)| { + let (u, v) = ((x as f32 + 0.5) / sw as f32, (y as f32 + 0.5) / sh as f32); + let p = src[(y * sw + x) as usize]; + (equirect_dir(u, v, 0.0), (v * std::f32::consts::PI).sin(), [p[0], p[1], p[2]]) + }) + .collect(); + let mut out = vec![[0.0f32; 4]; (w * h) as usize]; + for y in 0..h { + for x in 0..w { + let n = equirect_dir((x as f32 + 0.5) / w as f32, (y as f32 + 0.5) / h as f32, 0.0); + let (mut acc, mut wsum) = ([0.0f32; 3], 0.0f32); + for (d, sa, c) in &dirs { + let k = dot(n, *d).max(0.0) * sa; + if k > 0.0 { + for i in 0..3 { + acc[i] += c[i] * k; + } + wsum += k; + } + } + let inv = if wsum > 0.0 { 1.0 / wsum } else { 0.0 }; + out[(y * w + x) as usize] = [acc[0] * inv, acc[1] * inv, acc[2] * inv, 1.0]; + } + } + out +} + +/// Prefilter an environment image into the scene's textures. +pub(crate) fn add_environment(b: &mut Builder, w: u32, h: u32, data: Vec<[f32; 4]>, intensity: [f32; 3], rotation: f32) { + // Radiance level 0 at most 1024 wide. + let (mut w, mut h, mut d) = (w, h, data); + while w > 1024 { + (w, h, d) = half(w, h, &d); + } + let first = b.s.textures.len() as u32; + let mut levels = 0; + let mut small: Option<(u32, u32, Vec<[f32; 4]>)> = None; + loop { + b.texture_from(w, h, [0, 1], d.iter().copied()); + levels += 1; + if w <= 64 && small.is_none() { + small = Some((w, h, d.clone())); + } + if w <= 8 || h <= 4 { + break; + } + (w, h, d) = half(w, h, &d); + } + let (sw, sh, sd) = small.unwrap_or((w, h, d)); + let irr = irradiance_map(sw, sh, &sd, 32, 16); + let ii = b.texture_from(32, 16, [0, 1], irr.into_iter()) as u32; + // Intensity × colour folded into the radiance (one scalar on the GPU): tint the texels. + let k = (intensity[0] + intensity[1] + intensity[2]) / 3.0; + if k > 0.0 && (intensity[0] != intensity[1] || intensity[1] != intensity[2]) { + let tint = intensity.map(|c| c / k); + for t in first..=ii { + let ti = b.s.textures[t as usize]; + for p in &mut b.s.texels[ti.offset as usize..(ti.offset + ti.width * ti.height) as usize] { + for c in 0..3 { + p[c] *= tint[c]; + } + } + } + } + b.s.env = Some(EnvInfo { radiance: first, mips: levels, irradiance: ii, intensity: k, rotation }); +} + +/// The environment layer's pixels as linear straight RGBA. +fn env_pixels(r: &Renderer, ctx: &EvalCtx, src: Option, lin: bool) -> Option<(u32, u32, Vec<[f32; 4]>)> { + let layer = match src { + Some(id) => ctx.comp.layer(effectcraft_project::LayerId(id))?, + None => ctx.comp.layers.iter().find(|l| l.environment && l.is_active_at(ctx.time))?, + }; + let buf = r.blend_layer_buf(ctx, layer)?; + let img = &buf.img; + let data = img + .data + .iter() + .map(|p| { + let a = p[3]; + if a <= 1e-6 { + return [0.0, 0.0, 0.0, 1.0]; + } + let s = [p[0] / a, p[1] / a, p[2] / a]; + if lin { [s[0], s[1], s[2], 1.0] } else { [srgb_to_linear(s[0]), srgb_to_linear(s[1]), srgb_to_linear(s[2]), 1.0] } + }) + .collect(); + Some((img.width, img.height, data)) +} + +/// Supersampling factor: 2×2 unless draft or very large. +fn ssaa_for(r: &Renderer, out: (u32, u32)) -> u32 { + if r.opts.draft || (out.0 as u64 * out.1 as u64) > 8_000_000 { 1 } else { 2 } +} + +/// Build the scene of a run of 3D layers. +pub(crate) fn build(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], out: (u32, u32)) -> Scene { + let cam = camera_for(r, ctx); + let lin = r.pipe.linear || r.pipe.linear_blend; + let ssaa = ssaa_for(r, out); + let mut b = Builder { + s: Scene { width: out.0 * ssaa, height: out.1 * ssaa, ssaa, linear_io: lin, ..Scene::default() }, + tex_keys: HashMap::new(), + tri_depth: vec![], + cam, + }; + for layer in run { + if layer.environment || layer.switches.adjustment || ctx.opacity(layer) <= 0.0 || !layer.has_video() { + continue; + } + if layer.source.is_model() { + model_layer(r, ctx, layer, &mut b); + } else if matches!(layer.source, LayerSource::Text | LayerSource::Shape) + && let Some(p) = extrusion_params(ctx, layer) + { + extruded_layer(r, ctx, layer, &p, &mut b); + } else { + card_layer(r, ctx, layer, &mut b); + } + } + b.sort_triangles(); + // Lights, environment, shadows (Draft 3D: unlit, like After Effects). + if !ctx.comp.draft_3d { + let (ls, amb, env) = lights(ctx, lin); + b.s.lights = ls; + b.s.ambient = amb; + if let Some((src, k, rot)) = env + && let Some((w, h, d)) = env_pixels(r, ctx, src, lin) + { + add_environment(&mut b, w, h, d, k, rot); + } + super::raster::shadow_maps(&mut b.s, r.opts.draft); + } + // Camera matrices. + let s = r.opts.scale; + let (cw, ch) = (ctx.comp.width as f64, ctx.comp.height as f64); + let (ow, oh) = (out.0 as f64, out.1 as f64); + let (a, bb) = (2.0 * s * cam.zoom / ow, s * cw / ow - 1.0); + let (c, d) = (2.0 * s * cam.zoom / oh, s * ch / oh - 1.0); + let proj = if cam.ortho { + let (mut zmin, mut zmax) = (f64::INFINITY, f64::NEG_INFINITY); + for v in &b.s.vertices { + let z = cam.depth(Vec3::from(v.pos.map(|x| x as f64))); + zmin = zmin.min(z); + zmax = zmax.max(z); + } + if !zmin.is_finite() { + (zmin, zmax) = (-1.0, 1.0); + } + let pad = (zmax - zmin).abs() * 0.01 + 1.0; + let (zmin, zmax) = (zmin - pad, zmax + pad); + let zr = zmax - zmin; + Mat4([[a, 0.0, 0.0, bb], [0.0, -c, 0.0, -d], [0.0, 0.0, -1.0 / zr, zmax / zr], [0.0, 0.0, 0.0, 1.0]]) + } else { + Mat4([[a, 0.0, bb, 0.0], [0.0, -c, -d, 0.0], [0.0, 0.0, 0.0, NEAR], [0.0, 0.0, 1.0, 0.0]]) + }; + b.s.clip = m32(&(proj * cam.view)); + b.s.view = m32(&cam.view); + let f = cam.forward(); + b.s.cam_fwd = [f.x as f32, f.y as f32, f.z as f32]; + b.s.eye = [cam.eye.x as f32, cam.eye.y as f32, cam.eye.z as f32]; + b.s.ortho = cam.ortho; + b.s +} diff --git a/crates/render/src/three_d/adv/shade.rs b/crates/render/src/three_d/adv/shade.rs new file mode 100644 index 00000000..a8ab5e70 --- /dev/null +++ b/crates/render/src/three_d/adv/shade.rs @@ -0,0 +1,408 @@ +//! Physically based shading for the Advanced 3D renderer (CPU reference; `crates/gpu`'s +//! `advanced3d.wgsl` repeats it step for step). +//! +//! - Direct light: Cook–Torrance microfacet specular with the GGX/Trowbridge–Reitz normal +//! distribution, the Smith–Schlick-GGX geometry term (`k = (r + 1)² / 8`) and Schlick's +//! Fresnel (`F0` = 0.04 for dielectrics, the base colour for metals), plus a Lambertian +//! diffuse lobe weighted by `(1 − F)(1 − metallic)` (Karis 2013, "Real Shading in Unreal +//! Engine 4"; the glTF 2.0 metallic-roughness model). Light colours are the irradiance on a +//! surface facing the light (a white 100% light shows a white diffuse surface at its base +//! colour), i.e. π is folded into the light intensity. +//! - Image-based light: a cosine-convolved irradiance map for diffuse, a box-filtered mip +//! chain of the equirectangular environment sampled by roughness for specular, and the +//! analytic split-sum environment BRDF fit (Karis 2014, "Physically Based Shading on +//! Mobile"). +//! - Shadows: depth maps per light, percentage-closer filtered. + +use super::scene::{EnvInfo, Light, Material, Scene, ShadowMap, TexInfo}; + +pub const PI: f32 = std::f32::consts::PI; + +#[inline] +pub fn dot(a: [f32; 3], b: [f32; 3]) -> f32 { + a[0] * b[0] + a[1] * b[1] + a[2] * b[2] +} +#[inline] +pub fn cross(a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]] +} +#[inline] +pub fn norm(a: [f32; 3]) -> [f32; 3] { + let l = dot(a, a).sqrt(); + if l > 1e-20 { [a[0] / l, a[1] / l, a[2] / l] } else { [0.0, 0.0, -1.0] } +} +#[inline] +fn add(a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + [a[0] + b[0], a[1] + b[1], a[2] + b[2]] +} +#[inline] +fn sub(a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + [a[0] - b[0], a[1] - b[1], a[2] - b[2]] +} +#[inline] +fn scale(a: [f32; 3], k: f32) -> [f32; 3] { + [a[0] * k, a[1] * k, a[2] * k] +} +#[inline] +fn mix(a: f32, b: f32, t: f32) -> f32 { + a + (b - a) * t +} + +/// sRGB-encoded → linear light. +#[inline] +pub fn srgb_to_linear(c: f32) -> f32 { + if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) } +} +/// Linear light → sRGB-encoded (over-range values continue the curve). +#[inline] +pub fn linear_to_srgb(c: f32) -> f32 { + if c <= 0.0031308 { c * 12.92 } else { 1.055 * c.powf(1.0 / 2.4) - 0.055 } +} + +/// GGX / Trowbridge–Reitz normal distribution, `a = roughness²`. +#[inline] +pub fn d_ggx(n_h: f32, a: f32) -> f32 { + let a2 = a * a; + let d = n_h * n_h * (a2 - 1.0) + 1.0; + a2 / (PI * d * d).max(1e-12) +} + +/// Smith–Schlick-GGX geometry term for direct light (`k = (r + 1)² / 8`). +#[inline] +pub fn g_smith(n_v: f32, n_l: f32, rough: f32) -> f32 { + let k = (rough + 1.0) * (rough + 1.0) / 8.0; + let g1 = |x: f32| x / (x * (1.0 - k) + k); + g1(n_v) * g1(n_l) +} + +/// Schlick's Fresnel approximation. +#[inline] +pub fn f_schlick(f0: [f32; 3], v_h: f32) -> [f32; 3] { + let k = (1.0 - v_h).clamp(0.0, 1.0).powi(5); + [f0[0] + (1.0 - f0[0]) * k, f0[1] + (1.0 - f0[1]) * k, f0[2] + (1.0 - f0[2]) * k] +} + +/// Cook–Torrance BRDF × N·L × π for one light: (diffuse colour factor, specular colour). +/// Returned as the reflected fraction of the light's irradiance. +pub fn brdf(base: [f32; 3], metallic: f32, rough: f32, spec_k: f32, diff_k: f32, n: [f32; 3], v: [f32; 3], l: [f32; 3]) -> [f32; 3] { + let n_l = dot(n, l); + if n_l <= 0.0 { + return [0.0; 3]; + } + let n_v = dot(n, v).max(1e-4); + let h = norm(add(v, l)); + let n_h = dot(n, h).max(0.0); + let v_h = dot(v, h).max(0.0); + let r = rough.clamp(0.03, 1.0); + let f0 = [mix(0.04 * spec_k, base[0], metallic), mix(0.04 * spec_k, base[1], metallic), mix(0.04 * spec_k, base[2], metallic)]; + let f = f_schlick(f0, v_h); + let d = d_ggx(n_h, r * r); + let g = g_smith(n_v, n_l, r); + let spec = d * g / (4.0 * n_v * n_l).max(1e-4); + let mut out = [0.0; 3]; + for c in 0..3 { + let kd = (1.0 - f[c]) * (1.0 - metallic); + out[c] = (kd * base[c] / PI * diff_k + f[c] * spec) * n_l * PI; + } + out +} + +/// Split-sum environment BRDF (scale, bias) for `F0·A + B` (analytic fit). +#[inline] +pub fn env_brdf(n_v: f32, rough: f32) -> (f32, f32) { + let c0 = [-1.0, -0.0275, -0.572, 0.022]; + let c1 = [1.0, 0.0425, 1.04, -0.04]; + let r = [rough * c0[0] + c1[0], rough * c0[1] + c1[1], rough * c0[2] + c1[2], rough * c0[3] + c1[3]]; + let a004 = (r[0] * r[0]).min((-9.28 * n_v).exp2()) * r[0] + r[1]; + (-1.04 * a004 + r[2], 1.04 * a004 + r[3]) +} + +/// Wrap a texel index. +#[inline] +fn wrap(i: i64, n: i64, mode: u32) -> i64 { + match mode { + 1 => i.clamp(0, n - 1), + 2 => { + let m = i.rem_euclid(2 * n); + if m >= n { 2 * n - 1 - m } else { m } + } + _ => i.rem_euclid(n), + } +} + +/// Bilinear texture sample at (u, v) (0..1 across the texture, texel centres at +0.5). +pub fn sample(t: &TexInfo, texels: &[[f32; 4]], u: f32, v: f32) -> [f32; 4] { + let (w, h) = (t.width as i64, t.height as i64); + if w == 0 || h == 0 { + return [1.0; 4]; + } + let x = u * w as f32 - 0.5; + let y = v * h as f32 - 0.5; + let (x0, y0) = (x.floor(), y.floor()); + let (fx, fy) = (x - x0, y - y0); + let (x0, y0) = (x0 as i64, y0 as i64); + let get = |xi: i64, yi: i64| -> [f32; 4] { + let xi = wrap(xi, w, t.wrap_u); + let yi = wrap(yi, h, t.wrap_v); + texels.get(t.offset as usize + (yi * w + xi) as usize).copied().unwrap_or([0.0; 4]) + }; + let (a, b, c, d) = (get(x0, y0), get(x0 + 1, y0), get(x0, y0 + 1), get(x0 + 1, y0 + 1)); + let mut o = [0.0; 4]; + for k in 0..4 { + let top = a[k] + (b[k] - a[k]) * fx; + let bot = c[k] + (d[k] - c[k]) * fx; + o[k] = top + (bot - top) * fy; + } + o +} + +/// Equirectangular coordinates of a world direction (AE world: +Y down; the environment's up +/// is −Y), rotated about the vertical axis by `rot` radians. +#[inline] +pub fn equirect_uv(d: [f32; 3], rot: f32) -> (f32, f32) { + let u = 0.5 + (d[0].atan2(d[2]) + rot) / (2.0 * PI); + let v = (-d[1]).clamp(-1.0, 1.0).acos() / PI; + (u - u.floor(), v) +} + +/// Inverse of [`equirect_uv`] (texel centre → direction). +pub fn equirect_dir(u: f32, v: f32, rot: f32) -> [f32; 3] { + let phi = (u - 0.5) * 2.0 * PI - rot; + let th = v * PI; + [th.sin() * phi.sin(), -th.cos(), th.sin() * phi.cos()] +} + +/// Diffuse (irradiance) and roughness-filtered specular radiance of the environment. +pub fn env_light(s: &Scene, e: &EnvInfo, n: [f32; 3], r: [f32; 3], rough: f32) -> ([f32; 3], [f32; 3]) { + let (u, v) = equirect_uv(n, e.rotation); + let irr = s.textures.get(e.irradiance as usize).map_or([0.0; 4], |t| sample(t, &s.texels, u, v)); + let (u, v) = equirect_uv(r, e.rotation); + let lv = rough.clamp(0.0, 1.0) * (e.mips.saturating_sub(1)) as f32; + let l0 = lv.floor() as u32; + let l1 = (l0 + 1).min(e.mips.saturating_sub(1)); + let f = lv - l0 as f32; + let a = s.textures.get((e.radiance + l0) as usize).map_or([0.0; 4], |t| sample(t, &s.texels, u, v)); + let b = s.textures.get((e.radiance + l1) as usize).map_or([0.0; 4], |t| sample(t, &s.texels, u, v)); + let spec = [mix(a[0], b[0], f), mix(a[1], b[1], f), mix(a[2], b[2], f)]; + (scale([irr[0], irr[1], irr[2]], e.intensity), scale(spec, e.intensity)) +} + +/// Light-space position (u, v in shadow-map pixels, depth) of a world point. +#[inline] +pub fn shadow_coord(m: &ShadowMap, p: [f32; 3]) -> Option<(f32, f32, f32)> { + let v = &m.view; + let q = [ + v[0][0] * p[0] + v[0][1] * p[1] + v[0][2] * p[2] + v[0][3], + v[1][0] * p[0] + v[1][1] * p[1] + v[1][2] * p[2] + v[1][3], + v[2][0] * p[0] + v[2][1] * p[1] + v[2][2] * p[2] + v[2][3], + ]; + let c = m.size as f32 * 0.5; + if m.ortho { + Some((q[0] * m.focal + c, q[1] * m.focal + c, q[2])) + } else if q[2] > 1e-3 { + Some((q[0] * m.focal / q[2] + c, q[1] * m.focal / q[2] + c, q[2])) + } else { + None + } +} + +/// Fraction of light reaching `p` through shadow map `m` (1 = lit), PCF over a 5×5 grid +/// spread over `radius` texels. +pub fn shadow_factor(m: &ShadowMap, texels: &[f32], p: [f32; 3], n_l: f32) -> f32 { + let Some((u, v, z)) = shadow_coord(m, p) else { return 1.0 }; + let size = m.size as i64; + // Slope-scaled bias (world units are pixels). + let bias = m.bias * (1.0 + 2.0 * (1.0 - n_l.clamp(0.0, 1.0))) + z.abs() * 0.0015; + let step = m.radius / 2.0; + let mut lit = 0.0; + for j in -2..=2 { + for i in -2..=2 { + let x = (u + i as f32 * step).floor() as i64; + let y = (v + j as f32 * step).floor() as i64; + if x < 0 || y < 0 || x >= size || y >= size { + lit += 1.0; + continue; + } + let d = texels[m.offset as usize + (y * size + x) as usize]; + if z - bias <= d { + lit += 1.0; + } + } + } + lit / 25.0 +} + +/// The visibility of light `li` at `p` (1 = unshadowed), darkened by Shadow Darkness. +pub fn light_shadow(s: &Scene, l: &Light, p: [f32; 3], n_l: f32) -> f32 { + if l.shadow < 0 { + return 1.0; + } + let first = l.shadow as usize; + let m = if l.kind == 2 { + // Point light: the cube face of the major axis of the light → point direction. + let d = sub(p, l.pos); + let (ax, ay, az) = (d[0].abs(), d[1].abs(), d[2].abs()); + let face = if ax >= ay && ax >= az { + if d[0] > 0.0 { 0 } else { 1 } + } else if ay >= az { + if d[1] > 0.0 { 2 } else { 3 } + } else if d[2] > 0.0 { + 4 + } else { + 5 + }; + s.shadows.get(first + face) + } else { + s.shadows.get(first) + }; + let Some(m) = m else { return 1.0 }; + let f = shadow_factor(m, &s.shadow_texels, p, n_l); + 1.0 - l.shadow_darkness * (1.0 - f) +} + +/// Distance attenuation × spot cone of a light at `p` (AE falloff modes). +pub fn attenuation(l: &Light, p: [f32; 3], to_l: [f32; 3], dist: f32) -> f32 { + let mut k = match l.falloff { + 1 if l.kind != 0 => { + if dist <= l.radius { + 1.0 + } else if l.falloff_distance <= 0.0 { + 0.0 + } else { + let t = ((dist - l.radius) / l.falloff_distance).clamp(0.0, 1.0); + 1.0 - t * t * (3.0 - 2.0 * t) + } + } + 2 if l.kind != 0 => { + if dist <= l.radius || (l.radius <= 0.0 && dist <= 1.0) { + 1.0 + } else { + (l.radius.max(1.0) / dist).powi(2) + } + } + _ => 1.0, + }; + let _ = p; + if l.kind == 1 { + let cos_a = dot(scale(to_l, -1.0), l.dir).clamp(-1.0, 1.0); + k *= if cos_a >= l.cos_inner { + 1.0 + } else if cos_a <= l.cos_outer { + 0.0 + } else { + // Smoothstep across the feather in angle. + let a = cos_a.acos(); + let (ai, ao) = (l.cos_inner.acos(), l.cos_outer.acos()); + let t = ((a - ai) / (ao - ai).max(1e-6)).clamp(0.0, 1.0); + 1.0 - t * t * (3.0 - 2.0 * t) + }; + } + k +} + +/// Inputs of one fragment. +#[derive(Clone, Copy, Debug)] +pub struct Frag { + pub pos: [f32; 3], + /// Interpolated (unnormalised) vertex normal. + pub normal: [f32; 3], + pub uv: [f32; 2], + pub tangent: [f32; 4], + pub material: u32, + pub front: bool, +} + +/// Shade a fragment: premultiplied linear RGBA, or `None` when discarded (alpha test, back +/// face of a single-sided material). +pub fn shade(s: &Scene, f: &Frag) -> Option<[f32; 4]> { + let m: &Material = s.materials.get(f.material as usize)?; + if !f.front && !m.double_sided { + return None; + } + let tex = |i: i32| -> Option<[f32; 4]> { (i >= 0).then(|| s.textures.get(i as usize).map(|t| sample(t, &s.texels, f.uv[0], f.uv[1]))).flatten() }; + let mut base = m.base; + if let Some(t) = tex(m.tex_base) { + for k in 0..4 { + base[k] *= t[k]; + } + } + let alpha = base[3] * m.opacity; + if m.alpha_mode == 1 && alpha < m.alpha_cutoff { + return None; + } + if alpha <= 1e-5 && m.alpha_mode != 0 { + return None; + } + let alpha = if m.alpha_mode == 0 { m.opacity } else { alpha }; + let rgb = [base[0], base[1], base[2]]; + let mut emissive = m.emissive; + if let Some(t) = tex(m.tex_emissive) { + emissive = [emissive[0] * t[0], emissive[1] * t[1], emissive[2] * t[2]]; + } + if m.unlit || !m.accepts_lights || (s.lights.is_empty() && s.env.is_none() && s.ambient == [0.0; 3]) { + let c = add(rgb, emissive); + return Some([c[0] * alpha, c[1] * alpha, c[2] * alpha, alpha]); + } + let (mut metallic, mut rough) = (m.metallic, m.roughness); + if let Some(t) = tex(m.tex_mr) { + rough *= t[1]; + metallic *= t[2]; + } + let mut n = norm(f.normal); + if !f.front { + n = scale(n, -1.0); + } + if let Some(t) = tex(m.tex_normal) { + let tn = [(t[0] * 2.0 - 1.0) * m.normal_scale, (t[1] * 2.0 - 1.0) * m.normal_scale, t[2] * 2.0 - 1.0]; + let tg = [f.tangent[0], f.tangent[1], f.tangent[2]]; + // Gram-Schmidt against the shading normal. + let tg = norm(sub(tg, scale(n, dot(n, tg)))); + let sign = if f.front { f.tangent[3] } else { -f.tangent[3] }; + let bt = scale(cross(n, tg), sign); + n = norm(add(add(scale(tg, tn[0]), scale(bt, tn[1])), scale(n, tn[2]))); + } + let ao = match tex(m.tex_occlusion) { + Some(t) => 1.0 + m.occlusion_strength * (t[0] - 1.0), + None => 1.0, + }; + let v = if s.ortho { scale(s.cam_fwd, -1.0) } else { norm(sub(s.eye, f.pos)) }; + let mut out = [0.0f32; 3]; + for l in &s.lights { + let (to_l, dist) = if l.kind == 0 { + (scale(l.dir, -1.0), f32::INFINITY) + } else { + let d = sub(l.pos, f.pos); + let len = dot(d, d).sqrt(); + if len < 1e-6 { ([0.0, 0.0, -1.0], 0.0) } else { (scale(d, 1.0 / len), len) } + }; + let k = attenuation(l, f.pos, to_l, dist); + if k <= 0.0 { + continue; + } + let n_l = dot(n, to_l); + if n_l <= 0.0 { + continue; + } + let sh = if m.receives_shadows { light_shadow(s, l, f.pos, n_l) } else { 1.0 }; + let b = brdf(rgb, metallic, rough, m.specular_k, m.diffuse_k, n, v, to_l); + for c in 0..3 { + out[c] += b[c] * l.color[c] * k * sh; + } + } + for c in 0..3 { + out[c] += s.ambient[c] * rgb[c] * m.ambient_k * ao; + } + if let Some(e) = &s.env { + let n_v = dot(n, v).clamp(1e-4, 1.0); + let r = sub(scale(n, 2.0 * dot(n, v)), v); + let (irr, spec) = env_light(s, e, n, r, rough); + let f0 = [mix(0.04 * m.specular_k, rgb[0], metallic), mix(0.04 * m.specular_k, rgb[1], metallic), mix(0.04 * m.specular_k, rgb[2], metallic)]; + let (a, b) = env_brdf(n_v, rough); + for c in 0..3 { + let fs = f0[c] * a + b; + out[c] += (irr[c] * rgb[c] * (1.0 - fs) * (1.0 - metallic) * m.diffuse_k + spec[c] * fs) * ao; + } + } + let c = add(out, emissive); + Some([c[0] * alpha, c[1] * alpha, c[2] * alpha, alpha]) +} diff --git a/crates/render/src/three_d/adv/tests.rs b/crates/render/src/three_d/adv/tests.rs new file mode 100644 index 00000000..23db2cbc --- /dev/null +++ b/crates/render/src/three_d/adv/tests.rs @@ -0,0 +1,314 @@ +use effectcraft_keyframe::Value; +use effectcraft_project::{Comp, ItemId, ItemKind, LayerSource, LightKind, PrimitiveKind, Project, Renderer as R3, build}; +use effectcraft_time::{FrameRate, Tick}; + +use super::scene::{Material, Scene, TexInfo, Vertex, irradiance_map}; +use super::shade::{self, PI}; +use super::*; +use crate::{NoFootage, RenderOpts, Renderer}; + +// ---------------------------------------------------------------- shading + +#[test] +fn brdf_matches_cook_torrance_formula() { + // Reference: f = kd·c/π + D·G·F/(4·NV·NL), returned × NL × π. + let n = [0.0, 0.0, 1.0]; + let v = shade::norm([0.3, 0.1, 1.0]); + let l = shade::norm([-0.4, 0.2, 1.0]); + let (base, metal, rough) = ([0.8, 0.4, 0.2], 0.3f32, 0.45f32); + let got = shade::brdf(base, metal, rough, 1.0, 1.0, n, v, l); + let h = shade::norm([v[0] + l[0], v[1] + l[1], v[2] + l[2]]); + let (nv, nl, nh, vh) = (shade::dot(n, v), shade::dot(n, l), shade::dot(n, h), shade::dot(v, h)); + let a2 = (rough * rough).powi(2); + let d = a2 / (PI * ((nh * nh) * (a2 - 1.0) + 1.0).powi(2)); + let k = (rough + 1.0).powi(2) / 8.0; + let g = nv / (nv * (1.0 - k) + k) * nl / (nl * (1.0 - k) + k); + for c in 0..3 { + let f0 = 0.04 + (base[c] - 0.04) * metal; + let f = f0 + (1.0 - f0) * (1.0 - vh).powi(5); + let expect = ((1.0 - f) * (1.0 - metal) * base[c] / PI + d * g * f / (4.0 * nv * nl)) * nl * PI; + assert!((got[c] - expect).abs() < 1e-5, "channel {c}: {} vs {expect}", got[c]); + } + // No light from behind the surface. + assert_eq!(shade::brdf(base, metal, rough, 1.0, 1.0, n, v, [0.0, 0.0, -1.0]), [0.0; 3]); + // A white rough dielectric lit head-on reflects about its base colour (π folded in). + let w = shade::brdf([1.0; 3], 0.0, 1.0, 1.0, 1.0, n, n, n); + assert!(w[0] > 0.95 && w[0] < 1.05, "{w:?}"); +} + +#[test] +fn ggx_distribution_is_normalised() { + // ∫ D(h)·(n·h) dω = 1 over the hemisphere. + for &rough in &[0.2f32, 0.5, 0.9] { + let a = rough * rough; + let n = 4000; + let mut sum = 0.0f64; + for i in 0..n { + let th = (i as f64 + 0.5) / n as f64 * std::f64::consts::FRAC_PI_2; + let c = th.cos() as f32; + sum += shade::d_ggx(c, a) as f64 * c as f64 * th.sin() * std::f64::consts::FRAC_PI_2 / n as f64 * 2.0 * std::f64::consts::PI; + } + assert!((sum - 1.0).abs() < 0.02, "roughness {rough}: {sum}"); + } +} + +#[test] +fn constant_environment_has_constant_irradiance() { + let src = vec![[0.25, 0.5, 0.75, 1.0]; 64 * 32]; + let irr = irradiance_map(64, 32, &src, 32, 16); + for p in &irr { + for c in 0..3 { + assert!((p[c] - src[0][c]).abs() < 1e-4, "{p:?}"); + } + } + // And a sky brighter than the ground lights upward-facing normals more. + let sky: Vec<[f32; 4]> = (0..32).flat_map(|y| (0..64).map(move |_| if y < 16 { [1.0, 1.0, 1.0, 1.0] } else { [0.0, 0.0, 0.0, 1.0] })).collect(); + let irr = irradiance_map(64, 32, &sky, 32, 16); + assert!(irr[16][0] > 0.9 && irr[15 * 32 + 16][0] < 0.1, "{:?} {:?}", irr[16], irr[15 * 32 + 16]); +} + +#[test] +fn equirect_round_trip_and_env_brdf() { + for (u, v) in [(0.1f32, 0.2f32), (0.5, 0.5), (0.9, 0.7)] { + let d = shade::equirect_dir(u, v, 0.3); + let (u2, v2) = shade::equirect_uv(d, 0.3); + assert!((u - u2).abs() < 1e-4 && (v - v2).abs() < 1e-4); + } + // Up (−Y in AE space) is the top row. + assert!(shade::equirect_uv([0.0, -1.0, 0.0], 0.0).1 < 1e-3); + let (a, b) = shade::env_brdf(1.0, 0.0); + assert!(a + b > 0.9 && a + b <= 1.05); + let (a, b) = shade::env_brdf(0.1, 1.0); + assert!(a >= 0.0 && b > -0.01 && a + b < 1.0); +} + +// ---------------------------------------------------------------- rasteriser + +/// A scene of axis-aligned quads (z = camera depth) seen by an orthographic identity camera +/// mapping world x/y straight to raster pixels. +fn quad_scene(w: u32, h: u32, quads: &[([f32; 4], f32, [f32; 4])]) -> Scene { + let mut s = Scene { width: w, height: h, ssaa: 1, linear_io: true, ..Scene::default() }; + // clip: x → 2x/w − 1, y → 1 − 2y/h, z: depth 0..1000 → 1..0 (nearer = larger). + s.clip = [[2.0 / w as f32, 0.0, 0.0, -1.0], [0.0, -2.0 / h as f32, 0.0, 1.0], [0.0, 0.0, -0.001, 1.0], [0.0, 0.0, 0.0, 1.0]]; + s.view = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]; + s.ortho = true; + s.cam_fwd = [0.0, 0.0, 1.0]; + for (rect, z, color) in quads { + let m = s.materials.len() as u32; + s.materials.push(Material { base: *color, unlit: true, double_sided: true, alpha_mode: if color[3] < 1.0 { 2 } else { 0 }, ..Material::default() }); + let b = s.vertices.len() as u32; + for (x, y) in [(rect[0], rect[1]), (rect[2], rect[1]), (rect[2], rect[3]), (rect[0], rect[3])] { + s.vertices.push(Vertex { pos: [x, y, *z], normal: [0.0, 0.0, -1.0], uv: [0.0; 2], tangent: [1.0, 0.0, 0.0, 1.0], material: m }); + } + s.indices.extend([b, b + 2, b + 1, b, b + 3, b + 2]); + } + // Opaque first, transparent (in given order) after. + let tris: Vec<[u32; 3]> = s.indices.chunks(3).map(|t| [t[0], t[1], t[2]]).collect(); + let (o, t): (Vec<_>, Vec<_>) = tris.into_iter().partition(|t| !s.materials[s.vertices[t[0] as usize].material as usize].transparent()); + s.opaque_count = 3 * o.len() as u32; + s.indices = o.into_iter().chain(t).flatten().collect(); + s +} + +#[test] +fn depth_buffer_keeps_the_nearest_surface() { + let red = [1.0, 0.0, 0.0, 1.0]; + let blue = [0.0, 0.0, 1.0, 1.0]; + // Blue is listed first but is farther away. + let s = quad_scene(40, 30, &[([0.0, 0.0, 30.0, 20.0], 500.0, blue), ([10.0, 10.0, 40.0, 30.0], 100.0, red)]); + let t = raster::render(&s); + let px = |x: usize, y: usize| t.color[y * 40 + x]; + assert_eq!(px(5, 5), blue); + assert_eq!(px(15, 15), red, "overlap: the nearer quad wins"); + assert_eq!(px(35, 25), red); + assert_eq!(px(35, 5), [0.0; 4]); + assert_eq!(t.depth[15 * 40 + 15], 100.0); + assert!(t.depth[5 * 40 + 35].is_infinite()); +} + +#[test] +fn coverage_is_exact_for_pixel_aligned_quads() { + // Shared diagonal edges are filled once (top-left rule): exact pixel counts. + let s = quad_scene(32, 32, &[([4.0, 4.0, 20.0, 12.0], 10.0, [1.0, 1.0, 1.0, 0.5])]); + let t = raster::render(&s); + let covered = t.color.iter().filter(|c| c[3] > 0.0).count(); + assert_eq!(covered, 16 * 8); + // Blended once (no double coverage along the diagonal). + assert!(t.color.iter().all(|c| c[3] == 0.0 || (c[3] - 0.5).abs() < 1e-6)); + // Two transparent layers composite in order. + let s = quad_scene(8, 8, &[([0.0, 0.0, 8.0, 8.0], 100.0, [1.0, 0.0, 0.0, 0.5]), ([0.0, 0.0, 8.0, 8.0], 50.0, [0.0, 0.0, 1.0, 0.5])]); + let c = raster::render(&s).color[0]; + assert!((c[0] - 0.25).abs() < 1e-6 && (c[2] - 0.5).abs() < 1e-6 && (c[3] - 0.75).abs() < 1e-6, "{c:?}"); +} + +#[test] +fn textures_sample_bilinear_with_wrap_modes() { + let t = TexInfo { offset: 0, width: 2, height: 1, wrap_u: 0, wrap_v: 1 }; + let texels = vec![[0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]; + assert_eq!(shade::sample(&t, &texels, 0.25, 0.5)[0], 0.0); + assert_eq!(shade::sample(&t, &texels, 0.5, 0.5)[0], 0.5); + // Repeat wraps the right edge back to texel 0. + assert!((shade::sample(&t, &texels, 1.0, 0.5)[0] - 0.5).abs() < 1e-6); + let c = TexInfo { wrap_u: 1, ..t }; + assert_eq!(shade::sample(&c, &texels, 1.0, 0.5)[0], 1.0); +} + +// ---------------------------------------------------------------- full renderer + +fn project(w: u32, h: u32) -> (Project, ItemId) { + let mut p = Project::default(); + let mut c = Comp::new(w, h, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + c.renderer = R3::Advanced3D; + let id = p.add_item("C", Default::default(), None, ItemKind::Comp(c.into())); + (p, id) +} + +fn add(p: &mut Project, cid: ItemId, src: LayerSource, edit: impl FnOnce(&mut effectcraft_project::Layer)) { + let comp = p.comp(cid).unwrap().clone(); + let mut l = build::layer(p, &comp, "L", src, (comp.width, comp.height), None); + edit(&mut l); + p.comp_mut(cid).unwrap().layers.insert(0, l); +} + +fn set(l: &mut effectcraft_project::Layer, path: &str, v: Value) { + l.props.prop_mut(path).unwrap_or_else(|| panic!("{path}")).value = v; +} + +fn render(p: &Project, cid: ItemId) -> crate::Image { + Renderer::new(p, &NoFootage, RenderOpts::default()).comp_frame(cid, Tick::ZERO) +} + +#[test] +fn lit_cube_renders_with_depth_and_shading() { + let (mut p, cid) = project(160, 120); + add(&mut p, cid, LayerSource::Primitive { kind: PrimitiveKind::Cube }, |l| { + set(l, "geometryOptions/width", Value::Scalar(60.0)); + set(l, "geometryOptions/height", Value::Scalar(60.0)); + set(l, "geometryOptions/depth", Value::Scalar(60.0)); + set(l, "transform/rotationY", Value::Scalar(35.0)); + set(l, "transform/rotationX", Value::Scalar(-25.0)); + set(l, "materialOptions/baseColor", Value::Color([1.0, 1.0, 1.0, 1.0])); + }); + let img = render(&p, cid); + let centre = img.data[60 * 160 + 80]; + assert!(centre[3] > 0.99, "cube covers the centre: {centre:?}"); + assert_eq!(img.data[2 * 160 + 2][3], 0.0, "corner is empty"); + // No lights: unlit (base colour) — add a light and faces get different shades. + add(&mut p, cid, LayerSource::Light { kind: LightKind::Parallel }, |_| {}); + let lit = render(&p, cid); + let mut shades: Vec = lit.data.iter().filter(|q| q[3] > 0.99).map(|q| (q[0] * 20.0) as i32).collect(); + shades.sort(); + shades.dedup(); + assert!(shades.len() >= 3, "faces are shaded differently: {shades:?}"); + // Classic 3D draws no meshes. + p.comp_mut(cid).unwrap().renderer = R3::Classic3D; + assert!(render(&p, cid).data.iter().all(|q| q[3] == 0.0)); +} + +#[test] +fn shadows_darken_the_receiver() { + let (mut p, cid) = project(200, 150); + // A floor plane facing the camera, a sphere in front of it, a spot light behind the camera. + add(&mut p, cid, LayerSource::Primitive { kind: PrimitiveKind::Plane }, |l| { + set(l, "geometryOptions/width", Value::Scalar(400.0)); + set(l, "geometryOptions/height", Value::Scalar(400.0)); + set(l, "transform/position", Value::Vec3([100.0, 75.0, 200.0])); + set(l, "materialOptions/baseColor", Value::Color([1.0, 1.0, 1.0, 1.0])); + }); + add(&mut p, cid, LayerSource::Primitive { kind: PrimitiveKind::Sphere }, |l| { + set(l, "geometryOptions/radius", Value::Scalar(30.0)); + set(l, "transform/position", Value::Vec3([100.0, 75.0, 50.0])); + set(l, "materialOptions/castsShadows", Value::Enum(2)); + }); + add(&mut p, cid, LayerSource::Light { kind: LightKind::Point }, |l| { + set(l, "transform/position", Value::Vec3([100.0, 75.0, -300.0])); + set(l, "lightOptions/castsShadows", Value::Bool(true)); + }); + let img = render(&p, cid); + // The sphere casts only (invisible); its shadow lands on the plane behind it. + let mid = img.data[75 * 200 + 100]; + let side = img.data[75 * 200 + 20]; + assert!(mid[3] > 0.99 && side[3] > 0.99); + assert!(mid[0] < side[0] * 0.5, "shadowed centre {mid:?} vs lit side {side:?}"); + // Without shadow casting the centre is lit. + let l = &mut p.comp_mut(cid).unwrap().layers[0]; + set(l, "lightOptions/castsShadows", Value::Bool(false)); + let img = render(&p, cid); + assert!(img.data[75 * 200 + 100][0] > side[0] * 0.8); +} + +#[test] +fn cards_and_environment_light() { + let (mut p, cid) = project(120, 90); + let sid = p.add_item( + "S", + Default::default(), + None, + ItemKind::Solid(effectcraft_project::Solid { color: [0.5, 0.5, 0.5], width: 60, height: 40, pixel_aspect: 1.0 }), + ); + add(&mut p, cid, LayerSource::Solid { item: sid }, |l| l.switches.three_d = true); + let unlit = render(&p, cid); + let c = unlit.data[20 * 120 + 30]; + assert!((c[0] - 0.5).abs() < 0.01 && c[3] > 0.99, "unlit card keeps its colour: {c:?}"); + assert_eq!(unlit.data[80 * 120 + 110][3], 0.0); + // An Environment light sourcing a white environment layer brightens the card. + let env = p.add_item( + "E", + Default::default(), + None, + ItemKind::Solid(effectcraft_project::Solid { color: [1.0, 1.0, 1.0], width: 64, height: 32, pixel_aspect: 1.0 }), + ); + add(&mut p, cid, LayerSource::Solid { item: env }, |l| { + l.environment = true; + l.switches.three_d = true; + }); + add(&mut p, cid, LayerSource::Light { kind: LightKind::Environment }, |_| {}); + let lit = render(&p, cid); + let c2 = lit.data[20 * 120 + 30]; + assert!(c2[0] > 0.3, "{c2:?}"); + // The environment layer itself is not drawn. + assert_eq!(lit.data[80 * 120 + 110][3], 0.0); +} + +#[test] +fn depth_of_field_blurs_out_of_focus_surfaces() { + let mut img = Image::new(64, 64); + for y in 0..64 { + for x in 0..64 { + let v = if (x / 4 + y / 4) % 2 == 0 { 1.0 } else { 0.0 }; + img.data[y * 64 + x] = [v, v, v, 1.0]; + } + } + let var = |im: &Image| { + let m: f32 = im.data.iter().map(|p| p[0]).sum::() / im.data.len() as f32; + im.data.iter().map(|p| (p[0] - m).powi(2)).sum::() / im.data.len() as f32 + }; + let dof = crate::three_d::camera::Dof { focus: 1000.0, aperture: 40.0, blur_level: 1.0 }; + let sharp = depth_of_field(&img, &vec![1000.0; 64 * 64], &dof, 1.0); + assert!((var(&sharp) - var(&img)).abs() < 1e-6, "in focus: unchanged"); + let blurred = depth_of_field(&img, &vec![3000.0; 64 * 64], &dof, 1.0); + assert!(var(&blurred) < var(&img) * 0.3, "out of focus: blurred"); +} + +#[test] +fn extruded_text_renders_as_a_mesh() { + let (mut p, cid) = project(200, 100); + add(&mut p, cid, LayerSource::Text, |l| { + l.switches.three_d = true; + let doc = effectcraft_keyframe::TextDoc { text: "IO".into(), size: 60.0, fill: [1.0, 1.0, 1.0, 1.0], apply_fill: true, ..Default::default() }; + set(l, "text/sourceText", Value::Text(Box::new(doc))); + let mut next = 10_000u64; + l.props.children.push(build::extrusion_geometry_options(&mut build::Ids(&mut next)).into()); + set(l, "geometryOptions/extrusionDepth", Value::Scalar(30.0)); + set(l, "geometryOptions/bevelStyle", Value::Enum(1)); + set(l, "transform/rotationY", Value::Scalar(40.0)); + }); + let ctx = crate::EvalCtx { project: &p, comp_id: cid, comp: p.comp(cid).unwrap(), time: Tick::ZERO, expr: None }; + let l = &ctx.comp.layers[0]; + let params = scene::extrusion_params(&ctx, l).unwrap(); + let meshes = scene::extruded_meshes(&ctx, l, ¶ms); + assert!(!meshes.is_empty()); + assert!(meshes.iter().all(|(m, _)| m.positions.iter().any(|q| (q[2] - 30.0).abs() < 1e-3))); + let img = render(&p, cid); + assert!(img.data.iter().filter(|q| q[3] > 0.5).count() > 200); +} diff --git a/crates/render/src/three_d/compose.rs b/crates/render/src/three_d/compose.rs index 1ef7412b..e6fc9209 100644 --- a/crates/render/src/three_d/compose.rs +++ b/crates/render/src/three_d/compose.rs @@ -323,6 +323,12 @@ pub(crate) fn draw_run(r: &Renderer, ctx: &EvalCtx, run: &[&Layer], canvas: &mut if run.is_empty() { return; } + // Advanced 3D comps: meshes, physically based lights, shadow maps (collapsed precomps keep + // the parent's Classic 3D planes). + if ctx.comp.renderer == effectcraft_project::Renderer::Advanced3D && r.collapse3d.is_none() { + super::adv::draw_run(r, ctx, run, canvas); + return; + } let out = (canvas.width, canvas.height); // A collapsed precomp's 3D layers are lit by the outer comp's lights. let lights = scene_lights(&r.collapse3d.map_or(*ctx, |c| c.parent)); diff --git a/crates/render/src/three_d/light.rs b/crates/render/src/three_d/light.rs index 9476f98d..5a7963b4 100644 --- a/crates/render/src/three_d/light.rs +++ b/crates/render/src/three_d/light.rs @@ -117,7 +117,18 @@ impl LightState { /// All lights switched on and active at the context time. pub fn lights_at(ctx: &EvalCtx) -> Vec { - ctx.comp.layers.iter().filter(|l| l.is_light() && l.switches.video && l.is_active_at(ctx.time)).filter_map(|l| LightState::from_layer(ctx, l)).collect() + // Environment lights only light Advanced 3D comps (image-based light). + ctx.comp + .layers + .iter() + .filter(|l| { + l.is_light() + && l.switches.video + && l.is_active_at(ctx.time) + && l.source != (effectcraft_project::LayerSource::Light { kind: LightKind::Environment }) + }) + .filter_map(|l| LightState::from_layer(ctx, l)) + .collect() } /// Material Options of a 3D layer. diff --git a/crates/render/src/three_d/mod.rs b/crates/render/src/three_d/mod.rs index 8bef7360..e97c67fe 100644 --- a/crates/render/src/three_d/mod.rs +++ b/crates/render/src/three_d/mod.rs @@ -5,6 +5,7 @@ //! consecutive 3D layers is handed to [`compose::draw_run`] as a group (2D layers break 3D //! groups, as in After Effects). +pub mod adv; pub mod camera; pub(crate) mod compose; pub mod light; From 9a11c577f3617740876153880a3c9a86f5e6deba Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:28:37 -0400 Subject: [PATCH 09/23] M9.11: 3D Channel effects on auxiliary channels Aux-channel model (effectcraft_raster::channels3d: depth, object/material ID, normals, UV, coverage, Cryptomatte ranks + manifests, MurmurHash3); EffectHost::aux; multi-layer OpenEXR channels via the exr crate (FootageSource::aux); the compositor's depth / layer-ID / Cryptomatte pass (Renderer::comp_aux) so the effects work on precomps of 3D comps. 3D Channel Extract, Cryptomatte, Depth Matte, Depth of Field, EXtractoR, Fog 3D, ID Matte, IDentifier. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 3 + Cargo.toml | 1 + crates/effects/src/channel3d.rs | 681 +++++++++++++++++++++++++++ crates/effects/src/lib.rs | 11 +- crates/media/Cargo.toml | 2 + crates/media/src/exr_channels.rs | 152 ++++++ crates/media/src/lib.rs | 1 + crates/media/src/pool.rs | 12 + crates/raster/src/channels3d.rs | 206 ++++++++ crates/raster/src/lib.rs | 2 + crates/render/src/lib.rs | 38 ++ crates/render/src/tests_aux.rs | 98 ++++ crates/render/src/three_d/compose.rs | 179 +++++++ 13 files changed, 1385 insertions(+), 1 deletion(-) create mode 100644 crates/effects/src/channel3d.rs create mode 100644 crates/media/src/exr_channels.rs create mode 100644 crates/raster/src/channels3d.rs create mode 100644 crates/render/src/tests_aux.rs diff --git a/Cargo.lock b/Cargo.lock index 46f34e63..0394589c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1327,6 +1327,7 @@ dependencies = [ "effectcraft-raster", "effectcraft-render", "effectcraft-time", + "exr", "filmcraft-codecs", "filmcraft-color", "filmcraft-frame", @@ -1335,6 +1336,7 @@ dependencies = [ "image", "log", "rayon", + "serde_json", "thiserror 2.0.21", ] @@ -1768,6 +1770,7 @@ dependencies = [ "miniz_oxide 0.8.9", "num-complex", "pulp", + "rayon-core", "smallvec", "zune-inflate", ] diff --git a/Cargo.toml b/Cargo.toml index a1941cf4..61dd6d2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ kurbo = { version = "0.13", features = ["serde"] } tiny-skia = { version = "0.12", default-features = false, features = ["std", "simd"] } skrifa = { version = "0.44", default-features = false, features = ["std"] } harfrust = { version = "0.12", default-features = false, features = ["std"] } +exr = "1.74" image = { version = "0.25", default-features = false, features = ["png", "jpeg"] } # media codecs + containers: FilmCraft's pure-Rust crates, pinned to a revision (plan/adr/0001). diff --git a/crates/effects/src/channel3d.rs b/crates/effects/src/channel3d.rs new file mode 100644 index 00000000..5fd4d715 --- /dev/null +++ b/crates/effects/src/channel3d.rs @@ -0,0 +1,681 @@ +//! 3D Channel effects: 3D Channel Extract, Cryptomatte, Depth Matte, Depth of Field, EXtractoR, +//! Fog 3D, ID Matte and IDentifier. +//! +//! They read the layer's auxiliary channels ([`AuxChannels`], via +//! [`crate::EffectHost::aux`]): multi-layer OpenEXR footage (depth, object/material IDs, +//! Cryptomatte layers, any named channel) or the compositor's depth / layer-ID / Cryptomatte +//! pass for a precomp of a 3D comp. Without auxiliary channels they pass the layer through +//! (EXtractoR still maps the layer's own R, G, B and A). +//! +//! Aux pixels are looked up nearest-neighbour (IDs must not be interpolated) at each buffer +//! pixel's layer position, so render scale and layer padding are respected. + +use std::sync::Arc; + +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, crypto_hash}; +use effectcraft_raster::{AuxChannels, Image, Px, gaussian_blur}; +use rayon::prelude::*; + +use crate::util::{Plane, gauss_plane, hash1, premul, unpremul}; +use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider}; + +fn spec(id: &'static str, name: &'static str, params: Vec, render: crate::RenderFn) -> EffectSpec { + EffectSpec { id, name, category: "3D Channel", params, render, gpu: false, float: true } +} + +fn aux(ctx: &EffectCtx) -> Option> { + ctx.env.host?.aux() +} + +/// Aux index for each buffer pixel (None outside the aux image). +fn index_map(b: &Buf, a: &AuxChannels) -> Vec> { + let (w, h) = (b.img.width as usize, b.img.height as usize); + let inv = 1.0 / b.scale.max(1e-9); + (0..w * h) + .into_par_iter() + .map(|i| { + let (x, y) = ((i % w) as f64 + 0.5, (i / w) as f64 + 0.5); + a.index_at((x - b.offset[0]) * inv, (y - b.offset[1]) * inv) + }) + .collect() +} + +/// A channel sampled into the buffer grid (`fill` outside). +fn plane_of(b: &Buf, idx: &[Option], ch: &[f32], fill: f32) -> Plane { + Plane { w: b.img.width as usize, h: b.img.height as usize, data: idx.par_iter().map(|i| i.map_or(fill, |i| ch[i])).collect() } +} + +/// Distinct colour for an ID (hash → hue). +fn id_color(id: f32) -> [f32; 3] { + let h = hash1(id.to_bits(), 0x1d, 0x5eed); + let (r, g, b) = effectcraft_color::hsl_to_rgb(h, 0.75, 0.5); + if id == 0.0 { [0.0; 3] } else { [r, g, b] } +} + +// ---------------------------------------------------------------- 3D Channel Extract + +pub const EXTRACT_CHANNELS: [&str; 8] = ["Z-Depth", "Object ID", "Texture UV", "Surface Normals", "Coverage", "Background RGB", "Unclamped RGB", "Material ID"]; + +fn channel_extract(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(a) = aux(ctx) else { return b }; + let idx = index_map(&b, &a); + let (bp, wp) = (ctx.params.f("blackPoint") as f32, ctx.params.f("whitePoint") as f32); + let lv = |v: f32| ((v - bp) / (wp - bp).abs().max(1e-9) * (wp - bp).signum()).clamp(0.0, 1.0); + let lv = |v: f32| if wp >= bp { lv(v) } else { 1.0 - ((v - wp) / (bp - wp).max(1e-9)).clamp(0.0, 1.0) }; + let get3 = |names: [&[&str]; 3]| -> Option<[&[f32]; 3]> { Some([a.find(names[0])?, a.find(names[1])?, a.find(names[2])?]) }; + let img: Option = match ctx.params.e("channel") { + 0 => a.depth().map(|z| map_grey(&b, &idx, |i| lv(z[i]))), + 1 => a.object_id().map(|c| map_grey(&b, &idx, |i| lv(c[i]))), + 7 => a.material_id().map(|c| map_grey(&b, &idx, |i| lv(c[i]))), + 4 => a.coverage().map(|c| map_grey(&b, &idx, |i| lv(c[i]))), + 2 => get3([&["UV.U", "U", "uv.x"], &["UV.V", "V", "uv.y"], &["UV.W", "W", "uv.z"]]) + .or_else(|| Some([a.find(&["UV.U", "U", "uv.x"])?, a.find(&["UV.V", "V", "uv.y"])?, a.find(&["UV.V", "V", "uv.y"])?])) + .map(|c| map_rgb(&b, &idx, |i| [lv(c[0][i]), lv(c[1][i]), 0.0])), + 3 => get3([&["N.X", "Normal.X", "normals.x"], &["N.Y", "Normal.Y", "normals.y"], &["N.Z", "Normal.Z", "normals.z"]]) + .map(|c| map_rgb(&b, &idx, |i| [lv(c[0][i] * 0.5 + 0.5), lv(c[1][i] * 0.5 + 0.5), lv(c[2][i] * 0.5 + 0.5)])), + 5 => get3([&["BG.R", "Background.R"], &["BG.G", "Background.G"], &["BG.B", "Background.B"]]) + .map(|c| map_rgb(&b, &idx, |i| [lv(c[0][i]), lv(c[1][i]), lv(c[2][i])])), + _ => get3([&["R"], &["G"], &["B"]]).map(|c| map_rgb(&b, &idx, |i| [c[0][i], c[1][i], c[2][i]])), + }; + if let Some(i) = img { + b.img = i; + } + b +} + +fn map_grey(b: &Buf, idx: &[Option], f: impl Fn(usize) -> f32 + Sync) -> Image { + map_rgb(b, idx, |i| { + let v = f(i); + [v, v, v] + }) +} + +fn map_rgb(b: &Buf, idx: &[Option], f: impl Fn(usize) -> [f32; 3] + Sync) -> Image { + let mut img = Image::new(b.img.width, b.img.height); + img.data.par_iter_mut().zip(idx.par_iter()).for_each(|(p, i)| { + if let Some(i) = i { + let c = f(*i); + *p = [c[0], c[1], c[2], 1.0]; + } + }); + img +} + +// ---------------------------------------------------------------- Depth Matte + +fn depth_matte(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(a) = aux(ctx) else { return b }; + let Some(z) = a.depth() else { return b }; + let idx = index_map(&b, &a); + let depth = ctx.params.f("depth") as f32; + let feather = ctx.params.f("feather").max(0.0) as f32; + let invert = ctx.params.b("invert"); + // Keeps what lies at or beyond Depth (Invert keeps what is in front), with a soft ramp of + // `feather` depth units centred on the threshold. + let zp = plane_of(&b, &idx, z, BACKGROUND_DEPTH); + b.img.data.par_iter_mut().zip(zp.data.par_iter()).for_each(|(px, &zv)| { + let k = if feather > 0.0 { + ((zv - depth) / feather + 0.5).clamp(0.0, 1.0) + } else if zv >= depth { + 1.0 + } else { + 0.0 + }; + let k = if invert { 1.0 - k } else { k }; + *px = px.map(|v| v * k); + }); + b +} + +// ---------------------------------------------------------------- Depth of Field + +/// Blur radius (pixels) for depth `z`. +pub fn dof_radius(z: f32, focal: f32, thickness: f32, max_r: f32, bias: f32) -> f32 { + let d = ((z - focal).abs() - thickness * 0.5).max(0.0); + let range = focal.abs().max(1.0); + let n = (d / range).min(1.0); + // Bias 50 = linear; higher bias brings blur in sooner. + let gamma = 2f32.powf((50.0 - bias) / 25.0); + max_r * n.powf(gamma) +} + +fn depth_of_field(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(a) = aux(ctx) else { return b }; + let Some(z) = a.depth() else { return b }; + let max_r = (ctx.params.f("maximumRadius").max(0.0) * b.scale) as f32; + if max_r <= 0.0 { + return b; + } + let focal = ctx.params.f("focalPlane") as f32; + let thick = ctx.params.f("focalPlaneThickness").max(0.0) as f32; + let bias = ctx.params.f("focalBias") as f32; + let idx = index_map(&b, &a); + let zp = plane_of(&b, &idx, z, BACKGROUND_DEPTH); + // A stack of blurs at a few radii, interpolated per pixel by its circle of confusion. + const LEVELS: usize = 5; + let stack: Vec = (0..LEVELS) + .into_par_iter() + .map(|k| { + let r = max_r * k as f32 / (LEVELS - 1) as f32; + if r < 0.3 { b.img.clone() } else { gaussian_blur(&b.img, (r / 2.0) as f64, (r / 2.0) as f64, true) } + }) + .collect(); + b.img.data.par_iter_mut().enumerate().for_each(|(i, px)| { + let r = dof_radius(zp.data[i], focal, thick, max_r, bias); + let f = (r / max_r * (LEVELS - 1) as f32).clamp(0.0, (LEVELS - 1) as f32); + let k0 = (f.floor() as usize).min(LEVELS - 2); + let t = f - k0 as f32; + let (a0, a1) = (stack[k0].data[i], stack[k0 + 1].data[i]); + *px = [0, 1, 2, 3].map(|c| a0[c] + (a1[c] - a0[c]) * t); + }); + b +} + +// ---------------------------------------------------------------- Fog 3D + +fn fog_3d(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(a) = aux(ctx) else { return b }; + let Some(z) = a.depth() else { return b }; + let pr = ctx.params; + let fc = pr.color("fogColor"); + let (start, end) = (pr.f("fogStartDepth") as f32, pr.f("fogEndDepth") as f32); + let opacity = (pr.f("fogOpacity") / 100.0) as f32; + let density = (pr.f("scatteringDensity") / 100.0) as f32; + let foggy_bg = pr.b("foggyBackground"); + let contrib = (pr.f("layerContribution") / 100.0) as f32; + let grad = if contrib > 0.0 { ctx.layer_param("gradientLayer", true).map(|o| crate::util::fit_layer(ctx, &b, &o, true)) } else { None }; + let idx = index_map(&b, &a); + let zp = plane_of(&b, &idx, z, BACKGROUND_DEPTH); + b.img.data.par_iter_mut().enumerate().for_each(|(i, px)| { + let zv = zp.data[i]; + let bg = zv >= BACKGROUND_DEPTH * 0.5; + let mut f = if bg { + if foggy_bg { 1.0 } else { 0.0 } + } else { + let t = ((zv - start) / (end - start).abs().max(1e-6)).clamp(0.0, 1.0); + // Denser scattering thickens the fog sooner. + 1.0 - (1.0 - t).powf(1.0 + density * 3.0) + }; + if let Some(g) = &grad { + let (gc, ga) = unpremul(g.data[i]); + let l = effectcraft_color::luminance(gc[0], gc[1], gc[2]) * ga; + f *= 1.0 - contrib + contrib * l; + } + let f = (f * opacity).clamp(0.0, 1.0); + let (c, al) = unpremul(*px); + let al2 = if bg && foggy_bg { al + (1.0 - al) * f } else { al }; + let out = [0, 1, 2].map(|k| c[k] + (fc[k] - c[k]) * f); + *px = premul(out, al2); + }); + b +} + +// ---------------------------------------------------------------- ID Matte / IDentifier + +fn id_channel(a: &AuxChannels, which: u32) -> Option<&[f32]> { + if which == 1 { a.material_id() } else { a.object_id() } +} + +fn id_matte(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(a) = aux(ctx) else { return b }; + let Some(ids) = id_channel(&a, ctx.params.e("auxChannel")) else { return b }; + let idx = index_map(&b, &a); + let sel = ctx.params.f("idSelection").round() as f32; + let cov = if ctx.params.b("useCoverage") { a.coverage() } else { None }; + let mut m = Plane { + w: b.img.width as usize, + h: b.img.height as usize, + data: idx.par_iter().map(|i| i.map_or(0.0, |i| if ids[i].round() == sel { cov.map_or(1.0, |c| c[i].clamp(0.0, 1.0)) } else { 0.0 })).collect(), + }; + let feather = ctx.params.f("feather").max(0.0) * b.scale; + if feather > 0.0 { + m = gauss_plane(&m, feather * 0.5, feather * 0.5); + } + let invert = ctx.params.b("invert"); + b.img.data.par_iter_mut().zip(m.data.par_iter()).for_each(|(px, &k)| { + let k = if invert { 1.0 - k } else { k }; + *px = px.map(|v| v * k); + }); + b +} + +fn identifier(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(a) = aux(ctx) else { return b }; + let Some(ids) = id_channel(&a, ctx.params.e("channelType")) else { return b }; + let idx = index_map(&b, &a); + let sel = ctx.params.f("id").round() as f32; + let display = ctx.params.e("display"); + let max_id = ids.iter().cloned().fold(1.0f32, f32::max); + let src = b.img.clone(); + b.img.data.par_iter_mut().enumerate().for_each(|(i, px)| { + let Some(k) = idx[i] else { + if display != 2 { + *px = [0.0; 4]; + } + return; + }; + let id = ids[k].round(); + let hit = id == sel; + *px = match display { + // Colors: every ID in its own colour, the selected one highlighted white. + 0 => { + let c = if hit && sel != 0.0 { [1.0; 3] } else { id_color(id) }; + [c[0], c[1], c[2], 1.0] + } + 1 => { + let v = if hit { 1.0 } else { 0.0 }; + [v, v, v, 1.0] + } + 2 => { + let s = src.data[i]; + if hit { s } else { [0.0; 4] } + } + _ => { + let v = id / max_id; + [v, v, v, 1.0] + } + }; + }); + b +} + +// ---------------------------------------------------------------- Cryptomatte + +/// Does `name` match a selection pattern (`*` wildcards, case-sensitive like the spec)? +fn wild(pat: &str, name: &str) -> bool { + match pat.split_once('*') { + None => pat == name, + Some((pre, rest)) => { + let Some(tail) = name.strip_prefix(pre) else { return false }; + if rest.is_empty() { + return true; + } + (0..=tail.len()).filter(|&k| tail.is_char_boundary(k)).any(|k| wild(rest, &tail[k..])) + } + } +} + +/// Parse a selection: comma-separated names (optionally quoted) or `` hashes / wildcards. +pub fn selection_hashes(sel: &str, manifest: Option<&[(String, u32)]>) -> Vec { + let mut out = vec![]; + for tok in sel.split(',') { + let t = tok.trim().trim_matches('"').trim_matches('\''); + if t.is_empty() { + continue; + } + if t.contains('*') { + if let Some(m) = manifest { + out.extend(m.iter().filter(|(n, _)| wild(t, n)).map(|(_, h)| *h)); + } + continue; + } + if let Some(hex) = t.strip_prefix("<").and_then(|r| r.strip_suffix(">")) + && let Ok(h) = u32::from_str_radix(hex, 16) + { + out.push(h); + continue; + } + out.push(manifest.and_then(|m| m.iter().find(|(n, _)| n == t).map(|(_, h)| *h)).unwrap_or_else(|| crypto_hash(t))); + } + out +} + +pub const CRYPTO_LAYERS: [&str; 3] = ["CryptoObject", "CryptoMaterial", "CryptoAsset"]; + +fn cryptomatte(ctx: &EffectCtx, mut b: Buf) -> Buf { + let Some(a) = aux(ctx) else { return b }; + let layers = a.crypto_layers(); + if layers.is_empty() { + return b; + } + let want = CRYPTO_LAYERS[(ctx.params.e("layer") as usize).min(2)]; + let layer = layers.iter().find(|l| l.as_str() == want || l.ends_with(want)).unwrap_or(&layers[0]).clone(); + let manifest = a.manifests.iter().find(|(l, _)| *l == layer).map(|(_, m)| m.as_slice()); + let sel = selection_hashes(ctx.params.s("selection"), manifest); + let idx = index_map(&b, &a); + let display = ctx.params.e("display"); + let matte_only = ctx.params.b("matteOnly"); + let src = b.img.clone(); + b.img.data.par_iter_mut().enumerate().for_each(|(i, px)| { + let ranks = idx[i].map(|k| a.crypto_ranks(&layer, k)).unwrap_or_default(); + let m: f32 = ranks.iter().filter(|(id, c)| *c > 0.0 && sel.contains(&id.to_bits())).map(|(_, c)| *c).sum::().clamp(0.0, 1.0); + if matte_only { + *px = [m, m, m, m]; + return; + } + *px = match display { + // Colors: coverage-weighted ID colours (the spec's preview), selection brighter. + 0 => { + let mut c = [0.0f32; 3]; + for (id, cov) in &ranks { + let k = id_color(*id); + for j in 0..3 { + c[j] += k[j] * cov; + } + } + let c = c.map(|v| v * (1.0 - 0.5 * m) + 0.5 * m); + [c[0], c[1], c[2], 1.0] + } + // Matte: the layer keyed by the selection. + 1 => src.data[i].map(|v| v * m), + // Preview: the layer with the selection tinted. + _ => { + let s = src.data[i]; + [s[0] * (1.0 - 0.5 * m) + 0.5 * m * s[3], s[1] * (1.0 - 0.5 * m) + 0.5 * m * s[3], s[2] * (1.0 - 0.5 * m), s[3]] + } + }; + }); + b +} + +// ---------------------------------------------------------------- EXtractoR + +fn extractor(ctx: &EffectCtx, mut b: Buf) -> Buf { + let a = aux(ctx); + let names = [ctx.params.s("red"), ctx.params.s("green"), ctx.params.s("blue"), ctx.params.s("alpha")]; + let (bp, wp) = (ctx.params.f("blackPoint") as f32, ctx.params.f("whitePoint") as f32); + let unmult = ctx.params.b("unMult"); + let clip = ctx.params.b("clip"); + let idx = a.as_ref().map(|a| index_map(&b, a)); + // Each output channel: an aux channel by name, or the layer's own R/G/B/A. + let own = |n: &str| match n.trim().to_ascii_uppercase().as_str() { + "R" | "RED" => Some(0), + "G" | "GREEN" => Some(1), + "B" | "BLUE" => Some(2), + "A" | "ALPHA" => Some(3), + "" => None, + _ => None, + }; + let src_ch: Vec> = names.iter().map(|n| a.as_ref().and_then(|a| if n.trim().is_empty() { None } else { a.get(n.trim()) })).collect(); + if src_ch.iter().all(Option::is_none) && bp == 0.0 && wp == 1.0 && !unmult && names.iter().enumerate().all(|(k, n)| own(n) == Some(k)) { + return b; + } + let src = b.img.clone(); + let lv = |v: f32| { + let o = (v - bp) / (wp - bp).abs().max(1e-9) * (wp - bp).signum(); + if clip { o.clamp(0.0, 1.0) } else { o } + }; + b.img.data.par_iter_mut().enumerate().for_each(|(i, px)| { + let (sc, sa) = unpremul(src.data[i]); + let straight = [sc[0], sc[1], sc[2], sa]; + let mut o: Px = [0.0; 4]; + for k in 0..4 { + o[k] = match (src_ch[k], idx.as_ref().and_then(|m| m[i])) { + (Some(ch), Some(j)) => ch[j], + (Some(_), None) => 0.0, + (None, _) => match own(names[k]) { + Some(c) => straight[c], + None => { + if k == 3 { + 1.0 + } else { + 0.0 + } + } + }, + }; + if k < 3 { + o[k] = lv(o[k]); + } + } + let al = o[3].clamp(0.0, 1.0); + let c = if unmult && al > 1e-6 { [o[0] / al, o[1] / al, o[2] / al] } else { [o[0], o[1], o[2]] }; + *px = premul(c, al); + }); + b +} + +pub fn specs() -> Vec { + let text = |id: &'static str, name: &'static str, d: &str| p(id, name, Value::Str(d.into()), ParamUi::Text); + vec![ + spec( + "ec.3d.channelextract", + "3D Channel Extract", + vec![ + p("channel", "3D Channel", Value::Enum(0), popup(&EXTRACT_CHANNELS)), + p("blackPoint", "Black Point", num(0.0), slider(-1.0e6, 1.0e6, -10000.0, 10000.0, 2)), + p("whitePoint", "White Point", num(10000.0), slider(-1.0e6, 1.0e6, -10000.0, 10000.0, 2)), + ], + channel_extract, + ), + spec( + "ec.3d.cryptomatte", + "Cryptomatte", + vec![ + p("layer", "Layer", Value::Enum(0), popup(&CRYPTO_LAYERS)), + text("selection", "Selection", ""), + p("display", "Display", Value::Enum(2), popup(&["Colors", "Matte", "Preview"])), + p("matteOnly", "Matte Only", Value::Bool(false), ParamUi::Checkbox), + ], + cryptomatte, + ), + spec( + "ec.3d.depthmatte", + "Depth Matte", + vec![ + p("depth", "Depth", num(0.0), slider(-1.0e6, 1.0e6, -10000.0, 10000.0, 2)), + p("feather", "Feather", num(0.0), slider(0.0, 1.0e6, 0.0, 1000.0, 2)), + p("invert", "Invert", Value::Bool(false), ParamUi::Checkbox), + ], + depth_matte, + ), + spec( + "ec.3d.depthoffield", + "Depth of Field", + vec![ + p("focalPlane", "Focal Plane", num(0.0), slider(-1.0e6, 1.0e6, 0.0, 10000.0, 2)), + p("maximumRadius", "Maximum Radius", num(0.0), slider(0.0, 500.0, 0.0, 50.0, 2)), + p("focalPlaneThickness", "Focal Plane Thickness", num(0.0), slider(0.0, 1.0e6, 0.0, 1000.0, 2)), + p("focalBias", "Focal Bias", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 2)), + ], + depth_of_field, + ), + spec( + "ec.3d.extractor", + "EXtractoR", + vec![ + text("red", "Red", "R"), + text("green", "Green", "G"), + text("blue", "Blue", "B"), + text("alpha", "Alpha", "A"), + p("blackPoint", "Black Point", num(0.0), slider(-1.0e6, 1.0e6, -1.0, 2.0, 3)), + p("whitePoint", "White Point", num(1.0), slider(-1.0e6, 1.0e6, -1.0, 2.0, 3)), + p("unMult", "UnMult", Value::Bool(false), ParamUi::Checkbox), + p("clip", "Clip", Value::Bool(false), ParamUi::Checkbox), + ], + extractor, + ), + spec( + "ec.3d.fog3d", + "Fog 3D", + vec![ + p("fogColor", "Fog Color", col(1.0, 1.0, 1.0), ParamUi::Color), + p("fogStartDepth", "Fog Start Depth", num(0.0), slider(-1.0e6, 1.0e6, 0.0, 10000.0, 2)), + p("fogEndDepth", "Fog End Depth", num(3000.0), slider(-1.0e6, 1.0e6, 0.0, 10000.0, 2)), + p("fogOpacity", "Fog Opacity", num(100.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("scatteringDensity", "Scattering Density", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + p("foggyBackground", "Foggy Background", Value::Bool(true), ParamUi::Checkbox), + p("gradientLayer", "Gradient Layer", Value::Layer(None), ParamUi::Layer), + p("layerContribution", "Layer Contribution", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 1)), + ], + fog_3d, + ), + spec( + "ec.3d.idmatte", + "ID Matte", + vec![ + p("auxChannel", "Aux. Channel", Value::Enum(0), popup(&["Object ID", "Material ID"])), + p("idSelection", "ID Selection", num(0.0), slider(0.0, 65535.0, 0.0, 100.0, 0)), + p("feather", "Feather", num(0.0), slider(0.0, 100.0, 0.0, 10.0, 2)), + p("invert", "Invert", Value::Bool(false), ParamUi::Checkbox), + p("useCoverage", "Use Coverage", Value::Bool(false), ParamUi::Checkbox), + ], + id_matte, + ), + spec( + "ec.3d.identifier", + "IDentifier", + vec![ + p("channelType", "Channel Type", Value::Enum(0), popup(&["Object ID", "Material ID"])), + p("display", "Display", Value::Enum(0), popup(&["Colors", "Luma Matte", "Alpha Matte", "Raw"])), + p("id", "ID", num(0.0), slider(0.0, 65535.0, 0.0, 100.0, 0)), + ], + identifier, + ), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, EffectHost, LayerPixels, run_fx}; + use effectcraft_raster::channels3d::crypto_float; + + /// A 20×10 layer: left half object 1 at depth 100, right half object 2 at depth 500; the + /// top-right corner is background. Cryptomatte: "left" / "right". + struct AuxHost(Arc); + impl EffectHost for AuxHost { + fn layer(&self, _: u64, _: bool) -> Option { + None + } + fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option> { + None + } + fn aux(&self) -> Option> { + Some(self.0.clone()) + } + } + + fn scene() -> AuxHost { + let (w, h) = (20usize, 10usize); + let mut a = AuxChannels::new(w as u32, h as u32, 1.0); + let (mut z, mut id, mut r, mut g) = (vec![0.0; w * h], vec![0.0; w * h], vec![0.0; w * h], vec![0.0; w * h]); + let (hl, hr) = (crypto_hash("left"), crypto_hash("right")); + for y in 0..h { + for x in 0..w { + let i = y * w + x; + if x >= 15 && y < 3 { + z[i] = BACKGROUND_DEPTH; + } else if x < 10 { + z[i] = 100.0; + id[i] = 1.0; + r[i] = crypto_float(hl); + g[i] = 1.0; + } else { + z[i] = 500.0; + id[i] = 2.0; + r[i] = crypto_float(hr); + g[i] = 1.0; + } + } + } + a.channels = vec![("Z".into(), z), ("ObjectID".into(), id), ("CryptoObject00.R".into(), r), ("CryptoObject00.G".into(), g)]; + a.manifests = vec![("CryptoObject".into(), vec![("left".into(), hl), ("right".into(), hr)])]; + AuxHost(Arc::new(a)) + } + + fn img() -> Image { + Image::filled(20, 10, [0.5, 0.5, 0.5, 1.0]) + } + + fn run(id: &str, vals: &[(&str, Value)], host: &AuxHost) -> Image { + run_fx(id, vals, img(), 0.0, EffectEnv { host: Some(host), ..Default::default() }).img + } + + #[test] + fn passes_through_without_aux_channels() { + for s in specs() { + let out = run_fx(s.id, &[], img(), 0.0, EffectEnv::default()); + assert_eq!(out.img.data, img().data, "{}", s.id); + } + } + + #[test] + fn depth_matte_thresholds() { + let h = scene(); + let o = run("ec.3d.depthmatte", &[("depth", num(300.0))], &h); + assert_eq!(o.get(2, 5)[3], 0.0); + assert_eq!(o.get(12, 5)[3], 1.0); + let inv = run("ec.3d.depthmatte", &[("depth", num(300.0)), ("invert", Value::Bool(true))], &h); + assert_eq!(inv.get(2, 5)[3], 1.0); + assert_eq!(inv.get(12, 5)[3], 0.0); + // Feather: a ramp of 800 units centred on 300 → depth 100 at 0.25, 500 at 0.75. + let f = run("ec.3d.depthmatte", &[("depth", num(300.0)), ("feather", num(800.0))], &h); + assert!((f.get(2, 5)[3] - 0.25).abs() < 1e-5 && (f.get(12, 5)[3] - 0.75).abs() < 1e-5); + } + + #[test] + fn channel_extract_maps_depth_and_ids() { + let h = scene(); + let o = run("ec.3d.channelextract", &[("blackPoint", num(0.0)), ("whitePoint", num(1000.0))], &h); + assert!((o.get(2, 5)[0] - 0.1).abs() < 1e-6 && (o.get(12, 5)[0] - 0.5).abs() < 1e-6); + assert_eq!(o.get(17, 1)[0], 1.0); + let ids = run("ec.3d.channelextract", &[("channel", Value::Enum(1)), ("whitePoint", num(2.0))], &h); + assert_eq!(ids.get(2, 5)[0], 0.5); + } + + #[test] + fn id_matte_and_identifier() { + let h = scene(); + let o = run("ec.3d.idmatte", &[("idSelection", num(2.0))], &h); + assert_eq!(o.get(2, 5)[3], 0.0); + assert_eq!(o.get(12, 5)[3], 1.0); + let l = run("ec.3d.identifier", &[("display", Value::Enum(1)), ("id", num(1.0))], &h); + assert_eq!(l.get(2, 5)[0], 1.0); + assert_eq!(l.get(12, 5)[0], 0.0); + let c = run("ec.3d.identifier", &[], &h); + assert_ne!(c.get(2, 5), c.get(12, 5)); + } + + #[test] + fn cryptomatte_selects_by_name_and_wildcard() { + let h = scene(); + let m = run("ec.3d.cryptomatte", &[("selection", Value::Str("right".into())), ("matteOnly", Value::Bool(true))], &h); + assert_eq!(m.get(2, 5)[3], 0.0); + assert_eq!(m.get(12, 5)[3], 1.0); + let w = run("ec.3d.cryptomatte", &[("selection", Value::Str("l*".into())), ("display", Value::Enum(1))], &h); + assert_eq!(w.get(2, 5)[3], 1.0); + assert_eq!(w.get(12, 5)[3], 0.0); + assert!(wild("*ight", "right") && wild("r*t", "right") && !wild("x*", "right")); + let hex = format!("<{:08x}>", crypto_hash("left")); + assert_eq!(selection_hashes(&hex, None), vec![crypto_hash("left")]); + } + + #[test] + fn fog_and_depth_of_field_follow_depth() { + let h = scene(); + let f = run("ec.3d.fog3d", &[("fogColor", col(1.0, 0.0, 0.0)), ("fogEndDepth", num(1000.0)), ("scatteringDensity", num(0.0))], &h); + // Linear fog: 10 % at depth 100, 50 % at 500, full on the background. + assert!((f.get(2, 5)[0] - 0.55).abs() < 1e-4, "{:?}", f.get(2, 5)); + assert!((f.get(12, 5)[0] - 0.75).abs() < 1e-4); + assert!((f.get(17, 1)[0] - 1.0).abs() < 1e-4 && f.get(17, 1)[1] < 1e-4); + assert!((dof_radius(100.0, 100.0, 0.0, 10.0, 50.0)).abs() < 1e-6); + assert!((dof_radius(200.0, 100.0, 0.0, 10.0, 50.0) - 10.0).abs() < 1e-6); + assert_eq!(dof_radius(140.0, 100.0, 100.0, 10.0, 50.0), 0.0); + // A sharp edge at the far object blurs when focused near. + let mut pic = Image::new(20, 10); + for y in 0..10 { + for x in 0..20 { + let v = if x % 2 == 0 { 1.0 } else { 0.0 }; + pic.set(x, y, [v, v, v, 1.0]); + } + } + let env = EffectEnv { host: Some(&h), ..Default::default() }; + let d = run_fx("ec.3d.depthoffield", &[("focalPlane", num(100.0)), ("maximumRadius", num(4.0))], pic.clone(), 0.0, env).img; + assert_eq!(d.get(4, 5), pic.get(4, 5)); + assert!((d.get(12, 5)[0] - 0.5).abs() < 0.3 && d.get(12, 5) != pic.get(12, 5)); + } + + #[test] + fn extractor_maps_named_channels() { + let h = scene(); + let o = run("ec.3d.extractor", &[("red", Value::Str("Z".into())), ("whitePoint", num(1000.0))], &h); + assert!((o.get(2, 5)[0] - 0.1).abs() < 1e-6); + assert!((o.get(2, 5)[1] - 0.0005).abs() < 1e-6); + // Without aux: the layer's own channels can be swapped. + let s = run_fx("ec.3d.extractor", &[("red", Value::Str("A".into()))], Image::filled(2, 2, [0.2, 0.3, 0.4, 1.0]), 0.0, EffectEnv::default()); + assert_eq!(s.img.get(0, 0), [1.0, 0.3, 0.4, 1.0]); + } +} diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 2f6a463e..c4666b94 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -11,6 +11,7 @@ mod blur2; mod blur3; mod channel; mod channel2; +mod channel3d; mod color2; mod color3; mod color_fx; @@ -57,7 +58,7 @@ pub use color2::Curve; use effectcraft_keyframe::Value; use effectcraft_project::build::Ids; use effectcraft_project::{GroupKind, ParamUi, PropGroup, Property}; -pub use effectcraft_raster::Image; +pub use effectcraft_raster::{AuxChannels, Image}; /// Effect categories in Effects & Presets order. pub const CATEGORIES: &[&str] = &[ @@ -238,6 +239,13 @@ pub trait EffectHost: Sync { fn layer_at(&self, _id: u64, _comp_time: f64, _masks_and_effects: bool) -> Option { None } + /// Auxiliary 3D channels (depth, object / material IDs, normals, Cryptomatte, any named + /// EXR channel) of the effect's own layer source at the current time, in layer space (see + /// [`AuxChannels`]). `None` when the source has none (the 3D Channel effects then pass the + /// layer through). + fn aux(&self) -> Option> { + None + } } /// Extra context the renderer may supply (all optional; `Default` is "nothing known"). @@ -366,6 +374,7 @@ pub fn registry() -> &'static [EffectSpec] { v.extend(keylight::specs()); v.extend(mocha_shape::specs()); v.extend(sim3::specs()); + v.extend(channel3d::specs()); v.extend(time_fx::specs()); v.extend(audio_fx::specs()); v.extend(paint::specs()); diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml index 89992569..9466a6cd 100644 --- a/crates/media/Cargo.toml +++ b/crates/media/Cargo.toml @@ -19,7 +19,9 @@ filmcraft-media = { workspace = true } filmcraft-frame = { workspace = true } filmcraft-color = { workspace = true } filmcraft-time = { workspace = true } +exr = { workspace = true } image = { workspace = true, features = ["gif", "webp", "tiff", "bmp", "exr"] } rayon = { workspace = true } +serde_json = { workspace = true } thiserror = { workspace = true } log = { workspace = true } diff --git a/crates/media/src/exr_channels.rs b/crates/media/src/exr_channels.rs new file mode 100644 index 00000000..ba0034c3 --- /dev/null +++ b/crates/media/src/exr_channels.rs @@ -0,0 +1,152 @@ +//! Auxiliary channels from multi-layer OpenEXR files (depth, object / material IDs, normals, +//! Cryptomatte, …) for the 3D Channel effects, read with the pure-Rust `exr` crate. +//! +//! Every channel of every layer becomes a plane named `layer.channel` (or just `channel` for +//! the unnamed layer), placed in the display window (pixels outside a layer's data window are +//! 0, or the background depth for depth channels). Cryptomatte manifests come from the +//! `cryptomatte//name` and `cryptomatte//manifest` header attributes (the published +//! Cryptomatte metadata convention: the manifest is a JSON object of name → hex hash). + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, OnceLock}; + +use effectcraft_raster::AuxChannels; +use effectcraft_raster::channels3d::BACKGROUND_DEPTH; + +fn is_depth(name: &str) -> bool { + let last = name.rsplit('.').next().unwrap_or(name); + last.eq_ignore_ascii_case("z") || name.to_ascii_lowercase().contains("depth") +} + +/// Parse a Cryptomatte manifest (`{"name": "hexhash", …}`). +pub fn parse_manifest(json: &str) -> Vec<(String, u32)> { + let Ok(serde_json::Value::Object(m)) = serde_json::from_str::(json) else { return vec![] }; + let mut v: Vec<(String, u32)> = m.iter().filter_map(|(k, h)| Some((k.clone(), u32::from_str_radix(h.as_str()?.trim(), 16).ok()?))).collect(); + v.sort(); + v +} + +/// Read all channels of an EXR file's bytes. `None` when it is not a readable EXR. +pub fn read_exr_channels(bytes: &[u8]) -> Option { + use exr::prelude::*; + let img = read().no_deep_data().largest_resolution_level().all_channels().all_layers().all_attributes().from_buffered(std::io::Cursor::new(bytes)).ok()?; + let dw = img.attributes.display_window; + let (w, h) = (dw.size.0, dw.size.1); + if w == 0 || h == 0 { + return None; + } + let mut aux = AuxChannels::new(w as u32, h as u32, 1.0); + let mut crypto: HashMap, Option)> = HashMap::new(); + let mut collect_meta = |other: &HashMap| { + for (k, v) in other { + let key = k.to_string(); + let Some(rest) = key.strip_prefix("cryptomatte/") else { continue }; + let Some((id, field)) = rest.split_once('/') else { continue }; + let AttributeValue::Text(t) = v else { continue }; + let e = crypto.entry(id.to_string()).or_default(); + match field { + "name" => e.0 = Some(t.to_string()), + "manifest" => e.1 = Some(t.to_string()), + _ => {} + } + } + }; + collect_meta(&img.attributes.other); + for layer in img.layer_data.iter() { + collect_meta(&layer.attributes.other); + let prefix = layer.attributes.layer_name.as_ref().map(|t| t.to_string()).filter(|s| !s.is_empty()); + let (lw, lh) = (layer.size.0, layer.size.1); + let pos = layer.attributes.layer_position; + let (ox, oy) = (pos.0 as i64 - dw.position.0 as i64, pos.1 as i64 - dw.position.1 as i64); + for ch in layer.channel_data.list.iter() { + let name = match &prefix { + Some(p) => format!("{p}.{}", ch.name), + None => ch.name.to_string(), + }; + let fill = if is_depth(&name) { BACKGROUND_DEPTH } else { 0.0 }; + let mut plane = vec![fill; w * h]; + let vals: Vec = ch.sample_data.values_as_f32().collect(); + if vals.len() < lw * lh { + continue; + } + for y in 0..lh { + let ty = y as i64 + oy; + if ty < 0 || ty >= h as i64 { + continue; + } + for x in 0..lw { + let tx = x as i64 + ox; + if tx < 0 || tx >= w as i64 { + continue; + } + plane[ty as usize * w + tx as usize] = vals[y * lw + x]; + } + } + aux.channels.push((name, plane)); + } + } + for (_, (name, manifest)) in crypto { + if let (Some(n), Some(m)) = (name, manifest) { + aux.manifests.push((n, parse_manifest(&m))); + } + } + aux.manifests.sort(); + Some(aux) +} + +type Cache = Mutex>)>>; + +/// Read (and cache, a few files) the channels of the EXR at `path` via `read`. +pub(crate) fn cached(path: &str, read: impl FnOnce() -> Option>) -> Option> { + static CACHE: OnceLock = OnceLock::new(); + let cache = CACHE.get_or_init(|| Mutex::new(Vec::new())); + if let Some(v) = cache.lock().ok().and_then(|c| c.iter().find(|(p, _)| p == path).map(|(_, v)| v.clone())) { + return v; + } + let v = read().and_then(|b| read_exr_channels(&b)).map(Arc::new); + if let Ok(mut c) = cache.lock() { + if c.len() >= 4 { + c.remove(0); + } + c.push((path.to_string(), v.clone())); + } + v +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_layered_channels_and_cryptomatte_metadata() { + use exr::prelude::*; + let (w, h) = (4usize, 2usize); + let z: Vec = (0..w * h).map(|i| i as f32 * 10.0).collect(); + let id: Vec = (0..w * h).map(|i| (i % 2) as f32 + 1.0).collect(); + let r: Vec = vec![0.5; w * h]; + let channels = AnyChannels::sort( + vec![ + AnyChannel::new("R", FlatSamples::F32(r)), + AnyChannel::new("Z", FlatSamples::F32(z.clone())), + AnyChannel::new("ObjectID", FlatSamples::F32(id.clone())), + ] + .into(), + ); + let mut layer = Layer::new((w, h), LayerAttributes::named("depth"), Encoding::FAST_LOSSLESS, channels); + layer.attributes.other.insert(Text::from("cryptomatte/abc1234/name"), AttributeValue::Text(Text::from("CryptoObject"))); + layer + .attributes + .other + .insert(Text::from("cryptomatte/abc1234/manifest"), AttributeValue::Text(Text::from(r#"{"bunny":"13851a76","floor":"0a1b2c3d"}"#))); + let image = Image::from_layer(layer); + let mut bytes = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut bytes).unwrap(); + let aux = read_exr_channels(bytes.get_ref()).unwrap(); + assert_eq!((aux.width, aux.height), (4, 2)); + assert_eq!(aux.get("depth.Z").unwrap(), z.as_slice()); + assert_eq!(aux.depth().unwrap(), z.as_slice()); + assert_eq!(aux.object_id().unwrap(), id.as_slice()); + assert_eq!(aux.manifests, vec![("CryptoObject".to_string(), vec![("bunny".to_string(), 0x1385_1a76), ("floor".to_string(), 0x0a1b_2c3d)])]); + assert!(read_exr_channels(b"not an exr").is_none()); + } +} diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index 29546f7e..aa4a8f49 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -13,6 +13,7 @@ //! (PNG, JPEG, GIF, WebP, TIFF, BMP, OpenEXR). mod convert; +pub mod exr_channels; mod pool; mod probe; diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index b4cc51ef..caa0c4f9 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -470,6 +470,18 @@ impl FootageSource for MediaPool { } } + fn aux(&self, _item: ItemId, footage: &Footage, t: Tick) -> Option> { + if footage.missing || !footage.has_video { + return None; + } + let loc = Inner::locate(footage, t); + if loc.media_t.is_some() || !loc.key.path.to_ascii_lowercase().ends_with(".exr") { + return None; + } + let path = loc.key.path.clone(); + crate::exr_channels::cached(&path, || self.inner.read(&path).ok()) + } + fn audio(&self, _item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option> { if !footage.has_audio || footage.missing { return None; diff --git a/crates/raster/src/channels3d.rs b/crates/raster/src/channels3d.rs new file mode 100644 index 00000000..0402349b --- /dev/null +++ b/crates/raster/src/channels3d.rs @@ -0,0 +1,206 @@ +//! Auxiliary (3D) channels that travel with a layer's pixels: depth, object / material IDs, +//! surface normals, texture UVs, coverage and Cryptomatte ID/coverage pairs. +//! +//! They come from multi-layer OpenEXR footage (any channel by its full `layer.channel` name) +//! or from the compositor's own render passes for precomps of 3D comps. The 3D Channel effects +//! read them through `EffectHost::aux`. +//! +//! Conventions (shared by the EXR reader and the render pass): +//! +//! * channel planes are row-major `width × height` `f32`s covering the layer's source rectangle, +//! with `scale` aux pixels per layer pixel; +//! * depth is camera-space distance in pixels (`Z`), background `BACKGROUND_DEPTH`; +//! * `ObjectID` / `MaterialID` are small integers (0 = background); +//! * Cryptomatte layers follow the published Cryptomatte specification (Psyop, 2015): a layer +//! named `CryptoObject` stores rank pairs in `CryptoObject00.R/G/B/A`, `CryptoObject01.R/…` +//! (ID float, coverage, ID float, coverage, …), IDs are MurmurHash3 (x86, 32-bit, seed 0) of +//! the object name reinterpreted as a float with the exponent nudged off 0 and 255, and the +//! manifest maps names to hashes. + +/// Depth written where nothing was rendered. +pub const BACKGROUND_DEPTH: f32 = 1.0e10; + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct AuxChannels { + pub width: u32, + pub height: u32, + /// Aux pixels per layer pixel. + pub scale: f64, + /// Named planes (`Z`, `ObjectID`, `CryptoObject00.R`, …). + pub channels: Vec<(String, Vec)>, + /// Cryptomatte manifests: (layer name, [(object name, hash)]). + pub manifests: Vec<(String, Vec<(String, u32)>)>, +} + +impl AuxChannels { + pub fn new(width: u32, height: u32, scale: f64) -> AuxChannels { + AuxChannels { width, height, scale, channels: vec![], manifests: vec![] } + } + + /// A channel by exact name (case-insensitive). + pub fn get(&self, name: &str) -> Option<&[f32]> { + self.channels.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, v)| v.as_slice()) + } + + /// The first channel present among `names` (also matching `*.name` suffixes, so `Z` finds + /// `depth.Z`). + pub fn find(&self, names: &[&str]) -> Option<&[f32]> { + for n in names { + if let Some(v) = self.get(n) { + return Some(v); + } + } + for n in names { + let suffix = format!(".{}", n.to_ascii_lowercase()); + if let Some((_, v)) = self.channels.iter().find(|(c, _)| c.to_ascii_lowercase().ends_with(&suffix)) { + return Some(v); + } + } + None + } + + pub fn names(&self) -> Vec<&str> { + self.channels.iter().map(|(n, _)| n.as_str()).collect() + } + + pub fn depth(&self) -> Option<&[f32]> { + self.find(&["Z", "depth", "Depth", "zDepth", "Z-Depth"]) + } + pub fn object_id(&self) -> Option<&[f32]> { + self.find(&["ObjectID", "object_id", "objectId", "id"]) + } + pub fn material_id(&self) -> Option<&[f32]> { + self.find(&["MaterialID", "material_id", "materialId"]) + } + pub fn coverage(&self) -> Option<&[f32]> { + self.find(&["Coverage", "coverage"]) + } + + /// Cryptomatte layer names present (prefixes with a `00.R` channel). + pub fn crypto_layers(&self) -> Vec { + let mut v: Vec = + self.channels.iter().filter_map(|(n, _)| n.strip_suffix("00.R").or_else(|| n.strip_suffix("00.r")).map(str::to_string)).collect(); + v.sort(); + v.dedup(); + v + } + + /// Rank pairs (id, coverage) of Cryptomatte layer `layer` at aux pixel index `i`. + pub fn crypto_ranks(&self, layer: &str, i: usize) -> Vec<(f32, f32)> { + let mut out = vec![]; + for k in 0.. { + let (r, g, b, a) = ( + self.get(&format!("{layer}{k:02}.R")), + self.get(&format!("{layer}{k:02}.G")), + self.get(&format!("{layer}{k:02}.B")), + self.get(&format!("{layer}{k:02}.A")), + ); + let (Some(r), Some(g)) = (r, g) else { break }; + out.push((r[i], g[i])); + if let (Some(b), Some(a)) = (b, a) { + out.push((b[i], a[i])); + } + } + out + } + + /// Aux pixel index of a layer-space point (nearest; `None` outside). + #[inline] + pub fn index_at(&self, lx: f64, ly: f64) -> Option { + let x = (lx * self.scale).floor(); + let y = (ly * self.scale).floor(); + if x < 0.0 || y < 0.0 || x >= self.width as f64 || y >= self.height as f64 { + return None; + } + Some(y as usize * self.width as usize + x as usize) + } + + /// Manifest entry name for a hash in Cryptomatte layer `layer`. + pub fn manifest_name(&self, layer: &str, hash: u32) -> Option<&str> { + self.manifests.iter().find(|(l, _)| l == layer)?.1.iter().find(|(_, h)| *h == hash).map(|(n, _)| n.as_str()) + } +} + +/// MurmurHash3 x86 32-bit (public-domain algorithm by Austin Appleby). +pub fn murmur3_32(data: &[u8], seed: u32) -> u32 { + const C1: u32 = 0xcc9e_2d51; + const C2: u32 = 0x1b87_3593; + let mut h = seed; + let chunks = data.chunks_exact(4); + let tail = chunks.remainder(); + for c in chunks { + let mut k = u32::from_le_bytes([c[0], c[1], c[2], c[3]]); + k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); + h ^= k; + h = h.rotate_left(13).wrapping_mul(5).wrapping_add(0xe654_6b64); + } + let mut k = 0u32; + for (i, b) in tail.iter().enumerate() { + k ^= (*b as u32) << (8 * i); + } + if !tail.is_empty() { + k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); + h ^= k; + } + h ^= data.len() as u32; + h ^= h >> 16; + h = h.wrapping_mul(0x85eb_ca6b); + h ^= h >> 13; + h = h.wrapping_mul(0xc2b2_ae35); + h ^= h >> 16; + h +} + +/// Cryptomatte hash of a name (the manifest value). +pub fn crypto_hash(name: &str) -> u32 { + let h = murmur3_32(name.as_bytes(), 0); + // Keep the float finite and normal: flip the low exponent bit when it is all 0s or 1s. + let e = (h >> 23) & 0xff; + if e == 0 || e == 255 { h ^ (1 << 23) } else { h } +} + +/// The ID float stored in Cryptomatte channels for a hash. +pub fn crypto_float(hash: u32) -> f32 { + f32::from_bits(hash) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn murmur3_reference_values() { + // Published MurmurHash3_x86_32 test vectors. + assert_eq!(murmur3_32(b"", 0), 0); + assert_eq!(murmur3_32(b"", 1), 0x514e_28b7); + assert_eq!(murmur3_32(b"", 0xffff_ffff), 0x81f1_6f39); + assert_eq!(murmur3_32(&[0, 0, 0, 0], 0), 0x2362_f9de); + assert_eq!(murmur3_32(b"a", 0x9747_b28c), 0x7fa0_9ea6); + assert_eq!(murmur3_32(b"aaaa", 0x9747_b28c), 0x5a97_808a); + assert_eq!(murmur3_32(b"Hello, world!", 0x9747_b28c), 0x2488_4cba); + assert_eq!(murmur3_32(b"The quick brown fox jumps over the lazy dog", 0x9747_b28c), 0x2fa8_26cd); + } + + #[test] + fn crypto_floats_are_normal_and_round_trip() { + for n in ["bunny", "set", "ground", "a", ""] { + let h = crypto_hash(n); + let f = crypto_float(h); + assert!(f.is_finite() && (f == 0.0 || f.is_normal() || f.abs() > 0.0), "{n}"); + assert_eq!(f.to_bits(), h); + } + } + + #[test] + fn lookup_helpers() { + let mut a = AuxChannels::new(2, 1, 1.0); + a.channels.push(("depth.Z".into(), vec![5.0, 6.0])); + a.channels.push(("CryptoObject00.R".into(), vec![1.0, 2.0])); + a.channels.push(("CryptoObject00.G".into(), vec![0.5, 1.0])); + assert_eq!(a.depth(), Some(&[5.0, 6.0][..])); + assert_eq!(a.crypto_layers(), vec!["CryptoObject".to_string()]); + assert_eq!(a.crypto_ranks("CryptoObject", 0), vec![(1.0, 0.5)]); + assert_eq!(a.index_at(1.5, 0.2), Some(1)); + assert_eq!(a.index_at(2.5, 0.2), None); + } +} diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rs index dfe53848..4f244199 100644 --- a/crates/raster/src/lib.rs +++ b/crates/raster/src/lib.rs @@ -4,6 +4,7 @@ //! heavy operation is row-parallel with rayon. pub mod blur; +pub mod channels3d; pub mod flow; pub mod warp; @@ -11,6 +12,7 @@ use effectcraft_color::{BlendMode, blend_pixel}; use rayon::prelude::*; pub use blur::{box_blur, directional_blur, gaussian_blur, radial_blur}; +pub use channels3d::AuxChannels; pub use warp::{Sampling, WarpOpts, accumulate_warp, composite_warp, composite_warp_reference, resample}; pub type Px = [f32; 4]; diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index bd42d31a..f09f1a6e 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -39,6 +39,12 @@ pub trait FootageSource: Send + Sync { fn audio(&self, _item: ItemId, _footage: &Footage, _t: Tick, _frames: usize, _rate: u32) -> Option> { None } + /// Auxiliary 3D channels of `item` at source time `t` (multi-layer OpenEXR: depth, IDs, + /// Cryptomatte, any named channel), with `scale` relative to the file's pixels. `None` when + /// the footage has none. + fn aux(&self, _item: ItemId, _footage: &Footage, _t: Tick) -> Option> { + None + } /// Set the decoded-frame cache budget in bytes (Settings ▸ Memory & CPU); sources without /// a cache ignore it. fn set_cache_budget(&self, _bytes: usize) {} @@ -107,6 +113,29 @@ impl EffectHost for FxHost<'_, '_, '_> { sub.layer_input(&self.ctx.at(t), self.layer, n).map(|b| (*b).clone()) } + fn aux(&self) -> Option> { + match &self.layer.source { + LayerSource::Footage { item } => { + let ItemKind::Footage(f) = &self.r.project.item(*item)?.kind else { return None }; + let a = self.r.footage.aux(*item, f, self.ctx.source_time(self.layer))?; + // Aux pixels per layer pixel (the layer is the footage at its nominal size). + if f.width > 0 && a.width > 0 { + let mut a = (*a).clone(); + a.scale = a.width as f64 / f.width as f64; + return Some(Arc::new(a)); + } + Some(a) + } + LayerSource::Comp { item } => { + if self.r.depth > MAX_FX_DEPTH || self.r.project.comp_contains(*item, self.ctx.comp_id) { + return None; + } + self.r.nested().comp_aux(*item, self.ctx.source_time(self.layer)).map(Arc::new) + } + _ => None, + } + } + fn layer_at(&self, id: u64, comp_time: f64, masks_and_effects: bool) -> Option { let other = self.ctx.layer(effectcraft_project::LayerId(id))?; if other.id == self.layer.id || self.r.depth > MAX_FX_DEPTH { @@ -1175,6 +1204,13 @@ impl<'a> Renderer<'a> { self.depth } + /// The auxiliary 3D channels of comp `comp_id` at comp time `t` (depth, layer IDs, normals, + /// UVs, Cryptomatte; see `three_d::compose::aux_pass`), at the renderer's output scale. + pub fn comp_aux(&self, comp_id: ItemId, t: Tick) -> Option { + let ctx = self.eval_ctx(comp_id, t)?; + Some(three_d::compose::aux_pass(self, &ctx)) + } + /// Evaluation context of comp `comp_id` at comp time `t`. pub fn eval_ctx(&self, comp_id: ItemId, t: Tick) -> Option> { Some(self.ctx(comp_id, self.project.comp(comp_id)?, t)) @@ -1295,6 +1331,8 @@ pub fn kurbo_path(sp: &effectcraft_keyframe::ShapePath) -> kurbo::BezPath { #[cfg(test)] mod tests_audio_fx; #[cfg(test)] +mod tests_aux; +#[cfg(test)] mod tests_collapse; #[cfg(test)] mod tests_color; diff --git a/crates/render/src/tests_aux.rs b/crates/render/src/tests_aux.rs new file mode 100644 index 00000000..31bb1881 --- /dev/null +++ b/crates/render/src/tests_aux.rs @@ -0,0 +1,98 @@ +//! Auxiliary 3D channels: the comp's depth / ID / Cryptomatte pass and the 3D Channel effects on +//! a precomp of a 3D comp. + +use effectcraft_color::Label; +use effectcraft_keyframe::Value; +use effectcraft_project::build::{self, Ids}; +use effectcraft_project::{Comp, ItemId, ItemKind, Layer, LayerSource, Project, Solid}; +use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, crypto_hash}; +use effectcraft_time::{FrameRate, Tick}; + +use crate::{NoFootage, RenderOpts, Renderer, render_frame}; + +const W: u32 = 200; +const H: u32 = 100; + +fn zoom() -> f64 { + W as f64 * 50.0 / 36.0 +} + +fn solid3(p: &mut Project, comp: &Comp, name: &str, color: [f32; 3], pos: [f64; 3]) -> Layer { + let sid = p.add_item(name, Label::Red, None, ItemKind::Solid(Solid { color, width: 60, height: 60, pixel_aspect: 1.0 })); + let mut l = build::layer(p, comp, name, LayerSource::Solid { item: sid }, (60, 60), None); + l.name = name.to_string(); + l.switches.three_d = true; + l.props.prop_mut("transform/position").unwrap().value = Value::Vec3(pos); + l +} + +/// Inner 3D comp: "Near" (red, z 0, left) above "Far" (green, z 500, right). Outer comp: the +/// precomp layer, optionally with an effect. +fn scene(effect: Option<(&str, Vec<(&str, Value)>)>) -> (Project, ItemId, ItemId) { + let mut p = Project::default(); + p.settings.bit_depth = effectcraft_project::BitDepth::Bpc32; + let inner = Comp::new(W, H, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); + let iid = p.add_item("Inner", Label::Sandstone, None, ItemKind::Comp(inner.clone().into())); + let near = solid3(&mut p, &inner, "Near", [1.0, 0.0, 0.0], [60.0, 50.0, 0.0]); + let far = solid3(&mut p, &inner, "Far", [0.0, 1.0, 0.0], [140.0, 50.0, 500.0]); + p.comp_mut(iid).unwrap().layers.push(near); + p.comp_mut(iid).unwrap().layers.push(far); + let outer = Comp::new(W, H, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); + let oid = p.add_item("Outer", Label::Sandstone, None, ItemKind::Comp(outer.clone().into())); + let mut pre = build::layer(&mut p, &outer, "Inner", LayerSource::Comp { item: iid }, (W, H), None); + if let Some((id, vals)) = effect { + let spec = effectcraft_effects::find(id).unwrap(); + let mut next = p.next_id; + let mut g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [W as f64, H as f64]); + p.next_id = next; + for (k, v) in vals { + g.prop_mut(k).unwrap().value = v.clone(); + } + pre.props.sub_mut("effects").unwrap().children.push(g.into()); + } + p.comp_mut(oid).unwrap().layers.push(pre); + (p, iid, oid) +} + +#[test] +fn comp_aux_pass_has_depth_ids_and_cryptomatte() { + let (p, iid, _) = scene(None); + let r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + let a = r.comp_aux(iid, Tick::ZERO).unwrap(); + assert_eq!((a.width, a.height), (W, H)); + let at = |name: &str, x: usize, y: usize| a.get(name).unwrap()[y * W as usize + x]; + // Near plane at the camera's zoom distance, the far one 500 px behind it. + assert!((at("Z", 60, 50) as f64 - zoom()).abs() < 1e-2, "{}", at("Z", 60, 50)); + assert!((at("Z", 114, 50) as f64 - (zoom() + 500.0)).abs() < 1e-2, "{}", at("Z", 114, 50)); + assert_eq!(at("Z", 5, 5), BACKGROUND_DEPTH); + assert_eq!((at("ObjectID", 60, 50), at("ObjectID", 114, 50), at("ObjectID", 5, 5)), (1.0, 2.0, 0.0)); + assert_eq!(at("Coverage", 60, 50), 1.0); + // Planes face the camera. + assert!((at("N.Z", 60, 50) + 1.0).abs() < 1e-5); + // Cryptomatte rank 0 = the layer under the pixel, full coverage; manifest lists names. + assert_eq!(at("CryptoObject00.R", 60, 50).to_bits(), crypto_hash("Near")); + assert_eq!(at("CryptoObject00.G", 60, 50), 1.0); + assert_eq!(a.manifest_name("CryptoObject", crypto_hash("Far")), Some("Far")); +} + +#[test] +fn three_d_channel_effects_work_on_a_precomp_of_a_3d_comp() { + let near = (60, 50); + let far = (114, 50); + let alpha = |p: &Project, oid: ItemId, (x, y): (i64, i64)| render_frame(p, oid, Tick::ZERO, 1.0).get(x, y)[3]; + let (p, _, oid) = scene(Some(("ec.3d.idmatte", vec![("idSelection", Value::Scalar(2.0))]))); + assert_eq!(alpha(&p, oid, near), 0.0); + assert!(alpha(&p, oid, far) > 0.99); + let (p, _, oid) = scene(Some(("ec.3d.depthmatte", vec![("depth", Value::Scalar(zoom() + 250.0))]))); + assert_eq!(alpha(&p, oid, near), 0.0); + assert!(alpha(&p, oid, far) > 0.99); + let (p, _, oid) = scene(Some(("ec.3d.cryptomatte", vec![("selection", Value::Str("Near".into())), ("display", Value::Enum(1))]))); + assert!(alpha(&p, oid, near) > 0.99); + assert_eq!(alpha(&p, oid, far), 0.0); + // Half resolution: the pass follows the render scale. + let (p, iid, _) = scene(None); + let r = Renderer::new(&p, &NoFootage, RenderOpts { scale: 0.5, ..Default::default() }); + let a = r.comp_aux(iid, Tick::ZERO).unwrap(); + assert_eq!((a.width, a.height, a.scale), (W / 2, H / 2, 0.5)); + assert_eq!(a.get("ObjectID").unwrap()[25 * 100 + 30], 1.0); +} diff --git a/crates/render/src/three_d/compose.rs b/crates/render/src/three_d/compose.rs index 1ef7412b..8f8e6ff5 100644 --- a/crates/render/src/three_d/compose.rs +++ b/crates/render/src/three_d/compose.rs @@ -583,3 +583,182 @@ fn occlusion(c: &Item, p: Vec3, l: &LightState) -> [f32; 3] { } out } + +// ------------------------------------------------------------------ auxiliary render pass + +/// One surface seen through an output pixel (aux pass). +#[derive(Clone, Copy)] +struct AuxFrag { + z: f32, + a: f32, + obj: f32, + mat: f32, + n: [f32; 3], + uv: [f32; 2], + obj_hash: u32, + mat_hash: u32, +} + +/// The comp's auxiliary channels at the context time, at output resolution: camera-space depth +/// (`Z`), `ObjectID` (layer number), `MaterialID` (source item id), `Coverage`, camera-space +/// normals (`N.X/Y/Z`), texture coordinates (`UV.U/V`) and Cryptomatte `CryptoObject` (layer +/// names) / `CryptoMaterial` (source names) with two ranks each, plus their manifests. +/// +/// Layers are gathered in stack order (top first); a run of 3D layers contributes its planes +/// sorted near → far at each pixel, a 2D layer its coverage on the plane of the comp. The +/// front-most surface with ≥ 50 % opacity (else the strongest one) provides depth, IDs, normal +/// and UV; Cryptomatte coverage is each surface's visible share (`α · Π(1 − α_front)`). +pub(crate) fn aux_pass(r: &Renderer, ctx: &EvalCtx) -> effectcraft_raster::AuxChannels { + use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, crypto_float, crypto_hash}; + let s = r.opts.scale; + let (w, h) = (((ctx.comp.width as f64 * s).round() as u32).max(1), ((ctx.comp.height as f64 * s).round() as u32).max(1)); + let out = (w, h); + let (wu, n) = (w as usize, w as usize * h as usize); + let cam = camera_for(r, ctx); + let visible: Vec<&Layer> = r.visible_layers(ctx).into_iter().filter(|l| !l.switches.adjustment).collect(); + let number = |l: &Layer| ctx.comp.layers.iter().position(|x| x.id == l.id).map_or(0.0, |i| i as f32 + 1.0); + let source_name = |l: &Layer| l.source.item().and_then(|i| r.project.item(i)).map(|it| it.name.clone()).unwrap_or_else(|| l.name.clone()); + let source_id = |l: &Layer| l.source.item().map_or(0.0, |i| i.0 as f32); + let mut manifest_obj: Vec<(String, u32)> = vec![]; + let mut manifest_mat: Vec<(String, u32)> = vec![]; + for l in &visible { + let (on, mn) = (l.name.clone(), source_name(l)); + if !manifest_obj.iter().any(|(x, _)| *x == on) { + manifest_obj.push((on.clone(), crypto_hash(&on))); + } + if !manifest_mat.iter().any(|(x, _)| *x == mn) { + manifest_mat.push((mn.clone(), crypto_hash(&mn))); + } + } + // Groups in stack order: runs of 3D layers, single 2D layers. + let mut groups: Vec<&[&Layer]> = vec![]; + let mut i = 0; + while i < visible.len() { + let mut j = i + 1; + if visible[i].is_3d() { + while j < visible.len() && visible[j].is_3d() { + j += 1; + } + } + groups.push(&visible[i..j]); + i = j; + } + let mut frags: Vec> = vec![Vec::new(); n]; + let plane_z = cam.depth(vec3(ctx.comp.width as f64 * 0.5, ctx.comp.height as f64 * 0.5, 0.0)) as f32; + for g in groups.iter().rev() { + if g[0].is_3d() { + let items: Vec = g.iter().enumerate().flat_map(|(k, l)| prepare_all(r, ctx, l, k, out, true)).collect(); + let meta: Vec<(f32, f32, u32, u32, [f32; 3], [f64; 2])> = items + .iter() + .map(|it| { + let nrm = it.geos.first().map_or(vec3(0.0, 0.0, -1.0), |gm| cam.view.apply_vec(gm.normal)); + let mut nv = [nrm.x as f32, nrm.y as f32, nrm.z as f32]; + if nv[2] > 0.0 { + nv = nv.map(|v| -v); + } + let (sw, sh) = crate::source_size(r.project, it.layer); + let size = if sw == 0 { [ctx.comp.width as f64, ctx.comp.height as f64] } else { [sw as f64, sh as f64] }; + (number(it.layer), source_id(it.layer), crypto_hash(&it.layer.name), crypto_hash(&source_name(it.layer)), nv, size) + }) + .collect(); + frags.par_chunks_mut(wu).enumerate().for_each(|(y, row)| { + let yi = y as i64; + let mut local: Vec = Vec::new(); + for (x, cell) in row.iter_mut().enumerate() { + let xi = x as i64; + local.clear(); + for (it, m) in items.iter().zip(&meta) { + if xi < it.bbox[0] || xi >= it.bbox[2] || yi < it.bbox[1] || yi >= it.bbox[3] { + continue; + } + let Some(gm) = it.geos.first() else { continue }; + let Some((u, v, z)) = unproject(gm, x as f64 + 0.5, y as f64 + 0.5) else { continue }; + let a = it.texel(u, v)[3] * it.opacity; + if a > 1e-4 { + let uv = [(u / m.5[0]) as f32, (v / m.5[1]) as f32]; + local.push(AuxFrag { z: z as f32, a: a.min(1.0), obj: m.0, mat: m.1, n: m.4, uv, obj_hash: m.2, mat_hash: m.3 }); + } + } + local.sort_by(|p, q| p.z.total_cmp(&q.z)); + cell.extend_from_slice(&local); + } + }); + } else { + let l = g[0]; + let mut img = Image::new(w, h); + if let Some(b) = r.layer_buf(ctx, l) { + r.place(ctx, l, &b, &mut img, BlendMode::Normal, ctx.opacity(l) as f32); + } + let (obj, mat, oh, mh) = (number(l), source_id(l), crypto_hash(&l.name), crypto_hash(&source_name(l))); + frags.par_chunks_mut(wu).zip(img.data.par_chunks(wu)).enumerate().for_each(|(y, (row, px))| { + for (x, (cell, p)) in row.iter_mut().zip(px).enumerate() { + if p[3] > 1e-4 { + let uv = [(x as f32 + 0.5) / w as f32, (y as f32 + 0.5) / h as f32]; + cell.push(AuxFrag { z: plane_z, a: p[3].min(1.0), obj, mat, n: [0.0, 0.0, -1.0], uv, obj_hash: oh, mat_hash: mh }); + } + } + }); + } + } + // Resolve per pixel. + const NAMES: [&str; 9] = ["Z", "ObjectID", "MaterialID", "Coverage", "N.X", "N.Y", "N.Z", "UV.U", "UV.V"]; + const CRYPTO: usize = 16; + let resolved: Vec<([f32; 9], [f32; CRYPTO])> = frags + .par_iter() + .map(|fs| { + let mut o = [BACKGROUND_DEPTH, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; + let mut crypto = [0.0f32; CRYPTO]; + if fs.is_empty() { + return (o, crypto); + } + let mut t = 1.0f32; + let mut obj: Vec<(u32, f32)> = vec![]; + let mut mat: Vec<(u32, f32)> = vec![]; + let mut best: Option<(usize, f32)> = None; + let mut front: Option = None; + for (k, f) in fs.iter().enumerate() { + let c = f.a * t; + if front.is_none() && f.a >= 0.5 { + front = Some(k); + } + if best.is_none_or(|(_, bc)| c > bc) { + best = Some((k, c)); + } + for (list, hsh) in [(&mut obj, f.obj_hash), (&mut mat, f.mat_hash)] { + match list.iter_mut().find(|(x, _)| *x == hsh) { + Some(e) => e.1 += c, + None => list.push((hsh, c)), + } + } + t *= 1.0 - f.a; + if t <= 1e-5 { + break; + } + } + let f = fs[front.or(best.map(|b| b.0)).unwrap_or(0)]; + o = [f.z, f.obj, f.mat, 1.0 - t, f.n[0], f.n[1], f.n[2], f.uv[0], f.uv[1]]; + for (base, list) in [(0usize, &mut obj), (8usize, &mut mat)] { + list.sort_by(|a, b| b.1.total_cmp(&a.1).then(a.0.cmp(&b.0))); + for (rank, (hsh, c)) in list.iter().take(4).enumerate() { + crypto[base + rank * 2] = crypto_float(*hsh); + crypto[base + rank * 2 + 1] = *c; + } + } + (o, crypto) + }) + .collect(); + let mut aux = effectcraft_raster::AuxChannels::new(w, h, s); + for (k, name) in NAMES.iter().enumerate() { + aux.channels.push((name.to_string(), resolved.iter().map(|r| r.0[k]).collect())); + } + for (base, layer) in [(0usize, "CryptoObject"), (8usize, "CryptoMaterial")] { + for rank_pair in 0..2 { + for (c, ch) in ["R", "G", "B", "A"].iter().enumerate() { + let k = base + rank_pair * 4 + c; + aux.channels.push((format!("{layer}{rank_pair:02}.{ch}"), resolved.iter().map(|r| r.1[k]).collect())); + } + } + } + aux.manifests = vec![("CryptoObject".into(), manifest_obj), ("CryptoMaterial".into(), manifest_mat)]; + aux +} From 79ad7e184382f3e60d1205ad6d6e980f3f079f60 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:36:59 -0400 Subject: [PATCH 10/23] M7.6: extrusion + primitives Extruded text keeps per-character 3D: each glyph's outline is extruded in its own space and placed by its animated matrix; shape layers extrude their fill outlines (gradients use their mid colour). Shadow-map bias scales with the texel footprint at the receiver and the PCF reach (no acne on curved casters), identically on CPU and GPU. Docs: Advanced 3D in architecture.md, parity table updated (3D, Text). Co-Authored-By: Claude Opus 5.5 --- crates/gpu/src/shaders/advanced3d.wgsl | 3 ++- crates/render/src/three_d/adv/raster.rs | 6 ++--- crates/render/src/three_d/adv/scene.rs | 23 ++++++++++--------- crates/render/src/three_d/adv/shade.rs | 4 +++- crates/render/src/three_d/adv/tests.rs | 2 +- docs/architecture.md | 30 ++++++++++++++++++++++++- docs/parity.md | 8 +++---- 7 files changed, 55 insertions(+), 21 deletions(-) diff --git a/crates/gpu/src/shaders/advanced3d.wgsl b/crates/gpu/src/shaders/advanced3d.wgsl index 9ae81ff5..12100cc3 100644 --- a/crates/gpu/src/shaders/advanced3d.wgsl +++ b/crates/gpu/src/shaders/advanced3d.wgsl @@ -197,7 +197,8 @@ fn shadow_factor(m: u32, p: vec3, n_l: f32) -> f32 { v = q.y * focal / q.z + c; } let z = q.z; - let bias = extra.x * (1.0 + 2.0 * (1.0 - clamp(n_l, 0.0, 1.0))) + abs(z) * 0.0015; + let texel = select(abs(z) / focal, 1.0 / focal, ortho); + let bias = extra.x * texel * (1.0 + extra.y) * (1.0 + 3.0 * (1.0 - clamp(n_l, 0.0, 1.0))); let step = extra.y / 2.0; let sz = i32(size); var lit = 0.0; diff --git a/crates/render/src/three_d/adv/raster.rs b/crates/render/src/three_d/adv/raster.rs index 36cec108..972a627b 100644 --- a/crates/render/src/three_d/adv/raster.rs +++ b/crates/render/src/three_d/adv/raster.rs @@ -460,12 +460,12 @@ pub(crate) fn shadow_maps(s: &mut Scene, draft: bool) { 0 => { let eye = [centre[0] - dir[0] * radius * 2.0, centre[1] - dir[1] * radius * 2.0, centre[2] - dir[2] * radius * 2.0]; let focal = (size as f64 * 0.5 / radius) as f32; - maps.push(ShadowMap { view: basis(eye, dir, hint), focal, ortho: true, size, offset: 0, bias: 1.5 / focal, radius: soft }); + maps.push(ShadowMap { view: basis(eye, dir, hint), focal, ortho: true, size, offset: 0, bias: 1.5, radius: soft }); } 1 => { let half = (l.cos_outer.clamp(-1.0, 1.0).acos() as f64 + 0.05).min(1.45); let focal = (size as f64 * 0.5 / half.tan()) as f32; - maps.push(ShadowMap { view: basis(pos, dir, hint), focal, ortho: false, size, offset: 0, bias: 1.0, radius: soft }); + maps.push(ShadowMap { view: basis(pos, dir, hint), focal, ortho: false, size, offset: 0, bias: 1.5, radius: soft }); } _ => { let fs = size / 2; @@ -477,7 +477,7 @@ pub(crate) fn shadow_maps(s: &mut Scene, draft: bool) { ([0.0, 0.0, 1.0], [0.0, 1.0, 0.0]), ([0.0, 0.0, -1.0], [0.0, 1.0, 0.0]), ] { - maps.push(ShadowMap { view: basis(pos, f, d), focal: fs as f32 * 0.5, ortho: false, size: fs, offset: 0, bias: 1.0, radius: soft }); + maps.push(ShadowMap { view: basis(pos, f, d), focal: fs as f32 * 0.5, ortho: false, size: fs, offset: 0, bias: 1.5, radius: soft }); } } } diff --git a/crates/render/src/three_d/adv/scene.rs b/crates/render/src/three_d/adv/scene.rs index 1d3acc3c..14251745 100644 --- a/crates/render/src/three_d/adv/scene.rs +++ b/crates/render/src/three_d/adv/scene.rs @@ -152,7 +152,7 @@ pub struct ShadowMap { pub ortho: bool, pub size: u32, pub offset: u32, - /// Depth bias (world units). + /// Depth bias in texels (scaled by the texel footprint at the receiver). pub bias: f32, /// PCF spread (texels). pub radius: f32, @@ -531,33 +531,35 @@ pub fn extrusion_params(ctx: &EvalCtx, layer: &Layer) -> Option Vec<(Primitive, [f32; 4])> { - let mut groups: Vec<(Vec, [f32; 4])> = vec![]; +/// Extruded meshes of a text or shape layer: (mesh, straight colour, mesh space → layer +/// space). Text extrudes each character in its own space, so per-character 3D animation +/// (position, rotation, scale in Z) carries over. +pub fn extruded_meshes(ctx: &EvalCtx, layer: &Layer, p: &model::extrude::ExtrudeParams) -> Vec<(Primitive, [f32; 4], Mat4)> { + let mut groups: Vec<(Vec, [f32; 4], Mat4)> = vec![]; match layer.source { LayerSource::Text => { if let Some(tg) = crate::text::text_geom(ctx, layer) { for g in tg.glyphs.iter().filter(|g| g.apply_fill && g.fill[3] > 0.0) { - groups.push((vec![g.path()], g.fill)); + groups.push((vec![g.local.clone()], g.fill, g.m)); } } } LayerSource::Shape => { if let Some(c) = layer.props.sub("contents") { - groups = crate::shapes::fill_outlines(ctx, layer, c); + groups = crate::shapes::fill_outlines(ctx, layer, c).into_iter().map(|(ps, c)| (ps, c, Mat4::IDENTITY)).collect(); } } _ => {} } groups .into_iter() - .filter_map(|(paths, color)| { + .filter_map(|(paths, color, m)| { let cs: Vec> = paths.iter().flat_map(|bp| contours(bp, 0.25)).collect(); let outlines = model::extrude::group_contours(cs); if outlines.is_empty() { return None; } - Some((model::extrude::extrude(&outlines, p), color)) + Some((model::extrude::extrude(&outlines, p), color, m)) }) .collect() } @@ -567,7 +569,7 @@ fn extruded_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, p: &model::extrude let opacity = (ctx.opacity(layer) as f32 * r.opacity_mul()).clamp(0.0, 1.0); let world = ctx.world_matrix(layer); let lin = b.s.linear_io; - for (mesh, c) in extruded_meshes(ctx, layer, p) { + for (mesh, c, local) in extruded_meshes(ctx, layer, p) { let l = |v: f32| if lin { v } else { srgb_to_linear(v) }; let m = b.material(Material { base: [l(c[0]), l(c[1]), l(c[2]), c[3]], @@ -576,7 +578,8 @@ fn extruded_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, p: &model::extrude double_sided: false, ..base }); - b.emit(&mesh, m, &|_| world); + let xf = world * local; + b.emit(&mesh, m, &|_| xf); } } diff --git a/crates/render/src/three_d/adv/shade.rs b/crates/render/src/three_d/adv/shade.rs index a8ab5e70..16d05287 100644 --- a/crates/render/src/three_d/adv/shade.rs +++ b/crates/render/src/three_d/adv/shade.rs @@ -212,7 +212,9 @@ pub fn shadow_factor(m: &ShadowMap, texels: &[f32], p: [f32; 3], n_l: f32) -> f3 let Some((u, v, z)) = shadow_coord(m, p) else { return 1.0 }; let size = m.size as i64; // Slope-scaled bias (world units are pixels). - let bias = m.bias * (1.0 + 2.0 * (1.0 - n_l.clamp(0.0, 1.0))) + z.abs() * 0.0015; + // Texel footprint (world units) × PCF reach, steeper for grazing light. + let texel = if m.ortho { 1.0 / m.focal } else { z.abs() / m.focal }; + let bias = m.bias * texel * (1.0 + m.radius) * (1.0 + 3.0 * (1.0 - n_l.clamp(0.0, 1.0))); let step = m.radius / 2.0; let mut lit = 0.0; for j in -2..=2 { diff --git a/crates/render/src/three_d/adv/tests.rs b/crates/render/src/three_d/adv/tests.rs index 23db2cbc..47ad6178 100644 --- a/crates/render/src/three_d/adv/tests.rs +++ b/crates/render/src/three_d/adv/tests.rs @@ -308,7 +308,7 @@ fn extruded_text_renders_as_a_mesh() { let params = scene::extrusion_params(&ctx, l).unwrap(); let meshes = scene::extruded_meshes(&ctx, l, ¶ms); assert!(!meshes.is_empty()); - assert!(meshes.iter().all(|(m, _)| m.positions.iter().any(|q| (q[2] - 30.0).abs() < 1e-3))); + assert!(meshes.iter().all(|(m, _, _)| m.positions.iter().any(|q| (q[2] - 30.0).abs() < 1e-3))); let img = render(&p, cid); assert!(img.data.iter().filter(|q| q[3] > 0.5).count() > 200); } diff --git a/docs/architecture.md b/docs/architecture.md index 7e2921e0..c9dfc023 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -69,7 +69,35 @@ the **source** (solid, footage frame, text, shape contents, or a nested comp), a then **effects** in order, then **layer styles**, then the **transform** into comp space (with motion blur sub-samples when enabled), the **track matte**, and finally **blends** into the accumulator. Adjustment layers apply their effects to the accumulator. Runs of 3D layers are composited per pixel through the -active camera, with lights, shadows and depth of field. +active camera, with lights, shadows and depth of field (Classic 3D), or rasterised as meshes by +the Advanced 3D renderer (below). + +**Advanced 3D** (Composition Settings ▸ 3D Renderer ▸ Advanced 3D, `comp.renderer`; +`crates/render/src/three_d/adv`, `crates/model`). A run of 3D layers becomes one scene of +world-space triangles: 3D model layers (glTF 2.0 / OBJ footage, mapped from +Y-up model units +into the layer by Geometry Options ▸ Model Scale; skins and animation clips played along layer +time), primitive layers (Layer ▸ New ▸ Cube/Sphere/Plane/Torus/Cone/Cylinder, parametric in +Geometry Options), text and shape layers with Geometry Options ▸ Extrusion Depth (glyph and +fill outlines flattened, triangulated with holes and extruded with Angular/Concave/Convex +bevels, per character so per-character 3D carries over), and a textured card for every other +3D layer. Shading is physically based in linear light (glTF metallic-roughness; Cook–Torrance +GGX specular, Smith–Schlick geometry, Schlick Fresnel, Lambert diffuse, after Karis 2013): +parallel, spot and point lights with After Effects' falloffs, ambient lights, image-based +light from an Environment light whose Source (or the comp's Environment Layer, Layer ▸ +Environment Layer) is an equirectangular image (cosine irradiance map; roughness-indexed mip +chain with the split-sum environment BRDF fit), and percentage-closer-filtered shadow maps +(spot: perspective, parallel: orthographic over the scene, point: six cube faces). Cards keep +Material Options (Accepts Lights/Shadows, Casts Shadows On/Only, Ambient, Diffuse, Specular); +models and primitives add Base Color, Metallic, Roughness, Emissive. Rasterisation runs at 2×2 +supersampling with a reversed-Z depth buffer; opaque triangles resolve visibility first, the +transparent ones are sorted back to front and blended. The software rasteriser +(`adv::raster`) is the reference and the headless/CI path; `effectcraft-gpu` implements +`Accelerator::raster_3d` with a wgpu render pipeline (`advanced3d.wgsl`) that repeats the +shading step for step, and tests compare the two. Resolve, depth of field (a gather blur by +each pixel's circle of confusion from the camera's Depth of Field settings) and the conversion +back to the working encoding run on the CPU for both. Not yet: motion blur, track mattes and +blend modes inside an Advanced 3D run, collapsed 3D precomps (drawn flattened), morph targets, +extruded strokes. A **layer cache** keeps each layer's finished pixels (source, masks and effects) keyed by a hash of its evaluated inputs, excluding the transform. Static and transform-only layers render once; diff --git a/docs/parity.md b/docs/parity.md index af0c9420..3a1b0932 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -74,7 +74,7 @@ library groundwork for mask tracking and Warp Stabilizer. | Wall clock, five parallel agents | 42–50 h | **≈ 8–10 h at today's measured pace; ≈ 25 h by the audit's conservative figures** | What is left is concentrated in large systems: Roto Brush and Refine Edge, the 3D camera tracker, -Advanced 3D (models, PBR, image-based lighting), the Warp Stabilizer and mask-tracking UI on top of +the Warp Stabilizer and mask-tracking UI on top of the new tracking library, a JavaScript scripting object model, SVG/PSD import, WebM, a disk cache, the missing effect categories (3D Channel, Immersive Video, OCIO), native macOS menus, and wasm threads. @@ -91,9 +91,9 @@ wasm threads. | Shapes | 68% | 3.9 | taper and wave strokes (pen tool for shape paths and vertex editing landed in M6.5) | | Compositions | 67% | 7.4 | marker dialog, flowchart, Essential Graphics | | Animation | 65% | 10.4 | puppet, Wiggler/Smoother/Motion Sketch (motion-path handles and the graph editor transform box landed in M5.8) | -| Text | ≈ 85% | 1.5 | vertical type is basic (upright characters, no tate-chu-yoko), no OpenType feature panel, no text on 3D bevels; per-character styles, paragraph settings, on-canvas editing and the `sourceText` style API landed (M9.9–M9.10) | +| Text | ≈ 86% | 1.3 | vertical type is basic (upright characters, no tate-chu-yoko), no OpenType feature panel, no extruded strokes (extruded, bevelled text landed in M7.6); per-character styles, paragraph settings, on-canvas editing and the `sourceText` style API landed (M9.9–M9.10) | | Web | 60% | 2.5 | threads, browser storage, audio | -| 3D | 58% | 20.5 | Advanced 3D (models, PBR, image-based light), 3D camera tracker, multi-view layouts | +| 3D | 72% | 12.0 | 3D camera tracker, multi-view layouts, Advanced 3D motion blur and blend modes, cameras/lights from models; Advanced 3D (glTF/OBJ models, primitives, extruded text and shapes, PBR, image-based light, shadow maps, GPU rasteriser) landed in M7.4–M7.6 | | Effects | 58% | 13.6 | Effect Controls widgets (angle dial, point crosshair, eyedropper, curves), GPU versions of more effects, 41 missing effects | | Interface | 55% | 10.4 | drag-to-dock and floating panels, preferences, native macOS menus (viewer rulers/snapping/channels/snapshots landed in M0.13) | | Project | 53% | 8.1 | 8/16/32-bit pipeline and colour management, auto-save, proxies, folder moves | @@ -132,4 +132,4 @@ Mocha and Cineware are third-party and not counted. The engine underneath (keyframes, expressions, shape operators, text animators, Classic 3D, all 38 blend modes, the render queue) is deep. Most of what is missing is interactive tooling in the viewer, settings that are stored but not yet rendered, about 69 menu entries that are still -disabled, and the large systems: tracking, puppet, paint, roto, Advanced 3D and GPU rendering. +disabled, and the large systems: tracking, puppet, paint and roto (Advanced 3D landed in M7.4–M7.6). From b936ed0055e364ed652dc7def086124e90035455 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:41:29 -0400 Subject: [PATCH 11/23] OUT-2: Opus (CELT) encoder (effectcraft-opusenc) New L0 crate effectcraft-opusenc: clean-room RFC 6716 CELT-only encoder (fullband 48 kHz, 20 ms packets, mono/stereo as dual L/R stereo, constant packet size from the bitrate) with the RFC 7845 OpusHead and pre-skip. Forward low-overlap MDCT, inter/intra coarse energy, fine/final energy, the decoder's exact allocation, recursive band splitting, PVQ search and codeword indexing, silence frames. crates/export depends on it; filmcraft-opus (dev) is the round-trip oracle in tests/opus_roundtrip.rs, with ffmpeg reading an in-test Ogg Opus file as a second oracle when installed. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 6 + Cargo.toml | 6 + crates/export/Cargo.toml | 2 + crates/export/tests/opus_roundtrip.rs | 364 +++++++++++++++++ crates/opusenc/Cargo.toml | 12 + crates/opusenc/README.md | 33 ++ crates/opusenc/src/bands.rs | 497 +++++++++++++++++++++++ crates/opusenc/src/energy.rs | 230 +++++++++++ crates/opusenc/src/lib.rs | 311 ++++++++++++++ crates/opusenc/src/mdct.rs | 293 ++++++++++++++ crates/opusenc/src/range.rs | 472 ++++++++++++++++++++++ crates/opusenc/src/rate.rs | 561 ++++++++++++++++++++++++++ crates/opusenc/src/tables.rs | 122 ++++++ docs/architecture.md | 1 + xtask/src/main.rs | 1 + 15 files changed, 2911 insertions(+) create mode 100644 crates/export/tests/opus_roundtrip.rs create mode 100644 crates/opusenc/Cargo.toml create mode 100644 crates/opusenc/README.md create mode 100644 crates/opusenc/src/bands.rs create mode 100644 crates/opusenc/src/energy.rs create mode 100644 crates/opusenc/src/lib.rs create mode 100644 crates/opusenc/src/mdct.rs create mode 100644 crates/opusenc/src/range.rs create mode 100644 crates/opusenc/src/rate.rs create mode 100644 crates/opusenc/src/tables.rs diff --git a/Cargo.lock b/Cargo.lock index 46f34e63..1a1f0f60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1218,6 +1218,7 @@ version = "0.1.0" dependencies = [ "effectcraft-color", "effectcraft-media", + "effectcraft-opusenc", "effectcraft-project", "effectcraft-raster", "effectcraft-render", @@ -1225,6 +1226,7 @@ dependencies = [ "filmcraft-aac", "filmcraft-h264enc", "filmcraft-isobmff", + "filmcraft-opus", "filmcraft-prores", "gif", "image", @@ -1338,6 +1340,10 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "effectcraft-opusenc" +version = "0.1.0" + [[package]] name = "effectcraft-path" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index a1941cf4..da4f7047 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ effectcraft-engine = { path = "crates/engine" } effectcraft-host = { path = "crates/host" } effectcraft-ui-egui = { path = "crates/ui-egui" } effectcraft-automation = { path = "crates/automation" } +effectcraft-opusenc = { path = "crates/opusenc" } # foundation thiserror = "2" @@ -66,6 +67,8 @@ filmcraft-h264enc = { git = "https://github.com/storytold/filmcraft", rev = "89c filmcraft-prores = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf24f698b0f0e538eb7795929f19bbfb1dbb" } filmcraft-aac = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf24f698b0f0e538eb7795929f19bbfb1dbb" } filmcraft-isobmff = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf24f698b0f0e538eb7795929f19bbfb1dbb" } +# decoder used as the round-trip oracle of effectcraft-opusenc (crates/export dev-dependency) +filmcraft-opus = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf24f698b0f0e538eb7795929f19bbfb1dbb" } # gpu (the same wgpu eframe / egui-wgpu use) wgpu = { version = "30.0.1", features = ["fragile-send-sync-non-atomic-wasm"] } @@ -129,6 +132,9 @@ opt-level = 3 [profile.dev.package.effectcraft-media] opt-level = 3 +[profile.dev.package.effectcraft-opusenc] +opt-level = 3 + [profile.dev.package.effectcraft-export] opt-level = 3 diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 34c83aab..5bdd0fee 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -18,6 +18,7 @@ filmcraft-h264enc = { workspace = true } filmcraft-prores = { workspace = true } filmcraft-aac = { workspace = true } filmcraft-isobmff = { workspace = true } +effectcraft-opusenc = { workspace = true } image = { workspace = true, features = ["png", "jpeg", "tiff", "exr"] } gif = { workspace = true } rayon = { workspace = true } @@ -28,3 +29,4 @@ log = { workspace = true } [dev-dependencies] effectcraft-media = { workspace = true } effectcraft-color = { workspace = true } +filmcraft-opus = { workspace = true } diff --git a/crates/export/tests/opus_roundtrip.rs b/crates/export/tests/opus_roundtrip.rs new file mode 100644 index 00000000..5ea099f0 --- /dev/null +++ b/crates/export/tests/opus_roundtrip.rs @@ -0,0 +1,364 @@ +//! Round trip of `effectcraft-opusenc` through FilmCraft's spec-derived Opus decoder, plus an +//! ffmpeg oracle (skipped when ffmpeg is not installed) reading an Ogg Opus file written here. + +use std::process::Command; + +use effectcraft_opusenc::OpusEncoder; + +const SR: f32 = 48_000.0; +const FRAME: usize = OpusEncoder::FRAME_SIZE; + +/// Encodes interleaved `pcm`, padding the tail so the encoder's look-ahead is flushed. +fn encode(pcm: &[f32], channels: u8, bitrate: u32) -> (OpusEncoder, Vec>) { + let c = channels as usize; + let mut enc = OpusEncoder::new(channels, bitrate); + let total = pcm.len() / c + enc.pre_skip() as usize; + let frames = total.div_ceil(FRAME); + let mut padded = pcm.to_vec(); + padded.resize(frames * FRAME * c, 0.0); + let packets = padded.chunks(FRAME * c).map(|f| enc.encode_float(f)).collect(); + (enc, packets) +} + +/// Decodes with filmcraft-opus, drops the pre-skip and returns planar channels of `len` samples. +fn decode(head: &[u8], packets: &[Vec], len: usize) -> Vec> { + let mut dec = filmcraft_opus::Decoder::new(head).expect("OpusHead"); + dec.set_trim_pre_skip(true); + let mut out: Vec> = vec![Vec::new(); dec.channels()]; + for p in packets { + let planes = dec.decode(Some(p)).expect("decode"); + for (o, p) in out.iter_mut().zip(planes) { + o.extend_from_slice(&p); + } + } + for o in &mut out { + assert!(o.len() >= len, "decoded {} < {len}", o.len()); + o.truncate(len); + } + out +} + +fn roundtrip(planar: &[Vec], bitrate: u32) -> (Vec>, Vec>) { + let c = planar.len(); + let len = planar[0].len(); + let mut pcm = vec![0f32; len * c]; + for (ch, p) in planar.iter().enumerate() { + for (i, &v) in p.iter().enumerate() { + pcm[i * c + ch] = v; + } + } + let (enc, packets) = encode(&pcm, c as u8, bitrate); + let decoded = decode(&enc.opus_head(), &packets, len); + (packets, decoded) +} + +/// SNR in dB over the steady part (the first and last 20 ms are excluded). +fn snr(orig: &[f32], dec: &[f32]) -> f64 { + let (a, b) = (FRAME, orig.len() - FRAME); + let mut s = 0f64; + let mut e = 0f64; + for i in a..b { + s += (orig[i] as f64).powi(2); + e += (orig[i] as f64 - dec[i] as f64).powi(2); + } + 10.0 * (s / e.max(1e-30)).log10() +} + +fn sine(freq: f32, amp: f32, len: usize) -> Vec { + (0..len).map(|i| amp * (2.0 * std::f32::consts::PI * freq * i as f32 / SR).sin()).collect() +} + +fn chirp(f0: f32, f1: f32, amp: f32, len: usize) -> Vec { + let dur = len as f32 / SR; + let k = (f1 - f0) / dur; + (0..len) + .map(|i| { + let t = i as f32 / SR; + amp * (2.0 * std::f32::consts::PI * (f0 * t + 0.5 * k * t * t)).sin() + }) + .collect() +} + +fn noise(amp: f32, len: usize, seed: u32) -> Vec { + let mut s = seed; + (0..len) + .map(|_| { + s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + amp * ((s >> 8) as f32 / (1u32 << 24) as f32 * 2.0 - 1.0) + }) + .collect() +} + +/// Energy (dB) of `x` in each of a few octave bands up to 16 kHz, from a Hann-windowed FFT +/// averaged over 2048-sample blocks. +fn band_energies(x: &[f32]) -> Vec { + const N: usize = 2048; + let edges = [100.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0, 16000.0]; + let mut acc = vec![0f64; edges.len() - 1]; + let mut start = N; + while start + N <= x.len() - N { + let mut re: Vec = (0..N).map(|i| x[start + i] as f64 * (0.5 - 0.5 * (2.0 * std::f64::consts::PI * i as f64 / N as f64).cos())).collect(); + let mut im = vec![0f64; N]; + fft(&mut re, &mut im); + for k in 1..N / 2 { + let f = k as f64 * SR as f64 / N as f64; + if let Some(b) = edges.windows(2).position(|w| f >= w[0] && f < w[1]) { + acc[b] += re[k] * re[k] + im[k] * im[k]; + } + } + start += N; + } + acc.iter().map(|e| 10.0 * (e + 1e-20).log10()).collect() +} + +fn fft(re: &mut [f64], im: &mut [f64]) { + let n = re.len(); + let mut j = 0; + for i in 1..n { + let mut bit = n >> 1; + while j & bit != 0 { + j ^= bit; + bit >>= 1; + } + j |= bit; + if i < j { + re.swap(i, j); + im.swap(i, j); + } + } + let mut len = 2; + while len <= n { + let ang = -2.0 * std::f64::consts::PI / len as f64; + for s in (0..n).step_by(len) { + for k in 0..len / 2 { + let (wr, wi) = ((ang * k as f64).cos(), (ang * k as f64).sin()); + let (a, b) = (s + k, s + k + len / 2); + let tr = re[b] * wr - im[b] * wi; + let ti = re[b] * wi + im[b] * wr; + re[b] = re[a] - tr; + im[b] = im[a] - ti; + re[a] += tr; + im[a] += ti; + } + } + len <<= 1; + } +} + +#[test] +fn sine_440_mono() { + let x = sine(440.0, 0.5, 48_000); + for rate in [64_000, 128_000] { + let (packets, d) = roundtrip(std::slice::from_ref(&x), rate); + assert!(packets.iter().all(|p| p.len() == rate as usize / 400)); + let s = snr(&x, &d[0]); + eprintln!("sine 440 Hz mono @ {} kb/s: SNR {s:.1} dB", rate / 1000); + assert!(s > 20.0, "sine SNR {s:.1} dB at {rate}"); + } +} + +#[test] +fn chirp_mono() { + let x = chirp(100.0, 10_000.0, 0.4, 96_000); + for rate in [64_000, 128_000] { + let (_, d) = roundtrip(std::slice::from_ref(&x), rate); + let s = snr(&x, &d[0]); + eprintln!("chirp 100 Hz-10 kHz mono @ {} kb/s: SNR {s:.1} dB", rate / 1000); + assert!(s > 15.0, "chirp SNR {s:.1} dB at {rate}"); + } +} + +#[test] +fn white_noise_mono() { + let x = noise(0.3, 48_000, 12345); + for rate in [64_000, 128_000] { + let (_, d) = roundtrip(std::slice::from_ref(&x), rate); + let s = snr(&x, &d[0]); + let (eo, ed) = (band_energies(&x), band_energies(&d[0])); + let err: Vec = x.iter().zip(&d[0]).map(|(a, b)| a - b).collect(); + let ee = band_energies(&err); + let octave_snr: Vec = eo.iter().zip(&ee).map(|(a, b)| a - b).collect(); + let worst = eo.iter().zip(&ed).map(|(a, b)| (a - b).abs()).fold(0.0, f64::max); + let min_octave = octave_snr.iter().copied().fold(f64::MAX, f64::min); + eprintln!( + "white noise mono @ {} kb/s: full-band SNR {s:.1} dB, octave SNRs 100 Hz-16 kHz {:.1?} dB, worst octave energy error {worst:.2} dB", + rate / 1000, + octave_snr + ); + // Full-band noise loses everything above 20 kHz (CELT's last band ends there) and spends + // few bits per coefficient on 16-20 kHz, so the full-band waveform SNR is low; the band + // energies must match closely and the octaves below 16 kHz must be waveform-coded. + assert!(worst < 1.5, "band energy error {worst:.2} dB at {rate}: {eo:?} vs {ed:?}"); + assert!(s > 0.5, "noise SNR {s:.1} dB at {rate}"); + assert!(min_octave > if rate >= 128_000 { 10.0 } else { 3.0 }, "octave SNRs {octave_snr:?} at {rate}"); + } +} + +#[test] +fn lowpass_noise_mono() { + // White noise through a 4-pole low-pass at about 2 kHz: a dense spectrum the encoder can + // afford to code as a waveform. + let w = noise(0.8, 48_000, 999); + let a = (-2.0 * std::f32::consts::PI * 2_000.0 / SR).exp(); + let mut st = [0f32; 4]; + let x: Vec = w + .iter() + .map(|&v| { + let mut y = v; + for s in st.iter_mut() { + *s = (1.0 - a) * y + a * *s; + y = *s; + } + y + }) + .collect(); + for rate in [64_000, 128_000] { + let (_, d) = roundtrip(std::slice::from_ref(&x), rate); + let s = snr(&x, &d[0]); + eprintln!("low-pass noise mono @ {} kb/s: SNR {s:.1} dB", rate / 1000); + assert!(s > 15.0, "low-pass noise SNR {s:.1} dB at {rate}"); + } +} + +#[test] +fn silence_stays_silent() { + let x = vec![0f32; 48_000]; + let (packets, d) = roundtrip(&[x.clone(), x], 128_000); + assert!(packets.iter().all(|p| p.len() == 3), "silence frames are 3-byte packets"); + for ch in &d { + let peak = ch.iter().fold(0f32, |m, v| m.max(v.abs())); + assert!(peak < 1e-6, "peak {peak}"); + } +} + +#[test] +fn stereo_distinct_channels() { + let l = sine(440.0, 0.5, 48_000); + let r = chirp(200.0, 6_000.0, 0.3, 48_000); + let (packets, d) = roundtrip(&[l.clone(), r.clone()], 256_000); + assert!(packets.iter().all(|p| p[0] == 0xFC && p.len() == 640)); + let (sl, sr) = (snr(&l, &d[0]), snr(&r, &d[1])); + eprintln!("stereo @ 256 kb/s: L (sine) SNR {sl:.1} dB, R (chirp) SNR {sr:.1} dB"); + assert!(sl > 20.0 && sr > 15.0, "stereo SNR L {sl:.1} R {sr:.1}"); + // No leakage of the chirp into the left channel (2-4 kHz octave: chirp only). + let (el, er) = (band_energies(&d[0]), band_energies(&r)); + assert!(el[4] < er[4] - 30.0, "chirp energy leaked into L: {} vs {}", el[4], er[4]); +} + +#[test] +fn stereo_128k_total() { + let l = sine(1000.0, 0.4, 48_000); + let r = sine(330.0, 0.4, 48_000); + let (_, d) = roundtrip(&[l.clone(), r.clone()], 128_000); + let (sl, sr) = (snr(&l, &d[0]), snr(&r, &d[1])); + eprintln!("stereo @ 128 kb/s: L SNR {sl:.1} dB, R SNR {sr:.1} dB"); + assert!(sl > 15.0 && sr > 15.0, "stereo SNR L {sl:.1} R {sr:.1}"); +} + +#[test] +fn signal_after_silence_and_loud_transient() { + // Silence, then a burst at full scale, then silence again: energy prediction must recover. + let mut x = vec![0f32; 24_000]; + x.extend(sine(2_000.0, 0.99, 24_000)); + x.extend(vec![0f32; 24_000]); + let (_, d) = roundtrip(std::slice::from_ref(&x), 96_000); + let s = snr(&x[24_000 + 2 * FRAME..48_000], &d[0][24_000 + 2 * FRAME..48_000]); + eprintln!("burst after silence @ 96 kb/s: SNR {s:.1} dB"); + assert!(s > 15.0, "burst SNR {s:.1}"); + let tail_peak = d[0][48_000 + 2 * FRAME..].iter().fold(0f32, |m, v| m.max(v.abs())); + assert!(tail_peak < 1e-3, "tail peak {tail_peak}"); +} + +// ---- Ogg Opus (RFC 3533 + RFC 7845) for the ffmpeg oracle ---- + +fn ogg_crc(data: &[u8]) -> u32 { + let mut crc = 0u32; + for &b in data { + crc ^= (b as u32) << 24; + for _ in 0..8 { + crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 }; + } + } + crc +} + +fn ogg_page(out: &mut Vec, packet: &[u8], header_type: u8, granule: u64, seq: u32) { + let mut lacing = vec![255u8; packet.len() / 255]; + lacing.push((packet.len() % 255) as u8); + let start = out.len(); + out.extend_from_slice(b"OggS"); + out.push(0); + out.push(header_type); + out.extend_from_slice(&granule.to_le_bytes()); + out.extend_from_slice(&0x4543_4F50u32.to_le_bytes()); // serial + out.extend_from_slice(&seq.to_le_bytes()); + out.extend_from_slice(&0u32.to_le_bytes()); // CRC placeholder + out.push(lacing.len() as u8); + out.extend_from_slice(&lacing); + out.extend_from_slice(packet); + let crc = ogg_crc(&out[start..]); + out[start + 22..start + 26].copy_from_slice(&crc.to_le_bytes()); +} + +fn ogg_opus(enc: &OpusEncoder, packets: &[Vec], len: usize) -> Vec { + let mut out = Vec::new(); + ogg_page(&mut out, &enc.opus_head(), 0x02, 0, 0); + let vendor = b"effectcraft-opusenc"; + let mut tags = b"OpusTags".to_vec(); + tags.extend_from_slice(&(vendor.len() as u32).to_le_bytes()); + tags.extend_from_slice(vendor); + tags.extend_from_slice(&0u32.to_le_bytes()); + ogg_page(&mut out, &tags, 0, 0, 1); + let pre_skip = enc.pre_skip() as u64; + for (i, p) in packets.iter().enumerate() { + let last = i + 1 == packets.len(); + let granule = if last { pre_skip + len as u64 } else { ((i + 1) * FRAME) as u64 }; + ogg_page(&mut out, p, if last { 0x04 } else { 0 }, granule, i as u32 + 2); + } + out +} + +#[test] +fn ogg_crc_known_value() { + // CRC-32 (poly 0x04C11DB7, no reflection, init 0) of "123456789". + assert_eq!(ogg_crc(b"123456789"), 0x89A1_897F); +} + +#[test] +fn ffmpeg_oracle_decodes_ogg_opus() { + if Command::new("ffmpeg").arg("-version").output().is_err() { + eprintln!("ffmpeg not installed; skipping the oracle test"); + return; + } + for (channels, rate) in [(1u8, 96_000u32), (2, 192_000)] { + let len = 48_000; + let planar: Vec> = + if channels == 1 { vec![chirp(150.0, 9_000.0, 0.4, len)] } else { vec![sine(440.0, 0.5, len), chirp(150.0, 9_000.0, 0.3, len)] }; + let c = channels as usize; + let mut pcm = vec![0f32; len * c]; + for (ch, p) in planar.iter().enumerate() { + for (i, &v) in p.iter().enumerate() { + pcm[i * c + ch] = v; + } + } + let (enc, packets) = encode(&pcm, channels, rate); + let path = std::env::temp_dir().join(format!("effectcraft-opusenc-{}-{channels}.opus", std::process::id())); + std::fs::write(&path, ogg_opus(&enc, &packets, len)).unwrap(); + let out = Command::new("ffmpeg") + .args(["-v", "error", "-i"]) + .arg(&path) + .args(["-f", "f32le", "-ac", &channels.to_string(), "-ar", "48000", "-"]) + .output() + .expect("run ffmpeg"); + let _ = std::fs::remove_file(&path); + assert!(out.status.success(), "ffmpeg failed: {}", String::from_utf8_lossy(&out.stderr)); + let dec: Vec = out.stdout.chunks_exact(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect(); + assert_eq!(dec.len(), len * c, "ffmpeg must honour pre-skip and the end granule"); + for ch in 0..c { + let d: Vec = (0..len).map(|i| dec[i * c + ch]).collect(); + let s = snr(&planar[ch], &d); + eprintln!("ffmpeg {channels} ch @ {} kb/s, channel {ch}: SNR {s:.1} dB", rate / 1000); + assert!(s > 15.0, "ffmpeg-decoded SNR {s:.1} dB (channel {ch})"); + } + } +} diff --git a/crates/opusenc/Cargo.toml b/crates/opusenc/Cargo.toml new file mode 100644 index 00000000..90b0fd02 --- /dev/null +++ b/crates/opusenc/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "effectcraft-opusenc" +description = "Clean-room pure-Rust Opus encoder (RFC 6716 CELT-only, fullband 48 kHz, 20 ms; RFC 7845 OpusHead) for WebM export audio" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] diff --git a/crates/opusenc/README.md b/crates/opusenc/README.md new file mode 100644 index 00000000..0c76d49f --- /dev/null +++ b/crates/opusenc/README.md @@ -0,0 +1,33 @@ +# effectcraft-opusenc + +Clean-room, pure-Rust Opus encoder for EffectCraft's WebM export audio (layer L0, `std` only, +no `unsafe`). + +- **CELT-only** packets (TOC configuration 31: fullband 20 kHz, 20 ms, one frame per packet), + 48 kHz input, mono or stereo (stereo coded as dual L/R stereo), constant packet size from the + requested bitrate; all-zero frames become 3-byte silence packets. +- Forward MDCT with the low-overlap window, band energies with inter/intra coarse prediction + (cheaper of the two per frame), fine and final energy bits, the decoder's exact bit allocation, + spreading and recursive band splitting, greedy PVQ search and codeword indexing. +- Not implemented: SILK/hybrid modes, transient (short-block) frames, TF changes, pitch + post-filter, intensity stereo, dynamic allocation boosts, VBR rate control. +- `OpusEncoder::pre_skip()` is 120 samples (the MDCT overlap); `opus_head()` returns the RFC 7845 + identification header. + +```rust +let mut enc = effectcraft_opusenc::OpusEncoder::new(2, 128_000); +let packet = enc.encode_float(&vec![0.0; effectcraft_opusenc::OpusEncoder::FRAME_SIZE * 2]); +``` + +## Specifications + +- RFC 6716, *Definition of the Opus Audio Codec* (September 2012), with the RFC 8251 updates + (October 2017) as implemented by the decoder we round-trip against. +- RFC 7845, *Ogg Encapsulation for the Opus Audio Codec* (April 2016), §5.1 `OpusHead` and + §4.2 pre-skip. + +Implemented from the RFC text. Constant tables (band edges, `eMeans`, the coarse-energy Laplace +model, the allocation table, ICDFs) and the decoder-side allocation arithmetic are mirrored from +FilmCraft's first-party, spec-derived `filmcraft-opus` decoder (MIT OR Apache-2.0), which is also +the round-trip oracle in `crates/export/tests/opus_roundtrip.rs` (ffmpeg is a second, external +oracle when installed). No third-party encoder or decoder source was consulted. diff --git a/crates/opusenc/src/bands.rs b/crates/opusenc/src/bands.rs new file mode 100644 index 00000000..744de3fe --- /dev/null +++ b/crates/opusenc/src/bands.rs @@ -0,0 +1,497 @@ +//! Band shape coding: PVQ search and codeword indexing, spreading, recursive band splitting +//! (RFC 6716 §4.3.4, encoder direction). +//! +//! The encoder codes 20 ms long-block frames only (no transients, no TF changes), and stereo +//! as dual (independent L/R) stereo, so a band is always coded by `quant_partition` with a single +//! block. Every bit-budget computation mirrors the decoder so both sides take the same +//! split/pulse decisions. + +use crate::range::{BITRES, RangeEncoder, ilog}; +use crate::rate::{Mode, QTHETA_OFFSET, get_pulses}; +use crate::tables::*; + +pub const SPREAD_NORMAL: usize = 2; + +#[inline] +fn frac_mul16(a: i32, b: i32) -> i32 { + (16384 + (a as i16 as i32) * (b as i16 as i32)) >> 15 +} + +fn bitexact_cos(x: i32) -> i32 { + let tmp = (4096 + x * x) >> 13; + let x2 = tmp; + let x2 = (32767 - x2) + frac_mul16(x2, -7651 + frac_mul16(x2, 8277 + frac_mul16(-626, x2))); + 1 + x2 +} + +fn bitexact_log2tan(isin: i32, icos: i32) -> i32 { + let lc = ilog(icos as u32); + let ls = ilog(isin as u32); + let icos = icos << (15 - lc); + let isin = isin << (15 - ls); + (ls - lc) * (1 << 11) + frac_mul16(isin, frac_mul16(isin, -2597) + 7932) - frac_mul16(icos, frac_mul16(icos, -2597) + 7932) +} + +fn compute_qn(n: i32, b: i32, offset: i32, pulse_cap: i32, stereo: bool) -> i32 { + const EXP2_TABLE8: [i32; 8] = [16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048]; + let mut n2 = 2 * n - 1; + if stereo && n == 2 { + n2 -= 1; + } + let mut qb = (b + n2 * offset) / n2; + qb = qb.min(b - pulse_cap - (4 << BITRES)); + qb = qb.min(8 << BITRES); + if qb < (1 << BITRES >> 1) { + 1 + } else { + let qn = EXP2_TABLE8[(qb & 0x7) as usize] >> (14 - (qb >> BITRES)); + (qn + 1) >> 1 << 1 + } +} + +/// One elementary pass of the spreading rotation, inverted (the decoder applies the forward +/// pass; the encoder undoes it before the pulse search). +fn exp_rotation1_inv(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { + if len <= stride { + return; + } + if len >= 2 * stride + 1 { + for i in 0..=len - 2 * stride - 1 { + let x1 = x[i]; + let x2 = x[i + stride]; + x[i + stride] = c * x2 - s * x1; + x[i] = c * x1 + s * x2; + } + } + for i in (0..len - stride).rev() { + let x1 = x[i]; + let x2 = x[i + stride]; + x[i + stride] = c * x2 - s * x1; + x[i] = c * x1 + s * x2; + } +} + +/// Inverse of the decoder's spreading rotation (§4.3.4.3) for one block of `len` samples. +pub fn exp_rotation_inv(x: &mut [f32], len: usize, k: usize, spread: usize) { + const SPREAD_FACTOR: [usize; 3] = [15, 10, 5]; + if 2 * k >= len || spread == 0 { + return; + } + let factor = SPREAD_FACTOR[spread - 1]; + let gain = len as f32 / (len + factor * k) as f32; + let theta = 0.5 * gain * gain; + let c = (0.5 * std::f32::consts::PI * theta).cos(); + let s = (0.5 * std::f32::consts::PI * (1.0 - theta)).cos(); + let mut stride2 = 0; + if len >= 8 { + stride2 = 1; + while stride2 * stride2 + stride2 < len { + stride2 += 1; + } + } + // The decoder applies (stride2: s, c) then (1: c, s); undo in reverse order. + exp_rotation1_inv(x, len, 1, c, s); + if stride2 != 0 { + exp_rotation1_inv(x, len, stride2, s, c); + } +} + +/// Greedy PVQ search: the integer vector `y` with `sum |y| = k` closest in direction to `x`. +pub fn pvq_search(x: &[f32], k: usize, y: &mut [i32]) { + let n = x.len(); + y[..n].fill(0); + let abs: Vec = x.iter().map(|v| v.abs()).collect(); + let sum: f32 = abs.iter().sum(); + if !sum.is_finite() || sum <= 1e-15 { + y[0] = k as i32; + return; + } + let mut placed = 0usize; + let mut xy = 0f32; + let mut yy = 0f32; + if k > n / 2 { + // Pre-project onto the pyramid (rounding down) and place the rest greedily. + let r = (k as f32 - 1.0) / sum; + for j in 0..n { + let p = (abs[j] * r).floor() as i32; + y[j] = p; + placed += p as usize; + xy += abs[j] * p as f32; + yy += (p * p) as f32; + } + } + while placed < k { + let mut best = 0usize; + let mut best_num = -1f32; + let mut best_den = 1f32; + for j in 0..n { + let num = xy + abs[j]; + let num = num * num; + let den = yy + 2.0 * y[j] as f32 + 1.0; + if num * best_den > best_num * den { + best_num = num; + best_den = den; + best = j; + } + } + xy += abs[best]; + yy += 2.0 * y[best] as f32 + 1.0; + y[best] += 1; + placed += 1; + } + for j in 0..n { + if x[j] < 0.0 { + y[j] = -y[j]; + } + } +} + +/// Encodes the PVQ codeword `y` (`sum |y| = k`) as the index the RFC 6716 §4.3.4.2 decoder maps +/// back to `y`. +pub fn encode_pulses(y: &[i32], k: usize, enc: &mut RangeEncoder, v: &mut Vec) { + let n = y.len(); + let w = k + 1; + v.clear(); + v.resize((n + 1) * w, 0); + v[0] = 1; + for nn in 1..=n { + v[nn * w] = 1; + for kk in 1..=k { + let a = v[(nn - 1) * w + kk]; + let b = v[nn * w + kk - 1]; + let c = v[(nn - 1) * w + kk - 1]; + v[nn * w + kk] = a.saturating_add(b).saturating_add(c); + } + } + let total = v[n * w + k]; + let vv = |nn: usize, kx: usize| v[nn * w + kx]; + let mut index = 0u64; + let mut kk = k; + for j in 0..n { + if kk == 0 { + break; + } + let rem = n - j; + let t = y[j].unsigned_abs() as usize; + // Codewords with |y_j| = t start at sum_{m < kk - t} V(rem-1, m) within their sign half. + let mut base: u64 = (0..kk - t).map(|m| vv(rem - 1, m)).sum(); + if y[j] < 0 { + base += (vv(rem - 1, kk) + vv(rem, kk)) / 2; + } + index += base; + kk -= t; + } + debug_assert!(total <= u32::MAX as u64); + if total >= 2 { + enc.uint(index as u32, total as u32); + } +} + +pub struct BandEnc<'m, 'e> { + pub m: &'m Mode, + pub enc: &'e mut RangeEncoder, + pub i: usize, + pub spread: usize, + pub remaining_bits: i32, + pvq_v: Vec, + iy: Vec, + tmp: Vec, +} + +impl BandEnc<'_, '_> { + /// `compute_theta` for a mono split (single block): measures the energy split between the + /// two halves and codes the quantised angle with the triangular pdf. + fn compute_theta_split(&mut self, xa: &[f32], xb: &[f32], b: &mut i32, lm: i32) -> (i32, i32, i32) { + let n = xa.len() as i32; + let i = self.i; + let pulse_cap = self.m.log_n[i] + lm * (1 << BITRES); + let offset = (pulse_cap >> 1) - QTHETA_OFFSET; + let qn = compute_qn(n, *b, offset, pulse_cap, false); + let tell = self.enc.tell_frac(); + let mut itheta = 0i32; + if qn != 1 { + let mid = xa.iter().map(|v| v * v).sum::().sqrt(); + let side = xb.iter().map(|v| v * v).sum::().sqrt(); + let ang = (side as f64).atan2(mid as f64) * std::f64::consts::FRAC_2_PI; + let it = ((ang * qn as f64 + 0.5).floor() as i32).clamp(0, qn) as u32; + let q = qn as u32; + let half = q >> 1; + let ft = (half + 1) * (half + 1); + let (fl, fs) = if it <= half { (it * (it + 1) >> 1, it + 1) } else { (ft - ((q + 1 - it) * (q + 2 - it) >> 1), q + 1 - it) }; + self.enc.encode(fl, fl + fs, ft); + itheta = ((it * 16384) / q) as i32; + } + let qalloc = self.enc.tell_frac() - tell; + *b -= qalloc; + let delta = if itheta == 0 { + -16384 + } else if itheta == 16384 { + 16384 + } else { + let imid = bitexact_cos(itheta); + let iside = bitexact_cos(16384 - itheta); + frac_mul16((n - 1) << 7, bitexact_log2tan(iside, imid)) + }; + (itheta, delta, qalloc) + } + + /// `quant_partition` (single block, encoder side). + pub fn quant_partition(&mut self, x: &[f32], mut b: i32, mut lm: i32) { + let n = x.len(); + let i = self.i; + let cache = self.m.cache(lm, i); + let cache_max = cache[cache[0] as usize] as i32; + if lm != -1 && b > cache_max + 12 && n > 2 { + let half = n >> 1; + let (xa, xb) = x.split_at(half); + lm -= 1; + let (itheta, delta, qalloc) = self.compute_theta_split(xa, xb, &mut b, lm); + let mut mbits = 0.max(b.min((b - delta) / 2)); + let mut sbits = b - mbits; + self.remaining_bits -= qalloc; + let mut rebalance = self.remaining_bits; + if mbits >= sbits { + self.quant_partition(xa, mbits, lm); + rebalance = mbits - (rebalance - self.remaining_bits); + if rebalance > 3 << BITRES && itheta != 0 { + sbits += rebalance - (3 << BITRES); + } + self.quant_partition(xb, sbits, lm); + } else { + self.quant_partition(xb, sbits, lm); + rebalance = sbits - (rebalance - self.remaining_bits); + if rebalance > 3 << BITRES && itheta != 16384 { + mbits += rebalance - (3 << BITRES); + } + self.quant_partition(xa, mbits, lm); + } + return; + } + let m = self.m; + let mut q = m.bits2pulses(i, lm, b); + let mut curr_bits = m.pulses2bits(i, lm, q); + self.remaining_bits -= curr_bits; + while self.remaining_bits < 0 && q > 0 { + self.remaining_bits += curr_bits; + q -= 1; + curr_bits = m.pulses2bits(i, lm, q); + self.remaining_bits -= curr_bits; + } + if q != 0 { + let k = get_pulses(q) as usize; + let mut tmp = std::mem::take(&mut self.tmp); + tmp.clear(); + tmp.extend_from_slice(x); + exp_rotation_inv(&mut tmp, n, k, self.spread); + let mut iy = std::mem::take(&mut self.iy); + iy.clear(); + iy.resize(n, 0); + pvq_search(&tmp, k, &mut iy); + encode_pulses(&iy, k, self.enc, &mut self.pvq_v); + self.iy = iy; + self.tmp = tmp; + } + } +} + +/// Codes all band shapes (`quant_all_bands`, encoder side, long blocks). `x` holds `c` channels +/// of `n` normalised MDCT coefficients each. +#[allow(clippy::too_many_arguments)] +pub fn quant_all_bands( + m: &Mode, + start: usize, + end: usize, + x: &[f32], + n_total: usize, + c: usize, + pulses: &[i32; NB_EBANDS], + spread: usize, + dual_stereo: bool, + intensity: usize, + total_bits: i32, + mut balance: i32, + enc: &mut RangeEncoder, + lm: usize, + coded_bands: usize, +) { + let mm = 1usize << lm; + debug_assert!(c == 1 || dual_stereo || intensity <= start, "stereo frames are coded as dual stereo"); + let mut ctx = BandEnc { m, enc, i: 0, spread, remaining_bits: 0, pvq_v: Vec::new(), iy: Vec::new(), tmp: Vec::new() }; + for i in start..end { + ctx.i = i; + let xo = mm * EBANDS[i] as usize; + let n = mm * EBANDS[i + 1] as usize - xo; + let tell = ctx.enc.tell_frac(); + if i != start { + balance -= tell; + } + let remaining_bits = total_bits - tell - 1; + ctx.remaining_bits = remaining_bits; + let b = if i < coded_bands { + let curr_balance = balance / 3.min(coded_bands as i32 - i as i32); + 0.max(16383.min((remaining_bits + 1).min(pulses[i] + curr_balance))) + } else { + 0 + }; + // Bands at and above `intensity` (= coded_bands) carry no bits, so they emit no symbols. + if i < coded_bands || c == 1 { + if c == 2 && dual_stereo && i < intensity { + ctx.quant_partition(&x[xo..xo + n], b / 2, lm as i32); + ctx.quant_partition(&x[n_total + xo..n_total + xo + n], b / 2, lm as i32); + } else { + ctx.quant_partition(&x[xo..xo + n], b, lm as i32); + } + } + balance += pulses[i] + tell; + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::range::dec::RangeDecoder; + + fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { + let ms = -s; + if len <= stride { + return; + } + for i in 0..len - stride { + let x1 = x[i]; + let x2 = x[i + stride]; + x[i + stride] = c * x2 + s * x1; + x[i] = c * x1 + ms * x2; + } + if len >= 2 * stride + 1 { + for i in (0..=len - 2 * stride - 1).rev() { + let x1 = x[i]; + let x2 = x[i + stride]; + x[i + stride] = c * x2 + s * x1; + x[i] = c * x1 + ms * x2; + } + } + } + + /// The decoder's spreading rotation (single block). + fn exp_rotation(x: &mut [f32], len: usize, k: usize, spread: usize) { + const SPREAD_FACTOR: [usize; 3] = [15, 10, 5]; + if 2 * k >= len || spread == 0 { + return; + } + let factor = SPREAD_FACTOR[spread - 1]; + let gain = len as f32 / (len + factor * k) as f32; + let theta = 0.5 * gain * gain; + let c = (0.5 * std::f32::consts::PI * theta).cos(); + let s = (0.5 * std::f32::consts::PI * (1.0 - theta)).cos(); + let mut stride2 = 0; + if len >= 8 { + stride2 = 1; + while stride2 * stride2 + stride2 < len { + stride2 += 1; + } + } + if stride2 != 0 { + exp_rotation1(x, len, stride2, s, c); + } + exp_rotation1(x, len, 1, c, s); + } + + #[test] + fn spreading_inverse_is_exact() { + for &len in &[8usize, 16, 24, 48, 96, 176] { + for k in [1usize, 2, 3, 5] { + let orig: Vec = (0..len).map(|i| ((i * 31 % 17) as f32 - 8.0) / 8.0).collect(); + let mut x = orig.clone(); + exp_rotation_inv(&mut x, len, k, SPREAD_NORMAL); + exp_rotation(&mut x, len, k, SPREAD_NORMAL); + for (a, b) in x.iter().zip(&orig) { + assert!((a - b).abs() < 1e-4, "len={len} k={k}"); + } + } + } + } + + /// PVQ decoder of RFC 6716 §4.3.4.2 (as in FilmCraft's decoder). + fn decode_pulses(n: usize, k: usize, dec: &mut RangeDecoder) -> Vec { + let w = k + 1; + let mut v = vec![0u64; (n + 1) * w]; + v[0] = 1; + for nn in 1..=n { + v[nn * w] = 1; + for kk in 1..=k { + v[nn * w + kk] = v[(nn - 1) * w + kk].saturating_add(v[nn * w + kk - 1]).saturating_add(v[(nn - 1) * w + kk - 1]); + } + } + let total = v[n * w + k]; + let mut i = if total >= 2 { dec.uint(total as u32) as u64 } else { 0 }; + let mut y = vec![0i32; n]; + let mut kk = k; + for j in 0..n { + let rem = n - j; + if kk == 0 { + break; + } + let vv = |nn: usize, kx: usize| v[nn * w + kx]; + let mut p = (vv(rem - 1, kk) + vv(rem, kk)) / 2; + let neg = i >= p; + if neg { + i -= p; + } + let k0 = kk; + p -= vv(rem - 1, kk); + while p > i && kk > 0 { + kk -= 1; + p -= vv(rem - 1, kk); + } + let mag = (k0 - kk) as i32; + y[j] = if neg { -mag } else { mag }; + i -= p.min(i); + } + y + } + + fn all_codewords(n: usize, k: usize) -> Vec> { + if n == 0 { + return if k == 0 { vec![vec![]] } else { vec![] }; + } + let mut out = Vec::new(); + for t in -(k as i32)..=(k as i32) { + for mut rest in all_codewords(n - 1, k - t.unsigned_abs() as usize) { + rest.insert(0, t); + out.push(rest); + } + } + out + } + + #[test] + fn pvq_index_roundtrip_exhaustive() { + let mut v = Vec::new(); + for n in 1..6usize { + for k in 1..6usize { + for y in all_codewords(n, k) { + let mut e = RangeEncoder::new(); + encode_pulses(&y, k, &mut e, &mut v); + let buf = e.done(16); + let mut d = RangeDecoder::new(&buf); + assert_eq!(decode_pulses(n, k, &mut d), y, "n={n} k={k}"); + } + } + } + } + + #[test] + fn pvq_search_finds_good_direction() { + let x: Vec = (0..32).map(|i| ((i as f32) * 0.7).sin()).collect(); + let mut y = vec![0i32; 32]; + for k in [1usize, 4, 16, 64] { + pvq_search(&x, k, &mut y); + assert_eq!(y.iter().map(|v| v.unsigned_abs() as usize).sum::(), k); + let xy: f32 = x.iter().zip(&y).map(|(a, &b)| a * b as f32).sum(); + let yy: f32 = y.iter().map(|&b| (b * b) as f32).sum(); + let xx: f32 = x.iter().map(|a| a * a).sum(); + let corr = xy / (xx * yy).sqrt(); + assert!(corr > if k >= 16 { 0.9 } else { 0.2 }, "k={k} corr={corr}"); + } + } +} diff --git a/crates/opusenc/src/energy.rs b/crates/opusenc/src/energy.rs new file mode 100644 index 00000000..0057ae33 --- /dev/null +++ b/crates/opusenc/src/energy.rs @@ -0,0 +1,230 @@ +//! Band energy quantisation: Laplace-coded coarse energy with inter/intra prediction, fine +//! energy and the final refinement bits (RFC 6716 §4.3.2, encoder direction). +//! +//! The quantised energies are updated with exactly the decoder's arithmetic so the prediction +//! state never drifts between encoder and decoder. + +use crate::range::RangeEncoder; +use crate::rate::MAX_FINE_BITS; +use crate::tables::*; + +const LAPLACE_MINP: u32 = 1; +const LAPLACE_NMIN: u32 = 16; + +fn laplace_get_freq1(fs0: u32, decay: u32) -> u32 { + let ft = 32768 - LAPLACE_MINP * (2 * LAPLACE_NMIN) - fs0; + (ft * (16384 - decay)) >> 15 +} + +/// Encodes a Laplace-distributed integer (`fs` = probability of 0 in Q15, `decay` in Q14). +/// Returns the value actually coded (very large magnitudes are clamped to the representable +/// range). +pub fn laplace_encode(enc: &mut RangeEncoder, value: i32, mut fs: u32, decay: u32) -> i32 { + let mut fl = 0u32; + let mut coded = value; + if value != 0 { + let s: i32 = if value < 0 { -1 } else { 0 }; + let val = value.abs(); + fl = fs; + fs = laplace_get_freq1(fs, decay); + let mut i = 1; + while fs > 0 && i < val { + fs *= 2; + fl += fs + 2 * LAPLACE_MINP; + fs = (fs * decay) >> 15; + i += 1; + } + if fs == 0 { + let ndi_max = (32768 - fl + LAPLACE_MINP - 1) as i32; + let ndi_max = (ndi_max - s) >> 1; + let di = (val - i).min(ndi_max - 1); + fl = (fl as i32 + (2 * di + 1 + s) * LAPLACE_MINP as i32) as u32; + fs = LAPLACE_MINP.min(32768 - fl); + let mag = i + di; + coded = if s < 0 { -mag } else { mag }; + } else { + fs += LAPLACE_MINP; + if s == 0 { + fl += fs; + } + } + } + enc.encode_bin(fl, fl + fs, 15); + coded +} + +/// `quant_coarse_energy` for one prediction mode. `e` holds the target log2 energies (relative +/// to `E_MEANS`), `old_e` the quantised energies, updated in place. `budget` is the frame size in +/// bits. +#[allow(clippy::too_many_arguments)] +pub fn quant_coarse_energy( + start: usize, + end: usize, + e: &[f32; 2 * NB_EBANDS], + old_e: &mut [f32; 2 * NB_EBANDS], + intra: bool, + enc: &mut RangeEncoder, + c: usize, + lm: usize, + budget: i32, +) { + let prob = &E_PROB_MODEL[lm][intra as usize]; + let (coef, beta) = if intra { (0.0, BETA_INTRA) } else { (PRED_COEF[lm], BETA_COEF[lm]) }; + let mut prev = [0f32; 2]; + for i in start..end { + for ch in 0..c { + let idx = i + ch * NB_EBANDS; + let oldc = old_e[idx].max(-9.0); + let x = e[idx]; + let f = x - coef * oldc - prev[ch]; + let mut qi = (f + 0.5).floor().clamp(-64.0, 64.0) as i32; + let tell = enc.tell(); + // Keep a few bits per remaining band in reserve when the budget runs out. + let bits_left = budget - tell - 3 * c as i32 * (end - i) as i32; + if i != start && bits_left < 30 { + if bits_left < 24 { + qi = qi.min(1); + } + if bits_left < 16 { + qi = qi.max(-1); + } + } + if budget - tell >= 15 { + let pi = 2 * i.min(20); + qi = laplace_encode(enc, qi, (prob[pi] as u32) << 7, (prob[pi + 1] as u32) << 6); + } else if budget - tell >= 2 { + qi = qi.clamp(-1, 1); + enc.icdf(if qi < 0 { (-2 * qi - 1) as usize } else { (2 * qi) as usize }, &SMALL_ENERGY_ICDF, 2); + } else if budget - tell >= 1 { + qi = qi.clamp(-1, 0); + enc.bit_logp(qi == -1, 1); + } else { + qi = -1; + } + let q = qi as f32; + old_e[idx] = oldc; + let tmp = coef * old_e[idx] + prev[ch] + q; + old_e[idx] = tmp; + prev[ch] = prev[ch] + q - beta * q; + } + } +} + +/// `quant_fine_energy`: raw bits refining each band's quantised energy. +pub fn quant_fine_energy( + start: usize, + end: usize, + e: &[f32; 2 * NB_EBANDS], + old_e: &mut [f32; 2 * NB_EBANDS], + fine_quant: &[i32; NB_EBANDS], + enc: &mut RangeEncoder, + c: usize, +) { + for i in start..end { + let fq = fine_quant[i]; + if fq <= 0 { + continue; + } + for ch in 0..c { + let idx = i + ch * NB_EBANDS; + let err = e[idx] - old_e[idx]; + let levels = 1i32 << fq; + let q2 = (((err + 0.5) * levels as f32).floor() as i32).clamp(0, levels - 1); + enc.bits(q2 as u32, fq as u32); + let offset = (q2 as f32 + 0.5) * (1 << (14 - fq)) as f32 / 16384.0 - 0.5; + old_e[idx] += offset; + } + } +} + +/// `quant_energy_finalise`: spends the bits left at the end of the frame on one more bit of +/// energy resolution per band, in priority order. +#[allow(clippy::too_many_arguments)] +pub fn quant_energy_finalise( + start: usize, + end: usize, + e: &[f32; 2 * NB_EBANDS], + old_e: &mut [f32; 2 * NB_EBANDS], + fine_quant: &[i32; NB_EBANDS], + fine_priority: &[i32; NB_EBANDS], + mut bits_left: i32, + enc: &mut RangeEncoder, + c: usize, +) { + for prio in 0..2 { + let mut i = start; + while i < end && bits_left >= c as i32 { + if fine_quant[i] >= MAX_FINE_BITS || fine_priority[i] != prio { + i += 1; + continue; + } + for ch in 0..c { + let idx = i + ch * NB_EBANDS; + let q2 = (e[idx] - old_e[idx] >= 0.0) as u32; + enc.bits(q2, 1); + let offset = (q2 as f32 - 0.5) * (1 << (14 - fine_quant[i] - 1)) as f32 / 16384.0; + old_e[idx] += offset; + bits_left -= 1; + } + i += 1; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::range::dec::RangeDecoder; + + /// Laplace decoder of RFC 6716 §4.3.2.1 (as in FilmCraft's decoder). + fn laplace_decode(dec: &mut RangeDecoder, mut fs: u32, decay: u32) -> i32 { + let mut val = 0i32; + let fm = dec.decode(32768); + let mut fl = 0u32; + if fm >= fs { + val += 1; + fl = fs; + fs = laplace_get_freq1(fs, decay) + LAPLACE_MINP; + while fs > LAPLACE_MINP && fm >= fl + 2 * fs { + fs *= 2; + fl += fs; + fs = (((fs - 2 * LAPLACE_MINP) * decay) >> 15) + LAPLACE_MINP; + val += 1; + } + if fs <= LAPLACE_MINP { + let di = (fm - fl) >> 1; + val += di as i32; + fl += 2 * di * LAPLACE_MINP; + } + if fm < fl + fs { + val = -val; + } else { + fl += fs; + } + } + dec.update(fl, (fl + fs).min(32768), 32768); + val + } + + #[test] + fn laplace_roundtrip_all_models() { + for lm in 0..4 { + for intra in 0..2 { + for band in 0..21 { + let prob = &E_PROB_MODEL[lm][intra]; + let (fs, decay) = ((prob[2 * band] as u32) << 7, (prob[2 * band + 1] as u32) << 6); + let vals: Vec = (-64..=64).collect(); + let mut e = RangeEncoder::new(); + let coded: Vec = vals.iter().map(|&v| laplace_encode(&mut e, v, fs, decay)).collect(); + let size = (e.tell() as usize).div_ceil(8); + let buf = e.done(size); + let mut d = RangeDecoder::new(&buf); + for (&v, &c) in vals.iter().zip(&coded) { + assert!(c == v || (c.signum() == v.signum() && c.abs() < v.abs()), "clamp {v} -> {c}"); + assert_eq!(laplace_decode(&mut d, fs, decay), c, "lm={lm} intra={intra} band={band} v={v}"); + } + } + } + } + } +} diff --git a/crates/opusenc/src/lib.rs b/crates/opusenc/src/lib.rs new file mode 100644 index 00000000..1e7a2cec --- /dev/null +++ b/crates/opusenc/src/lib.rs @@ -0,0 +1,311 @@ +//! Clean-room pure-Rust Opus encoder (RFC 6716 CELT-only mode; RFC 7845 `OpusHead`). +//! +//! Produces fullband (20 kHz) 48 kHz Opus packets of 20 ms, mono or stereo, at a constant +//! packet size derived from the requested bitrate. Each packet is a single CELT frame (TOC +//! configuration 31, frame-count code 0). Stereo is coded as dual (independent L/R) stereo. +//! +//! Layer L0: no dependencies beyond `std`; no `unsafe`; builds for `wasm32-unknown-unknown`. +//! +//! ``` +//! use effectcraft_opusenc::OpusEncoder; +//! let mut enc = OpusEncoder::new(2, 128_000); +//! let pcm = vec![0.0f32; OpusEncoder::FRAME_SIZE * 2]; +//! let packet = enc.encode_float(&pcm); +//! assert_eq!(packet[0] >> 3, 31); // CELT-only, fullband, 20 ms +//! assert_eq!(enc.opus_head().len(), 19); +//! ``` + +// The bit-allocation code mirrors the normative integer arithmetic; keep C-like shift expressions. +#![allow(clippy::precedence, clippy::int_plus_one)] + +mod bands; +mod energy; +mod mdct; +mod range; +mod rate; +mod tables; + +use std::sync::OnceLock; + +use bands::{SPREAD_NORMAL, quant_all_bands}; +use energy::{quant_coarse_energy, quant_energy_finalise, quant_fine_energy}; +use mdct::Mdct; +use range::{BITRES, RangeEncoder}; +use rate::{Mode, compute_allocation}; +use tables::*; + +/// Samples per channel in one 20 ms frame at 48 kHz. +const N: usize = 960; +/// LM of a 20 ms frame (8 short blocks). +const LM: usize = 3; +/// Smallest CELT payload the encoder produces (bytes). +const MIN_FRAME_BYTES: usize = 16; +/// Largest CELT payload allowed in one frame (RFC 6716 §3.4, R2). +const MAX_FRAME_BYTES: usize = 1275; + +fn mode() -> &'static Mode { + static MODE: OnceLock = OnceLock::new(); + MODE.get_or_init(Mode::new) +} + +/// Opus encoder for one mono or stereo stream at 48 kHz. +pub struct OpusEncoder { + channels: usize, + /// CELT payload bytes per packet (the packet is one byte longer: the TOC). + frame_bytes: usize, + mdct: Mdct, + /// Last input sample of each channel (pre-emphasis filter memory, ×32768). + preemph_mem: [f32; 2], + /// Last `OVERLAP` pre-emphasised samples of each channel. + hist: Vec, + /// Quantised band energies, exactly as the decoder holds them. + old_band_e: [f32; 2 * NB_EBANDS], + block: Vec, + freq: Vec, +} + +impl OpusEncoder { + /// Samples per channel per packet (20 ms at 48 kHz). + pub const FRAME_SIZE: usize = N; + + /// Creates an encoder for `channels` (1 or 2) channels of 48 kHz audio at `bitrate_bps` + /// (total for all channels; packets are constant-size, clamped to 6.8–510 kb/s). + pub fn new(channels: u8, bitrate_bps: u32) -> Self { + assert!(channels == 1 || channels == 2, "Opus family-0 streams have 1 or 2 channels"); + let c = channels as usize; + let packet_bytes = (bitrate_bps as usize).div_ceil(400); + let frame_bytes = packet_bytes.saturating_sub(1).clamp(MIN_FRAME_BYTES, MAX_FRAME_BYTES); + OpusEncoder { + channels: c, + frame_bytes, + mdct: Mdct::new(N), + preemph_mem: [0.0; 2], + hist: vec![0.0; c * OVERLAP], + old_band_e: [-28.0; 2 * NB_EBANDS], + block: vec![0.0; N + OVERLAP], + freq: vec![0.0; c * N], + } + } + + /// Number of channels. + pub fn channels(&self) -> u8 { + self.channels as u8 + } + + /// Actual bitrate in bits per second (packet size × 50 packets per second). + pub fn bitrate(&self) -> u32 { + ((self.frame_bytes + 1) * 8 * 50) as u32 + } + + /// Samples at 48 kHz the decoder must discard from the start of the stream (the MDCT + /// overlap); this is the `OpusHead` pre-skip. + pub fn pre_skip(&self) -> u16 { + OVERLAP as u16 + } + + /// The 19-byte `OpusHead` identification header (RFC 7845 §5.1): version 1, channel count, + /// pre-skip, input sample rate 48000, output gain 0, channel mapping family 0. + pub fn opus_head(&self) -> Vec { + let mut h = Vec::with_capacity(19); + h.extend_from_slice(b"OpusHead"); + h.push(1); + h.push(self.channels as u8); + h.extend_from_slice(&self.pre_skip().to_le_bytes()); + h.extend_from_slice(&48_000u32.to_le_bytes()); + h.extend_from_slice(&0i16.to_le_bytes()); + h.push(0); + h + } + + /// Encodes one 20 ms frame of interleaved f32 samples in [-1, 1] (exactly + /// `FRAME_SIZE * channels` values) and returns one Opus packet (TOC byte + CELT frame). + pub fn encode_float(&mut self, pcm: &[f32]) -> Vec { + let c = self.channels; + assert_eq!(pcm.len(), N * c, "encode_float takes exactly FRAME_SIZE samples per channel"); + + // Pre-emphasis, the MDCT and the band energies. + let mut band_e = [0f32; 2 * NB_EBANDS]; + let mut log_e = [0f32; 2 * NB_EBANDS]; + let mut peak = 0f32; + for ch in 0..c { + self.block[..OVERLAP].copy_from_slice(&self.hist[ch * OVERLAP..(ch + 1) * OVERLAP]); + let mut mem = self.preemph_mem[ch]; + for j in 0..N { + let s = pcm[j * c + ch]; + let s = if s.is_finite() { s.clamp(-2.0, 2.0) * 32768.0 } else { 0.0 }; + let v = s - PREEMPH * mem; + mem = s; + self.block[OVERLAP + j] = v; + } + self.preemph_mem[ch] = mem; + self.hist[ch * OVERLAP..(ch + 1) * OVERLAP].copy_from_slice(&self.block[N..]); + peak = self.block.iter().fold(peak, |p, v| p.max(v.abs())); + let freq = &mut self.freq[ch * N..(ch + 1) * N]; + self.mdct.forward(&self.block, freq); + for i in 0..NB_EBANDS { + let lo = (EBANDS[i] as usize) << LM; + let hi = (EBANDS[i + 1] as usize) << LM; + let e = (1e-27f32 + freq[lo..hi].iter().map(|v| v * v).sum::()).sqrt(); + band_e[ch * NB_EBANDS + i] = e; + log_e[ch * NB_EBANDS + i] = e.log2() - E_MEANS[i]; + let g = 1.0 / e; + for v in freq[lo..hi].iter_mut() { + *v *= g; + } + } + } + let silence = peak < 1e-4; + let toc = (31u8 << 3) | if c == 2 { 4 } else { 0 }; + let mut packet = vec![toc]; + packet.extend_from_slice(&self.encode_celt(&log_e, silence)); + packet + } + + /// Codes one CELT frame (RFC 6716 §4.3, in bitstream order). + fn encode_celt(&mut self, log_e: &[f32; 2 * NB_EBANDS], silence: bool) -> Vec { + let m = mode(); + let c = self.channels; + let (start, end) = (0usize, NB_EBANDS); + let mut enc = RangeEncoder::new(); + if c == 1 { + for i in 0..NB_EBANDS { + self.old_band_e[i] = self.old_band_e[i].max(self.old_band_e[NB_EBANDS + i]); + } + } + if silence { + // The silence flag; the decoder treats every remaining bit as consumed. + enc.bit_logp(true, 15); + self.old_band_e = [-28.0; 2 * NB_EBANDS]; + return enc.done(2); + } + let len = self.frame_bytes; + let total_bits_raw = len as i32 * 8; + enc.bit_logp(false, 15); + // No pitch post-filter. + if enc.tell() + 16 <= total_bits_raw { + enc.bit_logp(false, 1); + } + // Long blocks only. + if enc.tell() + 3 <= total_bits_raw { + enc.bit_logp(false, 3); + } + // Coarse energy, choosing intra or inter prediction by trial (whichever is cheaper). + if enc.tell() + 3 <= total_bits_raw { + let mut enc_intra = enc.clone(); + let mut old_intra = self.old_band_e; + enc_intra.bit_logp(true, 3); + quant_coarse_energy(start, end, log_e, &mut old_intra, true, &mut enc_intra, c, LM, total_bits_raw); + enc.bit_logp(false, 3); + let mut old_inter = self.old_band_e; + quant_coarse_energy(start, end, log_e, &mut old_inter, false, &mut enc, c, LM, total_bits_raw); + if enc_intra.tell_frac() < enc.tell_frac() { + enc = enc_intra; + self.old_band_e = old_intra; + } else { + self.old_band_e = old_inter; + } + } else { + quant_coarse_energy(start, end, log_e, &mut self.old_band_e, false, &mut enc, c, LM, total_bits_raw); + } + // TF resolution: no changes. + { + let mut budget = total_bits_raw; + let mut tell = enc.tell(); + let mut logp = 4; + let tf_select_rsv = LM > 0 && tell + logp + 1 <= budget; + budget -= tf_select_rsv as i32; + for _ in start..end { + if tell + logp <= budget { + enc.bit_logp(false, logp as u32); + tell = enc.tell(); + } + logp = 5; + } + if tf_select_rsv && TF_SELECT_TABLE[LM][0] != TF_SELECT_TABLE[LM][2] { + enc.bit_logp(false, 1); + } + } + if enc.tell() + 4 <= total_bits_raw { + enc.icdf(SPREAD_NORMAL, &SPREAD_ICDF, 5); + } + // Dynamic allocation: no boosts. + let cap = m.init_caps(LM, c); + let offsets = [0i32; NB_EBANDS]; + let total_bits = total_bits_raw << BITRES; + let mut tellf = enc.tell_frac(); + for &cp in cap.iter().take(end).skip(start) { + if tellf + (6 << BITRES) < total_bits && 0 < cp { + enc.bit_logp(false, 6); + tellf = enc.tell_frac(); + } + } + let alloc_trim = 5; + if tellf + (6 << BITRES) <= total_bits { + enc.icdf(alloc_trim as usize, &TRIM_ICDF, 7); + } + let bits = (total_bits_raw << BITRES) - enc.tell_frac() - 1; + let alloc = compute_allocation(m, start, end, &offsets, &cap, alloc_trim, bits, c, LM, &mut enc); + quant_fine_energy(start, end, log_e, &mut self.old_band_e, &alloc.fine_quant, &mut enc, c); + quant_all_bands( + m, + start, + end, + &self.freq, + N, + c, + &alloc.pulses, + SPREAD_NORMAL, + alloc.dual_stereo, + alloc.intensity, + total_bits_raw << BITRES, + alloc.balance, + &mut enc, + LM, + alloc.coded_bands, + ); + let bits_left = total_bits_raw - enc.tell(); + quant_energy_finalise(start, end, log_e, &mut self.old_band_e, &alloc.fine_quant, &alloc.fine_priority, bits_left, &mut enc, c); + debug_assert!(enc.tell() <= total_bits_raw, "frame overflow: {} > {}", enc.tell(), total_bits_raw); + if c == 1 { + let (a, b) = self.old_band_e.split_at_mut(NB_EBANDS); + b.copy_from_slice(a); + } + enc.done(len) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn opus_head_layout() { + let e = OpusEncoder::new(2, 96_000); + let h = e.opus_head(); + assert_eq!(&h[..8], b"OpusHead"); + assert_eq!(h[8], 1); + assert_eq!(h[9], 2); + assert_eq!(u16::from_le_bytes([h[10], h[11]]), e.pre_skip()); + assert_eq!(u32::from_le_bytes([h[12], h[13], h[14], h[15]]), 48_000); + assert_eq!(&h[16..], &[0, 0, 0]); + } + + #[test] + fn packet_sizes_follow_bitrate() { + for (ch, rate) in [(1u8, 64_000u32), (2, 128_000), (2, 256_000)] { + let mut e = OpusEncoder::new(ch, rate); + let pcm: Vec = (0..N * ch as usize).map(|i| (i as f32 * 0.05).sin() * 0.5).collect(); + let p = e.encode_float(&pcm); + assert_eq!(p.len(), rate as usize / 400); + assert_eq!(p[0], 0xF8 | if ch == 2 { 4 } else { 0 }); + assert_eq!(e.bitrate(), rate); + } + } + + #[test] + fn silence_is_a_tiny_packet() { + let mut e = OpusEncoder::new(1, 64_000); + let p = e.encode_float(&vec![0.0; N]); + assert_eq!(p.len(), 3); + } +} diff --git a/crates/opusenc/src/mdct.rs b/crates/opusenc/src/mdct.rs new file mode 100644 index 00000000..814de037 --- /dev/null +++ b/crates/opusenc/src/mdct.rs @@ -0,0 +1,293 @@ +//! Forward MDCT with the CELT low-overlap window (RFC 6716 §4.3.7, run in the analysis +//! direction). +//! +//! A 20 ms frame is a 1920-point MDCT (960 coefficients) whose window is zero for 420 samples, +//! rises over the 120-sample overlap, is flat for 840 samples, falls over the next 120 samples +//! and is zero again for the last 420. The input block is therefore the 120 overlap samples kept +//! from the previous frame followed by the 960 new samples. The MDCT is folded into a DCT-IV, +//! which is computed with an N/2-point complex FFT. + +use crate::tables::OVERLAP; + +#[derive(Clone, Copy, Default, Debug)] +pub struct Cpx { + pub re: f32, + pub im: f32, +} + +impl Cpx { + #[inline] + fn mul(self, o: Cpx) -> Cpx { + Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re } + } + #[inline] + fn add(self, o: Cpx) -> Cpx { + Cpx { re: self.re + o.re, im: self.im + o.im } + } +} + +/// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled (decimation in time, recursive). +pub struct Fft { + n: usize, + factors: Vec, + twiddle: Vec, +} + +impl Fft { + pub fn new(n: usize) -> Fft { + let mut factors = Vec::new(); + let mut m = n; + for p in [4usize, 2, 3, 5] { + while m.is_multiple_of(p) { + factors.push(p); + m /= p; + } + } + assert_eq!(m, 1, "unsupported FFT size {n}"); + let twiddle = (0..n) + .map(|k| { + let ph = -2.0 * std::f64::consts::PI * k as f64 / n as f64; + Cpx { re: ph.cos() as f32, im: ph.sin() as f32 } + }) + .collect(); + Fft { n, factors, twiddle } + } + + pub fn process(&self, data: &mut [Cpx], scratch: &mut Vec) { + scratch.clear(); + scratch.extend_from_slice(&data[..self.n]); + self.rec(&mut data[..self.n], scratch, 1, self.n, 0, 1); + } + + fn rec(&self, out: &mut [Cpx], inp: &[Cpx], in_stride: usize, n: usize, fi: usize, tw_stride: usize) { + let p = self.factors[fi]; + let m = n / p; + if m == 1 { + for (i, o) in out.iter_mut().enumerate().take(p) { + *o = inp[i * in_stride]; + } + } else { + for q in 0..p { + self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p); + } + } + let big = self.n; + let mut t = [Cpx::default(); 5]; + for k in 0..m { + for (q, tq) in t.iter_mut().enumerate().take(p) { + *tq = out[q * m + k].mul(self.twiddle[(q * k * tw_stride) % big]); + } + for q2 in 0..p { + let mut acc = Cpx::default(); + for (q, tq) in t.iter().enumerate().take(p) { + acc = acc.add(tq.mul(self.twiddle[((q * q2) % p) * (big / p)])); + } + out[q2 * m + k] = acc; + } + } + } +} + +/// Forward MDCT producing `n` coefficients from a block of `n + OVERLAP` samples. +pub struct Mdct { + /// Number of coefficients (half the MDCT length). + n: usize, + fft: Fft, + pre: Vec, + post: Vec, + /// Rising half of the low-overlap window (RFC 6716 §4.3.7). + pub window: [f32; OVERLAP], + scale: f32, + z: Vec, + scratch: Vec, + v: Vec, +} + +impl Mdct { + pub fn new(n: usize) -> Mdct { + let n2 = n / 2; + let pi = std::f64::consts::PI; + let pre = (0..n2) + .map(|m| { + let ph = -pi * m as f64 / n as f64; + Cpx { re: ph.cos() as f32, im: ph.sin() as f32 } + }) + .collect(); + let post = (0..n2) + .map(|p| { + let ph = -pi * (p as f64 + 0.25) / n as f64; + Cpx { re: ph.cos() as f32, im: ph.sin() as f32 } + }) + .collect(); + let mut window = [0f32; OVERLAP]; + for (i, w) in window.iter_mut().enumerate() { + let x = std::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / OVERLAP as f64; + let s = x.sin(); + *w = (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32; + } + Mdct { n, fft: Fft::new(n2), pre, post, window, scale: 2.0 / n as f32, z: Vec::new(), scratch: Vec::new(), v: vec![0.0; n] } + } + + /// Transforms `block` (`n + OVERLAP` samples: the previous frame's last `OVERLAP` samples, then + /// `n` new ones) into `out[..n]`, scaled so that the RFC 6716 decoder's inverse MDCT and + /// overlap-add reproduce the input. + pub fn forward(&mut self, block: &[f32], out: &mut [f32]) { + let n = self.n; + let n2 = n / 2; + debug_assert_eq!(block.len(), n + OVERLAP); + // Place the windowed block into the (virtual) 2n-sample MDCT input u at offset `pad`. + let pad = (n - OVERLAP) / 2; + let w = &self.window; + let u = |t: usize| -> f32 { + if t < pad || t >= pad + n + OVERLAP { + return 0.0; + } + let j = t - pad; + let g = if j < OVERLAP { + w[j] + } else if j >= n { + w[OVERLAP - 1 - (j - n)] + } else { + 1.0 + }; + block[j] * g + }; + // Fold to a DCT-IV input: v = (-c_r - d, a - b_r) over the four quarters of u. + for m in 0..n2 { + self.v[m] = -u(n + n2 - 1 - m) - u(n + n2 + m); + self.v[n2 + m] = u(m) - u(n2 + n2 - 1 - m); + } + // DCT-IV via an n/2-point complex FFT. + self.z.clear(); + for m in 0..n2 { + let wv = Cpx { re: self.v[2 * m], im: self.v[n - 1 - 2 * m] }; + self.z.push(wv.mul(self.pre[m])); + } + self.fft.process(&mut self.z, &mut self.scratch); + for p in 0..n2 { + let y = self.z[p].mul(self.post[p]); + out[2 * p] = y.re * self.scale; + out[n - 1 - 2 * p] = -y.im * self.scale; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Direct MDCT definition X[k] = sum u[t] cos(pi/N (t + 1/2 + N/2)(k + 1/2)). + fn direct(block: &[f32], n: usize, window: &[f32; OVERLAP]) -> Vec { + let pad = (n - OVERLAP) / 2; + let mut u = vec![0f64; 2 * n]; + for (j, &b) in block.iter().enumerate() { + let g = if j < OVERLAP { + window[j] + } else if j >= n { + window[OVERLAP - 1 - (j - n)] + } else { + 1.0 + }; + u[pad + j] = (b * g) as f64; + } + (0..n) + .map(|k| { + (0..2 * n).map(|t| u[t] * (std::f64::consts::PI / n as f64 * (t as f64 + 0.5 + n as f64 / 2.0) * (k as f64 + 0.5)).cos()).sum::() * 2.0 + / n as f64 + }) + .collect() + } + + #[test] + fn matches_direct_formula() { + for &n in &[240usize, 960] { + let block: Vec = (0..n + OVERLAP).map(|i| ((i * 37 % 101) as f32 - 50.0) / 7.0).collect(); + let mut m = Mdct::new(n); + let mut out = vec![0f32; n]; + m.forward(&block, &mut out); + let d = direct(&block, n, &m.window); + for k in 0..n { + assert!((out[k] as f64 - d[k]).abs() < 1e-2, "n={n} k={k}: {} vs {}", out[k], d[k]); + } + } + } + + // ---- Replica of the RFC 6716 decoder inverse MDCT (as in FilmCraft's decoder) ---- + + fn imdct_backward(n_full: usize, input: &[f32], out: &mut [f32], window: &[f32]) { + let n2 = n_full >> 1; + let n4 = n_full >> 2; + let t: Vec = (0..n2).map(|i| (2.0 * std::f64::consts::PI * (i as f64 + 0.125) / n_full as f64).cos() as f32).collect(); + let fft = Fft::new(n4); + let mut z = Vec::new(); + for i in 0..n4 { + let x1 = input[2 * i]; + let x2 = input[n2 - 1 - 2 * i]; + let yr = x2 * t[i] + x1 * t[n4 + i]; + let yi = x1 * t[i] - x2 * t[n4 + i]; + z.push(Cpx { re: yi, im: yr }); + } + fft.process(&mut z, &mut Vec::new()); + let overlap = OVERLAP; + let base = overlap >> 1; + let y = &mut out[base..base + n2]; + for i in 0..n4.div_ceil(2) { + let front = z[i]; + let back = z[n4 - 1 - i]; + let (re, im) = (front.im, front.re); + let (t0, t1) = (t[i], t[n4 + i]); + let yr0 = re * t0 + im * t1; + let yi0 = re * t1 - im * t0; + let (re, im) = (back.im, back.re); + let (t0, t1) = (t[n4 - i - 1], t[n2 - i - 1]); + let yr1 = re * t0 + im * t1; + let yi1 = re * t1 - im * t0; + y[2 * i] = yr0; + y[n2 - 1 - 2 * i] = yi0; + y[n2 - 2 - 2 * i] = yr1; + y[2 * i + 1] = yi1; + } + for i in 0..overlap / 2 { + let x1 = out[overlap - 1 - i]; + let x2 = out[i]; + let w1 = window[i]; + let w2 = window[overlap - 1 - i]; + out[i] = x2 * w2 - x1 * w1; + out[overlap - 1 - i] = x2 * w1 + x1 * w2; + } + } + + /// MDCT analysis followed by the decoder's IMDCT + TDAC overlap-add reproduces the input + /// delayed by exactly `OVERLAP` samples. + #[test] + fn perfect_reconstruction_through_decoder_imdct() { + let n = 960; + let frames = 5; + let sig: Vec = (0..n * frames).map(|i| (i as f32 * 0.013).sin() * 1000.0 + ((i * 7919) % 211) as f32 - 105.0).collect(); + let mut m = Mdct::new(n); + let mut hist = vec![0f32; OVERLAP]; + // Decoder memory: [frame output n][folded tail OVERLAP/2] laid out like decode_mem. + let mut mem = vec![0f32; n + OVERLAP]; + let mut decoded = Vec::new(); + for f in 0..frames { + let mut block = hist.clone(); + block.extend_from_slice(&sig[f * n..(f + 1) * n]); + hist.copy_from_slice(&block[n..]); + let mut coefs = vec![0f32; n]; + m.forward(&block, &mut coefs); + // Shift: the previous folded tail (at n..n+OVERLAP/2) moves to 0..OVERLAP/2. + mem.copy_within(n..n + OVERLAP / 2, 0); + imdct_backward(2 * n, &coefs, &mut mem, &m.window); + decoded.extend_from_slice(&mem[..n]); + } + let mut err = 0f64; + let mut sum = 0f64; + for t in OVERLAP..n * frames { + let d = decoded[t] as f64 - sig[t - OVERLAP] as f64; + err += d * d; + sum += (sig[t - OVERLAP] as f64).powi(2); + } + let snr = 10.0 * (sum / err).log10(); + assert!(snr > 80.0, "reconstruction SNR {snr:.1} dB"); + } +} diff --git a/crates/opusenc/src/range.rs b/crates/opusenc/src/range.rs new file mode 100644 index 00000000..daedd58a --- /dev/null +++ b/crates/opusenc/src/range.rs @@ -0,0 +1,472 @@ +//! Range encoder (RFC 6716 §5.1), the exact inverse of the §4.1 range decoder. +//! +//! Range-coded symbols are written from the front of the buffer and raw bits from the back; the +//! bit accounting ([`RangeEncoder::tell`], [`RangeEncoder::tell_frac`]) matches the decoder's, so +//! every budget decision the decoder takes can be mirrored exactly. + +const SYM_BITS: u32 = 8; +const CODE_BITS: u32 = 32; +const SYM_MAX: u32 = (1 << SYM_BITS) - 1; +const CODE_SHIFT: u32 = CODE_BITS - SYM_BITS - 1; +const CODE_TOP: u32 = 1 << (CODE_BITS - 1); +const CODE_BOT: u32 = CODE_TOP >> SYM_BITS; +/// Bit resolution of [`RangeEncoder::tell_frac`] (1/8 bit). +pub const BITRES: u32 = 3; + +/// Number of bits needed to represent `x` (0 for 0). +#[inline] +pub fn ilog(x: u32) -> i32 { + (32 - x.leading_zeros()) as i32 +} + +#[derive(Clone, Debug)] +pub struct RangeEncoder { + out: Vec, + end: Vec, + end_window: u32, + nend_bits: i32, + nbits_total: i32, + rng: u32, + low: u32, + rem: i32, + ext: u32, +} + +impl Default for RangeEncoder { + fn default() -> Self { + Self::new() + } +} + +impl RangeEncoder { + pub fn new() -> Self { + RangeEncoder { + out: Vec::new(), + end: Vec::new(), + end_window: 0, + nend_bits: 0, + nbits_total: (CODE_BITS + 1) as i32, + rng: CODE_TOP, + low: 0, + rem: -1, + ext: 0, + } + } + + /// Current range (equals the decoder's range after the same symbols). + #[cfg(test)] + pub fn rng(&self) -> u32 { + self.rng + } + + fn carry_out(&mut self, c: u32) { + if c != SYM_MAX { + let carry = c >> SYM_BITS; + if self.rem >= 0 { + self.out.push((self.rem as u32 + carry) as u8); + } + if self.ext > 0 { + let sym = (SYM_MAX + carry) & SYM_MAX; + for _ in 0..self.ext { + self.out.push(sym as u8); + } + self.ext = 0; + } + self.rem = (c & SYM_MAX) as i32; + } else { + self.ext += 1; + } + } + + #[inline] + fn normalize(&mut self) { + while self.rng <= CODE_BOT { + self.carry_out(self.low >> CODE_SHIFT); + self.low = (self.low << SYM_BITS) & (CODE_TOP - 1); + self.rng <<= SYM_BITS; + self.nbits_total += SYM_BITS as i32; + } + } + + /// Encodes the symbol with cumulative range `[fl, fh)` of total `ft`. + pub fn encode(&mut self, fl: u32, fh: u32, ft: u32) { + let r = self.rng / ft; + if fl > 0 { + self.low += self.rng - r * (ft - fl); + self.rng = r * (fh - fl); + } else { + self.rng -= r * (ft - fh); + } + self.normalize(); + } + + /// Like [`Self::encode`] with `ft = 1 << bits`. + pub fn encode_bin(&mut self, fl: u32, fh: u32, bits: u32) { + let r = self.rng >> bits; + let ft = 1u32 << bits; + if fl > 0 { + self.low += self.rng - r * (ft - fl); + self.rng = r * (fh - fl); + } else { + self.rng -= r * (ft - fh); + } + self.normalize(); + } + + /// Encodes a binary symbol whose probability of being 1 is `1/2^logp`. + pub fn bit_logp(&mut self, val: bool, logp: u32) { + let r = self.rng; + let s = r >> logp; + let r2 = r - s; + if val { + self.low += r2; + self.rng = s; + } else { + self.rng = r2; + } + self.normalize(); + } + + /// Encodes symbol `s` with an "inverse CDF" table (`ft = 1 << ftb`). + pub fn icdf(&mut self, s: usize, icdf: &[u8], ftb: u32) { + let r = self.rng >> ftb; + if s > 0 { + self.low += self.rng - r * icdf[s - 1] as u32; + self.rng = r * (icdf[s - 1] as u32 - icdf[s] as u32); + } else { + self.rng -= r * icdf[s] as u32; + } + self.normalize(); + } + + /// Encodes a uniformly distributed integer `fl` in `[0, ft)` (`ft >= 2`). + pub fn uint(&mut self, fl: u32, ft: u32) { + debug_assert!(ft > 1 && fl < ft); + let ft1 = ft - 1; + let ftb = ilog(ft1); + if ftb > 8 { + let ftb = (ftb - 8) as u32; + let top = (ft1 >> ftb) + 1; + let v = fl >> ftb; + self.encode(v, v + 1, top); + self.bits(fl & ((1 << ftb) - 1), ftb); + } else { + self.encode(fl, fl + 1, ft); + } + } + + /// Writes `bits` raw bits (0..=25) to the end of the buffer. + pub fn bits(&mut self, val: u32, bits: u32) { + if bits == 0 { + return; + } + let mut window = self.end_window; + let mut used = self.nend_bits; + if used as u32 + bits > CODE_BITS { + while used >= SYM_BITS as i32 { + self.end.push((window & SYM_MAX) as u8); + window >>= SYM_BITS; + used -= SYM_BITS as i32; + } + } + window |= val << used; + used += bits as i32; + self.end_window = window; + self.nend_bits = used; + self.nbits_total += bits as i32; + } + + /// Number of bits written so far (rounded up), identical to the decoder's `tell`. + #[inline] + pub fn tell(&self) -> i32 { + self.nbits_total - ilog(self.rng) + } + + /// Number of 1/8 bits written so far (rounded up), identical to the decoder's `tell_frac`. + pub fn tell_frac(&self) -> i32 { + let nbits = (self.nbits_total as u32) << BITRES; + let mut l = ilog(self.rng) as u32; + let mut r = self.rng >> (l - 16); + for _ in 0..BITRES { + r = (r * r) >> 15; + let b = r >> 16; + l = (l << 1) | b; + r >>= b; + } + nbits.wrapping_sub(l) as i32 + } + + /// Finalises the stream into a buffer of exactly `size` bytes (range data at the front, raw + /// bits at the back, zero padding in between; a partial raw-bit byte is merged into the byte + /// just before the full raw-bit bytes). + pub fn done(mut self, size: usize) -> Vec { + let mut l = CODE_BITS as i32 - ilog(self.rng); + let mut msk = (CODE_TOP - 1) >> l; + let mut end = (self.low.wrapping_add(msk)) & !msk; + if (end | msk) >= self.low.wrapping_add(self.rng) { + l += 1; + msk >>= 1; + end = (self.low.wrapping_add(msk)) & !msk; + } + while l > 0 { + self.carry_out(end >> CODE_SHIFT); + end = (end << SYM_BITS) & (CODE_TOP - 1); + l -= SYM_BITS as i32; + } + if self.rem >= 0 || self.ext > 0 { + self.carry_out(0); + } + let mut window = self.end_window; + let mut used = self.nend_bits; + while used >= SYM_BITS as i32 { + self.end.push((window & SYM_MAX) as u8); + window >>= SYM_BITS; + used -= SYM_BITS as i32; + } + let mut buf = vec![0u8; size]; + let full = self.end.len().min(size); + let room = size - full; + let n = self.out.len().min(room); + buf[..n].copy_from_slice(&self.out[..n]); + for (k, &b) in self.end.iter().take(full).enumerate() { + buf[size - 1 - k] = b; + } + if used > 0 && room > 0 { + buf[room - 1] |= window as u8; + } + buf + } +} + +#[cfg(test)] +pub(crate) mod dec { + //! Minimal range decoder (RFC 6716 §4.1) used to verify the encoder. + use super::*; + const CODE_EXTRA: u32 = (CODE_BITS - 2) % SYM_BITS + 1; + + pub struct RangeDecoder<'a> { + buf: &'a [u8], + offs: usize, + end_offs: usize, + end_window: u32, + nend_bits: i32, + nbits_total: i32, + rng: u32, + val: u32, + rem: u32, + } + + impl<'a> RangeDecoder<'a> { + pub fn new(buf: &'a [u8]) -> Self { + let mut d = RangeDecoder { + buf, + offs: 0, + end_offs: 0, + end_window: 0, + nend_bits: 0, + nbits_total: (CODE_BITS + 1 - ((CODE_BITS - CODE_EXTRA) / SYM_BITS) * SYM_BITS) as i32, + rng: 1 << CODE_EXTRA, + val: 0, + rem: 0, + }; + d.rem = d.read_byte(); + d.val = d.rng - 1 - (d.rem >> (SYM_BITS - CODE_EXTRA)); + d.normalize(); + d + } + fn read_byte(&mut self) -> u32 { + if self.offs < self.buf.len() { + self.offs += 1; + self.buf[self.offs - 1] as u32 + } else { + 0 + } + } + fn read_byte_from_end(&mut self) -> u32 { + if self.end_offs < self.buf.len() { + self.end_offs += 1; + self.buf[self.buf.len() - self.end_offs] as u32 + } else { + 0 + } + } + fn normalize(&mut self) { + while self.rng <= CODE_BOT { + self.nbits_total += SYM_BITS as i32; + self.rng <<= SYM_BITS; + let sym = self.rem; + self.rem = self.read_byte(); + let sym = ((sym << SYM_BITS) | self.rem) >> (SYM_BITS - CODE_EXTRA); + self.val = ((self.val << SYM_BITS).wrapping_add(SYM_MAX & !sym)) & (CODE_TOP - 1); + } + } + pub fn rng(&self) -> u32 { + self.rng + } + pub fn decode(&mut self, ft: u32) -> u32 { + let ext = self.rng / ft; + let s = self.val / ext; + ft - (s + 1).min(ft) + } + pub fn update(&mut self, fl: u32, fh: u32, ft: u32) { + let ext = self.rng / ft; + let s = ext * (ft - fh); + self.val = self.val.wrapping_sub(s); + self.rng = if fl > 0 { ext * (fh - fl) } else { self.rng - s }; + self.normalize(); + } + pub fn bit_logp(&mut self, logp: u32) -> bool { + let r = self.rng; + let d = self.val; + let s = r >> logp; + let ret = d < s; + if !ret { + self.val = d - s; + } + self.rng = if ret { s } else { r - s }; + self.normalize(); + ret + } + pub fn icdf(&mut self, icdf: &[u8], ftb: u32) -> usize { + let mut s = self.rng; + let d = self.val; + let r = s >> ftb; + let mut ret = 0usize; + let mut t; + loop { + t = s; + s = r * icdf[ret] as u32; + if d >= s { + break; + } + ret += 1; + } + self.val = d - s; + self.rng = t - s; + self.normalize(); + ret + } + pub fn uint(&mut self, ft: u32) -> u32 { + let ft1 = ft - 1; + let ftb = ilog(ft1); + if ftb > 8 { + let ftb = (ftb - 8) as u32; + let top = (ft1 >> ftb) + 1; + let s = self.decode(top); + self.update(s, s + 1, top); + (s << ftb) | self.bits(ftb) + } else { + let s = self.decode(ft); + self.update(s, s + 1, ft); + s + } + } + pub fn bits(&mut self, bits: u32) -> u32 { + let mut window = self.end_window; + let mut available = self.nend_bits; + if (available as u32) < bits { + loop { + window |= self.read_byte_from_end() << available; + available += SYM_BITS as i32; + if available > (CODE_BITS - SYM_BITS) as i32 { + break; + } + } + } + let ret = window & ((1u32 << bits) - 1); + self.end_window = window >> bits; + self.nend_bits = available - bits as i32; + self.nbits_total += bits as i32; + ret + } + pub fn tell(&self) -> i32 { + self.nbits_total - ilog(self.rng) + } + } +} + +#[cfg(test)] +mod tests { + use super::dec::RangeDecoder; + use super::*; + + struct Lcg(u32); + impl Lcg { + fn next(&mut self) -> u32 { + self.0 = self.0.wrapping_mul(1664525).wrapping_add(1013904223); + self.0 >> 8 + } + } + + #[test] + fn starts_at_one_bit() { + let e = RangeEncoder::new(); + assert_eq!(e.tell(), 1); + assert_eq!(e.tell_frac(), 8); + } + + #[test] + fn roundtrip_mixed_symbols_and_tell_matches() { + let mut rng = Lcg(7); + let icdf: [u8; 4] = [200, 120, 30, 0]; + for trial in 0..300 { + let n = 1 + (rng.next() % 300) as usize; + let mut ops = Vec::new(); + for _ in 0..n { + match rng.next() % 5 { + 0 => { + let ft = 2 + rng.next() % 1000; + ops.push((0u8, rng.next() % ft, ft)); + } + 1 => { + let logp = 1 + rng.next() % 15; + ops.push((1, rng.next().is_multiple_of(5) as u32, logp)); + } + 2 => { + let bits = 1 + rng.next() % 24; + ops.push((2, rng.next() & ((1 << bits) - 1), bits)); + } + 3 => { + let ft = 2 + rng.next() % 100_000_000; + ops.push((3, rng.next() % ft, ft)); + } + _ => ops.push((4, rng.next() % 4, 0)), + } + } + let mut e = RangeEncoder::new(); + let mut tells = Vec::new(); + for &(k, v, p) in &ops { + match k { + 0 => e.encode(v, v + 1, p), + 1 => e.bit_logp(v != 0, p), + 2 => e.bits(v, p), + 3 => e.uint(v, p), + _ => e.icdf(v as usize, &icdf, 8), + } + tells.push(e.tell()); + } + // Exactly the number of bytes `tell` says were used must suffice. + let size = (e.tell() as usize).div_ceil(8); + let final_rng = e.rng(); + let buf = e.done(size); + assert_eq!(buf.len(), size); + let mut d = RangeDecoder::new(&buf); + for (i, &(k, v, p)) in ops.iter().enumerate() { + let got = match k { + 0 => { + let s = d.decode(p); + d.update(s, s + 1, p); + s + } + 1 => d.bit_logp(p) as u32, + 2 => d.bits(p), + 3 => d.uint(p), + _ => d.icdf(&icdf, 8) as u32, + }; + assert_eq!(got, v, "trial {trial} op {i} kind {k}"); + assert_eq!(d.tell(), tells[i], "tell mismatch trial {trial} op {i}"); + } + assert_eq!(d.rng(), final_rng); + } + } +} diff --git a/crates/opusenc/src/rate.rs b/crates/opusenc/src/rate.rs new file mode 100644 index 00000000..d8665bf1 --- /dev/null +++ b/crates/opusenc/src/rate.rs @@ -0,0 +1,561 @@ +//! Pulse cache and bit allocation (RFC 6716 §4.3.3, §4.3.4.1). +//! +//! The allocation must be bit-identical to the decoder's, so this mirrors FilmCraft's +//! spec-derived `filmcraft-opus` decoder (first-party, MIT OR Apache-2.0) step by step; the only +//! difference is that the encoder *writes* the band-skip, intensity and dual-stereo symbols. + +use crate::range::{BITRES, RangeEncoder, ilog}; +use crate::tables::*; + +const MAX_PSEUDO: i32 = 40; +const LOG_MAX_PSEUDO: i32 = 6; +pub const MAX_FINE_BITS: i32 = 8; +const FINE_OFFSET: i32 = 21; +pub const QTHETA_OFFSET: i32 = 4; +pub const QTHETA_OFFSET_TWOPHASE: i32 = 16; +const ALLOC_STEPS: i32 = 6; + +/// Number of pulses represented by pseudo-pulse index `i`. +pub fn get_pulses(i: i32) -> i32 { + if i < 8 { i } else { (8 + (i & 7)) << ((i >> 3) - 1) } +} + +/// Conservative (rounded up) log2 of `val` with `frac` fractional bits. +pub fn log2_frac(mut val: u32, mut frac: u32) -> i32 { + let mut l = ilog(val); + if val & val.wrapping_sub(1) != 0 { + if l > 16 { + val = ((val - 1) >> (l - 16)) + 1; + } else { + val <<= 16 - l; + } + let mut l = (l - 1) << frac; + loop { + let b = (val >> 16) as i32; + l += b << frac; + val = (val + b as u32) >> b; + val = (val * val + 0x7FFF) >> 15; + if frac == 0 { + break; + } + frac -= 1; + } + l + (val > 0x8000) as i32 + } else { + l -= 1; + l << frac + } +} + +/// PVQ codebook size V(N, K) (saturating far above 2^32). +pub fn pvq_v(n: usize, k: usize) -> u128 { + if k == 0 { + return 1; + } + if n == 0 { + return 0; + } + let mut row = vec![0u128; k + 1]; + row[0] = 1; + for _ in 0..n { + let mut prev_diag = row[0]; + for kk in 1..=k { + let up = row[kk]; + let v = up.saturating_add(row[kk - 1]).saturating_add(prev_diag); + prev_diag = up; + row[kk] = v.min(u128::MAX >> 8); + } + row[0] = 1; + } + row[k] +} + +fn fits_in32(n: usize, k: i32) -> bool { + pvq_v(n, k as usize) <= u32::MAX as u128 +} + +/// Precomputed per-mode data. +pub struct Mode { + /// log2 of band width in 1/8 bits. + pub log_n: [i32; NB_EBANDS], + cache_index: Vec, + cache_bits: Vec, + caps: Vec, +} + +impl Mode { + pub fn new() -> Mode { + let mut log_n = [0i32; NB_EBANDS]; + for (i, l) in log_n.iter_mut().enumerate() { + *l = log2_frac((EBANDS[i + 1] - EBANDS[i]) as u32, BITRES); + } + let (cache_index, cache_bits) = compute_pulse_cache(MAX_LM as i32); + let mut m = Mode { log_n, cache_index, cache_bits, caps: Vec::new() }; + m.caps = compute_caps(&m); + m + } + + #[inline] + pub fn cache(&self, lm: i32, band: usize) -> &[u8] { + let idx = self.cache_index[((lm + 1) as usize) * NB_EBANDS + band]; + &self.cache_bits[idx.max(0) as usize..] + } + + pub fn bits2pulses(&self, band: usize, lm: i32, bits: i32) -> i32 { + let cache = self.cache(lm, band); + let mut lo = 0i32; + let mut hi = cache[0] as i32; + let bits = bits - 1; + for _ in 0..LOG_MAX_PSEUDO { + let mid = (lo + hi + 1) >> 1; + if cache[mid as usize] as i32 >= bits { + hi = mid; + } else { + lo = mid; + } + } + let lo_bits = if lo == 0 { -1 } else { cache[lo as usize] as i32 }; + if bits - lo_bits <= cache[hi as usize] as i32 - bits { lo } else { hi } + } + + pub fn pulses2bits(&self, band: usize, lm: i32, pulses: i32) -> i32 { + if pulses == 0 { 0 } else { self.cache(lm, band)[pulses as usize] as i32 + 1 } + } + + /// Per-band allocation caps for this frame (`init_caps`). + pub fn init_caps(&self, lm: usize, c: usize) -> [i32; NB_EBANDS] { + let mut cap = [0i32; NB_EBANDS]; + for (i, cp) in cap.iter_mut().enumerate() { + let n = (EBANDS[i + 1] - EBANDS[i]) << lm; + *cp = (self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n >> 2; + } + cap + } +} + +fn compute_pulse_cache(lm_max: i32) -> (Vec, Vec) { + let nb = NB_EBANDS; + let rows = (lm_max + 2) as usize; + let mut index = vec![-1i16; nb * rows]; + let mut entries: Vec<(usize, i32, usize)> = Vec::new(); + let mut curr = 0usize; + for i in 0..rows { + for j in 0..nb { + let n = (((EBANDS[j + 1] - EBANDS[j]) << i) >> 1) as usize; + index[i * nb + j] = -1; + 'search: for k in 0..=i { + for nn in 0..nb { + if k == i && nn >= j { + break; + } + if n == (((EBANDS[nn + 1] - EBANDS[nn]) << k) >> 1) as usize { + index[i * nb + j] = index[k * nb + nn]; + break 'search; + } + } + } + if index[i * nb + j] == -1 && n != 0 { + let mut k = 0; + while k < MAX_PSEUDO && fits_in32(n, get_pulses(k + 1)) { + k += 1; + } + index[i * nb + j] = curr as i16; + entries.push((n, k, curr)); + curr += (k + 1) as usize; + } + } + } + let mut bits = vec![0u8; curr]; + for &(n, k, off) in &entries { + let maxk = get_pulses(k) as usize; + let tmp = required_bits(n, maxk, BITRES); + for j in 1..=k { + bits[off + j as usize] = (tmp[get_pulses(j) as usize] - 1) as u8; + } + bits[off] = k as u8; + } + (index, bits) +} + +fn required_bits(n: usize, maxk: usize, frac: u32) -> Vec { + let mut out = vec![0i32; maxk + 1]; + if n == 1 { + for o in out.iter_mut().skip(1) { + *o = 1 << frac; + } + } else { + for (k, o) in out.iter_mut().enumerate().skip(1) { + *o = log2_frac(pvq_v(n, k) as u32, frac); + } + } + out +} + +fn compute_caps(m: &Mode) -> Vec { + let nb = NB_EBANDS; + let mut caps = vec![0u8; (MAX_LM + 1) * 2 * nb]; + for i in 0..=MAX_LM as i32 { + for c in 1..=2i32 { + for j in 0..nb { + let mut n0 = EBANDS[j + 1] - EBANDS[j]; + let max_bits; + if n0 << i == 1 { + max_bits = c * (1 + MAX_FINE_BITS) << BITRES; + } else { + let mut lm0 = 0; + if n0 > 2 { + n0 >>= 1; + lm0 -= 1; + } else if n0 <= 1 { + lm0 = i.min(1); + n0 <<= lm0; + } + let pcache = m.cache(lm0, j); + let mut mb = pcache[pcache[0] as usize] as i32 + 1; + let mut n = n0; + for k in 0..(i - lm0) { + mb <<= 1; + let offset = ((m.log_n[j] + ((lm0 + k) << BITRES)) >> 1) - QTHETA_OFFSET; + let num = 459 * ((2 * n - 1) * offset + mb); + let den = ((2 * n - 1) << 9) - 459; + let qb = ((num + (den >> 1)) / den).min(57); + mb += qb; + n <<= 1; + } + if c == 2 { + mb <<= 1; + let offset = ((m.log_n[j] + (i << BITRES)) >> 1) - if n == 2 { QTHETA_OFFSET_TWOPHASE } else { QTHETA_OFFSET }; + let ndof = 2 * n - 1 - (n == 2) as i32; + let p = if n == 2 { 512 } else { 487 }; + let num = p * (mb + ndof * offset); + let den = (ndof << 9) - p; + let qb = ((num + (den >> 1)) / den).min(if n == 2 { 64 } else { 61 }); + mb += qb; + } + let ndof = c * n + if c == 2 && n > 2 { 1 } else { 0 }; + let mut offset = ((m.log_n[j] + (i << BITRES)) >> 1) - FINE_OFFSET; + if n == 2 { + offset += (1 << BITRES) >> 2; + } + let num = mb + ndof * offset; + let den = (ndof - 1) << BITRES; + let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS); + mb += c * qb << BITRES; + max_bits = mb; + } + let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i); + let v = (4 * max_bits / w) - 64; + caps[((i * 2 + c - 1) as usize) * nb + j] = v.clamp(0, 255) as u8; + } + } + } + caps +} + +/// Result of the allocation process. +pub struct Allocation { + pub coded_bands: usize, + pub intensity: usize, + pub dual_stereo: bool, + pub balance: i32, + pub pulses: [i32; NB_EBANDS], + pub fine_quant: [i32; NB_EBANDS], + pub fine_priority: [i32; NB_EBANDS], +} + +/// `clt_compute_allocation`, encoder side. Stereo frames are always coded as dual (L/R) stereo +/// with no intensity bands, and no band is skipped voluntarily. +#[allow(clippy::too_many_arguments)] +pub fn compute_allocation( + m: &Mode, + start: usize, + end: usize, + offsets: &[i32; NB_EBANDS], + cap: &[i32; NB_EBANDS], + alloc_trim: i32, + total: i32, + c: usize, + lm: usize, + enc: &mut RangeEncoder, +) -> Allocation { + let ci = c as i32; + let lmi = lm as i32; + let mut total = total.max(0); + let mut skip_start = start; + let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; + total -= skip_rsv; + let mut intensity_rsv = 0; + let mut dual_stereo_rsv = 0; + if c == 2 { + intensity_rsv = LOG2_FRAC_TABLE[end - start]; + if intensity_rsv > total { + intensity_rsv = 0; + } else { + total -= intensity_rsv; + dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 }; + total -= dual_stereo_rsv; + } + } + let mut bits1 = [0i32; NB_EBANDS]; + let mut bits2 = [0i32; NB_EBANDS]; + let mut thresh = [0i32; NB_EBANDS]; + let mut trim_offset = [0i32; NB_EBANDS]; + for j in start..end { + let w = EBANDS[j + 1] - EBANDS[j]; + thresh[j] = (ci << BITRES).max((3 * w << lmi << BITRES) >> 4); + trim_offset[j] = ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32)) >> 6; + if w << lmi == 1 { + trim_offset[j] -= ci << BITRES; + } + } + let nb_alloc = ALLOC.len() as i32; + let mut lo = 1; + let mut hi = nb_alloc - 1; + loop { + let mut done = false; + let mut psum = 0; + let mid = (lo + hi) >> 1; + for j in (start..end).rev() { + let n = EBANDS[j + 1] - EBANDS[j]; + let mut bitsj = ci * n * (ALLOC[mid as usize][j] as i32) << lmi >> 2; + if bitsj > 0 { + bitsj = (bitsj + trim_offset[j]).max(0); + } + bitsj += offsets[j]; + if bitsj >= thresh[j] || done { + done = true; + psum += bitsj.min(cap[j]); + } else if bitsj >= ci << BITRES { + psum += ci << BITRES; + } + } + if psum > total { + hi = mid - 1; + } else { + lo = mid + 1; + } + if lo > hi { + break; + } + } + hi = lo; + lo -= 1; + for j in start..end { + let n = EBANDS[j + 1] - EBANDS[j]; + let mut bits1j = ci * n * (ALLOC[lo as usize][j] as i32) << lmi >> 2; + let mut bits2j = if hi >= nb_alloc { cap[j] } else { ci * n * (ALLOC[hi as usize][j] as i32) << lmi >> 2 }; + if bits1j > 0 { + bits1j = (bits1j + trim_offset[j]).max(0); + } + if bits2j > 0 { + bits2j = (bits2j + trim_offset[j]).max(0); + } + if lo > 0 { + bits1j += offsets[j]; + } + bits2j += offsets[j]; + if offsets[j] > 0 { + skip_start = j; + } + bits2j = (bits2j - bits1j).max(0); + bits1[j] = bits1j; + bits2[j] = bits2j; + } + interp_bits2pulses(m, start, end, skip_start, &bits1, &bits2, &thresh, cap, total, skip_rsv, intensity_rsv, dual_stereo_rsv, c, lm, enc) +} + +#[allow(clippy::too_many_arguments)] +fn interp_bits2pulses( + m: &Mode, + start: usize, + end: usize, + skip_start: usize, + bits1: &[i32; NB_EBANDS], + bits2: &[i32; NB_EBANDS], + thresh: &[i32; NB_EBANDS], + cap: &[i32; NB_EBANDS], + mut total: i32, + skip_rsv: i32, + mut intensity_rsv: i32, + mut dual_stereo_rsv: i32, + c: usize, + lm: usize, + enc: &mut RangeEncoder, +) -> Allocation { + let ci = c as i32; + let alloc_floor = ci << BITRES; + let stereo = (c > 1) as i32; + let log_m = (lm as i32) << BITRES; + let mut lo = 0; + let mut hi = 1 << ALLOC_STEPS; + for _ in 0..ALLOC_STEPS { + let mid = (lo + hi) >> 1; + let mut psum = 0; + let mut done = false; + for j in (start..end).rev() { + let tmp = bits1[j] + (mid * bits2[j] >> ALLOC_STEPS); + if tmp >= thresh[j] || done { + done = true; + psum += tmp.min(cap[j]); + } else if tmp >= alloc_floor { + psum += alloc_floor; + } + } + if psum > total { + hi = mid; + } else { + lo = mid; + } + } + let mut bits = [0i32; NB_EBANDS]; + let mut psum = 0; + let mut done = false; + for j in (start..end).rev() { + let mut tmp = bits1[j] + (lo * bits2[j] >> ALLOC_STEPS); + if tmp < thresh[j] && !done { + tmp = if tmp >= alloc_floor { alloc_floor } else { 0 }; + } else { + done = true; + } + tmp = tmp.min(cap[j]); + bits[j] = tmp; + psum += tmp; + } + let s = start; + let mut coded_bands = end; + loop { + let j = coded_bands - 1; + if j <= skip_start { + total += skip_rsv; + break; + } + let mut left = total - psum; + let span = EBANDS[coded_bands] - EBANDS[s]; + let percoeff = left / span; + left -= span * percoeff; + let rem = (left - (EBANDS[j] - EBANDS[s])).max(0); + let band_width = EBANDS[coded_bands] - EBANDS[j]; + let band_bits = bits[j] + percoeff * band_width + rem; + if band_bits >= thresh[j].max(alloc_floor + (1 << BITRES)) { + // Keep this band (and every band below it). + enc.bit_logp(true, 1); + break; + } + psum -= bits[j] + intensity_rsv; + if intensity_rsv > 0 { + intensity_rsv = LOG2_FRAC_TABLE[j - s]; + } + psum += intensity_rsv; + if band_bits >= alloc_floor { + psum += alloc_floor; + bits[j] = alloc_floor; + } else { + bits[j] = 0; + } + coded_bands -= 1; + } + let intensity = if intensity_rsv > 0 { + // No intensity stereo: every coded band is coded in full. + enc.uint((coded_bands - s) as u32, (coded_bands + 1 - s) as u32); + coded_bands + } else { + 0 + }; + if intensity <= s { + total += dual_stereo_rsv; + dual_stereo_rsv = 0; + } + let dual_stereo = if dual_stereo_rsv > 0 { + enc.bit_logp(true, 1); + true + } else { + false + }; + let mut left = total - psum; + let span = EBANDS[coded_bands] - EBANDS[s]; + let percoeff = left / span; + left -= span * percoeff; + for j in s..coded_bands { + bits[j] += percoeff * (EBANDS[j + 1] - EBANDS[j]); + } + for j in s..coded_bands { + let tmp = left.min(EBANDS[j + 1] - EBANDS[j]); + bits[j] += tmp; + left -= tmp; + } + let mut ebits = [0i32; NB_EBANDS]; + let mut fine_priority = [0i32; NB_EBANDS]; + let mut balance = 0; + for j in s..coded_bands { + let n0 = EBANDS[j + 1] - EBANDS[j]; + let n = n0 << lm; + let bit = bits[j] + balance; + let mut excess; + if n > 1 { + excess = (bit - cap[j]).max(0); + bits[j] = bit - excess; + let den = ci * n + if c == 2 && n > 2 && !dual_stereo && j < intensity { 1 } else { 0 }; + let nclogn = den * (m.log_n[j] + log_m); + let mut offset = (nclogn >> 1) - den * FINE_OFFSET; + if n == 2 { + offset += (den << BITRES) >> 2; + } + if bits[j] + offset < den * 2 << BITRES { + offset += nclogn >> 2; + } else if bits[j] + offset < den * 3 << BITRES { + offset += nclogn >> 3; + } + let mut eb = (bits[j] + offset + (den << (BITRES - 1))).max(0); + eb = (eb / den) >> BITRES; + if ci * eb > (bits[j] >> BITRES) { + eb = bits[j] >> stereo >> BITRES; + } + eb = eb.min(MAX_FINE_BITS); + ebits[j] = eb; + fine_priority[j] = (eb * (den << BITRES) >= bits[j] + offset) as i32; + bits[j] -= ci * eb << BITRES; + } else { + excess = (bit - (ci << BITRES)).max(0); + bits[j] = bit - excess; + ebits[j] = 0; + fine_priority[j] = 1; + } + if excess > 0 { + let extra_fine = (excess >> (stereo + BITRES as i32)).min(MAX_FINE_BITS - ebits[j]); + ebits[j] += extra_fine; + let extra_bits = extra_fine * ci << BITRES; + fine_priority[j] = (extra_bits >= excess - balance) as i32; + excess -= extra_bits; + } + balance = excess; + } + for j in coded_bands..end { + ebits[j] = bits[j] >> stereo >> BITRES; + bits[j] = 0; + fine_priority[j] = (ebits[j] < 1) as i32; + } + Allocation { coded_bands, intensity, dual_stereo, balance, pulses: bits, fine_quant: ebits, fine_priority } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn caps_match_decoder_reference() { + let m = Mode::new(); + let first_rows: [u8; 42] = [ + 224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37, 224, 224, 224, 224, 224, 224, 224, 224, 240, + 240, 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40, + ]; + assert_eq!(&m.caps[..42], &first_rows[..]); + } + + #[test] + fn pvq_counts() { + assert_eq!(pvq_v(5, 1), 10); + assert_eq!(pvq_v(1, 7), 2); + assert_eq!(pvq_v(2, 9), 36); + assert_eq!(pvq_v(3, 2), 18); + } +} diff --git a/crates/opusenc/src/tables.rs b/crates/opusenc/src/tables.rs new file mode 100644 index 00000000..00d623af --- /dev/null +++ b/crates/opusenc/src/tables.rs @@ -0,0 +1,122 @@ +//! Constant tables of the 48 kHz CELT mode (RFC 6716 §4.3, Tables 55–58 and §4.3.2). +//! +//! Transcribed from RFC 6716; copied from FilmCraft's spec-derived `filmcraft-opus` decoder +//! (first-party, MIT OR Apache-2.0) so both sides share the exact same values. + +/// Number of energy bands. +pub const NB_EBANDS: usize = 21; +/// Band edges in units of 2.5 ms MDCT bins (Table 55). +pub const EBANDS: [i32; NB_EBANDS + 1] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100]; +/// Overlap of the low-overlap window (samples at 48 kHz). +pub const OVERLAP: usize = 120; +/// Largest LM (20 ms frames). +pub const MAX_LM: usize = 3; +/// Pre-emphasis coefficient. +pub const PREEMPH: f32 = 0.850_006_1; + +/// Static allocation table (Table 57), indexed `[quality][band]`, in 1/32 bit per MDCT bin. +pub const ALLOC: [[u8; NB_EBANDS]; 11] = transpose([ + [0, 90, 110, 118, 126, 134, 144, 152, 162, 172, 200], + [0, 80, 100, 110, 119, 127, 137, 145, 155, 165, 200], + [0, 75, 90, 103, 112, 120, 130, 138, 148, 158, 200], + [0, 69, 84, 93, 104, 114, 124, 132, 142, 152, 200], + [0, 63, 78, 86, 95, 103, 113, 123, 133, 143, 200], + [0, 56, 71, 80, 89, 97, 107, 117, 127, 137, 200], + [0, 49, 65, 75, 83, 91, 101, 111, 121, 131, 200], + [0, 40, 58, 70, 78, 85, 95, 105, 115, 125, 200], + [0, 34, 51, 65, 72, 78, 88, 98, 108, 118, 198], + [0, 29, 45, 59, 66, 72, 82, 92, 102, 112, 193], + [0, 20, 39, 53, 60, 66, 76, 86, 96, 106, 188], + [0, 18, 32, 47, 54, 60, 70, 80, 90, 100, 183], + [0, 10, 26, 40, 47, 54, 64, 74, 84, 94, 178], + [0, 0, 20, 31, 39, 47, 57, 67, 77, 87, 173], + [0, 0, 12, 23, 32, 41, 51, 61, 71, 81, 168], + [0, 0, 0, 15, 25, 35, 45, 55, 65, 75, 163], + [0, 0, 0, 4, 17, 29, 39, 49, 59, 69, 158], + [0, 0, 0, 0, 12, 23, 33, 43, 53, 63, 153], + [0, 0, 0, 0, 1, 16, 26, 36, 46, 56, 148], + [0, 0, 0, 0, 0, 10, 15, 20, 30, 45, 129], + [0, 0, 0, 0, 0, 1, 1, 1, 1, 20, 104], +]); + +const fn transpose(t: [[u8; 11]; NB_EBANDS]) -> [[u8; NB_EBANDS]; 11] { + let mut out = [[0u8; NB_EBANDS]; 11]; + let mut b = 0; + while b < NB_EBANDS { + let mut q = 0; + while q < 11 { + out[q][b] = t[b][q]; + q += 1; + } + b += 1; + } + out +} + +/// Mean band energy (log2 domain) removed before coarse quantisation. +pub const E_MEANS: [f32; 25] = [ + 6.4375, 6.25, 5.75, 5.3125, 5.0625, 4.8125, 4.5, 4.375, 4.875, 4.6875, 4.5625, 4.4375, 4.875, 4.625, 4.3125, 4.5, 4.375, 4.625, 4.75, 4.4375, 3.75, 3.75, + 3.75, 3.75, 3.75, +]; + +/// Inter-frame prediction coefficient (alpha) per LM, Q15. +pub const PRED_COEF: [f32; 4] = [29440.0 / 32768.0, 26112.0 / 32768.0, 21248.0 / 32768.0, 16384.0 / 32768.0]; +/// Inter-band prediction coefficient (beta) per LM for inter frames, Q15. +pub const BETA_COEF: [f32; 4] = [30147.0 / 32768.0, 22282.0 / 32768.0, 12124.0 / 32768.0, 6554.0 / 32768.0]; +/// Beta for intra frames. +pub const BETA_INTRA: f32 = 4915.0 / 32768.0; + +/// Laplace model parameters for coarse energy: `[LM][intra][2*band + {0: prob, 1: decay}]`. +pub const E_PROB_MODEL: [[[u8; 42]; 2]; 4] = [ + [ + [ + 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, 114, 40, 132, 26, 132, + 26, 145, 17, 161, 12, 176, 10, 177, 11, + ], + [ + 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, 91, 67, 100, 59, 108, 50, + 120, 40, 122, 37, 97, 43, 78, 50, + ], + ], + [ + [ + 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, 146, 19, 162, 12, 165, 10, + 178, 7, 189, 6, 190, 8, 177, 9, + ], + [ + 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, 104, 60, 117, 52, 123, 44, + 138, 35, 133, 31, 97, 38, 77, 45, + ], + ], + [ + [ + 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, 158, 18, 170, 13, 177, + 10, 187, 8, 192, 6, 175, 9, 159, 10, + ], + [ + 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, 112, 56, 129, 51, 132, 40, + 150, 33, 140, 29, 98, 35, 77, 42, + ], + ], + [ + [ + 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, 154, 26, 166, 21, 173, + 16, 184, 13, 184, 10, 150, 13, 139, 15, + ], + [ + 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, 117, 55, 135, 49, 137, 39, + 157, 32, 145, 29, 97, 33, 77, 40, + ], + ], +]; + +/// Inverse CDF for coarse energy when only a few bits remain. +pub const SMALL_ENERGY_ICDF: [u8; 3] = [2, 1, 0]; +/// Conservative log2 in 1/8 bits of the number of coded bands (intensity reservation). +pub const LOG2_FRAC_TABLE: [i32; 24] = [0, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37]; +/// TF resolution changes: `[LM][4*transient + 2*tf_select + tf_flag]`. +pub const TF_SELECT_TABLE: [[i8; 8]; 4] = [[0, -1, 0, -1, 0, -1, 0, -1], [0, -1, 0, -2, 1, 0, 1, -1], [0, -2, 0, -3, 2, 0, 1, -1], [0, -2, 0, -3, 3, 0, 1, -1]]; +/// Spreading decision `{7, 2, 21, 2}/32`. +pub const SPREAD_ICDF: [u8; 4] = [25, 23, 2, 0]; +/// Allocation trim (Table 58). +pub const TRIM_ICDF: [u8; 11] = [126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0]; diff --git a/docs/architecture.md b/docs/architecture.md index 033ed6cb..c3d869c0 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -16,6 +16,7 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for | L0 | `time` | `Tick` (254 016 000 000 per second), rational frame rates incl. NTSC, SMPTE and drop-frame timecode | | L0 | `geom` | Vectors, matrices, quaternions, the layer transform (anchor, position, scale, orientation, rotation) | | L0 | `color` | sRGB and linear, HSL/HSV, luminance, the 38 blend modes, label colors | +| L0 | `opusenc` | Opus (CELT) encoder: RFC 6716 CELT-only fullband 48 kHz 20 ms packets, mono/stereo, and the RFC 7845 `OpusHead`, for WebM export audio | | L1 | `raster` | Premultiplied float images, sampling, affine and projective warps, blurs, compositing (parallel with rayon) | | L1 | `keyframe` | Animated values, keyframes with temporal ease and spatial Bezier, roving, hold, velocity | | L1 | `path` | Bezier paths, path operators (trim, offset, round corners, zig zag, twist, merge…), stroking, coverage masks | diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 76ff6fa3..0cef3307 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -24,6 +24,7 @@ const LAYERS: &[(&str, u8)] = &[ ("geom", 0), ("color", 0), ("testkit", 0), + ("opusenc", 0), ("raster", 1), ("keyframe", 1), ("path", 1), From ad697a342d69da5b401944a42d109cabd544c0cb Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 21:48:11 -0400 Subject: [PATCH 12/23] =?UTF-8?q?M9.11:=20parity=20doc=20=E2=80=94=20296?= =?UTF-8?q?=20of=20298=20After=20Effects=20effects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- docs/parity.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/parity.md b/docs/parity.md index af0c9420..bbe28965 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -15,7 +15,7 @@ rendered, does not count as done. | P1 (professional daily use) | ≈ 38% | | P2 (long tail) | ≈ 17% | | Features done / partial / missing | 18 / 60 / 14 of 92 | -| **Effects** | **257 of 298** After Effects 2026 effects by name (86%); ≈ 70% allowing for simplified implementations | +| **Effects** | **296 of 298** After Effects 2026 effects by name (99%, after M9.11; was 257); the two left are Warp Stabilizer and the 3D Camera Tracker | | Remaining work | ≈ **167 agent-hours** (139 h of features + ≈ 20% for 1:1 polish against After Effects) | | Wall-clock estimate | ≈ **42–50 hours** with five agents working in parallel and one integrating; ≈ 31–35 h for P0 + P1 only | @@ -76,8 +76,8 @@ library groundwork for mask tracking and Warp Stabilizer. What is left is concentrated in large systems: Roto Brush and Refine Edge, the 3D camera tracker, Advanced 3D (models, PBR, image-based lighting), the Warp Stabilizer and mask-tracking UI on top of the new tracking library, a JavaScript scripting object model, SVG/PSD import, WebM, a disk cache, -the missing effect categories (3D Channel, Immersive Video, OCIO), native macOS menus, and -wasm threads. +native macOS menus, and wasm threads. (The missing effect categories, 3D Channel, Immersive Video +and OCIO, landed in M9.11.) ## By area @@ -94,7 +94,7 @@ wasm threads. | Text | ≈ 85% | 1.5 | vertical type is basic (upright characters, no tate-chu-yoko), no OpenType feature panel, no text on 3D bevels; per-character styles, paragraph settings, on-canvas editing and the `sourceText` style API landed (M9.9–M9.10) | | Web | 60% | 2.5 | threads, browser storage, audio | | 3D | 58% | 20.5 | Advanced 3D (models, PBR, image-based light), 3D camera tracker, multi-view layouts | -| Effects | 58% | 13.6 | Effect Controls widgets (angle dial, point crosshair, eyedropper, curves), GPU versions of more effects, 41 missing effects | +| Effects | ≈ 75% | 6.0 | GPU versions of more effects, Warp Stabilizer and the 3D Camera Tracker (all other After Effects effects exist since M9.11) | | Interface | 55% | 10.4 | drag-to-dock and floating panels, preferences, native macOS menus (viewer rulers/snapping/channels/snapshots landed in M0.13) | | Project | 53% | 8.1 | 8/16/32-bit pipeline and colour management, auto-save, proxies, folder moves | | Masks & roto | 44% | 13.0 | mask tracking, Roto Brush, variable-width mask feather points | @@ -104,12 +104,24 @@ wasm threads. ## Effects still missing -Immersive Video (all 12), 3D Channel (all 8, including Cryptomatte), Color Stabilizer and the five -OCIO transforms, CC Flo Motion, Liquify, Rolling Shutter Repair, Warp Stabilizer, Compressor, -Distortion and Gate (audio), Camera-Shake Deblur, CC Radial Blur, CC Hair, Particle Playground, -Keylight (our Screen Key stands in), Color Profile Converter and the 3D Camera Tracker. Boris FX +Warp Stabilizer and the 3D Camera Tracker (built on the tracking library by other work). Boris FX Mocha and Cineware are third-party and not counted. +### Added in M9.11 (39 effects) + +| Category | Effects | Notes | +|---|---|---| +| 3D Channel | 3D Channel Extract, Cryptomatte, Depth Matte, Depth of Field, EXtractoR, Fog 3D, ID Matte, IDentifier | Read auxiliary channels: multi-layer OpenEXR (any `layer.channel`, Cryptomatte manifests from the header) or the compositor's own depth / layer-ID / normal / UV / Cryptomatte pass for precomps of 3D comps. | +| Immersive Video | VR Blur, VR Chromatic Aberrations, VR Color Gradients, VR Converter, VR De-Noise, VR Digital Glitch, VR Fractal Noise, VR Glow, VR Plane to Sphere, VR Rotate Sphere, VR Sharpen, VR Sphere To Plane | Seam-aware equirectangular processing (wrap-around, pole continuation, latitude-scaled filters), mono / over-under / side-by-side layouts; VR Converter between equirectangular, cube maps (4:3, 3:2, 6:1), sphere map and 2D. | +| Color Correction | OCIO CDL / Color Space / Display / File / Look Transform, Color Stabilizer | Built-in minimal OCIO-style config (ACES2065-1, ACEScg, ACEScct, sRGB, Rec.709, Rec.2020, Display P3, linear variants, XYZ) from published matrices and transfer functions; `.cube` / `.3dl` / `.csp` LUTs and ASC `.cc` / `.ccc` / `.cdl`. Custom `.ocio` config files are not read yet. | +| Distort | CC Flo Motion, Liquify, Rolling Shutter Repair | Liquify strokes are effect data replayed into a displacement mesh (`liquify.stroke` / `liquify.clear` commands; no interactive viewer brush yet). Rolling Shutter Repair uses the KLT tracker. | +| Blur & Sharpen | Camera-Shake Deblur, CC Radial Blur | Deblur substitutes aligned patches from sharper neighbouring frames. | +| Audio | Compressor, Distortion, Gate | Applied in the mixdown like the other audio effects. | +| Simulation | CC Hair, Particle Playground | Particle Playground: cannon, grid, layer exploder, layer map, gravity, repel, wall, persistent property mapper (no Particle Exploder, text particles or ephemeral mapper yet). | +| Keying | Key Light | The full Keylight 1.2 control set under a generic name ("Keylight" is a vendor trademark); `lookup("Keylight (1.2)")` finds it. | +| Utility | Color Profile Converter | Our colour spaces and ACES; rendering intents (perceptual gamut compression, relative / absolute colorimetric, saturation). | +| Obsolete | mocha shape | Mocha's export format is not public: reads a documented JSON shape format instead. | + ## Highest-value gaps, in order 1. ~~On-canvas text editing and per-character styles~~ (landed: M9.9–M9.10). From 94e1c86d89a866771f80ad1610afda9aa12e85d8 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:10:48 -0400 Subject: [PATCH 13/23] IO-2/SHP-5/PRV-2: PSD import, SVG import + vector/text conversions, persistent disk cache - effectcraft-psd (L1): PSD/PSB reader from Adobe's published file format spec (layers, groups, masks, blend modes, text engine data, lfx2 effects, fill/adjustment layers, vector masks; 1/8/16/32-bit, RGB/CMYK/Gray/Lab/Indexed, raw/RLE/ZIP) and a minimal writer for fixtures. - File > Import: importAs footage|composition|compositionLayerSizes, layer (Choose Layer); groups -> precomps (pass-through collapses), text -> editable text, styles -> Layer Styles, adjustments -> adjustment layers with effects; UI asks the Import Kind for Photoshop files. - effectcraft-svg (L2): SVG parser (shapes, paths, transforms, use, CSS, gradients) and rasteriser; .svg imports as footage, continuously rasterised when the switch is on. - layer.create: shapesFromVector, shapesFromText, masksFromText, editableText (stub removed). - Disk cache (render::disk_cache): LZ4 content-keyed frames and layer buffers, LRU size limit from prefs, atomic writes, restart persistence; edit.purge clears it, cache.diskStats query; viewer frames read/write it and the timeline shows disk-only frames in blue. Co-Authored-By: Claude Opus 5.5 --- ATTRIBUTION.md | 2 + Cargo.lock | 44 + Cargo.toml | 2 + crates/engine/Cargo.toml | 3 + crates/engine/src/commands/create.rs | 139 +++ crates/engine/src/commands/edit.rs | 32 +- crates/engine/src/commands/file.rs | 98 +- crates/engine/src/commands/file_more.rs | 1 + crates/engine/src/commands/mod.rs | 2 + crates/engine/src/commands/stubs.rs | 1 - crates/engine/src/lib.rs | 12 + crates/engine/src/psd_import.rs | 634 ++++++++++++ crates/engine/src/session_settings.rs | 37 + crates/engine/src/tests_disk_cache.rs | 121 +++ crates/engine/src/tests_stubs.rs | 1 + crates/engine/src/tests_track.rs | 2 + crates/engine/src/tests_vector_import.rs | 237 +++++ crates/engine/src/vector.rs | 250 +++++ crates/gpu/src/tests.rs | 1 + crates/host/Cargo.toml | 4 + crates/host/tests/vector_import.rs | 163 +++ crates/lottie/src/import.rs | 1 + crates/lottie/src/tests.rs | 1 + crates/media/Cargo.toml | 2 + crates/media/src/convert.rs | 17 + crates/media/src/layered.rs | 62 ++ crates/media/src/lib.rs | 6 +- crates/media/src/pool.rs | 16 + crates/media/src/probe.rs | 7 +- crates/project/src/lib.rs | 15 + crates/psd/Cargo.toml | 13 + crates/psd/README.md | 17 + crates/psd/src/descriptor.rs | 430 ++++++++ crates/psd/src/engine_data.rs | 288 ++++++ crates/psd/src/lib.rs | 965 ++++++++++++++++++ crates/psd/src/read.rs | 86 ++ crates/psd/src/tests.rs | 189 ++++ crates/psd/src/write.rs | 491 +++++++++ crates/render/Cargo.toml | 1 + crates/render/src/audio.rs | 1 + crates/render/src/cache.rs | 62 +- crates/render/src/disk_cache.rs | 829 +++++++++++++++ crates/render/src/lib.rs | 29 +- crates/render/src/tests_audio_fx.rs | 1 + crates/render/src/tests_color.rs | 1 + crates/render/src/tests_frame_blend.rs | 1 + crates/svg/Cargo.toml | 18 + crates/svg/README.md | 26 + crates/svg/src/color.rs | 254 +++++ crates/svg/src/lib.rs | 949 +++++++++++++++++ crates/svg/src/pathdata.rs | 293 ++++++ crates/svg/src/raster.rs | 220 ++++ crates/svg/src/tests.rs | 108 ++ crates/svg/tests/fixtures/basic-shapes.svg | 15 + .../fixtures/basic-shapes.svg.attribution | 7 + crates/svg/tests/fixtures/paths-gradients.svg | 26 + .../fixtures/paths-gradients.svg.attribution | 7 + crates/ui-egui/src/frames.rs | 108 +- crates/ui-egui/src/lib.rs | 18 +- crates/ui-egui/src/menus.rs | 6 +- crates/ui-egui/src/panels/forms.rs | 20 + crates/ui-egui/src/panels/timeline.rs | 25 + crates/ui-egui/src/theme.rs | 3 + xtask/src/main.rs | 2 + 64 files changed, 7388 insertions(+), 34 deletions(-) create mode 100644 crates/engine/src/commands/create.rs create mode 100644 crates/engine/src/psd_import.rs create mode 100644 crates/engine/src/tests_disk_cache.rs create mode 100644 crates/engine/src/tests_vector_import.rs create mode 100644 crates/engine/src/vector.rs create mode 100644 crates/host/tests/vector_import.rs create mode 100644 crates/media/src/layered.rs create mode 100644 crates/psd/Cargo.toml create mode 100644 crates/psd/README.md create mode 100644 crates/psd/src/descriptor.rs create mode 100644 crates/psd/src/engine_data.rs create mode 100644 crates/psd/src/lib.rs create mode 100644 crates/psd/src/read.rs create mode 100644 crates/psd/src/tests.rs create mode 100644 crates/psd/src/write.rs create mode 100644 crates/render/src/disk_cache.rs create mode 100644 crates/svg/Cargo.toml create mode 100644 crates/svg/README.md create mode 100644 crates/svg/src/color.rs create mode 100644 crates/svg/src/lib.rs create mode 100644 crates/svg/src/pathdata.rs create mode 100644 crates/svg/src/raster.rs create mode 100644 crates/svg/src/tests.rs create mode 100644 crates/svg/tests/fixtures/basic-shapes.svg create mode 100644 crates/svg/tests/fixtures/basic-shapes.svg.attribution create mode 100644 crates/svg/tests/fixtures/paths-gradients.svg create mode 100644 crates/svg/tests/fixtures/paths-gradients.svg.attribution diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 9caf1c27..d0b936b8 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -36,6 +36,8 @@ iconography, images or other Adobe assets; open licences only; every asset attri | `crates/lottie/tests/fixtures/shapes-gradient.json` | EffectCraft contributors | Original work: hand-written Lottie test fixture (star, rectangle, gradient fill, dashed stroke, trim paths) | MIT OR Apache-2.0 | | `crates/lottie/tests/fixtures/precomp-matte-text.json` | EffectCraft contributors | Original work: hand-written Lottie test fixture (precomp, track matte, parented text) | MIT OR Apache-2.0 | | `crates/lottie/tests/fixtures/legacy-masks.json` | EffectCraft contributors | Original work: hand-written Lottie test fixture (legacy keyframes, masks) | MIT OR Apache-2.0 | +| `crates/svg/tests/fixtures/basic-shapes.svg` | EffectCraft contributors | Original work: hand-written SVG test fixture (basic shapes, groups, transforms) | MIT OR Apache-2.0 | +| `crates/svg/tests/fixtures/paths-gradients.svg` | EffectCraft contributors | Original work: hand-written SVG test fixture (path data, gradients, CSS, use) | MIT OR Apache-2.0 | ## Screenshots diff --git a/Cargo.lock b/Cargo.lock index 46f34e63..1d14bab9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1198,12 +1198,15 @@ dependencies = [ "effectcraft-lottie", "effectcraft-path", "effectcraft-project", + "effectcraft-psd", "effectcraft-raster", "effectcraft-render", + "effectcraft-svg", "effectcraft-text", "effectcraft-time", "effectcraft-track", "image", + "kurbo", "log", "rayon", "serde", @@ -1287,6 +1290,10 @@ dependencies = [ "effectcraft-expr", "effectcraft-media", "effectcraft-project", + "effectcraft-psd", + "effectcraft-raster", + "effectcraft-svg", + "effectcraft-time", "serde_json", ] @@ -1324,8 +1331,10 @@ name = "effectcraft-media" version = "0.1.0" dependencies = [ "effectcraft-project", + "effectcraft-psd", "effectcraft-raster", "effectcraft-render", + "effectcraft-svg", "effectcraft-time", "filmcraft-codecs", "filmcraft-color", @@ -1363,6 +1372,13 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "effectcraft-psd" +version = "0.1.0" +dependencies = [ + "miniz_oxide 0.8.9", +] + [[package]] name = "effectcraft-raster" version = "0.1.0" @@ -1387,11 +1403,24 @@ dependencies = [ "effectcraft-time", "kurbo", "log", + "lz4_flex", "rayon", "serde", "web-time", ] +[[package]] +name = "effectcraft-svg" +version = "0.1.0" +dependencies = [ + "effectcraft-geom", + "effectcraft-path", + "effectcraft-raster", + "kurbo", + "rayon", + "roxmltree", +] + [[package]] name = "effectcraft-text" version = "0.1.0" @@ -2749,6 +2778,12 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" +[[package]] +name = "lz4_flex" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373f5eceeeab7925e0c1098212f2fbc4d416adec9d35051a6ab251e824c1854a" + [[package]] name = "mach2" version = "0.4.3" @@ -3970,6 +4005,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "roxmltree" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1964b10c76125c36f8afe190065a4bf9a87bf324842c05701330bba9f1cacbb" +dependencies = [ + "memchr", +] + [[package]] name = "rustc-hash" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index a1941cf4..7aabc4af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ effectcraft-media = { path = "crates/media" } effectcraft-export = { path = "crates/export" } effectcraft-expr = { path = "crates/expr" } effectcraft-lottie = { path = "crates/lottie" } +effectcraft-psd = { path = "crates/psd" } +effectcraft-svg = { path = "crates/svg" } effectcraft-engine = { path = "crates/engine" } effectcraft-host = { path = "crates/host" } effectcraft-ui-egui = { path = "crates/ui-egui" } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 132bdcac..0830c78d 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -22,6 +22,9 @@ effectcraft-effects = { workspace = true } effectcraft-render = { workspace = true } effectcraft-track = { workspace = true } effectcraft-lottie = { workspace = true } +effectcraft-psd = { workspace = true } +kurbo = { workspace = true } +effectcraft-svg = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/engine/src/commands/create.rs b/crates/engine/src/commands/create.rs new file mode 100644 index 00000000..72cb2420 --- /dev/null +++ b/crates/engine/src/commands/create.rs @@ -0,0 +1,139 @@ +//! Layer ▸ Create: Convert to Editable Text (Photoshop type layers), Create Shapes from Text, +//! Create Masks from Text and Create Shapes from Vector Layer (SVG footage). + +use effectcraft_color::Label; +use effectcraft_project::{ItemKind, LayerSource, Solid}; +use serde_json::{Value, json}; + +use super::{CommandSpec, bad, has_layers, layers_p, str_p}; +use crate::{EngineError, Result, Session, cmd}; + +const OPS: &str = "editableText|shapesFromText|masksFromText|shapesFromVector"; + +fn create(s: &mut Session, p: &Value) -> Result { + let op = str_p(p, "op").ok_or_else(|| bad("layer.create", format!("missing `op` ({OPS})")))?; + let (cid, lids) = layers_p(s, p)?; + if lids.is_empty() { + return Err(bad("layer.create", "select a layer first")); + } + let time = s.time(); + let mut created = vec![]; + let mut skipped = vec![]; + for lid in lids { + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone(); + let Some(src) = comp.layer(lid).cloned() else { continue }; + let footage = src.source.item().and_then(|i| match &s.project.item(i)?.kind { + ItemKind::Footage(f) => Some(f.clone()), + _ => None, + }); + match op { + "shapesFromVector" => { + let Some(f) = footage.filter(|f| f.codec == "SVG" || f.path.to_ascii_lowercase().ends_with(".svg")) else { + skipped.push(format!("{}: not a vector (SVG) footage layer", src.name)); + continue; + }; + let bytes = s.services.read_file(&f.path).map_err(|e| EngineError::Other(format!("cannot read {}: {e}", f.path)))?; + let doc = effectcraft_svg::parse(&bytes).map_err(|e| EngineError::Other(format!("{}: {e}", f.path)))?; + let id = s.edit("Create Shapes from Vector Layer", None, |proj, st| { + let l = crate::vector::shapes_from_vector(proj, &comp, &src, &doc); + let id = l.id; + insert_above(proj, cid, lid, l, true)?; + st.selected_layers = vec![id]; + Ok(id) + })?; + created.push(id.0); + } + "shapesFromText" => { + if !matches!(src.source, LayerSource::Text) { + skipped.push(format!("{}: not a text layer", src.name)); + continue; + } + let snapshot = s.project.clone(); + let id = s.edit("Create Shapes from Text", None, |proj, st| { + let l = crate::vector::shapes_from_text(proj, &comp, &snapshot, cid, &src, time) + .ok_or_else(|| bad("layer.create", "the text layer has no glyphs"))?; + let id = l.id; + insert_above(proj, cid, lid, l, true)?; + st.selected_layers = vec![id]; + Ok(id) + })?; + created.push(id.0); + } + "masksFromText" => { + if !matches!(src.source, LayerSource::Text) { + skipped.push(format!("{}: not a text layer", src.name)); + continue; + } + let outlines = crate::vector::text_outlines_in_comp(&s.project, cid, &comp, &src, time); + if outlines.is_empty() { + skipped.push(format!("{}: no glyphs", src.name)); + continue; + } + let id = s.edit("Create Masks from Text", None, |proj, st| { + let name = comp.unique_layer_name(&format!("{} Outlines", src.name)); + let folder = proj.folder_named("Solids").unwrap_or_else(|| proj.add_item("Solids", Label::Yellow, None, ItemKind::Folder)); + let sid = proj.add_item( + &name, + Label::Red, + Some(folder), + ItemKind::Solid(Solid { color: [1.0, 1.0, 1.0], width: comp.width, height: comp.height, pixel_aspect: 1.0 }), + ); + let mut l = crate::vector::masks_layer(proj, &comp, &name, outlines, sid); + l.in_point = src.in_point; + l.out_point = src.out_point; + let id = l.id; + insert_above(proj, cid, lid, l, true)?; + st.selected_layers = vec![id]; + Ok(id) + })?; + created.push(id.0); + } + "editableText" => { + let Some((f, sl)) = footage.and_then(|f| f.layer.clone().map(|l| (f, l))) else { + skipped.push(format!("{}: not a Photoshop layer", src.name)); + continue; + }; + let bytes = s.services.read_file(&f.path).map_err(|e| EngineError::Other(format!("cannot read {}: {e}", f.path)))?; + let psd = effectcraft_psd::Psd::parse(bytes).map_err(|e| EngineError::Other(e.to_string()))?; + let id = s.edit("Convert to Editable Text", None, |proj, st| { + let l = crate::psd_import::editable_text(proj, &comp, &src, &psd, sl.index as usize) + .ok_or_else(|| bad("layer.create", format!("{}: the Photoshop layer has no text", src.name)))?; + let id = l.id; + let c = proj.comp_mut(cid).ok_or(EngineError::NoComp)?; + let at = c.layers.iter().position(|x| x.id == lid).ok_or(EngineError::NoComp)?; + c.layers[at] = l; + st.selected_layers = vec![id]; + Ok(id) + })?; + created.push(id.0); + } + other => return Err(bad("layer.create", format!("`{other}` is not available yet (supported: {OPS})"))), + } + } + if created.is_empty() && !skipped.is_empty() { + return Err(bad("layer.create", skipped.join("; "))); + } + Ok(json!({"layers": created, "skipped": skipped})) +} + +/// Insert `l` directly above layer `below`; optionally switch the source's video off (as After +/// Effects does after converting). +fn insert_above( + proj: &mut effectcraft_project::Project, + cid: effectcraft_project::ItemId, + below: effectcraft_project::LayerId, + l: effectcraft_project::Layer, + hide_source: bool, +) -> Result<()> { + let c = proj.comp_mut(cid).ok_or(EngineError::NoComp)?; + let at = c.layers.iter().position(|x| x.id == below).unwrap_or(0); + if hide_source && let Some(src) = c.layers.get_mut(at) { + src.switches.video = false; + } + c.layers.insert(at, l); + Ok(()) +} + +pub fn specs() -> Vec { + vec![cmd!("layer.create", "Create", [], None, "{op: editableText|shapesFromText|masksFromText|shapesFromVector, layers?}", has_layers, create)] +} diff --git a/crates/engine/src/commands/edit.rs b/crates/engine/src/commands/edit.rs index e7ca4bd7..9a134b05 100644 --- a/crates/engine/src/commands/edit.rs +++ b/crates/engine/src/commands/edit.rs @@ -525,9 +525,38 @@ fn purge_caches(s: &mut Session, p: &Value) -> Result { s.snapshot = None; s.state.viewer.show_snapshot = false; } + let memory = matches!(what.as_str(), "all" | "memoryAndDisk" | "memory" | "image"); + let disk = matches!(what.as_str(), "all" | "memoryAndDisk" | "disk"); + if memory { + s.layer_cache.clear(); + } + let mut disk_entries = 0; + if disk && let Some(dc) = &s.disk_cache { + disk_entries = dc.stats().entries; + dc.clear(); + } s.events.push(crate::Event::PurgeCaches); s.toast(format!("Purged {what} cache")); - Ok(json!({"purged": what})) + Ok(json!({"purged": what, "diskEntries": disk_entries})) +} + +/// `cache.diskStats`: the disk cache's folder, limit and contents. +pub(crate) fn disk_stats(s: &mut Session, _: &Value) -> Result { + let Some(dc) = &s.disk_cache else { return Ok(json!({"enabled": false})) }; + let st = dc.stats(); + Ok(json!({ + "enabled": true, + "folder": dc.folder().to_string_lossy(), + "entries": st.entries, + "frames": dc.keys(effectcraft_render::disk_cache::Kind::Frame).len(), + "layers": dc.keys(effectcraft_render::disk_cache::Kind::Layer).len(), + "bytes": st.bytes, + "maxBytes": st.max_bytes, + "hits": st.hits, + "misses": st.misses, + "writes": st.writes, + "evictions": st.evictions, + })) } /// Path of the footage behind the selected layer or project item. @@ -596,6 +625,7 @@ pub fn specs() -> Vec { cmd!("edit.selectLabelGroup", "Select Label Group", ["Edit", "Label"], None, "{}", has_props_or_layers_or_items, select_label_group), cmd!("edit.purgeUndo", "Undo", ["Edit", "Purge"], None, "{}", always_ok, purge), cmd!("edit.purge", "Purge", [], None, "{what?: all|memoryAndDisk|memory|disk|3d|image|snapshot}", always_ok, purge_caches), + crate::query!("cache.diskStats", "Disk Cache Statistics", "{}", disk_stats), cmd!("edit.editOriginal", "Edit Original...", ["Edit"], Some("Cmd+E"), "{}", has_selected_footage, edit_original), ] } diff --git a/crates/engine/src/commands/file.rs b/crates/engine/src/commands/file.rs index a85a391f..a176c400 100644 --- a/crates/engine/src/commands/file.rs +++ b/crates/engine/src/commands/file.rs @@ -84,10 +84,48 @@ fn import(s: &mut Session, p: &Value) -> Result { Some(Value::String(x)) => vec![x.clone()], _ => return Err(bad("file.import", "missing `paths`")), }; - let importer = s.importer.clone().ok_or_else(|| EngineError::Other("media import is not available in this build".into()))?; + // Photoshop documents as compositions (Import As: Composition / – Retain Layer Sizes). + let import_as = str_p(p, "importAs").unwrap_or("footage"); + let retain = match import_as { + "footage" => None, + "composition" | "comp" => Some(false), + "compositionLayerSizes" | "compositionRetainLayerSizes" | "layerSizes" => Some(true), + other => return Err(bad("file.import", format!("importAs: footage|composition|compositionLayerSizes, not `{other}`"))), + }; + let mut paths = paths; + let mut out_comps = vec![]; let mut ids = vec![]; let mut errors = vec![]; + if let Some(retain) = retain { + let mut rest = vec![]; + for path in paths { + let bytes = match s.services.read_file(&path) { + Ok(b) if effectcraft_psd::is_psd(&b) => b, + _ => { + rest.push(path); + continue; + } + }; + match import_psd_comp(s, &path, bytes, retain) { + Ok((comp, items, warnings)) => { + out_comps.push(comp); + ids.extend(items); + errors.extend(warnings); + } + Err(e) => errors.push(format!("{path}: {e}")), + } + } + paths = rest; + if paths.is_empty() { + if let Some(c) = out_comps.first() { + s.open_comp(effectcraft_project::ItemId(*c)); + } + return Ok(json!({"items": ids, "comps": out_comps, "errors": errors})); + } + } + let importer = s.importer.clone().ok_or_else(|| EngineError::Other("media import is not available in this build".into()))?; let mut probed = vec![]; + let psd_layer = p.get("layer").cloned(); let seq_rate = effectcraft_time::FrameRate::from_f64(s.prefs.import.sequence_fps); for path in &paths { match importer.probe(path) { @@ -99,6 +137,18 @@ fn import(s: &mut Session, p: &Value) -> Result { f.frame_rate = r; f.duration = r.tick_of(frames.max(1)); } + // Choose Layer: one layer of a Photoshop document (document-sized). + if let Some(sel) = &psd_layer + && f.codec == "PSD" + { + match s.services.read_file(path).ok().and_then(|b| effectcraft_psd::Psd::parse(b).ok()).and_then(|d| find_psd_layer(&d, sel)) { + Some((index, name)) => f.layer = Some(effectcraft_project::SourceLayer { index: index as u32, name, layer_size: false }), + None => { + errors.push(format!("{path}: no layer {sel}")); + continue; + } + } + } probed.push((path.clone(), f)) } Err(e) => errors.push(format!("{path}: {e}")), @@ -107,6 +157,10 @@ fn import(s: &mut Session, p: &Value) -> Result { s.edit("Import", None, |proj, st| { for (path, f) in probed { let name = std::path::Path::new(&path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or(path.clone()); + let name = match &f.layer { + Some(l) => format!("{}/{name}", l.name), + None => name, + }; let label = match f.kind { FootageKind::Still | FootageKind::Sequence => Label::Lavender, FootageKind::Audio => Label::SeaFoam, @@ -118,7 +172,37 @@ fn import(s: &mut Session, p: &Value) -> Result { } Ok(()) })?; - Ok(json!({"items": ids, "errors": errors})) + if let Some(c) = out_comps.first() { + s.open_comp(effectcraft_project::ItemId(*c)); + } + Ok(json!({"items": ids, "comps": out_comps, "errors": errors})) +} + +/// A Photoshop layer by index or name (pixel layers only). +fn find_psd_layer(d: &effectcraft_psd::Psd, sel: &Value) -> Option<(usize, String)> { + let l = match sel { + Value::Number(n) => d.layers.get(n.as_u64()? as usize)?, + Value::String(name) => d.layers.iter().find(|l| &l.name == name)?, + _ => return None, + }; + Some((l.index, l.name.clone())) +} + +/// Import a Photoshop document as a composition (one undo step). Returns (comp, items, warnings). +fn import_psd_comp(s: &mut Session, path: &str, bytes: Vec, retain: bool) -> Result<(u64, Vec, Vec)> { + let psd = effectcraft_psd::Psd::parse(bytes).map_err(|e| EngineError::Other(e.to_string()))?; + let name = std::path::Path::new(path).file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "Photoshop".into()); + let rate = effectcraft_time::FrameRate::FPS_29_97; + let secs = if s.prefs.import.still_footage == "seconds" { s.prefs.import.still_seconds } else { 10.0 }; + let duration = rate.snap_nearest(effectcraft_time::Tick::from_seconds_f64(secs)); + let r = s.edit("Import", None, |proj, st| { + let r = crate::psd_import::import(proj, &psd, path, &name, retain, rate, duration); + st.project_selection = vec![r.comp]; + Ok(r) + })?; + let mut items = vec![r.folder.0]; + items.extend(r.items.iter().map(|i| i.0)); + Ok((r.comp.0, items, r.warnings)) } fn project_settings(s: &mut Session, p: &Value) -> Result { @@ -224,7 +308,15 @@ pub fn specs() -> Vec { cmd!("file.saveAs", "Save As...", ["File", "Save As"], Some("Cmd+Shift+S"), "{path}", always, save_as), cmd!("file.incrementAndSave", "Increment and Save", ["File"], Some("Cmd+Alt+Shift+S"), "{}", has_path, increment_save), cmd!("file.revert", "Revert", ["File"], None, "{}", has_path, revert), - cmd!("file.import", "File...", ["File", "Import"], Some("Cmd+I"), "{paths: [string]}", always, import), + cmd!( + "file.import", + "File...", + ["File", "Import"], + Some("Cmd+I"), + "{paths: [string], importAs?: footage|composition|compositionLayerSizes (Photoshop files), layer?: name|index (footage of one Photoshop layer)}", + always, + import + ), cmd!( "file.projectSettings", "Project Settings...", diff --git a/crates/engine/src/commands/file_more.rs b/crates/engine/src/commands/file_more.rs index 87c1e2df..b62664af 100644 --- a/crates/engine/src/commands/file_more.rs +++ b/crates/engine/src/commands/file_more.rs @@ -107,6 +107,7 @@ fn placeholder_footage(p: &Value) -> Footage { missing: true, sequence: vec![], color_profile: None, + layer: None, } } diff --git a/crates/engine/src/commands/mod.rs b/crates/engine/src/commands/mod.rs index 4537752f..0791d1a3 100644 --- a/crates/engine/src/commands/mod.rs +++ b/crates/engine/src/commands/mod.rs @@ -6,6 +6,7 @@ pub mod anim_tools; mod animation; mod comp; mod comp_more; +mod create; mod edit; mod effect; mod file; @@ -118,6 +119,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(help::specs()); v.extend(query::specs()); v.extend(layer_menu::specs()); + v.extend(create::specs()); v.extend(animation::specs()); v.extend(view::specs()); v.extend(viewer_cmds::specs()); diff --git a/crates/engine/src/commands/stubs.rs b/crates/engine/src/commands/stubs.rs index f670ffd7..f8bca154 100644 --- a/crates/engine/src/commands/stubs.rs +++ b/crates/engine/src/commands/stubs.rs @@ -34,7 +34,6 @@ pub fn specs() -> Vec { // Time (timeline milestone). stub!("layer.alignVideoToData", "Align Video to Data", ["Layer", "Time"], "{}"), stub!("layer.environment", "Environment Layer", ["Layer"], "{}"), - stub!("layer.create", "Create", [], "{op}"), stub!("layer.autoTrace", "Auto-trace...", ["Layer"], "{}"), stub!("layer.sceneEditDetection", "Scene Edit Detection...", ["Layer"], "{}"), // Cameras / lights / materials / 3D views (3D milestone). diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index ba34488a..9d6afcc0 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -16,10 +16,12 @@ pub mod links; pub mod mask_track; pub mod menus; pub mod prefs; +pub mod psd_import; pub mod render_queue; mod session_settings; pub mod shortcuts; pub mod tracking; +pub mod vector; pub mod viewer; pub mod warp; @@ -268,6 +270,8 @@ pub struct Session { pub journal: Vec<(String, Value)>, /// Processed-layer pixels reused across frames and edits (content-keyed, never stale). pub layer_cache: Arc, + /// The persistent disk cache (Settings ▸ Media & Disk Cache), when enabled. + pub disk_cache: Option>, /// Settings (Preferences). pub prefs: prefs::Prefs, /// Bumped whenever settings change (frontends re-apply theme, labels…). @@ -311,6 +315,7 @@ impl Default for Session { events: vec![], journal: vec![], layer_cache: Arc::new(LayerCache::default()), + disk_cache: None, prefs: prefs::Prefs::default(), prefs_revision: 0, config: None, @@ -393,6 +398,9 @@ impl Session { pub fn bump(&mut self) { self.revision += 1; + if self.disk_cache.is_some() { + self.layer_cache.set_disk_salt(effectcraft_render::disk_cache::footage_salt(&self.project)); + } self.events.push(Event::ProjectChanged { revision: self.revision }); } @@ -574,6 +582,8 @@ mod tests_3d; #[cfg(test)] mod tests_anim_tools; #[cfg(test)] +mod tests_disk_cache; +#[cfg(test)] mod tests_effects; #[cfg(test)] mod tests_fidelity; @@ -602,6 +612,8 @@ mod tests_timeline; #[cfg(test)] mod tests_track; #[cfg(test)] +mod tests_vector_import; +#[cfg(test)] mod tests_viewer; /// Font families available to text layers (bundled + scanned system fonts). diff --git a/crates/engine/src/psd_import.rs b/crates/engine/src/psd_import.rs new file mode 100644 index 00000000..c14888ea --- /dev/null +++ b/crates/engine/src/psd_import.rs @@ -0,0 +1,634 @@ +//! Photoshop import as a composition (File ▸ Import ▸ File… with Import As: Composition or +//! Composition – Retain Layer Sizes), and Layer ▸ Create ▸ Convert to Editable Text. +//! +//! Every pixel layer becomes a footage item that names its layer (`Footage::layer`), placed in +//! a composition the size of the document. Groups become precomps (pass-through groups collapse +//! so their blend modes act on the layers below, like Photoshop); type layers become editable +//! text layers; layer masks are baked into the layer's alpha and vector masks become masks; +//! layer effects map to Layer Styles; adjustment layers become adjustment layers with the +//! matching effect (best effort); clipped layers preserve the transparency underneath. + +use std::sync::Arc; + +use effectcraft_color::BlendMode; +use effectcraft_color::Label; +use effectcraft_keyframe::{Gradient, Justify, ShapePath, TextDoc, Value}; +use effectcraft_project::build::{self, Ids}; +use effectcraft_project::styles::{self, STYLE_BLEND_MODES}; +use effectcraft_project::{AlphaMode, Comp, Footage, FootageKind, ItemId, ItemKind, Layer, LayerSource, MaskMode, Project, PropGroup, Solid, SourceLayer}; +use effectcraft_psd::{Adjustment, DValue, Descriptor, Node, Psd}; +use effectcraft_time::{FrameRate, Tick}; + +/// Photoshop blend mode key → compositor mode (`pass` = pass through, handled by collapsing). +pub fn blend_mode(key: &str) -> BlendMode { + match key { + "diss" => BlendMode::Dissolve, + "dark" => BlendMode::Darken, + "mul " => BlendMode::Multiply, + "idiv" => BlendMode::ColorBurn, + "lbrn" => BlendMode::LinearBurn, + "dkCl" => BlendMode::DarkerColor, + "lite" => BlendMode::Lighten, + "scrn" => BlendMode::Screen, + "div " => BlendMode::ColorDodge, + "lddg" => BlendMode::LinearDodge, + "lgCl" => BlendMode::LighterColor, + "over" => BlendMode::Overlay, + "sLit" => BlendMode::SoftLight, + "hLit" => BlendMode::HardLight, + "vLit" => BlendMode::VividLight, + "lLit" => BlendMode::LinearLight, + "pLit" => BlendMode::PinLight, + "hMix" => BlendMode::HardMix, + "diff" => BlendMode::Difference, + "smud" => BlendMode::Exclusion, + "fsub" => BlendMode::Subtract, + "fdiv" => BlendMode::Divide, + "hue " => BlendMode::Hue, + "sat " => BlendMode::Saturation, + "colr" => BlendMode::Color, + "lum " => BlendMode::Luminosity, + _ => BlendMode::Normal, + } +} + +/// Descriptor blend mode enum (`BlnM`) → compositor mode. +fn desc_blend(v: Option<&str>) -> Option { + Some(match v? { + "Nrml" => BlendMode::Normal, + "Dslv" => BlendMode::Dissolve, + "Drkn" => BlendMode::Darken, + "Mltp" => BlendMode::Multiply, + "CBrn" => BlendMode::ColorBurn, + "linearBurn" => BlendMode::LinearBurn, + "darkerColor" => BlendMode::DarkerColor, + "Lghn" => BlendMode::Lighten, + "Scrn" => BlendMode::Screen, + "CDdg" => BlendMode::ColorDodge, + "linearDodge" => BlendMode::LinearDodge, + "lighterColor" => BlendMode::LighterColor, + "Ovrl" => BlendMode::Overlay, + "SftL" => BlendMode::SoftLight, + "HrdL" => BlendMode::HardLight, + "vividLight" => BlendMode::VividLight, + "linearLight" => BlendMode::LinearLight, + "pinLight" => BlendMode::PinLight, + "hardMix" => BlendMode::HardMix, + "Dfrn" => BlendMode::Difference, + "Xclu" => BlendMode::Exclusion, + "blendSubtraction" => BlendMode::Subtract, + "blendDivide" => BlendMode::Divide, + "H " => BlendMode::Hue, + "Strt" => BlendMode::Saturation, + "Clr " => BlendMode::Color, + "Lmns" => BlendMode::Luminosity, + _ => return None, + }) +} + +/// What the import created. +pub struct PsdImport { + pub comp: ItemId, + pub folder: ItemId, + pub items: Vec, + pub warnings: Vec, +} + +struct Ctx<'a> { + psd: &'a Psd, + path: &'a str, + retain: bool, + folder: ItemId, + rate: FrameRate, + duration: Tick, + items: Vec, + warnings: Vec, +} + +/// Import `psd` (read from `path`) as a composition named `name`. +pub fn import(proj: &mut Project, psd: &Psd, path: &str, name: &str, retain: bool, rate: FrameRate, duration: Tick) -> PsdImport { + let folder = proj.add_item(&format!("{name} Layers"), Label::Yellow, None, ItemKind::Folder); + let mut cx = Ctx { psd, path, retain, folder, rate, duration, items: vec![], warnings: vec![] }; + let comp = build_comp(proj, &mut cx, name, &psd.tree(), None); + PsdImport { comp, folder, items: cx.items, warnings: cx.warnings } +} + +fn new_comp(cx: &Ctx) -> Comp { + Comp::new(cx.psd.width, cx.psd.height, cx.rate, cx.duration) +} + +fn build_comp(proj: &mut Project, cx: &mut Ctx, name: &str, nodes: &[Node], parent: Option) -> ItemId { + let cid = proj.add_item(name, Label::Sandstone, parent, ItemKind::Comp(Arc::new(new_comp(cx)))); + cx.items.push(cid); + let mut comp = new_comp(cx); + for n in nodes { + if let Some(l) = build_layer(proj, cx, &comp, n) { + comp.layers.push(l); + } + } + styles::sync_comp(&mut comp); + if let Some(item) = proj.item_mut(cid) { + item.kind = ItemKind::Comp(Arc::new(comp)); + } + cid +} + +fn footage_item(proj: &mut Project, cx: &mut Ctx, index: usize, name: &str) -> (ItemId, (u32, u32)) { + let l = &cx.psd.layers[index]; + let retain = cx.retain; + let (w, h) = if retain && !l.rect.is_empty() { (l.rect.width(), l.rect.height()) } else { (cx.psd.width, cx.psd.height) }; + let file = std::path::Path::new(cx.path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); + let f = Footage { + path: cx.path.to_string(), + kind: FootageKind::Still, + width: w, + height: h, + pixel_aspect: 1.0, + frame_rate: cx.rate, + native_rate: None, + duration: Tick::ZERO, + has_video: true, + has_audio: false, + alpha: AlphaMode::Straight, + premul_color: [0.0; 3], + loop_count: 1, + codec: "PSD".into(), + missing: false, + sequence: vec![], + color_profile: None, + layer: Some(SourceLayer { index: index as u32, name: name.to_string(), layer_size: retain && !l.rect.is_empty() }), + }; + let id = proj.add_item(&format!("{name}/{file}"), Label::Lavender, Some(cx.folder), ItemKind::Footage(f)); + cx.items.push(id); + (id, (w, h)) +} + +fn build_layer(proj: &mut Project, cx: &mut Ctx, comp: &Comp, n: &Node) -> Option { + let (index, children) = match n { + Node::Layer(i) => (*i, None), + Node::Group { layer, children } => (*layer, Some(children)), + }; + let pl = cx.psd.layers[index].clone(); + let name = if pl.name.is_empty() { format!("Layer {}", index + 1) } else { pl.name.clone() }; + let mut layer = if let Some(children) = children { + let sub = build_comp(proj, cx, &name, children, Some(cx.folder)); + let mut l = build::layer(proj, comp, &name, LayerSource::Comp { item: sub }, (comp.width, comp.height), None); + // Pass-through groups composite their layers straight into the parent. + l.switches.collapse = pl.blend_key() == "pass"; + l + } else if let Some(t) = &pl.text { + text_layer(proj, comp, &name, t) + } else if let Some(adj) = &pl.adjustment { + let mut l = adjustment_layer(proj, comp, &name); + add_adjustment_effect(proj, &mut l, adj, comp, &mut cx.warnings, &name); + l + } else if pl.has_pixels() || pl.fill.is_some() { + let (item, size) = footage_item(proj, cx, index, &name); + let mut l = build::layer(proj, comp, &name, LayerSource::Footage { item }, size, None); + if cx.retain && !pl.rect.is_empty() { + set_vec2(&mut l, "transform/position", [pl.rect.left as f64 + size.0 as f64 / 2.0, pl.rect.top as f64 + size.1 as f64 / 2.0]); + } + l + } else { + // Empty layers have nothing to show. + return None; + }; + layer.name = comp.unique_layer_name(&name); + layer.blend_mode = blend_mode(pl.blend_key()); + layer.switches.video = !pl.hidden; + layer.preserve_transparency = pl.clipping; + set_scalar(&mut layer, "transform/opacity", pl.opacity as f64 / 255.0 * 100.0); + // Vector mask → a mask in layer space. + let origin = layer_origin(&pl, cx.retain, &layer); + if let Some(vm) = pl.vector_mask.as_ref().filter(|v| !v.disabled) + && let Some(masks) = layer.props.sub_mut("masks") + { + let mut ids = Ids(&mut proj.next_id); + for (k, sp) in vm.subpaths.iter().enumerate() { + let n = sp.knots.len(); + let mut path = ShapePath { vertices: vec![], in_tangents: vec![], out_tangents: vec![], closed: sp.closed }; + for kn in &sp.knots { + let v = [kn[1][0] - origin[0], kn[1][1] - origin[1]]; + path.vertices.push(v); + path.in_tangents.push([kn[0][0] - kn[1][0], kn[0][1] - kn[1][1]]); + path.out_tangents.push([kn[2][0] - kn[1][0], kn[2][1] - kn[1][1]]); + } + if n == 0 { + continue; + } + let mode = if vm.inverted { MaskMode::Subtract } else { MaskMode::Add }; + masks.children.push(build::mask(&mut ids, &format!("Mask {}", k + 1), path, mode, [255, 255, 0]).into()); + } + } + // Layer effects → Layer Styles; Fill Opacity → Advanced Blending. + let gl = comp.global_light.clone(); + if let Some(fx) = &pl.effects + && fx.bool("masterFXSwitch") != Some(false) + && layer.can_have_styles() + { + apply_effects(proj, &mut layer, fx, &gl); + } + if pl.fill_opacity < 255 && layer.can_have_styles() { + let mut ids = Ids(&mut proj.next_id); + if layer.props.sub(styles::GROUP).is_none() { + let g = styles::layer_styles(&mut ids, &gl); + let at = layer.props.children.iter().position(|c| c.match_id() == "transform").map(|i| i + 1).unwrap_or(layer.props.children.len()); + layer.props.children.insert(at, g.into()); + } + if let Some(p) = layer.props.prop_mut("layerStyles/blendingOptions/advancedBlending/fillOpacity") { + p.value = Value::Scalar(pl.fill_opacity as f64 / 255.0 * 100.0); + } + } + Some(layer) +} + +/// Where layer space's origin sits in document pixels. +fn layer_origin(pl: &effectcraft_psd::Layer, retain: bool, layer: &Layer) -> [f64; 2] { + if let Some(t) = &pl.text { + return [t.transform[4], t.transform[5]]; + } + if matches!(layer.source, LayerSource::Footage { .. }) && retain && !pl.rect.is_empty() { + return [pl.rect.left as f64, pl.rect.top as f64]; + } + [0.0, 0.0] +} + +fn set_scalar(l: &mut Layer, path: &str, v: f64) { + if let Some(p) = l.props.prop_mut(path) { + p.value = Value::Scalar(v); + } +} + +fn set_vec2(l: &mut Layer, path: &str, v: [f64; 2]) { + if let Some(p) = l.props.prop_mut(path) { + p.value = match p.value { + Value::Vec3(o) => Value::Vec3([v[0], v[1], o[2]]), + _ => Value::Vec2(v), + }; + } +} + +/// Split a PostScript font name into family and style (`Inter-Bold` → `Inter`, `Bold`). +fn font_names(ps: &str) -> (String, String) { + match ps.split_once('-') { + Some((fam, style)) => (fam.to_string(), style.to_string()), + None => (ps.to_string(), "Regular".to_string()), + } +} + +fn text_doc(t: &effectcraft_psd::TextInfo) -> TextDoc { + let mut d = TextDoc::default(); + d.set_text(&t.text); + let [xx, xy, yx, yy, _, _] = t.transform; + let scale = (xx * yy - xy * yx).abs().sqrt().max(1e-6); + if let Some(s) = t.style.size { + d.size = s * scale; + } + if let Some(f) = &t.style.font { + let (fam, style) = font_names(f); + d.font = fam; + d.style = style; + } + if let Some(c) = t.style.color { + d.fill = [c[0] as f32, c[1] as f32, c[2] as f32, 1.0]; + } + d.justify = match t.style.justification { + Some(1) => Justify::Right, + Some(2) => Justify::Center, + Some(3) => Justify::JustifyLastLeft, + Some(4) => Justify::JustifyLastRight, + Some(5) => Justify::JustifyLastCenter, + Some(6) => Justify::JustifyAll, + _ => Justify::Left, + }; + if let Some(tr) = t.style.tracking { + d.tracking = tr; + } + if let Some(l) = t.style.leading { + d.leading = Some(l * scale); + } + d.faux_bold = t.style.faux_bold; + d.faux_italic = t.style.faux_italic; + if t.style.box_text + && let Some([l, top, r, b]) = t.bounds + { + d.box_size = Some([(r - l) * scale, (b - top) * scale]); + d.box_pos = [l * scale, top * scale]; + } + d +} + +fn text_layer(proj: &mut Project, comp: &Comp, name: &str, t: &effectcraft_psd::TextInfo) -> Layer { + let mut l = build::layer(proj, comp, name, LayerSource::Text, (0, 0), None); + if let Some(p) = l.props.prop_mut("text/sourceText") { + p.value = Value::Text(Box::new(text_doc(t))); + } + set_vec2(&mut l, "transform/position", [t.transform[4], t.transform[5]]); + let [xx, xy, _, _, _, _] = t.transform; + let rot = xy.atan2(xx).to_degrees(); + if rot.abs() > 1e-6 { + set_scalar(&mut l, "transform/rotation", rot); + } + l +} + +fn adjustment_layer(proj: &mut Project, comp: &Comp, name: &str) -> Layer { + let folder = proj.folder_named("Solids").unwrap_or_else(|| proj.add_item("Solids", Label::Yellow, None, ItemKind::Folder)); + let sid = proj.add_item( + name, + Label::Red, + Some(folder), + ItemKind::Solid(Solid { color: [1.0, 1.0, 1.0], width: comp.width, height: comp.height, pixel_aspect: 1.0 }), + ); + let mut l = build::layer(proj, comp, name, LayerSource::Solid { item: sid }, (comp.width, comp.height), None); + l.switches.adjustment = true; + l.label = Label::Purple; + l +} + +fn add_effect(proj: &mut Project, l: &mut Layer, effect: &str, comp: &Comp, params: &[(&str, Value)]) { + let Some(spec) = effectcraft_effects::lookup(effect) else { return }; + let mut ids = Ids(&mut proj.next_id); + let mut g = effectcraft_effects::instantiate(spec, &mut ids, spec.name, [comp.width as f64, comp.height as f64]); + for (k, v) in params { + if let Some(p) = g.get_mut(k) { + p.value = v.clone(); + } + } + if let Some(fx) = l.props.sub_mut("effects") { + fx.children.push(g.into()); + } +} + +fn add_adjustment_effect(proj: &mut Project, l: &mut Layer, adj: &Adjustment, comp: &Comp, warnings: &mut Vec, name: &str) { + match adj { + Adjustment::Invert => add_effect(proj, l, "Invert", comp, &[]), + Adjustment::BrightnessContrast { brightness, contrast, .. } => { + add_effect(proj, l, "Brightness & Contrast", comp, &[("brightness", Value::Scalar(*brightness)), ("contrast", Value::Scalar(*contrast))]) + } + Adjustment::HueSaturation { hue, saturation, lightness, colorize } => { + let mut p = vec![("hue", Value::Scalar(*hue)), ("saturation", Value::Scalar(*saturation)), ("lightness", Value::Scalar(*lightness))]; + if *colorize { + p = vec![ + ("colorize", Value::Bool(true)), + ("colorizeHue", Value::Scalar(*hue)), + ("colorizeSaturation", Value::Scalar(*saturation)), + ("colorizeLightness", Value::Scalar(*lightness)), + ]; + } + add_effect(proj, l, "Hue/Saturation", comp, &p) + } + Adjustment::Levels { in_black, in_white, gamma, out_black, out_white } => add_effect( + proj, + l, + "Levels", + comp, + &[ + ("inBlack", Value::Scalar(in_black / 255.0)), + ("inWhite", Value::Scalar(in_white / 255.0)), + ("gamma", Value::Scalar(*gamma)), + ("outBlack", Value::Scalar(out_black / 255.0)), + ("outWhite", Value::Scalar(out_white / 255.0)), + ], + ), + Adjustment::Exposure { exposure, offset, gamma } => add_effect( + proj, + l, + "Exposure", + comp, + &[("exposure", Value::Scalar(*exposure)), ("offset", Value::Scalar(*offset)), ("gamma", Value::Scalar(*gamma))], + ), + Adjustment::Other(k) => warnings.push(format!("{name}: adjustment `{k}` is not converted (layer kept as an empty adjustment layer)")), + } +} + +// ---------------------------------------------------------------- layer effects → styles + +fn style_mode(m: BlendMode) -> Value { + Value::Enum(STYLE_BLEND_MODES.iter().position(|x| x.1 == m).unwrap_or(0) as u32) +} + +fn set(g: &mut PropGroup, k: &str, v: Value) { + if let Some(p) = g.get_mut(k) { + p.value = v; + } +} + +fn color4(c: [f64; 3]) -> Value { + Value::Color([c[0], c[1], c[2], 1.0]) +} + +/// Copy the common style settings (`Md `, `Clr `, `Opct`, angle, distance, size…). +fn common(g: &mut PropGroup, d: &Descriptor) { + if let Some(m) = desc_blend(d.enum_value("Md ")) { + set(g, "blendMode", style_mode(m)); + } + if let Some(c) = d.color("Clr ") { + set(g, "color", color4(c)); + } + for (k, prop) in [("Opct", "opacity"), ("Nose", "noise"), ("Dstn", "distance"), ("blur", "size"), ("lagl", "angle"), ("ShdN", "jitter"), ("Inpr", "range")] + { + if let Some(v) = d.num(k) { + set(g, prop, Value::Scalar(v)); + } + } + if let Some(b) = d.bool("uglg") { + set(g, "useGlobalLight", Value::Bool(b)); + } +} + +fn gradient_of(d: &Descriptor) -> Option { + let g = d.obj("Grad")?; + let list = |k: &str| match g.get(k) { + Some(DValue::List(v)) => v.clone(), + _ => vec![], + }; + let colors: Vec<(f64, [f32; 4])> = list("Clrs") + .iter() + .filter_map(|c| match c { + DValue::Descriptor(s) => { + let col = s.color("Clr ")?; + Some((s.num("Lctn").unwrap_or(0.0) / 4096.0, [col[0] as f32, col[1] as f32, col[2] as f32, 1.0])) + } + _ => None, + }) + .collect(); + let opacities: Vec<(f64, f32)> = list("Trns") + .iter() + .filter_map(|c| match c { + DValue::Descriptor(s) => Some((s.num("Lctn").unwrap_or(0.0) / 4096.0, (s.num("Opct").unwrap_or(100.0) / 100.0) as f32)), + _ => None, + }) + .collect(); + (!colors.is_empty()).then(|| Gradient { colors, opacities: if opacities.is_empty() { vec![(0.0, 1.0), (1.0, 1.0)] } else { opacities } }) +} + +/// The effect descriptor for `key`, or the first enabled one of its `…Multi` list. +fn effect<'a>(fx: &'a Descriptor, key: &str, multi: &str) -> Option<&'a Descriptor> { + fx.obj(key).or_else(|| match fx.get(multi) { + Some(DValue::List(v)) => v.iter().find_map(|x| match x { + DValue::Descriptor(d) if d.bool("enab") != Some(false) => Some(d), + _ => None, + }), + _ => None, + }) +} + +fn apply_effects(proj: &mut Project, layer: &mut Layer, fx: &Descriptor, gl: &styles::GlobalLight) { + const MAP: &[(&str, &str, &str)] = &[ + ("DrSh", "dropShadowMulti", "dropShadow"), + ("IrSh", "innerShadowMulti", "innerShadow"), + ("OrGl", "outerGlowMulti", "outerGlow"), + ("IrGl", "innerGlowMulti", "innerGlow"), + ("ebbl", "bevelEmbossMulti", "bevelEmboss"), + ("ChFX", "satinMulti", "satin"), + ("SoFi", "solidFillMulti", "colorOverlay"), + ("GrFl", "gradientFillMulti", "gradientOverlay"), + ("FrFX", "frameFXMulti", "stroke"), + ]; + for (key, multi, id) in MAP { + let Some(d) = effect(fx, key, multi) else { continue }; + let enabled = d.bool("enab").unwrap_or(true); + let mut ids = Ids(&mut proj.next_id); + let Some(uid) = styles::add_style(layer, &mut ids, id, gl, enabled) else { continue }; + let Some(g) = layer.props.find_group_mut(uid) else { continue }; + common(g, d); + match *id { + "dropShadow" => { + if let Some(v) = d.num("Ckmt") { + set(g, "spread", Value::Scalar(v)); + } + if let Some(b) = d.bool("layerConceals") { + set(g, "knocksOut", Value::Bool(b)); + } + } + "innerShadow" | "innerGlow" => { + if let Some(v) = d.num("Ckmt") { + set(g, "choke", Value::Scalar(v)); + } + if d.enum_value("glwS") == Some("SrcC") { + set(g, "source", Value::Enum(0)); + } + } + "outerGlow" => { + if let Some(v) = d.num("Ckmt") { + set(g, "spread", Value::Scalar(v)); + } + } + "bevelEmboss" => { + let style = match d.enum_value("bvlS") { + Some("OtrB") => 0, + Some("Embs") => 2, + Some("PlEb") => 3, + Some("strokeEmboss") => 4, + _ => 1, + }; + set(g, "style", Value::Enum(style)); + let tech = match d.enum_value("bvlT") { + Some("PrBL") => 1, + Some("Slmt") => 2, + _ => 0, + }; + set(g, "technique", Value::Enum(tech)); + if let Some(v) = d.num("srgR") { + set(g, "depth", Value::Scalar(v)); + } + set(g, "direction", Value::Enum(u32::from(d.enum_value("bvlD") == Some("Out ")))); + if let Some(v) = d.num("Sftn") { + set(g, "soften", Value::Scalar(v)); + } + if let Some(v) = d.num("Lald") { + set(g, "altitude", Value::Scalar(v)); + } + for (k, p) in [("hglM", "highlightMode"), ("sdwM", "shadowMode")] { + if let Some(m) = desc_blend(d.enum_value(k)) { + set(g, p, style_mode(m)); + } + } + for (k, p) in [("hglC", "highlightColor"), ("sdwC", "shadowColor")] { + if let Some(c) = d.color(k) { + set(g, p, color4(c)); + } + } + for (k, p) in [("hglO", "highlightOpacity"), ("sdwO", "shadowOpacity")] { + if let Some(v) = d.num(k) { + set(g, p, Value::Scalar(v)); + } + } + } + "satin" => { + if let Some(b) = d.bool("Invr") { + set(g, "invert", Value::Bool(b)); + } + } + "gradientOverlay" => { + if let Some(gr) = gradient_of(d) { + set(g, "colors", Value::Gradient(gr)); + } + if let Some(a) = d.num("Angl") { + set(g, "angle", Value::Scalar(a)); + } + let st = match d.enum_value("Type") { + Some("Rdl ") => 1, + Some("Angl") => 2, + Some("Rflc") => 3, + Some("Dmnd") => 4, + _ => 0, + }; + set(g, "style", Value::Enum(st)); + if let Some(b) = d.bool("Rvrs") { + set(g, "reverse", Value::Bool(b)); + } + if let Some(b) = d.bool("Algn") { + set(g, "alignWithLayer", Value::Bool(b)); + } + if let Some(v) = d.num("Scl ") { + set(g, "scale", Value::Scalar(v)); + } + } + "stroke" => { + if let Some(v) = d.num("Sz ") { + set(g, "size", Value::Scalar(v)); + } + let pos = match d.enum_value("Styl") { + Some("InsF") => 1, + Some("CtrF") => 2, + _ => 0, + }; + set(g, "position", Value::Enum(pos)); + } + _ => {} + } + } +} + +/// Layer ▸ Create ▸ Convert to Editable Text: a footage layer showing a Photoshop type layer +/// becomes a text layer (`None` when the layer's source has no text data). +pub fn editable_text(proj: &mut Project, comp: &Comp, layer: &Layer, psd: &Psd, index: usize) -> Option { + let t = psd.layers.get(index)?.text.as_ref()?; + let mut l = text_layer(proj, comp, &layer.name, t); + l.name = layer.name.clone(); + l.blend_mode = layer.blend_mode; + l.switches.video = layer.switches.video; + l.in_point = layer.in_point; + l.out_point = layer.out_point; + l.start_time = layer.start_time; + if let (Some(o), Some(p)) = (layer.props.prop("transform/opacity"), l.props.prop_mut("transform/opacity")) { + *p = effectcraft_project::Property { uid: p.uid, ..o.clone() }; + } + Some(l) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn blend_keys() { + assert_eq!(blend_mode("mul "), BlendMode::Multiply); + assert_eq!(blend_mode("smud"), BlendMode::Exclusion); + assert_eq!(blend_mode("pass"), BlendMode::Normal); + assert_eq!(desc_blend(Some("linearDodge")), Some(BlendMode::LinearDodge)); + assert_eq!(font_names("Inter-Bold"), ("Inter".into(), "Bold".into())); + } +} diff --git a/crates/engine/src/session_settings.rs b/crates/engine/src/session_settings.rs index b0512a6a..e54eb93b 100644 --- a/crates/engine/src/session_settings.rs +++ b/crates/engine/src/session_settings.rs @@ -27,6 +27,7 @@ impl Session { self.prefs.normalize(); self.layer_cache.set_budget(self.prefs.layer_cache_bytes()); self.footage.set_cache_budget(self.prefs.media_cache_bytes()); + self.configure_disk_cache(); // Fewer undo levels apply right away. let levels = self.prefs.general.undo_levels.max(1) as usize; if self.history.undo.len() > levels { @@ -36,6 +37,42 @@ impl Session { self.prefs_revision += 1; } + /// Apply Settings ▸ Media & Disk Cache ▸ Disk Cache: open (or close) the persistent cache + /// and attach it to the layer cache. Without a folder setting the platform cache folder is + /// used, but only by frontends with a settings store (headless sessions stay off disk). + pub fn configure_disk_cache(&mut self) { + use effectcraft_render::disk_cache::{DiskCache, default_folder, footage_salt}; + let d = &self.prefs.disk; + let folder = if d.disk_cache_folder.trim().is_empty() { + self.config.is_some().then(default_folder) + } else { + Some(std::path::PathBuf::from(d.disk_cache_folder.trim())) + }; + let max = d.disk_cache_max_gb.max(1) as u64 * (1 << 30); + let Some(folder) = folder.filter(|_| d.disk_cache_enabled) else { + self.disk_cache = None; + self.layer_cache.set_disk(None); + return; + }; + let dc = match &self.disk_cache { + Some(c) if c.folder() == folder.as_path() => { + c.set_max_bytes(max); + c.clone() + } + _ => match DiskCache::open(&folder, max) { + Ok(c) => c, + Err(e) => { + log::warn!("disk cache at {}: {e}", folder.display()); + self.disk_cache = None; + self.layer_cache.set_disk(None); + return; + } + }, + }; + self.layer_cache.set_disk(Some((dc.clone(), footage_salt(&self.project)))); + self.disk_cache = Some(dc); + } + /// Write settings to the config store. pub fn save_prefs(&self) { if let Some(c) = &self.config diff --git a/crates/engine/src/tests_disk_cache.rs b/crates/engine/src/tests_disk_cache.rs new file mode 100644 index 00000000..65180ba8 --- /dev/null +++ b/crates/engine/src/tests_disk_cache.rs @@ -0,0 +1,121 @@ +//! The persistent disk cache (PRV-2) through the session: settings, layer cache persistence +//! across restarts, invalidation on edits, purging. + +use serde_json::json; + +use crate::Session; +use crate::render::RenderOpts; + +fn folder(name: &str) -> String { + let d = std::env::temp_dir().join(format!("effectcraft-session-disk-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d.to_string_lossy().to_string() +} + +fn session(dir: &str) -> Session { + let mut s = Session::default(); + s.execute_checked("prefs.set", json!({"values": {"disk.diskCacheFolder": dir, "disk.diskCacheEnabled": true}})).unwrap(); + // Write every layer buffer, however quick to render. + s.disk_cache.as_ref().unwrap().set_min_layer_ms(0); + s +} + +fn project(s: &mut Session) -> effectcraft_project::ItemId { + s.execute("comp.new", json!({"name": "D", "width": 160, "height": 90, "frameRate": 30, "duration": 1})).unwrap(); + s.execute("layer.newSolid", json!({"color": "#3080ff", "width": 80, "height": 40})).unwrap(); + s.execute("effect.apply", json!({"layer": "#1", "effect": "Gaussian Blur"})).unwrap(); + s.execute("prop.set", json!({"layer": "#1", "path": "effects/#1/blurriness", "value": 12})).unwrap(); + s.active_comp_id().unwrap() +} + +#[test] +fn disk_cache_settings_persistence_and_invalidation() { + let dir = folder("persist"); + let mut s = session(&dir); + let st = s.execute_checked("cache.diskStats", json!({})).unwrap(); + assert_eq!(st["enabled"], json!(true)); + assert_eq!(st["maxBytes"], json!(100u64 << 30)); + let cid = project(&mut s); + let t = s.time(); + let first = s.render(cid, t, RenderOpts::default()); + let dc = s.disk_cache.clone().unwrap(); + dc.flush(); + let written = dc.stats().entries; + assert!(written > 0, "layer buffers written"); + // "Restart": a new session on the same folder and project renders from disk. + let saved = s.project.clone(); + drop(s); + let mut s2 = session(&dir); + s2.project = saved; + s2.bump(); + s2.open_comp(cid); + let dc2 = s2.disk_cache.clone().unwrap(); + assert_eq!(dc2.stats().entries, written); + let again = s2.render(cid, t, RenderOpts::default()); + assert!(dc2.stats().hits > 0, "{:?}", dc2.stats()); + assert_eq!(again.data, first.data, "disk-cached pixels are exact"); + // An edit changes the keys: a miss, and new entries. + let hits = dc2.stats().hits; + s2.execute("prop.set", json!({"layer": "#1", "path": "effects/#1/blurriness", "value": 20})).unwrap(); + let edited = s2.render(cid, t, RenderOpts::default()); + assert_ne!(edited.data, first.data); + assert_eq!(dc2.stats().hits, hits); + dc2.flush(); + assert!(dc2.stats().entries > written); + // Purge ▸ All Disk Cache empties it; memory purge leaves the disk alone. + let r = s2.execute_checked("edit.purge", json!({"what": "memory"})).unwrap(); + assert_eq!(r["diskEntries"], json!(0)); + assert!(dc2.stats().entries > 0); + let r = s2.execute_checked("edit.purge", json!({"what": "disk"})).unwrap(); + assert!(r["diskEntries"].as_u64().unwrap() > 0); + assert_eq!(dc2.stats().entries, 0); + // Disabling detaches it. + s2.execute_checked("prefs.set", json!({"key": "disk.diskCacheEnabled", "value": false})).unwrap(); + assert!(s2.disk_cache.is_none()); + assert!(s2.layer_cache.disk().is_none()); +} + +#[test] +fn footage_changes_change_the_salt() { + let dir = folder("salt"); + let mut s = session(&dir); + let foot = std::env::temp_dir().join(format!("effectcraft-salt-{}.png", std::process::id())); + std::fs::write(&foot, b"one").unwrap(); + s.edit("add", None, |p, _| { + let f = effectcraft_project::Footage { + path: foot.to_string_lossy().to_string(), + kind: effectcraft_project::FootageKind::Still, + width: 1, + height: 1, + pixel_aspect: 1.0, + frame_rate: effectcraft_time::FrameRate::FPS_30, + native_rate: None, + duration: effectcraft_time::Tick::ZERO, + has_video: true, + has_audio: false, + alpha: effectcraft_project::AlphaMode::Ignore, + premul_color: [0.0; 3], + loop_count: 1, + codec: "PNG".into(), + missing: false, + sequence: vec![], + color_profile: None, + layer: None, + }; + p.add_item("f.png", effectcraft_color::Label::Lavender, None, effectcraft_project::ItemKind::Footage(f)); + Ok(()) + }) + .unwrap(); + let a = effectcraft_render::disk_cache::footage_salt(&s.project); + std::fs::write(&foot, b"two, longer").unwrap(); + let b = effectcraft_render::disk_cache::footage_salt(&s.project); + assert_ne!(a, b); + let cid = project(&mut s); + let k1 = effectcraft_render::disk_cache::comp_content_key(&s.project, cid); + s.execute("prop.set", json!({"layer": "#1", "path": "transform/opacity", "value": 50})).unwrap(); + let k2 = effectcraft_render::disk_cache::comp_content_key(&s.project, cid); + assert_ne!(k1, k2); + // Unrelated comps do not change it. + s.execute("comp.new", json!({"name": "Other", "width": 10, "height": 10, "frameRate": 30, "duration": 1})).unwrap(); + assert_eq!(effectcraft_render::disk_cache::comp_content_key(&s.project, cid), k2); +} diff --git a/crates/engine/src/tests_stubs.rs b/crates/engine/src/tests_stubs.rs index 141858ca..17ffda7f 100644 --- a/crates/engine/src/tests_stubs.rs +++ b/crates/engine/src/tests_stubs.rs @@ -257,6 +257,7 @@ impl Importer for MockImporter { missing: false, sequence: vec![], color_profile: None, + layer: None, }) } } diff --git a/crates/engine/src/tests_track.rs b/crates/engine/src/tests_track.rs index adcf7df0..8cf020f9 100644 --- a/crates/engine/src/tests_track.rs +++ b/crates/engine/src/tests_track.rs @@ -101,6 +101,7 @@ pub(crate) fn setup(make: impl Fn(u32) -> Image + Send + Sync + 'static) -> (Ses missing: false, sequence: vec![], color_profile: None, + layer: None, }; let item = p.add_item("clip", Label::Aqua, None, ItemKind::Footage(footage)); let comp = p.comp(cid).unwrap().clone(); @@ -410,6 +411,7 @@ fn analyze_1080p_single_point_speed() { missing: false, sequence: vec![], color_profile: None, + layer: None, }; let item = p.add_item("hd", Label::Aqua, None, ItemKind::Footage(footage)); let comp = p.comp(cid).unwrap().clone(); diff --git a/crates/engine/src/tests_vector_import.rs b/crates/engine/src/tests_vector_import.rs new file mode 100644 index 00000000..d205630c --- /dev/null +++ b/crates/engine/src/tests_vector_import.rs @@ -0,0 +1,237 @@ +//! Photoshop import as a composition (IO-2) and Layer ▸ Create text conversions (SHP-5). + +use effectcraft_color::BlendMode; +use effectcraft_keyframe::Value as KV; +use effectcraft_project::{ItemKind, LayerSource, MaskMode}; +use effectcraft_psd::write::*; +use effectcraft_psd::{DValue, Descriptor, Rect}; +use serde_json::json; + +use crate::Session; +use crate::render::RenderOpts; + +fn tmp(name: &str) -> String { + let d = std::env::temp_dir().join(format!("effectcraft-psd-tests-{}", std::process::id())); + std::fs::create_dir_all(&d).unwrap(); + d.join(name).to_string_lossy().to_string() +} + +/// A layered document exercising groups, masks, blend modes, text, styles and adjustments. +pub(crate) fn sample_psd() -> Vec { + let mut d = WDoc::new(120, 80); + let shadow = Descriptor::new("null").with("masterFXSwitch", DValue::Bool(true)).with( + "DrSh", + DValue::Descriptor( + Descriptor::new("DrSh") + .with("enab", DValue::Bool(true)) + .with("Md ", DValue::Enum("BlnM".into(), "Mltp".into())) + .with("Clr ", DValue::Descriptor(Descriptor::rgb([0.0, 0.0, 0.5]))) + .with("Opct", DValue::UnitFloat("#Prc".into(), 60.0)) + .with("uglg", DValue::Bool(false)) + .with("lagl", DValue::UnitFloat("#Ang".into(), 45.0)) + .with("Dstn", DValue::UnitFloat("#Pxl".into(), 9.0)) + .with("blur", DValue::UnitFloat("#Pxl".into(), 4.0)), + ), + ); + let stroke = Descriptor::new("null").with( + "FrFX", + DValue::Descriptor( + Descriptor::new("FrFX") + .with("enab", DValue::Bool(true)) + .with("Styl", DValue::Enum("FStl".into(), "InsF".into())) + .with("Md ", DValue::Enum("BlnM".into(), "Nrml".into())) + .with("Opct", DValue::UnitFloat("#Prc".into(), 100.0)) + .with("Sz ", DValue::UnitFloat("#Pxl".into(), 2.0)) + .with("Clr ", DValue::Descriptor(Descriptor::rgb([1.0, 1.0, 0.0]))), + ), + ); + let mut masked = WLayer::solid("Masked Square", Rect::new(10, 10, 40, 40), [0.0, 0.8, 0.2, 1.0]).with_blend(b"mul "); + masked.mask = Some(WMask { + rect: Rect::new(10, 10, 40, 40), + data: (0..1600).map(|i| if i % 40 < 20 { 1.0 } else { 0.0 }).collect(), + default_color: 0, + disabled: false, + }); + let mut hidden = WLayer::solid("Hidden", Rect::new(0, 0, 10, 10), [1.0, 0.0, 1.0, 1.0]); + hidden.hidden = true; + let mut clipped = WLayer::solid("Clipped", Rect::new(60, 30, 20, 20), [1.0, 1.0, 1.0, 1.0]).with_blend(b"scrn").with_opacity(128); + clipped.clipping = true; + let tri = vec![[[70.0, 10.0]; 3], [[110.0, 10.0]; 3], [[90.0, 40.0]; 3]]; + d.layers = vec![ + WLayer::solid("Background", Rect::new(0, 0, 120, 80), [0.2, 0.3, 0.4, 1.0]), + WLayer::group_end(), + masked, + hidden, + WLayer::group("Group A", true, *b"pass"), + WLayer::solid("Base", Rect::new(55, 25, 30, 30), [0.9, 0.1, 0.1, 1.0]).with_block(effects_block(&shadow)), + clipped, + WLayer::solid("Vector Masked", Rect::new(60, 0, 60, 50), [0.1, 0.1, 0.9, 1.0]) + .with_block(vector_mask_block(&[tri], 120, 80, false)) + .with_block(effects_block(&stroke)), + WLayer::empty("Hue Shift", vec![hue_sat_block(40, 10, -5)]), + WLayer::empty("Title", vec![text_block("Hello PSD", [8.0, 70.0], "Inter-Bold", 14.0, [1.0, 1.0, 1.0], 0)]), + ]; + write(&d) +} + +#[test] +fn psd_imports_as_composition() { + let path = tmp("sample.psd"); + std::fs::write(&path, sample_psd()).unwrap(); + let mut s = Session::default(); + let r = s.execute_checked("file.import", json!({"paths": [path], "importAs": "composition"})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let cid = effectcraft_project::ItemId(r["comps"][0].as_u64().unwrap()); + assert_eq!(s.state.active_comp, Some(cid)); + assert_eq!(s.project.item(cid).unwrap().name, "sample"); + let c = s.project.comp(cid).unwrap().clone(); + assert_eq!((c.width, c.height), (120, 80)); + let names: Vec<&str> = c.layers.iter().map(|l| l.name.as_str()).collect(); + assert_eq!(names, ["Title", "Hue Shift", "Vector Masked", "Clipped", "Base", "Group A", "Background"]); + // Text layer: editable text, positioned at the type origin. + let title = &c.layers[0]; + assert!(matches!(title.source, LayerSource::Text)); + let Some(KV::Text(doc)) = title.props.prop("text/sourceText").map(|p| p.value.clone()) else { panic!("text") }; + assert_eq!(doc.text, "Hello PSD"); + assert_eq!(doc.size, 14.0); + assert_eq!(doc.font, "Inter"); + assert_eq!(doc.style, "Bold"); + assert_eq!(title.props.prop("transform/position").unwrap().value.as_vec2(), [8.0, 70.0]); + // Adjustment layer with Hue/Saturation. + let hue = &c.layers[1]; + assert!(hue.switches.adjustment); + let fx = hue.effects().unwrap().groups().next().unwrap(); + assert_eq!(fx.get("hue").unwrap().value.as_f64(), 40.0); + assert_eq!(fx.get("saturation").unwrap().value.as_f64(), 10.0); + // Vector mask → mask; Stroke style. + let vm = &c.layers[2]; + let m = vm.masks().unwrap().groups().next().unwrap(); + assert!(matches!(m.kind, effectcraft_project::GroupKind::Mask { mode: MaskMode::Add, .. })); + let Some(KV::Path(p)) = m.get("path").map(|p| p.value.clone()) else { panic!("mask path") }; + assert_eq!(p.vertices.len(), 3); + assert!((p.vertices[1][0] - 110.0).abs() < 1e-3 && (p.vertices[2][1] - 40.0).abs() < 1e-3); + let st = vm.props.group("layerStyles/stroke").unwrap(); + assert_eq!(st.get("size").unwrap().value.as_f64(), 2.0); + assert_eq!(st.get("position").unwrap().value, KV::Enum(1)); + // Clipped → preserve transparency; opacity and blend mode. + let clipped = &c.layers[3]; + assert!(clipped.preserve_transparency); + assert_eq!(clipped.blend_mode, BlendMode::Screen); + assert!((clipped.props.prop("transform/opacity").unwrap().value.as_f64() - 128.0 / 255.0 * 100.0).abs() < 1e-9); + // Drop shadow. + let ds = c.layers[4].props.group("layerStyles/dropShadow").unwrap(); + assert_eq!(ds.get("distance").unwrap().value.as_f64(), 9.0); + assert_eq!(ds.get("angle").unwrap().value.as_f64(), 45.0); + assert_eq!(ds.get("useGlobalLight").unwrap().value, KV::Bool(false)); + assert_eq!(ds.get("opacity").unwrap().value.as_f64(), 60.0); + // Group → collapsed precomp (pass through) with its layers. + let g = &c.layers[5]; + assert!(g.switches.collapse); + let LayerSource::Comp { item } = g.source else { panic!("precomp") }; + let inner = s.project.comp(item).unwrap(); + let inner_names: Vec<&str> = inner.layers.iter().map(|l| l.name.as_str()).collect(); + assert_eq!(inner_names, ["Hidden", "Masked Square"]); + assert!(!inner.layers[0].switches.video); + assert_eq!(inner.layers[1].blend_mode, BlendMode::Multiply); + // Footage items name their Photoshop layer; document-sized footage. + let LayerSource::Footage { item } = inner.layers[1].source else { panic!("footage") }; + let ItemKind::Footage(f) = &s.project.item(item).unwrap().kind else { panic!() }; + let sl = f.layer.as_ref().unwrap(); + assert_eq!((sl.name.as_str(), sl.layer_size), ("Masked Square", false)); + assert_eq!((f.width, f.height), (120, 80)); + assert_eq!(s.project.item(item).unwrap().name, "Masked Square/sample.psd"); + // One undo step removes everything. + s.execute("edit.undo", json!({})).unwrap(); + assert!(s.project.comp(cid).is_none()); +} + +#[test] +fn psd_retain_layer_sizes() { + let path = tmp("retain.psd"); + std::fs::write(&path, sample_psd()).unwrap(); + let mut s = Session::default(); + let r = s.execute_checked("file.import", json!({"paths": [path], "importAs": "compositionLayerSizes"})).unwrap(); + let cid = effectcraft_project::ItemId(r["comps"][0].as_u64().unwrap()); + let c = s.project.comp(cid).unwrap(); + let base = c.layer_by_name("Base").unwrap(); + let LayerSource::Footage { item } = base.source else { panic!() }; + let ItemKind::Footage(f) = &s.project.item(item).unwrap().kind else { panic!() }; + assert_eq!((f.width, f.height), (30, 30)); + assert!(f.layer.as_ref().unwrap().layer_size); + assert_eq!(base.props.prop("transform/position").unwrap().value.as_vec2(), [70.0, 40.0]); + assert_eq!(base.props.prop("transform/anchor").unwrap().value.as_vec2(), [15.0, 15.0]); + // Vector mask vertices are in the layer's own space. + let vm = c.layer_by_name("Vector Masked").unwrap(); + let Some(KV::Path(p)) = vm.masks().unwrap().groups().next().unwrap().get("path").map(|p| p.value.clone()) else { panic!() }; + assert!((p.vertices[0][0] - 10.0).abs() < 1e-3, "{:?}", p.vertices); +} + +#[test] +fn import_rejects_unknown_kind() { + let mut s = Session::default(); + assert!(s.execute_checked("file.import", json!({"paths": ["x.psd"], "importAs": "banana"})).is_err()); +} + +fn text_session() -> (Session, u64) { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "T", "width": 320, "height": 120, "frameRate": 30, "duration": 2})).unwrap(); + let r = s.execute_checked("layer.newText", json!({"text": "Ab8", "size": 64, "position": [160, 80], "fill": "#ffcc00"})).unwrap(); + (s, r["layer"].as_u64().unwrap()) +} + +fn alpha_diff(a: &effectcraft_raster::Image, b: &effectcraft_raster::Image) -> (f32, f32) { + let mut sum = 0.0; + let mut mass = 0.0; + for (p, q) in a.data.iter().zip(&b.data) { + sum += (p[3] - q[3]).abs(); + mass += p[3]; + } + (sum / a.data.len() as f32, mass) +} + +#[test] +fn create_shapes_from_text_matches_text_render() { + let (mut s, lid) = text_session(); + let cid = s.active_comp_id().unwrap(); + let opts = RenderOpts::default(); + let text_img = s.render(cid, s.time(), opts); + s.execute("layer.select", json!({"layers": [lid]})).unwrap(); + let r = s.execute_checked("layer.create", json!({"op": "shapesFromText"})).unwrap(); + let sid = r["layers"][0].as_u64().unwrap(); + let c = s.active_comp().unwrap(); + assert_eq!(c.layers[0].id.0, sid); + assert!(matches!(c.layers[0].source, LayerSource::Shape)); + assert_eq!(c.layers[0].name, "Ab8 Outlines"); + assert!(!c.layer(effectcraft_project::LayerId(lid)).unwrap().switches.video, "source hidden"); + let groups: Vec = c.layers[0].props.sub("contents").unwrap().groups().map(|g| g.name.clone()).collect(); + assert_eq!(groups, ["A", "b", "8"]); + let shape_img = s.render(cid, s.time(), opts); + let (d, mass) = alpha_diff(&text_img, &shape_img); + assert!(mass > 500.0, "text drew something"); + assert!(d < 0.01, "mean alpha difference {d}"); + // Fill colour carried over. + let i = shape_img.data.iter().position(|p| p[3] > 0.99).unwrap(); + let p = shape_img.data[i]; + assert!((p[0] - 1.0).abs() < 0.02 && (p[1] - 0.8).abs() < 0.02 && p[2] < 0.02, "{p:?}"); +} + +#[test] +fn create_masks_from_text() { + let (mut s, lid) = text_session(); + let cid = s.active_comp_id().unwrap(); + let text_img = s.render(cid, s.time(), RenderOpts::default()); + s.execute("layer.select", json!({"layers": [lid]})).unwrap(); + let r = s.execute_checked("layer.create", json!({"op": "masksFromText"})).unwrap(); + let mid = effectcraft_project::LayerId(r["layers"][0].as_u64().unwrap()); + let c = s.active_comp().unwrap(); + let l = c.layer(mid).unwrap(); + assert!(matches!(l.source, LayerSource::Solid { .. })); + // "A", "b" and "8" have 2, 2 and 3 contours. + assert_eq!(l.masks().unwrap().groups().count(), 7); + let img = s.render(cid, s.time(), RenderOpts::default()); + let (d, _) = alpha_diff(&text_img, &img); + assert!(d < 0.01, "mean alpha difference {d}"); + // Other ops need the right layer type. + assert!(s.execute_checked("layer.create", json!({"op": "shapesFromVector", "layers": [lid]})).is_err()); + assert!(s.execute_checked("layer.create", json!({"op": "nullTracing", "layers": [lid]})).is_err()); +} diff --git a/crates/engine/src/vector.rs b/crates/engine/src/vector.rs new file mode 100644 index 00000000..a3f3e040 --- /dev/null +++ b/crates/engine/src/vector.rs @@ -0,0 +1,250 @@ +//! Vector conversions behind Layer ▸ Create: Create Shapes from Vector Layer (SVG footage → +//! shape layer), Create Shapes from Text and Create Masks from Text (glyph outlines). + +use effectcraft_keyframe::{Gradient, ShapePath, Value}; +use effectcraft_project::build::{self, Ids}; +use effectcraft_project::{Comp, ItemId, Layer, LayerSource, MaskMode, Project, PropGroup}; +use effectcraft_svg::{Affine, Doc, Geom, GradientKind, Node, Paint}; +use kurbo::{BezPath, Point}; + +fn shape_paths(ids: &mut Ids, path: &BezPath) -> Vec { + effectcraft_path::from_kurbo(path) + .into_iter() + .enumerate() + .map(|(i, sp)| { + let mut g = build::shape_path(ids, sp); + g.name = format!("Path {}", i + 1); + g + }) + .collect() +} + +fn det_scale(m: Affine) -> f64 { + let c = m.as_coeffs(); + (c[0] * c[3] - c[1] * c[2]).abs().sqrt() +} + +/// Axis-aligned scale + translation only (basic shapes stay parametric). +fn axis_aligned(m: Affine) -> Option<(f64, f64, f64, f64)> { + let c = m.as_coeffs(); + (c[1].abs() < 1e-9 && c[2].abs() < 1e-9 && c[0] > 0.0 && c[3] > 0.0).then_some((c[0], c[3], c[4], c[5])) +} + +fn gradient_value(g: &effectcraft_svg::Gradient) -> Gradient { + Gradient { + colors: g.stops.iter().map(|(o, c)| (*o, [c[0] as f32, c[1] as f32, c[2] as f32, 1.0])).collect(), + opacities: g.stops.iter().map(|(o, c)| (*o, c[3] as f32)).collect(), + } +} + +/// Gradient start/end points in shape-layer space (`m`: shape user space → layer space). +fn gradient_points(g: &effectcraft_svg::Gradient, m: Affine, bbox: kurbo::Rect) -> (bool, [f64; 2], [f64; 2]) { + let unit = if g.bbox_units { Affine::new([bbox.width(), 0.0, 0.0, bbox.height(), bbox.x0, bbox.y0]) } else { Affine::IDENTITY }; + let full = m * unit * g.transform; + let p = |x: f64, y: f64| { + let q = full * Point::new(x, y); + [q.x, q.y] + }; + match g.kind { + GradientKind::Linear { x1, y1, x2, y2 } => (false, p(x1, y1), p(x2, y2)), + GradientKind::Radial { cx, cy, r, .. } => (true, p(cx, cy), p(cx + r, cy)), + } +} + +fn shape_items(ids: &mut Ids, s: &effectcraft_svg::Shape, m: Affine) -> Vec { + use kurbo::Shape as _; + let mut items = vec![]; + // Geometry: parametric when the transform keeps rectangles/ellipses axis-aligned. + match (&s.geom, axis_aligned(m)) { + (Geom::Rect { x, y, w, h, rx, ry }, Some((sx, sy, tx, ty))) if (rx - ry).abs() < 1e-9 && (*rx == 0.0 || (sx - sy).abs() < 1e-9) => { + let (w2, h2) = (w * sx, h * sy); + let pos = [tx + (x + w / 2.0) * sx, ty + (y + h / 2.0) * sy]; + let mut g = build::shape_rect(ids, [w2, h2], pos, rx * sx); + g.name = "Rectangle Path 1".into(); + items.push(g); + } + (Geom::Ellipse { cx, cy, rx, ry }, Some((sx, sy, tx, ty))) => { + items.push(build::shape_ellipse(ids, [2.0 * rx * sx, 2.0 * ry * sy], [tx + cx * sx, ty + cy * sy])); + } + (g, _) => items.extend(shape_paths(ids, &(m * g.to_path()))), + } + let bbox = s.geom.to_path().bounding_box(); + if let Some(st) = &s.stroke { + let width = st.width * det_scale(m); + let mut g = match &st.paint { + Paint::Color(c) => build::shape_stroke(ids, [c[0], c[1], c[2], 1.0], width), + Paint::Gradient(gr) => { + let (radial, a, b) = gradient_points(gr, m, bbox); + build::shape_gradient_stroke(ids, radial, a, b, gradient_value(gr), width) + } + }; + set(&mut g, "opacity", Value::Scalar(st.opacity * 100.0)); + set(&mut g, "cap", Value::Enum(st.cap as u32)); + set(&mut g, "join", Value::Enum(st.join as u32)); + set(&mut g, "miter", Value::Scalar(st.miter)); + if let Some((d, off)) = &st.dash + && let Some(dg) = g.sub_mut("dashes") + { + let k = det_scale(m); + set(dg, "dash", Value::Scalar(d.first().copied().unwrap_or(0.0) * k)); + set(dg, "gap", Value::Scalar(d.get(1).or(d.first()).copied().unwrap_or(0.0) * k)); + set(dg, "offset", Value::Scalar(off * k)); + } + items.push(g); + } + if let Some(f) = &s.fill { + let mut g = match f { + Paint::Color(c) => build::shape_fill(ids, [c[0], c[1], c[2], 1.0]), + Paint::Gradient(gr) => { + let (radial, a, b) = gradient_points(gr, m, bbox); + build::shape_gradient_fill(ids, radial, a, b, gradient_value(gr)) + } + }; + set(&mut g, "opacity", Value::Scalar(s.fill_opacity * 100.0)); + set(&mut g, "rule", Value::Enum(u32::from(s.fill_rule == effectcraft_svg::FillRule::EvenOdd))); + items.push(g); + } + items +} + +fn set(g: &mut PropGroup, k: &str, v: Value) { + if let Some(p) = g.get_mut(k) { + p.value = v; + } +} + +fn group(ids: &mut Ids, name: &str, items: Vec, opacity: f64) -> PropGroup { + let mut g = build::shape_group(ids, name, items); + if let Some(p) = g.prop_mut("transform/opacity") { + p.value = Value::Scalar(opacity * 100.0); + } + g +} + +/// Shape layer contents for an SVG document, in document pixels (transforms are baked into the +/// geometry; group and element opacity go to the group transforms). Top of the paint order first. +pub fn svg_contents(ids: &mut Ids, doc: &Doc) -> Vec { + fn walk(ids: &mut Ids, nodes: &[Node], m: Affine) -> Vec { + let mut out = vec![]; + for n in nodes.iter().rev() { + match n { + Node::Group(g) => { + let items = walk(ids, &g.children, m * g.transform); + if !items.is_empty() { + out.push(group(ids, &g.name, items, g.opacity)); + } + } + Node::Shape(s) => { + let items = shape_items(ids, s, m * s.transform); + if !items.is_empty() { + out.push(group(ids, &s.name, items, s.opacity)); + } + } + } + } + out + } + let root = &doc.root; + let items = walk(ids, &root.children, root.transform); + if (root.opacity - 1.0).abs() > 1e-9 { vec![group(ids, "svg", items, root.opacity)] } else { items } +} + +/// A copy of `src`'s transform group with fresh uids. +fn copy_transform(proj: &mut Project, src: &Layer, dst: &mut Layer) { + let Some(tr) = src.props.sub("transform") else { return }; + let mut tr = tr.clone(); + let mut n = proj.next_id; + tr.reassign_uids(&mut n); + proj.next_id = n + 1; + if let Some(slot) = dst.props.children.iter_mut().find(|c| c.match_id() == "transform") { + *slot = tr.into(); + } +} + +fn like_source(dst: &mut Layer, src: &Layer) { + dst.start_time = src.start_time; + dst.in_point = src.in_point; + dst.out_point = src.out_point; + dst.stretch = src.stretch; + dst.parent = src.parent; + dst.switches.three_d = src.switches.three_d; +} + +/// Layer ▸ Create ▸ Create Shapes from Vector Layer: `doc` is the SVG shown by `src`. +pub fn shapes_from_vector(proj: &mut Project, comp: &Comp, src: &Layer, doc: &Doc) -> Layer { + let mut l = build::layer(proj, comp, &format!("{} Outlines", src.name), LayerSource::Shape, (0, 0), None); + let items = svg_contents(&mut Ids(&mut proj.next_id), doc); + if let Some(c) = l.props.sub_mut("contents") { + c.children.extend(items.into_iter().map(Into::into)); + } + copy_transform(proj, src, &mut l); + like_source(&mut l, src); + l.name = comp.unique_layer_name(&format!("{} Outlines", src.name)); + l +} + +/// Layer ▸ Create ▸ Create Shapes from Text: one group per character with its outline, fill and +/// stroke (as laid out at the comp time `ctx` was made for). +pub fn shapes_from_text(proj: &mut Project, comp: &Comp, ctx_project: &Project, cid: ItemId, src: &Layer, time: effectcraft_time::Tick) -> Option { + let ctx = effectcraft_render::EvalCtx::new(ctx_project, cid, comp, time); + let geom = effectcraft_render::text::text_geom(&ctx, src)?; + let chars: Vec = geom.doc.text.chars().filter(|c| !c.is_whitespace()).collect(); + let mut l = build::layer(proj, comp, &format!("{} Outlines", src.name), LayerSource::Shape, (0, 0), None); + let mut groups = vec![]; + { + let mut ids = Ids(&mut proj.next_id); + for (i, g) in geom.glyphs.iter().enumerate() { + let path = g.path(); + if path.elements().is_empty() { + continue; + } + let mut items = shape_paths(&mut ids, &path); + if g.stroke_width > 0.0 && g.stroke[3] > 0.0 { + let c = g.stroke; + items.push(build::shape_stroke(&mut ids, [c[0] as f64, c[1] as f64, c[2] as f64, 1.0], g.stroke_width)); + } + if g.apply_fill { + let c = g.fill; + let mut f = build::shape_fill(&mut ids, [c[0] as f64, c[1] as f64, c[2] as f64, 1.0]); + set(&mut f, "opacity", Value::Scalar(c[3] as f64 * 100.0)); + items.push(f); + } + let name = chars.get(i).map(|c| c.to_string()).unwrap_or_else(|| format!("Glyph {}", i + 1)); + groups.push(group(&mut ids, &name, items, 1.0)); + } + } + // First character on top, as in After Effects. + if let Some(c) = l.props.sub_mut("contents") { + c.children.extend(groups.into_iter().map(Into::into)); + } + copy_transform(proj, src, &mut l); + like_source(&mut l, src); + l.name = comp.unique_layer_name(&format!("{} Outlines", src.name)); + Some(l) +} + +/// Glyph outlines of a text layer in comp space (for Create Masks from Text). +pub fn text_outlines_in_comp(project: &Project, cid: ItemId, comp: &Comp, src: &Layer, time: effectcraft_time::Tick) -> Vec { + let ctx = effectcraft_render::EvalCtx::new(project, cid, comp, time); + let Some(geom) = effectcraft_render::text::text_geom(&ctx, src) else { return vec![] }; + let (m, _) = ctx.layer_to_comp(src); + let mut out = vec![]; + for g in &geom.glyphs { + let p = g.path(); + let p = effectcraft_path::transform(std::slice::from_ref(&p), &m).remove(0); + out.extend(effectcraft_path::from_kurbo(&p)); + } + out +} + +/// A comp-sized white solid with one mask per glyph contour (Difference, so counters stay open). +pub fn masks_layer(proj: &mut Project, comp: &Comp, name: &str, outlines: Vec, solid: ItemId) -> Layer { + let mut l = build::layer(proj, comp, name, LayerSource::Solid { item: solid }, (comp.width, comp.height), None); + let mut ids = Ids(&mut proj.next_id); + if let Some(masks) = l.props.sub_mut("masks") { + for (i, sp) in outlines.into_iter().enumerate() { + masks.children.push(build::mask(&mut ids, &format!("Mask {}", i + 1), sp, MaskMode::Difference, [255, 255, 0]).into()); + } + } + l +} diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index ca2fb1b0..cf63e593 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -100,6 +100,7 @@ impl Scene { missing: false, sequence: vec![], color_profile: None, + layer: None, }; let fid = self.p.add_item("Pattern", Label::Lavender, None, ItemKind::Footage(f)); build::layer(&mut self.p, &self.comp, "Pattern", LayerSource::Footage { item: fid }, (w, h), None) diff --git a/crates/host/Cargo.toml b/crates/host/Cargo.toml index b43271c7..7143510a 100644 --- a/crates/host/Cargo.toml +++ b/crates/host/Cargo.toml @@ -18,3 +18,7 @@ effectcraft-export = { workspace = true } [dev-dependencies] serde_json = { workspace = true } +effectcraft-psd = { workspace = true } +effectcraft-svg = { workspace = true } +effectcraft-raster = { workspace = true } +effectcraft-time = { workspace = true } diff --git a/crates/host/tests/vector_import.rs b/crates/host/tests/vector_import.rs new file mode 100644 index 00000000..eb5be076 --- /dev/null +++ b/crates/host/tests/vector_import.rs @@ -0,0 +1,163 @@ +//! Photoshop and SVG import through the fully wired session (media decoding included): pixels +//! of imported compositions, SVG footage at any scale, and Create Shapes from Vector Layer. + +use effectcraft_engine::render::RenderOpts; +use effectcraft_project::ItemId; +use effectcraft_psd::Rect; +use effectcraft_psd::write::*; +use effectcraft_raster::Image; +use serde_json::json; + +fn tmp(name: &str) -> String { + let d = std::env::temp_dir().join(format!("effectcraft-host-vector-{}", std::process::id())); + std::fs::create_dir_all(&d).unwrap(); + d.join(name).to_string_lossy().to_string() +} + +fn mean_diff(a: &Image, b: &Image) -> f32 { + assert_eq!((a.width, a.height), (b.width, b.height)); + let mut s = 0.0; + for (p, q) in a.data.iter().zip(&b.data) { + for c in 0..4 { + s += (p[c] - q[c]).abs(); + } + } + s / (a.data.len() * 4) as f32 +} + +fn psd_doc() -> WDoc { + let mut d = WDoc::new(64, 48); + let mut masked = WLayer::solid("Masked", Rect::new(30, 8, 24, 24), [0.1, 0.9, 0.3, 1.0]); + masked.mask = Some(WMask { + rect: Rect::new(30, 8, 24, 24), + data: (0..576).map(|i| if (i / 24) < 12 { 1.0 } else { 0.25 }).collect(), + default_color: 0, + disabled: false, + }); + d.layers = vec![ + WLayer::solid("Background", Rect::new(0, 0, 64, 48), [0.2, 0.25, 0.3, 1.0]), + WLayer::solid("Half Red", Rect::new(4, 4, 30, 20), [1.0, 0.0, 0.0, 1.0]).with_opacity(128), + masked, + WLayer::group_end(), + WLayer::solid("In Group", Rect::new(10, 30, 20, 12), [0.9, 0.9, 0.1, 1.0]), + WLayer::group("Group", true, *b"norm"), + ]; + d +} + +/// Straight-alpha over of the layers as Photoshop composites them (normal blending), as the +/// merged image to compare the imported composition with. +fn reference(bytes: &[u8]) -> Vec<[f32; 4]> { + let p = effectcraft_psd::Psd::parse(bytes.to_vec()).unwrap(); + let mut acc = vec![[0.0f32; 4]; (p.width * p.height) as usize]; + for l in p.layers.iter().filter(|l| l.has_pixels() && !l.hidden) { + let px = p.layer_pixels(l.index, true).unwrap(); + let o = l.opacity as f32 / 255.0; + for (d, s) in acc.iter_mut().zip(&px.data) { + let a = s[3] * o; + let oa = a + d[3] * (1.0 - a); + for c in 0..3 { + d[c] = if oa > 0.0 { (s[c] * a + d[c] * d[3] * (1.0 - a)) / oa } else { 0.0 }; + } + d[3] = oa; + } + } + acc +} + +#[test] +fn psd_composition_renders_like_the_document() { + let mut doc = psd_doc(); + let bytes = write(&doc); + let merged = reference(&bytes); + doc.composite = Some(merged.clone()); + let bytes = write(&doc); + let path = tmp("layers.psd"); + std::fs::write(&path, &bytes).unwrap(); + let mut s = effectcraft_host::session(); + // Footage (merged image). + let r = s.execute_checked("file.import", json!({"paths": [path]})).unwrap(); + let merged_item = r["items"][0].as_u64().unwrap(); + // Composition and Composition – Retain Layer Sizes. + let mut renders = vec![]; + for kind in ["composition", "compositionLayerSizes"] { + let r = s.execute_checked("file.import", json!({"paths": [path], "importAs": kind})).unwrap(); + let cid = ItemId(r["comps"][0].as_u64().unwrap()); + renders.push(s.render(cid, effectcraft_time::Tick::ZERO, RenderOpts::default())); + } + // The merged footage in a comp of the same size. + s.execute("comp.new", json!({"name": "Merged", "width": 64, "height": 48, "frameRate": 30, "duration": 1})).unwrap(); + s.execute("layer.addItem", json!({"item": merged_item})).unwrap(); + let mcid = s.active_comp_id().unwrap(); + let merged_img = s.render(mcid, effectcraft_time::Tick::ZERO, RenderOpts::default()); + let expected = Image { width: 64, height: 48, data: merged.iter().map(|p| [p[0] * p[3], p[1] * p[3], p[2] * p[3], p[3]]).collect() }; + assert!(mean_diff(&merged_img, &expected) < 2.0 / 255.0, "merged footage"); + for (i, img) in renders.iter().enumerate() { + let d = mean_diff(img, &expected); + assert!(d < 2.0 / 255.0, "import kind {i}: mean diff {d}"); + } + // A single layer as footage (Choose Layer). + let r = s.execute_checked("file.import", json!({"paths": [path], "layer": "Masked"})).unwrap(); + let it = s.project.item(ItemId(r["items"][0].as_u64().unwrap())).unwrap(); + assert_eq!(it.name, "Masked/layers.psd"); +} + +#[test] +fn psd_16bit_cmyk_composition_imports() { + let mut doc = psd_doc(); + doc.depth = 16; + doc.mode = effectcraft_psd::ColorMode::Cmyk; + doc.rle = false; + let bytes = write(&doc); + let merged = reference(&bytes); + let path = tmp("layers16.psd"); + std::fs::write(&path, &bytes).unwrap(); + let mut s = effectcraft_host::session(); + let r = s.execute_checked("file.import", json!({"paths": [path], "importAs": "composition"})).unwrap(); + let cid = ItemId(r["comps"][0].as_u64().unwrap()); + let img = s.render(cid, effectcraft_time::Tick::ZERO, RenderOpts::default()); + let expected = Image { width: 64, height: 48, data: merged.iter().map(|p| [p[0] * p[3], p[1] * p[3], p[2] * p[3], p[3]]).collect() }; + let d = mean_diff(&img, &expected); + assert!(d < 2.0 / 255.0, "mean diff {d}"); +} + +const SVG: &[u8] = include_bytes!("../../svg/tests/fixtures/basic-shapes.svg"); +const SVG2: &[u8] = include_bytes!("../../svg/tests/fixtures/paths-gradients.svg"); + +#[test] +fn svg_footage_and_shapes_from_vector_layer() { + for (name, bytes) in [("basic-shapes.svg", SVG), ("paths-gradients.svg", SVG2)] { + let path = tmp(name); + std::fs::write(&path, bytes).unwrap(); + let mut s = effectcraft_host::session(); + let r = s.execute_checked("file.import", json!({"paths": [path]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let item = r["items"][0].as_u64().unwrap(); + let doc = effectcraft_svg::parse(bytes).unwrap(); + let (w, h) = doc.pixel_size(); + s.execute("comp.new", json!({"name": "V", "width": w, "height": h, "frameRate": 30, "duration": 1})).unwrap(); + let lid = s.execute_checked("layer.addItem", json!({"item": item})).unwrap()["layer"].as_u64().unwrap(); + let cid = s.active_comp_id().unwrap(); + let t = effectcraft_time::Tick::ZERO; + let foot = s.render(cid, t, RenderOpts::default()); + let direct = effectcraft_svg::rasterize(&doc, w, h, 1.0); + assert!(mean_diff(&foot, &direct) < 1.0 / 255.0, "{name}: footage = rasterised SVG"); + s.execute("layer.select", json!({"layers": [lid]})).unwrap(); + let r = s.execute_checked("layer.create", json!({"op": "shapesFromVector"})).unwrap(); + let sid = r["layers"][0].as_u64().unwrap(); + let c = s.active_comp().unwrap(); + assert_eq!(c.layers[0].id.0, sid); + assert!(!c.layers[1].switches.video); + let shapes = s.render(cid, t, RenderOpts::default()); + let d = mean_diff(&shapes, &direct); + assert!(d < 0.02, "{name}: shapes vs SVG mean diff {d}"); + // Scaled up 3×, the shape layer stays sharp and matches the SVG rasterised at 3×. + s.execute("prop.set", json!({"layer": sid, "path": "transform/scale", "value": [300, 300]})).unwrap(); + s.execute("prop.set", json!({"layer": sid, "path": "transform/position", "value": [0, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": sid, "path": "transform/anchor", "value": [0, 0]})).unwrap(); + let big = s.render(cid, t, RenderOpts::default()); + let direct3 = effectcraft_svg::rasterize(&doc, w, h, 3.0); + let d3 = mean_diff(&big, &direct3); + assert!(d3 < 0.03, "{name}: ×3 mean diff {d3}"); + } +} diff --git a/crates/lottie/src/import.rs b/crates/lottie/src/import.rs index c87dc187..22bc5023 100644 --- a/crates/lottie/src/import.rs +++ b/crates/lottie/src/import.rs @@ -171,6 +171,7 @@ pub fn import( missing: false, sequence: vec![], color_profile: None, + layer: None, }; let item = project.add_item(&nm, Label::Lavender, Some(folder), ItemKind::Footage(foot)); cx.items.push(item); diff --git a/crates/lottie/src/tests.rs b/crates/lottie/src/tests.rs index 5140133c..01c724d7 100644 --- a/crates/lottie/src/tests.rs +++ b/crates/lottie/src/tests.rs @@ -592,6 +592,7 @@ fn images_embed_as_base64_and_come_back() { missing: false, sequence: vec![], color_profile: None, + layer: None, }), ); let c = comp(&p, cid); diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml index 89992569..6e83c949 100644 --- a/crates/media/Cargo.toml +++ b/crates/media/Cargo.toml @@ -14,6 +14,8 @@ effectcraft-time = { workspace = true } effectcraft-project = { workspace = true } effectcraft-raster = { workspace = true } effectcraft-render = { workspace = true } +effectcraft-psd = { workspace = true } +effectcraft-svg = { workspace = true } filmcraft-codecs = { workspace = true } filmcraft-media = { workspace = true } filmcraft-frame = { workspace = true } diff --git a/crates/media/src/convert.rs b/crates/media/src/convert.rs index fa5df238..dce33d8a 100644 --- a/crates/media/src/convert.rs +++ b/crates/media/src/convert.rs @@ -284,6 +284,22 @@ pub(crate) fn dynamic_to_image(img: &image::DynamicImage, op: AlphaOp) -> Image out } +/// Straight RGBA `f32` pixels (layered stills: Photoshop, SVG) to an [`Image`]. +pub(crate) fn straight_to_image(w: u32, h: u32, px: &[[f32; 4]], op: AlphaOp) -> Image { + let mut out = Image::new(w, h); + let wu = w as usize; + if wu == 0 || h == 0 { + return out; + } + out.data.par_chunks_mut(wu).enumerate().for_each(|(y, row)| { + for (x, o) in row.iter_mut().enumerate() { + let s = px[y * wu + x]; + *o = op.apply([s[0], s[1], s[2]], s[3].clamp(0.0, 1.0)); + } + }); + out +} + #[cfg(test)] mod tests { use super::*; @@ -376,3 +392,4 @@ mod tests { } } } + diff --git a/crates/media/src/layered.rs b/crates/media/src/layered.rs new file mode 100644 index 00000000..204312a5 --- /dev/null +++ b/crates/media/src/layered.rs @@ -0,0 +1,62 @@ +//! Layered and vector stills: Photoshop documents (`effectcraft-psd`: the merged image, or one +//! layer for footage imported as a composition) and SVG (`effectcraft-svg`, rasterised). + +use effectcraft_project::{AlphaMode, Footage}; +use effectcraft_raster::Image; + +use crate::convert::{AlphaOp, straight_to_image}; +use crate::{MediaError, Result}; + +fn is_svg(path: &str, bytes: &[u8]) -> bool { + path.to_ascii_lowercase().ends_with(".svg") || effectcraft_svg::looks_like_svg(bytes) +} + +/// Footage for a Photoshop or SVG file (`None` for other formats). +pub(crate) fn probe(path: &str, bytes: &[u8]) -> Result> { + if effectcraft_psd::is_psd(bytes) { + let psd = effectcraft_psd::Psd::parse(bytes.to_vec()).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; + let mut f = crate::probe::still_footage(path, psd.width, psd.height, image::ImageFormat::Png, true); + f.codec = if psd.psb { "PSB".into() } else { "PSD".into() }; + f.alpha = if psd.merged_alpha || !psd.layers.is_empty() { AlphaMode::Straight } else { AlphaMode::Ignore }; + return Ok(Some(f)); + } + if is_svg(path, bytes) { + let doc = effectcraft_svg::parse(bytes).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; + let (w, h) = doc.pixel_size(); + let mut f = crate::probe::still_footage(path, w, h, image::ImageFormat::Png, true); + f.codec = "SVG".into(); + f.alpha = AlphaMode::Straight; + return Ok(Some(f)); + } + Ok(None) +} + +/// Decode a Photoshop or SVG still (`None` for other formats). +pub(crate) fn decode(path: &str, bytes: &[u8], footage: &Footage, op: AlphaOp) -> Result> { + if effectcraft_psd::is_psd(bytes) { + let psd = effectcraft_psd::Psd::parse(bytes.to_vec()).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; + let px = match &footage.layer { + Some(l) => psd.layer_pixels(l.index as usize, !l.layer_size), + None => psd.composite(), + } + .map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; + return Ok(Some(straight_to_image(px.width, px.height, &px.data, op))); + } + if is_svg(path, bytes) { + let doc = effectcraft_svg::parse(bytes).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; + let (w, h) = doc.pixel_size(); + let img = effectcraft_svg::rasterize(&doc, w, h, 1.0); + // The rasteriser produces premultiplied pixels already. + let _ = op; + return Ok(Some(img)); + } + Ok(None) +} + +/// An SVG rasterised at `scale` × its pixel size (Continuously Rasterize). +pub(crate) fn rasterize_svg(bytes: &[u8], scale: f64) -> Option { + let doc = effectcraft_svg::parse(bytes).ok()?; + let (w, h) = doc.pixel_size(); + let (sw, sh) = (((w as f64 * scale).ceil() as u32).clamp(1, 16384), ((h as f64 * scale).ceil() as u32).clamp(1, 16384)); + Some(effectcraft_svg::rasterize(&doc, sw, sh, scale)) +} diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index 29546f7e..246054b3 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -10,9 +10,11 @@ //! Video containers (MP4/MOV, Matroska/WebM) and codecs (H.264, HEVC, VP9, AV1, ProRes, DNxHD, //! MJPEG; AAC, Opus, PCM, MP3/FLAC/Vorbis) come from FilmCraft's pure-Rust crates (git dependency, //! pinned; `plan/adr/0001`). FilmCraft types never leave this crate. Stills use the `image` crate -//! (PNG, JPEG, GIF, WebP, TIFF, BMP, OpenEXR). +//! (PNG, JPEG, GIF, WebP, TIFF, BMP, OpenEXR); Photoshop documents (merged image or one layer) come +//! from `effectcraft-psd` and SVG from `effectcraft-svg` (rasterised at any scale). mod convert; +mod layered; mod pool; mod probe; @@ -44,7 +46,7 @@ impl From for MediaError { } /// File extensions recognised as stills. -pub const STILL_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp", "tif", "tiff", "bmp", "exr"]; +pub const STILL_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp", "tif", "tiff", "bmp", "exr", "psd", "psb", "svg"]; /// File extensions recognised as audio-only files. pub const AUDIO_EXTENSIONS: &[&str] = filmcraft_media::AUDIO_EXTENSIONS; /// File extensions recognised as movies. diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index b4cc51ef..b5d94299 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -359,6 +359,11 @@ impl Inner { let i = Self::frame_index(footage.frame_rate, t, footage.sequence.len() as i64, footage.loop_count); Loc { key: key(&footage.sequence[i as usize], 0), media_t: None } } + // A layer of a layered still is keyed by its layer index and size mode. + FootageKind::Still if footage.layer.is_some() => { + let l = footage.layer.as_ref().map_or(0, |l| 1 + l.index as i64 * 2 + l.layer_size as i64); + Loc { key: key(&footage.path, l), media_t: None } + } FootageKind::Still | FootageKind::Sequence => Loc { key: key(&footage.path, 0), media_t: None }, FootageKind::Video | FootageKind::Audio => { let rate = footage.frame_rate; @@ -437,6 +442,9 @@ impl Inner { match loc.media_t { None => { let bytes = self.read(path)?; + if let Some(img) = crate::layered::decode(path, &bytes, footage, op)? { + return Ok(img); + } let img = image::load_from_memory(&bytes).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; Ok(dynamic_to_image(&img, op)) } @@ -470,6 +478,14 @@ impl FootageSource for MediaPool { } } + fn vector_frame(&self, _item: ItemId, footage: &Footage, scale: f64) -> Option> { + if footage.missing || !effectcraft_render::is_vector_footage(footage) { + return None; + } + let bytes = self.inner.read(&footage.path).ok()?; + crate::layered::rasterize_svg(&bytes, scale).map(Arc::new) + } + fn audio(&self, _item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option> { if !footage.has_audio || footage.missing { return None; diff --git a/crates/media/src/probe.rs b/crates/media/src/probe.rs index 4e1c5611..23432cc6 100644 --- a/crates/media/src/probe.rs +++ b/crates/media/src/probe.rs @@ -33,6 +33,9 @@ pub fn probe_single(path: impl AsRef) -> Result { /// Probe in-memory file contents; `path` names the footage (and is stored as `Footage::path`). pub fn probe_bytes(path: &str, bytes: Arc<[u8]>) -> Result { + if let Some(f) = crate::layered::probe(path, &bytes)? { + return Ok(f); + } if let Ok(fmt) = image::guess_format(&bytes) { return probe_still_bytes(path, &bytes, fmt); } @@ -72,6 +75,7 @@ pub(crate) fn footage_from_info(path: &str, info: &filmcraft_media::MediaInfo) - missing: false, sequence: Vec::new(), color_profile: v.and_then(|v| profile_of(&v.color)), + layer: None, } } @@ -88,7 +92,7 @@ fn profile_of(c: &filmcraft_color::ColorInfo) -> Option Footage { +pub(crate) fn still_footage(path: &str, w: u32, h: u32, fmt: image::ImageFormat, has_alpha: bool) -> Footage { let codec = fmt.extensions_str().first().map_or("image", |s| s).to_ascii_uppercase(); // OpenEXR stores premultiplied colour by convention; other formats straight alpha. let alpha = match (has_alpha, fmt) { @@ -114,6 +118,7 @@ fn still_footage(path: &str, w: u32, h: u32, fmt: image::ImageFormat, has_alpha: missing: false, sequence: Vec::new(), color_profile: None, + layer: None, } } diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index 1ed35c09..b4854f54 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -579,6 +579,21 @@ pub struct Footage { /// Colour profile (from the file's metadata, or Interpret Footage). `None` = sRGB. #[serde(default, skip_serializing_if = "Option::is_none")] pub color_profile: Option, + /// One layer of a layered file (Photoshop) instead of its merged image. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub layer: Option, +} + +/// A layer of a layered still (a Photoshop document) used as footage +/// (File ▸ Import ▸ Composition / Composition – Retain Layer Sizes, or Choose Layer). +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct SourceLayer { + /// Layer record index in the file (bottom of the stack = 0). + pub index: u32, + pub name: String, + /// The footage is the layer's own bounds (Retain Layer Sizes) rather than the document size. + #[serde(default)] + pub layer_size: bool, } fn one() -> u32 { diff --git a/crates/psd/Cargo.toml b/crates/psd/Cargo.toml new file mode 100644 index 00000000..49dda6a3 --- /dev/null +++ b/crates/psd/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "effectcraft-psd" +description = "Photoshop PSD/PSB reader (layers, groups, masks, blend modes, text, layer effects, adjustment layers; 8/16/32-bit, RGB/CMYK/Gray/Lab/Indexed, raw/RLE/ZIP) and a minimal writer, from Adobe's published file format specification" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] +miniz_oxide = "0.8" diff --git a/crates/psd/README.md b/crates/psd/README.md new file mode 100644 index 00000000..e238b9b2 --- /dev/null +++ b/crates/psd/README.md @@ -0,0 +1,17 @@ +# effectcraft-psd + +Reads Photoshop documents (`.psd`, and `.psb` large documents) for File ▸ Import (as footage, as a +composition, or as a composition with layer sizes retained), and writes a minimal subset (used to +generate test fixtures). + +Implemented from Adobe's public **Adobe Photoshop File Formats Specification** (November 2019 +edition): file header, colour mode data, image resources, layer and mask information (layer +records, channel image data, layer mask data, additional layer information: `luni`, `lsct`/`lsdk`, +`lyid`, `iOpa`, `TySh`, `lfx2`, `SoCo`, `nvrt`, `brit`, `hue2`, `levl`, `expA`, `vmsk`/`vsms`, +`Lr16`/`Lr32`), the descriptor (action) structure, and the merged image data. Raw, RLE (PackBits) +and ZIP (with and without prediction, inflated with `miniz_oxide`) compression; 1/8/16/32-bit; +Bitmap, Grayscale, Indexed, RGB, CMYK (naive conversion) and Lab colour modes. + +Text engine data (`EngineData`) is read best-effort for the font, size, fill colour and +justification of the first style run. No Adobe code, sample files or assets were used; test +fixtures are generated in-test by the writer in this crate ([`write`](src/write.rs)). diff --git a/crates/psd/src/descriptor.rs b/crates/psd/src/descriptor.rs new file mode 100644 index 00000000..e121f823 --- /dev/null +++ b/crates/psd/src/descriptor.rs @@ -0,0 +1,430 @@ +//! The descriptor ("action descriptor") structure: typed key/value trees used by text layers, +//! layer effects, fill layers and newer adjustment layers (spec: "Descriptor structure"). + +use crate::read::Reader; +use crate::{Error, Result}; + +/// A descriptor: a class and a list of keyed items (in file order). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Descriptor { + pub class_name: String, + pub class_id: String, + pub items: Vec<(String, DValue)>, +} + +/// One descriptor value. +#[derive(Clone, Debug, PartialEq)] +pub enum DValue { + Descriptor(Descriptor), + List(Vec), + Double(f64), + /// Unit (`#Pxl`, `#Prc`, `#Ang`, `#Pnt`…) and value. + UnitFloat(String, f64), + UnitFloats(String, Vec), + Text(String), + /// Enumerated value: type and value ids. + Enum(String, String), + Integer(i32), + LargeInteger(i64), + Bool(bool), + Class(String, String), + Raw(Vec), + Reference(Vec), +} + +/// An item of a reference (`obj `). +#[derive(Clone, Debug, PartialEq)] +pub enum RefItem { + Property(String, String), + Class(String), + Enum(String, String, String), + Offset(String, i32), + Identifier(i32), + Index(i32), + Name(String, String), +} + +impl Descriptor { + pub fn get(&self, key: &str) -> Option<&DValue> { + self.items.iter().find(|(k, _)| k == key).map(|(_, v)| v) + } + pub fn obj(&self, key: &str) -> Option<&Descriptor> { + match self.get(key)? { + DValue::Descriptor(d) => Some(d), + _ => None, + } + } + /// A number: doubles, unit floats, integers. + pub fn num(&self, key: &str) -> Option { + match self.get(key)? { + DValue::Double(v) | DValue::UnitFloat(_, v) => Some(*v), + DValue::Integer(v) => Some(*v as f64), + DValue::LargeInteger(v) => Some(*v as f64), + _ => None, + } + } + pub fn bool(&self, key: &str) -> Option { + match self.get(key)? { + DValue::Bool(b) => Some(*b), + _ => None, + } + } + pub fn text(&self, key: &str) -> Option<&str> { + match self.get(key)? { + DValue::Text(s) => Some(s), + _ => None, + } + } + /// The value id of an enumerated item. + pub fn enum_value(&self, key: &str) -> Option<&str> { + match self.get(key)? { + DValue::Enum(_, v) => Some(v), + _ => None, + } + } + pub fn raw(&self, key: &str) -> Option<&[u8]> { + match self.get(key)? { + DValue::Raw(v) => Some(v), + _ => None, + } + } + /// An RGB colour object (`RGBC` with `Rd `/`Grn `/`Bl ` in 0..255) as 0..1 values; also + /// reads grayscale (`Gry `) colours. + pub fn color(&self, key: &str) -> Option<[f64; 3]> { + let c = self.obj(key)?; + if let (Some(r), Some(g), Some(b)) = (c.num("Rd "), c.num("Grn "), c.num("Bl ")) { + return Some([r / 255.0, g / 255.0, b / 255.0]); + } + if let (Some(r), Some(g), Some(b)) = (c.num("redFloat"), c.num("greenFloat"), c.num("blueFloat")) { + return Some([r, g, b]); + } + c.num("Gry ").map(|g| { + let v = 1.0 - g / 100.0; + [v, v, v] + }) + } +} + +/// A key or class id: length-prefixed string, or a 4-character code when the length is 0. +pub(crate) fn read_id(r: &mut Reader) -> Result { + let n = r.u32()? as usize; + let n = if n == 0 { 4 } else { n }; + let b = r.bytes(n)?; + Ok(String::from_utf8_lossy(b).into_owned()) +} + +pub(crate) fn read_unicode(r: &mut Reader) -> Result { + let n = r.u32()? as usize; + let mut v = Vec::with_capacity(n); + for _ in 0..n { + v.push(r.u16()?); + } + while v.last() == Some(&0) { + v.pop(); + } + Ok(String::from_utf16_lossy(&v)) +} + +/// Read a descriptor (after its version field). +pub fn read_descriptor(r: &mut Reader) -> Result { + let class_name = read_unicode(r)?; + let class_id = read_id(r)?; + let n = r.u32()? as usize; + if n > 100_000 { + return Err(Error::Invalid("descriptor item count".into())); + } + let mut items = Vec::with_capacity(n.min(64)); + for _ in 0..n { + let key = read_id(r)?; + let ty = r.tag()?; + let v = read_value(r, &ty, 0)?; + items.push((key, v)); + } + Ok(Descriptor { class_name, class_id, items }) +} + +fn read_value(r: &mut Reader, ty: &[u8; 4], depth: usize) -> Result { + if depth > 64 { + return Err(Error::Invalid("descriptor nesting".into())); + } + Ok(match ty { + b"Objc" | b"GlbO" => DValue::Descriptor(read_descriptor(r)?), + b"VlLs" => { + let n = r.u32()? as usize; + if n > 1_000_000 { + return Err(Error::Invalid("descriptor list length".into())); + } + let mut v = Vec::with_capacity(n.min(256)); + for _ in 0..n { + let t = r.tag()?; + v.push(read_value(r, &t, depth + 1)?); + } + DValue::List(v) + } + b"doub" => DValue::Double(r.f64()?), + b"UntF" => { + let unit = String::from_utf8_lossy(&r.tag()?).into_owned(); + DValue::UnitFloat(unit, r.f64()?) + } + b"UnFl" => { + let unit = String::from_utf8_lossy(&r.tag()?).into_owned(); + let n = r.u32()? as usize; + let mut v = Vec::with_capacity(n.min(1024)); + for _ in 0..n { + v.push(r.f64()?); + } + DValue::UnitFloats(unit, v) + } + b"TEXT" => DValue::Text(read_unicode(r)?), + b"enum" => { + let t = read_id(r)?; + DValue::Enum(t, read_id(r)?) + } + b"long" => DValue::Integer(r.u32()? as i32), + b"comp" => DValue::LargeInteger(r.u64()? as i64), + b"bool" => DValue::Bool(r.u8()? != 0), + b"type" | b"GlbC" => { + let name = read_unicode(r)?; + DValue::Class(name, read_id(r)?) + } + b"alis" | b"tdta" | b"Pth " => { + let n = r.u32()? as usize; + DValue::Raw(r.bytes(n)?.to_vec()) + } + b"obj " => { + let n = r.u32()? as usize; + let mut items = Vec::with_capacity(n.min(16)); + for _ in 0..n { + let t = r.tag()?; + items.push(match &t { + b"prop" => { + let _name = read_unicode(r)?; + let class = read_id(r)?; + RefItem::Property(class, read_id(r)?) + } + b"Clss" => { + let _name = read_unicode(r)?; + RefItem::Class(read_id(r)?) + } + b"Enmr" => { + let _name = read_unicode(r)?; + let class = read_id(r)?; + let ty = read_id(r)?; + RefItem::Enum(class, ty, read_id(r)?) + } + b"rele" => { + let _name = read_unicode(r)?; + let class = read_id(r)?; + RefItem::Offset(class, r.u32()? as i32) + } + b"Idnt" => RefItem::Identifier(r.u32()? as i32), + b"indx" => RefItem::Index(r.u32()? as i32), + b"name" => { + let _name = read_unicode(r)?; + let class = read_id(r)?; + RefItem::Name(class, read_unicode(r)?) + } + other => return Err(Error::Invalid(format!("reference item `{}`", String::from_utf8_lossy(other)))), + }); + } + DValue::Reference(items) + } + other => return Err(Error::Invalid(format!("descriptor type `{}`", String::from_utf8_lossy(other)))), + }) +} + +// ---------------------------------------------------------------- writing + +fn put_u32(out: &mut Vec, v: u32) { + out.extend_from_slice(&v.to_be_bytes()); +} + +fn put_id(out: &mut Vec, id: &str) { + if id.len() == 4 { + put_u32(out, 0); + } else { + put_u32(out, id.len() as u32); + } + out.extend_from_slice(id.as_bytes()); +} + +pub(crate) fn put_unicode(out: &mut Vec, s: &str) { + let v: Vec = s.encode_utf16().chain(std::iter::once(0)).collect(); + put_u32(out, v.len() as u32); + for c in v { + out.extend_from_slice(&c.to_be_bytes()); + } +} + +/// Serialise a descriptor (without the version field). +pub fn write_descriptor(out: &mut Vec, d: &Descriptor) { + put_unicode(out, &d.class_name); + put_id(out, &d.class_id); + put_u32(out, d.items.len() as u32); + for (k, v) in &d.items { + put_id(out, k); + write_value(out, v); + } +} + +fn write_value(out: &mut Vec, v: &DValue) { + match v { + DValue::Descriptor(d) => { + out.extend_from_slice(b"Objc"); + write_descriptor(out, d); + } + DValue::List(l) => { + out.extend_from_slice(b"VlLs"); + put_u32(out, l.len() as u32); + for x in l { + write_value(out, x); + } + } + DValue::Double(x) => { + out.extend_from_slice(b"doub"); + out.extend_from_slice(&x.to_be_bytes()); + } + DValue::UnitFloat(u, x) => { + out.extend_from_slice(b"UntF"); + out.extend_from_slice(&tag4(u)); + out.extend_from_slice(&x.to_be_bytes()); + } + DValue::UnitFloats(u, xs) => { + out.extend_from_slice(b"UnFl"); + out.extend_from_slice(&tag4(u)); + put_u32(out, xs.len() as u32); + for x in xs { + out.extend_from_slice(&x.to_be_bytes()); + } + } + DValue::Text(s) => { + out.extend_from_slice(b"TEXT"); + put_unicode(out, s); + } + DValue::Enum(t, e) => { + out.extend_from_slice(b"enum"); + put_id(out, t); + put_id(out, e); + } + DValue::Integer(i) => { + out.extend_from_slice(b"long"); + out.extend_from_slice(&i.to_be_bytes()); + } + DValue::LargeInteger(i) => { + out.extend_from_slice(b"comp"); + out.extend_from_slice(&i.to_be_bytes()); + } + DValue::Bool(b) => { + out.extend_from_slice(b"bool"); + out.push(*b as u8); + } + DValue::Class(n, c) => { + out.extend_from_slice(b"type"); + put_unicode(out, n); + put_id(out, c); + } + DValue::Raw(b) => { + out.extend_from_slice(b"tdta"); + put_u32(out, b.len() as u32); + out.extend_from_slice(b); + } + DValue::Reference(items) => { + out.extend_from_slice(b"obj "); + put_u32(out, items.len() as u32); + for it in items { + match it { + RefItem::Property(c, k) => { + out.extend_from_slice(b"prop"); + put_unicode(out, ""); + put_id(out, c); + put_id(out, k); + } + RefItem::Class(c) => { + out.extend_from_slice(b"Clss"); + put_unicode(out, ""); + put_id(out, c); + } + RefItem::Enum(c, t, e) => { + out.extend_from_slice(b"Enmr"); + put_unicode(out, ""); + put_id(out, c); + put_id(out, t); + put_id(out, e); + } + RefItem::Offset(c, o) => { + out.extend_from_slice(b"rele"); + put_unicode(out, ""); + put_id(out, c); + out.extend_from_slice(&o.to_be_bytes()); + } + RefItem::Identifier(i) => { + out.extend_from_slice(b"Idnt"); + out.extend_from_slice(&i.to_be_bytes()); + } + RefItem::Index(i) => { + out.extend_from_slice(b"indx"); + out.extend_from_slice(&i.to_be_bytes()); + } + RefItem::Name(c, n) => { + out.extend_from_slice(b"name"); + put_unicode(out, ""); + put_id(out, c); + put_unicode(out, n); + } + } + } + } + } +} + +fn tag4(s: &str) -> [u8; 4] { + let mut t = *b" "; + for (i, b) in s.bytes().take(4).enumerate() { + t[i] = b; + } + t +} + +/// Builder helpers for descriptors (tests and the writer). +impl Descriptor { + pub fn new(class_id: &str) -> Descriptor { + Descriptor { class_name: String::new(), class_id: class_id.into(), items: vec![] } + } + pub fn with(mut self, key: &str, v: DValue) -> Descriptor { + self.items.push((key.into(), v)); + self + } + /// An `RGBC` colour object from 0..1 values. + pub fn rgb(c: [f64; 3]) -> Descriptor { + Descriptor::new("RGBC").with("Rd ", DValue::Double(c[0] * 255.0)).with("Grn ", DValue::Double(c[1] * 255.0)).with("Bl ", DValue::Double(c[2] * 255.0)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn round_trip() { + let d = Descriptor::new("null") + .with("Txt ", DValue::Text("Héllo".into())) + .with("Clr ", DValue::Descriptor(Descriptor::rgb([1.0, 0.5, 0.0]))) + .with("Opct", DValue::UnitFloat("#Prc".into(), 75.0)) + .with("Md ", DValue::Enum("BlnM".into(), "Mltp".into())) + .with("enab", DValue::Bool(true)) + .with("longKeyName", DValue::List(vec![DValue::Integer(3), DValue::Double(1.5)])) + .with("EngineData", DValue::Raw(b"<< >>".to_vec())) + .with("ref", DValue::Reference(vec![RefItem::Enum("Lyr ".into(), "Ordn".into(), "Trgt".into()), RefItem::Index(2)])); + let mut out = vec![]; + write_descriptor(&mut out, &d); + let back = read_descriptor(&mut Reader::new(&out)).unwrap(); + assert_eq!(back.class_id, "null"); + assert_eq!(back.text("Txt "), Some("Héllo")); + assert_eq!(back.color("Clr "), Some([1.0, 0.5, 0.0])); + assert_eq!(back.num("Opct"), Some(75.0)); + assert_eq!(back.enum_value("Md "), Some("Mltp")); + assert_eq!(back.bool("enab"), Some(true)); + assert_eq!(back.raw("EngineData"), Some(&b"<< >>"[..])); + assert_eq!(back, d); + } +} diff --git a/crates/psd/src/engine_data.rs b/crates/psd/src/engine_data.rs new file mode 100644 index 00000000..c4f23038 --- /dev/null +++ b/crates/psd/src/engine_data.rs @@ -0,0 +1,288 @@ +//! The text engine data of type layers (`EngineData` in the `TySh` descriptor): a PostScript-like +//! tree of dictionaries (`<< /Key value >>`), arrays (`[ ]`), numbers, names, booleans and +//! strings (`(þÿ` + UTF-16BE with backslash escapes `)`). Read best-effort. + +#[derive(Clone, Debug, PartialEq)] +pub enum EValue { + Dict(Vec<(String, EValue)>), + Array(Vec), + Num(f64), + Bool(bool), + Name(String), + Str(String), +} + +impl EValue { + pub fn get(&self, key: &str) -> Option<&EValue> { + match self { + EValue::Dict(v) => v.iter().find(|(k, _)| k == key).map(|(_, v)| v), + _ => None, + } + } + pub fn idx(&self, i: usize) -> Option<&EValue> { + match self { + EValue::Array(v) => v.get(i), + _ => None, + } + } + /// Follow a `/`-separated path; numeric segments index arrays. + pub fn path(&self, p: &str) -> Option<&EValue> { + let mut cur = self; + for seg in p.split('/').filter(|s| !s.is_empty()) { + cur = match seg.parse::() { + Ok(i) => cur.idx(i)?, + Err(_) => cur.get(seg)?, + }; + } + Some(cur) + } + pub fn num(&self) -> Option { + match self { + EValue::Num(n) => Some(*n), + _ => None, + } + } + pub fn str(&self) -> Option<&str> { + match self { + EValue::Str(s) | EValue::Name(s) => Some(s), + _ => None, + } + } +} + +struct P<'a> { + b: &'a [u8], + i: usize, +} + +impl P<'_> { + fn ws(&mut self) { + while self.i < self.b.len() && (self.b[self.i].is_ascii_whitespace() || self.b[self.i] == 0) { + self.i += 1; + } + } + fn peek(&self, s: &[u8]) -> bool { + self.b[self.i..].starts_with(s) + } + fn value(&mut self, depth: usize) -> Option { + if depth > 128 { + return None; + } + self.ws(); + if self.i >= self.b.len() { + return None; + } + if self.peek(b"<<") { + self.i += 2; + let mut v = vec![]; + loop { + self.ws(); + if self.i >= self.b.len() { + return Some(EValue::Dict(v)); + } + if self.peek(b">>") { + self.i += 2; + return Some(EValue::Dict(v)); + } + if self.b[self.i] != b'/' { + // Unexpected token: skip it. + self.i += 1; + continue; + } + let key = self.name(); + let val = self.value(depth + 1)?; + v.push((key, val)); + } + } + match self.b[self.i] { + b'[' => { + self.i += 1; + let mut v = vec![]; + loop { + self.ws(); + if self.i >= self.b.len() { + return Some(EValue::Array(v)); + } + if self.b[self.i] == b']' { + self.i += 1; + return Some(EValue::Array(v)); + } + let start = self.i; + match self.value(depth + 1) { + Some(x) => v.push(x), + None => return Some(EValue::Array(v)), + } + if self.i == start { + self.i += 1; + } + } + } + b'/' => Some(EValue::Name(self.name())), + b'(' => Some(EValue::Str(self.string())), + _ => { + let start = self.i; + while self.i < self.b.len() && !self.b[self.i].is_ascii_whitespace() && !b"[]<>/(".contains(&self.b[self.i]) { + self.i += 1; + } + let tok = std::str::from_utf8(&self.b[start..self.i]).unwrap_or(""); + if self.i == start { + self.i += 1; + return Some(EValue::Name(String::new())); + } + Some(match tok { + "true" => EValue::Bool(true), + "false" => EValue::Bool(false), + t => t.parse::().map(EValue::Num).unwrap_or_else(|_| EValue::Name(t.to_string())), + }) + } + } + } + fn name(&mut self) -> String { + self.i += 1; // '/' + let start = self.i; + while self.i < self.b.len() && !self.b[self.i].is_ascii_whitespace() && !b"[]<>/(".contains(&self.b[self.i]) { + self.i += 1; + } + String::from_utf8_lossy(&self.b[start..self.i]).into_owned() + } + fn string(&mut self) -> String { + self.i += 1; // '(' + let mut raw = vec![]; + while self.i < self.b.len() { + let c = self.b[self.i]; + self.i += 1; + match c { + b'\\' if self.i < self.b.len() => { + let e = self.b[self.i]; + self.i += 1; + raw.push(match e { + b'n' => b'\n', + b'r' => b'\r', + b't' => b'\t', + other => other, + }); + } + b')' => break, + _ => raw.push(c), + } + } + if raw.starts_with(&[0xFE, 0xFF]) { + let u: Vec = raw[2..].chunks_exact(2).map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); + String::from_utf16_lossy(&u).trim_end_matches('\0').to_string() + } else { + String::from_utf8_lossy(&raw).into_owned() + } + } +} + +/// Parse engine data (returns an empty dictionary on garbage). +pub fn parse(b: &[u8]) -> EValue { + let mut p = P { b, i: 0 }; + p.value(0).unwrap_or(EValue::Dict(vec![])) +} + +/// Encode a string the way engine data stores it (UTF-16BE with a BOM, escaped). +pub fn encode_string(s: &str) -> Vec { + let mut out = vec![b'(', 0xFE, 0xFF]; + for u in s.encode_utf16() { + for b in u.to_be_bytes() { + if matches!(b, b'(' | b')' | b'\\') { + out.push(b'\\'); + } + out.push(b); + } + } + out.push(b')'); + out +} + +/// The first style run's character attributes. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct TextStyle { + pub font: Option, + pub size: Option, + /// RGB 0..1. + pub color: Option<[f64; 3]>, + /// 0 left, 1 right, 2 centre, 3+ justified variants. + pub justification: Option, + pub tracking: Option, + pub leading: Option, + pub faux_bold: bool, + pub faux_italic: bool, + /// Paragraph (box) text rather than point text. + pub box_text: bool, +} + +pub fn text_style(ed: &EValue) -> TextStyle { + let engine = ed.get("EngineDict").unwrap_or(ed); + let resources = ed.get("ResourceDict").or_else(|| ed.get("DocumentResources")); + let run = engine.path("StyleRun/RunArray/0/StyleSheet/StyleSheetData"); + let normal = resources.and_then(|r| r.path("StyleSheetSet/0/StyleSheetData")); + let attr = |k: &str| run.and_then(|r| r.get(k)).or_else(|| normal.and_then(|n| n.get(k))); + let font = + attr("Font").and_then(EValue::num).and_then(|i| resources?.path(&format!("FontSet/{}/Name", i as usize))).and_then(EValue::str).map(str::to_string); + let color = attr("FillColor").and_then(|c| c.get("Values")).and_then(|v| match v { + EValue::Array(a) if a.len() >= 4 => Some([a[1].num()?, a[2].num()?, a[3].num()?]), + _ => None, + }); + let para = engine.path("ParagraphRun/RunArray/0/ParagraphSheet/Properties"); + let para_normal = resources.and_then(|r| r.path("ParagraphSheetSet/0/Properties")); + let justification = + para.and_then(|p| p.get("Justification")).or_else(|| para_normal.and_then(|p| p.get("Justification"))).and_then(EValue::num).map(|n| n as u8); + let auto_leading = attr("AutoLeading").is_none_or(|v| *v != EValue::Bool(false)); + let box_text = engine.path("Rendered/Shapes/Children/0/ShapeType").and_then(EValue::num) == Some(1.0); + TextStyle { + font, + size: attr("FontSize").and_then(EValue::num), + color, + justification, + tracking: attr("Tracking").and_then(EValue::num), + leading: if auto_leading { None } else { attr("Leading").and_then(EValue::num) }, + faux_bold: attr("FauxBold") == Some(&EValue::Bool(true)), + faux_italic: attr("FauxItalic") == Some(&EValue::Bool(true)), + box_text, + } +} + +/// Minimal engine data for a type layer with one style run (fixtures, and what a writer needs). +pub fn minimal(text: &str, font: &str, size: f64, color: [f64; 3], justification: u8) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(b"\n\n<<\n\t/EngineDict\n\t<<\n\t\t/Editor << /Text "); + out.extend_from_slice(&encode_string(&format!("{text}\r"))); + out.extend_from_slice(b" >>\n\t\t/ParagraphRun << /RunArray [ << /ParagraphSheet << /Properties << /Justification "); + out.extend_from_slice(justification.to_string().as_bytes()); + out.extend_from_slice(b" >> >> >> ] >>\n\t\t/StyleRun << /RunArray [ << /StyleSheet << /StyleSheetData << /Font 0 /FontSize "); + out.extend_from_slice(format!("{size:.1}").as_bytes()); + out.extend_from_slice( + format!(" /FillColor << /Type 1 /Values [ 1.0 {:.5} {:.5} {:.5} ] >> /AutoLeading true >> >> >> ] >>\n\t>>\n", color[0], color[1], color[2]).as_bytes(), + ); + out.extend_from_slice(b"\t/ResourceDict << /FontSet [ << /Name "); + out.extend_from_slice(&encode_string(font)); + out.extend_from_slice(b" /Type 0 >> ] >>\n>>"); + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_minimal() { + let b = minimal("Hi (there)", "Arial-BoldMT", 36.0, [1.0, 0.5, 0.0], 2); + let v = parse(&b); + assert_eq!(v.path("EngineDict/Editor/Text").and_then(EValue::str), Some("Hi (there)\r")); + let st = text_style(&v); + assert_eq!(st.font.as_deref(), Some("Arial-BoldMT")); + assert_eq!(st.size, Some(36.0)); + assert_eq!(st.color, Some([1.0, 0.5, 0.0])); + assert_eq!(st.justification, Some(2)); + assert!(!st.box_text); + } + + #[test] + fn garbage_is_harmless() { + let _ = parse(b"<< /A [ 1 2 << /B (unterminated"); + let _ = parse(b"]]]>>>> /x"); + let _ = parse(&[0xFF; 64]); + } +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs new file mode 100644 index 00000000..885ab3c2 --- /dev/null +++ b/crates/psd/src/lib.rs @@ -0,0 +1,965 @@ +//! Photoshop document (PSD / PSB) reader, implemented from Adobe's public *Photoshop File Formats +//! Specification* (see README.md), and a minimal writer ([`write`]) for fixtures. +//! +//! [`Psd::parse`] reads the structure (header, layer records and their additional information, +//! the layer tree) without decoding pixels; [`Psd::composite`] decodes the merged image and +//! [`Psd::layer_pixels`] one layer (with its layer mask applied to alpha) on demand. Pixels come +//! out as straight (unpremultiplied) RGBA `f32` in 0..1, display-encoded (32-bit documents, +//! which are linear, are encoded to sRGB). + +pub mod descriptor; +pub mod engine_data; +mod read; +pub mod write; + +use std::sync::Arc; + +pub use descriptor::{DValue, Descriptor}; +use read::Reader; + +#[derive(Debug, Clone, PartialEq)] +pub enum Error { + NotPsd, + Truncated, + Unsupported(String), + Invalid(String), +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::NotPsd => write!(f, "not a Photoshop document"), + Error::Truncated => write!(f, "truncated Photoshop document"), + Error::Unsupported(s) => write!(f, "unsupported Photoshop feature: {s}"), + Error::Invalid(s) => write!(f, "invalid Photoshop document: {s}"), + } + } +} + +impl std::error::Error for Error {} + +pub type Result = std::result::Result; + +/// Whether `bytes` start like a Photoshop document. +pub fn is_psd(bytes: &[u8]) -> bool { + bytes.len() >= 6 && &bytes[..4] == b"8BPS" && matches!(u16::from_be_bytes([bytes[4], bytes[5]]), 1 | 2) +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ColorMode { + Bitmap, + Grayscale, + Indexed, + Rgb, + Cmyk, + Multichannel, + Duotone, + Lab, +} + +impl ColorMode { + pub fn from_u16(v: u16) -> Option { + Some(match v { + 0 => ColorMode::Bitmap, + 1 => ColorMode::Grayscale, + 2 => ColorMode::Indexed, + 3 => ColorMode::Rgb, + 4 => ColorMode::Cmyk, + 7 => ColorMode::Multichannel, + 8 => ColorMode::Duotone, + 9 => ColorMode::Lab, + _ => return None, + }) + } + pub fn to_u16(self) -> u16 { + match self { + ColorMode::Bitmap => 0, + ColorMode::Grayscale => 1, + ColorMode::Indexed => 2, + ColorMode::Rgb => 3, + ColorMode::Cmyk => 4, + ColorMode::Multichannel => 7, + ColorMode::Duotone => 8, + ColorMode::Lab => 9, + } + } + /// Number of colour channels (before alpha). + pub fn color_channels(self) -> usize { + match self { + ColorMode::Rgb | ColorMode::Lab => 3, + ColorMode::Cmyk => 4, + _ => 1, + } + } +} + +/// A rectangle in document pixels (`right`/`bottom` exclusive). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Rect { + pub top: i32, + pub left: i32, + pub bottom: i32, + pub right: i32, +} + +impl Rect { + pub fn new(left: i32, top: i32, width: i32, height: i32) -> Rect { + Rect { top, left, bottom: top + height, right: left + width } + } + pub fn width(&self) -> u32 { + (self.right - self.left).max(0) as u32 + } + pub fn height(&self) -> u32 { + (self.bottom - self.top).max(0) as u32 + } + pub fn is_empty(&self) -> bool { + self.width() == 0 || self.height() == 0 + } +} + +/// One channel of a layer: id (0.. colour, -1 transparency, -2 layer mask, -3 real user mask) and +/// where its data (compression + image data) sits in the file. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ChannelInfo { + pub id: i16, + pub offset: usize, + pub len: usize, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct LayerMask { + pub rect: Rect, + /// Value outside the mask rectangle (0 or 255). + pub default_color: u8, + pub disabled: bool, + pub inverted: bool, +} + +/// Layer kind from the section divider setting (`lsct`). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Section { + #[default] + Layer, + OpenFolder, + ClosedFolder, + /// The hidden "" record that closes a group (below its children). + Divider, +} + +/// A type layer (`TySh`). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct TextInfo { + /// Text with `\n` line breaks. + pub text: String, + /// Affine transform (xx, xy, yx, yy, tx, ty): the text origin sits at (tx, ty). + pub transform: [f64; 6], + pub style: engine_data::TextStyle, + /// Text bounds (left, top, right, bottom) relative to the origin, when stored. + pub bounds: Option<[f64; 4]>, +} + +/// Adjustment layers (best effort). +#[derive(Clone, Debug, PartialEq)] +pub enum Adjustment { + Invert, + BrightnessContrast { + brightness: f64, + contrast: f64, + legacy: bool, + }, + HueSaturation { + hue: f64, + saturation: f64, + lightness: f64, + colorize: bool, + }, + Levels { + in_black: f64, + in_white: f64, + gamma: f64, + out_black: f64, + out_white: f64, + }, + Exposure { + exposure: f64, + offset: f64, + gamma: f64, + }, + /// Another adjustment (its tagged-block key, e.g. `curv`, `blnc`). + Other(String), +} + +/// A vector mask path: sub-paths of (in tangent, vertex, out tangent) points in document pixels. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct VectorMask { + pub inverted: bool, + pub disabled: bool, + pub subpaths: Vec, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct SubPath { + pub closed: bool, + /// Per knot: [control point preceding, anchor, control point leaving] as (x, y). + pub knots: Vec<[[f64; 2]; 3]>, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Layer { + /// Index in [`Psd::layers`] (file order: bottom of the stack first). + pub index: usize, + pub name: String, + pub id: Option, + pub rect: Rect, + /// Blend mode key (`norm`, `mul `, `pass`…). + pub blend: [u8; 4], + pub opacity: u8, + /// Fill opacity (`iOpa`), 255 when absent. + pub fill_opacity: u8, + /// Clipped to the layer below. + pub clipping: bool, + pub hidden: bool, + pub protect_transparency: bool, + pub section: Section, + pub channels: Vec, + pub mask: Option, + pub text: Option, + pub adjustment: Option, + /// Solid colour fill layer (`SoCo`), RGB 0..1. + pub fill: Option<[f64; 3]>, + /// Layer effects (`lfx2` descriptor). + pub effects: Option, + pub vector_mask: Option, + /// Keys of every additional-information block the layer carries. + pub keys: Vec<[u8; 4]>, +} + +impl Layer { + pub fn is_group(&self) -> bool { + matches!(self.section, Section::OpenFolder | Section::ClosedFolder) + } + pub fn blend_key(&self) -> &str { + std::str::from_utf8(&self.blend).unwrap_or("norm") + } + pub fn has_pixels(&self) -> bool { + !self.rect.is_empty() && self.channels.iter().any(|c| c.id >= 0) + } +} + +/// The layer tree, top of the stack first. +#[derive(Clone, Debug, PartialEq)] +pub enum Node { + Layer(usize), + Group { layer: usize, children: Vec }, +} + +/// Decoded pixels: straight RGBA, row-major. +#[derive(Clone, Debug, PartialEq)] +pub struct Pixels { + pub width: u32, + pub height: u32, + pub data: Vec<[f32; 4]>, +} + +/// A parsed Photoshop document. +#[derive(Clone, Debug)] +pub struct Psd { + /// PSB (large document format). + pub psb: bool, + pub width: u32, + pub height: u32, + pub channels: u16, + pub depth: u16, + pub mode: ColorMode, + /// Bottom of the stack first (file order). + pub layers: Vec, + /// The merged image's first extra channel is transparency. + pub merged_alpha: bool, + /// Indexed colour palette (256 R, then G, then B). + pub palette: Vec, + data: Arc<[u8]>, + merged_offset: usize, +} + +impl Psd { + pub fn parse(data: impl Into>) -> Result { + let data: Arc<[u8]> = data.into(); + let mut r = Reader::new(&data); + if r.bytes(4).ok() != Some(b"8BPS") { + return Err(Error::NotPsd); + } + let version = r.u16()?; + let psb = match version { + 1 => false, + 2 => true, + _ => return Err(Error::NotPsd), + }; + r.skip(6)?; + let channels = r.u16()?; + let height = r.u32()?; + let width = r.u32()?; + let depth = r.u16()?; + let mode_v = r.u16()?; + let mode = ColorMode::from_u16(mode_v).ok_or_else(|| Error::Unsupported(format!("colour mode {mode_v}")))?; + if !matches!(depth, 1 | 8 | 16 | 32) { + return Err(Error::Unsupported(format!("{depth}-bit"))); + } + if width == 0 || height == 0 || width > 300_000 || height > 300_000 { + return Err(Error::Invalid(format!("size {width}x{height}"))); + } + // Colour mode data. + let n = r.u32()? as usize; + let palette = r.bytes(n)?.to_vec(); + // Image resources. + let n = r.u32()? as usize; + r.skip(n)?; + // Layer and mask information. + let mut psd = Psd { psb, width, height, channels, depth, mode, layers: vec![], merged_alpha: false, palette, data: data.clone(), merged_offset: 0 }; + let lm_len = r.length(psb)?; + let lm_end = r.pos.checked_add(lm_len).filter(|&e| e <= data.len()).ok_or(Error::Truncated)?; + if lm_len > 0 { + let li_len = r.length(psb)?; + let li_end = r.pos.checked_add(li_len).filter(|&e| e <= lm_end).ok_or(Error::Truncated)?; + if li_len > 0 { + psd.layer_info(&data, r.pos, li_end)?; + } + r.pos = li_end; + if r.pos + 4 <= lm_end { + let g = r.u32()? as usize; + r.pos = (r.pos + g).min(lm_end); + } + // Global additional layer information (16/32-bit layer info lives here). + while let Some((key, start, end)) = next_block(&data, &mut r.pos, lm_end, psb) { + if matches!(&key, b"Lr16" | b"Lr32" | b"Layr") && psd.layers.is_empty() && end > start { + // The block holds the layer info, normally without its length field; accept + // both layouts. + if psd.layer_info(&data, start, end).is_err() || psd.layers.is_empty() { + psd.layers.clear(); + let skip = if psb { 8 } else { 4 }; + if start + skip < end && psd.layer_info(&data, start + skip, end).is_err() { + psd.layers.clear(); + } + } + } + r.pos = end; + } + } + psd.merged_offset = lm_end; + Ok(psd) + } + + fn layer_info(&mut self, data: &[u8], start: usize, end: usize) -> Result<()> { + let mut r = Reader::at(&data[..end], start); + let count = r.i16()?; + self.merged_alpha = count < 0; + let n = count.unsigned_abs() as usize; + let mut layers = Vec::with_capacity(n); + for i in 0..n { + layers.push(self.layer_record(&mut r, i)?); + } + // Channel image data follows in record order. + for l in &mut layers { + for c in &mut l.channels { + c.offset = r.pos; + r.skip(c.len)?; + } + } + self.layers = layers; + Ok(()) + } + + fn layer_record(&self, r: &mut Reader, index: usize) -> Result { + let psb = self.psb; + let (top, left, bottom, right) = (r.i32()?, r.i32()?, r.i32()?, r.i32()?); + let nch = r.u16()? as usize; + if nch > 64 { + return Err(Error::Invalid("channel count".into())); + } + let mut channels = Vec::with_capacity(nch); + for _ in 0..nch { + let id = r.i16()?; + let len = r.length(psb)?; + channels.push(ChannelInfo { id, offset: 0, len }); + } + if r.bytes(4)? != b"8BIM" { + return Err(Error::Invalid("layer record signature".into())); + } + let blend = r.tag()?; + let opacity = r.u8()?; + let clipping = r.u8()? != 0; + let flags = r.u8()?; + r.skip(1)?; + let extra = r.u32()? as usize; + let extra_end = r.pos.checked_add(extra).filter(|&e| e <= r.data.len()).ok_or(Error::Truncated)?; + let mut l = Layer { + index, + rect: Rect { top, left, bottom, right }, + blend, + opacity, + fill_opacity: 255, + clipping, + hidden: flags & 2 != 0, + protect_transparency: flags & 1 != 0, + channels, + ..Default::default() + }; + // Layer mask data. + let mlen = r.u32()? as usize; + let mstart = r.pos; + if mlen >= 18 { + let rect = Rect { top: r.i32()?, left: r.i32()?, bottom: r.i32()?, right: r.i32()? }; + let default_color = r.u8()?; + let mflags = r.u8()?; + l.mask = Some(LayerMask { rect, default_color, disabled: mflags & 2 != 0, inverted: mflags & 4 != 0 }); + } + r.pos = mstart + mlen; + // Blending ranges. + let blen = r.u32()? as usize; + r.skip(blen)?; + l.name = r.pascal(4)?; + let mut pos = r.pos; + let data = r.data; + while let Some((key, start, end)) = next_block(data, &mut pos, extra_end, psb) { + l.keys.push(key); + let block = &data[start..end]; + // A malformed block only loses that block's information. + let _ = self.layer_block(&mut l, &key, block); + pos = end; + } + r.pos = extra_end; + Ok(l) + } + + fn layer_block(&self, l: &mut Layer, key: &[u8; 4], b: &[u8]) -> Result<()> { + let mut r = Reader::new(b); + match key { + b"luni" => { + let name = descriptor::read_unicode(&mut r)?; + if !name.is_empty() { + l.name = name; + } + } + b"lyid" => l.id = Some(r.u32()?), + b"iOpa" => l.fill_opacity = r.u8()?, + b"lsct" | b"lsdk" => { + l.section = match r.u32()? { + 1 => Section::OpenFolder, + 2 => Section::ClosedFolder, + 3 => Section::Divider, + _ => Section::Layer, + }; + if b.len() >= 12 && &b[4..8] == b"8BIM" { + l.blend.copy_from_slice(&b[8..12]); + } + } + b"TySh" => { + let _ver = r.u16()?; + let mut t = [0.0; 6]; + for v in &mut t { + *v = r.f64()?; + } + let _text_ver = r.u16()?; + let _desc_ver = r.u32()?; + let d = descriptor::read_descriptor(&mut r)?; + let text = d.text("Txt ").unwrap_or_default().replace("\r\n", "\n").replace('\r', "\n"); + let text = text.strip_suffix('\n').map(str::to_string).unwrap_or(text); + let style = d.raw("EngineData").map(|e| engine_data::text_style(&engine_data::parse(e))).unwrap_or_default(); + let bounds = + d.obj("bounds").or_else(|| d.obj("boundingBox")).and_then(|o| Some([o.num("Left")?, o.num("Top ")?, o.num("Rght")?, o.num("Btom")?])); + l.text = Some(TextInfo { text, transform: t, style, bounds }); + } + b"lfx2" => { + let _ver = r.u32()?; + let _dver = r.u32()?; + l.effects = Some(descriptor::read_descriptor(&mut r)?); + } + b"SoCo" => { + let _ver = r.u32()?; + let d = descriptor::read_descriptor(&mut r)?; + l.fill = d.color("Clr "); + } + b"nvrt" => l.adjustment = Some(Adjustment::Invert), + b"brit" => { + let brightness = r.i16()? as f64; + let contrast = r.i16()? as f64; + if l.adjustment.is_none() { + l.adjustment = Some(Adjustment::BrightnessContrast { brightness, contrast, legacy: true }); + } + } + b"CgEd" => { + let _ver = r.u32()?; + let d = descriptor::read_descriptor(&mut r)?; + l.adjustment = Some(Adjustment::BrightnessContrast { + brightness: d.num("Brgh").unwrap_or(0.0), + contrast: d.num("Cntr").unwrap_or(0.0), + legacy: d.bool("useLegacy").unwrap_or(false), + }); + } + b"hue2" | b"hue " => { + let _ver = r.u16()?; + let colorize = r.u8()? != 0; + r.skip(1)?; + let ch = (r.i16()? as f64, r.i16()? as f64, r.i16()? as f64); + let m = (r.i16()? as f64, r.i16()? as f64, r.i16()? as f64); + let (hue, saturation, lightness) = if colorize { ch } else { m }; + l.adjustment = Some(Adjustment::HueSaturation { hue, saturation, lightness, colorize }); + } + b"levl" => { + let _ver = r.u16()?; + let (ib, iw, ob, ow, g) = (r.i16()?, r.i16()?, r.i16()?, r.i16()?, r.i16()?); + l.adjustment = + Some(Adjustment::Levels { in_black: ib as f64, in_white: iw as f64, gamma: g as f64 / 100.0, out_black: ob as f64, out_white: ow as f64 }); + } + b"expA" => { + let _ver = r.u16()?; + let exposure = r.f32()? as f64; + let offset = r.f32()? as f64; + let gamma = r.f32()? as f64; + l.adjustment = Some(Adjustment::Exposure { exposure, offset, gamma }); + } + b"curv" | b"blnc" | b"post" | b"thrs" | b"selc" | b"mixr" | b"phfl" | b"vibA" | b"blwh" | b"grdm" | b"clrL" => { + if l.adjustment.is_none() { + l.adjustment = Some(Adjustment::Other(String::from_utf8_lossy(key).into_owned())); + } + } + b"vmsk" | b"vsms" => { + let _ver = r.u32()?; + let flags = r.u32()?; + l.vector_mask = Some(self.vector_paths(&mut r, flags)?); + } + _ => {} + } + Ok(()) + } + + fn vector_paths(&self, r: &mut Reader, flags: u32) -> Result { + let (w, h) = (self.width as f64, self.height as f64); + let mut vm = VectorMask { inverted: flags & 1 != 0, disabled: flags & 4 != 0, subpaths: vec![] }; + let fixed = |r: &mut Reader| -> Result { Ok(r.i32()? as f64 / (1 << 24) as f64) }; + while r.remaining() >= 26 { + let sel = r.u16()?; + match sel { + 0 | 3 => { + let _n = r.u16()?; + r.skip(22)?; + vm.subpaths.push(SubPath { closed: sel == 0, knots: vec![] }); + } + 1 | 2 | 4 | 5 => { + let mut pts = [[0.0; 2]; 3]; + for p in &mut pts { + let y = fixed(r)?; + let x = fixed(r)?; + *p = [x * w, y * h]; + } + if let Some(sp) = vm.subpaths.last_mut() { + sp.knots.push(pts); + } + } + _ => r.skip(24)?, + } + } + vm.subpaths.retain(|s| !s.knots.is_empty()); + Ok(vm) + } + + /// The layer tree, top of the stack first. + pub fn tree(&self) -> Vec { + let mut stack: Vec> = vec![vec![]]; + for l in &self.layers { + match l.section { + Section::Divider => stack.push(vec![]), + Section::OpenFolder | Section::ClosedFolder => { + let children = if stack.len() > 1 { stack.pop().unwrap_or_default() } else { vec![] }; + if let Some(top) = stack.last_mut() { + top.push(Node::Group { layer: l.index, children }); + } + } + Section::Layer => { + if let Some(top) = stack.last_mut() { + top.push(Node::Layer(l.index)); + } + } + } + } + while stack.len() > 1 { + let c = stack.pop().unwrap_or_default(); + if let Some(top) = stack.last_mut() { + top.extend(c); + } + } + fn rev(v: &mut Vec) { + v.reverse(); + for n in v { + if let Node::Group { children, .. } = n { + rev(children); + } + } + } + let mut out = stack.pop().unwrap_or_default(); + rev(&mut out); + out + } + + fn bytes_per_sample(&self) -> usize { + match self.depth { + 16 => 2, + 32 => 4, + _ => 1, + } + } + + /// Decode one channel of `w`×`h` samples at `offset` (compression field first). Values 0..1. + fn decode_channel(&self, offset: usize, len: usize, w: usize, h: usize) -> Result> { + let end = offset.checked_add(len).filter(|&e| e <= self.data.len()).ok_or(Error::Truncated)?; + let mut r = Reader::at(&self.data[..end], offset); + let comp = r.u16()?; + let rowb = self.row_bytes(w); + let raw = match comp { + 0 => r.bytes(rowb * h)?.to_vec(), + 1 => { + let mut counts = Vec::with_capacity(h); + for _ in 0..h { + counts.push(if self.psb { r.u32()? as usize } else { r.u16()? as usize }); + } + let mut out = Vec::with_capacity(rowb * h); + for c in counts { + unpack_bits(r.bytes(c)?, rowb, &mut out)?; + } + out + } + 2 | 3 => { + let rest = r.bytes(r.remaining())?; + let mut v = miniz_oxide::inflate::decompress_to_vec_zlib(rest).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?; + v.resize(rowb * h, 0); + if comp == 3 { + self.unpredict(&mut v, w, h); + } + v + } + c => return Err(Error::Unsupported(format!("compression {c}"))), + }; + Ok(self.samples(&raw, w, h)) + } + + fn row_bytes(&self, w: usize) -> usize { + if self.depth == 1 { w.div_ceil(8) } else { w * self.bytes_per_sample() } + } + + fn unpredict(&self, v: &mut [u8], w: usize, h: usize) { + match self.depth { + 16 => { + for row in v.chunks_exact_mut(w * 2).take(h) { + let mut prev = 0u16; + for c in row.chunks_exact_mut(2) { + let x = u16::from_be_bytes([c[0], c[1]]).wrapping_add(prev); + c.copy_from_slice(&x.to_be_bytes()); + prev = x; + } + } + } + 32 => { + for row in v.chunks_exact_mut(w * 4).take(h) { + for i in 1..row.len() { + row[i] = row[i].wrapping_add(row[i - 1]); + } + // Bytes are stored plane by plane (all first bytes, then all second bytes…). + let planar = row.to_vec(); + for x in 0..w { + for b in 0..4 { + row[x * 4 + b] = planar[b * w + x]; + } + } + } + } + _ => { + for row in v.chunks_exact_mut(w).take(h) { + for i in 1..row.len() { + row[i] = row[i].wrapping_add(row[i - 1]); + } + } + } + } + } + + fn samples(&self, raw: &[u8], w: usize, h: usize) -> Vec { + match self.depth { + 1 => { + let rowb = w.div_ceil(8); + let mut out = Vec::with_capacity(w * h); + for y in 0..h { + for x in 0..w { + let b = raw.get(y * rowb + x / 8).copied().unwrap_or(0); + // 1 = black. + out.push(if b & (0x80 >> (x % 8)) != 0 { 0.0 } else { 1.0 }); + } + } + out + } + 16 => raw.chunks_exact(2).take(w * h).map(|c| u16::from_be_bytes([c[0], c[1]]) as f32 / 65535.0).collect(), + 32 => raw.chunks_exact(4).take(w * h).map(|c| f32::from_be_bytes([c[0], c[1], c[2], c[3]])).collect(), + _ => raw.iter().take(w * h).map(|&b| b as f32 / 255.0).collect(), + } + } + + /// Convert colour channels (in the document's mode) to RGB. + fn to_rgb(&self, ch: &[Option>], i: usize) -> [f32; 3] { + let g = |k: usize| ch.get(k).and_then(|c| c.as_ref()).map_or(0.0, |c| c[i]); + let rgb = match self.mode { + ColorMode::Rgb => [g(0), g(1), g(2)], + ColorMode::Cmyk => { + // Stored inverted (1 = no ink). + let k = g(3); + [g(0) * k, g(1) * k, g(2) * k] + } + ColorMode::Indexed if self.palette.len() >= 768 => { + let idx = (g(0) * 255.0).round() as usize & 255; + [self.palette[idx] as f32 / 255.0, self.palette[256 + idx] as f32 / 255.0, self.palette[512 + idx] as f32 / 255.0] + } + ColorMode::Lab => lab_to_rgb(g(0) * 100.0, g(1) * 255.0 - 128.0, g(2) * 255.0 - 128.0), + _ => [g(0); 3], + }; + if self.depth == 32 && self.mode != ColorMode::Lab { rgb.map(srgb_encode) } else { rgb } + } + + /// The merged (flattened) image. + pub fn composite(&self) -> Result { + let (w, h) = (self.width as usize, self.height as usize); + let mut r = Reader::at(&self.data, self.merged_offset); + let comp = r.u16()?; + let nch = self.channels as usize; + let cc = self.mode.color_channels(); + let want = (cc + usize::from(self.merged_alpha)).min(nch); + let rowb = self.row_bytes(w); + let mut planes: Vec>> = vec![None; nch]; + match comp { + 0 => { + for p in planes.iter_mut().take(want) { + *p = Some(self.samples(r.bytes(rowb * h)?, w, h)); + } + } + 1 => { + let mut counts = Vec::with_capacity(nch * h); + for _ in 0..nch * h { + counts.push(if self.psb { r.u32()? as usize } else { r.u16()? as usize }); + } + for (c, p) in planes.iter_mut().enumerate().take(want) { + let mut out = Vec::with_capacity(rowb * h); + for row in 0..h { + unpack_bits(r.bytes(counts[c * h + row])?, rowb, &mut out)?; + } + *p = Some(self.samples(&out, w, h)); + } + } + 2 | 3 => { + let rest = r.bytes(r.remaining())?; + let v = miniz_oxide::inflate::decompress_to_vec_zlib(rest).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?; + for (c, p) in planes.iter_mut().enumerate().take(want) { + let mut plane = v.get(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?.to_vec(); + if comp == 3 { + self.unpredict(&mut plane, w, h); + } + *p = Some(self.samples(&plane, w, h)); + } + } + c => return Err(Error::Unsupported(format!("compression {c}"))), + } + let alpha = if self.merged_alpha && nch > cc { planes[cc].take() } else { None }; + let mut data = Vec::with_capacity(w * h); + for i in 0..w * h { + let [r, g, b] = self.to_rgb(&planes, i); + let a = alpha.as_ref().map_or(1.0, |a| a[i]); + data.push([r, g, b, a]); + } + Ok(Pixels { width: w as u32, height: h as u32, data }) + } + + /// One layer's pixels with its layer mask (and vector-less fill colour) applied to alpha. + /// `canvas`: document-sized with the layer in place; otherwise the layer's own bounds. + pub fn layer_pixels(&self, index: usize, canvas: bool) -> Result { + let l = self.layers.get(index).ok_or_else(|| Error::Invalid(format!("no layer {index}")))?; + let doc = Rect { top: 0, left: 0, bottom: self.height as i32, right: self.width as i32 }; + // Fill layers often have no pixel data: they cover the canvas (or the mask). + let fill_rect = + if l.fill.is_some() && !l.has_pixels() { l.mask.filter(|m| !m.rect.is_empty() && m.default_color == 0).map_or(doc, |m| m.rect) } else { l.rect }; + let out_rect = if canvas { + doc + } else if fill_rect.is_empty() { + Rect { top: 0, left: 0, bottom: 1, right: 1 } + } else { + fill_rect + }; + let (ow, oh) = (out_rect.width() as usize, out_rect.height() as usize); + let mut out = vec![[0.0f32; 4]; ow * oh]; + let src = fill_rect; + let (sw, sh) = (src.width() as usize, src.height() as usize); + if sw > 0 && sh > 0 { + let mut planes: Vec>> = vec![None; self.mode.color_channels()]; + let mut alpha = None; + if l.has_pixels() { + for c in &l.channels { + match c.id { + -1 => alpha = Some(self.decode_channel(c.offset, c.len, sw, sh)?), + id if id >= 0 && (id as usize) < planes.len() => planes[id as usize] = Some(self.decode_channel(c.offset, c.len, sw, sh)?), + _ => {} + } + } + } + let fill = l.fill.map(|c| c.map(|v| v as f32)); + for y in 0..sh { + let dy = src.top + y as i32 - out_rect.top; + if dy < 0 || dy >= oh as i32 { + continue; + } + for x in 0..sw { + let dx = src.left + x as i32 - out_rect.left; + if dx < 0 || dx >= ow as i32 { + continue; + } + let i = y * sw + x; + let (rgb, a) = match fill { + Some(f) if !l.has_pixels() => (f, 1.0), + _ => (self.to_rgb(&planes, i), alpha.as_ref().map_or(1.0, |a| a[i])), + }; + out[dy as usize * ow + dx as usize] = [rgb[0], rgb[1], rgb[2], a]; + } + } + } + // Layer mask: multiplies alpha (also outside the mask rect, by its default colour). + if let Some(m) = l.mask.filter(|m| !m.disabled) { + let mc = l.channels.iter().find(|c| c.id == -2); + let (mw, mh) = (m.rect.width() as usize, m.rect.height() as usize); + let mask = match mc { + Some(c) if mw > 0 && mh > 0 => Some(self.decode_channel(c.offset, c.len, mw, mh)?), + _ => None, + }; + let def = m.default_color as f32 / 255.0; + for y in 0..oh { + let py = out_rect.top + y as i32; + for x in 0..ow { + let px = out_rect.left + x as i32; + let inside = py >= m.rect.top && py < m.rect.bottom && px >= m.rect.left && px < m.rect.right; + let mut v = match (&mask, inside) { + (Some(mk), true) => mk[(py - m.rect.top) as usize * mw + (px - m.rect.left) as usize], + _ => def, + }; + if m.inverted { + v = 1.0 - v; + } + out[y * ow + x][3] *= v; + } + } + } + Ok(Pixels { width: ow as u32, height: oh as u32, data: out }) + } +} + +/// The next additional-information block at `*pos` (signature `8BIM`/`8B64`, key, length): +/// returns (key, data start, data end). Skips up to 3 padding bytes before a signature. +fn next_block(data: &[u8], pos: &mut usize, end: usize, psb: bool) -> Option<([u8; 4], usize, usize)> { + let mut p = *pos; + for _ in 0..4 { + if p + 12 > end { + return None; + } + if &data[p..p + 4] == b"8BIM" || &data[p..p + 4] == b"8B64" { + break; + } + p += 1; + } + if p + 12 > end || !(&data[p..p + 4] == b"8BIM" || &data[p..p + 4] == b"8B64") { + return None; + } + let mut key = [0u8; 4]; + key.copy_from_slice(&data[p + 4..p + 8]); + let wide = + psb && matches!(&key, b"LMsk" | b"Lr16" | b"Lr32" | b"Layr" | b"Mt16" | b"Mt32" | b"Mtrn" | b"Alph" | b"FMsk" | b"lnk2" | b"FEid" | b"FXid" | b"PxSD"); + let mut r = Reader::at(&data[..end], p + 8); + let len = r.length(wide).ok()?; + let start = r.pos; + let stop = start.checked_add(len)?; + if stop > end { + return None; + } + *pos = stop; + Some((key, start, stop)) +} + +/// PackBits decoding of one row (appends exactly `row` bytes, zero-padded). +fn unpack_bits(src: &[u8], row: usize, out: &mut Vec) -> Result<()> { + let start = out.len(); + let mut i = 0; + while i < src.len() && out.len() - start < row { + let n = src[i] as i8; + i += 1; + if n >= 0 { + let k = n as usize + 1; + let s = src.get(i..i + k).ok_or(Error::Truncated)?; + out.extend_from_slice(s); + i += k; + } else if n != -128 { + let k = (1 - n as isize) as usize; + let v = *src.get(i).ok_or(Error::Truncated)?; + out.extend(std::iter::repeat_n(v, k)); + i += 1; + } + } + out.resize(start + row, 0); + Ok(()) +} + +/// PackBits encoding of one row. +pub(crate) fn pack_bits(src: &[u8]) -> Vec { + let mut out = vec![]; + let mut i = 0; + while i < src.len() { + // run of identical bytes + let mut run = 1; + while i + run < src.len() && src[i + run] == src[i] && run < 128 { + run += 1; + } + if run >= 2 { + out.push((1 - run as isize) as i8 as u8); + out.push(src[i]); + i += run; + continue; + } + // literal run + let start = i; + let mut n = 0; + while i < src.len() && n < 128 { + if i + 1 < src.len() && src[i + 1] == src[i] { + break; + } + i += 1; + n += 1; + } + if n == 0 { + i += 1; + n = 1; + } + out.push((n - 1) as u8); + out.extend_from_slice(&src[start..start + n]); + } + out +} + +fn srgb_encode(v: f32) -> f32 { + if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } +} + +/// CIE L*a*b* (D50) to sRGB (D65 via the Bradford-adapted matrix), clamped. +fn lab_to_rgb(l: f32, a: f32, b: f32) -> [f32; 3] { + let fy = (l + 16.0) / 116.0; + let fx = fy + a / 500.0; + let fz = fy - b / 200.0; + let f = |t: f32| if t > 6.0 / 29.0 { t * t * t } else { 3.0 * (6.0f32 / 29.0).powi(2) * (t - 4.0 / 29.0) }; + let (x, y, z) = (0.964_22 * f(fx), f(fy), 0.825_21 * f(fz)); + // XYZ (D50) → linear sRGB. + let r = 3.133_856 * x - 1.616_867 * y - 0.490_615 * z; + let g = -0.978_768 * x + 1.916_142 * y + 0.033_454 * z; + let bb = 0.071_945 * x - 0.228_991 * y + 1.405_243 * z; + [r, g, bb].map(|v| srgb_encode(v.clamp(0.0, 1.0))) +} + +#[cfg(test)] +mod tests; diff --git a/crates/psd/src/read.rs b/crates/psd/src/read.rs new file mode 100644 index 00000000..97651b2b --- /dev/null +++ b/crates/psd/src/read.rs @@ -0,0 +1,86 @@ +//! Big-endian byte reader. + +use crate::{Error, Result}; + +#[derive(Clone)] +pub struct Reader<'a> { + pub data: &'a [u8], + pub pos: usize, +} + +impl<'a> Reader<'a> { + pub fn new(data: &'a [u8]) -> Self { + Reader { data, pos: 0 } + } + pub fn at(data: &'a [u8], pos: usize) -> Self { + Reader { data, pos } + } + pub fn remaining(&self) -> usize { + self.data.len().saturating_sub(self.pos) + } + pub fn bytes(&mut self, n: usize) -> Result<&'a [u8]> { + if self.remaining() < n { + return Err(Error::Truncated); + } + let s = &self.data[self.pos..self.pos + n]; + self.pos += n; + Ok(s) + } + pub fn skip(&mut self, n: usize) -> Result<()> { + self.bytes(n).map(|_| ()) + } + pub fn u8(&mut self) -> Result { + Ok(self.bytes(1)?[0]) + } + pub fn u16(&mut self) -> Result { + let b = self.bytes(2)?; + Ok(u16::from_be_bytes([b[0], b[1]])) + } + pub fn i16(&mut self) -> Result { + Ok(self.u16()? as i16) + } + pub fn u32(&mut self) -> Result { + let b = self.bytes(4)?; + Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) + } + pub fn i32(&mut self) -> Result { + Ok(self.u32()? as i32) + } + pub fn u64(&mut self) -> Result { + let b = self.bytes(8)?; + let mut a = [0u8; 8]; + a.copy_from_slice(b); + Ok(u64::from_be_bytes(a)) + } + pub fn f32(&mut self) -> Result { + Ok(f32::from_bits(self.u32()?)) + } + pub fn f64(&mut self) -> Result { + Ok(f64::from_bits(self.u64()?)) + } + pub fn tag(&mut self) -> Result<[u8; 4]> { + let b = self.bytes(4)?; + Ok([b[0], b[1], b[2], b[3]]) + } + /// A length field: 4 bytes in PSD, 8 bytes in PSB (`wide`). + pub fn length(&mut self, wide: bool) -> Result { + let v = if wide { self.u64()? } else { self.u32()? as u64 }; + usize::try_from(v).map_err(|_| Error::Invalid("length".into())) + } + /// A Pascal string padded so that length byte + text is a multiple of `pad`. + pub fn pascal(&mut self, pad: usize) -> Result { + let n = self.u8()? as usize; + let s = self.bytes(n)?; + let total = 1 + n; + let padded = total.div_ceil(pad) * pad; + self.skip(padded - total)?; + // Pascal strings are in the system (Mac Roman) encoding; ASCII covers the common case. + Ok(s.iter().map(|&b| if b < 0x80 { b as char } else { mac_roman(b) }).collect()) + } +} + +/// Mac OS Roman upper half (0x80..=0xFF), as published by the Unicode Consortium mapping table. +fn mac_roman(b: u8) -> char { + const T: &str = "ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈∆«»… ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔ\u{F8FF}ÒÚÛÙıˆ˜¯˘˙˚¸˝˛ˇ"; + T.chars().nth((b - 0x80) as usize).unwrap_or('?') +} diff --git a/crates/psd/src/tests.rs b/crates/psd/src/tests.rs new file mode 100644 index 00000000..68680fe7 --- /dev/null +++ b/crates/psd/src/tests.rs @@ -0,0 +1,189 @@ +use super::write::*; +use super::*; + +fn gradient(w: u32, h: u32) -> Vec<[f32; 4]> { + (0..w * h).map(|i| [(i % w) as f32 / (w - 1).max(1) as f32, (i / w) as f32 / (h - 1).max(1) as f32, 0.25, 1.0]).collect() +} + +fn close(a: &[[f32; 4]], b: &[[f32; 4]], tol: f32) -> bool { + a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (0..4).all(|c| (x[c] - y[c]).abs() <= tol)) +} + +fn doc_with_layers(depth: u16, mode: ColorMode, rle: bool) -> WDoc { + let mut d = WDoc::new(40, 30); + d.depth = depth; + d.mode = mode; + d.rle = rle; + let mut bg = WLayer::pixels("Background", Rect::new(0, 0, 40, 30), gradient(40, 30)); + bg.opacity = 255; + let mut red = WLayer::solid("Red box", Rect::new(5, 6, 10, 8), [1.0, 0.0, 0.0, 1.0]).with_blend(b"mul ").with_opacity(128); + red.hidden = true; + let mut masked = WLayer::solid("Masked", Rect::new(20, 10, 12, 12), [0.0, 0.5, 1.0, 1.0]).with_blend(b"scrn"); + // Mask: left half opaque, right half transparent; default (outside) black. + let mrect = Rect::new(20, 10, 12, 12); + masked.mask = Some(WMask { rect: mrect, data: (0..144).map(|i| if i % 12 < 6 { 1.0 } else { 0.0 }).collect(), default_color: 0, disabled: false }); + d.layers = vec![bg, red, masked]; + d +} + +#[test] +fn layers_round_trip_8bit_rle() { + let bytes = write(&doc_with_layers(8, ColorMode::Rgb, true)); + assert!(is_psd(&bytes)); + let p = Psd::parse(bytes).unwrap(); + assert_eq!((p.width, p.height, p.depth, p.mode), (40, 30, 8, ColorMode::Rgb)); + assert_eq!(p.layers.len(), 3); + let names: Vec<&str> = p.layers.iter().map(|l| l.name.as_str()).collect(); + assert_eq!(names, ["Background", "Red box", "Masked"]); + assert_eq!(p.layers[1].blend_key(), "mul "); + assert_eq!(p.layers[1].opacity, 128); + assert!(p.layers[1].hidden && !p.layers[0].hidden); + assert_eq!(p.layers[2].blend_key(), "scrn"); + assert_eq!(p.layers[1].rect, Rect::new(5, 6, 10, 8)); + // Pixels (8-bit quantisation). + let bg = p.layer_pixels(0, false).unwrap(); + assert!(close(&bg.data, &gradient(40, 30), 1.0 / 255.0)); + // Retain layer size vs canvas. + let red = p.layer_pixels(1, false).unwrap(); + assert_eq!((red.width, red.height), (10, 8)); + let red_c = p.layer_pixels(1, true).unwrap(); + assert_eq!((red_c.width, red_c.height), (40, 30)); + assert_eq!(red_c.data[6 * 40 + 5], [1.0, 0.0, 0.0, 1.0]); + assert_eq!(red_c.data[0][3], 0.0); + // Layer mask becomes alpha. + let m = p.layer_pixels(2, false).unwrap(); + assert_eq!(m.data[0][3], 1.0); + assert_eq!(m.data[11][3], 0.0); + // Merged composite. + let c = p.composite().unwrap(); + assert_eq!((c.width, c.height), (40, 30)); + assert!(p.merged_alpha); +} + +#[test] +fn layers_round_trip_16bit_raw_and_cmyk_gray() { + for (depth, mode, rle) in + [(16, ColorMode::Rgb, false), (16, ColorMode::Rgb, true), (8, ColorMode::Cmyk, true), (16, ColorMode::Cmyk, false), (8, ColorMode::Grayscale, false)] + { + let bytes = write(&doc_with_layers(depth, mode, rle)); + let p = Psd::parse(bytes).unwrap_or_else(|e| panic!("{depth} {mode:?} {rle}: {e}")); + assert_eq!(p.layers.len(), 3, "{depth} {mode:?}"); + assert_eq!(p.depth, depth); + let bg = p.layer_pixels(0, true).unwrap(); + let tol = if depth == 16 { 1.0 / 30000.0 } else { 1.5 / 255.0 }; + let want: Vec<[f32; 4]> = + if mode == ColorMode::Grayscale { gradient(40, 30).iter().map(|p| [p[0], p[0], p[0], 1.0]).collect() } else { gradient(40, 30) }; + assert!(close(&bg.data, &want, tol), "{depth} {mode:?} {rle}: {:?} vs {:?}", bg.data[41], want[41]); + let c = p.composite().unwrap(); + assert_eq!(c.data.len(), 1200); + } +} + +#[test] +fn groups_build_a_tree() { + let mut d = WDoc::new(16, 16); + d.layers = vec![ + WLayer::solid("Bottom", Rect::new(0, 0, 16, 16), [1.0; 4]), + WLayer::group_end(), + WLayer::solid("Inner A", Rect::new(0, 0, 4, 4), [1.0, 0.0, 0.0, 1.0]), + WLayer::group_end(), + WLayer::solid("Deep", Rect::new(2, 2, 4, 4), [0.0, 1.0, 0.0, 1.0]), + WLayer::group("Nested", false, *b"norm"), + WLayer::group("Group 1", true, *b"pass"), + WLayer::solid("Top", Rect::new(8, 8, 4, 4), [0.0, 0.0, 1.0, 1.0]), + ]; + let p = Psd::parse(write(&d)).unwrap(); + assert_eq!(p.layers[6].section, Section::OpenFolder); + assert_eq!(p.layers[6].blend_key(), "pass"); + assert_eq!(p.layers[5].section, Section::ClosedFolder); + let t = p.tree(); + assert_eq!( + t, + vec![ + Node::Layer(7), + Node::Group { layer: 6, children: vec![Node::Group { layer: 5, children: vec![Node::Layer(4)] }, Node::Layer(2)] }, + Node::Layer(0) + ] + ); +} + +#[test] +fn text_effects_fill_and_adjustments() { + let mut d = WDoc::new(64, 32); + let fx = Descriptor::new("null").with("masterFXSwitch", DValue::Bool(true)).with( + "DrSh", + DValue::Descriptor( + Descriptor::new("DrSh") + .with("enab", DValue::Bool(true)) + .with("Md ", DValue::Enum("BlnM".into(), "Mltp".into())) + .with("Clr ", DValue::Descriptor(Descriptor::rgb([0.0, 0.0, 1.0]))) + .with("Opct", DValue::UnitFloat("#Prc".into(), 60.0)) + .with("Dstn", DValue::UnitFloat("#Pxl".into(), 7.0)), + ), + ); + d.layers = vec![ + WLayer::empty("Fill", vec![solid_color_block([0.0, 1.0, 0.0])]), + WLayer::empty("Title", vec![text_block("Hello\nWorld", [10.0, 20.0], "Inter-Bold", 18.0, [1.0, 0.0, 0.0], 2)]).with_block(effects_block(&fx)), + WLayer::empty("Hue", vec![hue_sat_block(30, -20, 10)]), + WLayer::empty("Lev", vec![levels_block(10, 240, 5, 250, 1.2)]), + WLayer::empty("Inv", vec![invert_block()]), + WLayer::empty("BC", vec![brightness_block(20, -10)]), + WLayer::empty("Exp", vec![exposure_block(1.5, 0.0, 1.0)]), + WLayer::solid("Vec", Rect::new(0, 0, 64, 32), [1.0; 4]).with_block(vector_mask_block( + &[vec![[[8.0, 4.0]; 3], [[40.0, 4.0]; 3], [[40.0, 20.0]; 3]]], + 64, + 32, + false, + )), + ]; + let p = Psd::parse(write(&d)).unwrap(); + assert_eq!(p.layers[0].fill, Some([0.0, 1.0, 0.0])); + let fill = p.layer_pixels(0, false).unwrap(); + assert_eq!((fill.width, fill.height), (64, 32)); + assert_eq!(fill.data[5], [0.0, 1.0, 0.0, 1.0]); + let t = p.layers[1].text.as_ref().unwrap(); + assert_eq!(t.text, "Hello\nWorld"); + assert_eq!(t.transform[4..], [10.0, 20.0]); + assert_eq!(t.style.font.as_deref(), Some("Inter-Bold")); + assert_eq!(t.style.size, Some(18.0)); + assert_eq!(t.style.color, Some([1.0, 0.0, 0.0])); + assert_eq!(t.style.justification, Some(2)); + let e = p.layers[1].effects.as_ref().unwrap(); + let ds = e.obj("DrSh").unwrap(); + assert_eq!(ds.enum_value("Md "), Some("Mltp")); + assert_eq!(ds.num("Dstn"), Some(7.0)); + assert_eq!(p.layers[2].adjustment, Some(Adjustment::HueSaturation { hue: 30.0, saturation: -20.0, lightness: 10.0, colorize: false })); + assert_eq!(p.layers[3].adjustment, Some(Adjustment::Levels { in_black: 10.0, in_white: 240.0, gamma: 1.2, out_black: 5.0, out_white: 250.0 })); + assert_eq!(p.layers[4].adjustment, Some(Adjustment::Invert)); + assert_eq!(p.layers[5].adjustment, Some(Adjustment::BrightnessContrast { brightness: 20.0, contrast: -10.0, legacy: true })); + assert_eq!(p.layers[6].adjustment, Some(Adjustment::Exposure { exposure: 1.5, offset: 0.0, gamma: 1.0 })); + let vm = p.layers[7].vector_mask.as_ref().unwrap(); + assert_eq!(vm.subpaths.len(), 1); + assert!(vm.subpaths[0].closed); + let k = &vm.subpaths[0].knots; + assert_eq!(k.len(), 3); + assert!((k[1][1][0] - 40.0).abs() < 1e-4 && (k[2][1][1] - 20.0).abs() < 1e-4); +} + +#[test] +fn packbits_round_trip() { + for src in [vec![], vec![1u8], vec![5; 300], (0..=255).collect::>(), vec![1, 1, 2, 3, 3, 3, 4, 5, 5]] { + let enc = pack_bits(&src); + let mut out = vec![]; + unpack_bits(&enc, src.len(), &mut out).unwrap(); + assert_eq!(out, src); + } +} + +#[test] +fn truncated_and_garbage_input_errors() { + let bytes = write(&doc_with_layers(8, ColorMode::Rgb, true)); + for n in [0, 3, 10, 26, 40, 100, bytes.len() / 2] { + let r = Psd::parse(bytes[..n].to_vec()); + if let Ok(p) = r { + let _ = p.composite(); + } + } + assert!(Psd::parse(b"8BPS\0\x09garbage".to_vec()).is_err()); + assert!(Psd::parse(b"PNG".to_vec()).is_err()); +} diff --git a/crates/psd/src/write.rs b/crates/psd/src/write.rs new file mode 100644 index 00000000..fb78cc78 --- /dev/null +++ b/crates/psd/src/write.rs @@ -0,0 +1,491 @@ +//! A minimal Photoshop document writer, per the same specification as the reader: RGB, CMYK or +//! Grayscale, 8 or 16 bits, raw or RLE channels, pixel layers with layer masks, groups and any +//! additional-information blocks (helpers below build text, effects, fill and adjustment +//! blocks). Used to generate test fixtures; not a full exporter. + +use crate::descriptor::{self, DValue, Descriptor}; +use crate::{ColorMode, Rect, Section, engine_data, pack_bits}; + +/// A layer mask for [`WLayer`]. +#[derive(Clone, Debug)] +pub struct WMask { + pub rect: Rect, + /// `rect.width() * rect.height()` values in 0..1. + pub data: Vec, + pub default_color: u8, + pub disabled: bool, +} + +/// A layer to write. +#[derive(Clone, Debug)] +pub struct WLayer { + pub name: String, + pub rect: Rect, + /// Straight RGBA, `rect.width() * rect.height()` pixels. + pub pixels: Vec<[f32; 4]>, + pub blend: [u8; 4], + pub opacity: u8, + pub hidden: bool, + pub clipping: bool, + pub mask: Option, + pub section: Section, + /// Extra additional-information blocks (key, data). + pub blocks: Vec<([u8; 4], Vec)>, +} + +impl WLayer { + /// A pixel layer. + pub fn pixels(name: &str, rect: Rect, pixels: Vec<[f32; 4]>) -> WLayer { + WLayer { + name: name.into(), + rect, + pixels, + blend: *b"norm", + opacity: 255, + hidden: false, + clipping: false, + mask: None, + section: Section::Layer, + blocks: vec![], + } + } + /// A layer filled with one colour. + pub fn solid(name: &str, rect: Rect, rgba: [f32; 4]) -> WLayer { + let n = (rect.width() * rect.height()) as usize; + WLayer::pixels(name, rect, vec![rgba; n]) + } + /// The top record of a group (its children come *before* it in file order, after a + /// [`WLayer::group_end`]). + pub fn group(name: &str, open: bool, blend: [u8; 4]) -> WLayer { + let mut l = WLayer::pixels(name, Rect::default(), vec![]); + l.section = if open { Section::OpenFolder } else { Section::ClosedFolder }; + l.blend = blend; + l + } + /// The hidden record that closes a group (written below its children). + pub fn group_end() -> WLayer { + let mut l = WLayer::pixels("", Rect::default(), vec![]); + l.section = Section::Divider; + l + } + /// A layer without pixels carrying `blocks` (adjustment and fill layers, type layers). + pub fn empty(name: &str, blocks: Vec<([u8; 4], Vec)>) -> WLayer { + let mut l = WLayer::pixels(name, Rect::default(), vec![]); + l.blocks = blocks; + l + } + pub fn with_blend(mut self, key: &[u8; 4]) -> WLayer { + self.blend = *key; + self + } + pub fn with_opacity(mut self, o: u8) -> WLayer { + self.opacity = o; + self + } + pub fn with_block(mut self, b: ([u8; 4], Vec)) -> WLayer { + self.blocks.push(b); + self + } +} + +/// A document to write. +#[derive(Clone, Debug)] +pub struct WDoc { + pub width: u32, + pub height: u32, + /// 8 or 16. + pub depth: u16, + /// Rgb, Cmyk or Grayscale. + pub mode: ColorMode, + pub rle: bool, + /// Bottom of the stack first (file order). + pub layers: Vec, + /// Merged image (straight RGBA, document-sized); `None` composites visible pixel layers + /// with Normal blending. + pub composite: Option>, +} + +impl WDoc { + pub fn new(width: u32, height: u32) -> WDoc { + WDoc { width, height, depth: 8, mode: ColorMode::Rgb, rle: true, layers: vec![], composite: None } + } +} + +fn u16b(out: &mut Vec, v: u16) { + out.extend_from_slice(&v.to_be_bytes()); +} +fn u32b(out: &mut Vec, v: u32) { + out.extend_from_slice(&v.to_be_bytes()); +} + +/// Colour channels for a straight RGB pixel in the document mode. +fn mode_channels(mode: ColorMode, p: [f32; 4]) -> Vec { + match mode { + ColorMode::Cmyk => { + // Stored values are inverted ink amounts: k = max, c = r / max (reader: r = c * k). + let m = p[0].max(p[1]).max(p[2]); + if m <= 0.0 { vec![0.0, 0.0, 0.0, 0.0] } else { vec![p[0] / m, p[1] / m, p[2] / m, m] } + } + ColorMode::Grayscale => vec![p[0]], + _ => vec![p[0], p[1], p[2]], + } +} + +fn encode_samples(v: &[f32], depth: u16) -> Vec { + let mut out = Vec::with_capacity(v.len() * if depth == 16 { 2 } else { 1 }); + for &x in v { + let x = x.clamp(0.0, 1.0); + if depth == 16 { + u16b(&mut out, (x * 65535.0).round() as u16); + } else { + out.push((x * 255.0).round() as u8); + } + } + out +} + +/// One channel's data (compression field + data) for a `w`×`h` plane. +fn channel_data(v: &[f32], w: usize, h: usize, depth: u16, rle: bool) -> Vec { + let raw = encode_samples(v, depth); + let mut out = vec![]; + let rowb = raw.len() / h.max(1); + if !rle || w == 0 || h == 0 { + u16b(&mut out, 0); + out.extend_from_slice(&raw); + return out; + } + u16b(&mut out, 1); + let rows: Vec> = raw.chunks(rowb.max(1)).map(pack_bits).collect(); + for r in &rows { + u16b(&mut out, r.len() as u16); + } + for r in rows { + out.extend_from_slice(&r); + } + out +} + +fn pascal4(out: &mut Vec, s: &str) { + let b: Vec = s.bytes().map(|c| if c < 0x80 { c } else { b'?' }).take(255).collect(); + out.push(b.len() as u8); + out.extend_from_slice(&b); + let total = 1 + b.len(); + out.resize(out.len() + total.div_ceil(4) * 4 - total, 0); +} + +fn block(out: &mut Vec, key: &[u8; 4], data: &[u8]) { + out.extend_from_slice(b"8BIM"); + out.extend_from_slice(key); + let padded = data.len().div_ceil(2) * 2; + u32b(out, padded as u32); + out.extend_from_slice(data); + out.resize(out.len() + padded - data.len(), 0); +} + +fn layer_info(doc: &WDoc) -> Vec { + let mut out = vec![]; + out.extend_from_slice(&(-(doc.layers.len() as i16)).to_be_bytes()); + let mut chan_data: Vec)>> = vec![]; + for l in &doc.layers { + let (w, h) = (l.rect.width() as usize, l.rect.height() as usize); + let mut chans: Vec<(i16, Vec)> = vec![]; + let n = w * h; + let alpha: Vec = l.pixels.iter().take(n).map(|p| p[3]).collect(); + chans.push((-1, channel_data(&alpha, w, h, doc.depth, doc.rle))); + let cc = doc.mode.color_channels(); + let planes: Vec> = l.pixels.iter().take(n).map(|p| mode_channels(doc.mode, *p)).collect(); + for c in 0..cc { + let plane: Vec = planes.iter().map(|v| v[c]).collect(); + chans.push((c as i16, channel_data(&plane, w, h, doc.depth, doc.rle))); + } + if let Some(m) = &l.mask { + chans.push((-2, channel_data(&m.data, m.rect.width() as usize, m.rect.height() as usize, doc.depth, doc.rle))); + } + // record + for v in [l.rect.top, l.rect.left, l.rect.bottom, l.rect.right] { + out.extend_from_slice(&v.to_be_bytes()); + } + u16b(&mut out, chans.len() as u16); + for (id, d) in &chans { + out.extend_from_slice(&id.to_be_bytes()); + u32b(&mut out, d.len() as u32); + } + out.extend_from_slice(b"8BIM"); + out.extend_from_slice(&l.blend); + out.push(l.opacity); + out.push(l.clipping as u8); + let mut flags = 8u8; + if l.hidden { + flags |= 2; + } + out.push(flags); + out.push(0); + let mut extra = vec![]; + match &l.mask { + Some(m) => { + u32b(&mut extra, 20); + for v in [m.rect.top, m.rect.left, m.rect.bottom, m.rect.right] { + extra.extend_from_slice(&v.to_be_bytes()); + } + extra.push(m.default_color); + extra.push(if m.disabled { 2 } else { 0 }); + extra.extend_from_slice(&[0, 0]); + } + None => u32b(&mut extra, 0), + } + u32b(&mut extra, 0); // blending ranges + pascal4(&mut extra, &l.name); + let luni = unicode_plain(&l.name); + block(&mut extra, b"luni", &luni); + match l.section { + Section::Layer => {} + s => { + let mut d = vec![]; + u32b( + &mut d, + match s { + Section::OpenFolder => 1, + Section::ClosedFolder => 2, + _ => 3, + }, + ); + d.extend_from_slice(b"8BIM"); + d.extend_from_slice(&l.blend); + block(&mut extra, b"lsct", &d); + } + } + for (k, d) in &l.blocks { + block(&mut extra, k, d); + } + u32b(&mut out, extra.len() as u32); + out.extend_from_slice(&extra); + chan_data.push(chans); + } + for chans in chan_data { + for (_, d) in chans { + out.extend_from_slice(&d); + } + } + if out.len() % 2 == 1 { + out.push(0); + } + out +} + +fn unicode_plain(s: &str) -> Vec { + let v: Vec = s.encode_utf16().collect(); + let mut out = vec![]; + u32b(&mut out, v.len() as u32); + for c in v { + u16b(&mut out, c); + } + out +} + +/// Normal "over" of the visible pixel layers (groups ignored), straight alpha. +fn flatten(doc: &WDoc) -> Vec<[f32; 4]> { + let (w, h) = (doc.width as i32, doc.height as i32); + let mut acc = vec![[0.0f32; 4]; (w * h) as usize]; + for l in doc.layers.iter().filter(|l| !l.hidden && l.section == Section::Layer) { + let lw = l.rect.width() as i32; + for (i, p) in l.pixels.iter().enumerate() { + let (x, y) = (l.rect.left + i as i32 % lw.max(1), l.rect.top + i as i32 / lw.max(1)); + if x < 0 || y < 0 || x >= w || y >= h { + continue; + } + let a = p[3] * l.opacity as f32 / 255.0; + let d = &mut acc[(y * w + x) as usize]; + let oa = a + d[3] * (1.0 - a); + for c in 0..3 { + d[c] = if oa > 0.0 { (p[c] * a + d[c] * d[3] * (1.0 - a)) / oa } else { 0.0 }; + } + d[3] = oa; + } + } + acc +} + +/// Serialise a document. +pub fn write(doc: &WDoc) -> Vec { + let cc = doc.mode.color_channels(); + let mut out = vec![]; + out.extend_from_slice(b"8BPS"); + u16b(&mut out, 1); + out.extend_from_slice(&[0; 6]); + u16b(&mut out, (cc + 1) as u16); + u32b(&mut out, doc.height); + u32b(&mut out, doc.width); + u16b(&mut out, doc.depth); + u16b(&mut out, doc.mode.to_u16()); + u32b(&mut out, 0); // colour mode data + u32b(&mut out, 0); // image resources + // Layer and mask information. + let mut lm = vec![]; + if !doc.layers.is_empty() { + let li = layer_info(doc); + if doc.depth == 16 { + u32b(&mut lm, 0); + u32b(&mut lm, 0); // global layer mask info + let mut padded = li; + while !padded.len().is_multiple_of(4) { + padded.push(0); + } + lm.extend_from_slice(b"8BIM"); + lm.extend_from_slice(b"Lr16"); + u32b(&mut lm, padded.len() as u32); + lm.extend_from_slice(&padded); + } else { + u32b(&mut lm, li.len() as u32); + lm.extend_from_slice(&li); + u32b(&mut lm, 0); + } + } + u32b(&mut out, lm.len() as u32); + out.extend_from_slice(&lm); + // Merged image. + let comp = doc.composite.clone().unwrap_or_else(|| flatten(doc)); + let (w, h) = (doc.width as usize, doc.height as usize); + let planes: Vec> = comp.iter().map(|p| mode_channels(doc.mode, *p)).collect(); + let mut chans: Vec> = (0..cc).map(|c| planes.iter().map(|v| v[c]).collect()).collect(); + chans.push(comp.iter().map(|p| p[3]).collect()); + if doc.rle { + u16b(&mut out, 1); + let rows: Vec>> = + chans.iter().map(|c| encode_samples(c, doc.depth).chunks((w * if doc.depth == 16 { 2 } else { 1 }).max(1)).map(pack_bits).collect()).collect(); + for c in &rows { + for r in c { + u16b(&mut out, r.len() as u16); + } + } + for c in rows { + for r in c { + out.extend_from_slice(&r); + } + } + } else { + u16b(&mut out, 0); + for c in &chans { + out.extend_from_slice(&encode_samples(c, doc.depth)); + } + } + let _ = h; + out +} + +// ---------------------------------------------------------------- block helpers + +/// `TySh`: a type layer with one style run; the text origin sits at `origin`. +pub fn text_block(text: &str, origin: [f64; 2], font: &str, size: f64, color: [f64; 3], justification: u8) -> ([u8; 4], Vec) { + let mut d = vec![]; + u16b(&mut d, 1); + for v in [1.0, 0.0, 0.0, 1.0, origin[0], origin[1]] { + d.extend_from_slice(&f64::to_be_bytes(v)); + } + u16b(&mut d, 50); + u32b(&mut d, 16); + let desc = Descriptor::new("TxLr") + .with("Txt ", DValue::Text(text.replace('\n', "\r"))) + .with("textGridding", DValue::Enum("textGridding".into(), "None".into())) + .with("Ornt", DValue::Enum("Ornt".into(), "Hrzn".into())) + .with("EngineData", DValue::Raw(engine_data::minimal(text, font, size, color, justification))); + descriptor::write_descriptor(&mut d, &desc); + // Warp: version, descriptor version, an empty warp descriptor. + u16b(&mut d, 1); + u32b(&mut d, 16); + descriptor::write_descriptor(&mut d, &Descriptor::new("warp").with("warpStyle", DValue::Enum("warpStyle".into(), "warpNone".into()))); + for _ in 0..4 { + u32b(&mut d, 0); + } + (*b"TySh", d) +} + +/// `lfx2`: layer effects from an effects descriptor (`null` class with `DrSh`, `FrFX`…). +pub fn effects_block(desc: &Descriptor) -> ([u8; 4], Vec) { + let mut d = vec![]; + u32b(&mut d, 0); + u32b(&mut d, 16); + descriptor::write_descriptor(&mut d, desc); + (*b"lfx2", d) +} + +/// `SoCo`: solid colour fill. +pub fn solid_color_block(rgb: [f64; 3]) -> ([u8; 4], Vec) { + let mut d = vec![]; + u32b(&mut d, 16); + descriptor::write_descriptor(&mut d, &Descriptor::new("null").with("Clr ", DValue::Descriptor(Descriptor::rgb(rgb)))); + (*b"SoCo", d) +} + +/// `hue2`: Hue/Saturation (master). +pub fn hue_sat_block(hue: i16, sat: i16, light: i16) -> ([u8; 4], Vec) { + let mut d = vec![]; + u16b(&mut d, 2); + d.extend_from_slice(&[0, 0]); + for v in [0i16, 0, 0, hue, sat, light] { + d.extend_from_slice(&v.to_be_bytes()); + } + // Six colour ranges (unused). + d.resize(d.len() + 6 * 24, 0); + (*b"hue2", d) +} + +/// `levl`: Levels (composite record). +pub fn levels_block(in_black: i16, in_white: i16, out_black: i16, out_white: i16, gamma: f64) -> ([u8; 4], Vec) { + let mut d = vec![]; + u16b(&mut d, 2); + for v in [in_black, in_white, out_black, out_white, (gamma * 100.0).round() as i16] { + d.extend_from_slice(&v.to_be_bytes()); + } + for _ in 0..28 { + for v in [0i16, 255, 0, 255, 100] { + d.extend_from_slice(&v.to_be_bytes()); + } + } + (*b"levl", d) +} + +pub fn invert_block() -> ([u8; 4], Vec) { + (*b"nvrt", vec![]) +} + +pub fn brightness_block(brightness: i16, contrast: i16) -> ([u8; 4], Vec) { + let mut d = vec![]; + for v in [brightness, contrast, 127] { + d.extend_from_slice(&v.to_be_bytes()); + } + d.push(0); + (*b"brit", d) +} + +pub fn exposure_block(exposure: f32, offset: f32, gamma: f32) -> ([u8; 4], Vec) { + let mut d = vec![]; + u16b(&mut d, 1); + for v in [exposure, offset, gamma] { + d.extend_from_slice(&v.to_be_bytes()); + } + (*b"expA", d) +} + +/// `vmsk`: a vector mask from closed polygons/curves given as (in, anchor, out) knots in pixels. +pub fn vector_mask_block(subpaths: &[Vec<[[f64; 2]; 3]>], width: u32, height: u32, inverted: bool) -> ([u8; 4], Vec) { + let mut d = vec![]; + u32b(&mut d, 3); + u32b(&mut d, inverted as u32); + // Path fill rule record. + u16b(&mut d, 6); + d.resize(d.len() + 24, 0); + let fx = |v: f64| ((v * (1 << 24) as f64).round() as i32).to_be_bytes(); + for sp in subpaths { + u16b(&mut d, 0); + u16b(&mut d, sp.len() as u16); + d.resize(d.len() + 22, 0); + for k in sp { + u16b(&mut d, 1); + for p in k { + d.extend_from_slice(&fx(p[1] / height as f64)); + d.extend_from_slice(&fx(p[0] / width as f64)); + } + } + } + (*b"vmsk", d) +} diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml index 4c8a9787..cdaa85c1 100644 --- a/crates/render/Cargo.toml +++ b/crates/render/Cargo.toml @@ -19,6 +19,7 @@ effectcraft-raster = { workspace = true } effectcraft-path = { workspace = true } effectcraft-text = { workspace = true } effectcraft-effects = { workspace = true } +lz4_flex = { version = "0.11", default-features = false, features = ["std", "safe-encode", "safe-decode", "checked-decode"] } kurbo = { workspace = true } rayon = { workspace = true } web-time = { workspace = true } diff --git a/crates/render/src/audio.rs b/crates/render/src/audio.rs index 4884411f..4b872ad2 100644 --- a/crates/render/src/audio.rs +++ b/crates/render/src/audio.rs @@ -330,6 +330,7 @@ mod tests { missing: false, sequence: vec![], color_profile: None, + layer: None, }; let fid = p.add_item("x.wav", effectcraft_color::Label::SeaFoam, None, ItemKind::Footage(f)); let comp = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index 8cd27d59..23702fd6 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -25,9 +25,13 @@ use effectcraft_project::{ItemKind, Layer, LayerSource, Node, PropGroup}; use crate::eval::EvalCtx; -/// Thread-safe, memory-budgeted cache of processed layer buffers. +/// Thread-safe, memory-budgeted cache of processed layer buffers, optionally backed by the +/// persistent [`DiskCache`](crate::disk_cache::DiskCache): a memory miss reads the disk, and +/// buffers that were slow to render are written there (keys salted per project, see +/// [`LayerCache::set_disk`]). pub struct LayerCache { inner: Mutex, + disk: Mutex, u64)>>, } struct Entry { @@ -43,6 +47,8 @@ struct Inner { clock: u64, hits: u64, misses: u64, + /// When each recent miss happened (to time the render that follows it). + missed_at: HashMap, } /// Hit/miss counters and memory use (for perf readouts and tests). @@ -63,7 +69,37 @@ impl Default for LayerCache { impl LayerCache { /// A cache holding at most `budget` bytes of pixels. pub fn new(budget: usize) -> LayerCache { - LayerCache { inner: Mutex::new(Inner { map: HashMap::new(), bytes: 0, budget, clock: 0, hits: 0, misses: 0 }) } + LayerCache { + inner: Mutex::new(Inner { map: HashMap::new(), bytes: 0, budget, clock: 0, hits: 0, misses: 0, missed_at: HashMap::new() }), + disk: Mutex::new(None), + } + } + + /// Attach (or detach) the disk cache. `salt` separates projects and footage versions (see + /// `footage_salt`); keys on disk are `(salt, key)`. + pub fn set_disk(&self, disk: Option<(Arc, u64)>) { + if let Ok(mut d) = self.disk.lock() { + *d = disk; + } + } + + /// Change the disk salt (after footage changed) keeping the attached disk cache. + pub fn set_disk_salt(&self, salt: u64) { + if let Ok(mut d) = self.disk.lock() + && let Some((_, s)) = d.as_mut() + { + *s = salt; + } + } + + pub fn disk(&self) -> Option> { + self.disk.lock().ok()?.as_ref().map(|d| d.0.clone()) + } + + fn disk_key(&self, key: u64) -> Option<(Arc, u128)> { + let d = self.disk.lock().ok()?; + let (dc, salt) = d.as_ref()?; + Some((dc.clone(), ((*salt as u128) << 64) | key as u128)) } pub fn get(&self, key: u64) -> Option> { @@ -79,12 +115,32 @@ impl LayerCache { } None => { g.misses += 1; - None + if g.missed_at.len() > 4096 { + g.missed_at.clear(); + } + g.missed_at.insert(key, web_time::Instant::now()); + drop(g); + // Disk cache: a hit comes back into memory. + let (dc, dk) = self.disk_key(key)?; + let buf = Arc::new(dc.get_layer(dk)?); + self.insert_mem(key, buf.clone()); + Some(buf) } } } pub fn insert(&self, key: u64, buf: Arc) { + let started = self.inner.lock().ok().and_then(|mut g| g.missed_at.remove(&key)); + if let Some((dc, dk)) = self.disk_key(key) { + let slow = started.is_some_and(|t| t.elapsed().as_secs_f64() * 1e3 >= dc.min_layer_ms() as f64); + if slow { + dc.put_layer(dk, &buf); + } + } + self.insert_mem(key, buf); + } + + fn insert_mem(&self, key: u64, buf: Arc) { let bytes = buf.img.data.len() * std::mem::size_of::(); let Ok(mut g) = self.inner.lock() else { return }; if bytes > g.budget / 4 { diff --git a/crates/render/src/disk_cache.rs b/crates/render/src/disk_cache.rs new file mode 100644 index 00000000..1afc9bbb --- /dev/null +++ b/crates/render/src/disk_cache.rs @@ -0,0 +1,829 @@ +//! The persistent disk cache (Settings ▸ Media & Disk Cache ▸ Disk Cache): RAM-preview frames +//! and layer cache entries kept on disk between sessions, like After Effects' disk cache. +//! +//! * Entries are files keyed by a 128-bit content hash (`/v1//<2 hex>/<32 hex>.ecc`): +//! frames by the hash of the composition's content (the comp and everything it uses, footage +//! files' size and modification time included) plus frame, scale and view; layer buffers by +//! the layer cache's content key salted with the project's footage fingerprint. A changed +//! project therefore never reads stale pixels: its keys simply change. +//! * Payloads are LZ4-compressed (`lz4_flex`, MIT): frames as 8-bit premultiplied RGBA, layer +//! buffers as `f32` RGBA with the bytes shuffled into planes (exact, so cached and fresh +//! renders are bit-identical). Every file ends in a checksum; damaged files read as misses and +//! are deleted. +//! * Size limit with least-recently-used eviction. The in-memory index is rebuilt from the +//! folder when the cache opens (file modification times order it), so the cache survives +//! restarts; reads refresh a file's time. +//! * Safe concurrent access: writes go to a unique temporary file that is renamed into place +//! (atomic on every desktop OS), so readers in this or another process never see a partial +//! entry; a missing or damaged file is just a miss. Writes run on a background thread with a +//! bounded queue (a full queue drops the write rather than stalling rendering). + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Condvar, Mutex}; + +use effectcraft_effects::Buf; +use effectcraft_raster::Image; + +/// File format version (part of the folder layout). +const VERSION: &str = "v1"; +const MAGIC: &[u8; 4] = b"ECDC"; +/// Pending writes before new ones are dropped. +const QUEUE_LIMIT: usize = 64; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Kind { + Frame, + Layer, +} + +impl Kind { + fn dir(self) -> &'static str { + match self { + Kind::Frame => "frames", + Kind::Layer => "layers", + } + } + fn tag(self) -> u8 { + match self { + Kind::Frame => 1, + Kind::Layer => 2, + } + } +} + +/// A cached viewer frame: 8-bit premultiplied RGBA. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Frame8 { + pub width: u32, + pub height: u32, + pub rgba: Vec, +} + +/// Counters for tests, the Info panel and `cache.diskStats`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct DiskStats { + pub entries: usize, + pub bytes: u64, + pub max_bytes: u64, + pub hits: u64, + pub misses: u64, + pub writes: u64, + pub evictions: u64, + pub dropped: u64, +} + +struct Meta { + bytes: u64, + last_use: u64, +} + +#[derive(Default)] +struct Index { + map: HashMap<(Kind, u128), Meta>, + total: u64, + clock: u64, +} + +enum Job { + Write(Kind, u128, Vec), +} + +struct Queue { + jobs: std::collections::VecDeque, + busy: bool, +} + +pub struct DiskCache { + dir: PathBuf, + index: Mutex, + max_bytes: AtomicU64, + hits: AtomicU64, + misses: AtomicU64, + writes: AtomicU64, + evictions: AtomicU64, + dropped: AtomicU64, + tmp_counter: AtomicU64, + queue: Arc<(Mutex, Condvar)>, + /// Layer buffers that took less than this to render are not written (ms). + min_layer_ms: AtomicU64, +} + +/// FNV-1a 128-bit (content keys). +#[derive(Clone, Copy)] +pub struct Hash128(u128); + +impl Default for Hash128 { + fn default() -> Self { + Hash128(0x6c62_272e_07bb_0142_62b8_2175_6295_c58d) + } +} + +impl Hash128 { + pub fn write(&mut self, bytes: &[u8]) { + for &b in bytes { + self.0 ^= b as u128; + self.0 = self.0.wrapping_mul(0x0000_0000_0100_0000_0000_0000_0000_013b); + } + } + pub fn write_u64(&mut self, v: u64) { + self.write(&v.to_le_bytes()); + } + pub fn finish(&self) -> u128 { + self.0 + } +} + +fn checksum(data: &[u8]) -> u64 { + let mut h: u64 = 0xcbf2_9ce4_8422_2325; + for &b in data { + h ^= b as u64; + h = h.wrapping_mul(0x0000_0100_0000_01b3); + } + h +} + +/// The platform's default disk cache folder. +pub fn default_folder() -> PathBuf { + let home = std::env::var_os("HOME").map(PathBuf::from); + if cfg!(target_os = "macos") + && let Some(h) = &home + { + return h.join("Library/Caches/EffectCraft/Disk Cache"); + } + if cfg!(windows) + && let Some(l) = std::env::var_os("LOCALAPPDATA") + { + return PathBuf::from(l).join("EffectCraft").join("Disk Cache"); + } + if let Some(x) = std::env::var_os("XDG_CACHE_HOME") { + return PathBuf::from(x).join("effectcraft/disk-cache"); + } + home.map(|h| h.join(".cache/effectcraft/disk-cache")).unwrap_or_else(|| std::env::temp_dir().join("effectcraft-disk-cache")) +} + +impl DiskCache { + /// Open (or create) the cache in `folder`, holding at most `max_bytes`. + pub fn open(folder: impl AsRef, max_bytes: u64) -> std::io::Result> { + let dir = folder.as_ref().join(VERSION); + std::fs::create_dir_all(&dir)?; + let dc = Arc::new(DiskCache { + dir, + index: Mutex::new(Index::default()), + max_bytes: AtomicU64::new(max_bytes), + hits: AtomicU64::new(0), + misses: AtomicU64::new(0), + writes: AtomicU64::new(0), + evictions: AtomicU64::new(0), + dropped: AtomicU64::new(0), + tmp_counter: AtomicU64::new(0), + queue: Arc::new((Mutex::new(Queue { jobs: Default::default(), busy: false }), Condvar::new())), + min_layer_ms: AtomicU64::new(30), + }); + dc.rescan(); + dc.evict(); + #[cfg(not(target_arch = "wasm32"))] + { + let weak = Arc::downgrade(&dc); + let q = dc.queue.clone(); + std::thread::Builder::new().name("ec-disk-cache".into()).spawn(move || writer(weak, q))?; + } + Ok(dc) + } + + pub fn folder(&self) -> &Path { + self.dir.parent().unwrap_or(&self.dir) + } + + /// Rebuild the index from the folder (oldest files first). + pub fn rescan(&self) { + let mut found: Vec<(std::time::SystemTime, Kind, u128, u64)> = vec![]; + for kind in [Kind::Frame, Kind::Layer] { + let Ok(shards) = std::fs::read_dir(self.dir.join(kind.dir())) else { continue }; + for shard in shards.flatten() { + let Ok(files) = std::fs::read_dir(shard.path()) else { continue }; + for f in files.flatten() { + let p = f.path(); + let name = p.file_name().and_then(|n| n.to_str()).unwrap_or(""); + if name.contains(".tmp") { + // A writer that died mid-write; old temporaries are removed. + if f.metadata().and_then(|m| m.modified()).is_ok_and(|t| t.elapsed().is_ok_and(|e| e.as_secs() > 600)) { + let _ = std::fs::remove_file(&p); + } + continue; + } + let Some(hex) = name.strip_suffix(".ecc") else { continue }; + let Ok(key) = u128::from_str_radix(hex, 16) else { continue }; + let Ok(md) = f.metadata() else { continue }; + found.push((md.modified().unwrap_or(std::time::UNIX_EPOCH), kind, key, md.len())); + } + } + } + found.sort_by_key(|e| e.0); + let mut idx = Index::default(); + for (_, kind, key, bytes) in found { + idx.clock += 1; + let clock = idx.clock; + idx.total += bytes; + idx.map.insert((kind, key), Meta { bytes, last_use: clock }); + } + if let Ok(mut g) = self.index.lock() { + *g = idx; + } + } + + fn path(&self, kind: Kind, key: u128) -> PathBuf { + let hex = format!("{key:032x}"); + self.dir.join(kind.dir()).join(&hex[..2]).join(format!("{hex}.ecc")) + } + + pub fn max_bytes(&self) -> u64 { + self.max_bytes.load(Ordering::Relaxed) + } + + /// Change the size limit (evicts the least recently used entries when shrinking). + pub fn set_max_bytes(&self, b: u64) { + self.max_bytes.store(b, Ordering::Relaxed); + self.evict(); + } + + /// Layer buffers faster to render than this are not worth a disk write. + pub fn set_min_layer_ms(&self, ms: u64) { + self.min_layer_ms.store(ms, Ordering::Relaxed); + } + + pub fn min_layer_ms(&self) -> u64 { + self.min_layer_ms.load(Ordering::Relaxed) + } + + pub fn contains(&self, kind: Kind, key: u128) -> bool { + self.index.lock().is_ok_and(|g| g.map.contains_key(&(kind, key))) + } + + /// Keys of one kind (unordered). + pub fn keys(&self, kind: Kind) -> Vec { + self.index.lock().map(|g| g.map.keys().filter(|k| k.0 == kind).map(|k| k.1).collect()).unwrap_or_default() + } + + pub fn stats(&self) -> DiskStats { + let (entries, bytes) = self.index.lock().map(|g| (g.map.len(), g.total)).unwrap_or_default(); + DiskStats { + entries, + bytes, + max_bytes: self.max_bytes(), + hits: self.hits.load(Ordering::Relaxed), + misses: self.misses.load(Ordering::Relaxed), + writes: self.writes.load(Ordering::Relaxed), + evictions: self.evictions.load(Ordering::Relaxed), + dropped: self.dropped.load(Ordering::Relaxed), + } + } + + /// Delete every entry (Edit ▸ Purge ▸ All Disk Cache / Empty Disk Cache). + pub fn clear(&self) { + self.flush(); + if let Ok(mut g) = self.index.lock() { + *g = Index::default(); + } + for kind in [Kind::Frame, Kind::Layer] { + let _ = std::fs::remove_dir_all(self.dir.join(kind.dir())); + } + } + + /// Wait until queued writes are on disk. + pub fn flush(&self) { + let (m, cv) = &*self.queue; + let Ok(mut q) = m.lock() else { return }; + #[cfg(target_arch = "wasm32")] + { + q.jobs.clear(); + } + while !q.jobs.is_empty() || q.busy { + q = match cv.wait(q) { + Ok(q) => q, + Err(_) => return, + }; + } + } + + fn read(&self, kind: Kind, key: u128) -> Option> { + if !self.contains(kind, key) { + self.misses.fetch_add(1, Ordering::Relaxed); + return None; + } + let path = self.path(kind, key); + let data = std::fs::read(&path).ok(); + let ok = data.as_deref().and_then(|d| verify(d, kind)); + match ok { + Some(payload) => { + let payload = payload.to_vec(); + self.hits.fetch_add(1, Ordering::Relaxed); + if let Ok(mut g) = self.index.lock() { + g.clock += 1; + let c = g.clock; + if let Some(m) = g.map.get_mut(&(kind, key)) { + m.last_use = c; + } + } + // Persist the recency for the next session (best effort). + if let Ok(f) = std::fs::File::options().append(true).open(&path) { + let _ = f.set_modified(std::time::SystemTime::now()); + } + Some(payload) + } + None => { + self.misses.fetch_add(1, Ordering::Relaxed); + // Gone (another process evicted it) or damaged: forget it. + if data.is_some() { + let _ = std::fs::remove_file(&path); + } + self.forget(kind, key); + None + } + } + } + + fn forget(&self, kind: Kind, key: u128) { + if let Ok(mut g) = self.index.lock() + && let Some(m) = g.map.remove(&(kind, key)) + { + g.total -= m.bytes; + } + } + + fn enqueue(&self, kind: Kind, key: u128, payload: Vec) { + #[cfg(target_arch = "wasm32")] + { + self.write_now(kind, key, &payload); + } + #[cfg(not(target_arch = "wasm32"))] + { + let (m, cv) = &*self.queue; + let Ok(mut q) = m.lock() else { return }; + if q.jobs.len() >= QUEUE_LIMIT { + self.dropped.fetch_add(1, Ordering::Relaxed); + return; + } + q.jobs.push_back(Job::Write(kind, key, payload)); + cv.notify_all(); + } + } + + /// Write an entry now (atomic rename), then evict down to the limit. + fn write_now(&self, kind: Kind, key: u128, payload: &[u8]) { + if self.contains(kind, key) { + return; + } + let path = self.path(kind, key); + let Some(parent) = path.parent() else { return }; + if std::fs::create_dir_all(parent).is_err() { + return; + } + let mut data = Vec::with_capacity(payload.len() + 16); + data.extend_from_slice(MAGIC); + data.push(kind.tag()); + data.extend_from_slice(&[0, 0, 0]); + data.extend_from_slice(payload); + let sum = checksum(&data); + data.extend_from_slice(&sum.to_le_bytes()); + let n = self.tmp_counter.fetch_add(1, Ordering::Relaxed); + let tmp = parent.join(format!("{key:032x}.tmp.{}.{n}", std::process::id())); + if std::fs::write(&tmp, &data).is_err() { + let _ = std::fs::remove_file(&tmp); + return; + } + if std::fs::rename(&tmp, &path).is_err() { + let _ = std::fs::remove_file(&tmp); + return; + } + self.writes.fetch_add(1, Ordering::Relaxed); + if let Ok(mut g) = self.index.lock() { + g.clock += 1; + let c = g.clock; + let bytes = data.len() as u64; + if let Some(old) = g.map.insert((kind, key), Meta { bytes, last_use: c }) { + g.total -= old.bytes; + } + g.total += bytes; + } + self.evict(); + } + + /// Remove least recently used entries until the cache fits its limit. + fn evict(&self) { + let max = self.max_bytes(); + let victims: Vec<(Kind, u128)> = { + let Ok(mut g) = self.index.lock() else { return }; + if g.total <= max { + return; + } + let mut by_age: Vec<(u64, (Kind, u128), u64)> = g.map.iter().map(|(k, m)| (m.last_use, *k, m.bytes)).collect(); + by_age.sort_unstable_by_key(|e| e.0); + let mut out = vec![]; + for (_, k, bytes) in by_age { + if g.total <= max { + break; + } + g.map.remove(&k); + g.total -= bytes; + out.push(k); + } + out + }; + for (kind, key) in victims { + let _ = std::fs::remove_file(self.path(kind, key)); + self.evictions.fetch_add(1, Ordering::Relaxed); + } + } + + // ------------------------------------------------------------ typed entries + + pub fn get_frame(&self, key: u128) -> Option { + let p = self.read(Kind::Frame, key)?; + decode_frame(&p) + } + + /// Queue a frame for writing (8-bit premultiplied RGBA, `width * height * 4` bytes). + pub fn put_frame(&self, key: u128, width: u32, height: u32, rgba: &[u8]) { + if self.contains(Kind::Frame, key) || rgba.len() != width as usize * height as usize * 4 { + return; + } + self.enqueue(Kind::Frame, key, encode_frame(width, height, rgba)); + } + + pub fn get_layer(&self, key: u128) -> Option { + let p = self.read(Kind::Layer, key)?; + decode_layer(&p) + } + + /// Queue a layer buffer for writing. + pub fn put_layer(&self, key: u128, buf: &Buf) { + if self.contains(Kind::Layer, key) { + return; + } + self.enqueue(Kind::Layer, key, encode_layer(buf)); + } + + /// Write a layer buffer synchronously (tests). + pub fn put_layer_now(&self, key: u128, buf: &Buf) { + self.write_now(Kind::Layer, key, &encode_layer(buf)); + } + + /// Write a frame synchronously (tests). + pub fn put_frame_now(&self, key: u128, width: u32, height: u32, rgba: &[u8]) { + self.write_now(Kind::Frame, key, &encode_frame(width, height, rgba)); + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn writer(dc: std::sync::Weak, q: Arc<(Mutex, Condvar)>) { + let (m, cv) = &*q; + loop { + let job = { + let Ok(mut g) = m.lock() else { return }; + loop { + if let Some(j) = g.jobs.pop_front() { + g.busy = true; + break j; + } + if dc.strong_count() == 0 { + return; + } + g = match cv.wait_timeout(g, std::time::Duration::from_millis(500)) { + Ok((g, _)) => g, + Err(_) => return, + }; + } + }; + let Some(cache) = dc.upgrade() else { return }; + match job { + Job::Write(kind, key, payload) => cache.write_now(kind, key, &payload), + } + drop(cache); + if let Ok(mut g) = m.lock() { + g.busy = false; + } + cv.notify_all(); + } +} + +/// Check magic, kind and checksum; returns the payload. +fn verify(d: &[u8], kind: Kind) -> Option<&[u8]> { + if d.len() < 16 || &d[..4] != MAGIC || d[4] != kind.tag() { + return None; + } + let (body, sum) = d.split_at(d.len() - 8); + let mut s = [0u8; 8]; + s.copy_from_slice(sum); + (checksum(body) == u64::from_le_bytes(s)).then_some(&body[8..]) +} + +fn encode_frame(w: u32, h: u32, rgba: &[u8]) -> Vec { + let mut out = Vec::with_capacity(rgba.len() / 2 + 16); + out.extend_from_slice(&w.to_le_bytes()); + out.extend_from_slice(&h.to_le_bytes()); + out.extend_from_slice(&lz4_flex::compress_prepend_size(rgba)); + out +} + +fn decode_frame(p: &[u8]) -> Option { + if p.len() < 8 { + return None; + } + let w = u32::from_le_bytes(p[0..4].try_into().ok()?); + let h = u32::from_le_bytes(p[4..8].try_into().ok()?); + let rgba = lz4_flex::decompress_size_prepended(&p[8..]).ok()?; + (rgba.len() == w as usize * h as usize * 4).then_some(Frame8 { width: w, height: h, rgba }) +} + +fn encode_layer(b: &Buf) -> Vec { + let (w, h) = (b.img.width, b.img.height); + let n = b.img.data.len(); + // Byte planes: all first bytes of every float, then all second bytes… (compresses well). + let mut planes = vec![0u8; n * 16]; + for (i, px) in b.img.data.iter().enumerate() { + for c in 0..4 { + let bytes = px[c].to_le_bytes(); + for (k, byte) in bytes.iter().enumerate() { + planes[(k * 4 + c) * n + i] = *byte; + } + } + } + let mut out = Vec::with_capacity(planes.len() / 4 + 40); + out.extend_from_slice(&w.to_le_bytes()); + out.extend_from_slice(&h.to_le_bytes()); + out.extend_from_slice(&b.offset[0].to_le_bytes()); + out.extend_from_slice(&b.offset[1].to_le_bytes()); + out.extend_from_slice(&b.scale.to_le_bytes()); + out.extend_from_slice(&lz4_flex::compress_prepend_size(&planes)); + out +} + +fn decode_layer(p: &[u8]) -> Option { + if p.len() < 32 { + return None; + } + let w = u32::from_le_bytes(p[0..4].try_into().ok()?); + let h = u32::from_le_bytes(p[4..8].try_into().ok()?); + let f = |i: usize| -> Option { Some(f64::from_le_bytes(p[i..i + 8].try_into().ok()?)) }; + let offset = [f(8)?, f(16)?]; + let scale = f(24)?; + let planes = lz4_flex::decompress_size_prepended(&p[32..]).ok()?; + let n = w as usize * h as usize; + if planes.len() != n * 16 { + return None; + } + let mut img = Image::new(w, h); + for (i, px) in img.data.iter_mut().enumerate() { + for c in 0..4 { + let mut bytes = [0u8; 4]; + for (k, byte) in bytes.iter_mut().enumerate() { + *byte = planes[(k * 4 + c) * n + i]; + } + px[c] = f32::from_le_bytes(bytes); + } + } + Some(Buf { img, offset, scale }) +} + +// ---------------------------------------------------------------- content keys + +/// Bump when rendering changes in a way that makes old disk entries wrong. +const RENDER_VERSION: &str = "effectcraft-render-1"; + +fn hash_debug(h: &mut Hash128, v: &impl std::fmt::Debug) { + h.write(format!("{v:?}").as_bytes()); + h.write(&[0xff]); +} + +/// A file's size and modification time (so edited footage gets new keys). +fn file_stamp(h: &mut Hash128, path: &str) { + if path.is_empty() { + return; + } + if let Ok(md) = std::fs::metadata(path) { + h.write_u64(md.len()); + if let Ok(t) = md.modified() + && let Ok(d) = t.duration_since(std::time::UNIX_EPOCH) + { + h.write_u64(d.as_nanos() as u64); + } + } +} + +fn footage_stamp(h: &mut Hash128, f: &effectcraft_project::Footage) { + file_stamp(h, &f.path); + for p in &f.sequence { + file_stamp(h, p); + } +} + +/// Salt for layer cache keys on disk: the renderer version and every footage file's identity +/// (layer keys name footage items, not file contents). +pub fn footage_salt(project: &effectcraft_project::Project) -> u64 { + let mut h = Hash128::default(); + h.write(RENDER_VERSION.as_bytes()); + for (id, it) in &project.items { + if let effectcraft_project::ItemKind::Footage(f) = &it.kind { + h.write_u64(id.0); + hash_debug(&mut h, f); + footage_stamp(&mut h, f); + } + } + let v = h.finish(); + (v as u64) ^ ((v >> 64) as u64) +} + +/// Content key of a composition's pixels: project render settings, the comp and everything it +/// uses (nested comps, footage, solids, footage files' size and time). With expressions in play +/// (which can read any comp) the whole project counts. +pub fn comp_content_key(project: &effectcraft_project::Project, comp: effectcraft_project::ItemId) -> u128 { + use effectcraft_project::{ItemKind, LayerSource}; + let mut h = Hash128::default(); + h.write(RENDER_VERSION.as_bytes()); + hash_debug(&mut h, &project.settings); + let mut seen = std::collections::BTreeSet::new(); + let mut stack = vec![comp]; + let mut expressions = false; + while let Some(id) = stack.pop() { + if !seen.insert(id) { + continue; + } + let Some(it) = project.item(id) else { continue }; + if let ItemKind::Comp(c) = &it.kind { + for l in &c.layers { + if let LayerSource::Comp { item } | LayerSource::Footage { item } | LayerSource::Solid { item } = l.source { + stack.push(item); + } + if !expressions { + l.props.walk("", &mut |_, p| expressions |= p.expr.is_some()); + } + } + } + } + let ids: Vec = if expressions { project.items.keys().copied().collect() } else { seen.into_iter().collect() }; + for id in ids { + let Some(it) = project.item(id) else { continue }; + h.write_u64(id.0); + h.write(it.name.as_bytes()); + hash_debug(&mut h, &it.kind); + if let ItemKind::Footage(f) = &it.kind { + footage_stamp(&mut h, f); + } + } + h.write_u64(comp.0); + h.finish() +} + +/// Disk key of one viewer frame. +pub fn frame_key(content: u128, frame: i64, scale_milli: u32, view: u64) -> u128 { + let mut h = Hash128::default(); + h.write(&content.to_le_bytes()); + h.write_u64(frame as u64); + h.write_u64(scale_milli as u64); + h.write_u64(view); + h.finish() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tmpdir(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("effectcraft-disk-cache-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d + } + + fn buf(seed: u32, w: u32, h: u32) -> Buf { + let mut img = Image::new(w, h); + for (i, p) in img.data.iter_mut().enumerate() { + let v = ((i as u32).wrapping_mul(2654435761u32).wrapping_add(seed) % 1000) as f32 / 999.0; + *p = [v, v * 0.5, 1.0 - v, if i % 7 == 0 { 0.0 } else { 1.0 }]; + } + Buf { img, offset: [1.5, -2.25], scale: 0.5 } + } + + #[test] + fn hit_miss_and_exact_round_trip() { + let dir = tmpdir("hit"); + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + assert!(dc.get_layer(42).is_none()); + let b = buf(1, 33, 17); + dc.put_layer(42, &b); + dc.flush(); + let back = dc.get_layer(42).unwrap(); + assert_eq!(back.img, b.img); + assert_eq!((back.offset, back.scale), (b.offset, b.scale)); + let rgba: Vec = (0..20 * 10 * 4).map(|i| (i % 251) as u8).collect(); + dc.put_frame(7, 20, 10, &rgba); + dc.flush(); + assert_eq!(dc.get_frame(7).unwrap().rgba, rgba); + let st = dc.stats(); + assert_eq!((st.entries, st.hits, st.misses, st.writes), (2, 2, 1, 2)); + // Wrong kind is a miss. + assert!(dc.get_frame(42).is_none()); + } + + #[test] + fn persists_across_restarts_and_clears() { + let dir = tmpdir("persist"); + { + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + dc.put_layer_now(1, &buf(1, 8, 8)); + dc.put_frame_now(2, 2, 2, &[9; 16]); + } + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + assert_eq!(dc.stats().entries, 2); + assert!(dc.contains(Kind::Layer, 1) && dc.contains(Kind::Frame, 2)); + assert_eq!(dc.get_frame(2).unwrap().rgba, vec![9; 16]); + dc.clear(); + assert_eq!(dc.stats().entries, 0); + assert!(dc.get_frame(2).is_none()); + let again = DiskCache::open(&dir, 1 << 30).unwrap(); + assert_eq!(again.stats().entries, 0); + } + + #[test] + fn lru_eviction_respects_the_limit() { + let dir = tmpdir("lru"); + let dc = DiskCache::open(&dir, u64::MAX).unwrap(); + for k in 0..6u128 { + dc.put_layer_now(k, &buf(k as u32, 64, 64)); + } + let per = dc.stats().bytes / 6; + // Touch 0 so it is the most recently used. + assert!(dc.get_layer(0).is_some()); + dc.set_max_bytes(per * 3 + per / 2); + let st = dc.stats(); + assert!(st.bytes <= per * 3 + per / 2, "{st:?}"); + assert_eq!(st.entries, 3); + assert!(dc.contains(Kind::Layer, 0), "recently used survives"); + assert!(!dc.contains(Kind::Layer, 1) && !dc.contains(Kind::Layer, 2) && !dc.contains(Kind::Layer, 3)); + assert_eq!(st.evictions, 3); + // Files are really gone. + let files = walk(&dir); + assert_eq!(files, 3); + // New writes keep evicting. + dc.put_layer_now(99, &buf(9, 64, 64)); + assert!(dc.stats().bytes <= per * 3 + per / 2); + } + + fn walk(d: &Path) -> usize { + let mut n = 0; + if let Ok(rd) = std::fs::read_dir(d) { + for e in rd.flatten() { + let p = e.path(); + if p.is_dir() { + n += walk(&p); + } else if p.extension().is_some_and(|x| x == "ecc") { + n += 1; + } + } + } + n + } + + #[test] + fn damaged_files_are_misses() { + let dir = tmpdir("damaged"); + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + dc.put_frame_now(5, 4, 4, &[1; 64]); + let p = dc.path(Kind::Frame, 5); + let mut d = std::fs::read(&p).unwrap(); + let n = d.len(); + d[n / 2] ^= 0xff; + std::fs::write(&p, d).unwrap(); + assert!(dc.get_frame(5).is_none()); + assert!(!dc.contains(Kind::Frame, 5)); + assert!(!p.exists()); + } + + #[test] + fn concurrent_readers_and_writers() { + let dir = tmpdir("concurrent"); + let a = DiskCache::open(&dir, 1 << 30).unwrap(); + // A second instance on the same folder (another process, in effect). + let b = DiskCache::open(&dir, 1 << 30).unwrap(); + std::thread::scope(|s| { + for t in 0..4u32 { + let (a, b) = (a.clone(), b.clone()); + s.spawn(move || { + for i in 0..20u128 { + let k = i % 5; + let w = if t % 2 == 0 { &a } else { &b }; + w.put_layer_now(k, &buf(k as u32, 16, 16)); + if let Some(x) = w.get_layer(k) { + assert_eq!(x.img, buf(k as u32, 16, 16).img); + } + } + }); + } + }); + b.rescan(); + for k in 0..5u128 { + assert_eq!(b.get_layer(k).unwrap().img, buf(k as u32, 16, 16).img); + } + } +} diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 14a81575..a4e718a3 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -9,6 +9,7 @@ pub mod audio; pub mod cache; pub mod color; +pub mod disk_cache; use color::Region; pub mod eval; pub mod masks; @@ -46,6 +47,16 @@ pub trait FootageSource: Send + Sync { fn cache_budget(&self) -> Option { None } + /// Vector footage (SVG) rasterised at `scale` × its pixel size, for Continuously Rasterize. + /// `None` when the footage is not vector or cannot be read. + fn vector_frame(&self, _item: ItemId, _footage: &Footage, _scale: f64) -> Option> { + None + } +} + +/// Footage that can be rasterised at any scale (SVG). +pub fn is_vector_footage(f: &Footage) -> bool { + f.codec == "SVG" } /// Layer parameters and audio for effects (see [`effectcraft_effects::EffectHost`]). @@ -348,7 +359,12 @@ impl<'a> Renderer<'a> { /// stays sharp when scaled up). pub fn raster_scale(&self, ctx: &EvalCtx, layer: &Layer) -> f64 { let s = self.opts.scale; - if !layer.switches.collapse || !matches!(layer.source, LayerSource::Text | LayerSource::Shape) { + let vector = match layer.source { + LayerSource::Text | LayerSource::Shape => true, + LayerSource::Footage { item } => matches!(self.project.item(item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if is_vector_footage(f)), + _ => false, + }; + if !layer.switches.collapse || !vector { return s; } let (l2c, _) = ctx.layer_to_comp(layer); @@ -631,6 +647,17 @@ impl<'a> Renderer<'a> { if !f.has_video { return None; } + // Continuously rasterised vector footage: drawn at the on-screen scale. + if layer.switches.collapse && is_vector_footage(f) { + let k = self.raster_scale(ctx, layer); + if let Some(img) = self.footage.vector_frame(*item, f, k) { + let mut buf = Buf { img: (*img).clone(), offset: [0.0; 2], scale: k }; + if let Some(c) = self.pipe.media_in(f.color_profile) { + color::convert(&mut buf.img, &c); + } + return Some(buf); + } + } let lt = ctx.source_time(layer); let img = self.footage_frame(ctx, layer, *item, f, lt)?; let k = if img.width > 0 { f.width.max(1) as f64 / img.width as f64 } else { 1.0 }; diff --git a/crates/render/src/tests_audio_fx.rs b/crates/render/src/tests_audio_fx.rs index dfd49ccc..991fac32 100644 --- a/crates/render/src/tests_audio_fx.rs +++ b/crates/render/src/tests_audio_fx.rs @@ -51,6 +51,7 @@ fn audio_footage() -> Footage { missing: false, sequence: vec![], color_profile: None, + layer: None, } } diff --git a/crates/render/src/tests_color.rs b/crates/render/src/tests_color.rs index 87f9dd1a..016cdc93 100644 --- a/crates/render/src/tests_color.rs +++ b/crates/render/src/tests_color.rs @@ -172,6 +172,7 @@ fn footage(profile: Option) -> Footage { missing: false, sequence: vec![], color_profile: profile, + layer: None, } } diff --git a/crates/render/src/tests_frame_blend.rs b/crates/render/src/tests_frame_blend.rs index c172b81c..3c301d38 100644 --- a/crates/render/src/tests_frame_blend.rs +++ b/crates/render/src/tests_frame_blend.rs @@ -80,6 +80,7 @@ fn setup(mode: FrameBlend, size: (u32, u32)) -> (Project, ItemId) { missing: false, sequence: vec![], color_profile: None, + layer: None, }; let fid = p.add_item("clip", Label::Aqua, None, ItemKind::Footage(f)); let mut l = build::layer(&mut p, &comp, "clip", LayerSource::Footage { item: fid }, size, None); diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml new file mode 100644 index 00000000..0e1af771 --- /dev/null +++ b/crates/svg/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "effectcraft-svg" +description = "SVG import: parses SVG 1.1 / SVG 2 static vector content (paths, basic shapes, groups, transforms, use, CSS styles, solid and gradient fills/strokes, opacity) and rasterises it at any scale" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] +effectcraft-geom = { workspace = true } +effectcraft-raster = { workspace = true } +effectcraft-path = { workspace = true } +kurbo = { workspace = true } +roxmltree = "0.21" +rayon = { workspace = true } diff --git a/crates/svg/README.md b/crates/svg/README.md new file mode 100644 index 00000000..862bd01d --- /dev/null +++ b/crates/svg/README.md @@ -0,0 +1,26 @@ +# effectcraft-svg + +SVG import for EffectCraft: `.svg` files import as footage (rasterised at any scale, so +Continuously Rasterize stays sharp) and Layer ▸ Create ▸ Create Shapes from Vector Layer turns +them into editable shape layers. + +Implemented from the W3C **Scalable Vector Graphics (SVG) 1.1 (Second Edition)** recommendation +(16 August 2011) and the **SVG 2** candidate recommendation (4 October 2018) for the parts listed +here, plus **CSS Color Module Level 3** for colour keywords and functions: + +- document: `svg` (width/height, `viewBox`, `preserveAspectRatio`), nested `svg`, `g`, `defs`, + `use` (x/y, `href` / `xlink:href`), `symbol` (through `use`), `title`/`desc`/`metadata` ignored; +- shapes: `path` (all path data commands incl. elliptical arcs), `rect` (rx/ry), `circle`, + `ellipse`, `line`, `polyline`, `polygon`; +- `transform` lists (matrix, translate, scale, rotate with centre, skewX, skewY); +- painting: `fill`, `fill-rule`, `fill-opacity`, `stroke`, `stroke-width`, `stroke-linecap`, + `stroke-linejoin`, `stroke-miterlimit`, `stroke-dasharray`, `stroke-dashoffset`, + `stroke-opacity`, `opacity`, `display`, `visibility`, `color`/`currentColor`; +- `linearGradient` and `radialGradient` (with `fx`/`fy` focal point), `gradientUnits`, + `gradientTransform`, `spreadMethod`, `href` inheritance of stops and attributes; +- styling through presentation attributes, the `style` attribute and ` + + + + + + + + + + + + + + + + + diff --git a/crates/svg/tests/fixtures/paths-gradients.svg.attribution b/crates/svg/tests/fixtures/paths-gradients.svg.attribution new file mode 100644 index 00000000..d4b8758f --- /dev/null +++ b/crates/svg/tests/fixtures/paths-gradients.svg.attribution @@ -0,0 +1,7 @@ +asset: paths-gradients.svg +title: SVG test fixture: path data, gradients, CSS styles and use +author: EffectCraft contributors +source: original work +license: MIT OR Apache-2.0 +added: 2026-10-02 by EffectCraft contributors (Claude agent, SVG import) +notes: Hand-written for the effectcraft-svg parser and Create Shapes from Vector Layer tests. diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index 62239f19..d0e5a6d9 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -7,6 +7,11 @@ //! wasm32 has no threads: jobs wait in the same priority queue and [`Frames::pump`] renders them //! on the UI thread between egui frames, within a time budget. //! +//! With the disk cache on (Settings ▸ Media & Disk Cache), every CPU-rendered frame is also +//! written to disk under a content key (the comp's content hash, frame, scale, view and render +//! options), and a job first looks there: frames survive restarts and project reopenings. The +//! timeline shows disk-only frames as a blue cache bar. +//! //! With Mercury GPU Acceleration (a [`Gpu`] on egui-wgpu's device and the project's renderer set //! to the GPU) frames stay on the GPU: the compositor writes an RGBA8 texture that the viewer //! registers with egui-wgpu and draws directly; pixels are read back only when something needs @@ -16,6 +21,7 @@ use std::collections::{HashMap, HashSet, VecDeque}; use std::sync::{Arc, Mutex}; use effectcraft_engine::project::{ItemId, Project}; +use effectcraft_engine::render::disk_cache::{self, DiskCache}; use effectcraft_engine::render::{ExprHost, FootageSource, LayerCache, RenderOpts, Renderer}; use effectcraft_engine::time::Tick; use effectcraft_gpu::{DisplayFrame, Gpu}; @@ -122,6 +128,37 @@ pub struct RenderSource { /// Show Channel / exposure / region-of-interest drawing read the CPU frame texture, so GPU /// frames are read back (the compositing still runs on the GPU). pub gpu_display: bool, + /// The persistent disk cache, when enabled. + pub disk: Option>, +} + +/// Content keys of (project revision, comp), computed once per revision. +type ContentKeys = Arc>>; + +fn content_key(keys: &ContentKeys, project: &Project, revision: u64, comp: u64) -> u128 { + if let Some(k) = keys.lock().ok().and_then(|m| m.get(&(revision, comp)).copied()) { + return k; + } + let k = disk_cache::comp_content_key(project, ItemId(comp)); + if let Ok(mut m) = keys.lock() { + if m.len() > 256 { + m.clear(); + } + m.insert((revision, comp), k); + } + k +} + +fn opts_hash(o: &RenderOpts) -> u64 { + let mut h = disk_cache::Hash128::default(); + h.write(format!("{o:?}").as_bytes()); + h.finish() as u64 +} + +/// Disk key of a viewer frame (render options included: region of interest, draft…). +fn frame_disk_key(keys: &ContentKeys, project: &Project, key: &FrameKey, opts_hash: u64) -> u128 { + let content = content_key(keys, project, key.revision, key.comp); + disk_cache::frame_key(content, key.frame, key.scale, key.view ^ opts_hash) } pub struct Frames { @@ -135,6 +172,7 @@ pub struct Frames { ctx: Option, /// Render time of the last viewer (urgent) frame in ms, for the Info panel / perf readout. pub last_ms: Arc>, + content_keys: ContentKeys, } impl Default for Frames { @@ -149,6 +187,7 @@ impl Default for Frames { pool: rayon::ThreadPoolBuilder::new().num_threads(threads).thread_name(|i| format!("ec-frame-{i}")).build().expect("frame pool"), ctx: None, last_ms: Arc::new(Mutex::new(0.0)), + content_keys: Arc::default(), } } } @@ -195,6 +234,21 @@ impl Frames { v } + /// Frames of (revision, comp, scale) in the disk cache but not in RAM — the blue cache bar. + /// `opts` are the viewer's render options (part of the disk key). + pub fn disk_frames(&self, src: &RenderSource, revision: u64, comp: u64, scale: u32, view: u64, frames: i64, opts: &RenderOpts) -> Vec { + let Some(dc) = &src.disk else { return vec![] }; + let ram: HashSet = self.cached_frames(revision, comp, scale).into_iter().collect(); + let oh = opts_hash(opts); + (0..frames.clamp(0, 100_000)) + .filter(|f| !ram.contains(f)) + .filter(|f| { + let key = FrameKey { revision, comp, frame: *f, scale, view }; + dc.contains(disk_cache::Kind::Frame, frame_disk_key(&self.content_keys, &src.project, &key, oh)) + }) + .collect() + } + /// Change the RAM preview cache budget (Settings ▸ Memory & CPU), evicting the oldest /// frames when it shrinks. pub fn set_budget(&self, bytes: usize) { @@ -278,7 +332,14 @@ impl Frames { } fn worker(&self) -> Worker { - Worker { queue: self.queue.clone(), cache: self.cache.clone(), inflight: self.inflight.clone(), ctx: self.ctx.clone(), last: self.last_ms.clone() } + Worker { + queue: self.queue.clone(), + cache: self.cache.clone(), + inflight: self.inflight.clone(), + ctx: self.ctx.clone(), + last: self.last_ms.clone(), + content_keys: self.content_keys.clone(), + } } /// Render queued frames on this thread, most important first, until the queue is empty or @@ -306,12 +367,13 @@ struct Worker { inflight: Arc>>, ctx: Option, last: Arc>, + content_keys: ContentKeys, } impl Worker { /// Render the queued job that matters most right now. `false` when the queue was empty. fn run_next(&self) -> bool { - let Worker { queue, cache, inflight, ctx, last } = self; + let Worker { queue, cache, inflight, ctx, last, content_keys } = self; let job = { let Ok(mut q) = queue.lock() else { return false }; // Urgent first (newest), then prefetch in request order. @@ -325,19 +387,18 @@ impl Worker { job }; let t0 = web_time::Instant::now(); - let mut r = Renderer::new(&job.src.project, job.src.footage.as_ref(), job.opts); - r.expr = job.src.expr.as_deref(); - r.cache = Some(&job.src.layer_cache); - r.accel = job.src.gpu.as_ref().map(|g| g as &dyn effectcraft_engine::render::Accelerator); - // The GPU leaves the frame in a texture for the viewer; otherwise (Software Only, no - // adapter, or a frame the GPU cannot finish here) the CPU renders it. - let gpu_frame = match (&job.src.gpu, r.active_accel()) { - (Some(g), Some(_)) if job.src.gpu_display => g.render_display(&r, job.comp, job.t), - _ => None, - }; - let ci = match gpu_frame { - Some(f) => FrameImage::Gpu(Arc::new(f)), - None => FrameImage::Cpu(Arc::new(to_color_image(&r.comp_frame(job.comp, job.t)))), + let disk = job.src.disk.as_ref().map(|dc| (dc.clone(), frame_disk_key(content_keys, &job.src.project, &job.key, opts_hash(&job.opts)))); + let from_disk = disk.as_ref().and_then(|(dc, k)| dc.get_frame(*k)); + let ci = match from_disk { + Some(f) => FrameImage::Cpu(Arc::new(egui::ColorImage::from_rgba_premultiplied([f.width as usize, f.height as usize], &f.rgba))), + None => { + let ci = self.render_job(&job); + if let (Some((dc, k)), FrameImage::Cpu(img)) = (&disk, &ci) { + let rgba: Vec = img.pixels.iter().flat_map(|c| c.to_array()).collect(); + dc.put_frame(*k, img.size[0] as u32, img.size[1] as u32, &rgba); + } + ci + } }; if job.urgent && let Ok(mut l) = last.lock() @@ -361,6 +422,23 @@ impl Worker { } true } + + fn render_job(&self, job: &Job) -> FrameImage { + let mut r = Renderer::new(&job.src.project, job.src.footage.as_ref(), job.opts); + r.expr = job.src.expr.as_deref(); + r.cache = Some(&job.src.layer_cache); + r.accel = job.src.gpu.as_ref().map(|g| g as &dyn effectcraft_engine::render::Accelerator); + // The GPU leaves the frame in a texture for the viewer; otherwise (Software Only, no + // adapter, or a frame the GPU cannot finish here) the CPU renders it. + let gpu_frame = match (&job.src.gpu, r.active_accel()) { + (Some(g), Some(_)) if job.src.gpu_display => g.render_display(&r, job.comp, job.t), + _ => None, + }; + match gpu_frame { + Some(f) => FrameImage::Gpu(Arc::new(f)), + None => FrameImage::Cpu(Arc::new(to_color_image(&r.comp_frame(job.comp, job.t)))), + } + } } struct Job { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 071c1948..ec6a2278 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -308,6 +308,7 @@ impl EffectcraftApp { layer_cache: self.session.layer_cache.clone(), gpu: self.gpu.clone(), gpu_display: false, + disk: self.session.disk_cache.clone(), } } @@ -405,13 +406,11 @@ impl EffectcraftApp { self.request_frame_with(comp, frame, scale, true); } - fn request_frame_with(&self, comp: ItemId, frame: i64, scale: f64, urgent: bool) { - let Some(c) = self.session.project.comp(comp) else { return }; - let key = self.frame_key(comp, frame, scale); - let t = c.frame_rate.tick_of(frame); + /// Render options of viewer frames of `comp` at `scale`. + pub fn frame_opts(&self, comp: ItemId, scale: f64) -> RenderOpts { let (draft, _) = self.session.state.viewer.fast_previews.render(self.ui.viewer.interacting); let roi = self.session.state.region_of_interest.filter(|_| self.session.active_comp_id() == Some(comp)); - let opts = RenderOpts { + RenderOpts { scale, motion_blur: true, guides: true, @@ -419,7 +418,14 @@ impl EffectcraftApp { view: self.session.view_camera(comp), roi, backend: effectcraft_engine::render::Backend::Auto, - }; + } + } + + fn request_frame_with(&self, comp: ItemId, frame: i64, scale: f64, urgent: bool) { + let Some(c) = self.session.project.comp(comp) else { return }; + let key = self.frame_key(comp, frame, scale); + let t = c.frame_rate.tick_of(frame); + let opts = self.frame_opts(comp, scale); if urgent { self.frames.request_urgent(&self.render_source(), key, comp, t, opts); } else { diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index da5eaec2..7ebadbf4 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -716,7 +716,7 @@ fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option None, @@ -739,6 +739,10 @@ fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option bool { } /// If `id` is a dialog command invoked without its parameters, open its form and return true. +/// The import includes a Photoshop document. +fn is_layered(p: &Value) -> bool { + let psd = |v: &Value| v.as_str().is_some_and(|s| matches!(s.rsplit('.').next().map(str::to_ascii_lowercase).as_deref(), Some("psd" | "psb"))); + match p.get("paths").or(p.get("path")) { + Some(Value::Array(a)) => a.iter().any(psd), + Some(v) => psd(v), + None => false, + } +} + pub fn open_form(app: &mut EffectcraftApp, id: &str, p: &Value) -> bool { let s = &app.session; let comp = s.active_comp(); @@ -116,6 +126,16 @@ pub fn open_form(app: &mut EffectcraftApp, id: &str, p: &Value) -> bool { let lt = layer.map(|l| l.layer_time(t)).unwrap_or(t); let base = p.clone(); let (title, fields): (String, Vec) = match id { + // Photoshop files: Import Kind (Footage / Composition / – Retain Layer Sizes). + "file.import" if !has(p, &["importAs"]) && is_layered(p) => ( + "Import Photoshop File".into(), + vec![Field::choice( + "importAs", + "Import Kind", + &[("Footage", json!("footage")), ("Composition", json!("composition")), ("Composition - Retain Layer Sizes", json!("compositionLayerSizes"))], + 1, + )], + ), "layer.setTransform" if !has(p, &["value"]) => { let prop = p.get("prop").and_then(Value::as_str).unwrap_or("position"); let cur = layer.and_then(|l| l.transform()).and_then(|tr| tr.get(if prop == "anchorPoint" { "anchor" } else { prop })).map(|pr| pr.value_at(lt)); diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 5b6c085f..785491d7 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -602,6 +602,31 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { if let Some((a, b)) = run { draw_run(a, b); } + // Disk-cached frames not in RAM (blue), like After Effects. + if app.session.disk_cache.is_some() { + let frames = comp.frame_rate.frame_at(comp.duration); + let opts = app.frame_opts(cid, scale_key as f64 / 1000.0); + let disk = app.frames.disk_frames(&app.render_source(), app.session.revision, cid.0, scale_key, app.view_hash(cid), frames, &opts); + let draw_blue = |a: i64, b: i64| { + let x0 = tm.x(a as f64 * fd); + let x1 = tm.x((b + 1) as f64 * fd); + p.rect_filled(Rect::from_min_max(pos2(x0, cy0), pos2(x1, cy0 + 2.5)), 0.0, t.cache_blue); + }; + let mut run: Option<(i64, i64)> = None; + for f in disk { + run = match run { + Some((a, b)) if f == b + 1 => Some((a, f)), + Some((a, b)) => { + draw_blue(a, b); + Some((f, f)) + } + None => Some((f, f)), + }; + } + if let Some((a, b)) = run { + draw_blue(a, b); + } + } // Ticks + labels. // Label spacing in whole frames (AE: `00:15f`-style seconds:frames labels). let fps_i = fr.as_f64().round().max(1.0) as i64; diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index 5c82e916..c32ecb24 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -60,6 +60,8 @@ pub struct Tokens { pub cti: Color32, pub work_area: Color32, pub cache_green: Color32, + /// Timeline cache bar for frames held in the disk cache (not in RAM). + pub cache_blue: Color32, pub keyframe: Color32, pub keyframe_selected: Color32, pub pasteboard: Color32, @@ -108,6 +110,7 @@ impl Tokens { cti: Color32::from_rgb(0x2d, 0x8c, 0xeb), work_area: Color32::from_rgb(0x4a, 0x4a, 0x4a), cache_green: Color32::from_rgb(0x3c, 0xa6, 0x4c), + cache_blue: Color32::from_rgb(0x3a, 0x6f, 0xd8), keyframe: Color32::from_rgb(0xa8, 0xa8, 0xa8), keyframe_selected: Color32::from_rgb(0x3d, 0x8f, 0xf5), pasteboard: Color32::from_rgb(0x1a, 0x1a, 0x1a), diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 76ff6fa3..09e15047 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -27,10 +27,12 @@ const LAYERS: &[(&str, u8)] = &[ ("raster", 1), ("keyframe", 1), ("path", 1), + ("psd", 1), ("project", 2), ("text", 2), ("effects", 2), ("track", 2), + ("svg", 2), ("render", 3), ("media", 3), ("expr", 3), From 78cd313d3cebfbcca543a260cb5ca52840d28c0e Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:13:09 -0400 Subject: [PATCH 14/23] M14.4: scripting with an After Effects-style JavaScript object model New crate effectcraft-script (L4): boa runtime with app/Project/CompItem/ Layer/PropertyGroup/Property/TextDocument/MarkerValue/RenderQueue/File object model over engine commands (undoable, undo groups), AE match-name table, file/network security gate. Entry points: script.run command, File > Scripts > Run Script File (.jsx/.js), Window > Script Console, effectcraft-cli script, MCP run_script. Engine: project.delete/duplicate, prop.renameGroup/setGroupEnabled/removeGroup/duplicateGroup/moveGroup. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 17 + Cargo.toml | 1 + apps/effectcraft-cli/src/main.rs | 51 +- apps/effectcraft-cli/tests/cli.rs | 31 + apps/effectcraft-web/src/lib.rs | 5 +- crates/automation/Cargo.toml | 3 + crates/automation/src/tests.rs | 28 + crates/automation/src/tools.rs | 24 + crates/engine/src/commands/file_more.rs | 60 +- crates/engine/src/commands/mod.rs | 2 + crates/engine/src/commands/project_items.rs | 96 ++ crates/engine/src/commands/prop_groups.rs | 107 ++ crates/engine/src/commands/text_anim.rs | 3 +- crates/engine/src/lib.rs | 18 + crates/engine/src/menus.rs | 1 + crates/engine/src/tests_project_items.rs | 57 + crates/host/Cargo.toml | 3 +- crates/host/src/lib.rs | 38 +- crates/script/Cargo.toml | 27 + crates/script/src/lib.rs | 64 + crates/script/src/matchnames.rs | 443 +++++ crates/script/src/model.rs | 679 ++++++++ crates/script/src/prelude.js | 1689 +++++++++++++++++++ crates/script/src/runtime.rs | 546 ++++++ crates/script/src/tests.rs | 392 +++++ crates/ui-egui/src/dock.rs | 5 +- crates/ui-egui/src/menus.rs | 2 +- crates/ui-egui/src/panels/mod.rs | 2 + crates/ui-egui/src/panels/project.rs | 14 +- crates/ui-egui/src/panels/script_console.rs | 144 ++ crates/ui-egui/src/state.rs | 4 + crates/ui-egui/tests/ui_script_console.rs | 44 + docs/agents.md | 14 + docs/architecture.md | 28 +- docs/parity.md | 4 +- xtask/src/main.rs | 3 + 36 files changed, 4621 insertions(+), 28 deletions(-) create mode 100644 crates/engine/src/commands/prop_groups.rs create mode 100644 crates/script/Cargo.toml create mode 100644 crates/script/src/lib.rs create mode 100644 crates/script/src/matchnames.rs create mode 100644 crates/script/src/model.rs create mode 100644 crates/script/src/prelude.js create mode 100644 crates/script/src/runtime.rs create mode 100644 crates/script/src/tests.rs create mode 100644 crates/ui-egui/src/panels/script_console.rs create mode 100644 crates/ui-egui/tests/ui_script_console.rs diff --git a/Cargo.lock b/Cargo.lock index 46f34e63..c1e86483 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1145,6 +1145,7 @@ name = "effectcraft-automation" version = "0.1.0" dependencies = [ "effectcraft-engine", + "effectcraft-script", "image", "serde_json", "thiserror 2.0.21", @@ -1287,6 +1288,7 @@ dependencies = [ "effectcraft-expr", "effectcraft-media", "effectcraft-project", + "effectcraft-script", "serde_json", ] @@ -1392,6 +1394,21 @@ dependencies = [ "web-time", ] +[[package]] +name = "effectcraft-script" +version = "0.1.0" +dependencies = [ + "boa_engine", + "effectcraft-engine", + "effectcraft-expr", + "effectcraft-keyframe", + "effectcraft-path", + "effectcraft-project", + "effectcraft-render", + "effectcraft-time", + "serde_json", +] + [[package]] name = "effectcraft-text" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index a1941cf4..6c5ac99a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ effectcraft-engine = { path = "crates/engine" } effectcraft-host = { path = "crates/host" } effectcraft-ui-egui = { path = "crates/ui-egui" } effectcraft-automation = { path = "crates/automation" } +effectcraft-script = { path = "crates/script" } # foundation thiserror = "2" diff --git a/apps/effectcraft-cli/src/main.rs b/apps/effectcraft-cli/src/main.rs index 90d7ef66..b8222ddc 100644 --- a/apps/effectcraft-cli/src/main.rs +++ b/apps/effectcraft-cli/src/main.rs @@ -15,6 +15,7 @@ //! effectcraft-cli render F.ecproj --queue render the project's Render Queue //! effectcraft-cli bench [--comp C] [--time S] [--scale K] [--n N] [--play N] [--gpu] render timings //! (--gpu: CPU vs GPU ms/frame for every comp at Full and Half) +//! effectcraft-cli script FILE.jsx [F.ecproj] | --eval CODE run an After Effects-style script //! effectcraft-cli mcp [--bridge PORT] MCP server on stdio //! //! Project: --project F.ecproj (or a positional *.ecproj) | --demo | --empty (default: demo; mcp: empty) @@ -32,7 +33,7 @@ use effectcraft_automation::tools::{self, Reply}; use effectcraft_automation::{Backend, McpServer}; use serde_json::{Value, json}; -const USAGE: &str = "usage: effectcraft-cli [args] [--json] +const USAGE: &str = "usage: effectcraft-cli [args] [--json] info project + engine summary commands [--filter TEXT] [--enabled] list engine commands exec [--params JSON] run one engine command @@ -47,6 +48,9 @@ const USAGE: &str = "usage: effectcraft-cli : id or name, '-' = active comp; : id, '#n' or name; : JSON or bare string"; @@ -68,6 +72,7 @@ const VALUED: &[&str] = &[ "--out", "--expression", "--depth", + "--eval", // render "--format", "--start", @@ -243,6 +248,49 @@ fn tool(b: &mut Backend, name: &str, a: Value) -> Result { } } +/// `script FILE.jsx [project]` / `script --eval CODE`: run JavaScript against a headless session +/// (an empty project unless one is given, or `--demo`) or the running app (`--bridge`). +fn script_cmd(args: &Args, json_out: bool) -> Result<(), Failure> { + let (code, name) = match (args.opt("--eval"), args.pos.first()) { + (Some(c), _) => (c.to_string(), "eval".to_string()), + (None, Some(f)) => (std::fs::read_to_string(f).map_err(|e| Failure::Error(format!("cannot read {f}: {e}")))?, f.clone()), + (None, None) => return usage_err("script needs a .jsx/.js file or --eval CODE"), + }; + let mut b = backend(args, false)?; + let r = b.exec("script.run", json!({"code": code, "name": name}))?; + let saved = maybe_save(&mut b, args)?; + if json_out { + let mut o = r.clone(); + if let Some(p) = &saved { + o["saved"] = json!(p); + } + emit(&o, true); + } else { + if let Some(out) = r["output"].as_str().filter(|o| !o.is_empty()) { + println!("{out}"); + } + if !r["result"].is_null() && r["error"].is_null() { + println!("{}", r["result"]); + } + if let Some(p) = saved { + eprintln!("saved {p}"); + } + } + if let Some(e) = r.get("error").filter(|e| !e.is_null()) { + let pos = match (e["line"].as_u64(), e["column"].as_u64()) { + (Some(l), Some(c)) => format!(":{l}:{c}"), + (Some(l), None) => format!(":{l}"), + _ => String::new(), + }; + let msg = format!("{}{pos}: {}", e["file"].as_str().unwrap_or(&name), e["message"].as_str().unwrap_or("script error")); + if json_out { + std::process::exit(1); + } + return Err(Failure::Error(msg)); + } + Ok(()) +} + /// Print a result: compact JSON with `--json`, else pretty JSON. fn emit(v: &Value, json_out: bool) { if json_out { @@ -315,6 +363,7 @@ fn run(cmd: &str, args: &Args, json_out: bool) -> Result<(), Failure> { } } } + "script" => script_cmd(args, json_out)?, "exec" => { let Some(id) = args.pos.first().cloned() else { return usage_err("exec needs a command id") }; let params = match (args.opt("--params"), args.pos.get(1).map(String::as_str)) { diff --git a/apps/effectcraft-cli/tests/cli.rs b/apps/effectcraft-cli/tests/cli.rs index c8427720..d7c48762 100644 --- a/apps/effectcraft-cli/tests/cli.rs +++ b/apps/effectcraft-cli/tests/cli.rs @@ -131,3 +131,34 @@ fn mcp_over_stdio() { assert_eq!(replies[2]["result"]["isError"], false, "{}", replies[2]); assert_eq!(replies[3]["result"]["content"][0]["type"], "image"); } + +#[test] +fn script_file_and_eval() { + let proj = tmp("scripted.ecproj"); + let p = proj.to_str().unwrap(); + let jsx = tmp("build.jsx"); + std::fs::write( + &jsx, + "app.beginUndoGroup('Build');\nvar c = app.project.items.addComp('FromJsx', 160, 90, 1, 2, 24);\nc.layers.addSolid([1, 0, 0], 'Red', 160, 90, 1);\napp.endUndoGroup();\nwriteLn('built ' + c.name);\nc.numLayers", + ) + .unwrap(); + let v = ok_json(&["script", jsx.to_str().unwrap(), "--save-as", p]); + assert_eq!(v["ok"], json!(true), "{v}"); + assert_eq!(v["result"], json!(1)); + assert_eq!(v["output"], json!("built FromJsx")); + assert_eq!(v["saved"], p); + // Run against the saved project (positional), plain output. + let out = bin().args(["script", "--eval", "app.project.item(1).name + ' has ' + app.project.item(1).numLayers", p]).output().unwrap(); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "\"FromJsx has 1\""); + // Errors: exit 1 with file:line:col. + let bad = tmp("bad.jsx"); + std::fs::write(&bad, "var a = 1;\n\nmissingFunction();\n").unwrap(); + let out = bin().args(["script", bad.to_str().unwrap()]).output().unwrap(); + assert_eq!(out.status.code(), Some(1)); + let err = String::from_utf8_lossy(&out.stderr); + assert!(err.contains("bad.jsx:3:1") && err.contains("missingFunction"), "{err}"); + let (code, v) = run_json(&["script", "--eval", "\nnope()"]); + assert_eq!(code, 1); + assert_eq!(v["error"]["line"], json!(2)); +} diff --git a/apps/effectcraft-web/src/lib.rs b/apps/effectcraft-web/src/lib.rs index 7a765bfc..edd90e31 100644 --- a/apps/effectcraft-web/src/lib.rs +++ b/apps/effectcraft-web/src/lib.rs @@ -105,8 +105,8 @@ fn query_flag(name: &str) -> bool { web_sys::window().and_then(|w| w.location().search().ok()).and_then(|s| web_sys::UrlSearchParams::new_with_str(&s).ok()).is_some_and(|p| p.has(name)) } -/// A session wired for the browser: media decoding from memory, expressions, Render Queue export -/// to downloads. +/// A session wired for the browser: media decoding from memory, expressions, scripting, Render +/// Queue export to downloads. pub fn session() -> Session { let pool = Arc::new(effectcraft_media::MediaPool::new()); Session { @@ -115,6 +115,7 @@ pub fn session() -> Session { importer: Some(Arc::new(files::WebImporter { pool })), expr: Some(Arc::new(effectcraft_expr::Expressions)), expr_check: Some(effectcraft_expr::check_syntax), + script: Some(effectcraft_host::script::runner), exporter: Some(Arc::new(effectcraft_host::FileExporter { sink: Some(files::export_sink()) })), ..Default::default() } diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index a3b16408..0b2bcb05 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -14,3 +14,6 @@ effectcraft-engine = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } image = { workspace = true } + +[dev-dependencies] +effectcraft-script = { workspace = true } diff --git a/crates/automation/src/tests.rs b/crates/automation/src/tests.rs index f175b12f..848d3ee0 100644 --- a/crates/automation/src/tests.rs +++ b/crates/automation/src/tests.rs @@ -54,6 +54,7 @@ fn initialize_and_list_tools() { for want in [ "list_commands", "execute_command", + "run_script", "get_project", "get_comp", "get_layer", @@ -263,3 +264,30 @@ fn bridge_rejects_non_loopback() { assert!(Backend::bridge("10.0.0.1:9877").is_err()); assert!(Backend::bridge("localhost:9877").is_ok()); } + +#[test] +fn run_script_round_trip() { + let mut sess = Session::default(); + effectcraft_script::install(&mut sess); + let mut s = McpServer::new(Backend::headless(sess)); + let r = call_json( + &mut s, + "run_script", + json!({"code": "var c = app.project.items.addComp('Scripted', 320, 180, 1, 2, 24);\nc.layers.addText('Hi');\nwriteLn('made ' + c.name);\nc.numLayers"}), + ); + assert_eq!(r["ok"], json!(true), "{r}"); + assert_eq!(r["result"], json!(1)); + assert_eq!(r["output"], json!("made Scripted")); + // The edit is visible to the other tools. + let p = call_json(&mut s, "get_project", json!({})); + assert!(p.to_string().contains("Scripted"), "{p}"); + // Errors come back with their line. + let r = call_json(&mut s, "run_script", json!({"code": "var a = 1;\nundefinedFn();", "name": "bad.jsx"})); + assert_eq!(r["ok"], json!(false)); + assert_eq!(r["error"]["line"], json!(2)); + assert_eq!(r["error"]["file"], json!("bad.jsx")); + // Without a scripting engine the command explains itself. + let mut bare = server(); + let (c, err) = call(&mut bare, "run_script", json!({"code": "1"})); + assert!(err && c[0]["text"].as_str().unwrap().contains("not available"), "{c:?}"); +} diff --git a/crates/automation/src/tools.rs b/crates/automation/src/tools.rs index 205573b6..739c5524 100644 --- a/crates/automation/src/tools.rs +++ b/crates/automation/src/tools.rs @@ -140,6 +140,15 @@ fn execute_command(b: &mut Backend, a: &Value) -> Result { json_reply(b.exec(id, params)?) } +fn run_script(b: &mut Backend, a: &Value) -> Result { + let code = get(a, "code").and_then(Value::as_str).ok_or_else(|| Error::BadArgs("missing `code` (JavaScript)".into()))?; + let mut p = json!({"code": code}); + if let Some(n) = get(a, "name") { + p["name"] = n.clone(); + } + json_reply(b.exec("script.run", p)?) +} + fn get_project(b: &mut Backend, _: &Value) -> Result { json_reply(b.exec("project.summary", json!({}))?) } @@ -386,6 +395,21 @@ static TOOLS: &[ToolDef] = &[ }, run: execute_command, }, + ToolDef { + name: "run_script", + description: "Run JavaScript with an After Effects-style scripting object model (app, app.project, items.addComp, comp.layers.addSolid/addText/addShape/addNull/addCamera/addLight, layer.property(\"ADBE Transform Group\").property(\"ADBE Position\").setValueAtTime(t, v), Effects.addProperty(\"ADBE Gaussian Blur 2\"), expressions, markers, renderQueue…). Every edit is an undoable engine command; app.beginUndoGroup/endUndoGroup make one undo step. Returns {ok, result (last expression value), output (writeLn/$.writeln/alert text), error: {message, line, column} | null}. File access is limited to the project folder unless the user allows scripts to write files.", + bridge_only: false, + schema: || { + schema( + json!({ + "code": {"type": "string", "description": "JavaScript source, e.g. `var c = app.project.items.addComp(\"Main\", 1920, 1080, 1, 5, 30); c.layers.addText(\"Hi\"); c.numLayers`."}, + "name": {"type": "string", "description": "Script name used in error messages (default `script`)."} + }), + &["code"], + ) + }, + run: run_script, + }, ToolDef { name: "get_project", description: "Project overview: file path, dirty flag, bit depth, every item (id, name, type Composition/Footage/Solid/Folder, size, duration, layer count), the active comp id, current time (s), selected layers and the undo stack.", diff --git a/crates/engine/src/commands/file_more.rs b/crates/engine/src/commands/file_more.rs index 87c1e2df..5ff744c3 100644 --- a/crates/engine/src/commands/file_more.rs +++ b/crates/engine/src/commands/file_more.rs @@ -358,9 +358,30 @@ fn collect_files(s: &mut Session, p: &Value) -> Result { // ---------------------------------------------------------------- scripts -/// Run a command script: a JSON array or JSON lines of `{"command": id, "params": {…}}` (also -/// accepts the control channel's `{"method": "engine.execute", "params": {"id", "params"}}`). +/// File ▸ Scripts ▸ Run Script File…: `.jsx` / `.js` files run as JavaScript against the After +/// Effects-style object model ([`Session::script`]); anything else is a command script: a JSON +/// array or JSON lines of `{"command": id, "params": {…}}` (also accepts the control channel's +/// `{"method": "engine.execute", "params": {"id", "params"}}`). fn run_script(s: &mut Session, p: &Value) -> Result { + if let Some(path) = str_p(p, "path") + && p.get("steps").is_none() + && is_js_script(path) + { + if path.to_ascii_lowercase().ends_with(".jsxbin") { + return Err(bad("file.runScript", "binary .jsxbin scripts are not supported: run the .jsx source")); + } + let bytes = s.services.read_file(path).map_err(|e| EngineError::Other(format!("cannot read {path}: {e}")))?; + let code = String::from_utf8_lossy(&bytes).to_string(); + let name = std::path::Path::new(path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.to_string()); + let out = run_js(s, &code, path, false)?; + if let Some(e) = out.get("error").filter(|e| !e.is_null()) { + let line = e.get("line").and_then(Value::as_u64).map(|l| format!(" (line {l})")).unwrap_or_default(); + let msg = e.get("message").and_then(Value::as_str).unwrap_or("script error"); + s.events.push(crate::Event::Toast { message: format!("{name}{line}: {msg}"), error: true }); + return Err(EngineError::Other(format!("{name}{line}: {msg}"))); + } + return Ok(out); + } let steps: Vec = match (p.get("steps"), str_p(p, "path")) { (Some(Value::Array(a)), _) => a.clone(), (_, Some(path)) => { @@ -388,7 +409,7 @@ fn run_script(s: &mut Session, p: &Value) -> Result { } _ => return Err(bad("file.runScript", format!("step {}: needs `command`", i + 1))), }; - if id == "file.runScript" { + if id == "file.runScript" || id == "script.run" { return Err(bad("file.runScript", "scripts can't run scripts")); } let r = s.execute(&id, params).map_err(|e| EngineError::Other(format!("step {} ({id}): {e}", i + 1)))?; @@ -397,6 +418,28 @@ fn run_script(s: &mut Session, p: &Value) -> Result { Ok(json!({"steps": results.len(), "results": results})) } +/// `.jsx` / `.js` (and `.jsxbin`, rejected) are JavaScript; `.json` / `.jsonl` / other files are +/// command scripts. +fn is_js_script(path: &str) -> bool { + let l = path.to_ascii_lowercase(); + l.ends_with(".jsx") || l.ends_with(".js") || l.ends_with(".jsxbin") +} + +/// Run JavaScript through the session's scripting engine. +fn run_js(s: &mut Session, code: &str, name: &str, console: bool) -> Result { + let runner = s.script.ok_or_else(|| EngineError::Other("JavaScript scripting is not available in this build".into()))?; + Ok(runner(s, &crate::ScriptRequest { code, name, console })) +} + +/// `script.run`: run JavaScript (the After Effects scripting object model: `app.project`, comps, +/// layers, properties…) and report `{ok, result, output, error}`; script errors are reported in +/// `error` (with line and column), not as a command failure. +fn script_run(s: &mut Session, p: &Value) -> Result { + let code = str_p(p, "code").ok_or_else(|| bad("script.run", "missing `code` (JavaScript)"))?.to_string(); + let name = str_p(p, "name").unwrap_or("script").to_string(); + run_js(s, &code, &name, super::b_p(p, "console").unwrap_or(false)) +} + // ---------------------------------------------------------------- footage fn interpret(s: &mut Session, p: &Value) -> Result { @@ -638,10 +681,19 @@ pub fn specs() -> Vec { "Run Script File...", ["File", "Scripts"], None, - "{path (.jsonl/.json command script) | steps: [{command, params}]}", + "{path (.jsx/.js JavaScript, or a .jsonl/.json command script) | steps: [{command, params}]}", always, run_script ), + cmd!( + "script.run", + "Run Script", + [], + None, + "{code (JavaScript: app, app.project, CompItem, Layer, Property… like After Effects scripting), name?, console? (Script Console context)} → {ok, result, output, error: {message, line, column} | null}", + always, + script_run + ), cmd!( "file.interpretFootage", "Main...", diff --git a/crates/engine/src/commands/mod.rs b/crates/engine/src/commands/mod.rs index 4537752f..2e40eb63 100644 --- a/crates/engine/src/commands/mod.rs +++ b/crates/engine/src/commands/mod.rs @@ -25,6 +25,7 @@ pub mod paint; mod paths; mod project_items; mod prop; +mod prop_groups; pub mod puppet; mod query; mod render_queue; @@ -105,6 +106,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(text_edit::specs()); v.extend(layer_time::specs()); v.extend(prop::specs()); + v.extend(prop_groups::specs()); v.extend(project_items::specs()); v.extend(anim::specs()); v.extend(anim_tools::specs()); diff --git a/crates/engine/src/commands/project_items.rs b/crates/engine/src/commands/project_items.rs index 832f5851..a3949896 100644 --- a/crates/engine/src/commands/project_items.rs +++ b/crates/engine/src/commands/project_items.rs @@ -140,6 +140,92 @@ fn set_comment(s: &mut Session, p: &Value) -> Result { Ok(Value::Null) } +/// Delete project items (folders with their contents). Layers that use a deleted item and Render +/// Queue items of deleted comps go too, like After Effects' Edit ▸ Clear in the Project panel. +fn delete(s: &mut Session, p: &Value) -> Result { + let c = "project.delete"; + let ids = items_p(s, p, c)?; + let mut doomed: Vec = vec![]; + for id in s.project.items.keys() { + if ids.iter().any(|d| is_within(&s.project, *id, *d)) { + doomed.push(*id); + } + } + let n = doomed.len(); + s.edit("Delete Items", None, |proj, st| { + for id in &doomed { + proj.items.remove(id); + } + let comp_ids: Vec = proj.comps().map(|(id, _)| *id).collect(); + for cid in comp_ids { + let uses = proj.comp(cid).is_some_and(|c| c.layers.iter().any(|l| l.source.item().is_some_and(|i| doomed.contains(&i)))); + if !uses { + continue; + } + if let Some(comp) = proj.comp_mut(cid) { + let gone: Vec = + comp.layers.iter().filter(|l| l.source.item().is_some_and(|i| doomed.contains(&i))).map(|l| l.id).collect(); + comp.layers.retain(|l| !gone.contains(&l.id)); + for l in &mut comp.layers { + if l.parent.is_some_and(|x| gone.contains(&x)) { + l.parent = None; + } + if l.track_matte.is_some_and(|m| gone.contains(&m.layer)) { + l.track_matte = None; + } + } + } + } + proj.render_queue.retain(|r| !doomed.contains(&r.comp)); + st.project_selection.retain(|i| !doomed.contains(i)); + Ok(()) + })?; + s.sanitize_state(); + Ok(json!(n)) +} + +/// Duplicate project items (Edit ▸ Duplicate in the Project panel): comps get fresh layer ids and +/// a numbered name; the copies are selected. +fn duplicate(s: &mut Session, p: &Value) -> Result { + let c = "project.duplicate"; + let ids = items_p(s, p, c)?; + let mut created = vec![]; + s.edit("Duplicate", None, |proj, st| { + for id in &ids { + let it = proj.item(*id).ok_or(EngineError::Other("item vanished".into()))?.clone(); + if it.is_folder() { + return Err(bad(c, "folders can't be duplicated")); + } + let base = it.name.trim_end_matches(|ch: char| ch.is_ascii_digit()).trim_end().to_string(); + let name = (2..).map(|i| format!("{base} {i}")).find(|n| proj.find_by_name(n).is_none()).unwrap_or_default(); + let mut kind = it.kind.clone(); + if let effectcraft_project::ItemKind::Comp(comp) = &mut kind { + let comp = std::sync::Arc::make_mut(comp); + let mut map = std::collections::HashMap::new(); + for l in &mut comp.layers { + let new = effectcraft_project::LayerId(proj.alloc()); + map.insert(l.id, new); + l.id = new; + let mut next = proj.next_id; + l.props.reassign_uids(&mut next); + proj.next_id = next + 1; + } + for l in &mut comp.layers { + l.parent = l.parent.and_then(|p| map.get(&p).copied()); + if let Some(m) = &mut l.track_matte { + m.layer = map.get(&m.layer).copied().unwrap_or(m.layer); + } + } + } + let nid = proj.add_item(&name, it.label, it.parent, kind); + created.push(nid); + } + st.project_selection = created.clone(); + Ok(()) + })?; + Ok(json!({"items": created.iter().map(|i| i.0).collect::>()})) +} + pub fn specs() -> Vec { vec![ cmd!("project.select", "Select Project Items", [], None, "{items: [id|name], add?}", always, select), @@ -147,5 +233,15 @@ pub fn specs() -> Vec { cmd!("project.move", "Move to Folder", [], None, "{items?: [id|name] (default: selected), folder: id|name|null (root)}", always, move_items), cmd!("project.setLabel", "Item Label", [], None, "{items?, label: name|index}", always, set_label), cmd!("project.setComment", "Item Comment", [], None, "{items?, comment}", always, set_comment), + cmd!( + "project.delete", + "Delete Project Items", + [], + None, + "{items?: [id|name] (default: selected)} (folders with their contents; layers using the items go too)", + always, + delete + ), + cmd!("project.duplicate", "Duplicate Project Items", [], None, "{items?: [id|name] (default: selected)} → {items}", always, duplicate), ] } diff --git a/crates/engine/src/commands/prop_groups.rs b/crates/engine/src/commands/prop_groups.rs new file mode 100644 index 00000000..7f5f2a2c --- /dev/null +++ b/crates/engine/src/commands/prop_groups.rs @@ -0,0 +1,107 @@ +//! Generic edits of property-tree groups by uid: rename, enable, remove, duplicate and reorder an +//! effect, mask, shape item or text animator (what scripting's `PropertyGroup.name`, `enabled`, +//! `remove()`, `duplicate()` and `moveTo()` do, and what agents need without a UI selection). + +use effectcraft_project::{GroupKind, ItemId, LayerId, Node, PropGroup, Uid}; +use serde_json::{Value, json}; + +use super::{CommandSpec, b_p, bad, has_layers, layer_mut, layer_p, str_p}; +use crate::{EngineError, Result, Session, cmd}; + +/// Groups that can be renamed, removed, duplicated and reordered: instances in indexed lists. +fn is_instance(g: &PropGroup) -> bool { + matches!(g.kind, GroupKind::Indexed | GroupKind::Effect { .. } | GroupKind::Mask { .. } | GroupKind::Tracker { .. }) +} + +/// `{layer, prop: uid}` → (comp, layer, group uid), checking that the group is an instance. +fn group_ref(s: &Session, p: &Value, cmd: &str, need_instance: bool) -> Result<(ItemId, LayerId, Uid)> { + let (cid, lid) = layer_p(s, p, cmd)?; + let uid = p.get("prop").and_then(Value::as_u64).ok_or_else(|| bad(cmd, "missing `prop` (group uid)"))?; + let l = s.project.comp(cid).and_then(|c| c.layer(lid)).ok_or(EngineError::NoComp)?; + let g = l.props.find_group(uid).ok_or_else(|| bad(cmd, format!("no property group @{uid}")))?; + if need_instance && !is_instance(g) { + return Err(bad(cmd, format!("`{}` is a fixed group (only effects, masks, shape items and animators)", g.name))); + } + Ok((cid, lid, uid)) +} + +fn rename(s: &mut Session, p: &Value) -> Result { + let c = "prop.renameGroup"; + let (cid, lid, uid) = group_ref(s, p, c, true)?; + let name = str_p(p, "name").ok_or_else(|| bad(c, "missing `name`"))?.to_string(); + if name.trim().is_empty() { + return Err(bad(c, "the name can't be empty")); + } + s.edit("Rename", None, |proj, _| { + let g = layer_mut(proj, cid, lid)?.props.find_group_mut(uid).ok_or_else(|| bad(c, "group vanished"))?; + g.name = name.clone(); + Ok(json!(name)) + }) +} + +fn set_enabled(s: &mut Session, p: &Value) -> Result { + let c = "prop.setGroupEnabled"; + let (cid, lid, uid) = group_ref(s, p, c, true)?; + let v = b_p(p, "value"); + s.edit("Toggle", None, |proj, _| { + let g = layer_mut(proj, cid, lid)?.props.find_group_mut(uid).ok_or_else(|| bad(c, "group vanished"))?; + g.enabled = v.unwrap_or(!g.enabled); + Ok(json!(g.enabled)) + }) +} + +fn remove(s: &mut Session, p: &Value) -> Result { + let c = "prop.removeGroup"; + let (cid, lid, uid) = group_ref(s, p, c, true)?; + s.edit("Delete", None, |proj, st| { + let l = layer_mut(proj, cid, lid)?; + let parent = l.props.parent_of_mut(uid).ok_or_else(|| bad(c, "group vanished"))?; + parent.children.retain(|n| n.uid() != uid); + st.selected_props.retain(|(_, u)| *u != uid); + Ok(Value::Null) + }) +} + +fn duplicate(s: &mut Session, p: &Value) -> Result { + let c = "prop.duplicateGroup"; + let (cid, lid, uid) = group_ref(s, p, c, true)?; + s.edit("Duplicate", None, |proj, _| { + let mut next = proj.next_id; + let l = layer_mut(proj, cid, lid)?; + let parent = l.props.parent_of_mut(uid).ok_or_else(|| bad(c, "group vanished"))?; + let i = parent.children.iter().position(|n| n.uid() == uid).ok_or_else(|| bad(c, "group vanished"))?; + let Node::Group(mut g) = parent.children[i].clone() else { return Err(bad(c, "not a group")) }; + g.reassign_uids(&mut next); + let new = g.uid; + parent.children.insert(i + 1, Node::Group(g)); + proj.next_id = next + 1; + Ok(json!({"prop": new})) + }) +} + +fn move_to(s: &mut Session, p: &Value) -> Result { + let c = "prop.moveGroup"; + let (cid, lid, uid) = group_ref(s, p, c, true)?; + let to = p.get("index").and_then(Value::as_u64).ok_or_else(|| bad(c, "missing `index` (1-based)"))? as usize; + s.edit("Reorder", None, |proj, _| { + let l = layer_mut(proj, cid, lid)?; + let parent = l.props.parent_of_mut(uid).ok_or_else(|| bad(c, "group vanished"))?; + let i = parent.children.iter().position(|n| n.uid() == uid).ok_or_else(|| bad(c, "group vanished"))?; + if to < 1 || to > parent.children.len() { + return Err(bad(c, format!("index {to} is out of range 1..{}", parent.children.len()))); + } + let n = parent.children.remove(i); + parent.children.insert(to - 1, n); + Ok(json!(to)) + }) +} + +pub fn specs() -> Vec { + vec![ + cmd!("prop.renameGroup", "Rename Property Group", [], None, "{layer?, prop: uid, name}", has_layers, rename), + cmd!("prop.setGroupEnabled", "Enable Property Group", [], None, "{layer?, prop: uid, value?}", has_layers, set_enabled), + cmd!("prop.removeGroup", "Delete Property Group", [], None, "{layer?, prop: uid}", has_layers, remove), + cmd!("prop.duplicateGroup", "Duplicate Property Group", [], None, "{layer?, prop: uid} → {prop: new uid}", has_layers, duplicate), + cmd!("prop.moveGroup", "Reorder Property Group", [], None, "{layer?, prop: uid, index (1-based among its siblings)}", has_layers, move_to), + ] +} diff --git a/crates/engine/src/commands/text_anim.rs b/crates/engine/src/commands/text_anim.rs index 73f34c5e..51367da9 100644 --- a/crates/engine/src/commands/text_anim.rs +++ b/crates/engine/src/commands/text_anim.rs @@ -114,7 +114,8 @@ fn add_animator(s: &mut Session, p: &Value) -> Result { let name = str_p(p, "name").map(str::to_string).unwrap_or_else(|| format!("Animator {}", anims.children.len() + 1)); let mut g = build::text_animator(&mut ids, &name, vec![]); let added: usize = kinds.iter().map(|k| add_props(&mut ids, &mut g, k, three)).sum(); - if added == 0 { + // `properties: []` adds an empty animator (scripting's addProperty("ADBE Text Animator")). + if added == 0 && !kinds.is_empty() { return Err(bad(C, "unknown animator property")); } let uid = g.uid; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index ba34488a..063b9bdf 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -285,8 +285,25 @@ pub struct Session { pub autosave: autosave::AutoSaveState, /// The viewer snapshot (Take Snapshot / Show Snapshot). pub snapshot: Option, + /// The JavaScript scripting engine (set by the host that links `effectcraft-script`): + /// `script.run`, File ▸ Scripts ▸ Run Script File… (`.jsx`/`.js`) and the Script Console. + pub script: Option, } +/// A script to run (see [`Session::script`]). +#[derive(Clone, Copy, Debug)] +pub struct ScriptRequest<'a> { + pub code: &'a str, + /// File name shown in errors and `$.fileName` (`console` for the Script Console). + pub name: &'a str, + /// Run in the Script Console's persistent context (variables survive between runs). + pub console: bool, +} + +/// Runs a script against the session and reports `{ok, result, output, error: {message, line, +/// column, file} | null}`. +pub type ScriptRunner = fn(&mut Session, &ScriptRequest) -> Value; + impl Default for Session { fn default() -> Self { Session { @@ -319,6 +336,7 @@ impl Default for Session { shortcut_table: std::sync::OnceLock::new(), autosave: autosave::AutoSaveState::default(), snapshot: None, + script: None, } } } diff --git a/crates/engine/src/menus.rs b/crates/engine/src/menus.rs index 38252a88..065d1762 100644 --- a/crates/engine/src/menus.rs +++ b/crates/engine/src/menus.rs @@ -942,6 +942,7 @@ Window Preview | window.panel {"panel":"preview"} | Cmd+3 Progress | window.unavailablePanel {"panel":"progress"} Properties | window.panel {"panel":"properties"} + Script Console | window.panel {"panel":"scriptConsole"} Smoother | window.panel {"panel":"smoother"} Tools | window.panel {"panel":"tools"} | Cmd+1 Tracker | window.panel {"panel":"tracker"} diff --git a/crates/engine/src/tests_project_items.rs b/crates/engine/src/tests_project_items.rs index 4d097aca..4ab9ce2b 100644 --- a/crates/engine/src/tests_project_items.rs +++ b/crates/engine/src/tests_project_items.rs @@ -39,3 +39,60 @@ fn move_into_folder_rename_label_comment_with_undo() { s.execute("edit.undo", json!({})).unwrap(); assert_eq!(s.project.item(comp).unwrap().comment, ""); } + +#[test] +fn delete_and_duplicate_items() { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Inner", "width": 64, "height": 64, "duration": 1})).unwrap(); + let inner = s.active_comp_id().unwrap(); + s.execute("layer.newNull", json!({})).unwrap(); + s.execute("comp.new", json!({"name": "Outer", "width": 64, "height": 64, "duration": 1})).unwrap(); + let outer = s.active_comp_id().unwrap(); + s.execute("layer.addItem", json!({"item": inner.0})).unwrap(); + s.execute("renderQueue.add", json!({"comp": inner.0})).unwrap(); + // Duplicate: a new comp with fresh layer ids. + let r = s.execute("project.duplicate", json!({"items": [inner.0]})).unwrap(); + let dup = ItemId(r["items"][0].as_u64().unwrap()); + assert_eq!(s.project.item(dup).unwrap().name, "Inner 2"); + let a = s.project.comp(inner).unwrap().layers[0].id; + let b = s.project.comp(dup).unwrap().layers[0].id; + assert_ne!(a, b); + // Delete: layers that use the item and its render items go too; undoable. + s.execute("project.delete", json!({"items": ["Inner"]})).unwrap(); + assert!(s.project.item(inner).is_none()); + assert!(s.project.comp(outer).unwrap().layers.is_empty()); + assert!(s.project.render_queue.is_empty()); + s.undo(); + assert!(s.project.item(inner).is_some()); + assert_eq!(s.project.comp(outer).unwrap().layers.len(), 1); + // Folders take their contents. + let f = ItemId(s.execute("project.newFolder", json!({"name": "F"})).unwrap()["item"].as_u64().unwrap()); + s.execute("project.move", json!({"items": [dup.0], "folder": f.0})).unwrap(); + s.execute("project.delete", json!({"items": [f.0]})).unwrap(); + assert!(s.project.item(dup).is_none()); +} + +#[test] +fn property_group_edits() { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "C", "width": 64, "height": 64, "duration": 1})).unwrap(); + let l = s.execute("layer.newSolid", json!({})).unwrap()["layer"].as_u64().unwrap(); + let a = s.execute("effect.apply", json!({"layer": l, "effect": "Gaussian Blur"})).unwrap()["effects"][0].as_u64().unwrap(); + let b = s.execute("effect.apply", json!({"layer": l, "effect": "Invert"})).unwrap()["effects"][0].as_u64().unwrap(); + let fx = |s: &Session| -> Vec<(u64, String, bool)> { + s.active_comp().unwrap().layers[0].props.sub("effects").unwrap().groups().map(|g| (g.uid, g.name.clone(), g.enabled)).collect() + }; + s.execute("prop.renameGroup", json!({"layer": l, "prop": a, "name": "Soft"})).unwrap(); + s.execute("prop.setGroupEnabled", json!({"layer": l, "prop": a, "value": false})).unwrap(); + s.execute("prop.moveGroup", json!({"layer": l, "prop": b, "index": 1})).unwrap(); + let d = s.execute("prop.duplicateGroup", json!({"layer": l, "prop": a})).unwrap()["prop"].as_u64().unwrap(); + let v = fx(&s); + assert_eq!(v.iter().map(|x| x.0).collect::>(), [b, a, d]); + assert_eq!(v[1].1, "Soft"); + assert!(!v[1].2); + s.execute("prop.removeGroup", json!({"layer": l, "prop": a})).unwrap(); + assert_eq!(fx(&s).len(), 2); + // Fixed groups can't be removed. + let tr = s.active_comp().unwrap().layers[0].props.sub("transform").unwrap().uid; + assert!(s.execute("prop.removeGroup", json!({"layer": l, "prop": tr})).is_err()); +} diff --git a/crates/host/Cargo.toml b/crates/host/Cargo.toml index b43271c7..dc7965d9 100644 --- a/crates/host/Cargo.toml +++ b/crates/host/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "effectcraft-host" -description = "Builds a fully wired EffectCraft session: media decoding (FilmCraft codecs), importer and the expression engine" +description = "Builds a fully wired EffectCraft session: media decoding (FilmCraft codecs), importer, the expression engine and scripting" version.workspace = true edition.workspace = true license.workspace = true @@ -14,6 +14,7 @@ effectcraft-engine = { workspace = true } effectcraft-project = { workspace = true } effectcraft-media = { workspace = true } effectcraft-expr = { workspace = true } +effectcraft-script = { workspace = true } effectcraft-export = { workspace = true } [dev-dependencies] diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index 16199dfb..5235feca 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -1,12 +1,14 @@ //! A fully wired [`Session`]: footage decoding through `effectcraft-media` (FilmCraft's codecs), -//! the media importer, the expression engine and Render Queue export through -//! `effectcraft-export` (FilmCraft's encoders). Frontends (desktop, CLI, MCP, web) start here. +//! the media importer, the expression engine, JavaScript scripting (`effectcraft-script`) and +//! Render Queue export through `effectcraft-export` (FilmCraft's encoders). Frontends (desktop, +//! CLI, MCP, web) start here. use std::sync::Arc; use effectcraft_engine::project::render_queue::OutputFormat; use effectcraft_engine::{ExportJob, ExportResult, Exporter, Importer, Session}; use effectcraft_project::Footage; +pub use effectcraft_script as script; struct MediaImporter; @@ -47,7 +49,7 @@ impl Exporter for FileExporter { } } -/// A new session with media, import, expressions and export enabled. +/// A new session with media, import, expressions, scripting and export enabled. pub fn session() -> Session { Session { exporter: Some(Arc::new(FileExporter::default())), @@ -55,6 +57,7 @@ pub fn session() -> Session { importer: Some(Arc::new(MediaImporter)), expr: Some(Arc::new(effectcraft_expr::Expressions)), expr_check: Some(effectcraft_expr::check_syntax), + script: Some(effectcraft_script::runner), ..Default::default() } } @@ -188,6 +191,35 @@ mod tests { assert_eq!(value(&s, a, "transform/rotation").as_f64(), 12.0); } + /// A script builds a comp, queues it and renders it through the Render Queue. + #[test] + fn script_renders_through_the_render_queue() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/test-out/host-script"); + let _ = std::fs::remove_dir_all(&dir); + let out = dir.join("scripted.gif"); + let mut s = super::session(); + let code = format!( + r#" + var comp = app.project.items.addComp("Scripted", 64, 48, 1, 0.2, 10); + var sq = comp.layers.addSolid([1, 0.5, 0], "Square", 16, 16, 1); + sq.transform.position.setValueAtTime(0, [8, 24]); + sq.transform.position.setValueAtTime(0.1, [56, 24]); + var item = app.project.renderQueue.items.add(comp); + item.outputModule(1).applyTemplate("GIF"); + item.outputModule(1).file = new File({}); + app.project.renderQueue.render(); + [item.status == RQItemStatus.DONE, item.outputModule(1).file.fsName, app.project.renderQueue.numItems] + "#, + serde_json::to_string(&out.to_string_lossy()).unwrap() + ); + let r = s.execute("script.run", json!({"code": code})).unwrap(); + assert_eq!(r["ok"], json!(true), "{r}"); + assert_eq!(r["result"][0], json!(true), "{r}"); + assert_eq!(r["result"][2], json!(1)); + let bytes = std::fs::read(&out).unwrap(); + assert!(bytes.starts_with(b"GIF89a")); + } + #[test] fn expression_syntax_errors_disable_the_expression() { let mut s = super::session(); diff --git a/crates/script/Cargo.toml b/crates/script/Cargo.toml new file mode 100644 index 00000000..ef756107 --- /dev/null +++ b/crates/script/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "effectcraft-script" +description = "EffectCraft scripting: JavaScript (boa) with an After Effects-style object model (app, project, CompItem, Layer, Property…) over the engine's undoable commands" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] +effectcraft-engine = { workspace = true } +effectcraft-project = { workspace = true } +effectcraft-keyframe = { workspace = true } +effectcraft-time = { workspace = true } +effectcraft-render = { workspace = true } +effectcraft-path = { workspace = true } +serde_json = { workspace = true } +boa_engine = { workspace = true } + +# On the web, boa's clock and Math.random need the browser (getrandom `wasm_js`, web-time). +[target.'cfg(target_arch = "wasm32")'.dependencies] +boa_engine = { workspace = true, features = ["js"] } + +[dev-dependencies] +effectcraft-expr = { workspace = true } diff --git a/crates/script/src/lib.rs b/crates/script/src/lib.rs new file mode 100644 index 00000000..a1e9e5d4 --- /dev/null +++ b/crates/script/src/lib.rs @@ -0,0 +1,64 @@ +//! # effectcraft-script (L4) +//! +//! Scripting with an After Effects-style object model, on the boa JavaScript engine (the one the +//! expressions use). Scripts written for After Effects' documented scripting API mostly run +//! unchanged: +//! +//! ```text +//! app.beginUndoGroup("Build"); +//! var comp = app.project.items.addComp("Main", 1920, 1080, 1, 10, 30); +//! var solid = comp.layers.addSolid([1, 0, 0], "Red", 1920, 1080, 1); +//! solid.property("ADBE Transform Group").property("ADBE Position").setValueAtTime(0, [0, 540]); +//! solid.Effects.addProperty("ADBE Gaussian Blur 2").property("Blurriness").setValue(20); +//! app.endUndoGroup(); +//! ``` +//! +//! * **Object model** (`prelude.js`): `app` (project, open/newProject, undo groups, +//! `executeCommand`/`findMenuCommandId`, `scheduleTask`), `Project`, `ItemCollection`, +//! `CompItem`/`FootageItem`/`FolderItem`, `LayerCollection`, `AVLayer`/`TextLayer`/ +//! `ShapeLayer`/`CameraLayer`/`LightLayer`, `PropertyGroup`/`Property` (values, keyframes, +//! eases, expressions), `MaskPropertyGroup`, `TextDocument`, `Shape`, `KeyframeEase`, +//! `MarkerValue`, `RenderQueue`/`RenderQueueItem`/`OutputModule`, `ImportOptions`, `File`/ +//! `Folder`, `$`, `alert`/`writeLn`, and the enums (`BlendingMode`, `KeyframeInterpolationType`, +//! `TrackMatteType`, `LightType`, `ParagraphJustification`, `PropertyValueType`…). +//! * **Edits are engine commands**: every mutating call runs a command through +//! [`Session::execute`](effectcraft_engine::Session::execute), so it is undoable, journaled +//! and identical to the UI's action; `app.beginUndoGroup`/`endUndoGroup` fold everything in +//! between into one undo step. +//! * **Match names** ([`matchnames`]): After Effects' documented match names for our properties +//! and effects (`ADBE Transform Group`, `ADBE Position`, `ADBE Gaussian Blur 2`…). +//! * **Security**: scripts can import/open/save projects and render, but `File` reads are limited +//! to the project's folder and writes (and the network) are off unless Preferences ▸ Scripting +//! & Expressions ▸ Allow Scripts to Write Files and Access Network is on. +//! +//! Entry points: [`install`] sets [`Session::script`](effectcraft_engine::Session::script), which +//! the `script.run` command, File ▸ Scripts ▸ Run Script File… (`.jsx`/`.js`), the Script Console +//! panel, `effectcraft-cli script` and the MCP `run_script` tool use. + +#![recursion_limit = "256"] + +pub mod matchnames; +mod model; +mod runtime; + +use effectcraft_engine::{ScriptRequest, Session}; + +pub use runtime::{Outcome, ScriptError, run}; + +/// The [`effectcraft_engine::ScriptRunner`] this crate provides. +pub fn runner(s: &mut Session, req: &ScriptRequest) -> serde_json::Value { + run(s, req).to_json() +} + +/// Enable scripting on a session. +pub fn install(s: &mut Session) { + s.script = Some(runner); +} + +/// Run `code` as a one-off script named `name`. +pub fn run_code(s: &mut Session, code: &str, name: &str) -> Outcome { + run(s, &ScriptRequest { code, name, console: false }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/script/src/matchnames.rs b/crates/script/src/matchnames.rs new file mode 100644 index 00000000..623027c1 --- /dev/null +++ b/crates/script/src/matchnames.rs @@ -0,0 +1,443 @@ +//! After Effects-compatible match names for our property tree and effects, so common scripts +//! (`layer.property("ADBE Transform Group").property("ADBE Position")`, +//! `layer.Effects.addProperty("ADBE Gaussian Blur 2")`) work unchanged. +//! +//! The names are the stable identifiers After Effects documents for scripting (Scripting Guide +//! ▸ match names). Properties without a documented equivalent keep our own match id +//! (`perChar3d`, `ec.blur.bilateral`), which `property()` and `addProperty()` accept too. + +use effectcraft_project::{LayerSource, Node}; + +/// Match name of a layer (its root property group). +pub fn layer(source: &LayerSource) -> &'static str { + match source { + LayerSource::Text => "ADBE Text Layer", + LayerSource::Shape => "ADBE Vector Layer", + LayerSource::Camera => "ADBE Camera Layer", + LayerSource::Light { .. } => "ADBE Light Layer", + _ => "ADBE AV Layer", + } +} + +/// Top-level groups of a layer. +const LAYER_GROUPS: &[(&str, &str)] = &[ + ("transform", "ADBE Transform Group"), + ("masks", "ADBE Mask Parade"), + ("effects", "ADBE Effect Parade"), + ("text", "ADBE Text Properties"), + ("contents", "ADBE Root Vectors Group"), + ("materialOptions", "ADBE Material Options Group"), + ("cameraOptions", "ADBE Camera Options Group"), + ("lightOptions", "ADBE Light Options Group"), + ("audio", "ADBE Audio Group"), + ("layerStyles", "ADBE Layer Styles"), + ("motionTrackers", "ADBE MTrackers"), + ("timeRemap", "ADBE Time Remapping"), +]; + +const TRANSFORM: &[(&str, &str)] = &[ + ("anchor", "ADBE Anchor Point"), + // Cameras and lights store the Point of Interest where AVLayers keep the Anchor Point. + ("poi", "ADBE Anchor Point"), + ("position", "ADBE Position"), + ("positionX", "ADBE Position_0"), + ("positionY", "ADBE Position_1"), + ("positionZ", "ADBE Position_2"), + ("scale", "ADBE Scale"), + ("orientation", "ADBE Orientation"), + ("rotationX", "ADBE Rotate X"), + ("rotationY", "ADBE Rotate Y"), + ("rotation", "ADBE Rotate Z"), + ("opacity", "ADBE Opacity"), +]; + +const MASK: &[(&str, &str)] = + &[("path", "ADBE Mask Shape"), ("feather", "ADBE Mask Feather"), ("opacity", "ADBE Mask Opacity"), ("expansion", "ADBE Mask Offset")]; + +const TEXT: &[(&str, &str)] = &[ + ("sourceText", "ADBE Text Document"), + ("pathOptions", "ADBE Text Path Options"), + ("moreOptions", "ADBE Text More Options"), + ("animators", "ADBE Text Animators"), +]; + +const TEXT_ANIMATOR: &[(&str, &str)] = &[("selectors", "ADBE Text Selectors"), ("properties", "ADBE Text Animator Properties")]; + +const CAMERA: &[(&str, &str)] = &[ + ("zoom", "ADBE Camera Zoom"), + ("dof", "ADBE Camera Depth of Field"), + ("focusDistance", "ADBE Camera Focus Distance"), + ("aperture", "ADBE Camera Aperture"), + ("blurLevel", "ADBE Camera Blur Level"), +]; + +const LIGHT: &[(&str, &str)] = &[ + ("intensity", "ADBE Light Intensity"), + ("color", "ADBE Light Color"), + ("coneAngle", "ADBE Light Cone Angle"), + ("coneFeather", "ADBE Light Cone Feather 2"), + ("falloff", "ADBE Light Falloff Type"), + ("radius", "ADBE Light Falloff Start"), + ("falloffDistance", "ADBE Light Falloff Distance"), + ("castsShadows", "ADBE Light Casts Shadows"), + ("shadowDarkness", "ADBE Light Shadow Darkness"), + ("shadowDiffusion", "ADBE Light Shadow Diffusion"), +]; + +const MATERIAL: &[(&str, &str)] = &[ + ("castsShadows", "ADBE Casts Shadows"), + ("lightTransmission", "ADBE Light Transmission"), + ("acceptsShadows", "ADBE Accepts Shadows"), + ("acceptsLights", "ADBE Accepts Lights"), + ("ambient", "ADBE Ambient Coefficient"), + ("diffuse", "ADBE Diffuse Coefficient"), + ("specularIntensity", "ADBE Specular Coefficient"), + ("specularShininess", "ADBE Shininess Coefficient"), + ("metal", "ADBE Metal Coefficient"), +]; + +/// Shape items (Contents ▸ Add): our match id → AE match name. +pub const SHAPE_ITEMS: &[(&str, &str)] = &[ + ("group", "ADBE Vector Group"), + ("rect", "ADBE Vector Shape - Rect"), + ("ellipse", "ADBE Vector Shape - Ellipse"), + ("star", "ADBE Vector Shape - Star"), + ("path", "ADBE Vector Shape - Group"), + ("fill", "ADBE Vector Graphic - Fill"), + ("stroke", "ADBE Vector Graphic - Stroke"), + ("gfill", "ADBE Vector Graphic - G-Fill"), + ("gstroke", "ADBE Vector Graphic - G-Stroke"), + ("merge", "ADBE Vector Filter - Merge"), + ("offset", "ADBE Vector Filter - Offset"), + ("pucker", "ADBE Vector Filter - PB"), + ("repeater", "ADBE Vector Filter - Repeater"), + ("round", "ADBE Vector Filter - RC"), + ("trim", "ADBE Vector Filter - Trim"), + ("twist", "ADBE Vector Filter - Twist"), + ("wiggle", "ADBE Vector Filter - Roughen"), + ("zigzag", "ADBE Vector Filter - Zigzag"), +]; + +/// Properties inside shape items: (item match id, property match id, AE match name). +const SHAPE_PROPS: &[(&str, &str, &str)] = &[ + ("group", "blend", "ADBE Vector Blend Mode"), + ("group", "contents", "ADBE Vectors Group"), + ("group", "transform", "ADBE Vector Transform Group"), + ("transform", "anchor", "ADBE Vector Anchor"), + ("transform", "position", "ADBE Vector Position"), + ("transform", "scale", "ADBE Vector Scale"), + ("transform", "skew", "ADBE Vector Skew"), + ("transform", "skewAxis", "ADBE Vector Skew Axis"), + ("transform", "rotation", "ADBE Vector Rotation"), + ("transform", "opacity", "ADBE Vector Group Opacity"), + ("rect", "direction", "ADBE Vector Shape Direction"), + ("rect", "size", "ADBE Vector Rect Size"), + ("rect", "position", "ADBE Vector Rect Position"), + ("rect", "roundness", "ADBE Vector Rect Roundness"), + ("ellipse", "direction", "ADBE Vector Shape Direction"), + ("ellipse", "size", "ADBE Vector Ellipse Size"), + ("ellipse", "position", "ADBE Vector Ellipse Position"), + ("star", "direction", "ADBE Vector Shape Direction"), + ("star", "type", "ADBE Vector Star Type"), + ("star", "points", "ADBE Vector Star Points"), + ("star", "position", "ADBE Vector Star Position"), + ("star", "rotation", "ADBE Vector Star Rotation"), + ("star", "innerRadius", "ADBE Vector Star Inner Radius"), + ("star", "outerRadius", "ADBE Vector Star Outer Radius"), + ("star", "innerRoundness", "ADBE Vector Star Inner Roundess"), + ("star", "outerRoundness", "ADBE Vector Star Outer Roundess"), + ("path", "direction", "ADBE Vector Shape Direction"), + ("path", "path", "ADBE Vector Shape"), + ("fill", "blend", "ADBE Vector Blend Mode"), + ("fill", "composite", "ADBE Vector Composite Order"), + ("fill", "rule", "ADBE Vector Fill Rule"), + ("fill", "color", "ADBE Vector Fill Color"), + ("fill", "opacity", "ADBE Vector Fill Opacity"), + ("stroke", "blend", "ADBE Vector Blend Mode"), + ("stroke", "composite", "ADBE Vector Composite Order"), + ("stroke", "color", "ADBE Vector Stroke Color"), + ("stroke", "opacity", "ADBE Vector Stroke Opacity"), + ("stroke", "width", "ADBE Vector Stroke Width"), + ("stroke", "cap", "ADBE Vector Stroke Line Cap"), + ("stroke", "join", "ADBE Vector Stroke Line Join"), + ("stroke", "miter", "ADBE Vector Stroke Miter Limit"), + ("stroke", "dashes", "ADBE Vector Stroke Dashes"), + ("trim", "start", "ADBE Vector Trim Start"), + ("trim", "end", "ADBE Vector Trim End"), + ("trim", "offset", "ADBE Vector Trim Offset"), + ("trim", "mode", "ADBE Vector Trim Type"), + ("repeater", "copies", "ADBE Vector Repeater Copies"), + ("repeater", "offset", "ADBE Vector Repeater Offset"), + ("repeater", "composite", "ADBE Vector Repeater Order"), + ("repeater", "transform", "ADBE Vector Repeater Transform"), + ("round", "radius", "ADBE Vector RoundCorner Radius"), + ("pucker", "amount", "ADBE Vector PuckerBloat Amount"), + ("twist", "angle", "ADBE Vector Twist Angle"), + ("twist", "center", "ADBE Vector Twist Center"), + ("merge", "mode", "ADBE Vector Merge Type"), +]; + +/// Effects: our effect id → AE match name (the documented ones; the rest keep our id). +pub const EFFECTS: &[(&str, &str)] = &[ + ("ec.blur.gaussian", "ADBE Gaussian Blur 2"), + ("ec.blur.fastbox", "ADBE Box Blur2"), + ("ec.blur.directional", "ADBE Motion Blur"), + ("ec.blur.radial", "ADBE Radial Blur"), + ("ec.blur.cameralens", "ADBE Camera Lens Blur"), + ("ec.blur.channel", "ADBE Channel Blur"), + ("ec.blur.compound", "ADBE Compound Blur"), + ("ec.blur.sharpen", "ADBE Sharpen"), + ("ec.blur.smart", "ADBE Smart Blur"), + ("ec.blur.unsharp", "ADBE Unsharp Mask2"), + ("ec.channel.invert", "ADBE Invert"), + ("ec.channel.setmatte", "ADBE Set Matte3"), + ("ec.channel.setchannels", "ADBE Set Channels"), + ("ec.channel.shiftchannels", "ADBE Shift Channels"), + ("ec.channel.minimax", "ADBE Minimax"), + ("ec.channel.blend", "ADBE Blend"), + ("ec.channel.arithmetic", "ADBE Arithmetic"), + ("ec.channel.calculations", "ADBE Calculations"), + ("ec.channel.solidcomposite", "ADBE Solid Composite"), + ("ec.color.brightnesscontrast", "ADBE Brightness & Contrast 2"), + ("ec.color.curves", "ADBE CurvesCustom"), + ("ec.color.levels", "ADBE Easy Levels2"), + ("ec.color.levelsic", "ADBE Pro Levels2"), + ("ec.color.huesaturation", "ADBE HUE SATURATION"), + ("ec.color.exposure", "ADBE Exposure2"), + ("ec.color.tint", "ADBE Tint"), + ("ec.color.tritone", "ADBE Tritone"), + ("ec.color.blackwhite", "ADBE Black&White"), + ("ec.color.channelmixer", "ADBE CHANNEL MIXER"), + ("ec.color.colorbalance", "ADBE Color Balance 2"), + ("ec.color.colorbalancehls", "ADBE Color Balance (HLS)"), + ("ec.color.changecolor", "ADBE Change Color"), + ("ec.color.changetocolor", "ADBE Change To Color"), + ("ec.color.colorama", "APC Colorama"), + ("ec.color.leavecolor", "ADBE Leave Color"), + ("ec.color.photofilter", "ADBE Photo Filter"), + ("ec.color.vibrance", "ADBE Vibrance"), + ("ec.color.lumetri", "ADBE Lumetri"), + ("ec.color.shadowhighlight", "ADBE Shadow/Highlight"), + ("ec.color.selectivecolor", "ADBE Selective Color"), + ("ec.color.gammapedestalgain", "ADBE Gamma/Pedestal/Gain"), + ("ec.color.equalize", "ADBE Equalize"), + ("ec.color.autocontrast", "ADBE AutoContrast"), + ("ec.color.autolevels", "ADBE AutoLevels"), + ("ec.color.autocolor", "ADBE AutoColor"), + ("ec.control.slider", "ADBE Slider Control"), + ("ec.control.angle", "ADBE Angle Control"), + ("ec.control.checkbox", "ADBE Checkbox Control"), + ("ec.control.color", "ADBE Color Control"), + ("ec.control.point", "ADBE Point Control"), + ("ec.control.point3d", "ADBE Point3D Control"), + ("ec.control.layer", "ADBE Layer Control"), + ("ec.control.dropdown", "ADBE Dropdown Control"), + ("ec.distort.transform", "ADBE Geometry2"), + ("ec.distort.cornerpin", "ADBE Corner Pin"), + ("ec.distort.bulge", "ADBE Bulge"), + ("ec.distort.mirror", "ADBE Mirror"), + ("ec.distort.offset", "ADBE Offset"), + ("ec.distort.ripple", "ADBE Ripple"), + ("ec.distort.spherize", "ADBE Spherize"), + ("ec.distort.twirl", "ADBE Twirl"), + ("ec.distort.wavewarp", "ADBE Wave Warp"), + ("ec.distort.turbulentdisplace", "ADBE Turbulent Displace"), + ("ec.distort.displacementmap", "ADBE Displacement Map"), + ("ec.distort.meshwarp", "ADBE MESH WARP"), + ("ec.distort.polar", "ADBE Polar Coordinates"), + ("ec.distort.opticscompensation", "ADBE Optics Compensation"), + ("ec.distort.magnify", "ADBE Magnify"), + ("ec.distort.warpstabilizer", "ADBE SubspaceStabilizer"), + ("ec.distort.puppet", "ADBE FreePin3"), + ("ec.generate.fill", "ADBE Fill"), + ("ec.generate.gradientramp", "ADBE Ramp"), + ("ec.generate.fourcolor", "ADBE 4ColorGradient"), + ("ec.generate.stroke", "ADBE Stroke"), + ("ec.generate.grid", "ADBE Grid"), + ("ec.generate.checkerboard", "ADBE Checkerboard"), + ("ec.generate.circle", "ADBE Circle"), + ("ec.generate.ellipse", "ADBE Ellipse"), + ("ec.generate.cellpattern", "ADBE Cell Pattern"), + ("ec.generate.lensflare", "ADBE Lens Flare"), + ("ec.generate.vegas", "ADBE Vegas"), + ("ec.generate.writeon", "ADBE Write On"), + ("ec.generate.audiospectrum", "ADBE AudSpect"), + ("ec.generate.audiowaveform", "ADBE AudWave"), + ("ec.generate.beam", "ADBE Laser"), + ("ec.generate.radiowaves", "ADBE Radio Waves"), + ("ec.generate.scribble", "ADBE Scribble Fill"), + ("ec.generate.paintbucket", "ADBE Paint Bucket"), + ("ec.key.colorkey", "ADBE Color Key"), + ("ec.key.luma", "ADBE Luma Key"), + ("ec.key.linearcolor", "ADBE Linear Color Key2"), + ("ec.key.colorrange", "ADBE Color Range"), + ("ec.key.colordifference", "ADBE Color Difference Key"), + ("ec.key.extract", "ADBE Extract"), + ("ec.key.spill", "ADBE Spill2"), + ("ec.matte.simplechoker", "ADBE Simple Choker"), + ("ec.matte.mattechoker", "ADBE Matte Choker"), + ("ec.noise.noise", "ADBE Noise"), + ("ec.noise.fractal", "ADBE Fractal Noise"), + ("ec.noise.median", "ADBE Median"), + ("ec.noise.addgrain", "VISINF Grain Implant"), + ("ec.noise.dustscratches", "ADBE Dust & Scratches"), + ("ec.noise.turbulent", "ADBE Turbulent Noise"), + ("ec.perspective.dropshadow", "ADBE Drop Shadow"), + ("ec.perspective.radialshadow", "ADBE Radial Shadow"), + ("ec.perspective.bevelalpha", "ADBE Bevel Alpha"), + ("ec.perspective.beveledges", "ADBE Bevel Edges"), + ("ec.perspective.3dglasses", "ADBE 3D Glasses2"), + ("ec.stylize.glow", "ADBE Glo2"), + ("ec.stylize.mosaic", "ADBE Mosaic"), + ("ec.stylize.posterize", "ADBE Posterize"), + ("ec.stylize.threshold", "ADBE Threshold2"), + ("ec.stylize.findedges", "ADBE Find Edges"), + ("ec.stylize.emboss", "ADBE Emboss"), + ("ec.stylize.coloremboss", "ADBE Color Emboss"), + ("ec.stylize.roughenedges", "ADBE Roughen Edges"), + ("ec.stylize.scatter", "ADBE Scatter"), + ("ec.stylize.strobe", "ADBE Strobe"), + ("ec.stylize.motiontile", "ADBE Tile"), + ("ec.stylize.texturize", "ADBE Texturize"), + ("ec.stylize.cartoon", "ADBE Cartoonify"), + ("ec.stylize.brushstrokes", "ADBE Brush Strokes"), + ("ec.text.numbers", "ADBE Numbers2"), + ("ec.text.timecode", "ADBE Timecode"), + ("ec.time.echo", "ADBE Echo"), + ("ec.time.posterizetime", "ADBE Posterize Time"), + ("ec.time.timedisplacement", "ADBE Time Displacement"), + ("ec.time.timedifference", "ADBE Time Difference"), + ("ec.time.pixelmotionblur", "ADBE Pixel Motion Blur"), + ("ec.time.timewarp", "ADBE Timewarp"), + ("ec.transition.linearwipe", "ADBE Linear Wipe"), + ("ec.transition.radialwipe", "ADBE Radial Wipe"), + ("ec.transition.venetian", "ADBE Venetian Blinds"), + ("ec.transition.blockdissolve", "ADBE Block Dissolve"), + ("ec.transition.gradientwipe", "ADBE Gradient Wipe"), + ("ec.transition.iriswipe", "ADBE Iris Wipe"), + ("ec.utility.applylut", "ADBE Apply Color LUT2"), + ("ec.utility.cineon", "ADBE Cineon Converter2"), + ("ec.utility.growbounds", "ADBE Grow Bounds"), +]; + +fn find<'a>(t: &'a [(&str, &'a str)], k: &str) -> Option<&'a str> { + t.iter().find(|(a, _)| *a == k).map(|(_, b)| *b) +} + +/// AE match name of an effect id (falls back to the id). +pub fn effect(id: &str) -> String { + find(EFFECTS, id).map(str::to_string).unwrap_or_else(|| id.to_string()) +} + +/// Our effect id for an AE effect match name. +pub fn effect_from_match(m: &str) -> Option<&'static str> { + EFFECTS.iter().find(|(_, b)| b.eq_ignore_ascii_case(m)).map(|(a, _)| *a) +} + +/// Our shape item kind (`layer.addShapeItem`) for an AE match name, our match id or a display +/// name (`Rectangle`, `Fill`, `Trim Paths`). +pub fn shape_kind(name: &str) -> Option<&'static str> { + if let Some((k, _)) = SHAPE_ITEMS.iter().find(|(k, m)| m.eq_ignore_ascii_case(name) || k.eq_ignore_ascii_case(name)) { + return Some(k); + } + let n: String = name.chars().filter(|c| c.is_alphanumeric()).collect::().to_ascii_lowercase(); + Some(match n.as_str() { + "rectangle" | "rectanglepath" => "rect", + "ellipsepath" => "ellipse", + "polystar" | "polystarpath" => "star", + "polygon" => "polygon", + "group" | "vectorgroup" => "group", + "fill" => "fill", + "stroke" => "stroke", + "gradientfill" => "gfill", + "gradientstroke" => "gstroke", + "trimpaths" | "trim" => "trim", + "repeater" => "repeater", + "roundcorners" => "round", + "offsetpaths" => "offset", + "puckerbloat" | "puckerandbloat" => "pucker", + "twist" => "twist", + "zigzag" => "zigzag", + "wigglepaths" => "wiggle", + "mergepaths" => "merge", + _ => return None, + }) +} + +/// Match name of the node at the end of `chain` (layer root's children → … → node). `effect` +/// params are numbered `-0001…` in parameter order, like After Effects. +pub fn node(chain: &[&Node]) -> String { + let Some(last) = chain.last() else { return String::new() }; + let id = last.match_id(); + let parent = chain.len().checked_sub(2).map(|i| chain[i]); + let grand = chain.len().checked_sub(3).map(|i| chain[i]); + let top = chain.first().map(|n| n.match_id()).unwrap_or(""); + let own = || id.to_string(); + if chain.len() == 1 { + return find(LAYER_GROUPS, id).map(str::to_string).unwrap_or_else(own); + } + let pid = parent.map(|p| p.match_id()).unwrap_or(""); + match top { + "transform" if chain.len() == 2 => find(TRANSFORM, id).map(str::to_string).unwrap_or_else(own), + "masks" if chain.len() == 2 => "ADBE Mask Atom".into(), + "masks" if chain.len() == 3 => find(MASK, id).map(str::to_string).unwrap_or_else(own), + "effects" if chain.len() == 2 => effect(id), + "effects" if chain.len() == 3 => { + let fx = parent.and_then(Node::as_group); + match fx { + Some(g) => { + let i = g.children.iter().position(|c| c.uid() == last.uid()).unwrap_or(0) + 1; + format!("{}-{i:04}", effect(&g.match_id)) + } + None => own(), + } + } + "text" if chain.len() == 2 => find(TEXT, id).map(str::to_string).unwrap_or_else(own), + "text" if pid == "animators" => "ADBE Text Animator".into(), + "text" if grand.map(|g| g.match_id()) == Some("animators") => find(TEXT_ANIMATOR, id).map(str::to_string).unwrap_or_else(own), + "cameraOptions" => find(CAMERA, id).map(str::to_string).unwrap_or_else(own), + "lightOptions" => find(LIGHT, id).map(str::to_string).unwrap_or_else(own), + "materialOptions" => find(MATERIAL, id).map(str::to_string).unwrap_or_else(own), + "audio" if id == "levels" => "ADBE Audio Levels".into(), + "contents" => { + // Items sit in a `contents` list; their properties under the item. + if pid == "contents" { + if id == "star" || id == "polygon" { + return "ADBE Vector Shape - Star".into(); + } + return find(SHAPE_ITEMS, id).map(str::to_string).unwrap_or_else(own); + } + if let Some((_, _, m)) = SHAPE_PROPS.iter().find(|(item, prop, _)| *item == pid && *prop == id) { + return m.to_string(); + } + own() + } + _ => own(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn effect_names_round_trip() { + for (id, m) in EFFECTS { + assert_eq!(effect(id), *m); + assert_eq!(effect_from_match(m), Some(*id)); + } + assert_eq!(effect("ec.blur.ccvector"), "ec.blur.ccvector"); + let mut ids: Vec<&str> = EFFECTS.iter().map(|(a, _)| *a).collect(); + ids.sort(); + ids.dedup(); + assert_eq!(ids.len(), EFFECTS.len(), "duplicate effect ids"); + } + + #[test] + fn shape_kinds() { + assert_eq!(shape_kind("ADBE Vector Shape - Rect"), Some("rect")); + assert_eq!(shape_kind("Trim Paths"), Some("trim")); + assert_eq!(shape_kind("ADBE Vector Graphic - Fill"), Some("fill")); + assert_eq!(shape_kind("nope"), None); + } +} diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs new file mode 100644 index 00000000..d1dd4768 --- /dev/null +++ b/crates/script/src/model.rs @@ -0,0 +1,679 @@ +//! Read-only queries behind the object model (`__query(kind, json)`): project, items, layers, +//! property nodes, values and keyframes as JSON shaped for the prelude. Every edit goes through +//! engine commands instead (`__exec`), so it is undoable and journaled. + +use effectcraft_engine::Session; +use effectcraft_engine::color::Label; +use effectcraft_keyframe::{Interp, TextDoc, Value as KV}; +use effectcraft_project::{Comp, GroupKind, ItemId, ItemKind, Layer, LayerId, LayerSource, Node, ParamUi, PropGroup}; +use effectcraft_render::EvalCtx; +use effectcraft_time::Tick; +use serde_json::{Value as J, json}; + +use crate::matchnames; + +type R = Result; + +fn u(a: &J, k: &str) -> Option { + a.get(k).and_then(J::as_u64) +} +fn f(a: &J, k: &str) -> Option { + a.get(k).and_then(J::as_f64) +} + +fn comp_of<'a>(s: &'a Session, a: &J) -> Result<(ItemId, &'a Comp), String> { + let id = ItemId(u(a, "comp").ok_or("missing comp")?); + s.project.comp(id).map(|c| (id, c)).ok_or_else(|| "the composition no longer exists".to_string()) +} + +fn layer_of<'a>(s: &'a Session, a: &J) -> Result<(ItemId, &'a Comp, &'a Layer), String> { + let (cid, c) = comp_of(s, a)?; + let l = c.layer(LayerId(u(a, "layer").ok_or("missing layer")?)).ok_or("the layer no longer exists (it was deleted)")?; + Ok((cid, c, l)) +} + +pub fn query(s: &mut Session, kind: &str, a: &J) -> R { + match kind { + "project" => Ok(project(s)), + "item" => item(s, a), + "layer" => layer(s, a), + "node" => node(s, a), + "child" => child(s, a), + "value" => value(s, a), + "keys" => keys(s, a), + "textdoc" => textdoc(s, a), + "markers" => markers(s, a), + "sourceRect" => source_rect(s, a), + "resolveAdd" => resolve_add(s, a), + "menuCommandId" => Ok(menu_command_id(a.get("name").and_then(J::as_str).unwrap_or(""))), + "commandAt" => { + let i = u(a, "index").unwrap_or(0) as usize; + Ok(effectcraft_engine::command_specs().get(i.wrapping_sub(1)).map(|c| json!(c.id)).unwrap_or(J::Null)) + } + "exprError" => { + let code = a.get("code").and_then(J::as_str).unwrap_or(""); + Ok(match s.expr_check { + Some(check) => json!(check(code).err().unwrap_or_default()), + None => json!(""), + }) + } + "labels" => Ok(json!(Label::ALL.iter().map(|l| l.name()).collect::>())), + "effects" => Ok(json!( + effectcraft_engine::effects::registry() + .iter() + .map(|e| json!({"id": e.id, "name": e.name, "matchName": matchnames::effect(e.id), "category": e.category})) + .collect::>() + )), + _ => Err(format!("unknown query `{kind}`")), + } +} + +fn project(s: &Session) -> J { + let p = &s.project; + let items: Vec = p.items.keys().map(|k| k.0).collect(); + let active = s.active_comp_id().map(|c| c.0).or_else(|| (s.state.project_selection.len() == 1).then(|| s.state.project_selection[0].0)); + json!({ + "items": items, + "active": active, + "selection": s.state.project_selection.iter().map(|i| i.0).collect::>(), + "path": s.path, + "dirty": s.is_dirty(), + "bitsPerChannel": p.settings.bit_depth.bits(), + "linearBlending": p.settings.blend_linear, + "linearizeWorkingSpace": p.settings.linearize, + "workingSpace": p.settings.working_space.map(|w| format!("{w:?}")).unwrap_or_default(), + "timeDisplayFrames": p.settings.time_display == effectcraft_project::TimeDisplayStyle::Frames, + "renderQueue": p.render_queue.len(), + "revision": s.revision, + }) +} + +fn label_index(l: Label) -> usize { + Label::ALL.iter().position(|x| *x == l).unwrap_or(0) +} + +fn item(s: &Session, a: &J) -> R { + let id = ItemId(u(a, "id").ok_or("missing id")?); + let it = s.project.item(id).ok_or("the item no longer exists (it was deleted)")?; + let mut o = json!({ + "id": id.0, + "name": it.name, + "typeName": it.type_name(), + "parent": it.parent.map(|p| p.0).unwrap_or(0), + "comment": it.comment, + "label": label_index(it.label), + "selected": s.state.project_selection.contains(&id), + }); + let used_in: Vec = s.project.comps().filter(|(_, c)| c.layers.iter().any(|l| l.source.item() == Some(id))).map(|(cid, _)| cid.0).collect(); + o["usedIn"] = json!(used_in); + match &it.kind { + ItemKind::Folder => { + o["kind"] = json!("folder"); + o["children"] = json!(s.project.items.values().filter(|c| c.parent == Some(id)).map(|c| c.id.0).collect::>()); + } + ItemKind::Comp(c) => { + let cid = id; + o["kind"] = json!("comp"); + let sel: Vec = s.state.selected_layers.iter().filter(|l| c.layer(**l).is_some()).map(|l| l.0).collect(); + let fps = c.frame_rate.as_f64(); + if let Some(m) = o.as_object_mut() { + m.extend( + json!({ + "width": c.width, "height": c.height, "pixelAspect": c.pixel_aspect, + "duration": c.duration.seconds(), "frameRate": fps, "frameDuration": c.frame_duration().seconds(), + "layers": c.layers.iter().map(|l| l.id.0).collect::>(), + "selectedLayers": sel, + "selectedProperties": s.state.selected_props.iter().filter(|(l, _)| c.layer(*l).is_some()).map(|(l, p)| json!([l.0, p])).collect::>(), + "time": s.time_of(cid).seconds(), + "workAreaStart": c.work_area.0.seconds(), "workAreaDuration": (c.work_area.1 - c.work_area.0).seconds(), + "bgColor": c.background, "displayStartTime": c.display_start.seconds(), + "hideShyLayers": c.hide_shy, "motionBlur": c.enable_motion_blur, "frameBlending": c.enable_frame_blending, + "draft3d": c.draft_3d, "shutterAngle": c.shutter_angle, "shutterPhase": c.shutter_phase, + "motionBlurSamplesPerFrame": c.motion_blur_samples, + "renderer": match c.renderer { effectcraft_project::Renderer::Classic3D => "ADBE Classic 3D", effectcraft_project::Renderer::Advanced3D => "ADBE Advanced 3d" }, + "open": s.state.open_comps.contains(&cid), "active": s.active_comp_id() == Some(cid), + "hasVideo": true, "hasAudio": c.layers.iter().any(|l| l.switches.audio && matches!(l.source, LayerSource::Footage { .. } | LayerSource::Comp { .. })), + "posterTime": c.poster_time.seconds(), + }) + .as_object() + .cloned() + .unwrap_or_default(), + ) + } + } + ItemKind::Footage(fo) => { + o["kind"] = json!("footage"); + o["file"] = json!(fo.path); + o["width"] = json!(fo.width); + o["height"] = json!(fo.height); + o["pixelAspect"] = json!(fo.pixel_aspect); + o["duration"] = json!(if fo.kind == effectcraft_project::FootageKind::Still { 0.0 } else { fo.duration.seconds() }); + o["frameRate"] = json!(if fo.kind == effectcraft_project::FootageKind::Still { 0.0 } else { fo.frame_rate.as_f64() }); + o["hasVideo"] = json!(fo.has_video); + o["hasAudio"] = json!(fo.has_audio); + o["footageMissing"] = json!(fo.missing); + o["isStill"] = json!(fo.kind == effectcraft_project::FootageKind::Still); + o["loop"] = json!(fo.loop_count); + } + ItemKind::Solid(so) => { + o["kind"] = json!("solid"); + o["width"] = json!(so.width); + o["height"] = json!(so.height); + o["pixelAspect"] = json!(so.pixel_aspect); + o["color"] = json!(so.color); + o["duration"] = json!(0.0); + o["frameRate"] = json!(0.0); + o["hasVideo"] = json!(true); + o["hasAudio"] = json!(false); + o["isStill"] = json!(true); + } + } + Ok(o) +} + +fn layer(s: &Session, a: &J) -> R { + let (cid, c, l) = layer_of(s, a)?; + let kind = match &l.source { + LayerSource::Text => "text", + LayerSource::Shape => "shape", + LayerSource::Camera => "camera", + LayerSource::Light { .. } => "light", + _ => "av", + }; + let (w, h) = effectcraft_render::source_size(&s.project, l); + let t = s.time_of(cid); + Ok(json!({ + "id": l.id.0, + "comp": cid.0, + "kind": kind, + "name": l.name, + "index": c.index_of(l.id).unwrap_or(0), + "matchName": matchnames::layer(&l.source), + "root": l.props.uid, + "source": l.source.item().map(|i| i.0), + "nullLayer": matches!(l.source, LayerSource::Null), + "lightType": match &l.source { LayerSource::Light { kind } => kind.label(), _ => "" }, + "enabled": l.switches.video, + "audioEnabled": l.switches.audio, + "solo": l.switches.solo, + "locked": l.switches.locked, + "shy": l.switches.shy, + "collapse": l.switches.collapse, + "quality": format!("{:?}", l.switches.quality), + "samplingQuality": format!("{:?}", l.switches.sampling), + "effectsActive": l.switches.effects, + "frameBlending": format!("{:?}", l.switches.frame_blend), + "motionBlur": l.switches.motion_blur, + "adjustmentLayer": l.switches.adjustment, + "threeDLayer": l.switches.three_d, + "guideLayer": l.switches.guide, + "preserveTransparency": l.preserve_transparency, + "inPoint": l.in_point.seconds(), + "outPoint": l.out_point.seconds(), + "startTime": l.start_time.seconds(), + "stretch": l.stretch, + "time": l.layer_time(t).seconds(), + "active": l.switches.video && l.is_active_at(t), + "parent": l.parent.map(|p| p.0), + "blendingMode": l.blend_mode.label(), + "trackMatte": l.track_matte.map(|m| json!({"layer": m.layer.0, "kind": format!("{:?}", m.kind)})), + "isTrackMatte": c.layers.iter().any(|x| x.track_matte.is_some_and(|m| m.layer == l.id)), + "label": label_index(l.label), + "comment": l.comment, + "selected": s.state.selected_layers.contains(&l.id), + "autoOrient": format!("{:?}", l.auto_orient), + "width": w, "height": h, + "hasVideo": l.source.is_av(), + "hasAudio": matches!(l.source, LayerSource::Footage { .. } | LayerSource::Comp { .. }) && l.props.sub("audio").is_some(), + "timeRemapEnabled": l.props.prop("timeRemap").is_some(), + })) +} + +/// Node at the end of a uid chain below the layer root, and the chain itself. +fn chain(l: &Layer, uid: u64) -> Option> { + l.props.node_chain(uid) +} + +/// PropertyValueType of a property. +fn value_type(p: &effectcraft_project::Property) -> &'static str { + if matches!(p.ui, ParamUi::Mask) { + return "MASK_INDEX"; + } + match &p.value { + KV::Scalar(_) | KV::Bool(_) | KV::Enum(_) => "OneD", + KV::Vec2(_) => { + if p.spatial { + "TwoD_SPATIAL" + } else { + "TwoD" + } + } + KV::Vec3(_) => { + if p.spatial { + "ThreeD_SPATIAL" + } else { + "ThreeD" + } + } + KV::Color(_) => "COLOR", + KV::Path(_) => "SHAPE", + KV::Text(_) => "TEXT_DOCUMENT", + KV::Layer(_) => "LAYER_INDEX", + KV::Gradient(_) | KV::Str(_) => "CUSTOM_VALUE", + } +} + +/// Groups whose children are instances (effects, masks, shape items, animators). +fn indexed_parade(g: &PropGroup) -> bool { + matches!(g.match_id.as_str(), "effects" | "masks" | "contents" | "animators" | "selectors" | "layerStyles" | "motionTrackers") +} + +fn node_json(c: &Comp, l: &Layer, uid: u64) -> R { + if uid == 0 || uid == l.props.uid { + let n = l.props.children.len(); + return Ok(json!({ + "uid": l.props.uid, "kind": "group", "name": l.name, "matchName": matchnames::layer(&l.source), "matchId": "layer", + "index": c.index_of(l.id).unwrap_or(0), "depth": 0, "parent": null, "isLayer": true, "numProperties": n, + "children": l.props.children.iter().map(Node::uid).collect::>(), "propertyType": "NAMED_GROUP", + "enabled": l.switches.video, "canSetEnabled": true, + })); + } + let ch = chain(l, uid).ok_or("the property no longer exists (it was deleted)")?; + let node = *ch.last().ok_or("bad property")?; + let parent_group: &PropGroup = if ch.len() >= 2 { ch[ch.len() - 2].as_group().ok_or("bad tree")? } else { &l.props }; + let index = parent_group.children.iter().position(|x| x.uid() == uid).map(|i| i + 1).unwrap_or(0); + let parent_uid = if ch.len() >= 2 { ch[ch.len() - 2].uid() } else { l.props.uid }; + let match_name = matchnames::node(&ch); + let instance_parent = indexed_parade(parent_group) && ch.len() >= 2 || (ch.len() == 2 && ch[0].match_id() == "contents"); + let mut o = json!({ + "uid": uid, "name": node.name(), "matchName": match_name, "matchId": node.match_id(), "index": index, + "depth": ch.len(), "parent": parent_uid, "isLayer": false, + "isEffect": matches!(node.as_group().map(|g| &g.kind), Some(GroupKind::Effect { .. })), + "isMask": matches!(node.as_group().map(|g| &g.kind), Some(GroupKind::Mask { .. })), + }); + match node { + Node::Group(g) => { + let instance = matches!(g.kind, GroupKind::Indexed | GroupKind::Effect { .. } | GroupKind::Mask { .. } | GroupKind::Tracker { .. }); + o["kind"] = json!("group"); + o["numProperties"] = json!(g.children.len()); + o["children"] = json!(g.children.iter().map(Node::uid).collect::>()); + o["propertyType"] = json!(if indexed_parade(g) { "INDEXED_GROUP" } else { "NAMED_GROUP" }); + o["enabled"] = json!(g.enabled); + o["canSetEnabled"] = json!(instance); + o["instance"] = json!(instance || instance_parent); + o["effectId"] = json!(match &g.kind { + GroupKind::Effect { effect } => Some(effect.clone()), + _ => None, + }); + if let GroupKind::Mask { mode, inverted, color, locked, .. } = &g.kind { + o["maskMode"] = json!(mode.label()); + o["inverted"] = json!(inverted); + o["color"] = json!([color[0] as f64 / 255.0, color[1] as f64 / 255.0, color[2] as f64 / 255.0]); + o["locked"] = json!(locked); + } + } + Node::Prop(p) => { + o["kind"] = json!("prop"); + o["propertyType"] = json!("PROPERTY"); + o["valueType"] = json!(value_type(p)); + o["type"] = json!(p.value.kind_name()); + o["dims"] = json!(p.value.dims()); + o["spatial"] = json!(p.spatial); + o["numKeys"] = json!(p.keys.len()); + o["expression"] = json!(p.expr.as_ref().map(|e| e.text.clone()).unwrap_or_default()); + o["expressionEnabled"] = json!(p.has_expression()); + o["canVaryOverTime"] = json!(!p.static_only); + o["canSetExpression"] = json!(!p.static_only && !matches!(p.ui, ParamUi::Hidden)); + o["hidden"] = json!(matches!(p.ui, ParamUi::Hidden)); + o["enabled"] = json!(true); + o["canSetEnabled"] = json!(false); + o["isModified"] = json!(p.is_animated() || p.has_expression()); + if let ParamUi::Slider { min, max, .. } = p.ui { + o["min"] = json!(min); + o["max"] = json!(max); + } + if let ParamUi::Popup { options } = &p.ui { + o["options"] = json!(options); + } + o["ui"] = json!(match &p.ui { + ParamUi::Percent => "%", + ParamUi::Angle => "degrees", + ParamUi::Pixels | ParamUi::Point | ParamUi::Point3 => "pixels", + _ => "", + }); + // Position with Separate Dimensions. + o["separated"] = json!(node.match_id() == "position" && parent_group.match_id == "transform" && parent_group.get("positionX").is_some()); + o["separationFollower"] = json!(matches!(node.match_id(), "positionX" | "positionY" | "positionZ")); + } + } + Ok(o) +} + +fn node(s: &Session, a: &J) -> R { + let (_, c, l) = layer_of(s, a)?; + node_json(c, l, u(a, "uid").unwrap_or(0)) +} + +/// Normalized name for loose lookups: `Anchor Point`, `anchorPoint`, `anchor_point` → `anchorpoint`. +fn loose(s: &str) -> String { + s.chars().filter(|c| c.is_alphanumeric()).collect::().to_ascii_lowercase() +} + +/// AE-style aliases for our match ids (attribute names scripts use). +fn alias(k: &str) -> Option<&'static str> { + Some(match k { + "anchorpoint" => "anchor", + "pointofinterest" => "poi", + "xrotation" => "rotationX", + "yrotation" => "rotationY", + "zrotation" => "rotation", + "xposition" => "positionX", + "yposition" => "positionY", + "zposition" => "positionZ", + "effect" | "effects" => "effects", + "mask" | "masks" => "masks", + "content" | "contents" => "contents", + "maskshape" | "maskpath" => "path", + "maskfeather" => "feather", + "maskopacity" => "opacity", + "maskexpansion" | "maskoffset" => "expansion", + "sourcetext" => "sourceText", + "timeremap" => "timeRemap", + "audiolevels" => "levels", + _ => return None, + }) +} + +fn child(s: &Session, a: &J) -> R { + let (_, c, l) = layer_of(s, a)?; + let uid = u(a, "uid").unwrap_or(0); + let g: &PropGroup = if uid == 0 || uid == l.props.uid { &l.props } else { l.props.find_group(uid).ok_or("not a property group")? }; + let key = a.get("key").cloned().unwrap_or(J::Null); + let found = match &key { + J::Number(n) => n.as_u64().and_then(|i| g.children.get((i as usize).checked_sub(1)?)), + J::String(k) => { + let chain_of = |n: &Node| -> String { + let mut ch = if uid == 0 || uid == l.props.uid { vec![] } else { chain(l, uid).unwrap_or_default() }; + ch.push(n); + matchnames::node(&ch) + }; + let lk = loose(k); + g.children + .iter() + .find(|n| chain_of(n) == *k) + .or_else(|| g.children.iter().find(|n| n.name() == k)) + .or_else(|| g.children.iter().find(|n| n.match_id() == k)) + .or_else(|| g.children.iter().find(|n| n.name().eq_ignore_ascii_case(k))) + .or_else(|| g.children.iter().find(|n| loose(n.name()) == lk || loose(n.match_id()) == lk)) + .or_else(|| alias(&lk).and_then(|m| g.children.iter().find(|n| n.match_id() == m))) + } + _ => None, + }; + match found { + Some(n) => node_json(c, l, n.uid()), + None => Ok(J::Null), + } +} + +/// A value as the object model shows it: popups 1-based, checkboxes 0/1, layer references as +/// layer indices, paths as `{vertices, inTangents, outTangents, closed}`, text as the document. +fn js_value(c: &Comp, v: &KV) -> J { + match v { + KV::Bool(b) => json!(if *b { 1 } else { 0 }), + KV::Enum(i) => json!(*i + 1), + KV::Layer(l) => json!(l.and_then(|id| c.index_of(LayerId(id))).unwrap_or(0)), + KV::Path(p) => json!({"vertices": p.vertices, "inTangents": p.in_tangents, "outTangents": p.out_tangents, "closed": p.closed}), + KV::Text(d) => text_json(d), + v => v.to_json(), + } +} + +/// A text document for `TextDocument`: the serialized document plus the first character's style. +fn text_json(d: &TextDoc) -> J { + let mut doc = serde_json::to_value(d).unwrap_or(J::Null); + let st = d.style_at(0); + if let Some(o) = doc.as_object_mut() { + o.insert("first".into(), effectcraft_keyframe::text_doc::char_style_json(&st)); + o.insert("para".into(), effectcraft_keyframe::text_doc::para_style_json(&d.para(0))); + } + doc +} + +fn value(s: &Session, a: &J) -> R { + let (cid, c, l) = layer_of(s, a)?; + let uid = u(a, "uid").ok_or("missing uid")?; + let p = l.props.find(uid).ok_or("the property no longer exists (it was deleted)")?; + let t = f(a, "time").map(Tick::from_seconds_f64).unwrap_or_else(|| s.time_of(cid)); + let pre = a.get("pre").and_then(J::as_bool).unwrap_or(false); + let v = if pre || !p.has_expression() { + p.value_at(l.layer_time(t)) + } else { + let mut ctx = EvalCtx::new(&s.project, cid, c, t); + ctx.expr = s.expr.as_deref(); + ctx.value(l, p) + }; + Ok(js_value(c, &v)) +} + +fn interp(i: Interp) -> &'static str { + match i { + Interp::Linear => "LINEAR", + Interp::Bezier => "BEZIER", + Interp::Hold => "HOLD", + } +} + +fn keys(s: &Session, a: &J) -> R { + let (cid, c, l) = layer_of(s, a)?; + let uid = u(a, "uid").ok_or("missing uid")?; + let p = l.props.find(uid).ok_or("the property no longer exists (it was deleted)")?; + let sel: Vec = s.state.selected_keys.iter().filter(|k| k.layer == l.id && k.prop == uid).map(|k| k.time).collect(); + let _ = cid; + let ease = |e: &[effectcraft_keyframe::Ease], n: usize| -> J { + let n = n.max(1); + // Influence is stored as a fraction; scripts see percent. + let mut v: Vec = e.iter().map(|x| json!({"speed": x.speed, "influence": x.influence * 100.0})).collect(); + let d = e.first().cloned().unwrap_or_default(); + while v.len() < n { + v.push(json!({"speed": d.speed, "influence": d.influence * 100.0})); + } + J::Array(v) + }; + let dims = if p.spatial { 1 } else { p.value.dims().max(1) }; + let out: Vec = p + .keys + .iter() + .map(|k| { + json!({ + "time": l.comp_time(k.time).seconds(), + "layerTime": k.time.seconds(), + "value": js_value(c, &k.value), + "inInterp": interp(k.in_interp), "outInterp": interp(k.out_interp), + "inEase": ease(&k.in_ease, dims), "outEase": ease(&k.out_ease, dims), + "continuous": k.continuous, "autoBezier": k.auto_bezier, "roving": k.roving, + "spatialIn": k.spatial_in, "spatialOut": k.spatial_out, + "spatialContinuous": k.spatial_continuous, "spatialAutoBezier": k.spatial_auto, + "selected": sel.contains(&k.time), + }) + }) + .collect(); + Ok(J::Array(out)) +} + +/// Apply `layer.setText` attribute keys to the Source Text document at `time` and return the new +/// document (for `prop.set` / `prop.addKey`). +fn textdoc(s: &Session, a: &J) -> R { + let (cid, _, l) = layer_of(s, a)?; + let uid = u(a, "uid").ok_or("missing uid")?; + let p = l.props.find(uid).ok_or("the property no longer exists")?; + let t = f(a, "time").map(Tick::from_seconds_f64).unwrap_or_else(|| s.time_of(cid)); + let KV::Text(doc) = p.value_at(l.layer_time(t)) else { return Err("not a Source Text property".into()) }; + let mut d = *doc; + if let Some(attrs) = a.get("attrs").and_then(J::as_object) { + if let Some(text) = attrs.get("text").and_then(J::as_str) { + d.set_text(text); + } + for (k, v) in attrs { + if k == "text" || v.is_null() { + continue; + } + match d.set_attr(k, v, None) { + Ok(true) => {} + Ok(false) => return Err(format!("TextDocument: unknown attribute `{k}`")), + Err(e) => return Err(format!("TextDocument.{k}: {e}")), + } + } + } + serde_json::to_value(&d).map_err(|e| e.to_string()) +} + +fn markers(s: &Session, a: &J) -> R { + let (_, c) = comp_of(s, a)?; + let (list, to_comp): (&[effectcraft_project::Marker], Option<&Layer>) = match u(a, "layer") { + Some(lid) => { + let l = c.layer(LayerId(lid)).ok_or("the layer no longer exists")?; + (&l.markers, Some(l)) + } + None => (&c.markers, None), + }; + let out: Vec = list + .iter() + .map(|m| { + let t = to_comp.map(|l| l.comp_time(m.time)).unwrap_or(m.time); + json!({ + "time": t.seconds(), "duration": m.duration.seconds(), "comment": m.comment, "chapter": m.chapter, "url": m.url, + "frameTarget": m.frame_target, "label": label_index(m.label), "protectedRegion": m.protected, + "cuePointName": m.cue_point.as_ref().map(|c| c.name.clone()).unwrap_or_default(), + "eventCuePoint": m.cue_point.as_ref().is_some_and(|c| !c.navigation), + "params": m.cue_point.as_ref().map(|c| c.params.clone()).unwrap_or_default(), + }) + }) + .collect(); + Ok(J::Array(out)) +} + +fn source_rect(s: &Session, a: &J) -> R { + let (cid, c, l) = layer_of(s, a)?; + let t = f(a, "time").map(Tick::from_seconds_f64).unwrap_or_else(|| s.time_of(cid)); + let mut ctx = EvalCtx::new(&s.project, cid, c, t); + ctx.expr = s.expr.as_deref(); + let r = match &l.source { + LayerSource::Text => { + let paths: Vec<_> = effectcraft_render::text::glyph_paths(&ctx, l).into_iter().map(|(p, _)| p).collect(); + effectcraft_path::bounds(&paths).map(|b| [b.y0, b.x0, b.width(), b.height()]).unwrap_or([0.0; 4]) + } + LayerSource::Shape => match l.props.sub("contents") { + Some(contents) => { + let buf = effectcraft_render::shapes::render(&ctx, l, contents, 1.0); + if buf.img.width <= 4 { [0.0; 4] } else { [2.0 - buf.offset[1], 2.0 - buf.offset[0], buf.img.width as f64 - 4.0, buf.img.height as f64 - 4.0] } + } + None => [0.0; 4], + }, + _ => { + let (w, h) = effectcraft_render::source_size(&s.project, l); + [0.0, 0.0, w as f64, h as f64] + } + }; + Ok(json!({"top": r[0], "left": r[1], "width": r[2], "height": r[3]})) +} + +/// What `PropertyGroup.addProperty(name)` adds to the group `uid`. +fn resolve_add(s: &Session, a: &J) -> R { + let (_, _, l) = layer_of(s, a)?; + let uid = u(a, "uid").unwrap_or(0); + let name = a.get("name").and_then(J::as_str).unwrap_or(""); + let g: &PropGroup = if uid == 0 || uid == l.props.uid { &l.props } else { l.props.find_group(uid).ok_or("not a property group")? }; + let in_contents = g.match_id == "contents"; + let in_group = l.props.sub("contents").is_some_and(|c| c.uid == g.uid || c.find_group(g.uid).is_some()); + Ok(match g.match_id.as_str() { + "effects" => { + let id = matchnames::effect_from_match(name).map(str::to_string).or_else(|| effectcraft_engine::effects::lookup(name).map(|e| e.id.to_string())); + match id { + Some(id) => json!({"kind": "effect", "effect": id}), + None => json!({"error": format!("Can not add a property with name \"{name}\" to this PropertyGroup (unknown effect)")}), + } + } + "masks" => { + if name == "ADBE Mask Atom" || loose(name) == "mask" { + json!({"kind": "mask"}) + } else { + json!({"error": format!("Can not add \"{name}\" to Masks (use \"ADBE Mask Atom\")")}) + } + } + "animators" => json!({"kind": "animator"}), + _ if in_contents && in_group => match matchnames::shape_kind(name) { + Some(k) => { + json!({"kind": "shape", "shape": k, "group": if l.props.sub("contents").is_some_and(|c| c.uid == g.uid) { J::Null } else { json!(g.uid) }}) + } + None => json!({"error": format!("Can not add \"{name}\" to Contents")}), + }, + "properties" if l.props.group("text/animators").is_some_and(|a| a.find_group(g.uid).is_some()) => { + let animator = l.props.group("text/animators").and_then(|a| a.groups().find(|x| x.find_group(g.uid).is_some()).map(|x| x.uid)); + match text_animator_property(name) { + Some(k) => json!({"kind": "animatorProperty", "property": k, "animator": animator}), + None => json!({"error": format!("Can not add \"{name}\" to an animator's Properties")}), + } + } + "selectors" if l.props.group("text/animators").is_some_and(|a| a.find_group(g.uid).is_some()) => { + let animator = l.props.group("text/animators").and_then(|a| a.groups().find(|x| x.find_group(g.uid).is_some()).map(|x| x.uid)); + let kind = match name { + "ADBE Text Wiggly Selector" => "wiggly", + "ADBE Text Expressible Selector" => "expression", + n if loose(n).contains("wiggly") => "wiggly", + n if loose(n).contains("expression") => "expression", + _ => "range", + }; + json!({"kind": "selector", "selector": kind, "animator": animator}) + } + _ => json!({"error": format!("Can not add a property to \"{}\"", g.name)}), + }) +} + +/// A text animator property (`layer.addTextAnimatorProperty` key) from an After Effects match +/// name or a display name. +fn text_animator_property(name: &str) -> Option<&'static str> { + const AE: &[(&str, &str)] = &[ + ("ADBE Text Anchor Point 3D", "anchor"), + ("ADBE Text Position 3D", "position"), + ("ADBE Text Scale 3D", "scale"), + ("ADBE Text Skew", "skew"), + ("ADBE Text Rotation", "rotation"), + ("ADBE Text Opacity", "opacity"), + ("ADBE Text Fill Color", "fillColor"), + ("ADBE Text Fill Hue", "fillHue"), + ("ADBE Text Fill Saturation", "fillSaturation"), + ("ADBE Text Fill Brightness", "fillBrightness"), + ("ADBE Text Fill Opacity", "fillOpacity"), + ("ADBE Text Stroke Color", "strokeColor"), + ("ADBE Text Stroke Hue", "strokeHue"), + ("ADBE Text Stroke Saturation", "strokeSaturation"), + ("ADBE Text Stroke Brightness", "strokeBrightness"), + ("ADBE Text Stroke Opacity", "strokeOpacity"), + ("ADBE Text Stroke Width", "strokeWidth"), + ("ADBE Text Tracking Amount", "tracking"), + ("ADBE Text Line Anchor", "lineAnchor"), + ("ADBE Text Line Spacing", "lineSpacing"), + ("ADBE Text Character Offset", "characterOffset"), + ("ADBE Text Character Replace", "characterValue"), + ("ADBE Text Blur", "blur"), + ]; + if let Some((_, k)) = AE.iter().find(|(m, _)| m.eq_ignore_ascii_case(name)) { + return Some(k); + } + let n = loose(name); + AE.iter().map(|(_, k)| *k).find(|k| loose(k) == n || (n == "anchorpoint" && *k == "anchor") || (n == "trackingamount" && *k == "tracking")) +} + +/// `app.findMenuCommandId(name)`: 1-based index of the command whose label matches (menu items +/// first), 0 when none does. +fn menu_command_id(name: &str) -> J { + let norm = |s: &str| s.trim().trim_end_matches("...").trim_end_matches('…').trim().to_ascii_lowercase(); + let n = norm(name); + let specs = effectcraft_engine::command_specs(); + let pick = specs + .iter() + .position(|c| !c.menu.is_empty() && norm(c.label) == n) + .or_else(|| specs.iter().position(|c| norm(c.label) == n)) + .or_else(|| specs.iter().position(|c| c.id == name)); + json!(pick.map(|i| i + 1).unwrap_or(0)) +} diff --git a/crates/script/src/prelude.js b/crates/script/src/prelude.js new file mode 100644 index 00000000..4f359926 --- /dev/null +++ b/crates/script/src/prelude.js @@ -0,0 +1,1689 @@ +// EffectCraft scripting: an After Effects-style object model over the engine. +// +// Written from the behaviour the After Effects Scripting Guide documents (object names, +// attributes, methods). Reads go through `__query(kind, json)`; every edit runs an engine command +// through `__exec(id, json)`, so it is undoable and identical to the UI's action. + +var __ecGlobal = this; + +function __call(id, p) { + var r = __exec(id, JSON.stringify(p || {})); + return r === undefined || r === "" ? null : JSON.parse(r); +} +function __get(kind, a) { + return JSON.parse(__query(kind, JSON.stringify(a || {}))); +} +function __err(msg) { + return new Error(msg); +} +function __num(v, what) { + var n = Number(v); + if (typeof v === "boolean" || isNaN(n)) throw __err((what || "value") + " must be a number"); + return n; +} +function __str(v) { + if (v === undefined || v === null) return ""; + if (v instanceof File || v instanceof Folder) return v.fsName; + return String(v); +} + +// ------------------------------------------------------------------ enums + +function __enum(name, keys, base) { + var e = {}; + for (var i = 0; i < keys.length; i++) e[keys[i]] = base + i; + e.__name = name; + e.__keys = keys; + e.__base = base; + return e; +} +function __enumKey(e, v) { + var i = v - e.__base; + return i >= 0 && i < e.__keys.length ? e.__keys[i] : null; +} + +var BlendingMode = __enum("BlendingMode", [ + "NORMAL", "DISSOLVE", "DANCING_DISSOLVE", "DARKEN", "MULTIPLY", "COLOR_BURN", "CLASSIC_COLOR_BURN", "LINEAR_BURN", + "DARKER_COLOR", "ADD", "LIGHTEN", "SCREEN", "COLOR_DODGE", "CLASSIC_COLOR_DODGE", "LINEAR_DODGE", "LIGHTER_COLOR", + "OVERLAY", "SOFT_LIGHT", "HARD_LIGHT", "LINEAR_LIGHT", "VIVID_LIGHT", "PIN_LIGHT", "HARD_MIX", "DIFFERENCE", + "CLASSIC_DIFFERENCE", "EXCLUSION", "SUBTRACT", "DIVIDE", "HUE", "SATURATION", "COLOR", "LUMINOSITY", "STENCIL_ALPHA", + "STENCIL_LUMA", "SILHOUETE_ALPHA", "SILHOUETTE_LUMA", "ALPHA_ADD", "LUMINESCENT_PREMUL"], 5212); +BlendingMode.SILHOUETTE_ALPHA = BlendingMode.SILHOUETE_ALPHA; +var KeyframeInterpolationType = __enum("KeyframeInterpolationType", ["LINEAR", "BEZIER", "HOLD"], 6612); +var TrackMatteType = __enum("TrackMatteType", ["NO_TRACK_MATTE", "ALPHA", "ALPHA_INVERTED", "LUMA", "LUMA_INVERTED"], 5012); +var LightType = __enum("LightType", ["PARALLEL", "SPOT", "POINT", "AMBIENT", "ENVIRONMENT"], 4412); +var ParagraphJustification = __enum("ParagraphJustification", [ + "LEFT_JUSTIFY", "CENTER_JUSTIFY", "RIGHT_JUSTIFY", "FULL_JUSTIFY_LASTLINE_LEFT", "FULL_JUSTIFY_LASTLINE_CENTER", + "FULL_JUSTIFY_LASTLINE_RIGHT", "FULL_JUSTIFY_LASTLINE_FULL", "MULTIPLE_JUSTIFICATIONS"], 7413); +var PropertyValueType = __enum("PropertyValueType", [ + "NO_VALUE", "ThreeD_SPATIAL", "ThreeD", "TwoD_SPATIAL", "TwoD", "OneD", "COLOR", "CUSTOM_VALUE", "MARKER", + "LAYER_INDEX", "MASK_INDEX", "SHAPE", "TEXT_DOCUMENT"], 6412); +var PropertyType = __enum("PropertyType", ["PROPERTY", "INDEXED_GROUP", "NAMED_GROUP"], 6212); +var LayerQuality = __enum("LayerQuality", ["BEST", "DRAFT", "WIREFRAME"], 4612); +var LayerSamplingQuality = __enum("LayerSamplingQuality", ["BILINEAR", "BICUBIC"], 4812); +var FrameBlendingType = __enum("FrameBlendingType", ["NO_FRAME_BLEND", "FRAME_MIX", "PIXEL_MOTION"], 4012); +var AutoOrientType = __enum("AutoOrientType", ["NO_AUTO_ORIENT", "ALONG_PATH", "CAMERA_OR_POINT_OF_INTEREST", "CHARACTERS_TOWARD_CAMERA"], 4212); +var MaskMode = __enum("MaskMode", ["NONE", "ADD", "SUBTRACT", "INTERSECT", "LIGHTEN", "DARKEN", "DIFFERENCE"], 6812); +var RQItemStatus = __enum("RQItemStatus", ["WILL_CONTINUE", "NEEDS_OUTPUT", "UNQUEUED", "QUEUED", "RENDERING", "USER_STOPPED", "ERR_STOPPED", "DONE"], 3012); +var CloseOptions = __enum("CloseOptions", ["DO_NOT_SAVE_CHANGES", "PROMPT_TO_SAVE_CHANGES", "SAVE_CHANGES"], 1212); +var ImportAsType = __enum("ImportAsType", ["COMP_CROPPED_LAYERS", "FOOTAGE", "COMP", "PROJECT"], 3812); +var TimeDisplayType = __enum("TimeDisplayType", ["FRAMES", "TIMECODE"], 2012); +var LogType = __enum("LogType", ["ERRORS_ONLY", "ERRORS_AND_SETTINGS", "ERRORS_AND_PER_FRAME_INFO"], 3212); +var KeyframeLabel = __enum("KeyframeLabel", ["NONE"], 0); + +var __blendNames = {}; +(function () { + var labels = ["Normal", "Dissolve", "Dancing Dissolve", "Darken", "Multiply", "Color Burn", "Classic Color Burn", + "Linear Burn", "Darker Color", "Add", "Lighten", "Screen", "Color Dodge", "Classic Color Dodge", "Linear Dodge", + "Lighter Color", "Overlay", "Soft Light", "Hard Light", "Linear Light", "Vivid Light", "Pin Light", "Hard Mix", + "Difference", "Classic Difference", "Exclusion", "Subtract", "Divide", "Hue", "Saturation", "Color", "Luminosity", + "Stencil Alpha", "Stencil Luma", "Silhouette Alpha", "Silhouette Luma", "Alpha Add", "Luminescent Premul"]; + for (var i = 0; i < labels.length; i++) __blendNames[labels[i]] = BlendingMode.__base + i; +})(); +function __blendLabel(v) { + for (var k in __blendNames) if (__blendNames[k] === v) return k; + throw __err("Bad blendingMode value " + v); +} + +var __interpKey = { 6612: "linear", 6613: "bezier", 6614: "hold" }; +var __matteKind = { 5013: "alpha", 5014: "alphaInverted", 5015: "luma", 5016: "lumaInverted" }; +var __matteEnum = { Alpha: 5013, AlphaInverted: 5014, Luma: 5015, LumaInverted: 5016 }; +var __justKey = { 7413: "left", 7414: "center", 7415: "right", 7416: "justifyLeft", 7417: "justifyCenter", 7418: "justifyRight", 7419: "justifyAll" }; + +// ------------------------------------------------------------------ output, $ and dialogs + +function writeLn(s) { __print(__str(s)); } +function write(s) { __print(__str(s)); } +function alert(s) { __print(__str(s)); } +function confirm(s) { __print(__str(s)); return true; } +function prompt(msg, def) { __print(__str(msg)); return def === undefined ? "" : def; } +function clearOutput() {} + +var $ = { + writeln: function () { __print(Array.prototype.slice.call(arguments).map(__str).join("")); }, + write: function () { __print(Array.prototype.slice.call(arguments).map(__str).join("")); }, + sleep: function () {}, + gc: function () {}, + global: __ecGlobal, + level: 0, + engineName: "boa", + get fileName() { return __get("app").scriptName; }, + get os() { return "EffectCraft"; }, + get version() { return "EffectCraft scripting"; }, + get locale() { return "en_US"; }, + evalFile: function () { throw __err("$.evalFile is not supported: scripts can't run scripts"); }, +}; + +// ------------------------------------------------------------------ File / Folder + +function __join(a, b) { return a.replace(/[\/\\]+$/, "") + "/" + b; } + +function File(path) { + if (!(this instanceof File)) return new File(path); + this.__path = path === undefined ? "" : __str(path); + this.__buf = null; + this.__mode = null; + this.__pos = 0; + this.encoding = "UTF-8"; + this.lineFeed = "Unix"; + this.error = ""; +} +File.prototype = { + constructor: File, + get fsName() { return this.__path; }, + get fullName() { return this.__path; }, + get absoluteURI() { return this.__path; }, + get name() { return this.__path.split(/[\/\\]/).pop(); }, + get displayName() { return this.name; }, + get path() { var p = this.__path.split(/[\/\\]/); p.pop(); return p.join("/"); }, + get parent() { return new Folder(this.path); }, + get exists() { return JSON.parse(__file("exists", this.__path)) === true; }, + get length() { return this.__buf !== null ? this.__buf.length : (this.exists ? JSON.parse(__file("read", this.__path)).length : 0); }, + get eof() { return this.__buf === null || this.__pos >= this.__buf.length; }, + toString: function () { return this.__path; }, + open: function (mode) { + mode = (mode || "r").charAt(0).toLowerCase(); + this.__mode = mode; + this.__pos = 0; + if (mode === "r" || mode === "e") { + // Throws when the location is off limits (the scripting file gate). + if (!JSON.parse(__file("access", this.__path))) { + if (mode === "r") { this.error = "File not found"; return false; } + this.__buf = ""; + return true; + } + this.__buf = JSON.parse(__file("read", this.__path)); + } else if (mode === "a") { + this.__buf = this.exists ? JSON.parse(__file("read", this.__path)) : ""; + this.__pos = this.__buf.length; + } else { + // Check the gate now, like After Effects refusing to open a file for writing. + JSON.parse(__file("write", this.__path, "")); + this.__buf = ""; + } + return true; + }, + read: function (n) { + if (this.__buf === null) this.open("r"); + if (this.__buf === null) return ""; + var s = n === undefined ? this.__buf.substring(this.__pos) : this.__buf.substr(this.__pos, n); + this.__pos += s.length; + return s; + }, + readln: function () { + if (this.__buf === null) this.open("r"); + if (this.__buf === null) return ""; + var i = this.__buf.indexOf("\n", this.__pos); + var end = i < 0 ? this.__buf.length : i; + var s = this.__buf.substring(this.__pos, end).replace(/\r$/, ""); + this.__pos = i < 0 ? this.__buf.length : i + 1; + return s; + }, + write: function () { + if (this.__mode !== "w" && this.__mode !== "a" && this.__mode !== "e") throw __err("File is not open for writing"); + var s = Array.prototype.slice.call(arguments).map(__str).join(""); + this.__buf = this.__buf.substring(0, this.__pos) + s + this.__buf.substring(this.__pos + s.length); + this.__pos += s.length; + return true; + }, + writeln: function () { return this.write(Array.prototype.slice.call(arguments).map(__str).join("") + "\n"); }, + seek: function (p) { this.__pos = p; return true; }, + tell: function () { return this.__pos; }, + close: function () { + if ((this.__mode === "w" || this.__mode === "a" || this.__mode === "e") && this.__buf !== null) { + __file("write", this.__path, this.__buf); + } + this.__buf = null; + this.__mode = null; + return true; + }, + remove: function () { return JSON.parse(__file("remove", this.__path)); }, + copy: function (to) { + var data = JSON.parse(__file("read", this.__path)); + __file("write", __str(to), data); + return true; + }, + rename: function () { throw __err("File.rename is not supported"); }, + execute: function () { throw __err("scripts can't execute files"); }, + openDlg: function () { return null; }, + saveDlg: function () { return null; }, +}; +File.openDialog = function () { return null; }; +File.saveDialog = function () { return null; }; +File.decode = function (s) { return decodeURIComponent(s); }; +File.encode = function (s) { return encodeURIComponent(s); }; +File.fs = "EffectCraft"; + +function Folder(path) { + if (!(this instanceof Folder)) return new Folder(path); + this.__path = path === undefined ? "" : __str(path); +} +Folder.prototype = { + constructor: Folder, + get fsName() { return this.__path; }, + get fullName() { return this.__path; }, + get absoluteURI() { return this.__path; }, + get name() { return this.__path.replace(/[\/\\]+$/, "").split(/[\/\\]/).pop(); }, + get displayName() { return this.name; }, + get path() { var p = this.__path.replace(/[\/\\]+$/, "").split(/[\/\\]/); p.pop(); return p.join("/"); }, + get parent() { return new Folder(this.path); }, + get exists() { return JSON.parse(__file("isDir", this.__path)) === true; }, + toString: function () { return this.__path; }, + create: function () { return JSON.parse(__file("mkdir", this.__path)); }, + remove: function () { return JSON.parse(__file("remove", this.__path)); }, + getFiles: function (mask) { + var names = JSON.parse(__file("list", this.__path)); + var out = []; + for (var i = 0; i < names.length; i++) { + var n = names[i]; + if (typeof mask === "string" && mask !== "*") { + var re = new RegExp("^" + mask.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".") + "$", "i"); + if (!re.test(n.split(/[\/\\]/).pop())) continue; + } + var isDir = JSON.parse(__file("isDir", n)); + var f = isDir ? new Folder(n) : new File(n); + if (typeof mask === "function" && !mask(f)) continue; + out.push(f); + } + return out; + }, + selectDlg: function () { return null; }, +}; +Folder.selectDialog = function () { return null; }; +Object.defineProperty(Folder, "current", { get: function () { var d = __get("app").projectDir; return d ? new Folder(d) : null; } }); +Object.defineProperty(Folder, "temp", { get: function () { return null; } }); +Object.defineProperty(Folder, "desktop", { get: function () { return null; } }); +Object.defineProperty(Folder, "myDocuments", { get: function () { return null; } }); + +function Socket() { __file("network", ""); } +var system = { callSystem: function () { throw __err("scripts can't run system commands"); } }; + +// ------------------------------------------------------------------ helpers + +// Unknown attributes of property groups and layers resolve to child properties, like After +// Effects (`layer.transform.position`, `layer.Effects`, `effect.blurriness`). +var __attr = { + get: function (t, k, r) { + if (typeof k !== "string" || k in t || k.charAt(0) === "_" || k === "toJSON" || k === "then") return Reflect.get(t, k, r); + var c = t.property(k); + return c === null ? undefined : c; + }, +}; + +// 1-based collections with `[n]` access. +var __coll = { + get: function (t, k, r) { + if (typeof k === "string" && /^[0-9]+$/.test(k)) return t.__at(parseInt(k, 10)); + return Reflect.get(t, k, r); + }, +}; + +function KeyframeEase(speed, influence) { + if (!(this instanceof KeyframeEase)) return new KeyframeEase(speed, influence); + this.speed = speed === undefined ? 0 : speed; + this.influence = influence === undefined ? 16.666666667 : influence; +} + +function Shape() { + if (!(this instanceof Shape)) return new Shape(); + this.vertices = []; + this.inTangents = []; + this.outTangents = []; + this.closed = true; + this.featherSegLocs = []; + this.featherRelSegLocs = []; + this.featherRadii = []; + this.featherInterps = []; + this.featherTensions = []; + this.featherTypes = []; + this.featherRelCornerAngles = []; +} + +function MarkerValue(comment, chapter, url, frameTarget, cuePointName, params) { + if (!(this instanceof MarkerValue)) return new MarkerValue(comment, chapter, url, frameTarget, cuePointName, params); + this.comment = comment === undefined ? "" : String(comment); + this.chapter = chapter === undefined ? "" : String(chapter); + this.url = url === undefined ? "" : String(url); + this.frameTarget = frameTarget === undefined ? "" : String(frameTarget); + this.cuePointName = cuePointName === undefined ? "" : String(cuePointName); + this.duration = 0; + this.eventCuePoint = false; + this.label = 0; + this.protectedRegion = false; + this.__params = []; + if (params) this.setParameters(params); +} +MarkerValue.prototype.getParameters = function () { + var o = {}; + for (var i = 0; i < this.__params.length; i++) o[this.__params[i][0]] = this.__params[i][1]; + return o; +}; +MarkerValue.prototype.setParameters = function (o) { + this.__params = []; + for (var k in o) this.__params.push([k, String(o[k])]); +}; + +// ------------------------------------------------------------------ TextDocument + +// `layer.setText` keys of each TextDocument attribute. +var __textKeys = { + font: "font", fontStyle: "style", fontSize: "size", fillColor: "fill", strokeColor: "stroke", strokeWidth: "strokeWidth", + applyFill: "applyFill", applyStroke: "applyStroke", strokeOverFill: "strokeOverFill", tracking: "tracking", + baselineShift: "baselineShift", horizontalScale: "hScale", verticalScale: "vScale", fauxBold: "fauxBold", + fauxItalic: "fauxItalic", allCaps: "allCaps", smallCaps: "smallCaps", tsume: "tsume", superscript: "superscript", + subscript: "subscript", ligature: "ligatures", startIndent: "indentLeft", endIndent: "indentRight", + firstLineIndent: "indentFirst", spaceBefore: "spaceBefore", spaceAfter: "spaceAfter", +}; + +function TextDocument(text) { + if (!(this instanceof TextDocument)) return new TextDocument(text); + this.__base = null; + this.__changed = {}; + this.__text = text === undefined ? "" : String(text); + this.__textSet = text !== undefined; +} +TextDocument.__from = function (doc) { + var t = new TextDocument(); + t.__base = doc; + t.__text = doc.text; + t.__textSet = false; + return t; +}; +TextDocument.prototype = { + constructor: TextDocument, + get text() { return this.__text; }, + set text(v) { this.__text = String(v); this.__textSet = true; }, + __style: function (k, d) { + if (k in this.__changed) return this.__changed[k]; + if (this.__base && this.__base.first && k in this.__base.first) return this.__base.first[k]; + if (this.__base && this.__base.para && k in this.__base.para) return this.__base.para[k]; + return d; + }, + __set: function (k, v) { this.__changed[k] = v; }, + get font() { return this.__style("font", "Inter"); }, + set font(v) { this.__set("font", String(v)); }, + get fontFamily() { return this.font; }, + get fontStyle() { return this.__style("style", "Regular"); }, + set fontStyle(v) { this.__set("style", String(v)); }, + get fontSize() { return this.__style("size", 72); }, + set fontSize(v) { this.__set("size", __num(v, "fontSize")); }, + get fillColor() { var c = this.__style("fill", [1, 1, 1, 1]); return [c[0], c[1], c[2]]; }, + set fillColor(v) { this.__set("fill", [v[0], v[1], v[2], v.length > 3 ? v[3] : 1]); }, + get strokeColor() { var c = this.__style("stroke", [0, 0, 0, 1]); return [c[0], c[1], c[2]]; }, + set strokeColor(v) { this.__set("stroke", [v[0], v[1], v[2], v.length > 3 ? v[3] : 1]); }, + get strokeWidth() { return this.__style("strokeWidth", 0); }, + set strokeWidth(v) { this.__set("strokeWidth", __num(v, "strokeWidth")); }, + get applyFill() { return this.__style("applyFill", true); }, + set applyFill(v) { this.__set("applyFill", !!v); }, + get applyStroke() { return this.__style("applyStroke", false); }, + set applyStroke(v) { this.__set("applyStroke", !!v); }, + get strokeOverFill() { return "strokeOverFill" in this.__changed ? this.__changed.strokeOverFill : (this.__base ? this.__base.stroke_over_fill : true); }, + set strokeOverFill(v) { this.__set("strokeOverFill", !!v); }, + get tracking() { return this.__style("tracking", 0); }, + set tracking(v) { this.__set("tracking", __num(v, "tracking")); }, + get leading() { var l = this.__style("leading", "auto"); return l === "auto" ? this.fontSize * 1.2 : l; }, + set leading(v) { this.__set("leading", __num(v, "leading")); }, + get autoLeading() { return this.__style("leading", "auto") === "auto"; }, + set autoLeading(v) { if (v) this.__set("leading", "auto"); }, + get justification() { + var j = this.__style("justify", "left"); + for (var k in __justKey) if (__justKey[k] === j) return Number(k); + return ParagraphJustification.LEFT_JUSTIFY; + }, + set justification(v) { + var k = __justKey[v]; + if (!k) throw __err("Bad justification value " + v); + this.__set("justify", k); + }, + get baselineShift() { return this.__style("baselineShift", 0); }, + set baselineShift(v) { this.__set("baselineShift", __num(v)); }, + get horizontalScale() { return this.__style("hScale", 100) / 100; }, + set horizontalScale(v) { this.__set("hScale", __num(v) * 100); }, + get verticalScale() { return this.__style("vScale", 100) / 100; }, + set verticalScale(v) { this.__set("vScale", __num(v) * 100); }, + get fauxBold() { return this.__style("fauxBold", false); }, + set fauxBold(v) { this.__set("fauxBold", !!v); }, + get fauxItalic() { return this.__style("fauxItalic", false); }, + set fauxItalic(v) { this.__set("fauxItalic", !!v); }, + get allCaps() { return this.__style("allCaps", false); }, + set allCaps(v) { this.__set("allCaps", !!v); }, + get smallCaps() { return this.__style("smallCaps", false); }, + set smallCaps(v) { this.__set("smallCaps", !!v); }, + get superscript() { return this.__style("baseline", "normal") === "superscript"; }, + set superscript(v) { this.__set("superscript", !!v); }, + get subscript() { return this.__style("baseline", "normal") === "subscript"; }, + set subscript(v) { this.__set("subscript", !!v); }, + get tsume() { return this.__style("tsume", 0); }, + set tsume(v) { this.__set("tsume", __num(v)); }, + get boxText() { return !!(this.__base && this.__base.box_size); }, + get pointText() { return !this.boxText; }, + get boxTextSize() { return this.__base && this.__base.box_size ? this.__base.box_size.slice() : undefined; }, + set boxTextSize(v) { + var p = this.boxTextPos || [0, 0]; + this.__set("box", [p[0], p[1], v[0], v[1]]); + }, + get boxTextPos() { return this.__base && this.__base.box_size ? this.__base.box_pos.slice() : undefined; }, + get startIndent() { return this.__style("indentLeft", 0); }, + set startIndent(v) { this.__set("indentLeft", __num(v)); }, + get endIndent() { return this.__style("indentRight", 0); }, + set endIndent(v) { this.__set("indentRight", __num(v)); }, + get firstLineIndent() { return this.__style("indentFirst", 0); }, + set firstLineIndent(v) { this.__set("indentFirst", __num(v)); }, + get spaceBefore() { return this.__style("spaceBefore", 0); }, + set spaceBefore(v) { this.__set("spaceBefore", __num(v)); }, + get spaceAfter() { return this.__style("spaceAfter", 0); }, + set spaceAfter(v) { this.__set("spaceAfter", __num(v)); }, + resetCharStyle: function () { + var d = { font: "Inter", style: "Regular", size: 72, fill: [1, 1, 1, 1], stroke: [0, 0, 0, 1], strokeWidth: 0, applyFill: true, + applyStroke: false, tracking: 0, leading: "auto", baselineShift: 0, hScale: 100, vScale: 100, tsume: 0, fauxBold: false, + fauxItalic: false, allCaps: false, smallCaps: false }; + for (var k in d) this.__changed[k] = d[k]; + }, + resetParagraphStyle: function () { + var d = { justify: "left", indentLeft: 0, indentRight: 0, indentFirst: 0, spaceBefore: 0, spaceAfter: 0 }; + for (var k in d) this.__changed[k] = d[k]; + }, + toString: function () { return this.__text; }, + // `layer.setText` attributes to apply. + __attrs: function () { + var a = {}; + for (var k in this.__changed) a[k] = this.__changed[k]; + if (this.__textSet || !this.__base || this.__text !== this.__base.text) a.text = this.__text; + return a; + }, +}; + +// ------------------------------------------------------------------ properties + +function __propOf(layer, info) { + if (!info) return null; + if (info.isLayer) return layer; + if (info.kind === "group") return info.isMask ? new MaskPropertyGroup(layer, info.uid) : new PropertyGroup(layer, info.uid); + return new Property(layer, info.uid); +} + +function PropertyBase() {} +PropertyBase.prototype = { + __info: function () { return __get("node", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid }); }, + __ref: function (p) { + p = p || {}; + p.comp = this.__layer.__comp; + p.layer = this.__layer.__id; + p.prop = this.__uid; + return p; + }, + get name() { return this.__info().name; }, + set name(v) { + var i = this.__info(); + if (!i.instance) throw __err("Can not set the name of \"" + i.name + "\": it is not an indexed group member"); + __call("prop.renameGroup", this.__ref({ name: String(v) })); + }, + get matchName() { return this.__info().matchName; }, + get propertyIndex() { return this.__info().index; }, + get propertyDepth() { return this.__info().depth; }, + get propertyType() { return PropertyType[this.__info().propertyType]; }, + get parentProperty() { + var i = this.__info(); + if (i.depth <= 1) return this.__layer; + return __propOf(this.__layer, __get("node", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: i.parent })); + }, + get isModified() { return !!this.__info().isModified; }, + get canSetEnabled() { return !!this.__info().canSetEnabled; }, + get enabled() { return this.__info().enabled !== false; }, + set enabled(v) { + if (!this.canSetEnabled) throw __err("Can not set enabled on \"" + this.name + "\""); + __call("prop.setGroupEnabled", this.__ref({ value: !!v })); + }, + get active() { return this.enabled && this.__layer.enabled; }, + get elided() { return false; }, + get isEffect() { return !!this.__info().isEffect; }, + get isMask() { return !!this.__info().isMask; }, + get selected() { + var c = __get("item", { id: this.__layer.__comp }).selectedProperties; + for (var i = 0; i < c.length; i++) if (c[i][0] === this.__layer.__id && c[i][1] === this.__uid) return true; + return false; + }, + set selected(v) { + if (v) __call("prop.select", { comp: this.__layer.__comp, layer: this.__layer.__id, prop: this.__uid, add: true }); + }, + propertyGroup: function (n) { + var p = this; + n = n === undefined ? 1 : n; + for (var i = 0; i < n && p; i++) p = p.parentProperty; + return p || null; + }, + remove: function () { __call("prop.removeGroup", this.__ref()); }, + moveTo: function (i) { __call("prop.moveGroup", this.__ref({ index: __num(i, "index") })); }, + duplicate: function () { + var r = __call("prop.duplicateGroup", this.__ref()); + return __propOf(this.__layer, __get("node", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: r.prop })); + }, + toString: function () { return "[object " + this.__kind + "]"; }, +}; + +function PropertyGroup(layer, uid) { + this.__layer = layer; + this.__uid = uid; + this.__kind = "PropertyGroup"; + return new Proxy(this, __attr); +} +PropertyGroup.prototype = Object.create(PropertyBase.prototype); +PropertyGroup.prototype.constructor = PropertyGroup; +Object.defineProperty(PropertyGroup.prototype, "numProperties", { get: function () { return this.__info().numProperties; } }); +PropertyGroup.prototype.property = function (key) { + if (key === undefined || key === null) throw __err("property(): missing name or index"); + var k = typeof key === "number" ? key : String(key); + return __propOf(this.__layer, __get("child", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid, key: k })); +}; +PropertyGroup.prototype.canAddProperty = function (name) { + var r = __get("resolveAdd", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid, name: String(name) }); + return !r.error; +}; +PropertyGroup.prototype.addProperty = function (name) { + var L = this.__layer; + var r = __get("resolveAdd", { comp: L.__comp, layer: L.__id, uid: this.__uid, name: String(name) }); + if (r.error) throw __err(r.error); + var uid; + if (r.kind === "effect") uid = __call("effect.apply", { comp: L.__comp, layers: [L.__id], effect: r.effect }).effects[0]; + else if (r.kind === "mask") uid = __call("layer.addMask", { comp: L.__comp, layer: L.__id }).mask; + else if (r.kind === "shape") { + var p = { comp: L.__comp, layer: L.__id, kind: r.shape }; + if (r.group !== null && r.group !== undefined) p.group = r.group; + uid = __call("layer.addShapeItem", p).uid; + } else if (r.kind === "animator") { + uid = __call("layer.addTextAnimator", { comp: L.__comp, layer: L.__id, properties: [] }).animator; + } else if (r.kind === "animatorProperty") { + var before = this.numProperties; + __call("layer.addTextAnimatorProperty", { comp: L.__comp, layer: L.__id, animator: r.animator, property: r.property }); + return this.property(before + 1); + } else if (r.kind === "selector") { + var n0 = this.numProperties; + __call("layer.addTextSelector", { comp: L.__comp, layer: L.__id, animator: r.animator, kind: r.selector }); + return this.property(n0 + 1); + } else throw __err("Can not add \"" + name + "\" here"); + return __propOf(L, __get("node", { comp: L.__comp, layer: L.__id, uid: uid })); +}; + +function MaskPropertyGroup(layer, uid) { + this.__layer = layer; + this.__uid = uid; + this.__kind = "MaskPropertyGroup"; + return new Proxy(this, __attr); +} +MaskPropertyGroup.prototype = Object.create(PropertyGroup.prototype); +MaskPropertyGroup.prototype.constructor = MaskPropertyGroup; +(function () { + var modes = { "None": 6812, "Add": 6813, "Subtract": 6814, "Intersect": 6815, "Lighten": 6816, "Darken": 6817, "Difference": 6818 }; + function mask(o) { return { comp: o.__layer.__comp, layer: o.__layer.__id, mask: o.__uid }; } + Object.defineProperty(MaskPropertyGroup.prototype, "maskMode", { + get: function () { return modes[this.__info().maskMode]; }, + set: function (v) { + var p = mask(this); + for (var k in modes) if (modes[k] === v) p.mode = k; + if (!p.mode) throw __err("Bad MaskMode value " + v); + __call("layer.mask.mode", p); + }, + }); + Object.defineProperty(MaskPropertyGroup.prototype, "inverted", { + get: function () { return !!this.__info().inverted; }, + set: function (v) { var p = mask(this); p.value = !!v; __call("layer.mask.invert", p); }, + }); + Object.defineProperty(MaskPropertyGroup.prototype, "locked", { + get: function () { return !!this.__info().locked; }, + set: function (v) { var p = mask(this); p.value = !!v; __call("layer.mask.lock", p); }, + }); + Object.defineProperty(MaskPropertyGroup.prototype, "color", { get: function () { return this.__info().color; } }); + MaskPropertyGroup.prototype.remove = function () { __call("layer.mask.remove", mask(this)); }; +})(); + +function Property(layer, uid) { + this.__layer = layer; + this.__uid = uid; + this.__kind = "Property"; + return new Proxy(this, __attr); +} +Property.prototype = Object.create(PropertyBase.prototype); +Property.prototype.constructor = Property; +Property.prototype.property = function () { return null; }; +(function () { + var P = Property.prototype; + function def(name, get, set) { Object.defineProperty(P, name, { get: get, set: set }); } + def("propertyValueType", function () { return PropertyValueType[this.__info().valueType]; }); + def("value", function () { return this.valueAtTime(this.__layer.containingComp.time, false); }); + def("numKeys", function () { return this.__info().numKeys; }); + def("expression", function () { return this.__info().expression; }, function (v) { + if (!this.canSetExpression) throw __err("Can not set an expression on \"" + this.name + "\""); + __call("prop.setExpression", this.__ref({ expression: String(v) })); + }); + def("expressionEnabled", function () { return !!this.__info().expressionEnabled; }, function (v) { + __call("prop.setExpression", this.__ref({ enabled: !!v })); + }); + def("expressionError", function () { + var e = this.expression; + return e ? __get("exprError", { code: e }) : ""; + }); + def("canSetExpression", function () { return !!this.__info().canSetExpression; }); + def("canVaryOverTime", function () { return !!this.__info().canVaryOverTime; }); + def("isTimeVarying", function () { var i = this.__info(); return i.numKeys > 0 || !!i.expressionEnabled; }); + def("isSpatial", function () { return !!this.__info().spatial; }); + def("unitsText", function () { return this.__info().ui; }); + def("hasMin", function () { return this.__info().min !== undefined; }); + def("hasMax", function () { return this.__info().max !== undefined; }); + def("minValue", function () { var i = this.__info(); if (i.min === undefined) throw __err("property has no minimum"); return i.min; }); + def("maxValue", function () { var i = this.__info(); if (i.max === undefined) throw __err("property has no maximum"); return i.max; }); + def("dimensionsSeparated", function () { return !!this.__info().separated; }, function (v) { + __call("prop.separateDimensions", { comp: this.__layer.__comp, layer: this.__layer.__id, value: !!v }); + }); + def("isSeparationFollower", function () { return !!this.__info().separationFollower; }); + def("isSeparationLeader", function () { return this.__info().matchId === "position" && this.__info().depth === 2; }); + def("separationDimension", function () { var m = this.__info().matchId; return m === "positionY" ? 1 : m === "positionZ" ? 2 : 0; }); + def("separationLeader", function () { return this.isSeparationFollower ? this.parentProperty.property("ADBE Position") : null; }); + def("selectedKeys", function () { + var k = this.__keys(), out = []; + for (var i = 0; i < k.length; i++) if (k[i].selected) out.push(i + 1); + return out; + }); + def("isDropdownEffect", function () { return false; }); + + P.getSeparationFollower = function (dim) { + return this.parentProperty.property(["ADBE Position_0", "ADBE Position_1", "ADBE Position_2"][dim]); + }; + P.__keys = function () { return __get("keys", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid }); }; + P.__key = function (i) { + var k = this.__keys(); + i = __num(i, "keyIndex"); + if (i < 1 || i > k.length || Math.floor(i) !== i) throw __err("keyIndex " + i + " is out of range 1.." + k.length); + return k[i - 1]; + }; + // Script value → engine JSON for `prop.set` / `prop.addKey`. + P.__toEngine = function (v, info, time) { + var t = info.valueType; + if (t === "TEXT_DOCUMENT") { + var td = v instanceof TextDocument ? v : new TextDocument(String(v)); + return __get("textdoc", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid, time: time, attrs: td.__attrs() }); + } + if (t === "SHAPE") { + if (!v || !v.vertices) throw __err("value must be a Shape"); + var n = v.vertices.length; + function fit(a) { var o = []; for (var i = 0; i < n; i++) o.push(a && a[i] ? [a[i][0], a[i][1]] : [0, 0]); return o; } + return { vertices: fit(v.vertices), in_tangents: fit(v.inTangents), out_tangents: fit(v.outTangents), closed: v.closed !== false }; + } + if (t === "LAYER_INDEX") { + var idx = v && v.__id !== undefined ? v.index : __num(v); + if (!idx) return null; + return this.__layer.containingComp.layer(idx).__id; + } + if (info.type === "enum") return __num(v) - 1; + if (info.type === "bool") return typeof v === "boolean" ? v : __num(v) !== 0; + if (info.type === "color") { + if (!(v instanceof Array) || v.length < 3) throw __err("value must be an array of 3 or 4 numbers"); + return [v[0], v[1], v[2], v.length > 3 ? v[3] : 1]; + } + if (info.dims > 1) { + if (!(v instanceof Array)) throw __err("value must be an array of " + info.dims + " numbers"); + var o = []; + for (var d = 0; d < info.dims; d++) o.push(d < v.length ? __num(v[d]) : (d === 2 ? 0 : __num(v[v.length - 1]))); + return o; + } + if (v instanceof Array) throw __err("value must be a number, not an array"); + return __num(v); + }; + P.__fromEngine = function (v, info) { + if (info.valueType === "TEXT_DOCUMENT") return TextDocument.__from(v); + if (info.valueType === "SHAPE") { + var s = new Shape(); + s.vertices = v.vertices; + s.inTangents = v.inTangents; + s.outTangents = v.outTangents; + s.closed = v.closed; + return s; + } + if (info.type === "color" && v instanceof Array) return v.slice(); + return v; + }; + P.valueAtTime = function (t, preExpression) { + var info = this.__info(); + var v = __get("value", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid, time: __num(t, "time"), pre: !!preExpression }); + return this.__fromEngine(v, info); + }; + P.setValue = function (v) { + var info = this.__info(); + if (info.numKeys > 0) throw __err("Can not use setValue on \"" + info.name + "\": it has keyframes (use setValueAtTime or setValueAtKey)"); + if (info.hidden) throw __err("\"" + info.name + "\" can not be set"); + __call("prop.set", this.__ref({ value: this.__toEngine(v, info) })); + }; + P.setValueAtTime = function (t, v) { + var info = this.__info(); + t = __num(t, "time"); + __call("prop.addKey", this.__ref({ time: this.__layer.__layerTime(t), value: this.__toEngine(v, info, t) })); + }; + P.setValuesAtTimes = function (ts, vs) { + if (!(ts instanceof Array) || !(vs instanceof Array) || ts.length !== vs.length) throw __err("setValuesAtTimes: times and values must be arrays of the same length"); + for (var i = 0; i < ts.length; i++) this.setValueAtTime(ts[i], vs[i]); + }; + P.setValueAtKey = function (i, v) { + var k = this.__key(i), info = this.__info(); + __call("keys.set", this.__ref({ time: k.layerTime, value: this.__toEngine(v, info, k.time) })); + }; + P.addKey = function (t) { + t = __num(t, "time"); + __call("prop.addKey", this.__ref({ time: this.__layer.__layerTime(t) })); + return this.nearestKeyIndex(t); + }; + P.nearestKeyIndex = function (t) { + var k = this.__keys(), best = 0, d = Infinity; + if (!k.length) throw __err("\"" + this.name + "\" has no keyframes"); + for (var i = 0; i < k.length; i++) { + var e = Math.abs(k[i].time - t); + if (e < d) { d = e; best = i + 1; } + } + return best; + }; + P.keyTime = function (i) { return this.__key(i).time; }; + P.keyValue = function (i) { return this.__fromEngine(this.__key(i).value, this.__info()); }; + // Select key `i` (only) so the selection-based keyframe commands act on it. + P.__select = function (i) { + var k = this.__key(i); + __call("keys.select", { comp: this.__layer.__comp, keys: [{ layer: this.__layer.__id, prop: this.__uid, time: k.layerTime }] }); + return k; + }; + P.removeKey = function (i) { + this.__select(i); + __call("keys.delete", { comp: this.__layer.__comp }); + }; + P.keyInInterpolationType = function (i) { return KeyframeInterpolationType[this.__key(i).inInterp]; }; + P.keyOutInterpolationType = function (i) { return KeyframeInterpolationType[this.__key(i).outInterp]; }; + P.isInterpolationTypeValid = function (t) { return t === 6612 || t === 6613 || t === 6614; }; + P.setInterpolationTypeAtKey = function (i, inType, outType) { + if (outType === undefined) outType = inType; + if (!__interpKey[inType] || !__interpKey[outType]) throw __err("Bad KeyframeInterpolationType"); + this.__select(i); + __call("keys.interpolation", { comp: this.__layer.__comp, "in": __interpKey[inType], out: __interpKey[outType] }); + }; + P.keyInTemporalEase = function (i) { return this.__key(i).inEase.map(function (e) { return new KeyframeEase(e.speed, e.influence); }); }; + P.keyOutTemporalEase = function (i) { return this.__key(i).outEase.map(function (e) { return new KeyframeEase(e.speed, e.influence); }); }; + P.setTemporalEaseAtKey = function (i, inEase, outEase) { + if (outEase === undefined) outEase = inEase; + if (!(inEase instanceof Array) || !(outEase instanceof Array)) throw __err("setTemporalEaseAtKey: eases must be arrays of KeyframeEase"); + this.__select(i); + function sp(a) { return a.map(function (e) { return __num(e.speed, "speed"); }); } + function inf(a) { + return a.map(function (e) { + var x = __num(e.influence, "influence"); + if (x < 0.1 || x > 100) throw __err("influence must be between 0.1 and 100"); + return x; + }); + } + __call("keys.velocity", { comp: this.__layer.__comp, inSpeed: sp(inEase), inInfluence: inf(inEase), outSpeed: sp(outEase), outInfluence: inf(outEase) }); + }; + P.keyTemporalContinuous = function (i) { return !!this.__key(i).continuous; }; + P.setTemporalContinuousAtKey = function (i, b) { this.__select(i); __call("keys.interpolation", { comp: this.__layer.__comp, continuous: !!b }); }; + P.keyTemporalAutoBezier = function (i) { return !!this.__key(i).autoBezier; }; + P.setTemporalAutoBezierAtKey = function (i, b) { this.__select(i); __call("keys.interpolation", { comp: this.__layer.__comp, autoBezier: !!b }); }; + P.keyRoving = function (i) { return !!this.__key(i).roving; }; + P.setRovingAtKey = function (i, b) { this.__select(i); __call("keys.interpolation", { comp: this.__layer.__comp, roving: !!b }); }; + P.keySpatialInTangent = function (i) { return this.__key(i).spatialIn.slice(0, this.__info().dims); }; + P.keySpatialOutTangent = function (i) { return this.__key(i).spatialOut.slice(0, this.__info().dims); }; + P.keySpatialContinuous = function (i) { return !!this.__key(i).spatialContinuous; }; + P.keySpatialAutoBezier = function (i) { return !!this.__key(i).spatialAutoBezier; }; + P.setSpatialTangentsAtKey = function (i, inT, outT) { + var k = this.__key(i); + __call("keys.setSpatialTangents", this.__ref({ time: k.layerTime, "in": inT, out: outT === undefined ? inT : outT })); + }; + P.setSpatialContinuousAtKey = function (i, b) { this.__select(i); __call("keys.interpolation", { comp: this.__layer.__comp, spatial: b ? "continuousBezier" : "bezier" }); }; + P.setSpatialAutoBezierAtKey = function (i, b) { this.__select(i); __call("keys.interpolation", { comp: this.__layer.__comp, spatial: b ? "autoBezier" : "bezier" }); }; + P.keySelected = function (i) { return !!this.__key(i).selected; }; + P.setSelectedAtKey = function (i, b) { + var k = this.__key(i); + if (b) { + __call("keys.select", { comp: this.__layer.__comp, add: true, keys: [{ layer: this.__layer.__id, prop: this.__uid, time: k.layerTime }] }); + } else { + var keep = [], all = this.__keys(); + for (var j = 0; j < all.length; j++) if (all[j].selected && j !== i - 1) keep.push({ layer: this.__layer.__id, prop: this.__uid, time: all[j].layerTime }); + __call("keys.select", { comp: this.__layer.__comp, keys: keep }); + } + }; + P.keyLabel = function () { return 0; }; + P.setLabelAtKey = function () {}; + P.addToMotionGraphicsTemplate = function () { return false; }; + P.canAddToMotionGraphicsTemplate = function () { return false; }; +})(); + +// Layer / composition markers (`layer.marker`, `comp.markerProperty`): keyframes are markers. +function MarkerProperty(comp, layer) { + this.__compId = comp; + this.__layerRef = layer; + this.__kind = "Property"; +} +MarkerProperty.prototype = { + constructor: MarkerProperty, + __p: function (o) { + o = o || {}; + o.comp = this.__compId; + if (this.__layerRef) o.layer = this.__layerRef.__id; + return o; + }, + __list: function () { return __get("markers", this.__p()); }, + get name() { return "Marker"; }, + get matchName() { return "ADBE Marker"; }, + get propertyValueType() { return PropertyValueType.MARKER; }, + get propertyType() { return PropertyType.PROPERTY; }, + get numKeys() { return this.__list().length; }, + get value() { return null; }, + get canSetExpression() { return false; }, + get canVaryOverTime() { return true; }, + get isTimeVarying() { return this.numKeys > 0; }, + get parentProperty() { return this.__layerRef || null; }, + __at: function (i) { + var l = this.__list(); + if (i < 1 || i > l.length) throw __err("marker index " + i + " is out of range 1.." + l.length); + return l[i - 1]; + }, + keyTime: function (i) { return this.__at(i).time; }, + keyValue: function (i) { + var m = this.__at(i); + var v = new MarkerValue(m.comment, m.chapter, m.url, m.frameTarget, m.cuePointName); + v.duration = m.duration; + v.eventCuePoint = m.eventCuePoint; + v.label = m.label; + v.protectedRegion = m.protectedRegion; + v.__params = m.params || []; + return v; + }, + nearestKeyIndex: function (t) { + var l = this.__list(), best = 0, d = Infinity; + for (var i = 0; i < l.length; i++) if (Math.abs(l[i].time - t) < d) { d = Math.abs(l[i].time - t); best = i + 1; } + if (!best) throw __err("there are no markers"); + return best; + }, + setValueAtTime: function (t, mv) { + t = __num(t, "time"); + if (!(mv instanceof MarkerValue)) throw __err("value must be a MarkerValue"); + var p = this.__p({ time: t, duration: mv.duration || 0, comment: mv.comment, chapter: mv.chapter, url: mv.url, frameTarget: mv.frameTarget, protected: !!mv.protectedRegion, label: mv.label || 0 }); + p.cuePoint = mv.cuePointName ? { name: mv.cuePointName, navigation: !mv.eventCuePoint, params: mv.__params } : null; + var l = this.__list(), at = -1; + for (var i = 0; i < l.length; i++) if (Math.abs(l[i].time - t) < 1e-6) at = i; + if (at >= 0) p.index = at; + else p["new"] = true; + __call("markers.set", p); + }, + setValueAtKey: function (i, mv) { this.setValueAtTime(this.keyTime(i), mv); }, + removeKey: function (i) { this.__at(i); __call("markers.delete", this.__p({ index: i - 1 })); }, + toString: function () { return "[object Property]"; }, +}; + +// ------------------------------------------------------------------ layers + +function __layer(compId, id) { + if (id === null || id === undefined) return null; + var i = __get("layer", { comp: compId, layer: id }); + switch (i.kind) { + case "text": return new TextLayer(compId, id); + case "shape": return new ShapeLayer(compId, id); + case "camera": return new CameraLayer(compId, id); + case "light": return new LightLayer(compId, id); + default: return new AVLayer(compId, id); + } +} + +function Layer() {} +Layer.prototype = { + __init: function (comp, id, kind) { + this.__comp = comp; + this.__id = id; + this.__kind = kind; + return new Proxy(this, __attr); + }, + __info: function () { return __get("layer", { comp: this.__comp, layer: this.__id }); }, + __p: function (o) { + o = o || {}; + o.comp = this.__comp; + o.layers = [this.__id]; + return o; + }, + __p1: function (o) { + o = o || {}; + o.comp = this.__comp; + o.layer = this.__id; + return o; + }, + __switch: function (sw, v) { __call("layer.setSwitch", this.__p({ "switch": sw, value: !!v })); }, + // Comp seconds → layer seconds (keyframe times are layer time). + __layerTime: function (t) { + var i = this.__info(); + return (t - i.startTime) * 100 / (i.stretch || 100); + }, + toString: function () { return "[object " + this.__kind + "]"; }, + get id() { return this.__id; }, + get name() { return this.__info().name; }, + set name(v) { __call("layer.rename", this.__p1({ name: String(v) })); }, + get index() { return this.__info().index; }, + get matchName() { return this.__info().matchName; }, + get containingComp() { return new CompItem(this.__comp); }, + get propertyDepth() { return 0; }, + get propertyType() { return PropertyType.INDEXED_GROUP; }, + get parentProperty() { return null; }, + get isModified() { return false; }, + get elided() { return false; }, + get isEffect() { return false; }, + get isMask() { return false; }, + get canSetEnabled() { return true; }, + get enabled() { return this.__info().enabled; }, + set enabled(v) { this.__switch("video", v); }, + get active() { return this.__info().active; }, + get solo() { return this.__info().solo; }, + set solo(v) { this.__switch("solo", v); }, + get locked() { return this.__info().locked; }, + set locked(v) { this.__switch("lock", v); }, + get shy() { return this.__info().shy; }, + set shy(v) { this.__switch("shy", v); }, + get audioEnabled() { return this.__info().audioEnabled; }, + set audioEnabled(v) { this.__switch("audio", v); }, + get hasVideo() { return this.__info().hasVideo; }, + get hasAudio() { return this.__info().hasAudio; }, + get audioActive() { var i = this.__info(); return i.hasAudio && i.audioEnabled; }, + get nullLayer() { return this.__info().nullLayer; }, + get isNameSet() { return true; }, + get inPoint() { return this.__info().inPoint; }, + set inPoint(v) { __call("layer.timing", this.__p({ "in": __num(v, "inPoint") })); }, + get outPoint() { return this.__info().outPoint; }, + set outPoint(v) { __call("layer.timing", this.__p({ out: __num(v, "outPoint") })); }, + get startTime() { return this.__info().startTime; }, + set startTime(v) { __call("layer.timing", this.__p({ start: __num(v, "startTime") })); }, + get stretch() { return this.__info().stretch; }, + set stretch(v) { __call("layer.timeStretch", this.__p({ percent: __num(v, "stretch") })); }, + get time() { return this.__info().time; }, + get label() { return this.__info().label; }, + set label(v) { + var names = __get("labels"); + var n = names[__num(v, "label")]; + if (n === undefined) throw __err("label must be 0-16"); + __call("edit.label", this.__p({ label: n })); + }, + get comment() { return this.__info().comment; }, + get selected() { return this.__info().selected; }, + set selected(v) { + var sel = this.containingComp.__info().selectedLayers; + var on = sel.indexOf(this.__id) >= 0; + if (!!v !== on) __call("layer.select", { comp: this.__comp, layers: [this.__id], toggle: true }); + }, + get selectedProperties() { + var c = this.containingComp.__info().selectedProperties, out = []; + for (var i = 0; i < c.length; i++) if (c[i][0] === this.__id) out.push(__propOf(this, __get("node", { comp: this.__comp, layer: this.__id, uid: c[i][1] }))); + return out; + }, + get parent() { var p = this.__info().parent; return p === null ? null : __layer(this.__comp, p); }, + set parent(v) { __call("layer.setParent", this.__p({ parent: v ? v.__id : null })); }, + setParentWithJump: function (v) { __call("layer.setParent", this.__p({ parent: v ? v.__id : null, compensate: false })); }, + get autoOrient() { return AutoOrientType[{ Off: "NO_AUTO_ORIENT", AlongPath: "ALONG_PATH", TowardsCamera: "CHARACTERS_TOWARD_CAMERA", TowardsPointOfInterest: "CAMERA_OR_POINT_OF_INTEREST" }[this.__info().autoOrient]]; }, + set autoOrient(v) { + var m = { 4212: "off", 4213: "alongPath", 4214: "towardsPointOfInterest", 4215: "towardsCamera" }[v]; + if (!m) throw __err("Bad AutoOrientType value " + v); + __call("layer.autoOrient", this.__p({ mode: m })); + }, + get marker() { return new MarkerProperty(this.__comp, this); }, + get numProperties() { return __get("node", { comp: this.__comp, layer: this.__id, uid: 0 }).numProperties; }, + property: function (key) { + if (key === undefined || key === null) throw __err("property(): missing name or index"); + if (key === "Marker" || key === "ADBE Marker" || key === "marker") return this.marker; + var k = typeof key === "number" ? key : String(key); + return __propOf(this, __get("child", { comp: this.__comp, layer: this.__id, uid: 0, key: k })); + }, + propertyGroup: function () { return null; }, + canAddProperty: function () { return false; }, + addProperty: function (n) { throw __err("Can not add \"" + n + "\" to a layer: use a property group (Effects, Masks, Contents)"); }, + remove: function () { __call("edit.clear", this.__p()); }, + duplicate: function () { + var before = this.containingComp.__info().layers; + __call("edit.duplicate", this.__p()); + var after = this.containingComp.__info().layers; + for (var i = 0; i < after.length; i++) if (before.indexOf(after[i]) < 0) return __layer(this.__comp, after[i]); + return null; + }, + copyToComp: function (comp) { + var c = __call("edit.copy", this.__p()); + var before = comp.__info().layers; + __call("comp.open", { comp: comp.__id }); + __call("edit.paste", { comp: comp.__id }); + return c; + }, + moveToBeginning: function () { __call("layer.arrange", this.__p({ to: "front" })); }, + moveToEnd: function () { __call("layer.arrange", this.__p({ to: "back" })); }, + moveBefore: function (other) { + var me = this.index, to = other.index; + __call("layer.arrange", this.__p({ to: "index", index: me < to ? to - 1 : to })); + }, + moveAfter: function (other) { + var me = this.index, to = other.index; + __call("layer.arrange", this.__p({ to: "index", index: me < to ? to : to + 1 })); + }, + activeAtTime: function (t) { var i = this.__info(); return i.enabled && t >= i.inPoint && t < i.outPoint; }, + applyPreset: function (f) { __call("anim.applyPreset", this.__p({ path: __str(f) })); }, +}; + +function AVLayer(comp, id) { return this.__init(comp, id, "AVLayer"); } +AVLayer.prototype = Object.create(Layer.prototype); +AVLayer.prototype.constructor = AVLayer; +(function () { + var P = AVLayer.prototype; + function def(name, get, set) { Object.defineProperty(P, name, { get: get, set: set }); } + def("source", function () { return __item(this.__info().source); }); + def("width", function () { return this.__info().width; }); + def("height", function () { return this.__info().height; }); + def("threeDLayer", function () { return this.__info().threeDLayer; }, function (v) { this.__switch("threeD", v); }); + def("adjustmentLayer", function () { return this.__info().adjustmentLayer; }, function (v) { this.__switch("adjustment", v); }); + def("guideLayer", function () { return this.__info().guideLayer; }, function (v) { this.__switch("guide", v); }); + def("motionBlur", function () { return this.__info().motionBlur; }, function (v) { this.__switch("motionBlur", v); }); + def("effectsActive", function () { return this.__info().effectsActive; }, function (v) { this.__switch("fx", v); }); + def("collapseTransformation", function () { return this.__info().collapse; }, function (v) { this.__switch("collapse", v); }); + def("preserveTransparency", function () { return this.__info().preserveTransparency; }, function (v) { this.__switch("preserveTransparency", v); }); + def("quality", function () { return LayerQuality[this.__info().quality.toUpperCase()]; }, function (v) { + var q = __enumKey(LayerQuality, v); + if (!q) throw __err("Bad LayerQuality value " + v); + __call("layer.quality", this.__p({ quality: q.toLowerCase() })); + }); + def("samplingQuality", function () { return LayerSamplingQuality[this.__info().samplingQuality.toUpperCase()]; }, function (v) { + var q = __enumKey(LayerSamplingQuality, v); + if (!q) throw __err("Bad LayerSamplingQuality value " + v); + __call("layer.sampling", this.__p({ sampling: q.toLowerCase() })); + }); + def("frameBlendingType", function () { return { Off: 4012, FrameMix: 4013, PixelMotion: 4014 }[this.__info().frameBlending]; }, function (v) { + var m = { 4012: "off", 4013: "frameMix", 4014: "pixelMotion" }[v]; + if (!m) throw __err("Bad FrameBlendingType value " + v); + __call("layer.frameBlending", this.__p({ mode: m })); + }); + def("frameBlending", function () { return this.__info().frameBlending !== "Off"; }); + def("blendingMode", function () { return __blendNames[this.__info().blendingMode]; }, function (v) { + __call("layer.setBlendMode", this.__p({ mode: __blendLabel(v) })); + }); + def("trackMatteType", function () { + var m = this.__info().trackMatte; + return m ? __matteEnum[m.kind] : TrackMatteType.NO_TRACK_MATTE; + }, function (v) { + if (v === TrackMatteType.NO_TRACK_MATTE) { __call("layer.trackMatte", this.__p({ op: "none" })); return; } + var k = __matteKind[v]; + if (!k) throw __err("Bad TrackMatteType value " + v); + var m = this.__info().trackMatte; + if (m) __call("layer.setTrackMatte", this.__p1({ matte: m.layer, kind: k })); + else __call("layer.trackMatte", this.__p({ op: k })); + }); + def("trackMatteLayer", function () { var m = this.__info().trackMatte; return m ? __layer(this.__comp, m.layer) : null; }); + def("hasTrackMatte", function () { return !!this.__info().trackMatte; }); + def("isTrackMatte", function () { return this.__info().isTrackMatte; }); + def("timeRemapEnabled", function () { return this.__info().timeRemapEnabled; }, function (v) { + __call("layer.enableTimeRemap", this.__p({ value: !!v })); + }); + def("canSetTimeRemapEnabled", function () { var s = this.source; return !!s && !(s instanceof FootageItem && s.mainSource.isStill); }); + def("canSetCollapseTransformation", function () { return true; }); + def("isNameFromSource", function () { var s = this.source; return !!s && s.name === this.name; }); + def("environmentLayer", function () { return false; }); + P.setTrackMatte = function (layer, type) { + if (!layer || type === TrackMatteType.NO_TRACK_MATTE) { this.removeTrackMatte(); return; } + var k = __matteKind[type]; + if (!k) throw __err("Bad TrackMatteType value " + type); + __call("layer.setTrackMatte", this.__p1({ matte: layer.__id, kind: k })); + }; + P.removeTrackMatte = function () { __call("layer.setTrackMatte", this.__p1({ matte: null })); }; + P.sourceRectAtTime = function (t, extents) { + return __get("sourceRect", { comp: this.__comp, layer: this.__id, time: __num(t, "time"), extents: !!extents }); + }; + P.sourcePointToComp = function (p) { return p; }; + P.compPointToSource = function (p) { return p; }; + P.replaceSource = function () { throw __err("replaceSource is not supported yet"); }; + P.openInViewer = function () { __call("layer.openLayer", this.__p1()); return null; }; +})(); + +function TextLayer(comp, id) { return this.__init(comp, id, "TextLayer"); } +TextLayer.prototype = Object.create(AVLayer.prototype); +TextLayer.prototype.constructor = TextLayer; +Object.defineProperty(TextLayer.prototype, "source", { get: function () { return null; } }); + +function ShapeLayer(comp, id) { return this.__init(comp, id, "ShapeLayer"); } +ShapeLayer.prototype = Object.create(AVLayer.prototype); +ShapeLayer.prototype.constructor = ShapeLayer; +Object.defineProperty(ShapeLayer.prototype, "source", { get: function () { return null; } }); + +function CameraLayer(comp, id) { return this.__init(comp, id, "CameraLayer"); } +CameraLayer.prototype = Object.create(Layer.prototype); +CameraLayer.prototype.constructor = CameraLayer; + +function LightLayer(comp, id) { return this.__init(comp, id, "LightLayer"); } +LightLayer.prototype = Object.create(Layer.prototype); +LightLayer.prototype.constructor = LightLayer; +Object.defineProperty(LightLayer.prototype, "lightType", { + get: function () { return LightType[this.__info().lightType.toUpperCase()]; }, + set: function (v) { + var k = __enumKey(LightType, v); + if (!k) throw __err("Bad LightType value " + v); + __call("layer.lightSettings", this.__p1({ kind: k.charAt(0) + k.substring(1).toLowerCase() })); + }, +}); + +// ------------------------------------------------------------------ collections + +function LayerCollection(comp) { + this.__comp = comp; + return new Proxy(this, __coll); +} +LayerCollection.prototype = { + constructor: LayerCollection, + __ids: function () { return __get("item", { id: this.__comp }).layers; }, + get length() { return this.__ids().length; }, + __at: function (i) { var ids = this.__ids(); return i >= 1 && i <= ids.length ? __layer(this.__comp, ids[i - 1]) : undefined; }, + __new: function (r) { return __layer(this.__comp, r.layer); }, + __dur: function (layer, duration) { + if (duration !== undefined && duration !== null) { + var i = layer.__info(); + __call("layer.timing", { comp: this.__comp, layers: [layer.__id], out: i.inPoint + __num(duration, "duration") }); + } + return layer; + }, + add: function (item, duration) { + if (!item || item.id === undefined) throw __err("add(): expected an item"); + var l = this.__new(__call("layer.addItem", { comp: this.__comp, item: item.id, time: 0 })); + return this.__dur(l, duration); + }, + addSolid: function (color, name, width, height, pixelAspect, duration) { + var p = { comp: this.__comp, color: [color[0], color[1], color[2]] }; + if (name !== undefined) p.name = String(name); + if (width !== undefined) p.width = Math.round(__num(width, "width")); + if (height !== undefined) p.height = Math.round(__num(height, "height")); + return this.__dur(this.__new(__call("layer.newSolid", p)), duration); + }, + addNull: function (duration) { return this.__dur(this.__new(__call("layer.newNull", { comp: this.__comp })), duration); }, + addText: function (text) { + var t = text instanceof TextDocument ? text : new TextDocument(text === undefined ? "" : String(text)); + var p = t.__attrs(); + p.comp = this.__comp; + if (p.text === undefined) p.text = t.text; + return this.__new(__call("layer.newText", p)); + }, + addBoxText: function (size, text) { + var c = __get("item", { id: this.__comp }); + var w = size[0], h = size[1]; + var t = text instanceof TextDocument ? text : new TextDocument(text === undefined ? "" : String(text)); + var p = t.__attrs(); + p.comp = this.__comp; + if (p.text === undefined) p.text = t.text; + p.box = [(c.width - w) / 2, (c.height - h) / 2, w, h]; + return this.__new(__call("layer.newText", p)); + }, + addShape: function () { return this.__new(__call("layer.newShape", { comp: this.__comp, kind: "none" })); }, + addCamera: function (name, center) { + var p = { comp: this.__comp, name: String(name) }; + if (center) p.poi = [center[0], center[1], 0]; + return this.__new(__call("layer.newCamera", p)); + }, + addLight: function (name, center) { + var p = { comp: this.__comp, name: String(name), kind: "Point" }; + if (center) p.poi = [center[0], center[1], 0]; + return this.__new(__call("layer.newLight", p)); + }, + precompose: function (indices, name, moveAllAttributes) { + var ids = this.__ids(), sel = []; + for (var i = 0; i < indices.length; i++) sel.push(ids[indices[i] - 1]); + var r = __call("layer.precompose", { comp: this.__comp, layers: sel, name: String(name), mode: moveAllAttributes === false ? "leave" : "move" }); + return new CompItem(r.comp); + }, + byName: function (name) { + var ids = this.__ids(); + for (var i = 0; i < ids.length; i++) { + var l = __get("layer", { comp: this.__comp, layer: ids[i] }); + if (l.name === name) return __layer(this.__comp, ids[i]); + } + return null; + }, + toString: function () { return "[object LayerCollection]"; }, +}; + +function ItemCollection(folder) { + this.__folder = folder; + return new Proxy(this, __coll); +} +ItemCollection.prototype = { + constructor: ItemCollection, + __ids: function () { + if (this.__folder === 0) return __get("project").items; + return __get("item", { id: this.__folder }).children; + }, + get length() { return this.__ids().length; }, + __at: function (i) { var ids = this.__ids(); return i >= 1 && i <= ids.length ? __item(ids[i - 1]) : undefined; }, + __place: function (id) { + if (this.__folder) __call("project.move", { items: [id], folder: this.__folder }); + return __item(id); + }, + addComp: function (name, width, height, pixelAspect, duration, frameRate) { + var r = __call("comp.new", { + name: String(name), width: Math.round(__num(width, "width")), height: Math.round(__num(height, "height")), + pixelAspect: __num(pixelAspect, "pixelAspect"), duration: __num(duration, "duration"), frameRate: __num(frameRate, "frameRate"), open: false, + }); + return this.__place(r.comp); + }, + addFolder: function (name) { + var r = __call("project.newFolder", { name: String(name) }); + var id = r && typeof r === "object" ? (r.folder || r.item || r.id) : r; + return this.__place(id); + }, + toString: function () { return "[object ItemCollection]"; }, +}; + +// ------------------------------------------------------------------ items + +function __item(id) { + if (id === null || id === undefined) return null; + if (id === 0) return new FolderItem(0); + var i = __get("item", { id: id }); + switch (i.kind) { + case "comp": return new CompItem(id); + case "folder": return new FolderItem(id); + default: return new FootageItem(id); + } +} + +function Item() {} +Item.prototype = { + __info: function () { return __get("item", { id: this.__id }); }, + toString: function () { return "[object " + this.__kind + "]"; }, + get id() { return this.__id; }, + get name() { return this.__info().name; }, + set name(v) { __call("project.rename", { item: this.__id, name: String(v) }); }, + get typeName() { return this.__info().typeName; }, + get comment() { return this.__info().comment; }, + set comment(v) { __call("project.setComment", { items: [this.__id], comment: String(v) }); }, + get label() { return this.__info().label; }, + set label(v) { __call("project.setLabel", { items: [this.__id], label: __num(v, "label") }); }, + get parentFolder() { return __item(this.__info().parent); }, + set parentFolder(f) { __call("project.move", { items: [this.__id], folder: f && f.__id ? f.__id : null }); }, + get selected() { return this.__info().selected; }, + set selected(v) { if (v) __call("project.select", { items: [this.__id], add: true }); }, + get usedIn() { return this.__info().usedIn.map(function (c) { return new CompItem(c); }); }, + remove: function () { __call("project.delete", { items: [this.__id] }); }, +}; + +function AVItem() {} +AVItem.prototype = Object.create(Item.prototype); +(function () { + function def(name, get, set) { Object.defineProperty(AVItem.prototype, name, { get: get, set: set }); } + def("width", function () { return this.__info().width; }, function (v) { this.__settings({ width: Math.round(__num(v, "width")) }); }); + def("height", function () { return this.__info().height; }, function (v) { this.__settings({ height: Math.round(__num(v, "height")) }); }); + def("pixelAspect", function () { return this.__info().pixelAspect; }, function (v) { this.__settings({ pixelAspect: __num(v) }); }); + def("duration", function () { return this.__info().duration; }, function (v) { this.__settings({ duration: __num(v, "duration") }); }); + def("frameRate", function () { return this.__info().frameRate; }, function (v) { this.__settings({ frameRate: __num(v, "frameRate") }); }); + def("frameDuration", function () { var f = this.__info().frameRate; return f ? 1 / f : 0; }, function (v) { this.__settings({ frameRate: 1 / __num(v) }); }); + def("hasVideo", function () { return this.__info().hasVideo; }); + def("hasAudio", function () { return this.__info().hasAudio; }); + def("footageMissing", function () { return !!this.__info().footageMissing; }); + def("useProxy", function () { return false; }); + def("proxySource", function () { return null; }); + def("isMediaReplacementCompatible", function () { return false; }); + def("time", function () { return 0; }); +})(); +AVItem.prototype.__settings = function () { throw __err("can't change this item's settings from a script"); }; + +function CompItem(id) { + this.__id = id; + this.__kind = "CompItem"; +} +CompItem.prototype = Object.create(AVItem.prototype); +CompItem.prototype.constructor = CompItem; +(function () { + var P = CompItem.prototype; + function def(name, get, set) { Object.defineProperty(P, name, { get: get, set: set }); } + P.__settings = function (o) { o.comp = this.__id; __call("comp.settings", o); }; + def("typeName", function () { return "Composition"; }); + def("layers", function () { return new LayerCollection(this.__id); }); + def("numLayers", function () { return this.__info().layers.length; }); + def("selectedLayers", function () { var c = this.__id; return this.__info().selectedLayers.map(function (l) { return __layer(c, l); }); }); + def("selectedProperties", function () { + var c = this.__id, out = []; + this.__info().selectedProperties.forEach(function (lp) { + var L = __layer(c, lp[0]); + out.push(__propOf(L, __get("node", { comp: c, layer: lp[0], uid: lp[1] }))); + }); + return out; + }); + def("activeCamera", function () { + var ids = this.__info().layers; + for (var i = 0; i < ids.length; i++) { + var l = __get("layer", { comp: this.__id, layer: ids[i] }); + if (l.kind === "camera" && l.enabled && l.active) return __layer(this.__id, ids[i]); + } + return null; + }); + def("time", function () { return this.__info().time; }, function (v) { __call("time.set", { comp: this.__id, time: __num(v, "time") }); }); + def("workAreaStart", function () { return this.__info().workAreaStart; }, function (v) { + var i = this.__info(); + __call("comp.workArea", { comp: this.__id, start: __num(v), end: __num(v) + i.workAreaDuration }); + }); + def("workAreaDuration", function () { return this.__info().workAreaDuration; }, function (v) { + var i = this.__info(); + __call("comp.workArea", { comp: this.__id, start: i.workAreaStart, end: i.workAreaStart + __num(v) }); + }); + def("bgColor", function () { return this.__info().bgColor; }, function (v) { this.__settings({ background: [v[0], v[1], v[2]] }); }); + def("displayStartTime", function () { return this.__info().displayStartTime; }, function (v) { this.__settings({ startTime: __num(v) }); }); + def("hideShyLayers", function () { return this.__info().hideShyLayers; }, function (v) { __call("comp.setSwitch", { comp: this.__id, "switch": "hideShy", value: !!v }); }); + def("motionBlur", function () { return this.__info().motionBlur; }, function (v) { __call("comp.setSwitch", { comp: this.__id, "switch": "motionBlur", value: !!v }); }); + def("frameBlending", function () { return this.__info().frameBlending; }, function (v) { __call("comp.setSwitch", { comp: this.__id, "switch": "frameBlending", value: !!v }); }); + def("draft3d", function () { return this.__info().draft3d; }, function (v) { __call("comp.setSwitch", { comp: this.__id, "switch": "draft3d", value: !!v }); }); + def("shutterAngle", function () { return this.__info().shutterAngle; }, function (v) { this.__settings({ shutterAngle: __num(v) }); }); + def("shutterPhase", function () { return this.__info().shutterPhase; }, function (v) { this.__settings({ shutterPhase: __num(v) }); }); + def("motionBlurSamplesPerFrame", function () { return this.__info().motionBlurSamplesPerFrame; }, function (v) { this.__settings({ motionBlurSamples: __num(v) }); }); + def("renderer", function () { return this.__info().renderer; }, function (v) { + this.__settings({ renderer: String(v).toLowerCase().indexOf("advanced") >= 0 ? "advanced3D" : "classic3D" }); + }); + def("renderers", function () { return ["ADBE Classic 3D", "ADBE Advanced 3d"]; }); + def("markerProperty", function () { return new MarkerProperty(this.__id, null); }); + def("hasVideo", function () { return true; }); + P.layer = function (a, b) { + var ids = this.__info().layers; + if (typeof a === "number") { + if (b !== undefined) { + // layer(otherLayer, relIndex) + throw __err("layer(index, relIndex) is not supported"); + } + if (a < 1 || a > ids.length || Math.floor(a) !== a) throw __err("layer index " + a + " is out of range 1.." + ids.length); + return __layer(this.__id, ids[a - 1]); + } + if (a && a.__id !== undefined && typeof b === "number") { + var j = a.index + b; + return j >= 1 && j <= ids.length ? __layer(this.__id, ids[j - 1]) : null; + } + return new LayerCollection(this.__id).byName(String(a)); + }; + P.openInViewer = function () { __call("comp.open", { comp: this.__id }); return null; }; + P.duplicate = function () { return __item(__call("project.duplicate", { items: [this.__id] }).items[0]); }; + P.exportAsMotionGraphicsTemplate = function () { return false; }; +})(); + +function FootageItem(id) { + this.__id = id; + this.__kind = "FootageItem"; +} +FootageItem.prototype = Object.create(AVItem.prototype); +FootageItem.prototype.constructor = FootageItem; +Object.defineProperty(FootageItem.prototype, "file", { + get: function () { var f = this.__info().file; return f ? new File(f) : null; }, +}); +Object.defineProperty(FootageItem.prototype, "mainSource", { + get: function () { + var i = this.__info(); + if (i.kind === "solid") return { isStill: true, color: i.color, hasAlpha: false, toString: function () { return "[object SolidSource]"; } }; + return { isStill: !!i.isStill, file: i.file ? new File(i.file) : null, missingFootagePath: i.footageMissing ? i.file : "", loop: i.loop, hasAlpha: false, toString: function () { return "[object FileSource]"; } }; + }, +}); +FootageItem.prototype.replace = function (f) { __call("file.replaceFootage", { item: this.__id, path: __str(f) }); }; +FootageItem.prototype.replaceWithSolid = function (color, name, w, h) { + __call("file.replaceWithSolid", { item: this.__id, color: [color[0], color[1], color[2]], width: w, height: h }); + if (name !== undefined) this.name = name; +}; +FootageItem.prototype.openInViewer = function () { return null; }; + +function FolderItem(id) { + this.__id = id; + this.__kind = "FolderItem"; +} +FolderItem.prototype = Object.create(Item.prototype); +FolderItem.prototype.constructor = FolderItem; +(function () { + var P = FolderItem.prototype; + P.__info = function () { + if (this.__id === 0) return { id: 0, name: "Root", typeName: "Folder", comment: "", label: 0, parent: null, selected: false, usedIn: [], children: [] }; + return __get("item", { id: this.__id }); + }; + Object.defineProperty(P, "items", { get: function () { return new ItemCollection(this.__id); } }); + Object.defineProperty(P, "numItems", { get: function () { return new ItemCollection(this.__id).length; } }); + Object.defineProperty(P, "parentFolder", { + get: function () { return this.__id === 0 ? null : __item(this.__info().parent); }, + set: function (f) { __call("project.move", { items: [this.__id], folder: f && f.__id ? f.__id : null }); }, + }); + P.item = function (i) { + var v = new ItemCollection(this.__id).__at(__num(i, "index")); + if (v === undefined) throw __err("item index " + i + " is out of range"); + return v; + }; +})(); + +// ------------------------------------------------------------------ render queue + +function OutputModule(rq, n) { + this.__rq = rq; + this.__n = n; +} +OutputModule.prototype = { + constructor: OutputModule, + __info: function () { return this.__rq.__info(); }, + get name() { var i = this.__info(); return this.__n === 1 ? i.outputModuleSummary : i.extraOutputs[this.__n - 2].summary; }, + get file() { + var i = this.__info(); + var p = this.__n === 1 ? i.outputPath : i.extraOutputs[this.__n - 2].outputPath; + return p ? new File(p) : null; + }, + set file(f) { __call("renderQueue.setOutput", { item: this.__rq.__id, module: this.__n, path: __str(f) }); }, + get templates() { return ["H.264", "ProRes", "PNG Sequence", "JPEG Sequence", "TIFF Sequence", "OpenEXR Sequence", "GIF"]; }, + applyTemplate: function (name) { + var n = String(name).toLowerCase(); + var f = n.indexOf("prores") >= 0 ? "prores" : n.indexOf("png") >= 0 || n.indexOf("lossless") >= 0 ? "png" : n.indexOf("jpeg") >= 0 || n.indexOf("jpg") >= 0 ? "jpeg" + : n.indexOf("tiff") >= 0 ? "tiff" : n.indexOf("exr") >= 0 ? "exr" : n.indexOf("gif") >= 0 ? "gif" : n.indexOf("264") >= 0 ? "h264" : null; + if (!f) throw __err("Unknown output module template \"" + name + "\""); + __call("renderQueue.setOutputModule", { item: this.__rq.__id, module: this.__n, format: f }); + }, + remove: function () { throw __err("removing output modules is not supported"); }, + toString: function () { return "[object OutputModule]"; }, +}; + +var __rqStatus = { "Unqueued": 3014, "Queued": 3015, "Needs Output": 3013, "Rendering": 3016, "Done": 3019, "User Stopped": 3017, "Failed": 3018 }; + +function RenderQueueItem(id) { this.__id = id; } +RenderQueueItem.prototype = { + constructor: RenderQueueItem, + __info: function () { + var l = __call("renderQueue.list", {}).items; + for (var i = 0; i < l.length; i++) if (l[i].id === this.__id) return l[i]; + throw __err("the render queue item no longer exists"); + }, + get comp() { return new CompItem(this.__info().comp); }, + get status() { + var s = this.__info().statusLabel; + for (var k in __rqStatus) if (s.indexOf(k) === 0) return __rqStatus[k]; + return RQItemStatus.QUEUED; + }, + get render() { return this.__info().render; }, + set render(v) { __call("renderQueue.setRender", { item: this.__id, render: !!v }); }, + get numOutputModules() { return this.__info().outputModules; }, + get outputModules() { + var n = this.numOutputModules, out = []; + for (var i = 1; i <= n; i++) out.push(new OutputModule(this, i)); + return out; + }, + outputModule: function (i) { + if (i < 1 || i > this.numOutputModules) throw __err("output module index " + i + " is out of range"); + return new OutputModule(this, i); + }, + get timeSpanStart() { var s = this.__info().settings; return s.start !== undefined ? s.start : 0; }, + set timeSpanStart(v) { __call("renderQueue.setRenderSettings", { item: this.__id, timeSpan: "custom", start: __num(v), end: __num(v) + this.timeSpanDuration }); }, + get timeSpanDuration() { var s = this.__info().settings; return s.end !== undefined && s.start !== undefined ? s.end - s.start : this.comp.duration; }, + set timeSpanDuration(v) { var s = this.timeSpanStart; __call("renderQueue.setRenderSettings", { item: this.__id, timeSpan: "custom", start: s, end: s + __num(v) }); }, + get elapsedSeconds() { return this.__info().render_time || 0; }, + get startTime() { var s = this.__info().started; return s ? new Date(s * 1000) : null; }, + get logType() { return LogType.ERRORS_ONLY; }, + set logType(v) {}, + get skipFrames() { return 0; }, + outputModuleAdd: function () { __call("render.addOutputModule", { item: this.__id }); }, + remove: function () { __call("renderQueue.remove", { item: this.__id }); }, + duplicate: function () { + var before = __call("renderQueue.list", {}).items.map(function (i) { return i.id; }); + __call("renderQueue.duplicate", { item: this.__id }); + var after = __call("renderQueue.list", {}).items; + for (var i = 0; i < after.length; i++) if (before.indexOf(after[i].id) < 0) return new RenderQueueItem(after[i].id); + return null; + }, + applyTemplate: function () {}, + getSettings: function () { return this.__info().settings; }, + setSettings: function (o) { __call("renderQueue.setRenderSettings", Object.assign({ item: this.__id }, o)); }, + toString: function () { return "[object RenderQueueItem]"; }, +}; + +function RQItemCollection() { return new Proxy(this, __coll); } +RQItemCollection.prototype = { + constructor: RQItemCollection, + __list: function () { return __call("renderQueue.list", {}).items; }, + get length() { return this.__list().length; }, + __at: function (i) { var l = this.__list(); return i >= 1 && i <= l.length ? new RenderQueueItem(l[i - 1].id) : undefined; }, + add: function (comp) { + if (!(comp instanceof CompItem)) throw __err("add(): expected a CompItem"); + __call("renderQueue.add", { comp: comp.__id }); + var l = this.__list(); + return new RenderQueueItem(l[l.length - 1].id); + }, + toString: function () { return "[object RQItemCollection]"; }, +}; + +function RenderQueue() {} +RenderQueue.prototype = { + constructor: RenderQueue, + get items() { return new RQItemCollection(); }, + get numItems() { return new RQItemCollection().length; }, + get rendering() { return !!__call("renderQueue.list", {}).rendering; }, + get canQueueInAME() { return false; }, + get queueNotify() { return false; }, + set queueNotify(v) {}, + item: function (i) { + var v = new RQItemCollection().__at(__num(i, "index")); + if (v === undefined) throw __err("render queue index " + i + " is out of range"); + return v; + }, + render: function () { __call("renderQueue.render", { wait: true }); }, + stopRendering: function () { __call("renderQueue.stop", {}); }, + pauseRendering: function () {}, + showWindow: function () {}, + queueInAME: function () { throw __err("Adobe Media Encoder is not available: render with app.project.renderQueue.render()"); }, + toString: function () { return "[object RenderQueue]"; }, +}; + +// ------------------------------------------------------------------ project and app + +function ImportOptions(file) { + if (!(this instanceof ImportOptions)) return new ImportOptions(file); + this.file = file || null; + this.importAs = ImportAsType.FOOTAGE; + this.sequence = false; + this.forceAlphabetical = false; + this.rangeStart = 0; + this.rangeEnd = 0; +} +ImportOptions.prototype.canImportAs = function (t) { return t === ImportAsType.FOOTAGE; }; + +function Project() {} +Project.prototype = { + constructor: Project, + __info: function () { return __get("project"); }, + get items() { return new ItemCollection(0); }, + get numItems() { return this.__info().items.length; }, + get rootFolder() { return new FolderItem(0); }, + get activeItem() { return __item(this.__info().active); }, + get selection() { return this.__info().selection.map(__item); }, + get file() { var p = this.__info().path; return p ? new File(p) : null; }, + get dirty() { return this.__info().dirty; }, + get renderQueue() { return new RenderQueue(); }, + get bitsPerChannel() { return this.__info().bitsPerChannel; }, + set bitsPerChannel(v) { __call("file.projectSettings", { bitDepth: __num(v) }); }, + get linearBlending() { return this.__info().linearBlending; }, + set linearBlending(v) { __call("file.projectSettings", { blendLinear: !!v }); }, + get linearizeWorkingSpace() { return this.__info().linearizeWorkingSpace; }, + set linearizeWorkingSpace(v) { __call("file.projectSettings", { linearize: !!v }); }, + get workingSpace() { return this.__info().workingSpace; }, + set workingSpace(v) { __call("file.projectSettings", { workingSpace: String(v) || "none" }); }, + get timeDisplayType() { return this.__info().timeDisplayFrames ? TimeDisplayType.FRAMES : TimeDisplayType.TIMECODE; }, + set timeDisplayType(v) { __call("file.projectSettings", { timeDisplay: v === TimeDisplayType.FRAMES ? "frames" : "timecode" }); }, + get expressionEngine() { return "javascript-1.0"; }, + set expressionEngine(v) {}, + item: function (i) { + var v = new ItemCollection(0).__at(__num(i, "index")); + if (v === undefined) throw __err("item index " + i + " is out of range 1.." + this.numItems); + return v; + }, + itemByID: function (id) { + try { return __item(__num(id)); } catch (e) { return null; } + }, + layerByID: function () { return null; }, + save: function (file) { + if (file) __call("file.saveAs", { path: __str(file) }); + else __call("file.save", {}); + return true; + }, + saveWithDialog: function () { if (this.file) return this.save(); return false; }, + close: function (opt) { + if (opt === CloseOptions.SAVE_CHANGES && this.file) this.save(); + __call("file.newProject", {}); + return true; + }, + importFile: function (opts) { + var f = opts instanceof ImportOptions ? opts.file : opts; + if (!f) throw __err("importFile(): no file"); + var r = __call("file.import", { paths: [__str(f)] }); + if (r.errors && r.errors.length) throw __err(r.errors.join("; ")); + return __item(r.items[0]); + }, + importFileWithDialog: function () { return null; }, + importPlaceholder: function (name, w, h, rate, dur) { + var r = __call("file.importPlaceholder", { name: String(name), width: w, height: h, frameRate: rate, duration: dur }); + var id = r && typeof r === "object" ? (r.item || r.id) : r; + return __item(id); + }, + consolidateFootage: function () { return __call("file.consolidateFootage", {}); }, + removeUnusedFootage: function () { return __call("file.removeUnusedFootage", {}); }, + reduceProject: function (items) { + __call("project.select", { items: items.map(function (i) { return i.__id; }) }); + return __call("file.reduceProject", {}); + }, + autoFixExpressions: function () {}, + showWindow: function () {}, + toString: function () { return "[object Project]"; }, +}; + +var __tasks = {}; +var app = { + get project() { return new Project(); }, + get version() { return __get("app").version; }, + get buildName() { return __get("app").buildName; }, + get buildNumber() { return 0; }, + get isWatchFolder() { return false; }, + get isRenderEngine() { return false; }, + get isoLanguage() { return "en_US"; }, + get language() { return 0; }, + get memoryInUse() { return 0; }, + get effects() { return __get("effects").map(function (e) { return { displayName: e.name, matchName: e.matchName, category: e.category, version: "1.0" }; }); }, + get activeViewer() { return null; }, + get availableGPUAccelTypes() { return []; }, + exitAfterLaunchAndEval: false, + exitCode: 0, + saveProjectOnCrash: true, + onError: null, + settings: { + __s: {}, + haveSetting: function (sec, key) { return (sec + "/" + key) in this.__s; }, + getSetting: function (sec, key) { return this.__s[sec + "/" + key] || ""; }, + saveSetting: function (sec, key, v) { this.__s[sec + "/" + key] = String(v); }, + }, + preferences: { + havePref: function () { return false; }, + getPrefAsString: function () { return ""; }, + getPrefAsLong: function () { return 0; }, + getPrefAsBool: function () { return false; }, + getPrefAsFloat: function () { return 0; }, + savePrefAsString: function () {}, + savePrefAsLong: function () {}, + savePrefAsBool: function () {}, + savePrefAsFloat: function () {}, + reload: function () {}, + }, + newProject: function () { __call("file.newProject", {}); return new Project(); }, + open: function (file) { + if (!file) return null; + __call("file.open", { path: __str(file) }); + return new Project(); + }, + quit: function () {}, + beginUndoGroup: function (name) { __undo("begin", name === undefined ? "Script" : String(name)); }, + endUndoGroup: function () { __undo("end", ""); }, + beginSuppressDialogs: function () {}, + endSuppressDialogs: function () {}, + purge: function () { __call("edit.purge", { what: "all" }); }, + findMenuCommandId: function (name) { return __get("menuCommandId", { name: String(name) }); }, + executeCommand: function (id) { + var cmd = typeof id === "number" ? __get("commandAt", { index: id }) : String(id); + if (!cmd) throw __err("Unknown command id " + id); + return __call(cmd, {}); + }, + scheduleTask: function (code, delay, repeat) { return __task("schedule", String(code), !!repeat); }, + cancelTask: function (id) { __task("cancel", id); }, + setMemoryUsageLimits: function () {}, + setMultiFrameRenderingConfig: function () {}, + setSavePreferencesOnQuit: function () {}, + activate: function () {}, + watchFolder: function () { throw __err("watch folders are not supported"); }, + pauseWatchFolder: function () {}, + endWatchFolder: function () {}, + parseSwatchFile: function () { throw __err("parseSwatchFile is not supported"); }, + // EffectCraft: run any engine command by id with parameters (see `command.list`). + run: function (id, params) { return __call(String(id), params || {}); }, + toString: function () { return "[object Application]"; }, +}; + +// The completion value of a script, as JSON text for the host. +function __result(v) { + if (v === undefined) return undefined; + if (v === null || typeof v === "number" || typeof v === "boolean" || typeof v === "string") return JSON.stringify(v); + if (v instanceof Layer || v instanceof Item || v instanceof PropertyBase || v instanceof Project || v instanceof TextDocument) { + var d = { type: v.toString() }; + try { d.name = v.name; } catch (e) {} + if (v instanceof TextDocument) d.text = v.text; + return JSON.stringify(d); + } + try { + var s = JSON.stringify(v); + return s === undefined ? JSON.stringify(String(v)) : s; + } catch (e) { + return JSON.stringify(String(v)); + } +} diff --git a/crates/script/src/runtime.rs b/crates/script/src/runtime.rs new file mode 100644 index 00000000..61b49595 --- /dev/null +++ b/crates/script/src/runtime.rs @@ -0,0 +1,546 @@ +//! The boa runtime: native functions (`__exec`, `__query`, `__print`, `__undo`, `__task`, +//! `__file`), the session hand-off while a script runs, undo groups and error reporting. + +use std::cell::RefCell; +use std::mem::ManuallyDrop; +use std::sync::Arc; + +use boa_engine::{Context, JsNativeError, JsResult, JsString, JsValue, NativeFunction, Source, js_string}; +use effectcraft_engine::project::Project; +use effectcraft_engine::{ScriptRequest, Session}; +use serde_json::{Value as J, json}; + +const PRELUDE: &str = include_str!("prelude.js"); + +/// Engine commands that create layers: like After Effects, scripts add new layers at the top of +/// the stack (the UI inserts above the selection). +const CREATES_LAYER: &[&str] = + &["layer.newSolid", "layer.newNull", "layer.newText", "layer.newShape", "layer.newAdjustment", "layer.newCamera", "layer.newLight", "layer.addItem"]; + +/// What a script run produced. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Outcome { + /// The value of the script's last expression statement (JSON when it converts; model objects + /// as strings like `"[object CompItem]"`). + pub result: J, + /// `writeLn` / `$.writeln` / `alert` output, one entry per line. + pub output: Vec, + pub error: Option, +} + +/// An uncaught script error. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct ScriptError { + pub message: String, + pub line: Option, + pub column: Option, + pub file: String, +} + +impl Outcome { + pub fn ok(&self) -> bool { + self.error.is_none() + } + pub fn to_json(&self) -> J { + json!({ + "ok": self.ok(), + "result": self.result, + "output": self.output.join("\n"), + "error": self.error.as_ref().map(|e| json!({"message": e.message, "line": e.line, "column": e.column, "file": e.file})), + }) + } +} + +/// The session and per-run state while a script runs (the natives reach it here; nothing +/// borrowed ever enters the JS heap). +struct Active { + session: Session, + output: Vec, + /// Open undo groups: (name, project snapshot when the outermost group began). + groups: Vec<(String, Arc)>, + /// `app.scheduleTask` queue: (id, code, repeat). + tasks: Vec<(u32, String, bool)>, + next_task: u32, + name: String, +} + +thread_local! { + static ACTIVE: RefCell> = const { RefCell::new(None) }; + /// The Script Console's persistent context (never dropped, like the expression runtime: boa's + /// own thread-local heap may be torn down first at thread exit). + static CONSOLE: RefCell>> = const { RefCell::new(None) }; +} + +fn with_active(f: impl FnOnce(&mut Active) -> Result) -> Result { + ACTIVE.with(|a| match a.try_borrow_mut() { + Ok(mut g) => match g.as_mut() { + Some(act) => f(act), + None => Err("no script is running".into()), + }, + Err(_) => Err("scripts can't run scripts".into()), + }) +} + +fn throw(msg: impl Into) -> boa_engine::JsError { + JsNativeError::error().with_message(msg.into()).into() +} + +fn arg_string(args: &[JsValue], i: usize, ctx: &mut Context) -> JsResult { + Ok(args.get(i).cloned().unwrap_or_default().to_string(ctx)?.to_std_string_escaped()) +} + +fn arg_json(args: &[JsValue], i: usize, ctx: &mut Context) -> JsResult { + let s = arg_string(args, i, ctx)?; + if s.is_empty() || s == "undefined" { + return Ok(json!({})); + } + serde_json::from_str(&s).map_err(|e| throw(format!("bad parameters: {e}"))) +} + +fn js_str(s: &str) -> JsValue { + JsValue::from(JsString::from(s)) +} + +/// `__exec(commandId, paramsJson)` → result JSON. Runs an engine command (undoable) with the +/// target comp made active for its duration. +fn native_exec(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + let id = arg_string(args, 0, ctx)?; + let params = arg_json(args, 1, ctx)?; + if id == "script.run" || id == "file.runScript" { + return Err(throw("scripts can't run scripts")); + } + let r = with_active(|a| Ok(exec(&mut a.session, &id, params))); + match r { + Ok(Ok(v)) => Ok(js_str(&v.to_string())), + Ok(Err(e)) | Err(e) => Err(throw(e)), + } +} + +/// Run a command for a script: the `comp` it targets is active while it runs (commands act on +/// the active comp), then the user's active comp comes back. +pub(crate) fn exec(s: &mut Session, id: &str, params: J) -> Result { + use effectcraft_engine::project::ItemId; + let target = params.get("comp").and_then(J::as_u64).map(ItemId).filter(|c| s.project.comp(*c).is_some()); + let prev = s.state.active_comp; + if let Some(c) = target + && prev != Some(c) + { + s.state.active_comp = Some(c); + } + if CREATES_LAYER.contains(&id) { + s.state.selected_layers.clear(); + } + let r = s.execute(id, params); + if let Some(c) = target + && prev != Some(c) + && s.state.active_comp == Some(c) + && id != "comp.open" + { + match prev { + Some(p) if s.project.comp(p).is_some() => s.state.active_comp = Some(p), + _ => { + if !s.state.open_comps.contains(&c) { + s.state.open_comps.push(c); + } + } + } + } + r.map_err(|e| e.to_string()) +} + +/// `__query(kind, json)` → JSON. +fn native_query(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + let kind = arg_string(args, 0, ctx)?; + let a = arg_json(args, 1, ctx)?; + let r = with_active(|act| match kind.as_str() { + "app" => Ok(json!({ + "version": format!("26.0 (EffectCraft {})", env!("CARGO_PKG_VERSION")), + "buildName": env!("CARGO_PKG_VERSION"), + "scriptName": act.name, + "allowFiles": act.session.prefs.scripting.allow_scripts_write_files, + "projectDir": project_dir(&act.session), + })), + _ => crate::model::query(&mut act.session, &kind, &a), + }); + match r { + Ok(v) => Ok(js_str(&v.to_string())), + Err(e) => Err(throw(e)), + } +} + +/// `__print(text)`: `writeLn`, `$.writeln`, `alert`. +fn native_print(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + let text = arg_string(args, 0, ctx)?; + let _ = with_active(|a| { + a.output.extend(text.split('\n').map(str::to_string)); + Ok(()) + }); + Ok(JsValue::undefined()) +} + +/// `__undo("begin" | "end", name)`: After Effects' `app.beginUndoGroup` / `endUndoGroup`. Every +/// command run inside the outermost group folds into one undo step named after the group. +fn native_undo(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + let op = arg_string(args, 0, ctx)?; + let name = arg_string(args, 1, ctx)?; + with_active(|a| { + match op.as_str() { + "begin" => { + let snap = a.session.project.clone(); + a.groups.push((if name.is_empty() || name == "undefined" { "Script".into() } else { name }, snap)); + } + _ => { + if a.groups.len() == 1 { + let (name, snap) = a.groups.pop().unwrap_or_default(); + collapse(&mut a.session, &name, &snap); + } else { + a.groups.pop(); + } + } + } + Ok(()) + }) + .map_err(throw)?; + Ok(JsValue::undefined()) +} + +/// Fold the undo steps recorded since the project was `snap` into one step called `name`. +fn collapse(s: &mut Session, name: &str, snap: &Arc) { + let h = &mut s.history; + let Some(start) = h.undo.iter().rposition(|(_, p)| Arc::ptr_eq(p, snap)) else { + // Nothing was recorded (or history was reset by New / Open Project). + return; + }; + let first = h.undo[start].1.clone(); + h.undo.truncate(start); + h.undo.push((name.to_string(), first)); + h.merge_key = None; +} + +/// `__task("schedule", code, repeat)` → id; `__task("cancel", id)`. +fn native_task(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + let op = arg_string(args, 0, ctx)?; + if op == "cancel" { + let id = args.get(1).cloned().unwrap_or_default().to_number(ctx)? as u32; + let _ = with_active(|a| { + a.tasks.retain(|t| t.0 != id); + Ok(()) + }); + return Ok(JsValue::undefined()); + } + let code = arg_string(args, 1, ctx)?; + let repeat = args.get(2).cloned().unwrap_or_default().to_boolean(); + let id = with_active(|a| { + a.next_task += 1; + let id = a.next_task; + a.tasks.push((id, code, repeat)); + Ok(id) + }) + .map_err(throw)?; + Ok(JsValue::from(id)) +} + +fn project_dir(s: &Session) -> Option { + let p = s.path.as_deref()?; + std::path::Path::new(p).parent().map(|d| d.to_string_lossy().to_string()) +} + +/// Lexically normalized absolute path (`a/./b/../c` → `a/c`). +fn normalize(p: &str) -> std::path::PathBuf { + use std::path::{Component, Path, PathBuf}; + let path = Path::new(p); + let abs = if path.is_absolute() { path.to_path_buf() } else { std::env::current_dir().unwrap_or_default().join(path) }; + let mut out = PathBuf::new(); + for c in abs.components() { + match c { + Component::ParentDir => { + out.pop(); + } + Component::CurDir => {} + c => out.push(c), + } + } + // Resolve symlinks where the file (or its folder) exists, so links can't escape the project. + std::fs::canonicalize(&out) + .or_else(|_| out.parent().map(std::fs::canonicalize).unwrap_or(Ok(out.clone())).map(|d| d.join(out.file_name().unwrap_or_default()))) + .unwrap_or(out) +} + +const GATE: &str = "Preferences ▸ Scripting & Expressions ▸ Allow Scripts to Write Files and Access Network"; + +/// The file-access gate: reads under the project's folder are always allowed; other reads, all +/// writes and listing other folders need the Allow Scripts to Write Files preference. +fn check_access(s: &Session, path: &str, write: bool) -> Result { + let p = normalize(path); + if s.prefs.scripting.allow_scripts_write_files { + return Ok(p); + } + if write { + return Err(format!("Unable to write {path}: scripts can't write files unless {GATE} is on")); + } + if let Some(dir) = project_dir(s) { + let d = normalize(&dir); + if p.starts_with(&d) { + return Ok(p); + } + } + Err(format!("Unable to access {path}: scripts can only read files in the project's folder unless {GATE} is on")) +} + +/// `__file(op, path, data)`: `read` → text, `write`/`append` → bytes, `exists` → bool, `list` → +/// names, `mkdir`, `remove`, `isDir`. +fn native_file(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + let op = arg_string(args, 0, ctx)?; + let path = arg_string(args, 1, ctx)?; + let data = if args.len() > 2 { arg_string(args, 2, ctx)? } else { String::new() }; + let r: Result = with_active(|a| { + let s = &a.session; + match op.as_str() { + "access" => Ok(json!(check_access(s, &path, false)?.exists())), + "exists" | "isDir" => { + // Unreadable locations report "missing" instead of failing. + let Ok(p) = check_access(s, &path, false) else { return Ok(json!(false)) }; + Ok(json!(if op == "exists" { p.exists() } else { p.is_dir() })) + } + "read" => { + let p = check_access(s, &path, false)?; + let bytes = s.services.read_file(&p.to_string_lossy()).map_err(|e| format!("Unable to read {path}: {e}"))?; + Ok(json!(String::from_utf8_lossy(&bytes))) + } + "write" | "append" => { + let p = check_access(s, &path, true)?; + let ps = p.to_string_lossy().to_string(); + let mut bytes = if op == "append" { s.services.read_file(&ps).unwrap_or_default() } else { vec![] }; + bytes.extend_from_slice(data.as_bytes()); + s.services.write_file(&ps, &bytes).map_err(|e| format!("Unable to write {path}: {e}"))?; + Ok(json!(bytes.len())) + } + "list" => { + let p = check_access(s, &path, false)?; + let mut names: Vec = std::fs::read_dir(&p) + .map_err(|e| format!("Unable to list {path}: {e}"))? + .filter_map(|e| e.ok()) + .map(|e| e.path().to_string_lossy().to_string()) + .collect(); + names.sort(); + Ok(json!(names)) + } + "mkdir" => { + let p = check_access(s, &path, true)?; + std::fs::create_dir_all(&p).map_err(|e| format!("Unable to create {path}: {e}"))?; + Ok(json!(true)) + } + "remove" => { + let p = check_access(s, &path, true)?; + let r = if p.is_dir() { std::fs::remove_dir(&p) } else { std::fs::remove_file(&p) }; + Ok(json!(r.is_ok())) + } + "network" => Err(if s.prefs.scripting.allow_scripts_write_files { + "network access from scripts is not supported in EffectCraft".to_string() + } else { + format!("scripts can't access the network unless {GATE} is on") + }), + _ => Err(format!("unknown file operation `{op}`")), + } + }); + match r { + Ok(v) => Ok(js_str(&v.to_string())), + Err(e) => Err(throw(e)), + } +} + +fn new_context() -> Result { + let mut ctx = Context::default(); + let limits = ctx.runtime_limits_mut(); + limits.set_loop_iteration_limit(200_000_000); + limits.set_recursion_limit(2000); + let natives: [(JsString, usize, fn(&JsValue, &[JsValue], &mut Context) -> JsResult); 6] = [ + (js_string!("__exec"), 2, native_exec), + (js_string!("__query"), 2, native_query), + (js_string!("__print"), 1, native_print), + (js_string!("__undo"), 2, native_undo), + (js_string!("__task"), 3, native_task), + (js_string!("__file"), 3, native_file), + ]; + for (name, len, f) in natives { + ctx.register_global_callable(name, len, NativeFunction::from_fn_ptr(f)).map_err(|e| e.to_string())?; + } + ctx.eval(Source::from_bytes(PRELUDE)).map_err(|e| format!("script prelude: {e}"))?; + Ok(ctx) +} + +/// Turn a boa error into a message with line and column (`code` is the user's script). +fn script_error(e: &boa_engine::JsError, ctx: &mut Context, file: &str, code: &str) -> ScriptError { + // Thrown values: `throw new Error("x")`, `throw "x"`, native errors. + let display = e.to_string(); + let message = match e.as_opaque() { + Some(v) => { + let o = v.as_object(); + let msg = o.as_ref().and_then(|o| o.get(js_string!("message"), ctx).ok()).filter(|m| !m.is_undefined()); + let name = o.as_ref().and_then(|o| o.get(js_string!("name"), ctx).ok()).filter(|m| !m.is_undefined()); + match (name, msg) { + (Some(n), Some(m)) => { + let n = n.to_string(ctx).map(|s| s.to_std_string_escaped()).unwrap_or_default(); + let m = m.to_string(ctx).map(|s| s.to_std_string_escaped()).unwrap_or_default(); + if n == "Error" { m } else { format!("{n}: {m}") } + } + _ => v.to_string(ctx).map(|s| s.to_std_string_escaped()).unwrap_or_else(|_| display.clone()), + } + } + None => { + let first = display.lines().next().unwrap_or("").trim(); + let first = first.split(" at line ").next().unwrap_or(first).trim(); + let first = match first.rfind(" (") { + Some(i) if first.ends_with(')') && first[i..].contains(':') => first[..i].trim(), + _ => first, + }; + first.strip_prefix("Error: ").unwrap_or(first).to_string() + } + }; + let (line, column) = position(&display, file, code); + ScriptError { message, line, column, file: file.to_string() } +} + +/// Line and column of an error: `at line N, col M` (syntax errors), else the innermost backtrace +/// frame in the user's script (`file:line:col`), else (names boa can't place) the first use of +/// the undefined name in `code`. +fn position(msg: &str, file: &str, code: &str) -> (Option, Option) { + if let Some(i) = msg.find("at line ") { + let rest = &msg[i + 8..]; + let line = rest.split(|c: char| !c.is_ascii_digit()).next().and_then(|n| n.parse().ok()); + let col = rest.find("col ").and_then(|j| rest[j + 4..].split(|c: char| !c.is_ascii_digit()).next()).and_then(|n| n.parse().ok()); + return (line, col); + } + // `(file:12:5)`: the digits after the file name. + let pick = |l: &str| -> Option<(u32, u32)> { + let i = l.find(&format!("({file}:"))? + file.len() + 2; + let mut it = l[i..].split([':', ')']); + Some((it.next()?.parse().ok()?, it.next()?.parse().ok()?)) + }; + let frames: Vec<&str> = msg.lines().skip(1).collect(); + if let Some((a, b)) = frames.iter().find_map(|l| pick(l)).or_else(|| msg.lines().next().and_then(pick)) { + return (Some(a), Some(b)); + } + // `ReferenceError: name is not defined` without a position. + let first = msg.lines().next().unwrap_or(""); + if let Some(name) = first.split("ReferenceError: ").nth(1).and_then(|r| r.split(" is not defined").next()) { + for (n, line) in code.lines().enumerate() { + let b = line.as_bytes(); + let mut from = 0; + while let Some(i) = line[from..].find(name).map(|i| i + from) { + let before = i.checked_sub(1).map(|j| b[j]); + let after = b.get(i + name.len()).copied(); + let ident = |c: u8| c.is_ascii_alphanumeric() || c == b'_' || c == b'$'; + if !before.is_some_and(|c| ident(c) || c == b'.') && !after.is_some_and(ident) { + return (Some(n as u32 + 1), Some(i as u32 + 1)); + } + from = i + name.len(); + } + } + } + (None, None) +} + +/// Evaluate `code` in `ctx` and convert the result. +fn eval_in(ctx: &mut Context, code: &str, file: &str) -> Result { + let src = Source::from_bytes(code.as_bytes()).with_path(std::path::Path::new(file)); + let script = boa_engine::Script::parse(src, None, ctx).map_err(|e| script_error(&e, ctx, file, code))?; + let v = script.evaluate(ctx).map_err(|e| script_error(&e, ctx, file, code))?; + Ok(result_json(&v, ctx)) +} + +/// The script's completion value as JSON (`__result` in the prelude does the conversion). +fn result_json(v: &JsValue, ctx: &mut Context) -> J { + if v.is_undefined() { + return J::Null; + } + let global = ctx.global_object(); + let Some(f) = global.get(js_string!("__result"), ctx).ok().and_then(|f| f.as_object()) else { return J::Null }; + match f.call(&JsValue::undefined(), std::slice::from_ref(v), ctx) { + Ok(s) if s.is_string() => { + let s = s.to_string(ctx).map(|s| s.to_std_string_escaped()).unwrap_or_default(); + serde_json::from_str(&s).unwrap_or(J::String(s)) + } + _ => J::Null, + } +} + +/// Restores the caller's session when a run ends (on every exit path). +struct Restore<'a> { + target: &'a mut Session, +} + +impl Drop for Restore<'_> { + fn drop(&mut self) { + if let Some(a) = ACTIVE.with(|c| c.borrow_mut().take()) { + *self.target = a.session; + } + } +} + +/// Run a script against `session`: the object model sees and edits that session through engine +/// commands. Script errors come back in [`Outcome::error`]. +pub fn run(session: &mut Session, req: &ScriptRequest) -> Outcome { + let running = ACTIVE.with(|a| a.try_borrow().map(|g| g.is_some()).unwrap_or(true)); + if running { + return Outcome { + error: Some(ScriptError { message: "scripts can't run scripts".into(), file: req.name.into(), ..Default::default() }), + ..Default::default() + }; + } + let s = std::mem::take(session); + ACTIVE.with(|a| *a.borrow_mut() = Some(Active { session: s, output: vec![], groups: vec![], tasks: vec![], next_task: 0, name: req.name.to_string() })); + let guard = Restore { target: session }; + let file = if req.name.is_empty() { "script" } else { req.name }; + let mut result = if req.console { + CONSOLE.with(|c| { + let mut c = c.borrow_mut(); + if c.is_none() { + match new_context() { + Ok(ctx) => *c = Some(ManuallyDrop::new(ctx)), + Err(e) => return Err(ScriptError { message: e, file: file.into(), ..Default::default() }), + } + } + let ctx = c.as_mut().map(|m| &mut **m).expect("console context"); + eval_in(ctx, req.code, file) + }) + } else { + match new_context() { + Ok(mut ctx) => { + let r = eval_in(&mut ctx, req.code, file); + // app.scheduleTask: run the queued one-shot tasks after the script, in order. + let tasks = with_active(|a| Ok(std::mem::take(&mut a.tasks))).unwrap_or_default(); + let mut r = r; + for (_, code, repeat) in tasks { + if repeat { + continue; + } + if let Err(e) = eval_in(&mut ctx, &code, "scheduled task") + && r.is_ok() + { + r = Err(e); + } + } + r + } + Err(e) => Err(ScriptError { message: e, file: file.into(), ..Default::default() }), + } + }; + // Close undo groups left open; drop selections that no longer exist. + let output = with_active(|a| { + if let Some((name, snap)) = a.groups.first().cloned() { + collapse(&mut a.session, &name, &snap); + a.groups.clear(); + } + a.session.sanitize_state(); + Ok(std::mem::take(&mut a.output)) + }) + .unwrap_or_default(); + drop(guard); + let mut out = Outcome { output, ..Default::default() }; + match result.as_mut() { + Ok(v) => out.result = std::mem::take(v), + Err(e) => out.error = Some(e.clone()), + } + out +} diff --git a/crates/script/src/tests.rs b/crates/script/src/tests.rs new file mode 100644 index 00000000..f4a4fcb6 --- /dev/null +++ b/crates/script/src/tests.rs @@ -0,0 +1,392 @@ +//! End-to-end scripts against a headless session: build with the object model, read back through +//! the engine. + +use std::sync::Arc; + +use effectcraft_engine::Session; +use effectcraft_engine::keyframe::Value as KV; +use effectcraft_engine::project::{Comp, ItemId, Layer}; +use serde_json::{Value, json}; + +use crate::{Outcome, run_code}; + +fn session() -> Session { + let mut s = Session { expr: Some(Arc::new(effectcraft_expr::Expressions)), expr_check: Some(effectcraft_expr::check_syntax), ..Default::default() }; + crate::install(&mut s); + s +} + +fn ok(s: &mut Session, code: &str) -> Outcome { + let o = run_code(s, code, "test.jsx"); + assert!(o.error.is_none(), "script failed: {:?}\noutput: {:?}", o.error, o.output); + o +} + +fn comp_named<'a>(s: &'a Session, name: &str) -> (ItemId, &'a Comp) { + let it = s.project.items.values().find(|i| i.name == name && i.as_comp().is_some()).unwrap_or_else(|| panic!("no comp {name}")); + (it.id, it.as_comp().unwrap()) +} + +fn layer<'a>(c: &'a Comp, name: &str) -> &'a Layer { + c.layers.iter().find(|l| l.name == name).unwrap_or_else(|| panic!("no layer {name}")) +} + +#[test] +fn builds_a_comp_end_to_end() { + let mut s = session(); + let o = ok( + &mut s, + r#" + app.beginUndoGroup("Build"); + var comp = app.project.items.addComp("Main", 640, 360, 1, 4, 25); + var bg = comp.layers.addSolid([0.2, 0.4, 0.6], "BG", 640, 360, 1); + var txt = comp.layers.addText("Hello"); + txt.name = "Title"; + var pos = txt.property("ADBE Transform Group").property("ADBE Position"); + pos.setValueAtTime(0, [100, 180]); + pos.setValueAtTime(2, [540, 180]); + txt.transform.opacity.expression = "50 + 25"; + var blur = bg.Effects.addProperty("ADBE Gaussian Blur 2"); + blur.property("Blurriness").setValue(12); + var nul = comp.layers.addNull(); + txt.setParentWithJump(nul); + bg.blendingMode = BlendingMode.MULTIPLY; + var rq = app.project.renderQueue.items.add(comp); + rq.outputModule(1).file = new File("/tmp/effectcraft-script-test/out.mp4"); + app.endUndoGroup(); + writeLn("layers: " + comp.numLayers); + [comp.numLayers, pos.numKeys, pos.keyValue(2)[0], txt.transform.opacity.value, comp.layer(1).name] + "#, + ); + assert_eq!(o.output, vec!["layers: 3"]); + assert_eq!(o.result, json!([3, 2, 540, 75, "Null 1"])); + let (cid, c) = comp_named(&s, "Main"); + assert_eq!((c.width, c.height), (640, 360)); + assert_eq!(c.duration.seconds(), 4.0); + // AE adds new layers at the top. + let names: Vec<&str> = c.layers.iter().map(|l| l.name.as_str()).collect(); + assert_eq!(names, ["Null 1", "Title", "BG"]); + let title = layer(c, "Title"); + let p = title.props.prop("transform/position").unwrap(); + assert_eq!(p.keys.len(), 2); + assert_eq!(p.keys[1].value, KV::Vec3([540.0, 180.0, 0.0])); + assert_eq!(title.props.prop("transform/opacity").unwrap().expr.as_ref().unwrap().text, "50 + 25"); + assert_eq!(title.parent, Some(layer(c, "Null 1").id)); + let bg = layer(c, "BG"); + assert_eq!(bg.blend_mode, effectcraft_engine::color::BlendMode::Multiply); + let fx = bg.props.group("effects/#1").unwrap(); + assert_eq!(fx.match_id, "ec.blur.gaussian"); + assert_eq!(bg.props.prop("effects/#1/blurriness").unwrap().value, KV::Scalar(12.0)); + assert_eq!(s.project.render_queue.len(), 1); + assert_eq!(s.project.render_queue[0].comp, cid); + // One undo step for the whole group. + assert_eq!(s.history.undo.len(), 1); + assert_eq!(s.history.undo[0].0, "Build"); + assert!(s.undo()); + assert!(s.project.items.is_empty()); +} + +#[test] +fn undo_steps_without_a_group() { + let mut s = session(); + ok(&mut s, r#"var c = app.project.items.addComp("A", 100, 100, 1, 1, 24); c.layers.addNull(); c.layers.addNull();"#); + assert_eq!(s.history.undo.len(), 3); + // An unbalanced group is closed when the script ends. + ok(&mut s, r#"app.beginUndoGroup("Two"); var c = app.project.item(1); c.layers.addNull(); c.layers.addNull();"#); + assert_eq!(s.history.undo.len(), 4); + assert_eq!(s.history.undo[3].0, "Two"); + // Nested groups fold into the outermost one. + ok( + &mut s, + r#"app.beginUndoGroup("Outer"); app.beginUndoGroup("Inner"); app.project.item(1).layers.addNull(); app.endUndoGroup(); app.project.item(1).layers.addNull(); app.endUndoGroup();"#, + ); + assert_eq!(s.history.undo.len(), 5); + assert_eq!(s.history.undo[4].0, "Outer"); + s.undo(); + assert_eq!(s.project.comp(ItemId(1)).unwrap().layers.len(), 4); +} + +#[test] +fn errors_report_line_and_column() { + let mut s = session(); + let o = run_code(&mut s, "var a = 1;\nvar b = 2;\nfoo.bar();\n", "broken.jsx"); + let e = o.error.expect("error"); + assert_eq!(e.line, Some(3), "{e:?}"); + assert!(e.message.contains("foo"), "{e:?}"); + assert_eq!(e.file, "broken.jsx"); + let o = run_code(&mut s, "var a = 1;\n\nvar = ;\n", "syntax.jsx"); + let e = o.error.expect("syntax error"); + assert_eq!(e.line, Some(3), "{e:?}"); + let o = run_code(&mut s, "writeLn('before');\nthrow new Error('custom failure');\n", "throw.jsx"); + let e = o.error.expect("thrown"); + assert_eq!(e.message, "custom failure"); + assert_eq!(e.line, Some(2), "{e:?}"); + assert_eq!(o.output, vec!["before"]); + // Errors from the object model point at the user's line. + let o = run_code(&mut s, "var c = app.project.items.addComp('C', 10, 10, 1, 1, 24);\n\nc.layer(5);\n", "range.jsx"); + let e = o.error.expect("range error"); + assert!(e.message.contains("out of range"), "{e:?}"); + assert_eq!(e.line, Some(3), "{e:?}"); +} + +#[test] +fn match_names_and_lookups() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("M", 200, 200, 1, 2, 30); + var l = c.layers.addSolid([1, 1, 1], "S", 200, 200, 1); + var tr = l.property("ADBE Transform Group"); + var fx = l.property("ADBE Effect Parade").addProperty("Gaussian Blur"); + var m = l.property("ADBE Mask Parade").addProperty("ADBE Mask Atom"); + var sh = c.layers.addShape(); + var grp = sh.property("ADBE Root Vectors Group").addProperty("ADBE Vector Group"); + var rect = grp.property("ADBE Vectors Group").addProperty("ADBE Vector Shape - Rect"); + var fill = grp.property("Contents").addProperty("ADBE Vector Graphic - Fill"); + fill.property("ADBE Vector Fill Color").setValue([1, 0, 0]); + rect.property("ADBE Vector Rect Size").setValue([50, 60]); + [ + l.matchName, tr.matchName, tr.property("ADBE Position").matchName, tr.property("Anchor Point").matchName, + tr.property(1).name, tr.anchorPoint.matchName, l.transform.position.propertyValueType == PropertyValueType.ThreeD_SPATIAL, + fx.matchName, fx.property(1).matchName, fx.property("ADBE Gaussian Blur 2-0001").name, m.matchName, + m.property("ADBE Mask Shape").name, sh.matchName, grp.matchName, rect.matchName, fill.matchName, + tr.propertyType == PropertyType.NAMED_GROUP, l.Effects.propertyType == PropertyType.INDEXED_GROUP, + tr.position.parentProperty.matchName, tr.position.propertyDepth, l.property("Nope") === null, + fx.isEffect, m.isMask, l.Effects.numProperties + ] + "#, + ); + assert_eq!( + o.result, + json!([ + "ADBE AV Layer", + "ADBE Transform Group", + "ADBE Position", + "ADBE Anchor Point", + "Anchor Point", + "ADBE Anchor Point", + true, + "ADBE Gaussian Blur 2", + "ADBE Gaussian Blur 2-0001", + "Blurriness", + "ADBE Mask Atom", + "Mask Path", + "ADBE Vector Layer", + "ADBE Vector Group", + "ADBE Vector Shape - Rect", + "ADBE Vector Graphic - Fill", + true, + true, + "ADBE Transform Group", + 2, + true, + true, + true, + 1 + ]) + ); + let (_, c) = comp_named(&s, "M"); + let sh = c.layers.iter().find(|l| l.props.sub("contents").is_some()).unwrap(); + assert_eq!(sh.props.prop("contents/#1/contents/rect/size").unwrap().value, KV::Vec2([50.0, 60.0])); + assert_eq!(sh.props.prop("contents/#1/contents/fill/color").unwrap().value, KV::Color([1.0, 0.0, 0.0, 1.0])); +} + +#[test] +fn keyframes_eases_and_interpolation() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("K", 100, 100, 1, 5, 25); + var l = c.layers.addNull(); + var r = l.transform.rotation; + r.setValuesAtTimes([0, 1, 2], [0, 90, 180]); + r.setInterpolationTypeAtKey(2, KeyframeInterpolationType.HOLD, KeyframeInterpolationType.LINEAR); + r.setTemporalEaseAtKey(3, [new KeyframeEase(0, 75)]); + var idx = r.addKey(1.5); + r.removeKey(1); + var out = [r.numKeys, idx, r.keyTime(1), r.keyInInterpolationType(1) == KeyframeInterpolationType.HOLD, + r.keyOutInterpolationType(1) == KeyframeInterpolationType.LINEAR, r.keyInTemporalEase(3)[0].influence, + r.valueAtTime(1, false), r.isTimeVarying, r.nearestKeyIndex(1.9)]; + l.startTime = 1; + out.push(r.keyTime(1)); + out + "#, + ); + let v = o.result.as_array().unwrap(); + assert_eq!(v[0], json!(3)); + assert_eq!(v[1], json!(3)); + assert_eq!(v[2], json!(1)); + assert_eq!(v[3], json!(true)); + assert_eq!(v[4], json!(true)); + assert!((v[5].as_f64().unwrap() - 75.0).abs() < 1e-6, "{v:?}"); + assert_eq!(v[6], json!(90)); + assert_eq!(v[7], json!(true)); + assert_eq!(v[8], json!(3)); + // Moving the layer moves its keys (layer time): key 1 now sits at 2 s comp time. + assert_eq!(v[9], json!(2)); +} + +#[test] +fn text_documents() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("T", 400, 200, 1, 2, 30); + var t = c.layers.addText("abc"); + var st = t.property("ADBE Text Properties").property("ADBE Text Document"); + var doc = st.value; + doc.text = "Changed"; + doc.fontSize = 40; + doc.fillColor = [1, 0, 0]; + doc.justification = ParagraphJustification.CENTER_JUSTIFY; + doc.applyStroke = true; + doc.strokeWidth = 3; + st.setValue(doc); + var d2 = t.text.sourceText.value; + [d2.text, d2.fontSize, d2.fillColor, d2.justification == ParagraphJustification.CENTER_JUSTIFY, d2.strokeWidth, t instanceof TextLayer, t.name] + "#, + ); + assert_eq!(o.result, json!(["Changed", 40, [1, 0, 0], true, 3, true, "abc"])); + let (_, c) = comp_named(&s, "T"); + let KV::Text(d) = &c.layers[0].props.prop("text/sourceText").unwrap().value else { panic!() }; + assert_eq!(d.text, "Changed"); + assert_eq!(d.style_at(0).size, 40.0); +} + +#[test] +fn layers_items_and_markers() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var f = app.project.items.addFolder("Comps"); + var c = f.items.addComp("Inner", 100, 100, 1, 3, 30); + var main = app.project.items.addComp("Outer", 200, 200, 1, 6, 30); + var nested = main.layers.add(c); + var a = main.layers.addSolid([1, 0, 0], "A", 50, 50, 1, 2); + var b = main.layers.addSolid([0, 1, 0], "B", 50, 50, 1); + b.moveAfter(nested); + a.enabled = false; a.solo = true; a.locked = true; a.shy = true; + a.threeDLayer = true; + var cam = main.layers.addCamera("Cam", [100, 100]); + var lt = main.layers.addLight("Key", [100, 100]); + lt.lightType = LightType.SPOT; + var d = a.duplicate(); + d.name = "A copy"; + var mv = new MarkerValue("hello"); + mv.duration = 0.5; + main.markerProperty.setValueAtTime(1, mv); + b.marker.setValueAtTime(0.5, new MarkerValue("layer marker")); + b.inPoint = 1; b.outPoint = 3; + var pc = main.layers.precompose([b.index], "Pre", true); + [c.parentFolder.name, nested.source.name, a.outPoint, a.enabled, a.solo, a.locked, a.shy, a.threeDLayer, + cam instanceof CameraLayer, lt.lightType == LightType.SPOT, main.markerProperty.numKeys, main.markerProperty.keyValue(1).comment, + main.markerProperty.keyValue(1).duration, pc.name, pc instanceof CompItem, main.layer("A copy").index, app.project.numItems, + main.layers.byName("Pre") !== null, f.numItems] + "#, + ); + assert_eq!(o.result, json!(["Comps", "Inner", 2, false, true, true, true, true, true, true, 1, "hello", 0.5, "Pre", true, 3, 7, true, 1])); + let (_, c) = comp_named(&s, "Outer"); + assert_eq!(c.markers.len(), 1); + assert!( + c.layers + .iter() + .any(|l| l.name == "Key" + && matches!(l.source, effectcraft_engine::project::LayerSource::Light { kind: effectcraft_engine::project::LightKind::Spot })) + ); +} + +#[test] +fn security_gate_for_files() { + let dir = std::env::temp_dir().join(format!("ec-script-gate-{}", std::process::id())); + let proj = dir.join("proj"); + std::fs::create_dir_all(&proj).unwrap(); + std::fs::write(proj.join("data.txt"), "inside").unwrap(); + std::fs::write(dir.join("secret.txt"), "outside").unwrap(); + let mut s = session(); + s.path = Some(proj.join("p.ecproj").to_string_lossy().to_string()); + let q = |p: &std::path::Path| serde_json::to_string(&p.to_string_lossy()).unwrap(); + // Reading inside the project folder works. + let o = ok(&mut s, &format!("var f = new File({}); f.open('r'); var t = f.read(); f.close(); t", q(&proj.join("data.txt")))); + assert_eq!(o.result, json!("inside")); + // Outside it (also via `..`) is refused. + for path in [dir.join("secret.txt"), proj.join("../secret.txt")] { + let o = run_code(&mut s, &format!("var f = new File({}); f.open('r');", q(&path)), "gate.jsx"); + let e = o.error.expect("blocked"); + assert!(e.message.contains("Allow Scripts to Write Files"), "{e:?}"); + } + // Writing is refused anywhere, the network too. + let o = run_code(&mut s, &format!("var f = new File({}); f.open('w'); f.write('x'); f.close();", q(&proj.join("out.txt"))), "gate.jsx"); + assert!(o.error.is_some()); + assert!(!proj.join("out.txt").exists()); + assert!(run_code(&mut s, "new Socket()", "net.jsx").error.is_some()); + assert!(run_code(&mut s, "system.callSystem('ls')", "sys.jsx").error.is_some()); + // With the preference on, scripts can write and read anywhere. + s.prefs.scripting.allow_scripts_write_files = true; + ok(&mut s, &format!("var f = new File({}); f.open('w'); f.writeln('written'); f.close();", q(&dir.join("w.txt")))); + assert_eq!(std::fs::read_to_string(dir.join("w.txt")).unwrap(), "written\n"); + let o = ok(&mut s, &format!("var f = new File({}); f.open('r'); f.readln()", q(&dir.join("secret.txt")))); + assert_eq!(o.result, json!("outside")); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn engine_entry_points() { + let mut s = session(); + // script.run reports errors in the result, not as a command failure. + let r = s.execute("script.run", json!({"code": "writeLn('hi'); 1 + 2"})).unwrap(); + assert_eq!(r["ok"], json!(true)); + assert_eq!(r["result"], json!(3)); + assert_eq!(r["output"], json!("hi")); + let r = s.execute("script.run", json!({"code": "\nnope()"})).unwrap(); + assert_eq!(r["ok"], json!(false)); + assert_eq!(r["error"]["line"], json!(2)); + // Scripts can't run scripts. + let r = s.execute("script.run", json!({"code": "app.run('script.run', {code: '1'})"})).unwrap(); + assert!(r["error"]["message"].as_str().unwrap().contains("scripts can't run scripts"), "{r}"); + // File ▸ Scripts ▸ Run Script File… runs .jsx as JavaScript. + let dir = std::env::temp_dir().join(format!("ec-script-file-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let f = dir.join("make.jsx"); + std::fs::write(&f, "app.project.items.addComp('From File', 320, 240, 1, 2, 24);").unwrap(); + s.execute("file.runScript", json!({"path": f.to_string_lossy()})).unwrap(); + assert!(s.project.items.values().any(|i| i.name == "From File")); + std::fs::write(&f, "\n\nbroken(;").unwrap(); + let e = s.execute("file.runScript", json!({"path": f.to_string_lossy()})).unwrap_err().to_string(); + assert!(e.contains("make.jsx (line 3)"), "{e}"); + // JSON command scripts still work. + let j = dir.join("steps.json"); + std::fs::write(&j, r#"[{"command": "comp.new", "params": {"name": "From Steps"}}]"#).unwrap(); + s.execute("file.runScript", json!({"path": j.to_string_lossy()})).unwrap(); + assert!(s.project.items.values().any(|i| i.name == "From Steps")); + // Menu commands by name. + let o = ok(&mut s, "app.findMenuCommandId('New Project') > 0 && app.findMenuCommandId('No Such Thing') === 0"); + assert_eq!(o.result, json!(true)); + let o = ok(&mut s, "app.executeCommand(app.findMenuCommandId('New Project')); app.project.numItems"); + assert_eq!(o.result, json!(0)); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn console_keeps_variables() { + let mut s = session(); + let req = |code: &'static str| effectcraft_engine::ScriptRequest { code, name: "console", console: true }; + let r = crate::run(&mut s, &req("var counter = 41;")); + assert!(r.error.is_none(), "{r:?}"); + let r = crate::run(&mut s, &req("counter + 1")); + assert_eq!(r.result, json!(42)); + let _ = Value::Null; +} + +#[test] +fn schedule_task_runs_after_the_script() { + let mut s = session(); + let o = ok( + &mut s, + "app.scheduleTask('writeLn(\"later\")', 100, false); var id = app.scheduleTask('writeLn(\"never\")', 10, false); app.cancelTask(id); writeLn('now');", + ); + assert_eq!(o.output, vec!["now", "later"]); +} diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index 99bc7c5e..d1a40946 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -34,10 +34,11 @@ pub enum PanelKind { History, Markers, MaskInterpolation, + ScriptConsole, } impl PanelKind { - pub const ALL: [PanelKind; 24] = [ + pub const ALL: [PanelKind; 25] = [ PanelKind::Project, PanelKind::EffectControls, PanelKind::Composition, @@ -62,6 +63,7 @@ impl PanelKind { PanelKind::History, PanelKind::Markers, PanelKind::MaskInterpolation, + PanelKind::ScriptConsole, ]; pub fn title(self) -> &'static str { match self { @@ -89,6 +91,7 @@ impl PanelKind { PanelKind::History => "History", PanelKind::Markers => "Markers", PanelKind::MaskInterpolation => "Mask Interpolation", + PanelKind::ScriptConsole => "Script Console", } } pub fn id(self) -> String { diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index da5eaec2..32d471b3 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -697,7 +697,7 @@ fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option ("path", Ask::Save("Animation.json")), "file.importLottie" => ("path", Ask::Open(&["json", "lottie"])), "render.saveCurrentPreview" => ("path", Ask::Save("Preview.mp4")), - "file.runScript" => ("path", Ask::Open(&["jsonl", "json", "txt"])), + "file.runScript" => ("path", Ask::Open(&["jsx", "js", "jsonl", "json", "txt"])), "file.replaceFootage" => ("path", Ask::Import), "file.collectFiles" => ("folder", Ask::Save("Collected Files")), _ => return None, diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs index 3f737502..bdd05305 100644 --- a/crates/ui-egui/src/panels/mod.rs +++ b/crates/ui-egui/src/panels/mod.rs @@ -21,6 +21,7 @@ pub mod project; pub mod properties; pub mod puppet_tool; pub mod render_queue; +pub mod script_console; pub mod settings; pub mod shortcut_editor; pub mod text_panels; @@ -82,6 +83,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: PanelKind, rect: Rec PanelKind::History => misc::history(app, ui, rect), PanelKind::Markers => misc::markers(app, ui, rect), PanelKind::MaskInterpolation => anim_tools::mask_interpolation(app, ui, rect), + PanelKind::ScriptConsole => script_console::show(app, ui, rect), PanelKind::Wiggler => anim_tools::wiggler(app, ui, rect), PanelKind::Smoother => anim_tools::smoother(app, ui, rect), PanelKind::MotionSketch => anim_tools::motion_sketch(app, ui, rect), diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index 1824fec5..e9a40e61 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -529,16 +529,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } dresp.on_hover_text("Project color depth: click to cycle 8/16/32 bpc, Alt-click for Project Settings"); let tr = Rect::from_min_size(pos2(foot.max.x - 30.0, foot.min.y + 3.0), vec2(22.0, 22.0)); - if widgets::icon_button(ui, tr, Icon::Trash, false, &t, egui::Id::new("ptrash")).on_hover_text("Delete selected project items").clicked() { - let sel = app.session.state.project_selection.clone(); - let _ = app.session.edit("Delete Items", None, |proj, st| { - for id in &sel { - proj.items.remove(id); - } - st.project_selection.clear(); - Ok(()) - }); - app.session.sanitize_state(); + if widgets::icon_button(ui, tr, Icon::Trash, false, &t, egui::Id::new("ptrash")).on_hover_text("Delete selected project items").clicked() + && !app.session.state.project_selection.is_empty() + { + actions.push(("project.delete".into(), json!({}))); } app.auto.add("project.delete", tr, "Delete"); for (id, params) in actions { diff --git a/crates/ui-egui/src/panels/script_console.rs b/crates/ui-egui/src/panels/script_console.rs new file mode 100644 index 00000000..3c258f6a --- /dev/null +++ b/crates/ui-egui/src/panels/script_console.rs @@ -0,0 +1,144 @@ +//! Window ▸ Script Console: a JavaScript REPL over the After Effects-style scripting object model +//! (`app.project`, comps, layers, properties…). Each run goes through the `script.run` command +//! in the console's persistent context, so variables survive between runs; edits are undoable. + +use egui::{Align2, Rect, Sense, pos2, vec2}; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +use crate::EffectcraftApp; +use crate::theme::Tokens; + +/// One line of console output. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct ConsoleLine { + pub text: String, + /// `input`, `output`, `result` or `error`. + pub kind: String, +} + +/// Script Console state (serde, so agents can read it). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct ScriptConsole { + pub input: String, + pub log: Vec, + /// Previously run inputs (Up/Down recall). + #[serde(default)] + pub history: Vec, +} + +impl ScriptConsole { + fn push(&mut self, kind: &str, text: &str) { + for l in text.lines() { + self.log.push(ConsoleLine { text: l.to_string(), kind: kind.to_string() }); + } + if self.log.len() > 2000 { + let extra = self.log.len() - 2000; + self.log.drain(..extra); + } + } + + /// Record a `script.run` result. + pub fn record(&mut self, code: &str, r: &Result) { + self.push("input", &format!("> {}", code.trim())); + match r { + Ok(v) => { + if let Some(o) = v["output"].as_str().filter(|o| !o.is_empty()) { + self.push("output", o); + } + if let Some(e) = v.get("error").filter(|e| !e.is_null()) { + let line = e["line"].as_u64().map(|l| format!("line {l}: ")).unwrap_or_default(); + self.push("error", &format!("{line}{}", e["message"].as_str().unwrap_or("error"))); + } else if !v["result"].is_null() { + self.push("result", &v["result"].to_string()); + } + } + Err(e) => self.push("error", e), + } + } +} + +/// Run the console input. +pub fn run(app: &mut EffectcraftApp, ctx: &egui::Context) { + let code = app.ui.script_console.input.trim().to_string(); + if code.is_empty() { + return; + } + let r = crate::menus::invoke(app, ctx, "script.run", json!({"code": code, "name": "Script Console", "console": true})); + let con = &mut app.ui.script_console; + con.record(&code, &r); + if con.history.last() != Some(&code) { + con.history.push(code); + } + con.input.clear(); +} + +pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { + let t = app.tokens; + let input_h = 86.0; + let bar_h = 26.0; + let out_rect = Rect::from_min_max(rect.min, pos2(rect.max.x, rect.max.y - input_h - bar_h)); + let p = ui.painter().with_clip_rect(rect); + p.rect_filled(out_rect, 0.0, t.field_bg); + // Output, newest at the bottom. + let line_h = 16.0; + let visible = ((out_rect.height() - 8.0) / line_h).max(1.0) as usize; + let log = &app.ui.script_console.log; + let start = log.len().saturating_sub(visible); + for (i, l) in log[start..].iter().enumerate() { + let color = match l.kind.as_str() { + "error" => t.danger, + "input" => t.text_faint, + "result" => t.accent, + _ => t.text, + }; + p.text(pos2(out_rect.min.x + 8.0, out_rect.min.y + 4.0 + i as f32 * line_h), Align2::LEFT_TOP, &l.text, Tokens::mono(11.5), color); + } + if log.is_empty() { + p.text( + out_rect.center(), + Align2::CENTER_CENTER, + "JavaScript with the After Effects scripting object model — try app.project.numItems", + Tokens::ui(12.0), + t.text_faint, + ); + } + app.auto.add("scriptConsole.output", out_rect, "Script Console output"); + // Input. + let in_rect = Rect::from_min_size(pos2(rect.min.x + 4.0, out_rect.max.y + 4.0), vec2(rect.width() - 8.0, input_h - 8.0)); + let mut run_now = false; + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(in_rect)); + let te = egui::TextEdit::multiline(&mut app.ui.script_console.input) + .id(egui::Id::new("script-console-input")) + .font(Tokens::mono(12.0)) + .desired_width(in_rect.width()) + .desired_rows(4) + .hint_text("Script… (⌘/Ctrl+Enter runs)"); + let resp = child.add_sized(in_rect.size(), te); + if resp.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter) && i.modifiers.command) { + run_now = true; + } + app.auto.add("scriptConsole.input", in_rect, "Script Console input"); + // Buttons. + let bar = Rect::from_min_size(pos2(rect.min.x, rect.max.y - bar_h), vec2(rect.width(), bar_h)); + let run_r = Rect::from_min_size(pos2(bar.max.x - 64.0, bar.min.y + 2.0), vec2(56.0, 20.0)); + let clear_r = Rect::from_min_size(pos2(bar.max.x - 128.0, bar.min.y + 2.0), vec2(56.0, 20.0)); + let run_resp = ui.interact(run_r, egui::Id::new("script-console-run"), Sense::click()); + p.rect_filled(run_r, 3.0, if run_resp.hovered() { t.accent_hover } else { t.accent }); + p.text(run_r.center(), Align2::CENTER_CENTER, "Run", Tokens::ui(12.0), egui::Color32::WHITE); + let clear_resp = ui.interact(clear_r, egui::Id::new("script-console-clear"), Sense::click()); + p.rect_stroke(clear_r, 3.0, egui::Stroke::new(1.0, t.field_border), egui::StrokeKind::Inside); + p.text(clear_r.center(), Align2::CENTER_CENTER, "Clear", Tokens::ui(12.0), t.text); + app.auto.add("scriptConsole.run", run_r, "Run"); + app.auto.add("scriptConsole.clear", clear_r, "Clear"); + if run_resp.clicked() { + run_now = true; + } + if clear_resp.clicked() { + app.ui.script_console.log.clear(); + } + if run_now { + let ctx = ui.ctx().clone(); + run(app, &ctx); + } +} diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index d1350103..7d423b8a 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -476,6 +476,9 @@ impl Default for AnimToolsState { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct UiState { pub tool: Tool, + /// Window ▸ Script Console input, output and history. + #[serde(default)] + pub script_console: crate::panels::script_console::ScriptConsole, /// Tool shown in each toolbar slot. pub slot_tools: Vec, pub workspace: String, @@ -576,6 +579,7 @@ impl Default for UiState { fn default() -> Self { UiState { tool: Tool::Selection, + script_console: Default::default(), slot_tools: Tool::SLOTS.iter().map(|s| s[0]).collect(), workspace: "Default".into(), dock: crate::dock::workspace("Default"), diff --git a/crates/ui-egui/tests/ui_script_console.rs b/crates/ui-egui/tests/ui_script_console.rs new file mode 100644 index 00000000..a8068c9b --- /dev/null +++ b/crates/ui-egui/tests/ui_script_console.rs @@ -0,0 +1,44 @@ +//! Window ▸ Script Console: the panel shows, runs JavaScript through `script.run` (persistent +//! context) and logs output, results and errors (egui_kittest, UI logic only). + +use effectcraft_ui_egui::EffectcraftApp; +use effectcraft_ui_egui::dock::PanelKind; +use egui_kittest::Harness; + +fn click(h: &mut Harness<'_, EffectcraftApp>, id: &str) { + let e = h.state().auto.find(id).unwrap_or_else(|| panic!("no {id}")).clone(); + let c = egui::pos2(e.rect[0] + e.rect[2] / 2.0, e.rect[1] + e.rect[3] / 2.0); + h.input_mut().events.push(egui::Event::PointerMoved(c)); + for pressed in [true, false] { + h.input_mut().events.push(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }); + } + h.run_steps(2); +} + +#[test] +fn script_console_runs_scripts() { + let s = effectcraft_host::session(); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_| EffectcraftApp::new(s)); + h.state_mut().show_panel(PanelKind::ScriptConsole); + h.run_steps(3); + for id in ["scriptConsole.input", "scriptConsole.output", "scriptConsole.run", "scriptConsole.clear"] { + assert!(h.state().auto.find(id).is_some(), "missing {id}"); + } + h.state_mut().ui.script_console.input = "var c = app.project.items.addComp('Console', 320, 180, 1, 2, 24); writeLn('made'); c.name".into(); + h.run_steps(1); + click(&mut h, "scriptConsole.run"); + assert!(h.state().session.project.items.values().any(|i| i.name == "Console")); + // Variables persist between runs; errors are logged with their line. + h.state_mut().ui.script_console.input = "c.numLayers + 5".into(); + click(&mut h, "scriptConsole.run"); + h.state_mut().ui.script_console.input = "nope()".into(); + click(&mut h, "scriptConsole.run"); + let log: Vec<(String, String)> = h.state().ui.script_console.log.iter().map(|l| (l.kind.clone(), l.text.clone())).collect(); + assert!(log.contains(&("output".into(), "made".into())), "{log:?}"); + assert!(log.contains(&("result".into(), "\"Console\"".into())), "{log:?}"); + assert!(log.contains(&("result".into(), "5".into())), "{log:?}"); + assert!(log.iter().any(|(k, t)| k == "error" && t.contains("line 1") && t.contains("nope")), "{log:?}"); + assert!(h.state_mut().session.undo(), "console edits are undoable"); + click(&mut h, "scriptConsole.clear"); + assert!(h.state().ui.script_console.log.is_empty()); +} diff --git a/docs/agents.md b/docs/agents.md index 510bc356..40dac3c9 100644 --- a/docs/agents.md +++ b/docs/agents.md @@ -49,6 +49,7 @@ The server speaks JSON-RPC 2.0 over stdio, one message per line, and supports MC |---|---| | `list_commands {filter?, enabled_only?}` | Discover command ids, their param docs, and whether each can run now. | | `execute_command {command, params?}` | Run any command (undoable). | +| `run_script {code, name?}` | Run JavaScript with the After Effects-style scripting object model (`app.project`, `comp.layers.addText(…)`, `layer.property("ADBE Transform Group").property("ADBE Position").setValueAtTime(…)`…). Returns `{ok, result, output, error: {message, line, column}}`; edits are undoable. | | `get_project` / `get_comp {comp?}` | Project items, comp settings and layers. | | `get_layer {layer, comp?, time?, flat?}` | A layer's property tree. Every node has a `path`. | | `get_property {layer, path, comp?, time?}` | Value at a time, keyframes and expression. | @@ -191,6 +192,19 @@ effectcraft-cli exec file.exportLottie '{"comp":"Main","path":"main.json","inclu effectcraft-cli exec file.importLottie '{"path":"anim.json"}' main.ecproj --save ``` +Scripts written against After Effects' documented scripting API run with `script`: + +```sh +effectcraft-cli script build.jsx --save-as main.ecproj # empty project unless one is given +effectcraft-cli script --eval 'app.project.item(1).numLayers' main.ecproj --json +effectcraft-cli script tweak.jsx --bridge 9877 # against the live app +``` + +`writeLn`/`$.writeln`/`alert` output is printed, then the value of the last expression. A script +error exits with status 1 and `file:line:col: message`. Scripts may read files only in the +project's folder and may not write files or use the network unless the user turns on Preferences ▸ +Scripting & Expressions ▸ Allow Scripts to Write Files and Access Network. + `file.exportLottie` returns `{path, bytes, warnings}`: the warnings list every feature Lottie cannot express (most effects, cameras and lights, layer styles, audio, video footage…), so an agent can check what a player will not show. A `.lottie` path writes a dotLottie archive. diff --git a/docs/architecture.md b/docs/architecture.md index 033ed6cb..dceb0e0c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -30,13 +30,15 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for | L3 | `gpu` | The GPU compositor and GPU effects on wgpu compute shaders (Metal, Vulkan, Direct3D 12, WebGPU), checked against the CPU renderer | | L3 | `lottie` | Lottie JSON / dotLottie import and export (layers, precomps, eased and spatial keyframes, shapes, masks, mattes) with a warnings list for what Lottie cannot express | | L4 | `engine` | `Session`: project, undo history, editor state, the command registry and menus | -| L4 | `host` | A fully wired `Session` (media, expressions, exporter) for the frontends | +| L4 | `script` | Scripting: JavaScript (boa) with an After Effects-style object model (`app.project`, comps, layers, properties, render queue) whose edits run engine commands; AE match names | +| L4 | `host` | A fully wired `Session` (media, expressions, scripting, exporter) for the frontends | | L5 | `ui-egui` | The desktop interface: docking, panels, viewer, timeline, graph editor, dialogs, control channel | | L5 | `automation` | The MCP server, headless or bridged to the running app | | L6 | apps `effectcraft`, `effectcraft-cli`, `effectcraft-web` | Desktop app; command-line tool (render, exec, get/set, MCP); the browser app (wasm32, [web.md](web.md)) | Allowed same-layer edges: `path → keyframe, raster`, `text → path`, `effects → project, text, path, track`, -`media / expr / export / gpu → render`, `export → media`, `lottie → format`, `host → engine`. +`media / expr / export / gpu → render`, `export → media`, `lottie → format`, `host → engine, script`, +`script → engine`. ## 2. Time @@ -182,6 +184,28 @@ Expressions are JavaScript, run by boa, with After Effects' object model (`thisC `time`, `value`, `wiggle`, `loopOut`, vector maths on arrays, and so on). A syntax error keeps the text, disables the expression and shows a warning, like After Effects. +## 5a. Scripting + +`effectcraft-script` runs JavaScript (boa, a fresh context per run; the Script Console keeps one) +with an object model written from the behaviour the After Effects Scripting Guide documents: +`app`, `Project`, `ItemCollection`, `CompItem`/`FootageItem`/`FolderItem`, `LayerCollection`, +`AVLayer`/`TextLayer`/`ShapeLayer`/`CameraLayer`/`LightLayer`, `PropertyGroup`/`Property` +(values, keyframes, eases, expressions), `TextDocument`, `Shape`, `KeyframeEase`, `MarkerValue`, +`RenderQueue`/`RenderQueueItem`/`OutputModule`, `File`/`Folder` and the enums. The model lives in +`prelude.js`; it reads the session through `__query` (JSON snapshots of items, layers, property +nodes, values and keys) and edits only through `__exec`, which runs engine commands, so every +scripted edit is undoable and identical to the UI's. While a script runs the session is moved +into a thread-local the natives borrow (nothing borrowed enters the JS heap); the comp a call +targets is made active for that command only. `app.beginUndoGroup`/`endUndoGroup` fold the undo +steps in between into one named step. Match names (`ADBE Transform Group`, `ADBE Position`, +`ADBE Gaussian Blur 2`, `ADBE Vector Shape - Rect`…) come from a table in `matchnames.rs`; effect +parameters are `-0001…`. `File` reads are limited to the project's folder, and +writes and the network are refused, unless Preferences ▸ Scripting & Expressions ▸ Allow Scripts +to Write Files and Access Network is on. Entry points: the `script.run` command +(`Session::script`, set by the host), File ▸ Scripts ▸ Run Script File… (`.jsx`/`.js`; `.json` +command scripts still run as steps), Window ▸ Script Console, `effectcraft-cli script` and the +MCP `run_script` tool. + ## 6. Commands, the UI seam and automation Every action is a command: an id, a label, a menu path, a shortcut, a parameter description, diff --git a/docs/parity.md b/docs/parity.md index d259fec2..02fa2175 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -75,7 +75,7 @@ library groundwork for mask tracking and Warp Stabilizer. What is left is concentrated in large systems: Roto Brush and Refine Edge, the 3D camera tracker, Advanced 3D (models, PBR, image-based lighting), the Warp Stabilizer and mask-tracking UI on top of -the new tracking library, a JavaScript scripting object model, SVG/PSD import, WebM, a disk cache, +the new tracking library, SVG/PSD import, WebM, a disk cache, the missing effect categories (3D Channel, Immersive Video, OCIO), native macOS menus, and wasm threads. @@ -87,7 +87,7 @@ wasm threads. | Output | 85% | 4.2 | multiple output modules, pre-render, WebM, audio-only | | Audio | 85% | 0.5 | audio to keyframes | | Import | 78% | 4.5 | PSD, SVG as shapes, Lottie | -| Automation | 76% | 3.3 | a JavaScript scripting object model | +| Automation | ≈ 90% | 0.8 | scripting covers the documented core object model (AUT-2, M14.4: `app`, project items, comps, layers, properties and keyframes, text documents, markers, render queue, Script Console, `effectcraft-cli script`, MCP `run_script`); still missing: ScriptUI panels/dialogs, `.jsxbin`, sockets | | Shapes | 68% | 3.9 | taper and wave strokes (pen tool for shape paths and vertex editing landed in M6.5) | | Compositions | 67% | 7.4 | marker dialog, flowchart, Essential Graphics | | Animation | 65% | 10.4 | puppet, Wiggler/Smoother/Motion Sketch (motion-path handles and the graph editor transform box landed in M5.8) | diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 76ff6fa3..2532f2a2 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -39,6 +39,7 @@ const LAYERS: &[(&str, u8)] = &[ ("lottie", 3), ("format", 3), ("engine", 4), + ("script", 4), ("host", 4), ("ui-egui", 5), ("automation", 5), @@ -63,6 +64,8 @@ const SAME_LAYER: &[(&str, &str)] = &[ ("gpu", "render"), ("lottie", "format"), ("host", "engine"), + ("script", "engine"), + ("host", "script"), ("cli", "effectcraft"), ]; From d880746fecf0c211c75983de380708e32a54d590 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:18:11 -0400 Subject: [PATCH 15/23] M14.4: scripting tests for layer/comp settings, import + save, console snapshot; Project.saveAs Co-Authored-By: Claude Opus 5.5 --- crates/host/src/lib.rs | 29 ++++++++++++ crates/script/src/prelude.js | 1 + crates/script/src/tests.rs | 56 +++++++++++++++++++++++ crates/ui-egui/tests/ui_script_console.rs | 24 ++++++++++ 4 files changed, 110 insertions(+) diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index 5235feca..45e850ff 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -220,6 +220,35 @@ mod tests { assert!(bytes.starts_with(b"GIF89a")); } + /// importFile, layers.add(footage) and project save/open from a script. + #[test] + fn script_imports_footage_and_saves() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/test-out/host-script-import"); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let q = |n: &str| serde_json::to_string(&dir.join(n).to_string_lossy()).unwrap(); + let mut s = super::session(); + let code = format!( + r#" + var c = app.project.items.addComp("Src", 32, 32, 1, 1, 10); + c.layers.addSolid([0, 1, 0], "G", 32, 32, 1); + app.run("comp.saveFrameAs", {{comp: c.id, path: {png}}}); + var still = app.project.importFile(new ImportOptions(new File({png}))); + var main = app.project.items.addComp("Main", 64, 64, 1, 1, 10); + var l = main.layers.add(still); + app.project.save(new File({proj})); + [still instanceof FootageItem, still.width, still.mainSource.isStill, l.source.name, app.project.file.name] + "#, + png = q("frame.png"), + proj = q("p.ecproj") + ); + let r = s.execute("script.run", json!({"code": code})).unwrap(); + assert_eq!(r["ok"], json!(true), "{r}"); + assert_eq!(r["result"], json!([true, 32, true, "frame.png", "p.ecproj"]), "{r}"); + let r = s.execute("script.run", json!({"code": format!("app.open(new File({})); app.project.numItems", q("p.ecproj"))})).unwrap(); + assert_eq!(r["result"], json!(5), "{r}"); // Src, Solids, G, frame.png, Main + } + #[test] fn expression_syntax_errors_disable_the_expression() { let mut s = super::session(); diff --git a/crates/script/src/prelude.js b/crates/script/src/prelude.js index 4f359926..c4f256f4 100644 --- a/crates/script/src/prelude.js +++ b/crates/script/src/prelude.js @@ -1571,6 +1571,7 @@ Project.prototype = { else __call("file.save", {}); return true; }, + saveAs: function (file) { return this.save(file); }, saveWithDialog: function () { if (this.file) return this.save(); return false; }, close: function (opt) { if (opt === CloseOptions.SAVE_CHANGES && this.file) this.save(); diff --git a/crates/script/src/tests.rs b/crates/script/src/tests.rs index f4a4fcb6..458f2b41 100644 --- a/crates/script/src/tests.rs +++ b/crates/script/src/tests.rs @@ -390,3 +390,59 @@ fn schedule_task_runs_after_the_script() { ); assert_eq!(o.output, vec!["now", "later"]); } + +#[test] +fn layer_switches_timing_and_comp_settings() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("S", 320, 240, 1, 5, 30); + var matte = c.layers.addText("M"); + var fill = c.layers.addSolid([1, 1, 1], "Fill", 320, 240, 1); + fill.moveToEnd(); + fill.setTrackMatte(matte, TrackMatteType.LUMA); + var t1 = fill.trackMatteType == TrackMatteType.LUMA && fill.hasTrackMatte && fill.trackMatteLayer.name == "M"; + fill.stretch = 200; + fill.label = 3; + c.width = 400; + c.duration = 8; + c.bgColor = [0.1, 0.2, 0.3]; + c.time = 2; + c.workAreaStart = 1; + c.workAreaDuration = 3; + var extra = c.layers.addNull(); + extra.remove(); + fill.selected = true; + var sel = c.selectedLayers.length; + fill.transform.opacity.expression = "thisLayer.nope("; + var err = fill.transform.opacity.expressionError; + fill.transform.opacity.expressionEnabled = false; + var copy = c.duplicate(); + var f = app.project.items.addFolder("Bin"); + copy.parentFolder = f; + [t1, fill.stretch, fill.label, c.width, c.duration, c.bgColor[2], c.time, c.workAreaStart, c.workAreaDuration, + c.numLayers, sel, err.length > 0, fill.transform.opacity.expressionEnabled, copy.name, f.numItems, copy.parentFolder.name] + "#, + ); + let v = o.result; + assert_eq!(v[0], json!(true), "{v}"); + assert_eq!(v[1], json!(200)); + assert_eq!(v[2], json!(3)); + assert_eq!(v[3], json!(400)); + assert_eq!(v[4], json!(8)); + assert!((v[5].as_f64().unwrap() - 0.3).abs() < 1e-6); + assert_eq!(v[6], json!(2)); + assert_eq!(v[7], json!(1)); + assert_eq!(v[8], json!(3)); + assert_eq!(v[9], json!(2)); + assert_eq!(v[10], json!(1)); + assert_eq!(v[11], json!(true)); + assert_eq!(v[12], json!(false)); + assert_eq!(v[13], json!("S 2")); + assert_eq!(v[14], json!(1)); + assert_eq!(v[15], json!("Bin")); + // Removing the copy through the object model. + let o = ok(&mut s, "var n = app.project.numItems; app.project.item(1).remove(); n - app.project.numItems"); + assert_eq!(o.result, json!(1)); +} diff --git a/crates/ui-egui/tests/ui_script_console.rs b/crates/ui-egui/tests/ui_script_console.rs index a8068c9b..526a5293 100644 --- a/crates/ui-egui/tests/ui_script_console.rs +++ b/crates/ui-egui/tests/ui_script_console.rs @@ -42,3 +42,27 @@ fn script_console_runs_scripts() { click(&mut h, "scriptConsole.clear"); assert!(h.state().ui.script_console.log.is_empty()); } + +/// Headless look at the panel (wgpu offscreen); run with +/// `SC_SNAPSHOT=/abs/out.png cargo test -p effectcraft-ui-egui --test ui_script_console -- --ignored`. +#[test] +#[ignore] +fn script_console_snapshot() { + let s = effectcraft_host::session(); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_| EffectcraftApp::new(s)); + h.state_mut().show_panel(PanelKind::ScriptConsole); + h.run_steps(2); + for code in ["var c = app.project.items.addComp('Console', 320, 180, 1, 2, 24); writeLn('made ' + c.name); c.id", "c.layers.addText('Hi').name", "nope()"] { + h.state_mut().ui.script_console.input = code.into(); + h.run_steps(1); + click(&mut h, "scriptConsole.run"); + } + h.state_mut().ui.script_console.input = "app.project.numItems".into(); + h.run_steps(3); + let img = h.render().expect("render"); + let out = std::env::var("SC_SNAPSHOT").unwrap_or_else(|_| concat!(env!("CARGO_MANIFEST_DIR"), "/../../target/test-out/script_console.png").into()); + if let Some(d) = std::path::Path::new(&out).parent() { + std::fs::create_dir_all(d).unwrap(); + } + img.save(&out).unwrap(); +} From 07330bca6b15da7653a95c7255a0e923c85837b4 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:20:25 -0400 Subject: [PATCH 16/23] M6.7: roto brush segmentation + propagation Graph-cut segmentation (Boykov-Kolmogorov max-flow, GrabCut GMMs, coarse to fine), optical-flow propagation with band re-segmentation, correction strokes, Roto Brush & Refine Edge effect with content-keyed derived cache, roto.* commands (stroke/propagate/span/freeze/unfreeze/clearStrokes/ cancel/options/status), refine-edge matting (guided filter + closed-form) and decontamination, Freeze job. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/effects/Cargo.toml | 3 +- crates/effects/src/lib.rs | 6 +- crates/effects/src/roto.rs | 698 ++++++++++++++++++++++ crates/engine/src/commands/edit.rs | 3 + crates/engine/src/commands/mod.rs | 2 + crates/engine/src/commands/roto_cmds.rs | 424 +++++++++++++ crates/engine/src/lib.rs | 22 + crates/engine/src/roto.rs | 569 ++++++++++++++++++ crates/engine/src/tests_roto.rs | 288 +++++++++ crates/track/Cargo.toml | 2 +- crates/track/src/lib.rs | 1 + crates/track/src/roto/gmm.rs | 228 +++++++ crates/track/src/roto/matting.rs | 465 +++++++++++++++ crates/track/src/roto/maxflow.rs | 438 ++++++++++++++ crates/track/src/roto/mod.rs | 754 ++++++++++++++++++++++++ crates/track/src/roto/refine.rs | 198 +++++++ crates/track/src/roto/rle.rs | 136 +++++ crates/track/src/roto/tests.rs | 331 +++++++++++ 19 files changed, 4565 insertions(+), 4 deletions(-) create mode 100644 crates/effects/src/roto.rs create mode 100644 crates/engine/src/commands/roto_cmds.rs create mode 100644 crates/engine/src/roto.rs create mode 100644 crates/engine/src/tests_roto.rs create mode 100644 crates/track/src/roto/gmm.rs create mode 100644 crates/track/src/roto/matting.rs create mode 100644 crates/track/src/roto/maxflow.rs create mode 100644 crates/track/src/roto/mod.rs create mode 100644 crates/track/src/roto/refine.rs create mode 100644 crates/track/src/roto/rle.rs create mode 100644 crates/track/src/roto/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 46f34e63..544b6615 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1184,6 +1184,7 @@ dependencies = [ "effectcraft-raster", "effectcraft-track", "rayon", + "serde", "serde_json", ] diff --git a/crates/effects/Cargo.toml b/crates/effects/Cargo.toml index e321fa2f..20068a93 100644 --- a/crates/effects/Cargo.toml +++ b/crates/effects/Cargo.toml @@ -17,6 +17,5 @@ effectcraft-project = { workspace = true } effectcraft-raster = { workspace = true } effectcraft-track = { workspace = true } rayon = { workspace = true } - -[dev-dependencies] +serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 6d11e19a..2c3255d7 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -31,6 +31,7 @@ pub mod paint; mod perspective; mod perspective2; pub mod puppet; +pub mod roto; mod sim; mod sim2; mod stylize2; @@ -354,6 +355,7 @@ pub fn registry() -> &'static [EffectSpec] { v.extend(paint::specs()); v.extend(puppet::specs()); v.extend(warp_stab::specs()); + v.extend(roto::specs()); v.sort_by(|a, b| a.category.cmp(b.category).then(a.name.cmp(b.name))); for s in v.iter_mut() { if GPU_EFFECTS.contains(&s.id) { @@ -426,7 +428,7 @@ pub fn instantiate(spec: &EffectSpec, ids: &mut Ids, instance_name: &str, layer_ /// Display names of nested parameter groups by match id. fn group_name(m: &str) -> &str { - warp_stab::GROUPS.iter().find(|(id, _)| *id == m).map(|(_, n)| *n).unwrap_or(m) + warp_stab::GROUPS.iter().chain(roto::GROUPS).find(|(id, _)| *id == m).map(|(_, n)| *n).unwrap_or(m) } /// The nested group at `path` (`borders/autoScale`) under `g`, created on first use. @@ -494,6 +496,8 @@ pub const TIME_DEPENDENT: &[&str] = &[ "ec.time.pixelmotionblur", // Each frame gets its own stabilizing warp. warp_stab::ID, + // Each frame has its own segmentation. + roto::ID, ]; /// See [`TIME_DEPENDENT`]. diff --git a/crates/effects/src/roto.rs b/crates/effects/src/roto.rs new file mode 100644 index 00000000..a1f93a92 --- /dev/null +++ b/crates/effects/src/roto.rs @@ -0,0 +1,698 @@ +//! Roto Brush & Refine Edge (applied by the Roto Brush tool; Effect ▸ Matte). +//! +//! The instance stores the user's strokes, base frame and segmentation span as JSON in its hidden +//! **Strokes** parameter ([`RotoData`]); the segmentation itself is *derived* data: each frame's +//! result is a deterministic function of its chain key ([`RotoData::chain_keys`] seeded with +//! [`seed`]: what the frames are, the Roto Brush settings and the strokes from the base frame +//! out to the frame), so it lives in a process-wide content-keyed cache ([`cached`]) that the +//! engine's propagation job fills at full resolution and rendering fills on demand (reading +//! neighbouring frames with [`crate::EffectHost::self_at`]). Undo, redo and reopening a project +//! therefore never see stale mattes. **Freeze** stores the final mattes in the hidden **Frozen** +//! parameter ([`FrozenCache`]); they are used while their key still matches. +//! +//! Rendering: binary segmentation → Roto Brush Matte (Shift Edge, Reduce Chatter, Feather, +//! Contrast) → Refine Edge matting inside the Refine Edge band (guided filter + closed-form +//! matting with Higher Quality) → Refine Edge Matte adjustments → matte motion blur → +//! decontamination of the edge colours. Outside the segmentation span the layer is transparent; +//! without strokes the effect passes its input through. +//! +//! *Version* is stored but all versions use the same (classic, graph-cut) engine; *Quality ▸ +//! Best* segments at a higher working resolution with more iterations. + +use std::collections::{BTreeMap, HashMap, VecDeque}; +use std::sync::{Arc, Mutex, OnceLock}; + +use effectcraft_keyframe::Value; +use effectcraft_project::ParamUi; +use effectcraft_raster::Image; +use effectcraft_track::roto::refine::{self, MatteParams}; +use effectcraft_track::roto::{self as rb, FrameSeg, RotoData, SegOpts, rle}; +use rayon::prelude::*; +use serde::{Deserialize, Serialize}; + +use crate::warp_stab::param; +use crate::{Buf, EffectCtx, EffectSpec, Params, num, p, popup, slider}; + +pub use effectcraft_track::roto::{Stroke, StrokeKind}; + +/// The effect's id. +pub const ID: &str = "ec.matte.rotobrush"; +/// Hidden parameters. +pub const STROKES: &str = "strokes"; +pub const INPUT_KEY: &str = "inputKey"; +pub const FROZEN: &str = "frozen"; + +/// Default segmentation span each side of the base frame (frames). +pub const DEFAULT_SPAN: i64 = 20; + +/// Display names of the instance's parameter groups. +pub const GROUPS: &[(&str, &str)] = &[("rotoBrushMatte", "Roto Brush Matte"), ("refineEdgeMatte", "Refine Edge Matte")]; + +pub const VERSIONS: [&str; 3] = ["1.0", "2.0", "3.0"]; +pub const QUALITIES: [&str; 2] = ["Standard", "Best"]; + +pub fn specs() -> Vec { + vec![EffectSpec { + id: ID, + name: "Roto Brush & Refine Edge", + category: "Matte", + params: vec![ + p("version", "Version", Value::Enum(2), popup(&VERSIONS)), + p("quality", "Quality", Value::Enum(0), popup(&QUALITIES)), + p("rotoBrushMatte/searchRadius", "Search Radius", num(15.0), slider(0.0, 100.0, 0.0, 50.0, 1)), + p("rotoBrushMatte/motionThreshold", "Motion Threshold", num(10.0), slider(0.0, 100.0, 0.0, 100.0, 0)), + p("rotoBrushMatte/motionDamping", "Motion Damping", num(20.0), slider(0.0, 100.0, 0.0, 100.0, 0)), + p("rotoBrushMatte/viewSearchRegion", "View Search Region", Value::Bool(false), ParamUi::Checkbox), + p("rotoBrushMatte/feather", "Feather", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("rotoBrushMatte/contrast", "Contrast", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 0)), + p("rotoBrushMatte/shiftEdge", "Shift Edge", num(0.0), slider(-100.0, 100.0, -100.0, 100.0, 0)), + p("rotoBrushMatte/reduceChatter", "Reduce Chatter", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 0)), + p("refineEdgeMatte/smooth", "Smooth", num(1.0), slider(0.0, 100.0, 0.0, 10.0, 1)), + p("refineEdgeMatte/feather", "Feather", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("refineEdgeMatte/contrast", "Contrast", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 0)), + p("refineEdgeMatte/shiftEdge", "Shift Edge", num(0.0), slider(-100.0, 100.0, -100.0, 100.0, 0)), + p("refineEdgeMatte/reduceChatter", "Reduce Chatter", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 0)), + p("refineEdgeMatte/useMotionBlur", "Use Motion Blur", Value::Bool(false), ParamUi::Checkbox), + p("refineEdgeMatte/motionBlurSamples", "Motion Blur Samples", num(11.0), slider(1.0, 64.0, 1.0, 32.0, 0)), + p("refineEdgeMatte/shutterAngle", "Shutter Angle", num(180.0), slider(0.0, 720.0, 0.0, 360.0, 0)), + p("refineEdgeMatte/higherQuality", "Higher Quality", Value::Bool(true), ParamUi::Checkbox), + p("refineEdgeMatte/decontaminateEdgeColors", "Decontaminate Edge Colors", Value::Bool(true), ParamUi::Checkbox), + p("refineEdgeMatte/decontaminationAmount", "Decontamination Amount", num(100.0), slider(0.0, 100.0, 0.0, 100.0, 0)), + p("refineEdgeMatte/extendWhereSmoothed", "Extend Where Smoothed", Value::Bool(true), ParamUi::Checkbox), + p("refineEdgeMatte/increaseDecontaminationRadius", "Increase Decontamination Radius", num(0.0), slider(0.0, 100.0, 0.0, 20.0, 1)), + p("refineEdgeMatte/viewDecontaminationMap", "View Decontamination Map", Value::Bool(false), ParamUi::Checkbox), + p(STROKES, "Strokes", Value::Str(String::new()), ParamUi::Hidden), + p(INPUT_KEY, "Input Key", Value::Str(String::new()), ParamUi::Hidden), + p(FROZEN, "Frozen", Value::Str(String::new()), ParamUi::Hidden), + ], + render, + gpu: false, + float: true, + }] +} + +fn f(params: &Params, id: &str) -> f64 { + param(params, id).map(Value::as_f64).unwrap_or(0.0) +} +fn e(params: &Params, id: &str) -> u32 { + param(params, id).map(Value::as_enum).unwrap_or(0) +} +fn b(params: &Params, id: &str) -> bool { + param(params, id).map(Value::as_bool).unwrap_or(false) +} +fn s<'a>(params: &'a Params, id: &str) -> &'a str { + match param(params, id) { + Some(Value::Str(s)) => s, + _ => "", + } +} + +fn fnv(h: &mut u64, bytes: &[u8]) { + for b in bytes { + *h ^= *b as u64; + *h = h.wrapping_mul(0x0100_0000_01b3); + } +} + +/// The instance's strokes (parsed once per content). +pub fn data(params: &Params) -> Arc { + static C: OnceLock>>> = OnceLock::new(); + let j = s(params, STROKES); + let mut h = 0xcbf2_9ce4_8422_2325; + fnv(&mut h, j.as_bytes()); + let c = C.get_or_init(Default::default); + if let Some(d) = c.lock().unwrap_or_else(|e| e.into_inner()).get(&h) { + return d.clone(); + } + let d = Arc::new(RotoData::from_json(j)); + let mut m = c.lock().unwrap_or_else(|e| e.into_inner()); + if m.len() > 64 { + m.clear(); + } + m.insert(h, d.clone()); + d +} + +/// Segmentation settings at `scale` (pixels per layer pixel). +pub fn seg_opts(params: &Params, scale: f64) -> SegOpts { + SegOpts { + scale, + search_radius: f(params, "rotoBrushMatte/searchRadius").max(0.0), + motion_threshold: f(params, "rotoBrushMatte/motionThreshold"), + motion_damping: f(params, "rotoBrushMatte/motionDamping"), + best: e(params, "quality") == 1, + } +} + +/// Matte settings. +pub fn matte_params(params: &Params) -> MatteParams { + MatteParams { + rb_feather: f(params, "rotoBrushMatte/feather").max(0.0), + rb_contrast: f(params, "rotoBrushMatte/contrast"), + rb_shift: f(params, "rotoBrushMatte/shiftEdge"), + rb_chatter: f(params, "rotoBrushMatte/reduceChatter"), + smooth: f(params, "refineEdgeMatte/smooth").max(0.0), + feather: f(params, "refineEdgeMatte/feather").max(0.0), + contrast: f(params, "refineEdgeMatte/contrast"), + shift: f(params, "refineEdgeMatte/shiftEdge"), + chatter: f(params, "refineEdgeMatte/reduceChatter"), + motion_blur: b(params, "refineEdgeMatte/useMotionBlur"), + mb_samples: f(params, "refineEdgeMatte/motionBlurSamples").round().clamp(1.0, 64.0) as usize, + shutter_angle: f(params, "refineEdgeMatte/shutterAngle"), + higher_quality: b(params, "refineEdgeMatte/higherQuality"), + decontaminate: b(params, "refineEdgeMatte/decontaminateEdgeColors"), + decontamination: f(params, "refineEdgeMatte/decontaminationAmount"), + extend_where_smoothed: b(params, "refineEdgeMatte/extendWhereSmoothed"), + increase_radius: f(params, "refineEdgeMatte/increaseDecontaminationRadius").max(0.0), + } +} + +/// Seed of the chain keys: what the frames are (the engine-maintained Input Key), the layer +/// size, the frame rate and the segmentation settings. +pub fn seed(params: &Params, layer_size: [f64; 2], fps: f64) -> u64 { + let mut h = 0xcbf2_9ce4_8422_2325; + fnv(&mut h, s(params, INPUT_KEY).as_bytes()); + for v in [ + layer_size[0], + layer_size[1], + fps, + f(params, "rotoBrushMatte/searchRadius"), + f(params, "rotoBrushMatte/motionThreshold"), + f(params, "rotoBrushMatte/motionDamping"), + ] { + fnv(&mut h, &v.to_bits().to_le_bytes()); + } + fnv(&mut h, &[e(params, "quality") as u8, e(params, "version") as u8]); + h +} + +/// The layer frame shown at layer time `t` seconds. +pub fn frame_of(t: f64, fps: f64) -> i64 { + (t * fps + 1e-6).floor() as i64 +} + +// ------------------------------------------------------------------ derived-data cache + +struct SegCache { + map: HashMap<(u64, u64), Arc>, + order: VecDeque<(u64, u64)>, + bytes: usize, +} + +const CACHE_BYTES: usize = 768 << 20; + +fn cache() -> &'static Mutex { + static C: OnceLock> = OnceLock::new(); + C.get_or_init(|| Mutex::new(SegCache { map: HashMap::new(), order: VecDeque::new(), bytes: 0 })) +} + +/// A cached segmentation (chain key, scale). +pub fn cached(key: u64, scale: f64) -> Option> { + cache().lock().unwrap_or_else(|e| e.into_inner()).map.get(&(key, scale.to_bits())).cloned() +} + +pub fn store(key: u64, scale: f64, seg: Arc) { + let mut c = cache().lock().unwrap_or_else(|e| e.into_inner()); + let k = (key, scale.to_bits()); + if c.map.contains_key(&k) { + return; + } + c.bytes += seg.matte.len() + seg.refine.len(); + c.map.insert(k, seg); + c.order.push_back(k); + while c.bytes > CACHE_BYTES { + let Some(old) = c.order.pop_front() else { break }; + if let Some(s) = c.map.remove(&old) { + c.bytes -= s.matte.len() + s.refine.len(); + } + } +} + +/// Drop the cached segmentations of these chain keys (every scale). +pub fn forget(keys: impl IntoIterator) { + let keys: std::collections::HashSet = keys.into_iter().collect(); + let mut c = cache().lock().unwrap_or_else(|e| e.into_inner()); + let gone: Vec<(u64, u64)> = c.map.keys().filter(|k| keys.contains(&k.0)).copied().collect(); + for k in gone { + if let Some(s) = c.map.remove(&k) { + c.bytes -= s.matte.len() + s.refine.len(); + } + } + c.order.retain(|k| !keys.contains(&k.0)); +} + +/// Drop every cached segmentation (Edit ▸ Purge). +pub fn purge() { + let mut c = cache().lock().unwrap_or_else(|e| e.into_inner()); + c.map.clear(); + c.order.clear(); + c.bytes = 0; +} + +/// The layer-space grid (`round(size · scale)` pixels) of a buffer. +pub fn layer_grid(buf: &Buf, layer_size: [f64; 2]) -> Image { + let s = if buf.scale > 0.0 { buf.scale } else { 1.0 }; + let (w, h) = (((layer_size[0] * s).round() as u32).max(1), ((layer_size[1] * s).round() as u32).max(1)); + let (ox, oy) = (buf.offset[0].round() as i64, buf.offset[1].round() as i64); + let mut img = Image::new(w, h); + img.rows_mut().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + *px = buf.img.get(x as i64 + ox, y as i64 + oy); + } + }); + img +} + +/// Everything needed to compute an instance's segmentations. +pub struct Chain { + pub data: Arc, + pub keys: BTreeMap, + pub opts: SegOpts, + pub refine_radius: f64, +} + +impl Chain { + pub fn new(params: &Params, layer_size: [f64; 2], fps: f64, scale: f64) -> Chain { + let data = data(params); + let keys = data.chain_keys(seed(params, layer_size, fps)); + Chain { refine_radius: data.refine_radius(), data, keys, opts: seg_opts(params, scale) } + } + + /// Frame `f`'s segmentation at this chain's scale: cached, or computed from the nearest + /// cached frame towards the base (full-resolution results serve smaller scales). + /// `frame(g)`: the effect's input on frame g on the layer grid at this scale. + pub fn ensure(&self, f: i64, frame: &mut dyn FnMut(i64) -> Option>, cancel: &dyn Fn() -> bool) -> Option> { + let sc = self.opts.scale; + let lookup = |g: i64| -> Option> { + let k = *self.keys.get(&g)?; + if let Some(s) = cached(k, sc) { + return Some(s); + } + if sc != 1.0 { + let full = cached(k, 1.0)?; + let s = Arc::new(downscale(&full, sc)); + store(k, sc, s.clone()); + return Some(s); + } + None + }; + if let Some(s) = lookup(f) { + return Some(s); + } + let base = self.data.base?; + if !self.data.in_span(f) { + return None; + } + // Walk towards the base to the nearest available frame. + let mut path = vec![f]; + let mut g = f; + let mut start: Option> = None; + while let Some(src) = self.data.source_of(g) { + if let Some(s) = lookup(src) { + start = Some(s); + break; + } + path.push(src); + g = src; + } + path.reverse(); + let mut prev_img: Option> = None; + let mut cur = start; + for g in path { + if cancel() { + return None; + } + let img = frame(g)?; + let strokes = self.data.strokes_at(g); + let seg = match (&cur, g == base) { + (_, true) | (None, _) => rb::segment(&img, &strokes, None, &self.opts, self.refine_radius), + (Some(p), false) => { + let src = self.data.source_of(g)?; + let pimg = match prev_img.take() { + Some(i) => i, + None => frame(src)?, + }; + if pimg.width != img.width || pimg.height != img.height { + return None; + } + rb::propagate(&pimg, p, &img, &strokes, &self.opts, self.refine_radius) + } + }; + let seg = Arc::new(seg); + store(*self.keys.get(&g)?, sc, seg.clone()); + cur = Some(seg); + prev_img = Some(img); + } + cur + } + + /// Whether frame `f` is computed (at full resolution or at `scale`). + pub fn is_cached(&self, f: i64, scale: f64) -> bool { + self.keys.get(&f).is_some_and(|k| cached(*k, 1.0).is_some() || cached(*k, scale).is_some()) + } +} + +/// Nearest-neighbour resampling of a segmentation to another scale. +pub fn downscale(s: &FrameSeg, scale: f64) -> FrameSeg { + let (w, h) = (((s.w as f64) * scale).round().max(1.0) as usize, ((s.h as f64) * scale).round().max(1.0) as usize); + resample(s, w, h) +} + +fn resample(s: &FrameSeg, w: usize, h: usize) -> FrameSeg { + if s.w == w && s.h == h { + return s.clone(); + } + let mut out = FrameSeg { w, h, matte: vec![0; w * h], refine: vec![0; w * h], fg: s.fg.clone(), bg: s.bg.clone() }; + if s.w == 0 || s.h == 0 { + return out; + } + for y in 0..h { + let sy = (((y as f64 + 0.5) * s.h as f64 / h as f64) as usize).min(s.h - 1); + for x in 0..w { + let sx = (((x as f64 + 0.5) * s.w as f64 / w as f64) as usize).min(s.w - 1); + out.matte[y * w + x] = s.matte[sy * s.w + sx]; + out.refine[y * w + x] = s.refine.get(sy * s.w + sx).copied().unwrap_or(0); + } + } + out +} + +/// The final matte of frame `seg` (with its neighbours), on its own grid. +pub fn final_alpha( + img: &Image, + seg: &FrameSeg, + prev: Option<&FrameSeg>, + next: Option<&FrameSeg>, + mp: &MatteParams, + refine_radius: f64, + scale: f64, +) -> Vec { + let rgb = rb::rgb_of(img); + refine::matte_alpha(&rgb, seg, prev, next, mp, refine_radius * scale, scale) +} + +fn needs_neighbours(mp: &MatteParams) -> bool { + mp.rb_chatter > 0.0 || mp.chatter > 0.0 || mp.motion_blur +} + +// ------------------------------------------------------------------ Freeze + +/// Frozen mattes (Freeze): the final 8-bit alpha and the Refine Edge band of every frame of +/// the span at full resolution, valid while `key` matches [`frozen_key`]. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct FrozenCache { + pub key: String, + pub size: [usize; 2], + pub frames: BTreeMap, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct FrozenFrame { + /// Alpha (RLE, base64). + pub a: String, + /// Refine Edge band (RLE, base64). + pub b: String, +} + +impl FrozenCache { + pub fn from_json(s: &str) -> Option { + if s.trim().is_empty() { + return None; + } + serde_json::from_str(s).ok() + } + pub fn to_json(&self) -> String { + serde_json::to_string(self).unwrap_or_default() + } +} + +/// What frozen mattes depend on: every chain key and the matte settings. +pub fn frozen_key(params: &Params, layer_size: [f64; 2], fps: f64) -> String { + let ch = Chain::new(params, layer_size, fps, 1.0); + let mut h = 0xcbf2_9ce4_8422_2325; + for (f, k) in &ch.keys { + fnv(&mut h, &f.to_le_bytes()); + fnv(&mut h, &k.to_le_bytes()); + } + fnv(&mut h, format!("{:?}", matte_params(params)).as_bytes()); + format!("{h:016x}") +} + +/// Whether the instance is frozen (has frozen mattes). +pub fn is_frozen(params: &Params) -> bool { + !s(params, FROZEN).is_empty() +} + +type FrozenParsed = Mutex>>; + +fn frozen(params: &Params) -> Option> { + static C: OnceLock = OnceLock::new(); + let j = s(params, FROZEN); + if j.is_empty() { + return None; + } + let mut h = 0xcbf2_9ce4_8422_2325; + fnv(&mut h, j.as_bytes()); + let c = C.get_or_init(Default::default); + if let Some(d) = c.lock().unwrap_or_else(|e| e.into_inner()).get(&h) { + return Some(d.clone()); + } + let d = Arc::new(FrozenCache::from_json(j)?); + let mut m = c.lock().unwrap_or_else(|e| e.into_inner()); + if m.len() > 8 { + m.clear(); + } + m.insert(h, d.clone()); + Some(d) +} + +/// Encode one frame for [`FrozenCache`]. +pub fn freeze_frame(alpha: &[f32], band: &[u8]) -> FrozenFrame { + let a: Vec = alpha.iter().map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8).collect(); + FrozenFrame { a: rle::encode(&a), b: rle::encode(band) } +} + +// ------------------------------------------------------------------ render + +/// A frame's matte (alpha and band) on the layer grid of `img` at `scale`. +fn matte_for(ctx: &EffectCtx, f: i64, img: &Arc, scale: f64) -> Option<(Vec, Vec, f64)> { + let (w, h) = (img.width as usize, img.height as usize); + // Frozen mattes. + if let Some(fc) = frozen(ctx.params) + && fc.key == frozen_key(ctx.params, ctx.layer_size, ctx.fps()) + && let Some(fr) = fc.frames.get(&f) + { + let a = rle::decode(&fr.a)?; + let bnd = rle::decode(&fr.b)?; + let [fw, fh] = fc.size; + if a.len() == fw * fh && bnd.len() == fw * fh { + let af: Vec = a.iter().map(|v| *v as f32 / 255.0).collect(); + let (alpha, band) = if fw == w && fh == h { + (af, bnd) + } else { + let mut al = vec![0.0f32; w * h]; + let mut bd = vec![0u8; w * h]; + for y in 0..h { + for x in 0..w { + let (sx, sy) = ((x as f64 + 0.5) * fw as f64 / w as f64, (y as f64 + 0.5) * fh as f64 / h as f64); + al[y * w + x] = rb::matting::sample(&af, fw, fh, sx, sy); + bd[y * w + x] = bnd[(sy as usize).min(fh - 1) * fw + (sx as usize).min(fw - 1)]; + } + } + (al, bd) + }; + let rr = data(ctx.params).refine_radius(); + return Some((alpha, band, rr)); + } + } + let host = ctx.env.host; + let chain = Chain::new(ctx.params, ctx.layer_size, ctx.fps(), scale); + let fps = ctx.fps(); + let effects = ctx.env.effect_index; + let layer_size = ctx.layer_size; + let me = img.clone(); + let mut frame = |g: i64| -> Option> { + if g == f { + return Some(me.clone()); + } + let buf = host?.self_at(g as f64 / fps, effects)?; + let im = layer_grid(&buf, layer_size); + (im.width as usize == w && im.height as usize == h).then(|| Arc::new(im)) + }; + let seg = chain.ensure(f, &mut frame, &|| false)?; + let seg = if seg.w == w && seg.h == h { (*seg).clone() } else { resample(&seg, w, h) }; + let mp = matte_params(ctx.params); + let (mut prev, mut next) = (None, None); + if needs_neighbours(&mp) { + for (slot, g) in [(&mut prev, f - 1), (&mut next, f + 1)] { + if chain.data.in_span(g) + && let Some(s) = chain.ensure(g, &mut frame, &|| false) + { + *slot = Some(if s.w == w && s.h == h { (*s).clone() } else { resample(&s, w, h) }); + } + } + } + let alpha = final_alpha(img, &seg, prev.as_ref(), next.as_ref(), &mp, chain.refine_radius, scale); + Some((alpha, seg.refine, chain.refine_radius)) +} + +fn render(ctx: &EffectCtx, buf: Buf) -> Buf { + let d = data(ctx.params); + if d.is_empty() || ctx.adjustment { + return buf; + } + let f = frame_of(ctx.time, ctx.fps()); + let sc = if buf.scale > 0.0 { buf.scale } else { 1.0 }; + let mut out = Buf { img: Image::new(buf.img.width, buf.img.height), offset: buf.offset, scale: buf.scale }; + if !d.in_span(f) { + return out; + } + let img = Arc::new(layer_grid(&buf, ctx.layer_size)); + let Some((alpha, band, rr)) = matte_for(ctx, f, &img, sc) else { return out }; + let (w, h) = (img.width as usize, img.height as usize); + let mp = matte_params(ctx.params); + let mut rgb = rb::rgb_of(&img); + let map = refine::decontaminate(&mut rgb, &alpha, &band, w, h, &mp, rr * sc, sc); + let view_map = b(ctx.params, "refineEdgeMatte/viewDecontaminationMap"); + let (ox, oy) = (buf.offset[0].round() as i64, buf.offset[1].round() as i64); + for y in 0..h { + for x in 0..w { + let (bx, by) = (x as i64 + ox, y as i64 + oy); + if bx < 0 || by < 0 || bx >= out.img.width as i64 || by >= out.img.height as i64 { + continue; + } + let i = y * w + x; + let a_in = img.data[i][3]; + let px = if view_map { + let v = map[i]; + [v, v, v, 1.0] + } else { + let a = alpha[i] * a_in; + [rgb[i][0] * a, rgb[i][1] * a, rgb[i][2] * a, a] + }; + out.img.set(bx as u32, by as u32, px); + } + } + out +} + +/// The final matte of the effect at layer frame `f` on the full-resolution layer grid of +/// `input` (the effect's input buffer at scale 1), computing what is missing. For Freeze and +/// the Layer panel overlays. +pub fn frame_matte( + params: &Params, + layer_size: [f64; 2], + fps: f64, + f: i64, + frame: &mut dyn FnMut(i64) -> Option>, + cancel: &dyn Fn() -> bool, +) -> Option<(Vec, Vec)> { + let chain = Chain::new(params, layer_size, fps, 1.0); + let img = frame(f)?; + let seg = chain.ensure(f, frame, cancel)?; + let mp = matte_params(params); + let (mut prev, mut next) = (None, None); + if needs_neighbours(&mp) { + if chain.data.in_span(f - 1) { + prev = chain.ensure(f - 1, frame, cancel); + } + if chain.data.in_span(f + 1) { + next = chain.ensure(f + 1, frame, cancel); + } + } + let alpha = final_alpha(&img, &seg, prev.as_deref(), next.as_deref(), &mp, chain.refine_radius, 1.0); + Some((alpha, seg.refine.clone())) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EffectEnv, EffectHost, LayerPixels}; + + /// A host serving a moving red disk over a blue/green background as the effect's input. + struct Seq; + fn frame(t: f64) -> Image { + let fi = (t * 25.0 + 1e-6).floor(); + let c = [40.0 + 2.0 * fi, 40.0]; + let mut img = Image::new(100, 80); + for y in 0..80 { + for x in 0..100 { + let d = ((x as f64 + 0.5 - c[0]).hypot(y as f64 + 0.5 - c[1])) - 15.0; + let a = (0.5 - d).clamp(0.0, 1.0) as f32; + let tex = ((x * 7 + y * 13) % 9) as f32 / 60.0; + let fg = [0.85 + tex, 0.2, 0.1]; + let bg = [0.1, 0.4 + tex, 0.7]; + img.set(x, y, [fg[0] * a + bg[0] * (1.0 - a), fg[1] * a + bg[1] * (1.0 - a), fg[2] * a + bg[2] * (1.0 - a), 1.0]); + } + } + img + } + impl EffectHost for Seq { + fn layer(&self, _: u64, _: bool) -> Option { + None + } + fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option> { + None + } + fn self_at(&self, t: f64, _: usize) -> Option { + Some(Buf { img: frame(t), offset: [0.0; 2], scale: 1.0 }) + } + } + + fn params_with(d: &RotoData) -> Params { + let s = &specs()[0]; + let mut params = Params { values: s.params.iter().map(|p| (p.id.to_string(), p.default.clone())).collect() }; + params.values.insert(STROKES.into(), Value::Str(d.to_json())); + params + } + + #[test] + fn renders_the_segmented_disk_on_demand() { + let mut d = RotoData::default(); + d.add(Stroke { kind: StrokeKind::Fg, frame: 0, radius: 3.0, points: vec![[35.0, 40.0], [45.0, 40.0]] }, 20, [0, 24]); + d.add(Stroke { kind: StrokeKind::Bg, frame: 0, radius: 3.0, points: vec![[5.0, 5.0], [95.0, 5.0], [95.0, 75.0]] }, 20, [0, 24]); + let params = params_with(&d); + let host = Seq; + let run = |t: f64| { + let ctx = EffectCtx { + params: ¶ms, + time: t, + layer_size: [100.0, 80.0], + seed: 1, + adjustment: false, + env: EffectEnv { host: Some(&host), frame_rate: 25.0, ..Default::default() }, + }; + crate::apply(&specs()[0], &ctx, Buf { img: frame(t), offset: [0.0; 2], scale: 1.0 }).img + }; + // Frame 5 (propagated through frames 1–4). + let out = run(5.0 / 25.0 + 0.001); + let c = [50.0, 40.0]; + let mut wrong = 0; + for y in 0..80u32 { + for x in 0..100u32 { + let d = (x as f64 + 0.5 - c[0]).hypot(y as f64 + 0.5 - c[1]); + let a = out.data[(y * 100 + x) as usize][3]; + if (d < 14.0 && a < 0.5) || (d > 16.0 && a > 0.5) { + wrong += 1; + } + } + } + assert!(wrong < 20, "{wrong} wrong pixels"); + // Outside the span: transparent. + let mut d2 = (*data(¶ms)).clone(); + d2.span = [0, 3]; + let p2 = params_with(&d2); + let ctx = EffectCtx { + params: &p2, + time: 0.3, + layer_size: [100.0, 80.0], + seed: 1, + adjustment: false, + env: EffectEnv { host: Some(&host), frame_rate: 25.0, ..Default::default() }, + }; + let o = crate::apply(&specs()[0], &ctx, Buf { img: frame(0.3), offset: [0.0; 2], scale: 1.0 }).img; + assert!(o.data.iter().all(|p| p[3] == 0.0)); + } +} diff --git a/crates/engine/src/commands/edit.rs b/crates/engine/src/commands/edit.rs index e7ca4bd7..8ebd78db 100644 --- a/crates/engine/src/commands/edit.rs +++ b/crates/engine/src/commands/edit.rs @@ -525,6 +525,9 @@ fn purge_caches(s: &mut Session, p: &Value) -> Result { s.snapshot = None; s.state.viewer.show_snapshot = false; } + if matches!(what.as_str(), "all" | "image" | "memory" | "memoryAndDisk") { + effectcraft_effects::roto::purge(); + } s.events.push(crate::Event::PurgeCaches); s.toast(format!("Purged {what} cache")); Ok(json!({"purged": what})) diff --git a/crates/engine/src/commands/mod.rs b/crates/engine/src/commands/mod.rs index 4537752f..ac451872 100644 --- a/crates/engine/src/commands/mod.rs +++ b/crates/engine/src/commands/mod.rs @@ -28,6 +28,7 @@ mod prop; pub mod puppet; mod query; mod render_queue; +pub mod roto_cmds; mod settings; mod stubs; mod styles; @@ -129,6 +130,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(track::specs()); v.extend(mask_interp::specs()); v.extend(warp_cmds::specs()); + v.extend(roto_cmds::specs()); v.extend(paint::specs()); v.extend(puppet::specs()); v.extend(settings::specs()); diff --git a/crates/engine/src/commands/roto_cmds.rs b/crates/engine/src/commands/roto_cmds.rs new file mode 100644 index 00000000..5b9db20f --- /dev/null +++ b/crates/engine/src/commands/roto_cmds.rs @@ -0,0 +1,424 @@ +//! Roto Brush & Refine Edge commands: strokes (the Roto Brush and Refine Edge tools in the Layer +//! panel), propagation, the segmentation span, Freeze / Unfreeze, tool options and status. +//! +//! `roto.stroke` records a stroke into the layer's Roto Brush & Refine Edge effect (applying the +//! effect on the first stroke, as After Effects does); the first foreground stroke sets the base +//! frame and a span of 20 frames each side. Strokes on a frozen effect are refused (Unfreeze +//! first). Every edit is one undo step. + +use effectcraft_effects::roto::{self as fx, FROZEN, Stroke, StrokeKind}; +use effectcraft_keyframe::Value as KV; +use effectcraft_project::build::Ids; +use effectcraft_project::{ItemId, Layer, LayerId, PropGroup, Uid}; +use effectcraft_track::roto::{RotoData, iou, rle}; +use serde_json::{Value, json}; + +use super::{CommandSpec, always, b_p, bad, f_p, has_comp, layer_mut, layer_p, str_p}; +use crate::roto::{self as rt, Direction, RotoOptions, RotoTask, VIEWS, is_roto, params_static}; +use crate::{EngineError, Result, Session, cmd, query}; + +pub fn specs() -> Vec { + vec![ + cmd!( + "roto.stroke", + "Roto Brush Stroke", + [], + None, + "{layer?, kind?: fg|bg|refine|refineErase, points: [[x, y], …] (layer pixels), frame? (layer frame; default current), radius? (layer px; default the tool's diameter / 2), effect?}", + has_comp, + stroke + ), + cmd!( + "roto.propagate", + "Propagate Roto Brush", + [], + None, + "{layer?, effect?, direction?: forward|backward|both, to? (layer frame: extend the span), wait?: block until done}", + has_comp, + propagate + ), + cmd!("roto.span", "Set Segmentation Span", [], None, "{layer?, effect?, start?, end? (layer frames)}", has_comp, span), + cmd!("roto.freeze", "Freeze", [], None, "{layer?, effect?, wait?}", has_comp, freeze), + cmd!("roto.unfreeze", "Unfreeze", [], None, "{layer?, effect?}", has_comp, unfreeze), + cmd!("roto.clearStrokes", "Remove Roto Brush Strokes", [], None, "{layer?, effect?, frame? (only this layer frame), kind?}", has_comp, clear_strokes), + cmd!("roto.cancel", "Stop Roto Brush", [], None, "{}", is_running, |s, _| Ok(json!({"stopped": s.stop_roto()}))), + cmd!( + "roto.options", + "Roto Brush Options", + [], + None, + "{diameter?, refineDiameter?, view?: alphaBoundary|alpha|alphaOverlay|none, overlayColor?: [r,g,b], overlayOpacity?, boundaryColor?: [r,g,b], autoPropagate?}", + always, + options + ), + query!( + "roto.status", + "Roto Brush Status", + "{layer?, effect?, frame? (layer frame: also report its matte), matte?: include the matte (RLE, base64), compute?: compute the frame if needed, compareTo?: RLE matte to report the IoU against}", + status + ), + ] +} + +fn is_running(s: &Session) -> std::result::Result<(), String> { + if s.is_roto_running() { Ok(()) } else { Err("no Roto Brush job is running".into()) } +} + +/// The Roto Brush instance targeted by `p`: `layer` / `effect` (uid, name or 1-based index), +/// else a selected one, else the first on the selected (or any) layer. +pub(crate) fn roto_p(s: &Session, p: &Value, cmd: &str) -> Result<(ItemId, LayerId, Uid)> { + let cid = super::comp_id(s, p)?; + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; + let candidates: Vec = match p.get("layer") { + Some(v) => vec![super::resolve_layer(comp, v).ok_or_else(|| bad(cmd, format!("no layer {v}")))?], + None => { + let mut v: Vec = s.state.selected_props.iter().map(|(l, _)| *l).collect(); + v.extend(s.state.selected_layers.iter().copied()); + v.extend(comp.layers.iter().map(|l| l.id)); + v + } + }; + for lid in candidates { + let Some(fxg) = comp.layer(lid).and_then(Layer::effects) else { continue }; + let g = match p.get("effect") { + Some(Value::Number(n)) => { + let n = n.as_u64().unwrap_or(0); + fxg.groups().find(|g| g.uid == n).or_else(|| fxg.groups().nth((n as usize).saturating_sub(1))) + } + Some(Value::String(name)) => fxg.groups().find(|g| &g.name == name), + _ => s + .state + .selected_props + .iter() + .find_map(|(l, u)| (*l == lid).then(|| fxg.groups().find(|g| g.uid == *u && is_roto(g))).flatten()) + .or_else(|| fxg.groups().find(|g| is_roto(g))), + }; + if let Some(g) = g.filter(|g| is_roto(g)) { + return Ok((cid, lid, g.uid)); + } + if p.get("layer").is_some() { + break; + } + } + Err(bad(cmd, "no Roto Brush & Refine Edge effect found (paint a stroke with the Roto Brush tool first)")) +} + +fn group(s: &Session, cid: ItemId, lid: LayerId, uid: Uid) -> Result<(&Layer, &PropGroup)> { + let l = s.project.comp(cid).and_then(|c| c.layer(lid)).ok_or(EngineError::NoComp)?; + let g = l.props.find_group(uid).ok_or_else(|| EngineError::Other("the effect is gone".into()))?; + Ok((l, g)) +} + +fn data_of(g: &PropGroup) -> RotoData { + fx::data(¶ms_static(g)).as_ref().clone() +} + +fn frozen_of(g: &PropGroup) -> bool { + matches!(g.get(FROZEN).map(|p| &p.value), Some(KV::Str(s)) if !s.is_empty()) +} + +/// Write an instance's data (one undo step). +fn write_data(s: &mut Session, label: &str, cid: ItemId, lid: LayerId, uid: Uid, d: &RotoData) -> Result<()> { + let j = d.to_json(); + s.edit(label, None, |proj, _| { + let g = layer_mut(proj, cid, lid)?.props.find_group_mut(uid).ok_or_else(|| EngineError::Other("the effect is gone".into()))?; + if let Some(pr) = g.get_mut(fx::STROKES) { + pr.value = KV::Str(j); + } + Ok(()) + }) +} + +/// Current layer frame of `layer` (or `frame` from the params). +fn frame_p(s: &Session, p: &Value, cid: ItemId, layer: &Layer) -> i64 { + if let Some(f) = f_p(p, "frame") { + return f.round() as i64; + } + let comp = s.project.comp(cid); + let fps = comp.map(|c| c.frame_rate.as_f64()).unwrap_or(30.0); + fx::frame_of(layer.layer_time(s.time_of(cid)).seconds(), fps) +} + +fn points_p(p: &Value, cmd: &str) -> Result> { + let raw = p.get("points").and_then(Value::as_array).ok_or_else(|| bad(cmd, "missing `points` [[x, y], …] (layer pixels)"))?; + let mut v = vec![]; + for q in raw { + let a = q.as_array().ok_or_else(|| bad(cmd, "each point is [x, y]"))?; + let (Some(x), Some(y)) = (a.first().and_then(Value::as_f64), a.get(1).and_then(Value::as_f64)) else { + return Err(bad(cmd, "each point is [x, y]")); + }; + v.push([x, y]); + } + if v.is_empty() { + return Err(bad(cmd, "`points` is empty")); + } + Ok(v) +} + +fn stroke(s: &mut Session, p: &Value) -> Result { + let cmd = "roto.stroke"; + let (cid, lid) = match roto_p(s, p, cmd) { + Ok((c, l, _)) => (c, l), + Err(_) => layer_p(s, p, cmd)?, + }; + let kind = match str_p(p, "kind") { + Some(k) => StrokeKind::from_name(k).ok_or_else(|| bad(cmd, format!("unknown kind `{k}` (fg | bg | refine | refineErase)")))?, + None => StrokeKind::Fg, + }; + let points = points_p(p, cmd)?; + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; + let layer = comp.layer(lid).ok_or_else(|| bad(cmd, "no layer"))?; + if !layer.has_video() || layer.switches.adjustment || layer.effects().is_none() { + return Err(bad(cmd, "the Roto Brush works on footage, solid and composition layers")); + } + let frame = frame_p(s, p, cid, layer); + let limits = rt::frame_limits(comp, layer); + if frame < limits[0] || frame > limits[1] { + return Err(bad(cmd, format!("frame {frame} is outside the layer ({}–{})", limits[0], limits[1]))); + } + let o = &s.state.roto; + let def_r = if matches!(kind, StrokeKind::Refine | StrokeKind::RefineErase) { o.refine_diameter / 2.0 } else { o.diameter / 2.0 }; + let radius = f_p(p, "radius").unwrap_or(def_r).clamp(0.5, 2500.0); + let size = rt::layer_size(&s.project, comp, layer); + // The instance: the requested / first one, else a new one. + let existing = roto_p(s, p, cmd).ok().filter(|(c, l, _)| *c == cid && *l == lid).map(|x| x.2); + if let Some(uid) = existing { + let (_, g) = group(s, cid, lid, uid)?; + if frozen_of(g) { + return Err(bad(cmd, "the Roto Brush is frozen: Unfreeze it to edit strokes")); + } + } + if existing.is_none() && !kind.is_segmentation() { + return Err(bad(cmd, "draw a foreground stroke with the Roto Brush tool first")); + } + let st = Stroke { kind, frame, radius, points }; + let label = if kind.is_segmentation() { "Roto Brush Stroke" } else { "Refine Edge Stroke" }; + let (uid, d) = s.edit(label, None, |proj, _| { + let mut next = proj.next_id; + let l = layer_mut(proj, cid, lid)?; + let fxg = l.props.sub_mut("effects").ok_or_else(|| bad(cmd, "this layer has no effects"))?; + let uid = match existing { + Some(u) => u, + None => { + let spec = effectcraft_effects::find(fx::ID).ok_or_else(|| EngineError::Other("Roto Brush effect missing".into()))?; + let n = fxg.groups().filter(|g| is_roto(g)).count(); + let name = if n == 0 { spec.name.to_string() } else { format!("{} {}", spec.name, n + 1) }; + let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), &name, size); + let u = g.uid; + fxg.children.push(g.into()); + u + } + }; + let g = fxg.find_group_mut(uid).ok_or_else(|| bad(cmd, "effect gone"))?; + let mut d = data_of(g); + d.add(st, fx::DEFAULT_SPAN, limits); + if let Some(pr) = g.get_mut(fx::STROKES) { + pr.value = KV::Str(d.to_json()); + } + proj.next_id = proj.next_id.max(next); + Ok((uid, d)) + })?; + if !s.state.selected_props.iter().any(|(l, u)| *l == lid && *u == uid) { + s.state.selected_layers = vec![lid]; + } + Ok(json!({ + "layer": lid.0, "effect": uid, "frame": frame, "kind": kind, "radius": radius, + "base": d.base, "span": d.span, "strokes": d.strokes.len(), + })) +} + +fn propagate(s: &mut Session, p: &Value) -> Result { + let cmd = "roto.propagate"; + let (cid, lid, uid) = roto_p(s, p, cmd)?; + let dir = match str_p(p, "direction") { + Some(d) => Direction::from_name(d).ok_or_else(|| bad(cmd, format!("unknown direction `{d}` (forward | backward | both)")))?, + None => Direction::Both, + }; + if let Some(to) = f_p(p, "to") { + let to = to.round() as i64; + let (l, g) = group(s, cid, lid, uid)?; + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; + let lim = rt::frame_limits(comp, l); + let mut d = data_of(g); + let to = to.clamp(lim[0], lim[1]); + let before = d.span; + d.span[0] = d.span[0].min(to); + d.span[1] = d.span[1].max(to); + if d.span != before { + if frozen_of(g) { + return Err(bad(cmd, "the Roto Brush is frozen: Unfreeze it to change the span")); + } + write_data(s, "Extend Segmentation Span", cid, lid, uid, &d)?; + } + } + let wait = b_p(p, "wait").unwrap_or(false); + let n = s.start_roto(cid, lid, uid, RotoTask::Propagate, dir, wait).map_err(|e| bad(cmd, e))?; + let mut out = json!({"layer": lid.0, "effect": uid, "frames": n, "running": s.is_roto_running(), "progress": s.roto_progress()}); + if wait { + out["status"] = status(s, &json!({"layer": lid.0, "effect": uid, "comp": cid.0}))?; + } + Ok(out) +} + +fn span(s: &mut Session, p: &Value) -> Result { + let cmd = "roto.span"; + let (cid, lid, uid) = roto_p(s, p, cmd)?; + let (l, g) = group(s, cid, lid, uid)?; + if frozen_of(g) { + return Err(bad(cmd, "the Roto Brush is frozen: Unfreeze it to change the span")); + } + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; + let lim = rt::frame_limits(comp, l); + let mut d = data_of(g); + let base = d.base.ok_or_else(|| bad(cmd, "no strokes yet"))?; + let first = d.strokes.iter().map(|s| s.frame).min().unwrap_or(base); + let last = d.strokes.iter().map(|s| s.frame).max().unwrap_or(base); + if let Some(a) = f_p(p, "start") { + d.span[0] = (a.round() as i64).clamp(lim[0], first); + } + if let Some(b) = f_p(p, "end") { + d.span[1] = (b.round() as i64).clamp(last, lim[1]); + } + write_data(s, "Segmentation Span", cid, lid, uid, &d)?; + Ok(json!({"layer": lid.0, "effect": uid, "span": d.span, "base": base})) +} + +fn freeze(s: &mut Session, p: &Value) -> Result { + let cmd = "roto.freeze"; + let (cid, lid, uid) = roto_p(s, p, cmd)?; + let wait = b_p(p, "wait").unwrap_or(false); + let n = s.start_roto(cid, lid, uid, RotoTask::Freeze, Direction::Both, wait).map_err(|e| bad(cmd, e))?; + let (_, g) = group(s, cid, lid, uid)?; + Ok(json!({"layer": lid.0, "effect": uid, "frames": n, "running": s.is_roto_running(), "frozen": frozen_of(g), "progress": s.roto_progress()})) +} + +fn unfreeze(s: &mut Session, p: &Value) -> Result { + let cmd = "roto.unfreeze"; + let (cid, lid, uid) = roto_p(s, p, cmd)?; + let (_, g) = group(s, cid, lid, uid)?; + if !frozen_of(g) { + return Ok(json!({"layer": lid.0, "effect": uid, "frozen": false})); + } + s.edit("Unfreeze", None, |proj, _| { + let g = layer_mut(proj, cid, lid)?.props.find_group_mut(uid).ok_or_else(|| EngineError::Other("the effect is gone".into()))?; + if let Some(pr) = g.get_mut(FROZEN) { + pr.value = KV::Str(String::new()); + } + Ok(()) + })?; + Ok(json!({"layer": lid.0, "effect": uid, "frozen": false})) +} + +fn clear_strokes(s: &mut Session, p: &Value) -> Result { + let cmd = "roto.clearStrokes"; + let (cid, lid, uid) = roto_p(s, p, cmd)?; + let (_, g) = group(s, cid, lid, uid)?; + if frozen_of(g) { + return Err(bad(cmd, "the Roto Brush is frozen: Unfreeze it to edit strokes")); + } + let mut d = data_of(g); + let frame = f_p(p, "frame").map(|f| f.round() as i64); + let kind = match str_p(p, "kind") { + Some(k) => Some(StrokeKind::from_name(k).ok_or_else(|| bad(cmd, format!("unknown kind `{k}`")))?), + None => None, + }; + let n0 = d.strokes.len(); + d.strokes.retain(|st| !(frame.is_none_or(|f| st.frame == f) && kind.is_none_or(|k| st.kind == k))); + if d.strokes.iter().all(|s| !s.kind.is_segmentation()) { + d.base = None; + } else if d.base.is_some_and(|b| !d.strokes.iter().any(|s| s.frame == b && s.kind.is_segmentation())) { + // The base moves to the earliest remaining stroked frame. + d.base = d.strokes.iter().filter(|s| s.kind.is_segmentation()).map(|s| s.frame).min(); + } + let removed = n0 - d.strokes.len(); + if removed > 0 { + write_data(s, "Remove Roto Brush Strokes", cid, lid, uid, &d)?; + } + Ok(json!({"layer": lid.0, "effect": uid, "removed": removed, "strokes": d.strokes.len(), "base": d.base})) +} + +fn options(s: &mut Session, p: &Value) -> Result { + let cmd = "roto.options"; + let mut o: RotoOptions = s.state.roto.clone(); + for (k, f) in [("diameter", &mut o.diameter as &mut f64), ("refineDiameter", &mut o.refine_diameter), ("overlayOpacity", &mut o.overlay_opacity)] { + if let Some(v) = f_p(p, k) { + *f = v; + } + } + o.diameter = o.diameter.clamp(1.0, 5000.0); + o.refine_diameter = o.refine_diameter.clamp(1.0, 5000.0); + o.overlay_opacity = o.overlay_opacity.clamp(0.0, 100.0); + if let Some(v) = str_p(p, "view") { + let v = VIEWS.iter().find(|x| x.eq_ignore_ascii_case(v)).ok_or_else(|| bad(cmd, format!("unknown view `{v}` ({})", VIEWS.join(" | "))))?; + o.view = v.to_string(); + } + for (k, c) in [("overlayColor", &mut o.overlay_color), ("boundaryColor", &mut o.boundary_color)] { + if let Some(a) = p.get(k).and_then(Value::as_array) { + if a.len() < 3 { + return Err(bad(cmd, format!("`{k}` is [r, g, b] (0–1)"))); + } + for (i, v) in a.iter().take(3).enumerate() { + c[i] = v.as_f64().unwrap_or(0.0).clamp(0.0, 1.0); + } + } + } + if let Some(b) = b_p(p, "autoPropagate") { + o.auto_propagate = b; + } + s.state.roto = o.clone(); + Ok(serde_json::to_value(o).unwrap_or(Value::Null)) +} + +fn status(s: &mut Session, p: &Value) -> Result { + s.poll_roto(false); + let mut out = + json!({"running": s.is_roto_running(), "progress": s.roto_progress(), "banner": s.roto_progress().map(|p| p.banner()), "options": s.state.roto}); + let Ok((cid, lid, uid)) = roto_p(s, p, "roto.status") else { return Ok(out) }; + let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone(); + let (layer, g) = group(s, cid, lid, uid)?; + let (layer, g) = (layer.clone(), g.clone()); + let d = data_of(&g); + let computed = s.roto_computed(cid, lid, uid); + out["layer"] = json!(lid.0); + out["effect"] = json!(uid); + out["base"] = json!(d.base); + out["span"] = json!(d.span); + out["strokes"] = json!(d.strokes.len()); + out["strokeFrames"] = json!(d.stroke_frames()); + out["computed"] = json!(computed.iter().filter(|(_, c)| **c).map(|(f, _)| *f).collect::>()); + out["frozen"] = json!(frozen_of(&g)); + out["pending"] = json!(s.roto_pending.contains(&(cid, lid, uid))); + out["limits"] = json!(rt::frame_limits(&comp, &layer)); + let frame = frame_p(s, p, cid, &layer); + out["frame"] = json!(frame); + let want_matte = b_p(p, "matte").unwrap_or(false); + let compute = b_p(p, "compute").unwrap_or(want_matte); + if (p.get("frame").is_some() || want_matte) && d.in_span(frame) { + let size = rt::layer_size(&s.project, &comp, &layer); + let fps = comp.frame_rate.as_f64(); + let ps = params_static(&g); + let chain = fx::Chain::new(&ps, size, fps, 1.0); + let seg = match chain.keys.get(&frame).and_then(|k| fx::cached(*k, 1.0)) { + Some(sg) => Some(sg), + None if compute => { + let index = layer.effects().and_then(|f| f.groups().position(|x| x.uid == uid)).unwrap_or(0); + let mut src = rt::FrameSource::new(&s.project, s.footage.as_ref(), s.expr.as_deref(), &s.layer_cache, cid, &comp, &layer, index, size); + chain.ensure(frame, &mut |f| src.get(f), &|| false) + } + None => None, + }; + if let Some(sg) = seg { + out["area"] = json!(sg.area()); + out["centroid"] = json!(sg.centroid()); + out["size"] = json!([sg.w, sg.h]); + out["refineArea"] = json!(sg.refine.iter().filter(|v| **v != 0).count()); + if want_matte { + out["matte"] = json!(rle::encode(&sg.matte)); + } + if let Some(gt) = p.get("compareTo").and_then(Value::as_str).and_then(rle::decode) { + out["iou"] = json!(iou(&sg.matte, >)); + } + } + } + Ok(out) +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index ba34488a..fa2e6fe4 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -17,6 +17,7 @@ pub mod mask_track; pub mod menus; pub mod prefs; pub mod render_queue; +pub mod roto; mod session_settings; pub mod shortcuts; pub mod tracking; @@ -41,6 +42,7 @@ pub use effectcraft_project as project; pub use effectcraft_render as render; pub use effectcraft_text as text; pub use effectcraft_time as time; +pub use effectcraft_track as track; pub use render_queue::{ExportJob, ExportResult, Exporter, JobState}; #[derive(Debug, thiserror::Error)] @@ -176,6 +178,9 @@ pub struct EditorState { /// Paint and Brushes panel options (Brush, Clone Stamp and Eraser tools). #[serde(default)] pub paint: commands::paint::PaintOptions, + /// Roto Brush and Refine Edge tool options. + #[serde(default)] + pub roto: roto::RotoOptions, /// Puppet tool options for new meshes. #[serde(default)] pub puppet: commands::puppet::PuppetOptions, @@ -263,6 +268,10 @@ pub struct Session { pub warp_job: Option, /// Warp Stabilizers waiting for (re-)analysis: (comp, layer, effect uid). pub warp_pending: Vec<(ItemId, LayerId, Uid)>, + /// The running (or finished, not yet polled) Roto Brush propagation / Freeze. + pub roto_job: Option, + /// Roto Brush instances edited since their last propagation: (comp, layer, effect uid). + pub roto_pending: Vec<(ItemId, LayerId, Uid)>, pub events: Vec, /// Commands executed: (id, params). pub journal: Vec<(String, Value)>, @@ -308,6 +317,8 @@ impl Default for Session { mask_job: None, warp_job: None, warp_pending: vec![], + roto_job: None, + roto_pending: vec![], events: vec![], journal: vec![], layer_cache: Arc::new(LayerCache::default()), @@ -374,6 +385,12 @@ impl Session { self.warp_pending.push(w); } } + // Roto Brush Input Keys follow the frames; edited instances propagate again. + for r in roto::sync(&before, &mut p) { + if !self.roto_pending.contains(&r) { + self.roto_pending.push(r); + } + } let same = merge.is_some() && merge.map(str::to_string) == self.history.merge_key; if !same { self.history.undo.push((label.to_string(), before)); @@ -551,6 +568,9 @@ impl Session { self.stop_warp(); self.warp_job = None; self.warp_pending.clear(); + self.stop_roto(); + self.roto_job = None; + self.roto_pending.clear(); p.fix_next_id(); self.project = Arc::new(p); self.history = History::default(); @@ -615,3 +635,5 @@ pub fn text_families() -> Vec { } #[cfg(test)] mod tests_paint; +#[cfg(test)] +mod tests_roto; diff --git a/crates/engine/src/roto.rs b/crates/engine/src/roto.rs new file mode 100644 index 00000000..5b2f635e --- /dev/null +++ b/crates/engine/src/roto.rs @@ -0,0 +1,569 @@ +//! Roto Brush & Refine Edge runtime: propagation and Freeze jobs, Input Key maintenance and the +//! tool options. +//! +//! Segmentations are derived data in `effectcraft_effects::roto`'s content-keyed cache (see that +//! module). `roto.propagate` fills it at full resolution in the background (or blocking with +//! `wait`), frame by frame outward from the base frame, rendering the effect's input +//! (`Renderer::layer_input`, source → masks → the effects above it). `roto.freeze` runs the same +//! pass and stores the final mattes in the effect (one undo step). [`sync`], run after every +//! edit, keeps each instance's hidden Input Key equal to a signature of what its frames are made +//! of (the layer's source and timing, its masks and the effects above it) and drops frozen +//! mattes made from other frames. + +use std::collections::{BTreeMap, HashMap}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; + +use effectcraft_effects::roto::{self as fx, FROZEN, FrozenCache, INPUT_KEY}; +use effectcraft_effects::{Params, flatten_params}; +use effectcraft_keyframe::Value; +use effectcraft_project::{GroupKind, ItemId, ItemKind, Layer, LayerId, Project, PropGroup, Uid}; +use effectcraft_raster::Image; +use effectcraft_render::{EvalCtx, ExprHost, FootageSource, LayerCache, RenderOpts, Renderer}; +use effectcraft_time::Tick; +use serde::{Deserialize, Serialize}; + +use crate::tracking::lock; +use crate::{Event, Session}; + +/// Roto Brush / Refine Edge tool options (serde: `roto.options`). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct RotoOptions { + /// Roto Brush tip diameter (layer pixels). + pub diameter: f64, + /// Refine Edge tip diameter (layer pixels). + pub refine_diameter: f64, + /// Layer panel view: `alphaBoundary` (pink outline), `alpha`, `alphaOverlay`, `none`. + pub view: String, + /// Alpha Overlay colour (straight RGB 0–1) and opacity (percent). + pub overlay_color: [f64; 3], + pub overlay_opacity: f64, + /// Alpha Boundary colour. + pub boundary_color: [f64; 3], + /// Start propagating in the background after each stroke. + pub auto_propagate: bool, +} + +impl Default for RotoOptions { + fn default() -> Self { + RotoOptions { + diameter: 20.0, + refine_diameter: 10.0, + view: "alphaBoundary".into(), + overlay_color: [1.0, 0.0, 0.0], + overlay_opacity: 50.0, + boundary_color: [1.0, 0.25, 0.75], + auto_propagate: true, + } + } +} + +pub const VIEWS: [&str; 4] = ["alphaBoundary", "alpha", "alphaOverlay", "none"]; + +/// What a job does. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum RotoTask { + Propagate, + Freeze, +} + +/// Which way to propagate. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Direction { + Forward, + Backward, + #[default] + Both, +} + +impl Direction { + pub fn from_name(s: &str) -> Option { + Some(match s.to_ascii_lowercase().as_str() { + "forward" | "fwd" => Direction::Forward, + "backward" | "back" => Direction::Backward, + "both" | "all" => Direction::Both, + _ => return None, + }) + } +} + +/// Live job progress (serde for agents / the control channel). +#[derive(Clone, Debug, Default, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RotoProgress { + pub task: Option, + pub done: u64, + pub total: u64, + pub elapsed: f64, + pub fps: f64, + pub finished: bool, + pub cancelled: bool, + pub error: Option, +} + +impl RotoProgress { + pub fn banner(&self) -> String { + let pc = (100 * self.done).checked_div(self.total).unwrap_or(0); + match self.task { + Some(RotoTask::Freeze) => format!("Freezing Roto Brush & Refine Edge: {pc}%"), + _ => format!("Propagating Roto Brush: {pc}%"), + } + } +} + +#[derive(Default)] +pub struct RotoShared { + pub cancel: AtomicBool, + pub state: Mutex, + frozen: Mutex>, +} + +pub struct RotoJob { + pub shared: Arc, + thread: Option>, + pub comp: ItemId, + pub layer: LayerId, + pub effect: Uid, + pub task: RotoTask, + /// The strokes the job was started with (a Freeze result is dropped when they changed). + strokes: String, +} + +impl RotoJob { + pub fn is_finished(&self) -> bool { + lock(&self.shared.state).finished + } + pub fn progress(&self) -> RotoProgress { + lock(&self.shared.state).clone() + } +} + +pub(crate) fn is_roto(g: &PropGroup) -> bool { + matches!(&g.kind, GroupKind::Effect { effect } if effect == fx::ID) +} + +fn str_of(g: &PropGroup, m: &str) -> String { + match g.get(m).map(|p| &p.value) { + Some(Value::Str(s)) => s.clone(), + _ => String::new(), + } +} + +fn set_str(g: &mut PropGroup, m: &str, v: String) { + if let Some(p) = g.get_mut(m) { + p.value = Value::Str(v); + p.keys.clear(); + } +} + +fn fnv(s: &str) -> u64 { + let mut h: u64 = 0xcbf2_9ce4_8422_2325; + for b in s.as_bytes() { + h ^= *b as u64; + h = h.wrapping_mul(0x0100_0000_01b3); + } + h +} + +/// What the effect's input frames are made of: the layer's source and timing, its masks and the +/// effects above the Roto Brush. +pub fn input_key(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> String { + let mut s = crate::warp::signature(p, comp, layer); + if let Some(m) = layer.masks() { + s.push_str(&format!("{m:?}")); + } + if let Some(fx) = layer.effects() { + for g in fx.groups() { + if g.uid == uid { + break; + } + s.push_str(&format!("{g:?}")); + } + } + format!("{:016x}", fnv(&s)) +} + +/// After an edit: refresh Input Keys; frozen mattes made from other frames are dropped. +/// Returns the instances whose segmentation changed (to propagate again). +pub(crate) fn sync(before: &Project, after: &mut Project) -> Vec<(ItemId, LayerId, Uid)> { + let mut todo = vec![]; + let mut set = vec![]; + for (cid, it) in &after.items { + let ItemKind::Comp(c) = &it.kind else { continue }; + for l in &c.layers { + let Some(fxg) = l.effects() else { continue }; + for g in fxg.groups().filter(|g| is_roto(g)) { + let key = input_key(after, *cid, l, g.uid); + let old = before.comp(*cid).and_then(|bc| bc.layer(l.id)).and_then(|ol| ol.props.find_group(g.uid)); + if str_of(g, INPUT_KEY) != key { + set.push((*cid, l.id, g.uid, key)); + todo.push((*cid, l.id, g.uid)); + } else if old.is_none_or(|o| str_of(o, fx::STROKES) != str_of(g, fx::STROKES)) { + todo.push((*cid, l.id, g.uid)); + } + } + } + } + for (cid, lid, uid, key) in set { + if let Some(g) = after.comp_mut(cid).and_then(|c| c.layer_mut(lid)).and_then(|l| l.props.find_group_mut(uid)) { + let had = !str_of(g, INPUT_KEY).is_empty(); + set_str(g, INPUT_KEY, key); + if had { + set_str(g, FROZEN, String::new()); + } + } + } + todo.retain(|(c, l, u)| { + after.comp(*c).and_then(|c| c.layer(*l)).and_then(|l| l.props.find_group(*u)).is_some_and(|g| !fx::data(¶ms_static(g)).is_empty()) + }); + todo +} + +/// Parameters of an instance with static values (strokes and keys are never animated). +pub(crate) fn params_static(g: &PropGroup) -> Params { + flatten_params(g, &mut |p| p.value.clone()) +} + +/// Evaluated parameters of an instance at comp time `t`. +pub(crate) fn params_at(p: &Project, cid: ItemId, layer: &Layer, g: &PropGroup, t: Tick, expr: Option<&dyn ExprHost>) -> Params { + match p.comp(cid) { + Some(c) => { + let ctx = EvalCtx { expr, ..EvalCtx::new(p, cid, c, t) }; + flatten_params(g, &mut |pr| ctx.value(layer, pr)) + } + None => params_static(g), + } +} + +/// The layer's source size (layer pixels). +pub(crate) fn layer_size(p: &Project, comp: &effectcraft_project::Comp, layer: &Layer) -> [f64; 2] { + let (w, h) = effectcraft_render::source_size(p, layer); + if w == 0 { [comp.width as f64, comp.height as f64] } else { [w as f64, h as f64] } +} + +/// First and last layer frame of the layer inside the comp (inclusive). +pub fn frame_limits(comp: &effectcraft_project::Comp, layer: &Layer) -> [i64; 2] { + let fps = comp.frame_rate.as_f64().max(1e-6); + let lo = layer.layer_time(layer.in_point.max(Tick::ZERO)).seconds(); + let hi = layer.layer_time(layer.out_point.min(comp.duration)).seconds(); + let a = fx::frame_of(lo, fps); + let b = (fx::frame_of(hi, fps) - 1).max(a); + [a, b] +} + +/// Comp time of layer frame `f`. +pub(crate) fn comp_time_of(comp: &effectcraft_project::Comp, layer: &Layer, f: i64) -> Tick { + layer.comp_time(comp.frame_rate.tick_of(f)) +} + +/// What the worker needs, detached from the session. +struct Work { + project: Arc, + footage: Arc, + expr: Option>, + cache: Arc, + comp: ItemId, + layer: LayerId, + effect_index: usize, + params: Params, + size: [f64; 2], + fps: f64, + frames: Vec, + task: RotoTask, +} + +/// Renders the effect's input frames for a worker, keeping the last few. +pub(crate) struct FrameSource<'a> { + r: Renderer<'a>, + ctx0: EvalCtx<'a>, + comp: &'a effectcraft_project::Comp, + layer: &'a Layer, + index: usize, + size: [f64; 2], + memo: HashMap>, +} + +impl<'a> FrameSource<'a> { + #[allow(clippy::too_many_arguments)] + pub(crate) fn new( + project: &'a Project, + footage: &'a dyn FootageSource, + expr: Option<&'a dyn ExprHost>, + cache: &'a LayerCache, + cid: ItemId, + comp: &'a effectcraft_project::Comp, + layer: &'a Layer, + index: usize, + size: [f64; 2], + ) -> FrameSource<'a> { + let mut r = Renderer::new(project, footage, RenderOpts { scale: 1.0, ..Default::default() }); + r.expr = expr; + r.cache = Some(cache); + let ctx0 = EvalCtx { expr, ..EvalCtx::new(project, cid, comp, Tick::ZERO) }; + FrameSource { r, ctx0, comp, layer, index, size, memo: HashMap::new() } + } + + pub(crate) fn get(&mut self, f: i64) -> Option> { + if let Some(i) = self.memo.get(&f) { + return Some(i.clone()); + } + let t = comp_time_of(self.comp, self.layer, f); + let buf = self.r.layer_input(&self.ctx0.at(t), self.layer, self.index)?; + let buf = if (buf.scale - 1.0).abs() > 1e-9 && buf.scale > 0.0 { + let w = (buf.img.width as f64 / buf.scale).round().max(1.0) as u32; + let h = (buf.img.height as f64 / buf.scale).round().max(1.0) as u32; + effectcraft_effects::Buf { + img: effectcraft_raster::resample(&buf.img, w, h), + offset: [buf.offset[0] / buf.scale, buf.offset[1] / buf.scale], + scale: 1.0, + } + } else { + (*buf).clone() + }; + let img = Arc::new(fx::layer_grid(&buf, self.size)); + if self.memo.len() > 4 { + self.memo.clear(); + } + self.memo.insert(f, img.clone()); + Some(img) + } +} + +fn run_work(w: Work, shared: &RotoShared) { + let t0 = web_time::Instant::now(); + let fail = |msg: &str| { + let mut s = lock(&shared.state); + s.error = Some(msg.to_string()); + s.finished = true; + }; + let Some(comp) = w.project.comp(w.comp) else { return fail("composition missing") }; + let Some(layer) = comp.layer(w.layer) else { return fail("layer missing") }; + let mut src = FrameSource::new(&w.project, w.footage.as_ref(), w.expr.as_deref(), &w.cache, w.comp, comp, layer, w.effect_index, w.size); + { + let mut s = lock(&shared.state); + s.task = Some(w.task); + s.total = w.frames.len() as u64; + } + let cancel = || shared.cancel.load(Ordering::Relaxed); + let chain = fx::Chain::new(&w.params, w.size, w.fps, 1.0); + let mut frozen = FrozenCache { key: fx::frozen_key(&w.params, w.size, w.fps), ..Default::default() }; + for (k, f) in w.frames.iter().enumerate() { + if cancel() { + let mut s = lock(&shared.state); + s.cancelled = true; + s.finished = true; + return; + } + let mut get = |g: i64| src.get(g); + let ok = match w.task { + RotoTask::Propagate => chain.ensure(*f, &mut get, &cancel).is_some(), + RotoTask::Freeze => match fx::frame_matte(&w.params, w.size, w.fps, *f, &mut get, &cancel) { + Some((a, b)) => { + if let Some(img) = src.get(*f) { + frozen.size = [img.width as usize, img.height as usize]; + } + frozen.frames.insert(*f, fx::freeze_frame(&a, &b)); + true + } + None => false, + }, + }; + if !ok && !cancel() { + return fail(&format!("frame {f} could not be segmented")); + } + let mut s = lock(&shared.state); + s.done = k as u64 + 1; + s.elapsed = t0.elapsed().as_secs_f64(); + s.fps = if s.elapsed > 0.0 { s.done as f64 / s.elapsed } else { 0.0 }; + } + if w.task == RotoTask::Freeze { + *lock(&shared.frozen) = Some(frozen); + } + let mut s = lock(&shared.state); + s.elapsed = t0.elapsed().as_secs_f64(); + s.finished = true; +} + +/// Frames to compute, outward from the base frame. +pub(crate) fn job_frames(d: &effectcraft_track::roto::RotoData, dir: Direction) -> Vec { + let Some(base) = d.base else { return vec![] }; + let mut v = vec![base]; + if dir != Direction::Backward { + v.extend(base + 1..=d.span[1]); + } + if dir != Direction::Forward { + v.extend((d.span[0]..base).rev()); + } + v +} + +impl Session { + pub fn is_roto_running(&self) -> bool { + self.roto_job.as_ref().is_some_and(|j| !j.is_finished()) + } + + pub fn roto_progress(&self) -> Option { + self.roto_job.as_ref().map(|j| j.progress()) + } + + /// (comp, layer, effect uid, task) of the running job. + pub fn roto_target(&self) -> Option<(ItemId, LayerId, Uid, RotoTask)> { + self.roto_job.as_ref().map(|j| (j.comp, j.layer, j.effect, j.task)) + } + + pub fn stop_roto(&mut self) -> bool { + match &self.roto_job { + Some(j) if !j.is_finished() => { + j.shared.cancel.store(true, Ordering::Relaxed); + true + } + _ => false, + } + } + + /// Start propagating (or freezing) Roto Brush `effect` on `layer`. `wait`: block until done + /// (wasm32 always does). Returns the number of frames. + pub fn start_roto(&mut self, comp: ItemId, layer: LayerId, effect: Uid, task: RotoTask, dir: Direction, wait: bool) -> Result { + if self.is_roto_running() { + if !wait { + return Err("a Roto Brush job is already running".into()); + } + self.stop_roto(); + if let Some(mut j) = self.roto_job.take() + && let Some(t) = j.thread.take() + { + let _ = t.join(); + } + } + self.poll_roto_job(); + self.roto_pending.retain(|x| *x != (comp, layer, effect)); + let c = self.project.comp(comp).ok_or("no composition")?; + let l = c.layer(layer).ok_or("no layer")?; + let fxg = l.effects().ok_or("the layer has no effects")?; + let index = fxg.groups().position(|g| g.uid == effect).ok_or("no such effect")?; + let g = fxg.groups().nth(index).filter(|g| is_roto(g)).ok_or("not a Roto Brush & Refine Edge effect")?; + let size = layer_size(&self.project, c, l); + let fps = c.frame_rate.as_f64(); + let pstat = params_static(g); + let d = fx::data(&pstat); + if d.is_empty() { + return Err("draw a foreground stroke with the Roto Brush tool first".into()); + } + let t = comp_time_of(c, l, d.base.unwrap_or(0)); + let params = params_at(&self.project, comp, l, g, t, self.expr.as_deref()); + let frames = job_frames(&d, if task == RotoTask::Freeze { Direction::Both } else { dir }); + let n = frames.len(); + let work = Work { + project: self.project.clone(), + footage: self.footage.clone(), + expr: self.expr.clone(), + cache: self.layer_cache.clone(), + comp, + layer, + effect_index: index, + params, + size, + fps, + frames, + task, + }; + let strokes = str_of(g, fx::STROKES); + let wait = wait || cfg!(target_arch = "wasm32"); + let shared = Arc::new(RotoShared::default()); + let thread = if wait { + run_work(work, &shared); + None + } else { + let sh = shared.clone(); + Some(std::thread::Builder::new().name("roto".into()).spawn(move || run_work(work, &sh)).map_err(|e| e.to_string())?) + }; + self.roto_job = Some(RotoJob { shared, thread, comp, layer, effect, task, strokes }); + self.events.push(Event::ProjectChanged { revision: self.revision }); + self.poll_roto_job(); + Ok(n) + } + + /// Finish a done job: Freeze writes the frozen mattes (one undo step). + fn poll_roto_job(&mut self) -> bool { + let Some(job) = &self.roto_job else { return false }; + if !job.is_finished() { + return false; + } + let mut job = self.roto_job.take().expect("job"); + if let Some(t) = job.thread.take() { + let _ = t.join(); + } + let st = job.progress(); + if let Some(e) = &st.error { + self.events.push(Event::Toast { message: format!("Roto Brush: {e}"), error: true }); + self.bump(); + return true; + } + if st.cancelled { + self.events.push(Event::Toast { message: "Roto Brush: stopped".into(), error: false }); + self.bump(); + return true; + } + match job.task { + RotoTask::Propagate => { + self.events.push(Event::Toast { message: format!("Roto Brush: propagated {} frame(s) in {:.1} s", st.done, st.elapsed), error: false }); + // Rendered frames depend on the cache: redraw. + self.bump(); + } + RotoTask::Freeze => { + let Some(fc) = lock(&job.shared.frozen).take() else { return true }; + let (cid, lid, uid, strokes) = (job.comp, job.layer, job.effect, job.strokes.clone()); + let r = self.edit("Freeze", None, |p, _| { + let g = p + .comp_mut(cid) + .and_then(|c| c.layer_mut(lid)) + .and_then(|l| l.props.find_group_mut(uid)) + .ok_or_else(|| crate::EngineError::Other("the Roto Brush effect was removed".into()))?; + if str_of(g, fx::STROKES) != strokes { + return Err(crate::EngineError::Other("the strokes changed while freezing".into())); + } + set_str(g, FROZEN, fc.to_json()); + Ok(()) + }); + let msg = match r { + Ok(()) => format!("Roto Brush: froze {} frame(s) in {:.1} s", fc.frames.len(), st.elapsed), + Err(e) => format!("Roto Brush: {e}"), + }; + self.events.push(Event::Toast { message: msg, error: false }); + } + } + true + } + + /// Frontends call this every frame: finishes jobs and (with `auto`) propagates edited + /// instances in the background. + pub fn poll_roto(&mut self, auto: bool) -> bool { + let changed = self.poll_roto_job(); + if auto && self.roto_job.is_none() && self.state.roto.auto_propagate { + while let Some((c, l, e)) = self.roto_pending.first().copied() { + self.roto_pending.remove(0); + if self.start_roto(c, l, e, RotoTask::Propagate, Direction::Both, false).is_ok() { + return true; + } + } + } + changed + } + + /// The computed frames of an instance (full resolution), by layer frame. + pub fn roto_computed(&self, comp: ItemId, layer: LayerId, effect: Uid) -> BTreeMap { + let mut out = BTreeMap::new(); + let Some(c) = self.project.comp(comp) else { return out }; + let Some(l) = c.layer(layer) else { return out }; + let Some(g) = l.props.find_group(effect) else { return out }; + let ps = params_static(g); + let ch = fx::Chain::new(&ps, layer_size(&self.project, c, l), c.frame_rate.as_f64(), 1.0); + for f in ch.keys.keys() { + out.insert(*f, ch.is_cached(*f, 1.0)); + } + out + } +} diff --git a/crates/engine/src/tests_roto.rs b/crates/engine/src/tests_roto.rs new file mode 100644 index 00000000..79403626 --- /dev/null +++ b/crates/engine/src/tests_roto.rs @@ -0,0 +1,288 @@ +//! Roto Brush & Refine Edge end to end through the commands, on synthetic footage: a textured +//! disk moving over a textured background. + +use effectcraft_project::LayerId; +use effectcraft_raster::Image; +use effectcraft_render::RenderOpts; +use effectcraft_track::roto::rle; +use serde_json::{Value, json}; + +use crate::Session; +use crate::tests_track::{H, W, frame_time, setup}; + +const R: f64 = 40.0; + +fn noise(x: f64, y: f64, seed: u32) -> f64 { + fn h(i: i64, j: i64, s: u32) -> f64 { + let mut v = (i.wrapping_mul(73856093) ^ j.wrapping_mul(19349663) ^ (s as i64).wrapping_mul(83492791)) as u64; + v ^= v >> 13; + v = v.wrapping_mul(0x5bd1e995); + v ^= v >> 15; + (v % 10_000) as f64 / 10_000.0 + } + let mut out = 0.0; + for (cell, amp) in [(9.0, 0.6), (3.5, 0.4)] { + let (fx, fy) = (x / cell, y / cell); + let (i, j) = (fx.floor() as i64, fy.floor() as i64); + let (tx, ty) = (fx - i as f64, fy - j as f64); + let (sx, sy) = (tx * tx * (3.0 - 2.0 * tx), ty * ty * (3.0 - 2.0 * ty)); + let a = h(i, j, seed) + (h(i + 1, j, seed) - h(i, j, seed)) * sx; + let b = h(i, j + 1, seed) + (h(i + 1, j + 1, seed) - h(i, j + 1, seed)) * sx; + out += amp * (a + (b - a) * sy); + } + out +} + +/// Disk centre on frame `f`. +fn center(f: u32) -> [f64; 2] { + let t = f as f64; + [90.0 + 4.0 * t, 110.0 + 1.5 * t + 5.0 * (t * 0.3).sin()] +} + +/// Disk centre with a jump at frame 8 (propagation loses it there). +fn jumping(f: u32) -> [f64; 2] { + if f < 8 { center(f) } else { [230.0 + 2.0 * (f - 8) as f64, 150.0] } +} + +fn draw(c: [f64; 2]) -> Image { + let mut img = Image::new(W, H); + for y in 0..H { + for x in 0..W { + let p = [x as f64 + 0.5, y as f64 + 0.5]; + let d = (p[0] - c[0]).hypot(p[1] - c[1]); + let a = (R + 0.5 - d).clamp(0.0, 1.0) as f32; + let n = noise(p[0] - c[0], p[1] - c[1], 11); + let f = [(0.75 + 0.25 * n) as f32, (0.35 + 0.3 * n) as f32, (0.1 + 0.15 * n) as f32]; + let (bn, bm) = (noise(p[0], p[1], 5), noise(p[0] * 1.7, p[1] * 1.7, 9)); + let b = [(0.05 + 0.25 * bm) as f32, (0.3 + 0.4 * bn) as f32, (0.45 + 0.4 * bm) as f32]; + img.set(x, y, [f[0] * a + b[0] * (1.0 - a), f[1] * a + b[1] * (1.0 - a), f[2] * a + b[2] * (1.0 - a), 1.0]); + } + } + img +} + +fn gt(c: [f64; 2]) -> String { + let m: Vec = (0..W * H).map(|i| (((i % W) as f64 + 0.5 - c[0]).hypot((i / W) as f64 + 0.5 - c[1]) <= R) as u8).collect(); + rle::encode(&m) +} + +fn line(a: [f64; 2], b: [f64; 2]) -> Value { + json!((0..=8).map(|k| [a[0] + (b[0] - a[0]) * k as f64 / 8.0, a[1] + (b[1] - a[1]) * k as f64 / 8.0]).collect::>()) +} + +/// A session with the clip (the Track solid removed) and foreground / background strokes on +/// frame 0. `name`: the footage file name (segmentations are cached process-wide by content key, +/// which includes the footage's identity: different clips need different names). +fn painted(path: fn(u32) -> [f64; 2], name: &str) -> (Session, LayerId, u64) { + let (mut s, clip, solid) = setup(move |f| draw(path(f))); + let cid = s.active_comp_id().unwrap(); + s.edit("remove solid", None, |p, _| { + p.comp_mut(cid).unwrap().layers.retain(|l| l.id != solid); + for it in p.items.values_mut() { + if let effectcraft_project::ItemKind::Footage(f) = &mut it.kind { + f.path = name.into(); + } + } + Ok(()) + }) + .unwrap(); + s.execute("time.set", json!({"time": 0})).unwrap(); + let c = path(0); + let r = + s.execute_checked("roto.stroke", json!({"layer": clip.0, "points": line([c[0] - 22.0, c[1] - 8.0], [c[0] + 22.0, c[1] + 8.0]), "radius": 6})).unwrap(); + let uid = r["effect"].as_u64().unwrap(); + assert_eq!(r["base"], 0); + assert_eq!(r["span"], json!([0, 20])); + for (a, b) in [([c[0] - 60.0, c[1] - 58.0], [c[0] + 70.0, c[1] - 58.0]), ([c[0] + 70.0, c[1] + 58.0], [c[0] - 60.0, c[1] + 58.0])] { + s.execute_checked("roto.stroke", json!({"layer": clip.0, "kind": "bg", "points": line(a, b), "radius": 6})).unwrap(); + } + (s, clip, uid) +} + +/// Drop an instance's derived segmentations from the process-wide cache. +fn forget(s: &Session, clip: LayerId, uid: u64) { + let comp = s.active_comp().unwrap(); + let l = comp.layer(clip).unwrap(); + let g = l.props.find_group(uid).unwrap(); + let size = crate::roto::layer_size(&s.project, comp, l); + let ch = effectcraft_effects::roto::Chain::new(&crate::roto::params_static(g), size, comp.frame_rate.as_f64(), 1.0); + effectcraft_effects::roto::forget(ch.keys.values().copied()); +} + +fn iou_at(s: &mut Session, clip: LayerId, f: u32, c: [f64; 2]) -> f64 { + let st = s.execute("roto.status", json!({"layer": clip.0, "frame": f, "compute": true, "compareTo": gt(c)})).unwrap(); + st["iou"].as_f64().unwrap_or_else(|| panic!("no matte on frame {f}: {st}")) +} + +fn alpha_at(s: &Session, f: u32) -> Vec { + let img = s.render(s.active_comp_id().unwrap(), frame_time(f), RenderOpts { scale: 1.0, ..Default::default() }); + img.data.iter().map(|p| p[3]).collect() +} + +#[test] +fn roto_brush_segments_and_propagates() { + let (mut s, clip, uid) = painted(center, "disk-a.mov"); + let l = s.active_comp().unwrap().layer(clip).unwrap().clone(); + assert_eq!(l.effects().unwrap().groups().filter(|g| crate::roto::is_roto(g)).count(), 1, "one effect for all strokes"); + let j0 = iou_at(&mut s, clip, 0, center(0)); + assert!(j0 > 0.95, "base frame IoU {j0}"); + // Propagate the span (blocking). + let r = s.execute_checked("roto.propagate", json!({"layer": clip.0, "direction": "both", "wait": true})).unwrap(); + assert_eq!(r["frames"], 21, "{r}"); + assert_eq!(r["status"]["computed"].as_array().unwrap().len(), 21); + let mut worst: f64 = 1.0; + for f in 0..=20 { + worst = worst.min(iou_at(&mut s, clip, f, center(f))); + } + assert!(worst > 0.9, "worst IoU over the span {worst}"); + // The render applies the matte; outside the span the layer is transparent. + let a = alpha_at(&s, 12); + let c = center(12); + let mut wrong = 0; + for (i, v) in a.iter().enumerate() { + let d = (((i as u32 % W) as f64 + 0.5 - c[0]).hypot((i as u32 / W) as f64 + 0.5 - c[1])) - R; + if (d < -3.0 && *v < 0.5) || (d > 3.0 && *v > 0.5) { + wrong += 1; + } + } + assert!(wrong < 150, "{wrong} wrong pixels in the render"); + assert!(alpha_at(&s, 23).iter().all(|v| *v == 0.0)); + // Extending the span propagates further. + let r = s.execute("roto.propagate", json!({"layer": clip.0, "direction": "forward", "to": 24, "wait": true})).unwrap(); + assert_eq!(r["status"]["span"], json!([0, 24])); + assert!(iou_at(&mut s, clip, 24, center(24)) > 0.9); + // Undo / redo of strokes and the span. + let n = |s: &Session| { + let st = s.active_comp().unwrap().layer(clip).unwrap().props.find_group(uid).unwrap().get(effectcraft_effects::roto::STROKES).unwrap().value.clone(); + match st { + effectcraft_keyframe::Value::Str(j) => effectcraft_track::roto::RotoData::from_json(&j), + _ => panic!(), + } + }; + assert_eq!(n(&s).span, [0, 24]); + assert!(s.undo()); + assert_eq!(n(&s).span, [0, 20]); + assert_eq!(n(&s).strokes.len(), 3); + assert!(s.undo()); + assert_eq!(n(&s).strokes.len(), 2); + assert!(s.redo()); + assert!(s.redo()); + assert_eq!(n(&s).strokes.len(), 3); + assert_eq!(n(&s).span, [0, 24]); + // Serde keeps the strokes; the reopened project renders the same matte. + let back = effectcraft_project::Project::from_json(&s.project.to_json()).unwrap(); + let g = back.comp(s.active_comp_id().unwrap()).unwrap().layer(clip).unwrap().props.find_group(uid).unwrap().clone(); + assert_eq!( + crate::roto::params_static(&g).get("strokes"), + crate::roto::params_static(s.active_comp().unwrap().layer(clip).unwrap().props.find_group(uid).unwrap()).get("strokes") + ); + // Bad parameters are reported. + assert!(s.execute_checked("roto.stroke", json!({"layer": clip.0, "kind": "sideways", "points": [[1, 1]]})).is_err()); + assert!(s.execute_checked("roto.propagate", json!({"layer": clip.0, "bogus": 1})).is_err()); + assert!(s.execute_checked("roto.stroke", json!({"layer": clip.0, "points": [[1, 1]], "frame": 99})).is_err()); +} + +#[test] +fn correction_strokes_restart_propagation() { + let (mut s, clip, _) = painted(jumping, "disk-jump.mov"); + s.execute("roto.propagate", json!({"layer": clip.0, "wait": true})).unwrap(); + assert!(iou_at(&mut s, clip, 6, jumping(6)) > 0.9); + let bad = iou_at(&mut s, clip, 8, jumping(8)); + assert!(bad < 0.5, "the jump should break propagation ({bad})"); + // Correct frame 8: paint the disk in and its old place out. + let (c, old) = (jumping(8), center(8)); + s.execute("roto.stroke", json!({"layer": clip.0, "frame": 8, "points": line([c[0] - 20.0, c[1]], [c[0] + 20.0, c[1]]), "radius": 6})).unwrap(); + s.execute( + "roto.stroke", + json!({"layer": clip.0, "frame": 8, "kind": "bg", "points": line([old[0] - 15.0, old[1] - 15.0], [old[0] + 15.0, old[1] + 15.0]), "radius": 8}), + ) + .unwrap(); + let st = s.execute("roto.status", json!({"layer": clip.0})).unwrap(); + let computed: Vec = serde_json::from_value(st["computed"].clone()).unwrap(); + assert!(computed.contains(&7) && !computed.contains(&8) && !computed.contains(&9), "frames from the correction on are recomputed: {computed:?}"); + assert_eq!(st["strokeFrames"], json!([0, 8])); + s.execute("roto.propagate", json!({"layer": clip.0, "direction": "forward", "wait": true})).unwrap(); + for f in 8..=20 { + let j = iou_at(&mut s, clip, f, jumping(f)); + assert!(j > 0.9, "frame {f}: IoU {j}"); + } + // Clearing the correction brings the bad frame back. + let r = s.execute("roto.clearStrokes", json!({"layer": clip.0, "frame": 8})).unwrap(); + assert_eq!(r["removed"], 2); + assert!(iou_at(&mut s, clip, 8, jumping(8)) < 0.5); +} + +#[test] +fn freeze_caches_the_mattes() { + let (mut s, clip, uid) = painted(center, "disk-freeze.mov"); + // A refine band along the edge on the base frame. + let c = center(0); + let ring: Vec<[f64; 2]> = (0..=40).map(|k| k as f64 / 40.0 * std::f64::consts::TAU).map(|a| [c[0] + R * a.cos(), c[1] + R * a.sin()]).collect(); + s.execute_checked("roto.stroke", json!({"layer": clip.0, "kind": "refine", "points": ring, "radius": 5})).unwrap(); + s.execute("roto.span", json!({"layer": clip.0, "end": 3})).unwrap(); + let live: Vec> = (0..=3).map(|f| alpha_at(&s, f)).collect(); + // Freeze in the background and wait for it. + let r = s.execute_checked("roto.freeze", json!({"layer": clip.0})).unwrap(); + assert_eq!(r["frames"], 4); + let t0 = std::time::Instant::now(); + while s.roto_job.is_some() { + assert!(t0.elapsed().as_secs() < 300); + std::thread::sleep(std::time::Duration::from_millis(5)); + s.poll_roto(false); + } + let st = s.execute("roto.status", json!({"layer": clip.0})).unwrap(); + assert_eq!(st["frozen"], true, "{st}"); + // Frozen renders match the live ones (8-bit alpha), even without the derived segmentations. + forget(&s, clip, uid); + s.layer_cache = Default::default(); + for (f, want) in live.iter().enumerate() { + let got = alpha_at(&s, f as u32); + let worst = got.iter().zip(want).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(worst <= 1.0 / 255.0 + 1e-4, "frame {f}: {worst}"); + } + assert!(st["computed"].as_array().unwrap().len() >= 4); + // Frozen effects refuse strokes; Unfreeze is undoable. + assert!(s.execute("roto.stroke", json!({"layer": clip.0, "points": [[10, 10]]})).is_err()); + s.execute("roto.unfreeze", json!({"layer": clip.0})).unwrap(); + assert_eq!(s.execute("roto.status", json!({"layer": clip.0})).unwrap()["frozen"], false); + assert!(s.undo()); + assert_eq!(s.execute("roto.status", json!({"layer": clip.0})).unwrap()["frozen"], true); + // Changing the source frames (the layer's timing) drops the frozen mattes. + let cid = s.active_comp_id().unwrap(); + s.edit("shift", None, |p, _| { + let l = p.comp_mut(cid).unwrap().layer_mut(clip).unwrap(); + l.start_time = frame_time(1); + Ok(()) + }) + .unwrap(); + assert_eq!(s.execute("roto.status", json!({"layer": clip.0, "effect": uid})).unwrap()["frozen"], false); +} + +#[test] +fn propagation_is_deterministic_and_runs_in_background() { + let (mut s, clip, uid) = painted(center, "disk-bg.mov"); + s.execute("roto.span", json!({"layer": clip.0, "end": 5})).unwrap(); + s.execute("roto.propagate", json!({"layer": clip.0})).unwrap(); + let t0 = std::time::Instant::now(); + while s.roto_job.is_some() { + assert!(t0.elapsed().as_secs() < 300); + std::thread::sleep(std::time::Duration::from_millis(5)); + s.poll_roto(false); + } + let st = s.execute("roto.status", json!({"layer": clip.0})).unwrap(); + assert_eq!(st["computed"], json!([0, 1, 2, 3, 4, 5]), "{st}"); + let a = s.execute("roto.status", json!({"layer": clip.0, "frame": 5, "matte": true})).unwrap()["matte"].clone(); + // Recomputing from scratch gives the same matte. + forget(&s, clip, uid); + let b = s.execute("roto.status", json!({"layer": clip.0, "frame": 5, "matte": true})).unwrap()["matte"].clone(); + assert!(a.is_string()); + assert_eq!(a, b); + // Tool options. + let o = s.execute_checked("roto.options", json!({"diameter": 30, "view": "alphaOverlay"})).unwrap(); + assert_eq!(o["view"], "alphaOverlay"); + assert_eq!(s.state.roto.diameter, 30.0); + assert!(s.execute("roto.options", json!({"view": "sideways"})).is_err()); + // The default radius follows the tool diameter. + let r = s.execute("roto.stroke", json!({"layer": clip.0, "points": [[90, 110]]})).unwrap(); + assert_eq!(r["radius"], 15.0); +} diff --git a/crates/track/Cargo.toml b/crates/track/Cargo.toml index ba449c92..70e08ae0 100644 --- a/crates/track/Cargo.toml +++ b/crates/track/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "effectcraft-track" -description = "Motion tracking: feature/search region point tracking (pyramid normalized cross-correlation + Lucas-Kanade refinement), confidence, and corner-pin / transform solves" +description = "Motion tracking: feature/search region point tracking (pyramid normalized cross-correlation + Lucas-Kanade refinement), confidence, corner-pin / transform solves, stabilization and Roto Brush segmentation (graph cut, matting)" version.workspace = true edition.workspace = true license.workspace = true diff --git a/crates/track/src/lib.rs b/crates/track/src/lib.rs index d7b239f5..629097cf 100644 --- a/crates/track/src/lib.rs +++ b/crates/track/src/lib.rs @@ -22,6 +22,7 @@ pub mod fit; pub mod klt; pub mod mask; pub mod plane; +pub mod roto; pub mod solve; pub mod stabilize; diff --git a/crates/track/src/roto/gmm.rs b/crates/track/src/roto/gmm.rs new file mode 100644 index 00000000..c0fe95b0 --- /dev/null +++ b/crates/track/src/roto/gmm.rs @@ -0,0 +1,228 @@ +//! Gaussian mixture colour models (full 3 × 3 covariances), as used by GrabCut (C. Rother, +//! V. Kolmogorov and A. Blake, "GrabCut: Interactive Foreground Extraction using Iterated Graph +//! Cuts", SIGGRAPH 2004). +//! +//! Fitting is deterministic: components are initialised by recursive splitting of the cluster +//! with the largest variance along its principal axis (Orchard & Bouman, "Color Quantization of +//! Images", IEEE TSP 1991), then refined by a few hard-assignment EM iterations. + +use serde::{Deserialize, Serialize}; + +/// One Gaussian: weight, mean, inverse covariance and log normaliser. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Gauss { + pub weight: f64, + pub mean: [f64; 3], + pub inv: [[f64; 3]; 3], + /// −log(weight) + ½·log(det Σ) (the 2π term is dropped: it cancels between models). + pub log_norm: f64, +} + +/// A colour model. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct Gmm { + pub comps: Vec, +} + +/// Regularisation added to every covariance's diagonal (colour noise floor). +const REG: f64 = 2e-4; + +fn det3(m: &[[f64; 3]; 3]) -> f64 { + m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]) +} + +fn inv3(m: &[[f64; 3]; 3]) -> Option<[[f64; 3]; 3]> { + let d = det3(m); + if d.abs() < 1e-30 { + return None; + } + let mut r = [[0.0; 3]; 3]; + r[0][0] = (m[1][1] * m[2][2] - m[1][2] * m[2][1]) / d; + r[0][1] = (m[0][2] * m[2][1] - m[0][1] * m[2][2]) / d; + r[0][2] = (m[0][1] * m[1][2] - m[0][2] * m[1][1]) / d; + r[1][0] = (m[1][2] * m[2][0] - m[1][0] * m[2][2]) / d; + r[1][1] = (m[0][0] * m[2][2] - m[0][2] * m[2][0]) / d; + r[1][2] = (m[0][2] * m[1][0] - m[0][0] * m[1][2]) / d; + r[2][0] = (m[1][0] * m[2][1] - m[1][1] * m[2][0]) / d; + r[2][1] = (m[0][1] * m[2][0] - m[0][0] * m[2][1]) / d; + r[2][2] = (m[0][0] * m[1][1] - m[0][1] * m[1][0]) / d; + Some(r) +} + +/// Sufficient statistics of a cluster. +#[derive(Clone, Copy, Default)] +struct Stats { + n: f64, + s: [f64; 3], + ss: [[f64; 3]; 3], +} + +impl Stats { + fn add(&mut self, c: [f32; 3]) { + let c = [c[0] as f64, c[1] as f64, c[2] as f64]; + self.n += 1.0; + for i in 0..3 { + self.s[i] += c[i]; + for j in 0..3 { + self.ss[i][j] += c[i] * c[j]; + } + } + } + fn mean(&self) -> [f64; 3] { + let n = self.n.max(1.0); + [self.s[0] / n, self.s[1] / n, self.s[2] / n] + } + fn cov(&self) -> [[f64; 3]; 3] { + let n = self.n.max(1.0); + let m = self.mean(); + let mut c = [[0.0; 3]; 3]; + for i in 0..3 { + for j in 0..3 { + c[i][j] = self.ss[i][j] / n - m[i] * m[j]; + } + c[i][i] += REG; + } + c + } +} + +/// Principal eigenvector and eigenvalue of a symmetric 3 × 3 matrix (power iteration). +fn principal(c: &[[f64; 3]; 3]) -> ([f64; 3], f64) { + let mut v = [0.577, 0.577, 0.577]; + let mut lambda = 0.0; + for _ in 0..40 { + let w = [ + c[0][0] * v[0] + c[0][1] * v[1] + c[0][2] * v[2], + c[1][0] * v[0] + c[1][1] * v[1] + c[1][2] * v[2], + c[2][0] * v[0] + c[2][1] * v[1] + c[2][2] * v[2], + ]; + let n = (w[0] * w[0] + w[1] * w[1] + w[2] * w[2]).sqrt(); + if n < 1e-30 { + break; + } + lambda = n; + v = [w[0] / n, w[1] / n, w[2] / n]; + } + (v, lambda) +} + +impl Gmm { + /// Fit `k` components to `samples` (at most `max_samples` are used, taken at a fixed stride). + pub fn fit(samples: &[[f32; 3]], k: usize, max_samples: usize) -> Gmm { + if samples.is_empty() { + return Gmm::default(); + } + let stride = samples.len().div_ceil(max_samples.max(1)).max(1); + let xs: Vec<[f32; 3]> = samples.iter().step_by(stride).copied().collect(); + // Orchard–Bouman splitting. + let mut label = vec![0usize; xs.len()]; + let mut ncl = 1; + while ncl < k { + let mut st = vec![Stats::default(); ncl]; + for (x, l) in xs.iter().zip(&label) { + st[*l].add(*x); + } + let (mut best, mut best_l, mut axis) = (usize::MAX, 0.0, [0.0; 3]); + for (c, s) in st.iter().enumerate() { + if s.n < 2.0 { + continue; + } + let (v, l) = principal(&s.cov()); + if l > best_l { + best = c; + best_l = l; + axis = v; + } + } + if best == usize::MAX || best_l <= REG * 1.01 { + break; + } + let m = st[best].mean(); + let thr = axis[0] * m[0] + axis[1] * m[1] + axis[2] * m[2]; + for (x, l) in xs.iter().zip(label.iter_mut()) { + if *l == best && axis[0] * x[0] as f64 + axis[1] * x[1] as f64 + axis[2] * x[2] as f64 > thr { + *l = ncl; + } + } + ncl += 1; + } + let mut g = Gmm::from_labels(&xs, &label, ncl); + // Hard EM. + for _ in 0..3 { + for (x, l) in xs.iter().zip(label.iter_mut()) { + *l = g.best(*x); + } + let ng = Gmm::from_labels(&xs, &label, g.comps.len()); + if ng.comps.is_empty() { + break; + } + g = ng; + } + g + } + + fn from_labels(xs: &[[f32; 3]], label: &[usize], k: usize) -> Gmm { + let mut st = vec![Stats::default(); k]; + for (x, l) in xs.iter().zip(label) { + st[*l].add(*x); + } + let total: f64 = st.iter().map(|s| s.n).sum::().max(1.0); + let comps = st + .iter() + .filter(|s| s.n >= 1.0) + .filter_map(|s| { + let c = s.cov(); + let inv = inv3(&c)?; + let det = det3(&c).max(1e-300); + let weight = s.n / total; + Some(Gauss { weight, mean: s.mean(), inv, log_norm: -weight.ln() + 0.5 * det.ln() }) + }) + .collect(); + Gmm { comps } + } + + #[inline] + fn comp_cost(g: &Gauss, c: [f32; 3]) -> f64 { + let d = [c[0] as f64 - g.mean[0], c[1] as f64 - g.mean[1], c[2] as f64 - g.mean[2]]; + let mut q = 0.0; + for i in 0..3 { + q += d[i] * (g.inv[i][0] * d[0] + g.inv[i][1] * d[1] + g.inv[i][2] * d[2]); + } + g.log_norm + 0.5 * q + } + + /// The most likely component of colour `c`. + pub fn best(&self, c: [f32; 3]) -> usize { + let mut b = 0; + let mut bv = f64::INFINITY; + for (i, g) in self.comps.iter().enumerate() { + let v = Self::comp_cost(g, c); + if v < bv { + bv = v; + b = i; + } + } + b + } + + /// −log p(c) (up to a constant shared by all models), clamped to a finite range. + pub fn cost(&self, c: [f32; 3]) -> f64 { + if self.comps.is_empty() { + return 10.0; + } + // log-sum-exp over components. + let mut m = f64::INFINITY; + let mut costs = [0.0f64; 16]; + let n = self.comps.len().min(16); + for (i, g) in self.comps.iter().take(n).enumerate() { + costs[i] = Self::comp_cost(g, c); + m = m.min(costs[i]); + } + let s: f64 = costs[..n].iter().map(|v| (m - v).exp()).sum(); + (m - s.ln()).clamp(-20.0, 60.0) + } + + pub fn is_empty(&self) -> bool { + self.comps.is_empty() + } +} diff --git a/crates/track/src/roto/matting.rs b/crates/track/src/roto/matting.rs new file mode 100644 index 00000000..439d9c43 --- /dev/null +++ b/crates/track/src/roto/matting.rs @@ -0,0 +1,465 @@ +//! Matte utilities: exact Euclidean distance transforms (Felzenszwalb & Huttenlocher, +//! "Distance Transforms of Sampled Functions", Theory of Computing 8, 2012), box and Gaussian +//! filters, the colour **guided filter** (K. He, J. Sun and X. Tang, "Guided Image Filtering", +//! ECCV 2010 / IEEE PAMI 2013) used for edge matting, foreground colour estimation +//! (decontamination, from the compositing equation I = αF + (1 − α)B with B and F estimated from +//! nearby pure background / foreground pixels), and the Refine Edge matte adjustments. + +use rayon::prelude::*; + +const BIG: f32 = 1e12; + +/// 1-D squared distance transform of `f` (lower envelope of parabolas). +fn dt1(f: &[f32], d: &mut [f32], v: &mut [usize], z: &mut [f64]) { + let n = f.len(); + if n == 0 { + return; + } + let ff = |q: usize| f[q] as f64 + (q * q) as f64; + let mut k = 0usize; + v[0] = 0; + z[0] = f64::NEG_INFINITY; + z[1] = f64::INFINITY; + for q in 1..n { + let mut s = (ff(q) - ff(v[k])) / (2.0 * (q as f64 - v[k] as f64)); + while s <= z[k] { + if k == 0 { + break; + } + k -= 1; + s = (ff(q) - ff(v[k])) / (2.0 * (q as f64 - v[k] as f64)); + } + k += 1; + v[k] = q; + z[k] = s; + z[k + 1] = f64::INFINITY; + } + k = 0; + for (q, dq) in d.iter_mut().enumerate().take(n) { + while z[k + 1] < q as f64 { + k += 1; + } + let p = v[k]; + let dd = q as f64 - p as f64; + *dq = (dd * dd + f[p] as f64) as f32; + } +} + +/// Euclidean distance from every pixel to the nearest pixel where `seed` is true (BIG.sqrt() +/// when there is none). +pub fn distance(seed: &[bool], w: usize, h: usize) -> Vec { + if w == 0 || h == 0 { + return vec![]; + } + let mut g: Vec = seed.iter().map(|s| if *s { 0.0 } else { BIG }).collect(); + // Columns. + let cols: Vec> = (0..w) + .into_par_iter() + .map(|x| { + let f: Vec = (0..h).map(|y| g[y * w + x]).collect(); + let mut d = vec![0.0; h]; + let (mut v, mut z) = (vec![0usize; h], vec![0.0f64; h + 1]); + dt1(&f, &mut d, &mut v, &mut z); + d + }) + .collect(); + for (x, c) in cols.iter().enumerate() { + for y in 0..h { + g[y * w + x] = c[y]; + } + } + // Rows. + g.par_chunks_mut(w).for_each(|row| { + let f = row.to_vec(); + let (mut v, mut z) = (vec![0usize; w], vec![0.0f64; w + 1]); + dt1(&f, row, &mut v, &mut z); + }); + g.par_iter_mut().for_each(|v| *v = v.sqrt()); + g +} + +/// Signed distance to a binary matte's edge: positive inside (distance to the nearest +/// background pixel centre, minus ½), negative outside. +pub fn signed_distance(m: &[u8], w: usize, h: usize) -> Vec { + let fg: Vec = m.iter().map(|v| *v != 0).collect(); + let bg: Vec = m.iter().map(|v| *v == 0).collect(); + let din = distance(&bg, w, h); + let dout = distance(&fg, w, h); + m.iter().enumerate().map(|(i, v)| if *v != 0 { din[i] - 0.5 } else { 0.5 - dout[i] }).collect() +} + +/// Mean over the (2r+1)² window (clamped at the edges: averages only the pixels inside). +pub fn box_mean(src: &[f32], w: usize, h: usize, r: usize) -> Vec { + if w == 0 || h == 0 { + return vec![]; + } + // Horizontal window sums per row. + let mut tmp = vec![0.0f64; w * h]; + tmp.par_chunks_mut(w).enumerate().for_each(|(y, row)| { + let s = &src[y * w..y * w + w]; + let mut acc = 0.0f64; + for x in 0..r.min(w) { + acc += s[x] as f64; + } + for (x, o) in row.iter_mut().enumerate() { + if x + r < w { + acc += s[x + r] as f64; + } + if x > r { + acc -= s[x - r - 1] as f64; + } + *o = acc; + } + }); + // Vertical prefix sums (row by row, cache friendly), then window differences. + let mut pre = vec![0.0f64; w * (h + 1)]; + for y in 0..h { + let (done, rest) = pre.split_at_mut((y + 1) * w); + let prev = &done[y * w..]; + let cur = &mut rest[..w]; + let t = &tmp[y * w..y * w + w]; + for x in 0..w { + cur[x] = prev[x] + t[x]; + } + } + let mut out = vec![0.0f32; w * h]; + out.par_chunks_mut(w).enumerate().for_each(|(y, row)| { + let a = y.saturating_sub(r); + let b = (y + r + 1).min(h); + let cy = (b - a) as f64; + for (x, o) in row.iter_mut().enumerate() { + let cx = ((x + r + 1).min(w) - x.saturating_sub(r)) as f64; + *o = ((pre[b * w + x] - pre[a * w + x]) / (cx * cy)) as f32; + } + }); + out +} + +/// Approximate Gaussian blur (three box passes) with standard deviation `sigma` pixels. +pub fn gauss(src: &[f32], w: usize, h: usize, sigma: f64) -> Vec { + if sigma < 0.3 { + return src.to_vec(); + } + // Box radius whose three passes match the variance: 3·((2r+1)² − 1)/12 = σ². + let r = (((4.0 * sigma * sigma + 1.0).sqrt() - 1.0) / 2.0).round().max(1.0) as usize; + let a = box_mean(src, w, h, r); + let b = box_mean(&a, w, h, r); + box_mean(&b, w, h, r) +} + +fn inv3(m: [[f32; 3]; 3]) -> [[f32; 3]; 3] { + let d = m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) + + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]); + let d = if d.abs() < 1e-20 { 1e-20 } else { d }; + [ + [(m[1][1] * m[2][2] - m[1][2] * m[2][1]) / d, (m[0][2] * m[2][1] - m[0][1] * m[2][2]) / d, (m[0][1] * m[1][2] - m[0][2] * m[1][1]) / d], + [(m[1][2] * m[2][0] - m[1][0] * m[2][2]) / d, (m[0][0] * m[2][2] - m[0][2] * m[2][0]) / d, (m[0][2] * m[1][0] - m[0][0] * m[1][2]) / d], + [(m[1][0] * m[2][1] - m[1][1] * m[2][0]) / d, (m[0][1] * m[2][0] - m[0][0] * m[2][1]) / d, (m[0][0] * m[1][1] - m[0][1] * m[1][0]) / d], + ] +} + +/// The colour guided filter of `p` with guide `img` (RGB, w × h), window radius `r`, +/// regularisation `eps` (He et al. 2010, section 3.5). +pub fn guided_filter(img: &[[f32; 3]], p: &[f32], w: usize, h: usize, r: usize, eps: f32) -> Vec { + let n = w * h; + let ch = |c: usize| -> Vec { img.iter().map(|v| v[c]).collect() }; + let (ir, ig, ib) = (ch(0), ch(1), ch(2)); + let mul = |a: &[f32], b: &[f32]| -> Vec { a.iter().zip(b).map(|(x, y)| x * y).collect() }; + let bm = |v: &[f32]| box_mean(v, w, h, r); + let (mr, mg, mb, mp) = (bm(&ir), bm(&ig), bm(&ib), bm(p)); + let (mrp, mgp, mbp) = (bm(&mul(&ir, p)), bm(&mul(&ig, p)), bm(&mul(&ib, p))); + let (vrr, vrg, vrb) = (bm(&mul(&ir, &ir)), bm(&mul(&ir, &ig)), bm(&mul(&ir, &ib))); + let (vgg, vgb, vbb) = (bm(&mul(&ig, &ig)), bm(&mul(&ig, &ib)), bm(&mul(&ib, &ib))); + let mut a = vec![[0.0f32; 3]; n]; + let mut b = vec![0.0f32; n]; + a.par_iter_mut().zip(b.par_iter_mut()).enumerate().for_each(|(i, (ai, bi))| { + let cov = [mrp[i] - mr[i] * mp[i], mgp[i] - mg[i] * mp[i], mbp[i] - mb[i] * mp[i]]; + let s = [ + [vrr[i] - mr[i] * mr[i] + eps, vrg[i] - mr[i] * mg[i], vrb[i] - mr[i] * mb[i]], + [vrg[i] - mr[i] * mg[i], vgg[i] - mg[i] * mg[i] + eps, vgb[i] - mg[i] * mb[i]], + [vrb[i] - mr[i] * mb[i], vgb[i] - mg[i] * mb[i], vbb[i] - mb[i] * mb[i] + eps], + ]; + let inv = inv3(s); + let av = [ + cov[0] * inv[0][0] + cov[1] * inv[1][0] + cov[2] * inv[2][0], + cov[0] * inv[0][1] + cov[1] * inv[1][1] + cov[2] * inv[2][1], + cov[0] * inv[0][2] + cov[1] * inv[1][2] + cov[2] * inv[2][2], + ]; + *ai = av; + *bi = mp[i] - av[0] * mr[i] - av[1] * mg[i] - av[2] * mb[i]; + }); + let (a0, a1, a2): (Vec, Vec, Vec) = (a.iter().map(|v| v[0]).collect(), a.iter().map(|v| v[1]).collect(), a.iter().map(|v| v[2]).collect()); + let (ma0, ma1, ma2, mbb) = (bm(&a0), bm(&a1), bm(&a2), bm(&b)); + (0..n).into_par_iter().map(|i| ma0[i] * ir[i] + ma1[i] * ig[i] + ma2[i] * ib[i] + mbb[i]).collect() +} + +/// Closed-form matting (A. Levin, D. Lischinski and Y. Weiss, "A Closed-Form Solution to Natural +/// Image Matting", IEEE PAMI 30(2), 2008): the alpha that minimises αᵀLα with the matting +/// Laplacian `L` of the colour image, with `known` pixels fixed to `init`. Solved by conjugate +/// gradients over the unknown pixels, starting from `init`; `L·x` is evaluated with box filters +/// (K. He, J. Sun and X. Tang, "Fast Matting Using Large Kernel Matting Laplacian Matrices", +/// CVPR 2010): `(L·x)ᵢ = Σ_{k∋i} (xᵢ − a_kᵀIᵢ − b_k)` with `(a_k, b_k)` the ridge-regression +/// fit of `x` to the colours in window k. +pub fn closed_form(img: &[[f32; 3]], init: &[f32], known: &[bool], w: usize, h: usize, r: usize, eps: f32, iters: usize) -> Vec { + let n = w * h; + if n == 0 || known.iter().all(|k| *k) { + return init.to_vec(); + } + let ch = |c: usize| -> Vec { img.iter().map(|v| v[c]).collect() }; + let ic = [ch(0), ch(1), ch(2)]; + let bm = |v: &[f32]| box_mean(v, w, h, r); + let mu = [bm(&ic[0]), bm(&ic[1]), bm(&ic[2])]; + let prod = |a: usize, b: usize| -> Vec { ic[a].iter().zip(&ic[b]).map(|(x, y)| x * y).collect() }; + let mut cov = [[vec![], vec![], vec![]], [vec![], vec![], vec![]], [vec![], vec![], vec![]]]; + for a in 0..3 { + for b in a..3 { + let m = bm(&prod(a, b)); + cov[a][b] = m.clone(); + cov[b][a] = m; + } + } + let inv: Vec<[[f32; 3]; 3]> = (0..n) + .into_par_iter() + .map(|i| { + let mut s = [[0.0f32; 3]; 3]; + for a in 0..3 { + for b in 0..3 { + s[a][b] = cov[a][b][i] - mu[a][i] * mu[b][i] + if a == b { eps } else { 0.0 }; + } + } + inv3(s) + }) + .collect(); + // Pixels per window (= windows per pixel). + let cnt: Vec = { + let (rr, mut out) = (r as i64, vec![0.0f32; n]); + for (i, o) in out.iter_mut().enumerate() { + let (x, y) = ((i % w) as i64, (i / w) as i64); + let cx = (x + rr).min(w as i64 - 1) - (x - rr).max(0) + 1; + let cy = (y + rr).min(h as i64 - 1) - (y - rr).max(0) + 1; + *o = (cx * cy) as f32; + } + out + }; + let lap = |x: &[f32]| -> Vec { + let pm = bm(x); + let ipm: Vec> = (0..3).map(|c| bm(&ic[c].iter().zip(x).map(|(a, b)| a * b).collect::>())).collect(); + let ab: Vec<[f32; 4]> = (0..n) + .into_par_iter() + .map(|i| { + let cv = [ipm[0][i] - mu[0][i] * pm[i], ipm[1][i] - mu[1][i] * pm[i], ipm[2][i] - mu[2][i] * pm[i]]; + let m = &inv[i]; + let a = [ + m[0][0] * cv[0] + m[0][1] * cv[1] + m[0][2] * cv[2], + m[1][0] * cv[0] + m[1][1] * cv[1] + m[1][2] * cv[2], + m[2][0] * cv[0] + m[2][1] * cv[1] + m[2][2] * cv[2], + ]; + [a[0], a[1], a[2], pm[i] - a[0] * mu[0][i] - a[1] * mu[1][i] - a[2] * mu[2][i]] + }) + .collect(); + let sa: Vec> = (0..3).map(|c| bm(&ab.iter().map(|v| v[c]).collect::>())).collect(); + let sb = bm(&ab.iter().map(|v| v[3]).collect::>()); + (0..n).into_par_iter().map(|i| cnt[i] * (x[i] - sa[0][i] * ic[0][i] - sa[1][i] * ic[1][i] - sa[2][i] * ic[2][i] - sb[i])).collect() + }; + // L_UU x = −(L α_K)_U. + let a0: Vec = (0..n).map(|i| if known[i] { init[i] } else { 0.0 }).collect(); + let la0 = lap(&a0); + let mut x: Vec = (0..n).map(|i| if known[i] { 0.0 } else { init[i] }).collect(); + let lx = lap(&x); + let mut res: Vec = (0..n).map(|i| if known[i] { 0.0 } else { (-la0[i] - lx[i]) as f64 }).collect(); + let mut p = res.clone(); + let mut rs: f64 = res.iter().map(|v| v * v).sum(); + let rs0 = rs.max(1e-30); + for _ in 0..iters { + if rs <= rs0 * 1e-8 { + break; + } + let pf: Vec = p.iter().map(|v| *v as f32).collect(); + let ap: Vec = lap(&pf).iter().enumerate().map(|(i, v)| if known[i] { 0.0 } else { *v as f64 }).collect(); + let pap: f64 = p.iter().zip(&ap).map(|(a, b)| a * b).sum(); + if pap <= 1e-30 { + break; + } + let alpha = rs / pap; + for i in 0..n { + x[i] += (alpha * p[i]) as f32; + res[i] -= alpha * ap[i]; + } + let rs_new: f64 = res.iter().map(|v| v * v).sum(); + let beta = rs_new / rs; + rs = rs_new; + for i in 0..n { + p[i] = res[i] + beta * p[i]; + } + } + (0..n).map(|i| if known[i] { init[i] } else { x[i].clamp(0.0, 1.0) }).collect() +} + +/// Contrast (0–100 %) around ½ on a soft alpha. +pub fn contrast(a: &mut [f32], pc: f64) { + if pc <= 0.0 { + return; + } + let k = 1.0 / (1.0 - 0.99 * (pc / 100.0).clamp(0.0, 1.0)) as f32; + a.par_iter_mut().for_each(|v| *v = (0.5 + (*v - 0.5) * k).clamp(0.0, 1.0)); +} + +/// Shift Edge (−100–100 %) on a soft alpha: positive values grow the matte (a gamma curve on α). +pub fn shift_soft(a: &mut [f32], pc: f64) { + if pc.abs() < 1e-9 { + return; + } + let g = 2f32.powf((-pc / 50.0) as f32); + a.par_iter_mut().for_each(|v| *v = v.clamp(0.0, 1.0).powf(g)); +} + +/// Estimate the pure foreground colour of every pixel listed in `region` (alpha strictly +/// between 0 and 1) and mix it in by `amount` (0–1). `radius`: neighbourhood (pixels) that pure +/// background / foreground estimates are taken from. Returns the decontamination map (how much +/// each pixel's colour changed, 0–1). +pub fn decontaminate(img: &mut [[f32; 3]], alpha: &[f32], region: &[bool], w: usize, h: usize, radius: usize, amount: f32) -> Vec { + let n = w * h; + let mut map = vec![0.0f32; n]; + if !region.iter().any(|r| *r) || amount <= 0.0 { + return map; + } + let lo: Vec = alpha.iter().map(|a| if *a < 0.02 { 1.0 } else { 0.0 }).collect(); + let hi: Vec = alpha.iter().map(|a| if *a > 0.98 { 1.0 } else { 0.0 }).collect(); + // Windowed means of pure background / foreground colours, widening where none is near. + let est = |m: &[f32]| -> Vec> { + let mut out: Vec> = vec![None; n]; + let mut r = radius.max(2); + for _ in 0..4 { + let cnt = box_mean(m, w, h, r); + let sums: Vec> = (0..3).map(|c| box_mean(&img.iter().zip(m).map(|(p, k)| p[c] * k).collect::>(), w, h, r)).collect(); + let mut missing = false; + for i in 0..n { + if region[i] && out[i].is_none() { + if cnt[i] > 1e-4 { + out[i] = Some([sums[0][i] / cnt[i], sums[1][i] / cnt[i], sums[2][i] / cnt[i]]); + } else { + missing = true; + } + } + } + if !missing { + break; + } + r *= 2; + } + out + }; + let bgc = est(&lo); + let fgc = est(&hi); + for i in 0..n { + if !region[i] { + continue; + } + let a = alpha[i].clamp(0.0, 1.0); + if a <= 0.0 || a >= 1.0 { + continue; + } + let c = img[i]; + let f = match (bgc[i], fgc[i]) { + (Some(b), fbar) => { + let mut f = [0.0f32; 3]; + for k in 0..3 { + f[k] = ((c[k] - (1.0 - a) * b[k]) / a).clamp(0.0, 1.0); + } + // Low alphas amplify noise: lean on the nearby foreground colour there. + if let Some(fb) = fbar { + let t = ((0.25 - a) / 0.25).clamp(0.0, 1.0); + for k in 0..3 { + f[k] += (fb[k] - f[k]) * t; + } + } + f + } + (None, Some(fb)) => fb, + (None, None) => c, + }; + let mut d = 0.0f32; + for k in 0..3 { + let v = c[k] + (f[k] - c[k]) * amount; + d = d.max((v - c[k]).abs()); + img[i][k] = v; + } + map[i] = d.min(1.0); + } + map +} + +/// Sample a float plane bilinearly at continuous coordinates (pixel centres at +0.5), clamped. +pub fn sample(p: &[f32], w: usize, h: usize, x: f64, y: f64) -> f32 { + if w == 0 || h == 0 { + return 0.0; + } + let fx = (x - 0.5).clamp(0.0, (w - 1) as f64); + let fy = (y - 0.5).clamp(0.0, (h - 1) as f64); + let (x0, y0) = (fx.floor() as usize, fy.floor() as usize); + let (x1, y1) = ((x0 + 1).min(w - 1), (y0 + 1).min(h - 1)); + let (tx, ty) = ((fx - x0 as f64) as f32, (fy - y0 as f64) as f32); + let g = |x: usize, y: usize| p[y * w + x]; + let top = g(x0, y0) + (g(x1, y0) - g(x0, y0)) * tx; + let bot = g(x0, y1) + (g(x1, y1) - g(x0, y1)) * tx; + top + (bot - top) * ty +} + +/// Average of `a` sampled along the segment `[-v/2, v/2]` with `samples` taps (motion blur of a +/// matte moving by `v` pixels during the shutter). +pub fn motion_blur(a: &[f32], w: usize, h: usize, v: [f64; 2], samples: usize) -> Vec { + let n = samples.max(2); + if v[0].hypot(v[1]) < 0.25 { + return a.to_vec(); + } + (0..w * h) + .into_par_iter() + .map(|i| { + let (x, y) = ((i % w) as f64 + 0.5, (i / w) as f64 + 0.5); + let mut s = 0.0; + for k in 0..n { + let t = k as f64 / (n - 1) as f64 - 0.5; + s += sample(a, w, h, x - v[0] * t, y - v[1] * t); + } + s / n as f32 + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn distance_matches_brute_force() { + let (w, h) = (23, 17); + let seed: Vec = (0..w * h).map(|i| (i * 7919) % 31 == 0).collect(); + let d = distance(&seed, w, h); + for y in 0..h { + for x in 0..w { + let mut b = f32::INFINITY; + for j in 0..h { + for i in 0..w { + if seed[j * w + i] { + b = b.min(((x as f32 - i as f32).powi(2) + (y as f32 - j as f32).powi(2)).sqrt()); + } + } + } + assert!((d[y * w + x] - b).abs() < 1e-3, "{x},{y}: {} vs {b}", d[y * w + x]); + } + } + } + + #[test] + fn guided_filter_recovers_a_linear_blend() { + // I = αF + (1 − α)B with constant F, B: the filtered binary matte is close to α. + let (w, h) = (64, 8); + let alpha: Vec = (0..w * h).map(|i| (((i % w) as f32 - 26.0) / 12.0).clamp(0.0, 1.0)).collect(); + let (f, b) = ([0.9f32, 0.2, 0.1], [0.1f32, 0.3, 0.8]); + let img: Vec<[f32; 3]> = alpha.iter().map(|a| [a * f[0] + (1.0 - a) * b[0], a * f[1] + (1.0 - a) * b[1], a * f[2] + (1.0 - a) * b[2]]).collect(); + let p: Vec = alpha.iter().map(|a| if *a >= 0.5 { 1.0 } else { 0.0 }).collect(); + let q = guided_filter(&img, &p, w, h, 8, 1e-5); + let err: f32 = q.iter().zip(&alpha).map(|(a, b)| (a.clamp(0.0, 1.0) - b).abs()).sum::() / (w * h) as f32; + assert!(err < 0.03, "mean error {err}"); + } +} diff --git a/crates/track/src/roto/maxflow.rs b/crates/track/src/roto/maxflow.rs new file mode 100644 index 00000000..dee4b010 --- /dev/null +++ b/crates/track/src/roto/maxflow.rs @@ -0,0 +1,438 @@ +//! Min-cut / max-flow on sparse graphs with source and sink terminals, by the +//! Boykov–Kolmogorov algorithm (Y. Boykov and V. Kolmogorov, "An Experimental Comparison of +//! Min-Cut/Max-Flow Algorithms for Energy Minimization in Vision", IEEE PAMI 26(9), 2004). +//! +//! Two search trees grow from the terminals (S from the source, T from the sink) through +//! non-saturated edges; when they touch, the path found is augmented; saturated tree edges turn +//! the nodes below them into *orphans*, which the adoption stage re-attaches to their tree (or +//! frees). Trees are reused between augmentations, which is what makes the method fast on the +//! grid graphs of image segmentation. The timestamp/distance heuristic of section 3.2.3 keeps +//! adoption paths short. +//! +//! Capacities are integers (callers scale their energies), so saturation tests are exact and +//! the result is deterministic. + +use std::collections::VecDeque; + +const NONE: u32 = u32::MAX; +const TERMINAL: u32 = u32::MAX - 1; +const ORPHAN: u32 = u32::MAX - 2; + +#[derive(Clone, Copy)] +struct Node { + first: u32, + /// Arc to the parent (from this node), or TERMINAL / ORPHAN / NONE (free). + parent: u32, + /// Residual terminal capacity: > 0 towards the source, < 0 towards the sink. + tr_cap: i64, + is_sink: bool, + active: bool, + ts: u32, + dist: u32, +} + +#[derive(Clone, Copy)] +struct Arc { + head: u32, + next: u32, + r_cap: i32, +} + +/// A graph for one min-cut computation. +pub struct Graph { + nodes: Vec, + arcs: Vec, + flow: i64, + time: u32, + queue: VecDeque, + orphans: VecDeque, +} + +/// Which side of the cut a node ended on. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Segment { + Source, + Sink, +} + +impl Graph { + pub fn new(nodes: usize, edges_hint: usize) -> Graph { + Graph { + nodes: vec![Node { first: NONE, parent: NONE, tr_cap: 0, is_sink: false, active: false, ts: 0, dist: 0 }; nodes], + arcs: Vec::with_capacity(edges_hint * 2), + flow: 0, + time: 0, + queue: VecDeque::new(), + orphans: VecDeque::new(), + } + } + + pub fn len(&self) -> usize { + self.nodes.len() + } + pub fn is_empty(&self) -> bool { + self.nodes.is_empty() + } + + /// Terminal capacities of node `i`: `source` is paid when `i` ends on the sink side, `sink` + /// when it ends on the source side. + pub fn add_tweights(&mut self, i: usize, source: i64, sink: i64) { + let n = &mut self.nodes[i]; + let d = n.tr_cap; + let (s, t) = if d > 0 { (source + d, sink) } else { (source, sink - d) }; + self.flow += s.min(t); + n.tr_cap = s - t; + } + + /// An edge between `i` and `j` with capacity `cap` from i to j and `rev` from j to i. + pub fn add_edge(&mut self, i: usize, j: usize, cap: i32, rev: i32) { + debug_assert!(i != j); + let a = self.arcs.len() as u32; + self.arcs.push(Arc { head: j as u32, next: self.nodes[i].first, r_cap: cap }); + self.nodes[i].first = a; + self.arcs.push(Arc { head: i as u32, next: self.nodes[j].first, r_cap: rev }); + self.nodes[j].first = a + 1; + } + + #[inline] + fn set_active(&mut self, i: u32) { + let n = &mut self.nodes[i as usize]; + if !n.active { + n.active = true; + self.queue.push_back(i); + } + } + + /// Compute the maximum flow (= the minimum cut's cost). + pub fn maxflow(&mut self) -> i64 { + for i in 0..self.nodes.len() { + let n = &mut self.nodes[i]; + if n.tr_cap != 0 { + n.is_sink = n.tr_cap < 0; + n.parent = TERMINAL; + n.ts = 0; + n.dist = 1; + self.set_active(i as u32); + } + } + let mut current: Option = None; + loop { + let i = match current.take() { + Some(i) if self.nodes[i as usize].parent != NONE => i, + _ => loop { + let Some(i) = self.queue.pop_front() else { return self.flow }; + self.nodes[i as usize].active = false; + if self.nodes[i as usize].parent != NONE { + break i; + } + }, + }; + // Growth. + let mut found = NONE; + let ni = self.nodes[i as usize]; + let mut a = ni.first; + if !ni.is_sink { + while a != NONE { + let arc = self.arcs[a as usize]; + if arc.r_cap > 0 { + let j = arc.head as usize; + let nj = self.nodes[j]; + if nj.parent == NONE { + let n = &mut self.nodes[j]; + n.is_sink = false; + n.parent = a ^ 1; + n.ts = ni.ts; + n.dist = ni.dist + 1; + self.set_active(j as u32); + } else if nj.is_sink { + found = a; + break; + } else if nj.ts <= ni.ts && nj.dist > ni.dist { + let n = &mut self.nodes[j]; + n.parent = a ^ 1; + n.ts = ni.ts; + n.dist = ni.dist + 1; + } + } + a = arc.next; + } + } else { + while a != NONE { + let arc = self.arcs[a as usize]; + if self.arcs[(a ^ 1) as usize].r_cap > 0 { + let j = arc.head as usize; + let nj = self.nodes[j]; + if nj.parent == NONE { + let n = &mut self.nodes[j]; + n.is_sink = true; + n.parent = a ^ 1; + n.ts = ni.ts; + n.dist = ni.dist + 1; + self.set_active(j as u32); + } else if !nj.is_sink { + found = a ^ 1; + break; + } else if nj.ts <= ni.ts && nj.dist > ni.dist { + let n = &mut self.nodes[j]; + n.parent = a ^ 1; + n.ts = ni.ts; + n.dist = ni.dist + 1; + } + } + a = arc.next; + } + } + self.time = self.time.wrapping_add(1); + if found != NONE { + // Keep `i` current: it may have more paths. + current = Some(i); + self.augment(found); + self.adopt(); + } + } + } + + fn augment(&mut self, middle: u32) { + let mut b = self.arcs[middle as usize].r_cap as i64; + // Source side. + let mut i = self.arcs[(middle ^ 1) as usize].head; + loop { + let a = self.nodes[i as usize].parent; + if a == TERMINAL { + break; + } + b = b.min(self.arcs[(a ^ 1) as usize].r_cap as i64); + i = self.arcs[a as usize].head; + } + b = b.min(self.nodes[i as usize].tr_cap); + // Sink side. + let mut i = self.arcs[middle as usize].head; + loop { + let a = self.nodes[i as usize].parent; + if a == TERMINAL { + break; + } + b = b.min(self.arcs[a as usize].r_cap as i64); + i = self.arcs[a as usize].head; + } + b = b.min(-self.nodes[i as usize].tr_cap); + let bi = b as i32; + self.arcs[(middle ^ 1) as usize].r_cap += bi; + self.arcs[middle as usize].r_cap -= bi; + // Source side. + let mut i = self.arcs[(middle ^ 1) as usize].head; + loop { + let a = self.nodes[i as usize].parent; + if a == TERMINAL { + break; + } + self.arcs[a as usize].r_cap += bi; + self.arcs[(a ^ 1) as usize].r_cap -= bi; + if self.arcs[(a ^ 1) as usize].r_cap == 0 { + self.make_orphan(i); + } + i = self.arcs[a as usize].head; + } + self.nodes[i as usize].tr_cap -= b; + if self.nodes[i as usize].tr_cap == 0 { + self.make_orphan(i); + } + // Sink side. + let mut i = self.arcs[middle as usize].head; + loop { + let a = self.nodes[i as usize].parent; + if a == TERMINAL { + break; + } + self.arcs[(a ^ 1) as usize].r_cap += bi; + self.arcs[a as usize].r_cap -= bi; + if self.arcs[a as usize].r_cap == 0 { + self.make_orphan(i); + } + i = self.arcs[a as usize].head; + } + self.nodes[i as usize].tr_cap += b; + if self.nodes[i as usize].tr_cap == 0 { + self.make_orphan(i); + } + self.flow += b; + } + + fn make_orphan(&mut self, i: u32) { + self.nodes[i as usize].parent = ORPHAN; + self.orphans.push_back(i); + } + + /// Whether node `j`'s path to its terminal is intact; returns its distance (marking the + /// path with the current time) or None. + fn origin_dist(&mut self, j: u32) -> Option { + let mut d = 0u32; + let mut k = j; + loop { + let n = self.nodes[k as usize]; + if n.ts == self.time { + d += n.dist; + break; + } + let a = n.parent; + d += 1; + if a == TERMINAL { + let n = &mut self.nodes[k as usize]; + n.ts = self.time; + n.dist = 1; + break; + } + if a == ORPHAN || a == NONE { + return None; + } + k = self.arcs[a as usize].head; + } + // Mark the path. + let total = d; + let mut k = j; + let mut dd = d; + while self.nodes[k as usize].ts != self.time { + let n = &mut self.nodes[k as usize]; + n.ts = self.time; + n.dist = dd; + dd -= 1; + k = self.arcs[n.parent as usize].head; + } + Some(total) + } + + fn adopt(&mut self) { + while let Some(i) = self.orphans.pop_front() { + let sink = self.nodes[i as usize].is_sink; + let mut best = NONE; + let mut best_d = u32::MAX; + let mut a = self.nodes[i as usize].first; + while a != NONE { + let arc = self.arcs[a as usize]; + // S orphan: the parent must push flow into i (residual on j→i = sister of i→j). + // T orphan: i must push flow into the parent (residual on i→j). + let cap = if sink { arc.r_cap } else { self.arcs[(a ^ 1) as usize].r_cap }; + if cap > 0 { + let j = arc.head; + let nj = self.nodes[j as usize]; + if nj.is_sink == sink + && nj.parent != NONE + && let Some(d) = self.origin_dist(j) + && d < best_d + { + best = a; + best_d = d; + } + } + a = arc.next; + } + if best != NONE { + let n = &mut self.nodes[i as usize]; + n.parent = best; + n.ts = self.time; + n.dist = best_d + 1; + continue; + } + // No parent: free the node; its children become orphans, neighbours that could + // reach it become active. + self.nodes[i as usize].ts = 0; + let mut a = self.nodes[i as usize].first; + while a != NONE { + let arc = self.arcs[a as usize]; + let j = arc.head; + let nj = self.nodes[j as usize]; + if nj.is_sink == sink && nj.parent != NONE { + let cap = if sink { arc.r_cap } else { self.arcs[(a ^ 1) as usize].r_cap }; + if cap > 0 { + self.set_active(j); + } + let pa = nj.parent; + if pa != TERMINAL && pa != ORPHAN && self.arcs[pa as usize].head == i { + self.make_orphan(j); + } + } + a = arc.next; + } + self.nodes[i as usize].parent = NONE; + } + } + + /// The side of node `i` after [`Graph::maxflow`] (free nodes go to the sink). + pub fn segment(&self, i: usize) -> Segment { + let n = &self.nodes[i]; + if n.parent != NONE && !n.is_sink { Segment::Source } else { Segment::Sink } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Brute-force min cut over all labelings of a small graph. + fn brute(n: usize, t: &[(i64, i64)], e: &[(usize, usize, i32, i32)]) -> i64 { + let mut best = i64::MAX; + for m in 0..(1u32 << n) { + let src = |i: usize| m & (1 << i) != 0; + let mut c = 0i64; + for (i, (s, k)) in t.iter().enumerate() { + c += if src(i) { *k } else { *s }; + } + for (i, j, a, b) in e { + if src(*i) && !src(*j) { + c += *a as i64; + } + if src(*j) && !src(*i) { + c += *b as i64; + } + } + best = best.min(c); + } + best + } + + #[test] + fn matches_brute_force_on_random_graphs() { + let mut s = 12345u64; + let mut rnd = |m: u64| { + s ^= s << 13; + s ^= s >> 7; + s ^= s << 17; + s % m + }; + for _ in 0..300 { + let n = 2 + rnd(9) as usize; + let t: Vec<(i64, i64)> = (0..n).map(|_| (rnd(20) as i64, rnd(20) as i64)).collect(); + let mut e = vec![]; + for _ in 0..rnd(20) { + let (i, j) = (rnd(n as u64) as usize, rnd(n as u64) as usize); + if i != j { + e.push((i, j, rnd(15) as i32, rnd(15) as i32)); + } + } + let mut g = Graph::new(n, e.len()); + for (i, (a, b)) in t.iter().enumerate() { + g.add_tweights(i, *a, *b); + } + for (i, j, a, b) in &e { + g.add_edge(*i, *j, *a, *b); + } + let f = g.maxflow(); + assert_eq!(f, brute(n, &t, &e)); + // The labeling realises the cut. + let src = |i: usize| g.segment(i) == Segment::Source; + let mut c = 0i64; + for (i, (a, b)) in t.iter().enumerate() { + c += if src(i) { *b } else { *a }; + } + for (i, j, a, b) in &e { + if src(*i) && !src(*j) { + c += *a as i64; + } + if src(*j) && !src(*i) { + c += *b as i64; + } + } + assert_eq!(c, f); + } + } +} diff --git a/crates/track/src/roto/mod.rs b/crates/track/src/roto/mod.rs new file mode 100644 index 00000000..393f743e --- /dev/null +++ b/crates/track/src/roto/mod.rs @@ -0,0 +1,754 @@ +//! Roto Brush segmentation and propagation, clean-room from published methods: +//! +//! - **Interactive graph-cut segmentation** (Y. Boykov and M.-P. Jolly, "Interactive Graph Cuts +//! for Optimal Boundary & Region Segmentation of Objects in N-D Images", ICCV 2001): the user's +//! foreground / background strokes are hard constraints, every other pixel gets a data cost +//! from colour models and neighbouring pixels a contrast-sensitive smoothness cost; the +//! minimum s–t cut ([`maxflow`], Boykov–Kolmogorov) is the segmentation. +//! - **Iterated colour models** (C. Rother, V. Kolmogorov and A. Blake, "GrabCut", SIGGRAPH +//! 2004): Gaussian mixtures ([`gmm`]) are fitted to the strokes, then re-fitted to the +//! segmentation and the cut repeated; the 8-connected n-link weight is +//! `γ/dist · exp(−β‖Iₚ − I_q‖²)` with `β = 1 / (2⟨‖Iₚ − I_q‖²⟩)`. +//! - **Coarse to fine**: large frames are segmented at a reduced size, then re-cut at full +//! resolution in a narrow band around the upsampled boundary (Lombaert et al., "A Multilevel +//! Banded Graph Cuts Method for Fast Image Segmentation", ICCV 2005). +//! - **Propagation** (in the spirit of X. Bai et al., "Video SnapCut", SIGGRAPH 2009): the +//! previous frame's matte is warped to the next frame by optical flow +//! (`effectcraft_raster::flow::block_flow`), and the frame re-segmented by graph cut only in a +//! band of **Search Radius** pixels around the warped boundary, with the previous frame's +//! colour models and the warped matte as a shape prior. Correction strokes on a frame re-cut +//! the whole frame with the warped matte as a soft prior, and propagation restarts from there. +//! +//! Mattes are in layer pixels at a given scale (1 = full resolution). Strokes are stored in +//! layer pixels ([`RotoData`], serde) and keyed per frame so that each frame's result is a +//! deterministic function of its *chain*: the base frame's strokes, then every frame's strokes +//! on the way out to it ([`RotoData::chain_keys`]). + +pub mod gmm; +pub mod matting; +pub mod maxflow; +pub mod refine; +pub mod rle; + +use std::collections::BTreeMap; + +use effectcraft_raster::Image; +use rayon::prelude::*; +use serde::{Deserialize, Serialize}; + +pub use gmm::Gmm; +use maxflow::{Graph, Segment}; + +/// What a stroke paints. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum StrokeKind { + /// Roto Brush (green): foreground. + Fg, + /// Roto Brush with Alt (red): background. + Bg, + /// Refine Edge tool: mark a band for edge matting. + Refine, + /// Refine Edge tool with Alt: remove from the band. + RefineErase, +} + +impl StrokeKind { + pub fn from_name(s: &str) -> Option { + Some(match s.to_ascii_lowercase().as_str() { + "fg" | "foreground" => StrokeKind::Fg, + "bg" | "background" => StrokeKind::Bg, + "refine" | "refineedge" => StrokeKind::Refine, + "refineerase" | "refine-erase" | "erase" => StrokeKind::RefineErase, + _ => return None, + }) + } + pub fn is_segmentation(self) -> bool { + matches!(self, StrokeKind::Fg | StrokeKind::Bg) + } +} + +/// One stroke: a polyline swept by a disc of `radius` layer pixels, on layer frame `frame`. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Stroke { + pub kind: StrokeKind, + pub frame: i64, + pub radius: f64, + pub points: Vec<[f64; 2]>, +} + +/// A Roto Brush instance's user data: strokes, the base frame and the segmentation span +/// (inclusive layer frames). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct RotoData { + pub base: Option, + pub span: [i64; 2], + pub strokes: Vec, +} + +fn fnv(h: &mut u64, bytes: &[u8]) { + for b in bytes { + *h ^= *b as u64; + *h = h.wrapping_mul(0x0100_0000_01b3); + } +} + +impl RotoData { + pub fn from_json(s: &str) -> RotoData { + if s.trim().is_empty() { + return RotoData::default(); + } + serde_json::from_str(s).unwrap_or_default() + } + pub fn to_json(&self) -> String { + serde_json::to_string(self).unwrap_or_default() + } + pub fn is_empty(&self) -> bool { + self.base.is_none() || !self.strokes.iter().any(|s| s.kind.is_segmentation()) + } + pub fn strokes_at(&self, frame: i64) -> Vec<&Stroke> { + self.strokes.iter().filter(|s| s.frame == frame).collect() + } + /// Frames that carry strokes. + pub fn stroke_frames(&self) -> Vec { + let mut v: Vec = self.strokes.iter().map(|s| s.frame).collect(); + v.sort_unstable(); + v.dedup(); + v + } + pub fn in_span(&self, f: i64) -> bool { + self.base.is_some() && f >= self.span[0] && f <= self.span[1] + } + /// Add a stroke: the first segmentation stroke sets the base frame and a span of + /// `default_span` frames each side (clamped to `limits`); strokes outside the span extend it. + pub fn add(&mut self, s: Stroke, default_span: i64, limits: [i64; 2]) { + let f = s.frame; + match self.base { + None if s.kind.is_segmentation() => { + self.base = Some(f); + self.span = [(f - default_span).max(limits[0]), (f + default_span).min(limits[1]).max(f)]; + } + Some(_) => { + self.span[0] = self.span[0].min(f); + self.span[1] = self.span[1].max(f); + } + None => {} + } + self.strokes.push(s); + } + /// Largest Refine Edge stroke radius (layer pixels). + pub fn refine_radius(&self) -> f64 { + self.strokes.iter().filter(|s| matches!(s.kind, StrokeKind::Refine)).map(|s| s.radius).fold(0.0, f64::max) + } + /// Content key of every frame in the span: the base frame's from `seed` (what the frames + /// and settings are) and its strokes, every other frame's from its neighbour towards the + /// base and its own strokes. + pub fn chain_keys(&self, seed: u64) -> BTreeMap { + let mut out = BTreeMap::new(); + let Some(base) = self.base else { return out }; + let key = |prev: u64, f: i64| { + let mut h = prev ^ 0x9e37_79b9_7f4a_7c15; + fnv(&mut h, &f.to_le_bytes()); + for s in self.strokes.iter().filter(|s| s.frame == f) { + fnv(&mut h, serde_json::to_string(s).unwrap_or_default().as_bytes()); + } + h + }; + let k0 = key(seed, base); + out.insert(base, k0); + let mut k = k0; + for f in base + 1..=self.span[1] { + k = key(k, f); + out.insert(f, k); + } + k = k0; + for f in (self.span[0]..base).rev() { + k = key(k, f); + out.insert(f, k); + } + out + } + /// The neighbour a frame is propagated from (None for the base frame and outside the span). + pub fn source_of(&self, f: i64) -> Option { + let base = self.base?; + if !self.in_span(f) || f == base { + return None; + } + Some(if f > base { f - 1 } else { f + 1 }) + } +} + +/// Segmentation settings (the effect's Roto Brush parameters, in frame pixels). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct SegOpts { + /// Pixels of the matte per layer pixel. + pub scale: f64, + /// Search Radius (layer pixels). + pub search_radius: f64, + /// Motion Threshold / Motion Damping, percent. + pub motion_threshold: f64, + pub motion_damping: f64, + /// Quality ▸ Best. + pub best: bool, +} + +impl Default for SegOpts { + fn default() -> Self { + SegOpts { scale: 1.0, search_radius: 15.0, motion_threshold: 10.0, motion_damping: 20.0, best: false } + } +} + +/// One frame's segmentation: the binary matte, the Refine Edge band and the colour models. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct FrameSeg { + pub w: usize, + pub h: usize, + pub matte: Vec, + pub refine: Vec, + pub fg: Gmm, + pub bg: Gmm, +} + +impl FrameSeg { + pub fn empty(w: usize, h: usize) -> FrameSeg { + FrameSeg { w, h, matte: vec![0; w * h], refine: vec![0; w * h], ..Default::default() } + } + pub fn area(&self) -> usize { + self.matte.iter().filter(|v| **v != 0).count() + } + /// Centroid of the matte (pixels), if any. + pub fn centroid(&self) -> Option<[f64; 2]> { + let (mut sx, mut sy, mut n) = (0.0, 0.0, 0.0); + for (i, v) in self.matte.iter().enumerate() { + if *v != 0 { + sx += (i % self.w) as f64 + 0.5; + sy += (i / self.w) as f64 + 0.5; + n += 1.0; + } + } + (n > 0.0).then(|| [sx / n, sy / n]) + } +} + +/// Straight RGB of an image (transparent pixels are black). +pub fn rgb_of(img: &Image) -> Vec<[f32; 3]> { + img.data.par_iter().map(|p| if p[3] > 1e-6 { [p[0] / p[3], p[1] / p[3], p[2] / p[3]] } else { [0.0; 3] }).collect() +} + +/// Pixels covered by `strokes` (of the given kinds) at `scale`. +pub fn rasterize(strokes: &[&Stroke], kinds: &[StrokeKind], scale: f64, w: usize, h: usize) -> Vec { + let mut m = vec![false; w * h]; + for s in strokes.iter().filter(|s| kinds.contains(&s.kind)) { + let r = (s.radius * scale).max(0.5); + let pts: Vec<[f64; 2]> = s.points.iter().map(|p| [p[0] * scale, p[1] * scale]).collect(); + let segs: Vec<([f64; 2], [f64; 2])> = if pts.len() == 1 { vec![(pts[0], pts[0])] } else { pts.windows(2).map(|w| (w[0], w[1])).collect() }; + for (a, b) in segs { + let x0 = (a[0].min(b[0]) - r).floor().max(0.0) as usize; + let x1 = ((a[0].max(b[0]) + r).ceil().max(0.0) as usize).min(w); + let y0 = (a[1].min(b[1]) - r).floor().max(0.0) as usize; + let y1 = ((a[1].max(b[1]) + r).ceil().max(0.0) as usize).min(h); + let d = [b[0] - a[0], b[1] - a[1]]; + let l2 = d[0] * d[0] + d[1] * d[1]; + for y in y0..y1 { + for x in x0..x1 { + let p = [x as f64 + 0.5, y as f64 + 0.5]; + let t = if l2 > 0.0 { (((p[0] - a[0]) * d[0] + (p[1] - a[1]) * d[1]) / l2).clamp(0.0, 1.0) } else { 0.0 }; + let q = [a[0] + d[0] * t, a[1] + d[1] * t]; + if (p[0] - q[0]).hypot(p[1] - q[1]) <= r { + m[y * w + x] = true; + } + } + } + } + } + m +} + +/// Hard constraints: 1 = foreground, 2 = background, 0 = free. +const HARD_FG: u8 = 1; +const HARD_BG: u8 = 2; + +/// Graph-cut energy weights. +const GAMMA: f64 = 50.0; +const PRIOR: f64 = 1.0; +/// Fixed-point scale of the energies. +const K: f64 = 16.0; +const INF: i64 = 1 << 40; + +const NB: [(i64, i64, bool); 8] = [(1, 0, true), (0, 1, true), (1, 1, true), (-1, 1, true), (-1, 0, false), (0, -1, false), (-1, -1, false), (1, -1, false)]; + +fn beta(img: &[[f32; 3]], free: &[bool], w: usize, h: usize) -> f64 { + let (mut s, mut n) = (0.0f64, 0.0f64); + for y in 0..h { + for x in 0..w { + let i = y * w + x; + if !free[i] { + continue; + } + let c = img[i]; + for (dx, dy) in [(1usize, 0usize), (0, 1)] { + let (nx, ny) = (x + dx, y + dy); + if nx < w && ny < h { + let q = img[ny * w + nx]; + s += ((c[0] - q[0]).powi(2) + (c[1] - q[1]).powi(2) + (c[2] - q[2]).powi(2)) as f64; + n += 1.0; + } + } + } + } + if s <= 1e-12 { 1.0 } else { n / (2.0 * s) } +} + +/// One min-cut over the `free` pixels; others keep `labels`. `prior`: P(foreground) per pixel. +fn cut(img: &[[f32; 3]], w: usize, h: usize, labels: &mut [u8], free: &[bool], hard: &[u8], fg: &Gmm, bg: &Gmm, prior: Option<&[f32]>) { + let mut idx = vec![u32::MAX; w * h]; + let mut nodes = vec![]; + for i in 0..w * h { + if free[i] { + idx[i] = nodes.len() as u32; + nodes.push(i); + } + } + if nodes.is_empty() { + return; + } + let b = beta(img, free, w, h); + let costs: Vec<(f64, f64)> = nodes + .par_iter() + .map(|&i| { + let c = img[i]; + let (mut cf, mut cb) = (fg.cost(c), bg.cost(c)); + if let Some(p) = prior { + let pf = (p[i] as f64).clamp(0.02, 0.98); + cf += PRIOR * -pf.ln(); + cb += PRIOR * -(1.0 - pf).ln(); + } + (cf, cb) + }) + .collect(); + let mut g = Graph::new(nodes.len(), nodes.len() * 4); + for (n, &i) in nodes.iter().enumerate() { + let (x, y) = ((i % w) as i64, (i / w) as i64); + let (cf, cb) = costs[n]; + let m = cf.min(cb); + let (mut src, mut snk) = (((cb - m) * K).round() as i64, ((cf - m) * K).round() as i64); + match hard[i] { + HARD_FG => (src, snk) = (INF, 0), + HARD_BG => (src, snk) = (0, INF), + _ => {} + } + g.add_tweights(n, src, snk); + let c = img[i]; + for (dx, dy, fwd) in NB { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || ny < 0 || nx >= w as i64 || ny >= h as i64 { + continue; + } + let j = ny as usize * w + nx as usize; + let q = img[j]; + let d2 = ((c[0] - q[0]).powi(2) + (c[1] - q[1]).powi(2) + (c[2] - q[2]).powi(2)) as f64; + let dist = if dx != 0 && dy != 0 { std::f64::consts::SQRT_2 } else { 1.0 }; + let wt = ((GAMMA / dist * (-b * d2).exp()) * K).round() as i32; + if wt <= 0 { + continue; + } + if idx[j] == u32::MAX { + // Fixed neighbour: disagreeing with it costs the edge. + if labels[j] != 0 { + g.add_tweights(n, wt as i64, 0); + } else { + g.add_tweights(n, 0, wt as i64); + } + } else if fwd { + g.add_edge(n, idx[j] as usize, wt, wt); + } + } + } + g.maxflow(); + for (n, &i) in nodes.iter().enumerate() { + labels[i] = (g.segment(n) == Segment::Source) as u8; + } +} + +/// Colour samples of the pixels where `sel` holds. +fn samples(img: &[[f32; 3]], sel: impl Fn(usize) -> bool) -> Vec<[f32; 3]> { + img.iter().enumerate().filter(|(i, _)| sel(*i)).map(|(_, c)| *c).collect() +} + +const COMPONENTS: usize = 5; +const MAX_SAMPLES: usize = 24_000; + +fn fit_models(img: &[[f32; 3]], labels: &[u8]) -> (Gmm, Gmm) { + let fg = samples(img, |i| labels[i] != 0); + let bg = samples(img, |i| labels[i] == 0); + (Gmm::fit(&fg, COMPONENTS, MAX_SAMPLES), Gmm::fit(&bg, COMPONENTS, MAX_SAMPLES)) +} + +/// Keep the foreground components (8-connected) that touch `seeds`; fill small background +/// holes that touch neither the frame edge nor a background seed. +fn clean(labels: &mut [u8], seeds: &[bool], bg_seeds: &[bool], w: usize, h: usize) { + let n = w * h; + let mut comp = vec![u32::MAX; n]; + let mut stack = vec![]; + let mut fg_area = 0usize; + for start in 0..n { + if comp[start] != u32::MAX { + continue; + } + let lab = labels[start]; + let id = start as u32; + comp[start] = id; + stack.push(start); + let mut members = vec![]; + let (mut seeded, mut edge, mut bgs) = (false, false, false); + while let Some(i) = stack.pop() { + members.push(i); + seeded |= seeds[i]; + bgs |= bg_seeds[i]; + let (x, y) = ((i % w) as i64, (i / w) as i64); + edge |= x == 0 || y == 0 || x == w as i64 - 1 || y == h as i64 - 1; + for (dx, dy, _) in NB { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || ny < 0 || nx >= w as i64 || ny >= h as i64 { + continue; + } + let j = ny as usize * w + nx as usize; + if comp[j] == u32::MAX && labels[j] == lab { + comp[j] = id; + stack.push(j); + } + } + } + if lab != 0 { + if seeded { + fg_area += members.len(); + } else { + for i in members { + labels[i] = 0; + } + } + } else if !edge && !bgs { + // Remember holes as negative candidates (resolved below once fg_area is known). + for i in members { + comp[i] = u32::MAX - 1; + } + } + } + // Fill holes smaller than 2 % of the foreground. + let mut seen = vec![false; n]; + for start in 0..n { + if comp[start] != u32::MAX - 1 || seen[start] || labels[start] != 0 { + continue; + } + let mut members = vec![start]; + seen[start] = true; + let mut k = 0; + while k < members.len() { + let i = members[k]; + k += 1; + let (x, y) = ((i % w) as i64, (i / w) as i64); + for (dx, dy, _) in NB { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || ny < 0 || nx >= w as i64 || ny >= h as i64 { + continue; + } + let j = ny as usize * w + nx as usize; + if !seen[j] && comp[j] == u32::MAX - 1 && labels[j] == 0 { + seen[j] = true; + members.push(j); + } + } + } + // A hole is enclosed by foreground that survived the component filter. + let enclosed = members.iter().all(|&i| { + let (x, y) = ((i % w) as i64, (i / w) as i64); + NB.iter().all(|(dx, dy, _)| { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || ny < 0 || nx >= w as i64 || ny >= h as i64 { + return false; + } + let j = ny as usize * w + nx as usize; + labels[j] != 0 || comp[j] == u32::MAX - 1 + }) + }); + if enclosed && (members.len() as f64) < 0.02 * fg_area as f64 { + for i in members { + labels[i] = 1; + } + } + } +} + +fn downsample_rgb(img: &[[f32; 3]], w: usize, h: usize, f: usize) -> (Vec<[f32; 3]>, usize, usize) { + let (cw, ch) = (w.div_ceil(f), h.div_ceil(f)); + let out = (0..cw * ch) + .into_par_iter() + .map(|i| { + let (cx, cy) = (i % cw, i / cw); + let mut s = [0.0f32; 3]; + let mut n = 0.0; + for y in cy * f..((cy + 1) * f).min(h) { + for x in cx * f..((cx + 1) * f).min(w) { + let c = img[y * w + x]; + s[0] += c[0]; + s[1] += c[1]; + s[2] += c[2]; + n += 1.0; + } + } + [s[0] / n, s[1] / n, s[2] / n] + }) + .collect(); + (out, cw, ch) +} + +/// What a frame is segmented with besides its strokes: the matte propagated from its neighbour. +pub struct Prior { + /// Warped neighbour matte, soft (0–1). + pub soft: Vec, + /// The neighbour's colour models. + pub fg: Gmm, + pub bg: Gmm, + /// Warped Refine Edge band. + pub refine: Vec, +} + +/// Segment a frame from its strokes (and a propagated prior): GrabCut iterations on a reduced +/// copy, then a full-resolution cut in a band around the boundary. +pub fn segment(img: &Image, strokes: &[&Stroke], prior: Option<&Prior>, opts: &SegOpts, refine_radius: f64) -> FrameSeg { + let (w, h) = (img.width as usize, img.height as usize); + if w == 0 || h == 0 { + return FrameSeg::empty(w, h); + } + let rgb = rgb_of(img); + let fgs = rasterize(strokes, &[StrokeKind::Fg], opts.scale, w, h); + let bgs = rasterize(strokes, &[StrokeKind::Bg], opts.scale, w, h); + let hard: Vec = fgs + .iter() + .zip(&bgs) + .map(|(f, b)| { + if *b { + HARD_BG + } else if *f { + HARD_FG + } else { + 0 + } + }) + .collect(); + let soft_prior: Option> = prior.map(|p| matting::gauss(&p.soft, w, h, (opts.search_radius * opts.scale / 4.0).max(1.0))); + // Initial colour models: the strokes (and the propagated matte). + let (fg0, bg0) = match prior { + Some(p) => { + let fgc = samples(&rgb, |i| (hard[i] == HARD_FG) || (hard[i] == 0 && p.soft[i] >= 0.5)); + let bgc = samples(&rgb, |i| (hard[i] == HARD_BG) || (hard[i] == 0 && p.soft[i] < 0.5)); + let f = Gmm::fit(&fgc, COMPONENTS, MAX_SAMPLES); + let b = Gmm::fit(&bgc, COMPONENTS, MAX_SAMPLES); + (if f.is_empty() { p.fg.clone() } else { f }, if b.is_empty() { p.bg.clone() } else { b }) + } + None => { + let fgc = samples(&rgb, |i| hard[i] == HARD_FG); + let mut bgc = samples(&rgb, |i| hard[i] == HARD_BG); + if bgc.is_empty() { + // No background strokes: what is far from the foreground strokes. + let d = matting::distance(&fgs, w, h); + let r = strokes.iter().filter(|s| s.kind == StrokeKind::Fg).map(|s| s.radius).fold(1.0, f64::max) * opts.scale; + let far = (4.0 * r).max(0.25 * w.max(h) as f64) as f32; + bgc = samples(&rgb, |i| d[i] > far); + if bgc.is_empty() { + bgc = samples(&rgb, |i| hard[i] != HARD_FG); + } + } + (Gmm::fit(&fgc, COMPONENTS, MAX_SAMPLES), Gmm::fit(&bgc, COMPONENTS, MAX_SAMPLES)) + } + }; + let iters = if opts.best { 4 } else { 3 }; + let work = if opts.best { 960 } else { 480 }; + let f = w.max(h).div_ceil(work).max(1); + let mut labels: Vec = match &soft_prior { + Some(p) => p.iter().map(|v| (*v >= 0.5) as u8).collect(), + None => fgs.iter().map(|v| *v as u8).collect(), + }; + for (l, hd) in labels.iter_mut().zip(&hard) { + if *hd != 0 { + *l = (*hd == HARD_FG) as u8; + } + } + let (mut fgm, mut bgm) = (fg0, bg0); + if f > 1 { + let (crgb, cw, ch) = downsample_rgb(&rgb, w, h, f); + let mut chard = vec![0u8; cw * ch]; + let mut cprior = soft_prior.as_ref().map(|_| vec![0.0f32; cw * ch]); + for cy in 0..ch { + for cx in 0..cw { + let (mut anyf, mut anyb, mut s, mut n) = (false, false, 0.0f32, 0.0f32); + for y in cy * f..((cy + 1) * f).min(h) { + for x in cx * f..((cx + 1) * f).min(w) { + let i = y * w + x; + anyf |= hard[i] == HARD_FG; + anyb |= hard[i] == HARD_BG; + if let Some(p) = &soft_prior { + s += p[i]; + } + n += 1.0; + } + } + chard[cy * cw + cx] = match (anyf, anyb) { + (true, false) => HARD_FG, + (false, true) => HARD_BG, + _ => 0, + }; + if let Some(cp) = &mut cprior { + cp[cy * cw + cx] = s / n; + } + } + } + let mut cl: Vec = match &cprior { + Some(p) => p.iter().map(|v| (*v >= 0.5) as u8).collect(), + None => chard.iter().map(|v| (*v == HARD_FG) as u8).collect(), + }; + let free = vec![true; cw * ch]; + for it in 0..iters { + cut(&crgb, cw, ch, &mut cl, &free, &chard, &fgm, &bgm, cprior.as_deref()); + if it + 1 < iters { + let (a, b) = fit_models(&crgb, &cl); + if !a.is_empty() && !b.is_empty() { + (fgm, bgm) = (a, b); + } + } + } + for y in 0..h { + for x in 0..w { + let i = y * w + x; + labels[i] = if hard[i] != 0 { (hard[i] == HARD_FG) as u8 } else { cl[(y / f) * cw + x / f] }; + } + } + let (a, b) = fit_models(&rgb, &labels); + if !a.is_empty() && !b.is_empty() { + (fgm, bgm) = (a, b); + } + // Full-resolution band. + let sd = matting::signed_distance(&labels, w, h); + let band = f as f32 + 1.5; + let free: Vec = sd.iter().map(|d| d.abs() <= band).collect(); + cut(&rgb, w, h, &mut labels, &free, &hard, &fgm, &bgm, soft_prior.as_deref()); + } else { + let free = vec![true; w * h]; + for it in 0..iters { + cut(&rgb, w, h, &mut labels, &free, &hard, &fgm, &bgm, soft_prior.as_deref()); + if it + 1 < iters { + let (a, b) = fit_models(&rgb, &labels); + if !a.is_empty() && !b.is_empty() { + (fgm, bgm) = (a, b); + } + } + } + } + // Keep what the strokes (or the prior's core) select. + let mut seeds = fgs.clone(); + if let Some(p) = &soft_prior { + let core = core_of(p, w, h, opts.search_radius * opts.scale); + for (s, c) in seeds.iter_mut().zip(core) { + *s |= c; + } + } + clean(&mut labels, &seeds, &bgs, w, h); + finish(img, &rgb, labels, strokes, prior.map(|p| p.refine.as_slice()), opts, refine_radius) +} + +/// Pixels deep inside a soft matte (more than `r` from its edge), else the matte itself. +fn core_of(soft: &[f32], w: usize, h: usize, r: f64) -> Vec { + let bin: Vec = soft.iter().map(|v| (*v >= 0.5) as u8).collect(); + let sd = matting::signed_distance(&bin, w, h); + let core: Vec = sd.iter().map(|d| *d as f64 > r).collect(); + if core.iter().any(|c| *c) { core } else { bin.iter().map(|v| *v != 0).collect() } +} + +/// Final models and Refine Edge band of a segmented frame. +fn finish(_img: &Image, rgb: &[[f32; 3]], labels: Vec, strokes: &[&Stroke], warped_refine: Option<&[u8]>, opts: &SegOpts, refine_radius: f64) -> FrameSeg { + let (w, h) = (_img.width as usize, _img.height as usize); + let (fg, bg) = fit_models(rgb, &labels); + let add = rasterize(strokes, &[StrokeKind::Refine], opts.scale, w, h); + let erase = rasterize(strokes, &[StrokeKind::RefineErase], opts.scale, w, h); + let near: Option> = warped_refine.filter(|r| r.iter().any(|v| *v != 0)).map(|_| matting::signed_distance(&labels, w, h)); + let rr = (refine_radius * opts.scale + 2.0) as f32; + let refine: Vec = (0..w * h) + .map(|i| { + let carried = match (warped_refine, &near) { + (Some(r), Some(sd)) => r[i] != 0 && sd[i].abs() <= rr, + _ => false, + }; + ((carried || add[i]) && !erase[i]) as u8 + }) + .collect(); + FrameSeg { w, h, matte: labels, refine, fg, bg } +} + +/// Warp the previous frame's segmentation onto the next frame by optical flow (next → prev), +/// with Motion Threshold / Damping applied to the vectors. +pub fn warp_prior(prev_img: &Image, prev: &FrameSeg, next_img: &Image, opts: &SegOpts) -> Prior { + let (w, h) = (next_img.width as usize, next_img.height as usize); + let flow = effectcraft_raster::flow::block_flow(next_img, prev_img, 8, 4); + let thr = (opts.motion_threshold / 100.0 * opts.search_radius * opts.scale).max(0.0); + let damp = (1.0 - opts.motion_damping / 100.0).clamp(0.0, 1.0); + let src: Vec = prev.matte.iter().map(|v| *v as f32).collect(); + let pw = prev.w.min(w); + let ph = prev.h.min(h); + let res: Vec<(f32, u8)> = (0..w * h) + .into_par_iter() + .map(|i| { + let (x, y) = ((i % w) as f64 + 0.5, (i / w) as f64 + 0.5); + let v = flow.at(x, y); + let (mut vx, mut vy) = (v[0] as f64, v[1] as f64); + if vx.hypot(vy) < thr { + vx *= damp; + vy *= damp; + } + let (sx, sy) = (x + vx, y + vy); + let soft = if pw == prev.w && ph == prev.h { matting::sample(&src, prev.w, prev.h, sx, sy) } else { 0.0 }; + let (ix, iy) = (sx.floor().clamp(0.0, (prev.w.max(1) - 1) as f64) as usize, sy.floor().clamp(0.0, (prev.h.max(1) - 1) as f64) as usize); + let r = prev.refine.get(iy * prev.w + ix).copied().unwrap_or(0); + (soft, r) + }) + .collect(); + Prior { soft: res.iter().map(|r| r.0).collect(), fg: prev.fg.clone(), bg: prev.bg.clone(), refine: res.iter().map(|r| r.1).collect() } +} + +/// Propagate the previous frame's segmentation to the next frame: warp it, then re-cut in a +/// band of Search Radius pixels around the warped boundary. Strokes on the next frame re-cut +/// the whole frame with the warped matte as a soft prior instead. +pub fn propagate(prev_img: &Image, prev: &FrameSeg, next_img: &Image, strokes: &[&Stroke], opts: &SegOpts, refine_radius: f64) -> FrameSeg { + let (w, h) = (next_img.width as usize, next_img.height as usize); + let prior = warp_prior(prev_img, prev, next_img, opts); + if strokes.iter().any(|s| s.kind.is_segmentation()) { + return segment(next_img, strokes, Some(&prior), opts, refine_radius); + } + let rgb = rgb_of(next_img); + let mut labels: Vec = prior.soft.iter().map(|v| (*v >= 0.5) as u8).collect(); + let sd = matting::signed_distance(&labels, w, h); + let r = (opts.search_radius * opts.scale).max(2.0) as f32; + let free: Vec = sd.iter().map(|d| d.abs() <= r).collect(); + let hard = vec![0u8; w * h]; + let soft = matting::gauss(&prior.soft, w, h, (r as f64 / 4.0).max(1.0)); + let (fg, bg) = if prev.fg.is_empty() || prev.bg.is_empty() { fit_models(&rgb, &labels) } else { (prev.fg.clone(), prev.bg.clone()) }; + cut(&rgb, w, h, &mut labels, &free, &hard, &fg, &bg, Some(&soft)); + let seeds = core_of(&prior.soft, w, h, r as f64); + clean(&mut labels, &seeds, &vec![false; w * h], w, h); + finish(next_img, &rgb, labels, strokes, Some(&prior.refine), opts, refine_radius) +} + +/// Intersection over union of two binary mattes. +pub fn iou(a: &[u8], b: &[u8]) -> f64 { + let (mut i, mut u) = (0usize, 0usize); + for (x, y) in a.iter().zip(b) { + let (x, y) = (*x != 0, *y != 0); + i += (x && y) as usize; + u += (x || y) as usize; + } + if u == 0 { 1.0 } else { i as f64 / u as f64 } +} + +#[cfg(test)] +mod tests; diff --git a/crates/track/src/roto/refine.rs b/crates/track/src/roto/refine.rs new file mode 100644 index 00000000..2a498e28 --- /dev/null +++ b/crates/track/src/roto/refine.rs @@ -0,0 +1,198 @@ +//! From a frame's binary segmentation to the final soft matte: the Roto Brush Matte and Refine +//! Edge Matte adjustments, edge matting inside the Refine Edge band (colour guided filter), +//! Reduce Chatter (temporal averaging with the neighbouring frames), matte motion blur and edge +//! colour decontamination. + +use super::FrameSeg; +use super::matting::{self, contrast, gauss, guided_filter, shift_soft, signed_distance}; + +/// The effect's matte parameters (After Effects' units: pixels and percent). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct MatteParams { + // Roto Brush Matte. + pub rb_feather: f64, + pub rb_contrast: f64, + pub rb_shift: f64, + pub rb_chatter: f64, + // Refine Edge Matte. + pub smooth: f64, + pub feather: f64, + pub contrast: f64, + pub shift: f64, + pub chatter: f64, + pub motion_blur: bool, + pub mb_samples: usize, + pub shutter_angle: f64, + /// Higher Quality: closed-form matting after the guided-filter estimate. + pub higher_quality: bool, + pub decontaminate: bool, + pub decontamination: f64, + pub extend_where_smoothed: bool, + pub increase_radius: f64, +} + +impl Default for MatteParams { + fn default() -> Self { + MatteParams { + rb_feather: 0.0, + rb_contrast: 0.0, + rb_shift: 0.0, + rb_chatter: 0.0, + smooth: 1.0, + feather: 0.0, + contrast: 0.0, + shift: 0.0, + chatter: 0.0, + motion_blur: false, + mb_samples: 11, + shutter_angle: 180.0, + higher_quality: true, + decontaminate: true, + decontamination: 100.0, + extend_where_smoothed: true, + increase_radius: 0.0, + } + } +} + +fn shifted(seg: &FrameSeg, px: f64) -> Vec { + if px.abs() < 0.25 { + return seg.matte.iter().map(|v| *v as f32).collect(); + } + let sd = signed_distance(&seg.matte, seg.w, seg.h); + sd.iter().map(|d| ((*d as f64) > -px) as u8 as f32).collect() +} + +/// The soft matte of `seg` (all planes `seg.w × seg.h`), with `rgb` the frame's straight colour. +/// `prev` / `next`: the neighbouring frames' segmentations (Reduce Chatter, motion blur). +/// `band_radius`: the Refine Edge brush radius in these pixels; `scale`: pixels per layer pixel. +pub fn matte_alpha( + rgb: &[[f32; 3]], + seg: &FrameSeg, + prev: Option<&FrameSeg>, + next: Option<&FrameSeg>, + p: &MatteParams, + band_radius: f64, + scale: f64, +) -> Vec { + let (w, h) = (seg.w, seg.h); + let n = w * h; + let same = |s: &&FrameSeg| s.w == w && s.h == h; + let (prev, next) = (prev.filter(same), next.filter(same)); + // Roto Brush Matte: Shift Edge (100 % = 10 px), Reduce Chatter, Feather, Contrast. + let shift_px = p.rb_shift / 100.0 * 10.0 * scale; + let mut a = shifted(seg, shift_px); + let nb: Vec> = [prev, next].into_iter().flatten().map(|s| shifted(s, shift_px)).collect(); + if p.rb_chatter > 0.0 && !nb.is_empty() { + let k = (p.rb_chatter / 100.0).clamp(0.0, 1.0) as f32 * (nb.len() as f32 / (nb.len() as f32 + 1.0)); + for i in 0..n { + let m = nb.iter().map(|v| v[i]).sum::() / nb.len() as f32; + a[i] += (m - a[i]) * k; + } + } + if p.rb_feather > 0.0 { + a = gauss(&a, w, h, p.rb_feather * scale / 2.0); + } + contrast(&mut a, p.rb_contrast); + // Refine Edge band: guided-filter matting. + if seg.refine.iter().any(|v| *v != 0) && seg.refine.len() == n { + let r = (band_radius * 1.0).round().max(2.0) as usize; + let eps = (2e-4 * (1.0 + p.smooth.max(0.0))) as f32; + // Work on the band's bounding box (plus the filter's reach). + let (mut x0, mut y0, mut x1, mut y1) = (w, h, 0, 0); + for (i, v) in seg.refine.iter().enumerate() { + if *v != 0 { + let (x, y) = (i % w, i / w); + x0 = x0.min(x); + y0 = y0.min(y); + x1 = x1.max(x + 1); + y1 = y1.max(y + 1); + } + } + let pad = 2 * r + 2; + let (x0, y0, x1, y1) = (x0.saturating_sub(pad), y0.saturating_sub(pad), (x1 + pad).min(w), (y1 + pad).min(h)); + let (cw, ch) = (x1 - x0, y1 - y0); + let crop = |src: &dyn Fn(usize) -> f32| -> Vec { (0..cw * ch).map(|j| src((y0 + j / cw) * w + x0 + j % cw)).collect() }; + let cimg: Vec<[f32; 3]> = (0..cw * ch).map(|j| rgb[(y0 + j / cw) * w + x0 + j % cw]).collect(); + let cp = crop(&|i| a[i]); + let band: Vec = crop(&|i| seg.refine[i] as f32).iter().map(|v| *v > 0.0).collect(); + let outside: Vec = band.iter().map(|b| !b).collect(); + // Guided filter estimate, then closed-form matting with everything outside the band known. + let mut q = guided_filter(&cimg, &cp, cw, ch, r, eps); + for j in 0..cw * ch { + q[j] = if band[j] { q[j].clamp(0.0, 1.0) } else { cp[j] }; + } + if p.higher_quality { + q = matting::closed_form(&cimg, &q, &outside, cw, ch, 1, 1e-5 * (1.0 + p.smooth.max(0.0)) as f32, 50); + } + // Refine Edge Matte adjustments. + if p.smooth > 1.0 { + q = gauss(&q, cw, ch, (p.smooth - 1.0) * 0.5 * scale); + } + if p.feather > 0.0 { + q = gauss(&q, cw, ch, p.feather * scale / 2.0); + } + contrast(&mut q, p.contrast); + shift_soft(&mut q, p.shift); + // Blend in, fading over the band's last two pixels. + let din = matting::distance(&outside, cw, ch); + for j in 0..cw * ch { + if !band[j] { + continue; + } + let wgt = (din[j] / 2.0).clamp(0.0, 1.0); + let i = (y0 + j / cw) * w + x0 + j % cw; + a[i] += (q[j] - a[i]) * wgt; + } + // Refine-stage Reduce Chatter: towards the neighbours' mattes inside the band. + if p.chatter > 0.0 && !nb.is_empty() { + let k = (p.chatter / 100.0).clamp(0.0, 1.0) as f32 * 0.5; + let soft: Vec> = nb.iter().map(|v| gauss(v, w, h, (r as f64 / 2.0).max(1.0))).collect(); + for i in 0..n { + if seg.refine[i] != 0 { + let m = soft.iter().map(|v| v[i]).sum::() / soft.len() as f32; + a[i] += (m - a[i]) * k; + } + } + } + } + // Motion blur: the matte's motion between the neighbouring frames. + if p.motion_blur { + let c = seg.centroid(); + let v = match (prev.and_then(FrameSeg::centroid), next.and_then(FrameSeg::centroid), c) { + (Some(a), Some(b), _) => Some([(b[0] - a[0]) / 2.0, (b[1] - a[1]) / 2.0]), + (Some(a), None, Some(c)) => Some([c[0] - a[0], c[1] - a[1]]), + (None, Some(b), Some(c)) => Some([b[0] - c[0], b[1] - c[1]]), + _ => None, + }; + if let Some(v) = v { + let s = p.shutter_angle / 360.0; + a = matting::motion_blur(&a, w, h, [v[0] * s, v[1] * s], p.mb_samples); + } + } + a.iter_mut().for_each(|v| *v = v.clamp(0.0, 1.0)); + a +} + +/// Decontaminate edge colours of `rgb` in place for the matte `alpha` (see +/// [`matting::decontaminate`]); returns the decontamination map. `band`: the Refine Edge band +/// (pixels where decontamination always applies). +pub fn decontaminate(rgb: &mut [[f32; 3]], alpha: &[f32], band: &[u8], w: usize, h: usize, p: &MatteParams, band_radius: f64, scale: f64) -> Vec { + if !p.decontaminate { + return vec![0.0; w * h]; + } + let grow = p.increase_radius * scale; + let in_band: Vec = if band.len() == w * h && band.iter().any(|v| *v != 0) { + if grow > 0.5 { + let d = matting::distance(&band.iter().map(|v| *v != 0).collect::>(), w, h); + d.iter().map(|v| (*v as f64) <= grow).collect() + } else { + band.iter().map(|v| *v != 0).collect() + } + } else { + vec![false; w * h] + }; + let region: Vec = (0..w * h).map(|i| alpha[i] > 0.004 && alpha[i] < 0.996 && (in_band[i] || p.extend_where_smoothed)).collect(); + let radius = (band_radius + grow).max(4.0 * scale).round() as usize; + matting::decontaminate(rgb, alpha, ®ion, w, h, radius, (p.decontamination / 100.0).clamp(0.0, 1.0) as f32) +} diff --git a/crates/track/src/roto/rle.rs b/crates/track/src/roto/rle.rs new file mode 100644 index 00000000..308b2689 --- /dev/null +++ b/crates/track/src/roto/rle.rs @@ -0,0 +1,136 @@ +//! Compact text encodings of mattes for project storage: run-length coding (LEB128 varints) +//! wrapped in standard base64 (RFC 4648 §4). + +const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +pub fn base64_encode(data: &[u8]) -> String { + let mut out = String::with_capacity(data.len().div_ceil(3) * 4); + for c in data.chunks(3) { + let b = [c[0], *c.get(1).unwrap_or(&0), *c.get(2).unwrap_or(&0)]; + let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32; + out.push(B64[(n >> 18) as usize & 63] as char); + out.push(B64[(n >> 12) as usize & 63] as char); + out.push(if c.len() > 1 { B64[(n >> 6) as usize & 63] as char } else { '=' }); + out.push(if c.len() > 2 { B64[n as usize & 63] as char } else { '=' }); + } + out +} + +pub fn base64_decode(s: &str) -> Option> { + let val = |c: u8| -> Option { + Some(match c { + b'A'..=b'Z' => (c - b'A') as u32, + b'a'..=b'z' => (c - b'a' + 26) as u32, + b'0'..=b'9' => (c - b'0' + 52) as u32, + b'+' => 62, + b'/' => 63, + _ => return None, + }) + }; + let bytes: Vec = s.bytes().filter(|c| !c.is_ascii_whitespace()).collect(); + if !bytes.len().is_multiple_of(4) { + return None; + } + let mut out = Vec::with_capacity(bytes.len() / 4 * 3); + for c in bytes.chunks(4) { + let pad = c.iter().rev().take_while(|b| **b == b'=').count(); + let mut n = 0u32; + for (i, b) in c.iter().enumerate() { + n |= if *b == b'=' { 0 } else { val(*b)? } << (18 - 6 * i); + } + out.push((n >> 16) as u8); + if pad < 2 { + out.push((n >> 8) as u8); + } + if pad < 1 { + out.push(n as u8); + } + } + Some(out) +} + +fn put(out: &mut Vec, mut v: u64) { + loop { + let b = (v & 0x7f) as u8; + v >>= 7; + if v == 0 { + out.push(b); + return; + } + out.push(b | 0x80); + } +} + +fn get(data: &[u8], pos: &mut usize) -> Option { + let mut v = 0u64; + let mut shift = 0; + loop { + let b = *data.get(*pos)?; + *pos += 1; + v |= ((b & 0x7f) as u64) << shift; + if b & 0x80 == 0 { + return Some(v); + } + shift += 7; + if shift > 63 { + return None; + } + } +} + +/// Encode a byte plane as (value, run length) pairs. +pub fn encode(values: &[u8]) -> String { + let mut out = vec![]; + put(&mut out, values.len() as u64); + let mut i = 0; + while i < values.len() { + let v = values[i]; + let mut j = i + 1; + while j < values.len() && values[j] == v { + j += 1; + } + out.push(v); + put(&mut out, (j - i) as u64); + i = j; + } + base64_encode(&out) +} + +/// Decode [`encode`]'s output (None when malformed). +pub fn decode(s: &str) -> Option> { + let data = base64_decode(s)?; + let mut pos = 0; + let n = get(&data, &mut pos)? as usize; + if n > 1 << 30 { + return None; + } + let mut out = Vec::with_capacity(n); + while out.len() < n { + let v = *data.get(pos)?; + pos += 1; + let run = get(&data, &mut pos)? as usize; + if out.len() + run > n { + return None; + } + out.resize(out.len() + run, v); + } + Some(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn round_trips() { + for n in [0usize, 1, 2, 3, 4, 5, 100, 1000] { + let v: Vec = (0..n).map(|i| ((i / 7) % 3) as u8 * 100).collect(); + assert_eq!(decode(&encode(&v)).unwrap(), v); + let raw: Vec = (0..n).map(|i| (i * 31) as u8).collect(); + assert_eq!(base64_decode(&base64_encode(&raw)).unwrap(), raw); + } + assert_eq!(base64_encode(b"Man"), "TWFu"); + assert_eq!(base64_encode(b"Ma"), "TWE="); + assert!(decode("!!!!").is_none()); + } +} diff --git a/crates/track/src/roto/tests.rs b/crates/track/src/roto/tests.rs new file mode 100644 index 00000000..bec3f378 --- /dev/null +++ b/crates/track/src/roto/tests.rs @@ -0,0 +1,331 @@ +//! Roto Brush on synthetic sequences: a textured disk moving over a textured background. + +use super::refine::{MatteParams, decontaminate, matte_alpha}; +use super::*; + +pub(crate) const W: u32 = 200; +pub(crate) const H: u32 = 150; +pub(crate) const R: f64 = 32.0; + +/// Smooth value noise in 0..1. +pub(crate) fn noise(x: f64, y: f64, seed: u32) -> f64 { + fn h(i: i64, j: i64, s: u32) -> f64 { + let mut v = (i.wrapping_mul(73856093) ^ j.wrapping_mul(19349663) ^ (s as i64).wrapping_mul(83492791)) as u64; + v ^= v >> 13; + v = v.wrapping_mul(0x5bd1e995); + v ^= v >> 15; + (v % 10_000) as f64 / 10_000.0 + } + let mut out = 0.0; + for (cell, amp) in [(9.0, 0.6), (3.5, 0.4)] { + let (fx, fy) = (x / cell, y / cell); + let (i, j) = (fx.floor() as i64, fy.floor() as i64); + let (tx, ty) = (fx - i as f64, fy - j as f64); + let (sx, sy) = (tx * tx * (3.0 - 2.0 * tx), ty * ty * (3.0 - 2.0 * ty)); + let a = h(i, j, seed) + (h(i + 1, j, seed) - h(i, j, seed)) * sx; + let b = h(i, j + 1, seed) + (h(i + 1, j + 1, seed) - h(i, j + 1, seed)) * sx; + out += amp * (a + (b - a) * sy); + } + out +} + +pub(crate) fn fg_color(x: f64, y: f64) -> [f32; 3] { + let n = noise(x, y, 11); + [(0.75 + 0.25 * n) as f32, (0.35 + 0.3 * n) as f32, (0.1 + 0.15 * n) as f32] +} + +pub(crate) fn bg_color(x: f64, y: f64) -> [f32; 3] { + let n = noise(x, y, 5); + let m = noise(x * 1.7, y * 1.7, 9); + [(0.05 + 0.25 * m) as f32, (0.3 + 0.4 * n) as f32, (0.45 + 0.4 * m) as f32] +} + +pub(crate) fn center(f: i64) -> [f64; 2] { + let t = f as f64; + [60.0 + 3.1 * t, 70.0 + 1.3 * t + 4.0 * (t * 0.3).sin()] +} + +/// Frame `f`: the disk (texture moving with it) over the static background, with a 1-pixel +/// anti-aliased edge; `c` overrides the centre. +pub(crate) fn frame_at(c: [f64; 2]) -> (Image, Vec) { + let mut img = Image::new(W, H); + let mut gt = vec![0u8; (W * H) as usize]; + for y in 0..H { + for x in 0..W { + let p = [x as f64 + 0.5, y as f64 + 0.5]; + let d = (p[0] - c[0]).hypot(p[1] - c[1]); + let a = (R + 0.5 - d).clamp(0.0, 1.0) as f32; + let f = fg_color(p[0] - c[0], p[1] - c[1]); + let b = bg_color(p[0], p[1]); + img.set(x, y, [f[0] * a + b[0] * (1.0 - a), f[1] * a + b[1] * (1.0 - a), f[2] * a + b[2] * (1.0 - a), 1.0]); + gt[(y * W + x) as usize] = (d <= R) as u8; + } + } + (img, gt) +} + +fn line(kind: StrokeKind, frame: i64, a: [f64; 2], b: [f64; 2], radius: f64) -> Stroke { + Stroke { kind, frame, radius, points: (0..=10).map(|k| [a[0] + (b[0] - a[0]) * k as f64 / 10.0, a[1] + (b[1] - a[1]) * k as f64 / 10.0]).collect() } +} + +/// The strokes an artist would paint on frame `f`: one through the disk, one around it. +pub(crate) fn base_strokes(f: i64) -> Vec { + let c = center(f); + vec![ + line(StrokeKind::Fg, f, [c[0] - 18.0, c[1] - 6.0], [c[0] + 18.0, c[1] + 6.0], 5.0), + line(StrokeKind::Bg, f, [c[0] - 50.0, c[1] - 45.0], [c[0] + 50.0, c[1] - 45.0], 5.0), + line(StrokeKind::Bg, f, [c[0] + 50.0, c[1] + 45.0], [c[0] - 45.0, c[1] + 45.0], 5.0), + ] +} + +#[test] +fn strokes_segment_the_base_frame() { + let (img, gt) = frame_at(center(0)); + let st = base_strokes(0); + let refs: Vec<&Stroke> = st.iter().collect(); + let seg = segment(&img, &refs, None, &SegOpts::default(), 0.0); + let j = iou(&seg.matte, >); + assert!(j > 0.95, "IoU {j}"); + // Deterministic. + let again = segment(&img, &refs, None, &SegOpts::default(), 0.0); + assert_eq!(seg, again); +} + +#[test] +fn coarse_to_fine_segments_large_frames() { + // A 2× upscaled frame goes through the reduced-size path (Standard works at 480 px). + let (small, _) = frame_at(center(0)); + let img = effectcraft_raster::resample(&small, W * 3, H * 3); + let c = center(0); + let st: Vec = base_strokes(0) + .into_iter() + .map(|mut s| { + s.points.iter_mut().for_each(|p| *p = [p[0] * 3.0, p[1] * 3.0]); + s.radius *= 3.0; + s + }) + .collect(); + let refs: Vec<&Stroke> = st.iter().collect(); + let seg = segment(&img, &refs, None, &SegOpts::default(), 0.0); + let gt: Vec = + (0..W * H * 9).map(|i| ((((i % (W * 3)) as f64 + 0.5) / 3.0 - c[0]).hypot((((i / (W * 3)) as f64 + 0.5) / 3.0) - c[1]) <= R) as u8).collect(); + let j = iou(&seg.matte, >); + assert!(j > 0.95, "IoU {j}"); +} + +#[test] +fn propagation_follows_the_disk_for_20_frames() { + let (img0, _) = frame_at(center(0)); + let st = base_strokes(0); + let refs: Vec<&Stroke> = st.iter().collect(); + let opts = SegOpts::default(); + let mut seg = segment(&img0, &refs, None, &opts, 0.0); + let mut prev = img0; + let mut worst: f64 = 1.0; + for f in 1..=20 { + let (img, gt) = frame_at(center(f)); + seg = propagate(&prev, &seg, &img, &[], &opts, 0.0); + let j = iou(&seg.matte, >); + worst = worst.min(j); + prev = img; + } + assert!(worst > 0.9, "worst IoU {worst}"); +} + +#[test] +fn a_correction_stroke_fixes_a_bad_frame() { + let opts = SegOpts::default(); + let (img0, _) = frame_at(center(0)); + let st = base_strokes(0); + let refs: Vec<&Stroke> = st.iter().collect(); + let seg0 = segment(&img0, &refs, None, &opts, 0.0); + // The disk jumps far away on frame 1: propagation cannot follow. + let jump = [150.0, 90.0]; + let (img1, gt1) = frame_at(jump); + let bad = propagate(&img0, &seg0, &img1, &[], &opts, 0.0); + assert!(iou(&bad.matte, >1) < 0.5, "the jump should break propagation"); + // Paint the disk in and the old place out. + let c0 = center(0); + let fix = [ + line(StrokeKind::Fg, 1, [jump[0] - 15.0, jump[1]], [jump[0] + 15.0, jump[1]], 5.0), + line(StrokeKind::Bg, 1, [c0[0] - 10.0, c0[1] - 10.0], [c0[0] + 10.0, c0[1] + 10.0], 6.0), + ]; + let fr: Vec<&Stroke> = fix.iter().collect(); + let good = propagate(&img0, &seg0, &img1, &fr, &opts, 0.0); + let j = iou(&good.matte, >1); + assert!(j > 0.95, "corrected IoU {j}"); + // And propagation continues from the corrected frame. + let c2 = [jump[0] + 3.0, jump[1] + 1.0]; + let (img2, gt2) = frame_at(c2); + let next = propagate(&img1, &good, &img2, &[], &opts, 0.0); + assert!(iou(&next.matte, >2) > 0.93); +} + +/// A soft-edged disk (alpha ramp over `width` pixels) over a background. +fn soft_frame(c: [f64; 2], width: f64, fg: [f32; 3], bg: [f32; 3]) -> (Image, Vec) { + let mut img = Image::new(W, H); + let mut gt = vec![0.0f32; (W * H) as usize]; + for y in 0..H { + for x in 0..W { + let p = [x as f64 + 0.5, y as f64 + 0.5]; + let d = (p[0] - c[0]).hypot(p[1] - c[1]); + let a = ((R + width / 2.0 - d) / width).clamp(0.0, 1.0) as f32; + // Faint texture on both layers. + let tf = (noise(p[0], p[1], 3) as f32 - 0.5) * 0.04; + let tb = (noise(p[0], p[1], 4) as f32 - 0.5) * 0.04; + let f = [fg[0] + tf, fg[1] + tf, fg[2] + tf]; + let b = [bg[0] + tb, bg[1] + tb, bg[2] + tb]; + img.set(x, y, [f[0] * a + b[0] * (1.0 - a), f[1] * a + b[1] * (1.0 - a), f[2] * a + b[2] * (1.0 - a), 1.0]); + gt[(y * W + x) as usize] = a; + } + } + (img, gt) +} + +fn ring(frame: i64, c: [f64; 2], r: f64, radius: f64) -> Stroke { + Stroke { + kind: StrokeKind::Refine, + frame, + radius, + points: (0..=48).map(|k| k as f64 / 48.0 * std::f64::consts::TAU).map(|a| [c[0] + r * a.cos(), c[1] + r * a.sin()]).collect(), + } +} + +#[test] +fn refine_edge_recovers_a_soft_edge_and_decontaminates() { + let c = [100.0, 75.0]; + let (fgc, bgc) = ([0.9f32, 0.25, 0.1], [0.1f32, 0.3, 0.9]); + let (img, gt) = soft_frame(c, 10.0, fgc, bgc); + let mut st = vec![ + line(StrokeKind::Fg, 0, [c[0] - 15.0, c[1]], [c[0] + 15.0, c[1]], 5.0), + line(StrokeKind::Bg, 0, [c[0] - 60.0, c[1] - 55.0], [c[0] + 60.0, c[1] - 55.0], 5.0), + ]; + st.push(ring(0, c, R, 9.0)); + let refs: Vec<&Stroke> = st.iter().collect(); + let seg = segment(&img, &refs, None, &SegOpts::default(), 9.0); + assert!(seg.refine.iter().filter(|v| **v != 0).count() > 1000); + let p = MatteParams::default(); + let rgb = rgb_of(&img); + let a = matte_alpha(&rgb, &seg, None, None, &p, 9.0, 1.0); + // Inside the ramp the matte is fractional and close to the truth. + let (mut err, mut n, mut frac) = (0.0f64, 0usize, 0usize); + let (mut err_bin, mut n_bin) = (0.0f64, 0usize); + for i in 0..a.len() { + if gt[i] > 0.0 && gt[i] < 1.0 { + err += (a[i] - gt[i]).abs() as f64; + err_bin += (seg.matte[i] as f32 - gt[i]).abs() as f64; + n += 1; + n_bin += 1; + frac += (a[i] > 0.05 && a[i] < 0.95) as usize; + } + } + let (err, err_bin) = (err / n as f64, err_bin / n_bin as f64); + assert!(err < 0.03, "mean alpha error {err} (binary {err_bin})"); + assert!(err < err_bin * 0.4); + assert!(frac as f64 > 0.6 * n as f64, "{frac}/{n} fractional"); + // Decontamination: edge pixels' colour is the foreground's, not a blue blend. + let mut col = rgb.clone(); + let map = decontaminate(&mut col, &a, &seg.refine, W as usize, H as usize, &p, 9.0, 1.0); + let (mut blue_before, mut blue_after, mut m) = (0.0f64, 0.0f64, 0usize); + for i in 0..a.len() { + if a[i] > 0.2 && a[i] < 0.8 { + blue_before += rgb[i][2] as f64; + blue_after += col[i][2] as f64; + m += 1; + } + } + let (bb, ba) = (blue_before / m as f64, blue_after / m as f64); + assert!(ba < 0.2 && bb > 0.35, "blue before {bb}, after {ba}"); + assert!(map.iter().any(|v| *v > 0.2)); + // Without the band, the binary matte passes through. + let mut seg2 = seg.clone(); + seg2.refine.iter_mut().for_each(|v| *v = 0); + let a2 = matte_alpha(&rgb, &seg2, None, None, &p, 9.0, 1.0); + assert!(a2.iter().zip(&seg.matte).all(|(x, y)| (*x - *y as f32).abs() < 1e-6)); +} + +#[test] +fn reduce_chatter_and_motion_blur_use_the_neighbours() { + let (w, h) = (40usize, 20usize); + let mk = |x0: usize| FrameSeg { + w, + h, + matte: (0..w * h).map(|i| (i % w >= x0 && i % w < x0 + 10) as u8).collect(), + refine: vec![0; w * h], + ..Default::default() + }; + let (a, b, c) = (mk(10), mk(14), mk(18)); + let rgb = vec![[0.5f32; 3]; w * h]; + let p = MatteParams { rb_chatter: 100.0, ..Default::default() }; + let al = matte_alpha(&rgb, &b, Some(&a), Some(&c), &p, 0.0, 1.0); + // Pixel 12 is only in the previous frame: partially on. + assert!(al[12] > 0.2 && al[12] < 0.5, "{}", al[12]); + let p = MatteParams { motion_blur: true, ..Default::default() }; + let al = matte_alpha(&rgb, &b, Some(&a), Some(&c), &p, 0.0, 1.0); + assert!(al[14] > 0.3 && al[14] < 0.9, "{}", al[14]); + assert!(al[19] > 0.99); +} + +#[test] +fn chain_keys_restart_at_correction_frames() { + let mut d = RotoData::default(); + for s in base_strokes(5) { + d.add(s, 20, [0, 30]); + } + assert_eq!(d.base, Some(5)); + assert_eq!(d.span, [0, 25]); + let k1 = d.chain_keys(1); + d.add(line(StrokeKind::Fg, 12, [0.0, 0.0], [1.0, 1.0], 3.0), 20, [0, 30]); + let k2 = d.chain_keys(1); + for f in 0..=11 { + assert_eq!(k1[&f], k2[&f], "frame {f}"); + } + for f in 12..=25 { + assert_ne!(k1[&f], k2[&f], "frame {f}"); + } + assert_ne!(d.chain_keys(2)[&5], k2[&5]); + let back = RotoData::from_json(&d.to_json()); + assert_eq!(back, d); + assert_eq!(d.source_of(12), Some(11)); + assert_eq!(d.source_of(3), Some(4)); + assert_eq!(d.source_of(5), None); +} + +/// `cargo test -p effectcraft-track --release roto_perf -- --ignored --nocapture` +#[test] +#[ignore] +fn roto_perf_1080p() { + let up = |c: [f64; 2]| { + let (img, _) = frame_at(c); + effectcraft_raster::resample(&img, 1920, 1080) + }; + let s = 1920.0 / W as f64; + let st: Vec = base_strokes(0) + .into_iter() + .map(|mut k| { + k.points.iter_mut().for_each(|p| *p = [p[0] * s, p[1] * s]); + k.radius *= s; + k + }) + .collect(); + let refs: Vec<&Stroke> = st.iter().collect(); + let opts = SegOpts { search_radius: 15.0 * s / 4.0, ..Default::default() }; + let a = up(center(0)); + let t = std::time::Instant::now(); + let seg = segment(&a, &refs, None, &opts, 0.0); + eprintln!("segment 1080p: {:?}", t.elapsed()); + let b = up(center(1)); + let t = std::time::Instant::now(); + let next = propagate(&a, &seg, &b, &[], &opts, 0.0); + eprintln!("propagate 1080p: {:?} (area {})", t.elapsed(), next.area()); + let mut seg2 = next.clone(); + let c = center(1); + let ring: Vec<[f64; 2]> = (0..=64).map(|k| k as f64 / 64.0 * std::f64::consts::TAU).map(|t| [(c[0] + R * t.cos()) * s, (c[1] + R * t.sin()) * s]).collect(); + let st = [Stroke { kind: StrokeKind::Refine, frame: 1, radius: 20.0, points: ring }]; + let r = rasterize(&st.iter().collect::>(), &[StrokeKind::Refine], 1.0, 1920, 1080); + seg2.refine = r.iter().map(|v| *v as u8).collect(); + let rgb = rgb_of(&b); + let t = std::time::Instant::now(); + let _ = matte_alpha(&rgb, &seg2, None, None, &MatteParams::default(), 20.0, 1.0); + eprintln!("refine 1080p: {:?}", t.elapsed()); +} From 8404fead60a35f3007f6fe1dae80530d48716a21 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:21:08 -0400 Subject: [PATCH 17/23] UI-8, UI-3: native macOS menu bar; timeline columns, search, reveal-add, UU, CTI snapping Native menu bar (apps/effectcraft, muda, macOS only): the engine's After Effects menu tree mirrored into NSMenu through a pure, serializable spec (ui-egui native_menu: submenus, separators, live labels, check marks, enabled state, Cmd/Ctrl/function-key shortcuts as accelerators, Open Recent). Hide / Hide Others / Show All are macOS' predefined items (app.hideOthers/app.showAll are real commands now, via NSApplication); Services added; About, Settings and Quit dispatch our commands. State is pushed only when the session revision, selection, history, settings or journal change; structural changes rebuild. Activations dispatch the same command ids as the in-window bar; Edit Cut/Copy/Paste/Select All/Undo/Redo act on a focused text field. Settings > Appearance > Use In-Window Menu Bar on macOS switches back. Timeline: column visibility from the column header's context menu (A/V Features, Keys, Label, #, Comment, Switches, Modes, Parent & Link, In, Out, Duration, Stretch; Source/Layer Name toggle), Comment editing (layer.setComment), draggable In/Out/Duration cells, Stretch opens Time Stretch; property search filters layers and reveals matching properties with their groups; Shift+P/S/R/T/A/E/M/F/L/U add to the revealed properties; UU reveals all modified properties; Shift-drag in the ruler snaps the CTI to keys, in/out points, markers and the work area. Commands: timeline.column, timeline.sourceName, timeline.search, timeline.revealAdd.*. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 42 ++ apps/effectcraft/Cargo.toml | 6 + apps/effectcraft/src/main.rs | 40 +- apps/effectcraft/src/native_menu.rs | 210 ++++++ crates/engine/src/commands/frontend.rs | 3 + crates/engine/src/commands/layer.rs | 15 + crates/engine/src/commands/stubs.rs | 3 - crates/engine/src/prefs.rs | 4 + crates/ui-egui/src/lib.rs | 6 + crates/ui-egui/src/menus.rs | 113 ++- crates/ui-egui/src/native_menu.rs | 459 ++++++++++++ crates/ui-egui/src/panels/timeline.rs | 931 ++++++++++++++++++++----- crates/ui-egui/src/state.rs | 14 + 13 files changed, 1640 insertions(+), 206 deletions(-) create mode 100644 apps/effectcraft/src/native_menu.rs create mode 100644 crates/ui-egui/src/native_menu.rs diff --git a/Cargo.lock b/Cargo.lock index 871c9fd8..fb72a303 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -971,6 +971,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-deque" version = "0.8.8" @@ -1133,6 +1142,10 @@ dependencies = [ "effectcraft-ui-egui", "eframe", "egui", + "log", + "muda", + "objc2 0.6.4", + "objc2-app-kit 0.3.2", "objc2-foundation 0.3.2", "rfd", "serde_json", @@ -2619,6 +2632,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "keyboard-types" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fbe853b403ae61a04233030ae8a79d94975281ed9770a1f9e246732b534b28d" +dependencies = [ + "bitflags 2.13.2", + "serde", +] + [[package]] name = "khronos-egl" version = "6.0.0" @@ -2826,6 +2849,25 @@ dependencies = [ "pxfm", ] +[[package]] +name = "muda" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6744ab645ebe79f9c428455c7cb9dfa4275fa3b50a25408e08f927da8dc618c" +dependencies = [ + "crossbeam-channel", + "dpi", + "keyboard-types", + "objc2 0.6.4", + "objc2-app-kit 0.3.2", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "once_cell", + "png", + "thiserror 2.0.21", + "windows-sys 0.61.2", +] + [[package]] name = "multiversion_no_op" version = "1.0.0" diff --git a/apps/effectcraft/Cargo.toml b/apps/effectcraft/Cargo.toml index 6b34763f..3d5e1bb1 100644 --- a/apps/effectcraft/Cargo.toml +++ b/apps/effectcraft/Cargo.toml @@ -18,6 +18,7 @@ eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", " serde_json = { workspace = true } rfd = { workspace = true } cpal = { workspace = true } +log = { workspace = true } [build-dependencies] # Icon + VERSIONINFO for effectcraft.exe; build.rs only uses it when targeting Windows. @@ -28,3 +29,8 @@ winresource = "0.1" winit = { version = "0.30", default-features = false } # Keep the control channel responsive when the window is covered (App Nap / occlusion throttling). objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSProcessInfo", "NSString", "NSObject"] } +# The native menu bar (NSMenu). MIT OR Apache-2.0; macOS only, never in the web build. +muda = { version = "0.21", default-features = false } +objc2 = "0.6" +# NSApplication hide / hide others / show all, and the pasteboard for Edit ▸ Paste in text fields. +objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSResponder", "NSPasteboard"] } diff --git a/apps/effectcraft/src/main.rs b/apps/effectcraft/src/main.rs index 34b40a84..b6426f5f 100644 --- a/apps/effectcraft/src/main.rs +++ b/apps/effectcraft/src/main.rs @@ -7,6 +7,8 @@ mod audio_out; mod control_server; +#[cfg(target_os = "macos")] +mod native_menu; use effectcraft_ui_egui::EffectcraftApp; use serde_json::json; @@ -100,16 +102,52 @@ fn main() -> eframe::Result { app.hooks.pick_save_file = Some(Box::new(|name: &str, ext: &str| { rfd::FileDialog::new().add_filter(ext, &[ext]).set_file_name(name).save_file().map(|p| p.to_string_lossy().to_string()) })); + #[cfg(target_os = "macos")] + { + app.hooks.app_action = Some(Box::new(native_menu::app_action)); + app.hooks.clipboard_text = Some(Box::new(native_menu::clipboard_text)); + } if let Some(port) = control_port { disable_app_nap(); let rx = control_server::start(port, cc.egui_ctx.clone()); app = app.with_control(rx); } - Ok(Box::new(app)) + Ok(Box::new(Desktop { + #[cfg(target_os = "macos")] + menu: native_menu::NativeBar::new(&cc.egui_ctx), + app, + })) }), ) } +/// The desktop app: the egui app plus, on macOS, the system menu bar. +struct Desktop { + app: EffectcraftApp, + #[cfg(target_os = "macos")] + menu: native_menu::NativeBar, +} + +impl eframe::App for Desktop { + fn logic(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { + #[cfg(target_os = "macos")] + self.menu.update(&mut self.app, ctx); + self.app.logic(ctx, frame); + } + + fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { + self.app.ui(ui, frame); + } + + fn on_exit(&mut self) { + self.app.on_exit(); + } + + fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) { + self.app.raw_input_hook(ctx, raw_input); + } +} + /// The platform config directory for EffectCraft (`EFFECTCRAFT_CONFIG_DIR` overrides): /// `~/Library/Application Support/EffectCraft` (macOS), `%APPDATA%\EffectCraft` (Windows), /// `$XDG_CONFIG_HOME/effectcraft` or `~/.config/effectcraft` (Linux and others). diff --git a/apps/effectcraft/src/native_menu.rs b/apps/effectcraft/src/native_menu.rs new file mode 100644 index 00000000..a20b27f5 --- /dev/null +++ b/apps/effectcraft/src/native_menu.rs @@ -0,0 +1,210 @@ +//! macOS: the menu bar in the system menu bar (`NSMenu` through `muda`), built from +//! [`effectcraft_ui_egui::native_menu`]'s spec of the engine's After Effects menu tree. +//! +//! Each frame [`NativeBar::update`] runs menu activations (the same command ids as the in-window +//! bar), and when [`native_menu::state_key`] changed it rebuilds the spec and pushes labels, +//! enabled and check state into the existing items; a structural change (Open Recent, a new +//! shortcut preset) rebuilds the menu. Settings ▸ Appearance ▸ Use In-Window Menu Bar on macOS +//! swaps back to the in-window bar. + +use std::collections::HashMap; +use std::sync::mpsc::{Receiver, channel}; + +use effectcraft_ui_egui::EffectcraftApp; +use effectcraft_ui_egui::native_menu::{self, NativeMenu, NativeNode, Role}; +use muda::{CheckMenuItem, Menu, MenuEvent, PredefinedMenuItem, Submenu}; + +/// The `muda` accelerator for a spec accelerator string. +pub fn parse_accelerator(s: &str) -> Option { + s.parse().ok() +} + +pub struct NativeBar { + menu: Option

, + items: HashMap, + spec: NativeMenu, + structure: u64, + state: Option, + events: Receiver, + /// The in-window-bar setting last applied (None: not yet). + in_window: Option, +} + +impl NativeBar { + pub fn new(ctx: &egui::Context) -> Self { + let (tx, rx) = channel(); + let ctx = ctx.clone(); + MenuEvent::set_event_handler(Some(move |e: MenuEvent| { + let _ = tx.send(e); + ctx.request_repaint(); + })); + NativeBar { menu: None, items: HashMap::new(), spec: NativeMenu::default(), structure: 0, state: None, events: rx, in_window: None } + } + + /// Run activations and keep the native menu in step with the app (call once per frame). + pub fn update(&mut self, app: &mut EffectcraftApp, ctx: &egui::Context) { + let in_window = app.session.prefs.appearance.in_window_menu_bar_mac; + if self.in_window != Some(in_window) { + self.in_window = Some(in_window); + app.ui.show_menu_bar = in_window; + if in_window { + if let Some(m) = self.menu.take() { + m.remove_for_nsapp(); + } + self.items.clear(); + } else { + self.state = None; + self.structure = 0; + } + } + while let Ok(e) = self.events.try_recv() { + if let Err(err) = native_menu::activate(app, ctx, &self.spec, &e.id.0) { + log::warn!("menu: {err}"); + } + // Check items toggle themselves when clicked: resync with the app. + self.state = None; + } + if in_window { + return; + } + let key = native_menu::state_key(app); + if self.state == Some(key) && self.menu.is_some() { + return; + } + self.state = Some(key); + let spec = native_menu::build(app); + let structure = spec.structure(); + if self.menu.is_none() || structure != self.structure { + self.install(&spec); + self.structure = structure; + } else { + self.refresh(&spec); + } + self.spec = spec; + } + + fn install(&mut self, spec: &NativeMenu) { + if let Some(m) = self.menu.take() { + m.remove_for_nsapp(); + } + self.items.clear(); + let menu = Menu::new(); + for node in &spec.menus { + if let NativeNode::Submenu { label, children } = node { + let sub = Submenu::new(label, true); + self.fill(&sub, children); + match label.as_str() { + "Window" => sub.set_as_windows_menu_for_nsapp(), + "Help" => sub.set_as_help_menu_for_nsapp(), + _ => {} + } + if let Err(e) = menu.append(&sub) { + log::warn!("menu {label}: {e}"); + } + } + } + menu.init_for_nsapp(); + self.menu = Some(menu); + } + + fn fill(&mut self, parent: &Submenu, nodes: &[NativeNode]) { + for n in nodes { + let r = match n { + NativeNode::Separator => parent.append(&PredefinedMenuItem::separator()), + NativeNode::Predefined { role, label, .. } => { + let item = match role { + Role::Services => PredefinedMenuItem::services(Some(label)), + Role::Hide => PredefinedMenuItem::hide(Some(label)), + Role::HideOthers => PredefinedMenuItem::hide_others(Some(label)), + Role::ShowAll => PredefinedMenuItem::show_all(Some(label)), + }; + parent.append(&item) + } + NativeNode::Submenu { label, children } => { + let sub = Submenu::new(label, true); + self.fill(&sub, children); + parent.append(&sub) + } + NativeNode::Item(i) => { + let accel = i.accelerator.as_deref().and_then(parse_accelerator); + let item = CheckMenuItem::with_id(i.id.clone(), &i.label, i.enabled, i.checked == Some(true), accel); + let r = parent.append(&item); + self.items.insert(i.id.clone(), item); + r + } + }; + if let Err(e) = r { + log::warn!("menu item: {e}"); + } + } + } + + /// Push label / enabled / check changes into the existing items. + fn refresh(&self, spec: &NativeMenu) { + let old: HashMap<&str, &native_menu::NativeItem> = self.spec.items().into_iter().map(|i| (i.id.as_str(), i)).collect(); + for i in spec.items() { + let Some(native) = self.items.get(&i.id) else { continue }; + let prev = old.get(i.id.as_str()); + if prev.is_none_or(|p| p.label != i.label) { + native.set_text(&i.label); + } + if prev.is_none_or(|p| p.enabled != i.enabled) { + native.set_enabled(i.enabled); + } + let checked = i.checked == Some(true); + if native.is_checked() != checked { + native.set_checked(checked); + } + } + } +} + +/// `app.hide` / `app.hideOthers` / `app.showAll` through NSApplication (main thread only). +pub fn app_action(id: &str) -> bool { + use objc2::MainThreadMarker; + use objc2_app_kit::NSApplication; + let Some(mtm) = MainThreadMarker::new() else { return false }; + let app = NSApplication::sharedApplication(mtm); + match id { + "app.hide" => app.hide(None), + "app.hideOthers" => app.hideOtherApplications(None), + "app.showAll" => app.unhideAllApplications(None), + _ => return false, + } + true +} + +/// The general pasteboard's text. +pub fn clipboard_text() -> Option { + use objc2_app_kit::NSPasteboard; + // NSPasteboardTypeString's value (the UTI), without touching the extern static. + let ty = objc2_foundation::NSString::from_str("public.utf8-plain-text"); + NSPasteboard::generalPasteboard().stringForType(&ty).map(|s| s.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn every_accelerator_parses_with_muda() { + let mut s = effectcraft_engine::Session::default(); + s.execute("file.openDemoProject", json!({})).unwrap(); + let app = EffectcraftApp::new(s); + let spec = native_menu::build(&app); + let mut n = 0; + for i in spec.items() { + if let Some(a) = &i.accelerator { + assert!(parse_accelerator(a).is_some(), "{} `{a}` does not parse", i.label); + n += 1; + } + } + assert!(n > 50, "only {n} accelerators"); + // Spot checks against the AE shortcuts. + let accel = |cmd: &str| spec.items().into_iter().find(|i| i.command == cmd).and_then(|i| i.accelerator.clone()); + assert_eq!(accel("file.saveAs").as_deref(), Some("Cmd+Shift+S")); + assert_eq!(accel("comp.new").as_deref(), Some("Cmd+N")); + assert_eq!(parse_accelerator("Cmd+Shift+S"), "CmdOrCtrl+Shift+S".parse().ok()); + } +} diff --git a/crates/engine/src/commands/frontend.rs b/crates/engine/src/commands/frontend.rs index 39107f25..f0068ae9 100644 --- a/crates/engine/src/commands/frontend.rs +++ b/crates/engine/src/commands/frontend.rs @@ -26,6 +26,9 @@ pub fn specs() -> Vec { ), fe!("app.gpuInfo", "GPU Information...", [], None, "{}", always), fe!("app.hide", "Hide EffectCraft", [], None, "{}", always), + // macOS: the native menu's Hide Others / Show All (the desktop app hides the other apps). + fe!("app.hideOthers", "Hide Others", [], None, "{}", always), + fe!("app.showAll", "Show All", [], None, "{}", always), fe!("app.quit", "Quit EffectCraft", [], Some("Cmd+Q"), "{}", always), fe!("app.commandPalette", "Quick Apply...", ["Edit"], Some("Cmd+Shift+Space"), "{query?}", always), fe!("app.keyboardShortcuts", "Keyboard Shortcuts", ["Edit"], Some("Cmd+Alt+'"), "{}", always), diff --git a/crates/engine/src/commands/layer.rs b/crates/engine/src/commands/layer.rs index ac728200..7b44096e 100644 --- a/crates/engine/src/commands/layer.rs +++ b/crates/engine/src/commands/layer.rs @@ -433,6 +433,20 @@ fn rename(s: &mut Session, p: &Value) -> Result { Ok(Value::Null) } +/// The Timeline's Comment column. +fn set_comment(s: &mut Session, p: &Value) -> Result { + let (cid, ids) = layers_p(s, p)?; + let text = str_p(p, "comment").ok_or_else(|| bad("layer.setComment", "missing `comment`"))?.to_string(); + s.edit("Layer Comment", None, |proj, _| { + let comp = proj.comp_mut(cid).ok_or(EngineError::NoComp)?; + for l in comp.layers.iter_mut().filter(|l| ids.contains(&l.id)) { + l.comment = text.clone(); + } + Ok(()) + })?; + Ok(Value::Null) +} + fn blend(s: &mut Session, p: &Value) -> Result { let (cid, ids) = layers_p(s, p)?; let mode = str_p(p, "mode").and_then(BlendMode::from_name); @@ -1073,6 +1087,7 @@ pub fn specs() -> Vec { set_switch ), cmd!("layer.rename", "Rename", [], Some("Enter"), "{layer?, name}", has_layers, rename), + cmd!("layer.setComment", "Layer Comment", [], None, "{layers?, comment}", has_layers, set_comment), cmd!("layer.setBlendMode", "Blending Mode", [], None, "{layers?, mode?: Normal|Multiply|Screen|…, step?: ±1}", has_layers, blend), cmd!( "layer.setTrackMatte", diff --git a/crates/engine/src/commands/stubs.rs b/crates/engine/src/commands/stubs.rs index 8b244bf6..11239de3 100644 --- a/crates/engine/src/commands/stubs.rs +++ b/crates/engine/src/commands/stubs.rs @@ -17,9 +17,6 @@ macro_rules! stub { pub fn specs() -> Vec { vec![ - // macOS application menu items handled by the OS. - stub!("app.hideOthers", "Hide Others", [], "{}"), - stub!("app.showAll", "Show All", [], "{}"), // Render queue / export (export milestone). stub!("file.createProxy", "Create Proxy", [], "{kind: still|movie}"), // Proxies (no proxy model yet). diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index ec06b3f0..5a2afab8 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -115,6 +115,8 @@ page!(Appearance { use_label_color_for_tabs: bool = true, cycle_mask_colors: bool = true, use_gradients: bool = true, + /// macOS: draw the menu bar inside the window instead of the system menu bar. + in_window_menu_bar_mac: bool = false, }); page!(Grids { @@ -740,6 +742,8 @@ pub fn pages() -> Vec { s("appearance.useLabelColorForTabs", "Use Label Color for Related Tabs", B, false), s("appearance.cycleMaskColors", "Cycle Mask Colors", B, false), s("appearance.useGradients", "Use Gradients", B, false), + Section("Menu Bar"), + s("appearance.inWindowMenuBarMac", "Use In-Window Menu Bar on macOS", B, true), ], }, Page { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 071c1948..e903811d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -14,6 +14,7 @@ pub mod frames; pub mod header; pub mod icons; pub mod menus; +pub mod native_menu; pub mod panels; pub mod state; pub mod theme; @@ -86,6 +87,11 @@ pub struct Hooks { pub pick_folder: Option Option>>, /// Save dialog for other file kinds: (default name, extension). pub pick_save_file: Option Option>>, + /// The system clipboard's text (native menu Edit ▸ Paste into a text field). + pub clipboard_text: Option Option>>, + /// Application actions the OS performs (`app.hide`, `app.hideOthers`, `app.showAll` on + /// macOS). Returns false when the host doesn't handle the id. + pub app_action: Option bool>>, } #[derive(Default)] diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 22b2bd40..4c85162e 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -59,6 +59,19 @@ pub const UI_COMMANDS: &[UiCommand] = &[ uic!("timeline.reveal.feather", "Reveal Mask Feather", [], Some("F")), uic!("timeline.reveal.levels", "Reveal Audio Levels (press twice: Waveform)", [], Some("L")), uic!("timeline.reveal.waveform", "Reveal Audio Waveform", [], None), + uic!("timeline.revealAdd.position", "Add Position to Revealed Properties", [], Some("Shift+P")), + uic!("timeline.revealAdd.scale", "Add Scale to Revealed Properties", [], Some("Shift+S")), + uic!("timeline.revealAdd.rotation", "Add Rotation to Revealed Properties", [], Some("Shift+R")), + uic!("timeline.revealAdd.opacity", "Add Opacity to Revealed Properties", [], Some("Shift+T")), + uic!("timeline.revealAdd.anchor", "Add Anchor Point to Revealed Properties", [], Some("Shift+A")), + uic!("timeline.revealAdd.effects", "Add Effects to Revealed Properties", [], Some("Shift+E")), + uic!("timeline.revealAdd.masks", "Add Masks to Revealed Properties", [], Some("Shift+M")), + uic!("timeline.revealAdd.feather", "Add Mask Feather to Revealed Properties", [], Some("Shift+F")), + uic!("timeline.revealAdd.levels", "Add Audio Levels to Revealed Properties", [], Some("Shift+L")), + uic!("timeline.revealAdd.animated", "Add Animated Properties to Revealed Properties", [], Some("Shift+U")), + uic!("timeline.column", "Show/Hide Timeline Column", [], None), + uic!("timeline.sourceName", "Source Name / Layer Name", [], None), + uic!("timeline.search", "Search Timeline", [], None), uic!("timeline.collapseAll", "Collapse All", [], Some("Cmd+`")), uic!("tool.selection", "Selection Tool", [], Some("V")), uic!("tool.hand", "Hand Tool", [], Some("H")), @@ -89,26 +102,42 @@ pub fn panel_command_id(p: PanelKind) -> String { format!("window.panel.{}", p.id()) } -fn reveal(app: &mut EffectcraftApp, kind: &str, now: f64) { +/// The reveal shortcuts (P, S, R, T, A, E, M, F, L…). `add` (Shift+key) adds the property to +/// (or removes it from) those already revealed instead of replacing them. +pub fn reveal(app: &mut EffectcraftApp, kind: &str, now: f64, add: bool) { // AE's double-press shortcuts: L then L again quickly reveals the Waveform (LL). let kind = match app.last_reveal.take() { Some((k, t)) if k == "levels" && kind == "levels" && now - t < 0.6 => "waveform", _ => kind, }; app.last_reveal = Some((kind.to_string(), now)); - let add = app.ui.timeline.reveal.contains(&kind.to_string()); - if add && app.ui.timeline.reveal.len() == 1 { - app.ui.timeline.reveal.clear(); - app.ui.timeline.open_layers.clear(); + let tl = &mut app.ui.timeline; + let present = tl.reveal.iter().any(|k| k == kind); + let sel: Vec = if app.session.state.selected_layers.is_empty() { + app.session.active_comp().map(|c| c.layers.iter().map(|l| l.id.0).collect()).unwrap_or_default() + } else { + app.session.state.selected_layers.iter().map(|l| l.0).collect() + }; + if add { + if present { + tl.reveal.retain(|k| k != kind); + if tl.reveal.is_empty() { + tl.open_layers.clear(); + } + } else { + tl.reveal.retain(|k| k != "props" || !tl.reveal_props.is_empty()); + tl.reveal.push(kind.to_string()); + tl.open_layers.extend(sel); + } return; } - app.ui.timeline.reveal = vec![kind.to_string()]; - let sel = if app.session.state.selected_layers.is_empty() { - app.session.active_comp().map(|c| c.layers.iter().map(|l| l.id).collect()).unwrap_or_default() - } else { - app.session.state.selected_layers.clone() - }; - app.ui.timeline.open_layers = sel.iter().map(|l| l.0).collect(); + if present && tl.reveal.len() == 1 { + tl.reveal.clear(); + tl.open_layers.clear(); + return; + } + tl.reveal = vec![kind.to_string()]; + tl.open_layers = sel.into_iter().collect(); } fn no_params(p: &Value) -> bool { @@ -166,9 +195,30 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V if k == "animated" { return run_engine(app, ctx, "anim.reveal", json!({"kind": "keyframes"})); } - reveal(app, k, now); + reveal(app, k, now, false); return Ok(Value::Null); } + if let Some(k) = id.strip_prefix("timeline.revealAdd.") { + reveal(app, k, now, true); + return Ok(json!({"revealed": app.ui.timeline.reveal})); + } + // U reveals keyframed properties; UU (pressed twice quickly) all modified properties; U + // again later hides them. + if id == "anim.reveal" && params.get("kind").and_then(Value::as_str) == Some("keyframes") { + let last = app.last_reveal.take(); + app.last_reveal = Some(("keyframes".into(), now)); + match last { + Some((k, t)) if k == "keyframes" && now - t < 0.6 => { + return run_engine(app, ctx, "anim.reveal", json!({"kind": "modified"})); + } + Some((k, _)) if k == "keyframes" && app.ui.timeline.reveal == ["props"] => { + app.ui.timeline.reveal.clear(); + app.ui.timeline.open_layers.clear(); + return Ok(json!({"revealed": 0})); + } + _ => {} + } + } // Docking: {panel, anchor, zone: center|left|right|top|bottom}, {panel, rect?}, {panel?}. if let Some(op) = id.strip_prefix("window.").filter(|o| matches!(*o, "maximizePanel" | "dockPanel" | "floatPanel" | "closePanel")) { let panel_of = |k: &str| params.get(k).and_then(Value::as_str).map(|s| PanelKind::from_name(s).ok_or(format!("unknown panel `{s}`"))); @@ -268,6 +318,23 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V return Ok(Value::Null); } "timeline.zoomFit" => app.ui.timeline.pps = None, + "timeline.column" => { + let col = params.get("column").and_then(Value::as_str).ok_or("timeline.column: need `column`")?; + let on = params.get("visible").and_then(Value::as_bool).unwrap_or(!crate::panels::timeline::column_visible(&app.ui.timeline, col)); + crate::panels::timeline::set_column(&mut app.ui.timeline, col, on)?; + let shown: Vec<&str> = + crate::panels::timeline::COLUMNS.iter().map(|c| c.0).filter(|c| crate::panels::timeline::column_visible(&app.ui.timeline, c)).collect(); + return Ok(json!({"columns": shown})); + } + "timeline.sourceName" => { + let tl = &mut app.ui.timeline; + tl.source_name = params.get("value").and_then(Value::as_bool).unwrap_or(!tl.source_name); + return Ok(json!({"sourceName": tl.source_name})); + } + "timeline.search" => { + app.ui.timeline.search = params.get("query").and_then(Value::as_str).unwrap_or_default().to_string(); + app.show_panel(PanelKind::Timeline); + } "timeline.graphEditor" => app.ui.timeline.graph_editor = !app.ui.timeline.graph_editor, "timeline.switchesModes" => app.ui.timeline.show_modes = !app.ui.timeline.show_modes, "timeline.collapseAll" => { @@ -399,8 +466,16 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu ); Value::Null } - "app.hide" => { - ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(true)); + "app.hide" | "app.hideOthers" | "app.showAll" => { + // The desktop app asks macOS (NSApplication hide / hideOtherApplications / + // unhideAllApplications); elsewhere Hide minimizes and the others don't apply. + if app.hooks.app_action.as_ref().is_some_and(|f| f(id)) { + return Ok(Value::Null); + } + match id { + "app.hide" => ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(true)), + _ => return Err(format!("`{id}` is only available on macOS")), + } Value::Null } "app.quit" => { @@ -768,7 +843,7 @@ pub fn menus() -> Vec<&'static str> { effectcraft_engine::menus::top_level() } -fn entry_label(app: &EffectcraftApp, e: &MenuEntry) -> String { +pub(crate) fn entry_label(app: &EffectcraftApp, e: &MenuEntry) -> String { match e.command.as_str() { "edit.undo" => app.session.history.undo.last().map(|u| format!("Undo {}", u.0)).unwrap_or_else(|| "Can't Undo".into()), "edit.redo" => app.session.history.redo.last().map(|u| format!("Redo {}", u.0)).unwrap_or_else(|| "Can't Redo".into()), @@ -777,12 +852,12 @@ fn entry_label(app: &EffectcraftApp, e: &MenuEntry) -> String { } /// The entry's shortcut in the active keyboard shortcut preset. -fn entry_shortcut(app: &EffectcraftApp, e: &MenuEntry) -> Option { +pub(crate) fn entry_shortcut(app: &EffectcraftApp, e: &MenuEntry) -> Option { app.session.shortcuts().shortcut_of(&e.command, &e.params).map(str::to_string) } /// Check-mark state of frontend toggles (engine state is answered by the engine). -fn entry_checked(app: &EffectcraftApp, e: &MenuEntry) -> Option { +pub(crate) fn entry_checked(app: &EffectcraftApp, e: &MenuEntry) -> Option { let v = &app.ui.viewer; let pstr = |k: &str| e.params.get(k).and_then(Value::as_str); match e.command.as_str() { @@ -814,7 +889,7 @@ fn entry_checked(app: &EffectcraftApp, e: &MenuEntry) -> Option { } } -fn entry_enabled(app: &EffectcraftApp, e: &MenuEntry) -> bool { +pub(crate) fn entry_enabled(app: &EffectcraftApp, e: &MenuEntry) -> bool { if e.command == "window.panel" { return true; } diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rs new file mode 100644 index 00000000..ef5070b3 --- /dev/null +++ b/crates/ui-egui/src/native_menu.rs @@ -0,0 +1,459 @@ +//! The native (macOS) menu bar as a plain, serializable spec. The desktop app turns it into an +//! `NSMenu` with `muda` (`apps/effectcraft/src/native_menu.rs`); everything that decides *what* +//! the menu contains lives here, so it is tested without creating real menus and builds for the +//! web (where the in-window menu bar is used). +//! +//! The spec mirrors the engine's After Effects menu tree ([`effectcraft_engine::menus`]): +//! submenus, separators, labels (Undo/Redo and label names are live), check marks, enabled +//! state, and the active shortcut preset's keys as accelerators. File ▸ Open Recent lists the +//! recent projects. The application menu's Hide / Hide Others / Show All are macOS' +//! predefined items, and a Services submenu is added as on every Mac app. +//! +//! Item ids are positional (`m.3.0.2`), stable while the menu's structure stays the same; the +//! app rebuilds the native menu when [`NativeMenu::structure`] changes and otherwise only pushes +//! label / enabled / checked updates, and only when [`state_key`] changes. + +use std::hash::{Hash, Hasher}; + +use serde::Serialize; +use serde_json::{Value, json}; + +use crate::EffectcraftApp; +use effectcraft_engine::menus::{MenuNode, menu_bar}; + +/// What macOS performs itself (no command dispatch). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum Role { + Services, + Hide, + HideOthers, + ShowAll, +} + +/// One node of the native menu. +#[derive(Clone, Debug, PartialEq, Serialize)] +#[serde(tag = "type", rename_all = "camelCase")] +pub enum NativeNode { + Item(NativeItem), + Separator, + Submenu { + label: String, + children: Vec, + }, + /// A predefined macOS item; `command` is the engine entry it stands for (empty for Services). + Predefined { + role: Role, + label: String, + command: String, + }, +} + +/// A clickable item: activating it runs `command` with `params`, exactly like the in-window bar. +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct NativeItem { + pub id: String, + pub label: String, + pub command: String, + #[serde(skip_serializing_if = "Value::is_null")] + pub params: Value, + /// The shortcut in EffectCraft notation (`Cmd+Shift+S`). + pub shortcut: Option, + /// The menu key equivalent in `muda` accelerator notation, when the shortcut can be one (see + /// [`accelerator`]). + pub accelerator: Option, + pub enabled: bool, + pub checked: Option, +} + +/// The whole menu bar: top-level menus in order (the application menu first). +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +pub struct NativeMenu { + pub menus: Vec, +} + +impl NativeMenu { + /// Every clickable item, depth first. + pub fn items(&self) -> Vec<&NativeItem> { + fn rec<'a>(n: &'a [NativeNode], out: &mut Vec<&'a NativeItem>) { + for x in n { + match x { + NativeNode::Item(i) => out.push(i), + NativeNode::Submenu { children, .. } => rec(children, out), + _ => {} + } + } + } + let mut out = vec![]; + rec(&self.menus, &mut out); + out + } + + pub fn find(&self, id: &str) -> Option<&NativeItem> { + self.items().into_iter().find(|i| i.id == id) + } + + /// Fingerprint of everything that needs a rebuild when it changes (tree shape, commands, + /// accelerators, submenu labels); labels, enabled and check state are updated in place. + pub fn structure(&self) -> u64 { + fn rec(n: &[NativeNode], h: &mut std::collections::hash_map::DefaultHasher) { + for x in n { + match x { + NativeNode::Item(i) => { + 0u8.hash(h); + i.id.hash(h); + i.command.hash(h); + i.params.to_string().hash(h); + i.accelerator.hash(h); + } + NativeNode::Separator => 1u8.hash(h), + NativeNode::Submenu { label, children } => { + 2u8.hash(h); + label.hash(h); + rec(children, h); + 3u8.hash(h); + } + NativeNode::Predefined { role, .. } => { + 4u8.hash(h); + role.hash(h); + } + } + } + } + let mut h = std::collections::hash_map::DefaultHasher::new(); + rec(&self.menus, &mut h); + h.finish() + } +} + +/// Build the native menu spec for the app's current state. +pub fn build(app: &EffectcraftApp) -> NativeMenu { + fn rec(app: &EffectcraftApp, nodes: &[MenuNode], path: &str, out: &mut Vec) { + let mut services_added = false; + for (i, n) in nodes.iter().enumerate() { + let id = format!("{path}.{i}"); + match n { + MenuNode::Separator => out.push(NativeNode::Separator), + MenuNode::Submenu { label, children } => { + let mut kids = vec![]; + if label == "Open Recent" { + for (k, p) in app.session.prefs.recent_projects.iter().enumerate() { + let name = std::path::Path::new(p).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or(p.clone()); + kids.push(NativeNode::Item(NativeItem { + id: format!("{id}.r{k}"), + label: name, + command: "file.openRecent".into(), + params: json!({"index": k}), + shortcut: None, + accelerator: None, + enabled: true, + checked: None, + })); + } + if !kids.is_empty() { + kids.push(NativeNode::Separator); + } + } + rec(app, children, &id, &mut kids); + out.push(NativeNode::Submenu { label: label.clone(), children: kids }); + } + MenuNode::Item(e) => { + let role = match e.command.as_str() { + "app.hide" => Some(Role::Hide), + "app.hideOthers" => Some(Role::HideOthers), + "app.showAll" => Some(Role::ShowAll), + _ => None, + }; + if let Some(role) = role { + // Services sits just above Hide, as in every Mac app. + if role == Role::Hide && !services_added { + out.push(NativeNode::Predefined { role: Role::Services, label: "Services".into(), command: String::new() }); + out.push(NativeNode::Separator); + services_added = true; + } + out.push(NativeNode::Predefined { role, label: e.label.clone(), command: e.command.clone() }); + continue; + } + let shortcut = crate::menus::entry_shortcut(app, e); + out.push(NativeNode::Item(NativeItem { + id, + label: crate::menus::entry_label(app, e), + command: e.command.clone(), + params: e.params.clone(), + accelerator: shortcut.as_deref().and_then(accelerator), + shortcut, + enabled: crate::menus::entry_enabled(app, e), + checked: crate::menus::entry_checked(app, e), + })); + } + } + } + } + let mut menus = vec![]; + rec(app, menu_bar(), "m", &mut menus); + NativeMenu { menus } +} + +/// The `muda` accelerator for an EffectCraft shortcut, or `None` when it must stay a plain +/// keyboard shortcut. Only shortcuts with Cmd or Ctrl (and function keys) become menu key +/// equivalents: a single-key shortcut (`V`, `Space`, `U`) or an Alt/Shift-only one would be +/// swallowed by the menu while typing into a text field. Those still work through the +/// window's shortcut dispatcher, as on the other platforms. +pub fn accelerator(shortcut: &str) -> Option { + let parts: Vec<&str> = match shortcut.strip_suffix("++") { + Some(head) => head.split('+').chain(std::iter::once("+")).collect(), + None => shortcut.split('+').collect(), + }; + let (key, mods) = parts.split_last()?; + let mut out: Vec = vec![]; + let mut primary = false; + for m in mods { + out.push( + match *m { + "Cmd" => { + primary = true; + "Cmd" + } + "Ctrl" => { + primary = true; + "Ctrl" + } + "Alt" => "Alt", + "Shift" => "Shift", + _ => return None, + } + .to_string(), + ); + } + let function_key = key.len() >= 2 && key.starts_with('F') && key[1..].parse::().is_ok_and(|n| (1..=24).contains(&n)); + if !primary && !function_key { + return None; + } + let k = match *key { + "Num*" => "NumpadMultiply".to_string(), + "+" => "Equal".to_string(), + k if k.starts_with("Num") && k.len() == 4 => format!("Numpad{}", &k[3..]), + "Escape" | "Space" | "Enter" | "Tab" | "Home" | "End" | "PageUp" | "PageDown" | "Delete" | "Backspace" => key.to_string(), + "ArrowLeft" | "ArrowRight" | "ArrowUp" | "ArrowDown" => key.to_string(), + k if k.chars().count() == 1 => { + let c = k.chars().next()?; + if c.is_ascii_alphanumeric() || "`\\[],-=./;'".contains(c) { + c.to_ascii_uppercase().to_string() + } else { + return None; + } + } + k if function_key => k.to_string(), + _ => return None, + }; + out.push(k); + Some(out.join("+")) +} + +/// Cheap per-frame fingerprint of everything menu labels, check marks and enabled state depend +/// on (project revision, selection, history, settings, executed commands, viewer toggles). +pub fn state_key(app: &EffectcraftApp) -> u64 { + let mut h = std::collections::hash_map::DefaultHasher::new(); + let s = &app.session; + s.revision.hash(&mut h); + s.prefs_revision.hash(&mut h); + s.path.hash(&mut h); + s.active_comp_id().map(|c| c.0).hash(&mut h); + for l in &s.state.selected_layers { + l.0.hash(&mut h); + } + s.state.selected_keys.len().hash(&mut h); + s.state.selected_props.len().hash(&mut h); + s.state.project_selection.len().hash(&mut h); + s.state.selected_vertices.len().hash(&mut h); + s.state.clipboard.len().hash(&mut h); + s.state.key_clipboard.len().hash(&mut h); + (s.history.undo.len(), s.history.redo.len()).hash(&mut h); + s.journal.len().hash(&mut h); + s.journal.last().map(|j| &j.0).hash(&mut h); + s.prefs.recent_projects.hash(&mut h); + s.keymaps.active.hash(&mut h); + let v = &app.ui.viewer; + (v.rulers, v.guides, v.snap_guides, v.lock_guides, v.grid, v.snap_grid, v.show_layer_controls, v.pasteboard).hash(&mut h); + v.res.label().hash(&mut h); + (app.ui.cache_when_idle, app.ui.preview_audio, &app.ui.workspace, app.dialog.is_some()).hash(&mut h); + h.finish() +} + +/// Commands that act on a focused text field instead (Edit menu while typing): the egui event to +/// send in their place. +fn text_field_event(command: &str, clipboard: Option) -> Option> { + let key = |k: egui::Key, shift: bool| egui::Event::Key { + key: k, + physical_key: None, + pressed: true, + repeat: false, + modifiers: egui::Modifiers { shift, ..egui::Modifiers::COMMAND }, + }; + Some(match command { + "edit.copy" => vec![egui::Event::Copy], + "edit.cut" => vec![egui::Event::Cut], + "edit.paste" => vec![egui::Event::Paste(clipboard?)], + "edit.selectAll" => vec![key(egui::Key::A, false)], + "edit.undo" => vec![key(egui::Key::Z, false)], + "edit.redo" => vec![key(egui::Key::Z, true)], + _ => return None, + }) +} + +/// Run a native menu item (by id) the way the in-window menu bar does. While a text field has +/// the keyboard, Edit ▸ Cut/Copy/Paste/Select All/Undo/Redo edit the text instead. +pub fn activate(app: &mut EffectcraftApp, ctx: &egui::Context, menu: &NativeMenu, id: &str) -> Result { + let item = menu.find(id).ok_or_else(|| format!("no native menu item `{id}`"))?; + let (command, params) = (item.command.clone(), item.params.clone()); + if ctx.egui_wants_keyboard_input() { + let clip = if command == "edit.paste" { app.hooks.clipboard_text.as_ref().and_then(|f| f()) } else { None }; + if let Some(events) = text_field_event(&command, clip) { + app.synthetic.extend(events); + ctx.request_repaint(); + return Ok(Value::Null); + } + } + let params = if params.is_null() { json!({}) } else { params }; + let r = crate::menus::invoke(app, ctx, &command, params); + if let Err(e) = &r { + app.ui.status = e.clone(); + } + ctx.request_repaint(); + r +} + +#[cfg(test)] +mod tests { + use super::*; + + fn app() -> EffectcraftApp { + let mut s = effectcraft_engine::Session::default(); + s.execute("file.openDemoProject", json!({})).unwrap(); + EffectcraftApp::new(s) + } + + #[test] + fn every_engine_entry_is_in_the_native_menu() { + let app = app(); + let spec = build(&app); + fn commands(n: &[NativeNode], out: &mut Vec<(String, String)>) { + for x in n { + match x { + NativeNode::Item(i) if i.command != "file.openRecent" => out.push((i.command.clone(), i.params.to_string())), + NativeNode::Predefined { command, .. } if !command.is_empty() => out.push((command.clone(), "null".into())), + NativeNode::Submenu { children, .. } => commands(children, out), + _ => {} + } + } + } + let mut got = vec![]; + commands(&spec.menus, &mut got); + let want: Vec<(String, String)> = effectcraft_engine::menus::entries().into_iter().map(|(_, e)| (e.command.clone(), e.params.to_string())).collect(); + assert_eq!(got, want, "native menu must mirror the engine tree entry for entry"); + // Top-level menus in order. + let tops: Vec<&str> = spec + .menus + .iter() + .filter_map(|n| match n { + NativeNode::Submenu { label, .. } => Some(label.as_str()), + _ => None, + }) + .collect(); + assert_eq!(tops, effectcraft_engine::menus::top_level()); + // Ids are unique. + let ids: std::collections::BTreeSet<&str> = spec.items().iter().map(|i| i.id.as_str()).collect(); + assert_eq!(ids.len(), spec.items().len()); + // The spec serializes (agents can read it). + let v = serde_json::to_value(&spec).unwrap(); + assert!(v["menus"].as_array().is_some_and(|a| a.len() >= 9)); + } + + #[test] + fn hide_items_are_predefined_on_macos() { + let spec = build(&app()); + let mut roles = vec![]; + fn rec(n: &[NativeNode], out: &mut Vec) { + for x in n { + match x { + NativeNode::Predefined { role, .. } => out.push(*role), + NativeNode::Submenu { children, .. } => rec(children, out), + _ => {} + } + } + } + rec(&spec.menus, &mut roles); + if cfg!(target_os = "macos") { + assert_eq!(roles, vec![Role::Services, Role::Hide, Role::HideOthers, Role::ShowAll]); + // Quit and About dispatch our commands (clean shutdown, our About dialog). + assert!(spec.items().iter().any(|i| i.command == "app.quit" && i.accelerator.as_deref() == Some("Cmd+Q"))); + assert!(spec.items().iter().any(|i| i.command == "app.about")); + } else { + assert!(roles.is_empty()); + } + } + + #[test] + fn accelerators_convert() { + assert_eq!(accelerator("Cmd+Shift+S").as_deref(), Some("Cmd+Shift+S")); + assert_eq!(accelerator("Cmd+Alt+;").as_deref(), Some("Cmd+Alt+;")); + assert_eq!(accelerator("Cmd+`").as_deref(), Some("Cmd+`")); + assert_eq!(accelerator("Ctrl+Cmd+F").as_deref(), Some("Ctrl+Cmd+F")); + assert_eq!(accelerator("F3").as_deref(), Some("F3")); + assert_eq!(accelerator("Cmd+Alt+0").as_deref(), Some("Cmd+Alt+0")); + // Keys that would swallow typing stay window shortcuts. + assert_eq!(accelerator("V"), None); + assert_eq!(accelerator("Space"), None); + assert_eq!(accelerator("Shift+="), None); + assert_eq!(accelerator("Alt+["), None); + assert_eq!(accelerator("Num*"), None); + // Every menu shortcut either converts or is a single-key/Alt/Shift shortcut. + let app = app(); + for i in build(&app).items() { + if let Some(sc) = &i.shortcut { + let primary = sc.contains("Cmd") || sc.contains("Ctrl"); + if primary { + assert!(i.accelerator.is_some(), "{} ({sc}) has no accelerator", i.label); + } + } + } + } + + #[test] + fn state_changes_flow_into_the_spec() { + let mut app = app(); + let k0 = state_key(&app); + let spec0 = build(&app); + let undo = |s: &NativeMenu| s.items().into_iter().find(|i| i.command == "edit.undo").map(|i| (i.label.clone(), i.enabled)).unwrap(); + // Select a layer and toggle Lock: the key changes, the Lock entry gets a check mark. + let first = app.session.active_comp().unwrap().layers[0].id.0; + app.session.execute("layer.select", json!({"layers": [first]})).unwrap(); + app.session.execute("layer.setSwitch", json!({"switch": "lock"})).unwrap(); + assert_ne!(state_key(&app), k0); + let spec1 = build(&app); + assert_eq!(spec0.structure(), spec1.structure(), "state changes don't rebuild the menu"); + let lock = spec1.items().into_iter().find(|i| i.command == "layer.setSwitch" && i.params["switch"] == "lock").unwrap().checked; + assert_eq!(lock, Some(true)); + assert_ne!(undo(&spec0), undo(&spec1)); + // A recent project changes the structure (Open Recent grows). + app.session.prefs.push_recent("/tmp/Recent Test.ecproj"); + let spec2 = build(&app); + assert_ne!(spec1.structure(), spec2.structure()); + assert!(spec2.items().iter().any(|i| i.command == "file.openRecent" && i.label == "Recent Test.ecproj")); + } + + #[test] + fn activation_dispatches_the_menu_command() { + let mut app = app(); + let ctx = egui::Context::default(); + let spec = build(&app); + let id = spec.items().into_iter().find(|i| i.command == "layer.newNull").unwrap().id.clone(); + let n = app.session.active_comp().unwrap().layers.len(); + activate(&mut app, &ctx, &spec, &id).unwrap(); + assert_eq!(app.session.active_comp().unwrap().layers.len(), n + 1); + assert!(activate(&mut app, &ctx, &spec, "m.999").is_err()); + assert!(text_field_event("edit.copy", None).is_some()); + assert!(text_field_event("edit.paste", None).is_none()); + assert!(text_field_event("layer.newNull", None).is_none()); + } +} diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index c9a42698..b9289e77 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -93,30 +93,207 @@ const LABEL_W: f32 = 20.0; const NUM_W: f32 = 26.0; const SW: f32 = 19.0; +/// The Timeline's columns in After Effects' order: (id, header label). Right-click a column +/// header to show or hide them; the name column is always shown. +pub const COLUMNS: [(&str, &str); 13] = [ + ("av", "A/V Features"), + ("keys", "Keys"), + ("label", "Label"), + ("num", "#"), + ("name", "Layer Name"), + ("comment", "Comment"), + ("switches", "Switches"), + ("modes", "Modes"), + ("parent", "Parent & Link"), + ("in", "In"), + ("out", "Out"), + ("duration", "Duration"), + ("stretch", "Stretch"), +]; + +/// Is a column shown? +pub fn column_visible(tl: &crate::state::TimelineState, id: &str) -> bool { + match id { + "name" => true, + "modes" => tl.show_modes, + _ => tl.columns.contains(id), + } +} + +/// Show or hide a column (`modes` is the Switches/Modes toggle, F4). +pub fn set_column(tl: &mut crate::state::TimelineState, id: &str, on: bool) -> Result<(), String> { + match id { + "name" if on => Ok(()), + "name" => Err("the name column can't be hidden".into()), + "modes" => { + tl.show_modes = on; + Ok(()) + } + _ if COLUMNS.iter().any(|c| c.0 == id) => { + if on { + tl.columns.insert(id.to_string()); + } else { + tl.columns.remove(id); + } + Ok(()) + } + _ => Err(format!("unknown column `{id}` (one of: {})", COLUMNS.map(|c| c.0).join(", "))), + } +} + +const KEYS_W: f32 = 56.0; +const COMMENT_W: f32 = 120.0; +const TIME_W: f32 = 72.0; +const STRETCH_W: f32 = 60.0; +const PARENT_W: f32 = 116.0; +const MODES_W: f32 = 92.0 + 112.0; + +#[derive(Clone, Copy, Default)] +struct Vis { + av: bool, + keys: bool, + label: bool, + num: bool, + comment: bool, + switches: bool, + modes: bool, + parent: bool, + in_: bool, + out: bool, + duration: bool, + stretch: bool, +} + +impl Vis { + fn of(tl: &crate::state::TimelineState) -> Vis { + let v = |id| column_visible(tl, id); + Vis { + av: v("av"), + keys: v("keys"), + label: v("label"), + num: v("num"), + comment: v("comment"), + switches: v("switches"), + modes: v("modes"), + parent: v("parent"), + in_: v("in"), + out: v("out"), + duration: v("duration"), + stretch: v("stretch"), + } + } + /// Width of every visible column but the name. + fn fixed(&self) -> f32 { + let w = |on: bool, w: f32| if on { w } else { 0.0 }; + w(self.av, AV_W) + + w(self.keys, KEYS_W) + + w(self.label, LABEL_W) + + w(self.num, NUM_W) + + w(self.comment, COMMENT_W) + + w(self.switches, SW * 8.0 + 6.0) + + w(self.modes, MODES_W) + + w(self.parent, PARENT_W) + + w(self.in_, TIME_W) + + w(self.out, TIME_W) + + w(self.duration, TIME_W) + + w(self.stretch, STRETCH_W) + } +} + struct Cols { + vis: Vis, av: f32, + keys: f32, label: f32, num: f32, name: f32, + /// End of the name column. + name_end: f32, + comment: f32, switches: f32, mode: f32, trkmat: f32, parent: f32, + in_: f32, + out: f32, + duration: f32, + stretch: f32, end: f32, } -fn cols(x0: f32, width: f32, show_modes: bool) -> Cols { - let fixed = AV_W + LABEL_W + NUM_W + SW * 8.0 + if show_modes { 92.0 + 112.0 } else { 0.0 } + 116.0; - let name_w = (width - fixed).max(120.0); - let av = x0; - let label = av + AV_W; - let num = label + LABEL_W; - let name = num + NUM_W; - let switches = name + name_w; - let mode = switches + SW * 8.0 + 6.0; - let trkmat = mode + if show_modes { 92.0 } else { 0.0 }; - let parent = trkmat + if show_modes { 112.0 } else { 0.0 }; - Cols { av, label, num, name, switches, mode, trkmat, parent, end: parent + 116.0 } +fn cols(x0: f32, width: f32, vis: Vis) -> Cols { + let name_w = (width - vis.fixed()).max(120.0); + let mut x = x0; + let mut next = |on: bool, w: f32| { + let at = x; + if on { + x += w; + } + at + }; + let av = next(vis.av, AV_W); + let keys = next(vis.keys, KEYS_W); + let label = next(vis.label, LABEL_W); + let num = next(vis.num, NUM_W); + let name = next(true, name_w); + let comment = next(vis.comment, COMMENT_W); + let switches = next(vis.switches, SW * 8.0 + 6.0); + let mode = next(vis.modes, 92.0); + let trkmat = next(vis.modes, 112.0); + let parent = next(vis.parent, PARENT_W); + let in_ = next(vis.in_, TIME_W); + let out = next(vis.out, TIME_W); + let duration = next(vis.duration, TIME_W); + let stretch = next(vis.stretch, STRETCH_W); + let end = next(true, 0.0); + Cols { vis, av, keys, label, num, name, name_end: name + name_w, comment, switches, mode, trkmat, parent, in_, out, duration, stretch, end } +} + +/// Which column a header x position falls in. +fn column_at(cw: &Cols, x: f32) -> Option<&'static str> { + let v = cw.vis; + let spans = [ + ("av", v.av, cw.av, cw.av + AV_W), + ("keys", v.keys, cw.keys, cw.keys + KEYS_W), + ("label", v.label, cw.label, cw.label + LABEL_W), + ("num", v.num, cw.num, cw.num + NUM_W), + ("name", true, cw.name, cw.name_end), + ("comment", v.comment, cw.comment, cw.comment + COMMENT_W), + ("switches", v.switches, cw.switches, cw.switches + SW * 8.0 + 6.0), + ("modes", v.modes, cw.mode, cw.mode + MODES_W), + ("parent", v.parent, cw.parent, cw.parent + PARENT_W), + ("in", v.in_, cw.in_, cw.in_ + TIME_W), + ("out", v.out, cw.out, cw.out + TIME_W), + ("duration", v.duration, cw.duration, cw.duration + TIME_W), + ("stretch", v.stretch, cw.stretch, cw.stretch + STRETCH_W), + ]; + spans.into_iter().find(|(_, on, a, b)| *on && x >= *a && x < *b).map(|s| s.0) +} + +/// Snap a time to the nearest candidate within `tolerance` (all seconds); Shift-dragging the +/// current-time indicator snaps to keyframes, layer in/out points, markers and the work area. +pub fn snap_time(candidates: &[f64], t: f64, tolerance: f64) -> f64 { + candidates.iter().copied().filter(|c| (c - t).abs() <= tolerance).min_by(|a, b| (a - t).abs().total_cmp(&(b - t).abs())).unwrap_or(t) +} + +/// Times the current-time indicator snaps to (comp seconds). +fn snap_candidates(comp: &Comp, rows: &[Row]) -> Vec { + let mut v = vec![0.0, comp.work_area.0.seconds(), comp.work_area.1.seconds(), comp.duration.seconds()]; + v.extend(comp.markers.iter().map(|m| m.time.seconds())); + for l in &comp.layers { + v.push(l.in_point.seconds()); + v.push(l.out_point.seconds()); + v.extend(l.markers.iter().map(|m| l.comp_time(m.time).seconds())); + } + for r in rows { + if let RowKind::Prop { uid } = r.kind + && let Some(l) = comp.layer(r.layer) + && let Some(p) = l.props.find(uid) + { + v.extend(p.keys.iter().map(|k| l.comp_time(k.time).seconds())); + } + } + v } /// Timeline horizontal mapping. @@ -222,127 +399,106 @@ fn reveal_matches(p: &Property, path_matches: &[&str], kind: &str) -> bool { } } +/// The name shown for a layer: its name, or its source's name (Source Name column). +fn display_name(app: &EffectcraftApp, l: &Layer) -> String { + if app.ui.timeline.source_name + && let Some(item) = l.source.item().and_then(|i| app.session.project.item(i)) + { + return item.name.clone(); + } + l.name.clone() +} + +/// The timeline search (After Effects' search field): rows for the properties and groups of a +/// layer whose names contain `q` (lowercase), each with its enclosing groups. A matching group +/// brings its whole contents. +fn search_rows(l: &Layer, q: &str) -> Vec { + fn group_row(l: &Layer, g: &PropGroup, depth: usize, parent: &PropGroup) -> Row { + let fx = matches!(g.kind, GroupKind::Effect { .. }).then_some(g.enabled); + let eye = + (parent.match_id == effectcraft_engine::project::styles::GROUP && g.match_id != effectcraft_engine::project::styles::BLENDING).then_some(g.enabled); + Row { layer: l.id, depth, kind: RowKind::Group { uid: g.uid, name: g.name.clone(), open: true, has_children: !g.children.is_empty(), fx, eye } } + } + fn all(l: &Layer, g: &PropGroup, depth: usize, out: &mut Vec) { + for c in &g.children { + match c { + Node::Group(sg) => { + out.push(group_row(l, sg, depth, g)); + all(l, sg, depth + 1, out); + } + Node::Prop(p) if prop_visible(p, l) => out.push(Row { layer: l.id, depth, kind: RowKind::Prop { uid: p.uid } }), + _ => {} + } + } + } + fn rec(l: &Layer, g: &PropGroup, depth: usize, q: &str, out: &mut Vec) { + for c in &g.children { + match c { + Node::Group(sg) if depth > 1 || group_visible(sg, l) => { + if sg.name.to_lowercase().contains(q) { + out.push(group_row(l, sg, depth, g)); + all(l, sg, depth + 1, out); + continue; + } + let mut inner = vec![]; + rec(l, sg, depth + 1, q, &mut inner); + if !inner.is_empty() { + out.push(group_row(l, sg, depth, g)); + out.extend(inner); + } + } + Node::Prop(p) if prop_visible(p, l) && p.name.to_lowercase().contains(q) => { + out.push(Row { layer: l.id, depth, kind: RowKind::Prop { uid: p.uid } }); + } + _ => {} + } + } + } + let mut out = vec![]; + rec(l, &l.props, 1, q, &mut out); + out +} + +/// The property each reveal key shows (P, S, R, T, A, F, L; `animated` = keyframed or with an +/// expression): (group searched, property match ids). +fn reveal_targets(kind: &str) -> Option<(&'static str, &'static [&'static str])> { + Some(match kind { + "position" => ("transform", &["position", "positionX", "positionY", "positionZ"]), + "scale" => ("transform", &["scale"]), + "rotation" => ("transform", &["rotation", "rotationX", "rotationY", "orientation"]), + "opacity" => ("transform", &["opacity"]), + "anchor" => ("transform", &["anchor"]), + "feather" => ("masks", &["feather"]), + "levels" => ("audio", &["levels"]), + "animated" => ("", &[]), + _ => return None, + }) +} + fn build_rows(app: &EffectcraftApp, comp: &Comp) -> Vec { let tl = &app.ui.timeline; - let search = tl.search.to_lowercase(); + let search = tl.search.trim().to_lowercase(); let mut rows = Vec::new(); for l in &comp.layers { if comp.hide_shy && l.switches.shy { continue; } - if !search.is_empty() && !l.name.to_lowercase().contains(&search) { + if !search.is_empty() { + // Layers whose name or properties match; matching properties are revealed. + let matches = search_rows(l, &search); + if matches.is_empty() && !display_name(app, l).to_lowercase().contains(&search) { + continue; + } + rows.push(Row { layer: l.id, depth: 0, kind: RowKind::Layer }); + rows.extend(matches); continue; } rows.push(Row { layer: l.id, depth: 0, kind: RowKind::Layer }); if !tl.open_layers.contains(&l.id.0) { continue; } - if let Some(kind) = tl.reveal.first() { - let (group, props): (&str, Vec<&str>) = match kind.as_str() { - "position" => ("transform", vec!["position", "positionX", "positionY", "positionZ"]), - "scale" => ("transform", vec!["scale"]), - "rotation" => ("transform", vec!["rotation", "rotationX", "rotationY", "orientation"]), - "opacity" => ("transform", vec!["opacity"]), - "anchor" => ("transform", vec!["anchor"]), - "feather" => ("masks", vec!["feather"]), - "levels" => ("audio", vec!["levels"]), - _ => ("", vec![]), - }; - match kind.as_str() { - "waveform" => { - if let Some(item) = super::waveform::audio_item(&app.session.project, l) { - rows.push(Row { layer: l.id, depth: 1, kind: RowKind::Waveform { item: item.0 } }); - } - } - "effects" => { - if let Some(fx) = l.effects() { - for g in fx.groups() { - rows.push(Row { - layer: l.id, - depth: 1, - kind: RowKind::Group { - uid: g.uid, - name: g.name.clone(), - open: tl.open_groups.contains(&g.uid), - has_children: true, - fx: Some(g.enabled), - eye: None, - }, - }); - if tl.open_groups.contains(&g.uid) { - push_group(&mut rows, l, g, 2, &tl.open_groups); - } - } - } - } - "masks" => { - if let Some(m) = l.masks() { - for g in m.groups() { - rows.push(Row { - layer: l.id, - depth: 1, - kind: RowKind::Group { - uid: g.uid, - name: g.name.clone(), - open: tl.open_groups.contains(&g.uid), - has_children: true, - fx: None, - eye: None, - }, - }); - if tl.open_groups.contains(&g.uid) { - push_group(&mut rows, l, g, 2, &tl.open_groups); - } - } - } - } - "props" => { - // Animation ▸ Reveal Properties…: the engine picked the uids. - fn groups_in<'a>(g: &'a PropGroup, set: &std::collections::BTreeSet, out: &mut Vec<&'a PropGroup>) { - for sg in g.groups() { - if set.contains(&sg.uid) { - out.push(sg); - } else { - groups_in(sg, set, out); - } - } - } - let mut groups = vec![]; - groups_in(&l.props, &tl.reveal_props, &mut groups); - for g in groups { - let open = tl.open_groups.contains(&g.uid); - rows.push(Row { - layer: l.id, - depth: 1, - kind: RowKind::Group { uid: g.uid, name: g.name.clone(), open, has_children: !g.children.is_empty(), fx: None, eye: None }, - }); - if open { - push_group(&mut rows, l, g, 2, &tl.open_groups); - } - } - let mut found = vec![]; - collect_props(&l.props, &mut found, &|p| prop_visible(p, l) && tl.reveal_props.contains(&p.uid)); - for uid in found { - rows.push(Row { layer: l.id, depth: 1, kind: RowKind::Prop { uid } }); - } - } - _ => { - let mut found = vec![]; - let root = if group.is_empty() { - Some(&l.props) - } else if group == "masks" { - l.masks() - } else { - l.props.sub(group) - }; - if let Some(g) = root { - collect_props(g, &mut found, &|p| prop_visible(p, l) && reveal_matches(p, &props, kind)); - } - for uid in found { - rows.push(Row { layer: l.id, depth: 1, kind: RowKind::Prop { uid } }); - } - } - } + if !tl.reveal.is_empty() { + reveal_rows(app, l, &mut rows); continue; } for c in &l.props.children { @@ -388,6 +544,95 @@ fn build_rows(app: &EffectcraftApp, comp: &Comp) -> Vec { rows } +/// Rows of a twirled-open layer under the reveal shortcuts (one, or several added with Shift): +/// masks, effects, the revealed properties in property-tree order, the Reveal Properties +/// selection, then the waveform. +fn reveal_rows(app: &EffectcraftApp, l: &Layer, rows: &mut Vec) { + let tl = &app.ui.timeline; + let has = |k: &str| tl.reveal.iter().any(|r| r == k); + let group_rows = |rows: &mut Vec, g: &PropGroup, fx: bool| { + for g in g.groups() { + rows.push(Row { + layer: l.id, + depth: 1, + kind: RowKind::Group { + uid: g.uid, + name: g.name.clone(), + open: tl.open_groups.contains(&g.uid), + has_children: true, + fx: fx.then_some(g.enabled), + eye: None, + }, + }); + if tl.open_groups.contains(&g.uid) { + push_group(rows, l, g, 2, &tl.open_groups); + } + } + }; + if has("masks") + && let Some(m) = l.masks() + { + group_rows(rows, m, false); + } + if has("effects") + && let Some(fx) = l.effects() + { + group_rows(rows, fx, true); + } + // P/S/R/T/A/F/L/animated: matching properties, in tree order, each once. + let mut wanted = std::collections::BTreeSet::new(); + for kind in &tl.reveal { + let Some((group, ids)) = reveal_targets(kind) else { continue }; + let root = match group { + "" => Some(&l.props), + "masks" => l.masks(), + g => l.props.sub(g), + }; + if let Some(g) = root { + let mut found = vec![]; + collect_props(g, &mut found, &|p| prop_visible(p, l) && reveal_matches(p, ids, kind)); + wanted.extend(found); + } + } + if !wanted.is_empty() { + let mut found = vec![]; + collect_props(&l.props, &mut found, &|p| wanted.contains(&p.uid)); + rows.extend(found.into_iter().map(|uid| Row { layer: l.id, depth: 1, kind: RowKind::Prop { uid } })); + } + if has("props") { + // Animation ▸ Reveal Properties…: the engine picked the uids. + fn groups_in<'a>(g: &'a PropGroup, set: &std::collections::BTreeSet, out: &mut Vec<&'a PropGroup>) { + for sg in g.groups() { + if set.contains(&sg.uid) { + out.push(sg); + } else { + groups_in(sg, set, out); + } + } + } + let mut groups = vec![]; + groups_in(&l.props, &tl.reveal_props, &mut groups); + for g in groups { + let open = tl.open_groups.contains(&g.uid); + rows.push(Row { + layer: l.id, + depth: 1, + kind: RowKind::Group { uid: g.uid, name: g.name.clone(), open, has_children: !g.children.is_empty(), fx: None, eye: None }, + }); + if open { + push_group(rows, l, g, 2, &tl.open_groups); + } + } + let mut found = vec![]; + collect_props(&l.props, &mut found, &|p| prop_visible(p, l) && tl.reveal_props.contains(&p.uid) && !wanted.contains(&p.uid)); + rows.extend(found.into_iter().map(|uid| Row { layer: l.id, depth: 1, kind: RowKind::Prop { uid } })); + } + if has("waveform") + && let Some(item) = super::waveform::audio_item(&app.session.project, l) + { + rows.push(Row { layer: l.id, depth: 1, kind: RowKind::Waveform { item: item.0 } }); + } +} fn collect_props(g: &PropGroup, out: &mut Vec, f: &dyn Fn(&Property) -> bool) { for c in &g.children { match c { @@ -467,9 +712,9 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let ruler_h = 34.0; let colhdr_h = 22.0; let footer_h = 24.0; - let fixed = AV_W + LABEL_W + NUM_W + SW * 8.0 + if app.ui.timeline.show_modes { 92.0 + 112.0 } else { 0.0 } + 116.0 + 6.0; - let left_w = (fixed + 190.0).clamp(420.0, (rect.width() * 0.62).max(420.0)); - let cw = cols(rect.min.x, left_w, app.ui.timeline.show_modes); + let vis = Vis::of(&app.ui.timeline); + let left_w = (vis.fixed() + 190.0).clamp(420.0, (rect.width() * 0.72).max(420.0)); + let cw = cols(rect.min.x, left_w, vis); let graph_x0 = rect.min.x + left_w + 1.0; let graph_x1 = rect.max.x - 10.0; ctx.data_mut(|d| d.insert_temp(egui::Id::new("timeline-graph-area"), (graph_x0, graph_x1 - graph_x0))); @@ -487,6 +732,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { p.rect_filled(Rect::from_min_max(pos2(graph_x0, top), rect.max), 0.0, t.tl_bg); p.line_segment([pos2(graph_x0 - 1.0, top), pos2(graph_x0 - 1.0, rect.max.y)], Stroke::new(1.0, t.app_bg)); + let mut actions: Vec<(String, serde_json::Value)> = Vec::new(); // ---- header (left): time display, search, switches. let tc = crate::panels::timecode(&app.session, &comp, time); let tc_rect = Rect::from_min_size(pos2(rect.min.x + 12.0, top + 6.0), vec2(150.0, 24.0)); @@ -636,36 +882,118 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { if (rresp.dragged() || rresp.clicked()) && let Some(pt) = rresp.interact_pointer_pos() { - let tt = Tick::from_seconds_f64(tm.t(pt.x).max(0.0)); + let mut secs = tm.t(pt.x).max(0.0); + // Shift-drag: snap to keyframes, in/out points, markers and the work area (8 px). + if ui.input(|i| i.modifiers.shift) { + let cands = snap_candidates(&comp, &build_rows(app, &comp)); + secs = snap_time(&cands, secs, 8.0 / pps.max(1e-6)); + } + let tt = Tick::from_seconds_f64(secs); app.session.set_time(tt); app.stop(); } - // ---- column headers. + // ---- column headers (right-click: show/hide columns; click the name header: Source/Layer Name). let ch = Rect::from_min_max(pos2(rect.min.x, top + header_h), pos2(rect.max.x, top + header_h + colhdr_h)); p.rect_filled(Rect::from_min_max(ch.min, pos2(graph_x0 - 1.0, ch.max.y)), 0.0, t.panel_bg); p.line_segment([pos2(rect.min.x, ch.max.y), pos2(rect.max.x, ch.max.y)], Stroke::new(1.0, t.separator)); let hy = ch.center().y; let hic = |icon: Icon, x: f32| icons::paint(&p, Rect::from_center_size(pos2(x, hy), vec2(12.0, 12.0)), icon, t.text_dim); - hic(Icon::Eye, cw.av + 10.0); - hic(Icon::Speaker, cw.av + 28.0); - hic(Icon::Solo, cw.av + 46.0); - hic(Icon::Lock, cw.av + 64.0); - icons::paint(&p, Rect::from_center_size(pos2(cw.label + 10.0, hy), vec2(10.0, 10.0)), Icon::Keyframe, t.text_dim); - p.text(pos2(cw.num + 6.0, hy), Align2::LEFT_CENTER, "#", Tokens::ui(11.0), t.text_dim); - p.text(pos2(cw.name + 22.0, hy), Align2::LEFT_CENTER, "Layer Name", Tokens::ui(11.0), t.text_dim); - for (i, icon) in - [Icon::Shy, Icon::Collapse, Icon::Quality, Icon::Fx, Icon::FrameBlend, Icon::MotionBlur, Icon::Adjustment, Icon::Cube].into_iter().enumerate() + let htext = |x: f32, s: &str| p.text(pos2(x, hy), Align2::LEFT_CENTER, s, Tokens::ui(11.0), t.text_dim); + if vis.av { + hic(Icon::Eye, cw.av + 10.0); + hic(Icon::Speaker, cw.av + 28.0); + hic(Icon::Solo, cw.av + 46.0); + hic(Icon::Lock, cw.av + 64.0); + } + if vis.keys { + htext(cw.keys + 6.0, "Keys"); + } + if vis.label { + icons::paint(&p, Rect::from_center_size(pos2(cw.label + 10.0, hy), vec2(10.0, 10.0)), Icon::Keyframe, t.text_dim); + } + if vis.num { + htext(cw.num + 6.0, "#"); + } + let name_hdr = if app.ui.timeline.source_name { "Source Name" } else { "Layer Name" }; + htext(cw.name + 22.0, name_hdr); + if vis.comment { + htext(cw.comment + 6.0, "Comment"); + } + if vis.switches { + for (i, icon) in + [Icon::Shy, Icon::Collapse, Icon::Quality, Icon::Fx, Icon::FrameBlend, Icon::MotionBlur, Icon::Adjustment, Icon::Cube].into_iter().enumerate() + { + hic(icon, cw.switches + SW * i as f32 + SW / 2.0); + } + } + if vis.modes { + htext(cw.mode + 4.0, "Mode"); + htext(cw.mode + 74.0, "T"); + htext(cw.trkmat + 4.0, "Track Matte"); + } + if vis.parent { + icons::paint(&p, Rect::from_center_size(pos2(cw.parent + 10.0, hy), vec2(12.0, 12.0)), Icon::PickWhip, t.text_dim); + htext(cw.parent + 20.0, "Parent & Link"); + } + for (on, x, s) in [(vis.in_, cw.in_, "In"), (vis.out, cw.out, "Out"), (vis.duration, cw.duration, "Duration"), (vis.stretch, cw.stretch, "Stretch")] { + if on { + htext(x + 6.0, s); + } + } + let hdr_rect = Rect::from_min_max(ch.min, pos2(graph_x0 - 1.0, ch.max.y)); + let hresp = ui.interact(hdr_rect, egui::Id::new("tl-colhdr"), Sense::click()); + app.auto.add("timeline.header", hdr_rect, "Columns (right-click)"); + for (id, label) in COLUMNS { + if let Some(x0) = match id { + "av" => vis.av.then_some((cw.av, AV_W)), + "keys" => vis.keys.then_some((cw.keys, KEYS_W)), + "label" => vis.label.then_some((cw.label, LABEL_W)), + "num" => vis.num.then_some((cw.num, NUM_W)), + "name" => Some((cw.name, cw.name_end - cw.name)), + "comment" => vis.comment.then_some((cw.comment, COMMENT_W)), + "switches" => vis.switches.then_some((cw.switches, SW * 8.0 + 6.0)), + "modes" => vis.modes.then_some((cw.mode, MODES_W)), + "parent" => vis.parent.then_some((cw.parent, PARENT_W)), + "in" => vis.in_.then_some((cw.in_, TIME_W)), + "out" => vis.out.then_some((cw.out, TIME_W)), + "duration" => vis.duration.then_some((cw.duration, TIME_W)), + _ => vis.stretch.then_some((cw.stretch, STRETCH_W)), + } { + app.auto.add(&format!("timeline.header.{id}"), Rect::from_min_size(pos2(x0.0, ch.min.y), vec2(x0.1, colhdr_h)), label); + } + } + if hresp.clicked() + && let Some(pt) = hresp.interact_pointer_pos() + && column_at(&cw, pt.x) == Some("name") { - hic(icon, cw.switches + SW * i as f32 + SW / 2.0); + app.ui.timeline.source_name = !app.ui.timeline.source_name; } - if app.ui.timeline.show_modes { - p.text(pos2(cw.mode + 4.0, hy), Align2::LEFT_CENTER, "Mode", Tokens::ui(11.0), t.text_dim); - p.text(pos2(cw.mode + 74.0, hy), Align2::LEFT_CENTER, "T", Tokens::ui(11.0), t.text_dim); - p.text(pos2(cw.trkmat + 4.0, hy), Align2::LEFT_CENTER, "Track Matte", Tokens::ui(11.0), t.text_dim); + if hresp.secondary_clicked() { + let under = hresp.interact_pointer_pos().and_then(|pt| column_at(&cw, pt.x)); + ctx.data_mut(|d| d.insert_temp(egui::Id::new("tl-colhdr-under"), under.unwrap_or(""))); } - icons::paint(&p, Rect::from_center_size(pos2(cw.parent + 10.0, hy), vec2(12.0, 12.0)), Icon::PickWhip, t.text_dim); - p.text(pos2(cw.parent + 20.0, hy), Align2::LEFT_CENTER, "Parent & Link", Tokens::ui(11.0), t.text_dim); + hresp.context_menu(|ui| { + let under: &str = ctx.data(|d| d.get_temp(egui::Id::new("tl-colhdr-under"))).unwrap_or(""); + if !under.is_empty() && under != "name" && ui.button("Hide This").clicked() { + actions.push(("timeline.column".into(), json!({"column": under, "visible": false}))); + ui.close(); + } + ui.menu_button("Columns", |ui| { + for (id, label) in COLUMNS { + let label = if id == "name" { if app.ui.timeline.source_name { "Source Name" } else { "Layer Name" } } else { label }; + let on = column_visible(&app.ui.timeline, id); + if ui.add_enabled(id != "name", egui::Button::new(label).selected(on)).clicked() { + actions.push(("timeline.column".into(), json!({"column": id, "visible": !on}))); + ui.close(); + } + } + }); + if ui.button(if app.ui.timeline.source_name { "Show Layer Name" } else { "Show Source Name" }).clicked() { + actions.push(("timeline.sourceName".into(), json!({}))); + ui.close(); + } + }); let _ = cw.end; // ---- rows. @@ -693,7 +1021,6 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let mut y = rows_rect.min.y - app.ui.timeline.scroll_y; let selected = app.session.state.selected_layers.clone(); let sel_keys = app.session.state.selected_keys.clone(); - let mut actions: Vec<(String, serde_json::Value)> = Vec::new(); let mut ui_actions: Vec = Vec::new(); let idx_of = |id: LayerId| comp.index_of(id).unwrap_or(0); let graph_on = app.ui.timeline.graph_editor; @@ -743,7 +1070,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { ]; for (i, (icon, on, name, applicable)) in av_items.into_iter().enumerate() { let br = Rect::from_center_size(pos2(cw.av + 10.0 + 18.0 * i as f32, cy), vec2(17.0, 17.0)); - if !applicable { + if !applicable || !vis.av { continue; } lp.rect_stroke(br.shrink(1.5), 2.0, Stroke::new(1.0, t.separator), StrokeKind::Inside); @@ -754,19 +1081,24 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } } // Label swatch. - let lr = Rect::from_center_size(pos2(cw.label + 10.0, cy), vec2(12.0, 12.0)); - lp.rect_filled(lr, 2.0, t.label(layer.label)); - let lresp = ui.interact(lr, egui::Id::new(("label", layer.id.0)), Sense::click()); - lresp.context_menu(|ui| { - for lab in effectcraft_engine::color::Label::ALL { - let name = if lab == effectcraft_engine::color::Label::None { lab.name().to_string() } else { app.session.prefs.label_name(lab) }; - if ui.button(name).clicked() { - actions.push(("edit.label".into(), json!({"layers": [layer.id.0], "label": lab.name()}))); - ui.close(); + if vis.label { + let lr = Rect::from_center_size(pos2(cw.label + 10.0, cy), vec2(12.0, 12.0)); + lp.rect_filled(lr, 2.0, t.label(layer.label)); + let lresp = ui.interact(lr, egui::Id::new(("label", layer.id.0)), Sense::click()); + lresp.context_menu(|ui| { + for lab in effectcraft_engine::color::Label::ALL { + let name = if lab == effectcraft_engine::color::Label::None { lab.name().to_string() } else { app.session.prefs.label_name(lab) }; + if ui.button(name).clicked() { + actions.push(("edit.label".into(), json!({"layers": [layer.id.0], "label": lab.name()}))); + ui.close(); + } } - } - }); - lp.text(pos2(cw.num + 13.0, cy), Align2::CENTER_CENTER, format!("{}", idx_of(layer.id)), Tokens::ui(11.5), t.text_dim); + }); + } + if vis.num { + lp.text(pos2(cw.num + 13.0, cy), Align2::CENTER_CENTER, format!("{}", idx_of(layer.id)), Tokens::ui(11.5), t.text_dim); + } + layer_cells(app, ui, &lp, &cw, &comp, layer, r, cy, &mut actions); // Twirl + icon + name. let tw = Rect::from_center_size(pos2(cw.name + 8.0, cy), vec2(14.0, 14.0)); let open = app.ui.timeline.open_layers.contains(&layer.id.0); @@ -775,7 +1107,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } app.auto.add(&format!("timeline.layer.{}.twirl", layer.id.0), tw, "twirl"); icons::paint(&lp, Rect::from_center_size(pos2(cw.name + 24.0, cy), vec2(13.0, 13.0)), layer_icon(layer, &app.session.project), t.text_dim); - let name_rect = Rect::from_min_max(pos2(cw.name + 34.0, r.min.y), pos2(cw.switches - 4.0, r.max.y)); + let name_rect = Rect::from_min_max(pos2(cw.name + 34.0, r.min.y), pos2(cw.name_end - 4.0, r.max.y)); let rename_id = egui::Id::new("tl-rename"); let renaming: Option<(u64, String)> = ctx.data(|d| d.get_temp(rename_id)); if let Some((rl, mut buf)) = renaming.filter(|(rl, _)| *rl == layer.id.0) { @@ -793,11 +1125,12 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } else { let name_col = if is_sel { Color32::WHITE } else { t.text }; let lpn = lp.with_clip_rect(name_rect.intersect(lp.clip_rect())); - lpn.text(pos2(name_rect.min.x, cy), Align2::LEFT_CENTER, &layer.name, Tokens::ui(12.0), name_col); + lpn.text(pos2(name_rect.min.x, cy), Align2::LEFT_CENTER, display_name(app, layer), Tokens::ui(12.0), name_col); } // Row click → select; double-click → rename; drag → reorder (later). + let row_x0 = if vis.num { cw.num } else { cw.name + 16.0 }; let row_resp = - ui.interact(Rect::from_min_max(pos2(cw.num, r.min.y), pos2(cw.switches, r.max.y)), egui::Id::new(("row", layer.id.0)), Sense::click()); + ui.interact(Rect::from_min_max(pos2(row_x0, r.min.y), pos2(cw.name_end, r.max.y)), egui::Id::new(("row", layer.id.0)), Sense::click()); app.auto.add(&format!("timeline.layer.{}.row", layer.id.0), left, &layer.name); if row_resp.clicked() { let m = ui.input(|i| i.modifiers); @@ -833,7 +1166,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { (Icon::Cube, layer.is_3d(), "threeD", layer.source.is_av() || matches!(layer.source, LayerSource::Null)), ]; for (i, (icon, on, name, applicable)) in sws.into_iter().enumerate() { - if !applicable { + if !applicable || !vis.switches { continue; } let br = Rect::from_center_size(pos2(cw.switches + SW * i as f32 + SW / 2.0, cy), vec2(16.0, 16.0)); @@ -904,28 +1237,31 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } } // Parent. - let pw_rect = Rect::from_center_size(pos2(cw.parent + 9.0, cy), vec2(15.0, 15.0)); - let pw = ui.interact(pw_rect, egui::Id::new(("parent-whip", layer.id.0)), Sense::drag()).on_hover_text("Parent pick whip: drag onto a layer"); - icons::paint(&lp, pw_rect.shrink(1.0), Icon::PickWhip, if pw.hovered() || pw.dragged() { t.text } else { t.text_dim }); - app.auto.add(&format!("timeline.layer.{}.pickWhip", layer.id.0), pw_rect, "Parent pick whip"); - if pw.drag_started() { - let pw_state: PickWhip = (0, layer.id.0, 0, pw_rect.center()); - ctx.data_mut(|d| d.insert_temp(pick_whip_id(), pw_state)); - } - let pr_rect = Rect::from_min_size(pos2(cw.parent + 20.0, cy - 9.0), vec2(94.0, 18.0)); - let plabel = layer.parent.and_then(|p| comp.layer(p)).map(|l| format!("{}. {}", idx_of(l.id), l.name)).unwrap_or_else(|| "None".into()); - let presp = widgets::dropdown(ui, pr_rect, &plabel, &t, egui::Id::new(("parent", layer.id.0))); - app.auto.add(&format!("timeline.layer.{}.parent", layer.id.0), pr_rect, "Parent"); - let pop = egui::Id::new(("par-pop", layer.id.0)); - if presp.clicked() { - widgets::open_popup(ui, pop); - } - let cands: Vec<&Layer> = comp.layers.iter().filter(|l| l.id != layer.id).collect(); - let mut popts = vec!["None".to_string(), "-".to_string()]; - popts.extend(cands.iter().map(|l| format!("{}. {}", idx_of(l.id), l.name))); - if let Some(i) = widgets::popup_menu(ui, pop, pr_rect.left_bottom(), &popts, None) { - let par = if i == 0 { serde_json::Value::Null } else { json!(cands[i - 2].id.0) }; - actions.push(("layer.setParent".into(), json!({"layers": [layer.id.0], "parent": par}))); + if vis.parent { + let pw_rect = Rect::from_center_size(pos2(cw.parent + 9.0, cy), vec2(15.0, 15.0)); + let pw = + ui.interact(pw_rect, egui::Id::new(("parent-whip", layer.id.0)), Sense::drag()).on_hover_text("Parent pick whip: drag onto a layer"); + icons::paint(&lp, pw_rect.shrink(1.0), Icon::PickWhip, if pw.hovered() || pw.dragged() { t.text } else { t.text_dim }); + app.auto.add(&format!("timeline.layer.{}.pickWhip", layer.id.0), pw_rect, "Parent pick whip"); + if pw.drag_started() { + let pw_state: PickWhip = (0, layer.id.0, 0, pw_rect.center()); + ctx.data_mut(|d| d.insert_temp(pick_whip_id(), pw_state)); + } + let pr_rect = Rect::from_min_size(pos2(cw.parent + 20.0, cy - 9.0), vec2(94.0, 18.0)); + let plabel = layer.parent.and_then(|p| comp.layer(p)).map(|l| format!("{}. {}", idx_of(l.id), l.name)).unwrap_or_else(|| "None".into()); + let presp = widgets::dropdown(ui, pr_rect, &plabel, &t, egui::Id::new(("parent", layer.id.0))); + app.auto.add(&format!("timeline.layer.{}.parent", layer.id.0), pr_rect, "Parent"); + let pop = egui::Id::new(("par-pop", layer.id.0)); + if presp.clicked() { + widgets::open_popup(ui, pop); + } + let cands: Vec<&Layer> = comp.layers.iter().filter(|l| l.id != layer.id).collect(); + let mut popts = vec!["None".to_string(), "-".to_string()]; + popts.extend(cands.iter().map(|l| format!("{}. {}", idx_of(l.id), l.name))); + if let Some(i) = widgets::popup_menu(ui, pop, pr_rect.left_bottom(), &popts, None) { + let par = if i == 0 { serde_json::Value::Null } else { json!(cands[i - 2].id.0) }; + actions.push(("layer.setParent".into(), json!({"layers": [layer.id.0], "parent": par}))); + } } // Layer bar. ui.set_clip_rect(right_clip); @@ -1190,10 +1526,11 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { gp.rect_filled(Rect::from_min_max(pos2(graph_x0, r.min.y), r.max), 0.0, t.tl_bg); let indent = cw.name + 6.0 + 14.0 * row.depth as f32; // Keyframe navigator (in the A/V column, like AE). - if prop.is_animated() { + // (in the Keys column when it is shown). + if prop.is_animated() && (vis.keys || vis.av) { let lt = layer.layer_time(time); let at_key = effectcraft_engine::keyframe::key_at(&prop.keys, lt).is_some(); - let nav_x = cw.av + 18.0; + let nav_x = if vis.keys { cw.keys + 12.0 } else { cw.av + 18.0 }; let prev = Rect::from_center_size(pos2(nav_x, cy), vec2(12.0, 14.0)); let mid = Rect::from_center_size(pos2(nav_x + 16.0, cy), vec2(14.0, 14.0)); let next = Rect::from_center_size(pos2(nav_x + 32.0, cy), vec2(12.0, 14.0)); @@ -1556,6 +1893,101 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } } +/// The optional per-layer cells: Comment (double-click to edit), In / Out / Duration (drag to +/// change by frames), Stretch (click: Time Stretch dialog). +#[allow(clippy::too_many_arguments)] +fn layer_cells( + app: &mut EffectcraftApp, + ui: &mut egui::Ui, + lp: &egui::Painter, + cw: &Cols, + comp: &Comp, + layer: &Layer, + r: Rect, + cy: f32, + actions: &mut Vec<(String, serde_json::Value)>, +) { + let t = app.tokens; + let ctx = ui.ctx().clone(); + let lid = layer.id.0; + if cw.vis.comment { + let cr = Rect::from_min_max(pos2(cw.comment + 4.0, r.min.y + 1.0), pos2(cw.comment + COMMENT_W - 4.0, r.max.y - 1.0)); + let edit_id = egui::Id::new("tl-comment-edit"); + let editing: Option<(u64, String)> = ctx.data(|d| d.get_temp(edit_id)); + app.auto.add(&format!("timeline.layer.{lid}.comment"), cr, &layer.comment); + if let Some((el, mut buf)) = editing.filter(|(el, _)| *el == lid) { + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(cr)); + let er = child.add(egui::TextEdit::singleline(&mut buf).font(Tokens::ui(11.5)).desired_width(cr.width())); + er.request_focus(); + if er.lost_focus() { + if !ui.input(|i| i.key_pressed(egui::Key::Escape)) && buf != layer.comment { + actions.push(("layer.setComment".into(), json!({"layers": [el], "comment": buf}))); + } + ctx.data_mut(|d| d.remove::<(u64, String)>(edit_id)); + } else { + ctx.data_mut(|d| d.insert_temp(edit_id, (el, buf))); + } + } else { + let resp = ui.interact(cr, egui::Id::new(("tl-comment", lid)), Sense::click()).on_hover_text("Double-click to edit the comment"); + lp.with_clip_rect(cr.intersect(lp.clip_rect())).text(pos2(cr.min.x + 2.0, cy), Align2::LEFT_CENTER, &layer.comment, Tokens::ui(11.5), t.text_dim); + if resp.double_clicked() { + ctx.data_mut(|d| d.insert_temp(edit_id, (lid, layer.comment.clone()))); + } + } + } + let fd = comp.frame_duration().seconds(); + let cells = [ + (cw.vis.in_, cw.in_, "in", layer.in_point.seconds()), + (cw.vis.out, cw.out, "out", layer.out_point.seconds()), + (cw.vis.duration, cw.duration, "duration", (layer.out_point - layer.in_point).seconds()), + ]; + for (on, x, kind, secs) in cells { + if !on { + continue; + } + let cr = Rect::from_min_max(pos2(x + 2.0, r.min.y + 1.0), pos2(x + TIME_W - 2.0, r.max.y - 1.0)); + let tc = crate::panels::timecode(&app.session, comp, Tick::from_seconds_f64(secs) - if kind == "duration" { comp.display_start } else { Tick(0) }); + let resp = ui.interact(cr, egui::Id::new(("tl-cell", kind, lid)), Sense::drag()).on_hover_text("Drag to change"); + lp.text(pos2(cr.min.x + 4.0, cy), Align2::LEFT_CENTER, &tc, Tokens::mono(11.0), if resp.hovered() || resp.dragged() { t.hot_text } else { t.timecode }); + app.auto.add(&format!("timeline.layer.{lid}.{kind}"), cr, &tc); + if resp.hovered() || resp.dragged() { + ctx.set_cursor_icon(egui::CursorIcon::ResizeHorizontal); + } + let acc_id = egui::Id::new(("tl-cell-acc", kind, lid)); + if resp.dragged() { + let mut acc: f32 = ctx.data(|d| d.get_temp(acc_id).unwrap_or(0.0)); + acc += resp.drag_delta().x; + let frames = (acc / 4.0).trunc() as i64; + if frames != 0 { + acc -= frames as f32 * 4.0; + let d = frames as f64 * fd; + let merge = format!("cell-{kind}-{lid}"); + let params = match kind { + "in" => json!({"layers": [lid], "in": (layer.in_point.seconds() + d).max(0.0), "merge": merge}), + _ => json!({"layers": [lid], "out": (layer.out_point.seconds() + d).max(layer.in_point.seconds() + fd), "merge": merge}), + }; + actions.push(("layer.timing".into(), params)); + } + ctx.data_mut(|d| d.insert_temp(acc_id, acc)); + } + if resp.drag_stopped() { + ctx.data_mut(|d| d.remove::(acc_id)); + actions.push(("__endMerge".into(), json!({}))); + } + } + if cw.vis.stretch { + let cr = Rect::from_min_max(pos2(cw.stretch + 2.0, r.min.y + 1.0), pos2(cw.stretch + STRETCH_W - 2.0, r.max.y - 1.0)); + let label = format!("{:.1}%", layer.stretch * 100.0); + let resp = ui.interact(cr, egui::Id::new(("tl-stretch", lid)), Sense::click()).on_hover_text("Time Stretch…"); + lp.text(pos2(cr.min.x + 4.0, cy), Align2::LEFT_CENTER, &label, Tokens::ui(11.5), if resp.hovered() { t.hot_text } else { t.text }); + app.auto.add(&format!("timeline.layer.{lid}.stretch"), cr, &label); + if resp.clicked() { + actions.push(("layer.select".into(), json!({"layers": [lid]}))); + actions.push(("layer.timeStretch".into(), json!({}))); + } + } +} + enum UiAct { ToggleLayer(u64, bool), ToggleGroup(u64), @@ -1856,3 +2288,136 @@ fn value_editor( } } } + +#[cfg(test)] +mod tests { + use super::*; + + /// A comp with two solids; "Box" has an animated Position and a Gaussian Blur. + fn app() -> EffectcraftApp { + let mut s = effectcraft_engine::Session::default(); + s.execute("comp.new", json!({"name": "T", "width": 320, "height": 180, "duration": 4})).unwrap(); + s.execute("layer.newSolid", json!({"name": "Plate", "color": "#406080", "width": 320, "height": 180})).unwrap(); + s.execute("layer.newSolid", json!({"name": "Box", "color": "#e04020", "width": 40, "height": 40})).unwrap(); + let comp = s.active_comp().unwrap(); + let bx = comp.layers.iter().find(|l| l.name == "Box").unwrap().id.0; + s.execute("layer.select", json!({"layers": [bx]})).unwrap(); + s.execute("effect.apply", json!({"effect": "ec.blur.gaussian"})).unwrap(); + let pos = s.active_comp().unwrap().layer(LayerId(bx)).unwrap().transform().unwrap().get("position").unwrap().uid; + s.execute("prop.toggleAnimation", json!({"layer": bx, "prop": pos})).unwrap(); + EffectcraftApp::new(s) + } + + fn labels(app: &EffectcraftApp) -> Vec { + let comp = app.session.active_comp().unwrap().clone(); + build_rows(app, &comp) + .into_iter() + .map(|r| { + let l = comp.layer(r.layer).unwrap(); + match r.kind { + RowKind::Layer => l.name.clone(), + RowKind::Group { name, .. } => format!("{}>{name}", " ".repeat(r.depth)), + RowKind::Prop { uid } => format!("{}{}", " ".repeat(r.depth), l.props.find(uid).unwrap().name), + _ => "?".into(), + } + }) + .collect() + } + + #[test] + fn search_filters_layers_and_reveals_matching_properties() { + let mut app = app(); + app.ui.timeline.search = "opac".into(); + let rows = labels(&app); + // Both layers have Transform > Opacity; nothing else shows. + assert_eq!(rows.iter().filter(|r| r.trim() == "Opacity").count(), 2, "{rows:?}"); + assert!(rows.iter().any(|r| r.trim() == ">Transform"), "{rows:?}"); + assert!(!rows.iter().any(|r| r.trim() == "Position"), "{rows:?}"); + // A layer name match shows the layer alone; a group match brings its contents. + app.ui.timeline.search = "plate".into(); + assert_eq!(labels(&app), vec!["Plate".to_string()]); + app.ui.timeline.search = "GAUSSIAN".into(); + let rows = labels(&app); + assert_eq!(rows[0], "Box"); + assert!(rows.iter().any(|r| r.trim() == ">Effects") && rows.iter().any(|r| r.trim() == "Blurriness"), "{rows:?}"); + assert!(!rows.contains(&"Plate".to_string())); + // Nothing matches → no rows. + app.ui.timeline.search = "zzz".into(); + assert!(labels(&app).is_empty()); + // Through the command. + let ctx = egui::Context::default(); + crate::menus::invoke(&mut app, &ctx, "timeline.search", json!({"query": "scale"})).unwrap(); + assert!(labels(&app).iter().any(|r| r.trim() == "Scale")); + } + + #[test] + fn columns_toggle_and_layout() { + let mut app = app(); + let tl = &mut app.ui.timeline; + assert!(column_visible(tl, "switches") && !column_visible(tl, "comment") && !column_visible(tl, "modes")); + set_column(tl, "comment", true).unwrap(); + set_column(tl, "stretch", true).unwrap(); + set_column(tl, "keys", false).unwrap(); + set_column(tl, "label", false).unwrap(); + set_column(tl, "modes", true).unwrap(); + assert!(tl.show_modes && column_visible(tl, "comment") && !column_visible(tl, "label")); + assert!(set_column(tl, "name", false).is_err()); + assert!(set_column(tl, "bogus", true).is_err()); + let vis = Vis::of(tl); + let cw = cols(0.0, 1400.0, vis); + // Hidden columns take no room; visible ones are in After Effects' order. + assert_eq!(cw.keys, cw.label); + assert_eq!(cw.label, cw.num); + assert!(cw.name < cw.comment && cw.comment < cw.switches && cw.switches < cw.mode && cw.parent < cw.stretch); + assert_eq!(column_at(&cw, cw.comment + 5.0), Some("comment")); + assert_eq!(column_at(&cw, cw.name + 5.0), Some("name")); + assert_eq!(column_at(&cw, cw.stretch + 5.0), Some("stretch")); + // Through the command (as agents do). + let ctx = egui::Context::default(); + let r = crate::menus::invoke(&mut app, &ctx, "timeline.column", json!({"column": "duration"})).unwrap(); + assert!(r["columns"].as_array().unwrap().iter().any(|c| c == "duration")); + crate::menus::invoke(&mut app, &ctx, "timeline.column", json!({"column": "duration", "visible": false})).unwrap(); + assert!(!column_visible(&app.ui.timeline, "duration")); + crate::menus::invoke(&mut app, &ctx, "timeline.sourceName", json!({})).unwrap(); + assert!(app.ui.timeline.source_name); + } + + #[test] + fn reveal_keys_add_with_shift_and_uu_reveals_modified() { + let mut app = app(); + let ctx = egui::Context::default(); + crate::menus::invoke(&mut app, &ctx, "timeline.reveal.opacity", json!({})).unwrap(); + assert_eq!(app.ui.timeline.reveal, vec!["opacity"]); + crate::menus::invoke(&mut app, &ctx, "timeline.revealAdd.scale", json!({})).unwrap(); + crate::menus::invoke(&mut app, &ctx, "timeline.revealAdd.effects", json!({})).unwrap(); + assert_eq!(app.ui.timeline.reveal, vec!["opacity", "scale", "effects"]); + let rows = labels(&app); + assert!(rows.iter().any(|r| r.trim() == "Scale") && rows.iter().any(|r| r.trim() == "Opacity"), "{rows:?}"); + assert!(rows.iter().any(|r| r.trim() == ">Gaussian Blur"), "{rows:?}"); + // Tree order: Scale before Opacity. + let si = rows.iter().position(|r| r.trim() == "Scale").unwrap(); + let oi = rows.iter().position(|r| r.trim() == "Opacity").unwrap(); + assert!(si < oi); + // Shift+S again removes Scale. + crate::menus::invoke(&mut app, &ctx, "timeline.revealAdd.scale", json!({})).unwrap(); + assert_eq!(app.ui.timeline.reveal, vec!["opacity", "effects"]); + // U: keyframed properties (Position); UU (twice quickly): every modified property. + crate::menus::invoke(&mut app, &ctx, "anim.reveal", json!({"kind": "keyframes"})).unwrap(); + let n_keys = app.ui.timeline.reveal_props.len(); + assert_eq!(app.ui.timeline.reveal, vec!["props"]); + assert!(n_keys >= 1); + crate::menus::invoke(&mut app, &ctx, "anim.reveal", json!({"kind": "keyframes"})).unwrap(); + assert!(app.ui.timeline.reveal_props.len() > n_keys, "UU reveals modified properties too"); + } + + #[test] + fn cti_snaps_to_the_nearest_candidate() { + assert_eq!(snap_time(&[0.0, 1.0, 2.0], 1.04, 0.1), 1.0); + assert_eq!(snap_time(&[0.0, 1.0, 2.0], 1.5, 0.1), 1.5); + assert_eq!(snap_time(&[1.0, 1.1], 1.06, 0.1), 1.1); + let app = app(); + let comp = app.session.active_comp().unwrap().clone(); + let c = snap_candidates(&comp, &build_rows(&app, &comp)); + assert!(c.contains(&comp.duration.seconds()) && c.contains(&0.0)); + } +} diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index d1350103..af59bb6b 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -360,6 +360,18 @@ pub struct TimelineState { /// Properties whose inline expression editor is collapsed. #[serde(default)] pub expr_closed: BTreeSet, + /// Visible optional columns (column header right-click ▸ Columns): `av`, `keys`, `label`, + /// `num`, `comment`, `switches`, `parent`, `in`, `out`, `duration`, `stretch`. The name + /// column is always shown; Modes follows `show_modes` (F4). + #[serde(default = "default_tl_columns")] + pub columns: BTreeSet, + /// The name column shows Source Name instead of Layer Name (click its header). + #[serde(default)] + pub source_name: bool, +} + +pub fn default_tl_columns() -> BTreeSet { + ["av", "label", "num", "switches", "parent"].map(String::from).into_iter().collect() } fn value_graph() -> String { @@ -391,6 +403,8 @@ impl Default for TimelineState { graph_reference: false, graph_transform_box: true, expr_closed: BTreeSet::new(), + columns: default_tl_columns(), + source_name: false, } } } From a86b02901e7d3c9134f41968bc396bcd7ecbab71 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:26:14 -0400 Subject: [PATCH 18/23] UI-7: Home screen with recent projects, thumbnails and community links The Home screen (panels/home.rs) gets New Project / Open Project / New Composition / Open Demo Project / Import, the recent projects from Settings (File > Open Recent) with thumbnails, folder and modified date (click opens, Clear list), and the ArtCraft community links (Discord, getartcraft.com, the EffectCraft page, GitHub, sibling apps) where After Effects has Learn / What's New. A 96x54 thumbnail is stored in the config store when a project is opened or saved and the viewer shows it. Automation ids: home., home.recent., home.clearRecent, home.sibling.. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/lib.rs | 7 + crates/ui-egui/src/panels/home.rs | 389 ++++++++++++++++++++++++++++++ crates/ui-egui/src/panels/misc.rs | 72 +----- crates/ui-egui/src/panels/mod.rs | 3 +- 4 files changed, 399 insertions(+), 72 deletions(-) create mode 100644 crates/ui-egui/src/panels/home.rs diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e903811d..59005cdc 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -164,6 +164,10 @@ pub struct EffectcraftApp { /// Docked groups laid out last frame: (active panel, group rect) — `~` maximizes the one /// under the pointer. pub(crate) dock_rects: Vec<(PanelKind, egui::Rect)>, + /// Home screen: recent-project thumbnail textures by path (None = no thumbnail). + pub(crate) home_thumbs: std::collections::HashMap>, + /// The (project path, saved revision) whose thumbnail was stored last. + pub(crate) home_thumb_saved: Option<(String, u64)>, } impl EffectcraftApp { @@ -208,6 +212,8 @@ impl EffectcraftApp { applied_prefs: None, recovery: None, dock_rects: vec![], + home_thumbs: Default::default(), + home_thumb_saved: None, } .with_ui_commands() } @@ -762,6 +768,7 @@ impl EffectcraftApp { let body = egui::Rect::from_min_max(egui::pos2(full.min.x + 4.0, header.max.y + 2.0), egui::pos2(full.max.x - 4.0, full.max.y - 4.0)); self.dock_area(ui, body); panels::precomp::mini_flowchart(self, &ctx); + panels::home::capture_thumbnail(self); panels::dialogs::show(self, &ctx); self.draw_toast(ui, full); } diff --git a/crates/ui-egui/src/panels/home.rs b/crates/ui-egui/src/panels/home.rs new file mode 100644 index 00000000..42ded4b7 --- /dev/null +++ b/crates/ui-egui/src/panels/home.rs @@ -0,0 +1,389 @@ +//! The Home screen (shown in the Composition panel at launch, from the header's Home button, and +//! by the Learn workspace): New Project / Open Project / New Composition / Open Demo, the recent +//! projects (File ▸ Open Recent, from Settings) with thumbnails, folders and dates, and where +//! After Effects has its Learn / What's New area, the ArtCraft community links. +//! +//! Thumbnails: when a project is opened or saved and the viewer has its frame, a 96×54 RGB +//! thumbnail is stored in the config store (`thumb-.txt`, hex) and shown here. + +use std::hash::{Hash, Hasher}; + +use egui::{Align2, Color32, Rect, Sense, Stroke, StrokeKind, pos2, vec2}; +use serde::Serialize; +use serde_json::json; + +use crate::icons::{self, Icon}; +use crate::theme::Tokens; +use crate::{EffectcraftApp, widgets}; + +pub const THUMB_W: usize = 96; +pub const THUMB_H: usize = 54; + +/// One row of the recent projects list. +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct RecentEntry { + pub index: usize, + pub path: String, + pub name: String, + pub folder: String, + /// Last modified (`2026-10-02 14:05`, UTC), when the file is readable. + pub modified: Option, + pub exists: bool, +} + +/// The recent projects, most recent first (Settings ▸ General ▸ recent items). +pub fn recent_entries(prefs: &effectcraft_engine::prefs::Prefs) -> Vec { + prefs + .recent_projects + .iter() + .enumerate() + .map(|(index, p)| { + let path = std::path::Path::new(p); + let meta = std::fs::metadata(path).ok(); + let modified = meta + .as_ref() + .and_then(|m| m.modified().ok()) + .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()) + .map(|d| format_utc(d.as_secs() as i64)); + RecentEntry { + index, + path: p.clone(), + name: path.file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| p.clone()), + folder: path.parent().map(|d| d.to_string_lossy().to_string()).unwrap_or_default(), + modified, + exists: meta.is_some(), + } + }) + .collect() +} + +/// `YYYY-MM-DD HH:MM` (UTC) from Unix seconds. +pub fn format_utc(secs: i64) -> String { + let days = secs.div_euclid(86_400); + let rem = secs.rem_euclid(86_400); + // Civil date from days since 1970-01-01 (Howard Hinnant's algorithm). + let z = days + 719_468; + let era = z.div_euclid(146_097); + let doe = z - era * 146_097; + let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + let mp = (5 * doy + 2) / 153; + let d = doy - (153 * mp + 2) / 5 + 1; + let m = if mp < 10 { mp + 3 } else { mp - 9 }; + let y = yoe + era * 400 + i64::from(m <= 2); + format!("{y:04}-{m:02}-{d:02} {:02}:{:02}", rem / 3600, (rem % 3600) / 60) +} + +/// The config-store name of a project's thumbnail. +pub fn thumb_name(path: &str) -> String { + let mut h = std::collections::hash_map::DefaultHasher::new(); + path.hash(&mut h); + format!("thumb-{:016x}.txt", h.finish()) +} + +/// Downscale an image to a letterboxed THUMB_W×THUMB_H RGB thumbnail (hex text). +pub fn encode_thumb(img: &egui::ColorImage) -> String { + let [w, h] = img.size; + let mut out = String::with_capacity(THUMB_W * THUMB_H * 6); + let k = (THUMB_W as f32 / w.max(1) as f32).min(THUMB_H as f32 / h.max(1) as f32); + let (dw, dh) = (w as f32 * k, h as f32 * k); + let (ox, oy) = ((THUMB_W as f32 - dw) / 2.0, (THUMB_H as f32 - dh) / 2.0); + for y in 0..THUMB_H { + for x in 0..THUMB_W { + let (fx, fy) = ((x as f32 + 0.5 - ox) / k, (y as f32 + 0.5 - oy) / k); + let c = if fx >= 0.0 && fy >= 0.0 && (fx as usize) < w && (fy as usize) < h { + let p = img.pixels[fy as usize * w + fx as usize]; + [p.r(), p.g(), p.b()] + } else { + [0x16, 0x17, 0x1c] + }; + for v in c { + out.push_str(&format!("{v:02x}")); + } + } + } + out +} + +pub fn decode_thumb(text: &str) -> Option { + let t = text.trim(); + if t.len() != THUMB_W * THUMB_H * 6 { + return None; + } + let b: Vec = (0..t.len()).step_by(2).map(|i| u8::from_str_radix(&t[i..i + 2], 16)).collect::>().ok()?; + let px = b.chunks_exact(3).map(|c| Color32::from_rgb(c[0], c[1], c[2])).collect(); + Some(egui::ColorImage::new([THUMB_W, THUMB_H], px)) +} + +/// Store the viewer's frame as the open project's thumbnail after it is opened or saved (once per +/// saved revision, when the viewer shows that revision). +pub fn capture_thumbnail(app: &mut EffectcraftApp) { + let s = &app.session; + let (Some(path), Some(_)) = (s.path.clone(), s.config.as_ref()) else { return }; + if s.is_dirty() { + return; + } + let key = (path.clone(), s.saved_revision); + if app.home_thumb_saved.as_ref() == Some(&key) { + return; + } + if app.viewer_shown.as_ref().map(|(_, k)| k.revision) != Some(s.revision) { + return; + } + let Some(img) = app.viewer_pixels() else { return }; + let text = encode_thumb(&img); + if let Some(c) = &app.session.config + && let Err(e) = c.write(&thumb_name(&path), &text) + { + log::warn!("project thumbnail: {e}"); + } + app.home_thumbs.remove(&path); + app.home_thumb_saved = Some(key); +} + +fn thumb_texture(app: &mut EffectcraftApp, ctx: &egui::Context, path: &str) -> Option { + if let Some(t) = app.home_thumbs.get(path) { + return t.clone(); + } + let tex = app + .session + .config + .as_ref() + .and_then(|c| c.read(&thumb_name(path))) + .and_then(|t| decode_thumb(&t)) + .map(|img| ctx.load_texture(format!("home-thumb-{path}"), img, egui::TextureOptions::LINEAR)); + app.home_thumbs.insert(path.to_string(), tex.clone()); + tex +} + +/// Draw the Home screen. +pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { + let t = app.tokens; + let ctx = ui.ctx().clone(); + let p = ui.painter().with_clip_rect(rect); + p.rect_filled(rect, 0.0, Color32::from_rgb(0x1b, 0x1d, 0x24)); + let wide = rect.width() >= 820.0; + let pad = 32.0; + let left_w = if wide { 250.0 } else { rect.width() - pad * 2.0 }; + let mut actions: Vec<(&str, serde_json::Value)> = vec![]; + + // ---- left: brand + start actions. + let x0 = rect.min.x + pad; + let mut y = rect.min.y + pad; + crate::header::paint_logo(&p, Rect::from_min_size(pos2(x0, y), vec2(40.0, 40.0))); + p.text(pos2(x0 + 52.0, y + 12.0), Align2::LEFT_CENTER, "EffectCraft", Tokens::semibold(22.0), Color32::WHITE); + p.text(pos2(x0 + 52.0, y + 32.0), Align2::LEFT_CENTER, format!("Version {}", env!("CARGO_PKG_VERSION")), Tokens::ui(11.0), t.text_faint); + y += 64.0; + for (label, id, primary) in [ + ("New Project", "file.newProject", true), + ("Open Project…", "file.open", false), + ("New Composition", "app.newComp", false), + ("Open Demo Project", "file.openDemoProject", false), + ("Import Footage…", "file.import", false), + ] { + let r = Rect::from_min_size(pos2(x0, y), vec2(left_w.min(250.0), 32.0)); + if widgets::text_button(ui, r, label, primary, &t, egui::Id::new(("home", id))).clicked() { + actions.push((id, json!({}))); + } + app.auto.add(&format!("home.{id}"), r, label); + y += 40.0; + } + + // ---- community (After Effects' Learn / What's New area). + let (cx, mut cy, cw) = if wide { (x0, y + 18.0, left_w) } else { (x0, y + 10.0, left_w) }; + p.text(pos2(cx, cy), Align2::LEFT_CENTER, "Community", Tokens::semibold(13.0), t.text); + cy += 16.0; + p.text(pos2(cx, cy), Align2::LEFT_CENTER, "Tutorials, help and what's new", Tokens::ui(11.0), t.text_faint); + cy += 16.0; + let links = [ + (Icon::Chat, "Join the ArtCraft Discord", "help.discord"), + (Icon::Globe, "getartcraft.com", "help.website"), + (Icon::Globe, "EffectCraft home page", "help.appPage"), + (Icon::Code, "EffectCraft on GitHub", "help.github"), + ]; + for (icon, label, cmd) in links { + let r = Rect::from_min_size(pos2(cx, cy), vec2(cw.min(250.0), 30.0)); + let resp = ui.interact(r, egui::Id::new(("home-link", cmd)), Sense::click()); + let discord = cmd == "help.discord"; + let bg = if discord { + Color32::from_rgb(0x58, 0x65, 0xf2) + } else if resp.hovered() { + t.hover + } else { + Color32::from_rgb(0x2a, 0x2c, 0x34) + }; + p.rect_filled(r, 15.0, bg); + icons::paint(&p, Rect::from_center_size(pos2(r.min.x + 20.0, r.center().y), vec2(14.0, 14.0)), icon, Color32::WHITE); + p.text(pos2(r.min.x + 36.0, r.center().y), Align2::LEFT_CENTER, label, Tokens::medium(12.0), Color32::WHITE); + app.auto.add(&format!("home.{cmd}"), r, label); + if resp.clicked() { + let _ = app.session.execute(cmd, json!({})); + } + cy += 36.0; + } + cy += 8.0; + p.text(pos2(cx, cy), Align2::LEFT_CENTER, "More ArtCraft apps", Tokens::ui(11.0), t.text_faint); + cy += 12.0; + let sib = effectcraft_engine::links::SIBLINGS; + let per_row = 2usize; + let sw = (cw.min(250.0) - 6.0) / per_row as f32; + for (i, (name, slug)) in sib.iter().enumerate() { + let r = Rect::from_min_size(pos2(cx + (i % per_row) as f32 * (sw + 6.0), cy + (i / per_row) as f32 * 28.0), vec2(sw, 24.0)); + let resp = ui.interact(r, egui::Id::new(("sib", *slug)), Sense::click()); + p.rect_filled(r, 12.0, if resp.hovered() { t.hover } else { Color32::from_rgb(0x24, 0x26, 0x2e) }); + p.text(r.center(), Align2::CENTER_CENTER, *name, Tokens::ui(11.5), t.text); + app.auto.add(&format!("home.sibling.{slug}"), r, name); + if resp.clicked() { + let _ = app.session.execute("help.sibling", json!({"app": slug})); + } + } + let community_bottom = cy + sib.len().div_ceil(per_row) as f32 * 28.0; + + // ---- recent projects. + let (rx, mut ry, rw) = if wide { + let rx = x0 + left_w + 40.0; + (rx, rect.min.y + pad + 6.0, rect.max.x - pad - rx) + } else { + (x0, community_bottom + 24.0, left_w) + }; + p.text(pos2(rx, ry + 6.0), Align2::LEFT_CENTER, "Recent", Tokens::semibold(16.0), Color32::WHITE); + let entries = recent_entries(&app.session.prefs); + if !entries.is_empty() { + let cr = Rect::from_min_size(pos2(rx + rw - 100.0, ry - 4.0), vec2(100.0, 20.0)); + let resp = ui.interact(cr, egui::Id::new("home-clear-recent"), Sense::click()); + p.text(cr.right_center(), Align2::RIGHT_CENTER, "Clear list", Tokens::ui(11.0), if resp.hovered() { t.text } else { t.text_faint }); + app.auto.add("home.clearRecent", cr, "Clear Recent Projects"); + if resp.clicked() { + actions.push(("file.clearRecent", json!({}))); + } + } + ry += 26.0; + // Column headings. + let date_w = 130.0; + p.text(pos2(rx + 112.0, ry), Align2::LEFT_CENTER, "Name", Tokens::ui(11.0), t.text_faint); + p.text(pos2(rx + rw - date_w, ry), Align2::LEFT_CENTER, "Modified", Tokens::ui(11.0), t.text_faint); + ry += 10.0; + p.line_segment([pos2(rx, ry), pos2(rx + rw, ry)], Stroke::new(1.0, t.separator)); + ry += 6.0; + if entries.is_empty() { + p.text(pos2(rx, ry + 20.0), Align2::LEFT_CENTER, "No recent projects. Projects you open or save appear here.", Tokens::ui(12.0), t.text_faint); + } + let row_h = 66.0; + for e in &entries { + let r = Rect::from_min_size(pos2(rx, ry), vec2(rw, row_h - 4.0)); + if r.min.y > rect.max.y { + break; + } + let resp = ui.interact(r, egui::Id::new(("home-recent", e.index)), Sense::click()).on_hover_text(&e.path); + if resp.hovered() { + p.rect_filled(r, 6.0, t.hover); + } + let tr = Rect::from_min_size(pos2(r.min.x + 6.0, r.min.y + 4.0), vec2(THUMB_W as f32, THUMB_H as f32)); + match thumb_texture(app, &ctx, &e.path) { + Some(tex) => { + p.image(tex.id(), tr, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); + } + None => { + p.rect_filled(tr, 3.0, Color32::from_rgb(0x24, 0x26, 0x2e)); + icons::paint(&p, Rect::from_center_size(tr.center(), vec2(20.0, 20.0)), Icon::Comp, t.text_faint); + } + } + p.rect_stroke(tr, 3.0, Stroke::new(1.0, t.separator), StrokeKind::Inside); + let tx = tr.max.x + 12.0; + let text_clip = p.with_clip_rect(Rect::from_min_max(pos2(tx, r.min.y), pos2(r.max.x - date_w - 8.0, r.max.y))); + text_clip.text(pos2(tx, r.min.y + 20.0), Align2::LEFT_CENTER, &e.name, Tokens::semibold(13.0), if e.exists { Color32::WHITE } else { t.text_dim }); + let sub = if e.exists { e.folder.clone() } else { format!("{} (missing)", e.folder) }; + text_clip.text(pos2(tx, r.min.y + 40.0), Align2::LEFT_CENTER, sub, Tokens::ui(11.0), t.text_faint); + if let Some(d) = &e.modified { + p.text(pos2(r.max.x - date_w, r.min.y + 20.0), Align2::LEFT_CENTER, d, Tokens::ui(11.0), t.text_dim); + } + app.auto.add(&format!("home.recent.{}", e.index), r, &e.path); + if resp.clicked() { + actions.push(("file.openRecent", json!({"index": e.index}))); + } + ry += row_h; + } + + for (id, params) in actions { + let before = (app.session.path.clone(), app.session.revision); + match crate::menus::invoke(app, &ctx, id, params) { + Err(e) => app.ui.status = e, + // Leave Home unless only the list changed or a file dialog was cancelled. + Ok(_) => { + let cancelled = matches!(id, "file.open" | "file.import") && (app.session.path.clone(), app.session.revision) == before; + if id != "file.clearRecent" && !cancelled { + app.ui.start_screen = false; + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn recent_list_comes_from_settings() { + let mut prefs = effectcraft_engine::prefs::Prefs::default(); + prefs.push_recent("/projects/a/Alpha.ecproj"); + prefs.push_recent("/projects/b/Beta.ecproj"); + let e = recent_entries(&prefs); + assert_eq!(e.len(), 2); + assert_eq!((e[0].name.as_str(), e[0].folder.as_str(), e[0].index), ("Beta", "/projects/b", 0)); + assert_eq!(e[1].name, "Alpha"); + assert!(!e[0].exists && e[0].modified.is_none()); + // A real file has a date. + let dir = std::env::temp_dir().join(format!("ec-home-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let f = dir.join("Real.ecproj"); + std::fs::write(&f, "{}").unwrap(); + prefs.push_recent(&f.to_string_lossy()); + let e = recent_entries(&prefs); + assert!(e[0].exists && e[0].modified.as_ref().is_some_and(|d| d.len() == 16), "{:?}", e[0]); + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn dates_and_thumbnails() { + assert_eq!(format_utc(0), "1970-01-01 00:00"); + assert_eq!(format_utc(1_790_000_000), "2026-09-21 14:13"); + assert_eq!(format_utc(951_782_400), "2000-02-29 00:00"); + let img = egui::ColorImage::new([200, 100], vec![Color32::from_rgb(10, 200, 30); 200 * 100]); + let text = encode_thumb(&img); + let back = decode_thumb(&text).unwrap(); + assert_eq!(back.size, [THUMB_W, THUMB_H]); + // Letterboxed: the middle is the image, the top row is the background (2:1 into 16:9). + assert_eq!(back.pixels[THUMB_H / 2 * THUMB_W + THUMB_W / 2], Color32::from_rgb(10, 200, 30)); + assert_ne!(back.pixels[THUMB_W / 2], Color32::from_rgb(10, 200, 30)); + assert!(decode_thumb("abc").is_none()); + assert_ne!(thumb_name("/a.ecproj"), thumb_name("/b.ecproj")); + } + + #[test] + fn home_screen_registers_its_controls() { + let mut s = effectcraft_engine::Session::default(); + s.prefs.push_recent("/projects/Alpha.ecproj"); + let mut app = EffectcraftApp::new(s); + let ctx = egui::Context::default(); + crate::theme::install(&ctx, &app.tokens); + ctx.run_ui(Default::default(), |_| {}).textures_delta.clear(); + ctx.run_ui(Default::default(), |ui| { + app.auto.begin_frame(); + show(&mut app, ui, Rect::from_min_size(pos2(0.0, 0.0), vec2(1200.0, 800.0))); + }) + .textures_delta + .clear(); + for id in [ + "home.file.newProject", + "home.file.open", + "home.app.newComp", + "home.file.openDemoProject", + "home.help.discord", + "home.help.github", + "home.recent.0", + ] { + assert!(app.auto.find(id).is_some(), "{id}"); + } + } +} diff --git a/crates/ui-egui/src/panels/misc.rs b/crates/ui-egui/src/panels/misc.rs index 4d8e1e24..0db1ed7f 100644 --- a/crates/ui-egui/src/panels/misc.rs +++ b/crates/ui-egui/src/panels/misc.rs @@ -4,7 +4,7 @@ use egui::{Align2, Color32, Rect, Sense, Stroke, pos2, vec2}; use serde_json::json; use crate::dock::PanelKind; -use crate::icons::{self, Icon}; +use crate::icons::Icon; use crate::theme::Tokens; use crate::{EffectcraftApp, widgets}; @@ -223,73 +223,3 @@ pub fn markers(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } } } - -/// Home screen: new/open, recent, and community links. -pub fn start_screen(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { - let t = app.tokens; - let ctx = ui.ctx().clone(); - let p = ui.painter().with_clip_rect(rect); - p.rect_filled(rect, 0.0, Color32::from_rgb(0x1b, 0x1d, 0x24)); - let c = rect.center(); - let logo = Rect::from_center_size(c + vec2(0.0, -150.0), vec2(64.0, 64.0)); - crate::header::paint_logo(&p, logo); - p.text(c + vec2(0.0, -96.0), Align2::CENTER_CENTER, "EffectCraft", Tokens::semibold(28.0), Color32::WHITE); - p.text(c + vec2(0.0, -66.0), Align2::CENTER_CENTER, "Motion graphics and visual effects, in pure Rust.", Tokens::ui(13.0), t.text_dim); - let mut y = c.y - 30.0; - for (label, id, primary) in [ - ("New Composition", "app.newComp", true), - ("Open Demo Project", "file.openDemoProject", false), - ("Open Project…", "file.open", false), - ("Import Footage…", "file.import", false), - ] { - let r = Rect::from_center_size(pos2(c.x, y + 16.0), vec2(240.0, 32.0)); - if widgets::text_button(ui, r, label, primary, &t, egui::Id::new(("home", id))).clicked() { - let _ = crate::menus::invoke(app, &ctx, id, json!({})); - app.ui.start_screen = false; - } - app.auto.add(&format!("home.{id}"), r, label); - y += 40.0; - } - // Community. - y += 14.0; - let links = [(Icon::Chat, "Join the Discord", "help.discord"), (Icon::Globe, "getartcraft.com", "help.website"), (Icon::Code, "GitHub", "help.github")]; - let w = 170.0; - let mut x = c.x - w * 1.5 - 8.0; - for (icon, label, cmd) in links { - let r = Rect::from_min_size(pos2(x, y), vec2(w, 30.0)); - let resp = ui.interact(r, egui::Id::new(("home-link", cmd)), Sense::click()); - let discord = cmd == "help.discord"; - let bg = if discord { - Color32::from_rgb(0x58, 0x65, 0xf2) - } else if resp.hovered() { - t.hover - } else { - Color32::from_rgb(0x2a, 0x2c, 0x34) - }; - p.rect_filled(r, 15.0, bg); - icons::paint(&p, Rect::from_center_size(pos2(r.min.x + 20.0, r.center().y), vec2(14.0, 14.0)), icon, Color32::WHITE); - p.text(pos2(r.min.x + 34.0, r.center().y), Align2::LEFT_CENTER, label, Tokens::medium(12.0), Color32::WHITE); - app.auto.add(&format!("home.{cmd}"), r, label); - if resp.clicked() { - let _ = app.session.execute(cmd, json!({})); - } - x += w + 8.0; - } - // Siblings. - y += 52.0; - p.text(pos2(c.x, y), Align2::CENTER_CENTER, "More ArtCraft apps", Tokens::ui(11.5), t.text_faint); - y += 18.0; - let sib = effectcraft_engine::links::SIBLINGS; - let sw = 104.0; - let mut x = c.x - sw * sib.len() as f32 / 2.0; - for (name, slug) in sib { - let r = Rect::from_min_size(pos2(x + 2.0, y), vec2(sw - 4.0, 24.0)); - let resp = ui.interact(r, egui::Id::new(("sib", *slug)), Sense::click()); - p.rect_filled(r, 12.0, if resp.hovered() { t.hover } else { Color32::from_rgb(0x24, 0x26, 0x2e) }); - p.text(r.center(), Align2::CENTER_CENTER, *name, Tokens::ui(11.5), t.text); - if resp.clicked() { - let _ = app.session.execute("help.sibling", json!({"app": slug})); - } - x += sw; - } -} diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs index 3f737502..dae89f16 100644 --- a/crates/ui-egui/src/panels/mod.rs +++ b/crates/ui-egui/src/panels/mod.rs @@ -10,6 +10,7 @@ pub mod forms; pub mod fx_widgets; pub mod graph; pub mod graph_tools; +pub mod home; pub mod info; pub mod key_dialogs; pub mod layer_panel; @@ -63,7 +64,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: PanelKind, rect: Rec match p { PanelKind::Composition => { if app.ui.start_screen { - misc::start_screen(app, ui, rect); + home::show(app, ui, rect); } else { viewer::show(app, ui, rect) } From ed72e67155e17a2b1349b66462b26bccf5ef06bc Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:27:22 -0400 Subject: [PATCH 19/23] M6.7: refine edge + freeze + UI Refine Edge tool (Alt+W cycles Roto Brush / Refine Edge), Layer panel roto strokes with matte overlays (Alpha Boundary in pink, Alpha, Alpha Overlay, View Search Region), segmentation span bar, Freeze/Propagate in the Layer panel and Effect Controls, background propagation polling, docs. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 2 +- crates/engine/src/commands/roto_cmds.rs | 4 +- crates/ui-egui/src/icons.rs | 10 + crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/menus.rs | 3 +- crates/ui-egui/src/panels/effect_controls.rs | 31 ++ crates/ui-egui/src/panels/layer_panel.rs | 39 ++- crates/ui-egui/src/panels/mod.rs | 1 + crates/ui-egui/src/panels/roto_tool.rs | 331 +++++++++++++++++++ crates/ui-egui/src/state.rs | 21 +- docs/agents.md | 22 ++ docs/architecture.md | 13 + docs/parity.md | 4 +- 13 files changed, 467 insertions(+), 15 deletions(-) create mode 100644 crates/ui-egui/src/panels/roto_tool.rs diff --git a/ROADMAP.md b/ROADMAP.md index a3ed979c..55b05822 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -32,6 +32,6 @@ Measured feature by feature in [docs/parity.md](docs/parity.md) (end of 2 Octobe - **≈ 100 agent-hours of work remain by the audit's estimate.** With five Claude Opus 5.5 agents in parallel that is **≈ 8–10 hours of wall-clock time at the pace measured so far, up to ≈ 25 hours** by the audit's conservative per-feature figures. -- Hardest remaining pieces: Roto Brush, the 3D camera tracker, Advanced 3D, a scripting object model. +- Hardest remaining pieces: the 3D camera tracker, Advanced 3D, a scripting object model (Roto Brush & Refine Edge landed in M6.7). Come tell us what matters most to you on [Discord](https://discord.gg/artcraft). diff --git a/crates/engine/src/commands/roto_cmds.rs b/crates/engine/src/commands/roto_cmds.rs index 5b9db20f..95326b42 100644 --- a/crates/engine/src/commands/roto_cmds.rs +++ b/crates/engine/src/commands/roto_cmds.rs @@ -378,14 +378,12 @@ fn status(s: &mut Session, p: &Value) -> Result { let (layer, g) = group(s, cid, lid, uid)?; let (layer, g) = (layer.clone(), g.clone()); let d = data_of(&g); - let computed = s.roto_computed(cid, lid, uid); out["layer"] = json!(lid.0); out["effect"] = json!(uid); out["base"] = json!(d.base); out["span"] = json!(d.span); out["strokes"] = json!(d.strokes.len()); out["strokeFrames"] = json!(d.stroke_frames()); - out["computed"] = json!(computed.iter().filter(|(_, c)| **c).map(|(f, _)| *f).collect::>()); out["frozen"] = json!(frozen_of(&g)); out["pending"] = json!(s.roto_pending.contains(&(cid, lid, uid))); out["limits"] = json!(rt::frame_limits(&comp, &layer)); @@ -420,5 +418,7 @@ fn status(s: &mut Session, p: &Value) -> Result { } } } + let computed = s.roto_computed(cid, lid, uid); + out["computed"] = json!(computed.iter().filter(|(_, c)| **c).map(|(f, _)| *f).collect::>()); Ok(out) } diff --git a/crates/ui-egui/src/icons.rs b/crates/ui-egui/src/icons.rs index d28cb950..d2de8fc3 100644 --- a/crates/ui-egui/src/icons.rs +++ b/crates/ui-egui/src/icons.rs @@ -33,6 +33,7 @@ pub enum Icon { Clone, Eraser, RotoBrush, + RefineEdge, Puppet, // switches & timeline Eye, @@ -282,6 +283,15 @@ pub fn paint(painter: &Painter, rect: Rect, icon: Icon, color: Color32) { pen.ellipse(6.0, 10.0, 3.6, 3.6); pen.dot(6.0, 10.0, 1.0); } + RefineEdge => { + // A brush over a soft (dashed) edge. + pen.line(&[(13.5, 2.5), (9.0, 7.0)]); + pen.ellipse(7.0, 9.0, 2.6, 2.6); + pen.line(&[(2.0, 14.0), (3.5, 12.5)]); + pen.line(&[(5.0, 14.0), (6.5, 12.5)]); + pen.line(&[(8.0, 14.0), (9.5, 12.5)]); + pen.line(&[(11.0, 14.0), (12.5, 12.5)]); + } Puppet => { pen.circle(8.0, 3.6, 1.6); pen.line(&[(8.0, 5.2), (8.0, 10.0)]); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 071c1948..71cb9f45 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -725,6 +725,7 @@ impl EffectcraftApp { // Warp Stabilizer: finish analyses and start queued (re-)analyses in the background. if self.session.warp_job.is_some() || !self.session.warp_pending.is_empty() { self.session.poll_warp(true); + self.session.poll_roto(true); ctx.request_repaint_after(std::time::Duration::from_millis(100)); } self.apply_prefs(&ctx); diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index da5eaec2..b14ade47 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -72,7 +72,7 @@ pub const UI_COMMANDS: &[UiCommand] = &[ uic!("tool.pen", "Pen Tool", [], Some("G")), uic!("tool.type", "Type Tools", [], Some("Cmd+T")), uic!("tool.brush", "Brush Tools", [], Some("Cmd+B")), - uic!("tool.rotoBrush", "Roto Brush Tool", [], Some("Alt+W")), + uic!("tool.rotoBrush", "Roto Brush & Refine Edge Tools", [], Some("Alt+W")), uic!("tool.puppet", "Puppet Tools", [], Some("Cmd+P")), uic!("app.newComp", "New Composition...", [], None), uic!("app.compSettings", "Composition Settings...", [], None), @@ -139,6 +139,7 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V "pen" => Some(9), "type" => Some(10), "brush" => Some(11), + "rotoBrush" => Some(14), "puppet" => Some(15), _ => None, }; diff --git a/crates/ui-egui/src/panels/effect_controls.rs b/crates/ui-egui/src/panels/effect_controls.rs index 0a19d57f..03afd94c 100644 --- a/crates/ui-egui/src/panels/effect_controls.rs +++ b/crates/ui-egui/src/panels/effect_controls.rs @@ -537,6 +537,35 @@ fn warp_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, l p.text(pos2(r.min.x + 24.0, r.min.y + 38.0), Align2::LEFT_CENTER, status, Tokens::ui(11.5), t.text_dim); } +/// Roto Brush & Refine Edge: Freeze / Unfreeze and Propagate buttons and the segmentation status. +fn roto_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, layer: &Layer, g: &PropGroup, r: Rect, actions: &mut Actions) { + use effectcraft_engine::effects::roto as fx; + let t = app.tokens; + let params = effectcraft_engine::effects::flatten_params(g, &mut |pr| pr.value.clone()); + let frozen = fx::is_frozen(¶ms); + let d = fx::data(¶ms); + let running = app.session.roto_target().is_some_and(|(_, l, u, _)| l == layer.id && u == g.uid) && app.session.is_roto_running(); + let b1 = Rect::from_min_size(pos2(r.min.x + 24.0, r.min.y + 4.0), vec2(84.0, 22.0)); + let b2 = Rect::from_min_size(pos2(b1.max.x + 8.0, b1.min.y), vec2(84.0, 22.0)); + let label = if frozen { "Unfreeze" } else { "Freeze" }; + if widgets::text_button(ui, b1, label, frozen, &t, egui::Id::new(("roto-freeze-ec", g.uid))).clicked() && !running { + let id = if frozen { "roto.unfreeze" } else { "roto.freeze" }; + actions.push((id.into(), json!({"layer": layer.id.0, "effect": g.uid}))); + } + app.auto.add(&format!("effectControls.roto.{}.freeze", g.uid), b1, label); + let (l2, id2) = if running { ("Stop", "roto.cancel") } else { ("Propagate", "roto.propagate") }; + if widgets::text_button(ui, b2, l2, false, &t, egui::Id::new(("roto-prop-ec", g.uid))).clicked() && !frozen { + actions.push((id2.into(), json!({"layer": layer.id.0, "effect": g.uid}))); + } + app.auto.add(&format!("effectControls.roto.{}.propagate", g.uid), b2, l2); + let status = match (app.session.roto_progress(), d.base) { + (Some(pr), _) if running => pr.banner(), + (_, None) => "No strokes: paint with the Roto Brush tool (Alt+W) in the Layer panel".to_string(), + (_, Some(b)) => format!("Base frame {b}, span {}–{}{}", d.span[0], d.span[1], if frozen { " (frozen)" } else { "" }), + }; + p.text(pos2(r.min.x + 24.0, r.min.y + 38.0), Align2::LEFT_CENTER, status, Tokens::ui(11.5), t.text_dim); +} + // --------------------------------------------------------------------------------------------- // Curves / Levels editors @@ -545,6 +574,7 @@ fn editor_height(effect: &str, width: f32) -> f32 { match effect { "ec.color.curves" => 34.0 + curves_size(width) + 26.0, effectcraft_engine::effects::warp_stab::ID => 50.0, + effectcraft_engine::effects::roto::ID => 50.0, "ec.color.levels" | "ec.color.levelsic" => 34.0 + 80.0 + 58.0, "ec.color.autolevels" | "ec.color.autocontrast" | "ec.color.autocolor" => 24.0 + 80.0 + 12.0, _ => 0.0, @@ -920,6 +950,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { match effect.as_str() { "ec.color.curves" => curves_editor(app, ui, &bp, &layer, g, &ectx, er, &mut actions), effectcraft_engine::effects::warp_stab::ID => warp_editor(app, ui, &bp, &layer, g, er, &mut actions), + effectcraft_engine::effects::roto::ID => roto_editor(app, ui, &bp, &layer, g, er, &mut actions), "ec.color.levels" | "ec.color.levelsic" => levels_editor(app, ui, &bp, &layer, g, effect, &ectx, er, &mut actions), _ => histogram_only(app, ui, &bp, g, er), } diff --git a/crates/ui-egui/src/panels/layer_panel.rs b/crates/ui-egui/src/panels/layer_panel.rs index e53e268e..c20a0fba 100644 --- a/crates/ui-egui/src/panels/layer_panel.rs +++ b/crates/ui-egui/src/panels/layer_panel.rs @@ -3,9 +3,13 @@ //! //! The image is the layer's source and masks followed by its effects up to the View menu's //! choice (by default the last Paint effect, as After Effects shows "Paint"). Dragging with a -//! paint tool records the stroke's points in layer space and, on release, runs `paint.stroke` +//! paint tools records the stroke's points in layer space and, on release, runs `paint.stroke` //! at the current time (the drag's duration feeds Write On). Alt/Option-click with the Clone //! Stamp sets the clone source. +//! +//! With a Roto Brush & Refine Edge effect the panel shows the effect's input with the matte +//! overlay, the segmentation span bar and the view / Freeze buttons; the Roto Brush and Refine +//! Edge tools paint their strokes here (see [`super::roto_tool`]). use effectcraft_engine::effects::paint; use effectcraft_engine::project::{Layer, LayerId}; @@ -46,9 +50,15 @@ pub fn current_layer(app: &EffectcraftApp) -> Option { comp.layer(id).cloned() } -/// Number of effects shown by default: through the last Paint effect (none when there isn't one). -fn default_view(layer: &Layer) -> usize { - layer.effects().and_then(|fx| fx.groups().enumerate().filter(|(_, g)| paint::is_paint(g)).map(|(i, _)| i + 1).last()).unwrap_or(0) +/// Number of effects shown by default: the Roto Brush's input while a Roto Brush tool is active +/// (or when there is no Paint effect), else through the last Paint effect (none when there +/// isn't one). +fn default_view(layer: &Layer, tool: Tool) -> usize { + let paint = layer.effects().and_then(|fx| fx.groups().enumerate().filter(|(_, g)| paint::is_paint(g)).map(|(i, _)| i + 1).last()); + match super::roto_tool::roto_index(layer) { + Some(r) if tool.roto_kind(false).is_some() || paint.is_none() => r, + _ => paint.unwrap_or(0), + } } pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { @@ -64,7 +74,8 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let comp = app.session.project.comp(cid).cloned().unwrap_or_else(|| effectcraft_engine::project::Comp::new(1, 1, Default::default(), Default::default())); let time = app.session.time(); let fx_count = layer.effects().map(|f| f.groups().count()).unwrap_or(0); - let view = app.ui.layer_view.map(|v| v.min(fx_count)).unwrap_or_else(|| default_view(&layer)); + let view = app.ui.layer_view.map(|v| v.min(fx_count)).unwrap_or_else(|| default_view(&layer, app.ui.tool)); + let roto = super::roto_tool::roto_index(&layer); // Title strip. let top = Rect::from_min_size(rect.min, vec2(rect.width(), 24.0)); @@ -90,9 +101,17 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { } let tc = super::timecode(&app.session, &comp, time); p.text(pos2(bar.max.x - 10.0, bar.center().y), Align2::RIGHT_CENTER, tc, Tokens::mono(12.0), t.timecode); + // Roto Brush: view / Freeze buttons and the segmentation span bar. + let mut canvas_bottom = bar.min.y; + if roto.is_some() { + super::roto_tool::bar_buttons(app, ui, &p, bar, vr.max.x + 12.0, &layer); + let sb = Rect::from_min_max(pos2(rect.min.x, bar.min.y - 18.0), pos2(rect.max.x, bar.min.y)); + super::roto_tool::span_bar(app, ui, &p, sb, &comp, &layer); + canvas_bottom = sb.min.y; + } // Canvas. - let area = Rect::from_min_max(pos2(rect.min.x, top.max.y), pos2(rect.max.x, bar.min.y)); + let area = Rect::from_min_max(pos2(rect.min.x, top.max.y), pos2(rect.max.x, canvas_bottom)); p.rect_filled(area, 0.0, t.pasteboard); let (w, h) = effectcraft_engine::render::source_size(&app.session.project, &layer); let (lw, lh) = if w == 0 { (comp.width as f64, comp.height as f64) } else { (w as f64, h as f64) }; @@ -139,10 +158,18 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let r = Rect::from_min_max(to_screen([tx.rect[0], tx.rect[1]]), to_screen([tx.rect[2], tx.rect[3]])); painter.image(tx.tex.id(), r, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); } + if roto.is_some_and(|r| r == view) { + super::roto_tool::draw_overlay(app, &ctx, &painter, &comp, &layer, scale, &to_screen); + } painter.rect_stroke(frame, 0.0, Stroke::new(1.0, Color32::from_black_alpha(160)), StrokeKind::Outside); app.auto.add("layerPanel.canvas", frame, &layer.name); app.auto.add("layerPanel.area", area, "Layer panel"); + // Interaction: Roto Brush / Refine Edge. + if app.ui.tool.roto_kind(false).is_some() { + super::roto_tool::interact(app, ui, &painter, area, &layer, zoom, &to_screen, &to_layer); + return; + } // Interaction: paint tools. let resp = ui.interact(area, egui::Id::new("layer-panel-interact"), Sense::click_and_drag()); let kind = app.ui.tool.paint_kind(); diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs index 3f737502..8a8ce0cf 100644 --- a/crates/ui-egui/src/panels/mod.rs +++ b/crates/ui-egui/src/panels/mod.rs @@ -21,6 +21,7 @@ pub mod project; pub mod properties; pub mod puppet_tool; pub mod render_queue; +pub mod roto_tool; pub mod settings; pub mod shortcut_editor; pub mod text_panels; diff --git a/crates/ui-egui/src/panels/roto_tool.rs b/crates/ui-egui/src/panels/roto_tool.rs new file mode 100644 index 00000000..25354ee3 --- /dev/null +++ b/crates/ui-egui/src/panels/roto_tool.rs @@ -0,0 +1,331 @@ +//! Roto Brush and Refine Edge tools in the Layer panel: green foreground strokes, Alt/Option red +//! background strokes, Refine Edge strokes along soft edges; the matte overlay (Alpha Boundary in +//! pink, Alpha, Alpha Overlay), the segmentation span bar and the Freeze button. + +use effectcraft_engine::effects::roto as fx; +use effectcraft_engine::project::{Comp, Layer}; +use effectcraft_engine::render::{EvalCtx, RenderOpts, Renderer}; +use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2}; +use serde_json::json; + +use crate::theme::Tokens; +use crate::{EffectcraftApp, widgets}; + +/// Position of the layer's first Roto Brush & Refine Edge effect. +pub fn roto_index(layer: &Layer) -> Option { + layer.effects()?.groups().position(|g| matches!(&g.kind, effectcraft_engine::project::GroupKind::Effect { effect } if effect == fx::ID)) +} + +fn roto_group(layer: &Layer) -> Option<&effectcraft_engine::project::PropGroup> { + layer.effects()?.groups().nth(roto_index(layer)?) +} + +/// A stroke being drawn. +#[derive(Clone, Debug)] +struct Drag { + layer: u64, + kind: &'static str, + points: Vec<[f64; 2]>, +} + +#[derive(Clone)] +struct Overlay { + key: (u64, u64, i64, u32, String), + tex: egui::TextureHandle, + rect: [f64; 4], +} + +fn c32(c: [f64; 3], a: u8) -> Color32 { + Color32::from_rgba_unmultiplied((c[0] * 255.0) as u8, (c[1] * 255.0) as u8, (c[2] * 255.0) as u8, a) +} + +/// Draw the matte overlay of the Roto Brush on `layer` (the panel shows the effect's input). +pub fn draw_overlay( + app: &mut EffectcraftApp, + ctx: &egui::Context, + painter: &egui::Painter, + comp: &Comp, + layer: &Layer, + scale: f64, + to_screen: &dyn Fn([f64; 2]) -> Pos2, +) { + let Some(idx) = roto_index(layer) else { return }; + let o = app.session.state.roto.clone(); + if o.view == "none" { + return; + } + let Some(cid) = app.session.active_comp_id() else { return }; + let time = app.session.time(); + let key = ( + app.session.revision, + layer.id.0, + time.0, + (scale * 1000.0) as u32, + format!("{}{:?}{:?}{}", o.view, o.overlay_color, o.boundary_color, o.overlay_opacity), + ); + let id = egui::Id::new("roto-overlay"); + let cached: Option = ctx.data(|d| d.get_temp(id)); + let ov = match cached { + Some(c) if c.key == key => Some(c), + _ => { + let ectx = EvalCtx { project: &app.session.project, comp_id: cid, comp, time, expr: app.session.expr.as_deref() }; + let mut r = Renderer::new(&app.session.project, &*app.session.footage, RenderOpts { scale, ..Default::default() }); + r.expr = app.session.expr.as_deref(); + r.cache = Some(&app.session.layer_cache); + let (Some(inp), Some(out)) = (r.layer_input(&ectx, layer, idx), r.layer_input(&ectx, layer, idx + 1)) else { return }; + let (w, h) = (inp.img.width as usize, inp.img.height as usize); + let a_at = |x: i64, y: i64| -> f32 { + // Output pixel at the same layer point. + let lx = (x as f64 + 0.5 - inp.offset[0]) / inp.scale; + let ly = (y as f64 + 0.5 - inp.offset[1]) / inp.scale; + let (ox, oy) = out.to_px([lx, ly]); + out.img.get(ox.floor() as i64, oy.floor() as i64)[3] + }; + let alpha: Vec = (0..w * h).map(|i| a_at((i % w) as i64, (i / w) as i64)).collect(); + let inside = |x: i64, y: i64| x >= 0 && y >= 0 && x < w as i64 && y < h as i64 && alpha[y as usize * w + x as usize] >= 0.5; + let mut px = vec![Color32::TRANSPARENT; w * h]; + let bc = c32(o.boundary_color, 255); + let oc = c32(o.overlay_color, (o.overlay_opacity / 100.0 * 255.0).clamp(0.0, 255.0) as u8); + // View Search Region: the band around the boundary (yellow). + let search = roto_group(layer).filter(|g| g.prop("rotoBrushMatte/viewSearchRegion").is_some_and(|p| p.value.as_bool())).map(|g| { + let r = g.prop("rotoBrushMatte/searchRadius").map(|p| p.value.as_f64()).unwrap_or(15.0) * scale; + let bin: Vec = alpha.iter().map(|a| (*a >= 0.5) as u8).collect(); + let sd = effectcraft_engine::track::roto::matting::signed_distance(&bin, w, h); + (sd, r as f32) + }); + for y in 0..h as i64 { + for x in 0..w as i64 { + let i = y as usize * w + x as usize; + let a = alpha[i]; + px[i] = match o.view.as_str() { + "alpha" => { + let v = (a.clamp(0.0, 1.0) * 255.0) as u8; + Color32::from_rgb(v, v, v) + } + "alphaOverlay" if a < 0.5 => oc, + "alphaOverlay" => Color32::TRANSPARENT, + _ => { + let me = a >= 0.5; + let edge = me && [(-1, 0), (1, 0), (0, -1), (0, 1)].iter().any(|(dx, dy)| !inside(x + dx, y + dy)); + if edge { bc } else { Color32::TRANSPARENT } + } + }; + if let Some((sd, r)) = &search { + let d = sd[i].abs(); + if (d - r).abs() < 0.75 { + px[i] = Color32::from_rgb(255, 220, 0); + } + } + } + } + let img = egui::ColorImage { size: [w, h], pixels: px, source_size: egui::Vec2::new(w as f32, h as f32) }; + let x0 = -inp.offset[0] / inp.scale; + let y0 = -inp.offset[1] / inp.scale; + let rect = [x0, y0, x0 + w as f64 / inp.scale, y0 + h as f64 / inp.scale]; + let tex = ctx.load_texture("roto-overlay", img, egui::TextureOptions::NEAREST); + let c = Overlay { key, tex, rect }; + ctx.data_mut(|d| d.insert_temp(id, c.clone())); + Some(c) + } + }; + if let Some(ov) = ov { + let r = Rect::from_min_max(to_screen([ov.rect[0], ov.rect[1]]), to_screen([ov.rect[2], ov.rect[3]])); + painter.image(ov.tex.id(), r, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); + } +} + +/// Pointer handling for the Roto Brush / Refine Edge tools. Returns true when it handled input. +pub fn interact( + app: &mut EffectcraftApp, + ui: &mut egui::Ui, + painter: &egui::Painter, + area: Rect, + layer: &Layer, + zoom: f64, + to_screen: &dyn Fn([f64; 2]) -> Pos2, + to_layer: &dyn Fn(Pos2) -> [f64; 2], +) -> bool { + let ctx = ui.ctx().clone(); + let mods = ui.input(|i| i.modifiers); + let Some(kind) = app.ui.tool.roto_kind(mods.alt) else { return false }; + let resp = ui.interact(area, egui::Id::new("layer-panel-roto"), Sense::click_and_drag()); + let o = app.session.state.roto.clone(); + let refine = kind.starts_with("refine"); + let dia = if refine { o.refine_diameter } else { o.diameter }; + let col = match kind { + "fg" => Color32::from_rgb(40, 220, 60), + "bg" => Color32::from_rgb(235, 50, 40), + "refine" => Color32::from_rgb(110, 200, 255), + _ => Color32::from_rgb(255, 160, 60), + }; + if let Some(hp) = resp.hover_pos() { + let lp = to_layer(hp); + app.pointer_comp = Some([lp[0] as f32, lp[1] as f32]); + ctx.set_cursor_icon(egui::CursorIcon::Crosshair); + let r = (dia * 0.5 * zoom) as f32; + painter.circle_stroke(hp, r, Stroke::new(1.5, col)); + let sign = if kind == "bg" || kind == "refineErase" { "−" } else { "+" }; + painter.text(hp, Align2::CENTER_CENTER, sign, Tokens::semibold(12.0), col); + } + let did = egui::Id::new("layer-panel-roto-drag"); + if resp.drag_started() + && let Some(pos) = ui.input(|i| i.pointer.press_origin()).or(resp.interact_pointer_pos()) + { + ctx.data_mut(|d| d.insert_temp(did, Drag { layer: layer.id.0, kind, points: vec![to_layer(pos)] })); + } + if let Some(mut d) = ctx.data(|d| d.get_temp::(did)) { + if let Some(pos) = resp.interact_pointer_pos() { + let lp = to_layer(pos); + let last = d.points.last().copied().unwrap_or(lp); + if (lp[0] - last[0]).hypot(lp[1] - last[1]) * zoom >= 1.5 { + d.points.push(lp); + ctx.data_mut(|dd| dd.insert_temp(did, d.clone())); + } + } + let width = ((dia * zoom) as f32).max(1.0); + let pts: Vec = d.points.iter().map(|q| to_screen(*q)).collect(); + let c = col.gamma_multiply(0.6); + if pts.len() == 1 { + painter.circle_filled(pts[0], width / 2.0, c); + } else { + painter.add(egui::Shape::line(pts, Stroke::new(width, c))); + } + if resp.drag_stopped() { + ctx.data_mut(|dd| dd.remove::(did)); + run_stroke(app, d.layer, d.kind, &d.points, dia / 2.0); + } + } else if resp.clicked() + && let Some(pos) = resp.interact_pointer_pos() + { + run_stroke(app, layer.id.0, kind, &[to_layer(pos)], dia / 2.0); + } + true +} + +fn run_stroke(app: &mut EffectcraftApp, layer: u64, kind: &str, points: &[[f64; 2]], radius: f64) { + let r = app.session.execute("roto.stroke", json!({"layer": layer, "kind": kind, "points": points, "radius": radius})); + if let Err(e) = r { + app.ui.status = e.to_string(); + } +} + +/// The segmentation span bar (above the panel's bottom bar): the layer's frames, the span, the +/// base frame (yellow), computed frames (green) and stroke frames. Click to go to a frame; drag +/// the span's ends to change it. +pub fn span_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, r: Rect, comp: &Comp, layer: &Layer) { + let t = app.tokens; + let Some(g) = roto_group(layer) else { return }; + let Some(cid) = app.session.active_comp_id() else { return }; + let uid = g.uid; + let params = effectcraft_engine::effects::flatten_params(g, &mut |p| p.value.clone()); + let d = fx::data(¶ms); + p.rect_filled(r, 0.0, t.panel_bg); + let lim = effectcraft_engine::roto::frame_limits(comp, layer); + let n = (lim[1] - lim[0] + 1).max(1) as f32; + let track = Rect::from_min_max(pos2(r.min.x + 10.0, r.min.y + 4.0), pos2(r.max.x - 10.0, r.max.y - 4.0)); + let x_of = |f: f64| track.min.x + ((f - lim[0] as f64) / n as f64) as f32 * track.width(); + let f_of = |x: f32| (lim[0] as f64 + ((x - track.min.x) / track.width()).clamp(0.0, 0.9999) as f64 * n as f64).floor() as i64; + p.rect_filled(track, 2.0, t.pasteboard); + if let Some(base) = d.base { + let sp = Rect::from_min_max(pos2(x_of(d.span[0] as f64), track.min.y), pos2(x_of(d.span[1] as f64 + 1.0), track.max.y)); + let frozen = fx::is_frozen(¶ms); + p.rect_filled(sp, 2.0, if frozen { Color32::from_rgb(60, 110, 190) } else { Color32::from_rgb(90, 90, 96) }); + for (f, done) in app.session.roto_computed(cid, layer.id, uid) { + if done { + let cr = Rect::from_min_max(pos2(x_of(f as f64), track.max.y - 4.0), pos2(x_of(f as f64 + 1.0), track.max.y)); + p.rect_filled(cr, 0.0, Color32::from_rgb(70, 200, 90)); + } + } + for f in d.stroke_frames() { + let x = x_of(f as f64 + 0.5); + p.line_segment([pos2(x, track.min.y), pos2(x, track.min.y + 5.0)], Stroke::new(2.0, Color32::WHITE)); + } + let bx = x_of(base as f64 + 0.5); + p.rect_filled(Rect::from_center_size(pos2(bx, track.center().y), vec2(6.0, track.height())), 1.0, Color32::from_rgb(240, 200, 40)); + } + // Current time. + let fps = comp.frame_rate.as_f64(); + let cur = fx::frame_of(layer.layer_time(app.session.time()).seconds(), fps); + let cx = x_of(cur as f64 + 0.5); + p.line_segment([pos2(cx, r.min.y), pos2(cx, r.max.y)], Stroke::new(1.0, t.timecode)); + p.rect_stroke(track, 2.0, Stroke::new(1.0, Color32::from_black_alpha(140)), StrokeKind::Outside); + app.auto.add("layerPanel.rotoSpan", track, "Segmentation span"); + let resp = ui.interact(track, egui::Id::new("roto-span-bar"), Sense::click_and_drag()); + let hid = egui::Id::new("roto-span-handle"); + if resp.drag_started() + && let (Some(pos), Some(_)) = (resp.interact_pointer_pos(), d.base) + { + let (a, b) = (x_of(d.span[0] as f64), x_of(d.span[1] as f64 + 1.0)); + let h = if (pos.x - a).abs() < 6.0 { + 1 + } else if (pos.x - b).abs() < 6.0 { + 2 + } else { + 0 + }; + ui.ctx().data_mut(|m| m.insert_temp(hid, h)); + } + let handle: i32 = ui.ctx().data(|m| m.get_temp(hid)).unwrap_or(0); + if let Some(pos) = resp.interact_pointer_pos() { + let f = f_of(pos.x); + if handle == 0 && (resp.clicked() || resp.dragged()) { + let t = layer.comp_time(comp.frame_rate.tick_of(f)); + let _ = app.session.execute("time.set", json!({"time": t.seconds()})); + } + if handle != 0 && resp.dragged() { + p.line_segment([pos2(pos.x, track.min.y), pos2(pos.x, track.max.y)], Stroke::new(2.0, Color32::from_rgb(240, 200, 40))); + } + if handle != 0 && resp.drag_stopped() { + let k = if handle == 1 { "start" } else { "end" }; + if let Err(e) = app.session.execute("roto.span", json!({"layer": layer.id.0, "effect": uid, k: f})) { + app.ui.status = e.to_string(); + } + } + } + if resp.drag_stopped() { + ui.ctx().data_mut(|m| m.remove::(hid)); + } +} + +/// View-mode toggles and Freeze / Propagate buttons in the Layer panel's bottom bar, from `x`. +pub fn bar_buttons(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, bar: Rect, x: f32, layer: &Layer) { + let t = app.tokens; + let mut x = x; + let view = app.session.state.roto.view.clone(); + for (id, label) in [("alpha", "Alpha"), ("alphaBoundary", "Boundary"), ("alphaOverlay", "Overlay")] { + let r = Rect::from_min_size(pos2(x, bar.center().y - 10.0), vec2(66.0, 20.0)); + let on = view == id; + if widgets::text_button(ui, r, label, on, &t, egui::Id::new(("roto-view", id))).clicked() { + let v = if on { "none" } else { id }; + let _ = app.session.execute("roto.options", json!({"view": v})); + } + app.auto.add(&format!("layerPanel.roto.view.{id}"), r, label); + x += 70.0; + } + let Some(g) = roto_group(layer) else { return }; + let uid = g.uid; + let params = effectcraft_engine::effects::flatten_params(g, &mut |p| p.value.clone()); + let frozen = fx::is_frozen(¶ms); + let running = app.session.is_roto_running(); + x += 8.0; + let r = Rect::from_min_size(pos2(x, bar.center().y - 10.0), vec2(74.0, 20.0)); + let label = if frozen { "Unfreeze" } else { "Freeze" }; + if widgets::text_button(ui, r, label, frozen, &t, egui::Id::new(("roto-freeze", uid))).clicked() { + let id = if frozen { "roto.unfreeze" } else { "roto.freeze" }; + if let Err(e) = app.session.execute(id, json!({"layer": layer.id.0, "effect": uid})) { + app.ui.status = e.to_string(); + } + } + app.auto.add("layerPanel.roto.freeze", r, label); + x += 80.0; + if running { + let r = Rect::from_min_size(pos2(x, bar.center().y - 10.0), vec2(60.0, 20.0)); + if widgets::text_button(ui, r, "Stop", false, &t, egui::Id::new("roto-stop")).clicked() { + let _ = app.session.execute("roto.cancel", json!({})); + } + app.auto.add("layerPanel.roto.stop", r, "Stop"); + if let Some(pr) = app.session.roto_progress() { + p.text(pos2(r.max.x + 8.0, bar.center().y), Align2::LEFT_CENTER, pr.banner(), Tokens::ui(11.5), t.text_dim); + } + } +} diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index d1350103..b89ddadb 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -34,6 +34,7 @@ pub enum Tool { Clone, Eraser, RotoBrush, + RefineEdge, Puppet, PuppetStarch, PuppetBend, @@ -58,10 +59,10 @@ impl Tool { &[Tool::Brush], &[Tool::Clone], &[Tool::Eraser], - &[Tool::RotoBrush], + &[Tool::RotoBrush, Tool::RefineEdge], &[Tool::Puppet, Tool::PuppetStarch, Tool::PuppetBend, Tool::PuppetAdvanced, Tool::PuppetOverlap], ]; - pub const ALL: [Tool; 29] = [ + pub const ALL: [Tool; 30] = [ Tool::Selection, Tool::Hand, Tool::Zoom, @@ -86,6 +87,7 @@ impl Tool { Tool::Clone, Tool::Eraser, Tool::RotoBrush, + Tool::RefineEdge, Tool::Puppet, Tool::PuppetStarch, Tool::PuppetBend, @@ -118,6 +120,7 @@ impl Tool { Tool::Clone => "Clone Stamp Tool", Tool::Eraser => "Eraser Tool", Tool::RotoBrush => "Roto Brush Tool", + Tool::RefineEdge => "Refine Edge Tool", Tool::Puppet => "Puppet Position Pin Tool", Tool::PuppetStarch => "Puppet Starch Pin Tool", Tool::PuppetBend => "Puppet Bend Pin Tool", @@ -139,7 +142,7 @@ impl Tool { Tool::Pen | Tool::PenAdd | Tool::PenDelete | Tool::PenConvert | Tool::MaskFeather => Some("G"), Tool::Type | Tool::TypeVertical => Some("Cmd+T"), Tool::Brush | Tool::Clone | Tool::Eraser => Some("Cmd+B"), - Tool::RotoBrush => Some("Alt+W"), + Tool::RotoBrush | Tool::RefineEdge => Some("Alt+W"), Tool::Puppet | Tool::PuppetStarch | Tool::PuppetBend | Tool::PuppetAdvanced | Tool::PuppetOverlap => Some("Cmd+P"), } } @@ -169,6 +172,7 @@ impl Tool { Tool::Clone => Icon::Clone, Tool::Eraser => Icon::Eraser, Tool::RotoBrush => Icon::RotoBrush, + Tool::RefineEdge => Icon::RefineEdge, Tool::Puppet | Tool::PuppetStarch | Tool::PuppetBend | Tool::PuppetAdvanced | Tool::PuppetOverlap => Icon::Puppet, } } @@ -187,6 +191,17 @@ impl Tool { _ => return None, }) } + /// Roto Brush / Refine Edge: the stroke kind they paint (`roto.stroke` kind), without and + /// with Alt/Option. + pub fn roto_kind(self, alt: bool) -> Option<&'static str> { + Some(match (self, alt) { + (Tool::RotoBrush, false) => "fg", + (Tool::RotoBrush, true) => "bg", + (Tool::RefineEdge, false) => "refine", + (Tool::RefineEdge, true) => "refineErase", + _ => return None, + }) + } /// Paint tools, with their stroke kind (`paint.stroke` kind). pub fn paint_kind(self) -> Option<&'static str> { Some(match self { diff --git a/docs/agents.md b/docs/agents.md index 510bc356..5c1b4187 100644 --- a/docs/agents.md +++ b/docs/agents.md @@ -146,6 +146,28 @@ effectcraft-cli exec warp.analyze '{"layer":"#1","wait":true}' shaky.ecproj --sa effectcraft-cli exec warp.status '{"layer":"#1"}' shaky.ecproj --json ``` +### Roto Brush & Refine Edge + +The Roto Brush tool (Alt+W cycles Roto Brush / Refine Edge) paints in the Layer panel; agents use +`roto.stroke {layer, kind: fg|bg|refine|refineErase, points: [[x, y], …], frame?, radius?}` +(layer pixels, layer frames). The first foreground stroke applies the Roto Brush & Refine Edge +effect and sets the base frame with a span of 20 frames each side. `roto.propagate {layer, +direction?: forward|backward|both, to?, wait?}` segments the span (in the background unless +`wait`); strokes on any other frame correct it and propagation restarts from there. +`roto.span {start?, end?}`, `roto.freeze {wait?}` / `roto.unfreeze`, `roto.clearStrokes {frame?, +kind?}`, `roto.cancel` and `roto.options {diameter?, refineDiameter?, view?: alphaBoundary|alpha| +alphaOverlay|none}` complete the set; every edit is one undo step. `roto.status {layer, frame?, +matte?, compute?, compareTo?}` reports the base frame, span, computed and stroked frames, frozen +state, job progress and, for a frame, the matte's area, centroid, RLE matte and IoU against a +reference. Matte settings are ordinary properties (`effects/#1/rotoBrushMatte/searchRadius`, +`effects/#1/refineEdgeMatte/decontaminateEdgeColors`, …). + +```sh +effectcraft-cli run clip.ecproj roto.stroke '{"layer":"#1","points":[[300,200],[360,230]],"radius":10}' \ + roto.stroke '{"layer":"#1","kind":"bg","points":[[40,40],[600,40]],"radius":12}' \ + roto.propagate '{"layer":"#1","wait":true}' roto.freeze '{"layer":"#1","wait":true}' --save +``` + ### Text: styles and editing Source Text holds character style runs and per-paragraph settings. `layer.setText` changes the diff --git a/docs/architecture.md b/docs/architecture.md index 033ed6cb..9f121608 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -176,6 +176,19 @@ auto-scale; cached per analysis and settings) and warps each frame; Synthesize E borders from neighbouring frames read with `EffectHost::self_at`. The effect lives in the effects crate, hence the `effects → track` edge. +**Roto Brush & Refine Edge** (`effectcraft_track::roto`, `effects::roto`, `engine::roto`): +strokes (foreground, background, Refine Edge) are stored as JSON in the effect's hidden Strokes +parameter with the base frame and segmentation span. Each frame is segmented by graph cut +(Boykov–Jolly hard constraints, GrabCut colour mixtures, our own Boykov–Kolmogorov max-flow, +coarse to fine) and propagated to the next frame by warping the matte with block optical flow and +re-cutting in a Search Radius band; frames with correction strokes are re-cut with the warped +matte as a soft prior. Refine Edge bands get guided-filter + closed-form matting and +foreground-colour decontamination. Segmentations are derived data: each frame's chain key (Input +Key, settings, strokes from the base frame out) indexes a process-wide cache that renders fill on +demand (`self_at`) and `roto.propagate` fills in the background; `Session::edit` keeps the Input +Key in step with the layer's source, masks and upstream effects. Freeze stores final 8-bit mattes +in the effect. + ## 5. Expressions Expressions are JavaScript, run by boa, with After Effects' object model (`thisComp`, `thisLayer`, diff --git a/docs/parity.md b/docs/parity.md index d259fec2..6e926dc7 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -73,7 +73,7 @@ library groundwork for mask tracking and Warp Stabilizer. | Remaining (audit-scale agent-hours) | ≈ 167 | ≈ 100 | | Wall clock, five parallel agents | 42–50 h | **≈ 8–10 h at today's measured pace; ≈ 25 h by the audit's conservative figures** | -What is left is concentrated in large systems: Roto Brush and Refine Edge, the 3D camera tracker, +What is left is concentrated in large systems (Roto Brush and Refine Edge landed in M6.7): the 3D camera tracker, Advanced 3D (models, PBR, image-based lighting), the Warp Stabilizer and mask-tracking UI on top of the new tracking library, a JavaScript scripting object model, SVG/PSD import, WebM, a disk cache, the missing effect categories (3D Channel, Immersive Video, OCIO), native macOS menus, and @@ -97,7 +97,7 @@ wasm threads. | Effects | 58% | 13.6 | Effect Controls widgets (angle dial, point crosshair, eyedropper, curves), GPU versions of more effects, 41 missing effects | | Interface | 55% | 10.4 | drag-to-dock and floating panels, preferences, native macOS menus (viewer rulers/snapping/channels/snapshots landed in M0.13) | | Project | 53% | 8.1 | 8/16/32-bit pipeline and colour management, auto-save, proxies, folder moves | -| Masks & roto | 52% | 10.0 | Roto Brush, variable-width mask feather points (mask tracking and Mask Interpolation landed in M6.6) | +| Masks & roto | 68% | 6.0 | variable-width mask feather points, Roto Brush's learned (3.0) segmentation model (mask tracking and Mask Interpolation landed in M6.6; Roto Brush & Refine Edge with graph-cut segmentation, flow propagation, edge matting, decontamination and Freeze in M6.7) | | Preview | 55% | 6.1 | GPU 3D and adjustment layers, disk cache (region of interest, snapshots, exposure and Fast Previews landed in M0.13) | | Tracking | ≈ 70% | 4.0 | 3D camera tracker, face tracking, Subspace Warp's mesh warp and rolling-shutter repair (point tracker, mask tracking and Warp Stabilizer landed in M6.x / M12.5) | | Paint | 0% | 4.0 | Brush, Clone Stamp, Eraser | From 406706e3d55b47f6addf342d1ddc08db12ff2b09 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:28:15 -0400 Subject: [PATCH 20/23] OUT-2: WebM export (VP9 intra encoder + alpha, Opus), WAV/AIFF audio-only outputs - effectcraft-vp9enc (L0): VP9 profile 0 key-frame encoder from the VP9 bitstream spec (8x8 intra blocks, DC/V/H/TM, 4x4 DCT/ADST, lossless WHT at quality 100, tile columns); round-trips exactly through FilmCraft's VP9 decoder and ffmpeg. - export: WebM muxer (EBML/Matroska spec) with VP9, VP9 alpha in BlockAdditional (AlphaMode 1), Opus (CodecDelay = pre-skip); WAV and AIFF outputs. - OutputFormat WebM/Wav/Aiff in the Render Queue, renderQueue.add formats, CLI --format. - Tests: FilmCraft decode-back (frames, size, pixels, audio RMS), ffprobe/ffmpeg oracles. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 6 + Cargo.toml | 5 + apps/effectcraft-cli/src/main.rs | 2 +- crates/engine/src/commands/render_queue.rs | 8 +- crates/engine/src/rq_tests.rs | 2 +- crates/export/Cargo.toml | 4 +- crates/export/src/lib.rs | 6 + crates/export/src/webm.rs | 368 +++++++++ crates/export/tests/roundtrip.rs | 92 +++ crates/export/tests/vp9_roundtrip.rs | 149 ++++ crates/host/tests/vector_import.rs | 23 + crates/media/src/convert.rs | 1 - crates/project/src/render_queue.rs | 30 +- crates/ui-egui/src/panels/render_queue.rs | 2 +- crates/vp9enc/Cargo.toml | 12 + crates/vp9enc/README.md | 15 + crates/vp9enc/src/bool.rs | 193 +++++ crates/vp9enc/src/lib.rs | 857 +++++++++++++++++++++ crates/vp9enc/src/tables.rs | 130 ++++ docs/architecture.md | 15 +- docs/parity.md | 10 +- xtask/src/main.rs | 1 + 22 files changed, 1911 insertions(+), 20 deletions(-) create mode 100644 crates/export/src/webm.rs create mode 100644 crates/export/tests/vp9_roundtrip.rs create mode 100644 crates/vp9enc/Cargo.toml create mode 100644 crates/vp9enc/README.md create mode 100644 crates/vp9enc/src/bool.rs create mode 100644 crates/vp9enc/src/lib.rs create mode 100644 crates/vp9enc/src/tables.rs diff --git a/Cargo.lock b/Cargo.lock index 866ae15f..9b28b8ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1226,11 +1226,13 @@ dependencies = [ "effectcraft-raster", "effectcraft-render", "effectcraft-time", + "effectcraft-vp9enc", "filmcraft-aac", "filmcraft-h264enc", "filmcraft-isobmff", "filmcraft-opus", "filmcraft-prores", + "filmcraft-vp9", "gif", "image", "log", @@ -1485,6 +1487,10 @@ dependencies = [ "web-time", ] +[[package]] +name = "effectcraft-vp9enc" +version = "0.1.0" + [[package]] name = "effectcraft-web" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 08a7d19d..dc2ce073 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ effectcraft-export = { path = "crates/export" } effectcraft-expr = { path = "crates/expr" } effectcraft-lottie = { path = "crates/lottie" } effectcraft-psd = { path = "crates/psd" } +effectcraft-vp9enc = { path = "crates/vp9enc" } effectcraft-svg = { path = "crates/svg" } effectcraft-engine = { path = "crates/engine" } effectcraft-host = { path = "crates/host" } @@ -71,6 +72,7 @@ filmcraft-aac = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf2 filmcraft-isobmff = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf24f698b0f0e538eb7795929f19bbfb1dbb" } # decoder used as the round-trip oracle of effectcraft-opusenc (crates/export dev-dependency) filmcraft-opus = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf24f698b0f0e538eb7795929f19bbfb1dbb" } +filmcraft-vp9 = { git = "https://github.com/storytold/filmcraft", rev = "89cfbf24f698b0f0e538eb7795929f19bbfb1dbb" } # gpu (the same wgpu eframe / egui-wgpu use) wgpu = { version = "30.0.1", features = ["fragile-send-sync-non-atomic-wasm"] } @@ -140,6 +142,9 @@ opt-level = 3 [profile.dev.package.effectcraft-export] opt-level = 3 +[profile.dev.package.effectcraft-vp9enc] +opt-level = 3 + # FilmCraft codec hot loops (same settings as FilmCraft's own workspace). [profile.dev.package.filmcraft-h264] opt-level = 3 diff --git a/apps/effectcraft-cli/src/main.rs b/apps/effectcraft-cli/src/main.rs index 90d7ef66..3a04e15c 100644 --- a/apps/effectcraft-cli/src/main.rs +++ b/apps/effectcraft-cli/src/main.rs @@ -9,7 +9,7 @@ //! effectcraft-cli get [--time S] read a property //! effectcraft-cli set [--time S] [--expression E] //! effectcraft-cli render-frame [--comp C] [--time S|--frame N] [--max-side PX|--scale K] [--out F.png] -//! effectcraft-cli render [--comp C] --out FILE [--format h264|prores|png|jpeg|tiff|exr|gif] [--start S] [--end S] +//! effectcraft-cli render [--comp C] --out FILE [--format h264|prores|webm|png|jpeg|tiff|exr|gif|wav|aiff] [--start S] [--end S] //! [--work-area] [--fps N] [--resolution full|half|third|quarter|K] [--quality best|draft] [--channels rgb|rgba] //! [--jpeg-quality N] [--bitrate KBPS] [--prores proxy|lt|standard|hq|4444|4444xq] [--audio auto|on|off] //! effectcraft-cli render F.ecproj --queue render the project's Render Queue diff --git a/crates/engine/src/commands/render_queue.rs b/crates/engine/src/commands/render_queue.rs index c905ce6c..be6cebfb 100644 --- a/crates/engine/src/commands/render_queue.rs +++ b/crates/engine/src/commands/render_queue.rs @@ -131,7 +131,7 @@ fn apply_settings(rs: &mut RenderSettings, p: &Value, cmd: &str) -> Result fn apply_output(om: &mut OutputModule, p: &Value, cmd: &str) -> Result { let mut any = false; if let Some(f) = str_p(p, "format") { - let f = OutputFormat::from_name(f).ok_or_else(|| bad(cmd, "format: h264|prores|png|jpeg|tiff|exr|gif"))?; + let f = OutputFormat::from_name(f).ok_or_else(|| bad(cmd, "format: h264|prores|webm|png|jpeg|tiff|exr|gif|wav|aiff"))?; om.set_format(f); any = true; } @@ -256,7 +256,7 @@ fn add(s: &mut Session, p: &Value) -> Result { let cid = s.comp_for_queue(p).ok_or(EngineError::NoComp)?; let mut it = RenderQueueItem::new(0, cid); if let Some(f) = str_p(p, "format") { - let f = OutputFormat::from_name(f).ok_or_else(|| bad("renderQueue.add", "format: h264|prores|png|jpeg|tiff|exr|gif"))?; + let f = OutputFormat::from_name(f).ok_or_else(|| bad("renderQueue.add", "format: h264|prores|webm|png|jpeg|tiff|exr|gif|wav|aiff"))?; it.output = OutputModule::for_format(f); } apply_settings(&mut it.settings, p, "renderQueue.add")?; @@ -434,7 +434,7 @@ fn add_output_module(s: &mut Session, p: &Value) -> Result { let n = it.extra_outputs.len() + 2; let mut om = it.output.clone(); if let Some(f) = str_p(p, "format") { - let f = OutputFormat::from_name(f).ok_or_else(|| bad("render.addOutputModule", "format: h264|prores|png|jpeg|tiff|exr|gif"))?; + let f = OutputFormat::from_name(f).ok_or_else(|| bad("render.addOutputModule", "format: h264|prores|webm|png|jpeg|tiff|exr|gif|wav|aiff"))?; om = OutputModule::for_format(f); } if str_p(p, "output").is_none() && str_p(p, "path").is_none() { @@ -509,7 +509,7 @@ pub fn specs() -> Vec { "Add to Render Queue", ["Composition"], Some("Cmd+M"), - "{comp?: id|name, format?: h264|prores|png|jpeg|tiff|exr|gif, output?: path|template, quality?: best|draft|1-100 (jpeg), resolution?: full|half|third|quarter|scale, timeSpan?: workArea|comp|custom, start?: s, end?: s, frameRate?: fps|null, motionBlur?: bool, skipExisting?: bool, channels?: rgb|rgba, bitrate?: kbps, proresProfile?: proxy|lt|standard|hq|4444|4444xq, audio?: auto|on|off, sampleRate?, loop?: bool}", + "{comp?: id|name, format?: h264|prores|webm|png|jpeg|tiff|exr|gif|wav|aiff, output?: path|template, quality?: best|draft|1-100 (jpeg, webm), resolution?: full|half|third|quarter|scale, timeSpan?: workArea|comp|custom, start?: s, end?: s, frameRate?: fps|null, motionBlur?: bool, skipExisting?: bool, channels?: rgb|rgba, bitrate?: kbps, proresProfile?: proxy|lt|standard|hq|4444|4444xq, audio?: auto|on|off, sampleRate?, loop?: bool}", can_add, add ), diff --git a/crates/engine/src/rq_tests.rs b/crates/engine/src/rq_tests.rs index c4aa08b8..f172b5e9 100644 --- a/crates/engine/src/rq_tests.rs +++ b/crates/engine/src/rq_tests.rs @@ -159,6 +159,6 @@ fn without_exporter() { s.execute("renderQueue.add", json!({})).unwrap(); assert!(!s.is_enabled("renderQueue.render")); let f = s.execute("renderQueue.formats", json!({})).unwrap(); - assert_eq!(f["formats"].as_array().unwrap().len(), 7); + assert_eq!(f["formats"].as_array().unwrap().len(), 10); assert_eq!(f["formats"][0]["available"], false); } diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 5bdd0fee..15f9b17d 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "effectcraft-export" -description = "Render Queue jobs and output modules: H.264 MP4 (+AAC) and ProRes MOV (+PCM) via FilmCraft's pure-Rust encoders, PNG/JPEG/TIFF/EXR sequences, animated GIF" +description = "Render Queue jobs and output modules: H.264 MP4 (+AAC) and ProRes MOV (+PCM) via FilmCraft's pure-Rust encoders, WebM (VP9 + Opus), PNG/JPEG/TIFF/EXR sequences, animated GIF, WAV/AIFF" version.workspace = true edition.workspace = true license.workspace = true @@ -19,6 +19,7 @@ filmcraft-prores = { workspace = true } filmcraft-aac = { workspace = true } filmcraft-isobmff = { workspace = true } effectcraft-opusenc = { workspace = true } +effectcraft-vp9enc = { workspace = true } image = { workspace = true, features = ["png", "jpeg", "tiff", "exr"] } gif = { workspace = true } rayon = { workspace = true } @@ -30,3 +31,4 @@ log = { workspace = true } effectcraft-media = { workspace = true } effectcraft-color = { workspace = true } filmcraft-opus = { workspace = true } +filmcraft-vp9 = { workspace = true } diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index dc4c35c8..a2af6a4f 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -7,6 +7,8 @@ //! | PNG / JPEG / TIFF sequence | one file per frame | 8-bit | PNG, TIFF | — | //! | OpenEXR sequence | one file per frame | 32-bit float, linear light, premultiplied | yes | — | //! | Animated GIF | GIF89a | 256-colour palette per frame (NeuQuant) | 1-bit | — | +//! | WebM | WebM (Matroska) | VP9 profile 0 intra frames (`effectcraft-vp9enc`), 8-bit 4:2:0 | VP9 alpha (BlockAdditional) | Opus (`effectcraft-opusenc`), 48 kHz stereo | +//! | WAV / AIFF | RIFF WAVE / AIFF | — | — | 16-bit PCM stereo (audio only) | //! //! Frames are rendered in parallel batches (rayon; one batch ≈ one frame per core) and handed to //! the encoder in order. Progress is reported after each batch through a callback that can cancel @@ -18,6 +20,7 @@ mod encode; mod out; +mod webm; use web_time::Instant; @@ -131,6 +134,9 @@ pub fn export(job: &Job, progress: &mut dyn FnMut(&Progress) -> bool) -> Result< let mut report = match job.output.format { OutputFormat::H264 | OutputFormat::ProRes => encode::movie(job, comp, w, h, &mut st)?, OutputFormat::Gif => gif_export(job, comp, w, h, &mut st)?, + OutputFormat::WebM => webm::webm(job, comp, w, h, &mut st)?, + OutputFormat::Wav => webm::audio_file(job, comp, false, &mut st)?, + OutputFormat::Aiff => webm::audio_file(job, comp, true, &mut st)?, f if f.is_sequence() => sequence(job, comp, w, h, &mut st)?, f => return Err(ExportError::Unsupported(f.label().into())), }; diff --git a/crates/export/src/webm.rs b/crates/export/src/webm.rs new file mode 100644 index 00000000..eb99cd22 --- /dev/null +++ b/crates/export/src/webm.rs @@ -0,0 +1,368 @@ +//! WebM output (VP9 video, optional VP9 alpha, Opus audio) and audio-only WAV / AIFF. +//! +//! The WebM container is written from the Matroska / WebM specifications (IETF RFC 8794 EBML, +//! the Matroska element registry and the WebM container guidelines): EBML header, Segment +//! (size patched at the end), Info, Tracks and one Cluster per render batch. Video frames are +//! SimpleBlocks; with alpha (Channels: RGB + Alpha) each frame is a BlockGroup whose +//! BlockAdditional (BlockAddID 1) carries a second VP9 frame coding the alpha channel as luma, +//! with `AlphaMode` 1 on the track, as WebM defines. VP9 comes from `effectcraft-vp9enc` (every +//! frame a key frame), Opus from `effectcraft-opusenc` (CELT, 48 kHz stereo, 20 ms packets, +//! `CodecDelay` = pre-skip). WAV (RIFF, 16-bit PCM) and AIFF (big-endian 16-bit, 80-bit +//! extended sample rate) follow their published file layouts. + +use std::io::{Seek, SeekFrom, Write}; + +use effectcraft_project::Comp; +use effectcraft_project::render_queue::Channels; +use effectcraft_time::{TICKS_PER_SECOND, Tick}; +use rayon::prelude::*; + +use crate::{Job, Report, Result, State, batch_size, io, render_frame, rgba8, wants_audio}; + +const OPUS_RATE: u32 = 48_000; +const OPUS_BITRATE: u32 = 192_000; + +// Element ids (with their length markers). +const EBML: u32 = 0x1A45_DFA3; +const SEGMENT: u32 = 0x1853_8067; +const INFO: u32 = 0x1549_A966; +const TRACKS: u32 = 0x1654_AE6B; +const TRACK_ENTRY: u32 = 0xAE; +const CLUSTER: u32 = 0x1F43_B675; + +fn id_bytes(id: u32) -> Vec { + let b = id.to_be_bytes(); + let skip = b.iter().position(|x| *x != 0).unwrap_or(3); + b[skip..].to_vec() +} + +/// EBML variable-size integer for an element data size (shortest form). +fn vint(n: u64) -> Vec { + for len in 1..=8u32 { + let max = (1u64 << (7 * len)) - 2; + if n <= max { + let marked = n | (1u64 << (7 * len)); + return marked.to_be_bytes()[8 - len as usize..].to_vec(); + } + } + let mut v = vec![0x01]; + v.extend_from_slice(&n.to_be_bytes()[1..]); + v +} + +fn el(out: &mut Vec, id: u32, data: &[u8]) { + out.extend(id_bytes(id)); + out.extend(vint(data.len() as u64)); + out.extend_from_slice(data); +} + +fn el_uint(out: &mut Vec, id: u32, v: u64) { + let b = v.to_be_bytes(); + let skip = b.iter().position(|x| *x != 0).unwrap_or(7); + el(out, id, &b[skip..]); +} + +fn el_float(out: &mut Vec, id: u32, v: f64) { + el(out, id, &v.to_be_bytes()); +} + +fn el_str(out: &mut Vec, id: u32, s: &str) { + el(out, id, s.as_bytes()); +} + +/// A block payload: track number, timestamp relative to the cluster, flags, frame. +fn block(track: u64, rel: i16, flags: u8, data: &[u8]) -> Vec { + let mut b = vint(track); + b.extend_from_slice(&rel.to_be_bytes()); + b.push(flags); + b.extend_from_slice(data); + b +} + +struct Frame { + ms: i64, + track: u64, + data: Vec, + alpha: Option>, +} + +fn cluster(frames: &mut [Frame]) -> Vec { + frames.sort_by_key(|f| (f.ms, f.track)); + let base = frames.first().map_or(0, |f| f.ms); + let mut body = vec![]; + el_uint(&mut body, 0xE7, base.max(0) as u64); + for f in frames.iter() { + let rel = (f.ms - base).clamp(i16::MIN as i64, i16::MAX as i64) as i16; + match &f.alpha { + None => el(&mut body, 0xA3, &block(f.track, rel, 0x80, &f.data)), + Some(a) => { + let mut g = vec![]; + el(&mut g, 0xA1, &block(f.track, rel, 0x00, &f.data)); + let mut more = vec![]; + el_uint(&mut more, 0xEE, 1); + el(&mut more, 0xA5, a); + let mut adds = vec![]; + el(&mut adds, 0xA6, &more); + el(&mut g, 0x75A1, &adds); + el(&mut body, 0xA0, &g); + } + } + } + let mut out = vec![]; + el(&mut out, CLUSTER, &body); + out +} + +pub(crate) fn webm(job: &Job, comp: &Comp, w: u32, h: u32, st: &mut State) -> Result { + let rate = job.settings.rate(comp); + let alpha = job.output.channels == Channels::Rgba; + let quality = job.output.quality.clamp(1, 100); + let cfg = effectcraft_vp9enc::EncoderConfig { width: w, height: h, quality, full_range: false }; + let with_audio = wants_audio(job); + let mut opus = with_audio.then(|| effectcraft_opusenc::OpusEncoder::new(2, OPUS_BITRATE)); + let total = st.total; + let frame_ms = |k: u64| -> i64 { ((k as i128 * 1000 * rate.den as i128 + rate.num as i128 / 2) / rate.num as i128) as i64 }; + let duration_ms = frame_ms(total) as f64; + + let mut file = crate::out::create(job.sink, job.path)?; + // EBML header. + let mut head = vec![]; + let mut eb = vec![]; + el_uint(&mut eb, 0x4286, 1); + el_uint(&mut eb, 0x42F7, 1); + el_uint(&mut eb, 0x42F2, 4); + el_uint(&mut eb, 0x42F3, 8); + el_str(&mut eb, 0x4282, "webm"); + el_uint(&mut eb, 0x4287, 4); + el_uint(&mut eb, 0x4285, 2); + el(&mut head, EBML, &eb); + // Segment with an 8-byte size patched at the end. + head.extend(id_bytes(SEGMENT)); + let size_pos = head.len() as u64; + head.extend_from_slice(&[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); + let seg_start = head.len() as u64; + let mut info = vec![]; + el_uint(&mut info, 0x2AD7B1, 1_000_000); + el_float(&mut info, 0x4489, duration_ms); + el_str(&mut info, 0x4D80, "EffectCraft"); + el_str(&mut info, 0x5741, "EffectCraft"); + el(&mut head, INFO, &info); + let mut tracks = vec![]; + let mut v = vec![]; + el_uint(&mut v, 0xD7, 1); + el_uint(&mut v, 0x73C5, 1); + el_uint(&mut v, 0x83, 1); + el_uint(&mut v, 0x9C, 0); + el_str(&mut v, 0x86, "V_VP9"); + el_uint(&mut v, 0x23E383, (1_000_000_000u128 * rate.den as u128 / rate.num.max(1) as u128) as u64); + if alpha { + el_uint(&mut v, 0x55EE, 1); + } + let mut video = vec![]; + el_uint(&mut video, 0xB0, w as u64); + el_uint(&mut video, 0xBA, h as u64); + if alpha { + el_uint(&mut video, 0x53C0, 1); + } + el(&mut v, 0xE0, &video); + el(&mut tracks, TRACK_ENTRY, &v); + if let Some(o) = &opus { + let mut a = vec![]; + el_uint(&mut a, 0xD7, 2); + el_uint(&mut a, 0x73C5, 2); + el_uint(&mut a, 0x83, 2); + el_uint(&mut a, 0x9C, 0); + el_str(&mut a, 0x86, "A_OPUS"); + el(&mut a, 0x63A2, &o.opus_head()); + el_uint(&mut a, 0x56AA, o.pre_skip() as u64 * 1_000_000_000 / OPUS_RATE as u64); + el_uint(&mut a, 0x56BB, 80_000_000); + let mut au = vec![]; + el_float(&mut au, 0xB5, OPUS_RATE as f64); + el_uint(&mut au, 0x9F, 2); + el(&mut a, 0xE1, &au); + el(&mut tracks, TRACK_ENTRY, &a); + } + el(&mut head, TRACKS, &tracks); + file.write_all(&head).map_err(io)?; + let mut seg_len = head.len() as u64 - seg_start; + + // Audio: mixed in blocks, encoded in 20 ms packets. + let (span_start, span_end) = job.settings.span(comp); + let mut cursor = span_start.to_units_floor(OPUS_RATE as i64); + let mut pcm_left: Vec = vec![]; + let mut packets = 0u64; + let mut audio_until = |until: Tick, frames: &mut Vec, finish: bool| { + let Some(enc) = opus.as_mut() else { return }; + let end = until.to_units_floor(OPUS_RATE as i64); + if end > cursor { + let n = (end - cursor) as usize; + let start = Tick(((cursor as i128 * TICKS_PER_SECOND as i128) / OPUS_RATE as i128) as i64); + pcm_left.extend(effectcraft_render::audio::mix_comp(job.project, job.footage, job.expr, job.comp, start, n, OPUS_RATE)); + cursor = end; + } + let fs = effectcraft_opusenc::OpusEncoder::FRAME_SIZE * 2; + if finish && !pcm_left.is_empty() { + // Pad the last packet (pre-skip + padding cover the encoder delay). + let pad = (fs - pcm_left.len() % fs) % fs + fs; + pcm_left.extend(std::iter::repeat_n(0.0, pad)); + } + while pcm_left.len() >= fs { + let pkt = enc.encode_float(&pcm_left[..fs]); + pcm_left.drain(..fs); + frames.push(Frame { ms: packets as i64 * 20, track: 2, data: pkt, alpha: None }); + packets += 1; + } + }; + + let batch = batch_size(); + let mut i = 0; + while i < total { + let end = (i + batch).min(total); + let encoded: Vec<(Vec, Option>)> = (i..end) + .into_par_iter() + .map(|k| { + let px = rgba8(&render_frame(job, comp, k), comp, if alpha { Channels::Rgba } else { Channels::Rgb }, w, h); + let mut enc = effectcraft_vp9enc::Vp9Encoder::new(cfg.clone()); + let (y, u, vv) = effectcraft_vp9enc::rgba_to_yuv420(&px, w, h, false); + let color = enc.encode_yuv420(&y, &u, &vv); + let a = alpha.then(|| { + let mut ea = effectcraft_vp9enc::Vp9Encoder::new(cfg.clone()); + let (y, u, vv) = effectcraft_vp9enc::alpha_to_yuv420(&px, w, h); + ea.encode_yuv420(&y, &u, &vv) + }); + (color, a) + }) + .collect(); + let mut frames: Vec = + encoded.into_iter().enumerate().map(|(j, (data, alpha))| Frame { ms: frame_ms(i + j as u64), track: 1, data, alpha }).collect(); + let t_end = if end >= total { span_end } else { job.settings.frame_time(comp, end) }; + audio_until(t_end, &mut frames, end >= total); + let c = cluster(&mut frames); + file.write_all(&c).map_err(io)?; + seg_len += c.len() as u64; + st.advance(end - i)?; + i = end; + } + if total == 0 { + let mut frames = vec![]; + audio_until(span_end, &mut frames, true); + if !frames.is_empty() { + let c = cluster(&mut frames); + file.write_all(&c).map_err(io)?; + seg_len += c.len() as u64; + } + } + // Patch the Segment size. + let mut size = [0u8; 8]; + size[0] = 0x01; + size[1..].copy_from_slice(&seg_len.to_be_bytes()[1..]); + file.seek(SeekFrom::Start(size_pos)).map_err(io)?; + file.write_all(&size).map_err(io)?; + file.seek(SeekFrom::End(0)).map_err(io)?; + let bytes = file.finish()?; + Ok(Report { path: job.path.to_string(), frames: 0, width: w, height: h, seconds: 0.0, bytes, audio: with_audio }) +} + +// ---------------------------------------------------------------- audio-only outputs + +fn ieee_extended(v: f64) -> [u8; 10] { + // 80-bit IEEE 754 extended precision (AIFF COMM sample rate). + let mut out = [0u8; 10]; + if v <= 0.0 { + return out; + } + let e = v.log2().floor() as i32; + let mant = (v / 2f64.powi(e) * (1u64 << 63) as f64) as u64; + out[..2].copy_from_slice(&((e + 16383) as u16).to_be_bytes()); + out[2..].copy_from_slice(&mant.to_be_bytes()); + out +} + +/// WAV or AIFF: the comp's audio over the render span, 16-bit stereo. +pub(crate) fn audio_file(job: &Job, comp: &Comp, aiff: bool, st: &mut State) -> Result { + let sr = job.output.audio_sample_rate.clamp(8_000, 192_000); + let (span_start, span_end) = job.settings.span(comp); + let start = span_start.to_units_floor(sr as i64); + let end = span_end.to_units_floor(sr as i64); + let n = (end - start).max(0) as usize; + let mut pcm = Vec::with_capacity(n * 4); + // Mix in one-second blocks, reporting progress in frames. + let block = sr as usize; + let mut done = 0usize; + let total_frames = st.total.max(1); + let mut reported = 0u64; + while done < n { + let k = block.min(n - done); + let t = Tick((((start + done as i64) as i128 * TICKS_PER_SECOND as i128) / sr as i128) as i64); + for s in effectcraft_render::audio::mix_comp(job.project, job.footage, job.expr, job.comp, t, k, sr) { + let v = (s.clamp(-1.0, 1.0) * 32767.0).round() as i16; + pcm.extend_from_slice(&if aiff { v.to_be_bytes() } else { v.to_le_bytes() }); + } + done += k; + let now = (done as u64 * total_frames / n.max(1) as u64).min(total_frames); + if now > reported && st.total > 0 { + st.advance(now - reported)?; + reported = now; + } + } + if reported < st.total { + st.advance(st.total - reported)?; + } + let mut out = vec![]; + let frames = (pcm.len() / 4) as u32; + if aiff { + let mut comm = vec![]; + comm.extend_from_slice(&2u16.to_be_bytes()); + comm.extend_from_slice(&frames.to_be_bytes()); + comm.extend_from_slice(&16u16.to_be_bytes()); + comm.extend_from_slice(&ieee_extended(sr as f64)); + let ssnd_len = 8 + pcm.len(); + out.extend_from_slice(b"FORM"); + out.extend_from_slice(&((4 + 8 + comm.len() + 8 + ssnd_len + ssnd_len % 2) as u32).to_be_bytes()); + out.extend_from_slice(b"AIFF"); + out.extend_from_slice(b"COMM"); + out.extend_from_slice(&(comm.len() as u32).to_be_bytes()); + out.extend_from_slice(&comm); + out.extend_from_slice(b"SSND"); + out.extend_from_slice(&(ssnd_len as u32).to_be_bytes()); + out.extend_from_slice(&[0; 8]); + out.extend_from_slice(&pcm); + if ssnd_len % 2 == 1 { + out.push(0); + } + } else { + out.extend_from_slice(b"RIFF"); + out.extend_from_slice(&((36 + pcm.len()) as u32).to_le_bytes()); + out.extend_from_slice(b"WAVEfmt "); + out.extend_from_slice(&16u32.to_le_bytes()); + out.extend_from_slice(&1u16.to_le_bytes()); + out.extend_from_slice(&2u16.to_le_bytes()); + out.extend_from_slice(&sr.to_le_bytes()); + out.extend_from_slice(&(sr * 4).to_le_bytes()); + out.extend_from_slice(&4u16.to_le_bytes()); + out.extend_from_slice(&16u16.to_le_bytes()); + out.extend_from_slice(b"data"); + out.extend_from_slice(&(pcm.len() as u32).to_le_bytes()); + out.extend_from_slice(&pcm); + } + let mut file = crate::out::create(job.sink, job.path)?; + file.write_all(&out).map_err(io)?; + let bytes = file.finish()?; + Ok(Report { path: job.path.to_string(), frames: 0, width: 0, height: 0, seconds: 0.0, bytes, audio: true }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn vints_and_ids() { + assert_eq!(vint(0), vec![0x80]); + assert_eq!(vint(126), vec![0xFE]); + assert_eq!(vint(127), vec![0x40, 0x7F]); + assert_eq!(id_bytes(0xA3), vec![0xA3]); + assert_eq!(id_bytes(EBML), vec![0x1A, 0x45, 0xDF, 0xA3]); + assert_eq!(ieee_extended(44100.0), [0x40, 0x0E, 0xAC, 0x44, 0, 0, 0, 0, 0, 0]); + assert_eq!(ieee_extended(48000.0)[..4], [0x40, 0x0E, 0xBB, 0x80]); + } +} diff --git a/crates/export/tests/roundtrip.rs b/crates/export/tests/roundtrip.rs index 186aa1a9..1ade594f 100644 --- a/crates/export/tests/roundtrip.rs +++ b/crates/export/tests/roundtrip.rs @@ -363,3 +363,95 @@ fn movies_carry_audio() { assert!(!effectcraft_media::probe(&path).expect("probe").has_audio); } } + +#[test] +fn webm_vp9_opus_roundtrip() { + let d = out_dir("webm"); + let wav = d.join("tone.wav"); + write_tone(&wav, 2.0); + let (p, cid) = project(Some(&wav)); + let pool = MediaPool::new(); + let om = OutputModule { quality: 90, audio: AudioOutput::Auto, ..OutputModule::for_format(OutputFormat::WebM) }; + let path = d.join("comp.webm"); + let r = run(&p, cid, &pool, &om, &path); + assert!(r.audio && r.bytes > 200, "{r:?}"); + // Decoded back through FilmCraft (Matroska demuxer, VP9 and Opus decoders). + let f = decode_movie(&path, FRAMES, 0.08); + assert!(f.has_audio); + let s = MediaPool::new().audio_samples(&f, Tick::from_seconds_f64(0.3), 24_000, 48_000); + let l = rms(s.iter().step_by(2).copied()); + assert!((l - 0.3536).abs() < 0.06, "left RMS {l}"); + ffmpeg_check(&path, 0.08); + if let Some(s) = ffprobe(&path) { + assert!(s.contains("vp9|64|48|yuv420p"), "{s}"); + assert!(s.contains("opus"), "{s}"); + } +} + +#[test] +fn webm_with_alpha() { + let d = out_dir("webm-alpha"); + let (p, cid) = project(None); + // Transparent outside the red solid. + let om = OutputModule { channels: Channels::Rgba, ..OutputModule::for_format(OutputFormat::WebM) }; + let path = d.join("alpha.webm"); + run(&p, cid, &NoFootage, &om, &path); + let f = effectcraft_media::probe(&path).expect("probe our output"); + assert_eq!((f.width, f.height), (W, H)); + let bytes = std::fs::read(&path).unwrap(); + // AlphaMode element (0x53C0) and BlockAdditions (0x75A1). + assert!(bytes.windows(3).any(|w| w == [0x53, 0xC0, 0x81]), "AlphaMode 1"); + assert!(bytes.windows(2).any(|w| w == [0x75, 0xA1]), "BlockAdditions"); + // ffmpeg's libvpx decoder reads the alpha channel. + let png = path.with_extension("alpha.png"); + let ok = Command::new("ffmpeg") + .args(["-hide_banner", "-loglevel", "error", "-y", "-c:v", "libvpx-vp9", "-i"]) + .arg(&path) + .args(["-vf", "select=eq(n\\,8)", "-frames:v", "1", "-pix_fmt", "rgba"]) + .arg(&png) + .status() + .map(|s| s.success()) + .unwrap_or(false); + if ok { + let img = image::open(&png).unwrap().to_rgba8(); + assert!(img.get_pixel(2, 2)[3] < 20, "corner transparent"); + assert!(img.get_pixel(W / 2, H / 2)[3] > 235, "centre opaque"); + } else { + eprintln!("ffmpeg with libvpx not available: skipping alpha oracle"); + } +} + +#[test] +fn wav_and_aiff_audio_only() { + let d = out_dir("audio-only"); + let wav = d.join("tone.wav"); + write_tone(&wav, 2.0); + let (p, cid) = project(Some(&wav)); + let pool = MediaPool::new(); + for (fmt, name, magic) in [(OutputFormat::Wav, "out.wav", &b"RIFF"[..]), (OutputFormat::Aiff, "out.aif", &b"FORM"[..])] { + let om = OutputModule::for_format(fmt); + let path = d.join(name); + let r = run(&p, cid, &pool, &om, &path); + assert!(r.audio); + let bytes = std::fs::read(&path).unwrap(); + assert_eq!(&bytes[..4], magic); + // 1.2 s of 48 kHz 16-bit stereo. + assert!((bytes.len() as i64 - 57_600 * 4).abs() < 200, "{name}: {}", bytes.len()); + let l = if fmt == OutputFormat::Wav { + // Decoded by FilmCraft. + let f = effectcraft_media::probe(&path).expect("probe audio"); + assert!(f.has_audio && !f.has_video, "{name}"); + let s = MediaPool::new().audio_samples(&f, Tick::from_seconds_f64(0.3), 24_000, 48_000); + rms(s.iter().step_by(2).copied()) + } else { + // AIFF: big-endian samples after the SSND chunk's offset/block size. + let at = bytes.windows(4).position(|w| w == b"SSND").expect("SSND") + 16; + let left = bytes[at..].chunks_exact(4).skip(14_400).take(24_000).map(|c| i16::from_be_bytes([c[0], c[1]]) as f32 / 32767.0); + rms(left) + }; + assert!((l - 0.3536).abs() < 0.01, "{name}: left RMS {l}"); + if let Some(s) = ffprobe(&path) { + assert!(s.contains("pcm_s16"), "{s}"); + } + } +} diff --git a/crates/export/tests/vp9_roundtrip.rs b/crates/export/tests/vp9_roundtrip.rs new file mode 100644 index 00000000..8156d850 --- /dev/null +++ b/crates/export/tests/vp9_roundtrip.rs @@ -0,0 +1,149 @@ +//! The VP9 intra encoder (`effectcraft-vp9enc`) round-trips through FilmCraft's VP9 decoder (and +//! ffmpeg as an external oracle when installed): sizes, lossless exactness, PSNR. + +use effectcraft_vp9enc::{EncoderConfig, Vp9Encoder}; +use filmcraft_vp9::{Decoder, Plane}; + +fn plane_u8(p: &Plane) -> Vec { + match p { + Plane::U8(v) => v.clone(), + Plane::U16(v) => v.iter().map(|x| *x as u8).collect(), + } +} + +/// Synthetic planes: gradients, noise and sharp edges. +fn source(w: usize, h: usize, kind: u32) -> (Vec, Vec, Vec) { + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let mut s = 99u32 + kind; + let mut rnd = || { + s = s.wrapping_mul(1664525).wrapping_add(1013904223); + (s >> 24) as u8 + }; + let y: Vec = (0..w * h) + .map(|i| { + let (x, yy) = (i % w, i / w); + match kind { + 0 => ((x * 255) / w.max(1)) as u8 / 2 + ((yy * 255) / h.max(1)) as u8 / 2, + 1 => rnd(), + _ => { + if (x / 7 + yy / 5) % 2 == 0 { + 30 + } else { + 220 + } + } + } + }) + .collect(); + let u: Vec = (0..cw * ch).map(|i| (64 + (i % cw) * 128 / cw.max(1)) as u8).collect(); + let v: Vec = (0..cw * ch).map(|i| (200 - (i / cw) * 100 / ch.max(1)) as u8).collect(); + (y, u, v) +} + +fn decode(frame: &[u8]) -> filmcraft_vp9::Picture { + let mut d = Decoder::new(); + let mut pics = d.decode(frame, 0).expect("decode"); + assert_eq!(pics.len(), 1); + pics.remove(0) +} + +fn crop(p: &[u8], stride: usize, w: usize, h: usize) -> Vec { + (0..h).flat_map(|y| p[y * stride..y * stride + w].to_vec()).collect() +} + +fn psnr(a: &[u8], b: &[u8]) -> f64 { + let mse: f64 = a.iter().zip(b).map(|(x, y)| (*x as f64 - *y as f64).powi(2)).sum::() / a.len() as f64; + if mse == 0.0 { 99.0 } else { 10.0 * (255.0f64 * 255.0 / mse).log10() } +} + +#[test] +fn lossless_is_exact_for_odd_sizes() { + for (w, h) in [(1, 1), (33, 17), (64, 64), (200, 120), (7, 70)] { + for kind in 0..3 { + let (y, u, v) = source(w, h, kind); + let mut e = Vp9Encoder::new(EncoderConfig { width: w as u32, height: h as u32, quality: 100, full_range: false }); + let f = e.encode_yuv420(&y, &u, &v); + let p = decode(&f); + assert_eq!((p.width, p.height), (w as u32, h as u32)); + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + assert_eq!(crop(&plane_u8(&p.y), p.y_stride, w, h), y, "{w}x{h} kind {kind}: luma"); + assert_eq!(crop(&plane_u8(&p.u), p.uv_stride, cw, ch), u, "{w}x{h} kind {kind}: u"); + assert_eq!(crop(&plane_u8(&p.v), p.uv_stride, cw, ch), v, "{w}x{h} kind {kind}: v"); + } + } +} + +#[test] +fn lossy_quality_and_size() { + let (w, h) = (200, 120); + for kind in [0, 2] { + let (y, u, v) = source(w, h, kind); + let mut last = 0usize; + for q in [30u8, 60, 80, 95] { + let mut e = Vp9Encoder::new(EncoderConfig { width: w as u32, height: h as u32, quality: q, full_range: false }); + let f = e.encode_yuv420(&y, &u, &v); + let p = decode(&f); + let got = crop(&plane_u8(&p.y), p.y_stride, w, h); + let db = psnr(&got, &y); + let min = match q { + 30 => 20.0, + 60 => 28.0, + 80 => 32.0, + _ => 38.0, + }; + assert!(db > min, "kind {kind} q{q}: PSNR {db:.1} dB, {} bytes", f.len()); + assert!(f.len() >= last, "higher quality, more bytes"); + last = f.len(); + } + } +} + +#[test] +fn wide_frames_use_tile_columns() { + // 4160 px > 64 superblocks: two tile columns are required. + let (w, h) = (4160, 16); + let (y, u, v) = source(w, h, 0); + let mut e = Vp9Encoder::new(EncoderConfig { width: w as u32, height: h as u32, quality: 90, full_range: false }); + let f = e.encode_yuv420(&y, &u, &v); + let p = decode(&f); + assert!(psnr(&crop(&plane_u8(&p.y), p.y_stride, w, h), &y) > 35.0); +} + +#[test] +fn ffmpeg_decodes_our_stream() { + let (w, h) = (96, 64); + let (y, u, v) = source(w, h, 0); + let mut e = Vp9Encoder::new(EncoderConfig { width: w as u32, height: h as u32, quality: 100, full_range: false }); + let frame = e.encode_yuv420(&y, &u, &v); + // IVF container (one frame). + let mut ivf = Vec::new(); + ivf.extend_from_slice(b"DKIF"); + ivf.extend_from_slice(&0u16.to_le_bytes()); + ivf.extend_from_slice(&32u16.to_le_bytes()); + ivf.extend_from_slice(b"VP90"); + ivf.extend_from_slice(&(w as u16).to_le_bytes()); + ivf.extend_from_slice(&(h as u16).to_le_bytes()); + ivf.extend_from_slice(&30u32.to_le_bytes()); + ivf.extend_from_slice(&1u32.to_le_bytes()); + ivf.extend_from_slice(&1u32.to_le_bytes()); + ivf.extend_from_slice(&0u32.to_le_bytes()); + ivf.extend_from_slice(&(frame.len() as u32).to_le_bytes()); + ivf.extend_from_slice(&0u64.to_le_bytes()); + ivf.extend_from_slice(&frame); + let dir = std::env::temp_dir().join(format!("effectcraft-vp9-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("a.ivf"); + std::fs::write(&path, &ivf).unwrap(); + let out = std::process::Command::new("ffmpeg") + .args(["-hide_banner", "-loglevel", "error", "-i"]) + .arg(&path) + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) + .output(); + let Ok(out) = out else { + eprintln!("ffmpeg not found: skipping oracle"); + return; + }; + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + assert_eq!(&out.stdout[..w * h], &y[..], "ffmpeg luma matches (lossless)"); + assert_eq!(&out.stdout[w * h..w * h + u.len()], &u[..]); +} diff --git a/crates/host/tests/vector_import.rs b/crates/host/tests/vector_import.rs index eb5be076..590112c9 100644 --- a/crates/host/tests/vector_import.rs +++ b/crates/host/tests/vector_import.rs @@ -161,3 +161,26 @@ fn svg_footage_and_shapes_from_vector_layer() { assert!(d3 < 0.03, "{name}: ×3 mean diff {d3}"); } } + +#[test] +fn svg_footage_continuously_rasterizes() { + let path = tmp("cr.svg"); + std::fs::write(&path, SVG).unwrap(); + let doc = effectcraft_svg::parse(SVG).unwrap(); + let (w, h) = doc.pixel_size(); + let mut s = effectcraft_host::session(); + let item = s.execute_checked("file.import", json!({"paths": [path]})).unwrap()["items"][0].as_u64().unwrap(); + s.execute("comp.new", json!({"name": "C", "width": w, "height": h, "frameRate": 30, "duration": 1})).unwrap(); + let lid = s.execute_checked("layer.addItem", json!({"item": item})).unwrap()["layer"].as_u64().unwrap(); + for (k, v) in [("transform/anchor", json!([0, 0])), ("transform/position", json!([0, 0])), ("transform/scale", json!([300, 300]))] { + s.execute("prop.set", json!({"layer": lid, "path": k, "value": v})).unwrap(); + } + let cid = s.active_comp_id().unwrap(); + let t = effectcraft_time::Tick::ZERO; + let want = effectcraft_svg::rasterize(&doc, w, h, 3.0); + let soft = mean_diff(&s.render(cid, t, RenderOpts::default()), &want); + s.execute("layer.setSwitch", json!({"layers": [lid], "switch": "collapse", "value": true})).unwrap(); + let sharp = mean_diff(&s.render(cid, t, RenderOpts::default()), &want); + assert!(sharp < 0.004, "continuously rasterised diff {sharp}"); + assert!(sharp < soft, "sharper than the upscaled pixels ({sharp} vs {soft})"); +} diff --git a/crates/media/src/convert.rs b/crates/media/src/convert.rs index dce33d8a..1edcf5fd 100644 --- a/crates/media/src/convert.rs +++ b/crates/media/src/convert.rs @@ -392,4 +392,3 @@ mod tests { } } } - diff --git a/crates/project/src/render_queue.rs b/crates/project/src/render_queue.rs index c41462b5..d1e6683c 100644 --- a/crates/project/src/render_queue.rs +++ b/crates/project/src/render_queue.rs @@ -136,17 +136,26 @@ pub enum OutputFormat { ExrSequence, /// Animated GIF. Gif, + /// WebM: VP9 video (alpha with RGB + Alpha) + Opus audio. + WebM, + /// Audio only: WAV (16-bit PCM). + Wav, + /// Audio only: AIFF (16-bit PCM). + Aiff, } impl OutputFormat { - pub const ALL: [OutputFormat; 7] = [ + pub const ALL: [OutputFormat; 10] = [ OutputFormat::H264, OutputFormat::ProRes, + OutputFormat::WebM, OutputFormat::PngSequence, OutputFormat::JpegSequence, OutputFormat::TiffSequence, OutputFormat::ExrSequence, OutputFormat::Gif, + OutputFormat::Wav, + OutputFormat::Aiff, ]; pub fn label(self) -> &'static str { @@ -158,6 +167,9 @@ impl OutputFormat { OutputFormat::TiffSequence => "TIFF Sequence", OutputFormat::ExrSequence => "OpenEXR Sequence", OutputFormat::Gif => "Animated GIF", + OutputFormat::WebM => "WebM (VP9 + Opus)", + OutputFormat::Wav => "WAV", + OutputFormat::Aiff => "AIFF", } } pub fn extension(self) -> &'static str { @@ -169,19 +181,26 @@ impl OutputFormat { OutputFormat::TiffSequence => "tif", OutputFormat::ExrSequence => "exr", OutputFormat::Gif => "gif", + OutputFormat::WebM => "webm", + OutputFormat::Wav => "wav", + OutputFormat::Aiff => "aif", } } pub fn is_sequence(self) -> bool { matches!(self, OutputFormat::PngSequence | OutputFormat::JpegSequence | OutputFormat::TiffSequence | OutputFormat::ExrSequence) } pub fn is_movie(self) -> bool { - matches!(self, OutputFormat::H264 | OutputFormat::ProRes) + matches!(self, OutputFormat::H264 | OutputFormat::ProRes | OutputFormat::WebM) + } + /// Audio-only outputs (no video is rendered). + pub fn is_audio_only(self) -> bool { + matches!(self, OutputFormat::Wav | OutputFormat::Aiff) } pub fn supports_alpha(self) -> bool { - !matches!(self, OutputFormat::H264 | OutputFormat::JpegSequence) + !matches!(self, OutputFormat::H264 | OutputFormat::JpegSequence | OutputFormat::Wav | OutputFormat::Aiff) } pub fn supports_audio(self) -> bool { - self.is_movie() + self.is_movie() || self.is_audio_only() } /// The written frame size for a rendered size: H.264 needs even dimensions (rounded down, /// at least 2). @@ -201,6 +220,9 @@ impl OutputFormat { "tif" | "tiff" | "tiffsequence" => OutputFormat::TiffSequence, "exr" | "openexr" | "openexrsequence" => OutputFormat::ExrSequence, "gif" | "animatedgif" => OutputFormat::Gif, + "webm" | "vp9" | "webmvp9opus" => OutputFormat::WebM, + "wav" | "wave" => OutputFormat::Wav, + "aif" | "aiff" | "aifc" => OutputFormat::Aiff, _ => return None, }) } diff --git a/crates/ui-egui/src/panels/render_queue.rs b/crates/ui-egui/src/panels/render_queue.rs index 154dd4c2..06403181 100644 --- a/crates/ui-egui/src/panels/render_queue.rs +++ b/crates/ui-egui/src/panels/render_queue.rs @@ -108,7 +108,7 @@ fn output_menu(it: &RenderQueueItem, available: &[OutputFormat]) -> Vec<(String, v.push((mark(o.bitrate_kbps == kbps, &format!("Bitrate: {} Mbps", kbps / 1000)), json!({"bitrate": kbps}))); } } - OutputFormat::JpegSequence => { + OutputFormat::JpegSequence | OutputFormat::WebM => { v.push(("-".into(), Value::Null)); for q in [60u8, 80, 90, 100] { v.push((mark(o.quality == q, &format!("Quality: {q}")), json!({"quality": q}))); diff --git a/crates/vp9enc/Cargo.toml b/crates/vp9enc/Cargo.toml new file mode 100644 index 00000000..62144b25 --- /dev/null +++ b/crates/vp9enc/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "effectcraft-vp9enc" +description = "VP9 intra-frame encoder (profile 0, 8-bit 4:2:0) for WebM export, from the VP9 Bitstream & Decoding Process Specification" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] diff --git a/crates/vp9enc/README.md b/crates/vp9enc/README.md new file mode 100644 index 00000000..c8f781b8 --- /dev/null +++ b/crates/vp9enc/README.md @@ -0,0 +1,15 @@ +# effectcraft-vp9enc + +A small VP9 encoder for WebM export: every frame is a key frame (profile 0, 8-bit 4:2:0, +BT.709), coded with 8×8 intra blocks (DC / V / H / TM prediction chosen per block), 4×4 +DCT/ADST transforms with default probabilities, or the Walsh–Hadamard transform at quality 100 +(mathematically lossless). + +Written from the **VP9 Bitstream & Decoding Process Specification, version 0.6 (31 March +2016)**: the encoder emits the uncompressed and compressed headers (§6.2, §6.3), partition, mode +info and token syntax (§6.4) and reconstructs with the specification's intra prediction (§8.5.1) +and integer inverse transforms (§8.7) so it stays in step with any conformant decoder. The +boolean encoder is the inverse of the §9.2 decoder in the form given in RFC 6386 §7.3. Spec +tables (§10) were transcribed from FilmCraft's first-party decoder `filmcraft-vp9` (MIT OR +Apache-2.0), which is also the decoder the tests round-trip through; ffmpeg is used only as an +external test oracle. No libvpx or other third-party encoder/decoder source was consulted. diff --git a/crates/vp9enc/src/bool.rs b/crates/vp9enc/src/bool.rs new file mode 100644 index 00000000..45db92dd --- /dev/null +++ b/crates/vp9enc/src/bool.rs @@ -0,0 +1,193 @@ +//! Boolean (arithmetic) encoder: the inverse of the VP9 boolean decoder (spec §9.2), written as +//! the classic carry-propagating range coder described in RFC 6386 §7.3 (VP8 and VP9 share the +//! bool coder). + +pub struct BoolEncoder { + out: Vec, + range: u32, + bottom: u32, + bit_count: i32, +} + +impl Default for BoolEncoder { + fn default() -> Self { + BoolEncoder::new() + } +} + +impl BoolEncoder { + pub fn new() -> Self { + BoolEncoder { out: Vec::new(), range: 255, bottom: 0, bit_count: 24 } + } + + fn add_one(&mut self) { + let mut i = self.out.len(); + while i > 0 { + i -= 1; + if self.out[i] == 255 { + self.out[i] = 0; + } else { + self.out[i] += 1; + return; + } + } + } + + /// Write `bit` with probability `prob`/256 of it being 0. + #[inline] + pub fn write(&mut self, bit: bool, prob: u8) { + let split = 1 + (((self.range - 1) * prob as u32) >> 8); + if bit { + self.bottom = self.bottom.wrapping_add(split); + self.range -= split; + } else { + self.range = split; + } + while self.range < 128 { + self.range <<= 1; + if self.bottom & (1 << 31) != 0 { + self.add_one(); + } + self.bottom <<= 1; + self.bit_count -= 1; + if self.bit_count == 0 { + self.out.push((self.bottom >> 24) as u8); + self.bottom &= (1 << 24) - 1; + self.bit_count = 8; + } + } + } + + pub fn literal(&mut self, v: u32, bits: u32) { + for i in (0..bits).rev() { + self.write((v >> i) & 1 != 0, 128); + } + } + + /// Write `value` with a tree (leaves stored negated, as in the decoder's tables). + pub fn tree(&mut self, tree: &[i8], probs: &[u8], value: u8) { + // Find the path to the leaf. + fn path(tree: &[i8], node: usize, value: u8, out: &mut Vec<(usize, bool)>) -> bool { + for b in 0..2 { + let t = tree[node + b]; + out.push((node >> 1, b == 1)); + if t <= 0 { + if (-t) as u8 == value { + return true; + } + } else if path(tree, t as usize, value, out) { + return true; + } + out.pop(); + } + false + } + let mut p = Vec::with_capacity(8); + path(tree, 0, value, &mut p); + for (i, bit) in p { + self.write(bit, probs[i]); + } + } + + /// Flush (RFC 6386 §7.3) and return the bytes. + pub fn finish(mut self) -> Vec { + let mut c = self.bit_count; + let mut v = self.bottom; + if v & (1u32 << (32 - c)) != 0 { + self.add_one(); + } + v <<= c & 7; + c >>= 3; + while { + c -= 1; + c >= 0 + } { + v <<= 8; + } + for _ in 0..4 { + self.out.push((v >> 24) as u8); + v <<= 8; + } + self.out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A straightforward decoder per VP9 spec §9.2. + struct Dec<'a> { + d: &'a [u8], + pos: usize, + value: u64, + bits: i32, + range: u32, + } + impl<'a> Dec<'a> { + fn new(d: &'a [u8]) -> Self { + let mut x = Dec { d, pos: 0, value: 0, bits: 0, range: 255 }; + x.fill(); + x + } + fn fill(&mut self) { + while self.bits <= 48 { + let b = if self.pos < self.d.len() { self.d[self.pos] } else { 0 }; + self.pos += 1; + self.value |= (b as u64) << (56 - self.bits); + self.bits += 8; + } + } + fn read(&mut self, p: u8) -> bool { + let split = 1 + (((self.range - 1) * p as u32) >> 8); + let big = (split as u64) << 56; + let bit = if self.value >= big { + self.range -= split; + self.value -= big; + true + } else { + self.range = split; + false + }; + while self.range < 128 { + self.range <<= 1; + self.value <<= 1; + self.bits -= 1; + } + self.fill(); + bit + } + } + + #[test] + fn round_trip() { + let mut e = BoolEncoder::new(); + let mut seq = vec![]; + let mut s = 12345u32; + for i in 0..20000 { + s = s.wrapping_mul(1103515245).wrapping_add(12345); + let p = ((s >> 8) % 255 + 1) as u8; + let bit = (s >> 20) % 256 >= p as u32 || i % 97 == 0; + seq.push((bit, p)); + e.write(bit, p); + } + let d = e.finish(); + let mut dec = Dec::new(&d); + for (i, (b, p)) in seq.into_iter().enumerate() { + assert_eq!(dec.read(p), b, "bit {i}"); + } + } + + #[test] + fn all_ones_carry() { + let mut e = BoolEncoder::new(); + for _ in 0..5000 { + e.write(true, 1); + } + let d = e.finish(); + let mut dec = Dec::new(&d); + for _ in 0..5000 { + assert!(dec.read(1)); + } + } +} diff --git a/crates/vp9enc/src/lib.rs b/crates/vp9enc/src/lib.rs new file mode 100644 index 00000000..02cbb0e2 --- /dev/null +++ b/crates/vp9enc/src/lib.rs @@ -0,0 +1,857 @@ +//! A VP9 intra-frame encoder (profile 0, 8-bit 4:2:0), written from the *VP9 Bitstream & +//! Decoding Process Specification* v0.6 by mirroring its decoding process in reverse (see +//! README.md). Every frame is a key frame: one tile row (tile columns as the frame width +//! requires), 64×64 superblocks split to 8×8 blocks, 4×4 transforms (the Walsh–Hadamard +//! transform at quality 100, which is lossless), intra modes DC / V / H / TM chosen per block by +//! prediction error, default probabilities (no probability updates). The encoder reconstructs +//! exactly what a decoder reconstructs (the specification's integer inverse transforms and +//! intra edge rules), so prediction never drifts. + +mod bool; +mod tables; + +use bool::BoolEncoder; +use tables::*; + +const DC_PRED: u8 = 0; +const V_PRED: u8 = 1; +const H_PRED: u8 = 2; +const TM_PRED: u8 = 9; +const MODES: [u8; 4] = [DC_PRED, V_PRED, H_PRED, TM_PRED]; + +const DCT_DCT: u8 = 0; +const ADST_DCT: u8 = 1; +const DCT_ADST: u8 = 2; +const ADST_ADST: u8 = 3; + +/// mode2txfm_map for the modes used here. +fn mode_tx_type(mode: u8) -> u8 { + match mode { + V_PRED => ADST_DCT, + H_PRED => DCT_ADST, + TM_PRED => ADST_ADST, + _ => DCT_DCT, + } +} + +const PARTITION_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; +const INTRA_MODE_TREE: [i8; 18] = [0, 2, -9, 4, -1, 6, 8, 12, -2, 10, -4, -5, -3, 14, -8, 16, -6, -7]; + +/// extra_bits[token] = (cat, numExtra, base). +const EXTRA_BITS: [(u8, u8, u16); 11] = + [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (1, 1, 5), (2, 2, 7), (3, 3, 11), (4, 4, 19), (5, 5, 35), (6, 14, 67)]; +const CAT_PROBS: [&[u8]; 7] = [ + &[], + &[159], + &[165, 145], + &[173, 148, 140], + &[176, 155, 140, 135], + &[180, 157, 141, 134, 130], + &[254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129], +]; + +/// Encoder settings. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct EncoderConfig { + pub width: u32, + pub height: u32, + /// 1..=100; 100 is lossless. + pub quality: u8, + /// Full-range (0–255) rather than limited (16–235) YUV; only signalled. + pub full_range: bool, +} + +/// Probabilities of the full token tree (9.3.2) for model probability `p`. +fn pareto(p: u8) -> [u8; 8] { + let p = p.max(1) as usize; + let x = (p - 1) / 2; + let mut t = [0u8; 8]; + for n in 0..8 { + t[n] = if p & 1 == 1 { PARETO_TABLE[x * 8 + n] } else { ((PARETO_TABLE[x * 8 + n] as u32 + PARETO_TABLE[(x + 1) * 8 + n] as u32) >> 1) as u8 }; + } + t +} + +fn scan(tx_type: u8) -> &'static [u16; 16] { + match tx_type { + ADST_DCT => &ROW_SCAN_4X4, + DCT_ADST => &COL_SCAN_4X4, + _ => &DEFAULT_SCAN_4X4, + } +} + +/// Coefficient context neighbours (9.3.2) of each scan position. +fn neighbors(tx_type: u8) -> [(usize, usize); 16] { + let sc = scan(tx_type); + let mut nb = [(0, 0); 16]; + for (c, &pos) in sc.iter().enumerate().skip(1) { + let pos = pos as usize; + let (i, j) = (pos / 4, pos % 4); + nb[c] = if i > 0 && j > 0 { + let a = (i - 1) * 4 + j; + let b = i * 4 + j - 1; + match tx_type { + DCT_ADST => (a, a), + ADST_DCT => (b, b), + _ => (a, b), + } + } else if i > 0 { + ((i - 1) * 4 + j, (i - 1) * 4 + j) + } else { + (j - 1, j - 1) + }; + } + nb +} + +// ---------------------------------------------------------------- transforms (8.7) + +fn r14(v: i32) -> i32 { + (v + (1 << 13)) >> 14 +} + +fn idct4(x: [i32; 4]) -> [i32; 4] { + let v0 = r14(x[0] * 11585 - x[2] * 11585); + let v1 = r14(x[0] * 11585 + x[2] * 11585); + let v2 = r14(x[1] * 6270 - x[3] * 15137); + let v3 = r14(x[1] * 15137 + x[3] * 6270); + [v1 + v3, v0 + v2, v0 - v2, v1 - v3] +} + +fn iadst4(x: [i32; 4]) -> [i32; 4] { + let (s19, s29, s39, s49) = (5283i64, 9929i64, 13377i64, 15212i64); + let x: [i64; 4] = x.map(|v| v as i64); + let s0 = s19 * x[0]; + let s1 = s29 * x[0]; + let s2 = s39 * x[1]; + let s3 = s49 * x[2]; + let s4 = s19 * x[2]; + let s5 = s29 * x[3]; + let s6 = s49 * x[3]; + let s7 = s39 * (x[0] - x[2] + x[3]); + let x0 = s0 + s3 + s5; + let x1 = s1 - s4 - s6; + let x2 = s7; + let x3 = s2; + let r = |v: i64| ((v + (1 << 13)) >> 14) as i32; + [r(x0 + x3), r(x1 + x3), r(x2), r(x0 + x1 - x3)] +} + +/// The decoder's 4×4 inverse transform of dequantised coefficients (row-major) to a residual. +fn inverse4(coefs: &[i32; 16], tx_type: u8) -> [i32; 16] { + let row_adst = matches!(tx_type, DCT_ADST | ADST_ADST); + let col_adst = matches!(tx_type, ADST_DCT | ADST_ADST); + let mut t = [[0i32; 4]; 4]; + for i in 0..4 { + let r = [coefs[i * 4], coefs[i * 4 + 1], coefs[i * 4 + 2], coefs[i * 4 + 3]]; + t[i] = if row_adst { iadst4(r) } else { idct4(r) }; + } + let mut out = [0i32; 16]; + for j in 0..4 { + let c = [t[0][j], t[1][j], t[2][j], t[3][j]]; + let c = if col_adst { iadst4(c) } else { idct4(c) }; + for i in 0..4 { + out[i * 4 + j] = (c[i] + 8) >> 4; + } + } + out +} + +/// Inverse Walsh–Hadamard (8.7.1.10) on one line. +fn iwht(x: [i32; 4], shift: u32) -> [i32; 4] { + let mut a = x[0] >> shift; + let mut c = x[1] >> shift; + let mut d = x[2] >> shift; + let mut b = x[3] >> shift; + a += c; + d -= b; + let e = (a - d) >> 1; + b = e - b; + c = e - c; + a -= b; + d += c; + [a, b, c, d] +} + +/// The exact inverse of [`iwht`] (with shift 0): inputs that produce outputs `y`. +fn fwht(y: [i32; 4]) -> [i32; 4] { + let (aa, bb, cc, dd) = (y[0], y[1], y[2], y[3]); + let d1 = dd - cc; + let a1 = aa + bb; + let e = (a1 - d1) >> 1; + let b0 = e - bb; + let c0 = e - cc; + let a0 = a1 - c0; + let d0 = d1 + b0; + [a0, c0, d0, b0] +} + +fn inverse_wht(coefs: &[i32; 16]) -> [i32; 16] { + let mut t = [[0i32; 4]; 4]; + for i in 0..4 { + t[i] = iwht([coefs[i * 4], coefs[i * 4 + 1], coefs[i * 4 + 2], coefs[i * 4 + 3]], 2); + } + let mut out = [0i32; 16]; + for j in 0..4 { + let r = iwht([t[0][j], t[1][j], t[2][j], t[3][j]], 0); + for i in 0..4 { + out[i * 4 + j] = r[i]; + } + } + out +} + +/// Lossless forward transform: levels (dequantised by 4) whose inverse is exactly `res`. +fn forward_wht(res: &[i32; 16]) -> [i32; 16] { + let mut t = [[0i32; 4]; 4]; + for j in 0..4 { + let col = fwht([res[j], res[4 + j], res[8 + j], res[12 + j]]); + for i in 0..4 { + t[i][j] = col[i]; + } + } + let mut lv = [0i32; 16]; + for i in 0..4 { + let r = fwht(t[i]); + lv[i * 4..i * 4 + 4].copy_from_slice(&r); + } + lv +} + +/// Forward transform matrices: the inverses of the decoder's (linear part of the) inverse +/// transforms, so `inverse4(forward(res)) ≈ res`. +fn forward_matrices() -> &'static [[[f64; 16]; 16]; 4] { + use std::sync::OnceLock; + static M: OnceLock<[[[f64; 16]; 16]; 4]> = OnceLock::new(); + M.get_or_init(|| { + let mut out = [[[0.0; 16]; 16]; 4]; + for (ty, o) in out.iter_mut().enumerate() { + // Columns of the inverse: response to a scaled unit coefficient. + let k = 1 << 16; + let mut inv = [[0.0f64; 16]; 16]; + for c in 0..16 { + let mut e = [0i32; 16]; + e[c] = k; + let r = inverse4(&e, ty as u8); + for p in 0..16 { + inv[p][c] = r[p] as f64 / k as f64; + } + } + *o = invert16(inv); + } + out + }) +} + +fn invert16(m: [[f64; 16]; 16]) -> [[f64; 16]; 16] { + let mut a = m; + let mut inv = [[0.0; 16]; 16]; + for (i, r) in inv.iter_mut().enumerate() { + r[i] = 1.0; + } + for col in 0..16 { + let piv = (col..16).max_by(|&x, &y| a[x][col].abs().total_cmp(&a[y][col].abs())).unwrap_or(col); + a.swap(col, piv); + inv.swap(col, piv); + let d = a[col][col]; + for j in 0..16 { + a[col][j] /= d; + inv[col][j] /= d; + } + for r in 0..16 { + if r != col { + let f = a[r][col]; + if f != 0.0 { + for j in 0..16 { + a[r][j] -= f * a[col][j]; + inv[r][j] -= f * inv[col][j]; + } + } + } + } + } + inv +} + +// ---------------------------------------------------------------- planes and prediction + +struct Plane { + w: usize, + /// Last valid column / row for prediction edges (the 8-aligned mode-info grid). + max_x: usize, + max_y: usize, + src: Vec, + rec: Vec, +} + +impl Plane { + fn new(data: &[u8], vw: usize, vh: usize, w: usize, h: usize, max_x: usize, max_y: usize) -> Plane { + let mut src = vec![0u8; w * h]; + for y in 0..h { + let sy = y.min(vh.saturating_sub(1)); + for x in 0..w { + let sx = x.min(vw.saturating_sub(1)); + src[y * w + x] = data.get(sy * vw + sx).copied().unwrap_or(128); + } + } + Plane { w, max_x, max_y, src, rec: vec![0; w * h] } + } +} + +/// Intra prediction (8.5.1) of a square block for DC / V / H / TM. +fn predict(p: &Plane, x: usize, y: usize, log2: usize, mode: u8, have_left: bool, have_above: bool) -> [i32; 64] { + let size = 1usize << log2; + let base = 128i32; + let buf = &p.rec; + let w = p.w; + let mut above = [0i32; 8]; + let mut left = [0i32; 8]; + if have_above { + for i in 0..size { + above[i] = buf[(y - 1) * w + (x + i).min(p.max_x)] as i32; + } + } else { + above[..size].fill(base - 1); + } + let top_left = if have_above && have_left { + buf[(y - 1) * w + (x - 1).min(p.max_x)] as i32 + } else if have_above { + base + 1 + } else { + base - 1 + }; + if have_left { + for i in 0..size { + left[i] = buf[(y + i).min(p.max_y) * w + x - 1] as i32; + } + } else { + left[..size].fill(base + 1); + } + let mut out = [0i32; 64]; + for i in 0..size { + for j in 0..size { + out[i * size + j] = match mode { + V_PRED => above[j], + H_PRED => left[i], + TM_PRED => (above[j] + left[i] - top_left).clamp(0, 255), + _ => 0, + }; + } + } + if mode == DC_PRED { + let v = match (have_left, have_above) { + (true, true) => (left[..size].iter().sum::() + above[..size].iter().sum::() + size as i32) >> (log2 + 1), + (true, false) => (left[..size].iter().sum::() + (1 << (log2 - 1))) >> log2, + (false, true) => (above[..size].iter().sum::() + (1 << (log2 - 1))) >> log2, + (false, false) => base, + }; + out[..size * size].fill(v); + } + out +} + +// ---------------------------------------------------------------- the encoder + +pub struct Vp9Encoder { + cfg: EncoderConfig, + qidx: u8, + lossless: bool, +} + +/// Per-tile coding state. +struct Tile<'a> { + enc: &'a Vp9Encoder, + bc: BoolEncoder, + planes: &'a mut [Plane; 3], + mi_cols: usize, + mi_rows: usize, + col_start: usize, + above_nz: [Vec; 3], + left_nz: [Vec; 3], + above_part: Vec, + left_part: Vec, + /// y mode of every 8×8 block (for mode contexts). + modes: &'a mut [u8], +} + +impl Vp9Encoder { + pub fn new(cfg: EncoderConfig) -> Self { + let q = cfg.quality.clamp(1, 100); + let lossless = q >= 100; + // Quality → base_q_idx: 99 ≈ 3, 90 ≈ 30, 50 ≈ 150, 1 ≈ 255. + let qidx = if lossless { 0 } else { ((100 - q as i32) * 255 / 85).clamp(1, 255) as u8 }; + Vp9Encoder { cfg, qidx, lossless } + } + + pub fn config(&self) -> &EncoderConfig { + &self.cfg + } + + /// Encode one key frame from planar 8-bit 4:2:0 (`y`: width×height, `u`/`v`: + /// ((w+1)/2)×((h+1)/2), tightly packed). + pub fn encode_yuv420(&mut self, y: &[u8], u: &[u8], v: &[u8]) -> Vec { + let (w, h) = (self.cfg.width.max(1) as usize, self.cfg.height.max(1) as usize); + let mi_cols = w.div_ceil(8); + let mi_rows = h.div_ceil(8); + let sb_cols = mi_cols.div_ceil(8); + let sb_rows = mi_rows.div_ceil(8); + let (pw, ph) = (sb_cols * 64, sb_rows * 64); + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let mut planes = [ + Plane::new(y, w, h, pw, ph, mi_cols * 8 - 1, mi_rows * 8 - 1), + Plane::new(u, cw, ch, pw / 2, ph / 2, mi_cols * 4 - 1, mi_rows * 4 - 1), + Plane::new(v, cw, ch, pw / 2, ph / 2, mi_cols * 4 - 1, mi_rows * 4 - 1), + ]; + // Tile columns: as few as the width allows (tile_info, 6.2.13). + let mut min_log2 = 0; + while (64usize << min_log2) < sb_cols { + min_log2 += 1; + } + let mut max_log2 = 1; + while (sb_cols >> max_log2) >= 4 { + max_log2 += 1; + } + max_log2 -= 1; + let tile_cols = 1usize << min_log2; + let mut modes = vec![0u8; mi_cols * mi_rows]; + let mut tiles = vec![]; + for t in 0..tile_cols { + let start = (((t * sb_cols) >> min_log2) * 8).min(mi_cols); + let end = ((((t + 1) * sb_cols) >> min_log2) * 8).min(mi_cols); + let mut tile = Tile { + enc: self, + bc: BoolEncoder::new(), + planes: &mut planes, + mi_cols, + mi_rows, + col_start: start, + above_nz: [vec![0; mi_cols * 2 + 16], vec![0; mi_cols * 2 + 16], vec![0; mi_cols * 2 + 16]], + left_nz: [vec![0; 16], vec![0; 16], vec![0; 16]], + above_part: vec![0; mi_cols + 8], + left_part: vec![0; 8], + modes: &mut modes, + }; + tile.bc.write(false, 128); + for r in (0..mi_rows).step_by(8) { + for p in 0..3 { + tile.left_nz[p].fill(0); + } + tile.left_part.fill(0); + for c in (start..end).step_by(8) { + tile.partition(r, c, 12); + } + } + tiles.push(tile.bc.finish()); + } + // Compressed header. + let mut ch_enc = BoolEncoder::new(); + ch_enc.write(false, 128); + if !self.lossless { + ch_enc.literal(0, 2); // tx_mode ONLY_4X4 + } + ch_enc.literal(0, 1); // no coefficient probability updates (TX_4X4) + for _ in 0..3 { + ch_enc.write(false, 252); // skip probabilities unchanged + } + let compressed = ch_enc.finish(); + // Uncompressed header. + let mut bw = BitWriter::default(); + bw.put(2, 2); // frame_marker + bw.put(0, 1); // profile low bit + bw.put(0, 1); // profile high bit + bw.put(0, 1); // show_existing_frame + bw.put(0, 1); // frame_type = KEY_FRAME + bw.put(1, 1); // show_frame + bw.put(0, 1); // error_resilient_mode + bw.put(0x49, 8); + bw.put(0x83, 8); + bw.put(0x42, 8); + bw.put(2, 3); // color_space CS_BT_709 + bw.put(self.cfg.full_range as u32, 1); + bw.put(w as u32 - 1, 16); + bw.put(h as u32 - 1, 16); + bw.put(0, 1); // render_and_frame_size_different + bw.put(0, 1); // refresh_frame_context + bw.put(1, 1); // frame_parallel_decoding_mode + bw.put(0, 2); // frame_context_idx + bw.put(0, 6); // loop_filter_level + bw.put(0, 3); // sharpness + bw.put(0, 1); // mode_ref_delta_enabled + bw.put(self.qidx as u32, 8); + bw.put(0, 1); // delta_q_y_dc + bw.put(0, 1); // delta_q_uv_dc + bw.put(0, 1); // delta_q_uv_ac + bw.put(0, 1); // segmentation_enabled + if min_log2 < max_log2 { + bw.put(0, 1); // no extra tile columns + } + bw.put(0, 1); // tile_rows_log2 = 0 + bw.put(compressed.len() as u32, 16); + let mut out = bw.finish(); + out.extend_from_slice(&compressed); + let n = tiles.len(); + for (i, t) in tiles.into_iter().enumerate() { + if i + 1 < n { + out.extend_from_slice(&(t.len() as u32).to_be_bytes()); + } + out.extend_from_slice(&t); + } + out + } +} + +#[derive(Default)] +struct BitWriter { + out: Vec, + acc: u32, + n: u32, +} + +impl BitWriter { + fn put(&mut self, v: u32, bits: u32) { + for i in (0..bits).rev() { + self.acc = (self.acc << 1) | ((v >> i) & 1); + self.n += 1; + if self.n == 8 { + self.out.push(self.acc as u8); + self.acc = 0; + self.n = 0; + } + } + } + fn finish(mut self) -> Vec { + if self.n > 0 { + self.out.push((self.acc << (8 - self.n)) as u8); + } + self.out + } +} + +const BLOCK_8X8: u8 = 3; +const NUM_8X8: [usize; 13] = [1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4, 8, 8]; +const MI_WIDTH_LOG2: [usize; 13] = [0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3]; + +impl Tile<'_> { + fn partition(&mut self, r: usize, c: usize, bsize: u8) { + if r >= self.mi_rows || c >= self.mi_cols { + return; + } + let num8 = NUM_8X8[bsize as usize]; + let half = num8 >> 1; + let has_rows = (r + half) < self.mi_rows; + let has_cols = (c + half) < self.mi_cols; + let bsl = MI_WIDTH_LOG2[bsize as usize]; + let boffset = 3 - bsl; + let mut above = 0u8; + let mut left = 0u8; + for i in 0..num8 { + above |= self.above_part[c + i]; + left |= self.left_part[(r & 7) + i]; + } + let above = (above >> boffset) & 1; + let left = (left >> boffset) & 1; + let ctx = bsl * 4 + left as usize * 2 + above as usize; + let probs = &KF_PARTITION_PROBS[ctx * 3..ctx * 3 + 3]; + if bsize == BLOCK_8X8 { + // PARTITION_NONE. + self.bc.write(false, probs[0]); + self.block(r, c); + self.above_part[c] = 15 >> 1; + self.left_part[r & 7] = 15 >> 1; + return; + } + // PARTITION_SPLIT. + if has_rows && has_cols { + self.bc.tree(&PARTITION_TREE, probs, 3); + } else if has_cols { + self.bc.write(true, probs[1]); + } else if has_rows { + self.bc.write(true, probs[2]); + } + let sub = bsize - 3; + self.partition(r, c, sub); + self.partition(r, c + half, sub); + self.partition(r + half, c, sub); + self.partition(r + half, c + half, sub); + } + + fn block(&mut self, r: usize, c: usize) { + let avail_u = r > 0; + let avail_l = c > self.col_start; + // Mode decision on the 8×8 luma block and the 4×4 chroma blocks. + let y_mode = self.choose(0, c * 8, r * 8, 3, avail_l, avail_u); + let uv_mode = { + let mut best = (i64::MAX, DC_PRED); + for m in MODES { + let cost: i64 = (1..3).map(|p| self.cost(p, c * 4, r * 4, 2, m, avail_l, avail_u)).sum(); + if cost < best.0 { + best = (cost, m); + } + } + best.1 + }; + // skip = 0 (context: neighbours' skip, always 0 here); tx_size implied (4×4). + self.bc.write(false, DEFAULT_SKIP_PROB[0]); + let above_mode = if avail_u { self.modes[(r - 1) * self.mi_cols + c] } else { DC_PRED }; + let left_mode = if avail_l { self.modes[r * self.mi_cols + c - 1] } else { DC_PRED }; + let o = (above_mode as usize * 10 + left_mode as usize) * 9; + self.bc.tree(&INTRA_MODE_TREE, &KF_Y_MODE_PROBS[o..o + 9], y_mode); + let o = y_mode as usize * 9; + self.bc.tree(&INTRA_MODE_TREE, &KF_UV_MODE_PROBS[o..o + 9], uv_mode); + self.modes[r * self.mi_cols + c] = y_mode; + // Residual: luma 2×2 transform blocks, then one 4×4 block per chroma plane. + for (dy, dx) in [(0, 0), (0, 1), (1, 0), (1, 1)] { + let tx = if self.enc.lossless { DCT_DCT } else { mode_tx_type(y_mode) }; + self.tx_block(0, c * 8 + dx * 4, r * 8 + dy * 4, y_mode, tx, avail_l || dx > 0, avail_u || dy > 0); + } + for p in 1..3 { + self.tx_block(p, c * 4, r * 4, uv_mode, DCT_DCT, avail_l, avail_u); + } + } + + fn cost(&self, plane: usize, x: usize, y: usize, log2: usize, mode: u8, have_left: bool, have_above: bool) -> i64 { + let p = &self.planes[plane]; + let pred = predict(p, x, y, log2, mode, have_left, have_above); + let size = 1 << log2; + let mut s = 0i64; + for i in 0..size { + for j in 0..size { + s += (p.src[(y + i) * p.w + x + j] as i32 - pred[i * size + j]).abs() as i64; + } + } + s + } + + fn choose(&self, plane: usize, x: usize, y: usize, log2: usize, have_left: bool, have_above: bool) -> u8 { + let mut best = (i64::MAX, DC_PRED); + for m in MODES { + let cost = self.cost(plane, x, y, log2, m, have_left, have_above); + if cost < best.0 { + best = (cost, m); + } + } + best.1 + } + + #[allow(clippy::too_many_arguments)] + fn tx_block(&mut self, plane: usize, x: usize, y: usize, mode: u8, tx_type: u8, have_left: bool, have_above: bool) { + let ss = (plane > 0) as usize; + let max_x = (self.mi_cols * 8) >> ss; + let max_y = (self.mi_rows * 8) >> ss; + let (x4, y4) = (x >> 2, y >> 2); + let ly = y4 & (15 >> ss); + if x >= max_x || y >= max_y { + self.above_nz[plane][x4] = 0; + self.left_nz[plane][ly] = 0; + return; + } + let p = &self.planes[plane]; + let pred = predict(p, x, y, 2, mode, have_left, have_above); + let mut res = [0i32; 16]; + for i in 0..4 { + for j in 0..4 { + res[i * 4 + j] = p.src[(y + i) * p.w + x + j] as i32 - pred[i * 4 + j]; + } + } + let (dcq, acq) = (DC_QLOOKUP[self.enc.qidx as usize], AC_QLOOKUP[self.enc.qidx as usize]); + // Quantised levels (raster order). + let levels: [i32; 16] = if self.enc.lossless { + forward_wht(&res) + } else { + let f = &forward_matrices()[tx_type as usize]; + let mut lv = [0i32; 16]; + for k in 0..16 { + let coef: f64 = (0..16).map(|q| f[k][q] * res[q] as f64).sum(); + let qs = if k == 0 { dcq } else { acq } as f64; + let a = coef.abs() / qs; + // Rounding with a small dead zone for AC coefficients. + let l = if k == 0 { (a + 0.5).floor() } else { (a + 0.38).floor() }; + lv[k] = if coef < 0.0 { -(l as i32) } else { l as i32 }; + } + lv + }; + let sc = scan(tx_type); + let eob = (0..16).rev().find(|&i| levels[sc[i] as usize] != 0).map_or(0, |i| i + 1); + // Tokens. + let ctx0 = (self.above_nz[plane][x4] + self.left_nz[plane][ly]) as usize; + self.tokens(plane, tx_type, &levels, eob, ctx0); + // Reconstruct exactly as the decoder does. + let mut deq = [0i32; 16]; + for k in 0..16 { + deq[k] = levels[k] * if k == 0 { dcq } else { acq }; + } + let resid = if eob == 0 { + [0i32; 16] + } else if self.enc.lossless { + inverse_wht(&deq) + } else { + inverse4(&deq, tx_type) + }; + let p = &mut self.planes[plane]; + for i in 0..4 { + for j in 0..4 { + p.rec[(y + i) * p.w + x + j] = (pred[i * 4 + j] + resid[i * 4 + j]).clamp(0, 255) as u8; + } + } + let nz = (eob > 0) as u8; + self.above_nz[plane][x4] = nz; + self.left_nz[plane][ly] = nz; + } + + fn tokens(&mut self, plane: usize, tx_type: u8, levels: &[i32; 16], eob: usize, ctx0: usize) { + let sc = scan(tx_type); + let nb = neighbors(tx_type); + let ptype = (plane > 0) as usize; + // coef_probs[TX_4X4][ptype][intra]: 6 bands × 6 contexts × 3. + let base = (ptype * 2) * 6 * 6 * 3; + let mut tc = [0u8; 16]; + let mut check_eob = true; + let mut c = 0; + while c < 16 { + let pos = sc[c] as usize; + let band = COEFBAND_4X4[c] as usize; + let ctx = if c == 0 { ctx0 } else { ((1 + tc[nb[c].0] + tc[nb[c].1]) >> 1) as usize }; + let o = base + (band * 6 + ctx) * 3; + let p = [DEFAULT_COEF_PROBS[o], DEFAULT_COEF_PROBS[o + 1], DEFAULT_COEF_PROBS[o + 2]]; + if check_eob { + let more = c < eob; + self.bc.write(more, p[0]); + if !more { + break; + } + } + let v = levels[pos].unsigned_abs(); + if v == 0 { + self.bc.write(false, p[1]); + tc[pos] = 0; + check_eob = false; + c += 1; + continue; + } + self.bc.write(true, p[1]); + check_eob = true; + let token = match v { + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5..=6 => 5, + 7..=10 => 6, + 11..=18 => 7, + 19..=34 => 8, + 35..=66 => 9, + _ => 10, + }; + if token == 1 { + self.bc.write(false, p[2]); + } else { + self.bc.write(true, p[2]); + let pp = pareto(p[2]); + let path: &[(usize, bool)] = match token { + 2 => &[(0, false), (1, false)], + 3 => &[(0, false), (1, true), (2, false)], + 4 => &[(0, false), (1, true), (2, true)], + 5 => &[(0, true), (3, false), (4, false)], + 6 => &[(0, true), (3, false), (4, true)], + 7 => &[(0, true), (3, true), (5, false), (6, false)], + 8 => &[(0, true), (3, true), (5, false), (6, true)], + 9 => &[(0, true), (3, true), (5, true), (7, false)], + _ => &[(0, true), (3, true), (5, true), (7, true)], + }; + for &(n, b) in path { + self.bc.write(b, pp[n]); + } + let (cat, num_extra, base_v) = EXTRA_BITS[token]; + let extra = (v - base_v as u32).min((1 << num_extra) - 1); + let cp = CAT_PROBS[cat as usize]; + for e in 0..num_extra as usize { + self.bc.write((extra >> (num_extra as usize - 1 - e)) & 1 != 0, cp[e]); + } + } + tc[pos] = ENERGY_CLASS[token]; + self.bc.write(levels[pos] < 0, 128); + c += 1; + } + } +} + +// ---------------------------------------------------------------- colour conversion + +/// BT.709 RGB(A) 8-bit → planar YUV 4:2:0 (limited or full range). +pub fn rgba_to_yuv420(rgba: &[u8], w: u32, h: u32, full_range: bool) -> (Vec, Vec, Vec) { + let (w, h) = (w as usize, h as usize); + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let mut y = vec![0u8; w * h]; + let mut u = vec![0u8; cw * ch]; + let mut v = vec![0u8; cw * ch]; + let (ys, yo, cs) = if full_range { (255.0, 0.0, 255.0) } else { (219.0, 16.0, 224.0) }; + let px = |x: usize, yy: usize| -> [f32; 3] { + let i = (yy.min(h - 1) * w + x.min(w - 1)) * 4; + [rgba[i] as f32 / 255.0, rgba[i + 1] as f32 / 255.0, rgba[i + 2] as f32 / 255.0] + }; + let luma = |c: [f32; 3]| 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; + for yy in 0..h { + for x in 0..w { + y[yy * w + x] = (luma(px(x, yy)) * ys + yo).round().clamp(0.0, 255.0) as u8; + } + } + for cy in 0..ch { + for cx in 0..cw { + let mut acc = [0.0f32; 3]; + for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { + let c = px(cx * 2 + dx, cy * 2 + dy); + for k in 0..3 { + acc[k] += c[k] / 4.0; + } + } + let l = luma(acc); + let cb = (acc[2] - l) / 1.8556; + let cr = (acc[0] - l) / 1.5748; + u[cy * cw + cx] = (cb * cs + 128.0).round().clamp(0.0, 255.0) as u8; + v[cy * cw + cx] = (cr * cs + 128.0).round().clamp(0.0, 255.0) as u8; + } + } + (y, u, v) +} + +/// The alpha channel as a luma plane (full range) with neutral chroma — the second VP9 stream of +/// WebM alpha. +pub fn alpha_to_yuv420(rgba: &[u8], w: u32, h: u32) -> (Vec, Vec, Vec) { + let (w, h) = (w as usize, h as usize); + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let y: Vec = rgba.chunks_exact(4).take(w * h).map(|p| p[3]).collect(); + (y, vec![128; cw * ch], vec![128; cw * ch]) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn wht_is_exactly_invertible() { + let mut s = 7u32; + for _ in 0..2000 { + let mut r = [0i32; 16]; + for v in &mut r { + s = s.wrapping_mul(1664525).wrapping_add(1013904223); + *v = (s >> 16) as i32 % 511 - 255; + } + let lv = forward_wht(&r); + let deq = lv.map(|l| l * 4); + assert_eq!(inverse_wht(&deq), r); + } + } + + #[test] + fn forward_transforms_invert_the_decoder() { + for ty in 0..4u8 { + let f = &forward_matrices()[ty as usize]; + let r: [i32; 16] = std::array::from_fn(|i| (i as i32 * 37 % 61) - 30); + let c: [i32; 16] = std::array::from_fn(|k| (0..16).map(|q| f[k][q] * r[q] as f64).sum::().round() as i32); + let back = inverse4(&c, ty); + for i in 0..16 { + assert!((back[i] - r[i]).abs() <= 1, "type {ty}: {back:?} vs {r:?}"); + } + } + } +} diff --git a/crates/vp9enc/src/tables.rs b/crates/vp9enc/src/tables.rs new file mode 100644 index 00000000..dc8027a6 --- /dev/null +++ b/crates/vp9enc/src/tables.rs @@ -0,0 +1,130 @@ +//! Tables from the VP9 Bitstream & Decoding Process Specification v0.6 (section 10), as +//! transcribed in FilmCraft's first-party decoder `filmcraft-vp9` (MIT OR Apache-2.0, the same +//! licence as this crate). + +#![allow(clippy::unreadable_literal)] + +pub const DC_QLOOKUP: [i32; 256] = [ + 4, 8, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 38, 39, 40, + 41, 42, 43, 43, 44, 45, 46, 47, 48, 48, 49, 50, 51, 52, 53, 53, 54, 55, 56, 57, 57, 58, 59, 60, 61, 62, 62, 63, 64, 65, 66, 66, 67, 68, 69, 70, 70, 71, 72, + 73, 74, 74, 75, 76, 77, 78, 78, 79, 80, 81, 81, 82, 83, 84, 85, 85, 87, 88, 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 108, 110, 111, 113, 114, + 116, 117, 118, 120, 121, 123, 125, 127, 129, 131, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 161, 164, 166, 169, 172, 174, 177, 180, + 182, 185, 187, 190, 192, 195, 199, 202, 205, 208, 211, 214, 217, 220, 223, 226, 230, 233, 237, 240, 243, 247, 250, 253, 257, 261, 265, 269, 272, 276, 280, + 284, 288, 292, 296, 300, 304, 309, 313, 317, 322, 326, 330, 335, 340, 344, 349, 354, 359, 364, 369, 374, 379, 384, 389, 395, 400, 406, 411, 417, 423, 429, + 435, 441, 447, 454, 461, 467, 475, 482, 489, 497, 505, 513, 522, 530, 539, 549, 559, 569, 579, 590, 602, 614, 626, 640, 654, 668, 684, 700, 717, 736, 755, + 775, 796, 819, 843, 869, 896, 925, 955, 988, 1022, 1058, 1098, 1139, 1184, 1232, 1282, 1336, +]; + +pub const AC_QLOOKUP: [i32; 256] = [ + 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, + 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, + 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, + 136, 138, 140, 142, 144, 146, 148, 150, 152, 155, 158, 161, 164, 167, 170, 173, 176, 179, 182, 185, 188, 191, 194, 197, 200, 203, 207, 211, 215, 219, 223, + 227, 231, 235, 239, 243, 247, 251, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 311, 317, 323, 329, 335, 341, 347, 353, 359, 366, 373, 380, 387, + 394, 401, 408, 416, 424, 432, 440, 448, 456, 465, 474, 483, 492, 501, 510, 520, 530, 540, 550, 560, 571, 582, 593, 604, 615, 627, 639, 651, 663, 676, 689, + 702, 715, 729, 743, 757, 771, 786, 801, 816, 832, 848, 864, 881, 898, 915, 933, 951, 969, 988, 1007, 1026, 1046, 1066, 1087, 1108, 1129, 1151, 1173, 1196, + 1219, 1243, 1267, 1292, 1317, 1343, 1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567, 1597, 1628, 1660, 1692, 1725, 1759, 1793, 1828, +]; + +pub const DEFAULT_SCAN_4X4: [u16; 16] = [0, 4, 1, 5, 8, 2, 12, 9, 3, 6, 13, 10, 7, 14, 11, 15]; + +pub const COL_SCAN_4X4: [u16; 16] = [0, 4, 8, 1, 12, 5, 9, 2, 13, 6, 10, 3, 7, 14, 11, 15]; + +pub const ROW_SCAN_4X4: [u16; 16] = [0, 1, 4, 2, 5, 3, 6, 8, 9, 7, 12, 10, 13, 11, 14, 15]; + +pub const COEFBAND_4X4: [u8; 16] = [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5]; + +pub const ENERGY_CLASS: [u8; 12] = [0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5]; + +pub const PARETO_TABLE: [u8; 1024] = [ + 3, 86, 128, 6, 86, 23, 88, 29, 9, 86, 129, 17, 88, 61, 94, 76, 15, 87, 129, 28, 89, 93, 100, 110, 20, 88, 130, 38, 91, 118, 106, 136, 26, 89, 131, 48, 92, + 139, 111, 156, 31, 90, 131, 58, 94, 156, 117, 171, 37, 90, 132, 66, 95, 171, 122, 184, 42, 91, 132, 75, 97, 183, 127, 194, 47, 92, 133, 83, 98, 193, 132, + 202, 52, 93, 133, 90, 100, 201, 137, 208, 57, 94, 134, 98, 101, 208, 142, 214, 62, 94, 135, 105, 103, 214, 146, 218, 66, 95, 135, 111, 104, 219, 151, 222, + 71, 96, 136, 117, 106, 224, 155, 225, 76, 97, 136, 123, 107, 227, 159, 228, 80, 98, 137, 129, 109, 231, 162, 231, 84, 98, 138, 134, 110, 234, 166, 233, 89, + 99, 138, 140, 112, 236, 170, 235, 93, 100, 139, 145, 113, 238, 173, 236, 97, 101, 140, 149, 115, 240, 176, 238, 101, 102, 140, 154, 116, 242, 179, 239, + 105, 103, 141, 158, 118, 243, 182, 240, 109, 104, 141, 162, 119, 244, 185, 241, 113, 104, 142, 166, 120, 245, 187, 242, 116, 105, 143, 170, 122, 246, 190, + 243, 120, 106, 143, 173, 123, 247, 192, 244, 123, 107, 144, 177, 125, 248, 195, 244, 127, 108, 145, 180, 126, 249, 197, 245, 130, 109, 145, 183, 128, 249, + 199, 245, 134, 110, 146, 186, 129, 250, 201, 246, 137, 111, 147, 189, 131, 251, 203, 246, 140, 112, 147, 192, 132, 251, 205, 247, 143, 113, 148, 194, 133, + 251, 207, 247, 146, 114, 149, 197, 135, 252, 208, 248, 149, 115, 149, 199, 136, 252, 210, 248, 152, 115, 150, 201, 138, 252, 211, 248, 155, 116, 151, 204, + 139, 253, 213, 249, 158, 117, 151, 206, 140, 253, 214, 249, 161, 118, 152, 208, 142, 253, 216, 249, 163, 119, 153, 210, 143, 253, 217, 249, 166, 120, 153, + 212, 144, 254, 218, 250, 168, 121, 154, 213, 146, 254, 220, 250, 171, 122, 155, 215, 147, 254, 221, 250, 173, 123, 155, 217, 148, 254, 222, 250, 176, 124, + 156, 218, 150, 254, 223, 250, 178, 125, 157, 220, 151, 254, 224, 251, 180, 126, 157, 221, 152, 254, 225, 251, 183, 127, 158, 222, 153, 254, 226, 251, 185, + 128, 159, 224, 155, 255, 227, 251, 187, 129, 160, 225, 156, 255, 228, 251, 189, 131, 160, 226, 157, 255, 228, 251, 191, 132, 161, 227, 159, 255, 229, 251, + 193, 133, 162, 228, 160, 255, 230, 252, 195, 134, 163, 230, 161, 255, 231, 252, 197, 135, 163, 231, 162, 255, 231, 252, 199, 136, 164, 232, 163, 255, 232, + 252, 201, 137, 165, 233, 165, 255, 233, 252, 202, 138, 166, 233, 166, 255, 233, 252, 204, 139, 166, 234, 167, 255, 234, 252, 206, 140, 167, 235, 168, 255, + 235, 252, 207, 141, 168, 236, 169, 255, 235, 252, 209, 142, 169, 237, 171, 255, 236, 252, 210, 144, 169, 237, 172, 255, 236, 252, 212, 145, 170, 238, 173, + 255, 237, 252, 214, 146, 171, 239, 174, 255, 237, 253, 215, 147, 172, 240, 175, 255, 238, 253, 216, 148, 173, 240, 176, 255, 238, 253, 218, 149, 173, 241, + 177, 255, 239, 253, 219, 150, 174, 241, 179, 255, 239, 253, 220, 152, 175, 242, 180, 255, 240, 253, 222, 153, 176, 242, 181, 255, 240, 253, 223, 154, 177, + 243, 182, 255, 240, 253, 224, 155, 178, 244, 183, 255, 241, 253, 225, 156, 178, 244, 184, 255, 241, 253, 226, 158, 179, 244, 185, 255, 242, 253, 228, 159, + 180, 245, 186, 255, 242, 253, 229, 160, 181, 245, 187, 255, 242, 253, 230, 161, 182, 246, 188, 255, 243, 253, 231, 163, 183, 246, 189, 255, 243, 253, 232, + 164, 184, 247, 190, 255, 243, 253, 233, 165, 185, 247, 191, 255, 244, 253, 234, 166, 185, 247, 192, 255, 244, 253, 235, 168, 186, 248, 193, 255, 244, 253, + 236, 169, 187, 248, 194, 255, 244, 253, 236, 170, 188, 248, 195, 255, 245, 253, 237, 171, 189, 249, 196, 255, 245, 254, 238, 173, 190, 249, 197, 255, 245, + 254, 239, 174, 191, 249, 198, 255, 245, 254, 240, 175, 192, 249, 199, 255, 246, 254, 240, 177, 193, 250, 200, 255, 246, 254, 241, 178, 194, 250, 201, 255, + 246, 254, 242, 179, 195, 250, 202, 255, 246, 254, 242, 181, 196, 250, 203, 255, 247, 254, 243, 182, 197, 251, 204, 255, 247, 254, 244, 184, 198, 251, 205, + 255, 247, 254, 244, 185, 199, 251, 206, 255, 247, 254, 245, 186, 200, 251, 207, 255, 247, 254, 246, 188, 201, 252, 207, 255, 248, 254, 246, 189, 202, 252, + 208, 255, 248, 254, 247, 191, 203, 252, 209, 255, 248, 254, 247, 192, 204, 252, 210, 255, 248, 254, 248, 194, 205, 252, 211, 255, 248, 254, 248, 195, 206, + 252, 212, 255, 249, 254, 249, 197, 207, 253, 213, 255, 249, 254, 249, 198, 208, 253, 214, 255, 249, 254, 250, 200, 210, 253, 215, 255, 249, 254, 250, 201, + 211, 253, 215, 255, 249, 254, 250, 203, 212, 253, 216, 255, 249, 254, 251, 204, 213, 253, 217, 255, 250, 254, 251, 206, 214, 254, 218, 255, 250, 254, 252, + 207, 216, 254, 219, 255, 250, 254, 252, 209, 217, 254, 220, 255, 250, 254, 252, 211, 218, 254, 221, 255, 250, 254, 253, 213, 219, 254, 222, 255, 250, 254, + 253, 214, 221, 254, 223, 255, 250, 254, 253, 216, 222, 254, 224, 255, 251, 254, 253, 218, 224, 254, 225, 255, 251, 254, 254, 220, 225, 254, 225, 255, 251, + 254, 254, 222, 227, 255, 226, 255, 251, 254, 254, 224, 228, 255, 227, 255, 251, 254, 254, 226, 230, 255, 228, 255, 251, 254, 255, 228, 231, 255, 230, 255, + 251, 254, 255, 230, 233, 255, 231, 255, 252, 254, 255, 232, 235, 255, 232, 255, 252, 254, 255, 235, 237, 255, 233, 255, 252, 254, 255, 238, 240, 255, 235, + 255, 252, 255, 255, 241, 243, 255, 236, 255, 252, 254, 255, 246, 247, 255, 239, 255, 253, 255, +]; + +pub const KF_PARTITION_PROBS: [u8; 48] = [ + 158, 97, 94, 93, 24, 99, 85, 119, 44, 62, 59, 67, 149, 53, 53, 94, 20, 48, 83, 53, 24, 52, 18, 18, 150, 40, 39, 78, 12, 26, 67, 33, 11, 24, 7, 5, 174, 35, + 49, 68, 11, 27, 57, 15, 9, 12, 3, 3, +]; + +pub const KF_Y_MODE_PROBS: [u8; 900] = [ + 137, 30, 42, 148, 151, 207, 70, 52, 91, 92, 45, 102, 136, 116, 180, 74, 90, 100, 73, 32, 19, 187, 222, 215, 46, 34, 100, 91, 30, 32, 116, 121, 186, 93, 86, + 94, 72, 35, 36, 149, 68, 206, 68, 63, 105, 73, 31, 28, 138, 57, 124, 55, 122, 151, 67, 23, 21, 140, 126, 197, 40, 37, 171, 86, 27, 28, 128, 154, 212, 45, + 43, 53, 74, 32, 27, 107, 86, 160, 63, 134, 102, 59, 67, 44, 140, 161, 202, 78, 67, 119, 63, 36, 126, 146, 123, 158, 60, 90, 96, 43, 46, 168, 134, 107, 128, + 69, 142, 92, 44, 29, 68, 159, 201, 177, 50, 57, 77, 58, 38, 76, 114, 97, 172, 78, 133, 92, 46, 41, 76, 140, 63, 184, 69, 112, 57, 38, 32, 85, 140, 46, 112, + 54, 151, 133, 39, 27, 61, 131, 110, 175, 44, 75, 136, 52, 30, 74, 113, 130, 175, 51, 64, 58, 47, 35, 80, 100, 74, 143, 64, 163, 74, 36, 61, 116, 114, 128, + 162, 80, 125, 82, 82, 26, 26, 171, 208, 204, 44, 32, 105, 55, 44, 68, 166, 179, 192, 57, 57, 108, 42, 26, 11, 199, 241, 228, 23, 15, 85, 68, 42, 19, 131, + 160, 199, 55, 52, 83, 58, 50, 25, 139, 115, 232, 39, 52, 118, 50, 35, 33, 153, 104, 162, 64, 59, 131, 44, 24, 16, 150, 177, 202, 33, 19, 156, 55, 27, 12, + 153, 203, 218, 26, 27, 49, 53, 49, 21, 110, 116, 168, 59, 80, 76, 38, 72, 19, 168, 203, 212, 50, 50, 107, 103, 26, 36, 129, 132, 201, 83, 80, 93, 59, 38, + 83, 112, 103, 162, 98, 136, 90, 62, 30, 23, 158, 200, 207, 59, 57, 50, 67, 30, 29, 84, 86, 191, 102, 91, 59, 60, 32, 33, 112, 71, 220, 64, 89, 104, 53, 26, + 34, 130, 56, 149, 84, 120, 103, 53, 21, 23, 133, 109, 210, 56, 77, 172, 77, 19, 29, 112, 142, 228, 55, 66, 36, 61, 29, 29, 93, 97, 165, 83, 175, 162, 47, + 47, 43, 114, 137, 181, 100, 99, 95, 69, 23, 29, 128, 83, 199, 46, 44, 101, 53, 40, 55, 139, 69, 183, 61, 80, 110, 40, 29, 19, 161, 180, 207, 43, 24, 91, + 60, 34, 19, 105, 61, 198, 53, 64, 89, 52, 31, 22, 158, 40, 209, 58, 62, 89, 44, 31, 29, 147, 46, 158, 56, 102, 198, 35, 19, 12, 135, 87, 209, 41, 45, 167, + 55, 25, 21, 118, 95, 215, 38, 39, 66, 51, 38, 25, 113, 58, 164, 70, 93, 97, 47, 54, 34, 146, 108, 203, 72, 103, 151, 64, 19, 37, 156, 66, 138, 49, 95, 133, + 46, 27, 80, 150, 55, 124, 55, 121, 135, 36, 23, 27, 165, 149, 166, 54, 64, 118, 53, 21, 36, 131, 63, 163, 60, 109, 81, 40, 26, 35, 154, 40, 185, 51, 97, + 123, 35, 19, 34, 179, 19, 97, 48, 129, 124, 36, 20, 26, 136, 62, 164, 33, 77, 154, 45, 18, 32, 130, 90, 157, 40, 79, 91, 45, 26, 28, 129, 45, 129, 49, 147, + 123, 38, 44, 51, 136, 74, 162, 57, 97, 121, 75, 17, 22, 136, 138, 185, 32, 34, 166, 56, 39, 58, 133, 117, 173, 48, 53, 187, 35, 21, 12, 161, 212, 207, 20, + 23, 145, 56, 29, 19, 117, 109, 181, 55, 68, 112, 47, 29, 17, 153, 64, 220, 59, 51, 114, 46, 16, 24, 136, 76, 147, 41, 64, 172, 34, 17, 11, 108, 152, 187, + 13, 15, 209, 51, 24, 14, 115, 133, 209, 32, 26, 104, 55, 30, 18, 122, 79, 179, 44, 88, 116, 37, 49, 25, 129, 168, 164, 41, 54, 148, 82, 22, 32, 127, 143, + 213, 39, 41, 70, 62, 44, 61, 123, 105, 189, 48, 57, 64, 47, 25, 17, 175, 222, 220, 24, 30, 86, 68, 36, 17, 106, 102, 206, 59, 74, 74, 57, 39, 23, 151, 68, + 216, 55, 63, 58, 49, 30, 35, 141, 70, 168, 82, 40, 115, 51, 25, 15, 136, 129, 202, 38, 35, 139, 68, 26, 16, 111, 141, 215, 29, 28, 28, 59, 39, 19, 114, 75, + 180, 77, 104, 42, 40, 61, 26, 126, 152, 206, 61, 59, 93, 78, 23, 39, 111, 117, 170, 74, 124, 94, 48, 34, 86, 101, 92, 146, 78, 179, 134, 47, 22, 24, 138, + 187, 178, 68, 69, 59, 56, 25, 33, 105, 112, 187, 95, 177, 129, 48, 31, 27, 114, 63, 183, 82, 116, 56, 43, 28, 37, 121, 63, 123, 61, 192, 169, 42, 17, 24, + 109, 97, 177, 56, 76, 122, 58, 18, 28, 105, 139, 182, 70, 92, 63, 46, 23, 32, 74, 86, 150, 67, 183, 88, 36, 38, 48, 92, 122, 165, 88, 137, 91, 65, 70, 60, + 155, 159, 199, 61, 60, 81, 44, 78, 115, 132, 119, 173, 71, 112, 93, 39, 38, 21, 184, 227, 206, 42, 32, 64, 58, 47, 36, 124, 137, 193, 80, 82, 78, 49, 50, + 35, 144, 95, 205, 63, 78, 59, 41, 53, 52, 148, 71, 142, 65, 128, 51, 40, 36, 28, 143, 143, 202, 40, 55, 137, 52, 34, 29, 129, 183, 227, 42, 35, 43, 42, 44, + 44, 104, 105, 164, 64, 130, 80, 43, 81, 53, 140, 169, 204, 68, 84, 72, +]; + +pub const KF_UV_MODE_PROBS: [u8; 90] = [ + 144, 11, 54, 157, 195, 130, 46, 58, 108, 118, 15, 123, 148, 131, 101, 44, 93, 131, 113, 12, 23, 188, 226, 142, 26, 32, 125, 120, 11, 50, 123, 163, 135, 64, + 77, 103, 113, 9, 36, 155, 111, 157, 32, 44, 161, 116, 9, 55, 176, 76, 96, 37, 61, 149, 115, 9, 28, 141, 161, 167, 21, 25, 193, 120, 12, 32, 145, 195, 142, + 32, 38, 86, 116, 12, 64, 120, 140, 125, 49, 115, 121, 102, 19, 66, 162, 182, 122, 35, 59, 128, +]; + +pub const DEFAULT_SKIP_PROB: [u8; 3] = [192, 128, 64]; + +pub const DEFAULT_COEF_PROBS: [u8; 432] = [ + 195, 29, 183, 84, 49, 136, 8, 42, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 107, 169, 35, 99, 159, 17, 82, 140, 8, 66, 114, 2, 44, 76, 1, 19, 32, 40, 132, 201, + 29, 114, 187, 13, 91, 157, 7, 75, 127, 3, 58, 95, 1, 28, 47, 69, 142, 221, 42, 122, 201, 15, 91, 159, 6, 67, 121, 1, 42, 77, 1, 17, 31, 102, 148, 228, 67, + 117, 204, 17, 82, 154, 6, 59, 114, 2, 39, 75, 1, 15, 29, 156, 57, 233, 119, 57, 212, 58, 48, 163, 29, 40, 124, 12, 30, 81, 3, 12, 31, 191, 107, 226, 124, + 117, 204, 25, 99, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 148, 210, 37, 126, 194, 8, 93, 157, 2, 68, 118, 1, 39, 69, 1, 17, 33, 41, 151, 213, 27, 123, 193, 3, + 82, 144, 1, 58, 105, 1, 32, 60, 1, 13, 26, 59, 159, 220, 23, 126, 198, 4, 88, 151, 1, 66, 114, 1, 38, 71, 1, 18, 34, 114, 136, 232, 51, 114, 207, 11, 83, + 155, 3, 56, 105, 1, 33, 65, 1, 17, 34, 149, 65, 234, 121, 57, 215, 61, 49, 166, 28, 36, 114, 12, 25, 76, 3, 16, 42, 214, 49, 220, 132, 63, 188, 42, 65, + 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 137, 221, 104, 131, 216, 49, 111, 192, 21, 87, 155, 2, 49, 87, 1, 16, 28, 89, 163, 230, 90, 137, 220, 29, 100, 183, 10, + 70, 135, 2, 42, 81, 1, 17, 33, 108, 167, 237, 55, 133, 222, 15, 97, 179, 4, 72, 135, 1, 45, 85, 1, 19, 38, 124, 146, 240, 66, 124, 224, 17, 88, 175, 4, 58, + 122, 1, 36, 75, 1, 18, 37, 141, 79, 241, 126, 70, 227, 66, 58, 182, 30, 44, 136, 12, 34, 96, 2, 20, 47, 229, 99, 249, 143, 111, 235, 46, 109, 192, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 82, 158, 236, 94, 146, 224, 25, 117, 191, 9, 87, 149, 3, 56, 99, 1, 33, 57, 83, 167, 237, 68, 145, 222, 10, 103, 177, 2, 72, 131, 1, 41, + 79, 1, 20, 39, 99, 167, 239, 47, 141, 224, 10, 104, 178, 2, 73, 133, 1, 44, 85, 1, 22, 47, 127, 145, 243, 71, 129, 228, 17, 93, 177, 3, 61, 124, 1, 41, 84, + 1, 21, 52, 157, 78, 244, 140, 72, 231, 69, 58, 184, 31, 44, 137, 14, 38, 105, 8, 23, 61, +]; diff --git a/docs/architecture.md b/docs/architecture.md index c3d869c0..096c75bd 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -16,18 +16,21 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for | L0 | `time` | `Tick` (254 016 000 000 per second), rational frame rates incl. NTSC, SMPTE and drop-frame timecode | | L0 | `geom` | Vectors, matrices, quaternions, the layer transform (anchor, position, scale, orientation, rotation) | | L0 | `color` | sRGB and linear, HSL/HSV, luminance, the 38 blend modes, label colors | +| L0 | `vp9enc` | VP9 intra-frame encoder (profile 0, 8-bit 4:2:0; lossless at quality 100) for WebM export, from the VP9 bitstream specification | | L0 | `opusenc` | Opus (CELT) encoder: RFC 6716 CELT-only fullband 48 kHz 20 ms packets, mono/stereo, and the RFC 7845 `OpusHead`, for WebM export audio | | L1 | `raster` | Premultiplied float images, sampling, affine and projective warps, blurs, compositing (parallel with rayon) | | L1 | `keyframe` | Animated values, keyframes with temporal ease and spatial Bezier, roving, hold, velocity | +| L1 | `psd` | Photoshop PSD/PSB reader (layers, groups, masks, blend modes, text, layer effects, adjustment layers; 8/16/32-bit; RGB/CMYK/Gray/Lab) and a minimal writer, from Adobe's published format specification | | L1 | `path` | Bezier paths, path operators (trim, offset, round corners, zig zag, twist, merge…), stroking, coverage masks | | L2 | `project` | The document: items, compositions, layers, the property tree, render queue model, `.ecproj` serde | | L2 | `text` | Fonts, shaping, layout, per-glyph geometry, text animators and selectors | | L2 | `effects` | The effect registry (241 effects) and their CPU implementations | +| L2 | `svg` | SVG import (W3C SVG 1.1/2 static subset: shapes, paths, transforms, `use`, CSS, gradients) and rasterisation at any scale | | L2 | `track` | Motion tracking: feature/search region point tracking (pyramid normalized cross-correlation, Lucas–Kanade sub-pixel refinement), confidence, homography/affine/similarity solves | | L3 | `render` | Evaluation and compositing: sources, masks, effects, transforms, 3D, motion blur, mattes, blending, layer cache, audio mixdown | -| L3 | `media` | Footage decoding (FilmCraft's pure-Rust codecs), image sequences, frame cache | +| L3 | `media` | Footage decoding (FilmCraft's pure-Rust codecs), image sequences, Photoshop and SVG stills, frame cache | | L3 | `expr` | The expression engine (JavaScript via boa) with the After Effects object model | -| L3 | `export` | Render queue encoding: H.264, ProRes, PNG/JPEG/TIFF/EXR sequences, GIF, audio | +| L3 | `export` | Render queue encoding: H.264, ProRes, WebM (VP9 + alpha, Opus), PNG/JPEG/TIFF/EXR sequences, GIF, WAV/AIFF | | L3 | `gpu` | The GPU compositor and GPU effects on wgpu compute shaders (Metal, Vulkan, Direct3D 12, WebGPU), checked against the CPU renderer | | L3 | `lottie` | Lottie JSON / dotLottie import and export (layers, precomps, eased and spatial keyframes, shapes, masks, mattes) with a warnings list for what Lottie cannot express | | L4 | `engine` | `Session`: project, undo history, editor state, the command registry and menus | @@ -76,6 +79,14 @@ its evaluated inputs, excluding the transform. Static and transform-only layers editing one layer re-renders only that layer. Effects that read the clock directly are declared in `effects::TIME_DEPENDENT`, and a test checks every registered effect against that list. +A persistent **disk cache** (`render::disk_cache`, Settings ▸ Media & Disk Cache) backs both +the layer cache and the viewer's RAM preview: layer buffers that were slow to render and every +preview frame are written (LZ4, atomic rename, checksummed) under 128-bit content keys — the +comp's content hash (the comp, what it uses, footage file size and time) plus frame, scale and +view for frames; the layer key salted with the footage fingerprint for layers — so entries +survive restarts and never go stale. LRU eviction keeps it under the size limit; Edit ▸ Purge +empties it, `cache.diskStats` reports it, and the timeline draws disk-only frames in blue. + **Time effects** (Echo, Posterize Time, Timewarp…) read the layer at other times through `EffectHost::self_at`: the renderer renders the layer's source and masks (plus, optionally, the effects before it) at that layer time via `Renderer::layer_input`, cached under separate *input* diff --git a/docs/parity.md b/docs/parity.md index d259fec2..5d291ca7 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -75,7 +75,7 @@ library groundwork for mask tracking and Warp Stabilizer. What is left is concentrated in large systems: Roto Brush and Refine Edge, the 3D camera tracker, Advanced 3D (models, PBR, image-based lighting), the Warp Stabilizer and mask-tracking UI on top of -the new tracking library, a JavaScript scripting object model, SVG/PSD import, WebM, a disk cache, +the new tracking library, a JavaScript scripting object model, the missing effect categories (3D Channel, Immersive Video, OCIO), native macOS menus, and wasm threads. @@ -84,9 +84,9 @@ wasm threads. | Area | Weighted parity | Remaining (agent-hours) | Biggest gaps | |---|---|---|---| | Layers | 88% | 4.8 | frame blending, collapse transformations, slip edit | -| Output | 85% | 4.2 | multiple output modules, pre-render, WebM, audio-only | +| Output | 90% | 3.0 | multiple output modules, pre-render (WebM with VP9 alpha + Opus and WAV/AIFF audio-only landed: VP9 is intra-only, Opus CELT-only) | | Audio | 85% | 0.5 | audio to keyframes | -| Import | 78% | 4.5 | PSD, SVG as shapes, Lottie | +| Import | 88% | 2.0 | AI/EPS/PDF vector footage, PSD smart objects and 3D layers (PSD as footage/composition/retain layer sizes, SVG footage and Create Shapes from Vector Layer landed) | | Automation | 76% | 3.3 | a JavaScript scripting object model | | Shapes | 68% | 3.9 | taper and wave strokes (pen tool for shape paths and vertex editing landed in M6.5) | | Compositions | 67% | 7.4 | marker dialog, flowchart, Essential Graphics | @@ -98,7 +98,7 @@ wasm threads. | Interface | 55% | 10.4 | drag-to-dock and floating panels, preferences, native macOS menus (viewer rulers/snapping/channels/snapshots landed in M0.13) | | Project | 53% | 8.1 | 8/16/32-bit pipeline and colour management, auto-save, proxies, folder moves | | Masks & roto | 52% | 10.0 | Roto Brush, variable-width mask feather points (mask tracking and Mask Interpolation landed in M6.6) | -| Preview | 55% | 6.1 | GPU 3D and adjustment layers, disk cache (region of interest, snapshots, exposure and Fast Previews landed in M0.13) | +| Preview | 62% | 5.0 | GPU 3D and adjustment layers (persistent disk cache with the blue cache bar landed; region of interest, snapshots, exposure and Fast Previews landed in M0.13) | | Tracking | ≈ 70% | 4.0 | 3D camera tracker, face tracking, Subspace Warp's mesh warp and rolling-shutter repair (point tracker, mask tracking and Warp Stabilizer landed in M6.x / M12.5) | | Paint | 0% | 4.0 | Brush, Clone Stamp, Eraser | @@ -125,7 +125,7 @@ Mocha and Cineware are third-party and not counted. 10. Drag-to-dock and floating panels, saved workspaces, native macOS menus. 11. Auto-save, crash recovery, recent projects (in progress). 12. Expression gaps: `sampleImage`, `footage()` (the `sourceText` style API landed in M9.9). -13. Lottie (in progress), WebM, SVG and PSD import. +13. ~~Lottie, WebM, SVG and PSD import~~ (landed; a disk cache too). 14. Puppet and paint tools (in progress). 15. Preferences and a shortcut editor that can rebind (in progress); real Wiggler, Smoother and Motion Sketch; the marker dialog. diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 7e7616a5..d822a1b9 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -23,6 +23,7 @@ const LAYERS: &[(&str, u8)] = &[ ("time", 0), ("geom", 0), ("color", 0), + ("vp9enc", 0), ("testkit", 0), ("opusenc", 0), ("raster", 1), From 541c740f1859b7e9cf072b868590b07b0e7cea85 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:30:04 -0400 Subject: [PATCH 21/23] CMP-6: Flowchart panel Composition > Composition Flowchart charts the active comp as a node graph: nested comps, footage and solids, optionally the layers and their effects (panel switches Solids / Layers / Effects), left to right or top to bottom. Double-click a comp to open it, Cmd/Ctrl-click to reveal an item in the Project panel (a layer: select it), drag or scroll to pan. Reveal Layer in Project Flowchart shows layers with the layer selected. The graph builder is pure and cycle-safe (shared items drawn once, back edges to comps on the path). Commands flowchart.options {layers, effects, solids, direction, comp} and flowchart.graph (the graph as JSON); automation ids flowchart.node. and flowchart.option.. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/menus.rs | 10 + crates/ui-egui/src/panels/flowchart.rs | 460 +++++++++++++++++++++++++ crates/ui-egui/src/panels/misc.rs | 2 +- crates/ui-egui/src/panels/mod.rs | 3 +- crates/ui-egui/src/state.rs | 4 + 5 files changed, 477 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/panels/flowchart.rs diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 4c85162e..da5eb842 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -72,6 +72,8 @@ pub const UI_COMMANDS: &[UiCommand] = &[ uic!("timeline.column", "Show/Hide Timeline Column", [], None), uic!("timeline.sourceName", "Source Name / Layer Name", [], None), uic!("timeline.search", "Search Timeline", [], None), + uic!("flowchart.options", "Flowchart Options", [], None), + uic!("flowchart.graph", "Flowchart Graph", [], None), uic!("timeline.collapseAll", "Collapse All", [], Some("Cmd+`")), uic!("tool.selection", "Selection Tool", [], Some("V")), uic!("tool.hand", "Hand Tool", [], Some("H")), @@ -331,6 +333,7 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V tl.source_name = params.get("value").and_then(Value::as_bool).unwrap_or(!tl.source_name); return Ok(json!({"sourceName": tl.source_name})); } + "flowchart.options" | "flowchart.graph" => return crate::panels::flowchart::command(app, id, ¶ms), "timeline.search" => { app.ui.timeline.search = params.get("query").and_then(Value::as_str).unwrap_or_default().to_string(); app.show_panel(PanelKind::Timeline); @@ -712,6 +715,13 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu Value::Null } "comp.flowchart" => { + // Chart the active comp; Layer ▸ Reveal ▸ Reveal Layer in Project Flowchart shows + // layers and selects the layer's node. + app.ui.flowchart.root = app.session.active_comp_id().map(|c| c.0); + if let (Some(c), Some(l)) = (app.session.active_comp_id(), app.session.state.selected_layers.first()) { + app.ui.flowchart.layers = true; + app.ui.flowchart.selected = Some(format!("layer:{}:{}", c.0, l.0)); + } app.show_panel(PanelKind::Flowchart); Value::Null } diff --git a/crates/ui-egui/src/panels/flowchart.rs b/crates/ui-egui/src/panels/flowchart.rs new file mode 100644 index 00000000..2ba345cf --- /dev/null +++ b/crates/ui-egui/src/panels/flowchart.rs @@ -0,0 +1,460 @@ +//! The Flowchart panel (Composition ▸ Composition Flowchart): a node graph of a composition and +//! everything it uses — nested comps, footage, solids — optionally with its layers and their +//! effects. Left to right or top to bottom; double-click a comp to open it; Cmd/Ctrl-click a +//! node to reveal it in the Project panel (layers: select the layer); drag to pan. +//! +//! [`build`] is pure and cycle-safe (a comp nested in itself is drawn once, with an edge back), +//! so agents get the same graph through `flowchart.graph`. + +use std::collections::{BTreeMap, BTreeSet}; + +use effectcraft_engine::project::{ItemId, ItemKind, LayerSource, Project}; +use egui::{Align2, Color32, Rect, Sense, Stroke, StrokeKind, pos2, vec2}; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +use crate::icons::{self, Icon}; +use crate::theme::Tokens; +use crate::{EffectcraftApp, widgets}; + +/// Flowchart panel options (the panel's switches). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct FlowOptions { + /// Show layers between a comp and its sources. + pub layers: bool, + /// Show each layer's effects (with layers shown). + pub effects: bool, + /// Show solids. + pub solids: bool, + /// Top to bottom instead of left to right. + pub vertical: bool, + /// The comp the chart starts from (None = the active comp). + pub root: Option, + #[serde(skip)] + pub pan: [f32; 2], + /// Selected node id. + #[serde(skip)] + pub selected: Option, +} + +impl Default for FlowOptions { + fn default() -> Self { + FlowOptions { layers: false, effects: false, solids: true, vertical: false, root: None, pan: [0.0, 0.0], selected: None } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct FlowNode { + /// `item:`, `layer::` or `fx:::`. + pub id: String, + /// `comp`, `footage`, `solid`, `layer` or `effect`. + pub kind: &'static str, + pub label: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub item: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub comp: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub layer: Option, + /// Distance from the root (column, or row when vertical). + pub depth: usize, +} + +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct FlowGraph { + pub nodes: Vec, + /// (from, to) node ids: from a comp to what it uses. + pub edges: Vec<(String, String)>, +} + +/// Build the flowchart of `root` (a comp). Shared items appear once; cycles stop at the comp +/// already on the path. +pub fn build(project: &Project, root: ItemId, opts: &FlowOptions) -> FlowGraph { + struct B<'a> { + p: &'a Project, + o: &'a FlowOptions, + nodes: Vec, + index: BTreeMap, + edges: Vec<(String, String)>, + stack: BTreeSet, + } + impl B<'_> { + fn node(&mut self, n: FlowNode) -> String { + let id = n.id.clone(); + match self.index.get(&id) { + Some(&i) => self.nodes[i].depth = self.nodes[i].depth.min(n.depth), + None => { + self.index.insert(id.clone(), self.nodes.len()); + self.nodes.push(n); + } + } + id + } + fn edge(&mut self, a: &str, b: &str) { + let e = (a.to_string(), b.to_string()); + if !self.edges.contains(&e) { + self.edges.push(e); + } + } + fn item(&mut self, id: ItemId, depth: usize) -> Option { + let it = self.p.item(id)?; + let kind = match &it.kind { + ItemKind::Comp(_) => "comp", + ItemKind::Footage(_) => "footage", + ItemKind::Solid(_) => "solid", + ItemKind::Folder => return None, + }; + if kind == "solid" && !self.o.solids { + return None; + } + let seen = self.index.contains_key(&format!("item:{}", id.0)); + let nid = self.node(FlowNode { id: format!("item:{}", id.0), kind, label: it.name.clone(), item: Some(id.0), comp: None, layer: None, depth }); + if kind == "comp" && !seen && !self.stack.contains(&id.0) { + self.comp(id, depth); + } + Some(nid) + } + fn comp(&mut self, cid: ItemId, depth: usize) { + let Some(c) = self.p.comp(cid) else { return }; + self.stack.insert(cid.0); + let from = format!("item:{}", cid.0); + for l in &c.layers { + let src_depth = if self.o.layers { depth + 2 } else { depth + 1 }; + let mut parent = from.clone(); + if self.o.layers { + let lid = self.node(FlowNode { + id: format!("layer:{}:{}", cid.0, l.id.0), + kind: "layer", + label: l.name.clone(), + item: None, + comp: Some(cid.0), + layer: Some(l.id.0), + depth: depth + 1, + }); + self.edge(&from, &lid); + if self.o.effects + && let Some(fx) = l.effects() + { + for g in fx.groups() { + let fid = self.node(FlowNode { + id: format!("fx:{}:{}:{}", cid.0, l.id.0, g.uid), + kind: "effect", + label: g.name.clone(), + item: None, + comp: Some(cid.0), + layer: Some(l.id.0), + depth: depth + 2, + }); + self.edge(&lid, &fid); + } + } + parent = lid; + } + let src = match l.source { + LayerSource::Footage { item } | LayerSource::Comp { item } | LayerSource::Solid { item } | LayerSource::Model { item } => Some(item), + _ => None, + }; + if let Some(item) = src { + // A comp already on the path: link back to it, don't descend (cycle). + if self.stack.contains(&item.0) { + self.edge(&parent, &format!("item:{}", item.0)); + } else if let Some(sid) = self.item(item, src_depth) { + self.edge(&parent, &sid); + } + } + } + self.stack.remove(&cid.0); + } + } + let mut b = B { p: project, o: opts, nodes: vec![], index: BTreeMap::new(), edges: vec![], stack: BTreeSet::new() }; + b.item(root, 0); + FlowGraph { nodes: b.nodes, edges: b.edges } +} + +const NODE_W: f32 = 168.0; +const NODE_H: f32 = 34.0; + +/// Node positions (top-left, before panning): one column (or row) per depth, in discovery order. +pub fn layout(g: &FlowGraph, vertical: bool) -> Vec<[f32; 2]> { + let mut slot: BTreeMap = BTreeMap::new(); + g.nodes + .iter() + .map(|n| { + let k = slot.entry(n.depth).or_insert(0); + let i = *k as f32; + *k += 1; + let d = n.depth as f32; + if vertical { [i * (NODE_W + 24.0), d * (NODE_H + 46.0)] } else { [d * (NODE_W + 60.0), i * (NODE_H + 14.0)] } + }) + .collect() +} + +/// The comp the panel charts. +pub fn root(app: &EffectcraftApp) -> Option { + app.ui.flowchart.root.map(ItemId).filter(|r| app.session.project.comp(*r).is_some()).or_else(|| app.session.active_comp_id()) +} + +/// `flowchart.options` / `flowchart.graph` (UI commands). +pub fn command(app: &mut EffectcraftApp, id: &str, p: &Value) -> Result { + let o = &mut app.ui.flowchart; + let b = |k: &str| p.get(k).and_then(Value::as_bool); + if let Some(v) = b("layers") { + o.layers = v; + } + if let Some(v) = b("effects") { + o.effects = v; + } + if let Some(v) = b("solids") { + o.solids = v; + } + if let Some(d) = p.get("direction").and_then(Value::as_str) { + o.vertical = match d { + "lr" | "leftToRight" => false, + "tb" | "topToBottom" => true, + _ => return Err(format!("direction: lr|tb, not `{d}`")), + }; + } + if let Some(c) = p.get("comp") { + let cid = match c { + Value::Number(n) => n.as_u64().map(ItemId), + Value::String(s) => app.session.project.find_by_name(s).map(|i| i.id), + _ => None, + }; + let cid = cid.filter(|c| app.session.project.comp(*c).is_some()).ok_or("comp: a composition id or name")?; + app.ui.flowchart.root = Some(cid.0); + } + let o = app.ui.flowchart.clone(); + if id == "flowchart.graph" { + let r = root(app).ok_or("no composition")?; + return Ok(serde_json::to_value(build(&app.session.project, r, &o)).unwrap_or_default()); + } + Ok(json!({"layers": o.layers, "effects": o.effects, "solids": o.solids, "direction": if o.vertical { "tb" } else { "lr" }, "root": o.root})) +} + +fn kind_style(kind: &str) -> (Icon, Color32) { + match kind { + "comp" => (Icon::Comp, Color32::from_rgb(0x3d, 0x5a, 0x8c)), + "footage" => (Icon::Footage, Color32::from_rgb(0x3c, 0x6e, 0x58)), + "solid" => (Icon::Solid, Color32::from_rgb(0x6a, 0x58, 0x3a)), + "effect" => (Icon::Fx, Color32::from_rgb(0x6c, 0x44, 0x6e)), + _ => (Icon::Null, Color32::from_rgb(0x44, 0x48, 0x52)), + } +} + +/// Draw the Flowchart panel. +pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { + let t = app.tokens; + let ctx = ui.ctx().clone(); + let p = ui.painter().with_clip_rect(rect); + p.rect_filled(rect, 0.0, t.tl_bg); + let Some(rid) = root(app) else { + p.text(rect.center(), Align2::CENTER_CENTER, "(no composition)", Tokens::ui(12.0), t.text_faint); + return; + }; + // ---- toolbar: switches and direction. + let bar = Rect::from_min_size(rect.min, vec2(rect.width(), 30.0)); + p.rect_filled(bar, 0.0, t.panel_bg); + let root_name = app.session.project.item(rid).map(|i| i.name.clone()).unwrap_or_default(); + p.text(pos2(bar.min.x + 10.0, bar.center().y), Align2::LEFT_CENTER, format!("Flowchart: {root_name}"), Tokens::medium(12.0), t.text); + let mut x = bar.max.x - 8.0; + let mut cmds: Vec<(String, Value)> = vec![]; + let o = app.ui.flowchart.clone(); + for (key, label, on) in [ + ("direction", if o.vertical { "Top to Bottom" } else { "Left to Right" }, false), + ("effects", "Effects", o.effects), + ("layers", "Layers", o.layers), + ("solids", "Solids", o.solids), + ] { + let w = 18.0 + label.len() as f32 * 6.6; + let r = Rect::from_min_size(pos2(x - w, bar.min.y + 5.0), vec2(w, 20.0)); + x -= w + 6.0; + if widgets::text_button(ui, r, label, on, &t, egui::Id::new(("flow-opt", key))).clicked() { + let v = if key == "direction" { json!({"direction": if o.vertical { "lr" } else { "tb" }}) } else { json!({key: !on}) }; + cmds.push(("flowchart.options".into(), v)); + } + app.auto.add(&format!("flowchart.option.{key}"), r, label); + } + + // ---- graph. + let body = Rect::from_min_max(pos2(rect.min.x, bar.max.y), rect.max); + let g = build(&app.session.project, rid, &o); + let pos = layout(&g, o.vertical); + let origin = body.min + vec2(24.0 + o.pan[0], 24.0 + o.pan[1]); + let bg = ui.interact(body, egui::Id::new("flow-bg"), Sense::click_and_drag()); + if bg.dragged() { + let d = bg.drag_delta(); + app.ui.flowchart.pan[0] += d.x; + app.ui.flowchart.pan[1] += d.y; + } + if ui.rect_contains_pointer(body) { + let s = ui.input(|i| i.smooth_scroll_delta); + if s != egui::Vec2::ZERO { + app.ui.flowchart.pan[0] += s.x; + app.ui.flowchart.pan[1] += s.y; + } + } + if bg.clicked() { + app.ui.flowchart.selected = None; + } + let gp = p.with_clip_rect(body); + let rect_of = |i: usize| Rect::from_min_size(origin + vec2(pos[i][0], pos[i][1]), vec2(NODE_W, NODE_H)); + let idx: BTreeMap<&str, usize> = g.nodes.iter().enumerate().map(|(i, n)| (n.id.as_str(), i)).collect(); + // Edges first (curves with an arrowhead at the target). + for (a, b) in &g.edges { + let (Some(&ia), Some(&ib)) = (idx.get(a.as_str()), idx.get(b.as_str())) else { continue }; + let (ra, rb) = (rect_of(ia), rect_of(ib)); + let back = g.nodes[ib].depth <= g.nodes[ia].depth; + let (s, e, n) = if o.vertical { (ra.center_bottom(), rb.center_top(), vec2(0.0, 1.0)) } else { (ra.right_center(), rb.left_center(), vec2(1.0, 0.0)) }; + let k = if o.vertical { (e.y - s.y).abs() * 0.5 } else { (e.x - s.x).abs() * 0.5 }.max(30.0); + let col = if back { Color32::from_rgb(0xd0, 0x80, 0x40) } else { t.text_faint }; + let curve = egui::epaint::CubicBezierShape::from_points_stroke([s, s + n * k, e - n * k, e], false, Color32::TRANSPARENT, Stroke::new(1.2, col)); + gp.add(curve); + let (l, r) = if o.vertical { (vec2(-4.0, -7.0), vec2(4.0, -7.0)) } else { (vec2(-7.0, -4.0), vec2(-7.0, 4.0)) }; + gp.add(egui::Shape::convex_polygon(vec![e, e + l, e + r], col, Stroke::NONE)); + } + let cmd_down = ui.input(|i| i.modifiers.command || i.modifiers.ctrl); + for (i, n) in g.nodes.iter().enumerate() { + let r = rect_of(i); + if !r.intersects(body) { + continue; + } + let (icon, fill) = kind_style(n.kind); + let selected = o.selected.as_deref() == Some(n.id.as_str()); + let resp = ui.interact(r.intersect(body), egui::Id::new(("flow-node", &n.id)), Sense::click()); + let fill = if resp.hovered() { fill.gamma_multiply(1.25) } else { fill }; + gp.rect_filled(r, 5.0, fill); + gp.rect_stroke( + r, + 5.0, + Stroke::new(if selected { 2.0 } else { 1.0 }, if selected { t.accent } else { Color32::from_black_alpha(120) }), + StrokeKind::Inside, + ); + icons::paint(&gp, Rect::from_center_size(pos2(r.min.x + 16.0, r.center().y), vec2(14.0, 14.0)), icon, Color32::WHITE); + gp.with_clip_rect(r.shrink(2.0).intersect(body)).text( + pos2(r.min.x + 30.0, r.center().y), + Align2::LEFT_CENTER, + &n.label, + Tokens::ui(12.0), + Color32::WHITE, + ); + app.auto.add(&format!("flowchart.node.{}", n.id), r, &n.label); + if resp.clicked() { + app.ui.flowchart.selected = Some(n.id.clone()); + if cmd_down { + // Reveal: items in the Project panel, layers selected in their comp. + match (n.item, n.comp, n.layer) { + (Some(item), ..) => { + cmds.push(("project.select".into(), json!({"items": [item]}))); + cmds.push(("window.panel".into(), json!({"panel": "project"}))); + } + (None, Some(c), Some(l)) => { + cmds.push(("comp.open".into(), json!({"comp": c}))); + cmds.push(("layer.select".into(), json!({"layers": [l]}))); + } + _ => {} + } + } + } + if resp.double_clicked() { + match (n.kind, n.item, n.comp) { + ("comp", Some(c), _) => cmds.push(("comp.open".into(), json!({"comp": c}))), + (_, _, Some(c)) => cmds.push(("comp.open".into(), json!({"comp": c}))), + _ => {} + } + } + resp.on_hover_text(format!("{} — double-click to open, {}-click to reveal", n.label, if cfg!(target_os = "macos") { "Cmd" } else { "Ctrl" })); + } + for (id, params) in cmds { + if let Err(e) = crate::menus::invoke(app, &ctx, &id, params) { + app.ui.status = e; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use effectcraft_engine::Session; + + /// Main ⊃ Mid ⊃ Inner (with a solid), Main also uses Inner directly. + fn session() -> (Session, ItemId, ItemId, ItemId) { + let mut s = Session::default(); + let id = |v: Value| ItemId(v["id"].as_u64().or_else(|| v["comp"].as_u64()).or_else(|| v["item"].as_u64()).unwrap()); + let inner = id(s.execute("comp.new", json!({"name": "Inner", "width": 100, "height": 100, "duration": 2})).unwrap()); + s.execute("layer.newSolid", json!({"name": "Red", "color": "#ff0000", "width": 100, "height": 100})).unwrap(); + s.execute("layer.select", json!({"layers": []})).unwrap(); + let mid = id(s.execute("comp.new", json!({"name": "Mid", "width": 100, "height": 100, "duration": 2})).unwrap()); + s.execute("layer.addItem", json!({"item": inner.0})).unwrap(); + let main = id(s.execute("comp.new", json!({"name": "Main", "width": 100, "height": 100, "duration": 2})).unwrap()); + s.execute("layer.addItem", json!({"item": mid.0})).unwrap(); + s.execute("layer.addItem", json!({"item": inner.0})).unwrap(); + s.execute("layer.newText", json!({"text": "Title"})).unwrap(); + (s, main, mid, inner) + } + + #[test] + fn nested_comps_make_a_graph() { + let (s, main, mid, inner) = session(); + let g = build(&s.project, main, &FlowOptions::default()); + let label = |id: &str| g.nodes.iter().find(|n| n.id == id).map(|n| (n.label.clone(), n.depth)); + assert_eq!(label(&format!("item:{}", main.0)), Some(("Main".into(), 0))); + assert_eq!(label(&format!("item:{}", mid.0)), Some(("Mid".into(), 1))); + // Inner is reached at depth 1 (directly) and 2 (through Mid): drawn once, at depth 1. + assert_eq!(label(&format!("item:{}", inner.0)), Some(("Inner".into(), 1))); + assert_eq!(g.nodes.iter().filter(|n| n.label == "Inner").count(), 1); + let has = |a: ItemId, b: ItemId| g.edges.contains(&(format!("item:{}", a.0), format!("item:{}", b.0))); + assert!(has(main, mid) && has(mid, inner) && has(main, inner)); + // The solid is shown (Solids on) and hidden with Solids off; text layers have no source. + assert!(g.nodes.iter().any(|n| n.kind == "solid")); + let g2 = build(&s.project, main, &FlowOptions { solids: false, ..Default::default() }); + assert!(!g2.nodes.iter().any(|n| n.kind == "solid")); + // Layers between comps and sources; effects under layers. + let g3 = build(&s.project, main, &FlowOptions { layers: true, ..Default::default() }); + assert!(g3.nodes.iter().any(|n| n.kind == "layer" && n.label == "Title")); + let mid_layer = g3.nodes.iter().find(|n| n.kind == "layer" && n.comp == Some(main.0) && n.label == "Mid").unwrap(); + assert!(g3.edges.contains(&(mid_layer.id.clone(), format!("item:{}", mid.0)))); + // Layout: one column per depth, no overlaps. + let pos = layout(&g3, false); + for i in 0..pos.len() { + for j in i + 1..pos.len() { + assert!(pos[i] != pos[j], "{} and {} overlap", g3.nodes[i].label, g3.nodes[j].label); + } + } + } + + #[test] + fn cycles_are_safe() { + let (mut s, main, mid, _) = session(); + // Make Mid contain Main: Main → Mid → Main. + s.execute("comp.open", json!({"comp": mid.0})).unwrap(); + // Adding a cyclic layer may be refused by the engine; build a cycle in the model directly. + let mut p = (*s.project).clone(); + let mut c = (*p.comp(mid).unwrap()).clone(); + let mut l = c.layers[0].clone(); + l.id = effectcraft_engine::project::LayerId(9999); + l.source = LayerSource::Comp { item: main }; + c.layers.push(l); + if let Some(ItemKind::Comp(arc)) = p.items.get_mut(&mid).map(|i| &mut i.kind) { + *arc = std::sync::Arc::new(c); + } + let g = build(&p, main, &FlowOptions { layers: true, effects: true, ..Default::default() }); + assert_eq!(g.nodes.iter().filter(|n| n.kind == "comp" && n.label == "Main").count(), 1); + // The back edge points at the root. + assert!(g.edges.iter().any(|(_, b)| *b == format!("item:{}", main.0))); + assert!(g.nodes.len() < 50); + } + + #[test] + fn options_command() { + let (s, main, ..) = session(); + let mut app = EffectcraftApp::new(s); + let r = command(&mut app, "flowchart.options", &json!({"layers": true, "direction": "tb"})).unwrap(); + assert_eq!(r["direction"], "tb"); + assert!(app.ui.flowchart.layers && app.ui.flowchart.vertical); + assert!(command(&mut app, "flowchart.options", &json!({"direction": "diagonal"})).is_err()); + let g = command(&mut app, "flowchart.graph", &json!({"comp": "Main"})).unwrap(); + assert_eq!(g["nodes"][0]["id"], format!("item:{}", main.0)); + assert!(g["edges"].as_array().is_some_and(|e| !e.is_empty())); + } +} diff --git a/crates/ui-egui/src/panels/misc.rs b/crates/ui-egui/src/panels/misc.rs index 0db1ed7f..69d95f66 100644 --- a/crates/ui-egui/src/panels/misc.rs +++ b/crates/ui-egui/src/panels/misc.rs @@ -1,4 +1,4 @@ -//! Smaller panels: Preview, Audio, History, Markers, the Home screen and placeholders. +//! Smaller panels: Preview, Audio, History, Markers and placeholders. use egui::{Align2, Color32, Rect, Sense, Stroke, pos2, vec2}; use serde_json::json; diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs index dae89f16..cc103e84 100644 --- a/crates/ui-egui/src/panels/mod.rs +++ b/crates/ui-egui/src/panels/mod.rs @@ -6,6 +6,7 @@ pub mod dialogs; pub mod dialogs_3d; pub mod effect_controls; pub mod effects_presets; +pub mod flowchart; pub mod forms; pub mod fx_widgets; pub mod graph; @@ -91,7 +92,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: PanelKind, rect: Rec PanelKind::Layer => layer_panel::show(app, ui, rect), PanelKind::Paint => paint_panels::paint(app, ui, rect), PanelKind::Brushes => paint_panels::brushes(app, ui, rect), - other => misc::placeholder(app, ui, rect, other), + PanelKind::Flowchart => flowchart::show(app, ui, rect), } } diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index af59bb6b..1d1dcb23 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -584,6 +584,9 @@ pub struct UiState { /// Composition Mini-Flowchart popup position (open when set). #[serde(default)] pub mini_flowchart: Option<[f32; 2]>, + /// Flowchart panel options (layers, effects, solids, direction, root comp). + #[serde(default)] + pub flowchart: crate::panels::flowchart::FlowOptions, } impl Default for UiState { @@ -634,6 +637,7 @@ impl Default for UiState { layer_view: None, anim_tools: AnimToolsState::default(), mini_flowchart: None, + flowchart: Default::default(), } } } From 6aca8fe85c9a2dd76fc02419e3270535ae313556 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:34:32 -0400 Subject: [PATCH 22/23] UI-1: every After Effects workspace Adds Review, Learn, Essential Graphics, Color and Undocked Panels to the built-in workspaces (Libraries, an Adobe service, is left out), lists them all in Window > Workspace in After Effects' order, and gives each its own layout: Review (large viewer, Markers and Project), Learn (Default plus the Home screen with the community links), Essential Graphics (Properties panel full height, type panels), Color (wide Effect Controls, Info), Undocked Panels (Composition docked, the other panels floating). Tests check every AE workspace exists, has its own layout, has each panel once and is in the menu. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/menus.rs | 15 +++-- crates/ui-egui/src/dock.rs | 125 ++++++++++++++++++++++++++++++++++++- crates/ui-egui/src/lib.rs | 6 +- 3 files changed, 139 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/menus.rs b/crates/engine/src/menus.rs index 38252a88..14c39dbb 100644 --- a/crates/engine/src/menus.rs +++ b/crates/engine/src/menus.rs @@ -17,7 +17,8 @@ //! //! An entry's shortcut is the command's default shortcut unless the line gives one (needed for //! entries with bound parameters). Adobe-service entries (Team Projects, Libraries, Bridge, Media -//! Encoder, Essential Graphics, Behance, Creative Cloud…) are intentionally absent. +//! Encoder, Behance, Creative Cloud…) are intentionally absent; the Essential Graphics workspace +//! uses the Properties panel. use std::sync::OnceLock; @@ -909,16 +910,22 @@ View Enter Full Screen | view.fullScreen Window Workspace + Default | window.workspace {"name":"Default"} | Shift+F10 + Review | window.workspace {"name":"Review"} + Learn | window.workspace {"name":"Learn"} + Small Screen | window.workspace {"name":"Small Screen"} | Shift+F12 + Standard | window.workspace {"name":"Standard"} | Shift+F11 + --- All Panels | window.workspace {"name":"All Panels"} Animation | window.workspace {"name":"Animation"} - Default | window.workspace {"name":"Default"} | Shift+F10 + Color | window.workspace {"name":"Color"} Effects | window.workspace {"name":"Effects"} + Essential Graphics | window.workspace {"name":"Essential Graphics"} Minimal | window.workspace {"name":"Minimal"} Motion Tracking | window.workspace {"name":"Motion Tracking"} Paint | window.workspace {"name":"Paint"} - Small Screen | window.workspace {"name":"Small Screen"} | Shift+F12 - Standard | window.workspace {"name":"Standard"} | Shift+F11 Text | window.workspace {"name":"Text"} + Undocked Panels | window.workspace {"name":"Undocked Panels"} --- Reset to Saved Layout | window.resetWorkspace Save Changes to this Workspace | window.saveWorkspace diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index 99bc7c5e..9d6d55cc 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -172,7 +172,43 @@ fn vsplit(size: SplitSize, a: DockNode, b: DockNode) -> DockNode { DockNode::Split { vertical: true, size, a: Box::new(a), b: Box::new(b) } } -pub const WORKSPACES: [&str; 10] = ["Default", "Standard", "Small Screen", "Animation", "Effects", "Motion Tracking", "Paint", "Text", "Minimal", "All Panels"]; +/// The built-in workspaces: After Effects 2026's (Window ▸ Workspace), less Libraries (an Adobe +/// service). Learn shows the Home screen (community links instead of Adobe's tutorials); +/// Essential Graphics uses the Properties panel; Undocked Panels floats the panels over the +/// Composition panel. +pub const WORKSPACES: &[&str] = &[ + "Default", + "Standard", + "Small Screen", + "Animation", + "Effects", + "Motion Tracking", + "Paint", + "Text", + "Minimal", + "All Panels", + "Review", + "Learn", + "Essential Graphics", + "Color", + "Undocked Panels", +]; + +/// The floating panels a workspace starts with (Undocked Panels), in points for a window of +/// about 1680×1020 (they are kept inside the window when drawn). +pub fn workspace_floating(name: &str) -> Vec { + use PanelKind::*; + let f = |panels: &[PanelKind], rect: [f32; 4]| Floating { panels: panels.to_vec(), active: 0, rect }; + match name { + "Undocked Panels" => vec![ + f(&[Project, EffectControls], [24.0, 110.0, 320.0, 420.0]), + f(&[Preview, Info, Audio], [1330.0, 110.0, 320.0, 300.0]), + f(&[EffectsPresets, Properties, Character, Paragraph], [1330.0, 430.0, 320.0, 360.0]), + f(&[Timeline, RenderQueue], [24.0, 640.0, 1280.0, 340.0]), + ], + _ => vec![], + } +} /// The default layout of a named workspace. pub fn workspace(name: &str) -> DockNode { @@ -226,6 +262,40 @@ pub fn workspace(name: &str) -> DockNode { tabs(&[Timeline], 0), ), "Minimal" => vsplit(Ratio(0.6), tabs(&[Composition], 0), tabs(&[Timeline], 0)), + // Review: a large viewer with the markers and the project beside it. + "Review" => vsplit( + Ratio(0.66), + hsplit(FixedA(280.0), tabs(&[Project, Markers], 0), hsplit(FixedB(260.0), tabs(&[Composition, Layer], 0), right(&[Preview, Info], &[Audio]))), + tabs(&[Timeline, RenderQueue], 0), + ), + // Essential Graphics: the Properties panel (After Effects' successor to the Essential + // Graphics panel) full height on the right, type panels beside the viewer. + "Essential Graphics" => hsplit( + FixedB(320.0), + vsplit( + Ratio(0.58), + hsplit( + FixedA(280.0), + tabs(&[Project, EffectControls], 0), + hsplit(FixedB(260.0), tabs(&[Composition, Layer], 0), right(&[Character, Paragraph], &[Align, Preview])), + ), + tabs(&[Timeline], 0), + ), + stack(&[(Properties, true, None), (EffectsPresets, false, None)]), + ), + // Color: wide Effect Controls for grading effects, Info for colour readouts. + "Color" => vsplit( + Ratio(0.6), + hsplit( + FixedA(360.0), + tabs(&[EffectControls, Project], 0), + hsplit(FixedB(300.0), tabs(&[Composition, Layer], 0), right(&[Info, Preview], &[EffectsPresets])), + ), + tabs(&[Timeline], 0), + ), + // Undocked Panels: the Composition panel docked, the others floating + // ([`workspace_floating`]). + "Undocked Panels" => tabs(&[Composition, Layer, Flowchart], 0), "Small Screen" => vsplit( Ratio(0.55), hsplit( @@ -814,8 +884,59 @@ mod tests { fn workspaces_contain_core_panels() { for w in WORKSPACES { let d = workspace(w); - assert!(d.contains(PanelKind::Timeline), "{w}"); + let floating: Vec = workspace_floating(w).into_iter().flat_map(|f| f.panels).collect(); + assert!(d.contains(PanelKind::Timeline) || floating.contains(&PanelKind::Timeline), "{w}"); assert!(d.contains(PanelKind::Composition) || d.contains(PanelKind::Layer), "{w}"); + // A panel is in one place only. + let mut all = vec![]; + d.panels(&mut all); + all.extend(floating); + let n = all.len(); + all.sort_by_key(|p| p.id()); + all.dedup(); + assert_eq!(all.len(), n, "{w} has a panel twice"); + } + } + + #[test] + fn after_effects_workspaces_exist() { + // Window ▸ Workspace in After Effects 2026, less Libraries (an Adobe service). + for w in [ + "Default", + "Review", + "Learn", + "Small Screen", + "Standard", + "All Panels", + "Animation", + "Essential Graphics", + "Color", + "Effects", + "Minimal", + "Motion Tracking", + "Paint", + "Text", + "Undocked Panels", + ] { + assert!(WORKSPACES.contains(&w), "{w}"); + } + assert!(!WORKSPACES.contains(&"Libraries")); + // Layouts differ from Default (except Learn, which is Default plus the Home screen). + let def = workspace("Default"); + for w in WORKSPACES.iter().filter(|w| !matches!(**w, "Default" | "Learn")) { + assert_ne!(workspace(w), def, "{w} falls back to Default"); + } + assert!(workspace("Essential Graphics").contains(PanelKind::Properties)); + assert!(workspace("Color").contains(PanelKind::EffectControls)); + assert!(!workspace_floating("Undocked Panels").is_empty()); + // Every built-in workspace is in Window ▸ Workspace. + let menu: Vec = effectcraft_engine::menus::entries() + .into_iter() + .filter(|(_, e)| e.command == "window.workspace") + .filter_map(|(_, e)| e.params.get("name").and_then(|v| v.as_str()).map(str::to_string)) + .collect(); + for w in WORKSPACES { + assert!(menu.iter().any(|m| m == w), "{w} missing from Window > Workspace"); } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 59005cdc..e1848063 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -279,8 +279,12 @@ impl EffectcraftApp { pub fn set_workspace(&mut self, name: &str) { self.ui.workspace = name.to_string(); self.ui.dock = self.ui.saved_workspaces.get(name).cloned().unwrap_or_else(|| dock::workspace(name)); - self.ui.floating = self.ui.saved_floating.get(name).cloned().unwrap_or_default(); + self.ui.floating = self.ui.saved_floating.get(name).cloned().unwrap_or_else(|| dock::workspace_floating(name)); self.ui.maximized = None; + // Learn: the Home screen (community links) in the Composition panel. + if name == "Learn" { + self.ui.start_screen = true; + } } /// Built-in workspaces followed by the saved ones (Window ▸ Workspace ▸ Save as New Workspace). From 5e441e859df4235427e865adb51044ad58654fa5 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 2 Oct 2026 22:36:30 -0400 Subject: [PATCH 23/23] UI-8, UI-3, UI-7, CMP-6, UI-1: parity and control-protocol docs Co-Authored-By: Claude Opus 5.5 --- docs/control-protocol.md | 1 + docs/parity.md | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 449a1898..7763ee03 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -27,6 +27,7 @@ printf '{"id":1,"method":"engine.execute","params":{"command":"comp.new","params |---|---|---| | `engine.execute` | `{command, params?}` | The command's result. Every engine command (`effectcraft-cli commands`) runs exactly as it does headless, so it never opens a dialog: `comp.new {}` creates a default comp. UI-only ids (`tool.*`, `view.*`, `window.*`, `playback.*`, `timeline.*`, `app.*`) fall through to the menu dispatcher. Unknown top-level params are rejected, and the error lists the accepted keys. | | `ui.menu.invoke` | `{id, params?}` | Like a menu click: commands with empty params may open their dialog (New Composition, Solid Settings, file pickers). | +| UI commands (via either) | | `timeline.column {column, visible?}` (av, keys, label, num, comment, switches, modes, parent, in, out, duration, stretch), `timeline.sourceName {value?}`, `timeline.search {query}`, `timeline.revealAdd.`, `flowchart.options {layers?, effects?, solids?, direction?: lr\|tb, comp?}`, `flowchart.graph {comp?}` → `{nodes, edges}`, `window.workspace {name}` (all After Effects workspaces). | | `engine.commands` | `{filter?, enabledOnly?}` | `[{id, label, menu, shortcut, params, enabled, why}]` | | `ui.menu.list` | `{}` | The menu tree with enablement. | | `render.frame` | `{comp?, time?, max_side?, path?, base64?}` | Renders a comp frame through the session, independent of the viewer zoom or resolution. `comp` is an id or name (default: the active comp), `time` is in comp seconds (default: the CTI), `max_side` caps the longest side (0 = full size). Writes a PNG to `path` (default: a temp file) and returns `{comp, time, width, height, path}`. With `base64: true` it returns `{…, png: ""}` and writes no file. | diff --git a/docs/parity.md b/docs/parity.md index aecb3f13..0be871b4 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -76,8 +76,9 @@ library groundwork for mask tracking and Warp Stabilizer. What is left is concentrated in large systems: Roto Brush and Refine Edge, the 3D camera tracker, the Warp Stabilizer and mask-tracking UI on top of the new tracking library, a JavaScript scripting object model, SVG/PSD import, WebM, a disk cache, -the missing effect categories (3D Channel, Immersive Video, OCIO), native macOS menus, and -wasm threads. +the missing effect categories (3D Channel, Immersive Video, OCIO), and wasm threads. (Native +macOS menus, the Composition Flowchart, Timeline column/search depth, the Home screen and every +After Effects workspace landed in the UI-polish wave.) ## By area @@ -89,13 +90,13 @@ wasm threads. | Import | 78% | 4.5 | PSD, SVG as shapes, Lottie | | Automation | 76% | 3.3 | a JavaScript scripting object model | | Shapes | 68% | 3.9 | taper and wave strokes (pen tool for shape paths and vertex editing landed in M6.5) | -| Compositions | 67% | 7.4 | marker dialog, flowchart, Essential Graphics | +| Compositions | 72% | 6.0 | Essential Graphics panel (the marker dialog and Composition Flowchart landed) | | Animation | 65% | 10.4 | puppet, Wiggler/Smoother/Motion Sketch (motion-path handles and the graph editor transform box landed in M5.8) | | Text | ≈ 86% | 1.3 | vertical type is basic (upright characters, no tate-chu-yoko), no OpenType feature panel, no extruded strokes (extruded, bevelled text landed in M7.6); per-character styles, paragraph settings, on-canvas editing and the `sourceText` style API landed (M9.9–M9.10) | | Web | 60% | 2.5 | threads, browser storage, audio | | 3D | 72% | 12.0 | 3D camera tracker, multi-view layouts, Advanced 3D motion blur and blend modes, cameras/lights from models; Advanced 3D (glTF/OBJ models, primitives, extruded text and shapes, PBR, image-based light, shadow maps, GPU rasteriser) landed in M7.4–M7.6 | | Effects | 58% | 13.6 | Effect Controls widgets (angle dial, point crosshair, eyedropper, curves), GPU versions of more effects, 41 missing effects | -| Interface | 55% | 10.4 | drag-to-dock and floating panels, preferences, native macOS menus (viewer rulers/snapping/channels/snapshots landed in M0.13) | +| Interface | 70% | 6.0 | Timeline horizontal scrolling of many columns, a richer Learn area (native macOS menu bar, Timeline columns/search/reveal-add, Home screen with recent projects and all AE workspaces landed; viewer rulers/snapping/channels/snapshots landed in M0.13) | | Project | 53% | 8.1 | 8/16/32-bit pipeline and colour management, auto-save, proxies, folder moves | | Masks & roto | 52% | 10.0 | Roto Brush, variable-width mask feather points (mask tracking and Mask Interpolation landed in M6.6) | | Preview | 55% | 6.1 | GPU 3D and adjustment layers, disk cache (region of interest, snapshots, exposure and Fast Previews landed in M0.13) | @@ -122,7 +123,7 @@ Mocha and Cineware are third-party and not counted. 7. Point tracking and stabilization (in progress). 8. Frame blending, collapse transformations, slip edit. 9. A real 8/16/32-bit pipeline with linear blending and colour management. -10. Drag-to-dock and floating panels, saved workspaces, native macOS menus. +10. ~~Drag-to-dock and floating panels, saved workspaces, native macOS menus.~~ 11. Auto-save, crash recovery, recent projects (in progress). 12. Expression gaps: `sampleImage`, `footage()` (the `sourceText` style API landed in M9.9). 13. Lottie (in progress), WebM, SVG and PSD import.