设置 THREE.PerspectiveCamera 的距离而不改变视图方向

Set distance of THREE.PerspectiveCamera without change view direction

我有 PerspectiveCamera 在 THREE.js 的某处 space 作为网格的 children。相机仍应注视网格(局部坐标 [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>

您需要 normalize the camera position vector, and then scalarly multiply 它达到所需的长度:

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

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