三个js第三人称相机不工作

时间:2013-04-09 17:04:06

标签: camera three.js

我正试图在我的物体上实施第三人称相机。

我已经用立方体尝试了它并且工作正常

立方体:

var object, camera;
cubeGeometry = new THREE.CubeGeometry( 50, 50, 50 );
cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000 });
object = new THREE.Mesh( cubeGeometry, cubeMaterial );
scene.add( object );


camera = new THREE.PerspectiveCamera( 45, ASPECT, 0.2, 10000);
camera.position.z = -300;
camera.position.y = 100;
object.add(camera); 
camera.lookAt(object.position);
renderer.render( scene, camera );

但我的模型不起作用:

var object, camera;
var loader = new THREE.OBJMTLLoader();
loader.addEventListener('load', function (event){
    object = event.content;
    object.updateMatrix();
    object.scale.set(20,20,20);
    scene.add(object);
}, false);
loader.load( "models/dragster.obj", "models/dragster.mtl" );

    camera = new THREE.PerspectiveCamera( 45, ASPECT, 0.2, 10000);
    object.add(camera);
camera.position.z = -300;
camera.position.y = 100;    
camera.lookAt(object.position);
renderer.render( scene, camera );

当我将相机添加到它可以工作的场景时,但是当我将它添加到对象时它不会

提前致谢

1 个答案:

答案 0 :(得分:0)

你的问题很简单。在您执行此操作时,OBJ尚未加载:

object.add( camera );

尝试将该代码移动到eventlistener中。