Dynamically adjust camera filed of view based on focusing object size

I’m trying to get a camera to dynamically adjust its field of view based on the 3D model size that the camera is focusing on. Such that the camera can see the entire model and not just part of it.

For example in this camera tutorial (https://developer.playcanvas.com/en/tutorials/more-cameras/)
I’d like the camera Field of View to auto-adjust based on the plane size. So if the plane get’s bigger the filed of view should increase so that the entire plane is visible, and if the plane gets smaller then the filed of view should decrease until the point where it chips of part of the plane (zoom in, to focus on the plane)

How to go about this? Some magic with raycasting at an angle to detect if the object was collided by the ray or any easier way?

I haven’t done this before so this is guessing:

I would find the extremities of the object (top to bottom, left to right) in relation to the camera position (so using camera space)
Take the longest ‘side’ (eg left to right) and calculate the angle between the direction of camera to left and camera to the right.

That should be the FOV? It may cause a fish eye effect if its too large.

The model view orbit camera does something similar to frame the object so it might be worth looking at that code as well?

1 Like

Thanks @yaustar.
That’s really smart to calculate the angle. I think in my setting it would just be tan inverse of (camera-hegiht/width of model) converted to degree.
Didn’t know about the move view orbit camera will check it out too, thanks.