I have a scene with multiple models (GLTF models) and would like to place camera in order to see all of the models placed randomly on screen.How can I achieve that?

Thanks in advance

you find some code and pointers here: Camera Zoom To Fit Object - #35 by pinki generally, search for â€śzoom to fitâ€ť.

itâ€™s still a complex issue and iâ€™ve not been able to find code that suits all usecases, camera types (orthographic/oerspective), animated. iâ€™ve searched through all forums i knew, stackoverflow, etc.

scraped every bit of info i could find together to form a magic bullet component for react + three: `<Bounds/>`

, and hereâ€™s a demo: Bounds and makeDefault - CodeSandbox but itâ€™s open source, if the discourse link above isnâ€™t enough you can cross check the components source.

@drcmda Yes I went through this post but couldnâ€™t make functions Iâ€™ve found over the net work even for one object. Iâ€™ve managed to create a pseudo bounding box object for the entire scene and didnâ€™t know what to do with it. Thank you for your component, Iâ€™ll try find the code but Iâ€™m using Angular at this point. Many thanks in advance.

i feel your pain, iâ€™ve had such a hard time - mostly because i struggle a lot with math, but thought something simple like that surely cannot be so hard. maybe ask him: https://twitter.com/Nartc1410/status/1496352326428282884 heâ€™s translated most of these drei components to angular already.

@drcmda Thanks for your answer. Iâ€™m looking at the getSize() function and donâ€™t understand how I could get size of Scene or Group in the manner you wrote it. At this point you retrieve Vector of default Box object. Any ideas? Thanks in advance.

In fact Iâ€™ve found the solution by copying part of your code for perspectiveCamera.Only problem I have now is that the camera donâ€™t face the objects, instead I get to see the objects from below ( still, Iâ€™m able to see all objects ). any Idea how I could avoid this behaviour? Thnaks in advance.

Hereâ€™s my code:

```
export const fitCameraToObjects = ( group:THREE.Group, camera:THREE.PerspectiveCamera ) => {
const box = new THREE.Box3().setFromObject( group );
const size = box.getSize( new THREE.Vector3() );
const center = box.getCenter( new THREE.Vector3() );
const maxSize = Math.max( size.x, size.y );
const margin = 1.2;
const fitHeightDistance = maxSize / ( 2 * Math.atan( ( Math.PI * camera.fov) / 360 ) );
const fitWidthDistance = fitHeightDistance / camera.aspect;
const distance = margin * Math.max( fitHeightDistance, fitWidthDistance );
const currentCameraPos = new THREE.Vector3().copy( camera.position )
const direction = center.clone().sub( camera.position ).normalize().multiplyScalar( distance )
const goalCameraPos = new THREE.Vector3().copy( center ).sub( direction )
const goalFocusVec = new THREE.Vector3().copy( center );
camera.position.copy( goalCameraPos )
camera.lookAt( goalFocusVec )
}
```

Itâ€™s actually not that hard. And it gets easier when you make a little sketch first:

Letâ€™s assume you have computed the * bounding sphere* â€śBSâ€ť of your collective geometry, having a radius of â€śrâ€ť. Hereâ€™s how to do that.

Two cases to consider:

- find the minimum Radius â€śRâ€ť for a given â€śfovâ€ť
- find the minimum â€śfovâ€ť for a given radius â€śRâ€ť

which are **guaranteed** to show all thatâ€™s inside of the bounding sphere silhouette.

The respective â€śminimumâ€ť is given by the requirement, that the frustum emanating from the camera just barely touches (tangent condition!) the bounding sphere â€śBSâ€ť. The tangent condition implies a **right angle** between the red BS-sphere radius â€śrâ€ť and the ray â€śLâ€ť touching the sphere.

Itâ€™s required, that the bounding sphere â€śBSâ€ť radius â€śrâ€ť is always known.

We can infer, that `L * L + r * r = R * R`

We can also infer, that `R * sin ( fov / 2 ) = r`

Thatâ€™s two equations having two unknows, so itâ€™s not hopeless.

Solving for case 1:

`R â‰Ą r / sin( fov / 2 )`

Solving for case 2:

`fov â‰Ą 2 * asin( r / R )`

Thatâ€™s about it.

@vielzutun.ch Hey thanks for your answer do you have same but for bounding boxes. Thanks in advance.

Bounding * spheres* have the advantage, that their silhouette is a

*constant*size circular shape for a given â€śRâ€ť-â€śfovâ€ť combination, regardless of the angle from which the camera is looking at the bounding sphere.

A bounding * box* will appear as

*differently*sized, depending on the angle from which you are looking at it. So, for that case Iâ€™m unable to provide formulas as above which would work from any direction of view.