Skip to content

armgit / kiln

Public Imported from GitHub
0

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.

feat(core): headless renderGLB via gltf-transform bridge

Matt Kissinger50660d66db7f
diff --git a/render.ts b/render.ts
new file mode 100644
index 00000000..eece03c6
--- /dev/null
+++ b/render.ts
@@ -0,0 +1,345 @@
+/**
+ * Headless Kiln GLB Renderer
+ *
+ * Takes Kiln-generated code and produces a binary GLB Buffer.
+ * Uses Three.js for scene construction (pure math, no WebGL) and
+ * @gltf-transform/core for serialization. No browser APIs required.
+ *
+ * Ported from scripts/export-glb.ts - same bridge pattern, exposed as a
+ * library function so core can be consumed by server/CLI without shelling
+ * out.
+ */
+
+import * as THREE from 'three';
+import { Document, NodeIO } from '@gltf-transform/core';
+
+import {
+ buildSandboxGlobals,
+ countTriangles,
+} from './primitives';
+
+// Gltf-transform type aliases for local readability.
+type GtNode = import('@gltf-transform/core').Node;
+type GtMesh = import('@gltf-transform/core').Mesh;
+type GtMaterial = import('@gltf-transform/core').Material;
+type GtBuffer = import('@gltf-transform/core').Buffer;
+
+// Accessor.Type is typed as Record<string, AccessorType> which runs afoul of
+// noUncheckedIndexedAccess. Use the literal strings directly - same values,
+// no type noise.
+type AccessorTypeStr = 'SCALAR' | 'VEC2' | 'VEC3' | 'VEC4' | 'MAT2' | 'MAT3' | 'MAT4';
+const TYPE_SCALAR: AccessorTypeStr = 'SCALAR';
+const TYPE_VEC2: AccessorTypeStr = 'VEC2';
+const TYPE_VEC3: AccessorTypeStr = 'VEC3';
+const TYPE_VEC4: AccessorTypeStr = 'VEC4';
+
+// =============================================================================
+// Execution
+// =============================================================================
+
+export interface KilnCodeMeta {
+ name?: string;
+ category?: string;
+ tris?: number;
+ [key: string]: unknown;
+}
+
+export interface ExecutedKilnCode {
+ meta: KilnCodeMeta;
+ root: THREE.Object3D;
+ clips: THREE.AnimationClip[];
+}
+
+/**
+ * Execute Kiln-generated code in a Function sandbox and return the built
+ * Three.js scene plus animation clips. Throws on any runtime error in the
+ * generated code.
+ */
+export function executeKilnCode(code: string): ExecutedKilnCode {
+ if (!code || typeof code !== 'string') {
+ throw new Error('executeKilnCode: code must be a non-empty string');
+ }
+
+ // Normalize line endings - Windows CRLF from LLM responses trips up the
+ // Function constructor.
+ const normalized = code.replace(/\r\n/g, '\n');
+
+ const globals = buildSandboxGlobals();
+ const globalNames = Object.keys(globals);
+ const globalValues = Object.values(globals);
+
+ const fn = new Function(
+ ...globalNames,
+ `${normalized}\nreturn { meta: typeof meta !== 'undefined' ? meta : {}, build, animate: typeof animate !== 'undefined' ? animate : null };`
+ );
+
+ const { meta, build, animate } = fn(...globalValues) as {
+ meta: KilnCodeMeta;
+ build: () => THREE.Object3D;
+ animate: ((root: THREE.Object3D) => THREE.AnimationClip[] | undefined) | null;
+ };
+
+ if (typeof build !== 'function') {
+ throw new Error('executeKilnCode: generated code did not define `build`');
+ }
+
+ const root = build();
+ if (!(root instanceof THREE.Object3D)) {
+ throw new Error('executeKilnCode: build() did not return a THREE.Object3D');
+ }
+
+ const clips = animate ? (animate(root) ?? []) : [];
+
+ return { meta: meta ?? {}, root, clips };
+}
+
+// =============================================================================
+// Three.js -> gltf-transform Bridge
+// =============================================================================
+
+function bridgeMaterial(
+ doc: Document,
+ threeMat: THREE.Material,
+ cache: Map<THREE.Material, GtMaterial>
+): GtMaterial {
+ const cached = cache.get(threeMat);
+ if (cached) return cached;
+
+ const mat = doc.createMaterial(threeMat.name || undefined);
+
+ if (threeMat instanceof THREE.MeshStandardMaterial) {
+ mat.setBaseColorFactor([threeMat.color.r, threeMat.color.g, threeMat.color.b, threeMat.opacity]);
+ mat.setRoughnessFactor(threeMat.roughness);
+ mat.setMetallicFactor(threeMat.metalness);
+ if (threeMat.emissive) {
+ mat.setEmissiveFactor([threeMat.emissive.r, threeMat.emissive.g, threeMat.emissive.b]);
+ }
+ if (threeMat.transparent) {
+ mat.setAlphaMode('BLEND');
+ }
+ if (threeMat.side === THREE.DoubleSide) {
+ mat.setDoubleSided(true);
+ }
+ } else if (threeMat instanceof THREE.MeshLambertMaterial) {
+ mat.setBaseColorFactor([threeMat.color.r, threeMat.color.g, threeMat.color.b, threeMat.opacity]);
+ mat.setRoughnessFactor(1.0);
+ mat.setMetallicFactor(0.0);
+ if (threeMat.emissive) {
+ mat.setEmissiveFactor([threeMat.emissive.r, threeMat.emissive.g, threeMat.emissive.b]);
+ }
+ } else if (threeMat instanceof THREE.MeshBasicMaterial) {
+ mat.setBaseColorFactor([threeMat.color.r, threeMat.color.g, threeMat.color.b, threeMat.opacity]);
+ mat.setRoughnessFactor(1.0);
+ mat.setMetallicFactor(0.0);
+ }
+
+ cache.set(threeMat, mat);
+ return mat;
+}
+
+function bridgeGeometry(
+ doc: Document,
+ buf: GtBuffer,
+ geometry: THREE.BufferGeometry,
+ material: GtMaterial,
+ meshName: string
+): GtMesh {
+ if (!geometry.getAttribute('normal')) {
+ geometry.computeVertexNormals();
+ }
+
+ const prim = doc.createPrimitive().setMaterial(material);
+
+ const posAttr = geometry.getAttribute('position') as THREE.BufferAttribute | undefined;
+ if (posAttr) {
+ prim.setAttribute(
+ 'POSITION',
+ doc.createAccessor(meshName + '_pos')
+ .setArray(new Float32Array(posAttr.array))
+ .setType(TYPE_VEC3)
+ .setBuffer(buf)
+ );
+ }
+
+ const normAttr = geometry.getAttribute('normal') as THREE.BufferAttribute | undefined;
+ if (normAttr) {
+ prim.setAttribute(
+ 'NORMAL',
+ doc.createAccessor(meshName + '_norm')
+ .setArray(new Float32Array(normAttr.array))
+ .setType(TYPE_VEC3)
+ .setBuffer(buf)
+ );
+ }
+
+ const uvAttr = geometry.getAttribute('uv') as THREE.BufferAttribute | undefined;
+ if (uvAttr) {
+ prim.setAttribute(
+ 'TEXCOORD_0',
+ doc.createAccessor(meshName + '_uv')
+ .setArray(new Float32Array(uvAttr.array))
+ .setType(TYPE_VEC2)
+ .setBuffer(buf)
+ );
+ }
+
+ const indexAttr = geometry.getIndex();
+ if (indexAttr) {
+ prim.setIndices(
+ doc.createAccessor(meshName + '_idx')
+ .setArray(new Uint16Array(indexAttr.array))
+ .setType(TYPE_SCALAR)
+ .setBuffer(buf)
+ );
+ }
+
+ return doc.createMesh(meshName).addPrimitive(prim);
+}
+
+function bridgeNode(
+ doc: Document,
+ buf: GtBuffer,
+ threeObj: THREE.Object3D,
+ matCache: Map<THREE.Material, GtMaterial>,
+ nodeMap: Map<string, GtNode>
+): GtNode {
+ const gtNode = doc.createNode(threeObj.name || undefined);
+
+ gtNode.setTranslation([threeObj.position.x, threeObj.position.y, threeObj.position.z]);
+ gtNode.setRotation([
+ threeObj.quaternion.x,
+ threeObj.quaternion.y,
+ threeObj.quaternion.z,
+ threeObj.quaternion.w,
+ ]);
+ gtNode.setScale([threeObj.scale.x, threeObj.scale.y, threeObj.scale.z]);
+
+ if (threeObj instanceof THREE.Mesh) {
+ const threeMat = threeObj.material as THREE.Material;
+ const gtMat = bridgeMaterial(doc, threeMat, matCache);
+ const gtMesh = bridgeGeometry(doc, buf, threeObj.geometry, gtMat, threeObj.name || 'mesh');
+ gtNode.setMesh(gtMesh);
+ }
+
+ if (threeObj.name) {
+ nodeMap.set(threeObj.name, gtNode);
+ }
+
+ for (const child of threeObj.children) {
+ const childNode = bridgeNode(doc, buf, child, matCache, nodeMap);
+ gtNode.addChild(childNode);
+ }
+
+ return gtNode;
+}
+
+function bridgeAnimations(
+ doc: Document,
+ buf: GtBuffer,
+ clips: THREE.AnimationClip[],
+ nodeMap: Map<string, GtNode>,
+ warnings: string[]
+): void {
+ for (const clip of clips) {
+ const anim = doc.createAnimation(clip.name);
+
+ for (const track of clip.tracks) {
+ const dotIdx = track.name.lastIndexOf('.');
+ const nodeName = track.name.substring(0, dotIdx);
+ const property = track.name.substring(dotIdx + 1);
+
+ const targetNode = nodeMap.get(nodeName);
+ if (!targetNode) {
+ warnings.push(`Animation target "${nodeName}" not found - skipped`);
+ continue;
+ }
+
+ let targetPath: 'translation' | 'rotation' | 'scale';
+ let valueType: AccessorTypeStr;
+ if (property === 'position') {
+ targetPath = 'translation';
+ valueType = TYPE_VEC3;
+ } else if (property === 'quaternion') {
+ targetPath = 'rotation';
+ valueType = TYPE_VEC4;
+ } else if (property === 'scale') {
+ targetPath = 'scale';
+ valueType = TYPE_VEC3;
+ } else {
+ warnings.push(`Unsupported animation property "${property}" on "${nodeName}" - skipped`);
+ continue;
+ }
+
+ const inputAcc = doc.createAccessor(clip.name + '_' + nodeName + '_input')
+ .setArray(new Float32Array(track.times))
+ .setType(TYPE_SCALAR)
+ .setBuffer(buf);
+
+ const outputAcc = doc.createAccessor(clip.name + '_' + nodeName + '_output')
+ .setArray(new Float32Array(track.values))
+ .setType(valueType)
+ .setBuffer(buf);
+
+ const sampler = doc.createAnimationSampler()
+ .setInput(inputAcc)
+ .setOutput(outputAcc)
+ .setInterpolation('LINEAR');
+
+ const channel = doc.createAnimationChannel()
+ .setTargetNode(targetNode)
+ .setTargetPath(targetPath)
+ .setSampler(sampler);
+
+ anim.addSampler(sampler);
+ anim.addChannel(channel);
+ }
+ }
+}
+
+// =============================================================================
+// Public API
+// =============================================================================
+
+export interface RenderResult {
+ glb: Buffer;
+ tris: number;
+ meta: KilnCodeMeta;
+ warnings: string[];
+}
+
+/**
+ * Execute Kiln code and serialize to a GLB Buffer.
+ *
+ * This is the main headless entry point. Takes the JS code Claude produces
+ * (the same string shape `/api/kiln/generate` returns) and returns ready-
+ * to-write bytes.
+ *
+ * Pure function: no file I/O, no globals, no WebGL.
+ */
+export async function renderGLB(code: string): Promise<RenderResult> {
+ const warnings: string[] = [];
+ const { meta, root, clips } = executeKilnCode(code);
+ const tris = countTriangles(root);
+
+ const doc = new Document();
+ const buf = doc.createBuffer();
+ const matCache = new Map<THREE.Material, GtMaterial>();
+ const nodeMap = new Map<string, GtNode>();
+
+ const rootNode = bridgeNode(doc, buf, root, matCache, nodeMap);
+ doc.createScene(meta.name || 'Scene').addChild(rootNode);
+
+ if (clips.length > 0) {
+ bridgeAnimations(doc, buf, clips, nodeMap, warnings);
+ }
+
+ const io = new NodeIO();
+ const glbBytes = await io.writeBinary(doc);
+
+ return {
+ glb: Buffer.from(glbBytes),
+ tris,
+ meta: { ...meta, tris },
+ warnings,
+ };
+}