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
SG5: one clock for every 3D part that takes a frame of its Sequence
shinyobjectz6e1d7ceee048
diff --git a/docs/stage-guards.feature b/docs/stage-guards.featureindex d02b0784..6ae0e854 100644--- a/docs/stage-guards.feature+++ b/docs/stage-guards.feature@@ -128,25 +128,43 @@ Feature: Stage guards: a 3D render an agent can't break# Status 11-21: on a build without the fix the cold probe cuts a Space Mono H (179 pixels off against warm); with# it, 0 pixels off (SSIM 0.99996). The upstream issue is drafted, not filed: filing it is a public post.+ @doneScenario: SG4. A part that won't despawn fails the render# Status 11-21: a live scene now drops the rows under another first (a block's glyphs, a part's components), in a# respawn and when rows are forgotten, so a block no longer takes its glyphs' entities before their rows go.+ Given the plan+ | crate | file | lines | job |+ | raea-sys-frame | systems/frame/src/store.rs | +25 | despawn: try_despawn; a row whose entity had already gone leaves the store and is named (its kind and id) in the error; the store test |+ | raea-render | engine/render/src/engine/spawn.rs, engine.rs | +8 | remove_rows_from and Engine::remove_rows return it |+ | raea-runtime-export | runtime/export/src/fed.rs, bevy_frame.rs | +4 | a render fails on it, with the frame |+ | raea-runtime-react, raea-web | scene/plugin.rs, lib.rs | +2 | a plain App and the web player pass it on |Then "Could not despawn entity" is an error naming the part and the frame, never a log line- And a 13,000-frame 3D render's memory stays within 10% of its first minute's+ # Status 11-21 (branch stage-guards-4): built ("frame N: Could not despawn entity: the text part 'quote' had+ # already gone, despawned with another part it hung from"); the store test covers it.++ Scenario: SG4a. A long render's memory stays flat+ Then a 13,000-frame 3D render's memory stays within 10% of its first minute's+ # Status 11-21: not measured yet (SG4's leak fixed; the bar is the proof).Rule: The SDK stops allowing it+ @doneScenario: SG5. One clockGiven the plan| crate | file | lines | job || @raea/render | sdk/render/src/3d/text.tsx | +12 | RichText refuses an `at` or `exitAt` outside its Sequence's frames |- | @raea/render | sdk/render/src/3d/clock.ts | 40 | the same check for every part that takes a frame (Video's startFrom, Animate3d keys) |+ | @raea/render | sdk/render/src/3d/clock.ts | 45 | the same check for every part that takes a frame of its Sequence (Animate3d's first key, BevyComponent's from) |Then a reveal set after the composition ends is refused at its first frame: "RichText 'quote': at 1013 is afterthis composition's last frame (149). Give `at` in this Sequence's frames (useCurrentFrame), or put the scenein a <Sequence from>."And the 3D guide says a scene is a <Sequence>, never a frame passed down as a prop# Status 11-21 (branch stage-guards): RichText refuses an `at` or `exitAt` at or past its Sequence's last frame,- # naming the prop and the fix. clock.ts (the same for the other parts that take a frame) is left.+ # naming the prop and the fix. Branch stage-guards-4: clock.ts holds the check, and RichText, a mesh's and a+ # material's `animate` (its first key: an animation the Sequence's end cuts short is fine, one that never starts+ # isn't), a lens effect's and MotionBlur's `animate`, and BevyComponent's `from` use it+ # (sdk/raea/conformance/one-clock.test.tsx). Left out, on purpose: Video's startFrom (a frame of the source+ # clip, which can run past the Sequence) and a VolumeSource's from/until (seconds). Screen's `from` isn't+ # checked yet: screenContent isn't a component, so it can't read the Sequence.Scenario: SG6. Lines are linesGiven the plandiff --git a/sdk/raea/conformance/one-clock.test.tsx b/sdk/raea/conformance/one-clock.test.tsxnew file mode 100644index 00000000..367cabba--- /dev/null+++ b/sdk/raea/conformance/one-clock.test.tsx@@ -0,0 +1,43 @@+// One clock (docs/stage-guards.feature SG5): a 3D part's frame props are frames of the Sequence it plays in. One at or+// past that Sequence's last frame is refused at the part's first frame, naming the prop and the fix: the stage film's+// store quote was given `at` 1013 in a 150-frame scene, and its reveal never started.+import { describe, expect, test } from "bun:test";+import { Cuboid, RichText } from "../src/3d";+import { Mesh3d, StandardMaterial } from "@raea/render/bevy3d";+import { BevyComponent } from "@raea/render/3d";+import { checkSequenceFrames, firstKey } from "@raea/render/3d/clock";+import { renderFrame } from "../src/test";++const html = (el: React.ReactElement) => renderFrame(el, { frame: 0, width: 320, height: 240, fps: 30, durationInFrames: 150 });+const FIX = "Give at in this Sequence's frames (useCurrentFrame), never a frame passed down as a prop, or put the scene in a <Sequence from>.";++describe("SG5: one clock", () => {+ test("RichText refuses an `at` past its Sequence, and takes one inside it", () => {+ expect(() => html(<RichText name="quote" at={1013}>Five stars</RichText>)).toThrow(`RichText 'quote': at 1013 is after this Sequence's last frame (149). ${FIX}`);+ expect(() => html(<RichText at={149}>Five stars</RichText>)).not.toThrow();+ });++ test("a mesh's animation that starts after its Sequence ends is refused; one cut off by the end is fine", () => {+ const late = { opacity: { 400: 0, 420: 1 } };+ expect(() => html(<Mesh3d name="dot" animate={late}><Cuboid /><StandardMaterial /></Mesh3d>)).toThrow("Mesh3d 'dot': animate's first key 400 is after this Sequence's last frame (149).");+ expect(() => html(<Mesh3d animate={{ opacity: { 100: 0, 300: 1 } }}><Cuboid /><StandardMaterial /></Mesh3d>)).not.toThrow();+ });++ test("a Bevy component added after its Sequence ends is refused", () => {+ expect(() => html(<Mesh3d><Cuboid /><BevyComponent type="avian3d::dynamics::rigid_body::RigidBody" value="Dynamic" from={200} /></Mesh3d>)).toThrow("BevyComponent: from 200 is after this Sequence's last frame (149).");+ });++ test("the check itself: each prop named, nothing left out checked, an endless Sequence never refused", () => {+ expect(() => checkSequenceFrames("Part", undefined, [["from", 3], ["until", 30]], 30)).toThrow("Part: until 30 is after this Sequence's last frame (29).");+ expect(() => checkSequenceFrames("Part", undefined, [["from", undefined]], 30)).not.toThrow();+ expect(() => checkSequenceFrames("Part", undefined, [["from", 1e6]], Infinity)).not.toThrow();+ });++ test("an animation's first key, from any of its shapes", () => {+ expect(firstKey({ opacity: { 12: 0, 30: 1 }, y: { 6: 10, 20: 0 } })).toBe(6);+ expect(firstKey({ x: { keys: [[40, 0], [60, 1]] } })).toBe(40);+ expect(firstKey({ x: [{ keys: [[9, 0], [12, 1]] }, { keys: [[3, 1], [5, 0]] }] })).toBe(3);+ expect(firstKey({ tracks: { scale: { keys: [[15, 1], [25, 2]] } }, frames: 25 })).toBe(15);+ expect(firstKey(undefined)).toBeUndefined();+ });+});diff --git a/sdk/render/package.json b/sdk/render/package.jsonindex ddabb5c8..5baa853b 100644--- a/sdk/render/package.json+++ b/sdk/render/package.json@@ -22,7 +22,8 @@"./screen": "./src/screen.tsx","./bevy-own": "./src/bevy-own.gen.tsx","./feathers": "./src/feathers.ts",- "./camera/path": "./src/camera/path.ts"+ "./camera/path": "./src/camera/path.ts",+ "./3d/clock": "./src/3d/clock.ts"},"raea": {"plugins": {diff --git a/sdk/render/src/3d/bevy.tsx b/sdk/render/src/3d/bevy.tsxindex ad741777..3659dffa 100644--- a/sdk/render/src/3d/bevy.tsx+++ b/sdk/render/src/3d/bevy.tsx@@ -1,5 +1,6 @@// Bevy's own components and resources by type path, and gizmos.import { Entity, Vec3, checkTextures, checkVec, colours, node3d, shaderDefs, type ShaderDefs } from "./all";+import { useSequenceFrames } from "./clock";export interface BevyComponentProps {/** the component's reflected type path, as Bevy's TypeRegistry names it ("avian3d::dynamics::rigid_body::RigidBody") */@@ -18,6 +19,7 @@ export interface BevyComponentProps {export function BevyComponent(props: BevyComponentProps) {if (typeof props.type !== "string" || !props.type.includes("::")) throw new TypeError(`BevyComponent: type is a full type path (crate::module::Type), got ${JSON.stringify(props.type)}`);if (props.from !== undefined && !(Number.isInteger(props.from) && props.from >= 0)) throw new RangeError(`BevyComponent: from is a frame (0 or more), got ${props.from}`);+ useSequenceFrames("BevyComponent", undefined, [["from", props.from]]);return node3d("BevyComponent", { type: props.type, value: props.value ?? null, ...(props.from ? { from: props.from } : {}) });}diff --git a/sdk/render/src/3d/clock.ts b/sdk/render/src/3d/clock.tsnew file mode 100644index 00000000..a35a961f--- /dev/null+++ b/sdk/render/src/3d/clock.ts@@ -0,0 +1,39 @@+// One clock (docs/stage-guards.feature SG5). A part's frame props are frames of the Sequence it plays in, as+// useCurrentFrame counts them. One set at or past that Sequence's last frame means the part never does what the prop+// says, and it is almost always a frame from another clock passed down as a prop: the stage film's store quote was+// given `at` 1013 in a 150-frame scene, so its reveal never started. Each part that takes such a frame refuses it here,+// at its first frame, naming the prop and the fix.+import { useVideoConfig } from "@raea/app/clock";++/** A frame prop: its name and its value (left out: nothing to check). */+export type FrameProp = readonly [prop: string, frame: number | undefined];++/** Refuses each frame prop at or past the last frame of a Sequence `durationInFrames` long. */+export function checkSequenceFrames(owner: string, name: string | undefined, frames: readonly FrameProp[], durationInFrames: number): void {+ if (!Number.isFinite(durationInFrames)) return;+ for (const [prop, v] of frames) {+ if (v === undefined || !(v >= durationInFrames)) continue;+ throw new RangeError(`${owner}${name ? ` '${name}'` : ""}: ${prop} ${v} is after this Sequence's last frame (${durationInFrames - 1}). Give ${prop} in this Sequence's frames (useCurrentFrame), never a frame passed down as a prop, or put the scene in a <Sequence from>.`);+ }+}++/** The same, for the Sequence the calling part is in. */+export function useSequenceFrames(owner: string, name: string | undefined, frames: readonly FrameProp[]): void {+ checkSequenceFrames(owner, name, frames, useVideoConfig().durationInFrames);+}++/** The frame an animation's first key falls on, from the animation's start: the frame it starts doing anything. A key+ * map ({ 0: 0, 6: 1 }), a track ({ keys: [[0, 0], [6, 1]] }) or a list of tracks, by property, or a move with them. */+export function firstKey(spec: unknown): number | undefined {+ if (!spec || typeof spec !== "object") return undefined;+ const tracks = (spec as { tracks?: unknown; frames?: unknown }).tracks !== undefined && (spec as { frames?: unknown }).frames !== undefined ? (spec as { tracks: object }).tracks : spec;+ let first: number | undefined;+ const see = (f: number) => { if (Number.isFinite(f) && (first === undefined || f < first)) first = f; };+ const track = (t: unknown) => {+ if (!t || typeof t !== "object") return;+ if (Array.isArray((t as { keys?: unknown }).keys)) for (const k of (t as { keys: unknown[] }).keys) Array.isArray(k) && see(Number(k[0]));+ else for (const k of Object.keys(t)) see(Number(k));+ };+ for (const t of Object.values(tracks)) Array.isArray(t) ? t.forEach(track) : track(t);+ return first;+}diff --git a/sdk/render/src/3d/materials.tsx b/sdk/render/src/3d/materials.tsxindex f9486637..987d65f6 100644--- a/sdk/render/src/3d/materials.tsx+++ b/sdk/render/src/3d/materials.tsx@@ -4,6 +4,7 @@ import { checkOverlays, type GridOverlay, type PaintOverlay, type ProceduralSurfimport { wornShader } from "../worn";import { useAnimation3d } from "@raea/math/animate";import { Animate3d, apply3d, node3d } from "./all";+import { firstKey, useSequenceFrames } from "./clock";/** How a surface's alpha and faces draw (Bevy's StandardMaterial alpha_mode, alpha cutoff, double_sided and cull_mode),* from the first frame. */@@ -332,6 +333,7 @@ export function checkMaterial(owner: string, props: Record<string, unknown>): vo* @deprecated <StandardMaterial>, raea/pbr (Bevy's fields: base_color, perceptual_roughness, metallic)*/export function MeshStandardMaterial(props: MeshStandardMaterialProps) {+ useSequenceFrames("MeshStandardMaterial", undefined, [["animate's first key", firstKey(props.animate)]]);const anim = useAnimation3d(props.animate);const p = apply3d({ ...props, animate: undefined } as Record<string, unknown>, anim.values);checkMaterial("MeshStandardMaterial", p);@@ -356,6 +358,7 @@ export interface MeshPhysicalMaterialProps extends MeshStandardMaterialProps {/** The standard surface plus a clearcoat layer and reflectance.* @deprecated <StandardMaterial>, raea/pbr (clearcoat and specular_transmission are its fields) */export function MeshPhysicalMaterial(props: MeshPhysicalMaterialProps) {+ useSequenceFrames("MeshPhysicalMaterial", undefined, [["animate's first key", firstKey(props.animate)]]);const anim = useAnimation3d(props.animate);const p = apply3d({ ...props, animate: undefined } as Record<string, unknown>, anim.values);checkMaterial("MeshPhysicalMaterial", p);@@ -449,6 +452,7 @@ export interface MeshToonMaterialProps {* @deprecated <ToonMaterial>, raea/pbr */export function MeshToonMaterial(props: MeshToonMaterialProps) {const owner = "MeshToonMaterial";+ useSequenceFrames("MeshToonMaterial", undefined, [["animate's first key", firstKey(props.animate)]]);const anim = useAnimation3d(props.animate);const p = apply3d({ ...props, animate: undefined } as Record<string, unknown>, anim.values);checkMaterial(owner, p);diff --git a/sdk/render/src/3d/post.tsx b/sdk/render/src/3d/post.tsxindex 7b295b0f..52493f96 100644--- a/sdk/render/src/3d/post.tsx+++ b/sdk/render/src/3d/post.tsx@@ -3,6 +3,7 @@ import { useCurrentFrame, useVideoConfig } from "@raea/app/clock";import { toHex } from "../colour";import { useAnimation3d } from "@raea/math/animate";import { Animate3d, Vec3, apply3d, checkShares, checkVec, node3d } from "./all";+import { firstKey, useSequenceFrames } from "./clock";export interface FireProps {/** parts are matched by name across frames: name a fire that comes and goes */@@ -169,6 +170,7 @@ export interface LensDistortionProps extends LensCommon {}function lensNode(owner: string, props: Record<string, unknown>) {+ useSequenceFrames(owner, undefined, [["animate's first key", firstKey(props.animate)]]);const anim = useAnimation3d(props.animate as Animate3d | undefined);const p = apply3d({ ...props, animate: undefined } as Record<string, unknown>, anim.values);const st = p.strength;@@ -211,6 +213,7 @@ export interface MotionBlurProps {* @bevy bevy_post_process::motion_blur::MotionBlur */export function MotionBlur(props: MotionBlurProps) {const owner = "MotionBlur";+ useSequenceFrames("MotionBlur", undefined, [["animate's first key", firstKey(props.animate)]]);const anim = useAnimation3d(props.animate);const p = apply3d({ ...props, animate: undefined } as Record<string, unknown>, anim.values);for (const [k, lo, hi] of [["shutterAngle", 0, 4], ["samples", 1, 32]] as const) {diff --git a/sdk/render/src/3d/shapes.tsx b/sdk/render/src/3d/shapes.tsxindex 8235ece8..1b9c6270 100644--- a/sdk/render/src/3d/shapes.tsx+++ b/sdk/render/src/3d/shapes.tsx@@ -6,6 +6,7 @@ import { Place3d, usePlace3d, usePointer3d, type LocalBounds, type PointerHandleimport { Canvas3DProps, Vec3, checkArgs, checkPass, checkVec, node3d, serialisable } from "./all";import { InCanvas3D } from "./model";import type { MeshAttribute } from "./attribute";+import { firstKey, useSequenceFrames } from "./clock";const FILL = { position: "absolute", left: 0, top: 0, width: "100%", height: "100%" } as const;@@ -477,6 +478,7 @@ function boundsFromChildren(children: ReactNode): LocalBounds | undefined {* @deprecated <Mesh3d>, raea/mesh (2D: <Mesh2d>, raea/sprite)*/export function Mesh(props: MeshProps) {+ useSequenceFrames("Mesh3d", props.name, [["animate's first key", firstKey(props.animate)]]);const anim = useAnimation3d(props.animate);const { position = [0, 0, 0], rotation = [0, 0, 0], scale = 1, shape } = apply3d(props, anim.values);checkVec("Mesh", "position", position);diff --git a/sdk/render/src/3d/text.tsx b/sdk/render/src/3d/text.tsxindex 4b661a79..d9ed42f2 100644--- a/sdk/render/src/3d/text.tsx+++ b/sdk/render/src/3d/text.tsx@@ -4,6 +4,7 @@ import { useCurrentFrame, useVideoConfig } from "@raea/app/clock";import { toHex } from "../colour";import { reverse, sampleMove, type MoveLike } from "@raea/math/animate";import { Vec3, checkVec, node3d } from "./all";+import { checkSequenceFrames } from "./clock";// ---- text in the 3D world (docs/ts-native.feature P19) ----@@ -183,12 +184,8 @@ export function RichText(props: RichTextProps) {if (!(size > 0) || !Number.isFinite(size)) throw new RangeError(`${owner}: size is the em in metres, a positive number, got ${size}`);if (!["left", "center", "right"].includes(align)) throw new RangeError(`${owner}: align is left, center or right, got ${JSON.stringify(align)}`);if (props.exit !== undefined && props.exitAt === undefined) throw new Error(`${owner}: an exit needs exitAt (the local frame it starts)`);- // one clock (docs/stage-guards.feature SG5): `at` and `exitAt` are frames of this Sequence, as useCurrentFrame counts- // them. A frame from another clock (a scene's own frame passed down as a prop) can fall past the Sequence's end, where- // the move never starts: 11-21, a quote whose reveal was set at its scene's frame 1013 never appeared in a 150-frame part- for (const [k, v] of [["at", props.at], ["exitAt", props.exitAt]] as const) {- if (v !== undefined && Number.isFinite(durationInFrames) && v >= durationInFrames) throw new RangeError(`${owner}${props.name ? ` '${props.name}'` : ""}: ${k} ${v} is after this Sequence's last frame (${durationInFrames - 1}). Give ${k} in this Sequence's frames (useCurrentFrame), never a frame passed down as a prop, or put the scene in a <Sequence from>.`);- }+ // one clock (docs/stage-guards.feature SG5): `at` and `exitAt` are frames of this Sequence+ checkSequenceFrames(owner, props.name, [["at", props.at], ["exitAt", props.exitAt]], durationInFrames);const runs = textRuns(owner, props);// the glyphs: every character that isn't a space, with its byte in the joined text (raea-render lays the text out whole)type G = { ch: string; at: number; span: number; color: string; line: number; word: number; inWord: number };