Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
SG7: cameraPath, a camera path that can't lurch
shinyobjectz9d311f5fded3
diff --git a/docs/stage-guards.feature b/docs/stage-guards.featureindex 1d23f458..d02b0784 100644--- a/docs/stage-guards.feature+++ b/docs/stage-guards.feature@@ -155,13 +155,26 @@ Feature: Stage guards: a 3D render an agent can't breakThen a curve is one part, solid at every frame, and the 3D guide's curve recipe uses itAnd SG12 flags a chain of more than 8 parts named `NAME-i` whose ends touch as "a line made of parts: Line3d"+ @doneScenario: SG7. A camera path that can't lurchGiven the plan- | crate | file | lines | job |- | @raea/render | sdk/render/src/camera/path.ts | 160 | cameraPath(keys): keys timed in frames, a spline paced by time so speed carries smoothly across a key, `hold` keys where it stops, an ease only at the ends; pos, look and fov at any frame |- Then cam-diag.ts on a path built with it finds no speed change over 1.5x across a key and no framing reversal- within 2 s+ | crate | file | lines | job |+ | @raea/render | sdk/render/src/camera/path.ts | 150 | cameraPath(keys): keys timed in frames, a cubic per channel paced by time with one velocity a key (so speed carries across it), `hold` keys where it stops, an ease only at the ends, no channel past a key (monotone); pos, look and fov at any frame; camera(f), Camera3d's props; check(), the keys whose timing still brakes hard |+ | raea | sdk/raea/src/camera.ts | +2 | raea/camera exports it |+ | raea (test) | sdk/raea/conformance/camera-path.test.ts | 140 | speed across keys (the per-segment path jumps over 2x on the same keys), keys passed through, ends, holds, no overshoot, Camera3d's props, check(), refusals |+ | (guide) | sdk/guides/craft/bevy.md | +7 | rule 10, the camera recipe, uses it |+ Then the camera's speed is continuous across every key it moves through, and no channel passes a key and comes back+ And on keys that don't set a quick move against a slow one, cam-diag.ts finds no speed change over 1.5x across a+ key and no framing reversal within 2 s the keys don't make themselves+ And path.check() names each key that does, with the fix (more frames for the quick move, or a hold)And the 3D guide's camera recipe uses it, and hand-written paths are left to the check (SG13)+ # Status 11-21: the speed is continuous across every key, so a path never jumps; a channel never passes a key and+ # comes back. On the film's 155 keys (cam-diag's measure) framing reversals went from 24 to 5 and speed changes+ # of 2x within 3 frames from 51 to 29. What's left is in the keys: the 5 reversals are turns the keys make+ # within 2 s; 11 of the 29 are stops the keys ask for (a pose given twice); the other 18 sit where a quick move+ # meets a slow one (10 m in 30 frames against 0.7 m in 134), which any path through those keys at those frames+ # must brake for, and check() names exactly those 18 with the fix. One scene's keys weren't in time order: the+ # old path drew them anyway, and cameraPath refuses them.Scenario: SG8. Labels that readGiven the plandiff --git a/sdk/guides/craft/bevy.md b/sdk/guides/craft/bevy.mdindex b6712ac4..5e4d4703 100644--- a/sdk/guides/craft/bevy.md+++ b/sdk/guides/craft/bevy.md@@ -88,9 +88,12 @@ the rest come from `raea`.kept clear of the parts they name. Words in the world (a sign, a page in a book) are RichText: at least 22 px tall(caps) at 1080 lines, turned within 35° of the camera, inside the title-safe area, held still for their reading time(words ÷ 3 a second, plus half a second) and fully revealed before they go.-10. **A camera that settles.** Time a camera's keys by frames and pace the path by time, so its speed carries across- each key: a spline whose segments each run 0 to 1 whatever their length jumps speed at every key (11-21: 51 of 155- keys jumped 2x or more). Never push in and pull back, or pan one way then the other, within 1.5 s. A camera still- moving while words are up gives them their reading time first.+10. **A camera that settles.** Build a moving camera with `cameraPath` from `raea/camera`: keys timed by frames, the+ path paced by time so its speed carries across each key, `hold` where it stops, and `<Camera3d+ {...path.camera(frame)} />`. A hand-written spline whose segments each run 0 to 1 whatever their length jumps speed+ at every key (11-21: 51 of 155 keys jumped 2x or more). `path.check()` names a key whose timing still makes the+ camera brake hard (a quick move against a slow one): give the quick move more frames or end the slow one in a hold.+ Never push in and pull back, or pan one way then the other, within 1.5 s. A camera still moving while words are up+ gives them their reading time first.11. **Render and look.** Render stills where things happen (`raea render edit.tsx --at A,B,C -o stills/`) and look at them. A 3D frame that rendersblack usually has no light or a camera inside or behind the subject.diff --git a/sdk/raea/conformance/camera-path.test.ts b/sdk/raea/conformance/camera-path.test.tsnew file mode 100644index 00000000..cf5650da--- /dev/null+++ b/sdk/raea/conformance/camera-path.test.ts@@ -0,0 +1,140 @@+// A camera path that can't lurch (docs/stage-guards.feature SG7): the speed carries across every key, the camera+// stops only at the ends and at a hold, and no channel overshoots a key and comes back. The measures are the film's+// camera diagnosis (11-21): the speed 3 frames either side of a key, and a framing that turns back within 2 s.+import { describe, expect, test } from "bun:test";+import { cameraPath, type CameraKey, type CameraVec3 } from "../src/camera";++type Pose = { pos: CameraVec3; look: CameraVec3; fov: number };++// uneven keys, as a film's are: a quick move, a long slow one, a short one, a long one with a narrowing lens+const KEYS: CameraKey[] = [+ { at: 0, pos: [0, 2, 12], look: [0, 1, 0] },+ { at: 20, pos: [1, 2, 9], look: [0.2, 1, 0] },+ { at: 110, pos: [2, 1.8, 5], look: [0.5, 1, 0] },+ { at: 125, pos: [2.2, 1.7, 4.2], look: [0.6, 1, 0] },+ { at: 200, pos: [2.6, 1.5, 3], look: [0.8, 1.2, 0], fov: 30 },+];++// the hand-written path the film used: each segment's Catmull-Rom runs 0 to 1 over its own frames+function perSegment(keys: CameraKey[]) {+ const cr = (p0: number, p1: number, p2: number, p3: number, t: number) =>+ 0.5 * (2 * p1 + (-p0 + p2) * t + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t + (-p0 + 3 * p1 - 3 * p2 + p3) * t * t * t);+ return (f: number): Pose => {+ if (f <= keys[0].at) return { pos: keys[0].pos, look: keys[0].look, fov: 38 };+ const L = keys[keys.length - 1];+ if (f >= L.at) return { pos: L.pos, look: L.look, fov: L.fov ?? 38 };+ let i = 0;+ while (i < keys.length - 2 && f >= keys[i + 1].at) i++;+ const a = keys[Math.max(0, i - 1)], b = keys[i], c = keys[i + 1], d = keys[Math.min(keys.length - 1, i + 2)];+ const t = (f - b.at) / (c.at - b.at);+ const v = (j: number, w: "pos" | "look") => cr(a[w][j], b[w][j], c[w][j], d[w][j], t);+ return { pos: [v(0, "pos"), v(1, "pos"), v(2, "pos")], look: [v(0, "look"), v(1, "look"), v(2, "look")], fov: 38 };+ };+}++const speed = (at: (f: number) => Pose, f: number) => {+ const a = at(f).pos, b = at(f + 1).pos;+ return Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);+};+// the speed at an instant (units a frame), over a thousandth of a frame+const rate = (at: (f: number) => Pose, f: number) => {+ const a = at(f).pos, b = at(f + 1e-3).pos;+ return Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]) / 1e-3;+};+// the worst ratio of the speed 3 frames before an inner key to 3 frames after it+const worstJump = (at: (f: number) => Pose, keys: CameraKey[]) =>+ Math.max(...keys.slice(1, -1).map((k) => {+ const a = speed(at, k.at - 3), b = speed(at, k.at + 3);+ return Math.max(a, b) / Math.max(1e-6, Math.min(a, b));+ }));+// framing turns: a series that moves one way, then the other, within 60 frames+const turns = (v: number[], min: number) => {+ let last = 0, lastF = -999, n = 0;+ for (let f = 1; f < v.length; f++) {+ const dv = v[f] - v[f - 1];+ if (Math.abs(dv) < min) continue;+ const s = Math.sign(dv);+ if (last && s !== last && f - lastF < 60) n++;+ if (s !== last) { last = s; lastF = f; }+ }+ return n;+};++describe("cameraPath", () => {+ test("the speed carries across every key (the per-segment path jumps 2x or more on the same keys)", () => {+ const path = cameraPath(KEYS);+ expect(worstJump(perSegment(KEYS), KEYS)).toBeGreaterThan(2);+ expect(worstJump(path.at, KEYS)).toBeLessThan(1.5);+ });++ test("it passes through every key, and holds the first and last pose outside them", () => {+ const path = cameraPath(KEYS);+ for (const k of KEYS) {+ const p = path.at(k.at);+ for (let j = 0; j < 3; j++) {+ expect(p.pos[j]).toBeCloseTo(k.pos[j], 9);+ expect(p.look[j]).toBeCloseTo(k.look[j], 9);+ }+ }+ expect(path.at(-50)).toEqual({ pos: KEYS[0].pos, look: KEYS[0].look, fov: 38 });+ expect(path.at(900).fov).toBe(30);+ expect([path.start, path.end]).toEqual([0, 200]);+ });++ test("it eases only at the ends: still at the first and last key, moving at every key between", () => {+ const path = cameraPath(KEYS);+ expect(rate(path.at, 0)).toBeLessThan(0.01 * rate(path.at, 10));+ expect(rate(path.at, 200 - 1e-3)).toBeLessThan(0.01 * rate(path.at, 160));+ for (const k of KEYS.slice(1, -1)) expect(rate(path.at, k.at)).toBeGreaterThan(0.005);+ });++ test("a hold stops the camera at its key, and it leaves from rest", () => {+ const keys: CameraKey[] = [KEYS[0], { ...KEYS[1], hold: 30 }, { ...KEYS[2], at: 140 }];+ const path = cameraPath(keys);+ for (const f of [20, 25, 35, 49.5, 50]) expect(path.at(f).pos).toEqual(keys[1].pos);+ expect(rate(path.at, 50)).toBeLessThan(0.01 * rate(path.at, 95));+ expect(path.end).toBe(140);+ });++ test("no channel overshoots a key: keys that move one way give a framing that never turns back", () => {+ const path = cameraPath(KEYS);+ const zoom: number[] = [], pan: number[] = [];+ for (let f = 0; f <= 200; f++) {+ const c = path.at(f);+ const d = Math.hypot(c.pos[0] - c.look[0], c.pos[1] - c.look[1], c.pos[2] - c.look[2]);+ zoom.push(1 / (d * Math.tan((c.fov * Math.PI) / 360)));+ pan.push(c.look[0]);+ }+ expect(turns(zoom, 0.0004)).toBe(0);+ expect(turns(pan, 0.0001)).toBe(0);+ // a key that turns back in x (1 -> 3 -> 2.9) is the furthest the camera goes: it never swings past it+ const back = cameraPath([{ at: 0, pos: [1, 0, 5], look: [0, 0, 0] }, { at: 10, pos: [3, 0, 5], look: [0, 0, 0] }, { at: 100, pos: [2.9, 0, 5], look: [0, 0, 0] }]);+ for (let f = 0; f <= 100; f += 0.5) expect(back.at(f).pos[0]).toBeLessThanOrEqual(3 + 1e-12);+ });++ test("Camera3d's props: the transform at the camera, the field of view in radians", () => {+ const path = cameraPath(KEYS);+ const { transform, projection } = path.camera(200);+ expect(JSON.stringify(transform)).toContain("2.6");+ expect("Perspective" in projection && projection.Perspective.fov).toBeCloseTo((30 * Math.PI) / 180, 12);+ });++ test("check() names a key whose timing makes the camera brake hard, with the fix, and nothing on smooth keys", () => {+ expect(cameraPath(KEYS).check()).toEqual([]);+ // the film's pattern (11-21): 10 m in 30 frames, then 0.7 m over 134+ const hard = cameraPath([{ at: 0, pos: [0, 2, 20], look: [0, 1, 0] }, { at: 60, pos: [0, 2, 18], look: [0, 1, 0] }, { at: 90, pos: [0, 2, 8], look: [0, 1, 0] }, { at: 224, pos: [0, 2, 7.3], look: [0, 1, 0] }]);+ const said = hard.check();+ // it speeds up hard into the quick move (key 1) and brakes hard out of it (key 2)+ expect(said.map((l) => l.match(/key \d+ \(frame \d+\)/)?.[0])).toEqual(["key 1 (frame 60)", "key 2 (frame 90)"]);+ expect(said[1]).toContain("the move before covers 10 in 30 frames and the one after 0.70 in 134. Give the faster move more frames, or end the slower one in a hold.");+ // a hold is a stop the keys ask for, never a lurch+ expect(cameraPath([KEYS[0], { ...KEYS[1], hold: 20 }, { ...KEYS[2], at: 130 }]).check()).toEqual([]);+ });++ test("keys out of order, or inside the one before's hold, are refused with the fix", () => {+ expect(() => cameraPath([])).toThrow("no keys");+ expect(() => cameraPath([KEYS[1], KEYS[0]])).toThrow("key 1 is at frame 0, not after the key before (at 20)");+ expect(() => cameraPath([{ ...KEYS[0], hold: 30 }, KEYS[1]])).toThrow("key 1 is at frame 20, not after the key before (held until 30)");+ expect(() => cameraPath([{ ...KEYS[0], pos: [0, NaN, 1] }])).toThrow("isn't a number");+ });+});diff --git a/sdk/raea/src/camera.ts b/sdk/raea/src/camera.tsindex 6c287dc1..b7008498 100644--- a/sdk/raea/src/camera.ts+++ b/sdk/raea/src/camera.ts@@ -6,3 +6,5 @@ export { Matchmove, useMatchmove, matchmoveView, matchmoveIndex, checkMatchmove,export { Camera3d, Hdr, type Camera3dProps, type CameraViewport, type ClearColorConfig, type Projection } from "@raea/render/bevy3d";// a Bevy texture (a camera's target) drawn in an element (B3 H7)export { TextureView } from "@raea/render/bevy3d";+// a camera path paced by time, so it can't lurch at a key (docs/stage-guards.feature SG7)+export { cameraPath, type CameraKey, type CameraPath, type CameraPose, type CameraVec3 } from "@raea/render/camera/path";diff --git a/sdk/render/package.json b/sdk/render/package.jsonindex 67ce5f42..ddabb5c8 100644--- a/sdk/render/package.json+++ b/sdk/render/package.json@@ -21,7 +21,8 @@"./plugin": "./src/plugin.ts","./screen": "./src/screen.tsx","./bevy-own": "./src/bevy-own.gen.tsx",- "./feathers": "./src/feathers.ts"+ "./feathers": "./src/feathers.ts",+ "./camera/path": "./src/camera/path.ts"},"raea": {"plugins": {diff --git a/sdk/render/src/camera/path.ts b/sdk/render/src/camera/path.tsnew file mode 100644index 00000000..fd1ad08b--- /dev/null+++ b/sdk/render/src/camera/path.ts@@ -0,0 +1,147 @@+// A camera path that can't lurch (docs/stage-guards.feature SG7). The usual hand-written path is a Catmull-Rom curve+// whose every segment runs 0 to 1 over its own frames, so a short segment after a long one (or the reverse) jumps the+// camera's speed at the key between them: on 11-21, 51 of a film's 155 keys jumped 2x or more. Here every channel+// (position, look point, field of view) is one curve paced by time: each key has one velocity, shared by the segments+// on both sides, so the speed carries across it. The camera eases only at the first key, the last and a `hold`, and a+// channel never overshoots a key and comes back (a push in that pulls back, a pan that swings past and returns).+import { Transform, type Projection, type TransformBuilt } from "../bevy3d/core";++export type CameraVec3 = [number, number, number];++/** A key of a camera path: where the camera is at a frame, and what it looks at. */+export type CameraKey = {+ /** The frame the camera reaches this key, in the frames of the Sequence it plays in (useCurrentFrame). */+ at: number;+ /** Where the camera is. */+ pos: CameraVec3;+ /** The point it looks at. */+ look: CameraVec3;+ /** Its vertical field of view in degrees. Left out, the key before's (38 at the first). */+ fov?: number;+ /** Frames the camera holds still here: it arrives at `at` and leaves at `at + hold`. */+ hold?: number;+};++/** Where the camera is at a frame, what it looks at and its vertical field of view in degrees. */+export type CameraPose = { pos: CameraVec3; look: CameraVec3; fov: number };++/** A camera path built by cameraPath. */+export type CameraPath = {+ /** The camera at a frame (a fraction is fine): the first key's before it, the last key's after it. */+ at(frame: number): CameraPose;+ /** Camera3d's props at a frame: its transform, looking at the look point, and its projection (fov in radians, as+ * Bevy's is; the keys' are degrees). `<Camera3d {...path.camera(frame)} />` */+ camera(frame: number): { transform: TransformBuilt; projection: Projection };+ /** Keys whose timing makes the camera brake or speed up hard: its speed changes 2x or more within 3 frames of a key+ * it moves through (the film diagnosis's measure, and the render's camera check, SG13), each with the fix. The+ * path through such keys still never jumps or overshoots; the keys ask a fast move to meet a slow one. Empty when+ * every key reads smoothly. */+ check(): string[];+ /** The frame the path starts moving from: the first key's `at`. */+ readonly start: number;+ /** The frame it comes to rest at: the last key's `at` plus its `hold`. */+ readonly end: number;+};++const DEFAULT_FOV = 38;++/** a knot: a time, its seven channels (position, look point, fov) and whether the camera is still there */+type Knot = { t: number; v: number[]; still: boolean };++/**+ * A camera path through keys timed in frames, paced by time so the camera's speed carries smoothly across every key.+ * It eases at the first key and the last, stops at a key with `hold`, and never overshoots a key and comes back.+ *+ * ```tsx+ * const path = cameraPath([+ * { at: 0, pos: [0, 2, 9], look: [0, 1, 0] },+ * { at: 60, pos: [3, 2, 6], look: [0, 1, 0], hold: 30 },+ * { at: 150, pos: [1, 1.5, 3], look: [0, 1.2, 0], fov: 30 },+ * ]);+ * <Camera3d {...path.camera(useCurrentFrame())} />+ * ```+ */+export function cameraPath(keys: readonly CameraKey[]): CameraPath {+ if (!keys.length) throw new Error("cameraPath: no keys. Give at least one { at, pos, look }.");+ const knots: Knot[] = [];+ let fov = keys[0].fov ?? DEFAULT_FOV;+ const keyKnot: number[] = [];+ keys.forEach((k, i) => {+ if (!Number.isFinite(k.at)) throw new Error(`cameraPath: key ${i}'s at is ${k.at}. Give the frame it's reached.`);+ const hold = k.hold ?? 0;+ if (!(hold >= 0)) throw new Error(`cameraPath: key ${i}'s hold is ${k.hold}. Give the frames it holds still, 0 or more.`);+ const last = knots[knots.length - 1];+ if (last && !(k.at > last.t))+ throw new Error(`cameraPath: key ${i} is at frame ${k.at}, not after the key before (${last.t === keys[i - 1].at ? "at" : "held until"} ${last.t}). Give keys in time order, each after the one before and its hold.`);+ fov = k.fov ?? fov;+ const v = [...k.pos, ...k.look, fov];+ if (v.some((x) => !Number.isFinite(x))) throw new Error(`cameraPath: key ${i} (frame ${k.at}) has a pos, look or fov that isn't a number.`);+ keyKnot.push(knots.length);+ knots.push({ t: k.at, v, still: hold > 0 });+ if (hold > 0) knots.push({ t: k.at + hold, v, still: true });+ });+ knots[0].still = true;+ knots[knots.length - 1].still = true;+ const vel = knots.map((k, i) => (k.still ? k.v.map(() => 0) : k.v.map((_, c) => velocity(knots, i, c))));+ return {+ start: knots[0].t,+ end: knots[knots.length - 1].t,+ at,+ check() {+ const speed = (f: number) => dist(at(f).pos, at(f + 1).pos);+ let peak = 0;+ for (let f = knots[0].t; f < knots[knots.length - 1].t; f++) peak = Math.max(peak, speed(f));+ const said: string[] = [];+ keys.forEach((k, i) => {+ const j = keyKnot[i];+ if (i === 0 || i === keys.length - 1 || knots[j].still || vel[j].slice(0, 3).every((x) => x === 0)) return;+ const a = speed(k.at - 3), b = speed(k.at + 3), r = Math.max(a, b) / Math.max(1e-9, Math.min(a, b));+ if (r < 2 || Math.max(a, b) <= 0.01 * peak) return;+ const p = keys[i - 1], n = keys[i + 1], h0 = k.at - (p.at + (p.hold ?? 0)), h1 = n.at - k.at;+ const d0 = dist(p.pos, k.pos), d1 = dist(k.pos, n.pos), fmt = (x: number) => x.toPrecision(2);+ said.push(`cameraPath: key ${i} (frame ${k.at}): the camera's speed goes ${fmt(a)} -> ${fmt(b)} a frame across it (${r.toFixed(1)}x within 3 frames): the move before covers ${fmt(d0)} in ${h0} frames and the one after ${fmt(d1)} in ${h1}. Give the faster move more frames, or end the slower one in a hold.`);+ });+ return said;+ },+ camera(frame: number) {+ const { pos, look, fov } = at(frame);+ return { transform: Transform.fromXyz(...pos).lookingAt(look), projection: { Perspective: { fov: (fov * Math.PI) / 180 } } };+ },+ };+ function at(frame: number): CameraPose {+ const v = sample(knots, vel, frame);+ return { pos: [v[0], v[1], v[2]], look: [v[3], v[4], v[5]], fov: v[6] };+ }+}++const dist = (a: CameraVec3, b: CameraVec3) => Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);++// The velocity of channel c at an inner knot, in units a frame: the three-point estimate over the times on both sides+// (exact for a curve that is a parabola in time), then limited so the channel doesn't overshoot (Fritsch and Carlson):+// zero where the keys turn back in this channel, and at most three times the slope of either side.+function velocity(knots: Knot[], i: number, c: number): number {+ const a = knots[i - 1], b = knots[i], n = knots[i + 1];+ const h0 = b.t - a.t, h1 = n.t - b.t;+ const d0 = (b.v[c] - a.v[c]) / h0, d1 = (n.v[c] - b.v[c]) / h1;+ if (d0 * d1 <= 0) return 0;+ const m = (h1 * d0 + h0 * d1) / (h0 + h1);+ const cap = 3 * Math.min(Math.abs(d0), Math.abs(d1));+ return Math.sign(m) * Math.min(Math.abs(m), cap);+}++// Channel values at a frame: cubic Hermite between the two knots around it, each with its own velocity.+function sample(knots: Knot[], vel: number[][], f: number): number[] {+ if (f <= knots[0].t) return knots[0].v.slice();+ const last = knots.length - 1;+ if (f >= knots[last].t) return knots[last].v.slice();+ let lo = 0, hi = last;+ while (hi - lo > 1) {+ const mid = (lo + hi) >> 1;+ if (knots[mid].t <= f) lo = mid;+ else hi = mid;+ }+ const a = knots[lo], b = knots[hi], h = b.t - a.t, s = (f - a.t) / h;+ const s2 = s * s, s3 = s2 * s;+ const h00 = 2 * s3 - 3 * s2 + 1, h10 = s3 - 2 * s2 + s, h01 = -2 * s3 + 3 * s2, h11 = s3 - s2;+ return a.v.map((p0, c) => h00 * p0 + h10 * h * vel[lo][c] + h01 * b.v[c] + h11 * h * vel[hi][c]);+}