相信您在概念上寻找的是相机装备设置。在这种方法中,您首先构建一个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();
}
我本来打算提到 PointLockControl 或 FirstPersonControl,但我看到 WestLangley 提出的建议非常好。总之祝你好运