drei icon indicating copy to clipboard operation
drei copied to clipboard

Support KTX2 textures in `useGLTF`

Open mattrossman opened this issue 4 years ago • 1 comments

Describe the feature you'd like:

Include a way to enable KTX2Loader in useGLTF to support models with basis compressed KTX2 textures.

This model used in the Three.js examples has KTX2 compressed textures and throws an error when you attempt to load it with drei: facecap.glb

Suggested implementation:

From this example:

const ktx2Loader = new KTX2Loader()
        .setTranscoderPath( 'js/libs/basis/' )
        .detectSupport( renderer );

gltfLoader.setKTX2Loader( ktx2Loader )

I believe we'd need to add the basis transcoder from three.js/examples/js/libs/basis somewhere ~~(probably into three-stdlib)~~ and adjust the path accordingly. I also understand that some special considerations had to be added for MeshOpt compression in three-stdlib/pull/84 to allow for proper tree shaking, we'll probably need to do something similar to allow the Basis stuff to be tree shaken.

API consequence to consider

The number of positional parameters passed to useGLTF is already large and will continue to grow as this and future extensions are added. I propose changing the API to use an object of options rather than positional parameters.

For example:

useGLTF('/model.glb', {
  useMeshopt: true,
  useKtx2: true,
  extendLoader: (loader) => { ... }
})

We could also explore reorganizing the extension flags:

useGLTF('/model.glb', {
  extensions: ['meshopt', 'ktx2'],
  extendLoader: (loader) => { ... }
})

Other thoughts

There's a lot of acronyms around compressed texture support in Three.js that I'm not very familiar with, perhaps someone with more experience in that area could look things over to ensure we support all the Basis stuff properly. E.g. I see there is also a BasisTextureLoader for loading .basis files, though I don't see a corresponding setter method for it on the GLTFLoader like I see with setKTX2Loader()

mattrossman avatar Oct 17 '21 19:10 mattrossman

Since KTX2Loader.setTranscoderPath expects a path where the .js and .wasm Basis transcoder files can be loaded at runtime (i.e. not something that a bundler would know about), perhaps those files should be statically hosted in the drei-assets repo. Or perhaps there is already a reputable host for them, like with the Draco decoder.

mattrossman avatar Oct 17 '21 20:10 mattrossman