Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Teach the build-67js-scene skill mesh modeling

Nguyen Xuan An4a89560b4eed
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 20794406..f2ec867f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Unreleased
+- Teach the `build-67js-scene` skill mesh modeling: a workflow, face and edge rules, tested recipes (crate, pillar, drum, bracket, bench row, planter, soap) and an error table; a test builds every recipe in the reference and requires a clean lint
- Shrink the MCP server bundle from 1,072 KB to 590 KB by keeping the scene engine out of it: capabilities and default limits now come from plain-data modules (`agent-scene-capabilities.js`, `mesh-operations.js`), and the MCP build fails if engine code returns; the npm archive drops from 1,003 KB to 887 KB
- Add `67js/addons/mesh`, a polygon mesh core for agent modeling: compact `MeshData` with attributes on point, edge, face and corner domains, a half-edge `EditMesh` with typed-array disk and radial cycles, `validateTopology`, conversion from renderer geometry with welding, quads and axis face tags, hard-edge corner normals, n-gon triangulation, render extraction and `lintMesh`
- Add semantic face selection and mesh edits (extrude region and individual, inset region and individual, delete, flip normals, translate with proportional falloff, merge by distance, tag) that tag the faces they create
diff --git a/skills/build-67js-scene/SKILL.md b/skills/build-67js-scene/SKILL.md
index 112d33e9..b6a5e2b5 100644
--- a/skills/build-67js-scene/SKILL.md
+++ b/skills/build-67js-scene/SKILL.md
@@ -35,6 +35,7 @@ Use `simplify-code` and `engineering-workflows` for implementation.
- Static vertex wrapping uses optional `src/shrinkwrap.js` and the checkout's `docs/shrinkwrap.md`.
- A rigid accessory on a morphing or skinned surface uses optional `src/surface-attachment.js` and `docs/surface-attachment.md`.
- Agent scene recipes use optional `src/agent-scene.js` and `docs/agent-scene.md`. Verify source versus published availability before importing.
+- Polygon modeling by an agent (extrude, inset, loop cut, bevel, booleans, mirror, array, solidify) uses the recipe `mesh` geometry and the MCP tools `mesh_inspect`, `mesh_edit` and `mesh_stack`. Read [mesh-modeling.md](references/mesh-modeling.md) before the first edit; check that the checkout's `docs/mesh.md` exists, since it is unreleased after npm 0.10.0.
- Agent tool calls use the installed `67js-mcp` stdio server from release `0.5.0` onward. Read `docs/mcp.md` for launch and PNG feedback.
- Agent render jobs use `docs/agent-render-jobs.md`. Check source versus published support, configure a private store and poll until terminal.
- Agent procedural animation uses `docs/agent-motion.md` from release `0.7.0`. Author recipe tracks, then seek through `sampleMotion` or MCP `motionTime`.
diff --git a/skills/build-67js-scene/references/mesh-modeling.md b/skills/build-67js-scene/references/mesh-modeling.md
new file mode 100644
index 00000000..650749d6
--- /dev/null
+++ b/skills/build-67js-scene/references/mesh-modeling.md
@@ -0,0 +1,188 @@
+# Mesh modeling for agents
+
+<!-- skill-navigation:start -->
+This is the [mesh modeling guide](#mesh-modeling-for-agents). Read [Workflow](#workflow) and
+[Choosing faces and edges](#choosing-faces-and-edges) completely before the first edit. Read the
+[Recipes](#recipes) entry closest to the shape you are building, and [Errors and fixes](#errors-and-fixes) when a
+call fails. The full contract (every op, field and default) is the checkout's `docs/mesh.md`; `docs/mcp.md` lists
+the tools.
+<!-- skill-navigation:end -->
+
+Every `json` block in this file is a complete recipe geometry. `tests/skill-mesh-recipes.test.js` builds each one and
+requires a clean lint, so keep them valid when the API changes.
+
+## Workflow
+
+1. Name the parts and their rough sizes first. One part is one `mesh` geometry: a `base` primitive, an `edits` log and
+ an optional `stack`. Separate parts (a lid, a handle) are separate geometries placed by nodes.
+2. Start from the closest base. `box` is a unit cube from -0.5 to 0.5 (`bevel: 0` for crisp edits); `cylinder` has
+ radius 1 and height 1 centred on the origin (`bevel: 0`); `sphere` has radius 1; `lathe` turns a profile;
+ `extrude` makes outlines with holes. Set proportions first by translating tagged faces, so later widths and offsets
+ stay in real units: a 0.5 x 0.6 x 1.0 box is `right` by -0.25, `left` by 0.25, `top` by -0.4.
+3. Add edits in small batches with `mesh_edit` (or in the recipe). Name every edit (`name: "lid"` gives `lid.cap`,
+ `lid.side`); an unnamed one gets `<op>-<index>`, which is hard to predict. Select faces by tags, never by index.
+4. After each batch read the `mesh_edit` result: `edits[i].selected`, `tags` and `edges` say what each edit touched;
+ `problems` lists lint errors with a fix. `ok: false`, a lower count than expected, or `clamped: true` on a bevel
+ means stop and correct before adding more.
+5. Look at it with `scene_look` (or `scene_review` for several angles). Lint proves the mesh is valid, not that it
+ looks right.
+6. Put symmetry, repetition and wall thickness in the `stack` (`mirror`, `array`, `solidify`, `bevel`) so they stay
+ adjustable. `mesh_stack` with `apply: n` bakes the first nodes into the edit log when later edits need them.
+7. Bevel big shaping edges (an arched top, rounded corners of the body) before insets and extrusions near them:
+ those edits put untagged rim and side faces next to the edge, so a later `between` rule misses it or the narrow
+ rim clamps the width. Small finishing bevels go last, selected with `boundaryOf` or `angle`.
+
+Tag rules worth knowing: caps keep the tags their faces had (an inset on `right` gives a cap tagged `right` and
+`flag.cap`); rim, side, strip and corner faces start with only their own new tag.
+
+Outside MCP, the same recipe runs in Node:
+
+```js
+import { createAgentScene } from './src/agent-scene.js'; // or '@codepawlhq/67js/addons/agent-scene' once released
+const scene = createAgentScene({
+ format: '67js.scene', schemaVersion: 1,
+ geometries: [mesh], materials: [{ id: 'paint', color: [0.6, 0.6, 0.6] }],
+ nodes: [{ id: 'part', geometry: mesh.id, material: 'paint' }],
+});
+const [report] = scene.inspectMeshes().meshes; // { ok, counts, faces, parts, tags, bounds, problems, edits, stack }
+```
+
+Booleans first need `await loadMeshBooleans()` from the same module (and the `manifold-3d` peer installed).
+
+Use checkpoints before risky batches. A failed `mesh_edit` changes nothing, so retrying with a corrected edit is safe.
+
+## Choosing faces and edges
+
+Faces carry tags. A base gets `top`, `bottom`, `side`, `left` (-X), `right` (+X), `front` (+Z), `back` (-Z).
+Edits add `<name>.cap`, `<name>.side`, `<name>.rim`, `<name>.cut`, `<name>.band-<k>`, `<name>.strip`, and so on.
+`mesh_inspect` lists every tag with its face count.
+
+| Need | Face rule |
+| --- | --- |
+| A tagged region | `{ "tag": "top" }` |
+| Faces facing a way | `{ "normal": [0, 1, 0], "maxAngle": 10 }` |
+| Faces in a box | `{ "inside": { "min": [-1, 0.4, -1], "max": [1, 1, 1] } }` |
+| Combinations | `{ "and": [...] }`, `{ "or": [...] }`, `{ "not": rule }` |
+| A ring around a region | `{ "grow": { "tag": "top" }, "steps": 1 }` |
+
+Edge rules (for `bevel` and `loop_cut`):
+
+| Need | Edge rule |
+| --- | --- |
+| The rim between two regions | `{ "between": [{ "tag": "top" }, { "tag": "side" }] }` |
+| Only some of that rim | `{ "between": [{ "tag": "top" }, { "or": [{ "tag": "left" }, { "tag": "right" }] }] }` |
+| Every sharp edge | `{ "angle": 30 }` |
+| The border of a region | `{ "boundaryOf": { "tag": "lid.cap" } }` |
+| Edges parallel to an axis (loop cut rings) | `{ "along": [0, 1, 0] }` |
+
+Choosing the extrude: `extrude_face_region` moves each connected group along its average normal;
+`extrude_individual` moves each face along its own; `extrude_face_region` with `alongNormals: true` keeps a ring or
+curved band joined (a hoop around a cylinder). Faces facing opposite ways in one group need one of the last two.
+
+## Recipes
+
+A crate with recessed panels and a raised lid:
+
+```json
+{ "id": "crate", "type": "mesh", "base": { "type": "box", "bevel": 0 }, "edits": [
+ { "op": "inset_individual", "select": { "tag": "side" }, "thickness": 0.07, "name": "panel" },
+ { "op": "extrude_individual", "select": { "tag": "panel.cap" }, "offset": -0.04 },
+ { "op": "inset_region", "select": { "tag": "top" }, "thickness": 0.08, "name": "lid" },
+ { "op": "extrude_face_region", "select": { "tag": "lid.cap" }, "offset": 0.06 }
+] }
+```
+
+A mailbox: proportions first, the arched top bevelled before anything touches its edges, a loop cut to carve a
+vertical strip out of the side, then a door and a flag built by chaining extrusions (each edit selects faces the
+previous one created):
+
+```json
+{ "id": "mailbox", "type": "mesh", "base": { "type": "box", "bevel": 0 }, "edits": [
+ { "op": "translate", "select": { "tag": "right" }, "offset": [-0.25, 0, 0] },
+ { "op": "translate", "select": { "tag": "left" }, "offset": [0.25, 0, 0] },
+ { "op": "translate", "select": { "tag": "top" }, "offset": [0, -0.4, 0] },
+ { "op": "bevel", "name": "arch", "edges": { "between": [{ "tag": "top" }, { "or": [{ "tag": "left" }, { "tag": "right" }] }] }, "width": 0.2, "segments": 5 },
+ { "op": "loop_cut", "name": "slice", "edges": { "along": [0, 0, 1] }, "cuts": 5 },
+ { "op": "inset_region", "select": { "tag": "front" }, "thickness": 0.07, "name": "door" },
+ { "op": "extrude_face_region", "select": { "tag": "door.cap" }, "offset": -0.03, "name": "recess" },
+ { "op": "inset_region", "select": { "and": [{ "tag": "right" }, { "tag": "slice.band-1" }] }, "thickness": 0.03, "name": "flagbase" },
+ { "op": "extrude_face_region", "select": { "tag": "flagbase.cap" }, "offset": 0.04, "name": "arm" },
+ { "op": "extrude_face_region", "select": { "and": [{ "tag": "arm.side" }, { "normal": [0, 1, 0], "maxAngle": 10 }] }, "offset": 0.35, "name": "pole" },
+ { "op": "extrude_face_region", "select": { "and": [{ "tag": "pole.side" }, { "normal": [0, 0, 1], "maxAngle": 10 }] }, "offset": 0.14, "name": "flag" }
+] }
+```
+
+A pillar with raised bands (loop cut, then extrude every other band):
+
+```json
+{ "id": "pillar", "type": "mesh", "base": { "type": "box", "bevel": 0 }, "edits": [
+ { "op": "translate", "select": { "tag": "top" }, "offset": [0, 2, 0] },
+ { "op": "loop_cut", "edges": { "along": [0, 1, 0] }, "cuts": 5, "name": "ring" },
+ { "op": "extrude_face_region", "select": { "or": [{ "tag": "ring.band-1" }, { "tag": "ring.band-3" }] }, "offset": 0.05, "alongNormals": true }
+] }
+```
+
+A drum with a hoop:
+
+```json
+{ "id": "drum", "type": "mesh", "base": { "type": "cylinder", "segments": 32, "bevel": 0 }, "edits": [
+ { "op": "loop_cut", "edges": { "along": [0, 1, 0] }, "cuts": 2, "name": "hoop" },
+ { "op": "extrude_face_region", "select": { "tag": "hoop.band-1" }, "offset": 0.04, "alongNormals": true }
+] }
+```
+
+A bracket with a boss and a through bore (booleans need the optional `manifold-3d` peer):
+
+```json
+{ "id": "bracket", "type": "mesh", "base": { "type": "box", "bevel": 0 }, "edits": [
+ { "op": "translate", "select": { "tag": "top" }, "offset": [0, -0.5, 0] },
+ { "op": "boolean", "operation": "union", "name": "boss", "cutter": { "type": "cylinder", "segments": 48, "bevel": 0 }, "translation": [0, 0.15, 0], "scale": [0.42, 0.3, 0.42] },
+ { "op": "boolean", "operation": "difference", "name": "bore", "cutter": { "type": "cylinder", "segments": 48, "bevel": 0 }, "scale": [0.28, 2, 0.28] }
+] }
+```
+
+A bench row: model half a bench, mirror it, array the result:
+
+```json
+{ "id": "bench", "type": "mesh", "base": { "type": "box", "bevel": 0 }, "edits": [
+ { "op": "translate", "offset": [0.5, 0, 0] },
+ { "op": "translate", "select": { "tag": "top" }, "offset": [0, -0.8, 0] },
+ { "op": "translate", "select": { "tag": "right" }, "offset": [0.4, 0, 0] },
+ { "op": "inset_region", "select": { "tag": "bottom" }, "thickness": 0.05, "name": "underside" },
+ { "op": "extrude_face_region", "select": { "and": [{ "tag": "underside.rim" }, { "inside": { "min": [1.2, -1, -1], "max": [2, 1, 1] } }] }, "offset": 0.6 }
+], "stack": [{ "op": "mirror" }, { "op": "array", "count": 3, "offset": [0, 0, -1.4], "relativeOffset": [0, 0, 0] }] }
+```
+
+A planter (open box with walls) and a rounded soap bar:
+
+```json
+{ "id": "planter", "type": "mesh", "base": { "type": "box", "bevel": 0 }, "edits": [
+ { "op": "delete", "select": { "tag": "top" } }
+], "stack": [{ "op": "solidify", "thickness": 0.06 }] }
+```
+
+```json
+{ "id": "soap", "type": "mesh", "base": { "type": "box", "bevel": 0 }, "edits": [
+ { "op": "translate", "select": { "tag": "top" }, "offset": [0, -0.4, 0] },
+ { "op": "bevel", "edges": { "all": true }, "width": 0.12, "segments": 4 }
+] }
+```
+
+## Errors and fixes
+
+| Message contains | Do |
+| --- | --- |
+| `Unknown tag x. Known tags: ...` | Pick a listed tag; earlier edits may have used another name |
+| `selects no faces` / `selects no edges` | Loosen the rule (`maxAngle`, box) or check tags with `mesh_inspect` |
+| `average normal cancels out` | Use `alongNormals: true`, an `_individual` op, or a `direction` |
+| `must be closed for a boolean` | Fix the open or non-manifold edges it counts, or `merge_by_distance` first |
+| `need the optional peer dependency manifold-3d` | Booleans are unavailable here; model the cut another way or ask for the peer |
+| `rings cross` | Cut each direction in its own `loop_cut` edit |
+| bevel `edges` lower than expected | An earlier inset or extrude left untagged rim faces at the edge; bevel earlier, or widen the rule |
+| bevel `clamped: true` | A neighbouring face is narrower than the width; bevel before the narrow inset, or use a smaller width |
+| `over the limit of N vertices` | Fewer segments, fewer array copies, or split the part |
+| lint `inverted-part` | `flip_normals` on that part |
+| lint `degenerate-face` | An offset or width of 0, or a clamp at the edge length; change the value |
+
+Bevel with 2 segments on a right angle renders faceted at the default `smoothAngle` of 30; use 3 or more segments
+or raise `smoothAngle` on the geometry.
diff --git a/tests/skill-mesh-recipes.test.js b/tests/skill-mesh-recipes.test.js
new file mode 100644
index 00000000..f6951e17
--- /dev/null
+++ b/tests/skill-mesh-recipes.test.js
@@ -0,0 +1,31 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { readFile } from 'node:fs/promises';
+import { createAgentScene, loadMeshBooleans, needsMeshBooleans } from '../src/agent-scene.js';
+
+// The build-67js-scene skill teaches agents mesh modeling by example. Every json block in its reference must keep
+// building a clean mesh, so an API change that breaks an example fails here instead of misleading an agent.
+const referenceUrl = new URL('../skills/build-67js-scene/references/mesh-modeling.md', import.meta.url);
+const reference = await readFile(referenceUrl, 'utf8');
+const geometries = [...reference.matchAll(/```json\r?\n([\s\S]*?)```/g)].map(match => JSON.parse(match[1]));
+
+test('the skill reference has mesh recipes to check', () => {
+ assert.ok(geometries.length >= 6);
+ for (const geometry of geometries) assert.equal(geometry.type, 'mesh');
+});
+
+for (const geometry of geometries) {
+ test(`skill recipe "${geometry.id}" builds a clean mesh`, async () => {
+ if (needsMeshBooleans(geometry)) await loadMeshBooleans();
+ const scene = createAgentScene({
+ format: '67js.scene', schemaVersion: 1,
+ geometries: [geometry],
+ materials: [{ id: 'grey', color: [0.6, 0.6, 0.6] }],
+ nodes: [{ id: geometry.id, geometry: geometry.id, material: 'grey' }],
+ });
+ const [report] = scene.inspectMeshes().meshes;
+ assert.equal(report.ok, true, JSON.stringify(report.problems));
+ assert.deepEqual(report.problems, []);
+ assert.equal(report.parts.closed, report.parts.count, 'every part is closed');
+ });
+}