【问题标题】:using three.js JSONLoader使用three.js JSONLoader
【发布时间】:2012-11-19 06:09:37
【问题描述】:

只是看不到导入到 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 );
}

【问题讨论】:

  • GitHub 上模型的链接已损坏 - 您能否将其更新为新 URL?
  • 模型 js 链接断开

标签: json import three.js loader


【解决方案1】:

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

移动线

scene.add( mesh );

进入加载器回调函数。

【讨论】:

  • 我知道它必须是那么简单。感谢您花时间查看我的代码
【解决方案2】:

认为这可能会帮助任何寻找更准确答案的人:

loader.onLoadComplete=function(){scene.add( mesh )} 

还有完整的加载器参考,请参考这里:

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

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    animate() 也应该在回调函数中,以消除控制台错误。

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2013-04-27
      • 2014-10-26
      • 2013-06-16
      • 2016-06-19
      • 2017-03-19
      相关资源
      最近更新 更多