Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

feat(effects): clip-relative time in the shader frame uniform

maubrickastuto11e516140ffd
diff --git a/src/crates/concat-effects/packages/concat.word-reveal/effect.toml b/src/crates/concat-effects/packages/concat.word-reveal/effect.toml
index 07f16dde..aba82535 100644
--- a/src/crates/concat-effects/packages/concat.word-reveal/effect.toml
+++ b/src/crates/concat-effects/packages/concat.word-reveal/effect.toml
@@ -4,7 +4,7 @@ name = "Word Reveal"
kind = "effect"
category = "Text"
order = 97
-description = "A title's words appear one at a time as progress rises, in reading order."
+description = "A title's words appear one at a time as the clip plays, in reading order."
[[param]]
key = "style"
@@ -17,13 +17,13 @@ values = [0, 1, 2]
labels = ["Fade", "Blur", "Typewriter"]
[[param]]
-key = "progress"
-label = "Progress"
-min = 0
-max = 100
-default = 100
-step = 1
-unit = "%"
+key = "duration"
+label = "Duration"
+min = 0.2
+max = 5
+default = 1.5
+step = 0.1
+unit = "s"
[[param]]
key = "softness"
diff --git a/src/crates/concat-effects/packages/concat.word-reveal/effect.wgsl b/src/crates/concat-effects/packages/concat.word-reveal/effect.wgsl
index ad57c012..7337c4ab 100644
--- a/src/crates/concat-effects/packages/concat.word-reveal/effect.wgsl
+++ b/src/crates/concat-effects/packages/concat.word-reveal/effect.wgsl
@@ -1,14 +1,17 @@
-struct Params { style: f32, progress: f32, softness: f32 }
+struct Params { style: f32, duration: f32, softness: f32 }
// `reveal_order(uv)` is baked with the title: 0 for its first word, 1 for
-// its last, 0 everywhere off a title (a no-op there, since `progress`
-// starting at 0 would otherwise hide everything). A word crosses from
-// hidden to shown as `progress` passes its own order, softened by
+// its last, 0 everywhere off a title (a no-op there, since progress
+// starting at 0 would otherwise hide everything). Progress comes from
+// `clip_time`, not a knob: the reveal starts the instant the clip does
+// and finishes `duration` seconds later, so dropping the effect on a
+// title is the whole gesture - nothing to key by hand. A word crosses
+// from hidden to shown as progress passes its own order, softened by
// `softness` into a band so words do not pop in lock-step.
fn effect(uv: vec2<f32>) -> vec4<f32> {
let c = sample(uv);
let order = reveal_order(uv);
- let p = clamp(params.progress / 100.0, 0.0, 1.0);
+ let p = clamp(frame.clip_time / max(params.duration, 0.05), 0.0, 1.0);
let soft = max(params.softness / 100.0, 0.001);
let shown = 1.0 - smoothstep(p, p + soft, order);
diff --git a/src/crates/concat-effects/src/shader.rs b/src/crates/concat-effects/src/shader.rs
index e37c01a9..2b1d087d 100644
--- a/src/crates/concat-effects/src/shader.rs
+++ b/src/crates/concat-effects/src/shader.rs
@@ -35,6 +35,15 @@ struct Frame {
time: f32,
/// How much of the effect to keep over the untouched layer.
intensity: f32,
+ /// Seconds since this layer's own clip began - zero at its first
+ /// frame, however far into the timeline that is. A one-shot look
+ /// times itself to this instead of `time`, so it plays the same
+ /// whether the clip starts at zero or at the twenty-minute mark; a
+ /// looping one can still read `time` for a phase nothing needs to
+ /// reset. Layers with no single clip of their own - a treatment's
+ /// stack, a synthesized ground - carry zero here always, which reads
+ /// as "just started" forever; a look that only loops is unaffected.
+ clip_time: f32,
}
@group(0) @binding(0) var source: texture_2d<f32>;
diff --git a/src/crates/concat-render/src/gpu.rs b/src/crates/concat-render/src/gpu.rs
index c272f94b..5ec3f5cd 100644
--- a/src/crates/concat-render/src/gpu.rs
+++ b/src/crates/concat-render/src/gpu.rs
@@ -580,7 +580,11 @@ impl WgpuCompositor {
ground = Some(below);
continue;
}
- let treated = self.run_passes(width, height, below, &treatment.effects, seconds);
+ // A treatment has no single clip of its own - it runs over
+ // whatever stack sits beneath it - so `clip_time` falls back
+ // to the timeline's own clock, exactly what a package read
+ // before this existed.
+ let treated = self.run_passes(width, height, below, &treatment.effects, seconds, 0.0);
ground = Some(if strength >= 1.0 {
treated
} else {
@@ -624,6 +628,7 @@ impl WgpuCompositor {
};
let geometry = layer.geometry(source, plan.width, plan.height);
let seconds = plan.seconds();
+ let clip_start = layer.clip_start.as_f64() as f32;
let (size, texture, uvs, flips) = if layer.needs_preparing(&geometry) {
let uploaded = self.upload(source);
let made = self.make_picture(
@@ -639,6 +644,7 @@ impl WgpuCompositor {
made,
&layer.effects,
seconds,
+ clip_start,
);
(
geometry.fitted,
@@ -655,6 +661,7 @@ impl WgpuCompositor {
index,
&layer.effects,
seconds,
+ clip_start,
);
}
(
@@ -1138,7 +1145,9 @@ impl WgpuCompositor {
});
let frame = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("concat pass frame"),
- size: 16,
+ // size(vec2), time, intensity, clip_time, padded to Frame's own
+ // 8-byte alignment (from its vec2 member): 20 bytes rounds to 24.
+ size: 24,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
@@ -1437,6 +1446,7 @@ impl WgpuCompositor {
source: usize,
passes: &[ShaderPass],
time: f32,
+ clip_start: f32,
) -> usize {
let mut current = source;
for pass in passes {
@@ -1447,7 +1457,14 @@ impl WgpuCompositor {
let shader = &self.shaders[&pass.key];
let lut_group = &self.luts[&lut_id];
let reveal_group = &self.reveals[&reveal_id];
- let frame_block: [f32; 4] = [width as f32, height as f32, time, pass.intensity];
+ let frame_block: [f32; 6] = [
+ width as f32,
+ height as f32,
+ time,
+ pass.intensity,
+ time - clip_start,
+ 0.0,
+ ];
let frame_bytes: Vec<u8> = frame_block.iter().flat_map(|v| v.to_le_bytes()).collect();
self.queue.write_buffer(&shader.frame, 0, &frame_bytes);
let mut params = pass.params.clone();
diff --git a/src/crates/concat-render/src/gpu/tests.rs b/src/crates/concat-render/src/gpu/tests.rs
index f9905607..58a148ea 100644
--- a/src/crates/concat-render/src/gpu/tests.rs
+++ b/src/crates/concat-render/src/gpu/tests.rs
@@ -476,6 +476,24 @@ fn every_shader_package_renders_at_its_defaults() {
}
}
+/// A pass reads how long its own clip has been on screen through
+/// `frame.clip_time` - the gap between the frame's own time and where the
+/// clip begins on the timeline, not the timeline's absolute clock. A
+/// clip starting at 2s, five seconds into the timeline, has been on
+/// screen for exactly three.
+#[test]
+fn a_pass_reads_its_layers_clip_relative_time() {
+ let Some(mut gpu) = gpu() else { return };
+ let body = "fn effect(uv: vec2<f32>) -> vec4<f32> { if (abs(frame.clip_time - 3.0) < 0.001) { return vec4<f32>(0.0, 1.0, 0.0, 1.0); } return vec4<f32>(1.0, 0.0, 0.0, 1.0); }";
+ let mut timed = layer(solid(4, 4, [0, 0, 0, 255]));
+ timed.clip_start = concat_core::time::Rational::approximate(2.0).expect("a rational");
+ timed.effects = vec![package("test.cliptime", body, "", &[], 1.0)];
+ let mut p = plan(4, 4, vec![timed]);
+ p.time = concat_core::time::Rational::approximate(5.0).expect("a rational");
+ let out = gpu.render(&p);
+ assert_eq!(&out.pixels()[..3], &[0, 255, 0], "clip_time should read 3.0");
+}
+
/// Every packaged transition's pipeline actually creates and runs on the
/// GPU, across its whole progress range - naga's validation at load
/// catches a broken shader's syntax and types, but only a real pipeline
diff --git a/src/crates/concat-render/src/plan.rs b/src/crates/concat-render/src/plan.rs
index a9740cff..b1bec277 100644
--- a/src/crates/concat-render/src/plan.rs
+++ b/src/crates/concat-render/src/plan.rs
@@ -180,6 +180,12 @@ impl Shading {
pub struct PlannedLayer {
/// Which clip produced this layer.
pub clip: ClipId,
+ /// Where this clip begins on the timeline - not the source's own time,
+ /// `source_time`, but where the clip itself sits. A shader effect reads
+ /// the gap between this and the frame's own time as `frame.clip_time`,
+ /// so a one-shot look can size itself to however long the clip has
+ /// been on screen instead of the timeline's absolute clock.
+ pub clip_start: Rational,
/// The track the layer sits on, bottom-most zero: every layer on a
/// lower track is under it, and a treatment names the track it sits
/// above.
@@ -232,6 +238,7 @@ impl PlannedLayer {
pub fn picture(clip: ClipId, frame: Arc<Frame>) -> PlannedLayer {
PlannedLayer {
clip,
+ clip_start: Rational::ZERO,
track: 0,
media: PathBuf::new(),
source_time: Rational::ZERO,
@@ -501,6 +508,7 @@ pub fn plan_frame(timeline: &Timeline, time: Rational) -> FramePlan {
layers.push(PlannedLayer {
clip: clip_id,
+ clip_start: clip.start,
track,
media: clip.media.path.clone(),
source_time,