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.

Serve the text-to-3D mech as one skinned mesh (#340)

Matthew Kissinger40acd50b2e47
diff --git a/site/scripts/stage-text-to-3d.mjs b/site/scripts/stage-text-to-3d.mjs
index 5536d30f..98342922 100644
--- a/site/scripts/stage-text-to-3d.mjs
+++ b/site/scripts/stage-text-to-3d.mjs
@@ -18,7 +18,8 @@ import { ASSET_BASE } from './mirror-core.mjs';
*
* <directory>/tools.json names the tools. <directory>/<subject>/ is a workspace holding `assets/`, `run/` with the prompts and
* `steps.json`, which lists each step's prompt and saved revision, and `renders/manifest.json` from
- * scripts/render-text-to-3d.mjs.
+ * scripts/render-text-to-3d.mjs. A steps.json may name a `delivery` file, `{ file, madeFrom: "sha256:…" }`, to serve
+ * in place of the final revision's GLB (see `checkDelivery`); the page's claims are still checked on the revisions.
*
* Images and programs are committed under public/text-to-3d/. The two GLBs go to the asset host, the way Golden Gate's do:
* each is named by its own hash, pinned in the mirror manifest and copied to .localdata/asset-delivery with an upload list
@@ -128,6 +129,32 @@ export function clipDigests(bytes) {
return new Map((gltf.animations ?? []).map((clip) => [clip.name, digest(clip)]));
}
+/**
+ * The file a subject is served as, when its steps.json names one in `delivery`: a form of the final revision made for
+ * playing, such as its parts merged into one skinned mesh. It is served only if it was made from that revision's exact
+ * bytes and keeps what code reaches for: every named node that is not a mesh part, with its notes, every clip's
+ * targets and keyframes, the material names and the triangle count. Returns the delivery file.
+ */
+export function checkDelivery(saved, delivered, madeFrom) {
+ if (madeFrom !== `sha256:${hash(saved)}`) throw Error(`the delivery file was made from ${madeFrom}, not the final revision; make it again`);
+ const before = openGlb(saved).gltf;
+ const after = openGlb(delivered).gltf;
+ const named = new Map(after.nodes.map((node) => [node.name, node]));
+ for (const node of before.nodes) {
+ if (node.mesh !== undefined) continue;
+ const kept = named.get(node.name);
+ if (!kept) throw Error(`the delivery file has no ${node.name}`);
+ if (JSON.stringify(kept.extras ?? null) !== JSON.stringify(node.extras ?? null)) throw Error(`the delivery file changes the notes on ${node.name}`);
+ }
+ const clips = clipDigests(delivered);
+ for (const [name, digest] of clipDigests(saved)) if (clips.get(name) !== digest) throw Error(`the delivery file's ${name} clip differs`);
+ const materials = (gltf) => JSON.stringify((gltf.materials ?? []).map(({ name }) => name).sort());
+ if (materials(before) !== materials(after)) throw Error('the delivery file has other materials');
+ const [was, now] = [inspectGlb(saved).triangles, inspectGlb(delivered).triangles];
+ if (was !== now) throw Error(`the delivery file has ${now} triangles, not ${was}`);
+ return delivered;
+}
+
/** Every mesh node in a GLB by name, with where it sits in the world and a digest of its vertex positions alone: baked colours may share the buffer. */
function placedMeshes(bytes) {
const { gltf } = openGlb(bytes);
@@ -314,19 +341,22 @@ async function stageSubject(run, subject) {
images[render.name] = { name, width: meta.width, height: meta.height, alt: render.alt, ...(render.caption ? { caption: render.caption } : {}), variants };
}
const diffAt = plan.steps.findIndex((step) => step.id === plan.diffStep);
- const { triangles } = inspectGlb(glbs.at(-1));
+ const model = plan.delivery
+ ? checkDelivery(glbs.at(-1), await readFile(join(directory, plan.delivery.file)), plan.delivery.madeFrom)
+ : glbs.at(-1);
+ const { triangles } = inspectGlb(model);
return {
record: {
id: subject,
name: plan.name,
- glbBytes: glbs.at(-1).length,
+ glbBytes: model.length,
triangles,
steps,
...(plan.diffStep ? { diff: await unifiedDiff(sources[diffAt - 1], sources[diffAt], steps[diffAt - 1].sourceFile, steps[diffAt].sourceFile) } : {}),
images,
},
files,
- model: glbs.at(-1),
+ model,
hero: renders.find(({ name }) => name === 'hero'),
finalRevision: plan.steps.at(-1).revision,
};
diff --git a/site/scripts/text-to-3d-delivery.test.ts b/site/scripts/text-to-3d-delivery.test.ts
new file mode 100644
index 00000000..6808d41e
--- /dev/null
+++ b/site/scripts/text-to-3d-delivery.test.ts
@@ -0,0 +1,98 @@
+import { expect, test } from 'bun:test';
+import { createHash } from 'node:crypto';
+import { checkDelivery } from './stage-text-to-3d.mjs';
+
+/** A GLB from a glTF record and its one binary chunk. */
+function glb(gltf: object, bin: Buffer): Buffer {
+ const pad = (bytes: Buffer, fill: number) => Buffer.concat([bytes, Buffer.alloc((4 - (bytes.length % 4)) % 4, fill)]);
+ const json = pad(Buffer.from(JSON.stringify(gltf)), 0x20);
+ const body = pad(bin, 0);
+ const chunk = (bytes: Buffer, type: number) => {
+ const head = Buffer.alloc(8);
+ head.writeUInt32LE(bytes.length, 0);
+ head.writeUInt32LE(type, 4);
+ return Buffer.concat([head, bytes]);
+ };
+ const header = Buffer.alloc(12);
+ header.writeUInt32LE(0x46546c67, 0);
+ header.writeUInt32LE(2, 4);
+ header.writeUInt32LE(12 + 8 + json.length + 8 + body.length, 8);
+ return Buffer.concat([header, chunk(json, 0x4e4f534a), chunk(body, 0x004e4942)]);
+}
+
+/** One triangle under a joint that a clip turns: as saved, with a mesh node per part, or delivered skinned. */
+function model({ skinned = false, joint = 'Joint_Knee', turn = 0.5, triangles = 1, aimAt = 'Joint_Hip' } = {}) {
+ const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);
+ const indices = new Uint16Array(Array.from({ length: triangles * 3 }, (_, i) => i % 3));
+ const times = new Float32Array([0, 1]);
+ const turns = new Float32Array([0, 0, 0, 1, 0, Math.sin(turn / 2), 0, Math.cos(turn / 2)]);
+ const parts = [positions, indices, times, turns].map((array) => Buffer.from(array.buffer));
+ const views = [];
+ let offset = 0;
+ for (const part of parts) {
+ views.push({ buffer: 0, byteOffset: offset, byteLength: part.length });
+ offset += part.length + ((4 - (part.length % 4)) % 4);
+ }
+ const bin = Buffer.concat(parts.map((part) => Buffer.concat([part, Buffer.alloc((4 - (part.length % 4)) % 4)])));
+ const nodes = skinned
+ ? [
+ { name: 'Walker', children: [1, 2, 3], extras: { rig: { clip: 'Walk' } } },
+ { name: 'Joint_Hip' },
+ { name: joint, extras: { aimAt } },
+ { name: 'Walker_Body', mesh: 0 },
+ ]
+ : [
+ { name: 'Walker', children: [1, 2], extras: { rig: { clip: 'Walk' } } },
+ { name: 'Joint_Hip' },
+ { name: joint, extras: { aimAt }, children: [3] },
+ { name: 'Mesh_Shin_steel', mesh: 0 },
+ ];
+ return glb(
+ {
+ asset: { version: '2.0' },
+ scene: 0,
+ scenes: [{ nodes: [0] }],
+ nodes,
+ meshes: [{ primitives: [{ attributes: { POSITION: 0 }, indices: 1, material: 0 }] }],
+ materials: [{ name: 'Gunmetal' }],
+ buffers: [{ byteLength: bin.length }],
+ bufferViews: views,
+ accessors: [
+ { bufferView: 0, componentType: 5126, count: 3, type: 'VEC3', min: [0, 0, 0], max: [1, 1, 0] },
+ { bufferView: 1, componentType: 5123, count: triangles * 3, type: 'SCALAR' },
+ { bufferView: 2, componentType: 5126, count: 2, type: 'SCALAR', min: [0], max: [1] },
+ { bufferView: 3, componentType: 5126, count: 2, type: 'VEC4' },
+ ],
+ animations: [{ name: 'Walk', samplers: [{ input: 2, output: 3 }], channels: [{ sampler: 0, target: { node: 2, path: 'rotation' } }] }],
+ },
+ bin,
+ );
+}
+
+const saved = model();
+const madeFrom = `sha256:${createHash('sha256').update(saved).digest('hex')}`;
+
+test('a delivery file made from the final revision, with its joints, notes, clips, materials and triangles, is served', () => {
+ const delivered = model({ skinned: true });
+ expect(checkDelivery(saved, delivered, madeFrom)).toBe(delivered);
+});
+
+test('a delivery file made from another revision is refused', () => {
+ expect(() => checkDelivery(saved, model({ skinned: true }), 'sha256:0000')).toThrow(/made from/);
+});
+
+test('a delivery file that loses or renames a named node is refused', () => {
+ expect(() => checkDelivery(saved, model({ skinned: true, joint: 'Joint_Shin' }), madeFrom)).toThrow(/Joint_Knee/);
+});
+
+test('a delivery file whose node notes differ is refused', () => {
+ expect(() => checkDelivery(saved, model({ skinned: true, aimAt: 'Joint_Toe' }), madeFrom)).toThrow(/notes/);
+});
+
+test('a delivery file whose clip moves differently is refused', () => {
+ expect(() => checkDelivery(saved, model({ skinned: true, turn: 0.6 }), madeFrom)).toThrow(/Walk/);
+});
+
+test('a delivery file with other triangles is refused', () => {
+ expect(() => checkDelivery(saved, model({ skinned: true, triangles: 2 }), madeFrom)).toThrow(/triangles/);
+});
diff --git a/site/src/data/commons-build.json b/site/src/data/commons-build.json
index f16854eb..6fb86742 100644
--- a/site/src/data/commons-build.json
+++ b/site/src/data/commons-build.json
@@ -190,7 +190,7 @@
"packs/holodeck/objects/3f2c302d07380e20fafe794a56f7f3b7ca0ecc886529accb6fee7eed0bece6f4/meridian-cube-mirror-far.glb",
"packs/holodeck/objects/bc5ba472b23aa8aa40b3bcf84d4f83284617f6480858b7955d9bcb47fad9684b/meridian-statue-figure.glb",
"packs/holodeck/objects/fc54ddf1a7d1f0d70b8c6b414758ed7ddd690c6650d7053e1506ab95c94ccd94/meridian-statue-whale.glb",
- "text-to-3d/9b1b004c460cc0a9/mech.glb",
+ "text-to-3d/c4100fecfc3b94ea/mech.glb",
"text-to-3d/6f1ee50efb3aa2ab/cathedral.glb"
],
"images": [
diff --git a/site/src/data/mirror-manifest.json b/site/src/data/mirror-manifest.json
index cc57ed02..1914026d 100644
--- a/site/src/data/mirror-manifest.json
+++ b/site/src/data/mirror-manifest.json
@@ -6537,9 +6537,9 @@
"sha256": "fcb5b7444adbd92180e943b0207ef923c222134c340b5cda4f281494d352f71d"
},
{
- "path": "text-to-3d/9b1b004c460cc0a9/mech.glb",
- "bytes": 4777424,
- "sha256": "9b1b004c460cc0a924f6a4d2d218a2d292ebbd507add1653cfc5152799d96df5"
+ "path": "text-to-3d/c4100fecfc3b94ea/mech.glb",
+ "bytes": 5541308,
+ "sha256": "c4100fecfc3b94ea2f4de76f34b11b191abcddee22b1fdc714f06de624df221b"
},
{
"path": "text-to-3d/6f1ee50efb3aa2ab/cathedral.glb",
diff --git a/site/src/data/text-to-3d.json b/site/src/data/text-to-3d.json
index 0520deed..119b640f 100644
--- a/site/src/data/text-to-3d.json
+++ b/site/src/data/text-to-3d.json
@@ -10,7 +10,7 @@
{
"id": "mech",
"name": "Heavy Walker Mech",
- "glbBytes": 4777424,
+ "glbBytes": 5541308,
"triangles": 68604,
"steps": [
{
@@ -82,7 +82,7 @@
"srcsetAvif": "/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.avif 480w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-768.avif 768w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.avif 1024w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.avif 1168w"
}
},
- "glbHref": "https://assets.kilnstudio.tools/text-to-3d/9b1b004c460cc0a9/mech.glb"
+ "glbHref": "https://assets.kilnstudio.tools/text-to-3d/c4100fecfc3b94ea/mech.glb"
},
{
"id": "cathedral",