GLTF is read incorrectly?

Hey, everyone!

I’ve been working on a way of automatically processing specific meshes in the scene to their proper collision types like the guide on the wiki, but I’m running into a strange problem here.

The monkey head on the right is the GLB model that is being imported, and the white box on the left with a reversed position and rotation is the visible collision box that is being generated by my code.

For some reason when reading the position and rotation values of the GLB file, it’s mirrored across the world X-axis. I can fix the position by typing in -X in my function, but the rotation is no easy fix unfortunately, at least in terms of quaternions. Does anyone know what would cause the input values of a GLB to be scaled over the X-axis like this?

Here is my code for processing the monkey GLB file:

BABYLON.SceneLoader.ImportMesh("", “./textures/monkey.glb”, “”, scene, function(newMeshes)
{
processCollisions(newMeshes);
});

function processCollisions(newMeshes)
{
newMeshes.forEach((m)=>{
if(m.name.indexOf(“box”) != -1){
m.scaling.x = Math.abs(m.scaling.x)
m.scaling.y = Math.abs(m.scaling.y)
m.scaling.z = Math.abs(m.scaling.z)
let tempPhys = BABYLON.MeshBuilder.CreateBox("", { width: m.scaling.x * 2, height: m.scaling.y * 2, depth: m.scaling.z * 2 }, scene);
tempPhys.rotationQuaternion = m.rotationQuaternion;
tempPhys.position = new BABYLON.Vector3(m.position.x, m.position.y, m.position.z);
tempPhys.physicsImpostor = new BABYLON.PhysicsImpostor(tempPhys, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }, scene);
tempPhys.visibility = true;
m.dispose();
}
});
}

I can’t figure out this new fangled code formatting system, so here’s a screencap of the syntax highlighting :joy:

Basically it looks for any objects named “box” in the GLB and uses BABYLON.MeshBuilder to create a box over it with the exact dimensions, multiplied by 2 because for some reason the box created this way is half the size.

Also it’s reading the exact position and quaternionRotation, so I can’t imagine it being anything besides the GLB being misread… As far as I can see quaternionRotation and position are both absolute aren’t they?

Not an expert with glTF at all, but saw a topic yesterday about it being right handed. Perhaps that would explain things.

2 Likes

Yes, after looking around I think you’re right! I don’t mind there being a root node scaled in -Z or however it’s fixed, but if I could calculate what the real location is with this adjustment that’d be great!

GLB seems like a much more convenient format to me.

Oh! I got it! If I parent the new collision box to the same root node that’s being scaled in the -Z it goes to the right location and rotation!

All it needs is to take in newMeshes[0] as the root and parent it and it’s good to go!

Thank you so much for pushing me in the right direction!

2 Likes