Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat: three-stage mesh simplification in readme-3d (prune, quadric, sloppy fallback)
fudjiaef1e5994794d
diff --git a/packages/readme-3d/CHANGELOG.md b/packages/readme-3d/CHANGELOG.mdindex e05c06265..2eb9e4b57 100644--- a/packages/readme-3d/CHANGELOG.md+++ b/packages/readme-3d/CHANGELOG.md@@ -5,7 +5,7 @@Initial release.- Convert GLB, glTF (incl. Draco and meshopt compression), OBJ, binary STL, and ASCII STL into GitHub-renderable ASCII STL markdown blocks.-- Mesh welding + simplification (meshoptimizer) to a `--facets` or `--budget` target.+- Mesh welding + three-stage simplification (meshoptimizer) to a `--facets` or `--budget` target: floater pruning, quadric simplification, and a topology-ignoring sloppy fallback that guarantees the target on disconnected meshes.- Coordinate normalization, Y-up → Z-up correction, precision trimming.- `readme-3d check` - size-check any markdown file against GitHub's 512 KB render limit.- Library API: `convert`, `toMarkdown`, `checkMarkdownFile`, plus geometry/parser primitives.diff --git a/packages/readme-3d/README.md b/packages/readme-3d/README.mdindex e0cfe493a..efcb442c7 100644--- a/packages/readme-3d/README.md+++ b/packages/readme-3d/README.md@@ -4248,7 +4248,7 @@ endsolid cubeTwo things make this awkward to use by hand, and `readme-3d` fixes both:1. **Real models aren't ASCII STL.** They're GLBs, binary STLs, OBJs - often with hundreds of thousands of triangles. `readme-3d` parses all of these (including Draco- and meshopt-compressed glTF), bakes node transforms, and re-serializes to the exact ASCII dialect GitHub renders.-2. **ASCII STL is huge and GitHub stops rendering markdown files over 512 KB.** `readme-3d` welds and simplifies the mesh with [meshoptimizer](https://github.com/zeux/meshoptimizer) down to a facet or byte budget, normalizes coordinates to a compact range, and trims float precision - a 10 MB GLB becomes a ~150 KB block that still looks like the model.+2. **ASCII STL is huge and GitHub stops rendering markdown files over 512 KB.** `readme-3d` welds the mesh, prunes tiny disconnected debris, and simplifies with [meshoptimizer](https://github.com/zeux/meshoptimizer) down to a facet or byte budget (quadric simplification first, with a topology-ignoring sloppy pass as fallback so the budget is always reached), then normalizes coordinates to a compact range and trims float precision - a 10 MB GLB becomes a ~150 KB block that still looks like the model.It also rotates Y-up assets (glTF, OBJ) into the Z-up convention STL viewers expect, so your model stands upright instead of lying on its face.diff --git a/packages/readme-3d/docs/tutorials/02-convert-a-glb.md b/packages/readme-3d/docs/tutorials/02-convert-a-glb.mdindex 00db138f6..a35b57878 100644--- a/packages/readme-3d/docs/tutorials/02-convert-a-glb.md+++ b/packages/readme-3d/docs/tutorials/02-convert-a-glb.md@@ -50,7 +50,7 @@ npx readme-3d check README.md## 4. What the converter actually does1. **Parses** GLB/glTF (including Draco- and meshopt-compressed), OBJ, or binary/ASCII STL into a triangle soup, baking every node transform.-2. **Welds** duplicate vertices and **simplifies** with [meshoptimizer](https://github.com/zeux/meshoptimizer)'s quadric simplifier, progressively loosening the error tolerance until it hits your target - this is what tames disconnected AI-generated meshes.+2. **Welds** duplicate vertices, **prunes** tiny disconnected floaters (typical AI-mesh debris), and **simplifies** with [meshoptimizer](https://github.com/zeux/meshoptimizer): the quadric simplifier runs first with a progressively loosened error tolerance, and a topology-ignoring sloppy pass finishes the job when a heavily-disconnected mesh will not collapse cleanly - so your `--facets`/`--budget` target is always reached.3. **Rotates** Y-up sources to STL's Z-up convention, **normalizes** to a compact 100-unit scale, grounds the model at z=0, and centers it.4. **Serializes** ASCII STL with fixed 2-decimal precision and recomputed facet normals.diff --git a/packages/readme-3d/src/geometry.js b/packages/readme-3d/src/geometry.jsindex dabf7599f..c8c95bc1c 100644--- a/packages/readme-3d/src/geometry.js+++ b/packages/readme-3d/src/geometry.js@@ -52,10 +52,16 @@ export function normalize(triangles, { size = 100 } = {}) {}/**- * Weld exact-duplicate vertices into an indexed mesh, run meshoptimizer's- * simplifier down to ~targetFacets, and return a triangle soup again.- * The error tolerance is loosened progressively when the simplifier stalls,- * which is what lets heavily-disconnected AI-generated meshes collapse.+ * Weld exact-duplicate vertices into an indexed mesh, simplify down to+ * ~targetFacets with meshoptimizer, and return a triangle soup again.+ *+ * Three stages, each only if the previous one left us over target:+ * 1. prune - drop tiny disconnected floaters (AI meshes are full of them)+ * so the facet budget goes to the silhouette;+ * 2. quality - topology-preserving quadric simplification with a+ * progressively loosened error tolerance;+ * 3. sloppy - topology-ignoring pass that always reaches the target, for+ * heavily-disconnected meshes the quality pass cannot collapse.*/export async function simplifyTriangles(triangles, targetFacets) {if (triangles.length <= targetFacets) return triangles;@@ -79,8 +85,18 @@ export async function simplifyTriangles(triangles, targetFacets) {}const pos = new Float32Array(positions);+ // 1. prune: components smaller than ~2% of the model scale are visual+ // noise at README size. Skip the result if pruning ate a big share of the+ // mesh - then the "floaters" ARE the model (e.g. a particle swarm).+ const pruned = MeshoptSimplifier.simplifyPrune(indices, pos, 3, 0.02);+ if (pruned.length >= indices.length * 0.5 && pruned.length > 0) {+ indices = pruned;+ }++ // 2. quality: loosen the tolerance when the simplifier stalls, but stay+ // in a range where the shape survives.let error = 1e-4;- while (indices.length / 3 > targetFacets && error <= 0.5) {+ while (indices.length / 3 > targetFacets && error <= 0.25) {const [next] = MeshoptSimplifier.simplify(indices,pos,@@ -91,7 +107,19 @@ export async function simplifyTriangles(triangles, targetFacets) {);if (next.length >= indices.length * 0.98) error *= 4;indices = next;- if (next.length / 3 <= targetFacets) break;+ }++ // 3. sloppy: guaranteed to hit the budget whatever the topology.+ if (indices.length / 3 > targetFacets) {+ const [sloppy] = MeshoptSimplifier.simplifySloppy(+ indices,+ pos,+ 3,+ null,+ targetFacets * 3,+ 1.0+ );+ if (sloppy.length > 0) indices = sloppy;}const out = [];diff --git a/packages/readme-3d/test/geometry.test.js b/packages/readme-3d/test/geometry.test.jsindex b1168dcb0..8d7d77ce1 100644--- a/packages/readme-3d/test/geometry.test.js+++ b/packages/readme-3d/test/geometry.test.js@@ -62,3 +62,30 @@ test('simplifyTriangles is a no-op below target', async () => {const out = await simplifyTriangles(tris, 100);assert.equal(out.length, 12);});++test('simplifyTriangles hits the target on heavily-disconnected meshes (sloppy fallback)', async () => {+ // 200 separate far-apart cubes: topology-preserving simplification cannot+ // merge components, so only the sloppy fallback can reach the budget.+ const tris = [];+ for (let i = 0; i < 200; i++) {+ const ox = (i % 20) * 3, oy = Math.floor(i / 20) * 3;+ for (const t of cubeTriangles()) {+ tris.push(t.map(([x, y, z]) => [x + ox, y + oy, z]));+ }+ }+ const out = await simplifyTriangles(tris, 300); // 2400 -> 300+ assert.ok(out.length <= 330, `expected <=330, got ${out.length}`);+ assert.ok(out.length >= 3, 'mesh must not vanish');+});++test('simplifyTriangles prunes tiny floaters but keeps the main body', async () => {+ const tris = gridTriangles(24); // 1152 facets, 24x24 units+ // add 20 microscopic distant triangles (debris typical of AI meshes)+ for (let i = 0; i < 20; i++) {+ const o = 100 + i;+ tris.push([[o, o, 0], [o + 0.01, o, 0], [o, o + 0.01, 0]]);+ }+ const out = await simplifyTriangles(tris, 400);+ const { max } = bounds(out);+ assert.ok(max[0] < 50, `floaters should be pruned, max x = ${max[0]}`);+});