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.

A material or a shape whose props say the same thing isn't rebuilt: memo compares their values, so an array or a colour written inline no longer reads as a change every frame, and usePlace3d keeps its matrix while its numbers are the same (3000 parts: 103 to 78 ms a frame, the same pixels; #160)

shinyobjectz95d8236e71f5
diff --git a/sdk/render/src/3d/core.tsx b/sdk/render/src/3d/core.tsx
index c1596618..24ce4bb1 100644
--- a/sdk/render/src/3d/core.tsx
+++ b/sdk/render/src/3d/core.tsx
@@ -13,6 +13,21 @@ export function serialisable(props: Record<string, unknown>): Record<string, unk
return out;
}
+const same = (a: unknown, b: unknown, depth: number): boolean => {
+ if (Object.is(a, b)) return true;
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null || depth === 0) return false;
+ // a React element is its identity: its props can hold anything
+ if ("$$typeof" in a || Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false;
+ const [ka, kb] = [Object.keys(a), Object.keys(b)];
+ return ka.length === kb.length && ka.every((k) => Object.hasOwn(b, k) && same((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k], depth - 1));
+};
+
+/** Whether two elements' props say the same thing: `memo`'s compare for a part whose props are values. An array or a
+ * colour written inline (`half_size={[1, 0.5, 1]}`, `Srgba.hex("#f80")`) is made again every frame, which the default
+ * compare by identity reads as a change, so every such part was rebuilt every frame (#160). Functions, elements and
+ * children still compare by identity. */
+export const sameProps = (a: object, b: object): boolean => same(a, b, 4);
+
export function node3d(kind: string, props: Record<string, unknown>, children?: ReactNode, extra?: Record<string, string>) {
return (
<div data-3d={kind} data-props={JSON.stringify(serialisable(props))} {...extra}>
diff --git a/sdk/render/src/3d/shapes.tsx b/sdk/render/src/3d/shapes.tsx
index 772b7287..2077cfd3 100644
--- a/sdk/render/src/3d/shapes.tsx
+++ b/sdk/render/src/3d/shapes.tsx
@@ -3,7 +3,7 @@ import { Children, Fragment, isValidElement, memo, useId, type ReactElement, typ
import { Solid } from "../solid";
import { useAnimation3d, type Keys, type Move, type TrackSpec } from "@raea/math/animate";
import { Place3d, usePlace3d, usePointer3d, type LocalBounds, type PointerHandlers3d } from "../events3d";
-import { Canvas3DProps, Vec3, checkArgs, checkPass, checkVec, node3d, serialisable } from "./all";
+import { Canvas3DProps, Vec3, checkArgs, checkPass, checkVec, node3d, sameProps, serialisable } from "./all";
import { InCanvas3D } from "./model";
import type { MeshAttribute } from "./attribute";
import { firstKey, useSequenceFrames } from "./clock";
@@ -128,7 +128,7 @@ function geometry<P extends { args?: number[] }>(name: string, defaults: number[
};
Object.defineProperty(Component, "name", { value: display });
// memo: a geometry's props seldom change while its Mesh3d moves every frame, so React skips it (FC1b, bloom_3d)
- const Memo = Object.assign(memo(Component), { displayName: display });
+ const Memo = Object.assign(memo(Component, sameProps), { displayName: display });
GEOMETRY_SHAPES.set(Memo, toShape);
GEOMETRY_BOUNDS.set(Memo, (props) => boundsOf(name, argsOf(props as P)));
return Memo as unknown as typeof Component;
diff --git a/sdk/render/src/bevy3d/materials.tsx b/sdk/render/src/bevy3d/materials.tsx
index b60872e6..02499179 100644
--- a/sdk/render/src/bevy3d/materials.tsx
+++ b/sdk/render/src/bevy3d/materials.tsx
@@ -1,7 +1,7 @@
// Materials: standard, toon and extended (their colours in colors.ts, the extensions in extensions.tsx).
import { isValidElement, memo, useContext, type ReactElement } from "react";
import type { TrackSpec } from "@raea/math/animate";
-import { InModel } from "../3d/all";
+import { InModel, sameProps } from "../3d/all";
import { LinearRgba, MADE_FROM, baseOf, emissiveOf, GridExtension, PaintExtension, WearExtension, ShaderExtension, ToonExtension, wearOf, type Affine2, type Srgba, type Only, type GridExtensionProps, type PaintExtensionProps, type WearExtensionProps, type ShaderExtensionProps, type ToonExtensionProps } from "./all";
import { samplerSpec, surfaceShader, type SamplerSpec, type SurfaceDrawProps } from "../3d";
import { MeshBasicMaterial, MeshPhysicalMaterial, MeshStandardMaterial, MeshToonMaterial, type MeshStandardMaterialProps } from "../three";
@@ -240,7 +240,7 @@ function uvOf(t: Affine2 | undefined): { repeat?: [number, number]; uvTransform?
* @bevy bevy_pbr::pbr_material::StandardMaterial */
export const StandardMaterial = memo(function StandardMaterial<A>(p: StandardMaterialProps & { animate?: Only<A, StandardMaterialAnimate> }) {
return standard(p, {});
-}) as unknown as <A>(p: StandardMaterialProps & { animate?: Only<A, StandardMaterialAnimate> }) => ReactElement;
+}, sameProps) as unknown as <A>(p: StandardMaterialProps & { animate?: Only<A, StandardMaterialAnimate> }) => ReactElement;
// raea's extensions an ExtendedMaterial puts on its StandardMaterial, as the old material's props
type Extended = { grid?: GridExtensionProps; paint?: PaintExtensionProps; wear?: MeshStandardMaterialProps["wear"]; procedural?: ReturnType<typeof proceduralOf>; shader?: Record<string, unknown> };
diff --git a/sdk/render/src/events3d.tsx b/sdk/render/src/events3d.tsx
index 5977e6a0..258dd87a 100644
--- a/sdk/render/src/events3d.tsx
+++ b/sdk/render/src/events3d.tsx
@@ -11,7 +11,7 @@
// frame renders, the camera its view; once the frame commits, the pointer is picked against them once and each part's
// handlers for that frame are called. Frames go in order (a play, a render of a run of frames), as a page's own
// events do: a render of one frame on its own has no frame before it to press in.
-import { createContext, useContext, useEffect } from "react";
+import { createContext, useContext, useEffect, useRef } from "react";
import { useCurrentFrame, useVideoConfig } from "@raea/app/clock";
import { useInput } from "@raea/app/games";
import { hasNative, native } from "@raea/app/native";
@@ -342,7 +342,12 @@ export function pickIn(pointer: { x: number; y: number }, camera: PickCamera, pa
/** Place3d's value inside a part at `m` (its parent's world matrix times its own). */
export function usePlace3d(position: Vec3, rotation: Vec3, scale: number | Vec3, q?: [number, number, number, number]): Mat4 {
- return under(useContext(Place3d), local(position, rotation, scale, q));
+ const m = under(useContext(Place3d), local(position, rotation, scale, q));
+ // the same matrix while its numbers are: a new one each frame is a new Place3d value, which rebuilds every part
+ // under this one, memoised or not (#160)
+ const kept = useRef(m);
+ if (kept.current !== m && kept.current.length === m.length && kept.current.every((v, i) => v === m[i])) return kept.current;
+ return (kept.current = m);
}
/** A part's pointer handlers, as a page element's. */