Hi,
Anyone got an example that loads and displays a GLTF that uses KTX2 textures?
The KTX2 demo on the threejs website is just loading a texture and applying it to a mesh.
I have a GLTF which I modified to use KTX2 textures (trimmed down version of the json in the GLTF is below).
The following code which works for normal GLTF and Draco but doesn’t if I use KTX2, it errors with:
THREE.GLTFLoader: setKTX2Loader must be called before loading KTX2 textures
Anyone got any ideas and even better a working example online with source code I can review? The example on the threejs website is just loading a texture in and applying it to
a mesh, I don’t really want to setup all the textures for complex models manually on every GLTF I get supplied from the designers.
Anyone got any ideas or working examples?
Here's the code (trimmed down for pasting here)
import * as THREE from '../../node_modules/three/build/three.module';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { KTX2Loader } from "three/examples/jsm/loaders/KTX2Loader";
class Preview {
// var defs
constructor(){
// do lots of things setup three etc
await this.loadModel();
// do more things
}
async loadModel() {
log(`Preview.loadModel()`);
this.ktx2Loader = new KTX2Loader();
this.ktx2Loader.setTranscoderPath('./includes/js/libs/basis/');
this.ktx2Loader.detectSupport(this.renderer);
const loader = new GLTFLoader();
loader.setKTX2Loader = this.ktx2Loader;
// Works for Draco
// const dracoLoader = new DRACOLoader();
// dracoLoader.setDecoderPath('./includes/js/libs/draco/');
// loader.setDRACOLoader(dracoLoader);
await loader.load(
"./assets/models/" + this.resource,
// loaded
async (gltf) => {
this.meshes.gallery = gltf.scene;
this.scene.add(this.meshes.gallery);
},
(xhr) => {
let p = Math.ceil(xhr.loaded / xhr.total * 100);
log((xhr.loaded / xhr.total * 100) + '% loaded');
if (p < 100) {
this.dom.preloaderMessage.innerHTML = `Loading ${p}%`;
}
}
);
}
}
Here’s the GLTF (Trimmed down for pasting here)
{
"asset": {
"generator": "Khronos glTF Blender I/O v1.7.33",
"version": "2.0"
},
"extensionsUsed": [
"KHR_texture_basisu"
],
"extensionsRequired": [
"KHR_texture_basisu"
],
"scene": 0,
"scenes": [
{
"name": "Scene",
"nodes": [
0,
"TRIMMED ARRAY 0 to 24 FOR PASTING ON WEBSITE",
24
]
}
],
"nodes": ["REMOVED FOR PREVIEW - USUAL STUFF"],
"materials": [
{
"name": "TableSupports",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 0
},
"metallicFactor": 0,
"roughnessFactor": 0.5
}
},
{
"name": "TablesMid",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 1
},
"metallicFactor": 0
}
},
{
"name": "BenchesMid",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 2
},
"metallicFactor": 0
}
},
{
"name": "Walls",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 3
},
"metallicFactor": 0,
"roughnessFactor": 0.5
}
},
"AND MORE"
],
"meshes": [
{
"name": "Cylinder.007",
"primitives": [
{
"attributes": {
"POSITION": 0,
"NORMAL": 1,
"TEXCOORD_0": 2
},
"indices": 3,
"material": 0
}
]
},
{
"name": "Cube.042",
"primitives": [
{
"attributes": {
"POSITION": 4,
"NORMAL": 5,
"TEXCOORD_0": 6
},
"indices": 7,
"material": 1
}
]
},
{
"name": "Cube.043",
"primitives": [
{
"attributes": {
"POSITION": 8,
"NORMAL": 9,
"TEXCOORD_0": 10
},
"indices": 11,
"material": 2
}
]
},
{
"name": "Cube.045",
"primitives": [
{
"attributes": {
"POSITION": 12,
"NORMAL": 13,
"TEXCOORD_0": 14
},
"indices": 15,
"material": 3
}
]
},
{
"name": "Cube.048",
"primitives": [
{
"attributes": {
"POSITION": 16,
"NORMAL": 17,
"TEXCOORD_0": 18
},
"indices": 19,
"material": 4
}
]
},
"AND MORE...."
],
"textures": [
{
"sampler": 0,
"source": 0,
"idx": 0,
"extensions": {
"KHR_texture_basisu": {
"source": 0
}
}
},
{
"sampler": 0,
"source": 1,
"idx": 1,
"extensions": {
"KHR_texture_basisu": {
"source": 1
}
}
},
{
"sampler": 0,
"source": 2,
"idx": 2,
"extensions": {
"KHR_texture_basisu": {
"source": 2
}
}
},
"THERE ARE MORE..........."
],
"images": [
{
"mimeType": "image/ktx2",
"name": "TableSupports_BC",
"uri": "TableSupports_BC.ktx2",
"idx": 0
},
{
"mimeType": "image/ktx2",
"name": "TablesMid_BC",
"uri": "TablesMid_BC.ktx2",
"idx": 1
},
{
"mimeType": "image/ktx2",
"name": "BenchesMid_BC",
"uri": "BenchesMid_BC.ktx2",
"idx": 2
},
"THERE ARE MORE..........."
],
"accessors": ["REMOVED FOR PREVIEW - USUAL STUFF"],
"bufferViews": ["REMOVED FOR PREVIEW - USUAL STUFF"],
"samplers": ["REMOVED FOR PREVIEW - USUAL STUFF"],
"buffers": ["REMOVED FOR PREVIEW - USUAL STUFF"]
}
```