Atlaslog
Atlaslog / three.js / Tutorial / Loading models
three.js · Tutorial

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.

Last verified 2026-11-03 · three.js 0.160.0 · Cite this page
Key points
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
Runnable examplesChinese originalAll Tutorials
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Cite this page

Atlaslog (2026). Loading models. https://www.jingweizhi.com/en/three/tutorials/models.html (accessed 2026-11-03).