What does calling InstancedMesh.computeBoundingBox() actually do?

Here is the description for InstancedMesh.computeBoundingBox():

InstancedMesh.computeBoundingBox()

Computes the bounding box of the instanced mesh, and updates InstancedMesh.boundingBox. The bounding box is not automatically computed by the engine; this method must be called by your app. You may need to recompute the bounding box if an instance is transformed via InstancedMesh.setMatrixAt().

Does it calculate a bounding box based on the positions and sizes of the instances as a whole?

Also, is it required to call this method for proper frustum culling of the InstancedMesh?

My understanding is that both computeBoundingBox and computeBoundingSphere are inherited from the Mesh base class,

but for an InstancedMesh, they are not going to produce the correct values for proper frustum culling, so the awkward note in the docs is trying to explain that.

(edit: I am wrong here as @vis_prime points out below. Those methods are in fact implemented now for InstancedMesh to do The Right Thing with instances, however info about whether this occurs automatically is still relevant, so I’m leaving hte rest of my answer up. )

If I have 3 instances in the instancedMesh placed at 0,0,0, 0,10,0, and 0,20,0
computeboundingbox will only compute the bounds of the mesh itself.. not any of the instances… so I would have to manually set its ,boundingBox.min/max to something that encloses 0,0,0 and 0,20,0 plus and minus the dimensions of the mesh itself.

So no.. this method will not enable proper frustum culling, rather, you can only get proper frustum culling by setting the instanceMesh.boundingBox.min and max, by yourself, through some means.. (usually by making a best guess as to how big the bounds will ever be, and initializing it to that… like a loose bounds around the entire cloud of instances )

The reason this is so, is that it’s a costly operation to accurately compute the bounds of a bunch (potentially millions) of instances, so it cannot be performed efficiently every frame.. so it is left up to the user, how, and whether they want to incur the performance hit for doing it.. or instead, just set .frustumCulled=false and take the hit of not having them culled.
It’s not ideal.. but that’s why. :slight_smile:

Edit: Read @vis_prime 's answer below for more up to date/accurate info…

1 Like

yup, its the bounding box of the entire mesh as a whole

it uses the bbox of the source geometry and updates the bbox so that it contains every instance

2 Likes