Loading models
Quick answer
This tutorial loads building and equipment models through GLTFLoader with DRACOLoader and KTX2Loader attached for compression and texture decoding. It shows three corrections every imported asset needs: measure the Box3 size and rescale to target metres, recenter the ground point, and wrap Z-up BIM content in a Group rotated minus 90 degrees. It also covers traverse-based shadow flags, material consolidation and SkeletonUtils.clone for rigged copies. Readers can bring real assets into a scene at the right size and orientation.
Key points
- Attach DRACOLoader and KTX2Loader to GLTFLoader before calling load.
- Scale by the Box3 maximum dimension, then recenter the model on the ground.
- Wrap Z-up BIM models in a Group rotated minus 90 degrees about the X axis.
Example
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
const draco = new DRACOLoader();
draco.setDecoderPath('https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/libs/draco/');
const ktx2 = new KTX2Loader();
ktx2.setTranscoderPath('https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/libs/basis/');
ktx2.detectSupport(renderer);
const loader = new GLTFLoader();
loader.setDRACOLoader(draco);
loader.setKTX2Loader(ktx2);
loader.load(
'/models/block-a.glb',
function (gltf) {
const model = gltf.scene;
model.traverse(function (obj) {
if (obj.isMesh) {
obj.castShadow = true;
obj.receiveShadow = true;
}
});
scene.add(model);
},
function (event) {
if (event.total) {
console.log('加载进度', Math.round(event.loaded / event.total * 100) + '%');
}
},
function (error) {
console.error('模型加载失败', error);
}
);const box = new THREE.Box3().setFromObject(model);
const size = box.getSize(new THREE.Vector3());
const maxDimension = Math.max(size.x, size.y, size.z);
const targetMeters = 120;
const scale = targetMeters / maxDimension;
model.scale.setScalar(scale);
box.setFromObject(model);
const center = box.getCenter(new THREE.Vector3());
model.position.x -= center.x;
model.position.z -= center.z;
model.position.y -= box.min.y;
Notes
- Progress totals are missing when the server strips the Content-Length header.
- model.clone shares skeletons, so rigged copies need SkeletonUtils.clone.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Loading models. https://www.jingweizhi.com/en/three/tutorials/models.html (accessed 2026-11-03).