Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.
JavaScript 3d3d-assets3d-modelingagent-skillsagent-toolsai-agentsasset-pipelinecligame-assetsglbgltfmcpmcp-servermodel-context-protocolprocedural-generationprocedural-modelingtext-to-3dthreejstypescript
/**
* Regression test for the cross-module THREE bug — see
* `kiln-cross-module-three.md` memory entry.
*
* The kiln executor uses `new Function(...)` to run LLM-generated code,
* which under bun creates an isolated module realm. `new THREE.Mesh()`
* inside that sandbox produces objects whose constructor is NOT the same
* class object as `THREE.Mesh` imported by render.ts / inspect.ts /
* solids.ts / etc.
*
* Before this was fixed, every consumer using `instanceof THREE.Mesh`
* silently dropped sandbox meshes (treated them as not-meshes), producing
* GLBs with empty Mesh nodes and corrupted texture export.
*
* The fix: every cross-sandbox check uses Three.js's `.isMesh`,
* `.isObject3D`, `.isTexture`, `.isMeshStandardMaterial` etc. duck-typing
* flags, which are set on the prototype and survive across module realms.
*
* This test ensures the canonical "LLM emitted `new THREE.Mesh()` directly"
* code path produces a GLB with the mesh attached (not stripped). If the
* test fails, an `instanceof THREE.X` check regressed somewhere on the
* bridge path.
*/
import { test, expect } from 'bun:test';
import { NodeIO } from '@gltf-transform/core';
import { renderGLB } from '../render';
test('sandbox new THREE.Mesh() survives the GLB bridge (cross-module realm)', async () => {
const code = `
const meta = { name: 'sandbox-mesh-test', category: 'prop' };
function build() {
const root = createRoot('Root');
// The LLM canonical pattern: \`new THREE.Mesh(geo, mat)\` directly,
// NOT via createPart. This goes through the cross-module realm.
const m = new THREE.Mesh(boxGeo(1, 1, 1), gameMaterial(0xff0000));
m.name = 'Mesh_DirectMesh';
root.add(m);
return root;
}
`;
const result = await renderGLB(code);
expect(result.warnings).toEqual([]);
expect(result.meta.tris).toBeGreaterThan(0);
// Confirm the mesh ACTUALLY made it into the GLB binary, not just the
// Three.js scene. Read back via NodeIO and verify a node named
// "Mesh_DirectMesh" has an attached mesh with non-zero tris.
const io = new NodeIO();
const doc = await io.readBinary(result.glb);
const directNode = doc
.getRoot()
.listNodes()
.find((n) => n.getName() === 'Mesh_DirectMesh');
expect(directNode).toBeDefined();
const mesh = directNode!.getMesh();
expect(mesh).toBeDefined();
let tris = 0;
for (const p of mesh!.listPrimitives()) {
tris += (p.getIndices()?.getCount() ?? 0) / 3;
}
expect(tris).toBeGreaterThan(0);
});
test('sandbox new THREE.Mesh() with pbrMaterial+texture survives — texture serialised', async () => {
// Reproduces the textured-aircraft cycle issue: LLM does
// new THREE.Mesh(unwrappedGeo, pbrMaterial({ albedo: loadedTex }))
// and the resulting GLB needs to actually carry the texture.
// Use the closed approved-ID surface: raw paths are intentionally absent
// from new-generation globals.
const code = `
const meta = { name: 'sandbox-textured-mesh-test', category: 'prop' };
async function build() {
const root = createRoot('Root');
const tex = await loadApprovedTexture('kiln.texture.bark-albedo.v1');
const mat = pbrMaterial({ albedo: tex, roughness: 0.85, metalness: 0 });
const geo = copyGeometry(boxGeo(1, 1, 1));
const m = new THREE.Mesh(geo, mat);
m.name = 'Mesh_TexturedDirect';
root.add(m);
return root;
}
`;
const result = await renderGLB(code);
expect(result.warnings).toEqual([]);
const io = new NodeIO();
const doc = await io.readBinary(result.glb);
const root = doc.getRoot();
// Texture carried through?
expect(root.listTextures().length).toBeGreaterThan(0);
// Material with base-color texture present?
const matWithTex = root.listMaterials().find((m) => m.getBaseColorTexture());
expect(matWithTex).toBeDefined();
});