3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Load the Dark store showcase GLB: skip unread empty meshopt views (#2235)
Nguyen Xuan An00540bf6aa37
diff --git a/changes/2235.md b/changes/2235.mdnew file mode 100644index 00000000..3d3344a7--- /dev/null+++ b/changes/2235.md@@ -0,0 +1 @@+- The glTF loader skips a meshopt view with count 0 that no accessor reads (with a warning) instead of refusing the file, `prepare-hero-glb` no longer writes one, and the Dark store showcase scene gets its camera so it opens in the showcase page (#2235)diff --git a/changes/2235.size.md b/changes/2235.size.mdnew file mode 100644index 00000000..7f779b91--- /dev/null+++ b/changes/2235.size.md@@ -0,0 +1,3 @@+- scene:gltf-box: 145 bytes gzip for the rule that skips an unread meshopt view with count 0 and its warning, which lets the bundled Dark store showcase GLB (and any file from a tool that writes such views) load instead of failing.+- scene:gltf-box+lazy: 160 bytes gzip, the same rule.+- goal:gltf-box: the 145 bytes make the glTF scene load a real file it refused before; the shared accessor reader is the only place that knows which views are read.diff --git a/public/showcase/darkstore/scene.json b/public/showcase/darkstore/scene.jsonindex e157d98c..2b793e68 100644--- a/public/showcase/darkstore/scene.json+++ b/public/showcase/darkstore/scene.json@@ -7,6 +7,9 @@"hdriOptions": { "intensity": 0.6, "rotation": 0, "showBackground": true, "backgroundBlur": 0.6 },"gltfOptions": { "maxTexturePixels": 67108864 },"exposure": 1,+ "eye": [8, 9, 3],+ "target": [-3, 1, -12],+ "maxDistance": 60,"renderer": {"ground": false,"hdr": true,@@ -21,7 +24,7 @@"fogDensity": 0.006}},- "frame": { "fov": 34, "near": 0.25, "far": 300, "effects": { "ambientOcclusion": 0.6, "saturation": 1.05, "vignette": 0.3, "bloom": { "strength": 0.3, "threshold": 1, "radius": 1 } } },+ "frame": { "fov": 44, "near": 0.25, "far": 300, "effects": { "ambientOcclusion": 0.6, "saturation": 1.05, "vignette": 0.3, "bloom": { "strength": 0.3, "threshold": 1, "radius": 1 } } },"credits": [{ "model": "Shelving, crates, products and equipment (NVIDIA SimReady assets)", "author": "NVIDIA", "licence": "CC BY 4.0", "source": "https://docs.omniverse.nvidia.com/usd/latest/usd_content_samples/simready_assets.html" },{ "model": "Store layout and remaining props", "author": "CodePawl", "licence": "MIT", "source": "https://github.com/codepawl/crucible" },diff --git a/scripts/prepare-hero-glb.mjs b/scripts/prepare-hero-glb.mjsindex dc074742..b207429e 100644--- a/scripts/prepare-hero-glb.mjs+++ b/scripts/prepare-hero-glb.mjs@@ -6,7 +6,8 @@// [--simplify-above 40000 --simplify-ratio 0.5 --simplify-error 0.01]//// A primitive that is not a plain triangle list with POSITION, NORMAL and TEXCOORD_0 only is copied byte for byte.-// Meshes shared by several primitives are encoded once. Nothing is read from the network and no input script is run.+// A triangle primitive with no triangles is dropped, and a mesh left with no primitives is removed from its nodes, because a meshopt+// view with count 0 is not valid glTF. Meshes shared by several primitives are encoded once. Nothing is read from the network and no input script is run.import { readFileSync, writeFileSync } from 'node:fs';import sharp from 'sharp';import { MeshoptEncoder, MeshoptSimplifier } from 'meshoptimizer';@@ -15,6 +16,7 @@ const GLB_MAGIC = 0x46546c67;const JSON_CHUNK = 0x4e4f534a;const BINARY_CHUNK = 0x004e4942;const COMPONENT_BYTES = { 5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4 };+const EMPTY_PRIMITIVE = Symbol('empty primitive');const TYPE_COMPONENTS = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 };function parseArguments(argv) {@@ -172,6 +174,29 @@ function boundsOf(positions) {return { min, max };}+/** Removes meshes without primitives and points the nodes that used them at nothing, so no node references a mesh that does not exist. */+function removeEmptyMeshes(document) {+ const newIndexOf = new Map();+ const keptMeshes = [];+ document.meshes.forEach((mesh, index) => {+ if (mesh.primitives.length === 0) return;+ newIndexOf.set(index, keptMeshes.length);+ keptMeshes.push(mesh);+ });+ if (keptMeshes.length === document.meshes.length) return;+ for (const node of document.nodes ?? []) {+ if (node.mesh === undefined) continue;+ if (newIndexOf.has(node.mesh)) {+ node.mesh = newIndexOf.get(node.mesh);+ continue;+ }+ delete node.mesh;+ delete node.skin;+ delete node.weights;+ }+ document.meshes = keptMeshes;+}+async function main() {const { input, output, options } = parseArguments(process.argv.slice(2));await Promise.all([MeshoptEncoder.ready, MeshoptSimplifier.ready]);@@ -198,7 +223,7 @@ async function main() {const processedGeometry = new Map();const copiedAccessors = new Map();- const stats = { primitives: 0, copied: 0, verticesBefore: 0, verticesAfter: 0, trianglesAfter: 0 };+ const stats = { primitives: 0, dropped: 0, copied: 0, verticesBefore: 0, verticesAfter: 0, trianglesAfter: 0 };function copyAccessor(index) {if (copiedAccessors.has(index)) return copiedAccessors.get(index);@@ -230,6 +255,10 @@ async function main() {const sourceIndices = primitive.indices === undefined ? undefined : readIndices(document, binary, primitive.indices);stats.verticesBefore += sourceIndices ? sourceIndices.length : positions.length / 3;const mesh = reorderMesh(simplifyMesh(buildIndexedMesh(positions, normals, uvs, sourceIndices), options));+ if (mesh.indices.length === 0) {+ processedGeometry.set(cacheKey, EMPTY_PRIMITIVE);+ return EMPTY_PRIMITIVE;+ }stats.verticesAfter += mesh.count;stats.trianglesAfter += mesh.indices.length / 3;@@ -269,9 +298,15 @@ async function main() {}for (const mesh of document.meshes) {+ const keptPrimitives = [];for (const primitive of mesh.primitives) {stats.primitives++;const encoded = isEncodablePrimitive(document, primitive) ? encodePrimitive(primitive) : null;+ if (encoded === EMPTY_PRIMITIVE) {+ stats.dropped++;+ continue;+ }+ keptPrimitives.push(primitive);if (encoded) {primitive.attributes = encoded.attributes;primitive.indices = encoded.indices;@@ -282,7 +317,9 @@ async function main() {if (primitive.indices !== undefined) primitive.indices = copyAccessor(primitive.indices);for (const target of primitive.targets ?? []) for (const name of Object.keys(target)) target[name] = copyAccessor(target[name]);}+ mesh.primitives = keptPrimitives;}+ removeEmptyMeshes(document);for (const node of document.nodes ?? []) {const instancing = node.extensions?.EXT_mesh_gpu_instancing;diff --git a/src/gltf-accessors.js b/src/gltf-accessors.jsindex a6fcf386..572c3848 100644--- a/src/gltf-accessors.js+++ b/src/gltf-accessors.js@@ -102,6 +102,27 @@ function checkSparseShape(accessor) {}}+/**+ * Indices of meshopt views that hold no elements (count 0) and that nothing reads: every accessor on them is empty. Other tools write+ * such views for a mesh that simplified to nothing. A view that a non-empty accessor, a sparse accessor or an image reads is not listed,+ * so it still fails with the count error instead of producing a short array.+ */+export function unreadEmptyMeshoptViews(document) {+ const empty = new Set();+ (document.bufferViews ?? []).forEach((view, index) => {+ if (view?.extensions?.EXT_meshopt_compression?.count === 0) empty.add(index);+ });+ if (!empty.size) return empty;+ for (const accessor of document.accessors ?? []) {+ if (accessor.count === 0 && !accessor.sparse) continue;+ empty.delete(accessor.bufferView);+ empty.delete(accessor.sparse?.indices?.bufferView);+ empty.delete(accessor.sparse?.values?.bufferView);+ }+ for (const image of document.images ?? []) empty.delete(image.bufferView);+ return empty;+}+/*** Reads accessors as tightly packed typed arrays, with sparse substitution and meshopt buffer views.* `bufferBytes(index)` returns the bytes of a glTF buffer. `meshoptDecoder` replaces the built-in decoder of compressed views.@@ -124,7 +145,8 @@ export function createAccessorReader(document, bufferBytes, meshoptDecoder) {const views = document.bufferViews ?? [];if (!views.some(view => view.extensions?.EXT_meshopt_compression)) return;const { decodeMeshoptViews } = await import('./gltf-meshopt.js');- for (const [index, bytes] of await decodeMeshoptViews(views, bufferBytes, meshoptDecoder)) viewCache.set(index, bytes);+ const skippedViews = unreadEmptyMeshoptViews(document);+ for (const [index, bytes] of await decodeMeshoptViews(views, bufferBytes, meshoptDecoder, skippedViews)) viewCache.set(index, bytes);}function viewBytes(index) {diff --git a/src/gltf-meshopt.js b/src/gltf-meshopt.jsindex 6c6264ed..2d3b319e 100644--- a/src/gltf-meshopt.js+++ b/src/gltf-meshopt.js@@ -62,13 +62,14 @@ function decodeView(view, viewIndex, bufferBytes, decoder) {return decoded;}-/** Returns a Map from buffer view index to its decoded bytes. The fallback buffer is never read. */-export async function decodeMeshoptViews(bufferViews, bufferBytes, suppliedDecoder) {+/** Returns a Map from buffer view index to its decoded bytes. The fallback buffer is never read. A view in `skippedViews` decodes to no bytes. */+export async function decodeMeshoptViews(bufferViews, bufferBytes, suppliedDecoder, skippedViews = new Set()) {const decoder = suppliedDecoder ?? MeshoptDecoder;await decoder.ready;const decodedViews = new Map();bufferViews.forEach((view, index) => {- if (view.extensions?.EXT_meshopt_compression) decodedViews.set(index, decodeView(view, index, bufferBytes, decoder));+ if (!view.extensions?.EXT_meshopt_compression) return;+ decodedViews.set(index, skippedViews.has(index) ? new Uint8Array(0) : decodeView(view, index, bufferBytes, decoder));});return decodedViews;}diff --git a/src/gltf.js b/src/gltf.jsindex 5d5fa142..82587da3 100644--- a/src/gltf.js+++ b/src/gltf.js@@ -1,7 +1,7 @@import { mat4 } from 'gl-matrix';import { applyClip } from './gltf-animation.js';import { GLTF_LAYER_EXTENSIONS, clampedIorWarnings, layerTextureReferences, materialLayersFromGltf } from './gltf-layers.js';-import { attributeFloatsInSlices } from './gltf-accessors.js';+import { attributeFloatsInSlices, unreadEmptyMeshoptViews } from './gltf-accessors.js';import { createSlicer, forEachChunk } from './cooperative.js';import { describeCompressedImage } from './compressed-texture.js';import { readImageDimensions } from './image-header.js';@@ -514,6 +514,11 @@ function requireNonNegativeSafeInteger(value, fieldPath) {throw new Error(`Invalid glTF ${fieldPath} ${JSON.stringify(value)}: expected a non-negative safe integer.`);}+/** One warning per meshopt view with count 0 that no accessor reads; the loader skips it instead of refusing the file. */+function emptyMeshoptViewWarnings(document) {+ return [...unreadEmptyMeshoptViews(document)].map(index => `Skipped bufferViews[${index}]: its EXT_meshopt_compression count is 0 and no accessor reads it.`);+}+/** Total decoded size of the meshopt views, after checking each size field so a negative length cannot cancel a large one. */function sumMeshoptViewBytes(document) {let total = 0;@@ -1270,7 +1275,7 @@ export async function parseGltf(data, options = {}) {}const { ignoredExtensions, overruns } = validated;await loadLayerShadersFor(document);- const warnings = [...fallbackWarnings, ...ignoredExtensions.map(extension => `${extension} is accepted but not rendered.`), ...skippedPrimitiveWarnings(document), ...unusablePrimitiveWarnings, ...clampedIorWarnings(document), ...ignoredMorphTangentWarnings(document)];+ const warnings = [...fallbackWarnings, ...ignoredExtensions.map(extension => `${extension} is accepted but not rendered.`), ...skippedPrimitiveWarnings(document), ...unusablePrimitiveWarnings, ...emptyMeshoptViewWarnings(document), ...clampedIorWarnings(document), ...ignoredMorphTangentWarnings(document)];const dataBytes = typeof data === 'string' ? new TextEncoder().encode(data).byteLength : data.byteLength;const { resourceBytes, textureScaling } = await prepareResources(document, data, dataBytes, options, limits, overruns);if (textureScaling.scaledTextures.length) {diff --git a/tests/gltf-meshopt.test.js b/tests/gltf-meshopt.test.jsindex c9be7eae..01dc3144 100644--- a/tests/gltf-meshopt.test.js+++ b/tests/gltf-meshopt.test.js@@ -91,7 +91,7 @@ function compressedView({ buffer = 1, data, count, byteStride, mode, filter, tar}/** The same sphere, either as plain buffer views or as one meshopt-compressed GLB (positions, octahedral normals, triangle indices). */-function sphereGlb({ compressed, fallback = 'zero' }) {+function sphereGlb({ compressed, fallback = 'zero', emptyView = null }) {const mesh = sphere();const vertexCount = mesh.positions.length / 3;const normalBytes = compressed ? octahedralNormals(mesh.normals) : bytesOf(mesh.normals);@@ -119,10 +119,23 @@ function sphereGlb({ compressed, fallback = 'zero' }) {const { binary, offsets } = joinChunks([position.encoded, normal.encoded, index.encoded]);const fallbackBuffer = { byteLength: 1 << 20, extensions: { [MESHOPT]: { fallback: true } } };if (fallback === 'unreachable') fallbackBuffer.uri = 'fallback.bin';+ const bufferViews = [position.view(offsets[0]), normal.view(offsets[1]), index.view(offsets[2])];+ const nodes = base.nodes;+ const meshes = base.meshes;+ if (emptyView) {+ // The shape another tool wrote for a mesh that simplified to nothing: a second mesh whose accessors and meshopt view are empty.+ bufferViews.push({ buffer: 1, byteLength: 0, byteStride: 12, target: 34962, extensions: { [MESHOPT]: { buffer: 0, byteOffset: offsets[0], byteLength: 1, mode: 'ATTRIBUTES', byteStride: 12, count: 0 } } });+ accessors.push({ bufferView: 3, componentType: FLOAT, count: emptyView === 'read' ? vertexCount : 0, type: 'VEC3' });+ nodes.push({ mesh: 1 });+ meshes.push({ primitives: [{ attributes: { POSITION: 3 } }] });+ }const document = {...base,+ nodes,+ meshes,+ scenes: [{ nodes: nodes.map((_, nodeIndex) => nodeIndex) }],accessors,- bufferViews: [position.view(offsets[0]), normal.view(offsets[1]), index.view(offsets[2])],+ bufferViews,buffers: [{ byteLength: binary.length }, fallbackBuffer],extensionsUsed: [MESHOPT, 'KHR_mesh_quantization'],extensionsRequired: [MESHOPT, 'KHR_mesh_quantization'],@@ -169,6 +182,17 @@ test('a compressed buffer view that cannot be decoded names the extension', asynawait assert.rejects(parseGltf(glb.buffer), /EXT_meshopt_compression could not decode a buffer view/);});+test('an unread meshopt view with count 0 is skipped with a warning and the rest of the file loads', async () => {+ const asset = await parseGltf(sphereGlb({ compressed: true, emptyView: 'unread' }));+ assert.equal(asset.parts.length, 1);+ assert.ok(asset.parts[0].geometry.positions.length > 0);+ assert.ok(asset.warnings.some(warning => /bufferViews\[3\].*count is 0 and no accessor reads it/.test(warning)), asset.warnings.join(' | '));+});++test('a meshopt view with count 0 that an accessor reads still fails with the count error', async () => {+ await assert.rejects(parseGltf(sphereGlb({ compressed: true, emptyView: 'read' })), /bufferViews\[3\]\.extensions\.EXT_meshopt_compression\.count 0: expected a positive safe integer/);+});+test('a caller supplied decoder still takes the place of the built-in one', async () => {let calls = 0;const supplied = {@@ -239,6 +263,14 @@ test('the public showcase and sample GLBs load without a decoder option', asyncassert.ok(asset.parts.length > 0);});+test('the bundled Dark store showcase GLB loads and reports its empty meshopt views', async () => {+ globalThis.createImageBitmap ??= async () => ({ width: 4, height: 4, close() {} });+ const bytes = await readFile(new URL('../public/showcase/darkstore/scene.glb', import.meta.url));+ const asset = await parseGltf(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength), { maxTexturePixels: 67108864 });+ assert.ok(asset.parts.length > 0);+ assert.ok(asset.warnings.some(warning => /count is 0 and no accessor reads it/.test(warning)));+});+/** One valid small view whose extension fields are then overridden, read with a decoder that counts its calls. */async function loadWithExtensionOverrides(overrides, mode = 'ATTRIBUTES') {const byteStride = mode === 'ATTRIBUTES' ? 4 : 2;diff --git a/tests/prepare-hero-glb.test.js b/tests/prepare-hero-glb.test.jsnew file mode 100644index 00000000..4de88035--- /dev/null+++ b/tests/prepare-hero-glb.test.js@@ -0,0 +1,72 @@+import assert from 'node:assert/strict';+import { execFileSync } from 'node:child_process';+import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import path from 'node:path';+import test from 'node:test';++const SCRIPT = new URL('../scripts/prepare-hero-glb.mjs', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1');+const FLOAT = 5126;+const UNSIGNED_SHORT = 5123;+const MESHOPT = 'EXT_meshopt_compression';++function paddedChunk(typeCode, bytes, paddingByte) {+ const padded = Buffer.concat([bytes, Buffer.alloc((4 - (bytes.length % 4)) % 4, paddingByte)]);+ const header = Buffer.alloc(8);+ header.writeUInt32LE(padded.length, 0);+ header.writeUInt32LE(typeCode, 4);+ return Buffer.concat([header, padded]);+}++function buildGlb(document, binary) {+ const body = Buffer.concat([paddedChunk(0x4e4f534a, Buffer.from(JSON.stringify(document)), 0x20), paddedChunk(0x004e4942, binary, 0)]);+ const header = Buffer.alloc(12);+ header.writeUInt32LE(0x46546c67, 0);+ header.writeUInt32LE(2, 4);+ header.writeUInt32LE(12 + body.length, 8);+ return Buffer.concat([header, body]);+}++function readJson(glb) {+ return JSON.parse(glb.subarray(20, 20 + glb.readUInt32LE(12)).toString('utf8'));+}++/** Two meshes: a triangle, and a second one whose only primitive has an index accessor with no indices. */+function triangleAndEmptyMeshGlb() {+ const positions = Buffer.from(new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]).buffer);+ const indices = Buffer.from(new Uint16Array([0, 1, 2]).buffer);+ const binary = Buffer.concat([positions, indices]);+ const document = {+ asset: { version: '2.0' },+ scenes: [{ nodes: [0, 1] }],+ nodes: [{ mesh: 0 }, { mesh: 1 }],+ meshes: [{ primitives: [{ attributes: { POSITION: 0 }, indices: 1 }] }, { primitives: [{ attributes: { POSITION: 0 }, indices: 2 }] }],+ accessors: [+ { bufferView: 0, componentType: FLOAT, count: 3, type: 'VEC3', min: [0, 0, 0], max: [1, 1, 0] },+ { bufferView: 1, componentType: UNSIGNED_SHORT, count: 3, type: 'SCALAR' },+ { bufferView: 1, componentType: UNSIGNED_SHORT, count: 0, type: 'SCALAR' },+ ],+ bufferViews: [{ buffer: 0, byteOffset: 0, byteLength: 36 }, { buffer: 0, byteOffset: 36, byteLength: 6 }],+ buffers: [{ byteLength: binary.length }],+ };+ return buildGlb(document, binary);+}++test('packing never writes a meshopt view with count 0 and removes a mesh that has no triangles', async () => {+ const folder = await mkdtemp(path.join(tmpdir(), 'prepare-hero-glb-'));+ try {+ const input = path.join(folder, 'in.glb');+ const output = path.join(folder, 'out.glb');+ await writeFile(input, triangleAndEmptyMeshGlb());+ execFileSync(process.execPath, [SCRIPT, input, output], { stdio: 'pipe' });+ const packed = readJson(await readFile(output));+ const counts = packed.bufferViews.map(view => view.extensions?.[MESHOPT]?.count).filter(count => count !== undefined);+ assert.ok(counts.length > 0 && counts.every(count => count >= 1), `meshopt counts ${counts}`);+ assert.ok(packed.accessors.every(accessor => accessor.count >= 1));+ assert.equal(packed.meshes.length, 1);+ assert.equal(packed.nodes[0].mesh, 0);+ assert.equal(packed.nodes[1].mesh, undefined);+ } finally {+ await rm(folder, { recursive: true, force: true });+ }+});