从三个js中的特定点开始相机旋转

时间:2018-08-27 12:40:23

标签: javascript three.js

我在threejs中加载了一张全景图像,但是它从下面的逻辑开始旋转相机,这在threejs中是默认的

if ( isUserInteracting === false ) {

    lon += 0.1;
}

lat = Math.max( - 85, Math.min( 85, lat ) );
phi = THREE.Math.degToRad( 90 - lat );
theta = THREE.Math.degToRad( lon );

camera.target.x = 100 * Math.sin( phi ) * Math.cos( theta );
camera.target.y = 100 * Math.cos( phi );
camera.target.z = 100 * Math.sin( phi ) * Math.sin( theta );

我想要做的是将相机放置在我可以使用的特定位置上

camera.lookAt( -56.86954186163314,  0,  -71.49481268065709 );

现在,我想从上述lookAt点开始正常旋转相机。我当前正在做的是

camera.lookAt( -56.86954186163314 + camera.target.x,  0,  -71.49481268065709 + camera.target.z);

我认为这是错的。.PS(我的几何,罪孽,cos非常虚弱)。任何一个都可以帮助我吗? PS(我不想更改camera.target.y应该为0)..预先感谢..

1 个答案:

答案 0 :(得分:0)

最好从向量的角度看。

采取您的lookAt位置:这是向量。您可以使用Vector3.applyAxisAngle使矢量绕轴旋转。当您更新矢量时,请让相机看着它。

以您的示例为例,您希望照相机看着-56.86954186163314, 0, -71.49481268065709,然后绕Y轴旋转360°(照相机位置不变,lookAt目标不变更改其Y值)。

var lookVector = new THREE.Vector3();

// later...
lookVector.set(x, y, z); // -56.86954186163314,  0,  -71.49481268065709

// down with your render function...
var axis = new THREE.Vector3(0, 1, 0);

function render(){
    lookVector.applyAxisAngle(axis, 0.001); // or some other theta
    camera.lookAt(lookVector);
    renderer.render(scene, camera);
}

您需要跟踪theta总计达到360°的时间,并执行逻辑来停止旋转,但我将其留给您练习。