ost*_*che 4 javascript camera rotation perspectivecamera 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);
Run Code Online (Sandbox Code Playgroud)
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));
}
Run Code Online (Sandbox Code Playgroud)
相机旋转,但不是我需要的方式.我希望那台相机在飞机上像FPS(第一人称射击游戏)一样旋转.看图片了解我想要的东西......
我试着用它sin(1),cos(1)但无法理解它是如何rotateOnAxis工作的,使translate功能像魅力一样工作,并将相机移动到她所看到的方向.
PS
这里是一个文档在three.js它帮助也许.
要处理键盘事件我使用KeyboardJS
这是一个degInRad函数:
function degInRad(deg) {
return deg * Math.PI / 180;
}
Run Code Online (Sandbox Code Playgroud)
关于JSFiddle的链接

O- 相机的位置
O-O1- 当前的相机方向
R1- 当前的旋转方向
R- 方向我想要的
对不起的好照片.
Ice*_*You 14
你可以通过设置得到你想要的东西 camera.rotation.order = 'YXZ';
相信你在概念上寻找的是相机装备设置.在这种方法中,您首先构建一个Object3D"颈部",然后将相机连接到具有所需旋转的对象.然后你可以应用旋转Object3D来环顾四周而不会出现摆动效果.为了进一步实现它,您可以将"颈部"对象嵌套在另一个Object3D中作为"主体",并将翻译应用于该对象以在场景中移动.通过这种方式,您可以使用基本的相机控制器装备 我这样修改你的小提琴:
编辑:实现更改以更好地适应@ostapische用例.这是小提琴链接
function degInRad(deg) {
return deg * Math.PI / 180;
}
/////////////////////////////////
function render() {
requestAnimationFrame(render);
if (leftPressed) {
neck.rotation.y += degInRad(1);
} else if (rightPressed) {
neck.rotation.y -= degInRad(1);
}
if (upPressed) {
camera.rotation.x += degInRad(1);
} else if (downPressed) {
camera.rotation.x -= degInRad(1);
}
renderer.render(scene, camera);
}
/////////////////////////////////
var scene, camera, renderer, grass, neck;
var leftPressed = false;
var rightPressed = false;
var upPressed = false;
var downPressed = false;
/////////////////////////////////
window.onload = function() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
cameraHolder = new THREE.Object3D();
cameraHolder.add( camera );
renderer = new THREE.WebGLRenderer();
renderer.rendererSize = {width: window.innerWidth, height: window.innerHeight, quality: 100, maxQuality: 400, minQuality: 20};
renderer.setSize( renderer.rendererSize.width, renderer.rendererSize.height );
document.body.appendChild( renderer.domElement );
var texture, material, geometry, element;
material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
material.side = THREE.DoubleSide;
geometry = new THREE.PlaneGeometry(24, 24);
grass = new THREE.Mesh( geometry, material );
grass.name = "grass";
scene.add( grass );
neck = new THREE.Object3D();
neck.rotateOnAxis(new THREE.Vector3(1, 0, 0), degInRad(90));
neck.up = new THREE.Vector3(0, 0, 1);
neck.position.z = 1;
neck.position.y = -5;
neck.add(camera);
scene.add(neck);
KeyboardJS.on('left', function() { leftPressed = true; }, function() { leftPressed = false; });
KeyboardJS.on('right', function() { rightPressed = true; }, function() { rightPressed = false; });
KeyboardJS.on('up', function() { upPressed = true; }, function() { upPressed = false; });
KeyboardJS.on('down', function() { downPressed = true; }, function() { downPressed = false; });
render();
}
Run Code Online (Sandbox Code Playgroud)
我要提到PointLockControl或FirstPersonControl,但是我看到WestLangley非常友好.无论如何祝你好运,