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.

raea/world's answers are read in a component with useWorldAnswer(ticket), which renders that component again the frame the answer arrives, in live play and in a render alike (part of #38). world.answer(ticket) was the only read, a pull with no subscription, so a component that never re-rendered never saw its answer. bevy-ts's world-render, world-host and live-world drew green in a render only because the program route steps React twice: the first pass's Engine answers the calls queued at load, and the second pass reads them at frame 0. Made live (one pass, RAEA_3D=live), all three stayed red on every frame.

shinyobjectz1af64f2a41af
diff --git a/sdk/app/src/clock.tsx b/sdk/app/src/clock.tsx
index e045fa39..f622e255 100644
--- a/sdk/app/src/clock.tsx
+++ b/sdk/app/src/clock.tsx
@@ -125,6 +125,14 @@ export function useClockTick(clock: ClockState | null): void {
return () => void t.subs.delete(cb);
}, [t]);
}
+/** Calls `cb` each time `clock`'s frame moves on (in the batch that renders the new frame), until the returned stop is
+ * called: for a store whose value can change only from frame to frame (useWorldAnswer). */
+export function onClockTick(clock: ClockState, cb: () => void): () => void {
+ const t = TICKERS.get(clock);
+ if (!t) return () => {};
+ t.subs.add(cb);
+ return () => void t.subs.delete(cb);
+}
export const LocalTimeContext = createContext<LocalTime | null>(null);
/** Where a subtree sits in time, as data that changes only when its place does (not every frame): the composition
diff --git a/sdk/app/src/world.ts b/sdk/app/src/world.ts
index 51866f39..5f3e35af 100644
--- a/sdk/app/src/world.ts
+++ b/sdk/app/src/world.ts
@@ -3,11 +3,17 @@
// world.trigger_event, a compiled-in plugin's methods such as avian3d/cast_ray...) on anything Reflect and
// registered, through the same layer BevyApp programs and void's spaces use (engine/bevy-world). The game's step runs
// before the Engine updates and holds no handle on its World, so a call is queued: `send` gives a ticket, the Engine
-// runs it at the start of its next update, and `answer(ticket)` has its answer from the next frame on. An entity in
-// params (`entity`, `parent`, each of `entities`) may be a part's name (<Mesh name>, <Entity name>) or row id.
+// runs it at the start of its next update, and its answer is there from the next frame on. An entity in params
+// (`entity`, `parent`, each of `entities`) may be a part's name (<Mesh name>, <Entity name>) or row id.
+//
+// In a component, read an answer with useWorldAnswer(ticket): the component renders again the frame it arrives, and
+// nothing else does. `world.answer(ticket)` is the plain read for code outside React (a game's step, a system); a
+// component that reads it is only as fresh as whatever else makes it render.
//
// const t = world.send("world.get_components", { entity: "ball", components: [TRANSFORM] });
-// ... a frame later: world.answer(t)?.value
+// function Ball() { const got = useWorldAnswer(t); ... got?.value ... }
+import { useCallback, useSyncExternalStore } from "react";
+import { onClockTick, useClockState } from "./clock";
import { native } from "./native";
type Call = (name: string, args: unknown) => unknown;
@@ -16,13 +22,45 @@ const call: Call = (name, args) => (native as unknown as Call)(name, args);
/** A queued call's answer: its value, or the method's error. */
export type Answer<T> = { value: T; error?: undefined } | { value?: undefined; error: string };
+// the answers come in by ticket, once each, and never change: kept here, a reader gets the same object every time
+// (useSyncExternalStore compares snapshots by identity), and an answer outlives the Engine's queue (it keeps the last
+// few hundred)
+const g = globalThis as { __raea_world_answers?: Map<number, Answer<unknown>> };
+const answers: Map<number, Answer<unknown>> = (g.__raea_world_answers ??= new Map());
+
export const world = {
/** A BRP method queued for the Engine's next update: its ticket. */
send(method: string, params?: unknown): number {
return call("bevy.world.send", params === undefined ? { method } : { method, params }) as number;
},
- /** The answer to a ticket once the Engine has run its call, undefined before. */
+ /** The answer to a ticket once the Engine has run its call, undefined before. The plain read, for code outside
+ * React; inside a component, useWorldAnswer renders it again when the answer arrives. */
answer<T = unknown>(ticket: number): Answer<T> | undefined {
- return (call("bevy.world.answer", { ticket }) as Answer<T> | null) ?? undefined;
+ let a = answers.get(ticket);
+ if (a === undefined) {
+ a = (call("bevy.world.answer", { ticket }) as Answer<unknown> | null) ?? undefined;
+ if (a !== undefined) answers.set(ticket, a);
+ }
+ return a as Answer<T> | undefined;
},
};
+
+/** A queued call's answer, read in a component: undefined until the Engine has run the call, then its answer, the
+ * component rendering again on the frame it arrives (in live play and in a render alike). Only the components that
+ * read it render again. */
+export function useWorldAnswer<T = unknown>(ticket: number): Answer<T> | undefined {
+ const clock = useClockState("useWorldAnswer");
+ const subscribe = useCallback(
+ (changed: () => void) => {
+ if (answers.has(ticket)) return () => {};
+ const stop = onClockTick(clock, () => {
+ if (world.answer(ticket) === undefined) return;
+ stop();
+ changed();
+ });
+ return stop;
+ },
+ [clock, ticket],
+ );
+ return useSyncExternalStore(subscribe, () => world.answer<T>(ticket));
+}
diff --git a/sdk/guides/craft/bevy/limits.md b/sdk/guides/craft/bevy/limits.md
index 5bb28695..49c4627e 100644
--- a/sdk/guides/craft/bevy/limits.md
+++ b/sdk/guides/craft/bevy/limits.md
@@ -62,7 +62,9 @@ Mesh3d, Model, light or camera (`raea/bevy-own`). A bare `<Entity name>` takes a
fields as an asset path, an inline asset (`{ base_color: ..., unlit: true }`) or a Mesh as `"shape:box(1, 1, 1)"`, and
its Entity fields as another part's name (a `ChildOf` naming its parent). `<BufferMesh positions indices normals uvs>`
builds a mesh from vertices. `useInput()` has `buttons`, `wheel`, `gamepads` and `touches`. A live game reaches the
-Bevy World with `raea/world` (`send` a BRP method, read `answer(ticket)` a frame later).
+Bevy World with `raea/world`: `send` a BRP method for a ticket, then read its answer in a component with
+`useWorldAnswer(ticket)`, which renders that component again the frame the answer arrives (`world.answer(ticket)` is
+the plain read for code outside React; in a component it's only as fresh as whatever else makes it render).
`<Model animations={[{ clip, time, weight }]}>` blends up to 4 clips (a transition is weights moved over frames), and
`<Model morphs={{ smile: 0.8 }}>` sets morph targets. `<Gizmo lines strip color width depthBias>` draws debug lines.
A second camera is `<Entity position>` with `<BevyComponent type="bevy_camera::components::Camera3d" value={{}} />` and
diff --git a/sdk/raea/bench/bevy-ts/cases/live-world.tsx b/sdk/raea/bench/bevy-ts/cases/live-world.tsx
index efdb86b9..a5c40cb3 100644
--- a/sdk/raea/bench/bevy-ts/cases/live-world.tsx
+++ b/sdk/raea/bench/bevy-ts/cases/live-world.tsx
@@ -8,7 +8,7 @@
// probe: 48 8 g>150 r<100
import { AbsoluteFill, Composition, registerRoot } from "raea";
import { Canvas3D, Entity } from "raea/3d";
-import { world } from "raea/world";
+import { useWorldAnswer, world } from "raea/world";
import { StandardMaterial } from "raea/pbr";
import { Cuboid, Mesh3d } from "raea/mesh";
import { Transform } from "raea/transform";
@@ -21,8 +21,8 @@ const back = world.send("world.get_components", { entity: "marker", components:
type Got = { components: Record<string, { translation: [number, number, number] }> };
function Shot() {
- const a = world.answer<Got>(read)?.value?.components[T]?.translation[0];
- const b = world.answer<Got>(back)?.value?.components[T]?.translation[0];
+ const a = useWorldAnswer<Got>(read)?.value?.components[T]?.translation[0];
+ const b = useWorldAnswer<Got>(back)?.value?.components[T]?.translation[0];
return (
<AbsoluteFill style={{ background: "#000" }}>
<Canvas3D layered>
diff --git a/sdk/raea/bench/bevy-ts/cases/world-host.tsx b/sdk/raea/bench/bevy-ts/cases/world-host.tsx
index c340db58..95ecf2e8 100644
--- a/sdk/raea/bench/bevy-ts/cases/world-host.tsx
+++ b/sdk/raea/bench/bevy-ts/cases/world-host.tsx
@@ -14,7 +14,7 @@
// probe: 48 8 g>150 r<100
import { AbsoluteFill, Composition, registerRoot } from "raea";
import { Canvas3D, Entity } from "raea/3d";
-import { world } from "raea/world";
+import { useWorldAnswer, world } from "raea/world";
import { Transform } from "raea/transform";
import { Camera3d } from "raea/camera";
@@ -30,10 +30,10 @@ const diag = world.send("raea/diagnostics", {});
function Shot() {
// DynamicWorldSerializer's JSON: { resources: {}, entities: { bits: { components } } }
- const s = world.answer<{ entities: Record<string, { components: Record<string, unknown> }> }>(saved)?.value;
+ const s = useWorldAnswer<{ entities: Record<string, { components: Record<string, unknown> }> }>(saved)?.value;
const names = Object.values(s?.entities ?? {}).map((e) => JSON.stringify(e.components["bevy_ecs::name::Name"] ?? ""));
const okSave = names.some((n) => n.includes("marker")) && names.some((n) => n.includes("other"));
- const d = world.answer<Record<string, unknown>>(diag)?.value ?? {};
+ const d = useWorldAnswer<Record<string, unknown>>(diag)?.value ?? {};
const okDiag = "fps" in d && "entity_count" in d;
return (
<AbsoluteFill style={{ background: "#000" }}>
diff --git a/sdk/raea/bench/bevy-ts/cases/world-render.tsx b/sdk/raea/bench/bevy-ts/cases/world-render.tsx
index 198c451c..f41d8923 100644
--- a/sdk/raea/bench/bevy-ts/cases/world-render.tsx
+++ b/sdk/raea/bench/bevy-ts/cases/world-render.tsx
@@ -8,7 +8,7 @@
// probe: 48 8 g>150 r<100
import { AbsoluteFill, Composition, registerRoot } from "raea";
import { Canvas3D, Entity } from "raea/3d";
-import { world } from "raea/world";
+import { useWorldAnswer, world } from "raea/world";
import { StandardMaterial } from "raea/pbr";
import { Cuboid, Mesh3d } from "raea/mesh";
import { Transform } from "raea/transform";
@@ -21,8 +21,8 @@ const back = world.send("world.get_components", { entity: "marker", components:
type Got = { components: Record<string, { translation: [number, number, number] }> };
function Shot() {
- const a = world.answer<Got>(read)?.value?.components[T]?.translation[0];
- const b = world.answer<Got>(back)?.value?.components[T]?.translation[0];
+ const a = useWorldAnswer<Got>(read)?.value?.components[T]?.translation[0];
+ const b = useWorldAnswer<Got>(back)?.value?.components[T]?.translation[0];
return (
<AbsoluteFill style={{ background: "#000" }}>
<Canvas3D layered>