GLTFLoader
Quick answer
GLTFLoader reads .glb and .gltf assets and returns an object containing scene, scenes, animations, and cameras. Add gltf.scene to your scene, and hand gltf.animations to an AnimationMixer. Configure DRACOLoader, KTX2Loader, or MeshoptDecoder before loading compressed assets. loadAsync() returns a promise when you prefer async/await over callbacks.
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'
Key points
- Use load(url, onLoad, onProgress, onError) or loadAsync(url) to fetch the asset.
- setDRACOLoader() and setKTX2Loader() must be called before load() for compressed files.
- parse() accepts an ArrayBuffer or JSON string without issuing a network request.
Example
const loader = new GLTFLoader();
const gltf = await loader.loadAsync('/models/building.glb', function (event) {
if (event.lengthComputable) {
console.log('加载进度', (event.loaded / event.total * 100).toFixed(1) + '%');
}
});
const model = gltf.scene;
model.traverse(function (obj) {
if (obj.isMesh) {
obj.castShadow = true;
obj.receiveShadow = true;
if (obj.material && obj.material.map) {
obj.material.map.anisotropy = renderer.capabilities.getMaxAnisotropy();
}
}
});
scene.add(model);
if (gltf.animations.length) {
const mixer = new THREE.AnimationMixer(model);
mixer.clipAction(gltf.animations[0]).play();
mixers.push(mixer);
}import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
const draco = new DRACOLoader();
draco.setDecoderPath('/wasm/draco/');
const ktx2 = new KTX2Loader()
.setTranscoderPath('/wasm/basis/')
.detectSupport(renderer);
loader.setDRACOLoader(draco);
loader.setKTX2Loader(ktx2);
Notes
- Draco or KTX2 assets fail at runtime if the matching decoder was not attached first.
- Removing a loaded model does not free geometry or textures; call dispose() explicitly.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). GLTFLoader. https://www.jingweizhi.com/en/three/api/gltf-loader.html (accessed 2026-11-03).