如何在THREE.js中将等距相机指向立方体的中心?

时间:2015-09-28 10:49:38

标签: javascript 3d three.js geometry

我正在努力解决一个运动问题,也许你可以帮助我。 我必须用THREE.js设置相机和立方体。立方体应保持其初始比率,相机应该看到立方体的中心。 我想我设法做了等距投影,但我无法弄清楚如何将相机指向中心。

以下是我的尝试:

<!DOCTYPE html>
 <html>

 <head>
<meta charset=utf-8>
<title>Cube</title>
<style>

</style>
</head>

<body>
<script src="js/three.min.js"></script>
<script>
        var scene = new THREE.Scene();
        var camera = new THREE.OrthographicCamera(-3,3,3,-3, 0, 100);


        var ambientLight = new THREE.AmbientLight(0x222222);
        scene.add(ambientLight);
        var light = new THREE.PointLight( 0xffffff );
        light.position.set(-10, 10, 10);
        scene.add(light);
        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(800,800);

        document.body.appendChild(renderer.domElement);

        var geometry = new THREE.BoxGeometry(1,1,1);
        var cube = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( { color: 0x00ffff }));
        scene.add( cube );

  function render() { //this is to be used for animation later
  requestAnimationFrame(render);
        camera.position.x=1;
        camera.position.y=1;
        camera.position.z=1;

        camera.lookAt(new THREE.Vector3(0,0,0));
        renderer.render( scene, camera);

        }
 render();
</script>

1 个答案:

答案 0 :(得分:0)

这看起来可以用于什么。在你的情况下,立方体的位置等于它的中心,所以答案是:

camera.lookAt( cube.position );

http://jsfiddle.net/L0rdzbej/135/