title: Adjusting camera for visible Three.js shape
body: <p>I have a CubeGeometry which the camera is looking at, and I want the camera to zoom so that the cube is entirely visible, but no larger.</p>
  
  <p>My initial attempt was to convert the cube verticies to the camera coordinate system,</p>
  
  <pre><code>function toScreenXY(position, camera) {
    var pos = position.clone();
    var projScreenMat = new THREE.Matrix4();
    projScreenMat.multiply(camera.projectionMatrix, camera.matrixWorldInverse);
    projScreenMat.multiplyVector3( pos );
  
    return pos;
  }
  function ScaleInView() {
    camera.fov = 0.0;
    for (var i=0; i&lt;8; i++) {
      proj2d = toScreenXY(cube.geometry.vertices[i],camera);
      angle = 57.296 * Math.max(Math.atan(proj2d.x/proj2d.z), Math.atan(proj2d.y/proj2d.z));
      camera.fov = Math.max(camera.fov,angle);
    }
    camera.updateProjectionMatrix();
  }
  </code></pre>
  
  <p>I thought this would work, but sometimes it's too small, and other times too large (depending on the position of the camera).</p>
  
  <p>I also need to do this for Orthographic Camera.</p>
  
  <p><strong>Edit:</strong>
  I know how to do this when the cube is facing the camera, I'm looking for a way to do it when the camera is moved to some arbitrary (r, theta, phi) position (spherical polar coordinates; r is actually constant for my purposes).</p>