【发布时间】:2013-09-20 23:11:56
【问题描述】:
我的应用中有一个摄像头:
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 1;
camera.position.y = -5;
camera.rotateOnAxis(new THREE.Vector3(1, 0, 0), degInRad(90));
camera.up = new THREE.Vector3(0, 0, 1);
render 函数中的那些代码必须在我按下按键时旋转相机:
if (leftPressed) {
camera.rotateOnAxis((new THREE.Vector3(0, 1, 0)).normalize(), degInRad(1));
} else if (rightPressed) {
camera.rotateOnAxis((new THREE.Vector3(0, 1, 0)).normalize(), degInRad(-1));
}
if (upPressed) {
camera.rotateOnAxis((new THREE.Vector3(1, 0, 0)).normalize(), degInRad(1));
} else if (downPressed) {
camera.rotateOnAxis((new THREE.Vector3(1, 0, 0)).normalize(), degInRad(-1));
}
相机旋转,但不是我需要的方式。我希望相机像飞机上的 FPS(第一人称射击游戏)一样旋转。看图了解我想要什么...
我尝试使用sin(1) 和cos(1),但无法理解rotateOnAxis 的工作原理,因为translate 的功能就像魅力一样工作,并且可以将相机朝她看到的方向移动。
PS
这是docs 到three.js 的地址,也许有帮助。
为了处理键盘事件,我使用KeyboardJS
这是一个degInRad 函数:
function degInRad(deg) {
return deg * Math.PI / 180;
}
链接JSFiddle
O - 相机位置O-O1 - 当前相机方向R1 - 当前旋转方向R - 我想要的方向
抱歉好图片。
【问题讨论】:
-
请考虑使用/修改
THREE.PointerLockControls。见threejs.org/examples/misc_controls_pointerlock.html。请注意在指针锁定源代码中如何将相机添加为 2 个对象的子/孙子对象以实现您想要的效果。 -
@WestLangley,这是一个很好的链接,谢谢,但我不需要用鼠标移动相机。而
camera仅在controls = new THREE.PointerLockControls( camera ); scene.add( controls.getObject() );中使用——它是PointerLockControls内部对象添加到场景中,而不是camera本身和onWindowResize函数中——只是应用新属性,在renderer.render( scene, camera );中——这里使用的是原始@987654349 @ 多变的。关于child/grandchild你在说什么?实在看不懂,能解释一下吗?提前致谢。 -
请注意
PointerLockControls如何随心所欲地旋转相机,但它使用鼠标而不是键盘。为了达到这个效果,它使用了pitchObject和yawObject技术。请参阅源代码。这就是我所说的child/grandchild。您不必使用PointerLockControls;你可以自己实现类似的逻辑。 -
@WestLangley 这是我的失败。我在页面上没有看到PointerLockControls 的源代码。现在我试一试,如果一切顺利,我会回答一个问题或问你是否犯了错误。
-
@WestLangley 我写了工作示例there。但是我不明白为什么three.js 的开发人员在代码中使用那些
pitchObject.rotation.x = Math.max( - PI_2, Math.min( PI_2, pitchObject.rotation.x ) );构造。当我在代码中取消注释此行时,垂直移动效果不好。你可以解释吗?谢谢。
标签: javascript camera rotation three.js perspectivecamera