设置三个PerspectiveCamera的距离,而不更改视图方向

时间:2018-12-18 23:13:48

标签: javascript three.js perspectivecamera

在THREE.js中,我在空间中的某个位置有PerspectiveCamera作为网格的子代。相机仍应看着网格(局部坐标[0,0,0])。我想在不更改视图方向的情况下更改相机与网格的距离。有没有简单的方法可以做到这一点?

camera.position.z = 13 // Works only if position.x and position.y are 0.
camera.setDistanceFromTarget(13) // I need something like this.

我只是考虑手动计算实际摄像机的旋转角度和新摄像机的位置。这是fiddle

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild(renderer.domElement);

var geometry = new THREE.BoxGeometry(3, 3, 3);

var cube = new THREE.Mesh(geometry);
scene.add(cube);
cube.add(camera);

camera.position.set(2, 4, 6)
camera.lookAt(cube.position)

function animate() {
	requestAnimationFrame(animate);
	renderer.render(scene, camera);
}

animate();

// How to set distance of camera from [0, 0, 0] to 13 without change view direction?
camera.position.z = 13;
<!DOCTYPE html>
<html>
	<head>
		<meta charset=utf-8>
		<title>THREE.js camera demo</title>
	</head>
	<body>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js"></script>
	</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要normalize相机位置矢量,然后scalarly multiply以所需的长度:

camera.position.normalize().multiplyScalar(13)

[https://jsfiddle.net/0jnw5guo/]