As far as I know, I can calculate a model’s aabb by the following code:
var aabb = new pc.BoundingBox();
this.entity.model.model.meshInstances.forEach((mi, i) => {
if (i === 0)
aabb.copy(mi.aabb);
else
aabb.add(mi.aabb);
});
But this aabb is in world space, not model space.
Now I want to create obb for the model, and I need to know the half extents of aabb in model space, so that I can transform it.
To get the tightest AABB possible (and even then, it won’t necessarily by the best fit), you would have to reset all the rotations to 0 and then build the AABB.
Edit: You are also passing the wrong params to pc.OrientatedBox
Your code:
var obb = new pc.OrientedBox(aabb.halfExtents);
Constructor
var OrientedBox = function OrientedBox(worldTransform, halfExtents) {
Edit: You are also passing the wrong params to pc.OrientatedBox
That’s my hand write mistake.
To get the tightest AABB possible (and even then, it won’t necessarily by the best fit), you would have to reset all the rotations to 0 and then build the AABB.
Do you mean I need to do this ?
var obb = new pc.OrientedBox();
var prevQuat = entity.getRotation();
entity.setRotation(pc.Quat.IDENTITY);
var aabb = calculateAabb(); // fake code
obb.halfExtents.copy(aabb.halfExtents);
obb.worldTransform = entity.getWorldTransform();
entity.setRotation(prevQuat);
// draw obb
You have have to work out which frame takes the most space or set the halfextents and center yourself by hand.
Or you have to update the bounding box every frame.
Edit: It should be possible to work out a model space AABB which saves you having to do the double rotation workaround but off the top of head, I wouldn’t know how to do it.