So I have a bunch of rooms in a space I am showing, and I would like to be able to snap the camera to a good orientation to view the rooms for editing furniture, etc. In my blender file, I placed in empty objects and oriented them in certain positions and rotations in order to have the camera viewing the space that I want to view in that area properly. Each empty camera anchor is a child of an “Area” empty object, and each “Area” empty object is a child of a “Building” empty object in the scene hierarchy. When converting from .blend to .glb for view in Babylon, all of the empty objects obviously get converted to TransformNodes. In code, I would like to set the camera (which is not a child of any node) to the position, rotation (orientation), etc. of each respective transformNode anchor, when appropriate.
The issue I am getting is that the camera is not being placed in the proper position or rotation when the position and rotation of the transformNode anchor are assigned as the camera’s position and rotation.
I tried a couple of methods so far. First, I tried parenting the camera under the same Area anchor, and then setting the camera’s position and rotation to that of the transformNode (due to the difference of local vs global coordinates, especially how blender calculates them versus how they are saved in glb/how babylon calculates them/reads them. This worked for 2/3 of the transformNode anchors, but none of the rotations were applied correctly. Parenting them also oddly mirrored the scene in the camera viewport, so that all the assets appeared mirrored horizontally (not sure why). Because of those issues, I then tried gathering up and performing vector addition on all of the positions of the parents in the hierarchy that I would have to go through to get to the same local position reference as the transformNode anchor. This got the position close, but was slightly off for some unknown reason, and the rotations were still off. Lastly, and what has worked the best but still not sufficiently, I calculated the worldMatrix as such:
const worldMatrix = cameraAnchor.computeWorldMatrix(true);
var newCameraPosition = Vector3.TransformCoordinates(cameraAnchor.position, worldMatrix);
//I applied the same to the newCameraRotation, and also tried just using the regular: newCameraRotation = cameraAnchor.rotation;
This put the camera in the correct position again for only 2/3 of the transformNode anchors (the third anchor is following the same parenting structure as the other two, and is in the same sibling level as the other two in the hierarchy as well, so I am not sure why that is standing out…could it be because the Area associated with this third anchor has more InstancedMeshes and those are somehow throwing off the local coordinate system reference?). It also did not do anything to set the rotations properly.
I tried to reproduce this issue in a playground, but the way the meshes and transformNodes are set up is too complicated to be feasibly shown without having the ability to import my own glb file. Plus, my code is pretty convoluted in setup that that might also be a part of the issue. So really I am looking for any useful thoughts and ideas of what to look into/what functions or collections of functions to try out to fix this issue. Thanks!