UNPKG

@babylonjs/viewer

Version:

The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.

86 lines (82 loc) 2.83 kB
import { T as ThrowLiteError, j as U8, ak as DV } from './index-By0tcgYN.esm.js'; let meshoptBaseUrl = "/"; let scriptLoadPromise = null; function loadMeshoptScript() { if (scriptLoadPromise) { return scriptLoadPromise; } scriptLoadPromise = new Promise((resolve, reject) => { const existing = globalThis.MeshoptDecoder; if (existing) { resolve(existing); return; } const script = document.createElement("script"); script.src = meshoptBaseUrl + "meshopt_decoder.js"; script.onload = () => { const mod = globalThis.MeshoptDecoder; if (!mod) { reject(new Error("meshopt_decoder.js loaded but MeshoptDecoder is undefined")); } else { resolve(mod); } }; script.onerror = () => reject(new Error("Failed to load meshopt_decoder.js from " + script.src)); document.head.appendChild(script); }); return scriptLoadPromise; } async function getMeshoptDecoder() { const mod = await loadMeshoptScript(); await mod.ready; return mod; } function align4(n) { return n + 3 & -4; } const feature = { id: "EXT_meshopt_compression", async preParse(json, binChunk) { const bufferViews = json.bufferViews ?? []; const decoder = await getMeshoptDecoder(); const materialized = new Array(bufferViews.length); const newOffsets = new Array(bufferViews.length); let total = 0; for (let i = 0; i < bufferViews.length; i++) { const bv = bufferViews[i]; const ext = bv.extensions?.EXT_meshopt_compression; let bytes; if (ext) { if ((ext.buffer ?? 0) !== 0) { ThrowLiteError(86, ext.buffer); } const source = new U8(binChunk.buffer, binChunk.byteOffset + (ext.byteOffset ?? 0), ext.byteLength); const target = new U8(ext.count * ext.byteStride); decoder.decodeGltfBuffer(target, ext.count, ext.byteStride, source, ext.mode, ext.filter ?? "NONE"); bytes = target; } else { if ((bv.buffer ?? 0) !== 0) { ThrowLiteError(87, bv.buffer); } bytes = new U8(binChunk.buffer.slice(binChunk.byteOffset + (bv.byteOffset ?? 0), binChunk.byteOffset + (bv.byteOffset ?? 0) + bv.byteLength)); } materialized[i] = bytes; newOffsets[i] = total; total = align4(total + bytes.length); } const packed = new U8(total); for (let i = 0; i < bufferViews.length; i++) { const bv = bufferViews[i]; packed.set(materialized[i], newOffsets[i]); bv.buffer = 0; bv.byteOffset = newOffsets[i]; bv.byteLength = materialized[i].length; if (bv.extensions) { delete bv.extensions.EXT_meshopt_compression; } } return new DV(packed.buffer); } }; export { feature as default }; //# sourceMappingURL=gltf-feature-meshopt-Bdj0KhKJ.esm.js.map