diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 547e1e35..07546ef3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,8 +4,8 @@ on: branches: [ main ] # A v1.2.3 tag from scripts/release.sh publishes that version, see the publish job tags: [ "v*" ] + # Include stacked feature PRs so their visual changes are checked before retargeting. pull_request: - branches: [ main ] # GitHub evicts caches that were not used for 7 days, rebuild them before that # happens so the first PR after a quiet week does not rebuild Skia and FFmpeg. schedule: diff --git a/Cargo.lock b/Cargo.lock index 2b0fff32..1ddf6642 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -516,6 +516,16 @@ dependencies = [ "serde", ] +[[package]] +name = "captions-showcase-example" +version = "1.3.0" +dependencies = [ + "fframes", + "fframes_skia_renderer", + "serde", + "serde_json", +] + [[package]] name = "cargo-fframes" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index 54ae18a1..b7982a4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ members = [ "cargo-fframes", # Examples "examples/captions", + "examples/captions-showcase", "examples/podcast", "examples/podcast/editor/editor-bridge", "examples/audio-announce", diff --git a/README.md b/README.md index f220973e..f6c8f615 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,14 @@ A small example of Highlight, Karaoke, Reveal and Word modes sharing one JSON tr Caption presets and custom word highlights +#### [captions-showcase](examples/captions-showcase) + +Words in Orbit: a cinematic Moon speech with JSON-directed typography, handwritten accents, folding word planes, and liquid metal, fire, irradiance and nebula shaders. Run the standalone typography with `cargo run --release -p captions-showcase-example -- --shader --captions-only render`. Footage and audio stay outside Git; use `--media-dir /path/to/prepared-media` for the full film. CI checks six typography frames without downloading footage. + + + Words in Orbit: Moon caption shader showcase + + #### [audio-announce](examples/audio-announce) An automated workflow to create audio visualisation with automated subtitles. diff --git a/e2e/snapshots/captions-showcase/1.4s.png b/e2e/snapshots/captions-showcase/1.4s.png new file mode 100644 index 00000000..f55c5053 Binary files /dev/null and b/e2e/snapshots/captions-showcase/1.4s.png differ diff --git a/e2e/snapshots/captions-showcase/13.3s.png b/e2e/snapshots/captions-showcase/13.3s.png new file mode 100644 index 00000000..bb30c054 Binary files /dev/null and b/e2e/snapshots/captions-showcase/13.3s.png differ diff --git a/e2e/snapshots/captions-showcase/19s.png b/e2e/snapshots/captions-showcase/19s.png new file mode 100644 index 00000000..900e1e91 Binary files /dev/null and b/e2e/snapshots/captions-showcase/19s.png differ diff --git a/e2e/snapshots/captions-showcase/2.3s.png b/e2e/snapshots/captions-showcase/2.3s.png new file mode 100644 index 00000000..b7ad0b28 Binary files /dev/null and b/e2e/snapshots/captions-showcase/2.3s.png differ diff --git a/e2e/snapshots/captions-showcase/4.2s.png b/e2e/snapshots/captions-showcase/4.2s.png new file mode 100644 index 00000000..e47f245d Binary files /dev/null and b/e2e/snapshots/captions-showcase/4.2s.png differ diff --git a/e2e/snapshots/captions-showcase/6.3s.png b/e2e/snapshots/captions-showcase/6.3s.png new file mode 100644 index 00000000..3e467dd4 Binary files /dev/null and b/e2e/snapshots/captions-showcase/6.3s.png differ diff --git a/examples/_previews/captions-showcase.png b/examples/_previews/captions-showcase.png new file mode 100644 index 00000000..e9d959e9 Binary files /dev/null and b/examples/_previews/captions-showcase.png differ diff --git a/examples/captions-showcase/.gitignore b/examples/captions-showcase/.gitignore new file mode 100644 index 00000000..7c749ba0 --- /dev/null +++ b/examples/captions-showcase/.gitignore @@ -0,0 +1,3 @@ +media/*.mp4 +media/*.wav +*.mp4 diff --git a/examples/captions-showcase/Cargo.toml b/examples/captions-showcase/Cargo.toml new file mode 100644 index 00000000..f0ab5148 --- /dev/null +++ b/examples/captions-showcase/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "captions-showcase-example" +version = "1.3.0" +edition = "2024" +publish = false + +[dependencies] +fframes = { workspace = true, features = ["cli", "captions", "compile-time-svgtree"] } +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +[features] +default = ["skia"] +skia = ["dep:fframes_skia_renderer"] + +[target.'cfg(target_os = "macos")'.dependencies] +fframes_skia_renderer = { workspace = true, optional = true, features = ["cli", "metal"] } + +[target.'cfg(not(any(target_os = "macos", target_arch = "wasm32")))'.dependencies] +fframes_skia_renderer = { workspace = true, optional = true, features = ["cli", "vulkan"] } + +[target.'cfg(not(any(windows, target_arch = "wasm32")))'.dependencies] +fframes = { workspace = true, features = ["h264", "libav-agree-gpl", "build-portable"] } + +[lints] +workspace = true diff --git a/examples/captions-showcase/media/BarlowCondensed-SemiBold.ttf b/examples/captions-showcase/media/BarlowCondensed-SemiBold.ttf new file mode 100644 index 00000000..9df66576 Binary files /dev/null and b/examples/captions-showcase/media/BarlowCondensed-SemiBold.ttf differ diff --git a/examples/captions-showcase/media/CormorantGaramond-SemiBoldItalic.ttf b/examples/captions-showcase/media/CormorantGaramond-SemiBoldItalic.ttf new file mode 100644 index 00000000..d3816e07 Binary files /dev/null and b/examples/captions-showcase/media/CormorantGaramond-SemiBoldItalic.ttf differ diff --git a/examples/captions-showcase/media/DMSans-Medium.ttf b/examples/captions-showcase/media/DMSans-Medium.ttf new file mode 100644 index 00000000..fc8a5ebd Binary files /dev/null and b/examples/captions-showcase/media/DMSans-Medium.ttf differ diff --git a/examples/captions-showcase/media/IBMPlexMono-Regular.ttf b/examples/captions-showcase/media/IBMPlexMono-Regular.ttf new file mode 100644 index 00000000..0c9770d5 Binary files /dev/null and b/examples/captions-showcase/media/IBMPlexMono-Regular.ttf differ diff --git a/examples/captions-showcase/media/moon.json b/examples/captions-showcase/media/moon.json new file mode 100644 index 00000000..c99d388e --- /dev/null +++ b/examples/captions-showcase/media/moon.json @@ -0,0 +1,1663 @@ +{ + "cues": [ + { + "id": "handwritten-commitment", + "speaker": "John F. Kennedy", + "start": 0.14, + "end": 1.72, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04}}, + "words": [ + { + "text": "WE", + "start": 0.14, + "end": 0.5, + "id": "w01", + "metadata": {"layout":{"font_size":120,"center_x":532,"baseline":400},"motion":{"enter_seconds":0.14}} + }, + { + "text": "CHOOSE", + "start": 0.5, + "end": 0.9, + "id": "w02", + "metadata": { + "layout": {"font_size":250,"center_x":971,"baseline":400}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "accents": [{"kind":"circle","color":"#ef6847","start":0.53,"duration":0.36,"width":7,"padding":16,"seed":254}] + } + }, + { + "text": "TO", + "start": 0.9, + "end": 1.26, + "id": "w03", + "metadata": {"layout":{"font_size":120,"center_x":1399,"baseline":400},"motion":{"enter_seconds":0.14}} + }, + { + "text": "GO", + "start": 1.26, + "end": 1.44, + "id": "w04", + "metadata": { + "layout": {"font_size":360,"center_x":781,"baseline":800}, + "motion": {"entrance":"stamp"}, + "accents": [{"kind":"double-underline","color":"#ef6847","start":1.3,"duration":0.22,"width":5,"padding":12,"seed":508}] + } + }, + { + "text": "TO", + "start": 1.44, + "end": 1.6, + "id": "w05", + "metadata": {"layout":{"font_size":130,"center_x":1039,"baseline":800},"motion":{"enter_seconds":0.14}} + }, + { + "text": "THE", + "start": 1.6, + "end": 1.72, + "id": "w06", + "metadata": {"layout":{"font_size":130,"center_x":1218,"baseline":800},"motion":{"enter_seconds":0.14}} + } + ] + }, + { + "id": "cosmic-destination", + "speaker": "John F. Kennedy", + "start": 1.72, + "end": 2.9, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04}}, + "words": [ + { + "text": "MOON", + "start": 1.72, + "end": 2.02, + "id": "w07", + "metadata": { + "layout": {"font_size":550,"center_x":960,"baseline":745}, + "motion": {"entrance":"stamp","enter_seconds":0.36}, + "material": { + "effect": "glass-nebula", + "variation": 0.211, + "hold_until": 2.9, + "glass_nebula": { + "density": 0.8, + "cavity": 0.28, + "dust": 0.45, + "gas_scale": 0.9, + "billow": 0.8, + "glow": 0.2, + "seed": 3, + "stars": 0.3, + "speed": 2, + "environment": 0.2, + "highlight": 0.4, + "optical_depth": 0.65 + } + } + } + }, + { + "text": "IN", + "start": 2.02, + "end": 2.28, + "id": "w08", + "metadata": {"layout":{"font_size":95,"center_x":650,"baseline":945},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "THIS", + "start": 2.28, + "end": 2.5, + "id": "w09", + "metadata": {"layout":{"font_size":95,"center_x":790,"baseline":945},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "DECADE", + "start": 2.5, + "end": 2.9, + "id": "w10", + "metadata": {"layout":{"font_size":150,"center_x":1101,"baseline":945},"motion":{"entrance":"none","enter_seconds":0.14}} + } + ] + }, + { + "id": "glyph-relay", + "speaker": "John F. Kennedy", + "start": 2.9, + "end": 4.56, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04,"graphics":[]}}, + "words": [ + { + "text": "AND", + "start": 2.9, + "end": 3.22, + "id": "w11", + "metadata": {"layout":{"font_size":110,"center_x":320,"baseline":430},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "DO", + "start": 3.22, + "end": 3.38, + "id": "w12", + "metadata": { + "layout": {"font_size":400,"center_x":650,"baseline":430}, + "appearance": {"fill":"#efcbb0"}, + "motion": { + "entrance": "choreograph", + "start": 3.16, + "enter_seconds": 0.4, + "choreography": [ + { + "index": 0, + "delay": 0, + "duration": 0.34, + "from": {"x":-55,"y":30,"rotate":-3,"scale":0.98,"opacity":0.85}, + "easing": [0.22,0.75,0.24,1], + "control1": [-50,8], + "control2": [-8,0] + }, + { + "index": 1, + "delay": 0.018, + "duration": 0.34, + "from": {"x":-55,"y":30,"rotate":-3,"scale":0.98,"opacity":0.85}, + "easing": [0.22,0.75,0.24,1], + "control1": [-50,8], + "control2": [-8,0] + } + ] + }, + "replicas": [ + { + "offset": [-82,50], + "start": 3.16, + "duration": 0.5, + "collapse": true, + "color": "#9bbfcb", + "opacity": 0.68, + "stroke_width": 2.6, + "scale": 1, + "rotate": 0, + "hold_seconds": 0.18, + "fill": "#173e50" + }, + { + "offset": [-41,25], + "start": 3.195, + "duration": 0.47, + "collapse": true, + "color": "#dcac77", + "opacity": 0.88, + "stroke_width": 2.8, + "scale": 1, + "rotate": 0, + "hold_seconds": 0.18, + "fill": "#9d4d35" + } + ] + } + }, + { + "text": "THE", + "start": 3.38, + "end": 3.56, + "id": "w13", + "metadata": {"layout":{"font_size":100,"center_x":380,"baseline":730},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "OTHER", + "start": 3.56, + "end": 3.78, + "id": "w14", + "metadata": { + "layout": {"font_size":260,"center_x":890,"baseline":730}, + "motion": { + "entrance": "choreograph", + "start": 3.5, + "enter_seconds": 0.4, + "choreography": [ + { + "index": 0, + "delay": 0, + "duration": 0.42, + "from": {"x":-66,"y":34,"rotate":-2,"scale":1,"opacity":0.85}, + "easing": [0.22,0.75,0.24,1], + "control1": [-53,18], + "control2": [-12,0] + }, + { + "index": 1, + "delay": 0.012, + "duration": 0.42, + "from": {"x":-66,"y":34,"rotate":-2,"scale":1,"opacity":0.85}, + "easing": [0.22,0.75,0.24,1], + "control1": [-53,18], + "control2": [-12,0] + }, + { + "index": 2, + "delay": 0.024, + "duration": 0.42, + "from": {"x":-66,"y":34,"rotate":-2,"scale":1,"opacity":0.85}, + "easing": [0.22,0.75,0.24,1], + "control1": [-53,18], + "control2": [-12,0] + }, + { + "index": 3, + "delay": 0.036, + "duration": 0.42, + "from": {"x":-66,"y":34,"rotate":-2,"scale":1,"opacity":0.85}, + "easing": [0.22,0.75,0.24,1], + "control1": [-53,18], + "control2": [-12,0] + }, + { + "index": 4, + "delay": 0.048, + "duration": 0.42, + "from": {"x":-66,"y":34,"rotate":-2,"scale":1,"opacity":0.85}, + "easing": [0.22,0.75,0.24,1], + "control1": [-53,18], + "control2": [-12,0] + } + ] + }, + "replicas": [ + { + "offset": [-82,50], + "start": 3.5, + "duration": 0.5, + "collapse": true, + "color": "#9bbfcb", + "opacity": 0.68, + "stroke_width": 2.6, + "scale": 1, + "rotate": 0, + "hold_seconds": 0.18, + "fill": "#173e50" + }, + { + "offset": [-41,25], + "start": 3.535, + "duration": 0.47, + "collapse": true, + "color": "#dcac77", + "opacity": 0.88, + "stroke_width": 2.8, + "scale": 1, + "rotate": 0, + "hold_seconds": 0.18, + "fill": "#9d4d35" + } + ] + } + }, + { + "text": "THINGS", + "start": 3.78, + "end": 4.08, + "id": "w15", + "metadata": { + "layout": {"font_size":280,"center_x":1280,"baseline":980}, + "motion": {"start":3.78}, + "material": { + "effect": "goo", + "variation": 0.3, + "hold_until": 4.56, + "exit_seconds": 0.12, + "goo": { + "goo_color": "#41c879", + "containment": 0.82, + "blob_scale": 0.33, + "spread": 0.85, + "merge": 0.85, + "threshold": 0.45, + "bulge": 1.15, + "wetness": 2.5, + "speed": 2, + "wobble": 0.8, + "breathe": 0.55, + "ambient": 0.2, + "light_angle": 240 + } + } + } + } + ] + }, + { + "id": "liquid-ease", + "speaker": "John F. Kennedy", + "start": 4.64, + "end": 6.56, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04,"graphics":[]}}, + "words": [ + { + "text": "NOT", + "start": 4.64, + "end": 4.9, + "id": "w16", + "metadata": {"layout":{"font_size":210,"center_x":756,"baseline":320},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "BECAUSE", + "start": 4.9, + "end": 5.32, + "id": "w17", + "metadata": {"layout":{"font_size":120,"center_x":1125,"baseline":320},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "THEY", + "start": 5.32, + "end": 5.68, + "id": "w18", + "metadata": {"layout":{"font_size":110,"center_x":571,"baseline":500},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "ARE", + "start": 5.68, + "end": 5.86, + "id": "w19", + "metadata": {"layout":{"font_size":110,"center_x":776,"baseline":500},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "EASY", + "start": 5.86, + "end": 6.1, + "id": "w20", + "metadata": { + "layout": {"font_size":520,"center_x":1080,"baseline":900}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "material": { + "effect": "liquid-metal", + "variation": 0.32, + "exit_seconds": 0.2, + "hold_until": 6.56, + "liquid": { + "turbulence": 1.35, + "ripple": 1.6, + "warp": 1.25, + "speed": 4, + "sharpness": 0.62, + "environment": 1.3, + "env_rotation": 18, + "dispersion": 0.15, + "bevel_width": 0.04, + "dark_color": "#263443" + } + } + } + } + ] + }, + { + "id": "combustion", + "speaker": "John F. Kennedy", + "start": 6.56, + "end": 8.6, + "metadata": { + "editorial": { + "enter_seconds": 0.14, + "exit_seconds": 0.04, + "graphics": [ + { + "kind": "path", + "points": [[650,953],[650,967]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.58, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[712,953],[712,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.588, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[774,953],[774,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.596, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[836,953],[836,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.604, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[898,953],[898,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.612, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[960,953],[960,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.62, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[1022,953],[1022,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.628, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[1084,953],[1084,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.636, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[1146,953],[1146,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.644, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[1208,953],[1208,960]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.652, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + }, + { + "kind": "path", + "points": [[1270,953],[1270,967]], + "cubic": false, + "color": "#c29876", + "width": 2.2, + "start": 7.66, + "duration": 0.18, + "opacity": 0.77, + "keyline": true + } + ] + } + }, + "words": [ + { + "text": "BUT", + "start": 6.56, + "end": 6.66, + "id": "w21", + "metadata": {"layout":{"font_size":100,"center_x":724,"baseline":260},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "BECAUSE", + "start": 6.66, + "end": 7.1, + "id": "w22", + "metadata": {"layout":{"font_size":140,"center_x":1045,"baseline":260},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "THEY", + "start": 7.1, + "end": 7.34, + "id": "w23", + "metadata": {"layout":{"font_size":110,"center_x":1361,"baseline":430},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "ARE", + "start": 7.34, + "end": 7.48, + "id": "w24", + "metadata": {"layout":{"font_size":110,"center_x":1566,"baseline":430},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "HARD", + "start": 7.48, + "end": 7.78, + "id": "w25", + "metadata": { + "layout": {"font_size":530,"center_x":960,"baseline":880}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "material": {"effect":"combustion","variation":0.325,"warm":true,"exit_seconds":0.2,"hold_until":8.6} + } + } + ] + }, + { + "id": "drafted-purpose", + "speaker": "John F. Kennedy", + "start": 8.66, + "end": 11.1, + "metadata": { + "editorial": { + "enter_seconds": 0.14, + "exit_seconds": 0.04, + "graphics": [ + { + "kind": "path", + "points": [[180,390],[400,370],[550,430],[859.5,430]], + "cubic": true, + "color": "#e8c7a0", + "width": 3.3, + "start": 8.93, + "duration": 0.45, + "opacity": 0.77, + "arrow": false, + "hold_until": 9.95, + "fade_out": 0.6, + "keyline": true, + "trace": {"head_radius":5.5,"tail":0.18,"linger":0.16,"color":"#fff1d7","construction":false,"arrival_ring":false} + }, + { + "kind": "path", + "points": [[180,472],[400,418],[550,430],[859.5,430]], + "cubic": true, + "color": "#e8c7a0", + "width": 3.3, + "start": 8.97, + "duration": 0.41, + "opacity": 0.77, + "arrow": false, + "hold_until": 9.95, + "fade_out": 0.6, + "keyline": true, + "trace": {"head_radius":5.5,"tail":0.18,"linger":0.16,"color":"#fff1d7","construction":false,"arrival_ring":false} + }, + { + "kind": "path", + "points": [[180,554],[400,466],[550,430],[859.5,430]], + "cubic": true, + "color": "#e8c7a0", + "width": 3.3, + "start": 9.01, + "duration": 0.37, + "opacity": 0.77, + "arrow": false, + "hold_until": 9.95, + "fade_out": 0.6, + "keyline": true, + "trace": {"head_radius":5.5,"tail":0.18,"linger":0.16,"color":"#fff1d7","construction":false,"arrival_ring":false} + }, + { + "kind": "path", + "points": [[180,636],[400,514],[550,430],[859.5,430]], + "cubic": true, + "color": "#e8c7a0", + "width": 3.3, + "start": 9.05, + "duration": 0.33, + "opacity": 0.77, + "arrow": false, + "hold_until": 9.95, + "fade_out": 0.6, + "keyline": true, + "trace": {"head_radius":5.5,"tail":0.18,"linger":0.16,"color":"#fff1d7","construction":false,"arrival_ring":false} + }, + { + "kind": "path", + "points": [[1521.5,442],[1780,440],[1780,720],[1584.5,785]], + "cubic": true, + "color": "#edcda1", + "width": 3.8, + "start": 9.84, + "duration": 0.94, + "opacity": 0.85, + "arrow": false, + "keyline": true, + "trace": { + "head_radius": 10, + "tail": 0.24, + "linger": 0.18, + "color": "#ffedd0", + "construction": true, + "construction_opacity": 0.32, + "arrival_ring": true + } + } + ] + } + }, + "words": [ + { + "text": "BECAUSE", + "start": 8.66, + "end": 8.96, + "id": "w26", + "metadata": {"layout":{"font_size":100,"center_x":534,"baseline":280},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "THAT", + "start": 8.96, + "end": 9.38, + "id": "w27", + "metadata": {"layout":{"font_size":110,"center_x":823,"baseline":280},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "GOAL", + "start": 9.38, + "end": 9.68, + "id": "w28", + "metadata": { + "layout": {"font_size":340,"center_x":1190,"baseline":560}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "accents": [{"kind":"corner-frame","color":"#b8dbea","start":9.38,"duration":0.32,"width":2,"padding":17,"seed":3556}], + "material": {"effect":"contour","variation":0.844,"exit_seconds":0.2,"hold_until":11.1} + } + }, + { + "text": "WILL", + "start": 9.68, + "end": 10.78, + "id": "w29", + "metadata": {"layout":{"font_size":180,"center_x":430,"baseline":800},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "SERVE", + "start": 10.78, + "end": 11.1, + "id": "w30", + "metadata": { + "layout": {"font_size":320,"center_x":1210,"baseline":890}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "material": {"effect":"contour","variation":0.78,"exit_seconds":0.2,"hold_until":11.1} + } + } + ] + }, + { + "id": "mechanical-assembly", + "speaker": "John F. Kennedy", + "start": 11.1, + "end": 12.5, + "metadata": { + "editorial": { + "enter_seconds": 0.14, + "exit_seconds": 0.04, + "graphics": [ + { + "kind": "measure", + "from": [590.5,457], + "to": [1441.5,457], + "offset": 60, + "label": "851 px", + "font_size": 33, + "color": "#bed5dd", + "width": 3, + "start": 11.3, + "duration": 0.72, + "opacity": 0.93, + "keyline": true, + "caliper": {"from_extra":190,"moving_end":"to","lock_fraction":0.86,"jaw_size":34,"tick_count":7,"unit":"px","precision":0} + }, + { + "kind": "measure", + "from": [703.5,807], + "to": [1444.5,807], + "offset": 60, + "label": "741 px", + "font_size": 33, + "color": "#bed5dd", + "width": 3, + "start": 11.93, + "duration": 0.41, + "opacity": 0.93, + "keyline": true, + "caliper": {"from_extra":145,"moving_end":"from","lock_fraction":0.86,"jaw_size":34,"tick_count":7,"unit":"px","precision":0} + }, + { + "kind": "label", + "text": "1969·07·20", + "x": 1444.5, + "y": 924, + "start": 12.06, + "duration": 0.15, + "font_size": 19, + "font_family": "IBM Plex Mono", + "font_weight": 400, + "color": "#bfd2d8", + "opacity": 0.63, + "align": "end" + } + ] + } + }, + "words": [ + { + "text": "TO", + "start": 11.1, + "end": 11.4, + "id": "w31", + "metadata": {"layout":{"font_size":110,"center_x":508,"baseline":450},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "ORGANIZE", + "start": 11.4, + "end": 11.8, + "id": "w32", + "metadata": { + "layout": {"font_size":250,"center_x":1016,"baseline":450}, + "motion": { + "entrance": "choreograph", + "start": 11.24, + "enter_seconds": 0.4, + "choreography": [ + { + "index": 0, + "delay": 0, + "duration": 0.57, + "from": {"x":-48,"y":-78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [-44,-78], + "control2": [0,-15] + }, + { + "index": 1, + "delay": 0.015, + "duration": 0.57, + "from": {"x":-48,"y":-78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [-44,-78], + "control2": [0,-15] + }, + { + "index": 2, + "delay": 0.03, + "duration": 0.57, + "from": {"x":-48,"y":-78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [-44,-78], + "control2": [0,-15] + }, + { + "index": 3, + "delay": 0.045, + "duration": 0.57, + "from": {"x":-48,"y":-78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [-44,-78], + "control2": [0,-15] + }, + { + "index": 4, + "delay": 0.06, + "duration": 0.57, + "from": {"x":48,"y":78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [44,78], + "control2": [0,15] + }, + { + "index": 5, + "delay": 0.075, + "duration": 0.57, + "from": {"x":48,"y":78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [44,78], + "control2": [0,15] + }, + { + "index": 6, + "delay": 0.09, + "duration": 0.57, + "from": {"x":48,"y":78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [44,78], + "control2": [0,15] + }, + { + "index": 7, + "delay": 0.105, + "duration": 0.57, + "from": {"x":48,"y":78,"rotate":0,"scale":1,"opacity":0.84}, + "easing": [0.48,0.02,0.42,1], + "control1": [44,78], + "control2": [0,15] + } + ] + }, + "replicas": [ + { + "offset": [0,0], + "start": 11.24, + "duration": 0.67, + "collapse": true, + "color": "#95aeb9", + "opacity": 0.32, + "stroke_width": 1.5, + "scale": 1, + "rotate": 0 + } + ] + } + }, + { + "text": "AND", + "start": 11.8, + "end": 12.32, + "id": "w33", + "metadata": {"layout":{"font_size":110,"center_x":593,"baseline":800},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "MEASURE", + "start": 12.32, + "end": 12.5, + "id": "w34", + "metadata": { + "layout": {"font_size":230,"center_x":1074,"baseline":800}, + "motion": { + "entrance": "choreograph", + "start": 11.93, + "enter_seconds": 0.4, + "choreography": [ + { + "index": 0, + "delay": 0, + "duration": 0.3, + "from": {"x":52,"y":0,"rotate":0,"scale":1,"opacity":0.5}, + "easing": [0.2,0.7,0.2,1], + "control1": [45,0], + "control2": [4,0] + }, + { + "index": 1, + "delay": 0.012, + "duration": 0.3, + "from": {"x":46,"y":0,"rotate":0,"scale":1,"opacity":0.5}, + "easing": [0.2,0.7,0.2,1], + "control1": [40,0], + "control2": [4,0] + }, + { + "index": 2, + "delay": 0.024, + "duration": 0.3, + "from": {"x":40,"y":0,"rotate":0,"scale":1,"opacity":0.5}, + "easing": [0.2,0.7,0.2,1], + "control1": [35,0], + "control2": [4,0] + }, + { + "index": 3, + "delay": 0.036, + "duration": 0.3, + "from": {"x":34,"y":0,"rotate":0,"scale":1,"opacity":0.5}, + "easing": [0.2,0.7,0.2,1], + "control1": [30,0], + "control2": [4,0] + }, + { + "index": 4, + "delay": 0.048, + "duration": 0.3, + "from": {"x":28,"y":0,"rotate":0,"scale":1,"opacity":0.5}, + "easing": [0.2,0.7,0.2,1], + "control1": [25,0], + "control2": [4,0] + }, + { + "index": 5, + "delay": 0.06, + "duration": 0.3, + "from": {"x":22,"y":0,"rotate":0,"scale":1,"opacity":0.5}, + "easing": [0.2,0.7,0.2,1], + "control1": [20,0], + "control2": [4,0] + }, + { + "index": 6, + "delay": 0.072, + "duration": 0.3, + "from": {"x":16,"y":0,"rotate":0,"scale":1,"opacity":0.5}, + "easing": [0.2,0.7,0.2,1], + "control1": [15,0], + "control2": [4,0] + } + ] + }, + "replicas": [ + { + "offset": [0,0], + "start": 11.93, + "duration": 0.37, + "collapse": true, + "color": "#95bed2", + "opacity": 0.34, + "stroke_width": 1, + "scale": 1, + "rotate": 0 + } + ] + } + } + ] + }, + { + "id": "optical-energy", + "speaker": "John F. Kennedy", + "start": 12.5, + "end": 15.58, + "metadata": { + "editorial": { + "enter_seconds": 0.14, + "exit_seconds": 0.04, + "graphics": [ + { + "kind": "path", + "points": [[1076.5,215],[1270,210],[1510,410],[1568.5,433.6]], + "cubic": true, + "color": "#a1d4db", + "width": 2.2, + "start": 13.2, + "duration": 0.82, + "opacity": 0.672, + "flow": {"count":2,"speed":0.46,"phase":0,"radius":2.8,"tail":0.1125}, + "keyline": true + }, + { + "kind": "path", + "points": [[1681.5,560],[1805,650],[1750,810],[1719,855]], + "cubic": true, + "color": "#c7dbdf", + "width": 2.2, + "start": 14.36, + "duration": 0.47, + "opacity": 0.77, + "flow": {"count":1,"speed":0.7,"phase":0,"radius":3.36,"tail":0.125}, + "keyline": true + }, + { + "kind": "path", + "points": [[1076.5,235],[1270,240],[1510,425],[1568.5,443.6]], + "cubic": true, + "color": "#a1d4db", + "width": 2.79, + "start": 13.255, + "duration": 0.82, + "opacity": 0.672, + "flow": {"count":2,"speed":0.46,"phase":0.12,"radius":4.2,"tail":0.1125}, + "keyline": true + }, + { + "kind": "path", + "points": [[1681.5,578],[1805,664],[1750,820],[1719,865]], + "cubic": true, + "color": "#c7dbdf", + "width": 2.2, + "start": 14.395, + "duration": 0.47, + "opacity": 0.77, + "flow": {"count":1,"speed":0.7,"phase":0.2,"radius":3.36,"tail":0.125}, + "keyline": true + }, + { + "kind": "path", + "points": [[1076.5,255],[1270,270],[1510,440],[1568.5,453.6]], + "cubic": true, + "color": "#a1d4db", + "width": 2.2, + "start": 13.31, + "duration": 0.82, + "opacity": 0.672, + "flow": {"count":2,"speed":0.46,"phase":0.24,"radius":2.8,"tail":0.1125}, + "keyline": true + }, + { + "kind": "path", + "points": [[1681.5,596],[1805,678],[1750,830],[1719,875]], + "cubic": true, + "color": "#c7dbdf", + "width": 2.2, + "start": 14.43, + "duration": 0.47, + "opacity": 0.77, + "flow": {"count":1,"speed":0.7,"phase":0.4,"radius":3.36,"tail":0.125}, + "keyline": true + } + ] + } + }, + "words": [ + { + "text": "THE", + "start": 12.5, + "end": 12.86, + "id": "w35", + "metadata": {"layout":{"font_size":90,"center_x":381,"baseline":360},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "BEST", + "start": 12.86, + "end": 13.16, + "id": "w36", + "metadata": { + "layout": {"font_size":330,"center_x":767,"baseline":360}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "material": { + "effect": "irradiance", + "variation": 0.14, + "exit_seconds": 0.2, + "hold_until": 15.58, + "rest_at": 13.65, + "rest_seconds": 0.24, + "irradiance": { + "body_color": "#71859b", + "body_light": 1, + "core": 1.8, + "speed": 2, + "orbit": 0.25, + "lights": [{"position":[0.12,-0.25],"color":"#ffb347","intensity":5},{"position":[0.9,1.25],"color":"#7a5cff","intensity":4}] + } + } + } + }, + { + "text": "OF", + "start": 13.16, + "end": 14.02, + "id": "w37", + "metadata": {"layout":{"font_size":90,"center_x":474,"baseline":650},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "OUR", + "start": 14.02, + "end": 14.18, + "id": "w38", + "metadata": {"layout":{"font_size":100,"center_x":615,"baseline":650},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "ENERGIES", + "start": 14.18, + "end": 14.5, + "id": "w39", + "metadata": { + "layout": {"font_size":280,"center_x":1191,"baseline":650}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "material": { + "effect": "photonic", + "variation": 0.61, + "exit_seconds": 0.2, + "hold_until": 15.58, + "rest_at": 14.96, + "rest_seconds": 0.34, + "relief": {"depth":1,"detail_scale":1,"speed":1,"light":1} + } + } + }, + { + "text": "AND", + "start": 14.5, + "end": 14.78, + "id": "w40", + "metadata": {"layout":{"font_size":90,"center_x":993,"baseline":930},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "SKILLS", + "start": 14.78, + "end": 15.1, + "id": "w41", + "metadata": { + "layout": {"font_size":260,"center_x":1400,"baseline":930}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "material": {"effect":"lattice","variation":0.45,"exit_seconds":0.2,"hold_until":15.58} + } + } + ] + }, + { + "id": "aperture", + "speaker": "John F. Kennedy", + "start": 15.74, + "end": 17.14, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04,"graphics":[]}}, + "words": [ + { + "text": "BECAUSE", + "start": 15.74, + "end": 16.3, + "id": "w42", + "metadata": {"layout":{"font_size":110,"center_x":414,"baseline":320},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "THAT", + "start": 16.3, + "end": 16.64, + "id": "w43", + "metadata": {"layout":{"font_size":110,"center_x":719,"baseline":320},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "CHALLENGE", + "start": 16.64, + "end": 17.14, + "id": "w44", + "metadata": { + "layout": {"font_size":300,"center_x":960,"baseline":800}, + "motion": {"entrance":"none","start":16.28,"enter_seconds":0.14}, + "replicas": [ + { + "offset": [22,-14], + "start": 16.28, + "duration": 0.68, + "collapse": true, + "color": "#99bac9", + "opacity": 0.26, + "stroke_width": 1.2, + "scale": 1.015, + "rotate": 0 + } + ], + "plane": { + "start": 16.28, + "duration": 0.72, + "corners": [[-0.1,0.24],[0.075,-0.34],[0.025,0.17],[-0.055,-0.04]], + "easing": [0.4,0.03,0.35,1], + "wire_color": "#c5dde5", + "wire_width": 2.5, + "wire_opacity": 0.7, + "handles": false, + "wire_fade": 0.12 + } + } + } + ] + }, + { + "id": "spatial-procession", + "speaker": "John F. Kennedy", + "start": 17.14, + "end": 18.48, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04,"graphics":[]}}, + "words": [ + { + "text": "IS", + "start": 17.14, + "end": 17.42, + "id": "w45", + "metadata": { + "layout": {"font_size":120,"center_x":330,"baseline":350}, + "appearance": {"fill":"#dcebf1"}, + "motion": {"entrance":"none","enter_seconds":0.14} + } + }, + { + "text": "ONE", + "start": 17.42, + "end": 17.7, + "id": "w46", + "metadata": { + "layout": {"font_size":290,"center_x":730,"baseline":510}, + "appearance": {"fill":"#dcebf1"}, + "motion": {"entrance":"none","enter_seconds":0.14} + } + }, + { + "text": "THAT", + "start": 17.7, + "end": 17.96, + "id": "w47", + "metadata": { + "layout": {"font_size":100,"center_x":560,"baseline":710}, + "appearance": {"fill":"#dcebf1"}, + "motion": {"entrance":"none","enter_seconds":0.14} + } + }, + { + "text": "WE'RE", + "start": 17.96, + "end": 18.2, + "id": "w48", + "metadata": { + "layout": {"font_size":150,"center_x":930,"baseline":660}, + "appearance": {"fill":"#dcebf1"}, + "motion": {"entrance":"none","enter_seconds":0.14} + } + }, + { + "text": "WILLING", + "start": 18.2, + "end": 18.48, + "id": "w49", + "metadata": { + "layout": {"font_size":260,"center_x":1350,"baseline":900}, + "appearance": {"fill":"#dcebf1"}, + "motion": { + "entrance": "choreograph", + "start": 17.96, + "enter_seconds": 0.4, + "choreography": [ + { + "index": 0, + "delay": 0, + "duration": 0.32, + "from": {"x":72.49,"y":-9.1,"rotate":0,"scale":1,"scale_x":0.42,"scale_y":1.1,"opacity":1}, + "easing": [0.32,0.02,0.22,1], + "control1": [47.8434,-6.1], + "control2": [15.9478,-2] + }, + { + "index": 1, + "delay": 0.018, + "duration": 0.32, + "from": {"x":17.53,"y":-9.1,"rotate":0,"scale":1,"scale_x":0.48,"scale_y":1.1,"opacity":1}, + "easing": [0.32,0.02,0.22,1], + "control1": [11.5698,-6.1], + "control2": [3.8566,-2] + }, + { + "index": 2, + "delay": 0.036, + "duration": 0.32, + "from": {"x":-12.16,"y":-9.1,"rotate":0,"scale":1,"scale_x":0.54,"scale_y":1.1,"opacity":1}, + "easing": [0.32,0.02,0.22,1], + "control1": [-8.0256,-6.1], + "control2": [-2.6752,-2] + }, + { + "index": 3, + "delay": 0.054, + "duration": 0.32, + "from": {"x":-48.17,"y":-9.1,"rotate":0,"scale":1,"scale_x":0.6,"scale_y":1.1,"opacity":1}, + "easing": [0.32,0.02,0.22,1], + "control1": [-31.7922,-6.1], + "control2": [-10.5974,-2] + }, + { + "index": 4, + "delay": 0.072, + "duration": 0.32, + "from": {"x":-69.43,"y":-9.1,"rotate":0,"scale":1,"scale_x":0.66,"scale_y":1.1,"opacity":1}, + "easing": [0.32,0.02,0.22,1], + "control1": [-45.8238,-6.1], + "control2": [-15.2746,-2] + }, + { + "index": 5, + "delay": 0.09, + "duration": 0.32, + "from": {"x":-87.63,"y":-9.1,"rotate":0,"scale":1,"scale_x":0.72,"scale_y":1.1,"opacity":1}, + "easing": [0.32,0.02,0.22,1], + "control1": [-57.8358,-6.1], + "control2": [-19.2786,-2] + }, + { + "index": 6, + "delay": 0.108, + "duration": 0.32, + "from": {"x":-109.17,"y":-9.1,"rotate":0,"scale":1,"scale_x":0.78,"scale_y":1.1,"opacity":1}, + "easing": [0.32,0.02,0.22,1], + "control1": [-72.0522,-6.1], + "control2": [-24.0174,-2] + } + ] + } + } + } + ] + }, + { + "id": "printed-consent", + "speaker": "John F. Kennedy", + "start": 18.48, + "end": 19.54, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04,"graphics":[]}}, + "words": [ + { + "text": "TO", + "start": 18.48, + "end": 18.66, + "id": "w50", + "metadata": {"layout":{"font_size":110,"center_x":330,"baseline":570},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "ACCEPT", + "start": 18.66, + "end": 19.06, + "id": "w51", + "metadata": { + "layout": {"font_size":320,"center_x":1080,"baseline":745,"font_family":"DM Sans","font_weight":500}, + "appearance": {"fill":"#edf0e7","outline":"#101a20","outline_width":1.4,"background_padding":32}, + "motion": {"entrance":"none","start":18.52,"enter_seconds":0.1}, + "plane": { + "start": 18.52, + "duration": 0.6, + "panels": [ + {"range":[0,0.5],"corners":[[0.095,0.25],[0,0],[0,0],[0.095,-0.25]],"shade":0.68}, + {"range":[0.5,1],"corners":[[0,0],[-0.095,0.25],[-0.095,-0.25],[0,0]],"shade":0.96} + ], + "wire_color": "#d3a76a", + "wire_width": 2.8, + "wire_opacity": 0.55, + "wire_fade": 0.12, + "easing": [0.5,0.02,0.35,1] + } + } + } + ] + }, + { + "id": "digital-refusal", + "speaker": "John F. Kennedy", + "start": 19.78, + "end": 22.4, + "metadata": { + "editorial": { + "enter_seconds": 0.14, + "exit_seconds": 0.37, + "graphics": [ + { + "kind": "path", + "points": [[1696.5,850],[1790,850],[1790,470],[1533,470]], + "cubic": true, + "color": "#8ec3cb", + "width": 2.2, + "start": 21.58, + "duration": 0.37, + "opacity": 0.742, + "arrow": true, + "hold_until": 22.05, + "fade_out": 0.3, + "flow": {"count":1,"speed":1.1,"phase":0,"radius":3.64,"tail":0.15}, + "keyline": true + }, + { + "kind": "label", + "text": "1202", + "x": 1748, + "y": 760, + "start": 21.6, + "duration": 0.08, + "font_size": 22, + "font_family": "IBM Plex Mono", + "font_weight": 400, + "color": "#bfd2d8", + "opacity": 0.7, + "align": "end", + "hold_until": 21.92, + "fade_out": 0.12 + }, + { + "kind": "label", + "text": "GO", + "x": 1748, + "y": 760, + "start": 22.02, + "duration": 0.07, + "font_size": 22, + "font_family": "IBM Plex Mono", + "font_weight": 400, + "color": "#d6e5d9", + "opacity": 0.7, + "align": "end", + "hold_until": 22.24, + "fade_out": 0.12 + } + ] + } + }, + "words": [ + { + "text": "ONE", + "start": 19.78, + "end": 20.1, + "id": "w52", + "metadata": {"layout":{"font_size":100,"center_x":497,"baseline":300},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "WE", + "start": 20.1, + "end": 20.34, + "id": "w53", + "metadata": {"layout":{"font_size":100,"center_x":653,"baseline":300},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "ARE", + "start": 20.34, + "end": 20.56, + "id": "w54", + "metadata": {"layout":{"font_size":100,"center_x":806,"baseline":300},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "UNWILLING", + "start": 20.56, + "end": 20.86, + "id": "w55", + "metadata": { + "layout": {"font_size":280,"center_x":960,"baseline":570}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "accents": [{"kind":"rule","color":"#a7d9d6","start":20.56,"duration":0.28,"width":2.5,"padding":15,"seed":6985}] + } + }, + { + "text": "TO", + "start": 20.86, + "end": 21.24, + "id": "w56", + "metadata": {"layout":{"font_size":100,"center_x":400,"baseline":880},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "POSTPONE", + "start": 21.24, + "end": 21.58, + "id": "w57", + "metadata": { + "layout": {"font_size":290,"center_x":1150,"baseline":900}, + "motion": {"entrance":"none","enter_seconds":0.14}, + "replicas": [ + { + "offset": [56,-30], + "start": 21.24, + "duration": 0.5, + "collapse": true, + "color": "#c1e1df", + "opacity": 0.62, + "stroke_width": 3, + "scale": 1, + "rotate": 0, + "hold_seconds": 0.24, + "fill": "#153947" + }, + { + "offset": [112,-60], + "start": 21.31, + "duration": 0.45, + "collapse": true, + "color": "#87bcc5", + "opacity": 0.45, + "stroke_width": 3, + "scale": 1, + "rotate": 0, + "hold_seconds": 0.24, + "fill": "#153947" + } + ] + } + } + ] + }, + { + "id": "tracking-dock", + "speaker": "John F. Kennedy", + "start": 22.5, + "end": 23.78, + "metadata": {"editorial":{"enter_seconds":0.14,"exit_seconds":0.04,"graphics":[]}}, + "words": [ + { + "text": "AND", + "start": 22.5, + "end": 22.76, + "id": "w58", + "metadata": {"layout":{"font_size":100,"center_x":472,"baseline":420},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "ONE", + "start": 22.76, + "end": 22.98, + "id": "w59", + "metadata": {"layout":{"font_size":260,"center_x":757,"baseline":420},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "WE", + "start": 22.98, + "end": 23.22, + "id": "w60", + "metadata": {"layout":{"font_size":100,"center_x":550,"baseline":810},"motion":{"entrance":"none","enter_seconds":0.14}} + }, + { + "text": "INTEND", + "start": 23.22, + "end": 23.42, + "id": "w61", + "metadata": { + "layout": {"font_size":280,"center_x":1200,"baseline":810}, + "motion": { + "entrance": "choreograph", + "start": 22.98, + "enter_seconds": 0.4, + "choreography": [ + { + "index": 0, + "delay": 0, + "duration": 0.5, + "from": {"x":-220,"y":100,"rotate":-3,"scale":1,"opacity":0.72}, + "easing": [0.45,0.02,0.25,1], + "control1": [-175,36], + "control2": [-25,0] + }, + { + "index": 1, + "delay": 0.014, + "duration": 0.5, + "from": {"x":-211,"y":92,"rotate":-3,"scale":1,"opacity":0.72}, + "easing": [0.45,0.02,0.25,1], + "control1": [-168,36], + "control2": [-25,0] + }, + { + "index": 2, + "delay": 0.028, + "duration": 0.5, + "from": {"x":-202,"y":84,"rotate":-3,"scale":1,"opacity":0.72}, + "easing": [0.45,0.02,0.25,1], + "control1": [-161,36], + "control2": [-25,0] + }, + { + "index": 3, + "delay": 0.042, + "duration": 0.5, + "from": {"x":-193,"y":76,"rotate":-3,"scale":1,"opacity":0.72}, + "easing": [0.45,0.02,0.25,1], + "control1": [-154,36], + "control2": [-25,0] + }, + { + "index": 4, + "delay": 0.056, + "duration": 0.5, + "from": {"x":-184,"y":68,"rotate":-3,"scale":1,"opacity":0.72}, + "easing": [0.45,0.02,0.25,1], + "control1": [-147,36], + "control2": [-25,0] + }, + { + "index": 5, + "delay": 0.07, + "duration": 0.5, + "from": {"x":-175,"y":60,"rotate":-3,"scale":1,"opacity":0.72}, + "easing": [0.45,0.02,0.25,1], + "control1": [-140,36], + "control2": [-25,0] + } + ], + "echoes": [ + {"future_seconds":0.0175,"opacity":0.16,"fill":"#e3dfd0","retire_seconds":0.18}, + {"future_seconds":0.035,"opacity":0.155,"fill":"#d6d7ca","retire_seconds":0.18}, + {"future_seconds":0.0525,"opacity":0.15,"fill":"#c9cfc4","retire_seconds":0.18}, + {"future_seconds":0.07,"opacity":0.145,"fill":"#b9c6c0","retire_seconds":0.18}, + {"future_seconds":0.0875,"opacity":0.14,"fill":"#a9bdbc","retire_seconds":0.18}, + {"future_seconds":0.105,"opacity":0.135,"fill":"#96aeb3","retire_seconds":0.18}, + {"future_seconds":0.1225,"opacity":0.13,"fill":"#7f9da5","retire_seconds":0.18}, + {"future_seconds":0.14,"opacity":0.125,"fill":"#6a8a96","retire_seconds":0.18}, + {"future_seconds":0.1575,"opacity":0.12,"fill":"#567583","retire_seconds":0.18}, + {"future_seconds":0.175,"opacity":0.115,"fill":"#436271","retire_seconds":0.18}, + {"future_seconds":0.1925,"opacity":0.11,"fill":"#365260","retire_seconds":0.18}, + {"future_seconds":0.21,"opacity":0.105,"fill":"#29434f","retire_seconds":0.18} + ] + } + } + }, + { + "text": "TO", + "start": 23.42, + "end": 23.78, + "id": "w62", + "metadata": {"layout":{"font_size":100,"center_x":1715,"baseline":810},"motion":{"entrance":"none","enter_seconds":0.14}} + } + ] + }, + { + "id": "optical-resolve", + "speaker": "John F. Kennedy", + "start": 23.78, + "end": 24.3, + "metadata": { + "editorial": {"enter_seconds":0.14,"exit_seconds":0.04}, + "output": {"picture_fade_seconds":0.066667,"voice_fade_seconds":0.1,"music_fade_seconds":0.24} + }, + "words": [ + { + "text": "WIN", + "start": 23.78, + "end": 24, + "id": "w63", + "metadata": { + "layout": {"font_size":620,"center_x":960,"baseline":820}, + "motion": {"entrance":"stamp"}, + "material": {"effect":"horizon","variation":0.63,"warm":true,"exit_seconds":0.04,"hold_until":24.3} + } + } + ] + } + ] +} diff --git a/examples/captions-showcase/media/phrases.vtt b/examples/captions-showcase/media/phrases.vtt new file mode 100644 index 00000000..70ee870c --- /dev/null +++ b/examples/captions-showcase/media/phrases.vtt @@ -0,0 +1,7 @@ +WEBVTT + +00:00.400 --> 00:02.200 +One track, every word. + +00:02.200 --> 00:04.800 +VTT, same API. diff --git a/examples/captions-showcase/src/annotations.rs b/examples/captions-showcase/src/annotations.rs new file mode 100644 index 00000000..ea1e6c51 --- /dev/null +++ b/examples/captions-showcase/src/annotations.rs @@ -0,0 +1,468 @@ +//! Word-local editorial marks. Their timing and art direction come from subtitle metadata. +//! +//! Drawn marks stay on the phrase; a glyph echo instead settles and disappears. +//! Geometry is deterministic for a given seed: the hand-drawn character is in the +//! stroke, not random frame jitter. Precision marks use measured, straight edges. +use std::{f64::consts::TAU, fmt::Write}; + +use fframes::{Svgr, captions::CaptionWordScope, svgr}; +use serde::Deserialize; + +#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "kebab-case")] +pub enum AccentKind { + Circle, + Underline, + DoubleUnderline, + Box, + Brackets, + Strike, + Arrow, + Burst, + Marker, + GlyphOutline, + /// A precise, persistent rule drawn beneath the word. + Rule, + /// Four crisp registration corners, drawn clockwise. + CornerFrame, + /// A measured rule and two terminal ticks. + AlignmentGuide, + /// A temporary glyph contour that catches up with the word and fades. + Echo, +} + +fn default_width() -> f64 { + 6.0 +} + +fn default_padding() -> f64 { + 12.0 +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Accent { + pub kind: AccentKind, + pub color: String, + /// Absolute subtitle-clock seconds, independent of a word's spoken end. + pub start: f64, + pub duration: f64, + #[serde(default = "default_width")] + pub width: f64, + #[serde(default = "default_padding")] + pub padding: f64, + #[serde(default)] + pub seed: u32, +} + +#[derive(Clone, Copy)] +struct Point(f64, f64); + +struct PencilPath { + data: String, + length: f64, +} + +impl PencilPath { + fn through(points: &[Point]) -> Self { + let mut data = String::new(); + let mut length = 0.0; + for (index, point) in points.iter().enumerate() { + let command = if index == 0 { 'M' } else { 'L' }; + // Writing into a String cannot fail. + let _ = write!(data, "{command}{:.3},{:.3} ", point.0, point.1); + if let Some(previous) = index.checked_sub(1).map(|i| points[i]) { + length += (point.0 - previous.0).hypot(point.1 - previous.1); + } + } + Self { data, length } + } + + fn cubic(start: Point, first: Point, second: Point, end: Point) -> Self { + // Sample the actual curve used for drawing, so draw-on distance does not + // depend on a renderer's support for the SVG pathLength attribute. + let points: Vec<_> = (0..=40) + .map(|index| { + let t = f64::from(index) / 40.0; + let u = 1.0 - t; + Point( + u.powi(3) * start.0 + + 3.0 * u.powi(2) * t * first.0 + + 3.0 * u * t.powi(2) * second.0 + + t.powi(3) * end.0, + u.powi(3) * start.1 + + 3.0 * u.powi(2) * t * first.1 + + 3.0 * u * t.powi(2) * second.1 + + t.powi(3) * end.1, + ) + }) + .collect(); + Self::through(&points) + } + + fn draw<'a>(self, accent: &Accent, progress: f64, width: f64, opacity: f64) -> Svgr<'a> { + self.draw_with_caps(accent, progress, width, opacity, "round", "round") + } + + fn draw_precise<'a>( + self, + accent: &Accent, + progress: f64, + width: f64, + opacity: f64, + ) -> Svgr<'a> { + self.draw_with_caps(accent, progress, width, opacity, "butt", "miter") + } + + fn draw_with_caps<'a>( + self, + accent: &Accent, + progress: f64, + width: f64, + opacity: f64, + cap: &'static str, + join: &'static str, + ) -> Svgr<'a> { + if progress <= 0.0 || self.length <= 0.0 { + return Svgr::empty(); + } + // A long empty interval ensures only the first stroke is ever visible. + let dash = format!("{} {}", self.length, self.length + 1.0); + let offset = self.length * (1.0 - progress.clamp(0.0, 1.0)); + svgr!() + } +} + +fn phase(seed: u32, salt: u32) -> f64 { + let mut bits = seed.wrapping_add(salt.wrapping_mul(0x9e37_79b9)); + bits ^= bits >> 16; + bits = bits.wrapping_mul(0x85eb_ca6b); + bits ^= bits >> 13; + f64::from(bits) / f64::from(u32::MAX) * TAU +} + +fn portion(progress: f64, start: f64, end: f64) -> f64 { + ((progress - start) / (end - start)).clamp(0.0, 1.0) +} + +fn oval(width: f64, height: f64, padding: f64, seed: u32, secondary: bool) -> PencilPath { + let tilt = 0.025 * phase(seed, 2).sin(); + let cx = width * 0.5; + let cy = height * 0.60; + let rx = width * 0.53 + padding; + let ry = height * 0.43 + padding; + let wobble = phase(seed, 3); + let start = if secondary { 0.70 } else { 0.57 }; + let turns = if secondary { 0.73 } else { 1.035 }; + let points: Vec<_> = (0..=112) + .map(|index| { + let theta = TAU * (start + f64::from(index) / 112.0 * turns); + let irregularity = + 1.0 + 0.018 * (3.0 * theta + wobble).sin() + 0.009 * (7.0 * theta - wobble).cos(); + let x = rx * theta.cos() * irregularity; + let y = ry * theta.sin() * (1.0 + 0.025 * (2.0 * theta + wobble).sin()); + Point( + cx + x * tilt.cos() - y * tilt.sin() + if secondary { 3.5 } else { 0.0 }, + cy + x * tilt.sin() + y * tilt.cos() + if secondary { -2.5 } else { 0.0 }, + ) + }) + .collect(); + PencilPath::through(&points) +} + +/// Render an annotation in the current word's local coordinate system. +/// +/// Put `Marker` behind the preset text; put the other kinds above it. This helper +/// never consumes the content, guesses font metrics, or changes text placement. +/// Marks persist after drawing, except `Echo`, which settles and disappears after +/// its authored duration. `padding` controls that echo's initial displacement. +pub fn render<'a>(scope: &CaptionWordScope<'a, '_>, accent: &Accent, time: f64) -> Svgr<'a> { + let width = scope.width(); + let height = scope.height(); + if !time.is_finite() + || !accent.start.is_finite() + || !accent.duration.is_finite() + || accent.duration <= 0.0 + || !accent.width.is_finite() + || accent.width <= 0.0 + || !accent.padding.is_finite() + || width <= 0.0 + || height <= 0.0 + || time <= accent.start + { + return Svgr::empty(); + } + let progress = ((time - accent.start) / accent.duration).clamp(0.0, 1.0); + let padding = accent.padding.max(0.0); + let weight = accent.width; + let wobble = phase(accent.seed, 1).sin(); + let left = -padding; + let right = width + padding; + let top = height * 0.19 - padding; + let bottom = height + padding * 0.55; + + match accent.kind { + AccentKind::Circle => { + let primary = oval(width, height, padding, accent.seed, false).draw( + accent, + portion(progress, 0.0, 0.82), + weight, + 0.98, + ); + let retrace = oval(width, height, padding, accent.seed, true).draw( + accent, + portion(progress, 0.62, 1.0), + weight * 0.42, + 0.65, + ); + svgr!({primary}{retrace}) + } + AccentKind::Underline | AccentKind::DoubleUnderline => { + let primary = PencilPath::cubic( + Point(left - 3.0, bottom), + Point(width * 0.24, bottom + 3.0 + wobble * 2.0), + Point(width * 0.72, bottom - 4.0), + Point(right + 5.0, bottom - 2.5), + ) + .draw(accent, portion(progress, 0.0, 0.72), weight, 0.98); + let secondary = if accent.kind == AccentKind::DoubleUnderline { + PencilPath::cubic( + Point(left + width * 0.08, bottom + weight * 1.8), + Point(width * 0.33, bottom + weight * 2.5), + Point(width * 0.71, bottom + weight * 0.9), + Point(right - width * 0.03, bottom + weight * 1.3), + ) + .draw(accent, portion(progress, 0.50, 1.0), weight * 0.55, 0.82) + } else { + Svgr::empty() + }; + svgr!({primary}{secondary}) + } + AccentKind::Box => { + let main = PencilPath::through(&[ + Point(left - 3.0, top + 3.0), + Point(right + 2.0, top - 1.5), + Point(right - 1.0, bottom + 3.0), + Point(left + 1.0, bottom - 2.0), + Point(left - 2.0, top - 7.0), + ]) + .draw(accent, portion(progress, 0.0, 0.86), weight, 0.97); + let lip = PencilPath::through(&[ + Point(left + 6.0, bottom + 4.0), + Point(right + 7.0, bottom + 7.0), + ]) + .draw(accent, portion(progress, 0.74, 1.0), weight * 0.45, 0.68); + svgr!({main}{lip}) + } + AccentKind::Brackets => { + let arm = (height * 0.15).max(14.0); + let first = PencilPath::through(&[ + Point(left + arm, top - 2.0), + Point(left - 1.0, top), + Point(left + 1.5, bottom + 2.0), + Point(left + arm, bottom), + ]) + .draw(accent, portion(progress, 0.0, 0.58), weight, 0.98); + let second = PencilPath::through(&[ + Point(right - arm, top + 1.0), + Point(right + 2.0, top - 2.0), + Point(right, bottom + 1.0), + Point(right - arm, bottom + 3.0), + ]) + .draw(accent, portion(progress, 0.35, 1.0), weight, 0.98); + svgr!({first}{second}) + } + AccentKind::Strike => { + let middle = height * 0.62; + let first = PencilPath::cubic( + Point(left - 5.0, middle + height * 0.08), + Point(width * 0.30, middle - height * 0.01), + Point(width * 0.65, middle + height * 0.01), + Point(right + 5.0, middle - height * 0.07), + ) + .draw(accent, portion(progress, 0.0, 0.72), weight, 0.94); + let second = PencilPath::through(&[ + Point(left + 3.0, middle - height * 0.065), + Point(right - 2.0, middle + height * 0.09), + ]) + .draw(accent, portion(progress, 0.48, 1.0), weight * 0.55, 0.78); + svgr!({first}{second}) + } + AccentKind::Arrow => { + let reach = (height * 0.45).max(34.0); + let tip = Point(right + 3.0, height * 0.58); + let stem = PencilPath::cubic( + Point(right + reach * 0.86, bottom + reach * 0.57), + Point(right + reach * 1.30, bottom + reach * 0.10), + Point(right + reach * 0.75, height * 0.68), + tip, + ) + .draw(accent, portion(progress, 0.0, 0.75), weight, 0.96); + let head = PencilPath::through(&[ + Point(tip.0 + reach * 0.34, tip.1 - reach * 0.19), + tip, + Point(tip.0 + reach * 0.24, tip.1 + reach * 0.37), + ]) + .draw(accent, portion(progress, 0.62, 1.0), weight * 0.93, 0.96); + svgr!({stem}{head}) + } + AccentKind::Burst => { + let reach = (height * 0.23).max(18.0); + [ + ( + Point(right + 7.0, top + 9.0), + Point(right + reach, top - reach * 0.63), + ), + ( + Point(right + 14.0, top + reach * 0.9), + Point(right + reach * 1.65, top + reach * 0.57), + ), + ( + Point(width * 0.88, top - 7.0), + Point(width * 0.90, top - reach * 1.2), + ), + ( + Point(left - 7.0, bottom - 4.0), + Point(left - reach, bottom + reach * 0.38), + ), + ] + .into_iter() + .enumerate() + .map(|(index, (start, end))| { + PencilPath::through(&[start, end]).draw( + accent, + portion(progress, index as f64 * 0.14, 0.52 + index as f64 * 0.16), + weight * if index == 1 { 0.72 } else { 1.0 }, + 0.92, + ) + }) + .collect() + } + AccentKind::Marker => { + let x = left + (right - left) * progress; + let y = height * 0.25; + let h = height * 0.72; + let nib = (height * 0.10).min((x - left) * 0.4); + let path = format!( + "M{left},{y} L{},{} L{x},{} L{},{} L{},{} Z", + x - nib, + y - 3.0, + y + h - 4.0, + left + nib * 0.4, + y + h + 2.0, + left - nib * 0.25, + y + h * 0.22, + ); + svgr!() + } + AccentKind::GlyphOutline => glyph_outline(scope, accent, progress), + AccentKind::Rule => PencilPath::through(&[Point(left, bottom), Point(right, bottom)]) + .draw_precise(accent, progress, weight, 0.95), + AccentKind::CornerFrame => { + let arm = (height * 0.13).clamp(14.0, 46.0); + [ + [ + Point(left, top + arm), + Point(left, top), + Point(left + arm, top), + ], + [ + Point(right - arm, top), + Point(right, top), + Point(right, top + arm), + ], + [ + Point(right, bottom - arm), + Point(right, bottom), + Point(right - arm, bottom), + ], + [ + Point(left + arm, bottom), + Point(left, bottom), + Point(left, bottom - arm), + ], + ] + .into_iter() + .enumerate() + .map(|(index, points)| { + PencilPath::through(&points).draw_precise( + accent, + portion(progress, index as f64 * 0.18, 0.46 + index as f64 * 0.18), + weight, + 0.90, + ) + }) + .collect() + } + AccentKind::AlignmentGuide => { + let tick = (height * 0.035).clamp(6.0, 12.0); + let rule = PencilPath::through(&[Point(left, bottom), Point(right, bottom)]) + .draw_precise(accent, portion(progress, 0.0, 0.78), weight, 0.80); + let ticks = [left, right] + .into_iter() + .enumerate() + .map(|(index, x)| { + PencilPath::through(&[Point(x, bottom - tick), Point(x, bottom + tick)]) + .draw_precise( + accent, + portion( + progress, + 0.52 + index as f64 * 0.14, + 0.86 + index as f64 * 0.14, + ), + weight, + 0.90, + ) + }) + .collect::>(); + svgr!({rule}{ticks}) + } + AccentKind::Echo => { + if progress >= 1.0 { + return Svgr::empty(); + } + let remaining = (1.0 - progress).powi(3); + let direction = if accent.seed.is_multiple_of(2) { + 1.0 + } else { + -1.0 + }; + let offset = padding * remaining * direction; + let opacity = 0.62 * portion(progress, 0.0, 0.12) * (1.0 - progress); + let outline = glyph_outline(scope, accent, 1.0); + svgr!({outline}) + } + } +} + +fn glyph_outline<'a>(scope: &CaptionWordScope<'a, '_>, accent: &Accent, progress: f64) -> Svgr<'a> { + let padding = accent.width * 2.0 + 4.0; + let width = scope.width() + padding * 2.0; + let height = scope.height() + padding * 2.0; + let key = format!("pencil-glyph-{}-{}", accent.seed, accent.start.to_bits()); + let filter = scope.id(&format!("{key}-filter")); + let clip = scope.id(&format!("{key}-clip")); + let glyph = scope.mask_text(svgr!( + + )); + svgr!( + + + + + + + + + + + + + + {glyph} + + ) +} diff --git a/examples/captions-showcase/src/choreography.rs b/examples/captions-showcase/src/choreography.rs new file mode 100644 index 00000000..b8017c25 --- /dev/null +++ b/examples/captions-showcase/src/choreography.rs @@ -0,0 +1,450 @@ +//! Authored paths and optical replicas, independent of subtitle text or cue identity. +//! All path offsets are relative to the caption engine's measured final glyph pose. +use fframes::{ + Svgr, + animation::{AnimationRuntime, Easing}, + captions::{CaptionError, CaptionWordScope}, + svgr, +}; +use serde::Deserialize; + +fn one() -> f64 { + 1.0 +} +fn duration() -> f64 { + 0.45 +} +fn curve() -> [f64; 4] { + [0.22, 0.75, 0.24, 1.0] +} +fn line_width() -> f64 { + 1.4 +} +fn faint() -> f64 { + 0.22 +} +fn silver() -> String { + "#a8cfdf".into() +} +fn yes() -> bool { + true +} +fn retirement() -> f64 { + 0.18 +} + +#[derive(Debug, Clone, Copy, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct Pose { + pub x: f64, + pub y: f64, + pub rotate: f64, + pub scale: f64, + /// Per-axis factors multiply `scale`; all three settle to one. + pub scale_x: f64, + pub scale_y: f64, + pub opacity: f64, +} + +impl Default for Pose { + fn default() -> Self { + Self { + x: 0.0, + y: 0.0, + rotate: 0.0, + scale: 1.0, + scale_x: 1.0, + scale_y: 1.0, + opacity: 1.0, + } + } +} + +/// A character-indexed path. No text is replaced and spoken intervals stay intact. +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GlyphMotion { + pub index: usize, + #[serde(default)] + pub delay: f64, + #[serde(default = "duration")] + pub duration: f64, + #[serde(default)] + pub from: Pose, + /// Absolute offsets relative to the final glyph, not relative to `from`. + #[serde(default)] + pub control1: Option<[f64; 2]>, + #[serde(default)] + pub control2: Option<[f64; 2]>, + #[serde(default = "curve")] + pub easing: [f64; 4], +} + +/// A filled sample of a glyph's actual future trajectory. The current glyph +/// catches up to these poses; they retire instead of lingering as a second word. +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Echo { + pub future_seconds: f64, + #[serde(default = "faint")] + pub opacity: f64, + #[serde(default = "silver")] + pub fill: String, + /// Smooth fade during the final part of the primary glyph's path. + #[serde(default = "retirement")] + pub retire_seconds: f64, +} + +pub fn validate_echoes(echoes: &[Echo], path: &str) -> Result<(), CaptionError> { + if echoes.len() > 32 { + return Err(CaptionError::invalid( + path, + "at most thirty-two echoes per word", + )); + } + for (index, echo) in echoes.iter().enumerate() { + let p = format!("{path}[{index}]"); + finite( + echo.future_seconds, + 0.001, + 2.0, + format!("{p}.future_seconds"), + )?; + finite(echo.opacity, 0.0, 1.0, format!("{p}.opacity"))?; + finite( + echo.retire_seconds, + 0.01, + 2.0, + format!("{p}.retire_seconds"), + )?; + if echo.fill.trim().is_empty() { + return Err(CaptionError::invalid( + format!("{p}.fill"), + "must be a nonempty color", + )); + } + } + Ok(()) +} + +/// An exact glyph copy behind the word, outlined and optionally filled. A +/// collapsing copy can hold its authored pose before converging and fading. +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Replica { + pub offset: [f64; 2], + #[serde(default)] + pub rotate: f64, + #[serde(default = "one")] + pub scale: f64, + #[serde(default = "silver")] + pub color: String, + /// A solid glyph-masked face beneath the outline; absent keeps it hollow. + #[serde(default)] + pub fill: Option, + #[serde(default = "faint")] + pub opacity: f64, + #[serde(default = "line_width")] + pub stroke_width: f64, + /// Absolute caption-clock seconds. + pub start: f64, + #[serde(default = "duration")] + pub duration: f64, + /// Keep a collapsing replica at its full authored pose before its duration + /// begins. Non-collapsing replicas keep their existing entrance behavior. + #[serde(default)] + pub hold_seconds: f64, + #[serde(default = "yes")] + pub collapse: bool, +} + +pub struct Choreography { + paths: Vec>, + travel_padding: f64, + transform_padding: f64, + settled_at: f64, +} + +struct Path { + delay: f64, + duration: f64, + from: Pose, + control1: [f64; 2], + control2: [f64; 2], + easing: AnimationRuntime, +} + +fn finite(value: f64, low: f64, high: f64, path: impl Into) -> Result<(), CaptionError> { + if value.is_finite() && (low..=high).contains(&value) { + return Ok(()); + } + Err(CaptionError::invalid( + path, + format!("must be finite and between {low} and {high}"), + )) +} + +fn vector(value: [f64; 2], path: &str) -> Result<(), CaptionError> { + for (axis, value) in value.into_iter().enumerate() { + finite(value, -4000.0, 4000.0, format!("{path}[{axis}]"))?; + } + Ok(()) +} + +impl Choreography { + pub fn prepare( + moves: &[GlyphMotion], + glyph_count: usize, + path: &str, + ) -> Result { + let mut paths: Vec> = (0..glyph_count).map(|_| None).collect(); + for (position, movement) in moves.iter().enumerate() { + let p = format!("{path}[{position}]"); + if movement.index >= glyph_count { + return Err(CaptionError::invalid( + format!("{p}.index"), + "must refer to a character in this word", + )); + } + if paths[movement.index].is_some() { + return Err(CaptionError::invalid( + format!("{p}.index"), + "a character may have only one entrance path", + )); + } + finite(movement.delay, 0.0, 10.0, format!("{p}.delay"))?; + finite(movement.duration, 0.01, 10.0, format!("{p}.duration"))?; + vector([movement.from.x, movement.from.y], &format!("{p}.from"))?; + finite( + movement.from.rotate, + -720.0, + 720.0, + format!("{p}.from.rotate"), + )?; + finite(movement.from.scale, 0.05, 4.0, format!("{p}.from.scale"))?; + finite( + movement.from.scale_x, + 0.05, + 4.0, + format!("{p}.from.scale_x"), + )?; + finite( + movement.from.scale_y, + 0.05, + 4.0, + format!("{p}.from.scale_y"), + )?; + finite(movement.from.opacity, 0.0, 1.0, format!("{p}.from.opacity"))?; + let control1 = movement + .control1 + .unwrap_or([movement.from.x * 2.0 / 3.0, movement.from.y * 2.0 / 3.0]); + let control2 = movement + .control2 + .unwrap_or([movement.from.x / 3.0, movement.from.y / 3.0]); + vector(control1, &format!("{p}.control1"))?; + vector(control2, &format!("{p}.control2"))?; + for (index, value) in movement.easing.into_iter().enumerate() { + finite(value, 0.0, 1.0, format!("{p}.easing[{index}]"))?; + } + let [x1, y1, x2, y2] = movement.easing.map(|value| value as f32); + paths[movement.index] = Some(Path { + delay: movement.delay, + duration: movement.duration, + from: movement.from, + control1, + control2, + easing: AnimationRuntime::new( + movement.duration as f32, + &Easing::CubicBezier(x1, y1, x2, y2), + ), + }); + } + let travel_padding = paths + .iter() + .flatten() + .flat_map(|path| { + [ + path.from.x.abs(), + path.from.y.abs(), + path.control1[0].abs(), + path.control1[1].abs(), + path.control2[0].abs(), + path.control2[1].abs(), + ] + }) + .fold(0.0, f64::max); + let transform_padding = paths + .iter() + .flatten() + .map(|path| { + let turn = path.from.rotate.abs().min(90.0).to_radians().sin(); + // Each scale factor interpolates independently. The product of + // their maxima is conservative even when their directions differ. + let scale = + path.from.scale.max(1.0) * path.from.scale_x.max(path.from.scale_y).max(1.0); + (turn * scale + (scale - 1.0).max(0.0)) * 1.5 + }) + .fold(0.0, f64::max); + let settled_at = paths + .iter() + .flatten() + .map(|path| path.delay + path.duration) + .fold(0.0, f64::max); + Ok(Self { + paths, + travel_padding, + transform_padding, + settled_at, + }) + } + + /// The path is contained in the convex hull of its control points. Reserve + /// that travel plus a conservative character rotation/scale clearance while + /// it moves; settled glyphs need only their ordinary shadow region. + pub fn shadow_clearance(&self, elapsed: f64, font_size: f64) -> f64 { + if elapsed >= self.settled_at { + 0.0 + } else { + self.travel_padding + self.transform_padding * font_size + } + } + + pub fn pose(&self, index: usize, elapsed: f64) -> Pose { + let Some(Some(path)) = self.paths.get(index) else { + return Pose::default(); + }; + let time = elapsed - path.delay; + if time <= 0.0 { + return path.from; + } + if time >= path.duration { + return Pose::default(); + } + let t = f64::from(path.easing.solve(&(time as f32))).clamp(0.0, 1.0); + let u = 1.0 - t; + let evaluate = + |a: f64, b: f64, c: f64| u * u * u * a + 3.0 * u * u * t * b + 3.0 * u * t * t * c; + Pose { + x: evaluate(path.from.x, path.control1[0], path.control2[0]), + y: evaluate(path.from.y, path.control1[1], path.control2[1]), + rotate: path.from.rotate * u, + scale: 1.0 + (path.from.scale - 1.0) * u, + scale_x: 1.0 + (path.from.scale_x - 1.0) * u, + scale_y: 1.0 + (path.from.scale_y - 1.0) * u, + opacity: 1.0 + (path.from.opacity - 1.0) * u, + } + } + + /// Sample the same prepared path in the future. `elapsed` is relative to + /// the word's authored visual start, just as for `pose`. Seeking is stateless. + pub fn echo_pose(&self, index: usize, elapsed: f64, echo: &Echo) -> Option { + let path = self.paths.get(index)?.as_ref()?; + if elapsed < 0.0 || elapsed >= path.delay + path.duration { + return None; + } + let remaining = path.delay + path.duration - elapsed; + let fade = (remaining / echo.retire_seconds).clamp(0.0, 1.0); + let fade = fade * fade * (3.0 - 2.0 * fade); + let mut pose = self.pose(index, elapsed + echo.future_seconds); + pose.opacity *= echo.opacity * fade; + (pose.opacity > 0.0).then_some(pose) + } +} + +pub fn validate_replicas( + replicas: &[Replica], + cue_start: f64, + cue_end: f64, + path: &str, +) -> Result<(), CaptionError> { + if replicas.len() > 16 { + return Err(CaptionError::invalid( + path, + "at most sixteen replicas per word", + )); + } + for (index, copy) in replicas.iter().enumerate() { + let p = format!("{path}[{index}]"); + vector(copy.offset, &format!("{p}.offset"))?; + finite(copy.rotate, -360.0, 360.0, format!("{p}.rotate"))?; + finite(copy.scale, 0.1, 4.0, format!("{p}.scale"))?; + finite(copy.opacity, 0.0, 1.0, format!("{p}.opacity"))?; + finite(copy.stroke_width, 0.2, 20.0, format!("{p}.stroke_width"))?; + finite(copy.start, cue_start, cue_end, format!("{p}.start"))?; + finite(copy.duration, 0.01, 10.0, format!("{p}.duration"))?; + finite(copy.hold_seconds, 0.0, 10.0, format!("{p}.hold_seconds"))?; + if copy + .fill + .as_ref() + .is_some_and(|fill| fill.trim().is_empty()) + { + return Err(CaptionError::invalid( + format!("{p}.fill"), + "must be a nonempty color when provided", + )); + } + } + Ok(()) +} + +fn replica_phase(copy: &Replica, time: f64) -> Option<(f64, f64)> { + if time < copy.start { + return None; + } + let elapsed = time + - copy.start + - if copy.collapse { + copy.hold_seconds + } else { + 0.0 + }; + let progress = (elapsed / copy.duration).clamp(0.0, 1.0); + if copy.collapse && progress >= 1.0 { + return None; + } + let eased = progress * progress * (3.0 - 2.0 * progress); + let travel = if copy.collapse { 1.0 - eased } else { 1.0 }; + let opacity = copy.opacity + * if copy.collapse { + (1.0 - progress).sqrt() + } else { + eased + }; + Some((travel, opacity)) +} + +pub fn replicas<'a>( + scope: &CaptionWordScope<'a, '_>, + copies: &'a [Replica], + time: f64, +) -> Svgr<'a> { + copies.iter().enumerate().filter_map(|(index, copy)| { + let (travel, opacity) = replica_phase(copy, time)?; + let x = copy.offset[0] * travel; + let y = copy.offset[1] * travel; + let rotation = copy.rotate * travel; + let scale = 1.0 + (copy.scale - 1.0) * travel; + let cx = scope.width() * 0.5; + let cy = scope.height() * 0.5; + let padding = copy.stroke_width * 2.0 + 4.0; + let width = scope.width() + padding * 2.0; + let height = scope.height() + padding * 2.0; + let id = scope.id(&format!("choreography-replica-{index}")); + let glyph = scope.mask_text(svgr!()); + let solid = copy.fill.as_ref().map(|fill| scope.mask_text(svgr!( + + ))).unwrap_or_default(); + Some(svgr!( + {solid} + + + + + + + + {glyph} + )) + }).collect() +} diff --git a/examples/captions-showcase/src/cue_graphics.rs b/examples/captions-showcase/src/cue_graphics.rs new file mode 100644 index 00000000..6ab03e8f --- /dev/null +++ b/examples/captions-showcase/src/cue_graphics.rs @@ -0,0 +1,1016 @@ +//! Cue-level relationships: calibrated paths, coherent flow and quiet measurements. +//! +//! All positions and timing are authored subtitle metadata. Curves and arc lengths +//! are prepared once; rendering is a deterministic function of the caption clock. +use std::fmt::Write; + +use fframes::{Svgr, captions::CaptionError, svgr}; +use serde::Deserialize; + +type Point = [f64; 2]; + +fn one() -> f64 { + 1.0 +} +fn fade() -> f64 { + 0.2 +} +fn width() -> f64 { + 1.5 +} +fn font_size() -> usize { + 20 +} +fn family() -> String { + "DM Sans".into() +} +fn weight() -> u16 { + 500 +} +fn count() -> usize { + 3 +} +fn speed() -> f64 { + 0.5 +} +fn radius() -> f64 { + 2.0 +} +fn tail() -> f64 { + 0.08 +} +fn trace_radius() -> f64 { + 6.0 +} +fn trace_tail() -> f64 { + 0.16 +} +fn construction_opacity() -> f64 { + 0.25 +} +fn enabled() -> bool { + true +} +fn lock_fraction() -> f64 { + 0.82 +} +fn jaw_size() -> f64 { + 32.0 +} +fn tick_count() -> usize { + 7 +} +fn pixel_unit() -> String { + "px".into() +} + +#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "kebab-case")] +pub enum Layer { + #[default] + Under, + Over, +} + +/// Shared flat JSON fields. Times are absolute caption-clock seconds. +#[derive(Debug, Clone, Deserialize)] +pub struct Timing { + pub start: f64, + pub duration: f64, + #[serde(default)] + pub hold_until: Option, + #[serde(default = "fade")] + pub fade_out: f64, + #[serde(default = "one")] + pub opacity: f64, + #[serde(default)] + pub layer: Layer, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(tag = "kind", rename_all = "kebab-case")] +pub enum Graphic { + Path(PathGraphic), + Label(LabelGraphic), + Measure(MeasureGraphic), +} + +#[derive(Debug, Clone, Deserialize)] +pub struct PathGraphic { + #[serde(flatten)] + pub timing: Timing, + /// Polyline vertices; a cubic instead requires start, control1, control2, end. + pub points: Vec, + #[serde(default)] + pub cubic: bool, + pub color: String, + #[serde(default = "width")] + pub width: f64, + /// A dark companion stroke supports the colored path over bright footage. + #[serde(default)] + pub keyline: bool, + #[serde(default)] + pub arrow: bool, + #[serde(default)] + pub flow: Option, + /// A single luminous front follows this path's reveal, then settles at its destination. + #[serde(default)] + pub trace: Option, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Trace { + #[serde(default = "trace_radius")] + pub head_radius: f64, + /// Fraction of the prepared arc length illuminated behind the reveal front. + #[serde(default = "trace_tail")] + pub tail: f64, + /// Seconds for the arrived front to dissipate; the drawn path stays in place. + #[serde(default = "trace_tail")] + pub linger: f64, + #[serde(default)] + pub color: Option, + /// For cubic paths, display their actual de Casteljau interpolation scaffold. + #[serde(default)] + pub construction: bool, + #[serde(default = "construction_opacity")] + pub construction_opacity: f64, + #[serde(default = "enabled")] + pub arrival_ring: bool, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Flow { + #[serde(default = "count")] + pub count: usize, + /// Completed traversals per second, independent of curve control-point spacing. + #[serde(default = "speed")] + pub speed: f64, + #[serde(default)] + pub phase: f64, + #[serde(default = "radius")] + pub radius: f64, + /// Tail length as a fraction of the path's measured arc length. + #[serde(default = "tail")] + pub tail: f64, + #[serde(default)] + pub color: Option, +} + +#[derive(Debug, Clone, Copy, Default, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum TextAlign { + #[default] + Start, + Middle, + End, +} + +impl TextAlign { + fn svg(self) -> &'static str { + match self { + Self::Start => "start", + Self::Middle => "middle", + Self::End => "end", + } + } +} + +#[derive(Debug, Clone, Deserialize)] +pub struct LabelGraphic { + #[serde(flatten)] + pub timing: Timing, + pub text: String, + pub x: f64, + pub y: f64, + #[serde(default = "font_size")] + pub font_size: usize, + #[serde(default = "family")] + pub font_family: String, + #[serde(default = "weight")] + pub font_weight: u16, + #[serde(default)] + pub align: TextAlign, + pub color: String, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct MeasureGraphic { + #[serde(flatten)] + pub timing: Timing, + pub from: Point, + pub to: Point, + /// Signed distance from the measured segment, along its left-hand normal. + pub offset: f64, + pub label: String, + #[serde(default = "font_size")] + pub font_size: usize, + #[serde(default = "family")] + pub font_family: String, + pub color: String, + #[serde(default = "width")] + pub width: f64, + #[serde(default)] + pub keyline: bool, + /// A physical measuring jaw closes from an extended aperture onto these endpoints. + #[serde(default)] + pub caliper: Option, +} + +#[derive(Debug, Clone, Copy, Default, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum MovingEnd { + From, + #[default] + To, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Caliper { + /// Extra opening in composition pixels before the moving jaw closes. + pub from_extra: f64, + #[serde(default)] + pub moving_end: MovingEnd, + /// Portion of duration spent closing; the rest confirms the locked dimension. + #[serde(default = "lock_fraction")] + pub lock_fraction: f64, + #[serde(default = "jaw_size")] + pub jaw_size: f64, + #[serde(default = "tick_count")] + pub tick_count: usize, + #[serde(default = "pixel_unit")] + pub unit: String, + #[serde(default)] + pub precision: usize, +} + +#[derive(Debug)] +struct Sample { + point: Point, + distance: f64, +} + +#[derive(Debug)] +struct Geometry { + data: String, + length: f64, + samples: Vec, +} + +impl Geometry { + fn new(points: &[Point], cubic: bool) -> Self { + let mut data = format!("M{:.4},{:.4}", points[0][0], points[0][1]); + let evaluated = if cubic { + let _ = write!( + data, + " C{:.4},{:.4} {:.4},{:.4} {:.4},{:.4}", + points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1] + ); + (0..=128) + .map(|i| { + let t = f64::from(i) / 128.0; + let u = 1.0 - t; + [0, 1].map(|a| { + u.powi(3) * points[0][a] + + 3.0 * u.powi(2) * t * points[1][a] + + 3.0 * u * t.powi(2) * points[2][a] + + t.powi(3) * points[3][a] + }) + }) + .collect::>() + } else { + for point in &points[1..] { + let _ = write!(data, " L{:.4},{:.4}", point[0], point[1]); + } + points.to_vec() + }; + let mut length = 0.0; + let samples = evaluated + .iter() + .enumerate() + .map(|(i, point)| { + if i > 0 { + length += distance(*point, evaluated[i - 1]); + } + Sample { + point: *point, + distance: length, + } + }) + .collect(); + Self { + data, + length, + samples, + } + } + + /// Arc-length lookup means flow has a coherent speed on bends and straight lines. + fn at(&self, fraction: f64) -> (Point, Point) { + let d = fraction.clamp(0.0, 1.0) * self.length; + let next = self + .samples + .partition_point(|sample| sample.distance < d) + .max(1) + .min(self.samples.len() - 1); + let a = &self.samples[next - 1]; + let b = &self.samples[next]; + let span = b.distance - a.distance; + let t = if span > 0.0 { + (d - a.distance) / span + } else { + 0.0 + }; + let tangent = unit([b.point[0] - a.point[0], b.point[1] - a.point[1]]); + ( + [ + a.point[0] + (b.point[0] - a.point[0]) * t, + a.point[1] + (b.point[1] - a.point[1]) * t, + ], + tangent, + ) + } + + /// Cubic samples have uniform parameter spacing, unlike their arc lengths. + fn cubic_parameter_at(&self, fraction: f64) -> f64 { + let d = fraction.clamp(0.0, 1.0) * self.length; + let next = self + .samples + .partition_point(|sample| sample.distance < d) + .max(1) + .min(self.samples.len() - 1); + let a = &self.samples[next - 1]; + let span = self.samples[next].distance - a.distance; + let local = if span > 0.0 { + (d - a.distance) / span + } else { + 0.0 + }; + (next as f64 - 1.0 + local) / (self.samples.len() - 1) as f64 + } +} + +#[derive(Debug)] +struct PreparedPath { + graphic: PathGraphic, + geometry: Geometry, +} + +#[derive(Debug)] +struct PreparedMeasure { + graphic: MeasureGraphic, + geometry: Geometry, + extensions: String, + ticks: String, + center: Point, + angle: f64, + from: Point, + to: Point, + tangent: Point, + normal: Point, + caliper_rail: String, + caliper_ticks: String, +} + +#[derive(Debug)] +enum PreparedKind { + Path(PreparedPath), + Label(LabelGraphic), + Measure(PreparedMeasure), +} + +#[derive(Debug)] +struct PreparedGraphic { + timing: Timing, + hold_until: f64, + kind: PreparedKind, +} + +/// Geometry and path strings are immutable after preparation and can be shared by workers. +#[derive(Debug, Default)] +pub struct PreparedGraphics { + items: Vec, +} + +impl PreparedGraphics { + pub fn new(graphics: &[Graphic], cue_start: f64, cue_end: f64) -> Result { + let mut items = Vec::with_capacity(graphics.len()); + for (i, graphic) in graphics.iter().enumerate() { + let path = format!("metadata.editorial.graphics[{i}]"); + let timing = match graphic { + Graphic::Path(g) => &g.timing, + Graphic::Label(g) => &g.timing, + Graphic::Measure(g) => &g.timing, + }; + range(timing.start, cue_start, cue_end, &format!("{path}.start"))?; + range( + timing.duration, + 0.001, + (cue_end - timing.start).max(0.001), + &format!("{path}.duration"), + )?; + let hold_until = timing.hold_until.unwrap_or(cue_end); + range( + hold_until, + timing.start + timing.duration, + cue_end + 1e-9, + &format!("{path}.hold_until"), + )?; + range(timing.fade_out, 0.001, 5.0, &format!("{path}.fade_out"))?; + range(timing.opacity, 0.0, 1.0, &format!("{path}.opacity"))?; + let kind = match graphic { + Graphic::Path(g) => { + if g.points.len() < 2 + || (g.cubic && g.points.len() != 4) + || g.points.len() > 128 + { + return Err(CaptionError::invalid( + format!("{path}.points"), + "a polyline needs 2–128 points; a cubic needs start, two controls and end", + )); + } + for (j, point) in g.points.iter().enumerate() { + validate_point(*point, &format!("{path}.points[{j}]"))?; + } + range(g.width, 0.1, 20.0, &format!("{path}.width"))?; + color(&g.color, &format!("{path}.color"))?; + if let Some(flow) = &g.flow { + if !(1..=8).contains(&flow.count) { + return Err(CaptionError::invalid( + format!("{path}.flow.count"), + "must be between 1 and 8", + )); + } + range(flow.speed, 0.0, 5.0, &format!("{path}.flow.speed"))?; + range(flow.phase, -10.0, 10.0, &format!("{path}.flow.phase"))?; + range(flow.radius, 0.25, 12.0, &format!("{path}.flow.radius"))?; + range(flow.tail, 0.0, 0.35, &format!("{path}.flow.tail"))?; + if let Some(value) = &flow.color { + color(value, &format!("{path}.flow.color"))?; + } + } + if let Some(trace) = &g.trace { + range( + trace.head_radius, + 1.0, + 16.0, + &format!("{path}.trace.head_radius"), + )?; + range(trace.tail, 0.0, 0.5, &format!("{path}.trace.tail"))?; + range(trace.linger, 0.01, 2.0, &format!("{path}.trace.linger"))?; + range( + trace.construction_opacity, + 0.0, + 1.0, + &format!("{path}.trace.construction_opacity"), + )?; + if trace.construction && !g.cubic { + return Err(CaptionError::invalid( + format!("{path}.trace.construction"), + "de Casteljau construction requires a cubic path", + )); + } + if let Some(value) = &trace.color { + color(value, &format!("{path}.trace.color"))?; + } + } + let geometry = Geometry::new(&g.points, g.cubic); + range(geometry.length, 0.01, 100_000.0, &format!("{path}.points"))?; + PreparedKind::Path(PreparedPath { + graphic: g.clone(), + geometry, + }) + } + Graphic::Label(g) => { + validate_point([g.x, g.y], &path)?; + text(&g.text, &g.font_family, g.font_size, &path)?; + if !(100..=900).contains(&g.font_weight) { + return Err(CaptionError::invalid( + format!("{path}.font_weight"), + "must be between 100 and 900", + )); + } + color(&g.color, &format!("{path}.color"))?; + PreparedKind::Label(g.clone()) + } + Graphic::Measure(g) => { + validate_point(g.from, &format!("{path}.from"))?; + validate_point(g.to, &format!("{path}.to"))?; + range(distance(g.from, g.to), 1.0, 10000.0, &format!("{path}.to"))?; + range(g.offset, -500.0, 500.0, &format!("{path}.offset"))?; + range(g.width, 0.1, 20.0, &format!("{path}.width"))?; + text(&g.label, &g.font_family, g.font_size, &path)?; + color(&g.color, &format!("{path}.color"))?; + if let Some(caliper) = &g.caliper { + range( + caliper.from_extra, + 0.0, + 1000.0, + &format!("{path}.caliper.from_extra"), + )?; + range( + caliper.lock_fraction, + 0.2, + 0.98, + &format!("{path}.caliper.lock_fraction"), + )?; + range( + caliper.jaw_size, + 8.0, + 120.0, + &format!("{path}.caliper.jaw_size"), + )?; + if caliper.tick_count > 15 || caliper.precision > 2 { + return Err(CaptionError::invalid( + format!("{path}.caliper"), + "tick_count must be 0–15; precision must be 0–2", + )); + } + if caliper.unit.trim().is_empty() || caliper.unit.chars().count() > 16 { + return Err(CaptionError::invalid( + format!("{path}.caliper.unit"), + "must contain 1–16 characters", + )); + } + } + PreparedKind::Measure(prepare_measure(g)) + } + }; + items.push(PreparedGraphic { + timing: timing.clone(), + hold_until, + kind, + }); + } + Ok(Self { items }) + } + + pub fn render(&self, layer: Layer, time: f64) -> Svgr<'_> { + self.items + .iter() + .filter(|item| item.timing.layer == layer) + .map(|item| item.render(time)) + .collect() + } +} + +impl PreparedGraphic { + fn render(&self, time: f64) -> Svgr<'_> { + if time < self.timing.start || time >= self.hold_until + self.timing.fade_out { + return Svgr::empty(); + } + let elapsed = time - self.timing.start; + let linear_progress = (elapsed / self.timing.duration).clamp(0.0, 1.0); + let progress = smooth(linear_progress); + let opacity = self.timing.opacity + * (1.0 - smooth(((time - self.hold_until) / self.timing.fade_out).clamp(0.0, 1.0))); + let content = match &self.kind { + PreparedKind::Path(path) => path.render(progress, elapsed), + PreparedKind::Label(label) => svgr!( + {label.text.as_str()} + ), + PreparedKind::Measure(measure) => measure.render(progress, linear_progress), + }; + svgr!({content}) + } +} + +impl PreparedPath { + fn render<'a>(&'a self, progress: f64, elapsed: f64) -> Svgr<'a> { + let g = &self.graphic; + let line = draw_supported(&self.geometry, &g.color, g.width, progress, g.keyline); + let arrow = if g.arrow && progress > 0.01 { + let (point, tangent) = self.geometry.at(progress); + let normal = [-tangent[1], tangent[0]]; + let size = 7.0 + 2.0 * g.width; + let left = [ + point[0] - tangent[0] * size + normal[0] * size * 0.45, + point[1] - tangent[1] * size + normal[1] * size * 0.45, + ]; + let right = [ + point[0] - tangent[0] * size - normal[0] * size * 0.45, + point[1] - tangent[1] * size - normal[1] * size * 0.45, + ]; + let data = format!( + "M{},{} L{},{} L{},{}", + left[0], left[1], point[0], point[1], right[0], right[1] + ); + let keyline = if g.keyline { + svgr!() + } else { + Svgr::empty() + }; + svgr!({keyline}) + } else { + Svgr::empty() + }; + let flow = g.flow.as_ref().map(|flow| { + let color = flow.color.as_deref().unwrap_or(g.color.as_str()); + (0..flow.count).map(|index| { + let phase = (elapsed * flow.speed + flow.phase + index as f64 / flow.count as f64).rem_euclid(1.0); + if phase > progress { return Svgr::empty(); } + let (point, _) = self.geometry.at(phase); + let intensity = (std::f64::consts::PI * phase).sin().max(0.0).sqrt(); + let tail_length = (self.geometry.length * flow.tail).max(0.1); + svgr!( + + + + ) + }).collect::>() + }).unwrap_or_default(); + let Some(trace) = &g.trace else { + return svgr!({line}{arrow}{flow}); + }; + let front = self.render_trace(trace, progress, elapsed); + let construction = self.render_construction(trace, progress); + svgr!({construction}{line}{arrow}{flow}{front}) + } + + fn render_trace<'a>(&'a self, trace: &'a Trace, progress: f64, elapsed: f64) -> Svgr<'a> { + let arrived = ((elapsed - self.graphic.timing.duration) / trace.linger).clamp(0.0, 1.0); + if progress <= 0.0 || arrived >= 1.0 { + return Svgr::empty(); + } + let color = trace + .color + .as_deref() + .unwrap_or(self.graphic.color.as_str()); + let (head, _) = self.geometry.at(progress); + let opacity = smooth((progress / 0.04).clamp(0.0, 1.0)) * (1.0 - smooth(arrived)); + let tail_length = self.geometry.length * trace.tail; + let tail = if tail_length > 0.0 { + let dash = format!("{tail_length} {}", self.geometry.length * 2.0); + let offset = tail_length - progress * self.geometry.length; + svgr!( + + + ) + } else { + Svgr::empty() + }; + let ring = if trace.arrival_ring && arrived > 0.0 { + svgr!() + } else { + Svgr::empty() + }; + let support = if self.graphic.keyline { + svgr!() + } else { + Svgr::empty() + }; + svgr!( + {tail}{ring} + + {support} + + + ) + } + + fn render_construction<'a>(&'a self, trace: &'a Trace, progress: f64) -> Svgr<'a> { + if !trace.construction || progress <= 0.0 || progress >= 0.97 { + return Svgr::empty(); + } + let t = self.geometry.cubic_parameter_at(progress); + let construction = cubic_construction(&self.graphic.points, t); + let opacity = trace.construction_opacity + * smooth((progress / 0.08).clamp(0.0, 1.0)) + * (1.0 - smooth(((progress - 0.65) / 0.32).clamp(0.0, 1.0))); + let color = trace + .color + .as_deref() + .unwrap_or(self.graphic.color.as_str()); + let nodes = construction + .level1 + .iter() + .chain(construction.level2.iter()) + .map(|p| { + svgr!() + }) + .collect::>(); + svgr!( + + + + {nodes} + + ) + } +} + +struct CubicConstruction { + level1: [Point; 3], + level2: [Point; 2], + point: Point, +} + +fn cubic_construction(points: &[Point], t: f64) -> CubicConstruction { + let level1 = [0, 1, 2].map(|i| lerp(points[i], points[i + 1], t)); + let level2 = [0, 1].map(|i| lerp(level1[i], level1[i + 1], t)); + CubicConstruction { + level1, + level2, + point: lerp(level2[0], level2[1], t), + } +} + +fn prepare_measure(g: &MeasureGraphic) -> PreparedMeasure { + let tangent = unit([g.to[0] - g.from[0], g.to[1] - g.from[1]]); + let normal = [-tangent[1], tangent[0]]; + let shifted = + |point: Point, offset: f64| [point[0] + normal[0] * offset, point[1] + normal[1] * offset]; + let a = shifted(g.from, g.offset); + let b = shifted(g.to, g.offset); + let beyond = g.offset + 7.0 * if g.offset < 0.0 { -1.0 } else { 1.0 }; + let ea = shifted(g.from, beyond); + let eb = shifted(g.to, beyond); + let extensions = format!( + "M{},{} L{},{} M{},{} L{},{}", + g.from[0], g.from[1], ea[0], ea[1], g.to[0], g.to[1], eb[0], eb[1] + ); + let tick_a = shifted(a, -5.0); + let tick_b = shifted(a, 5.0); + let tick_c = shifted(b, -5.0); + let tick_d = shifted(b, 5.0); + let ticks = format!( + "M{},{} L{},{} M{},{} L{},{}", + tick_a[0], tick_a[1], tick_b[0], tick_b[1], tick_c[0], tick_c[1], tick_d[0], tick_d[1] + ); + let mut angle = tangent[1].atan2(tangent[0]).to_degrees(); + if angle > 90.0 { + angle -= 180.0; + } + if angle < -90.0 { + angle += 180.0; + } + let mut caliper_ticks = String::new(); + let caliper_rail = if let Some(caliper) = &g.caliper { + let (fixed, moving, direction) = match caliper.moving_end { + MovingEnd::To => (a, b, 1.0), + MovingEnd::From => (b, a, -1.0), + }; + let extended = translate(moving, tangent, direction * caliper.from_extra); + for i in 0..caliper.tick_count { + let fraction = (i + 1) as f64 / (caliper.tick_count + 1) as f64; + let p = lerp(moving, extended, fraction); + let extent = if i % 2 == 0 { 7.0 } else { 4.0 }; + append_segment( + &mut caliper_ticks, + translate(p, normal, -extent), + translate(p, normal, extent), + ); + } + polyline_data(&[fixed, extended]) + } else { + String::new() + }; + PreparedMeasure { + graphic: g.clone(), + geometry: Geometry::new(&[a, b], false), + extensions, + ticks, + center: [f64::midpoint(a[0], b[0]), f64::midpoint(a[1], b[1])], + angle, + from: a, + to: b, + tangent, + normal, + caliper_rail, + caliper_ticks, + } +} + +impl PreparedMeasure { + fn render(&self, progress: f64, linear_progress: f64) -> Svgr<'_> { + if let Some(caliper) = &self.graphic.caliper { + return self.render_caliper(caliper, linear_progress); + } + let g = &self.graphic; + let finish = smooth(((progress - 0.55) / 0.45).clamp(0.0, 1.0)); + let line = draw_supported(&self.geometry, &g.color, g.width, progress, g.keyline); + let support = if g.keyline { + svgr!( + + + ) + } else { + Svgr::empty() + }; + svgr!( + {support} + + {line} + + + {g.label.as_str()} + + ) + } + + fn caliper_pose(&self, caliper: &Caliper, linear_progress: f64) -> CaliperPose { + let close = smooth((linear_progress / caliper.lock_fraction).clamp(0.0, 1.0)); + let extra = caliper.from_extra * (1.0 - close); + let (from, to) = match caliper.moving_end { + MovingEnd::To => (self.from, translate(self.to, self.tangent, extra)), + MovingEnd::From => (translate(self.from, self.tangent, -extra), self.to), + }; + CaliperPose { + from, + to, + aperture: self.geometry.length + extra, + locked: linear_progress >= caliper.lock_fraction, + } + } + + fn render_caliper<'a>(&'a self, caliper: &'a Caliper, linear_progress: f64) -> Svgr<'a> { + if linear_progress <= 0.0 { + return Svgr::empty(); + } + let g = &self.graphic; + let pose = self.caliper_pose(caliper, linear_progress); + let side = if g.offset < 0.0 { -1.0 } else { 1.0 }; + let mut jaws = String::new(); + for (endpoint, inward) in [(pose.from, 1.0), (pose.to, -1.0)] { + let outer = translate(endpoint, self.normal, side * 9.0); + let inner = translate(endpoint, self.normal, -side * caliper.jaw_size); + append_segment(&mut jaws, outer, inner); + append_segment( + &mut jaws, + inner, + translate(inner, self.tangent, inward * 12.0), + ); + } + let span = polyline_data(&[pose.from, pose.to]); + let label = if pose.locked { + g.label.clone() + } else { + format!("{:.*} {}", caliper.precision, pose.aperture, caliper.unit) + }; + let label_center = lerp(pose.from, pose.to, 0.5); + let visible = smooth((linear_progress / 0.1).clamp(0.0, 1.0)); + let support = if g.keyline { + svgr!( + + + ) + } else { + Svgr::empty() + }; + // A small contact ring is a single confirmation, never a repeating pulse. + let confirm = ((linear_progress - caliper.lock_fraction) / (1.0 - caliper.lock_fraction)) + .clamp(0.0, 1.0); + let contact = if pose.locked && confirm < 1.0 { + let endpoint = match caliper.moving_end { + MovingEnd::To => pose.to, + MovingEnd::From => pose.from, + }; + svgr!() + } else { + Svgr::empty() + }; + svgr!( + + + {support} + + + {contact} + + {label} + + ) + } +} + +#[derive(Debug)] +struct CaliperPose { + from: Point, + to: Point, + aperture: f64, + locked: bool, +} + +fn draw_supported<'a>( + geometry: &'a Geometry, + color: &'a str, + width: f64, + progress: f64, + keyline: bool, +) -> Svgr<'a> { + let line = draw(geometry, color, width, progress); + if !keyline { + return line; + } + let support = draw(geometry, "#050a10", width + 3.0, progress); + svgr!({support}{line}) +} + +fn draw<'a>(geometry: &'a Geometry, color: &'a str, width: f64, progress: f64) -> Svgr<'a> { + if progress <= 0.0 { + return Svgr::empty(); + } + svgr!() +} + +fn smooth(value: f64) -> f64 { + value * value * (3.0 - 2.0 * value) +} +fn lerp(a: Point, b: Point, t: f64) -> Point { + [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t] +} +fn translate(point: Point, direction: Point, distance: f64) -> Point { + [ + point[0] + direction[0] * distance, + point[1] + direction[1] * distance, + ] +} +fn polyline_data(points: &[Point]) -> String { + let mut data = format!("M{},{}", points[0][0], points[0][1]); + for point in &points[1..] { + let _ = write!(data, " L{},{}", point[0], point[1]); + } + data +} +fn append_segment(data: &mut String, from: Point, to: Point) { + let _ = write!(data, " M{},{} L{},{}", from[0], from[1], to[0], to[1]); +} +fn distance(a: Point, b: Point) -> f64 { + (a[0] - b[0]).hypot(a[1] - b[1]) +} +fn unit(point: Point) -> Point { + let length = point[0].hypot(point[1]); + if length > 1e-9 { + [point[0] / length, point[1] / length] + } else { + [1.0, 0.0] + } +} +fn range(value: f64, min: f64, max: f64, path: &str) -> Result<(), CaptionError> { + if !value.is_finite() || value < min || value > max { + return Err(CaptionError::invalid( + path, + format!("must be finite and between {min} and {max}"), + )); + } + Ok(()) +} +fn validate_point(point: Point, path: &str) -> Result<(), CaptionError> { + range(point[0], -3840.0, 5760.0, &format!("{path}[0]"))?; + range(point[1], -2160.0, 3240.0, &format!("{path}[1]")) +} +fn color(value: &str, path: &str) -> Result<(), CaptionError> { + if value.trim().is_empty() { + return Err(CaptionError::invalid(path, "color cannot be empty")); + } + Ok(()) +} +fn text(value: &str, family: &str, size: usize, path: &str) -> Result<(), CaptionError> { + if value.trim().is_empty() || family.trim().is_empty() || !(8..=120).contains(&size) { + return Err(CaptionError::invalid( + path, + "text and font family must be nonempty; font size must be 8–120 pixels", + )); + } + Ok(()) +} diff --git a/examples/captions-showcase/src/editorial.rs b/examples/captions-showcase/src/editorial.rs new file mode 100644 index 00000000..b13b1d63 --- /dev/null +++ b/examples/captions-showcase/src/editorial.rs @@ -0,0 +1,1056 @@ +//! Persistent phrases, optical materials and glyph motion directed by subtitle metadata. +//! Font geometry comes from Captions; application metadata is lazily decoded and cached there. +use std::sync::OnceLock; + +use fframes::{ + EstimateTextWidthOptions, FFramesContext, FontStretch, FontStyle, Frame, Svgr, + animation::{AnimationRuntime, Easing}, + captions::{ + CaptionCue, CaptionError, CaptionMode, CaptionRect, CaptionTrack, CaptionWordScope, + Captions, WordState, + }, + estimate_text_width, svgr, +}; +use serde::Deserialize; + +#[path = "choreography.rs"] +mod choreography; +pub use choreography::{Echo, GlyphMotion, Replica}; + +#[path = "cue_graphics.rs"] +mod cue_graphics; + +#[path = "word_plane.rs"] +mod word_plane; +pub use word_plane::PlaneMotion; + +use crate::{ + annotations::{self, Accent, AccentKind}, + word_effects::{ + GlassNebulaTuning, GooTuning, IrradianceTuning, LiquidTuning, MaterialControls, + ReliefTuning, WordDirection, WordEffects, + }, +}; + +fn scene_enter() -> f64 { + 0.16 +} +fn scene_exit() -> f64 { + 0.15 +} +fn word_enter() -> f64 { + 0.18 +} +fn family() -> String { + "Barlow Condensed".into() +} +fn weight() -> u16 { + 600 +} +fn paper() -> String { + "#f3eee3".into() +} +fn ink() -> String { + "#080c15".into() +} +fn outline_width() -> f64 { + 1.2 +} + +/// Cue-level application data. Extra top-level keys remain available to other consumers. +#[derive(Debug, Clone, Deserialize)] +pub struct CueStyle { + pub editorial: SceneStyle, + /// Optional composition endpoint. Its containing cue must be the final cue; + /// that cue's end supplies the duration, without another copy of the clock. + #[serde(default)] + pub output: Option, +} + +/// Fades applied at an explicitly authored composition end. Omitted fades mean +/// a cut; tracks without an output marker retain the application's old policy. +#[derive(Debug, Clone, Copy, Default, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct OutputFades { + pub picture_fade_seconds: f64, + pub voice_fade_seconds: f64, + pub music_fade_seconds: f64, +} + +/// Validated output settings resolved once from the loaded caption metadata. +#[derive(Debug, Clone, Copy)] +pub struct OutputPolicy { + pub end_seconds: f64, + pub picture_fade_seconds: f64, + pub voice_fade_seconds: f64, + pub music_fade_seconds: f64, +} + +impl OutputFades { + fn resolve(self, end_seconds: f64, path: &str) -> Result { + for (name, value) in [ + ("picture_fade_seconds", self.picture_fade_seconds), + ("voice_fade_seconds", self.voice_fade_seconds), + ("music_fade_seconds", self.music_fade_seconds), + ] { + range(value, 0.0, end_seconds, &format!("{path}.{name}"))?; + } + Ok(OutputPolicy { + end_seconds, + picture_fade_seconds: self.picture_fade_seconds, + voice_fade_seconds: self.voice_fade_seconds, + music_fade_seconds: self.music_fade_seconds, + }) + } +} + +/// Resolve an optional output marker through the same cached cue metadata used +/// for drawing. No file is reopened and the subtitle JSON is never reparsed. +pub fn output_policy(track: &CaptionTrack) -> Result, CaptionError> { + let mut selected = None; + for (index, cue) in track.cues().iter().enumerate() { + if cue + .metadata + .get("output") + .is_none_or(serde_json::Value::is_null) + { + continue; + } + let path = format!("cues[{index}].metadata.output"); + if selected.is_some() { + return Err(CaptionError::invalid( + path, + "only one cue may define output settings", + )); + } + let fades = cue + .metadata + .typed::() + .and_then(|style| style.output) + .ok_or_else(|| { + CaptionError::invalid( + &path, + "provide CueStyle metadata with valid output fade settings", + ) + })?; + selected = Some((index, fades)); + } + let Some((index, fades)) = selected else { + return Ok(None); + }; + let path = format!("cues[{index}].metadata.output"); + if index + 1 != track.cues().len() { + return Err(CaptionError::invalid( + path, + "output settings must belong to the final cue", + )); + } + let end_seconds = track.cues()[index].end; + if track.cues().iter().any(|cue| cue.end > end_seconds) { + return Err(CaptionError::invalid( + path, + "the output end must not truncate an overlapping cue", + )); + } + fades.resolve(end_seconds, &path).map(Some) +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SceneStyle { + #[serde(default = "scene_enter")] + pub enter_seconds: f64, + #[serde(default = "scene_exit")] + pub exit_seconds: f64, + #[serde(default)] + pub graphics: Vec, +} + +/// Word-level application data, decoded lazily once per metadata object. +#[derive(Debug, Clone, Deserialize)] +pub struct WordStyle { + pub layout: WordLayout, + #[serde(default)] + pub appearance: Appearance, + #[serde(default)] + pub motion: Motion, + #[serde(default)] + pub accents: Vec, + #[serde(default)] + pub material: Option, + #[serde(default)] + pub replicas: Vec, + #[serde(default)] + pub plane: Option, +} + +/// Application-level material names. The subtitle parser does not interpret them. +#[derive(Debug, Clone, Copy, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum MaterialEffect { + Plasma, + Nebula, + Diffraction, + Etch, + Interference, + Discharge, + Shear, + Mercury, + Fracture, + ScanGlass, + Engraving, + Wavefront, + Quartz, + Crystal, + ThinFilm, + Horizon, + Astral, + Combustion, + Fluid, + Raster, + Contour, + Prism, + Lattice, + Flux, + Stress, + Lamella, + Photonic, + LiquidMetal, + Irradiance, + Goo, + GlassNebula, +} + +impl MaterialEffect { + pub fn index(self) -> usize { + self as usize + } +} + +fn material_variation() -> f32 { + 0.5 +} +fn material_hero() -> bool { + true +} +fn material_exit() -> f64 { + 0.18 +} +fn material_rest() -> f64 { + 0.25 +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Material { + pub effect: MaterialEffect, + /// Optional art controls for the geometric lamella/photonic materials. + #[serde(default)] + pub relief: Option, + /// Optional controls ported from shaders.com's `LiquidMetal` material. + #[serde(default)] + pub liquid: Option, + /// Optional controls for shaders.com's edge-light transport material. + #[serde(default)] + pub irradiance: Option, + /// Optional controls for shaders.com's thick translucent Goo material. + #[serde(default)] + pub goo: Option, + /// Optional controls for shaders.com's glass-enclosed Nebula material. + #[serde(default)] + pub glass_nebula: Option, + #[serde(default = "material_variation")] + pub variation: f32, + #[serde(default = "material_hero")] + pub hero: bool, + #[serde(default)] + pub warm: bool, + /// Absolute visual release time; omitted means the end of the cue. + #[serde(default)] + pub hold_until: Option, + #[serde(default = "material_exit")] + pub exit_seconds: f64, + /// Return the highlighted word to ordinary text while its phrase stays visible. + #[serde(default)] + pub rest_at: Option, + #[serde(default = "material_rest")] + pub rest_seconds: f64, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct WordLayout { + pub font_size: usize, + pub center_x: f64, + pub baseline: f64, + #[serde(default)] + pub angle: f64, + #[serde(default = "family")] + pub font_family: String, + #[serde(default = "weight")] + pub font_weight: u16, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct Appearance { + pub fill: String, + pub outline: String, + pub outline_width: f64, + pub outline_before: bool, + pub shadow_blur: f64, + pub shadow_opacity: f64, + pub background: Option, + pub background_padding: f64, + pub background_radius: f64, +} + +impl Default for Appearance { + fn default() -> Self { + Self { + fill: paper(), + outline: ink(), + outline_width: outline_width(), + outline_before: false, + shadow_blur: 4.0, + shadow_opacity: 0.8, + background: None, + background_padding: 12.0, + background_radius: 0.0, + } + } +} + +#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "kebab-case")] +pub enum Reveal { + Cue, + #[default] + Word, +} + +#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "kebab-case")] +pub enum Entrance { + None, + #[default] + Rise, + Stamp, + Wipe, + SlideLeft, + SlideRight, + Aperture, + Arc, + GlyphHop, + GlyphAssemble, + Tracking, + Flip, + Choreograph, +} + +impl Entrance { + fn uses_glyphs(self) -> bool { + matches!( + self, + Self::GlyphHop | Self::GlyphAssemble | Self::Tracking | Self::Flip | Self::Choreograph + ) + } +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct Motion { + pub reveal: Reveal, + pub entrance: Entrance, + pub enter_seconds: f64, + /// Delay between adjacent glyphs, independent of spoken word timing. + pub stagger_seconds: f64, + /// Maximum displacement in pixels, or total spread for tracking. + pub amplitude: f64, + /// Optional absolute visual start, without changing the spoken timestamp. + pub start: Option, + /// Reverse the order of the character stagger without reversing text. + pub reverse: bool, + /// Explicit character paths relative to the measured resting positions. + pub choreography: Vec, + /// Filled samples of the same authored paths at future instants. + pub echoes: Vec, +} + +impl Default for Motion { + fn default() -> Self { + Self { + reveal: Reveal::Word, + entrance: Entrance::Rise, + enter_seconds: word_enter(), + stagger_seconds: 0.025, + amplitude: 32.0, + start: None, + reverse: false, + choreography: Vec::new(), + echoes: Vec::new(), + } + } +} + +struct EditorialWord { + captions: Captions, + starts: f64, + entrance: AnimationRuntime, + glyphs: Vec, + glyph_layout: OnceLock>, + plane_clearance: OnceLock, + choreography: choreography::Choreography, + plane: word_plane::PlaneRenderer, +} + +struct GlyphPlacement { + x: f64, + width: f64, +} + +pub struct EditorialCaption { + cue: CaptionCue, + scene: SceneStyle, + words: Vec, + exit: AnimationRuntime, + graphics: cue_graphics::PreparedGraphics, +} + +fn range(value: f64, min: f64, max: f64, path: &str) -> Result<(), CaptionError> { + if !value.is_finite() || !(min..=max).contains(&value) { + return Err(CaptionError::invalid( + path, + format!("must be finite and between {min} and {max}"), + )); + } + Ok(()) +} + +impl EditorialCaption { + pub fn new(cue: CaptionCue) -> Result { + let path = format!("cue[{}]", cue.id.as_deref().unwrap_or("unnamed")); + let scene = cue + .metadata + .typed::() + .ok_or_else(|| { + CaptionError::invalid( + format!("{path}.metadata.editorial"), + "provide CueStyle metadata with valid editorial settings", + ) + })? + .editorial + .clone(); + range( + scene.enter_seconds, + 0.01, + 2.0, + &format!("{path}.metadata.editorial.enter_seconds"), + )?; + range( + scene.exit_seconds, + 0.01, + 2.0, + &format!("{path}.metadata.editorial.exit_seconds"), + )?; + if cue.words.is_empty() { + return Err(CaptionError::invalid( + format!("{path}.words"), + "editorial composition requires timed words", + )); + } + let mut words = Vec::with_capacity(cue.words.len()); + for (index, word) in cue.words.iter().enumerate() { + let path = format!("{path}.words[{index}].metadata"); + let style = word.metadata.typed::().ok_or_else(|| { + CaptionError::invalid(&path, + "provide WordStyle metadata with layout, appearance, motion and accents with valid types") + })?; + if !(16..=700).contains(&style.layout.font_size) { + return Err(CaptionError::invalid( + format!("{path}.layout.font_size"), + "must be between 16 and 700 pixels", + )); + } + range( + style.layout.center_x, + 0.0, + 1920.0, + &format!("{path}.layout.center_x"), + )?; + range( + style.layout.baseline, + 0.0, + 1080.0, + &format!("{path}.layout.baseline"), + )?; + range( + style.layout.angle, + -15.0, + 15.0, + &format!("{path}.layout.angle"), + )?; + if style.layout.font_family.trim().is_empty() + || !(100..=900).contains(&style.layout.font_weight) + { + return Err(CaptionError::invalid( + format!("{path}.layout"), + "font family must be nonempty and weight must be between 100 and 900", + )); + } + range( + style.motion.enter_seconds, + 0.01, + 2.0, + &format!("{path}.motion.enter_seconds"), + )?; + range( + style.motion.stagger_seconds, + 0.0, + 0.2, + &format!("{path}.motion.stagger_seconds"), + )?; + range( + style.motion.amplitude, + 0.0, + 400.0, + &format!("{path}.motion.amplitude"), + )?; + if let Some(start) = style.motion.start { + range(start, cue.start, cue.end, &format!("{path}.motion.start"))?; + } + if !style.motion.choreography.is_empty() + && style.motion.entrance != Entrance::Choreograph + { + return Err(CaptionError::invalid( + format!("{path}.motion.choreography"), + "explicit glyph paths require entrance: choreograph", + )); + } + if style.motion.entrance == Entrance::Choreograph + && style.motion.choreography.is_empty() + { + return Err(CaptionError::invalid( + format!("{path}.motion.choreography"), + "entrance: choreograph needs at least one explicit character path", + )); + } + let choreography = choreography::Choreography::prepare( + &style.motion.choreography, + word.text.chars().count(), + &format!("{path}.motion.choreography"), + )?; + choreography::validate_echoes(&style.motion.echoes, &format!("{path}.motion.echoes"))?; + if !style.motion.echoes.is_empty() && style.motion.entrance != Entrance::Choreograph { + return Err(CaptionError::invalid( + format!("{path}.motion.echoes"), + "future echoes require explicit glyph choreography", + )); + } + choreography::validate_replicas( + &style.replicas, + cue.start, + cue.end, + &format!("{path}.replicas"), + )?; + if let Some(plane) = &style.plane { + plane.validate(cue.start, cue.end, &format!("{path}.plane"))?; + if style.material.is_some() || style.motion.entrance.uses_glyphs() { + return Err(CaptionError::invalid( + format!("{path}.plane"), + "projective planes require a plain whole-word caption; material shaders and per-glyph entrances are separate techniques", + )); + } + if style.appearance.outline_before { + return Err(CaptionError::invalid( + format!("{path}.appearance.outline_before"), + "a projective plane captures one stable glyph appearance; use a filled caption throughout its cue", + )); + } + } + if let Some(material) = &style.material { + if let Some(relief) = material.relief { + if !matches!( + material.effect, + MaterialEffect::Lamella | MaterialEffect::Photonic + ) { + return Err(CaptionError::invalid( + format!("{path}.material.relief"), + "relief controls require lamella or photonic", + )); + } + relief.validate().map_err(|message| { + CaptionError::invalid(format!("{path}.material.relief"), message) + })?; + } + if let Some(liquid) = material.liquid { + if !matches!(material.effect, MaterialEffect::LiquidMetal) { + return Err(CaptionError::invalid( + format!("{path}.material.liquid"), + "liquid controls require liquid-metal", + )); + } + liquid.validate().map_err(|message| { + CaptionError::invalid(format!("{path}.material.liquid"), message) + })?; + } + if let Some(tuning) = material.irradiance { + if !matches!(material.effect, MaterialEffect::Irradiance) { + return Err(CaptionError::invalid( + format!("{path}.material.irradiance"), + "irradiance controls require irradiance", + )); + } + tuning.validate().map_err(|message| { + CaptionError::invalid(format!("{path}.material.irradiance"), message) + })?; + } + if let Some(tuning) = material.goo { + if !matches!(material.effect, MaterialEffect::Goo) { + return Err(CaptionError::invalid( + format!("{path}.material.goo"), + "goo controls require goo", + )); + } + tuning.validate().map_err(|message| { + CaptionError::invalid(format!("{path}.material.goo"), message) + })?; + } + if let Some(tuning) = material.glass_nebula { + if !matches!(material.effect, MaterialEffect::GlassNebula) { + return Err(CaptionError::invalid( + format!("{path}.material.glass_nebula"), + "glass_nebula controls require glass-nebula", + )); + } + tuning.validate().map_err(|message| { + CaptionError::invalid(format!("{path}.material.glass_nebula"), message) + })?; + } + range( + f64::from(material.variation), + 0.0, + 1.0, + &format!("{path}.material.variation"), + )?; + range( + material.exit_seconds, + 0.01, + 2.0, + &format!("{path}.material.exit_seconds"), + )?; + range( + material.rest_seconds, + 0.01, + 2.0, + &format!("{path}.material.rest_seconds"), + )?; + if let Some(rest_at) = material.rest_at { + range( + rest_at, + word.end, + cue.end, + &format!("{path}.material.rest_at"), + )?; + } + if let Some(until) = material.hold_until { + range( + until, + word.end, + cue.end, + &format!("{path}.material.hold_until"), + )?; + } + if style.motion.entrance.uses_glyphs() { + return Err(CaptionError::invalid( + format!("{path}.motion.entrance"), + "this example's glyph entrances require plain text; use rise, stamp, wipe, slide-left or slide-right with a material", + )); + } + } + range( + style.appearance.outline_width, + 0.0, + 20.0, + &format!("{path}.appearance.outline_width"), + )?; + range( + style.appearance.shadow_blur, + 0.0, + 20.0, + &format!("{path}.appearance.shadow_blur"), + )?; + range( + style.appearance.shadow_opacity, + 0.0, + 1.0, + &format!("{path}.appearance.shadow_opacity"), + )?; + range( + style.appearance.background_padding, + 0.0, + 100.0, + &format!("{path}.appearance.background_padding"), + )?; + range( + style.appearance.background_radius, + 0.0, + 100.0, + &format!("{path}.appearance.background_radius"), + )?; + for (index, accent) in style.accents.iter().enumerate() { + let accent_path = format!("{path}.accents[{index}]"); + range( + accent.start, + cue.start, + cue.end, + &format!("{accent_path}.start"), + )?; + range( + accent.duration, + 0.01, + 3.0, + &format!("{accent_path}.duration"), + )?; + range(accent.width, 0.1, 30.0, &format!("{accent_path}.width"))?; + range(accent.padding, 0.0, 90.0, &format!("{accent_path}.padding"))?; + } + let starts = style.motion.start.unwrap_or(match style.motion.reveal { + Reveal::Cue => cue.start, + Reveal::Word => word.start, + }); + let enter_seconds = if style.motion.reveal == Reveal::Cue + && style.motion.start.is_none() + && !style.motion.entrance.uses_glyphs() + { + scene.enter_seconds + } else { + style.motion.enter_seconds + }; + let word_cue = CaptionCue::words(cue.start, cue.end, [word.clone()]) + .with_metadata(cue.metadata.clone()); + let captions = Captions::new( + CaptionTrack::new([word_cue])?, + CaptionRect::new(30.0, 0.0, 1860.0, 1080.0), + ) + .clock(fframes::captions::CaptionClock::Global) + .font(style.layout.font_family.clone(), style.layout.font_size) + .font_weight(style.layout.font_weight) + .line_height(1.0) + .mode(CaptionMode::Highlight) + .outline( + style.appearance.outline.clone(), + style.appearance.outline_width, + ) + .upcoming_fill(if style.appearance.outline_before { + "none".to_owned() + } else { + style.appearance.fill.clone() + }) + .active_fill(style.appearance.fill.clone()) + .spoken_fill(style.appearance.fill.clone()); + words.push(EditorialWord { + captions, + starts, + entrance: AnimationRuntime::new(enter_seconds as f32, &Easing::EaseOut), + glyphs: word.text.chars().map(|glyph| glyph.to_string()).collect(), + glyph_layout: OnceLock::new(), + plane_clearance: OnceLock::new(), + choreography, + plane: word_plane::PlaneRenderer::new(), + }); + } + let exit = AnimationRuntime::new(scene.exit_seconds as f32, &Easing::EaseIn); + let graphics = cue_graphics::PreparedGraphics::new(&scene.graphics, cue.start, cue.end)?; + Ok(Self { + cue, + scene, + words, + exit, + graphics, + }) + } + + pub fn render<'a>( + &'a self, + frame: &mut Frame, + ctx: &FFramesContext<'a, '_>, + effects: &'a WordEffects, + effect_override: Option, + ) -> Svgr<'a> { + let time = frame.global_index as f64 / frame.fps as f64; + let opacity = 1.0 - ease(&self.exit, time - (self.cue.end - self.scene.exit_seconds)); + let words: Svgr<'a> = self.words.iter().filter(|word| time >= word.starts).map(|word| { + let progress = ease(&word.entrance, time - word.starts); + word.captions.render_with(frame, ctx, |scope, text| { + // Constructor validation initializes this shared cache once. Rendering + // only borrows it, including when frames are evaluated out of order. + let Some(style) = scope.word.metadata.typed::() else { return text; }; + let width = scope.width(); + let height = scope.height(); + let (shift, rise, scale_x, scale_y) = match style.motion.entrance { + Entrance::Rise => (0.0, 16.0 * (1.0 - progress), 1.0, 1.0), + Entrance::Stamp => (0.0, 0.0, 1.0 + 0.08 * (1.0 - progress), 1.0 + 0.08 * (1.0 - progress)), + Entrance::SlideLeft => (-style.motion.amplitude * (1.0 - progress), 0.0, 1.0, 1.0), + Entrance::SlideRight => (style.motion.amplitude * (1.0 - progress), 0.0, 1.0, 1.0), + Entrance::Aperture => (0.0, 0.0, 1.0 - 0.12 * (1.0 - progress), 1.0 + 0.08 * (1.0 - progress)), + Entrance::Arc => { + let linear = ((time - word.starts) / f64::from(word.entrance.get_duration())).clamp(0.0, 1.0); + (-style.motion.amplitude * (1.0 - progress), + -style.motion.amplitude * (std::f64::consts::PI * linear).sin(), 1.0, 1.0) + } + _ => (0.0, 0.0, 1.0, 1.0), + }; + let dx = style.layout.center_x - width * 0.5 - scope.composition_rect.x + shift; + let dy = style.layout.baseline - style.layout.font_size as f64 + rise - scope.composition_rect.y; + let cx = width * 0.5; + let cy = height * 0.5; + let underlay = style.accents.iter().filter(|accent| accent.kind == AccentKind::Marker) + .map(|accent| annotations::render(scope, accent, time)).collect::>(); + let overlay = style.accents.iter().filter(|accent| accent.kind != AccentKind::Marker) + .map(|accent| annotations::render(scope, accent, time)).collect::>(); + let replicas = choreography::replicas(scope, &style.replicas, time); + let background = style.appearance.background.as_ref().map(|fill| { + let padding = style.appearance.background_padding; + svgr!() + }).unwrap_or_default(); + let wipe_id = (style.motion.entrance == Entrance::Wipe && progress < 1.0) + .then(|| scope.id("editorial-wipe")); + let shadow_id = (style.appearance.shadow_blur > 0.0 && style.appearance.shadow_opacity > 0.0) + .then(|| scope.id("editorial-shadow")); + // Optical effects reserve 72px outside the glyph texture. Include + // that region plus three shadow deviations so flames and stars + // are not clipped by an otherwise ordinary word shadow filter. + let effect_padding = if style.material.is_some() || effect_override.is_some() { 72.0 } else { 0.0 }; + let motion_padding = word.choreography.shadow_clearance(time - word.starts, style.layout.font_size as f64); + let plane_padding = style.plane.as_ref().map_or(0.0, |plane| { + *word.plane_clearance.get_or_init(|| plane.shadow_clearance(width, height)) + }); + let shadow_padding = (effect_padding + motion_padding + plane_padding + 3.0 * style.appearance.shadow_blur + + style.appearance.outline_width + 4.0).max(40.0); + let material = effect_override.map(|effect| (effect, WordDirection { + hold_until: Some(self.cue.end), exit_seconds: self.scene.exit_seconds, + hero: true, warm: false, variation: 0.5, + })).or_else(|| style.material.as_ref().map(|material| ( + material.effect.index(), WordDirection { + hold_until: Some(material.hold_until.unwrap_or(self.cue.end)), + exit_seconds: material.exit_seconds, hero: material.hero, + warm: material.warm, variation: material.variation, + } + ))); + let glyphs = (material.is_none() && style.motion.entrance.uses_glyphs()) + .then(|| word.render_glyphs(scope, ctx, style, time)) + .flatten(); + let rest = if effect_override.is_none() { + style.material.as_ref().and_then(|material| material.rest_at.map(|at| + ((time - at) / material.rest_seconds).clamp(0.0, 1.0) + )).unwrap_or(0.0) + } else { 0.0 }; + let content = if effect_override.is_none() && let Some(plane) = &style.plane { + word.plane.render(scope, &text, ctx, plane, time, effects.shaders_enabled()) + } else if rest < 1.0 && scope.word.state != WordState::Upcoming + && let Some((effect, direction)) = material { + let plain = text.clone(); + let relief = style.material.as_ref().and_then(|material| material.relief).unwrap_or_default(); + let liquid = style.material.as_ref().and_then(|material| material.liquid).unwrap_or_default(); + let irradiance = style.material.as_ref().and_then(|material| material.irradiance).unwrap_or_default(); + let goo = style.material.as_ref().and_then(|material| material.goo).unwrap_or_default(); + let glass_nebula = style.material.as_ref().and_then(|material| material.glass_nebula).unwrap_or_default(); + let highlighted = effects.render_directed_with_controls(scope, text, ctx, effect, direction, &MaterialControls { relief, liquid, irradiance, goo, glass_nebula }); + if rest > 0.0 { + let blend = rest * rest * (3.0 - 2.0 * rest); + svgr!( + {highlighted} + {plain} + ) + } else { highlighted } + } else { + // A whole phrase may enter before it is spoken. Keep those words + // readable; the material starts on their original speech time. + glyphs.unwrap_or(text) + }; + let content = if let Some(id) = &shadow_id { + svgr!( + + + + {content} + ) + } else { content }; + let local = if let Some(id) = wipe_id { + let (pad_x, pad_y) = if shadow_id.is_some() { (shadow_padding, shadow_padding) } else { (16.0, 30.0) }; + let revealed = (width + 2.0 * pad_x) * (0.12 + 0.88 * progress); + svgr!( + + + + {background}{replicas}{underlay}{content}{overlay} + ) + } else { + svgr!({background}{replicas}{underlay}{content}{overlay}) + }; + svgr!({local}) + }) + }).collect(); + if self.scene.graphics.is_empty() { + return words; + } + let under = self.graphics.render(cue_graphics::Layer::Under, time); + let over = self.graphics.render(cue_graphics::Layer::Over, time); + svgr!({under}{words}{over}) + } +} + +fn ease(runtime: &AnimationRuntime, elapsed: f64) -> f64 { + if elapsed <= 0.0 { + 0.0 + } else if elapsed >= f64::from(runtime.get_duration()) { + 1.0 + } else { + f64::from(runtime.solve(&(elapsed as f32))) + } +} + +impl EditorialWord { + fn measured_glyphs<'a>( + &'a self, + ctx: &FFramesContext<'a, '_>, + style: &'a WordStyle, + ) -> Option<&'a [GlyphPlacement]> { + if let Some(layout) = self.glyph_layout.get() { + return Some(layout); + } + // Use exactly the caption engine's font lookup and advances. Missing fonts + // are not cached: the editor may provide them on a later frame. + let mut x = 0.0; + let mut layout = Vec::with_capacity(self.glyphs.len()); + for glyph in &self.glyphs { + let width = estimate_text_width( + ctx, + glyph, + EstimateTextWidthOptions { + font_family: &style.layout.font_family, + font_size: style.layout.font_size, + font_weight: style.layout.font_weight, + font_style: FontStyle::default(), + font_stretch: FontStretch::default(), + }, + )? as f64; + layout.push(GlyphPlacement { x, width }); + x += width; + } + // Concurrent render workers can compute the same small layout on first use; + // all subsequent frames share the first successful immutable result. + let _ = self.glyph_layout.set(layout); + self.glyph_layout.get().map(Vec::as_slice) + } + + fn render_glyphs<'a>( + &'a self, + scope: &CaptionWordScope<'a, '_>, + ctx: &FFramesContext<'a, '_>, + style: &'a WordStyle, + time: f64, + ) -> Option> { + let duration = f64::from(self.entrance.get_duration()); + let elapsed = time - self.starts; + // Keep the same measured glyphs through the hold. Switching back to a + // shaped whole-word node here can change kerning on the settling frame. + let layout = self.measured_glyphs(ctx, style)?; + let fill = if scope.word.state == WordState::Upcoming && style.appearance.outline_before { + "none" + } else { + style.appearance.fill.as_str() + }; + let foreground: Svgr<'a> = self.glyphs.iter().zip(layout).enumerate().map(|(index, (glyph, placement))| { + let order = if style.motion.reverse { self.glyphs.len() - 1 - index } else { index }; + let local = elapsed - order as f64 * style.motion.stagger_seconds; + let progress = (local / duration).clamp(0.0, 1.0); + let eased = ease(&self.entrance, local); + let remaining = 1.0 - eased; + let direction = if index % 2 == 0 { -1.0 } else { 1.0 }; + let amplitude = style.motion.amplitude; + let mut x = 0.0; + let mut y = 0.0; + let mut scale_y = 1.0; + let mut scale_x = 1.0; + let mut skew = 0.0; + let mut rotation = 0.0; + let mut opacity = 1.0; + match style.motion.entrance { + Entrance::GlyphHop => { + let arc = (std::f64::consts::PI * progress).sin(); + y = -amplitude * arc; + rotation = direction * 2.0 * arc; + } + Entrance::GlyphAssemble => { + x = direction * amplitude * 0.35 * remaining; + y = direction * amplitude * remaining; + rotation = direction * 4.0 * remaining; + opacity = 0.55 + 0.45 * eased; + } + Entrance::Tracking => { + let position = if self.glyphs.len() > 1 { + index as f64 / (self.glyphs.len() - 1) as f64 - 0.5 + } else { 0.0 }; + x = position * amplitude * remaining; + } + Entrance::Flip => { + scale_y = 0.22 + 0.78 * eased; + skew = direction * 12.0 * remaining; + } + Entrance::Choreograph => { + let pose = self.choreography.pose(index, elapsed); + x = pose.x; + y = pose.y; + scale_x = pose.scale * pose.scale_x; + scale_y = pose.scale * pose.scale_y; + rotation = pose.rotate; + opacity = pose.opacity; + } + _ => {} + } + let cx = placement.x + placement.width * 0.5; + let cy = style.layout.font_size as f64 * 0.65; + svgr!( + {glyph.as_str()} + ) + }).collect(); + if style.motion.echoes.is_empty() { + return Some(foreground); + } + let echoes: Vec<_> = style.motion.echoes.iter().rev().flat_map(|echo| { + self.glyphs.iter().zip(layout).enumerate().filter_map(move |(index, (glyph, placement))| { + let pose = self.choreography.echo_pose(index, elapsed, echo)?; + let cx = placement.x + placement.width * 0.5; + let cy = style.layout.font_size as f64 * 0.65; + let sx = pose.scale * pose.scale_x; + let sy = pose.scale * pose.scale_y; + Some(svgr!( + {glyph.as_str()} + )) + }) + }).collect(); + Some(svgr!({echoes}{foreground})) + } +} diff --git a/examples/captions-showcase/src/glass_nebula_material.rs b/examples/captions-showcase/src/glass_nebula_material.rs new file mode 100644 index 00000000..fee83d0f --- /dev/null +++ b/examples/captions-showcase/src/glass_nebula_material.rs @@ -0,0 +1,220 @@ +use super::LiquidColor; +use fframes::ShaderUniforms; + +/// Shaders.com Nebula controls over a cached `FFrames` glyph volume. +/// Colors interpolate in linear sRGB; the upstream color-space selector is not exposed. +#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct GlassNebulaTuning { + pub core_color: LiquidColor, + pub gas_color: LiquidColor, + pub veil_color: LiquidColor, + /// Maximum optical chord through a rounded glyph stem, in gas-domain units. + /// This adapts the upstream sphere/flat-shape thickness to thin text strokes. + pub optical_depth: f32, + pub density: f32, + pub cavity: f32, + pub dust: f32, + pub gas_scale: f32, + pub billow: f32, + pub glow: f32, + pub seed: f32, + pub stars: f32, + pub star_scale: f32, + pub twinkle: f32, + pub refraction: f32, + pub environment: f32, + pub highlight: f32, + pub highlight_softness: f32, + pub light_angle: f32, + pub speed: f32, +} + +impl Default for GlassNebulaTuning { + fn default() -> Self { + Self { + core_color: LiquidColor::from_srgb_bytes([255, 217, 176]), + gas_color: LiquidColor::from_srgb_bytes([255, 94, 122]), + veil_color: LiquidColor::from_srgb_bytes([58, 91, 219]), + optical_depth: 0.65, + density: 1.0, + cavity: 0.0, + dust: 0.55, + gas_scale: 0.4, + billow: 0.6, + glow: 0.0, + seed: 0.0, + stars: 0.07, + star_scale: 1.0, + twinkle: 0.35, + refraction: 0.5, + environment: 0.7, + highlight: 1.15, + highlight_softness: 0.5, + light_angle: 315.0, + speed: 0.3, + } + } +} + +impl GlassNebulaTuning { + pub fn validate(self) -> Result<(), &'static str> { + for (value, low, high, error) in [ + ( + self.optical_depth, + 0.05, + 1.2, + "glass_nebula.optical_depth must be finite and within 0.05..=1.2", + ), + ( + self.density, + 0.0, + 1.0, + "glass_nebula.density must be finite and within 0..=1", + ), + ( + self.cavity, + 0.0, + 1.0, + "glass_nebula.cavity must be finite and within 0..=1", + ), + ( + self.dust, + 0.0, + 1.0, + "glass_nebula.dust must be finite and within 0..=1", + ), + ( + self.gas_scale, + 0.1, + 1.0, + "glass_nebula.gas_scale must be finite and within 0.1..=1", + ), + ( + self.billow, + 0.0, + 1.0, + "glass_nebula.billow must be finite and within 0..=1", + ), + ( + self.glow, + 0.0, + 2.0, + "glass_nebula.glow must be finite and within 0..=2", + ), + ( + self.seed, + 0.0, + 100.0, + "glass_nebula.seed must be finite and within 0..=100", + ), + ( + self.stars, + 0.0, + 1.0, + "glass_nebula.stars must be finite and within 0..=1", + ), + ( + self.star_scale, + 0.25, + 3.0, + "glass_nebula.star_scale must be finite and within 0.25..=3", + ), + ( + self.twinkle, + 0.0, + 1.0, + "glass_nebula.twinkle must be finite and within 0..=1", + ), + ( + self.refraction, + 0.0, + 2.0, + "glass_nebula.refraction must be finite and within 0..=2", + ), + ( + self.environment, + 0.0, + 2.0, + "glass_nebula.environment must be finite and within 0..=2", + ), + ( + self.highlight, + 0.0, + 4.0, + "glass_nebula.highlight must be finite and within 0..=4", + ), + ( + self.highlight_softness, + 0.0, + 1.0, + "glass_nebula.highlight_softness must be finite and within 0..=1", + ), + ( + self.light_angle, + 0.0, + 360.0, + "glass_nebula.light_angle must be finite degrees within 0..=360", + ), + ( + self.speed, + 0.0, + 4.0, + "glass_nebula.speed must be finite and within 0..=4", + ), + ] { + if !value.is_finite() || !(low..=high).contains(&value) { + return Err(error); + } + } + for color in [self.core_color, self.gas_color, self.veil_color] { + if color + .0 + .iter() + .any(|value| !value.is_finite() || !(0.0..=1.0).contains(value)) + { + return Err("glass_nebula colors must contain finite linear RGB within 0..=1"); + } + } + Ok(()) + } + + pub fn bind(self, uniforms: ShaderUniforms) -> ShaderUniforms { + uniforms + .float3( + "uCoreColor", + self.core_color.0[0], + self.core_color.0[1], + self.core_color.0[2], + ) + .float3( + "uGasColor", + self.gas_color.0[0], + self.gas_color.0[1], + self.gas_color.0[2], + ) + .float3( + "uVeilColor", + self.veil_color.0[0], + self.veil_color.0[1], + self.veil_color.0[2], + ) + .float("uDensity", self.density) + .float("uOpticalDepth", self.optical_depth) + .float("uCavity", self.cavity) + .float("uDust", self.dust) + .float("uGasScale", self.gas_scale) + .float("uBillow", self.billow) + .float("uGlow", self.glow) + .float("uSeed", self.seed) + .float("uStars", self.stars) + .float("uStarScale", self.star_scale) + .float("uTwinkle", self.twinkle) + .float("uRefraction", self.refraction) + .float("uEnvironment", self.environment) + .float("uHighlight", self.highlight) + .float("uHighlightSoftness", self.highlight_softness) + .float("uLightAngle", self.light_angle) + .float("uSpeed", self.speed) + } +} diff --git a/examples/captions-showcase/src/glyph_field.rs b/examples/captions-showcase/src/glyph_field.rs new file mode 100644 index 00000000..fde5f5b5 --- /dev/null +++ b/examples/captions-showcase/src/glyph_field.rs @@ -0,0 +1,227 @@ +//! A cached distance/normal texture derived from `FFrames`' actual rasterized glyphs. +//! +//! R stores signed distance in pixels as `0.5 + distance / (2 * DISTANCE_RANGE)`; +//! positive distances are inside the glyph. G/B store the unit gradient toward +//! increasing distance as `0.5 + gradient.xy * 0.5`. A is always opaque. Keep the +//! original glyph texture for exact edge coverage: this 8-bit field supplies the +//! wider bevel, rim and material shading, not replacement text antialiasing. + +use fframes::media::ImageData; + +pub const DISTANCE_RANGE: f32 = 32.0; + +/// Build once per cached glyph, retaining its dimensions and padding. +/// +/// The two separable Euclidean distance transforms take O(width * height) time. +/// A transparent virtual border also gives fully filled input a finite interior +/// distance. WASM currently has no native pixel readback and returns `None`. +pub fn from_glyph(image: &ImageData<'_>) -> Option> { + #[cfg(not(target_arch = "wasm32"))] + { + let pixels = fframes::media::image_pixels(&image.image)?; + let rgba = field_pixels( + pixels.width as usize, + pixels.height as usize, + pixels.data.as_ref(), + )?; + Some(fframes::media::rendered_image( + pixels.width, + pixels.height, + rgba, + )) + } + #[cfg(target_arch = "wasm32")] + { + let _ = image; + None + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn field_pixels(width: usize, height: usize, rgba: &[u8]) -> Option> { + let count = width.checked_mul(height)?; + if width == 0 || height == 0 || rgba.len() != count.checked_mul(4)? { + return None; + } + let padded_width = width.checked_add(2)?; + let padded_height = height.checked_add(2)?; + let mut inside = vec![false; padded_width.checked_mul(padded_height)?]; + let mut glyph_top = height; + let mut glyph_bottom = 0; + for y in 0..height { + for x in 0..width { + let covered = rgba[(y * width + x) * 4 + 3] >= 128; + inside[(y + 1) * padded_width + x + 1] = covered; + if covered { + glyph_top = glyph_top.min(y); + glyph_bottom = glyph_bottom.max(y + 1); + } + } + } + let to_inside = distance_squared(&inside, padded_width, padded_height, true); + let to_outside = distance_squared(&inside, padded_width, padded_height, false); + let mut signed = vec![0.0; count]; + for y in 0..height { + for x in 0..width { + let index = y * width + x; + let padded = (y + 1) * padded_width + x + 1; + let alpha = rgba[index * 4 + 3]; + let distance = match alpha { + 0 => 0.5 - to_inside[padded].sqrt(), + 255 => to_outside[padded].sqrt() - 0.5, + // Preserve subpixel location at the coverage edge. Distances away + // from that one-pixel edge use the exact binary EDT above. + _ => f32::from(alpha) / 255.0 - 0.5, + }; + // An empty glyph has no inside seed. Keep its background finite so + // normal differences cannot produce NaN; it still encodes saturated R. + signed[index] = distance.max(-DISTANCE_RANGE * 2.0); + } + } + + // Binary distance-transform sites step between neighboring coverage pixels. + // Their one-pixel differences can therefore make a smooth diagonal bevel + // alternate between two lighting normals. Smooth only the surface used for + // normals, once when this cached texture is prepared. R retains the exact + // signed distances and the original glyph retains its antialiased coverage. + let normal_surface = smooth_normal_surface(&signed, width, height); + // Large hero lettering magnifies even a tiny remaining difference between + // raster sites. Estimate its normal across a wider local neighborhood; use + // actual covered glyph height, so transparent effect padding does not change + // the lighting. This leaves the distance used for contour positions intact. + let normal_radius = glyph_bottom + .saturating_sub(glyph_top) + .div_ceil(64) + .clamp(2, 8); + let mut output = vec![0; count * 4]; + for y in 0..height { + for x in 0..width { + let index = y * width + x; + let left = normal_surface[y * width + x.saturating_sub(normal_radius)]; + let right = normal_surface[y * width + (x + normal_radius).min(width - 1)]; + let up = normal_surface[y.saturating_sub(normal_radius) * width + x]; + let down = normal_surface[(y + normal_radius).min(height - 1) * width + x]; + let dx = right - left; + let dy = down - up; + let length = dx.hypot(dy); + let (nx, ny) = if length > 1e-6 { + (dx / length, dy / length) + } else { + (0.0, 0.0) + }; + let encode = |value: f32| (value.clamp(0.0, 1.0) * 255.0).round() as u8; + output[index * 4] = encode(0.5 + signed[index] / (2.0 * DISTANCE_RANGE)); + output[index * 4 + 1] = encode(0.5 + nx * 0.5); + output[index * 4 + 2] = encode(0.5 + ny * 0.5); + output[index * 4 + 3] = 255; + } + } + Some(output) +} + +/// Separable five-tap binomial blur for the cached lighting surface only. +#[cfg(not(target_arch = "wasm32"))] +fn smooth_normal_surface(signed: &[f32], width: usize, height: usize) -> Vec { + const WEIGHTS: [f32; 5] = [1.0, 4.0, 6.0, 4.0, 1.0]; + let mut horizontal = vec![0.0; signed.len()]; + let mut smoothed = vec![0.0; signed.len()]; + for y in 0..height { + for x in 0..width { + horizontal[y * width + x] = WEIGHTS + .iter() + .enumerate() + .map(|(tap, weight)| { + let sample_x = x.saturating_add(tap).saturating_sub(2).min(width - 1); + signed[y * width + sample_x] * weight + }) + .sum::() + / 16.0; + } + } + for y in 0..height { + for x in 0..width { + smoothed[y * width + x] = WEIGHTS + .iter() + .enumerate() + .map(|(tap, weight)| { + let sample_y = y.saturating_add(tap).saturating_sub(2).min(height - 1); + horizontal[sample_y * width + x] * weight + }) + .sum::() + / 16.0; + } + } + smoothed +} + +#[cfg(not(target_arch = "wasm32"))] +fn distance_squared(mask: &[bool], width: usize, height: usize, seed: bool) -> Vec { + let mut grid: Vec<_> = mask + .iter() + .map(|inside| if *inside == seed { 0.0 } else { f32::INFINITY }) + .collect(); + let longest = width.max(height); + let mut input = vec![0.0; longest]; + let mut output = vec![0.0; longest]; + let mut sites = vec![0; longest]; + let mut breaks = vec![0.0; longest + 1]; + for row in grid.chunks_exact_mut(width) { + edt_line(row, &mut output[..width], &mut sites, &mut breaks); + row.copy_from_slice(&output[..width]); + } + for x in 0..width { + for y in 0..height { + input[y] = grid[y * width + x]; + } + edt_line( + &input[..height], + &mut output[..height], + &mut sites, + &mut breaks, + ); + for y in 0..height { + grid[y * width + x] = output[y]; + } + } + grid +} + +/// Lower envelope of the finite parabolas `(x - site)^2 + input[site]`. +#[cfg(not(target_arch = "wasm32"))] +fn edt_line(input: &[f32], output: &mut [f32], sites: &mut [usize], breaks: &mut [f64]) { + let Some(first) = input.iter().position(|value| value.is_finite()) else { + output.fill(f32::INFINITY); + return; + }; + sites[0] = first; + breaks[0] = f64::NEG_INFINITY; + breaks[1] = f64::INFINITY; + let mut last = 0; + for (q, value) in input.iter().enumerate().skip(first + 1) { + if !value.is_finite() { + continue; + } + let intersection = |site: usize| { + let q = q as f64; + let p = site as f64; + (f64::from(*value) + q * q - f64::from(input[site]) - p * p) / (2.0 * (q - p)) + }; + let mut split = intersection(sites[last]); + while last > 0 && split <= breaks[last] { + last -= 1; + split = intersection(sites[last]); + } + last += 1; + sites[last] = q; + breaks[last] = split; + breaks[last + 1] = f64::INFINITY; + } + let mut segment = 0; + for (x, value) in output.iter_mut().enumerate() { + while segment < last && breaks[segment + 1] < x as f64 { + segment += 1; + } + let delta = x as f64 - sites[segment] as f64; + *value = (delta * delta + f64::from(input[sites[segment]])) as f32; + } +} diff --git a/examples/captions-showcase/src/goo_material.rs b/examples/captions-showcase/src/goo_material.rs new file mode 100644 index 00000000..d6d01a79 --- /dev/null +++ b/examples/captions-showcase/src/goo_material.rs @@ -0,0 +1,211 @@ +//! Typed controls for the shaders.com Goo glyph material. +use fframes::ShaderUniforms; + +use super::LiquidColor; + +/// The upstream seven-charge wet metaball material, bounded to caption geometry. +#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct GooTuning { + pub goo_color: LiquidColor, + pub translucency: f32, + pub absorb: f32, + pub containment: f32, + pub edge_softness: f32, + pub blob_scale: f32, + pub spread: f32, + pub merge: f32, + pub bulge: f32, + pub threshold: f32, + pub light_angle: f32, + pub wetness: f32, + pub fresnel: f32, + pub spec_color: LiquidColor, + pub ambient: f32, + pub speed: f32, + pub wobble: f32, + pub breathe: f32, + pub seed: f32, +} + +impl Default for GooTuning { + fn default() -> Self { + Self { + goo_color: LiquidColor::from_srgb_bytes([55, 201, 90]), + translucency: 0.6, + absorb: 2.0, + containment: 0.9, + edge_softness: 0.06, + blob_scale: 0.3, + spread: 1.0, + merge: 0.6, + bulge: 1.0, + threshold: 0.5, + light_angle: 300.0, + wetness: 2.0, + fresnel: 0.25, + spec_color: LiquidColor::from_srgb_bytes([255, 255, 255]), + ambient: 0.25, + speed: 0.5, + wobble: 0.5, + breathe: 0.3, + seed: 1.0, + } + } +} + +impl GooTuning { + pub fn validate(self) -> Result<(), &'static str> { + for (value, low, high, error) in [ + ( + self.translucency, + 0.0, + 1.0, + "goo.translucency must be finite within 0..=1", + ), + ( + self.absorb, + 0.0, + 6.0, + "goo.absorb must be finite within 0..=6", + ), + ( + self.containment, + 0.0, + 1.0, + "goo.containment must be finite within 0..=1", + ), + ( + self.edge_softness, + 0.0, + 0.3, + "goo.edge_softness must be finite within 0..=0.3", + ), + ( + self.blob_scale, + 0.1, + 1.0, + "goo.blob_scale must be finite within 0.1..=1", + ), + ( + self.spread, + 0.0, + 2.0, + "goo.spread must be finite within 0..=2", + ), + ( + self.merge, + 0.0, + 1.0, + "goo.merge must be finite within 0..=1", + ), + ( + self.bulge, + 0.0, + 1.5, + "goo.bulge must be finite within 0..=1.5", + ), + ( + self.threshold, + 0.2, + 0.9, + "goo.threshold must be finite within 0.2..=0.9", + ), + ( + self.light_angle, + 0.0, + 360.0, + "goo.light_angle must be finite degrees within 0..=360", + ), + ( + self.wetness, + 0.0, + 5.0, + "goo.wetness must be finite within 0..=5", + ), + ( + self.fresnel, + 0.0, + 1.0, + "goo.fresnel must be finite within 0..=1", + ), + ( + self.ambient, + 0.0, + 1.0, + "goo.ambient must be finite within 0..=1", + ), + ( + self.speed, + 0.0, + 2.0, + "goo.speed must be finite within 0..=2", + ), + ( + self.wobble, + 0.0, + 1.0, + "goo.wobble must be finite within 0..=1", + ), + ( + self.breathe, + 0.0, + 1.0, + "goo.breathe must be finite within 0..=1", + ), + ( + self.seed, + 0.0, + 100.0, + "goo.seed must be finite within 0..=100", + ), + ] { + if !value.is_finite() || !(low..=high).contains(&value) { + return Err(error); + } + } + for color in [self.goo_color, self.spec_color] { + if color + .0 + .iter() + .any(|v| !v.is_finite() || !(0.0..=1.0).contains(v)) + { + return Err("goo colors must contain finite linear RGB within 0..=1"); + } + } + Ok(()) + } + + pub fn bind(self, uniforms: ShaderUniforms) -> ShaderUniforms { + uniforms + .float3( + "uGooColor", + self.goo_color.0[0], + self.goo_color.0[1], + self.goo_color.0[2], + ) + .float3( + "uSpecColor", + self.spec_color.0[0], + self.spec_color.0[1], + self.spec_color.0[2], + ) + .float("uTranslucency", self.translucency) + .float("uAbsorb", self.absorb) + .float("uContainment", self.containment) + .float("uEdgeSoftness", self.edge_softness) + .float("uBlobScale", self.blob_scale) + .float("uSpread", self.spread) + .float("uMerge", self.merge) + .float("uBulge", self.bulge) + .float("uThreshold", self.threshold) + .float("uLightAngle", self.light_angle) + .float("uWetness", self.wetness) + .float("uFresnel", self.fresnel) + .float("uAmbient", self.ambient) + .float("uSpeed", self.speed) + .float("uWobble", self.wobble) + .float("uBreathe", self.breathe) + .float("uSeed", self.seed) + } +} diff --git a/examples/captions-showcase/src/irradiance_material.rs b/examples/captions-showcase/src/irradiance_material.rs new file mode 100644 index 00000000..11d1373b --- /dev/null +++ b/examples/captions-showcase/src/irradiance_material.rs @@ -0,0 +1,188 @@ +//! Typed controls for the shaders.com Irradiance glyph adaptation. +use fframes::ShaderUniforms; + +use super::LiquidColor; + +/// One point light, positioned relative to the unpadded word rectangle. +#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize)] +#[serde(deny_unknown_fields)] +pub struct IrradianceLight { + pub position: [f32; 2], + pub color: LiquidColor, + pub intensity: f32, +} + +/// Two-light, stateless adaptation of the upstream edge irradiance material. +/// Reach is in pixels and deliberately stays inside the cached distance band. +#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct IrradianceTuning { + pub lights: [IrradianceLight; 2], + pub light_height: f32, + pub light_range: f32, + pub reach_px: f32, + pub core: f32, + pub wrap: f32, + pub shadows: bool, + pub shadow_softness: f32, + pub body_color: LiquidColor, + pub body_light: f32, + pub bevel_width: f32, + /// Optional deterministic movement of the two lights; zero freezes them. + pub speed: f32, + /// Orbit radius in word-height units; zero keeps the exact authored positions. + pub orbit: f32, +} + +impl Default for IrradianceTuning { + fn default() -> Self { + Self { + lights: [ + IrradianceLight { + position: [0.12, -0.25], + color: LiquidColor::from_srgb_bytes([255, 179, 71]), + intensity: 3.0, + }, + IrradianceLight { + position: [0.9, 1.25], + color: LiquidColor::from_srgb_bytes([122, 92, 255]), + intensity: 2.0, + }, + ], + light_height: 0.5, + light_range: 1.2, + reach_px: 28.0, + core: 1.2, + wrap: 0.1, + shadows: true, + shadow_softness: 0.15, + body_color: LiquidColor::from_srgb_bytes([221, 228, 235]), + body_light: 0.6, + bevel_width: 0.035, + speed: 0.7, + orbit: 0.12, + } + } +} + +impl IrradianceTuning { + pub fn validate(self) -> Result<(), &'static str> { + for (value, low, high, message) in [ + ( + self.light_height, + 0.05, + 3.0, + "irradiance.light_height must be finite within 0.05..=3", + ), + ( + self.light_range, + 0.1, + 4.0, + "irradiance.light_range must be finite within 0.1..=4", + ), + ( + self.reach_px, + 4.0, + 28.0, + "irradiance.reach_px must be finite within 4..=28", + ), + ( + self.core, + 0.0, + 2.0, + "irradiance.core must be finite within 0..=2", + ), + ( + self.wrap, + 0.0, + 1.0, + "irradiance.wrap must be finite within 0..=1", + ), + ( + self.shadow_softness, + 0.0, + 1.0, + "irradiance.shadow_softness must be finite within 0..=1", + ), + ( + self.body_light, + 0.0, + 2.0, + "irradiance.body_light must be finite within 0..=2", + ), + ( + self.bevel_width, + 0.005, + 0.2, + "irradiance.bevel_width must be finite within 0.005..=0.2", + ), + ( + self.speed, + 0.0, + 4.0, + "irradiance.speed must be finite within 0..=4", + ), + ( + self.orbit, + 0.0, + 0.5, + "irradiance.orbit must be finite within 0..=0.5", + ), + ] { + if !value.is_finite() || !(low..=high).contains(&value) { + return Err(message); + } + } + for light in self.lights { + if light + .position + .iter() + .any(|value| !value.is_finite() || !(-2.0..=3.0).contains(value)) + { + return Err("irradiance light positions must be finite within -2..=3"); + } + if !light.intensity.is_finite() || !(0.0..=8.0).contains(&light.intensity) { + return Err("irradiance light intensity must be finite within 0..=8"); + } + } + for color in [self.body_color, self.lights[0].color, self.lights[1].color] { + if color + .0 + .iter() + .any(|value| !value.is_finite() || !(0.0..=1.0).contains(value)) + { + return Err("irradiance colors must be finite linear RGB within 0..=1"); + } + } + Ok(()) + } + + pub fn bind(self, uniforms: ShaderUniforms) -> ShaderUniforms { + let a = self.lights[0]; + let b = self.lights[1]; + uniforms + .float2("uLightPosition0", a.position[0], a.position[1]) + .float3("uLightColor0", a.color.0[0], a.color.0[1], a.color.0[2]) + .float("uLightIntensity0", a.intensity) + .float2("uLightPosition1", b.position[0], b.position[1]) + .float3("uLightColor1", b.color.0[0], b.color.0[1], b.color.0[2]) + .float("uLightIntensity1", b.intensity) + .float("uIrradianceHeight", self.light_height) + .float("uIrradianceRange", self.light_range) + .float("uIrradianceReach", self.reach_px) + .float("uIrradianceCore", self.core) + .float("uIrradianceWrap", self.wrap) + .float("uIrradianceShadows", if self.shadows { 1.0 } else { 0.0 }) + .float("uIrradianceShadowSoftness", self.shadow_softness) + .float3( + "uIrradianceBodyColor", + self.body_color.0[0], + self.body_color.0[1], + self.body_color.0[2], + ) + .float("uIrradianceBodyLight", self.body_light) + .float("uIrradianceBevel", self.bevel_width) + .float("uIrradianceSpeed", self.speed) + .float("uIrradianceOrbit", self.orbit) + } +} diff --git a/examples/captions-showcase/src/main.rs b/examples/captions-showcase/src/main.rs new file mode 100644 index 00000000..d2cf2e33 --- /dev/null +++ b/examples/captions-showcase/src/main.rs @@ -0,0 +1,337 @@ +//! Cinematic kinetic typography. Speech, fonts and word timings use the `FFrames` media loader. +mod annotations; +mod editorial; +mod montage; +mod motion; +mod word_effects; + +use std::{path::PathBuf, process::ExitCode}; + +use fframes::{ + AudioMap, + AudioTimestamp::{Eof, Second}, + AudioTrack, Color, Duration, EncoderOptions, FFramesContext, Frame, MediaDirectory, + MediaProcessingOptions, MediaProvider, RenderOptions, Svgr, Video, + captions::{CaptionError, CaptionTrack}, + cli::{self, clap}, + svgr, +}; +use motion::DirectedCaption; +use word_effects::WordEffects; + +#[derive(Debug, Clone, Copy, clap::ValueEnum)] +enum Style { + Showcase, + #[value(alias = "flame")] + Plasma, + #[value(alias = "space")] + Nebula, + #[value(alias = "sparkles")] + Diffraction, + #[value(alias = "outline")] + Etch, + #[value(alias = "juggle")] + Interference, + #[value(alias = "lightning")] + Discharge, + #[value(alias = "comet")] + Shear, + #[value(alias = "liquid")] + Mercury, + #[value(alias = "confetti")] + Fracture, + #[value(alias = "neon")] + ScanGlass, + #[value(alias = "ink")] + Engraving, + #[value(alias = "shockwave")] + Wavefront, + #[value(alias = "chrome")] + Quartz, + #[value(alias = "frost")] + Crystal, + #[value(alias = "ribbon")] + ThinFilm, + #[value(alias = "gold")] + Horizon, + Astral, + Combustion, + Fluid, + Raster, + Contour, + Prism, + Lattice, + Flux, + Stress, + Lamella, + Photonic, + LiquidMetal, + Irradiance, + Goo, + GlassNebula, +} + +impl Style { + fn effect_override(self) -> Option { + match self { + Self::Showcase => None, + Self::Plasma => Some(0), + Self::Nebula => Some(1), + Self::Diffraction => Some(2), + Self::Etch => Some(3), + Self::Interference => Some(4), + Self::Discharge => Some(5), + Self::Shear => Some(6), + Self::Mercury => Some(7), + Self::Fracture => Some(8), + Self::ScanGlass => Some(9), + Self::Engraving => Some(10), + Self::Wavefront => Some(11), + Self::Quartz => Some(12), + Self::Crystal => Some(13), + Self::ThinFilm => Some(14), + Self::Horizon => Some(15), + Self::Astral => Some(16), + Self::Combustion => Some(17), + Self::Fluid => Some(18), + Self::Raster => Some(19), + Self::Contour => Some(20), + Self::Prism => Some(21), + Self::Lattice => Some(22), + Self::Flux => Some(23), + Self::Stress => Some(24), + Self::Lamella => Some(25), + Self::Photonic => Some(26), + Self::LiquidMetal => Some(27), + Self::Irradiance => Some(28), + Self::Goo => Some(29), + Self::GlassNebula => Some(30), + } + } +} + +#[derive(Debug, clap::Args)] +struct CaptionArgs { + #[arg(long, global = true, default_value = concat!(env!("CARGO_MANIFEST_DIR"), "/media"))] + media_dir: PathBuf, + #[arg(long, global = true, default_value = "moon.json")] + track: String, + #[arg(long, global = true, default_value = "kennedy-moon.wav")] + voice: String, + /// Follow the authored motion score, or use one effect for every word. + #[arg(long, global = true, value_enum, default_value = "showcase")] + style: Style, + /// Optical materials, plasma and precision light shaders; requires --features skia. + #[arg(long, global = true)] + shader: bool, + #[arg(long, global = true)] + silent: bool, + /// Render the typography on a dark background without external footage or audio. + #[arg(long, global = true)] + captions_only: bool, + /// Keep only the original Kennedy recording, without the original synth score. + #[arg(long, global = true)] + voice_only: bool, + /// Parallel Skia CPU render workers. GPU rendering keeps a single context. + #[arg(long, global = true, default_value_t = 3, value_parser = clap::value_parser!(u8).range(1..=8))] + render_workers: u8, +} + +struct CaptionVideo { + captions: Vec, + effects: WordEffects, + style: Style, + audio: String, + silent: bool, + score: bool, + shaders: bool, + captions_only: bool, + seconds: f32, + output: Option, +} + +impl CaptionVideo { + fn new( + args: &CaptionArgs, + seconds: f32, + track: &CaptionTrack, + output: Option, + ) -> Result { + Ok(Self { + captions: track + .cues() + .iter() + .cloned() + .map(DirectedCaption::new) + .collect::, _>>()?, + effects: WordEffects::new(args.shader), + style: args.style, + audio: args.voice.clone(), + silent: args.silent || args.captions_only, + score: !args.voice_only, + shaders: args.shader, + captions_only: args.captions_only, + seconds, + output, + }) + } +} + +impl Video for CaptionVideo { + const FPS: usize = 30; + const WIDTH: usize = 1920; + const HEIGHT: usize = 1080; + const BACKGROUND_COLOR: Color = Color::BLACK; + fn duration(&self) -> Duration<'_> { + if let Some(output) = self.output { + Duration::Frames((output.end_seconds * Self::FPS as f64).ceil() as usize) + } else { + Duration::Seconds(self.seconds) + } + } + fn audio(&self) -> AudioMap<'_> { + if self.silent { + return AudioMap::none(); + } + let mut tracks = vec![ + AudioTrack::new( + &self.audio, + Second(0.0)..self.output.map_or(Eof, |_| Second(self.seconds)), + ) + .highpass(65.0) + .normalize_lufs(-16.0) + .fade_in(0.025) + .fade_out( + self.output + .map_or(0.22, |output| output.voice_fade_seconds as f32), + ) + .voice(), + ]; + if self.score { + tracks.push( + AudioTrack::new("mission-score.wav", Second(0.0)..Second(self.seconds)) + .normalize_lufs(-29.0) + .fade_in(0.1) + .fade_out( + self.output + .map_or(0.7, |output| output.music_fade_seconds as f32), + ), + ); + } + AudioMap::from(tracks) + } + fn render_frame<'a>(&'a self, mut frame: Frame, ctx: &FFramesContext<'a, '_>) -> Svgr<'a> { + let t = frame.global_index as f64 / Self::FPS as f64; + let captions = self + .captions + .iter() + .filter(|cue| cue.active_at(t)) + .map(|cue| cue.render(&mut frame, ctx, &self.effects, self.style.effect_override())) + .collect::>(); + let environment = if self.captions_only { + Svgr::empty() + } else { + montage::render(&frame, ctx, self.shaders) + }; + let fade_seconds = self + .output + .map_or(0.65, |output| output.picture_fade_seconds); + let fade = if fade_seconds > 0.0 { + ((f64::from(self.seconds) - t) / fade_seconds).clamp(0.0, 1.0) + } else { + 1.0 + }; + svgr!( + + + {environment} + {captions} + + ) + } +} + +fn run() -> Result> { + let args = cli::parse::(); + if args.app.shader && !cfg!(feature = "skia") { + return Err("--shader requires --features skia".into()); + } + if args.app.shader && args.renderer == Some(cli::Renderer::Cpu) { + return Err("--shader requires --renderer skia-cpu or gpu".into()); + } + let directory = MediaDirectory::read_folder(&args.app.media_dir)?; + let media = directory.process_media_source_with_options(MediaProcessingOptions { + decode_video_audio: false, + })?; + let track = media + .resolve_subtitles(&args.app.track) + .ok_or("selected subtitles are missing")?; + let track_end = track.cues().iter().map(|cue| cue.end).fold(0.0, f64::max); + let output = editorial::output_policy(track)?; + let legacy_seconds = if args.app.silent || args.app.captions_only { + track_end as f32 + } else { + let audio = media + .resolve_audio(&args.app.voice) + .ok_or("selected audio is missing")?; + (track_end as f32).max(audio.duration_in_seconds()) + }; + let seconds = output.map_or(legacy_seconds, |output| output.end_seconds as f32); + if !args.app.captions_only { + montage::validate(&media, seconds)?; + } + if !args.app.silent + && !args.app.captions_only + && !args.app.voice_only + && media.resolve_audio("mission-score.wav").is_none() + { + return Err( + "mission-score.wav is missing; use --voice-only to render just the speech".into(), + ); + } + let video = CaptionVideo::new(&args.app, seconds, track, output)?; + #[cfg(feature = "skia")] + let pipeline = fframes_skia_renderer::SkiaPipelineConfig { + concurrency_policy: if args.renderer == Some(cli::Renderer::Gpu) { + fframes_skia_renderer::SkiaPipelineConcurrencyPolicy::OnePipeline + } else { + fframes_skia_renderer::SkiaPipelineConcurrencyPolicy::Concurrency(usize::from( + args.app.render_workers, + )) + }, + encoder_threads: 4, + buffer_queue_size: 3, + ..Default::default() + }; + let runner = cli::new( + &video, + RenderOptions { + media: Some(&media), + video_encoder_options: EncoderOptions { + preferred_encoder: Some("libx264"), + codec_params: Some(&[("crf", "18"), ("preset", "medium")]), + color_matrix: fframes::YuvMatrix::Bt709, + ..EncoderOptions::default() + }, + ..Default::default() + }, + ) + .args(args) + .default_output("words-in-orbit.mp4"); + #[cfg(feature = "skia")] + let runner = runner.renderers(fframes_skia_renderer::cli::SkiaRenderers { + default: Some(cli::Renderer::SkiaCpu), + pipeline, + }); + Ok(runner.run()) +} + +fn main() -> ExitCode { + match run() { + Ok(code) => code, + Err(error) => { + eprintln!("captions: {error}"); + ExitCode::FAILURE + } + } +} diff --git a/examples/captions-showcase/src/montage.rs b/examples/captions-showcase/src/montage.rs new file mode 100644 index 00000000..45b796b3 --- /dev/null +++ b/examples/captions-showcase/src/montage.rs @@ -0,0 +1,243 @@ +//! A frame-exact edit. All footage is resolved and decoded by `FFrames` media loading. +use std::sync::LazyLock; + +use fframes::{ + FFramesContext, FFramesSyncedVideoFrame, Frame, MediaProvider, Shader, ShaderUniforms, Svgr, + SyncVideoFrameInput, svgr, +}; + +static FILM: LazyLock = + LazyLock::new(|| Shader::sksl(include_str!("shaders/mission-film.sksl"))); + +/// Timeline and source coordinates use the composition's 30 fps clock, [in, out). +struct Shot { + timeline_in: usize, + timeline_out: usize, + media: &'static str, + source_in: usize, +} + +impl Shot { + const fn new( + timeline_in: usize, + timeline_out: usize, + media: &'static str, + source_in: usize, + ) -> Self { + Self { + timeline_in, + timeline_out, + media, + source_in, + } + } +} + +// Cuts land on spoken ideas. Repeated clips use distinct source intervals. +const SHOTS: [Shot; 16] = [ + Shot::new(0, 38, "apollo-launch.mp4", 80), + Shot::new(38, 52, "apollo-ignition.mp4", 0), + Shot::new(52, 87, "moon-walk.mp4", 0), + Shot::new(87, 139, "shuttle-onboard.mp4", 0), + Shot::new(139, 197, "shuttle-launch.mp4", 0), + Shot::new(197, 224, "rocket-engines.mp4", 0), + Shot::new(224, 260, "artemis-launch.mp4", 0), + Shot::new(260, 333, "earth-orbit.mp4", 0), + Shot::new(333, 375, "moon-ladder.mp4", 0), + Shot::new(375, 425, "apollo-launch.mp4", 39), + Shot::new(425, 472, "artemis-engines.mp4", 0), + Shot::new(472, 531, "artemis-launch.mp4", 39), + Shot::new(531, 593, "cosmos.mp4", 180), + Shot::new(593, 675, "shuttle-launch.mp4", 60), + Shot::new(675, 713, "moon-ladder.mp4", 70), + Shot::new(713, 780, "earth-orbit.mp4", 111), +]; + +/// The lens follows the subject, not the image rectangle. Coordinates are in the +/// prepared source; zoom is relative to a full-frame cover fit. Archival footage +/// remains archival footage: these crops do not manufacture additional detail. +#[derive(Clone, Copy)] +struct Camera { + zoom: [f32; 2], + center: [[f32; 2]; 2], + roll: [f32; 2], + grade: f32, +} + +impl Camera { + const fn new(zoom: [f32; 2], center: [[f32; 2]; 2], roll: [f32; 2], grade: f32) -> Self { + Self { + zoom, + center, + roll, + grade, + } + } +} + +const CAMERAS: [Camera; 16] = [ + Camera::new([1.10, 1.07], [[0.53, 0.50], [0.525, 0.50]], [0.0, 0.0], 0.0), + Camera::new([1.07, 1.12], [[0.50, 0.50], [0.495, 0.51]], [0.0, 0.0], 0.0), + Camera::new([1.07, 1.10], [[0.50, 0.49], [0.505, 0.49]], [0.0, 0.0], 1.0), + Camera::new( + [1.11, 1.15], + [[0.50, 0.50], [0.495, 0.495]], + [0.0, 0.0], + 2.0, + ), + Camera::new([1.39, 1.47], [[0.53, 0.61], [0.535, 0.62]], [0.0, 0.0], 0.0), + Camera::new([1.06, 1.10], [[0.50, 0.51], [0.505, 0.52]], [0.0, 0.0], 0.0), + // The wide Artemis view keeps the entire source mark within the frame. + Camera::new([1.04, 1.06], [[0.50, 0.50], [0.50, 0.50]], [0.0, 0.0], 2.0), + Camera::new([1.08, 1.05], [[0.50, 0.50], [0.50, 0.50]], [0.0, 0.0], 3.0), + Camera::new( + [1.07, 1.11], + [[0.50, 0.50], [0.505, 0.495]], + [0.0, 0.0], + 1.0, + ), + Camera::new([1.10, 1.07], [[0.53, 0.485], [0.52, 0.49]], [0.0, 0.0], 0.0), + // These closer views follow the engine bells and ascending vehicle. Their + // crop bounds keep the corner mark wholly outside, never sliced at an edge. + Camera::new([1.18, 1.25], [[0.50, 0.56], [0.50, 0.57]], [0.0, 0.0], 2.0), + Camera::new([1.32, 1.38], [[0.57, 0.43], [0.57, 0.40]], [0.0, 0.0], 2.0), + Camera::new([1.06, 1.10], [[0.50, 0.50], [0.50, 0.49]], [0.0, 0.0], 3.0), + Camera::new([1.57, 1.65], [[0.54, 0.62], [0.54, 0.615]], [0.0, 0.0], 0.0), + Camera::new([1.14, 1.09], [[0.53, 0.50], [0.52, 0.50]], [0.0, 0.0], 1.0), + Camera::new([1.09, 1.055], [[0.50, 0.50], [0.50, 0.50]], [0.0, 0.0], 3.0), +]; + +/// Check required assets and source ranges before initializing the renderer. +pub fn validate<'a>(media: &'a impl MediaProvider<'a>, seconds: f32) -> Result<(), String> { + if seconds > 26.0 { + return Err( + "this shot score covers 26 seconds; extend montage.rs for a longer track".into(), + ); + } + let mut previous_end = 0; + for shot in &SHOTS { + if shot.timeline_in != previous_end || shot.timeline_out <= shot.timeline_in { + return Err(format!( + "invalid montage interval at frame {}", + shot.timeline_in + )); + } + previous_end = shot.timeline_out; + let source = media + .resolve_video(shot.media) + .ok_or_else(|| format!("montage source is missing: {}", shot.media))?; + let duration = if let Some(metadata) = source.metadata { + metadata.duration + } else { + // Runtime providers defer metadata to their decoder. Probe that same FFrames + // media API here, before allocating a renderer or discovering EOF mid-export. + // SAFETY: the decoder owns the initialized libav contexts and buffers; the + // provider's path lives through this call, and no raw pointers escape it. + let decoder = unsafe { fframes::media::FFmpegDecoder::new(&source.path, 30, 1) } + .map_err(|error| format!("cannot read {}: {error:?}", shot.media))?; + decoder.get_raw_frame().stream_duration_in_seconds() + }; + let source_out = shot.source_in + shot.timeline_out - shot.timeline_in; + if source_out as f64 / 30.0 > f64::from(duration) + 0.001 { + return Err(format!( + "{} ends before required source frame {source_out}", + shot.media + )); + } + } + Ok(()) +} + +pub fn render<'a>(frame: &Frame, ctx: &FFramesContext<'a, '_>, shaders: bool) -> Svgr<'a> { + let Some((shot_index, shot)) = SHOTS.iter().enumerate().find(|(_, shot)| { + shot.timeline_in <= frame.global_index && frame.global_index < shot.timeline_out + }) else { + return missing_frame("outside the montage", frame.global_index); + }; + let age = (frame.global_index - shot.timeline_in) as f32 / 30.0; + let progress = (frame.global_index - shot.timeline_in) as f32 + / (shot.timeline_out - shot.timeline_in) as f32; + // A restrained linear drift keeps the documentary image level and measured. + // The underlying launch or orbital footage supplies the primary movement. + let camera = CAMERAS[shot_index]; + let lerp = |values: [f32; 2]| values[0] + (values[1] - values[0]) * progress; + let zoom = lerp(camera.zoom); + let center = [ + lerp([camera.center[0][0], camera.center[1][0]]), + lerp([camera.center[0][1], camera.center[1][1]]), + ]; + let roll = lerp(camera.roll); + let mut source_frame = frame.clone(); + source_frame.index = shot.source_in + frame.global_index - shot.timeline_in; + // `start_from` is a timeline delay, not source trim. The source index above is explicit. + let Some(source) = source_frame.get_synced_video_frame( + ctx, + shot.media, + &SyncVideoFrameInput { + start_from: 0.0, + looping: false, + editor_fallback_image: None, + speed: 1.0, + }, + ) else { + return missing_frame(shot.media, source_frame.index); + }; + let source_size = [source.width() as f32, source.height() as f32]; + let image = source.into_image(); + let picture = if shaders { + // Three major spoken beats receive a subtle neutral exposure response. + // The picture shader keeps one source sample, including these frames. + let optical = if matches!(shot_index, 2 | 6 | 15) { + (1.0 - age / (4.0 / 30.0)).clamp(0.0, 1.0).powi(2) + } else { + 0.0 + }; + let layer = FILM.draw( + frame, + ShaderUniforms::new() + .image("uSource", &image) + .float2("uSourceSize", source_size[0], source_size[1]) + .float2("uCenter", center[0], center[1]) + .float("uZoom", zoom) + .float("uRoll", roll) + .float("uGrade", camera.grade) + .float("uOptical", optical), + ); + svgr!() + } else { + // Geometry remains the same in the CPU SVG fallback; only the optical + // film treatment needs a shader-capable renderer. + let dx = 960.0 - center[0] * 1920.0 * zoom; + let dy = 540.0 - center[1] * 1080.0 * zoom; + svgr!( + + ) + }; + svgr!( + + + + + + + + + + + + + + {picture} + + + ) +} + +fn missing_frame(media: &str, index: usize) -> Svgr<'static> { + // An unexpected decode failure remains visible instead of silently producing black. + let message = format!("Missing footage: {media}, source frame {index}"); + svgr!( + + {message} + ) +} diff --git a/examples/captions-showcase/src/motion.rs b/examples/captions-showcase/src/motion.rs new file mode 100644 index 00000000..e2031d01 --- /dev/null +++ b/examples/captions-showcase/src/motion.rs @@ -0,0 +1,65 @@ +//! Prepare caption scenes once; frame rendering only reads the prepared components. +use crate::{editorial::EditorialCaption, word_effects::WordEffects}; +use fframes::captions::{CaptionCue, CaptionError, CaptionRect, CaptionTrack, Captions}; +use fframes::{FFramesContext, Frame, Svgr}; + +enum Composition { + Authored(Box), + Plain(Box), +} + +pub struct DirectedCaption { + start: f64, + end: f64, + composition: Composition, +} + +impl DirectedCaption { + pub fn new(cue: CaptionCue) -> Result { + let start = cue.start; + let end = cue.end; + let composition = if cue.metadata.contains_key("editorial") { + Composition::Authored(Box::new(EditorialCaption::new(cue)?)) + } else { + if !cue.metadata.is_empty() || cue.words.iter().any(|word| !word.metadata.is_empty()) { + return Err(CaptionError::invalid( + "metadata", + "authored Moon captions require cue.metadata.editorial and word metadata matching WordStyle", + )); + } + Composition::Plain(Box::new( + Captions::new( + CaptionTrack::new([cue])?, + CaptionRect::new(160.0, 300.0, 1600.0, 600.0), + ) + .clock(fframes::captions::CaptionClock::Global) + .font("Barlow Condensed", 110) + .font_weight(600) + .outline("#080c15", 2.0) + .active_fill("#ffd166"), + )) + }; + Ok(Self { + start, + end, + composition, + }) + } + + pub fn active_at(&self, seconds: f64) -> bool { + self.start <= seconds && seconds < self.end + } + + pub fn render<'a>( + &'a self, + frame: &mut Frame, + ctx: &FFramesContext<'a, '_>, + effects: &'a WordEffects, + effect_override: Option, + ) -> Svgr<'a> { + match &self.composition { + Composition::Authored(caption) => caption.render(frame, ctx, effects, effect_override), + Composition::Plain(captions) => captions.render(frame, ctx), + } + } +} diff --git a/examples/captions-showcase/src/shaders/glass-nebula.sksl b/examples/captions-showcase/src/shaders/glass-nebula.sksl new file mode 100644 index 00000000..4870054c --- /dev/null +++ b/examples/captions-showcase/src/shaders/glass-nebula.sksl @@ -0,0 +1,221 @@ +// Nebula from shaders.com / Shader Effects Inc. (MIT). +// Copyright (c) 2026 Shader Effects Inc. +// Upstream: shader-effects-inc/shaders, commit 9eb037d8db63a0783f67d58ce7291726d698933f. +// Source recipe: packages/core/src/shaders/Nebula/index.ts and std/paint/volume.ts. +// Retained: six-sample front-to-back emission/absorption, domain-warped three-octave +// gas, cavity/ridge/dust terms, two occluded star planes, shell refraction, five-bank +// studio reflection, dual-lobe glint and neutral tone curve. +// Adapters: cached flat-font SDF/coverage instead of a sphere; float-only gradient +// noise and Worley hashes instead of uint MaterialX hashes; linear-sRGB color ramp; +// glyph-height domain and caption-local time. No numerical pixel-equivalence claim. + +// MIT License +// +// Copyright (c) 2026 Shader Effects Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float2 uWordSize; +uniform float uPadding; +uniform float uDistanceRange; +uniform float uLightAge; +uniform float uTail; +uniform float uVariation; +uniform float3 uCoreColor; +uniform float3 uGasColor; +uniform float3 uVeilColor; +uniform float uOpticalDepth; +uniform float uDensity; +uniform float uCavity; +uniform float uDust; +uniform float uGasScale; +uniform float uBillow; +uniform float uGlow; +uniform float uSeed; +uniform float uStars; +uniform float uStarScale; +uniform float uTwinkle; +uniform float uRefraction; +uniform float uEnvironment; +uniform float uHighlight; +uniform float uHighlightSoftness; +uniform float uLightAngle; +uniform float uSpeed; + +float3 hash33(float3 p) { + p=fract(p*float3(.1031,.1030,.0973)); + p+=dot(p,p.yxz+33.33); + return fract((p.xxy+p.yxx)*p.zyx); +} +float hash12(float2 p) {return hash33(float3(p,17.31)).x;} +float fade(float t) {return t*t*t*(t*(t*6.0-15.0)+10.0);} +float grad(float3 cell,float3 delta) { + return dot(hash33(cell)*2.0-1.0,delta); +} +// Quintic gradient-noise interpolation; the seed hash is the ES2 adaptation. +float gasNoise(float3 p) { + float3 cell=floor(p),f=fract(p); + float3 u=float3(fade(f.x),fade(f.y),fade(f.z)); + float a=grad(cell,f),b=grad(cell+float3(1,0,0),f-float3(1,0,0)); + float c=grad(cell+float3(0,1,0),f-float3(0,1,0)); + float d=grad(cell+float3(1,1,0),f-float3(1,1,0)); + float e=grad(cell+float3(0,0,1),f-float3(0,0,1)); + float g=grad(cell+float3(1,0,1),f-float3(1,0,1)); + float h=grad(cell+float3(0,1,1),f-float3(0,1,1)); + float j=grad(cell+float3(1,1,1),f-float3(1,1,1)); + return mix(mix(mix(a,b,u.x),mix(c,d,u.x),u.y), + mix(mix(e,g,u.x),mix(h,j,u.x),u.y),u.z)*.982; +} +float surfaceNoise(float2 p) {return gasNoise(float3(p,.371));} +float3 medium(float3 p,float frequency,float drift,float seed) { + float3 q=float3(p.x*frequency+drift+seed, + p.y*frequency-drift*.7+seed*.53, + p.z*frequency*1.25+drift*1.3+seed*.79); + float warp=gasNoise(q*.5+float3(drift*1.4,0,-drift*.9)); + float3 qw=q+float3(warp,-warp*.73,warp*.41)*(uBillow*.55); + float mid=gasNoise(qw*2.13+float3(5.2,1.3,8.4)); + float fine=gasNoise(qw*4.31+float3(9.1,3.7,1.2)); + float density=(gasNoise(qw)+mid*.5+fine*.27)*.57; + return float3(density,mid,fine); +} +float cellDistance(float2 p) { + float2 cell=floor(p),f=fract(p); + float nearest=10.0; + for(int y=-1;y<=1;++y) { + for(int x=-1;x<=1;++x) { + float2 offset=float2(float(x),float(y)); + float2 feature=hash33(float3(cell+offset,13.7)).xy; + float2 delta=offset+feature-f; + nearest=min(nearest,dot(delta,delta)); + } + } + return nearest; +} +float3 starsAt(float2 p,float time,bool nearPlane) { + float variation=surfaceNoise(p*(nearPlane?.11:.13))*.5+.5; + float radius=(nearPlane?.003:.004)+variation*(nearPlane?.01:.013); + float glint=(1.0-smoothstep(.0005,radius,cellDistance(p))) + *smoothstep(nearPlane?.45:.35,nearPlane?.8:.75,variation); + float shimmer=1.0-uTwinkle*.5+uTwinkle*.5*sin(time*(nearPlane?2.3:1.7)+variation*(nearPlane?34.0:40.0)); + float brightness=(nearPlane?.35:.3)+variation*variation*(nearPlane?1.3:1.2); + float3 cool=nearPlane?float3(.75,.85,1):float3(.72,.82,1); + float3 warm=nearPlane?float3(1,.95,.88):float3(1,.92,.82); + return mix(cool,warm,variation)*glint*brightness*shimmer*uStars*(nearPlane?1.8:1.6); +} +float softbox(float2 p,float2 center,float radius,float gain) { + return (1.0-smoothstep(radius*.2,radius,length(p-center)))*gain*uEnvironment; +} +float studio(float2 reflected,float2 rotation) { + float2 p=float2(reflected.x*rotation.x-reflected.y*rotation.y, + reflected.x*rotation.y+reflected.y*rotation.x); + float sky=smoothstep(-.7,.85,p.y); + return sky*sky*.32 + +softbox(p,float2(0,.55),.2,1.7) + +softbox(p,float2(-.55,.18),.23,1.05) + +softbox(p,float2(.55,.1),.2,1.0) + +softbox(p,float2(.2,-.5),.26,.7) + +softbox(p,float2(-.35,-.25),.22,.85); +} +float3 neutralTone(float3 color) { + float x=min(color.x,min(color.y,color.z)); + float offset=x<.08?x-6.25*x*x:.04; + float3 c=color-offset; + float peak=max(c.x,max(c.y,c.z)); + if(peak<.76) return c; + float newPeak=1.0-(.24*.24)/(peak+.24-.76); + c*=newPeak/peak; + float g=1.0-1.0/(.15*(peak-newPeak)+1.0); + return mix(c,float3(newPeak),g); +} +float srgb(float value) { + value=clamp(value,0.0,1.0); + return value<=.0031308?12.92*value:1.055*pow(value,1.0/2.4)-.055; +} +half4 main(float2 coord) { + float coverage=float(uGlyph.eval(coord).a); + if(coverage<=0.0) return half4(0); + float4 field=float4(uField.eval(coord)); + float distance=max((field.r-.5)*2.0*uDistanceRange,0.0); + float height=max(uWordSize.y,1.0); + float2 centered=(coord-float2(uPadding)-uWordSize*.5)/height; + float2 outward=-(field.gb*2.0-1.0); + float bevelPixels=min(.09*height,uDistanceRange-2.0); + float tilt=min(1.0-clamp(distance/max(bevelPixels,1.0),0.0,1.0),.9995); + float3 normal=float3(outward/max(length(outward),.0001)*tilt,-sqrt(max(1.0-tilt*tilt,0.0))); + float3 view=normalize(float3(centered*.6,1.0)); + float2 interior=centered+normal.xy*(uRefraction*.16); + // A rounded glyph stem replaces the source sphere's optical chord. Distance + // is measured across the same rounded edge used by the shell normal, not + // divided by the height of the whole word: that would flatten a thin letter + // so much that the source's wall fade suppresses the gas volume entirely. + // The half-chord sqrt(1-x*x) closes at the edge and reaches authored optical + // depth at the stem centre. This changes depth, never font coverage/counters. + float stemRadius=max(bevelPixels,1.0); + float radial=1.0-clamp(distance/stemRadius,0.0,1.0); + float pathLen=sqrt(max(1.0-radial*radial,0.0))*uOpticalDepth; + float time=uLightAge*uSpeed,drift=time*.14,seed=uSeed*7.31; + float jitter=hash12(interior*float2(311.7,173.3))*.025; + float macro=surfaceNoise(float2(centered.x*1.3+drift*.6+seed*.37,centered.y*1.3+seed*.61))*.5; + float cavity=mix(-.35,.5,uCavity); + float eGain=.3+pathLen*.9; + float aGain=(.5+pathLen*2.2)*uDensity*.5; + float3 color=float3(0),transmit=float3(1); + for(int i=0;i<6;++i) { + float fraction=(float(i)+.5)/6.0; + float z=pathLen*(fraction+jitter); + float2 pos=interior+view.xy*(.55*z); + float3 gas=medium(float3(pos,z),uGasScale*3.1,drift,seed); + float wall=smoothstep(0.0,.11,pathLen*min(fraction,1.0-fraction)); + float ridge=1.0-clamp(abs(gas.z)*1.6,0.0,1.0); + float density=smoothstep(0.0,.5,gas.x+macro-cavity)*wall*(.65+ridge*.7); + float dust=smoothstep(.45,.75,gas.y)*wall*uDust; + float3 ramp=mix(mix(uVeilColor,uGasColor,smoothstep(.05,.75,density)),uCoreColor,smoothstep(.68,1.0,density)); + float dim=1.0-fraction*.45; + float haze=smoothstep(-.7,.3,gas.x)*.05*wall; + float3 emit=ramp*density*dim+uCoreColor*pow(density,3.0)*uGlow*1.2*dim+uVeilColor*haze*dim; + float3 absorb=float3(density*aGain)+float3(1.5,1.0,.55)*dust*aGain*2.2; + color+=emit*transmit*eGain; + transmit*=exp(-absorb); + } + float starFrequency=uStarScale*34.0; + float2 far=(interior+view.xy*(.55*pathLen*1.1))*starFrequency; + float2 near=(interior+view.xy*(.55*pathLen*.25))*(starFrequency*.56)+float2(7.3,2.9); + color+=mix(uVeilColor,uGasColor,.25)*.06*transmit; + color+=starsAt(far,time,false)*transmit; + color+=starsAt(near,time,true)*mix(transmit,float3(1),.35); + float angle=uLightAngle*.017453292519943295; + float2 lightDirection=float2(cos(angle),sin(angle)); + float3 light=normalize(float3(lightDirection,-.7)); + float3 halfway=normalize(light+float3(0,0,-1)); + float ndh=clamp(dot(normal,halfway),0.0,1.0); + // Source dualLobeGlint: softness widens both lobes while conserving its recipe. + float widen=exp2(mix(2.0,-2.0,uHighlightSoftness)); + float tight=140.0*widen; + float broad=22.0*widen; + float spec=(pow(ndh,tight)*1.3+pow(ndh,broad)*.14)*uHighlight; + color+=float3(.85,.92,1.08)*studio(reflect(view,normal).xy,lightDirection)*.38; + color+=float3(1,.98,.95)*spec; + color=neutralTone(color); + color=float3(srgb(color.r),srgb(color.g),srgb(color.b)); + float alpha=coverage*(1.0-clamp(uTail,0.0,1.0)); + return half4(color*alpha,alpha); +} diff --git a/examples/captions-showcase/src/shaders/goo.sksl b/examples/captions-showcase/src/shaders/goo.sksl new file mode 100644 index 00000000..81fec9af --- /dev/null +++ b/examples/captions-showcase/src/shaders/goo.sksl @@ -0,0 +1,152 @@ +// Goo from shaders.com / Shader Effects Inc. (MIT). +// Copyright (c) 2026 Shader Effects Inc. +// Source: github.com/shader-effects-inc/shaders, commit 9eb037d8db63a0783f67d58ce7291726d698933f +// Adapted from shaders/Goo/index.ts and its +// emitted WGSL: the seven Gaussian charges, occupancy blend, wet surface +// surface, Beer-Lambert subsurface, wet highlight and clearcoat equations. +// Font adaptation: the source square maps to the word box, while shape distance +// uses glyph height. Analytic charge gradients and the cached smooth glyph normal +// replace forward differences of an 8-bit distance texture; the x derivative is +// scaled by height/width so the surface normal uses isotropic physical units. +// The flat glyph has no 3D front-depth channel. High containment +// resolves to exact font coverage; lower containment can bulge within a bounded +// 28px SDF halo. Color is linear sRGB then display-encoded and premultiplied. + +// MIT License +// +// Copyright (c) 2026 Shader Effects Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float2 uWordSize; +uniform float uPadding; +uniform float uDistanceRange; +uniform float uLightAge; +uniform float uTail; +uniform float uVariation; +uniform float3 uGooColor; +uniform float3 uSpecColor; +uniform float uTranslucency; +uniform float uAbsorb; +uniform float uContainment; +uniform float uEdgeSoftness; +uniform float uBlobScale; +uniform float uSpread; +uniform float uMerge; +uniform float uBulge; +uniform float uThreshold; +uniform float uLightAngle; +uniform float uWetness; +uniform float uFresnel; +uniform float uAmbient; +uniform float uSpeed; +uniform float uWobble; +uniform float uBreathe; +uniform float uSeed; + +float distanceAt(float2 p) { + if (p.x < 0.0 || p.y < 0.0 || p.x >= iResolution.x || p.y >= iResolution.y) + return -uDistanceRange; + return (float(uField.eval(p).r) - .5) * 2.0 * uDistanceRange; +} + +// Return potential and its analytic derivatives in the source square's x/y. +// These are the upstream Gaussian lobes; constants below are baked by its JS hash. +float3 charge(float2 p, float time, float4 pf, float4 ph) { + float spread = uSpread * .45; + float driftAmp = uWobble * .12; + float baseRadius = uBlobScale * .38 * (.65 + uMerge * .7); + float2 centre = pf.xy * spread + driftAmp * float2( + sin(time * pf.z + ph.y), cos(time * pf.w + ph.z)); + float radius = baseRadius * (1.0 + uBreathe * sin(time * ph.x + ph.w)); + float invRadius2 = 1.0 / max(radius * radius, .0001); + float2 d = p - centre; + float value = exp(-dot(d, d) * invRadius2); + return float3(value, -2.0 * d * value * invRadius2); +} + +float3 potentials(float2 p, float time) { + float3 potential = float3(0); + potential += charge(p, time, float4(0.2672612419, 0.0000000000, 1.4216903898, 0.6655801301), float4(1.4951718029, 0.3595123606, 2.8492958301, 3.3905610437)); + potential += charge(p, time, float4(-0.3413353923, 0.3126913242, 0.5572181693, 0.8291109666), float4(1.3160251931, 3.5074159627, 3.9338192086, 6.1041921356)); + potential += charge(p, time, float4(0.0522465902, -0.5953260879, 1.0582225886, 1.0396245506), float4(0.9418164056, 2.3482381801, 1.0403706397, 5.1596862320)); + potential += charge(p, time, float4(0.4302316313, 0.5611601763, 0.8737337143, 1.4715123531), float4(1.4268788171, 2.8492958301, 2.0678651871, 1.1249650119)); + potential += charge(p, time, float4(-0.7895273754, -0.1396555276, 0.9534795162, 1.3211895696), float4(0.9341956140, 3.9338192086, 3.3905610437, 6.2528488430)); + potential += charge(p, time, float4(0.7479085854, -0.4757594283, 1.1260867730, 0.6790437426), float4(1.1441862511, 1.0403706397, 6.1041921356, 5.1272374979)); + potential += charge(p, time, float4(-0.2501596850, 0.9305866755, 0.6655801301, 1.4951718029), float4(0.8226436057, 2.0678651871, 5.1596862320, 2.7760143450)); + return potential; +} + +float srgb(float value) { + value = clamp(value, 0.0, 1.0); + return value <= .0031308 ? value * 12.92 : 1.055 * pow(value, 1.0 / 2.4) - .055; +} + +half4 main(float2 coord) { + float d0 = distanceAt(coord); + // Preserve the finite glyph texture contract rather than cutting free blobs + // at a rectangular image boundary. At full containment this band is invisible. + float halo = 1.0 - smoothstep(20.0, 28.0, -d0); + if (halo <= 0.0) return half4(0); + float2 size = max(uWordSize, float2(1)); + float2 p = (coord - float2(uPadding)) / size - .5; + float time = uLightAge * uSpeed + uSeed; + float3 potential = potentials(p, time); + float height = size.y; + float distance = d0 / height; + // The source SDF is negative inside; our cached field is positive inside. + float inside = smoothstep(-.06, .06, distance); + float occupation = mix(1.0, inside, uContainment); + float H = potential.x * occupation + inside * uContainment; + float softness = max(uEdgeSoftness, .001); + float coverage = smoothstep(uThreshold - softness, uThreshold + softness, H); + // A caption's full-containment endpoint must reproduce the actual font mask. + coverage = mix(coverage, float(uGlyph.eval(coord).a), smoothstep(.94, 1.0, uContainment)); + float2 inward = float2(uField.eval(coord).gb) * 2.0 - 1.0; + float ramp = clamp((distance + .06) / .12, 0.0, 1.0); + float2 insideGradient = inward * (6.0 * ramp * (1.0 - ramp) / .12); + float2 potentialGradient = potential.yz * float2(height / size.x, 1.0); + float2 gradient = potentialGradient * occupation + + insideGradient * uContainment * (potential.x + 1.0); + float slope = uBulge * .12 + .03; + float3 normal = normalize(float3(-gradient * slope, -1.0)); + float dome = sqrt(max(H - uThreshold, 0.0)) * uBulge; + float thickness = clamp(dome + max(distance, 0.0) * uContainment * 1.2, 0.0, 1.0); + + float angle = uLightAngle * .017453292519943295; + float3 light = normalize(float3(cos(angle), sin(angle), -.8)); + float3 halfway = normalize(light + float3(0, 0, -1)); + float ndl = clamp(dot(normal, light), 0.0, 1.0); + float ndh = clamp(dot(normal, halfway), 0.0, 1.0); + float specCore = pow(ndh, 232.0) * 1.7; + float specHalo = pow(ndh, 32.0) * .25; + float glint = smoothstep(.992, .999, ndh) * 1.9; + float fresnel = pow(1.0 - clamp(-normal.z, 0.0, 1.0), 5.0) * uFresnel; + float3 specular = uSpecColor * (specCore + specHalo + glint + fresnel) * uWetness; + float transmit = exp(-uAbsorb * (1.0 - thickness)); + float3 subsurface = uGooColor * (uTranslucency * transmit) * (ndl * .5 + .5); + float3 body = uGooColor * (uAmbient + ndl * .7); + float3 linear = body + subsurface + specular; + float3 color = float3(srgb(linear.r), srgb(linear.g), srgb(linear.b)); + float alpha = coverage * halo * (1.0 - clamp(uTail, 0.0, 1.0)); + return half4(color * alpha, alpha); +} diff --git a/examples/captions-showcase/src/shaders/irradiance.sksl b/examples/captions-showcase/src/shaders/irradiance.sksl new file mode 100644 index 00000000..1c991f38 --- /dev/null +++ b/examples/captions-showcase/src/shaders/irradiance.sksl @@ -0,0 +1,190 @@ +// Irradiance from shaders.com / Shader Effects Inc. (MIT). +// Copyright (c) 2026 Shader Effects Inc. +// Source: github.com/shader-effects-inc/shaders at +// 9eb037d8db63a0783f67d58ce7291726d698933f, Irradiance/index.ts, +// std/paint/radiance.ts (gatherIrradiance + shadowVisibility) and materials.ts. +// Adaptation: deterministic sixteen-cone fragment gather, not the upstream temporal +// compute/denoise pass. Twelve march/shadow steps, two lights, a 28-pixel spill +// radius inside FFrames' cached 32-pixel field. Shape alpha/normals come from the +// actual loaded font. Optional light orbit is a caption-time addition. + +// MIT License +// +// Copyright (c) 2026 Shader Effects Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float2 uWordSize; +uniform float uPadding; +uniform float uDistanceRange; +uniform float uLightAge; +uniform float uTail; +uniform float uVariation; +uniform float2 uLightPosition0; +uniform float2 uLightPosition1; +uniform float3 uLightColor0; +uniform float3 uLightColor1; +uniform float uLightIntensity0; +uniform float uLightIntensity1; +uniform float uIrradianceHeight; +uniform float uIrradianceRange; +uniform float uIrradianceReach; +uniform float uIrradianceCore; +uniform float uIrradianceWrap; +uniform float uIrradianceShadows; +uniform float uIrradianceShadowSoftness; +uniform float3 uIrradianceBodyColor; +uniform float uIrradianceBodyLight; +uniform float uIrradianceBevel; +uniform float uIrradianceSpeed; +uniform float uIrradianceOrbit; + +// Match upstream negative-inside convention. The cache saturates at32px; +// outside its rectangle there is no shape, so continue conservatively outward. +float distanceAt(float2 p) { + float2 outside = max(max(-p, p-iResolution.xy), float2(0)); + if (outside.x>0.0 || outside.y>0.0) return uDistanceRange+length(outside); + return (.5-float(uField.eval(p).r))*2.0*uDistanceRange; +} +float2 outwardAt(float2 p) { + float2 n=-(float2(uField.eval(p).gb)*2.0-1.0); + return n/max(length(n),.0001); +} +float2 lightPoint(float2 position, float phase) { + float t=uLightAge*uIrradianceSpeed+uVariation*6.2831853+phase; + float2 drift=float2(cos(t),sin(t*.83))*uIrradianceOrbit*uWordSize.y; + return float2(uPadding)+position*uWordSize+drift; +} +float falloff(float distance) { + float ratio=distance/max(uIrradianceRange*uWordSize.y,.01); + return 1.0/(1.0+ratio*ratio); +} + +// Source shadowVisibility: distance-field penumbra, with an explicit finite +// ray budget. A near concavity or intervening glyph blocks its light. +float visibility(float2 from, float2 toward) { + if (uIrradianceShadows<.5) return 1.0; + float2 segment=toward-from; + float total=length(segment); + float2 direction=segment/max(total,.0001); + float travel=1.2, visible=1.0; + for(int step=0;step<12;++step) { + if(travel>=total) break; + float d=distanceAt(from+direction*travel); + visible=min(visible,clamp(d/max(travel*uIrradianceShadowSoftness,.6),0.0,1.0)); + if(visible<=0.0) break; + travel+=max(d,.6); + } + return visible; +} +float3 edgeLight(float2 point, float2 normal, float2 light, float3 color, float intensity) { + float2 delta=light-point; + float distance=length(delta); + float struck=clamp((dot(normal,delta/max(distance,.0001))+uIrradianceWrap)/(1.0+uIrradianceWrap),0.0,1.0); + struck*=falloff(distance)*visibility(point+normal*2.0,light); + return color*(intensity*struck); +} +float3 emission(float2 point,float2 normal,float2 key,float2 fill) { + return edgeLight(point,normal,key,uLightColor0,uLightIntensity0) + + edgeLight(point,normal,fill,uLightColor1,uLightIntensity1); +} + +// Adapted directly from gatherIrradiance's cone coverage integral. It samples +// emitting boundaries in several directions rather than painting one nearest- +// edge glow (which would partition letters and miss occlusion in their counters). +float3 gather(float2 origin,float start,float2 key,float2 fill) { + float3 light=float3(0); + float reach=uIrradianceReach; + // Source-style fan rotation dissolves directional quadrature boundaries into + // fine stationary sampling grain instead of visible triangular light fans. + float jitter=fract(52.9829189*fract(dot(floor(origin),float2(.06711056,.00583715)))); + for(int cone=0;cone<16;++cone) { + float angle=(float(cone)+jitter)*.3926990817; + float2 direction=float2(cos(angle),sin(angle)); + float travel=max(start,.6),best=1.0,bestT=travel,bestD=0.0; + for(int step=0;step<12;++step) { + float2 point=origin+direction*travel; + float d=distanceAt(point); + float clear=d/max(travel*.19891236738,.01); + if(clearreach) break; + } + float coverage=1.0-clamp(best,0.0,1.0); + if(coverage>.002) { + float2 at=origin+direction*bestT; + float2 normal=outwardAt(at); + float2 boundary=at-normal*max(bestD,0.0); + light+=emission(boundary,normal,key,fill)*coverage; + } + } + return light*.0625; +} +float3 bodyLight(float2 point,float3 normal,float2 light,float3 color,float intensity) { + float2 delta=light-point; + float3 direction=normalize(float3(delta,-uIrradianceHeight*uWordSize.y)); + float response=falloff(length(delta))*uIrradianceBodyLight; + float3 halfway=normalize(direction+float3(0,0,-1)); + float glint=pow(max(dot(normal,halfway),0.0),60.0)*.6; + return uIrradianceBodyColor*(max(dot(normal,direction),0.0)*response) + + color*(intensity*glint*response); +} +float3 exposed(float3 energy) {return 1.0-exp(-max(energy,float3(0)));} +float srgb(float value) { + value=clamp(value,0.0,1.0); + return value<=.0031308 ? value*12.92 : 1.055*pow(value,1.0/2.4)-.055; +} + +half4 main(float2 coord) { + float d=distanceAt(coord); + if(d>=uIrradianceReach) return half4(0); + float coverage=float(uGlyph.eval(coord).a); + float2 key=lightPoint(uLightPosition0,0.0),fill=lightPoint(uLightPosition1,3.14159265); + float2 outward=outwardAt(coord); + float3 body=float3(0),spill=float3(0); + if(coverage>0.0) { + float bevel=min(max(uIrradianceBevel*uWordSize.y,1.0),uDistanceRange-2.0); + float tilt=min(1.0-clamp(-d/bevel,0.0,1.0),.9995); + float3 normal=float3(outward*tilt,-sqrt(max(1.0-tilt*tilt,0.0))); + body=exposed(uIrradianceBodyColor*.3 + +bodyLight(coord,normal,key,uLightColor0,uLightIntensity0) + +bodyLight(coord,normal,fill,uLightColor1,uLightIntensity1)); + } + if(coverage<1.0) { + float3 arriving=gather(coord,max(d,.6),key,fill); + float rim=1.0-smoothstep(0.0,2.5,max(d,0.0)); + if(rim>0.0) { + float2 boundary=coord-outward*max(d,0.0); + arriving+=emission(boundary,outward,key,fill)*(rim*uIrradianceCore); + } + float fade=1.0-smoothstep(uIrradianceReach*.68,uIrradianceReach,max(d,0.0)); + spill=exposed(arriving)*fade; + } + float glowAlpha=max(max(spill.r,spill.g),spill.b); + float alpha=coverage+(1.0-coverage)*glowAlpha; + if(alpha<=.00001) return half4(0); + float3 straight=(body*coverage+spill*(1.0-coverage))/alpha; + float3 color=float3(srgb(straight.r),srgb(straight.g),srgb(straight.b)); + alpha*=1.0-clamp(uTail,0.0,1.0); + return half4(color*alpha,alpha); +} diff --git a/examples/captions-showcase/src/shaders/liquid-metal.sksl b/examples/captions-showcase/src/shaders/liquid-metal.sksl new file mode 100644 index 00000000..95c10c43 --- /dev/null +++ b/examples/captions-showcase/src/shaders/liquid-metal.sksl @@ -0,0 +1,159 @@ +// LiquidMetal from shaders.com / Shader Effects Inc. (MIT). +// Copyright (c) 2026 Shader Effects Inc. +// Source: github.com/shader-effects-inc/shaders, commit 9eb037d8db63a0783f67d58ce7291726d698933f +// SkSL port of the upstream flat-shape material: analytic Perlin slope, flow warp, +// bevel normal, three-bank studio, sharp glint and two-tap chromatic dispersion. +// Adaptations: FFrames' cached positive-inside glyph SDF/normal and exact glyph +// coverage replace upstream shape routing; glyph-height coordinates replace its +// square shape viewport; the caption clock controls flow and release. Color uses +// linear sRGB (rather than the upstream default P3) before display encoding. + +// MIT License +// +// Copyright (c) 2026 Shader Effects Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float2 uWordSize; +uniform float uPadding; +uniform float uDistanceRange; +uniform float uLightAge; +uniform float uTail; +uniform float uVariation; +uniform float uTurbulence; +uniform float uRipple; +uniform float uWarp; +uniform float uSpeed; +uniform float uSharpness; +uniform float uEnvironment; +uniform float uEnvRotation; +uniform float uDispersion; +uniform float uLightAngle; +uniform float uBevelWidth; +uniform float uBevelShape; +uniform float3 uLightColor; +uniform float3 uDarkColor; + +// The upstream hash uses uint bitcasts unavailable in portable SkSL runtime effects. +// Use the example's deterministic float-only hash family here. This changes the +// noise realization, while retaining the analytic Perlin derivative and material +// equations below; this adaptation does not claim pixel identity to WGSL. +float2 hash22(float2 p) { + float3 q=fract(float3(p.xyx)*float3(.1031,.1030,.0973)); + q+=dot(q,q.yzx+33.33); + return fract((q.xx+q.yz)*q.zy); +} + +float3 perlin12d(float2 p) { + float2 i = floor(p), f = fract(p); + float2 u = f*f*f*(f*(f*6.0-15.0)+10.0); + float2 du = f*f*(f*(f-2.0)+1.0)*30.0; + float2 ga = hash22(i)*2.0-1.0; + float2 gb = hash22(i+float2(1,0))*2.0-1.0; + float2 gc = hash22(i+float2(0,1))*2.0-1.0; + float2 gd = hash22(i+float2(1,1))*2.0-1.0; + float va = dot(ga,f), vb = dot(gb,f-float2(1,0)); + float vc = dot(gc,f-float2(0,1)), vd = dot(gd,f-float2(1,1)); + float k = va-vb-vc+vd; + float value = va+u.x*(vb-va)+u.y*(vc-va)+u.x*u.y*k; + float2 deriv = ga+(gb-ga)*u.x+(gc-ga)*u.y+(ga-gb-gc+gd)*(u.x*u.y) + +du*(float2(u.y,u.x)*k+float2(vb,vc)-va); + return float3(value,deriv); +} + +float2 flowWarp(float2 p, float time) { + return float2( + sin(p.x*1.7+time*.7+1.7)*cos(p.y*1.3+time*.5+2.3), + cos(p.x*1.4+time*.9+8.7)*sin(p.y*1.6+time*.6+5.1)); +} + +float bevelSin(float t) { + float roundProfile = 1.0-t; + float k1 = smoothstep(0.0,.14,t), k2 = smoothstep(.68,.92,t); + float machined = mix(1.0,.72*(1.0-k2),k1); + return mix(roundProfile,machined,uBevelShape); +} + +float studio(float2 reflection, float2 env, float keyRadius, float drift) { + float2 p = float2(reflection.x*env.x-reflection.y*env.y, + reflection.x*env.y+reflection.y*env.x); + float sky = smoothstep(-.7,.85,p.y); + float lum = sky*sky*.4; + float r0 = keyRadius*1.05, r1 = keyRadius*1.25, r2 = keyRadius*1.15; + // Invert an increasing smoothstep to preserve the original reversed-edge + // softbox formula without relying on undefined GLSL smoothstep edge order. + lum += (1.0-smoothstep(r0*.2,r0,length(p-float2(drift,.55))))*1.75*uEnvironment; + lum += (1.0-smoothstep(r1*.2,r1,length(p-float2(-.45+drift,-.4))))*1.15*uEnvironment; + lum += (1.0-smoothstep(r2*.2,r2,length(p-float2(.55+drift,.1))))*1.1*uEnvironment; + return lum; +} + +float srgb(float value) { + value = clamp(value,0.0,1.0); + return value <= .0031308 ? value*12.92 : 1.055*pow(value,1.0/2.4)-.055; +} + +half4 main(float2 coord) { + float coverage = float(uGlyph.eval(coord).a); + if (coverage <= 0.0) return half4(0); + float4 field = float4(uField.eval(coord)); + float distance = (field.r-.5)*2.0*uDistanceRange; + float height = max(uWordSize.y,1.0); + // Negate the stored inward gradient to match upstream's outward normal. + float2 outward = -(field.gb*2.0-1.0); + float bevelPixels = min(max(uBevelWidth*height,1.0),uDistanceRange-2.0); + float rim = clamp(distance/bevelPixels,0.0,1.0); + float tilt = min(bevelSin(rim),.9995); + float2 edge = outward/max(length(outward),.0001)*tilt; + float3 geometric = float3(edge,-sqrt(max(1.0-tilt*tilt,0.0))); + + float2 pat = (coord-float2(uPadding)-uWordSize*.5)/height; + float time = uLightAge*uSpeed; + float2 q = pat*(uRipple*2.6)+float2(uVariation*2.1,uVariation*.7); + float2 warped = q+flowWarp(q*.6,time*.12)*(uWarp*.9); + float2 slope = clamp(perlin12d(warped).yz*.62,-4.0,4.0); + float relief = uTurbulence*.5; + float3 normal = normalize(float3(geometric.xy-slope*relief,geometric.z)); + float3 view = normalize(float3(pat*.6,1.0)); + float3 reflected = reflect(view,normal); + float sharp = clamp(uSharpness,0.0,1.0); + float keyRadius = mix(.5,.18,sharp); + float drift = sin(time*.07)*.05; + float envAngle = uEnvRotation*.017453292519943295; + float2 env = float2(cos(envAngle),sin(envAngle)); + float lightAngle = uLightAngle*.017453292519943295; + float3 light = normalize(float3(cos(lightAngle),sin(lightAngle),-.7)); + float3 halfway = normalize(light+float3(0,0,-1)); + float ndh = clamp(dot(normal,halfway),0.0,1.0); + float spec = pow(ndh,mix(60.0,900.0,sharp))*mix(.4,1.4,sharp); + float lumG = studio(reflected.xy,env,keyRadius,drift)+spec; + float grazing = 1.0-clamp(-normal.z,0.0,1.0); + float dispersion = uDispersion*.09*(.4+grazing*1.3); + float2 dispersed = float2(reflected.x+reflected.y*dispersion, + reflected.y-reflected.x*dispersion); + float lumB = studio(dispersed,env,keyRadius,drift)+spec; + float lumR = 2.0*lumG-lumB; + float3 linear = mix(uDarkColor,uLightColor,clamp(float3(lumR,lumG,lumB),0.0,1.8)); + float3 color = float3(srgb(linear.r),srgb(linear.g),srgb(linear.b)); + float alpha = coverage*(1.0-clamp(uTail,0.0,1.0)); + return half4(color*alpha,alpha); +} diff --git a/examples/captions-showcase/src/shaders/mission-film.sksl b/examples/captions-showcase/src/shaders/mission-film.sksl new file mode 100644 index 00000000..e3d3b398 --- /dev/null +++ b/examples/captions-showcase/src/shaders/mission-film.sksl @@ -0,0 +1,71 @@ +// Original picture treatment for the spaceflight edit. The source is an actual +// decoded FFrames image, not a shader marker or a generated environment. +// Inspired by the source-coordinate passes in fframes-intro. The sober mission +// grade keeps neutral highlights and source detail, with restrained chroma and +// a cool separation in the midtones. This is a creative SDR grade, not an HDR +// conversion or an attempt to recover detail from the archival recordings. +uniform shader uSource; +uniform float2 uSourceSize; +uniform float2 uCenter; +uniform float uZoom; +uniform float uRoll; +uniform float uGrade; +uniform float uOptical; +uniform float3 iResolution; +uniform int iFrame; + +float grainHash(float2 p) { + p=fract(p*float2(443.897,441.423)); + p+=dot(p,p.yx+19.19); + return fract((p.x+p.y)*p.x); +} + +float2 sourcePoint(float2 screen) { + float2 q=screen/iResolution.xy-.5; + float cs=cos(uRoll), sn=sin(uRoll); + q=float2(cs*q.x+sn*q.y,-sn*q.x+cs*q.y); + float sourceAspect=uSourceSize.x/uSourceSize.y; + float frameAspect=iResolution.x/iResolution.y; + float2 cover=sourceAspect>frameAspect ? float2(frameAspect/sourceAspect,1.) + : float2(1.,sourceAspect/frameAspect); + float2 uv=uCenter+q*cover/uZoom; + return clamp(uv*uSourceSize,float2(.5),uSourceSize-.5); +} + +float3 samplePicture(float2 p) { return float3(uSource.eval(sourcePoint(p)).rgb); } + +half4 main(float2 p) { + float2 uv=p/iResolution.xy; + float3 c=samplePicture(p); + float luma=dot(c,float3(.2126,.7152,.0722)); + // Shape luminance rather than clipping channels with a contrast offset. + // The toe keeps the archival launch structure readable; the shoulder + // preserves luminous exhaust and does not claim to recover clipped detail. + float shaped=luma+.16*(luma-.43)*luma*(1.-luma); + c*=shaped/max(luma,.0001); + if(uGrade>.5 && uGrade<1.5) { + // Restored lunar television stays neutral monochrome. + c=float3(dot(c,float3(.2126,.7152,.0722))); + } else { + // Cool the amber midtone cast, not the source's actual white-hot plume. + // Neutral objects remain nearly neutral; this is not a teal/orange LUT. + float mid=smoothstep(.035,.22,luma)*(1.-smoothstep(.48,.86,luma)); + float warmCast=max(c.r-c.b,0.0)*mid; + c+=warmCast*float3(-.16,-.025,.13); + float saturation=uGrade>2.5 ? .90 : (uGrade>1.5 ? .82 : .78); + saturation=mix(saturation,.96,smoothstep(.58,.92,luma)); + float gray=dot(c,float3(.2126,.7152,.0722)); + c=mix(float3(gray),c,saturation); + // A small luminance-scaled steel bounce avoids tinting true black. + float shadow=(1.-smoothstep(.10,.48,luma))*luma; + c+=shadow*float3(-.022,.003,.035); + } + // Three major cuts receive a very small neutral exposure response. No + // radial zoom, colored streak or extra source fetch competes with the type. + c+=float3(.025)*smoothstep(.58,.98,luma)*uOptical; + float2 vignette=(uv-.5)*float2(1.,.82); + c*=1.-smoothstep(.28,.70,length(vignette))*.16; + float grain=(grainHash(floor(p*.86)+float(iFrame)*float2(17.13,3.71))-.5); + c+=grain*.008*(.3+.7*sqrt(clamp(luma,0.,1.))); + return half4(half3(clamp(c,0.,1.)),1.); +} diff --git a/examples/captions-showcase/src/shaders/relief-effects.sksl b/examples/captions-showcase/src/shaders/relief-effects.sksl new file mode 100644 index 00000000..52cd5d56 --- /dev/null +++ b/examples/captions-showcase/src/shaders/relief-effects.sksl @@ -0,0 +1,199 @@ +// Original glyph-volume studies for the caption showcase. Both effects raycast +// a shallow extrusion of the cached font SDF: ribs and channels are geometry, +// with a shared light direction, surface normals, cavity occlusion and side faces. +// Inspiration: Universal Everything / Walking City, GMUNK / BOX, Jan Tomas / +// Ignite, and the real ribbon geometry in this repository's shader-mode example. +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float iTime; +uniform float uEffect; +uniform float uProgress; +uniform float uAge; +uniform float uLightAge; +uniform float uTail; +uniform float uSeed; +uniform float uDirected; +uniform float uEntrance; +uniform float uHero; +uniform float uWarm; +uniform float uVariation; +uniform float uDistanceRange; +uniform float2 uWordSize; +uniform float uPadding; +uniform float uDepth; +uniform float uDetailScale; +uniform float uSpeed; +uniform float uLight; + +float glyph(float2 p) { + if(p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return 0.0; + return float(uGlyph.eval(p).a); +} +float distanceAt(float2 p) { + if(p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return -uDistanceRange; + return (float(uField.eval(p).r)-.5)*2.0*uDistanceRange; +} +float2 gradientAt(float2 p) { + float2 g=float2(uField.eval(p).gb)*2.0-1.0; + return g/max(length(g),.001); +} +float reliefScale() {return clamp(uWordSize.y/210.0,.42,1.7);} +float phase() {return uVariation*6.283185;} +float artTime() {return uLightAge*uSpeed;} + +float lamellaWave(float2 p) { + float s=max(uWordSize.y/210.0,.42); + float2 uv=(p-iResolution.xy*.5)/uWordSize.y; + // One slow curvature field bends all ribs together. Broad, rounded bands + // preserve even the thinnest serif; no horizontal strip is removed. + float curved=p.y+sin(uv.x*3.5+artTime()*.55+phase())*8.0*s; + return cos(curved/(42.0*s*uDetailScale)*6.283185); +} +float channelPosition(float2 p) { + float s=max(uWordSize.y/210.0,.42); + float2 uv=(p-iResolution.xy*.5)/uWordSize.y; + // Fixed engraved paths. Only the energy packets move through this geometry. + return p.y*.82+p.x*.43+sin(uv.x*2.7+uv.y*1.4+phase())*20.0*s; +} +float channelDistance(float2 p) { + float s=max(uWordSize.y/210.0,.42),pitch=43.0*s*uDetailScale; + return abs(mod(channelPosition(p)+pitch*.5,pitch)-pitch*.5); +} +float heightAt(float2 p) { + float s=reliefScale(); + if(uEffect<.5) { + float develop=smoothstep(.02,.38,artTime()); + float wave=lamellaWave(p); + float rib=.5+.5*wave; + return (24.0*s+(rib-.5)*24.0*s*develop)*uDepth; + } + float distance=channelDistance(p); + float groove=exp(-pow(distance/(2.3*s+.5),2.0)); + return (17.0*s-groove*6.0*s)*uDepth; +} +float smoothMaximum(float a,float b,float width) { + float h=clamp(.5+.5*(a-b)/width,0.0,1.0); + return mix(b,a,h)+width*h*(1.0-h); +} +float volume(float3 p) { + float side=-distanceAt(p.xy); + float front=-heightAt(p.xy)-p.z; + float back=p.z-12.0*reliefScale()*uDepth; + return max(smoothMaximum(side,front,1.8),back)*.34; +} +float3 viewDirection() { + return normalize(uEffect<.5 ? float3(.62,.22,1.0) : float3(.42,.19,1.0)); +} +float4 raycast(float2 coord) { + float3 direction=viewDirection(); + float extent=86.0*max(uDepth,1.0); + float3 origin=float3(coord-direction.xy/direction.z*extent,-extent); + float travel=0.0; + for(int i=0;i<80;++i) { + float3 at=origin+direction*travel; + float d=volume(at); + if(d<.16) return float4(at,1.0); + travel+=max(d,.12); + if(travel>150.0*max(uDepth,1.0)) break; + } + return float4(0); +} +float3 normalAt(float3 p) { + // The cached font gradient is filtered, avoiding pixel teeth at the bevel. + // Height derivatives describe real front-face relief, not painted lighting. + float e=.8; + float dx=(heightAt(p.xy+float2(e,0))-heightAt(p.xy-float2(e,0)))/(2.0*e); + float dy=(heightAt(p.xy+float2(0,e))-heightAt(p.xy-float2(0,e)))/(2.0*e); + float3 front=normalize(float3(-dx,-dy,-1.0)); + float2 g=gradientAt(p.xy); + float3 side=float3(-g,0.0); + float d=distanceAt(p.xy); + float face=smoothstep(.0,2.8,d); + return normalize(mix(side,front,face)); +} +float occlusion(float3 p,float3 n) { + // Two probes are enough for these shallow grooves. Cavity darkness follows + // actual geometry and remains stable when the highlights move. + float a=max(2.5-volume(p+n*2.5)/.34,0.0); + float b=max(6.0-volume(p+n*6.0)/.34,0.0); + return clamp(1.0-a*.10-b*.035,.48,1.0); +} +float3 studio(float3 n,float3 view) { + float3 light=normalize(float3(-.55,-.72,-1.0)); + float diffuse=max(dot(n,light),0.0); + float3 halfVector=normalize(light+view); + float broad=pow(max(dot(n,halfVector),0.0),16.0); + float thin=pow(max(dot(n,normalize(float3(.55,-.25,-1.0))),0.0),35.0); + // One broad softbox and a small cool edge light; never a full-word flash. + return float3(.22+.63*diffuse)+broad*float3(.27,.27,.24)+thin*float3(.10,.14,.19); +} +float4 over(float4 back,float a,float3 color) { + a=clamp(a,0.0,1.0); + return float4(clamp(color,0.0,1.0)*a,a)+back*(1.0-a); +} + +half4 main(float2 coord) { + // The distance texture saturates at32px; testing only coord would cut off + // the new deeper sides. Sample a capsule covering the projected volume. + float envelope=max(distanceAt(coord+float2(8,3)*uDepth),distanceAt(coord)); + envelope=max(envelope,distanceAt(coord-float2(22,8)*uDepth)); + envelope=max(envelope,distanceAt(coord-float2(44,16)*uDepth)); + if(envelope < -18.0) return half4(0); + float4 hit=raycast(coord); + if(hit.w<.5) return half4(0); + float3 p=hit.xyz,n=normalAt(p); + float3 view=-viewDirection(); + float cavity=occlusion(p,n); + float3 lighting=studio(n,view); + float3 color; + if(uEffect<.5) { + // 25 LAMELLA. A softly reflective ivory body, with darker real side faces + // and broad curved ribbons cut into its continuous connected surface. + float3 ivory=mix(float3(.91,.93,.92),float3(.98,.86,.71),uWarm*.32); + float rib=lamellaWave(p.xy); + float face=smoothstep(.08,.72,-n.z); + float3 body=mix(float3(.023,.047,.068),ivory,face); + color=body*lighting*cavity; + float fresnel=pow(1.0-max(dot(n,view),0.0),3.0); + color+=float3(.08,.13,.16)*fresnel; + color+=float3(.04,.035,.025)*smoothstep(.2,.9,rib); + // Fine parallel machining marks are subservient to the large relief. + float detail=sin(p.x*.19+p.y*.07)*.012; + color+=detail*max(-n.z,0.0); + } else { + // 26 PHOTONIC. Energy charges machined recesses in dark titanium. The + // bright physical bevel preserves the source glyph over archival film. + float s=reliefScale(),d=channelDistance(p.xy); + float channel=exp(-pow(d/(2.3*s+.5),2.0)); + float core=exp(-pow(d/(.75*s+.25),2.0)); + float2 uv=(p.xy-iResolution.xy*.5)/uWordSize.y; + float row=floor(channelPosition(p.xy)/(43.0*max(uWordSize.y/210.0,.42)*uDetailScale)+.5); + float path=(uv.x*.82-uv.y*.24)+row*.12-artTime()*.62+phase()*.06; + float packet=exp(-pow((fract(path)-.5)/.10,2.0)); + float leading=exp(-pow((fract(path+.07)-.5)/.028,2.0)); + float x=(p.x-uPadding)/max(uWordSize.x,1.0); + float charged=artTime()/.68-x+row*.018; + float chargeHead=exp(-pow(charged/.08,2.0)); + float chargeTail=smoothstep(-.015,.04,charged)*(1.0-smoothstep(.04,.24,charged)); + float3 titanium=mix(float3(.045,.058,.073),float3(.012,.022,.028),channel); + color=titanium*lighting*cavity; + float bevel=1.0-smoothstep(1.1*s,3.3*s,distanceAt(p.xy)); + color=mix(color,float3(.56,.69,.74)*(.65+lighting*.30),bevel*.93); + float3 cold=float3(.32,.73,.83),warm=float3(1.0,.68,.29); + float3 energy=mix(cold,warm,uWarm*.75); + float transport=chargeHead*.95+chargeTail*.32+packet*.08; + color+=channel*(.008+transport)*energy; + color+=core*(.008+transport*1.2+leading*.045)*float3(.80,.96,1.0); + // A contained bounce on the adjacent wall reinforces depth of emission. + color+=exp(-d/(6.0*s))*transport*energy*.22; + } + // Geometry stays opaque. A narrow AA band at the exact font boundary avoids + // crisp square pixels without blurring the relief or closing glyph counters. + // Lighting is evaluated in linear radiance; display encoding retains a pale + // readable face while preserving the darker side faces and recessed floors. + color=pow(clamp(color*uLight,0.0,1.0),float3(.454545)); + float alpha=clamp(distanceAt(p.xy)+.8,.0,1.0); + float4 result=over(float4(0),alpha,color); + return half4(result*(1.0-clamp(uTail,0.0,1.0))); +} diff --git a/examples/captions-showcase/src/shaders/semantic-effects.sksl b/examples/captions-showcase/src/shaders/semantic-effects.sksl new file mode 100644 index 00000000..92f424ef --- /dev/null +++ b/examples/captions-showcase/src/shaders/semantic-effects.sksl @@ -0,0 +1,199 @@ +// Semantic caption transformations. Original shader implementations inspired by +// the structural reveal of Wide Open, coherent transport in Max Cooper's Waves / +// onformative's IBM FLUX, and photoelastic visualizations of loaded structures. +// These are authored analytic fields, not a physical simulation or copied film. +// Every silhouette, counter and boundary comes from FFrames' actual glyph raster. +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float iTime; +uniform float uEffect; +uniform float uProgress; +uniform float uAge; +uniform float uLightAge; +uniform float uTail; +uniform float uSeed; +uniform float uDirected; +uniform float uEntrance; +uniform float uHero; +uniform float uWarm; +uniform float uVariation; +uniform float uDistanceRange; +uniform float2 uWordSize; +uniform float uPadding; + +float glyph(float2 p) { + if(p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return 0.0; + return float(uGlyph.eval(p).a); +} +float distanceAt(float2 p) { + if(p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return -uDistanceRange; + return (float(uField.eval(p).r)-.5)*2.0*uDistanceRange; +} +float2 gradientAt(float2 p) { + float2 g=float2(uField.eval(p).gb)*2.0-1.0; + return g/max(length(g),.001); +} +float hash(float2 p) { + float3 q=fract(float3(p.xyx)*.1031); + q+=dot(q,q.yzx+33.33+uSeed*.001); + return fract((q.x+q.y)*q.z); +} +float4 over(float4 back,float a,float3 color) { + a=clamp(a,0.0,1.0); + return float4(clamp(color,0.0,1.0)*a,a)+back*(1.0-a); +} +float band(float d,float center,float width) { + float support=smoothstep(-uDistanceRange+.5,-uDistanceRange+2.5,d); + return exp(-abs(d-center)/max(width,.1))*support; +} + +// Triangulated structural cells. Three rod directions and small, lit nodes make +// an actual open structure rather than a grid painted on a solid letter. +float trussDistance(float2 p,float cell) { + float2 q=mod(p+cell*.5,cell)-cell*.5; + float vertical=abs(q.x); + float horizontal=abs(q.y); + float diagonal=abs(q.x-q.y)*.70710678; + return min(min(vertical,horizontal),diagonal); +} +float truss(float2 p,float cell,float radius) { + return 1.0-smoothstep(radius-.55,radius+.55,trussDistance(p,cell)); +} +float3 rodLight(float2 p,float cell,float radius,float depth,float phase) { + float d=trussDistance(p,cell); + float roundness=sqrt(max(0.0,1.0-pow(clamp(d/max(radius,.1),0.0,1.0),2.0))); + float along=.80+.20*sin((p.x+p.y)/cell*.55+phase); + float light=(.34+.66*roundness)*along; + return mix(float3(.30,.39,.43),float3(.91,.91,.84),light)*depth; +} + +half4 main(float2 coord) { + float h=uWordSize.y,s=max(h/210.0,.42),t=uLightAge; + float phase=uVariation*6.283185; + float2 uv=(coord-iResolution.xy*.5)/h; + float d=distanceAt(coord),a=glyph(coord),tail=clamp(uTail,0.0,1.0); + float4 result=float4(0); + + // 22 LATTICE — a solid ivory object opens into an engineered cellular body. + // Front, middle and rear trusses have distinct projected depth and occlude + // one another. Its true glyph boundary stays continuous through the reveal. + if(uEffect<.5) { + if(d < -uDistanceRange+1.0) return half4(0); + // Readable primary bays, about40px at260px type. The foreground rods + // carry the silhouette; finer structure is subordinate to this scaffold. + float cell=32.0*s; + float radius=1.45*s+.45; + float rearCell=cell*1.90,middleCell=cell*1.50; + float2 p=coord-uPadding; + // Perspective shifts the rear structure smoothly by a few pixels; the + // front frame stays fixed, so the open holes expose changing parallax. + float2 depth=float2(7.5+1.8*sin(t*.72+phase),5.2+.8*cos(t*.65+phase))*s; + depth=min(depth,float2(18.0,14.0)); + float2 front=p; + float2 middle=p-depth*.5; + float2 rear=p-depth; + float3 solid=float3(.91,.90,.83); + float reveal=smoothstep(.08,.70,t); + float x=(coord.x-uPadding)/max(uWordSize.x,1.0); + float opening=smoothstep(x-.16,x+.16,reveal*1.35-.12); + // Keep solid for the first spoken frames; opening then travels through + // the letters as one causal operation, not unrelated random erasures. + float face=a*(1.0-opening); + // Keep depth inside the front silhouette so the outer frame never turns + // into three competing outlines when the film is viewed at a small size. + float backMask=glyph(coord-depth)*a; + float middleMask=glyph(coord-depth*.5)*a; + float rearRod=truss(rear+cell*.21,rearCell,radius*.65)*backMask; + float middleRod=truss(middle+float2(cell*.43,0),middleCell,radius*.70)*middleMask; + float frontRod=truss(front,cell,radius)*a; + float frameWidth=clamp(2.5*s,2.2,4.0); + float boundary=a*(1.0-smoothstep(frameWidth-.65,frameWidth+.65,max(d,0.0))); + // First expose the main load-bearing frame. Secondary inner braces + // resolve afterwards at reduced opacity, rather than appearing as hatch. + float secondary=smoothstep(.22,.64,t)*smoothstep(.40,1.0,opening); + // Dark rear cavities make the depth readable over bright launch footage; + // most cell centers are genuinely open to the underlying image. + result=over(result,a*opening*.10,float3(.015,.025,.032)); + result=over(result,rearRod*secondary*.28,rodLight(rear+cell*.21,rearCell,radius,.48,phase)); + result=over(result,middleRod*secondary*.36,rodLight(middle+float2(cell*.43,0),middleCell,radius,.69,phase)); + // Small connectors between projected layers make each triangle a volume. + float connector=truss(p-depth*.25+cell*.10,cell*2.0,radius*.65)*a*.12; + result=over(result,connector*secondary,float3(.60,.72,.77)); + result=over(result,frontRod*opening,rodLight(front,cell,radius,1.0,phase)); + result=over(result,face,solid); + result=over(result,boundary,solid); + // A measured blue inspection light crosses the exposed structure once. + float scan=exp(-pow((x-fract(t*.24+phase*.03))/.045,2.0)); + result=over(result,(frontRod+boundary)*scan*opening*.34,float3(.56,.83,.93)); + result=over(result,band(d,-.8*s,.65*s)*.64,float3(.09,.14,.18)); + } + // 23 FLUX — propagating packets carry energy across a stationary field. + // Flow follows a broad traveling wave and bends around the actual glyph + // boundary. Bright crests and short particle trains share that same phase. + else if(uEffect<1.5) { + if(d < -uDistanceRange+1.0) return half4(0); + float2 grad=gradientAt(coord),tangent=float2(-grad.y,grad.x); + float boundaryWeight=1.0-smoothstep(2.0*s,17.0*s,max(d,0.0)); + float arc=uv.x+.22*sin(uv.y*3.6+phase)+boundaryWeight*.045*dot(tangent,float2(1,.7)); + float wave=sin(arc*10.0-t*4.8+phase); + float packet=pow(.5+.5*wave,5.0); + float carrier=uv.y*37.0+sin(uv.x*4.0-t*.72+phase)*2.2; + carrier+=boundaryWeight*dot(grad,float2(1.4,-.9)); + float filament=exp(-abs(sin(carrier))*10.0); + float standing=pow(.5+.5*sin(uv.y*12.0+phase)*cos(uv.x*7.0),4.0); + float3 face=mix(float3(.13,.28,.34),float3(.34,.54,.57),standing); + face+=packet*float3(.15,.25,.25); + face+=filament*(.32+.68*packet)*float3(.30,.57,.55); + // Points remain spatially organized; only their carrier phase advances. + float pulse=pow(.5+.5*sin(arc*68.0-t*13.0+phase),16.0)*filament; + face+=pulse*packet*float3(.65,.61,.43); + float exterior=band(d,-3.5*s,1.1*s)*packet; + result=over(result,exterior*.75,float3(.48,.85,.81)); + result=over(result,band(d,-8.0*s,3.0*s)*packet*.18,float3(.17,.46,.52)); + result=over(result,smoothstep(-1.8*s,-.1*s,d),float3(.014,.031,.037)); + // A neutral face at onset becomes the field; never flash an empty word. + float developed=smoothstep(.0,.24,t); + result=over(result,a,mix(float3(.83,.91,.88),face,developed)); + result=over(result,a*band(d,.75*s,.70*s)*.88,float3(.83,.94,.90)); + result=over(result,a*filament*pulse*packet*.44,float3(1.0,.87,.57)); + } + // 24 STRESS — a loaded plate flexes coherently while analytic strain bands + // expose internal structure. The dark blue / amber photoelastic response is + // shared across the word, not independent noisy textures on each letter. + else { + float x=(coord.x-uPadding)/max(uWordSize.x,1.0)*2.0-1.0; + float load=smoothstep(0.0,.26,t)*(.82+.18*sin(t*1.2+phase)); + float beam=max(0.0,1.0-x*x); + float travel=sin(x*3.5-t*2.5+phase)*.30; + float deflection=min(10.0*s,24.0)*beam*load; + float shear=min(3.0*s,8.0)*load*sin(uv.y*4.0+x*2.5); + float2 p=coord+float2(shear,-deflection*(.78+travel)); + float actual=glyph(p),dist=distanceAt(p); + if(dist < -uDistanceRange+1.0) return half4(0); + float2 grad=gradientAt(p); + float boundary=1.0-smoothstep(0.0,11.0*s,max(dist,0.0)); + // Bending and local boundary direction form one smooth strain field. + float strain=beam*(.70+.30*cos(uv.y*3.0))+uv.y*.30; + strain+=sin(x*4.0+uv.y*2.4-t*.45+phase)*.16; + strain+=boundary*(grad.x*.13-grad.y*.09); + float iso=sin(strain*25.0-load*4.0); + float darkBand=smoothstep(-.75,-.28,iso); + float3 face=mix(float3(.19,.29,.40),float3(.63,.75,.80),darkBand); + float hot=exp(-abs(iso-.20)*20.0); + face=mix(face,float3(.98,.66,.34),hot*.78); + float highlight=pow(.5+.5*cos(strain*13.0+phase),8.0); + face+=highlight*float3(.10,.13,.14); + // Thin fracture-like interfaces are tied to the strain gradient; all + // remain in the body. No random shards or confetti are emitted. + float facets=abs(fract(strain*8.0+uv.x*.9)-.5); + face+=exp(-facets*100.0)*float3(.12,.17,.20)*load; + float onset=smoothstep(0.0,.22,t); + face=mix(float3(.90,.90,.84),face,onset); + result=over(result,band(dist,-3.0*s,.7*s)*.5,float3(.53,.72,.83)); + result=over(result,smoothstep(-1.5*s,-.1*s,dist),float3(.018,.027,.035)); + result=over(result,actual,face); + result=over(result,actual*band(dist,.8*s,.8*s)*.86,float3(.88,.94,.94)); + } + return half4(result*(1.0-tail)); +} diff --git a/examples/captions-showcase/src/shaders/showcase-effects.sksl b/examples/captions-showcase/src/shaders/showcase-effects.sksl new file mode 100644 index 00000000..28c56789 --- /dev/null +++ b/examples/captions-showcase/src/shaders/showcase-effects.sksl @@ -0,0 +1,230 @@ +// Six separate visual techniques, rather than recolorings of a shared metal. +// Authored for the caption showcase. Geometry is always the cached real glyph. +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float iTime; +uniform float uEffect; +uniform float uProgress; +uniform float uAge; +uniform float uLightAge; +uniform float uTail; +uniform float uSeed; +uniform float uDirected; +uniform float uEntrance; +uniform float uHero; +uniform float uWarm; +uniform float uVariation; +uniform float uDistanceRange; +uniform float2 uWordSize; +uniform float uPadding; + +// Hash without trigonometry keeps spatial choices stable across CPU/GPU backends. +float hash(float2 p) { + float3 q=fract(float3(p.xyx)*.1031); + q+=dot(q,q.yzx+33.33+uSeed*.001); + return fract((q.x+q.y)*q.z); +} +float noise(float2 p) { + float2 i=floor(p),f=fract(p); + f=f*f*(3.0-2.0*f); + return mix(mix(hash(i),hash(i+float2(1,0)),f.x), + mix(hash(i+float2(0,1)),hash(i+float2(1,1)),f.x),f.y); +} +float fbm(float2 p) { + return noise(p)*.57+noise(p*2.03+17.1)*.28+noise(p*4.09-7.3)*.15; +} +float glyph(float2 p) { + if(p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return 0.0; + return float(uGlyph.eval(p).a); +} +float distanceAt(float2 p) { + if(p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return -uDistanceRange; + return (float(uField.eval(p).r)-.5)*2.0*uDistanceRange; +} +float band(float d,float center,float width) { + // A saturated SDF is an absence of geometry, not another contour. + float support=smoothstep(-uDistanceRange+.5,-uDistanceRange+2.5,d); + return exp(-abs(d-center)/max(width,.1))*support; +} +float4 over(float4 back,float a,float3 color) { + a=clamp(a,0.0,1.0); + return float4(clamp(color,0.0,1.0)*a,a)+back*(1.0-a); +} +float star(float2 p,float r) { + p/=r; + float rays=exp(-abs(p.x)*2.0-abs(p.y)*24.0)+exp(-abs(p.y)*3.0-abs(p.x)*24.0); + return clamp(exp(-dot(p,p)*12.0)+rays*.5,0.0,1.0); +} + +half4 main(float2 coord) { + float h=uWordSize.y,s=max(h/210.0,.42),t=uLightAge; + float2 uv=(coord-iResolution.xy*.5)/h; + float phase=uVariation*6.283185+uSeed*.019; + float d=distanceAt(coord),a=glyph(coord),tail=clamp(uTail,0.0,1.0); + float4 result=float4(0); + + // 16 ASTRAL: moving deep-blue clouds, bright nebular filaments and distinct + // star points inside the face. A few close stars extend into the near field. + // This deliberately has no metal bevel, studio reflection or common rim. + if(uEffect<.5) { + if(d < -uDistanceRange+1.0) return half4(0); + float2 flow=float2(fbm(uv*2.7+float2(t*.08,phase)),fbm(uv*2.7+8.0-t*.05)); + float n=fbm(uv*4.2+flow*2.9+float2(t*.10,-t*.06)); + float filament=pow(clamp(1.0-abs(n-.53)*5.0,0.0,1.0),5.0); + float3 color=mix(float3(.19,.25,.51),float3(.40,.73,.90),smoothstep(.20,.76,n)); + color=mix(color,float3(.80,.60,.86),smoothstep(.45,.80,flow.y)*.5); + color+=filament*float3(.33,.36,.37); + float2 grid=coord/(31.0*s),cell=floor(grid),q=fract(grid)-.5; + float seed=hash(cell+phase); + float spark=star(q*31.0*s,(2.7+seed*3.5)*s)*step(.84,seed); + float twinkle=.58+.42*sin(t*.85+seed*23.0)*sin(t*.85+seed*23.0); + color+=spark*twinkle*float3(.62,.68,.70); + float halo=band(d,-4.0*s,6.0*s)*(.20+.16*filament); + result=over(result,halo,float3(.23,.43,.70)); + result=over(result,spark*twinkle*band(d,-8.0*s,9.0*s)*.8,float3(.80,.89,1.0)); + result=over(result,smoothstep(-1.8*s,-.3*s,d),float3(.025,.035,.075)); + result=over(result,a,color); + result=over(result,band(d,.7*s,.75*s)*a*.84,float3(.87,.93,1.0)); + } + // 17 COMBUSTION: ivory letters stay still while real glyph-shaped plumes + // rise 20–60px above them. Copper folds and sparse embers make a readable + // incandescent object; the fire is allowed to escape the letter silhouette. + else if(uEffect<1.5) { + float2 q=uv*float2(8.0,5.0)+float2(phase,-t*1.5); + float curl=fbm(q+float2(noise(q*.7+t*.2),noise(q*.8+6.0))*2.0); + float2 drift=float2(sin(uv.y*7.0+curl*4.0+t)*min(7.0*s,16.0),0); + float plume=0.0; + plume=max(plume,glyph(coord+drift+float2(0,min(12.0*s,22.0)))*.74); + plume=max(plume,glyph(coord+drift*1.5+float2(0,min(27.0*s,44.0)))*.48); + plume=max(plume,glyph(coord+drift*2.0+float2(0,min(46.0*s,64.0)))*.22); + float channels=smoothstep(.27,.77,curl); + plume*=channels*(1.0-a)*(.60+.40*noise(q*2.4)); + float3 heat=mix(float3(.65,.16,.055),float3(1.0,.65,.24),channels); + result=over(result,plume*.85,heat); + float near=band(d,-4.0*s,4.8*s); + result=over(result,near*(.18+.28*channels),float3(.88,.32,.09)); + float2 grid=coord/(23.0*s),cell=floor(grid),part=fract(grid+float2(t*.12,t*.65))-.5; + float ember=exp(-dot(part,part)*190.0)*step(.94,hash(cell)); + result=over(result,ember*band(d,-16.0*s,12.0*s)*.8,float3(1.0,.68,.29)); + result=over(result,smoothstep(-2.0*s,-.3*s,d),float3(.09,.035,.02)); + float edge=1.0-smoothstep(0.0,6.0*s,max(d,0.0)); + float3 face=mix(float3(.95,.90,.77),float3(.88,.47,.20),edge*(.40+.42*channels)); + // Thin hot currents run through the warm solid face, without eroding it. + float current=exp(-abs(curl-.51)*75.0); + face+=current*float3(.05,.08,.12); + result=over(result,a,face); + } + // 18 FLUID: a bounded physical displacement changes the silhouette, while + // broad silver/black reflected ribbons flow through the same deformed face. + // Amplitude is small enough to preserve every counter and consonant. + else if(uEffect<2.5) { + float wave=sin(uv.y*8.2+sin(uv.x*3.7+t*.9)*1.8-t*1.3+phase); + float cross=sin(uv.x*7.5+uv.y*2.0+t*.8+phase); + float amplitude=(2.0+1.5*(1.0-uEntrance))*s; + float2 warp=float2(wave,cross*.60)*amplitude; + float2 p=coord+warp; + float actual=glyph(p),dist=distanceAt(p); + if(dist < -uDistanceRange+1.0) return half4(0); + float ribbon=sin(uv.y*5.5+wave*.58+uv.x*.55+t*.35); + float plate=smoothstep(-.38,.42,ribbon); + float3 face=mix(float3(.24,.29,.33),float3(.91,.95,.96),plate); + float shine=exp(-pow((ribbon-.13)/.12,2.0)); + face+=shine*float3(.30,.32,.34); + float edge=band(dist,.85*s,1.0*s); + result=over(result,band(dist,-2.2*s,1.4*s)*.55,float3(.26,.44,.52)); + result=over(result,smoothstep(-1.3*s,-.1*s,dist),float3(.015,.022,.025)); + result=over(result,actual,face); + result=over(result,edge*actual*.74,float3(.93,.97,1.0)); + } + // 19 RASTER: horizontal scan reconstruction, discrete pixel cells, a quiet + // telemetry scan during the hold, and spatially released fragments on exit. + // The short entrance keeps a faint complete glyph underneath the assembly. + else if(uEffect<3.5) { + float enter=1.0-smoothstep(0.0,.26,t); + float block=(1.0+floor(enter*7.0+tail*5.0))*s; + float2 tile=floor(coord/(9.0*s)); + float seed=hash(tile+phase); + float2 drift=float2((seed-.35)*36.0,-12.0-hash(tile+4.0)*28.0)*s*tail; + float2 p=coord-drift; + p=(floor(p/block)+.5)*block; + float actual=glyph(p); + float assembly=smoothstep(0.0,.26,t); + float gate=step(seed,.45+.55*assembly)*(1.0-smoothstep(seed-.09,seed+.09,tail)); + float scanY=mod(t*53.0*s+phase*15.0,h+30.0*s)+uPadding; + float scan=exp(-abs(coord.y-scanY)/(3.0*s)); + float line=.87+.13*step(.7,mod(coord.y,3.0*s)/s); + float3 face=mix(float3(.57,.86,.88),float3(.92,.98,.94),assembly)*line; + face+=scan*float3(.18,.17,.10); + // Thin warm registration bands are an accent, not continuous RGB damage. + float register=step(.94,hash(float2(floor(coord.y/(8.0*s)),3.0)))*enter; + result=over(result,glyph(coord+float2(4.5*s,0))*register*.35,float3(.94,.44,.23)); + result=over(result,a*.27*enter*(1.0-tail),float3(.58,.78,.78)); + result=over(result,actual*gate*(1.0-tail*.45),face); + } + // 20 CONTOUR: a moving drafting light writes two clean real-glyph outlines. + // Ceramic fill follows behind the light; the contrast between wire and solid + // is intentional and confined to this cue, rather than shared by all effects. + else if(uEffect<4.5) { + if(d < -uDistanceRange+1.0) return half4(0); + float progress=smoothstep(0.0,.65,t)*1.15; + float write=(coord.x-uPadding)/max(uWordSize.x,1.0); + float reveal=1.0-smoothstep(progress-.025,progress+.025,write); + float light=exp(-pow((write-progress)/.022,2.0)); + float inner=band(d,1.0*s,.8*s)*a; + float outer=band(d,-4.5*s,.6*s); + // A quiet complete face remains readable even when a short spoken word + // ends before the drafting light reaches its final letter. + result=over(result,a*.64,float3(.78,.84,.87)); + result=over(result,inner*.92,float3(.91,.96,.97)); + result=over(result,outer*(.25+.65*reveal),float3(.37,.70,.79)); + float filled=smoothstep(.02,.14,progress-write); + result=over(result,a*filled,float3(.92,.94,.90)); + result=over(result,(inner+outer)*light,float3(1.0,.73,.40)); + result=over(result,a*light*.25,float3(1.0,.88,.65)); + } + // 21 PRISM: a light slit refracts through a translucent icy face, producing + // offset spectral edge images. Broad matte fields keep this unlike chrome. + else { + if(d < -uDistanceRange+1.0) return half4(0); + float diagonal=uv.x*.45+uv.y*.85; + float sweep=.65*sin(t*.63+phase*.24); + float slit=exp(-pow((diagonal-sweep)/.065,2.0)); + float wide=exp(-pow((diagonal-sweep)/.23,2.0)); + float spectrum=.5+.5*sin(diagonal*7.0-t*.7+phase); + float3 face=mix(float3(.44,.60,.76),float3(.81,.77,.88),spectrum); + face=mix(face,float3(.88,.96,.96),wide*.50); + face+=slit*float3(.20,.22,.24); + float split=(2.0+wide*3.0)*s; + float red=glyph(coord+float2(split,-.6*s)); + float blue=glyph(coord-float2(split,-.6*s)); + result=over(result,red*.42,float3(.88,.55,.40)); + result=over(result,blue*.48,float3(.40,.70,.96)); + result=over(result,band(d,-5.0*s,.8*s)*(.24+.46*wide),float3(.64,.82,.95)); + result=over(result,smoothstep(-1.0*s,-.1*s,d),float3(.04,.07,.11)); + result=over(result,a,face); + result=over(result,band(d,.7*s,.6*s)*a*.80,float3(.94,.96,1.0)); + // Sparse four-point specular glints belong to the glyph's actual edge. + // Project a few stable cell centers onto its distance field; their slow + // envelopes never blink or shower unrelated stars across the picture. + float2 cellSize=float2(max(uWordSize.x/7.0,80.0*s),80.0*s); + float2 cell=floor(coord/cellSize),site=(cell+.5)*cellSize; + half4 field=uField.eval(site); + float siteDistance=(float(field.r)-.5)*2.0*uDistanceRange; + float2 gradient=float2(field.gb)*2.0-1.0; + float gradientLength=length(gradient); + float seed=hash(cell+phase); + if(seed>.69 && siteDistance>-uDistanceRange+2.0 && gradientLength>.05) { + float2 anchor=site-gradient/gradientLength*siteDistance; + float2 anchorUv=(anchor-iResolution.xy*.5)/h; + float alignment=exp(-pow((anchorUv.x*.45+anchorUv.y*.85-sweep)/.40,2.0)); + float breath=.42+.58*pow(.5+.5*sin(t*2.2+seed*9.0),2.0); + float spark=star(coord-anchor,14.0*s)*breath*(.60+.40*alignment); + result=over(result,spark*.94,float3(1.0,.96,.86)); + } + } + float life=1.0-tail; + // Raster owns its geometric departure; all other families release cleanly. + if(uEffect>2.5 && uEffect<3.5) life=1.0; + return half4(result*life); +} diff --git a/examples/captions-showcase/src/shaders/word-effects.sksl b/examples/captions-showcase/src/shaders/word-effects.sksl new file mode 100644 index 00000000..9266fd82 --- /dev/null +++ b/examples/captions-showcase/src/shaders/word-effects.sksl @@ -0,0 +1,565 @@ +// Samples the real white glyph alpha. The texture includes padding for displaced edges. +uniform shader uGlyph; +uniform shader uField; +uniform float3 iResolution; +uniform float iTime; +uniform float uEffect; +uniform float uProgress; +uniform float uAge; +uniform float uLightAge; +uniform float uTail; +uniform float uSeed; +uniform float uDirected; +uniform float uEntrance; +uniform float uHero; +uniform float uWarm; +uniform float uVariation; +uniform float uDistanceRange; +uniform float2 uWordSize; +uniform float uPadding; + +float hash(float2 p) { + return fract(sin(dot(p,float2(127.1,311.7)) + uSeed*13.4)*43758.5453); +} +float noise(float2 p) { + float2 i=floor(p), f=fract(p); f=f*f*(3.0-2.0*f); + return mix(mix(hash(i),hash(i+float2(1,0)),f.x), + mix(hash(i+float2(0,1)),hash(i+float2(1,1)),f.x),f.y); +} +float glyph(float2 p) { + if (p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return 0.0; + return float(uGlyph.eval(p).a); +} +half4 colored(float a, half3 color) { return half4(color*a,a); } + +// A cached distance field from the real font raster supplies bevel normals, contour +// spacing and prism rims. R is distance; GB is its unit gradient toward the interior. +float distanceAt(float2 p) { + if (p.x<0.0 || p.y<0.0 || p.x>=iResolution.x || p.y>=iResolution.y) return -uDistanceRange; + return (float(uField.eval(p).r)-0.5)*2.0*uDistanceRange; +} +float3 bevelAt(float2 p, float width) { + half4 field=uField.eval(p); + float d=(float(field.r)-0.5)*2.0*uDistanceRange; + float2 gradient=float2(field.gb)*2.0-1.0; + float magnitude=length(gradient); + gradient=magnitude<0.02 ? float2(0) : gradient/magnitude; + float slope=0.92*(1.0-smoothstep(0.0,width,max(d,0.0))); + return normalize(float3(-gradient*slope,sqrt(max(0.05,1.0-slope*slope)))); +} +float2 turn(float2 p,float a) { + float c=cos(a),s=sin(a); + return float2(c*p.x-s*p.y,s*p.x+c*p.y); +} +float cloud(float2 p) { + float v=noise(p)*0.57; + v+=noise(p*2.03+17.1)*0.28; + v+=noise(p*4.09-7.3)*0.15; + return v; +} +float3 spectral(float phase) { + return 0.55+0.45*cos(phase+float3(0.0,2.1,4.2)); +} +// Original analytic studio lighting: two long softboxes, a dark flag and a blue bounce. +float3 polished(float3 n,float fold,float tone) { + float3 r=reflect(float3(0,0,-1),n); + float horizon=0.5+0.5*sin(r.y*5.0+r.x*1.4+fold); + float strip=pow(0.5+0.5*cos(r.y*8.0-r.x*2.0+fold),14.0); + float pin=pow(max(dot(n,normalize(float3(-0.45,-0.65,1.0))),0.0),38.0); + float luma=0.26+0.49*smoothstep(0.12,0.8,horizon)+strip*0.62+pin*0.72; + float3 tint=mix(float3(0.68,0.83,1.0),float3(0.95,0.96,1.0),tone); + return min(tint*luma,float3(1.0)); +} +half4 material(float a,float3 color) { + return colored(clamp(a,0.0,1.0),half3(clamp(color,0.0,1.0))); +} + +// All colors below are premultiplied before compositing. The exact source glyph +// remains an opaque, light face; animation is a separate layer around that face. +float4 paint(float4 under,float alpha,float3 color) { + alpha=clamp(alpha,0.0,1.0); + return float4(clamp(color,0.0,1.0)*alpha,alpha)+under*(1.0-alpha); +} +float lineAt(float2 p,float2 a,float2 b,float width) { + float2 ab=b-a; + float t=clamp(dot(p-a,ab)/max(dot(ab,ab),0.01),0.0,1.0); + return 1.0-smoothstep(width,width+1.0,length(p-a-ab*t)); +} +float starAt(float2 p,float radius) { + p=abs(p)/max(radius,1.0); + float rays=max(exp(-p.x*2.4-p.y*22.0),exp(-p.y*2.4-p.x*22.0)); + return clamp(rays+exp(-length(p)*12.0),0.0,1.0)* + (1.0-smoothstep(0.8,1.15,max(p.x,p.y))); +} +float ringAt(float2 p,float2 radii,float width) { + return 1.0-smoothstep(width,width+1.0,abs(length(p/radii)-1.0)*min(radii.x,radii.y)); +} +float discAt(float2 p,float radius) { + return 1.0-smoothstep(radius-0.8,radius+0.8,length(p)); +} +float3 candy(float index) { return spectral(index*2.39996)*0.82+0.18; } + +// surfaceNormal, studioRadiance and displayRadiance adapt the photographic studio +// environment in examples/shader-mode/src/shaders/source-effects.sksl. +// MIT License — Copyright (c) 2026 Shader Effects Inc. +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +float3 displayRadiance(float3 color) { + color=max(color,0.0); + float x=min(color.r,min(color.g,color.b)); + color-=x<.08 ? x-6.25*x*x : .04; + float peak=max(color.r,max(color.g,color.b)); + if(peak>.76) { + float mapped=1.0-.24*.24/(peak+.24-.76); + color*=mapped/peak; + color=mix(color,float3(mapped),1.0-1.0/(.15*(peak-mapped)+1.0)); + } + return pow(max(color,0.0),float3(.454545)); +} +float3 studioRadiance(float3 r,float roughness) { + float3 warm=float3(1,.46,.12),cool=float3(.15,.38,.83); + float up=smoothstep(-.65,1.0,r.y); + float3 env=mix(float3(.012,.019,.036),float3(.56,.59,.64),up*up); + env+=float3(.55,.56,.59)*smoothstep(.3,.9,r.y); + env+=cool*exp(-pow((r.y+.45)/.2,2.0))*.16; + float facing=1.0-smoothstep(-.1,.18,r.z); + float2 p=r.xy/max(.32-r.z,.06); + float2 box=abs(p-float2(0,.3))-float2(.62,.28); + float sd=length(max(box,0.0))+min(max(box.x,box.y),0.0); + // Broad photographic softboxes remain continuous under the small angular + // quantization of a raster-derived normal field. + float soft=.14+roughness*.35; + env+=float3(2.9,2.87,2.82)*(1.0-smoothstep(-soft*.6,soft,sd))*facing; + env*=mix(1.0,.12,smoothstep(.5,.92,abs(r.x))); + env+=warm*exp(-pow(max(sd,0.0)/(.11+roughness*.08),2.0))*smoothstep(-.10,.18,sd)*.28*facing; + box=abs(p-float2(0,-.78))-float2(.7,.045); + sd=length(max(box,0.0))+min(max(box.x,box.y),0.0); + env+=float3(.58,.66,.82)*(1.0-smoothstep(-soft*.6,soft,sd))*facing; + float strip=exp(-pow((dot(r,normalize(float3(.65,.7,-.15)))-.42)/(.11+roughness*.16),2.0)); + return env+float3(1.35,1.45,1.6)*strip; +} +float3 surfaceNormal(float2 p,float2 uv,float s,float t,float phase,float fold) { + // GB is prefiltered once in the cached glyph field. One sample gives a + // smooth bevel normal without blurring font coverage or paying extra taps + // for every output pixel and frame. R retains the exact signed distance. + half4 field=uField.eval(p); + float d=(float(field.r)-.5)*2.0*uDistanceRange; + float2 grad=float2(field.gb)*2.0-1.0; + grad/=max(length(grad),.001); + float f=clamp(max(d,0.0)/(2.4*s),0.0,1.0); + // A continuously rounded fillet avoids a flat machined facet whose entire + // width flips light/dark under tiny normal changes along diagonal edges. + float slope=.66*cos(f*1.5707963); + float3 n=float3(-grad*slope,-sqrt(max(.001,1.0-slope*slope))); + // Broad curvature continues across the front face: reflected flags describe + // a solid object rather than a flat gradient pasted inside a text mask. + n.xy+=float2(sin(uv.x*3.5+uv.y*1.3+phase)*.13, + sin(uv.y*5.8+uv.x*.7+t*.63+phase)*.18)*fold; + return normalize(n); +} +float3 litMetal(float2 p,float3 tint,float roughness,float fold,float phase) { + if(glyph(p)<.001) return float3(0); + float h=uWordSize.y,s=max(h/210.0,.45); + float2 uv=(p-iResolution.xy*.5)/h; + float3 n=surfaceNormal(p,uv,s,uLightAge,phase,fold); + float3 view=normalize(float3(uv*.045,1.0)); + float3 r=reflect(view,n); + r.xy=turn(float2(r.x,-r.y),-.34+.13*sin(uLightAge*.75+phase)); + r.yz=turn(r.yz,.20+.16*sin(uLightAge*.56+phase*.3)); + float3 env=studioRadiance(r,roughness); + // A broad black studio flag crosses the curved front, separating polished + // highlights from reflected darkness even when a letter has a wide flat face. + float flag=.05+.95*smoothstep(-.60,.60,sin(r.y*6.0+r.x*1.8+phase*.17)); + env*=flag; + // A long reflected dark flag sweeps the front independently of the narrow + // bevel. This supplies real light/dark separation instead of a uniformly + // white plate when a low-curvature material faces the camera directly. + float plate=sin(uv.y*6.2+uv.x*1.1+phase*.31+uLightAge*.48); + float frontFlag=.045+.955*smoothstep(-.35,.45,plate); + env*=mix(1.0,frontFlag,(1.0-roughness)*.88); + float fresnel=pow(1.0-max(-dot(n,view),0.0),4.0); + float3 radiance=env*(tint*.86+.09)+float3(.055,.061,.078); + radiance+=env*.18*fresnel; + float3 color=displayRadiance(radiance); + // Controlled reflected bounce keeps even the short words readable over film. + return .18+.82*color; +} +float3 sphereLight(float2 q,float radius,float3 tint) { + float2 xy=q/max(radius,1.0); + float z=sqrt(max(0.0,1.0-dot(xy,xy))); + float3 n=normalize(float3(xy,-z)); + float diffuse=max(dot(n,normalize(float3(-.4,-.6,-1))),0.0); + float spec=pow(max(dot(n,normalize(float3(-.35,-.55,-1))),0.0),24.0); + return displayRadiance(tint*(.06+diffuse*.6)+float3(1.4)*spec); +} +float softStar(float2 p,float radius) { + float2 q=p/max(radius,1.0); + float core=exp(-dot(q,q)*40.0); + float rays=exp(-abs(q.x)*2.5-abs(q.y)*35.0)+exp(-abs(q.y)*5.0-abs(q.x)*35.0); + return clamp(core+rays*.65,0.0,1.0)*(1.0-smoothstep(.8,1.8,length(q))); +} +float bloomLine(float d,float width) { return exp(-abs(d)/width); } +// The cached SDF saturates at -uDistanceRange outside its narrow band. Never +// interpret that sentinel as a real contour, especially on giant hero glyphs. +float sdfGlow(float distance,float offset,float width) { + float support=smoothstep(-uDistanceRange+.5,-uDistanceRange+3.0,distance); + return bloomLine(distance+min(offset,uDistanceRange-5.0),width)*support; +} + +// V6 material library. The exact glyph is retained; only radiance and a narrow +// field around its edge change. These are optical materials, not toy ornaments. +float sweepLight(float2 uv,float time,float phase,float width) { + float x=uv.x*.48+uv.y*.85-sin(time*.72+phase)*.62; + return exp(-x*x/(width*width)); +} +float cellularStars(float2 p,float depth,float time) { + p=p*depth+float2(time*.11,-time*.04); + float2 cell=floor(p),q=fract(p)-.5; + float seed=hash(cell+depth); + float r=dot(q,q); + return step(.965,seed)*exp(-r*(150.0+seed*210.0))*(.65+.35*sin(seed*29.0+time)); +} +float opticalEdge(float d,float2 uv,float time,float phase) { + float scan=.28+.72*pow(.5+.5*sin(uv.x*4.1+uv.y*5.0-time*2.6+phase),5.0); + return sdfGlow(d,7.0+min(uWordSize.y/210.0,3.0),.85)*scan; +} +float3 restrainedSpectrum(float phase) { + return mix(float3(.42,.68,.88),float3(.96,.79,.58),.5+.5*sin(phase)); +} + +half4 directedWord(float2 coord) { + float tail=clamp(uTail,0.0,1.0),life=1.0-tail*mix(.72,1.0,uHero); + float h=uWordSize.y,s=max(h/210.0,.45),t=uLightAge; + float phase=uVariation*6.283185+uSeed*.031; + float2 center=iResolution.xy*.5,local=coord-center,uv=local/h; + float d=distanceAt(coord); + // The SDF is a narrow band, never an infinite shape. Far transparent pixels + // need neither material noise nor fake saturated-distance contours. + if(d < -uDistanceRange+1.0) return half4(0); + float actual=glyph(coord); + float3 face=float3(.86),edgeColor=float3(.61,.79,.94); + float4 back=float4(0); + float3 titanium=float3(.68,.78,.90); + + // 0 — PLASMA SHEATH. A film of advected combustion follows the real outline. + // A white-hot filament, carbon-blue body and sparse orange thermal fractures + // replace the cartoon flame crown. Intensity is spatial, never a global flash. + if(uEffect<.5) { + float2 q=uv*7.0+float2(phase,-t*1.7); + float curl=cloud(q+float2(noise(q*.6+t*.17),noise(q*.7+4.0))*2.0); + float flame=cloud(uv*float2(13.0,5.0)+float2(phase+t*.27,-t*3.5)); + float sheath=exp(-abs(d+5.0*s+curl*9.0*s)/(1.2*s+.7)); + float heat=pow(smoothstep(.25,.85,flame),1.5); + float glow=sdfGlow(d,6.0*s,6.0*s)*(.2+.6*heat); + back=paint(back,glow*.42,float3(.18,.39,.64)); + back=paint(back,sheath*(.35+.65*heat),mix(float3(.35,.64,.90),float3(1.0,.72,.34),heat)); + float filament=sdfGlow(d,2.2*s,.65*s)*(.38+.62*heat); + back=paint(back,filament,float3(.91,.96,1.0)); + face=litMetal(coord,titanium,.19,.75,phase)*.55+float3(.055,.085,.13); + float fracture=exp(-abs(curl-.49)*64.0); + float thermal=sweepLight(uv,t,phase,.13); + face=face*(.86+.14*curl); + face=mix(face,float3(.98,.64,.25),fracture*(.45+.55*heat)); + face+=pow(fracture,2.0)*float3(.16,.18,.19); + face+=thermal*float3(.14,.21,.27); + edgeColor=mix(float3(.52,.74,.94),float3(.97,.64,.28),heat); + } + // 1 — DEEP FIELD. Independently drifting nebular layers and sparse pinpoints + // live under the glass face. There are no decorative stars outside the word. + else if(uEffect<1.5) { + float mist=cloud(uv*4.0+float2(t*.13,phase)); + float deeper=cloud(uv*7.5+float2(-t*.08,phase+mist*1.7)); + face=litMetal(coord,float3(.48,.66,.86),.18,.65,phase); + face=face*(.46+.34*mist)+float3(.04,.075,.12); + float seam=exp(-abs(mist+deeper*.32-.65)*18.0); + face+=seam*float3(.16,.32,.44); + face+=float3(.45,.54,.61)*(cellularStars(uv,26.0,t)+cellularStars(uv,47.0,-t*.7)*.5); + float halo=sdfGlow(d,3.0*s,2.0*s)*(.2+.4*deeper); + back=paint(back,halo,float3(.22,.47,.72)); + } + // 2 — DIFFRACTION. Fine coherent wave intersections travel through silver. + // Their caustic bands are continuous and stay inside the actual typography. + else if(uEffect<2.5) { + float a=sin(uv.x*15.0+uv.y*6.0-t*1.7+phase); + float b=sin(uv.y*18.0-uv.x*3.0+t*.95); + float caustic=exp(-abs(a+b*.63)*19.0); + face=litMetal(coord,float3(.78,.86,.93),.12,.25,phase); + face*=.78+.22*smoothstep(-.5,.7,a*b); + face+=caustic*float3(.24,.29,.32); + back=paint(back,sdfGlow(d,4.5*s,.8*s)*(.3+.6*caustic),float3(.81,.89,.96)); + } + // 3 — LASER ETCH. Two hairline construction contours reveal with a measured + // moving light, leaving a clean matte face and sharp architectural silhouette. + else if(uEffect<3.5) { + face=litMetal(coord,float3(.89,.91,.93),.65,.10,phase); + float scan=.5+.5*sin(uv.x*2.0-t*2.2+phase); + float reveal=smoothstep(.22,.72,scan); + back=paint(back,sdfGlow(d,4.0*s,.65*s)*reveal,float3(.90,.94,.97)); + back=paint(back,sdfGlow(d,11.0*s,.55*s)*(.18+.58*reveal),float3(.39,.63,.80)); + face+=sweepLight(uv,t*1.5,phase,.035)*.16; + } + // 4 — GRAVITATIONAL INTERFERENCE. The slowly moving origin bends wavefronts + // through the face. No objects bounce and the complete word remains fixed. + else if(uEffect<4.5) { + float2 lens=uv-float2(sin(t*.5+phase)*.55,cos(t*.43+phase)*.12); + float radius=length(lens*float2(.75,1.5)); + float warp=1.0/(.2+radius); + float waves=sin(radius*24.0-warp*2.2-t*2.0); + float caustic=pow(.5+.5*waves,12.0); + face=litMetal(coord,float3(.72,.81,.89),.18,.20,phase+warp*.07); + face=face*(.83+.17*waves)+caustic*float3(.14,.19,.23); + back=paint(back,sdfGlow(d,4.0*s+caustic*3.0*s,.7*s)*.6,float3(.60,.78,.91)); + } + // 5 — DISCHARGE. Branching field lines and fine edge arcs, driven by a + // continuously moving flow field rather than whole-word jitter or flicker. + else if(uEffect<5.5) { + float flow=noise(float2(uv.y*5.0+t*.8,phase))*2.4; + float branch=sin(uv.x*16.0+flow+sin(uv.y*12.0-t*2.0)*.6); + float arc=exp(-abs(branch)*42.0); + float energy=.6+.4*noise(float2(t*2.0,phase)); + face=litMetal(coord,float3(.60,.77,.91),.25,.15,phase); + face+=arc*energy*float3(.20,.29,.35); + float contour=sdfGlow(d,5.0*s+noise(uv*9.0+t)*3.0*s,.7*s); + back=paint(back,contour*(.45+.55*arc),float3(.60,.82,1.0)); + back=paint(back,sdfGlow(d,3.0*s,3.0*s)*arc*.3,float3(.16,.35,.58)); + } + // 6 — LIGHT SHEAR. A narrow slanted exposure crosses a brushed surface; + // displaced exterior contour lines describe optical dispersion, not orbiting dots. + else if(uEffect<6.5) { + float sweep=sweepLight(uv,t*1.2,phase,.08); + face=litMetal(coord,float3(.76,.84,.90),.40,.10,phase); + face=face*(.91+.09*sin(coord.y/(1.3*s)))+sweep*float3(.23,.27,.30); + float shifted=distanceAt(coord+float2(2.5,-.7)*s); + back=paint(back,sdfGlow(shifted,6.0*s,.75*s)*(.20+.75*sweep),float3(.64,.81,.94)); + } + // 7 — MERCURY. Moving normal ripples bend a broad reflected light. The alpha + // edge never swims, so flowing fluid can be used on short spoken words too. + else if(uEffect<7.5) { + float liquid=sin(uv.y*11.0+sin(uv.x*5.0+t*.8)*1.6-t*1.65+phase); + float ripple=cos(uv.x*9.0-uv.y*4.0+t*.6+phase); + face=litMetal(coord,float3(.70,.85,.90),.07,.72,phase+liquid*.18); + face*=.77+.23*smoothstep(-.6,.7,liquid); + face+=pow(.5+.5*sin(liquid*3.0+ripple),10.0)*float3(.23,.28,.30); + back=paint(back,sdfGlow(d,4.0*s+liquid*1.2*s,.85*s)*(.4+.3*ripple),float3(.53,.76,.86)); + } + // 8 — SPECTRAL FRACTURE. Angular internal planes refract soft light. All + // fragments remain part of the word, with no confetti emitted into the shot. + else if(uEffect<8.5) { + float2 grid=uv*float2(8.0,12.0); + float facet=noise(floor(grid)+phase); + float plane=fract(grid.x+grid.y*.63+facet*.3); + float seam=exp(-min(plane,1.0-plane)*55.0); + face=litMetal(coord,float3(.73,.81,.90),.18,.15,phase+facet*.9); + face*=.76+.24*smoothstep(.0,1.0,facet); + face+=seam*(.12+.16*sin(t*1.4+facet*6.0))*float3(.70,.84,.94); + back=paint(back,sdfGlow(d,5.0*s,.7*s)*(.25+.55*sin(t+phase+uv.x*3.0)*sin(t+phase+uv.x*3.0)),float3(.68,.82,.94)); + } + // 9 — SCAN GLASS. A luminous scan plane rolls through thin optical layers. + // Microscopic interference remains local; no RGB glitch breaks the letters. + else if(uEffect<9.5) { + float scan=sweepLight(uv,t*.9,phase,.055); + float lines=.94+.06*sin(coord.y/(.75*s)+t*2.5); + face=litMetal(coord,float3(.55,.73,.88),.14,.24,phase)*lines; + face+=scan*float3(.22,.29,.34); + back=paint(back,sdfGlow(d,4.0*s,.65*s)*(.30+.70*scan),float3(.72,.88,1.0)); + back=paint(back,sdfGlow(d,10.0*s,.55*s)*(.12+.40*scan),float3(.32,.57,.77)); + } + // 10 — TOPOGRAPHIC ENGRAVING. Concentric, measured surface lines are carved + // into a ceramic plate and picked out by a raking white light. + else if(uEffect<10.5) { + float terrain=cloud(uv*5.0+float2(phase,t*.06)); + float contour=pow(.5+.5*cos(terrain*77.0),22.0); + face=litMetal(coord,float3(.90,.90,.86),.74,.08,phase); + face*=1.0-contour*.18; + face+=sweepLight(uv,t,phase,.12)*contour*.20; + back=paint(back,sdfGlow(d,4.5*s,.7*s)*(.3+.5*sweepLight(uv,t,phase,.35)),float3(.82,.88,.91)); + } + // 11 — WAVEFRONT. Coherent pressure fronts pass through the metal while + // only their near-field energy escapes the edges. + else if(uEffect<11.5) { + float radius=length(uv-float2(sin(phase)*.6,-.3)); + float wave=sin(radius*23.0-t*3.1+phase); + float crest=pow(.5+.5*wave,18.0); + face=litMetal(coord,float3(.73,.83,.91),.16,.25,phase); + face*=.86+.14*wave; + face+=crest*float3(.18,.24,.28); + back=paint(back,sdfGlow(d,4.0*s+crest*4.0*s,.8*s)*(.25+.7*crest),float3(.59,.80,.94)); + } + // 12 — LUNAR QUARTZ. A restrained machined rim encloses stratified optical + // depth. A broad light-sheet exposes fine lunar-like striations under the face. + else if(uEffect<12.5) { + float strata=cloud(uv*float2(5.0,16.0)+float2(phase,-t*.10)); + float refract=sin(uv.y*12.0+strata*3.0+sin(uv.x*4.0+t*.3)); + float sheet=sweepLight(uv,t*.85,phase*.35,.11); + float reflected=sin(uv.y*5.8+uv.x*1.0+strata*.30+t*.30+phase*.16); + float reflectedBand=smoothstep(-.38,.42,reflected); + float3 opticalBase=mix(float3(.25,.34,.43),float3(.70,.82,.90),reflectedBand); + face=opticalBase*.68+litMetal(coord,float3(.68,.82,.94),.075,1.15,phase*.35)*.32; + face*=.86+.14*smoothstep(-.6,.7,refract); + face+=sheet*float3(.30,.34,.38); + face+=pow(.5+.5*refract,16.0)*float3(.10,.18,.25); + face+=exp(-max(d-1.1*s,0.0)/(1.1*s))*float3(.20,.23,.25); + back=paint(back,sdfGlow(d,3.0*s,1.2*s)*(.20+.55*sheet),float3(.53,.74,.90)); + back=paint(back,sdfGlow(d,9.0*s,.65*s)*(.15+.60*sheet),float3(.81,.89,.94)); + } + // 13 — CRYSTAL. Fine branching mineral veins and diffuse internal refraction; + // no snowflakes or falling glass diamonds. + else if(uEffect<13.5) { + float warp=noise(float2(uv.y*7.0+t*.12,phase))*3.0; + float a=sin(uv.x*18.0+warp),b=sin(uv.y*13.0+uv.x*8.0+phase); + float veins=exp(-abs(a+b*.27)*35.0); + face=litMetal(coord,float3(.67,.84,.93),.34,.18,phase); + face*=.82+.18*noise(uv*8.0+phase); + face+=veins*float3(.22,.28,.31); + back=paint(back,sdfGlow(d,4.0*s,.9*s)*(.25+.65*veins),float3(.58,.81,.96)); + } + // 14 — POLARIZED FILM. Narrow-band blue/amber interference is part of the + // surface response; thin reflected contours remain anchored to the lettering. + else if(uEffect<14.5) { + float bands=sin(uv.x*6.0+uv.y*9.0+sin(uv.x*2.0+t*.5)+phase-t*.65); + float3 tint=restrainedSpectrum(bands*1.4+phase*.3); + face=litMetal(coord,mix(float3(.75,.82,.87),tint,.4),.18,.22,phase); + face+=pow(.5+.5*bands,18.0)*tint*.24; + back=paint(back,sdfGlow(d,4.0*s+bands*.8*s,.7*s)*(.30+.50*pow(.5+.5*bands,3.0)),tint); + } + // 15 — PLATINUM HORIZON. A pale final object with one warm horizon reflected + // through it. The held word breathes with a slow light field, without glitter. + else { + float horizon=uv.y*.8+uv.x*.12-.10*sin(t*.45+phase*.15); + float line=exp(-horizon*horizon/.0018); + float transition=smoothstep(-.16,.16,horizon); + float3 horizonMetal=mix(float3(.27,.34,.40),float3(.80,.85,.87),transition); + face=horizonMetal*.76+litMetal(coord,float3(.80,.87,.90),.12,.85,phase*.15)*.24; + face+=line*float3(.30,.25,.16); + float light=sweepLight(uv,t*.55,phase*.15,.16); + face+=light*.11; + face+=exp(-max(d-1.1*s,0.0)/(1.1*s))*float3(.18,.21,.23); + edgeColor=mix(float3(.63,.79,.88),float3(.95,.81,.59),line); + back=paint(back,sdfGlow(d,3.5*s,.9*s)*(.22+.68*line),edgeColor); + back=paint(back,sdfGlow(d,10.0*s,.65*s)*(.18+.45*light),float3(.76,.85,.91)); + } + + // A shared precision rim travels around the true outline. Its finite support + // preserves counters and gives each optical material a small near-field presence. + back=paint(back,opticalEdge(d,uv,t,phase)*.70,edgeColor); + float shadow=smoothstep(-2.8*s,.5*s,distanceAt(coord-float2(.8,1.4)*s))*.42*(1.0-actual); + back=paint(back,shadow,float3(.008,.012,.018)); + float keyline=smoothstep(-.85*s,-.1*s,d); + float4 result=paint(back,keyline,float3(.018,.026,.036)); + // A light material floor keeps very short connectors legible without flattening + // the specular structures. Glyph alpha is never eroded by the material. + result=paint(result,actual,clamp(max(face,float3(.34)),0.0,1.0)); + return half4(result*life); +} + +half4 main(float2 coord) { + if (uDirected>0.5) return directedWord(coord); + float2 uv=coord/iResolution.xy; + float enter=1.0-smoothstep(0.0,0.22,uProgress); + float tail=clamp(uTail,0.0,1.0); + half3 ivory=half3(1.0,0.98,0.90); + half3 cyan=half3(0.25,0.93,1.0); + half3 amber=half3(1.0,0.54,0.18); + float trace=1.0-tail*0.72; + + // Liquid: both letter contours and their specular bands undulate. + if (uEffect<0.5) { + float amplitude=(4.0+enter*22.0+tail*28.0)*(iResolution.y/300.0); + float2 warp=float2(sin(coord.y*0.050-uAge*17.0+uSeed), + sin(coord.x*0.027+uAge*10.0+sin(coord.y*0.031))); + float2 p=coord+warp*amplitude; + p.y += tail*18.0; + float a=glyph(p); + float ridge=0.5+0.5*sin(coord.y*0.043+warp.x*1.4-uAge*4.0); + half3 color=mix(cyan,ivory,smoothstep(0.15,0.75,ridge)); + float shine=max(0.0,glyph(p+float2(0,2.0))-glyph(p+float2(0,-2.0))); + color=mix(color,half3(1.0),shine); + return colored(a*trace,color); + } + + // Digital: separate color channels and shear only selected horizontal rows. + if (uEffect<1.5) { + float row=floor(coord.y/10.0); + float tick=floor(uAge*24.0); + float h=hash(float2(row,tick)); + float burst=max(enter,step(0.88,h)*0.45)+tail; + float dx=(h-0.5)*52.0*burst; + float2 p=coord+float2(dx,0.0); + float split=2.0+burst*8.0; + float ar=glyph(p+float2(split,0)); + float ag=glyph(p); + float ab=glyph(p-float2(split,0)); + float scan=0.87+0.13*step(1.0,mod(coord.y,4.0)); + float alpha=max(ar,max(ag,ab))*scan*trace; + half3 color=half3(ar,ag,ab)*scan*trace; + return half4(min(color,half3(alpha)),alpha); + } + + // Pixel materialization: coarse fragments become crisp glyphs during the readable hold. + if (uEffect<2.5) { + float block=1.0+floor(enter*13.0+tail*10.0); + float2 cell=floor(coord/block); + float2 p=(cell+0.5)*block; + float n=hash(floor(coord/8.0)); + float reveal=smoothstep(0.0,0.22,uProgress); + float visible=step(n,reveal)*(1.0-step(1.0-tail,n)); + float a=glyph(p)*visible; + half3 color=mix(cyan,ivory,reveal); + return colored(a,color); + } + + // Dust: the same tiled noise erodes the glyph and advects its remaining fragments. + if (uEffect<3.5) { + float erosion=max(tail,smoothstep(0.78,1.0,uProgress)*0.26); + float2 cell=floor(coord/7.0); + float n=hash(cell); + float2 drift=float2(18.0+hash(cell+5.0)*32.0,-12.0-hash(cell+17.0)*40.0)*erosion; + float2 p=coord-drift; + float a=glyph(p); + float threshold=erosion*1.12; + float keep=smoothstep(threshold-0.045,threshold+0.045,n); + float edge=1.0-smoothstep(0.0,0.10,abs(n-threshold)); + half3 color=mix(ivory,amber,edge); + a*=keep*(1.0-tail); + return colored(a,color); + } + + // Hologram: scanning light and a faint offset reconstruction, inside letter silhouettes. + if (uEffect<4.5) { + float scanY=fract(uAge*1.7+uSeed)*iResolution.y; + float scan=exp(-abs(coord.y-scanY)*0.10); + float stripe=0.74+0.26*step(1.0,mod(coord.y,4.0)); + float reveal=smoothstep(0.0,0.18,uProgress); + float mask=1.0-smoothstep(reveal,reveal+0.06,1.0-uv.y); + float a=glyph(coord)*stripe*mask; + float echo=glyph(coord+float2(3.0*sin(uAge*17.0),0.0))*0.15; + a=max(a,echo)*trace; + return colored(a,mix(cyan,ivory,scan)); + } + + // Elastic echo: spring-like scale, displaced spectral copies, then a restrained trace. + float scale=1.0+0.16*exp(-uAge*20.0)*cos(uAge*28.0); + float2 center=iResolution.xy*0.5; + float2 p=(coord-center)/scale+center; + float separation=enter*15.0+tail*22.0; + float a=glyph(p); + float ar=glyph(p+float2(separation,-separation*0.22))*0.48; + float ab=glyph(p-float2(separation,-separation*0.22))*0.48; + float alpha=max(a,max(ar,ab))*trace; + half3 color=(ivory*a + half3(1.0,0.23,0.49)*ar + cyan*ab)*trace; + return half4(min(color,half3(alpha)),alpha); +} diff --git a/examples/captions-showcase/src/shaders/word-plane.sksl b/examples/captions-showcase/src/shaders/word-plane.sksl new file mode 100644 index 00000000..27a742e6 --- /dev/null +++ b/examples/captions-showcase/src/shaders/word-plane.sksl @@ -0,0 +1,30 @@ +// Inverse projective mapping of one cached, styled caption drawing. +// Input/output are premultiplied RGBA. Transparent texture padding preserves +// antialiasing and the original outline beyond the nominal advance rectangle. +uniform shader uSource; +uniform float2 uWordSize; +uniform float2 uTextureSize; +uniform float2 uOrigin; +uniform float uPadding; +uniform float2 uSourceRange; +uniform float uShade; +uniform float3 uInverse0; +uniform float3 uInverse1; +uniform float3 uInverse2; + +half4 main(float2 coord) { + float2 normalized = (coord + uOrigin) / uWordSize; + float3 position = float3(normalized, 1.0); + float denominator = dot(uInverse2, position); + if (abs(denominator) < 0.000001) return half4(0.0); + float2 uv = float2(dot(uInverse0, position), dot(uInverse1, position)) / denominator; + // Clip in original source coordinates, after inverse projection. A screen + // mask would cut the wrong glyph fragment as a plane moves. Intervals are + // half-open; outer panels retain their original padded font overhang. + if (uSourceRange.x > 0.0 && uv.x < uSourceRange.x) return half4(0.0); + if (uSourceRange.y < 1.0 && uv.x >= uSourceRange.y) return half4(0.0); + float2 source = uv * uWordSize + uPadding; + if (source.x < 0.0 || source.y < 0.0 || source.x >= uTextureSize.x || source.y >= uTextureSize.y) return half4(0.0); + half4 rgba = uSource.eval(source); + return half4(rgba.rgb * uShade, rgba.a); +} diff --git a/examples/captions-showcase/src/word_effects.rs b/examples/captions-showcase/src/word_effects.rs new file mode 100644 index 00000000..1ca6d8ce --- /dev/null +++ b/examples/captions-showcase/src/word_effects.rs @@ -0,0 +1,697 @@ +//! Actual glyph deformation using a cached, padded texture from `FFrames`' SVG renderer. +//! Six legacy treatments, sixteen lit materials and fifteen distinct showcase techniques. +//! Every shader samples actual glyph geometry prepared by the `FFrames` text renderer. +use std::{ + collections::HashMap, + sync::{Mutex, PoisonError}, +}; + +use fframes::{ + FFramesContext, Shader, ShaderUniforms, Svgr, + captions::{CaptionWordScope, WordState}, + media::ImageData, + svgr, +}; + +type GlyphKey = (String, u32, u32, String, u64); + +#[path = "glyph_field.rs"] +mod glyph_field; + +#[path = "glass_nebula_material.rs"] +mod glass_nebula_material; +#[path = "goo_material.rs"] +mod goo_material; +#[path = "irradiance_material.rs"] +mod irradiance_material; +pub use glass_nebula_material::GlassNebulaTuning; +pub use goo_material::GooTuning; +pub use irradiance_material::IrradianceTuning; + +#[derive(Clone)] +struct GlyphTexture { + image: ImageData<'static>, + field: Option>, +} + +/// Visual timing is independent of the source word's spoken interval. +#[derive(Clone, Copy)] +pub struct WordDirection { + /// Keep this word fully present until this caption-clock time, without retiming speech. + pub hold_until: Option, + /// Duration of the departure after the visual release. + pub exit_seconds: f64, + /// Make the first frame readable and preserve full brightness through a planned hold. + pub hero: bool, + /// Reserve a subtle warm horizon within the final platinum WIN treatment. + pub warm: bool, + /// Per-word art direction in [0, 1]: motion phase, particle placement and tint. + pub variation: f32, +} + +/// A #RRGGBB display color converted once to the linear RGB used by `LiquidMetal`. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct LiquidColor(pub [f32; 3]); + +impl<'de> serde::Deserialize<'de> for LiquidColor { + fn deserialize>(deserializer: D) -> Result { + let text = ::deserialize(deserializer)?; + let hex = text + .strip_prefix('#') + .filter(|s| s.len() == 6 && s.bytes().all(|byte| byte.is_ascii_hexdigit())) + .ok_or_else(|| serde::de::Error::custom("liquid color must be #RRGGBB"))?; + let value = u32::from_str_radix(hex, 16) + .map_err(|_| serde::de::Error::custom("liquid color must be #RRGGBB"))?; + Ok(Self::from_srgb_bytes([ + (value >> 16) as u8, + (value >> 8) as u8, + value as u8, + ])) + } +} + +impl LiquidColor { + fn from_srgb_bytes(rgb: [u8; 3]) -> Self { + Self(rgb.map(|byte| { + let value = f32::from(byte) / 255.0; + if value <= 0.04045 { + value / 12.92 + } else { + ((value + 0.055) / 1.055).powf(2.4) + } + })) + } +} + +/// Shaders.com `LiquidMetal` controls. Defaults and limits follow the upstream material. +/// Placement and shape come from the caption scope; no separate image loader is involved. +#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct LiquidTuning { + pub turbulence: f32, + pub ripple: f32, + pub warp: f32, + pub speed: f32, + pub sharpness: f32, + pub environment: f32, + pub env_rotation: f32, + pub dispersion: f32, + pub light_angle: f32, + /// Relative to glyph height; bounded by the cached 32-pixel distance band. + pub bevel_width: f32, + pub bevel_shape: f32, + pub light_color: LiquidColor, + pub dark_color: LiquidColor, +} + +impl Default for LiquidTuning { + fn default() -> Self { + Self { + turbulence: 1.0, + ripple: 4.0, + warp: 1.5, + speed: 0.5, + sharpness: 0.6, + environment: 1.5, + env_rotation: 0.0, + dispersion: 0.25, + light_angle: 265.0, + bevel_width: 0.05, + bevel_shape: 0.0, + light_color: LiquidColor::from_srgb_bytes([238, 240, 246]), + dark_color: LiquidColor::from_srgb_bytes([20, 20, 20]), + } + } +} + +impl LiquidTuning { + pub fn validate(self) -> Result<(), &'static str> { + for (value, low, high, error) in [ + ( + self.turbulence, + 0.0, + 1.5, + "liquid.turbulence must be finite and within 0..=1.5", + ), + ( + self.ripple, + 0.5, + 8.0, + "liquid.ripple must be finite and within 0.5..=8", + ), + ( + self.warp, + 0.0, + 1.5, + "liquid.warp must be finite and within 0..=1.5", + ), + ( + self.speed, + 0.0, + 4.0, + "liquid.speed must be finite and within 0..=4", + ), + ( + self.sharpness, + 0.0, + 1.0, + "liquid.sharpness must be finite and within 0..=1", + ), + ( + self.environment, + 0.0, + 2.0, + "liquid.environment must be finite and within 0..=2", + ), + ( + self.env_rotation, + 0.0, + 360.0, + "liquid.env_rotation must be finite degrees within 0..=360", + ), + ( + self.dispersion, + 0.0, + 1.0, + "liquid.dispersion must be finite and within 0..=1", + ), + ( + self.light_angle, + 0.0, + 360.0, + "liquid.light_angle must be finite degrees within 0..=360", + ), + ( + self.bevel_width, + 0.005, + 0.2, + "liquid.bevel_width must be finite and within 0.005..=0.2", + ), + ( + self.bevel_shape, + 0.0, + 1.0, + "liquid.bevel_shape must be finite and within 0..=1", + ), + ] { + if !value.is_finite() || !(low..=high).contains(&value) { + return Err(error); + } + } + for color in [self.light_color, self.dark_color] { + if color + .0 + .iter() + .any(|value| !value.is_finite() || !(0.0..=1.0).contains(value)) + { + return Err("liquid colors must contain finite linear RGB within 0..=1"); + } + } + Ok(()) + } +} + +/// Data-driven art controls for the two geometric relief materials (25/26). +/// The cached glyph and compiled shader are shared across all settings. +#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct ReliefTuning { + /// Extrusion and recess depth, in [0.5, 1.5]. Bounded by the texture padding. + pub depth: f32, + /// Rib/channel spacing, in [0.65, 1.75]. Larger values give broader features. + pub detail_scale: f32, + /// Material motion speed, in [0.25, 2]. Does not retime speech or the hold. + pub speed: f32, + /// Exposure multiplier, in [0.5, 1.5], before display encoding. + pub light: f32, +} + +impl Default for ReliefTuning { + fn default() -> Self { + Self { + depth: 1.0, + detail_scale: 1.0, + speed: 1.0, + light: 1.0, + } + } +} + +impl ReliefTuning { + /// Validate once when reading authored direction, before rendering frames. + pub fn validate(self) -> Result<(), &'static str> { + for (value, low, high, error) in [ + ( + self.depth, + 0.5, + 1.5, + "relief.depth must be finite and within 0.5..=1.5", + ), + ( + self.detail_scale, + 0.65, + 1.75, + "relief.detail_scale must be finite and within 0.65..=1.75", + ), + ( + self.speed, + 0.25, + 2.0, + "relief.speed must be finite and within 0.25..=2.0", + ), + ( + self.light, + 0.5, + 1.5, + "relief.light must be finite and within 0.5..=1.5", + ), + ] { + if !value.is_finite() || !(low..=high).contains(&value) { + return Err(error); + } + } + Ok(()) + } + + fn bounded(self) -> Self { + fn value(value: f32, low: f32, high: f32) -> f32 { + if value.is_finite() { + value.clamp(low, high) + } else { + 1.0 + } + } + Self { + depth: value(self.depth, 0.5, 1.5), + detail_scale: value(self.detail_scale, 0.65, 1.75), + speed: value(self.speed, 0.25, 2.0), + light: value(self.light, 0.5, 1.5), + } + } +} + +/// Optional material controls, resolved once from typed caption metadata. +#[derive(Clone, Copy, Debug, Default)] +pub struct MaterialControls { + pub relief: ReliefTuning, + pub liquid: LiquidTuning, + pub irradiance: IrradianceTuning, + pub goo: GooTuning, + pub glass_nebula: GlassNebulaTuning, +} + +fn entrance_seconds(duration: f64) -> f64 { + (duration * 0.28) + .clamp(0.06, 0.10) + .min(duration * 0.4) + .max(0.001) +} + +pub struct WordEffects { + shader: Option, + // Specializing a family lets Skia eliminate the other fifteen material paths. + // The compiled shader cache then reuses each small program across all its words. + directed_shaders: Vec, + // Authored font family/weight join the dimensions in the cache key. Reconstruct + // WordEffects if registered font bytes change in a persistent application. + glyphs: Mutex>, +} + +impl WordEffects { + /// Whether this renderer can execute glyph-local runtime shaders. + #[allow(dead_code)] // Consumers without projective glyphs do not need this capability query. + pub fn shaders_enabled(&self) -> bool { + self.shader.is_some() + } + + pub fn new(shaders: bool) -> Self { + let source = include_str!("shaders/word-effects.sksl"); + Self { + shader: shaders.then(|| Shader::sksl(source)), + directed_shaders: if shaders { + (0..31) + .map(|effect| { + let (source, effect) = if effect < 16 { + (source, effect) + } else if effect < 22 { + (include_str!("shaders/showcase-effects.sksl"), effect - 16) + } else if effect < 25 { + (include_str!("shaders/semantic-effects.sksl"), effect - 22) + } else if effect < 27 { + (include_str!("shaders/relief-effects.sksl"), effect - 25) + } else if effect == 27 { + (include_str!("shaders/liquid-metal.sksl"), 0) + } else if effect == 28 { + (include_str!("shaders/irradiance.sksl"), 0) + } else if effect == 29 { + (include_str!("shaders/goo.sksl"), 0) + } else { + (include_str!("shaders/glass-nebula.sksl"), 0) + }; + Shader::sksl( + source + .replace( + "uniform float uEffect;", + &format!("const float uEffect = {effect}.0;"), + ) + .replace( + "uniform float uDirected;", + "const float uDirected = 1.0;", + ), + ) + }) + .collect() + } else { + Vec::new() + }, + glyphs: Mutex::new(HashMap::new()), + } + } + + fn glyph( + &self, + scope: &CaptionWordScope<'_, '_>, + ctx: &FFramesContext<'_, '_>, + padding: f64, + width: u32, + height: u32, + ) -> Option { + let layout = scope.word.metadata.get("layout"); + let family = layout + .and_then(|layout| layout.get("font_family")) + .and_then(serde_json::Value::as_str) + .unwrap_or(""); + let weight = layout + .and_then(|layout| layout.get("font_weight")) + .and_then(serde_json::Value::as_u64) + .unwrap_or(0); + let key = ( + scope.word.text.to_owned(), + width, + height, + family.to_owned(), + weight, + ); + if let Some(image) = self + .glyphs + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get(&key) + { + return Some(image.clone()); + } + // The scope makes this mask using the actual font and glyph positions used by captions. + // Its white alpha is the shader's texture, rather than a rectangular paint layer. + let glyph = scope.mask_text(svgr!( + + )); + let image = ctx.render_svgr( + svgr!( + + {glyph} + + ), + width, + height, + )?; + let texture = GlyphTexture { + field: glyph_field::from_glyph(&image), + image, + }; + self.glyphs + .lock() + .unwrap_or_else(PoisonError::into_inner) + .insert(key, texture.clone()); + Some(texture) + } + + /// Render a material with its typed, cached caption metadata controls. + pub fn render_directed_with_controls<'a>( + &'a self, + scope: &CaptionWordScope<'a, '_>, + text: Svgr<'a>, + ctx: &FFramesContext<'_, '_>, + effect_index: usize, + direction: WordDirection, + controls: &MaterialControls, + ) -> Svgr<'a> { + let liquid = if controls.liquid.validate().is_ok() { + controls.liquid + } else { + LiquidTuning::default() + }; + self.render_impl( + scope, + text, + ctx, + effect_index, + Some(direction), + &MaterialControls { + relief: controls.relief.bounded(), + liquid, + ..*controls + }, + ) + } + + fn render_impl<'a>( + &'a self, + scope: &CaptionWordScope<'a, '_>, + text: Svgr<'a>, + ctx: &FFramesContext<'_, '_>, + effect_index: usize, + direction: Option, + controls: &MaterialControls, + ) -> Svgr<'a> { + let MaterialControls { relief, liquid, .. } = *controls; + let effect = effect_index % if direction.is_some() { 31 } else { 6 }; + let Some(start) = scope.word.start else { + return text; + }; + if scope.word.state == WordState::Upcoming { + return svgr!({text}); + } + let end = scope.word.end.unwrap_or(start); + let age = (scope.time - start).max(0.0); + let entrance = entrance_seconds((end - start).max(0.001)); + let exit_seconds = direction.map_or(0.24, |d| { + if d.exit_seconds.is_finite() { + d.exit_seconds.max(0.001) + } else { + 0.24 + } + }); + // Holds extend picture duration; they never shorten or stretch the source word. + let release = direction + .and_then(|d| d.hold_until) + .filter(|hold| hold.is_finite()) + .unwrap_or(end) + .max(end); + let tail = ((scope.time - release) / exit_seconds).clamp(0.0, 1.0); + let hero = direction.is_some_and(|d| d.hero); + let warm = direction.is_some_and(|d| d.warm); + if scope.word.state == WordState::Spoken && tail >= 1.0 { + return if (direction.is_none() && effect == 3) || hero { + Svgr::empty() + } else { + svgr!({text}) + }; + } + // Directed materials occupy the glyph plus its cached 32px distance band. + // Constant padding bounds shader work and lets different treatments reuse + // the same glyph texture. The legacy deformations retain proportional room. + let padding = if direction.is_some() && effect >= 16 { + // Visible fire wisps and released raster fragments need more room than + // the material library's near-field rims. The glyph cache includes size. + 72.0 + } else if direction.is_some() { + f64::from(glyph_field::DISTANCE_RANGE) + 4.0 + } else { + (scope.height() * 0.27).ceil().max(24.0) + }; + let width = (scope.width() + padding * 2.0).ceil().max(1.0) as u32; + let height = (scope.height() + padding * 2.0).ceil().max(1.0) as u32; + let shader = if direction.is_some() { + self.directed_shaders.get(effect) + } else { + self.shader.as_ref() + }; + if let Some(shader) = shader + && let Some(glyph) = self.glyph(scope, ctx, padding, width, height) + && (direction.is_none() || glyph.field.is_some()) + { + let seed = start as f32 * 17.13 + scope.word.token_index as f32 * 3.7; + // Legacy geometry gets its short entrance clock; directed lighting, fire and + // particles use uLightAge and continue moving through a visual hold. + let shader_age = if direction.is_some() { + age.min(entrance) + tail * exit_seconds + } else { + age + }; + let mut uniforms = ShaderUniforms::new() + .image("uGlyph", &glyph.image) + .image("uField", glyph.field.as_ref().unwrap_or(&glyph.image)) + .float("uDistanceRange", glyph_field::DISTANCE_RANGE) + .float2("uWordSize", scope.width() as f32, scope.height() as f32) + .float("uPadding", padding as f32) + .float("uEffect", effect as f32) + .float("uProgress", scope.word.progress) + .float("uAge", shader_age as f32) + .float("uLightAge", age as f32) + .float("uTail", tail as f32); + // Goo and glass nebula bind their validated material seed below. + if !(direction.is_some() && matches!(effect, 29 | 30)) { + uniforms = uniforms.float("uSeed", seed); + } + uniforms = uniforms + .float("uDirected", if direction.is_some() { 1.0 } else { 0.0 }) + .float("uEntrance", (age / entrance).clamp(0.0, 1.0) as f32) + .float("uHero", if hero { 1.0 } else { 0.0 }) + .float("uWarm", if warm { 1.0 } else { 0.0 }) + .float( + "uVariation", + direction.map_or(0.0, |d| { + if d.variation.is_finite() { + d.variation.clamp(0.0, 1.0) + } else { + 0.0 + } + }), + ); + if direction.is_some() && matches!(effect, 25 | 26) { + uniforms = uniforms + .float("uDepth", relief.depth) + .float("uDetailScale", relief.detail_scale) + .float("uSpeed", relief.speed) + .float("uLight", relief.light); + } + if direction.is_some() && effect == 27 { + uniforms = uniforms + .float("uTurbulence", liquid.turbulence) + .float("uRipple", liquid.ripple) + .float("uWarp", liquid.warp) + .float("uSpeed", liquid.speed) + .float("uSharpness", liquid.sharpness) + .float("uEnvironment", liquid.environment) + .float("uEnvRotation", liquid.env_rotation) + .float("uDispersion", liquid.dispersion) + .float("uLightAngle", liquid.light_angle) + .float("uBevelWidth", liquid.bevel_width) + .float("uBevelShape", liquid.bevel_shape) + .float3( + "uLightColor", + liquid.light_color.0[0], + liquid.light_color.0[1], + liquid.light_color.0[2], + ) + .float3( + "uDarkColor", + liquid.dark_color.0[0], + liquid.dark_color.0[1], + liquid.dark_color.0[2], + ); + } + if direction.is_some() { + uniforms = match effect { + 28 => { + let tuning = if controls.irradiance.validate().is_ok() { + controls.irradiance + } else { + IrradianceTuning::default() + }; + tuning.bind(uniforms) + } + 29 => { + let tuning = if controls.goo.validate().is_ok() { + controls.goo + } else { + GooTuning::default() + }; + tuning.bind(uniforms) + } + 30 => { + let tuning = if controls.glass_nebula.validate().is_ok() { + controls.glass_nebula + } else { + GlassNebulaTuning::default() + }; + tuning.bind(uniforms) + } + _ => uniforms, + }; + } + let image = shader.draw(scope.frame, uniforms); + return svgr!(); + } + self.svg_fallback(scope, text, effect, tail, direction) + } + + fn svg_fallback<'a>( + &'a self, + scope: &CaptionWordScope<'a, '_>, + text: Svgr<'a>, + effect: usize, + tail: f64, + direction: Option, + ) -> Svgr<'a> { + let p = f64::from(scope.word.progress); + let hero = direction.is_some_and(|d| d.hero); + let warm = direction.is_some_and(|d| d.warm); + let directed = direction.is_some(); + let enter = if directed { + let duration = scope.word.end.unwrap_or(0.0) - scope.word.start.unwrap_or(0.0); + let progress = (scope.elapsed().unwrap_or(0.0) / entrance_seconds(duration.max(0.001))) + .clamp(0.0, 1.0); + 1.0 - progress * progress * (3.0 - 2.0 * progress) + } else { + (1.0 - p / 0.22).clamp(0.0, 1.0) + }; + let opacity = if (direction.is_none() && effect == 3) || hero { + 1.0 - tail + } else { + 1.0 - tail * 0.72 + }; + let cx = scope.width() * 0.5; + let cy = scope.height() * 0.5; + let content = if hero || warm { + scope.mask_text(svgr!()) + } else { + text + }; + match effect % 6 { + 0 => { + let skew = (p * 12.0).sin() * (enter * 7.0 + if directed { 0.0 } else { 1.3 }); + svgr!({content}) + } + 1 => { + let dx = (p * 35.0).sin() * (enter * 15.0 + if directed { 0.0 } else { 1.5 }); + let echo_opacity = 0.25 * if directed { enter.max(tail) } else { 1.0 }; + svgr!( + {content.clone()} + {content} + ) + } + 2 => { + let sy = 1.0 - enter * if hero { 0.0 } else { 0.7 }; + svgr!({content}) + } + 3 => { + svgr!({content}) + } + 4 => { + svgr!({content}) + } + _ => { + let scale = 1.0 + enter * if hero { 0.08 } else { 0.16 }; + let echo_opacity = 0.22 * if directed { enter.max(tail) } else { 1.0 }; + svgr!( + {content.clone()} + {content} + ) + } + } + } +} diff --git a/examples/captions-showcase/src/word_plane.rs b/examples/captions-showcase/src/word_plane.rs new file mode 100644 index 00000000..ef817d40 --- /dev/null +++ b/examples/captions-showcase/src/word_plane.rs @@ -0,0 +1,641 @@ +//! A caption word as a constrained projective plane. +//! +//! The source is the actual caption drawing, captured once through `FFrames`. A +//! homography changes the plane, not the spelling, font metrics or speech clock. +use std::sync::{LazyLock, OnceLock}; + +use fframes::{ + FFramesContext, Shader, ShaderUniforms, Svgr, + captions::{CaptionError, CaptionWordScope}, + media::ImageData, + svgr, +}; +use serde::Deserialize; + +const SOURCE_PADDING: f64 = 32.0; +const OUTPUT_PADDING: f64 = 48.0; +const REST: [[f64; 2]; 4] = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]; + +static PLANE: LazyLock = + LazyLock::new(|| Shader::sksl(include_str!("shaders/word-plane.sksl"))); + +fn duration() -> f64 { + 0.45 +} +fn easing() -> [f64; 4] { + [0.22, 0.78, 0.24, 1.0] +} +fn wire_color() -> String { + "#b8dbea".into() +} +fn wire_width() -> f64 { + 1.5 +} +fn wire_opacity() -> f64 { + 0.6 +} +fn wire_fade() -> f64 { + 0.2 +} +fn full_light() -> f64 { + 1.0 +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PlaneMotion { + /// Absolute visual-clock seconds. Spoken word intervals are unaffected. + pub start: f64, + #[serde(default = "duration")] + pub duration: f64, + /// Offsets from TL, TR, BR, BL, as fractions of measured word width/height. + /// Example: [-0.08, 0.12] moves that corner left 8% and down 12%. + #[serde(default)] + pub corners: [[f64; 2]; 4], + /// Optional contiguous portions of one source word. Each portion has an + /// independent projective pose and delay, resolving to the same flat word. + /// Empty retains the original whole-word plane behavior. + #[serde(default)] + pub panels: Vec, + #[serde(default = "easing")] + pub easing: [f64; 4], + #[serde(default = "wire_color")] + pub wire_color: String, + #[serde(default = "wire_width")] + pub wire_width: f64, + #[serde(default = "wire_opacity")] + pub wire_opacity: f64, + #[serde(default)] + pub handles: bool, + #[serde(default = "wire_fade")] + pub wire_fade: f64, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PlanePanel { + /// Horizontal source interval in whole-word coordinates; panels partition + /// [0, 1] in order. The last panel also retains the right font overhang. + pub range: [f64; 2], + /// TL, TR, BR, BL offsets from this panel's resting rectangle, measured in + /// whole-word width/height units, not fractions of the narrower panel. + pub corners: [[f64; 2]; 4], + #[serde(default)] + pub delay: f64, + /// Defaults to the parent plane's duration; starts at parent start + delay. + pub duration: Option, + /// Authored initial face brightness, resolving to 1 with the panel. This + /// is a small lighting cue, not reconstructed physical lighting. + #[serde(default = "full_light")] + pub shade: f64, +} + +fn finite(value: f64, min: f64, max: f64, path: impl Into) -> Result<(), CaptionError> { + if value.is_finite() && (min..=max).contains(&value) { + Ok(()) + } else { + Err(CaptionError::invalid( + path, + format!("must be finite and between {min} and {max}"), + )) + } +} + +impl PlaneMotion { + pub fn validate(&self, cue_start: f64, cue_end: f64, path: &str) -> Result<(), CaptionError> { + finite(self.start, cue_start, cue_end, format!("{path}.start"))?; + finite(self.duration, 0.01, 3.0, format!("{path}.duration"))?; + if self.start + self.settle_after() > cue_end + 1e-9 { + return Err(CaptionError::invalid( + format!("{path}.duration"), + "the plane must settle before the cue ends", + )); + } + for (corner, values) in self.corners.iter().enumerate() { + for (axis, value) in values.iter().enumerate() { + finite( + *value, + -0.8, + 0.8, + format!("{path}.corners[{corner}][{axis}]"), + )?; + } + } + for (index, value) in self.easing.iter().enumerate() { + finite(*value, 0.0, 1.0, format!("{path}.easing[{index}]"))?; + } + finite(self.wire_width, 0.0, 8.0, format!("{path}.wire_width"))?; + finite(self.wire_opacity, 0.0, 1.0, format!("{path}.wire_opacity"))?; + finite(self.wire_fade, 0.01, 2.0, format!("{path}.wire_fade"))?; + if self.wire_color.trim().is_empty() { + return Err(CaptionError::invalid( + format!("{path}.wire_color"), + "must not be empty", + )); + } + // Each adjacent-edge cross product is quadratic in the interpolation + // parameter. Checking its interior minimum proves no intermediate fold, + // unlike testing only the initial and final quad or a few time samples. + if !unfolded_throughout(self.corners) { + return Err(CaptionError::invalid( + format!("{path}.corners"), + "the plane must remain strictly convex and correctly wound throughout its motion", + )); + } + if !self.panels.is_empty() { + if self.panels.len() > 12 || self.corners != [[0.0; 2]; 4] { + return Err(CaptionError::invalid( + format!("{path}.panels"), + "use at most 12 panels and omit the parent corners when panels are present", + )); + } + let mut edge = 0.0; + for (index, panel) in self.panels.iter().enumerate() { + let field = format!("{path}.panels[{index}]"); + for (axis, value) in panel.range.iter().enumerate() { + finite(*value, 0.0, 1.0, format!("{field}.range[{axis}]"))?; + } + if (panel.range[0] - edge).abs() > 1e-9 || panel.range[1] - panel.range[0] < 0.04 { + return Err(CaptionError::invalid( + format!("{field}.range"), + "panels must partition [0,1] in order, without gaps or overlaps, each at least 0.04 wide", + )); + } + edge = panel.range[1]; + finite(panel.delay, 0.0, 3.0, format!("{field}.delay"))?; + finite(panel.shade, 0.5, 1.0, format!("{field}.shade"))?; + finite( + panel.duration.unwrap_or(self.duration), + 0.01, + 3.0, + format!("{field}.duration"), + )?; + for (corner, values) in panel.corners.iter().enumerate() { + for (axis, value) in values.iter().enumerate() { + finite( + *value, + -0.8, + 0.8, + format!("{field}.corners[{corner}][{axis}]"), + )?; + } + } + if !unfolded_panel(panel.range, panel.corners) { + return Err(CaptionError::invalid( + format!("{field}.corners"), + "each panel must remain strictly convex and correctly wound throughout its motion", + )); + } + } + if (edge - 1.0).abs() > 1e-9 { + return Err(CaptionError::invalid( + format!("{path}.panels"), + "the final panel must end at 1", + )); + } + } + Ok(()) + } + + fn settle_after(&self) -> f64 { + if self.panels.is_empty() { + self.duration + } else { + self.panels + .iter() + .map(|panel| panel.delay + panel.duration.unwrap_or(self.duration)) + .fold(0.0, f64::max) + } + } + + pub fn shadow_clearance(&self, width: f64, height: f64) -> f64 { + (0..=32) + .flat_map(|index| { + self.poses(self.start + self.settle_after() * f64::from(index) / 32.0) + }) + .filter_map(|pose| { + panel_matrix(pose.points, pose.range) + .and_then(|matrix| panel_bounds(matrix, width, height, pose.range)) + }) + .map(|[left, top, right, bottom]| { + (-left).max(-top).max(right - width).max(bottom - height) + }) + .fold(OUTPUT_PADDING, f64::max) + + OUTPUT_PADDING + } + + fn progress(&self, time: f64) -> f64 { + if time <= self.start { + return 0.0; + } + if time >= self.start + self.duration { + return 1.0; + } + let position = ((time - self.start) / self.duration).clamp(0.0, 1.0); + cubic_ease(position, self.easing) + } + + fn quad(&self, time: f64) -> [[f64; 2]; 4] { + quad(self.corners, 1.0 - self.progress(time)) + } + + fn poses(&self, time: f64) -> Vec { + if self.panels.is_empty() { + return vec![PanelPose { + range: [0.0, 1.0], + points: self.quad(time), + shade: 1.0, + }]; + } + self.panels + .iter() + .enumerate() + .map(|(index, panel)| { + // Validation permits numerical tolerance; use one canonical + // boundary so adjacent source clips always have one owner. + let range = [ + if index == 0 { + 0.0 + } else { + self.panels[index - 1].range[1] + }, + if index + 1 == self.panels.len() { + 1.0 + } else { + panel.range[1] + }, + ]; + let position = ((time - self.start - panel.delay) + / panel.duration.unwrap_or(self.duration)) + .clamp(0.0, 1.0); + let remaining = 1.0 - cubic_ease(position, self.easing); + PanelPose { + range, + points: panel_quad(range, panel.corners, remaining), + shade: 1.0 - (1.0 - panel.shade) * remaining, + } + }) + .collect() + } +} + +struct PanelPose { + range: [f64; 2], + points: [[f64; 2]; 4], + shade: f64, +} + +fn cubic_coordinate(t: f64, first: f64, second: f64) -> f64 { + let remaining = 1.0 - t; + 3.0 * remaining * remaining * t * first + 3.0 * remaining * t * t * second + t * t * t +} + +fn cubic_ease(position: f64, [x1, y1, x2, y2]: [f64; 4]) -> f64 { + if position <= 0.0 || position >= 1.0 { + return position; + } + let (mut lower, mut upper) = (0.0, 1.0); + for _ in 0..24 { + let middle = f64::midpoint(lower, upper); + if cubic_coordinate(middle, x1, x2) < position { + lower = middle; + } else { + upper = middle; + } + } + cubic_coordinate(f64::midpoint(lower, upper), y1, y2) +} + +fn quad(offsets: [[f64; 2]; 4], remaining: f64) -> [[f64; 2]; 4] { + panel_quad([0.0, 1.0], offsets, remaining) +} + +fn panel_quad([left, right]: [f64; 2], offsets: [[f64; 2]; 4], remaining: f64) -> [[f64; 2]; 4] { + let rest = [[left, 0.0], [right, 0.0], [right, 1.0], [left, 1.0]]; + std::array::from_fn(|index| { + [ + rest[index][0] + offsets[index][0] * remaining, + rest[index][1] + offsets[index][1] * remaining, + ] + }) +} + +fn turn(points: [[f64; 2]; 4], corner: usize) -> f64 { + let a = points[corner]; + let b = points[(corner + 1) % 4]; + let c = points[(corner + 2) % 4]; + (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]) +} + +fn unfolded_throughout(offsets: [[f64; 2]; 4]) -> bool { + unfolded_panel([0.0, 1.0], offsets) +} + +fn unfolded_panel(range: [f64; 2], offsets: [[f64; 2]; 4]) -> bool { + (0..4).all(|corner| { + let start = turn(panel_quad(range, offsets, 0.0), corner); + let middle = turn(panel_quad(range, offsets, 0.5), corner); + let end = turn(panel_quad(range, offsets, 1.0), corner); + let quadratic = 2.0 * (end + start - 2.0 * middle); + let linear = end - start - quadratic; + let at = if quadratic > 0.0 { + (-linear / (2.0 * quadratic)).clamp(0.0, 1.0) + } else { + 0.0 + }; + let minimum = start + .min(end) + .min(quadratic * at * at + linear * at + start); + minimum.is_finite() && minimum > 0.015 * (range[1] - range[0]) + }) +} + +/// Convert local-panel coordinates into global source coordinates, so the +/// inverse shader always samples the same original word texture. +fn panel_matrix(points: [[f64; 2]; 4], [left, right]: [f64; 2]) -> Option<[f64; 9]> { + if points == panel_quad([left, right], [[0.0; 2]; 4], 0.0) { + return Some([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); + } + let local = square_to_quad(points)?; + let span = right - left; + if span <= 0.0 { + return None; + } + Some([ + local[0] / span, + local[1], + local[2] - left * local[0] / span, + local[3] / span, + local[4], + local[5] - left * local[3] / span, + local[6] / span, + local[7], + local[8] - left * local[6] / span, + ]) +} + +fn square_to_quad(points: [[f64; 2]; 4]) -> Option<[f64; 9]> { + let [[x0, y0], [x1, y1], [x2, y2], [x3, y3]] = points; + let (sx, sy) = (x0 - x1 + x2 - x3, y0 - y1 + y2 - y3); + let (g, h) = if sx.abs() + sy.abs() < 1e-12 { + (0.0, 0.0) + } else { + let (dx1, dx2, dy1, dy2) = (x1 - x2, x3 - x2, y1 - y2, y3 - y2); + let determinant = dx1 * dy2 - dx2 * dy1; + if determinant.abs() < 1e-12 { + return None; + } + ( + (sx * dy2 - dx2 * sy) / determinant, + (dx1 * sy - sx * dy1) / determinant, + ) + }; + Some([ + x1 - x0 + g * x1, + x3 - x0 + h * x3, + x0, + y1 - y0 + g * y1, + y3 - y0 + h * y3, + y0, + g, + h, + 1.0, + ]) +} + +fn inverse([a, b, c, d, e, f, g, h, i]: [f64; 9]) -> Option<[f64; 9]> { + let adjugate = [ + e * i - f * h, + c * h - b * i, + b * f - c * e, + f * g - d * i, + a * i - c * g, + c * d - a * f, + d * h - e * g, + b * g - a * h, + a * e - b * d, + ]; + let determinant = a * adjugate[0] + b * adjugate[3] + c * adjugate[6]; + if !determinant.is_finite() || determinant.abs() < 1e-12 { + return None; + } + Some(adjugate.map(|value| value / determinant)) +} + +fn panel_bounds( + matrix: [f64; 9], + width: f64, + height: f64, + [left, right]: [f64; 2], +) -> Option<[f64; 4]> { + let px = SOURCE_PADDING / width; + let py = SOURCE_PADDING / height; + let left = if left == 0.0 { -px } else { left }; + let right = if right == 1.0 { 1.0 + px } else { right }; + let padded = [ + [left, -py], + [right, -py], + [right, 1.0 + py], + [left, 1.0 + py], + ]; + let mut bounds = [ + f64::INFINITY, + f64::INFINITY, + f64::NEG_INFINITY, + f64::NEG_INFINITY, + ]; + for [x, y] in padded { + let denominator = matrix[6] * x + matrix[7] * y + matrix[8]; + // No projection horizon may cross the captured source region. A caller + // gets ordinary readable type if an extreme small-word pose violates it. + if !denominator.is_finite() || denominator < 0.015 { + return None; + } + let projected_x = (matrix[0] * x + matrix[1] * y + matrix[2]) / denominator * width; + let projected_y = (matrix[3] * x + matrix[4] * y + matrix[5]) / denominator * height; + bounds[0] = bounds[0].min(projected_x); + bounds[1] = bounds[1].min(projected_y); + bounds[2] = bounds[2].max(projected_x); + bounds[3] = bounds[3].max(projected_y); + } + // Integer origins keep the same output pixel grid as the plane moves. + bounds = [ + bounds[0].floor() - 8.0, + bounds[1].floor() - 8.0, + bounds[2].ceil() + 8.0, + bounds[3].ceil() + 8.0, + ]; + if bounds.iter().all(|value| value.is_finite()) + && bounds[2] - bounds[0] <= 8192.0 + && bounds[3] - bounds[1] <= 8192.0 + { + Some(bounds) + } else { + None + } +} + +struct SourceTexture { + image: ImageData<'static>, + width: u32, + height: u32, +} + +/// One immutable word drawing, shared by arbitrary frame order and render workers. +pub struct PlaneRenderer { + source: OnceLock, +} + +impl PlaneRenderer { + pub fn new() -> Self { + Self { + source: OnceLock::new(), + } + } + + pub fn render<'a>( + &'a self, + scope: &CaptionWordScope<'a, '_>, + text: &Svgr<'a>, + ctx: &FFramesContext<'_, '_>, + motion: &'a PlaneMotion, + time: f64, + enabled: bool, + ) -> Svgr<'a> { + if !enabled || scope.width() <= 0.0 || scope.height() <= 0.0 { + return text.clone(); + } + let width = scope.width(); + let height = scope.height(); + let poses = motion.poses(time); + // The settled word uses one identity sample of the same cached texture. + // This preserves outlines and avoids a subpixel filtering seam between + // adjacent regions. Wires keep their original geometry while fading. + let settled = [PanelPose { + range: [0.0, 1.0], + points: REST, + shade: 1.0, + }]; + let visible_poses = + if !motion.panels.is_empty() && time >= motion.start + motion.settle_after() { + settled.as_slice() + } else { + poses.as_slice() + }; + let prepared: Option> = visible_poses + .iter() + .map(|pose| { + let forward = panel_matrix(pose.points, pose.range)?; + Some(( + pose.range, + pose.shade, + inverse(forward)?, + panel_bounds(forward, width, height, pose.range)?, + )) + }) + .collect(); + let Some(prepared) = prepared else { + return text.clone(); + }; + if self.source.get().is_none() { + let texture_width = (width + 2.0 * SOURCE_PADDING).ceil() as u32; + let texture_height = (height + 2.0 * SOURCE_PADDING).ceil() as u32; + let Some(image) = ctx.render_svgr(svgr!( + + + {text.clone()} + + + ), texture_width, texture_height) else { return text.clone(); }; + // Concurrent first captures are identical. Cache only a successful + // capture, so missing fonts can retry without locking panel draws. + let _ = self.source.set(SourceTexture { + image, + width: texture_width, + height: texture_height, + }); + } + let Some(texture) = self.source.get() else { + return text.clone(); + }; + let images: Vec<_> = prepared.into_iter().map(|(range, shade, matrix, [origin_x, origin_y, end_x, end_y])| { + let image = PLANE.draw( + scope.frame, + ShaderUniforms::new() + .image("uSource", &texture.image) + .float2("uWordSize", width as f32, height as f32) + .float2("uTextureSize", texture.width as f32, texture.height as f32) + .float2("uOrigin", origin_x as f32, origin_y as f32) + .float("uPadding", SOURCE_PADDING as f32) + .float2("uSourceRange", range[0] as f32, range[1] as f32) + .float("uShade", shade as f32) + .float3( + "uInverse0", + matrix[0] as f32, + matrix[1] as f32, + matrix[2] as f32, + ) + .float3( + "uInverse1", + matrix[3] as f32, + matrix[4] as f32, + matrix[5] as f32, + ) + .float3( + "uInverse2", + matrix[6] as f32, + matrix[7] as f32, + matrix[8] as f32, + ), + ); + svgr!() + }).collect(); + let elapsed = time - motion.start; + let wire_opacity = motion.wire_opacity + * (1.0 - ((elapsed - motion.settle_after()) / motion.wire_fade).clamp(0.0, 1.0)); + let wires: Vec<_> = if wire_opacity > 0.0 && motion.wire_width > 0.0 { + poses.iter().map(|pose| { + let vertices = pose.points.map(|point| [point[0] * width, point[1] * height]); + let path = if motion.panels.is_empty() { format!( + "M{} {} L{} {} L{} {} L{} {} Z", + vertices[0][0], + vertices[0][1], + vertices[1][0], + vertices[1][1], + vertices[2][0], + vertices[2][1], + vertices[3][0], + vertices[3][1] + ) } else { + // Interior fold edges, not opaque panel cards or outer boxes. + let mut edges = String::new(); + if pose.range[0] > 0.0 { + let _ = std::fmt::Write::write_fmt(&mut edges, format_args!("M{} {} L{} {} ", vertices[0][0], vertices[0][1], vertices[3][0], vertices[3][1])); + } + if pose.range[1] < 1.0 { + let _ = std::fmt::Write::write_fmt(&mut edges, format_args!("M{} {} L{} {}", vertices[1][0], vertices[1][1], vertices[2][0], vertices[2][1])); + } + edges + }; + let handles: Vec<_> = if motion.handles { + vertices.into_iter().map(|[x,y]| svgr!( + + )).collect() + } else { + Vec::new() + }; + svgr!( + + {handles} + ) + }).collect() + } else { + Vec::new() + }; + svgr!( + {images} + {wires} + ) + } +} diff --git a/scripts/e2e.mjs b/scripts/e2e.mjs index 937410a0..b3abae7e 100644 --- a/scripts/e2e.mjs +++ b/scripts/e2e.mjs @@ -49,6 +49,7 @@ const allExamples = [ { dir: "audio-announce", pkg: "audio-announce-example", bin: "audio_announce_example_bin", frames: "5s,30s" }, { dir: "beta", pkg: "beta-example", bin: "beta_example_bin", frames: "CodeDemoScene@50%,IphoneScene@50%,BetaExamples@50%" }, // Preset layouts, typed highlights, wrapping and simultaneous cues. + { dir: "captions-showcase", pkg: "captions-showcase-example", bin: "captions-showcase-example", frames: "1.4s,2.3s,4.2s,6.3s,13.3s,19s", skia: true, args: ["--shader", "--captions-only"] }, { dir: "captions", pkg: "captions-example", bin: "captions-example", frames: "1.5s,5.2s,7s" }, { dir: "conference-splash-screen", pkg: "conference-splash-screen-example", bin: "conference_splash_screen_bin", frames: "SponsorScene@50%,SpeakerScene@50%" }, { dir: "fframes-intro", pkg: "fframes-intro", bin: "fframes-intro", frames: "CodeScene@50%,ShaderScene@2.6s,BenchmarkScene@12.4s,OutroScene@7s", skia: true },