How to load a 3D Model
three.js examples
ThreeJS gltf loader demo
Only a few loaders (e.g. ObjectLoader) are included by default with three.js β€” others should be added to your app individually.
1
import { GLTFLoader } from '/jsm/loaders/GLTFLoader.js';
2
​
3
//Model loader
4
const manager = new LoadingManager();
5
const loader = new GLTFLoader(manager).setPath("/assets/models/AyaSofia/");
6
let modelLoaded = false;
Copied!
Once you've imported a loader, you're ready to add a model to your scene. Syntax varies among different loaders β€” when using another format, check the examples and documentation for that loader. For glTF, usage with global scripts would be:
Change the onSelect function to load and place the model, instead of the Sphere mesh we were placing previously.
1
function onSelect() {
2
if (reticle.visible && !modelLoaded) {
3
loader.load(
4
"GM_poly.gltf",
5
function (gltf) {
6
gltf.scene.children[0].position.setFromMatrixPosition(reticle.matrix);
7
scene.add(gltf.scene);
8
modelLoaded = true;
9
},
10
undefined,
11
function (error) {
12
console.error(error);
13
}
14
);
15
}
16
}
Copied!
We can add event callbacks for loading manager.
1
manager.onStart = function (url, itemsLoaded, itemsTotal) {
2
console.log("Started loading file: " + url + ".\nLoaded " + itemsLoaded + " of " + itemsTotal + " files.");
3
};
4
​
5
manager.onLoad = function () {
6
console.log("Loading complete!");
7
};
8
​
9
manager.onProgress = function (url, itemsLoaded, itemsTotal) {
10
console.log("Loading file: " + url + ".\nLoaded " + itemsLoaded + " of " + itemsTotal + " files.");
11
};
12
​
13
manager.onError = function (url) {
14
console.log("There was an error loading " + url);
15
};
16
​
Copied!
See GLTFLoader documentation for further details.

Exercise

  • Try other object types and loaders to add different models to your scene. You can find more models under three.js/examples/models.

Troubleshooting

You've spent hours modeling an artisanal masterpiece, you load it into the webpage, and β€” oh no! 😭 It's distorted, miscolored, or missing entirely. Start with these troubleshooting steps:
  1. 1.
    Check the JavaScript console for errors, and make sure you've used an onError callback when calling .load() to log the result.
  2. 2.
    View the model in another application. For glTF, drag-and-drop viewers are available for three.js and babylon.js. If the model appears correctly in one or more applications, file a bug against three.js. If the model cannot be shown in any application, we strongly encourage filing a bug with the application used to create the model.
  3. 3.
    Try scaling the model up or down by a factor of 1000. Many models are scaled differently, and large models may not appear if the camera is inside the model.
  4. 4.
    Try to add and position a light source. The model may be hidden in the dark.
  5. 5.
    Look for failed texture requests in the network tab, like C:\\Path\To\Model\texture.jpg. Use paths relative to your model instead, such as images/texture.jpg β€” this may require editing the model file in a text editor.
Export as PDF
Copy link