Making Camera from GLTF file be a universal camera

Hello Everyone, I am trying to get a camera created with Babylonjs to match the position and rotation of a camera from a loaded gltf file. (It’s a long story why I need to do this…)

This is what I did:
cameraFromFile = scene.getNodeByName(“Camera”); //This is the camera inside the gltf file. I also tried getCameraByName(“Camera”) but it doesn’t hold any position or rotation information. So getNodeByName seems to be the right way.

I can match the position of the babylonjs camera with the gltf camera ok by doing this:
camera.position.x = cameraFromFile.position.x;
camera.position.y = cameraFromFile.position.y;
camera.position.z = cameraFromFile.position.z;

but I am having trouble getting the rotations to match.

I just need the y rotation to match (looking left and right) The other rotations are not important.

I know I need to assign the camera.rotation.y but I am not sure how to get that from the cameraFromFile:
camera.rotation.y = [cameraFromFile rotation y goes here]

I know there’s this:

But I am not sure how to get the rotation from the quaternion or if there’s some other way to do what I am trying to do.

Does anyone know the solution?

Hello and welcome!

Your logic is right but there are more simple ways do to it:

const cameraFromFile = scene.getCameraByName("Camera");
scene.activeCamera = cameraFromFile;

Example -

You may play with cameras in the Inspector - turn them on and off with the camera sign

1 Like

Thank you very much for taking time to help me!

I did try that but I found I could not get it to work the same way as a universal camera created with babylonjs. I wanted free movement with arrow keys but the keys didn’t move in the correct direction for some reason. Also collisions didn’t work when I attached an ellipsoid to the camera.

Everything seemed to work fine with the babylonjs camera so I figured matching the cameras may be a good solution. Also in the future I wanted to use cameras from the file as markers to position and align things for a variety of purposes.

Is there a way to make the direction keys and collisions work for the caneraFromFile? And just for my understanding is there a way to pull out the rotation from the camera in the loaded scene?

In the example - - camera direction keys seem to be OK.
It may depend on the mode in which camera was created and saved to GLB. Below is the screenshot from Blender, as one may see, there is a lot of options there.

In Blender, what should be the mode that allows for correct movement?

Here is an example of what’s wrong with the movement:

If you uncomment line 31 and make it use the babylonjs camera it works fine.

You may set
scene.activeCamera.parent = null;
to get rid of transform nodes which define camera position and rotation.
And seems it works fine but actually there is no need to use GLTF camera in this case -

That’s amazing! Thank you, I understand now.

I know the GLTF camera isn’t really needed, but it’s nice to be able to use Blender to create the camera, position it, set FOV, clip distance etc and then it becomes the Babylonjs camera.

It is good for the static render or if the camera doesn’t move, otherwise one needs to get rid of transform nodes.
Personally for me it is much easier to set all camera parameters just in code, because while tuning a scene there inevitably will be the need to tune them more :slight_smile:

1 Like

I flagged a random one from @labris cause I guess the whole conversation helped :slight_smile:

1 Like