@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.
173 lines (168 loc) • 6.03 kB
JavaScript
import { b as GLTFLoader, A as ArrayItem } from './glTFLoader.pure-BaIedO7s.esm.js';
import { aY as Tools, bo as unregisterGLTFExtension, bp as registerGLTFExtension } from './index-MZPybX0H.esm.js';
import './morphTargetManager-CK7B3gaT.esm.js';
import './bone.pure-CjsCww39.esm.js';
import './skeleton-D7Sw58cv.esm.js';
import './assetContainer-Bm0vsreJ.esm.js';
import './objectModelMapping-De5EKNEZ.esm.js';
import './spotLight.pure-CRdZC6Ci.esm.js';
let NumberOfWorkers = 0;
let WorkerTimeout = null;
/**
* Meshopt compression (https://github.com/zeux/meshoptimizer)
*
* This class wraps the meshopt library from https://github.com/zeux/meshoptimizer/tree/master/js.
*
* **Encoder**
*
* The encoder is not currently implemented.
*
* **Decoder**
*
* By default, the configuration points to a copy of the meshopt files on the Babylon.js preview CDN (e.g. https://preview.babylonjs.com/meshopt_decoder.js).
*
* To update the configuration, use the following code:
* ```javascript
* MeshoptCompression.Configuration = {
* decoder: {
* url: "<url to the meshopt decoder library>"
* }
* };
* ```
*/
class MeshoptCompression {
/**
* Default instance for the meshoptimizer object.
*/
static get Default() {
if (!MeshoptCompression._Default) {
MeshoptCompression._Default = new MeshoptCompression();
}
return MeshoptCompression._Default;
}
/**
* Constructor
*/
constructor() {
const decoder = MeshoptCompression.Configuration.decoder;
// eslint-disable-next-line github/no-then
this._decoderModulePromise = Tools.LoadBabylonScriptAsync(decoder.url).then(() => {
// Wait for WebAssembly compilation before resolving promise
return MeshoptDecoder.ready;
});
}
/**
* Stop all async operations and release resources.
*/
dispose() {
delete this._decoderModulePromise;
}
/**
* Decode meshopt data.
* @see https://github.com/zeux/meshoptimizer/tree/master/js#decoder
* @param source The input data.
* @param count The number of elements.
* @param stride The stride in bytes.
* @param mode The compression mode.
* @param filter The compression filter.
* @returns a Promise<Uint8Array> that resolves to the decoded data
*/
async decodeGltfBufferAsync(source, count, stride, mode, filter) {
await this._decoderModulePromise;
if (NumberOfWorkers === 0) {
MeshoptDecoder.useWorkers(1);
NumberOfWorkers = 1;
}
const result = await MeshoptDecoder.decodeGltfBufferAsync(count, stride, source, mode, filter);
// a simple debounce to avoid switching back and forth between workers and no workers while decoding
if (WorkerTimeout !== null) {
clearTimeout(WorkerTimeout);
}
WorkerTimeout = setTimeout(() => {
MeshoptDecoder.useWorkers(0);
NumberOfWorkers = 0;
WorkerTimeout = null;
}, 1000);
return result;
}
}
/**
* The configuration. Defaults to the following:
* ```javascript
* decoder: {
* url: "https://cdn.babylonjs.com/meshopt_decoder.js"
* }
* ```
*/
MeshoptCompression.Configuration = {
decoder: {
url: `${Tools._DefaultCdnUrl}/meshopt_decoder.js`,
},
};
MeshoptCompression._Default = null;
const NAME = "EXT_meshopt_compression";
/**
* [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_meshopt_compression/README.md)
*
* This extension uses a WebAssembly decoder module from https://github.com/zeux/meshoptimizer/tree/master/js
* @since 5.0.0
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
class EXT_meshopt_compression {
/**
* @internal
*/
constructor(loader) {
/**
* The name of this extension.
*/
this.name = NAME;
this.enabled = loader.isExtensionUsed(NAME);
this._loader = loader;
}
/** @internal */
dispose() {
this._loader = null;
}
/**
* @internal
*/
// eslint-disable-next-line no-restricted-syntax
loadBufferViewAsync(context, bufferView) {
return GLTFLoader.LoadExtensionAsync(context, bufferView, this.name, async (extensionContext, extension) => {
const bufferViewMeshopt = bufferView;
if (bufferViewMeshopt._meshOptData) {
return await bufferViewMeshopt._meshOptData;
}
const buffer = ArrayItem.Get(`${context}/buffer`, this._loader.gltf.buffers, extension.buffer);
bufferViewMeshopt._meshOptData = this._loader
.loadBufferAsync(`/buffers/${buffer.index}`, buffer, extension.byteOffset || 0, extension.byteLength)
// eslint-disable-next-line github/no-then
.then(async (buffer) => {
return await MeshoptCompression.Default.decodeGltfBufferAsync(buffer, extension.count, extension.byteStride, extension.mode, extension.filter);
});
return await bufferViewMeshopt._meshOptData;
});
}
}
let _Registered = false;
/**
* Registers the EXT_meshopt_compression glTF loader extension.
* Safe to call multiple times; only the first call has an effect.
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
function RegisterEXT_meshopt_compression() {
if (_Registered) {
return;
}
_Registered = true;
unregisterGLTFExtension(NAME);
registerGLTFExtension(NAME, true, (loader) => new EXT_meshopt_compression(loader));
}
/**
* Re-exports the pure implementation and applies the runtime registration side effect.
* Import "./EXT_meshopt_compression.pure" for tree-shakeable, side-effect-free usage.
*/
RegisterEXT_meshopt_compression();
export { EXT_meshopt_compression, RegisterEXT_meshopt_compression };
//# sourceMappingURL=EXT_meshopt_compression-CTT8fLjx.esm.js.map