Moving Camera to a Certain Position so that it fully Shows a Specified Geometry

Hey everyone,

oftentimes I find myself wanting to move my camera to a new position so that:

  • I am looking at the center of a certain geometry that I specify
  • from a certain perspective that I specify
  • I am perfectly zoomed so that the whole geometry is visible
  • the transition from my old to my new camera position is direct

What I do for now is:

  1. Look at the center of the geometries bounding box and (assume I want to have top perspective) just put the z-coordinate a little bit above that. Now I have the right perspective but not the right zoom distance.
  2. Then I use ZoomAsync on the bounding box once I have arrived at the right perspective. If i use it before the animation has finished it will stop and zoom out from its current position.

The problem is, that this does not give me a smooth transition. See GIF below. The commands I do in the video are:

`api.scene.camera.updateAsync({position: {x:0,y:0,z:3000}, target: {x:0,y:0,z:0}}, {duration:1500});`

When this command has finished after 1500ms, I go for

`api.scene.camera.zoomAsync([api.scene.get({name:"Test123"},'CommPlugin_1').data[1].scenePath])`


Final Question: How can I get the camera position for looking at a certain geometry so that it is fully shown from a certain perspective without moving my camera to determine this camera position? I want to move the camera only once I am aware of where I want to move it.

Thank you for reading through all of this and any helpful advice that you can give!

David

Hi @DAUD, if I understand well, I have a little trick to make this happen:

//Pause the scene to avoid displaying any changes, for example camera movements.
api.scene.pause();

//Set a timeout to be sure the scene has been paused.`
setTimeout(function () {

//store current camera position
var currentCamera = api.scene.camera.get().data;

//position your camera in the angle in which you want to see your object with a 0 duration
api.scene.camera.updateAsync({
    position:{x:1000,y:1000,z:1000},
    target:{x:0,y:0,z:0}
},
{
    duration: 0
}
).then(function () {
    
    //zoom you camera in the object you want to see with a 0 duration
    api.scene.camera.zoomAsync([scenePath], { duration: 0 }).then(function () {

        //store the final camera position after the zoom
        var zoomCamera = api.scene.camera.get().data;

        //Go back to the original camera position with duration 0
        api.scene.camera.updateAsync(currentCamera, { duration: 0 }).then(function () {

            //resume your viewer
            api.scene.resume();

            //set timeout to make sure the scene was resumed and send the camera update with the info obtained from the previous zoom
            setTimeout(function () {
                api.scene.camera.updateyAsync(zoomCamera, { duration: 2000});
            }, 5);

        });
    });
});
}, 5); 

I hope this helps!

2 Likes

Exactly what I was looking for and even including some tricks that I can use for other things too :slight_smile:

Thank you! I am pretty sure this will work perfectly for me.

David