You will need to loop through all of the entities and their mesh instances and add/sum the bounding boxes.
That will give you the total bounding box that encloses all models. You can take a look at the model viewer → orbit-camera.js script, it includes a similar method used to frame your scene.
This is great thanks but I’m creating a new box renderer and trying to scale it to the same size and scale as the bounding box but both the size and scale dont match. I have the following code where
boundingBoxEntitySize is a new pc.BoundingBox();
Thanks, but that code just seems to call some other internal function (this.app.drawWireAlignedBox(this.min, this.max, this.color) and even once I have the result of that I would need to turn the result into an entity at correct position and scale…so effectively I would be doing exactly the same again but instead trying to match the size and position of the wireframe box instead of the boundingBoxEntitySize.
Maybe I should have said that I need the visualization as an entity so I can manipulate the material and do other stuff later.
Yes, I want an entity that matches exactly the bounding box. The above code is really all there is…apart from my bounding box calculation:
// Calculate the bounding box visualization
this.boundingBoxEntitySize = this.calculateBoundingBox();
BoundingBoxVisualizer.prototype.calculateBoundingBox = function () {
var meshInstances = this.entity.render.meshInstances;
if (meshInstances.length > 0)
{
var bbox = new pc.BoundingBox();
bbox.copy(meshInstances[0].aabb);
for (var i = 1; i < meshInstances.length; i++)
{
bbox.add(meshInstances[i].aabb);
}
}
return bbox;
};