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.

R3F: a mesh swapped in with another shader program is another part, not the old one's layout

shinyobjectzd6c7bf8d360b
diff --git a/sdk/r3f/src/input/scene.ts b/sdk/r3f/src/input/scene.ts
index 97cb23df..816ed6dd 100644
--- a/sdk/r3f/src/input/scene.ts
+++ b/sdk/r3f/src/input/scene.ts
@@ -340,6 +340,17 @@ function spriteParts(s: THREE.Sprite, name: string, w: Walk): Part[] {
return [{ type: "Mesh", props: { name, position: pos.toArray(), quaternion: q.toArray(), scale: [scale.x, scale.y, 1], castShadow: false, receiveShadow: false }, children: [{ type: "BufferMesh", props: { positions, uvs, normals: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], indices: [0, 1, 2, 0, 2, 3] } }, surface] }];
}
+/** A mesh part with a three.js shader named for its program too: a part's shader and uniform layout are its first
+ * frame's, so a mesh swapped in at the same place in the tree (a <primitive object> given another mesh) with another
+ * program is another part, not the old one's layout fed the new one's uniforms. */
+function byProgram(p: Part): Part {
+ const sh = p.type === "Mesh" ? p.children?.find((c) => c.type === "ThreeShaderMaterial") : undefined;
+ if (!sh) return p;
+ const program = JSON.stringify(sh.props.glsl ?? sh.props.wgsl ?? null);
+ const h = [...program].reduce((a, c) => Math.imul(a ^ c.charCodeAt(0), 16777619), 2166136261) >>> 0;
+ return { ...p, props: { ...p.props, name: `${p.props.name}~${h.toString(36)}` } };
+}
+
/** one object and what hangs from it */
function walk(o: THREE.Object3D, path: string, w: Walk): Part[] {
if (!o.visible || !o.layers.test(w.camera.layers)) return [];
@@ -360,7 +371,7 @@ function walk(o: THREE.Object3D, path: string, w: Walk): Part[] {
if (isTroikaText(o)) own = textParts(o, name);
else if ((o as THREE.BatchedMesh).isBatchedMesh) own = batchedParts(o as THREE.BatchedMesh, name, w);
else if ((o as THREE.InstancedMesh).isInstancedMesh) own = instancedParts(o as THREE.InstancedMesh, name, w, {});
- else if ((o as THREE.Mesh).isMesh) own = meshParts(o as THREE.Mesh, name, w, {});
+ else if ((o as THREE.Mesh).isMesh) own = meshParts(o as THREE.Mesh, name, w, {}).map(byProgram);
else if ((o as THREE.Points).isPoints) return [...pointParts(o as THREE.Points, name, w, at), ...kids.length ? [{ type: "Group", props: { name: `${name}/`, ...at }, children: kids }] : []];
else if ((o as THREE.Line).isLine) return [...lineParts(o as THREE.Line, name), ...kids.length ? [{ type: "Group", props: { name: `${name}/`, ...at }, children: kids }] : []];
else if ((o as THREE.Sprite).isSprite) return [...spriteParts(o as THREE.Sprite, name, w), ...kids.length ? [{ type: "Group", props: { name: `${name}/`, ...at }, children: kids }] : []];
diff --git a/sdk/r3f/test/input.test.tsx b/sdk/r3f/test/input.test.tsx
index 79e72e16..097dbbff 100644
--- a/sdk/r3f/test/input.test.tsx
+++ b/sdk/r3f/test/input.test.tsx
@@ -112,6 +112,21 @@ describe("the scene graph", () => {
end(a.key);
});
+ test("a mesh swapped into the same place with another shader program is another part; the same program keeps its", () => {
+ const vs = "void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }";
+ const shaded = (fs: string, uniforms: Record<string, THREE.IUniform>) => new THREE.Mesh(new THREE.PlaneGeometry(), new THREE.ShaderMaterial({ uniforms, vertexShader: vs, fragmentShader: fs }));
+ const a = shaded("uniform float k; void main() { gl_FragColor = vec4(k); }", { k: { value: 0.5 } });
+ const a2 = shaded("uniform float k; void main() { gl_FragColor = vec4(k); }", { k: { value: 0.9 } });
+ const b = shaded("uniform mat4 m; void main() { gl_FragColor = m[0]; }", { m: { value: new THREE.Matrix4() } });
+ const nameOf = (o: THREE.Mesh) => {
+ const r = partsOf(() => <primitive object={o} />);
+ end(r.key);
+ return flat(r.parts).find((p) => p.type === "Mesh")!.props.name;
+ };
+ expect(nameOf(a)).toBe(nameOf(a2));
+ expect(nameOf(a)).not.toBe(nameOf(b));
+ });
+
test("Points are quads facing the camera with three's size, lines are gizmo lines, a sprite a quad", () => {
const pts = new THREE.BufferGeometry().setAttribute("position", new THREE.Float32BufferAttribute([0, 0, 0, 1, 0, 0], 3));
const line = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 1, 0), new THREE.Vector3(1, 1, 0)]);