How to inject `@babylonjs/ktx2decoder` into Babylon?

According to the source code, Babylon’s KhronosTextureContainer2 automatically fetches ktx2decoderfrom the CDN (https://cdn.babylonjs.com/babylon.ktx2Decoder.js). However, I’d like to locally host all files and avoid any fetches from outside sources.

(I was on a plane recently and found out mid-flight that Babylon was failing to fetch ktx2decoderfrom the CDN, so I wasn’t able to work :sad_but_relieved_face: . I should have caught this earlier by turning off Wi-Fi to see if my Babylon app could load. I just started using gltfpack on some .glb files, which I just realized activates Babylon’s KTX 2.0 decoder.

Now I’m trying to figure out how to make Babylon use local ktx2decoderJS and WASM files (instead of CDN) to avoid this issue in the future.)

I see the npm package @babylonjs/ktx2decoder. Is there a simple way to use this npm package to load ktx2decoderand then inject this into Babylon (so that it uses the one loaded from npm instead of fetching from the CDN)?

Something along the lines of:

import * as KTX2Decoder from '@babylonjs/ktx2decoder';

globalThis.KTX2DECODER = KTX2Decoder;

// Is there an option to inject `KTX2DECODER` (and its WASM files) into Babylon?

I see another thread and documentation on using @babylonjs/ktx2decoder, however they all seem to be writing some new setup code instead of simply injecting the loaded ktx2decoderinto Babylon.

I really don’t want to be writing new code here, since Babylon’s existing KhronosTextureContainer2works great (but relies on CDN). Can we just make it use the JS and WASM files from the npm package (maybe an option to use an injection)?

Thank you all for your help! :smiley:

Check out this thread of solving similar issue with draco encoder:

Maybe that approach works with krx as well.

1 Like

Thank you for sharing, @qwiglydee ! Unfortunately, ktx2decoder doesn’t have a DefaultConfigurationlike Draco does. And I’d like to avoid Webpack configs or any change to the bundling process

I hope that the Babylon team would know if there exists (or if it’s possible) to have a one-line option to tell ktx2decoderto use local JS and WASM files from @babylonjs/ktx2decoder?

@RaananW thank you for helping on the previous thread! I was wondering if you would have an idea here? :smiley:

Let me add @alexchuber to have a look.

1 Like