如何旋转相机使其面向球体上方/上方点(Three.js)

时间:2016-04-18 17:22:39

标签: javascript 3d three.js perspectivecamera

我想知道是否有人知道如何旋转相机,以便相机直接面向球体上的一个点。

更新

Radio的回答是正确的。经过一些调试后,看起来我的坐标被反转了。

1 个答案:

答案 0 :(得分:1)

我创造了一个小提琴:http://jsfiddle.net/kbtn6pz5/

这会将相机移动到球体上的随机点上,应用lookAt属性,然后在球体表面上设置相机的高度。

基于小提琴样板...

    var camera, scene, renderer, geometry, material, mesh;

init();
animate();

function init() {

    scene = new THREE.Scene();

    camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000);




    scene.add(camera);
        var rad = 200;
    geometry = new THREE.SphereGeometry( rad, 32, 32 );
    material = new THREE.MeshNormalMaterial();

    mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

    var point = THREE.GeometryUtils.randomPointsInGeometry( geometry, 1 );

    var altitude = 100;

    var coeff = 1+ altitude/rad;

    camera.position.x = point[0].x * coeff;
    camera.position.y = point[0].y * coeff;
    camera.position.z = point[0].z * coeff;
    camera.lookAt(mesh.position);

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

    document.body.appendChild(renderer.domElement);

}

function animate() {

    requestAnimationFrame(animate);
    render();

}


function render() {
    renderer.render(scene, camera);
}