camera so 3D object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dodgyville


I’m making a simple object viewer. When a user loads in a object I’d like the camera to move/resize itself so the object fills the view.

var body = load(user_model).instance()

Is there a simple way or technique to place the camera?

Even something basic like moving the camera backwards until the object fits on the screen.

:bust_in_silhouette: Reply From: SIsilicon

The following steps assume the loaded object is a visual instance.

Step 1: Retrieve the mesh’s AABB. This will be needed later on.
Step 2: Put your current camera at the center of this AABB:

//bound_box is the retrieved AABB.
cam.translation = bound_box.position + bound_box.size/2.0

Step 3: Move the camera along its local z axis by the AABB’s size/2.

cam.translation += cam.get_camera_transform().basis.z * bound_box.size.length()/2.0

If all goes well, you should see the entire mesh from any angle.