Skip to content

armgit / raea

Public Imported from GitHub
0

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.

An <R3F> scene's first frame waits for what it loads: a render holds the frame while the scene suspends

shinyobjectzc60db71abf25
diff --git a/runtime/bundle/src/entry.rs b/runtime/bundle/src/entry.rs
index 6bc3b122..5bce765f 100644
--- a/runtime/bundle/src/entry.rs
+++ b/runtime/bundle/src/entry.rs
@@ -56,6 +56,9 @@ const INPUT: any = (() => {{
}}
return out;
}})();
+// a frame held until what it waits on has come (an <R3F> scene suspended on a load: drei's Environment, a model): while
+// any hold is open the frame isn't marked done, and the last release draws it again (each wake), so it's taken whole
+const HOLD: any = ((globalThis as any).__raeaHold ??= {{ n: 0, wake: new Set() }});
// a live session's input replayed (render --input of what play or the legacy app logged, every entry marked live): rendered as it
// was played, on an endless clock, so frames past the composition's length are the frames the person saw
const REPLAYS_LIVE = Object.values(INPUT).some((e: any) => e && e.live);
@@ -124,9 +127,11 @@ function Root() {{
// (many_cubes' 10 000 parts, docs/control.feature FC1a)
function Marker({{ f, to, conf, next }}: {{ f: number; to: number; conf: string; next: (f: number) => void }}) {{
const [done, setDone] = useState(-1);
+ const [woke, setWoke] = useState(0);
+ useEffect(() => {{ const w = () => setWoke((n: number) => n + 1); HOLD.wake.add(w); return () => void HOLD.wake.delete(w); }}, []);
useEffect(() => {{
- if (f >= 0) setDone(f);
- }}, [f]);
+ if (f >= 0 && HOLD.n === 0) setDone(f);
+ }}, [f, woke]);
useEffect(() => {{
if (f >= 0 && done === f && f + 1 < to) next(f + 1);
}}, [done]);
@@ -180,8 +185,9 @@ function tree() {{
}}
// --comps (a bake): the marker rendered last, carrying the code of every component this frame rendered
function Marker(p: any) {{
- return <div data-raea-frame={{p.f}} data-raea-done={{p.f}} data-raea-config={{conf}} data-raea-comps={{takeRendered()}} />;
+ return <div data-raea-frame={{p.f}} data-raea-done={{HOLD.n ? -3 : p.f}} data-raea-config={{conf}} data-raea-comps={{takeRendered()}} />;
}}
+HOLD.wake.add(() => {{ if (f >= 0) draw(); }});
// a throw while rendering is put where Rust reads it (settle), as React's entry's Catch did
function draw() {{
try {{
@@ -216,6 +222,6 @@ draw();
runEventLoop();
"#,
preamble = &full[start..end],
- marker = if o.comps { "<Marker f={f} />" } else { "<div data-raea-frame={f} data-raea-done={f} data-raea-config={conf} />" },
+ marker = if o.comps { "<Marker f={f} />" } else { "<div data-raea-frame={f} data-raea-done={HOLD.n ? -3 : f} data-raea-config={conf} />" },
)
}
diff --git a/sdk/r3f/README.md b/sdk/r3f/README.md
index 37986636..775b1b8d 100644
--- a/sdk/r3f/README.md
+++ b/sdk/r3f/README.md
@@ -58,7 +58,9 @@ named in a warning and left out.
**drei.** A typical drei scene draws (`test/parity`'s `in-drei`, R3F13):
- `<Text>`: raea's bundler swaps troika-three-text for a stand-in, and raea lays the words out as its own `<RichText>`.
The face is found by its family name.
-- `<Environment preset>` and `useGLTF`: three's loaders read files and URLs through raea's cache.
+- `<Environment preset>` and `useGLTF`: three's loaders read files and URLs through raea's cache. While anything in
+ the scene suspends on a load, raea holds the frame and draws it once the load has come, so a film's first frame
+ is never missing its 3D (`test/hold`).
- `<ContactShadows>`: its depth and blur passes are done on the CPU.
- `<Float>`: `Math.random` is seeded for each `<R3F>`, so a render equals its play.
diff --git a/sdk/r3f/src/input/R3F.tsx b/sdk/r3f/src/input/R3F.tsx
index 2472bbef..eff6ddd5 100644
--- a/sdk/r3f/src/input/R3F.tsx
+++ b/sdk/r3f/src/input/R3F.tsx
@@ -9,6 +9,7 @@ import { AbsoluteFill, useCurrentFrame, useVideoConfig } from "raea";
import { ClockContext, LocalTimeContext } from "@raea/app/clock";
import type * as THREE from "three";
import { FiberOf, useBridge } from "./bridge";
+import { Held, useWake } from "./hold";
import { stepTo, type Config } from "./session";
import { convert } from "./scene";
import { standInImages } from "./texture";
@@ -28,6 +29,8 @@ function Inner({ fiber, children, name, ...config }: R3FProps & { fiber: Paramet
const clock = useContext(ClockContext);
const local = useContext(LocalTimeContext);
const bridge = useBridge(fiber);
+ // drawn again when a load it waited on comes (hold.tsx): the frame isn't taken before
+ useWake();
const id = useId();
// the scene at local frame i: raea's clock as it was then, inside every context from outside
const at = (i: number) => {
@@ -35,7 +38,9 @@ function Inner({ fiber, children, name, ...config }: R3FProps & { fiber: Paramet
const l = local ? { ...local, frame: i } : null;
return bridge(
<ClockContext value={c}>
- <LocalTimeContext value={l}>{children}</LocalTimeContext>
+ <LocalTimeContext value={l}>
+ <Held>{children}</Held>
+ </LocalTimeContext>
</ClockContext>,
);
};
diff --git a/sdk/r3f/src/input/hold.tsx b/sdk/r3f/src/input/hold.tsx
new file mode 100644
index 00000000..1ef82af3
--- /dev/null
+++ b/sdk/r3f/src/input/hold.tsx
@@ -0,0 +1,39 @@
+// A frame held while its R3F scene waits on a load (drei's <Environment>, useGLTF, useTexture: whatever suspends). The
+// scene renders under a Suspense whose fallback opens a hold on raea's frame (the entry's HOLD, runtime/bundle
+// entry.rs): the frame isn't marked done while one is open, so a render never takes a frame from before its loads came
+// (the first frame of a film was the 2D alone). The last release wakes the frame: <R3F> draws it again with what loaded.
+import { Suspense, useEffect, useLayoutEffect, useState, type ReactNode } from "react";
+
+interface Hold {
+ n: number;
+ wake: Set<() => void>;
+}
+
+const HOLD = ((globalThis as { __raeaHold?: Hold }).__raeaHold ??= { n: 0, wake: new Set() });
+
+function Waiting() {
+ useLayoutEffect(() => {
+ HOLD.n++;
+ return () => {
+ HOLD.n = Math.max(0, HOLD.n - 1);
+ if (HOLD.n === 0) for (const w of [...HOLD.wake]) w();
+ };
+ }, []);
+ return null;
+}
+
+/** `children` under a Suspense that holds raea's frame while any of it suspends */
+export function Held({ children }: { children?: ReactNode }) {
+ return <Suspense fallback={<Waiting />}>{children}</Suspense>;
+}
+
+/** a count that goes up each time raea's frame is woken: the component reading it draws the frame again */
+export function useWake(): number {
+ const [n, setN] = useState(0);
+ useEffect(() => {
+ const w = () => setN((x) => x + 1);
+ HOLD.wake.add(w);
+ return () => void HOLD.wake.delete(w);
+ }, []);
+ return n;
+}
diff --git a/sdk/r3f/test/hold/check.sh b/sdk/r3f/test/hold/check.sh
new file mode 100755
index 00000000..cf17d716
--- /dev/null
+++ b/sdk/r3f/test/hold/check.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+# An <R3F> scene that suspends on its first frame (first-frame.tsx): frame 0 of an mp4 and a --frame 0 still must be
+# the red box. Before the frame hold (sdk/r3f hold.tsx, runtime/bundle entry.rs HOLD) frame 0 was the background
+# alone, so a looping film opened on a blank frame. Exits 1 on a failure.
+# sdk/r3f/test/hold/check.sh (RAEA_BIN: the raea to run, default target/release/raea)
+cd "$(dirname "$0")/../../../.." || exit 2
+BIN=${RAEA_BIN:-$PWD/target/release/raea}
+export RAEA_SIGN=0 RAEA_MARK=0 RAEA_EXPORT_CACHE=0
+OUT=$(mktemp -d "${TMPDIR:-/tmp}/raea-r3f-hold.XXXXXX")
+trap 'rm -rf "$OUT"' EXIT
+T=sdk/r3f/test/hold/first-frame.tsx
+red() { magick "$1" -format "%[fx:int(255*p{80,80}.r)] %[fx:int(255*p{80,80}.g)]" info: | awk '{ exit !($1 > 200 && $2 < 40) }'; }
+"$BIN" render $T -o "$OUT/v.mp4" > "$OUT/v.log" 2>&1 || { echo "FAIL: the mp4 render failed"; tail -3 "$OUT/v.log"; exit 1; }
+ffmpeg -loglevel error -y -i "$OUT/v.mp4" -frames:v 1 "$OUT/m0.png" || { echo "FAIL: no frame 0 in the mp4"; exit 1; }
+red "$OUT/m0.png" || { echo "FAIL: the mp4's frame 0 is $(magick "$OUT/m0.png" -format '%[pixel:p{80,80}]' info:), not the red box"; exit 1; }
+"$BIN" render $T --frame 0 -o "$OUT/f0.png" > "$OUT/f0.log" 2>&1 || { echo "FAIL: the --frame 0 render failed"; tail -3 "$OUT/f0.log"; exit 1; }
+red "$OUT/f0.png" || { echo "FAIL: --frame 0 is $(magick "$OUT/f0.png" -format '%[pixel:p{80,80}]' info:), not the red box"; exit 1; }
+echo "PASS: a suspended <R3F> scene's frame 0 waits for its load (mp4 and still)"
diff --git a/sdk/r3f/test/hold/first-frame.tsx b/sdk/r3f/test/hold/first-frame.tsx
new file mode 100644
index 00000000..ad4f48dc
--- /dev/null
+++ b/sdk/r3f/test/hold/first-frame.tsx
@@ -0,0 +1,29 @@
+// An <R3F> scene that suspends on its first frame, as drei's <Environment> does while its HDR loads: the box's colour
+// comes from a promise that resolves 50 ms later. A render must wait for it (hold.tsx): frame 0 is the red box, not
+// the 2D background alone.
+import { use } from "react";
+import { AbsoluteFill, Composition, registerRoot } from "raea";
+import { R3F } from "raea/r3f";
+
+const COLOUR = new Promise<string>((ok) => setTimeout(() => ok("red"), 50));
+
+function Box() {
+ const color = use(COLOUR);
+ return (
+ <mesh>
+ <boxGeometry args={[1.5, 1.5, 1.5]} />
+ <meshBasicMaterial color={color} />
+ </mesh>
+ );
+}
+
+function Scene() {
+ return (
+ <AbsoluteFill style={{ background: "#224466" }}>
+ <R3F camera={{ position: [0, 0, 3] }}>
+ <Box />
+ </R3F>
+ </AbsoluteFill>
+ );
+}
+registerRoot(() => <Composition id="hold" component={Scene} width={160} height={160} fps={30} durationInFrames={3} />);