Skip to content

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.

fix(glb): register the meshopt codec so compressed avatars parse server-side

nirholas4028368aff0f
diff --git a/api/_lib/material-studio-store.js b/api/_lib/material-studio-store.js
index 979724514..0fbda8a54 100644
--- a/api/_lib/material-studio-store.js
+++ b/api/_lib/material-studio-store.js
@@ -133,12 +133,24 @@ export async function validateGlbUrl(raw, field = 'glb_url') {
// low for the (more common) code paths that never touch a document, e.g. the
// discovery GET). ────────────────────────────────────────────────────────────
-async function loadDocument(bytes) {
- const [{ NodeIO }, { ALL_EXTENSIONS }] = await Promise.all([
+// EXT_meshopt_compression is in ALL_EXTENSIONS, but glTF-Transform still needs
+// the codec injected before it can read or write one, and most three.ws avatars
+// ship compressed. Without it every compressed source failed here as
+// "source GLB failed to parse", which reads like a corrupt upload and is not.
+async function meshoptIO() {
+ const [{ NodeIO }, { ALL_EXTENSIONS }, { MeshoptDecoder, MeshoptEncoder }] = await Promise.all([
import('@gltf-transform/core'),
import('@gltf-transform/extensions'),
+ import('meshoptimizer'),
]);
- const io = new NodeIO().registerExtensions(ALL_EXTENSIONS);
+ await Promise.all([MeshoptDecoder.ready, MeshoptEncoder.ready]);
+ return new NodeIO()
+ .registerExtensions(ALL_EXTENSIONS)
+ .registerDependencies({ 'meshopt.decoder': MeshoptDecoder, 'meshopt.encoder': MeshoptEncoder });
+}
+
+async function loadDocument(bytes) {
+ const io = await meshoptIO();
try {
return await io.readBinary(new Uint8Array(bytes));
} catch (err) {
@@ -150,9 +162,7 @@ async function loadDocument(bytes) {
}
async function writeAndValidate(doc) {
- const { NodeIO } = await import('@gltf-transform/core');
- const { ALL_EXTENSIONS } = await import('@gltf-transform/extensions');
- const io = new NodeIO().registerExtensions(ALL_EXTENSIONS);
+ const io = await meshoptIO();
const bytes = await io.writeBinary(doc);
const { validateBytes } = await import('gltf-validator');
diff --git a/packages/avatar-agent-mcp/package.json b/packages/avatar-agent-mcp/package.json
index e2bf9be85..e50c2cebc 100644
--- a/packages/avatar-agent-mcp/package.json
+++ b/packages/avatar-agent-mcp/package.json
@@ -2,7 +2,7 @@
"name": "@three-ws/avatar-agent",
"version": "1.2.2",
"mcpName": "io.github.nirholas/3D-AI-Agent-Avatar",
- "description": "3D AI Agent Avatar — MCP server that spawns a textured GLB avatar, inspects/validates/optimizes any 3D model, gives the agent a Solana wallet + a voice, and ships full pump.fun powers (atomic Jito-bundled launches + creator-fee collection). Powered by three.ws.",
+ "description": "3D AI Agent Avatar \u2014 MCP server that spawns a textured GLB avatar, inspects/validates/optimizes any 3D model, gives the agent a Solana wallet + a voice, and ships full pump.fun powers (atomic Jito-bundled launches + creator-fee collection). Powered by three.ws.",
"type": "module",
"author": "nirholas <[email protected]>",
"license": "SEE LICENSE IN LICENSE",
@@ -59,10 +59,10 @@
},
"dependencies": {
"@gltf-transform/core": "^4.3.0",
- "@grpc/grpc-js": "^1.14.4",
- "@grpc/proto-loader": "^0.8.1",
"@gltf-transform/extensions": "^4.3.0",
"@gltf-transform/functions": "^4.3.0",
+ "@grpc/grpc-js": "^1.14.4",
+ "@grpc/proto-loader": "^0.8.1",
"@modelcontextprotocol/sdk": "^1.29.0",
"@nirholas/pump-sdk": "^1.30.0",
"@solana/spl-token": "^0.4.14",
@@ -72,6 +72,7 @@
"ethers": "^6.16.0",
"form-data": "^4.0.5",
"gltf-validator": "^2.0.0-dev.3.10",
+ "meshoptimizer": "^1.1.1",
"zod": "^3.23.8"
}
}
diff --git a/packages/avatar-agent-mcp/src/lib/glb-io.js b/packages/avatar-agent-mcp/src/lib/glb-io.js
index 37f224b86..1cb4babc0 100644
--- a/packages/avatar-agent-mcp/src/lib/glb-io.js
+++ b/packages/avatar-agent-mcp/src/lib/glb-io.js
@@ -2,13 +2,16 @@
// a @gltf-transform/core Document, and serialize back to bytes when needed.
//
// The @gltf-transform NodeIO reader does the binary parsing (BIN chunk +
-// JSON chunk per the glTF 2.0 spec). We attach the Draco extension on
-// both reader + writer so already-Draco-compressed meshes round-trip
-// correctly.
+// JSON chunk per the glTF 2.0 spec). We attach the Draco AND meshopt codecs on
+// both reader + writer so an already-compressed mesh round-trips correctly.
+// Registering the extensions is not enough on its own: glTF-Transform needs the
+// codec injected too, and without the meshopt one every EXT_meshopt_compression
+// asset (the format most three.ws avatars ship as) failed to parse here.
import { NodeIO } from '@gltf-transform/core';
import { KHRDracoMeshCompression, ALL_EXTENSIONS } from '@gltf-transform/extensions';
import draco3dgltf from 'draco3dgltf';
+import { MeshoptDecoder, MeshoptEncoder } from 'meshoptimizer';
let _io = null;
@@ -16,12 +19,16 @@ async function buildIo() {
const [encoder, decoder] = await Promise.all([
draco3dgltf.createEncoderModule(),
draco3dgltf.createDecoderModule(),
+ MeshoptDecoder.ready,
+ MeshoptEncoder.ready,
]);
return new NodeIO()
.registerExtensions(ALL_EXTENSIONS)
.registerDependencies({
'draco3d.encoder': encoder,
'draco3d.decoder': decoder,
+ 'meshopt.encoder': MeshoptEncoder,
+ 'meshopt.decoder': MeshoptDecoder,
});
}
diff --git a/tests/api/material-studio-store.test.js b/tests/api/material-studio-store.test.js
index cd9627ef8..d96107949 100644
--- a/tests/api/material-studio-store.test.js
+++ b/tests/api/material-studio-store.test.js
@@ -272,3 +272,39 @@ describe('validateAndPersistGlb', () => {
await expect(validateAndPersistGlb(Buffer.from('not a glb'))).rejects.toBeInstanceOf(MaterialStudioError);
});
});
+
+describe('meshopt-compressed sources', () => {
+ /** The fixture, re-encoded with EXT_meshopt_compression, as three.ws ships avatars. */
+ async function compressFixture() {
+ const { NodeIO } = await import('@gltf-transform/core');
+ const { ALL_EXTENSIONS, EXTMeshoptCompression } = await import('@gltf-transform/extensions');
+ const { MeshoptDecoder, MeshoptEncoder } = await import('meshoptimizer');
+ await Promise.all([MeshoptDecoder.ready, MeshoptEncoder.ready]);
+ const io = new NodeIO()
+ .registerExtensions(ALL_EXTENSIONS)
+ .registerDependencies({ 'meshopt.decoder': MeshoptDecoder, 'meshopt.encoder': MeshoptEncoder });
+ const doc = await io.readBinary(new Uint8Array(fixtureBytes));
+ doc.createExtension(EXTMeshoptCompression).setRequired(true).setEncoderOptions({ method: EXTMeshoptCompression.EncoderMethod.QUANTIZE });
+ return Buffer.from(await io.writeBinary(doc));
+ }
+
+ it('restyles a compressed avatar instead of rejecting it as an invalid GLB', async () => {
+ // Regression: the reader registered the extension but never the codec, so
+ // every compressed source (most three.ws avatars) died as "failed to parse".
+ fixtureBytes = await compressFixture();
+ const { NodeIO } = await import('@gltf-transform/core');
+ const { ALL_EXTENSIONS } = await import('@gltf-transform/extensions');
+ const bare = new NodeIO().registerExtensions(ALL_EXTENSIONS);
+ await expect(bare.readBinary(new Uint8Array(fixtureBytes))).rejects.toBeTruthy();
+
+ const { restyleMaterialFromInstruction } = await import('../../api/_lib/material-studio-store.js');
+ const result = await restyleMaterialFromInstruction({
+ glbUrl: FIXTURE_URL,
+ instruction: 'make it chrome',
+ });
+
+ expect(result.glbUrl).toMatch(persistedGlbUrl('material-studio/restyle'));
+ expect(result.materialsEdited).toBeGreaterThan(0);
+ expect(uploadedObjects).toHaveLength(1);
+ });
+});