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.

R3F23: a pass is the same one frame to frame by what was drawn, and a ShaderMaterial reads a target's image as drawn (R3F23c, part). targets.ts names what a pass drew (its scene, and the lone mesh's material or the override material) and counts passes into the same target by that, so GPUComputationRenderer's quad, drawn once with its pass-through shader and then each frame with the variable's, keeps one layer and camera for its compute pass from frame 0 on, not the init's (passes.ts keys a pass by texture, what and count). shader.ts never asks for a raw linear copy of a target raea's engine draws: three-shader takes that copy once, before any pass has filled it. Tests: targets.test.tsx (the swapped quad's compute pass at frames 0 and 2; a ShaderMaterial's texture is the camera's image, v turned over)

shinyobjectzeb4366424530
diff --git a/sdk/r3f/src/input/passes.ts b/sdk/r3f/src/input/passes.ts
index 80315570..a2ce99b4 100644
--- a/sdk/r3f/src/input/passes.ts
+++ b/sdk/r3f/src/input/passes.ts
@@ -15,8 +15,9 @@ const FIRST_LAYER = 64;
/** the order of the first pass's camera; the screen's is 0 */
const FIRST_ORDER = -10000;
-/** each pass's place by its key (the target's texture and its place among that target's passes a frame), as it was
- * first drawn: its layer and camera order stay the same frame to frame, so its parts and camera are the same rows */
+/** each pass's place by its key (the target's texture, what was drawn into it and its place among those passes a
+ * frame), as it was first drawn: its layer and camera order stay the same frame to frame, so its parts and camera are
+ * the same rows */
const seen = new Map<string, number>();
function placeOf(key: string): number {
@@ -45,7 +46,7 @@ const QUAD: Part = { type: "OrthographicCamera", props: { position: [0, 0, 1], l
/** a pass as parts: its scene's on its layer and its camera into the target's image */
function passParts(p: Omit<Pass, "parts">, gl: THREE.WebGLRenderer): Part[] {
- const key = `${p.target.texture.uuid}#${p.k}`;
+ const key = `${p.target.texture.uuid}#${p.what}#${p.k}`;
const i = placeOf(key);
const layer = FIRST_LAYER + i;
const root = `pass/${key}`;
diff --git a/sdk/r3f/src/input/shader.ts b/sdk/r3f/src/input/shader.ts
index 32e994bb..6169acb0 100644
--- a/sdk/r3f/src/input/shader.ts
+++ b/sdk/r3f/src/input/shader.ts
@@ -8,6 +8,7 @@
// that includes <tonemapping_fragment> on a tone-mapped material goes as "linear", those includes dropped, and Bevy's
// tone mapping maps it as three's would.
import * as THREE from "three";
+import { onGpu } from "./targets";
import { flipsY, srcOf } from "./texture";
import type { Part, Props } from "./material";
@@ -180,8 +181,10 @@ export function shaderPart(m: THREE.ShaderMaterial, points = false): Part {
const t = v as THREE.Texture;
const src = srcOf(t);
if (!src) warnOnce(`${m.type} texture ${name}`, "has no picture raea can load (a render target's?): drawn as white");
- // three samples a picture without a colour space raw, an sRGB one decoded (as Bevy's sRGB image is)
- textures[name] = src && t.colorSpace !== THREE.SRGBColorSpace ? { src, colorSpace: "linear" } : src ?? WHITE;
+ // three samples a picture without a colour space raw, an sRGB one decoded (as Bevy's sRGB image is); a target
+ // raea's engine draws is read as drawn (R3F23: a float image has no sRGB view, and an 8-bit one decodes to the
+ // linear numbers three's target holds), never a raw copy, which would be taken once, before any pass filled it
+ textures[name] = src && t.colorSpace !== THREE.SRGBColorSpace && !onGpu(t) ? { src, colorSpace: "linear" } : src ?? WHITE;
if (flipsY(t) && src) flipY.push(name);
continue;
}
diff --git a/sdk/r3f/src/input/targets.ts b/sdk/r3f/src/input/targets.ts
index 3cf9d8c6..fc905270 100644
--- a/sdk/r3f/src/input/targets.ts
+++ b/sdk/r3f/src/input/targets.ts
@@ -81,10 +81,22 @@ export interface Pass {
/** the renderer's clear colour, linear rgba, when the render was asked for */
clear: [number, number, number, number];
autoClear: boolean;
+ /** what was drawn (drawnOf), and its place among this frame's passes of the same into the same target */
+ what: string;
k: number;
parts: Part[];
}
+/** what a pass draws, by identity: its scene or object, the material a lone mesh is drawn with (GPUComputationRenderer
+ * swaps one quad's material between its pass-through and each variable's shader) and the scene's override. A pass's
+ * rows (its camera, its parts) are those of the same thing drawn into the same target, frame to frame */
+function drawnOf(scene: THREE.Object3D): string {
+ const mat = (scene as THREE.Mesh).isMesh ? (scene as THREE.Mesh).material : undefined;
+ const mats = Array.isArray(mat) ? mat.map((m) => m.uuid).join(",") : mat?.uuid;
+ const over = (scene as THREE.Scene).overrideMaterial?.uuid;
+ return [scene.uuid, mats, over].filter(Boolean).join(":");
+}
+
/** the targets a pass was drawn into on raea's GPU (recordPass), ever, by their texture */
const onGpuTargets = new WeakMap<THREE.Texture, THREE.RenderTarget>();
@@ -115,7 +127,8 @@ function recordPass(rt: THREE.RenderTarget, scene: THREE.Object3D, camera: THREE
// as before, the scene drawn without it (lights.ts skyOf says so for an environment)
if ((rt as { isWebGLCubeRenderTarget?: boolean }).isWebGLCubeRenderTarget || (rt.texture as { isCubeTexture?: boolean }).isCubeTexture || rt.depth > 1) return;
const passes = passesFor(gl);
- const p = { target: rt, scene, camera, clear: [...clear] as Pass["clear"], autoClear: gl.autoClear !== false, k: passes.filter((x) => x.target === rt).length };
+ const p = { target: rt, scene, camera, clear: [...clear] as Pass["clear"], autoClear: gl.autoClear !== false, what: drawnOf(scene), k: 0 };
+ p.k = passes.filter((x) => x.target === rt && x.what === p.what).length;
onGpuTargets.set(rt.texture, rt);
passes.push({ ...p, parts: partsOf ? partsOf(p, gl) : [] });
// a renderer whose frames nobody steps (step() not called) keeps its latest passes, not every one ever drawn
diff --git a/sdk/r3f/test/targets.test.tsx b/sdk/r3f/test/targets.test.tsx
index 8a0e403a..6c66769e 100644
--- a/sdk/r3f/test/targets.test.tsx
+++ b/sdk/r3f/test/targets.test.tsx
@@ -103,4 +103,63 @@ describe("a render into a target, drawn by raea's engine", () => {
expect(k((cams[0].props.layers as number[])[0])).toBe(0.25);
expect(k((cams[1].props.layers as number[])[0])).toBe(0.75);
});
+
+ test("is the same thing's frame to frame: one quad, its material swapped, keeps a pass for each (GPUComputationRenderer)", () => {
+ // its init draws the quad with its pass-through shader into the target once, then each frame with the variable's:
+ // a later frame's compute pass is frame 0's compute pass again (its rows, its shader), not the init's
+ function Swapped() {
+ const t = useMemo(() => new THREE.WebGLRenderTarget(16, 16, { type: THREE.FloatType }), []);
+ const shader = (k: number) => new THREE.ShaderMaterial({ uniforms: { k: { value: k } }, vertexShader: "void main() { gl_Position = vec4(position, 1.0); }", fragmentShader: "uniform float k; void main() { gl_FragColor = vec4(k); }" });
+ const [through, compute] = useMemo(() => [shader(0.1), shader(0.9)], []);
+ const quad = useMemo(() => new THREE.Mesh(new THREE.PlaneGeometry(2, 2), through), [through]);
+ const cam = useMemo(() => new THREE.Camera(), []);
+ const init = useMemo(() => ({ done: false }), []);
+ useFrame(({ gl }) => {
+ gl.setRenderTarget(t);
+ if (!init.done) {
+ quad.material = through;
+ gl.render(quad, cam);
+ init.done = true;
+ }
+ quad.material = compute;
+ gl.render(quad, cam);
+ gl.setRenderTarget(null);
+ });
+ return null;
+ }
+ const key = `targets${n++}`;
+ const camsAt = (frame: number) => {
+ const st = stepTo(key, frame, 30, 400, 400, {}, () => <Swapped />).state as unknown as { scene: THREE.Scene; camera: THREE.Camera; gl: THREE.WebGLRenderer };
+ return flat(convert(st.scene, st.camera, st.gl, 400).parts).filter((p) => typeof p.props.target === "string");
+ };
+ const first = camsAt(0);
+ const later = camsAt(2);
+ end(key);
+ expect(first.length).toBe(2);
+ expect(later.length).toBe(1);
+ expect(later[0].props.name).toBe(first[1].props.name);
+ expect(later[0].props.layers).toEqual(first[1].props.layers);
+ expect(later[0].props.name).not.toBe(first[0].props.name);
+ });
+
+ test("is read by a ShaderMaterial as its camera draws it: the image itself, v turned over, never a raw copy", () => {
+ // a raw copy ({colorSpace: "linear"}: three-shader's linear::raw) is taken once, before any pass has drawn
+ function Read() {
+ const t = useMemo(() => new THREE.WebGLRenderTarget(32, 32), []);
+ const scene = useMemo(() => Object.assign(new THREE.Scene(), { background: new THREE.Color("#336699") }), []);
+ const cam = useMemo(() => new THREE.PerspectiveCamera(), []);
+ const mat = useMemo(() => new THREE.ShaderMaterial({ uniforms: { tex: { value: t.texture } }, vertexShader: "varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }", fragmentShader: "uniform sampler2D tex; varying vec2 vUv; void main() { gl_FragColor = texture2D(tex, vUv); }" }), [t]);
+ useFrame(({ gl }) => {
+ gl.setRenderTarget(t);
+ gl.render(scene, cam);
+ gl.setRenderTarget(null);
+ });
+ return <mesh name="screen" material={mat}><planeGeometry args={[2, 2]} /></mesh>;
+ }
+ const out = partsOf(() => <Read />);
+ const target = out.find((p) => typeof p.props.target === "string")!.props.target as string;
+ const shader = flat(out.find((p) => p.type === "Mesh" && String(p.props.name).includes("screen"))!.children ?? []).find((c) => c.props.textures)!;
+ expect((shader.props.textures as Record<string, unknown>).tex).toBe(target);
+ expect(shader.props.flipY).toContain("tex");
+ });
});