Calculate the camera scale required to fit the object to the height of the screen

In Three.js, I use these formulas to calculate the visible width and 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 this, I calculate the camera scale needed to ensure that the object fits exactly into the visualization area using WIDTH

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

How to calculate the scale of the camera needed to ensure that the object fits exactly in the visualization area in height?

Thanks to GuyGood, I found a solution:

 var zoom = (ObjectHeight/2) / Math.tan(vFOV/2) - ObjectDepth; 
+1
javascript 3d
source share
1 answer

I do the following based on the Sphere bounding radius:

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

This is based on this material right here, and I hope I understand it correctly: http://www.flipcode.com/forums/thread/4172

This distanceFactor is a factor necessary to move the camera in the direction of view so that it matches it. At the moment, I'm not sure if it is in height or in width, but maybe this helps you figure it out. :)

+1
source share

All Articles