How to set camera as per height of 3d-model


I want to set set camera so that whole 3d object should come inside FOV.

In IMAGE (Shared below ) a 3d-model having height 15m and one having 45m .

I want camera position should change when I import 1st 3d-model so that whole model come inside its FOV.

and when I import 2nd 3d-model camera position should changed accordingly so that 2nd 3d model comes in its FOV.

importing one model at a time


Thanks in advance!

And if you’re not a great fan of calculating a visible area, you can try the following (it’s a bit of an inverse):

  1. Place the camera in a way that a cube of size 1 x 1 x 1 is visible without zooming.
  2. Load and place the model.
  3. Use Box3 to calculate the size of the model.
  4. Calculate the maximum difference in size for all axes:
const delta = Math.min(1.0 / modelSize.x, 1.0 / modelSize.y, 1.0 / modelSize.z);
  1. Scale the model down (or the camera position up) by the calculated delta.

(If you’re rotating the camera a lot, this way is a bit less troubling than calculating the visible viewport - since you’re scaling the models to fit the view, not the camera to fit the models.)


Hi @mjurczyk

Thank you so much
code is working fine.

just a correction in code

const delta = Math.min(1.0 / modelSize.x, 1.0 / modelSize.y, 1.0 / modelSize.z);

1 Like

(fyi, I did update the original post for clarification - no need to duplicate it, unless you added some changes?)

sorry!! :slight_smile: