2

In Three.js I'm using this formulas to calculate visible width & height

var vFOV = camera.fov * Math.PI / 180;        // convert vertical fov to radians
var height = 2 * Math.tan( vFOV / 2 ) * dist; // visible height

var aspect = window.width / window.height;
var width = height * aspect;                  // visible width

And with that I calculate the camera zoom required for object to fit exactly into render area by WIDTH

var zoom = (ObjectHeight/aspect) / (2*Math.tan(vFOV/2)) + ObjectDepth;

How do I calculate the camera zoom required for object to fit exactly into render area by HEIGHT?

Thanks to GuyGood, I found the solution:

var zoom = (ObjectHeight/2) / Math.tan(vFOV/2) - ObjectDepth;
itzikpel
  • 165
  • 3
  • 17
  • Duplicate of http://stackoverflow.com/questions/15331358/three-js-get-object-size-with-respect-to-camera-and-object-position-on-screen/15331885#15331885 If you know `dist`, then you solve for `vFov`. – WestLangley Nov 18 '13 at 23:03
  • I know the dist , and I know the vFov. I need to get the zoom (camera.position.z) – itzikpel Nov 19 '13 at 07:08
  • How can we do it for orthographic camera,since they don't have fov parameter. – Aasha joney Jan 10 '17 at 09:58

1 Answers1

3

I am doing the following which is based on the boundingSphere radius:

geometry.computeBoundingSphere();
radius = geometry.boundingSphere.radius;
distanceFactor = Math.abs( aspect * radius / Math.sin( fov/2 );

This is based on this stuff right here and I hope i interpreted it the right way: http://www.flipcode.com/forums/thread/4172

This distanceFactor is the factor you need to move the camera along its viewing direction to fit it correctly. At the moment i am not sure if it is by height or width but maybe it helps you figure it out. :)

Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73
GuyGood
  • 1,377
  • 1
  • 9
  • 12
  • I do I get the radius? – itzikpel Nov 19 '13 at 07:30
  • 1
    all standard objects have boundingSphere and boundingBox calculated for them, if not, you can call geometry.computeBoundingBox/computeBoundingSphere btw. maybe also see this thread here: http://stackoverflow.com/questions/2866350/move-camera-to-fit-3d-scene – GuyGood Nov 19 '13 at 09:51
  • 1
    how would you do this for orthographic camera as they don't have fov parameter. – Aasha joney Jan 10 '17 at 10:13