A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(assets): a Draco model is still a model
esengined95d92bdd131
diff --git a/desktop/package.json b/desktop/package.jsonindex 8a3c92c3b..f4c0655ac 100644--- a/desktop/package.json+++ b/desktop/package.json@@ -77,6 +77,7 @@"@anthropic-ai/sdk": "^0.115.0","@modelcontextprotocol/sdk": "^1.29.0","dockview": "^6.6.1",+ "draco3dgltf": "^1.5.7","electron-updater": "^6.8.9","esbuild": "^0.27.0","esengine": "workspace:*",diff --git a/desktop/scripts/editor-checks/compressed-import.mjs b/desktop/scripts/editor-checks/compressed-import.mjsnew file mode 100644index 000000000..ef9c4ed2c--- /dev/null+++ b/desktop/scripts/editor-checks/compressed-import.mjs@@ -0,0 +1,73 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file compressed-import — a compressed model, dropped into the real editor.+ *+ * A unit test can prove the importer decodes meshopt and Draco; it runs the+ * module straight from source. The editor reaches it through a BUNDLED main+ * process, where one decoder's wasm rides along as base64 and the other is+ * loaded off disk beside its package — both of which a bundler can break, with+ * a model of vertices on the origin as the symptom rather than an error. So+ * this drops all three files through the real import door.+ */+import { mkdir, readFile, writeFile } from 'node:fs/promises';+import path from 'node:path';+import { makeProject, checker } from '../lib/editorDriver.mjs';+import { plainTriangle, meshoptTriangle, dracoTriangle } from '../lib/gltfFixtures.mjs';++export const name = 'compressed-import';+export const describes = 'a meshopt- or Draco-compressed glTF imports to the mesh the plain one does';++export async function run(ed) {+ const root = await makeProject({+ 'project.esproject': JSON.stringify({ name: 'Meshopt', version: '1.0' }, null, 2),+ 'assets/scenes/main.esscene': JSON.stringify({+ version: '1.0', name: 'Main',+ entities: [{+ id: 0, name: 'Camera', parent: null, children: [], visible: true,+ components: [+ { type: 'Transform', data: { position: { x: 0, y: 0, z: 10 } } },+ { type: 'Camera', data: { projectionType: 1, orthoSize: 300, isActive: true, priority: 0 } },+ ],+ }],+ }, null, 1),+ });+ await mkdir(path.join(root, 'assets'), { recursive: true });+ const plain = path.join(root, 'assets', 'plain.gltf');+ const packed = path.join(root, 'assets', 'packed.gltf');+ const draco = path.join(root, 'assets', 'draco.gltf');+ await writeFile(plain, plainTriangle());+ await writeFile(packed, await meshoptTriangle());+ await writeFile(draco, await dracoTriangle());++ const check = checker();+ await ed.open(root, 'assets/scenes/main.esscene');+ const result = await ed.json('import_assets', { destDir: 'assets', sources: [plain, packed, draco] }, 120000);+ const wrote = (stem) => (result?.imported ?? []).includes(`assets/${stem}.esmesh`);+ if (!check(wrote('plain') && wrote('packed') && wrote('draco'),+ `import reported ${JSON.stringify(result)}`)) {+ return check.failures;+ }+ check(!result.warnings?.length, `import warned: ${JSON.stringify(result.warnings)}`);++ const mesh = async (stem) => {+ try {+ return await readFile(path.join(root, 'assets', `${stem}.esmesh`));+ } catch {+ return null;+ }+ };+ const a = await mesh('plain');+ const b = await mesh('packed');+ const c = await mesh('draco');+ if (!check(a != null && b != null && c != null, 'one of the imports wrote no .esmesh')) {+ return check.failures;+ }++ // meshopt is lossless, so byte-for-byte: anything of the compression that+ // survives into the product is a decode gone wrong. Draco quantizes and+ // reorders, so its claim here is the vertex count the same payload implies.+ check(a.equals(b), `the meshopt model produced a different .esmesh (${a.length} vs ${b.length} bytes)`);+ check(c.length === a.length, `the Draco model produced ${c.length} bytes, not ${a.length}`);+ return check.failures;+}diff --git a/desktop/scripts/editor-checks/meshopt-import.mjs b/desktop/scripts/editor-checks/meshopt-import.mjsdeleted file mode 100644index c1f26ecc8..000000000--- a/desktop/scripts/editor-checks/meshopt-import.mjs+++ /dev/null@@ -1,64 +0,0 @@-// SPDX-License-Identifier: Apache-2.0-// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team-/**- * @file meshopt-import — a compressed model, dropped into the real editor.- *- * A unit test can prove the importer decodes `EXT_meshopt_compression`; it runs- * the module straight from source. The editor reaches it through a bundled main- * process, and the decoder is a WebAssembly module carried as base64 inside that- * bundle — a bundler can mangle it, and the failure is a model of vertices on the- * origin rather than an error. So this drops both files through the real import- * door and asks that the compressed one produce the very same `.esmesh`.- */-import { mkdir, readFile, writeFile } from 'node:fs/promises';-import path from 'node:path';-import { makeProject, checker } from '../lib/editorDriver.mjs';-import { plainTriangle, meshoptTriangle } from '../lib/gltfFixtures.mjs';--export const name = 'meshopt-import';-export const describes = 'a meshopt-compressed glTF imports to the same mesh the plain one does';--export async function run(ed) {- const root = await makeProject({- 'project.esproject': JSON.stringify({ name: 'Meshopt', version: '1.0' }, null, 2),- 'assets/scenes/main.esscene': JSON.stringify({- version: '1.0', name: 'Main',- entities: [{- id: 0, name: 'Camera', parent: null, children: [], visible: true,- components: [- { type: 'Transform', data: { position: { x: 0, y: 0, z: 10 } } },- { type: 'Camera', data: { projectionType: 1, orthoSize: 300, isActive: true, priority: 0 } },- ],- }],- }, null, 1),- });- await mkdir(path.join(root, 'assets'), { recursive: true });- const plain = path.join(root, 'assets', 'plain.gltf');- const packed = path.join(root, 'assets', 'packed.gltf');- await writeFile(plain, plainTriangle());- await writeFile(packed, await meshoptTriangle());-- const check = checker();- await ed.open(root, 'assets/scenes/main.esscene');- const result = await ed.json('import_assets', { destDir: 'assets', sources: [plain, packed] }, 120000);- const wrote = (stem) => (result?.imported ?? []).includes(`assets/${stem}.esmesh`);- if (!check(wrote('plain') && wrote('packed'), `import reported ${JSON.stringify(result)}`)) {- return check.failures;- }-- const mesh = async (stem) => {- try {- return await readFile(path.join(root, 'assets', `${stem}.esmesh`));- } catch {- return null;- }- };- const a = await mesh('plain');- const b = await mesh('packed');- if (!check(a != null && b != null, 'one of the imports wrote no .esmesh')) return check.failures;-- // Byte-for-byte: the compression is a property of the file, not of the mesh- // in it, so anything that survives into the product is a decode gone wrong.- check(a.equals(b), `the compressed model produced a different .esmesh (${a.length} vs ${b.length} bytes)`);- return check.failures;-}diff --git a/desktop/scripts/lib/gltfFixtures.d.mts b/desktop/scripts/lib/gltfFixtures.d.mtsindex a3a8c9823..341658603 100644--- a/desktop/scripts/lib/gltfFixtures.d.mts+++ b/desktop/scripts/lib/gltfFixtures.d.mts@@ -6,3 +6,6 @@ export function plainTriangle(): Uint8Array;/** The same triangle with every bufferView meshopt-compressed. */export function meshoptTriangle(): Promise<Uint8Array>;++/** The same triangle with its geometry in a Draco blob. */+export function dracoTriangle(): Promise<Uint8Array>;diff --git a/desktop/scripts/lib/gltfFixtures.mjs b/desktop/scripts/lib/gltfFixtures.mjsindex d80d88774..0f45a82a7 100644--- a/desktop/scripts/lib/gltfFixtures.mjs+++ b/desktop/scripts/lib/gltfFixtures.mjs@@ -83,3 +83,48 @@ export async function meshoptTriangle() {meshes: MESHES,});}++/**+ * The same triangle Draco-compressed — what Blender writes when its glTF export+ * has compression on, and the only compression Blender offers.+ *+ * Draco reorders vertices to compress them, so a product built from this is+ * geometrically the same triangle rather than the same bytes.+ */+export async function dracoTriangle() {+ const { createEncoderModule } = await import('draco3dgltf');+ const draco = await createEncoderModule();+ const mesh = new draco.Mesh();+ const builder = new draco.MeshBuilder();+ builder.AddFacesToMesh(mesh, 1, new Uint32Array(INDICES));+ const position = builder.AddFloatAttributeToMesh(mesh, draco.POSITION, 3, 3, POSITIONS);+ const texCoord = builder.AddFloatAttributeToMesh(mesh, draco.TEX_COORD, 3, 2, UVS);++ const encoder = new draco.Encoder();+ const encoded = new draco.DracoInt8Array();+ const length = encoder.EncodeMeshToDracoBuffer(mesh, encoded);+ const blob = new Uint8Array(length);+ for (let i = 0; i < length; i++) blob[i] = encoded.GetValue(i) & 0xff;+ for (const handle of [encoded, encoder, mesh, builder]) draco.destroy(handle);++ return encode({+ asset: { version: '2.0' },+ extensionsRequired: ['KHR_draco_mesh_compression'],+ buffers: [{ byteLength: blob.length, uri: `data:application/octet-stream;base64,${Buffer.from(blob).toString('base64')}` }],+ bufferViews: [{ buffer: 0, byteOffset: 0, byteLength: blob.length }],+ // The accessors keep saying what the data MEANS — count and type — while+ // Draco holds the data itself. That is the shape the spec asks for.+ accessors: ACCESSORS.map(a => ({ ...a, bufferView: undefined })),+ meshes: [{+ name: 'Tri',+ primitives: [{+ attributes: { POSITION: 0, TEXCOORD_0: 1 }, indices: 2, mode: 4,+ extensions: {+ KHR_draco_mesh_compression: {+ bufferView: 0, attributes: { POSITION: position, TEXCOORD_0: texCoord },+ },+ },+ }],+ }],+ });+}diff --git a/desktop/tests/gltf-import.test.ts b/desktop/tests/gltf-import.test.tsindex 4aa667719..3fb97db9f 100644--- a/desktop/tests/gltf-import.test.ts+++ b/desktop/tests/gltf-import.test.ts@@ -12,7 +12,7 @@ import {importGltfMeshes, assembleGltfPrefab, type ImportedMesh, type PrefabAssembly,} from '../../pipeline/src/assets/gltfImport';import { MeshChannel } from 'esengine';-import { plainTriangle, meshoptTriangle } from '../scripts/lib/gltfFixtures.mjs';+import { plainTriangle, meshoptTriangle, dracoTriangle } from '../scripts/lib/gltfFixtures.mjs';const PNG_1PX = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';@@ -194,10 +194,12 @@ describe('glTF material import', () => {.toEqual({ file: 'bump.png', external: true });});- it('skips a Draco-compressed primitive rather than reading zeroes', async () => {+ it('says so when a Draco blob will not decode, rather than reading zeroes', async () => {+ // The accessors of a Draco primitive point at nothing by design, so a blob+ // that fails to decode has to be reported: read as ordinary accessors, the+ // very same file is a mesh of vertices on the origin.const doc = {...withInlineImage(),- // Draco holds the geometry itself, so the accessors point at nothing.accessors: [{ componentType: 5126, count: 3, type: 'VEC3' },{ componentType: 5126, count: 3, type: 'VEC2' },@@ -209,8 +211,7 @@ describe('glTF material import', () => {extensions: { KHR_draco_mesh_compression: { bufferView: 0, attributes: {} } },}]), 'model');expect(meshes).toEqual([]);- expect(warnings.join('\n')).toContain('Draco-compressed');- expect(warnings.join('\n')).toContain('re-export without compression');+ expect(warnings.join('\n')).toContain('Draco decode failed');});it('skips a primitive whose POSITION carries no data', async () => {@@ -449,3 +450,37 @@ describe('meshopt-compressed geometry', () => {expect(warnings).toEqual([]);});});++describe('Draco-compressed geometry', () => {+ /** Every vertex as one "x,y,z/u,v" string, sorted — Draco reorders vertices to+ * compress them, so what survives is the set of them, not their order. */+ const vertexSet = (mesh: ImportedMesh): string[] => {+ const view = new DataView(mesh.data.vertices.buffer, mesh.data.vertices.byteOffset);+ const at = (semantic: number): number =>+ mesh.data.channels.find(c => c.semantic === semantic)!.offset;+ const out: string[] = [];+ for (let i = 0; i < mesh.vertexCount; i++) {+ const base = i * mesh.data.vertexStride;+ const read = (offset: number, n: number): string => Array.from({ length: n },+ (_, c) => view.getFloat32(base + offset + c * 4, true).toFixed(3)).join(',');+ out.push(`${read(at(MeshChannel.Position), 3)}/${read(at(MeshChannel.TexCoord0), 2)}`);+ }+ return out.sort();+ };++ it('imports to the same triangle the uncompressed file does', async () => {+ const plain = (await importGltfMeshes(plainTriangle(), 'model')).meshes[0]!;+ const packed = (await importGltfMeshes(await dracoTriangle(), 'model')).meshes[0]!;+ expect(packed.vertexCount).toBe(plain.vertexCount);+ expect(packed.triangleCount).toBe(plain.triangleCount);+ expect(vertexSet(packed)).toEqual(vertexSet(plain));+ });++ it('reads it without complaint, accessors and all', async () => {+ // A Draco accessor has no bufferView by spec. Read as an ordinary one it is+ // "no data" — the check that catches a genuinely empty POSITION.+ const { warnings, meshes } = await importGltfMeshes(await dracoTriangle(), 'model');+ expect(warnings).toEqual([]);+ expect(meshes).toHaveLength(1);+ });+});diff --git a/desktop/vite.config.ts b/desktop/vite.config.tsindex 9925fbaa0..895715898 100644--- a/desktop/vite.config.ts+++ b/desktop/vite.config.ts@@ -92,10 +92,9 @@ export default defineConfig({// (`__filename is not defined in ES module scope`). It also ships as a// RUNTIME dependency — the editor's language service (scriptService.ts)// is a shipped feature, not a dev tool.- // esbuild + the vendored Basis KTX2 encoder + ffmpeg-static all resolve- // sibling binaries at runtime (native exe / wasm via import.meta.url /- // module __dirname), so bundling them breaks that — keep them external,- // loaded from disk by the cook.+ // esbuild, the Basis KTX2 encoder, ffmpeg-static and the Draco decoder+ // all resolve sibling binaries at runtime (native exe / wasm via+ // import.meta.url / module __dirname), which bundling breaks.// electron-updater is external for a different reason: it resolves its own// package.json and requires its providers by path, and it must stay the// CommonJS package Node's interop knows how to load (see autoUpdate.ts).@@ -105,7 +104,7 @@ export default defineConfig({vite: {build: {rolldownOptions: {- external: ['esbuild', 'typescript', 'ffmpeg-static', 'electron-updater', /[\\/]build-tools[\\/]basis[\\/]/],+ external: ['esbuild', 'typescript', 'ffmpeg-static', 'draco3dgltf', 'electron-updater', /[\\/]build-tools[\\/]basis[\\/]/],},} as never,},diff --git a/pipeline/bin/estella.mjs b/pipeline/bin/estella.mjsindex 90f3f9e70..2cc7ce09b 100644--- a/pipeline/bin/estella.mjs+++ b/pipeline/bin/estella.mjs@@ -111,7 +111,7 @@ async function loadPipeline(entry, outName) {format: 'esm',platform: 'node',target: 'node20',- external: ['esbuild', 'electron', 'sharp', '../../../build-tools/basis/encoder.mjs'],+ external: ['esbuild', 'electron', 'sharp', 'draco3dgltf', '../../../build-tools/basis/encoder.mjs'],logLevel: 'error',banner: {js: "import { createRequire as __esCreateRequire } from 'node:module';\n"diff --git a/pipeline/package.json b/pipeline/package.jsonindex 733051ef6..50449b4c2 100644--- a/pipeline/package.json+++ b/pipeline/package.json@@ -14,6 +14,7 @@"test": "vitest run"},"dependencies": {+ "draco3dgltf": "^1.5.7","esbuild": "^0.27.0","esengine": "workspace:*","ffmpeg-static": "^5.3.0",diff --git a/pipeline/src/assets/draco3dgltf.d.ts b/pipeline/src/assets/draco3dgltf.d.tsnew file mode 100644index 000000000..fb48fb888--- /dev/null+++ b/pipeline/src/assets/draco3dgltf.d.ts@@ -0,0 +1,12 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team++/**+ * The Draco glTF decoder ships no types. Only the factory is named here; what it+ * returns is described where it is used (gltfImport.ts), so the shape and the+ * calls that rely on it stay in one place.+ */+declare module 'draco3dgltf' {+ export function createDecoderModule(): Promise<unknown>;+ export function createEncoderModule(): Promise<unknown>;+}diff --git a/pipeline/src/assets/gltfImport.ts b/pipeline/src/assets/gltfImport.tsindex 620daf677..b13b29574 100644--- a/pipeline/src/assets/gltfImport.ts+++ b/pipeline/src/assets/gltfImport.ts@@ -9,6 +9,7 @@* disk, where a project can see them, reference them and diff them, and* the cook then ships them as the engine format they already are.*/+/// <reference path="./draco3dgltf.d.ts" />import {MeshChannel, MeshChannelType, packChannels, encodeMesh, PREFAB_FORMAT_VERSION,type MeshData, type PrefabData, type PrefabEntityData,@@ -144,7 +145,7 @@ interface GltfJson {}[];meshes?: { name?: string; primitives: {attributes: Record<string, number>; indices?: number; mode?: number; material?: number;- extensions?: Record<string, unknown>;+ extensions?: { KHR_draco_mesh_compression?: DracoPrimitive } & Record<string, unknown>;/** Morph targets — shapes the runtime blends between; not imported. */targets?: unknown[];}[] }[];@@ -175,16 +176,15 @@ interface GltfJson {}/**- * Extensions that hold a primitive's geometry themselves, so its accessors point- * at nothing this importer can read. Left undetected they decode as zeroes — a- * mesh whose every vertex sits on the origin, with nothing said about it.- *- * meshopt is not among them: it compresses bufferVIEWS, which decode back into- * the bytes the accessors already describe (see {@link viewBytes}).+ * `KHR_draco_mesh_compression` on a primitive: the geometry is one compressed+ * blob holding every attribute, so unlike meshopt this is NOT a bufferView that+ * decodes in place — the accessors keep their count and type and lose their data.*/-const COMPRESSED_GEOMETRY: Record<string, string> = {- KHR_draco_mesh_compression: 'Draco',-};+interface DracoPrimitive {+ bufferView: number;+ /** Attribute semantic → the id Draco knows it by (not an accessor index). */+ attributes: Record<string, number>;+}const MIME_EXTENSION: Record<string, string> = {'image/png': '.png', 'image/jpeg': '.jpg', 'image/webp': '.webp', 'image/ktx2': '.ktx2',@@ -267,6 +267,110 @@ function decodeMeshopt(src: GltfBytes, packed: MeshoptView): Uint8Array | null {return out;}+let dracoModule: Promise<DracoModule | null> | null = null;++/** The decoder, instantiated once per process — a wasm module per file would be+ * most of the cost of importing one. */+function loadDraco(warnings: string[]): Promise<DracoModule | null> {+ dracoModule ??= import('draco3dgltf')+ .then(m => (m as unknown as { createDecoderModule(): Promise<DracoModule> }).createDecoderModule())+ .catch(() => null);+ return dracoModule.then((m) => {+ if (!m) warnings.push('the Draco decoder could not be loaded');+ return m;+ });+}++/**+ * The slice of the Draco decoder this import uses, typed here because the package+ * ships none. Loaded by dynamic import and kept OUT of the Electron-main bundle+ * (see its externals): it locates its wasm beside itself on disk, which bundling+ * breaks — the same shape as the KTX2 encoder the cook loads.+ */+interface DracoModule {+ Decoder: new () => DracoDecoder;+ DecoderBuffer: new () => { Init(bytes: Uint8Array, length: number): void };+ Mesh: new () => DracoMesh;+ TRIANGULAR_MESH: number;+ DT_FLOAT32: number;+ HEAPF32: { buffer: ArrayBuffer };+ _malloc(bytes: number): number;+ _free(ptr: number): void;+ destroy(object: unknown): void;+}+interface DracoMesh { num_points(): number; num_faces(): number }+interface DracoAttribute { num_components(): number }+interface DracoDecoder {+ GetEncodedGeometryType(buffer: unknown): number;+ DecodeBufferToMesh(buffer: unknown, mesh: DracoMesh): { ok(): boolean; error_msg(): string };+ GetAttributeByUniqueId(mesh: DracoMesh, id: number): DracoAttribute | null;+ GetAttributeDataArrayForAllPoints(+ mesh: DracoMesh, attribute: DracoAttribute, type: number, byteLength: number, ptr: number): boolean;+ GetTrianglesUInt32Array(mesh: DracoMesh, byteLength: number, ptr: number): boolean;+}++/** One primitive's decoded geometry: every attribute Draco held, and its faces. */+interface DracoGeometry {+ attributes: Map<string, Float32Array>;+ indices: Uint32Array;+}++/**+ * Draco's blob for one primitive, expanded. Values come back as floats without+ * the glTF `normalized` scale applied — that stays the accessor's word, as it is+ * for uncompressed data, so a u8 COLOR_0 does not arrive 255× too bright.+ */+function decodeDraco(draco: DracoModule, bytes: Uint8Array, ext: DracoPrimitive): DracoGeometry {+ const decoder = new draco.Decoder();+ const buffer = new draco.DecoderBuffer();+ const mesh = new draco.Mesh();+ try {+ buffer.Init(bytes, bytes.byteLength);+ if (decoder.GetEncodedGeometryType(buffer) !== draco.TRIANGULAR_MESH) {+ throw new Error('the Draco blob is not a triangle mesh');+ }+ const status = decoder.DecodeBufferToMesh(buffer, mesh);+ if (!status.ok()) throw new Error(status.error_msg());++ const attributes = new Map<string, Float32Array>();+ for (const [semantic, id] of Object.entries(ext.attributes)) {+ const attribute = decoder.GetAttributeByUniqueId(mesh, id);+ if (!attribute) continue;+ attributes.set(semantic, readDracoArray(draco, decoder, mesh, attribute));+ }+ return { attributes, indices: readDracoIndices(draco, decoder, mesh) };+ } finally {+ // Every handle is wasm memory this side has to hand back, on the error+ // path too — an import that reports a bad file must not also leak it.+ draco.destroy(mesh);+ draco.destroy(buffer);+ draco.destroy(decoder);+ }+}++function readDracoArray(draco: DracoModule, decoder: DracoDecoder,+ mesh: DracoMesh, attribute: DracoAttribute): Float32Array {+ const values = mesh.num_points() * attribute.num_components();+ const ptr = draco._malloc(values * 4);+ try {+ decoder.GetAttributeDataArrayForAllPoints(mesh, attribute, draco.DT_FLOAT32, values * 4, ptr);+ return new Float32Array(draco.HEAPF32.buffer, ptr, values).slice();+ } finally {+ draco._free(ptr);+ }+}++function readDracoIndices(draco: DracoModule, decoder: DracoDecoder, mesh: DracoMesh): Uint32Array {+ const count = mesh.num_faces() * 3;+ const ptr = draco._malloc(count * 4);+ try {+ decoder.GetTrianglesUInt32Array(mesh, count * 4, ptr);+ return new Uint32Array(draco.HEAPF32.buffer, ptr, count).slice();+ } finally {+ draco._free(ptr);+ }+}+/*** Reads one accessor as floats. Byte stride is honoured — an interleaved glTF is* the common export, and reading it as tightly packed yields wrong vertices@@ -633,6 +737,13 @@ export async function importGltfMeshes(const meshIndexOf = new Map<string, number>();const single = (json.meshes ?? []).reduce((n, m) => n + m.primitives.length, 0) === 1;+ // ~800KB of decoder, loaded only for a file that carries Draco — the same+ // lazy dynamic import the cook uses for its KTX2 encoder, and the reason the+ // import is async in the first place.+ const draco = (json.meshes ?? []).some(m => m.primitives.some(p => p.extensions?.KHR_draco_mesh_compression))+ ? await loadDraco(warnings)+ : null;+const src: GltfBytes = { json, bin, buffers, decoded: new Map() };const materialCtx: MaterialContext = { src, json, stem, textures, warnings };const textureCache = new Map<number, ImportedImageRef | null>();@@ -654,15 +765,23 @@ export async function importGltfMeshes(warnings.push(`${label}: mode ${mode} is not TRIANGLES — skipped`);return;}- const compressed = Object.keys(prim.extensions ?? {})- .find(name => name in COMPRESSED_GEOMETRY);- if (compressed) {- warnings.push(`${label}: geometry is ${COMPRESSED_GEOMETRY[compressed]}-compressed`- + ' — skipped (re-export without compression)');- return;+ const packed = prim.extensions?.KHR_draco_mesh_compression;+ let geometry: DracoGeometry | null = null;+ if (packed) {+ const blob = viewBytes(src, packed.bufferView);+ if (!blob || !draco) {+ warnings.push(`${label}: Draco geometry could not be read — skipped`);+ return;+ }+ try {+ geometry = decodeDraco(draco, blob, packed);+ } catch (err) {+ warnings.push(`${label}: Draco decode failed (${(err as Error).message}) — skipped`);+ return;+ }}for (const name of Object.keys(prim.extensions ?? {})) {- if (!(name in COMPRESSED_GEOMETRY)) warnings.push(`${label}: ${name} not imported`);+ if (name !== 'KHR_draco_mesh_compression') warnings.push(`${label}: ${name} not imported`);}// Deformation is the difference between a model that moves and one// that does not, so it is never dropped in silence.@@ -678,30 +797,44 @@ export async function importGltfMeshes(return;}// Zeroes are a legal accessor per spec, and for POSITION they are a- // heap of vertices on the origin. Say so instead of writing one.+ // heap of vertices on the origin. Say so instead of writing one. A+ // Draco accessor has no view BY spec — its data is in the blob.const posAccessor = json.accessors?.[posIndex];- if (posAccessor && posAccessor.bufferView === undefined && !posAccessor.sparse) {+ if (!geometry && posAccessor+ && posAccessor.bufferView === undefined && !posAccessor.sparse) {warnings.push(`${label}: POSITION has no data — skipped`);return;}try {- const positions = readAccessor(src, posIndex);+ // One reader for both storages. Draco hands over whole attribute+ // arrays, leaving each accessor to say what it MEANS — its type,+ // and whether its integers are normalized — as it always does.+ const attribute = (semantic: string, index: number | undefined): Float32Array | null => {+ if (index === undefined) return null;+ if (!geometry) return readAccessor(src, index);+ const values = geometry.attributes.get(semantic);+ if (!values) throw new Error(`${semantic} is missing from the Draco blob`);+ const acc = json.accessors?.[index];+ if (!acc?.normalized) return values;+ const scale = normalizedScale(acc.componentType);+ return values.map(v => v * scale);+ };+ const positions = attribute('POSITION', posIndex)!;const vertexCount = positions.length / 3;const uvIndex = prim.attributes.TEXCOORD_0;const colorIndex = prim.attributes.COLOR_0;- const uvs = uvIndex !== undefined ? readAccessor(src, uvIndex) : null;+ const uvs = attribute('TEXCOORD_0', uvIndex);const normalIndex = prim.attributes.NORMAL;- const normals = normalIndex !== undefined- ? readAccessor(src, normalIndex) : null;- const colorsRaw = colorIndex !== undefined- ? readAccessor(src, colorIndex) : null;+ const normals = attribute('NORMAL', normalIndex);+ const colorsRaw = attribute('COLOR_0', colorIndex);const colorComps = colorIndex !== undefined? TYPE_COMPONENTS[json.accessors?.[colorIndex]?.type ?? 'VEC4'] ?? 4 : 4;- const indices = prim.indices !== undefined- ? Uint32Array.from(readAccessor(src, prim.indices))- : Uint32Array.from({ length: vertexCount }, (_, i) => i);+ const indices = geometry ? geometry.indices+ : prim.indices !== undefined+ ? Uint32Array.from(readAccessor(src, prim.indices))+ : Uint32Array.from({ length: vertexCount }, (_, i) => i);if (indices.length % 3 !== 0) {warnings.push(`${label}: ${indices.length} indices is not a triangle list — skipped`);return;diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 41ca8fa07..476f0d653 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -36,6 +36,9 @@ importers:dockview:specifier: ^6.6.1version: 6.6.1([email protected])+ draco3dgltf:+ specifier: ^1.5.7+ version: 1.5.7electron-updater:specifier: ^6.8.9version: 6.8.9@@ -118,6 +121,9 @@ importers:pipeline:dependencies:+ draco3dgltf:+ specifier: ^1.5.7+ version: 1.5.7esbuild:specifier: ^0.27.0version: 0.27.7@@ -1829,6 +1835,9 @@ packages:resolution: {integrity: sha512-uBq4egWHTcTt33a72vpSG0z3HnPuIl6NqYcTrKEg2azoEyl2hpW0zqlxysq2pK9HlDIHyHyakeYaYnSAwd8bow==}engines: {node: '>=12'}+ resolution: {integrity: sha512-LeqcpmoHIyYUi0z70/H3tMkGj8QhqVxq6FJGPjlzR24BNkQ6jyMheMvFKJBI0dzGZrEOUyQEmZ8axM1xRrbRiw==}+resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==}engines: {node: '>= 0.4'}@@ -5191,6 +5200,8 @@ snapshots:+ [email protected]: {}+dependencies:call-bind-apply-helpers: 1.0.2