KTX2 Transcoder Support Progress

Yo @bghgary … Is there any progress on the KTX2 support.

I dont see libktx.js and libktx.wasm in the preview relese folder for 4.2 … Are we not using those anymore ?

What is the stuff in the KTX2 Transcoder folder… Is that for KTX2 in GLTF… And how do we use it ?

Thanks again for all you help… As Always :slight_smile:

Adding @Evgeni_Popov who just finished some implementations yesterday.

There is a new implementation of the KTX2 loader, see here for more information: https://github.com/BabylonJS/Babylon.js/pull/8775

We don’t use libktx anymore but the universal texture transcoders + msc transcoder instead. The footprint is a lot less than with libktx (19ko + 15ko to transcode UASTC to BC7 (standard formats on PC)) vs 1070ko for libktx.js + libktx.wasm. Also, performances are a lot better, see https://github.com/BabylonJS/Babylon.js/pull/8775#issuecomment-687332030.

You have nothing special to do to use the new loader/decoder, just load/use ktx2 files! For GLTF files, you need to use the KHR_texture_basisu extension.

1 Like

Yo @Evgeni_Popov … i need just a bit more clarification…

so to get the ktx2 file working for gtlf…

1… What javascript libs do i need to load on the page (since we dont use libktx.js)

2… What should the texture filenames be (.ktx or .ktx2)

3… Do i still need a .png/.jpg version of the texture (for fallback or something)

4… Is there anything i need to load in the gltf some kind of extension properties or extras for the KHR_texture_basisu

and finally is there any kind of write up on this stuff…

I ask because i am trying to add default support for ktx2 compressed textures to he Babylon Toolkit - Unity Exporter :slight_smile:

Yo @Deltakosh … How do we use .ktx2 textures for Skybox CubeTexture… i tried setting extension _px_ktx2 … but no luck… looks like the parser does not take into account .ktx2 file extensions

None, the babylon.ktx2Decoder.js file needed to decode ktx2 files will be automatically loaded by the ktx2 loader when needed (from https://preview.babylonjs.com/babylon.ktx2Decoder.js).

Should be .ktx2

You don’t need to provide a fallback (at least in Babylon context) because the fallback is for gltf loaders that don’t support the KHR_texture_basisu extension.

Yes, see the spec: glTF/README.md at 0ac09684d67ee8feea417832da3085ac25daeb91 · KhronosGroup/glTF · GitHub

You basically just need to add something like this in a gltf texture:

      "source": 10, <== only if you want fallback
      "extensions": {
        "KHR_texture_basisu": {
          "source": 0

And at the root level:

  "extensionsUsed": [

Not yet as the spec is not finalized.

The ktx2 loader does not support cubemaps yet.

I don’t really like loading from Babylon preview server to get ktx2 decoder

Can I just explicitly put that on the page … and there any others besides ktx2 decoder script that I should be putting on page

You can set the BABYLON.KhronosTextureContainer2.JSModuleURL property as you want. By default, the value is https://preview.babylonjs.com/babylon.ktx2Decoder.js.

Depending on the source and transcoded format, there are other files that are downloaded dynamically:

  • UASTC -> ASTC_4x4_RGBA: the wasm module pointed to by KTX2DECODER.LiteTranscoder_UASTC_ASTC.WasmModuleURL is downloaded (default value: https://preview.babylonjs.com/ktx2Transcoders/uastc_astc.wasm).
  • UASTC -> BC7_RGBA: the wasm module pointed to by KTX2DECODER.LiteTranscoder_UASTC_BC7.WasmModuleURL is downloaded (default value: https://preview.babylonjs.com/ktx2Transcoders/uastc_bc7.wasm).
  • other transcoding: the js module pointed to by KTX2DECODER.MSCTranscoder.JSModuleURL (default value: https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.js) and the wasm module pointed to by KTX2DECODER.MSCTranscoder.WasmModuleURL (default value: https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.wasm) are downloaded .

For the time being those values can’t be changed because the ktx2 decoder module is imported in worker threads so you can’t access those properties.

Maybe we should provide a way to change the values? @bghgary?

So your saying … i should not put ktx2 stuff on the page and let the loaders auto-magic the loading of any need libs to decode the ktx2 image referenced in the GLTF (i am requiring the basis extension)

Yes indeed.

I have not really been keeping up with this, but taking a look at this, I wonder how Babylon native would get any of the libs?

Also, for some platforms, like headsets, it will not change what format to use.

Yeah, we should probably provide a way to configure this. I didn’t realize this can’t be changed right now.

Depending on the situation, you might still want to host the files and reconfigure the decoder to point to your own copies. This will prevent newer versions of the decoder from breaking older code.

Babylon Native should be able to consume this as is. There is nothing special about this from the native perspective. The only thing is that Babylon Native currently doesn’t have web workers which will come eventually.

1 Like

Here’s the PR to be able to configure the URLs:

This PG is showing the default value used for each URL: https://playground.babylonjs.com/#EIJH8L#10


Yo @bghgary … so is this the final way to load my own local copies of the various ktx2 libraries ???

so i assume i need to setup the url config before i use the SceneLoader to load a gltf that is using ktx2 files

if (BABYLON.KhronosTextureContainer2.URLConfig) {
        BABYLON.KhronosTextureContainer2.URLConfig.jsDecoderModule = "https://preview.babylonjs.com/babylon.ktx2Decoder.js";
        BABYLON.KhronosTextureContainer2.URLConfig.wasmUASTCToASTC = "https://preview.babylonjs.com/ktx2Transcoders/uastc_astc.wasm";
        BABYLON.KhronosTextureContainer2.URLConfig.wasmUASTCToBC7 = "https://preview.babylonjs.com/ktx2Transcoders/uastc_bc7.wasm";
        BABYLON.KhronosTextureContainer2.URLConfig.jsMSCTranscoder = "https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.js";
        BABYLON.KhronosTextureContainer2.URLConfig.wasmMSCTranscoder = "https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.wasm";

@Evgeni_Popov is probably better suited to answer this.

Yes, this is the way to go if you don’t want to use the default library locations.

Note that there are some more urls, see doc: KhronosTextureContainer2 | Babylon.js Documentation

Updated PG with all urls: https://playground.babylonjs.com/#EIJH8L#22


Hi Evgeni, would you expect this to work with a .glb file as well? I’m struggling to get a .glb model that I have with ktx2 to load up in my scene.

Ignore that ^ I just managed to get it to work - I may have had some pathing wrong or something!