Moving an object to best fit the camera frustum

I want the object to be as close to the camera as possible but not too close so that the whole body stays in the camera frustum. The camera must not move. The object moves. Any ideas?

Kia ora,

I have a code snippet I got somewhere on here that I use to get the distance an object need to be to fill the camera FOV (apologies- I can’t remember where so can’t credit the person!). Maybe you can use it to find your required object position?

// get distance from object so box fills the screen
const box = new Box3();
box.expandByObject( object );
const size = box.getSize();
const maxSize = Math.max( size.x, size.y, size.z );
const fitHeightDistance = maxSize / ( 2 * Math.atan( ( Math.PI * camera.fov ) / 360 ) );
const fitWidthDistance = fitHeightDistance / camera.aspect;
let distance = fitOffset * Math.max( fitHeightDistance, fitWidthDistance );

I would use this distance, as well as the camera world direction and position to figure out where the object should be

yes, I recognizes this piece of snippet from the community, thanks to the original author. I’m using it to zoom the camera. I’m just wondering how to “zoom” the object over to the fixed camera view.

to “zoom” the camera, you tween the camera position to a new one based on the position of the object and the distance.

To “zoom” the object, flip the calculation around and tween the object position.
Something like this (not tested but to give you an idea)

const direction = camera.getWorldDirection();
direction.normalize().multiplyScalar( distance )
const objectPosition =  camera.position.clone().sub( direction );

thanks! will test it.