无法在three.js中加载GLB 3D模型

5

我想使用threejs加载一个GLB格式的3D模型,但是无法正常工作。我按照文档中的步骤进行操作,但是卡住了。

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);

这是我的浏览器渲染出来的样子

1个回答

5

尝试这样做:

  • 只渲染一次场景。不幸的是,你在将资源添加到场景之前就已经这样做了。因此,将 renderer.render(scene, camera); 这行代码移到你的 onLoad() 回调函数中。

  • 尝试远离原点一点。根据模型的大小,你可能需要增加以下值。

camera.position.set( 0, 0, 10 );
  • 在你的场景中添加一些灯光:
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 );

此外,在加载glTF时最好启用sRGB工作流程,如下所示:
renderer.outputEncoding = THREE.sRGBEncoding;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接