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: in-pingpong, three's GPUComputationRenderer stepping a wave between two float targets, the plane's ShaderMaterial showing the one written (test/parity/targets.tsx). targets.test.tsx follows a GPUComputationRenderer through frames 0 to 2: the start drawn into both targets and the first step at frame 0, one step a frame after, alternating targets, the display reading the target each step wrote, and a step's pass keeping its name

shinyobjectz186098d162c7
diff --git a/sdk/r3f/test/parity/targets.tsx b/sdk/r3f/test/parity/targets.tsx
index 58292816..ca4c0ec1 100644
--- a/sdk/r3f/test/parity/targets.tsx
+++ b/sdk/r3f/test/parity/targets.tsx
@@ -1,9 +1,10 @@
// The parity cases for render targets an app draws into itself, drawn on raea's GPU (docs/r3f.feature R3F23,
// src/input/passes.ts): each drawn by raea and by three.js in Chrome.
import { useFBO } from "@react-three/drei";
-import { useFrame } from "@react-three/fiber";
+import { useFrame, useThree } from "@react-three/fiber";
import { useMemo } from "react";
import * as THREE from "three";
+import { GPUComputationRenderer } from "three/examples/jsm/misc/GPUComputationRenderer.js";
import type { InputCase } from "./fixtures";
/** the 11-15 audit's fbo probe: a lit, turning knot drawn into a useFBO from its own scene and camera, every frame,
@@ -38,10 +39,67 @@ function FboPlane() {
);
}
+/** a wave on water, three's GPUComputationRenderer stepping it: each frame its shader reads the last two heights
+ * (x now, y before) from one float target and writes the next into the other, and the plane shows the one written */
+const WAVE = `void main() {
+ vec2 uv = gl_FragCoord.xy / resolution.xy;
+ vec2 c = 1.0 / resolution.xy;
+ vec4 h = texture2D(heightmap, uv);
+ float n = texture2D(heightmap, uv + vec2(0.0, c.y)).x + texture2D(heightmap, uv - vec2(0.0, c.y)).x
+ + texture2D(heightmap, uv + vec2(c.x, 0.0)).x + texture2D(heightmap, uv - vec2(c.x, 0.0)).x;
+ gl_FragColor = vec4((n * 0.5 - h.y) * 0.99, h.x, 0.0, 1.0);
+}`;
+
+const SHOW = {
+ vertexShader: "varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }",
+ fragmentShader: `uniform sampler2D heightmap; varying vec2 vUv;
+void main() {
+ float h = texture2D(heightmap, vUv).x;
+ vec3 deep = vec3(0.04, 0.12, 0.3), crest = vec3(0.85, 0.95, 1.0), trough = vec3(0.0, 0.02, 0.08);
+ gl_FragColor = vec4(h > 0.0 ? mix(deep, crest, clamp(h * 3.0, 0.0, 1.0)) : mix(deep, trough, clamp(-h * 3.0, 0.0, 1.0)), 1.0);
+}`,
+};
+
+function PingPong() {
+ const gl = useThree((s) => s.gl);
+ const sim = useMemo(() => {
+ const gpu = new GPUComputationRenderer(128, 128, gl);
+ const start = gpu.createTexture();
+ const d = start.image.data as Float32Array;
+ // two drops, raised discs off the centre
+ for (let y = 0; y < 128; y++) {
+ for (let x = 0; x < 128; x++) {
+ const h = Math.max(0, 1 - Math.hypot(x - 44, y - 64) / 9) + 0.8 * Math.max(0, 1 - Math.hypot(x - 88, y - 40) / 7);
+ d.set([h, h, 0, 1], (y * 128 + x) * 4);
+ }
+ }
+ const height = gpu.addVariable("heightmap", WAVE, start);
+ gpu.setVariableDependencies(height, [height]);
+ const err = gpu.init();
+ if (err) throw new Error(err);
+ return { gpu, height };
+ }, [gl]);
+ const show = useMemo(() => new THREE.ShaderMaterial({ uniforms: { heightmap: { value: null } }, ...SHOW }), []);
+ useFrame(() => {
+ sim.gpu.compute();
+ show.uniforms.heightmap.value = sim.gpu.getCurrentRenderTarget(sim.height).texture;
+ });
+ return (
+ <mesh material={show} rotation={[-0.5, 0, 0]}>
+ <planeGeometry args={[3, 3]} />
+ </mesh>
+ );
+}
+
export const TARGET_INPUTS: Record<string, InputCase> = {
fbo: {
canvas: { camera: { position: [0, 0, 4], fov: 45 } },
frame: 12,
scene: () => <FboPlane />,
},
+ pingpong: {
+ canvas: { camera: { position: [0, 0, 4], fov: 45 } },
+ frame: 12,
+ scene: () => <PingPong />,
+ },
};
diff --git a/sdk/r3f/test/targets.test.tsx b/sdk/r3f/test/targets.test.tsx
index 6c66769e..153be5da 100644
--- a/sdk/r3f/test/targets.test.tsx
+++ b/sdk/r3f/test/targets.test.tsx
@@ -5,6 +5,7 @@ import { describe, expect, test } from "bun:test";
import { createPortal, useFrame } from "@react-three/fiber";
import { useMemo, type ReactNode } from "react";
import * as THREE from "three";
+import { GPUComputationRenderer } from "three/examples/jsm/misc/GPUComputationRenderer.js";
import { convert } from "../src/input/scene";
import { end, stepTo } from "../src/input/session";
import type { Part } from "../src/parts";
@@ -162,4 +163,42 @@ describe("a render into a target, drawn by raea's engine", () => {
expect((shader.props.textures as Record<string, unknown>).tex).toBe(target);
expect(shader.props.flipY).toContain("tex");
});
+
+ test("ping-pongs as three's GPUComputationRenderer does: its two targets' passes on their frames, the display reading the one drawn", () => {
+ function Wave() {
+ const sim = useMemo(() => ({ gpu: undefined as GPUComputationRenderer | undefined, v: undefined as unknown }), []);
+ const show = useMemo(() => new THREE.ShaderMaterial({ uniforms: { heightmap: { value: null } }, vertexShader: "varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }", fragmentShader: "uniform sampler2D heightmap; varying vec2 vUv; void main() { gl_FragColor = texture2D(heightmap, vUv); }" }), []);
+ useFrame(({ gl }) => {
+ if (!sim.gpu) {
+ sim.gpu = new GPUComputationRenderer(16, 16, gl);
+ sim.v = sim.gpu.addVariable("heightmap", "void main() { gl_FragColor = texture2D(heightmap, gl_FragCoord.xy / resolution.xy) * 0.9; }", sim.gpu.createTexture());
+ sim.gpu.setVariableDependencies(sim.v as never, [sim.v as never]);
+ expect(sim.gpu.init()).toBeNull();
+ }
+ sim.gpu.compute();
+ show.uniforms.heightmap.value = sim.gpu.getCurrentRenderTarget(sim.v as never).texture;
+ });
+ return <mesh name="screen" material={show}><planeGeometry args={[2, 2]} /></mesh>;
+ }
+ const key = `targets${n++}`;
+ const at = (frame: number) => {
+ const st = stepTo(key, frame, 30, 400, 400, {}, () => <Wave />).state as unknown as { scene: THREE.Scene; camera: THREE.Camera; gl: THREE.WebGLRenderer };
+ const parts = flat(convert(st.scene, st.camera, st.gl, 400).parts);
+ const cams = parts.filter((p) => typeof p.props.target === "string");
+ const shader = flat(parts.find((p) => p.type === "Mesh" && String(p.props.name).includes("screen"))!.children ?? []).find((c) => c.props.textures)!;
+ return { cams, read: (shader.props.textures as Record<string, string>).heightmap };
+ };
+ const [f0, f1, f2] = [at(0), at(1), at(2)];
+ end(key);
+ // frame 0: the start drawn into both targets, then the first step into the second; each later frame one step
+ expect(f0.cams.length).toBe(3);
+ expect(f1.cams.length).toBe(1);
+ expect(f2.cams.length).toBe(1);
+ const [a, b] = [f0.cams[0].props.target, f0.cams[1].props.target];
+ expect(a).not.toBe(b);
+ expect([f0.cams[2].props.target, f1.cams[0].props.target, f2.cams[0].props.target]).toEqual([b, a, b]);
+ // the display reads the target each frame's step wrote, and a step's pass keeps its name from frame to frame
+ expect([f0.read, f1.read, f2.read]).toEqual([b, a, b] as string[]);
+ expect(f2.cams[0].props.name).toBe(f0.cams[2].props.name);
+ });
});