diff --git a/packages/model-viewer/src/features/loading.ts b/packages/model-viewer/src/features/loading.ts index 31e50265c0..6dfa721b2d 100644 --- a/packages/model-viewer/src/features/loading.ts +++ b/packages/model-viewer/src/features/loading.ts @@ -90,12 +90,14 @@ export declare interface LoadingInterface { export declare interface LoadingStaticInterface { dracoDecoderLocation: string; ktx2TranscoderLocation: string; + meshoptDecoderLocation: string; mapURLs(callback: (url: string) => string): void; } export interface ModelViewerGlobalConfig { dracoDecoderLocation?: string; ktx2TranscoderLocation?: string; + meshoptDecoderLocation?: string; powerPreference?: string; } @@ -114,8 +116,8 @@ export interface ModelViewerGlobalConfig { * * ```html * * ``` @@ -168,6 +170,14 @@ export const LoadingMixin = >( return CachingGLTFLoader.getKTX2TranscoderLocation(); } + static set meshoptDecoderLocation(value: string) { + CachingGLTFLoader.setMeshoptDecoderLocation(value); + } + + static get meshoptDecoderLocation() { + return CachingGLTFLoader.getMeshoptDecoderLocation(); + } + /** * If provided, the callback will be passed each resource URL before a * request is sent. The callback may return the original URL, or a new URL @@ -319,6 +329,10 @@ export const LoadingMixin = >( ModelViewerElement.ktx2TranscoderLocation || DEFAULT_KTX2_TRANSCODER_LOCATION; CachingGLTFLoader.setKTX2TranscoderLocation(ktx2TranscoderLocation); + + if (ModelViewerElement.meshoptDecoderLocation) { + CachingGLTFLoader.setMeshoptDecoderLocation(ModelViewerElement.meshoptDecoderLocation); + } } connectedCallback() { diff --git a/packages/model-viewer/src/three-components/CachingGLTFLoader.ts b/packages/model-viewer/src/three-components/CachingGLTFLoader.ts index ef247de686..4e8327d196 100644 --- a/packages/model-viewer/src/three-components/CachingGLTFLoader.ts +++ b/packages/model-viewer/src/three-components/CachingGLTFLoader.ts @@ -49,6 +49,18 @@ export const loadWithLoader = }); }; +/** Helper to load a script tag. */ +const fetchScript = (src: string): Promise => { + return new Promise((resolve, reject) => { + const script = document.createElement('script'); + document.body.appendChild(script); + script.onload = resolve; + script.onerror = reject; + script.async = true; + script.src = src; + }); +}; + const cache = new Map>(); const preloaded = new Map(); @@ -58,6 +70,18 @@ const dracoLoader = new DRACOLoader(); let ktx2TranscoderLocation: string; const ktx2Loader = new KTX2Loader(); +let meshoptDecoderLocation: string; +let meshoptDecoder: Promise | undefined; + +interface MeshoptDecoder { + ready: Promise; + supported: boolean; +} + +declare global { + const MeshoptDecoder: MeshoptDecoder; +} + export const $loader = Symbol('loader'); export const $evictionPolicy = Symbol('evictionPolicy'); const $GLTFInstance = Symbol('GLTFInstance'); @@ -83,6 +107,19 @@ export class CachingGLTFLoader MeshoptDecoder.ready) + .then(() => MeshoptDecoder); + } + } + + static getMeshoptDecoderLocation() { + return meshoptDecoderLocation; + } + static initializeKTX2Loader(renderer: WebGLRenderer) { ktx2Loader.detectSupport(renderer); } @@ -155,6 +192,10 @@ export class CachingGLTFLoader { progressCallback(progress * 0.8); diff --git a/packages/modelviewer.dev/data/docs.json b/packages/modelviewer.dev/data/docs.json index df3e8c3996..17acb2b660 100644 --- a/packages/modelviewer.dev/data/docs.json +++ b/packages/modelviewer.dev/data/docs.json @@ -112,7 +112,15 @@ "htmlName": "ktx2TranscoderLocation", "description": "This static, writable property sets <model-viewer>'s KTX2 transcoder location URL. By default, the KTX2 transcoder will be loaded from a Google CDN.", "links": [ - "dracoDecoderLocation example" + "ktx2TranscoderLocation example" + ] + }, + { + "name": "meshoptDecoderLocation", + "htmlName": "meshoptDecoderLocation", + "description": "This static, writable property sets <model-viewer>'s Meshopt decoder location URL. By default, the Meshopt decoder is not enabled.", + "links": [ + "meshoptDecoderLocation example" ] }, { diff --git a/packages/modelviewer.dev/data/examples.json b/packages/modelviewer.dev/data/examples.json index db18665490..5dd0443683 100644 --- a/packages/modelviewer.dev/data/examples.json +++ b/packages/modelviewer.dev/data/examples.json @@ -39,6 +39,10 @@ "htmlId": "ktx2Support", "name": "KTX2 Support" }, + { + "htmlId": "meshoptSupport", + "name": "Meshopt Support" + }, { "htmlId": "usdzModel", "name": "USDZ Model" diff --git a/packages/modelviewer.dev/examples/loading/index.html b/packages/modelviewer.dev/examples/loading/index.html index 61e26a6bd7..a3ceeee275 100644 --- a/packages/modelviewer.dev/examples/loading/index.html +++ b/packages/modelviewer.dev/examples/loading/index.html @@ -41,7 +41,7 @@ -
+
@@ -57,7 +57,7 @@

Display a poster until loaded

- +