Twistere
Twistere

Reputation: 137

I can't load GLB 3D model in threejs

I want to load a GLB 3D model with threejs but it doesn't work anymore. I follow the steps in the documentation but I'm stuck.

import * as THREE from './node_modules/three/src/Three.js';
import { GLTFLoader } from './node_modules/three/examples/jsm/loaders/GLTFLoader.js';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

const loader = new GLTFLoader();



loader.load( './cargo-ship/source/cargo_ship.glb', function ( gltf ) {

    scene.add( gltf.scene );
    console.log('test');
    

}, undefined, function ( error ) {

    console.error( error );

} );

renderer.render(scene, camera);

This is what my browser render

Upvotes: 5

Views: 5669

Answers (1)

Mugen87
Mugen87

Reputation: 31026

Try this:

  • You render your scene only once. Unfortunately, you do this before the asset is added to your scene. Hence, move the line renderer.render(scene, camera); into your onLoad() callback.

  • Try to move the camera a bit away from the origin. Depending on the size of your model, you might want to increase the following values.

camera.position.set( 0, 0, 10 );
  • Add a few lights to your scene:
const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 0.4 );
hemiLight.position.set( 0, 20, 0 );
scene.add( hemiLight );

const dirLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
dirLight.position.set( - 3, 10, - 10 );
scene.add( dirLight );
  • Besides, when loading glTF is good to enable a sRGB workflow like so:
renderer.outputEncoding = THREE.sRGBEncoding;

Upvotes: 5

Related Questions