我有我的應用程序的相機:three.js所旋轉攝像頭在平面
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
職能的工作就像一個魅力和方向,她看到什麼動作相機。
P.S.
這是一個docs到three.js
也許它有幫助。
要處理我用KeyboardJS
這裏鍵盤事件是degInRad
功能:
function degInRad(deg) {
return deg * Math.PI/180;
}
鏈接上JSFiddle
O
- 當前攝像機的方向
- 相機
O-O1
的位置 R1
- 當前旋轉方向
R
- 我想要
對不起好畫面方向。
考慮,而不是使用/修改,'THREE.PointerLockControls'。請參閱http://threejs.org/examples/misc_controls_pointerlock.html。請注意指針鎖定源代碼如何將相機添加爲2個對象的子/孫,以實現所需的效果。 – WestLangley
@WestLangley,這是很好的鏈接,謝謝,但我不需要用鼠標移動相機。 'camera'只用於'controls = new THREE.PointerLockControls(camera); scene.add(controls.getObject());' - 它是'PointerLockControls'內部對象添加到場景中,而不是'camera'本身和'onWindowResize'函數 - 只是應用新屬性,並在'渲染器中。渲染(場景,相機);' - 這裏使用原始的'camera'變量。關於你在說什麼「小孩/孫子」?我真的不明白,你能解釋一下嗎?提前致謝。 – ostapische
注意'PointerLockControls'如何根據需要旋轉相機,除了它使用鼠標而不是鍵盤。爲了達到這個效果,它使用'pitchObject'和'yawObject'技術。查看源代碼。這就是我的意思是「孩子/孫子」。你不必使用'PointerLockControls';你可以自己實現類似的邏輯。 – WestLangley