首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用three.js JSONLoader

使用three.js JSONLoader
EN

Stack Overflow用户
提问于 2012-12-01 08:25:54
回答 3查看 35.9K关注 0票数 22

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

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-01 10:11:49

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

移动线条

scene.add( mesh );

传入加载器回调函数。

票数 25
EN

Stack Overflow用户

发布于 2014-03-17 06:01:59

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

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

此外,有关完整的加载器参考,请参阅此处:

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

希望这能有所帮助。

票数 5
EN

Stack Overflow用户

发布于 2017-10-15 07:30:03

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13655092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档