使用three.js JSONLoader

rob*_*don 22 import json loader three.js

只是看不到模型导入到three.js场景中.几何图形看起来很好,但无论我应用哪种材料,模型都不会显示.

我是WebGL的新手,所以我很难诊断,但我的猜测是在JSONLoader回调期间出现了问题.

谢谢你的帮助.

var camera, scene, renderer, mesh, loader;

init();
animate();

function init() {

    camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
    camera.position.z = 1000;

    scene = new THREE.Scene();

    loader = new THREE.JSONLoader();

    loader.load( "scripts/model.js", function( geometry ) {
        mesh = new THREE.Mesh( geometry, new THREE.MeshNormalMaterial() );
        mesh.scale.set( 10, 10, 10 );
        mesh.position.y = 150;
        mesh.position.x = 0;
    } );

    scene.add( mesh );

    var ambientLight = new THREE.AmbientLight(0x555555);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight(0xffffff);
    directionalLight.position.set(1, 1, 1).normalize();
    scene.add(directionalLight);

    renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );

    document.body.appendChild( renderer.domElement );

}

function animate() {

    requestAnimationFrame( animate );

    mesh.rotation.x += 0.05;

    renderer.render( scene, camera );
}
Run Code Online (Sandbox Code Playgroud)

Wes*_*ley 25

您在模型完成加载之前将网格添加到场景中.

移动线

scene.add( mesh );
Run Code Online (Sandbox Code Playgroud)

进入loader回调函数.

  • 我知道它必须是那么简单的东西.感谢您抽出宝贵时间查看我的代码 (4认同)

Pro*_*eek 5

认为这可能有助于搜索更准确答案的任何人:

loader.onLoadComplete=function(){scene.add( mesh )} 
Run Code Online (Sandbox Code Playgroud)

还有完整的Loader参考,请参考这里:

https://threejs.org/docs/index.html#api/loaders/Loader

希望这可以帮助.