Activating Mesh Colliders on the Fly for Improved Performance

I’d like an opinion on whether the following approach will actually improve performance.

I have a scene with a few hundred rocks and each one has a Mesh collider.

In order to get even more performance, I’ve disabled all the rock Mesh colliders in the scene. then I’ve added a Box collider (Trigger) inside each rock that detects when the player is nearby and only enables the mesh collider when the player is close. As you walk across the scene the mesh colliders turn on and off, so there is essentially only one or two mesh colliders active in the scene at any one time, whereas before there were hundreds.

Now of course there are hundreds of active box colliders, but I figure it is cheaper to have 500 box collider triggers + 1 or 2 mesh colliders than 500 mesh colliders active at one time. Plus I’ve heard Triggers are super inexpensive to use.

Doe anyone think this makes sense or is worth it?

It’s not worth it because Unity already does optimizations similar to this. It doesn’t check the actual mesh colliders unless you’re within the bounding box anyway, so you’re basically just adding more overhead. (Especially since turning colliders on and off isn’t free; it has to rebuild the scene graph.)

–Eric

Profiler would have probably told you this wasn’t the bottleneck, do you have it to hand so we can take a look? Sort by time/ms.

In fact I’m really convinced this is slower, what you’re doing. It’s just adding more for physx to deal with that it already optimises. It even does the bounding box thing itself.

For open world its much more likely to be draw calls, shader cost, fill rate, camera culling, that sort of thing.

Gah just read Eric’s reply. What he said :slight_smile:

Thanks Eric. this is just the kind of insider tech information i was looking for:)

Im wondering, if Unity only check Mesh Colliders after you enter the objects bounding box, does that mean ina scene with 500 rocks, each with a mesh ollider, unity sees it as 500 box collider triggers and only when you enter one the mesh collider is activated?

If yes, doesnt that mean a scene with 500 rocks and mesh colliders is really only as expensive as a scene with 500 box coliiders? And in such scene likely only one mesh collider (the active one inside the objects bounding box that ouve triggered) is active at any one time? And if yes, doesnt that mean we shoudlnt worry about using mesh colliders ovs box/compund colliders because it’s niot like there would be many mesh colliders active at any one time? (kind of effectively negating the general theory that mesh colliders are so much more expensive because only 1 or two is active at any given moment, unless you have several mesh colliders intersecting each other)

Thanks Hippocoder:)

I havent figured out the profiler yet but i should. I guess looking at how high the ms are in the graph and see if my approach makes any difference?

But as Eric said, and i read elsewhere, Unity has ti rebuild the scene graph so i guess turning things on an off as you run over each rock would lead to spikes in performance.

But if it does make a bit of difference, maybe instead i could divide the terrain into quadrants, group mesh colliders, and turn on and off a few dozen as you enter and exit an area.

I guess im trying to set up the world so that as much as possible, if you are not near it, it absolutely doesnt exist. turning off all extras and scripts and colliders on an object. it’s just a general goal but im not sure where Unity optimizations end and where we can begin setting up the scene to get even better performance.

Also see my questions above about mesh colliders - if unity has them all in bounding boxes anyways, shouldn’t 500 mesh colliders in a scene be equal to 499 box collider triggers and 1 mesh collider (the active one you are touching) negating the warning to use box colliders instead of meshes? Or is there more performance to be gained here by some custom setup? Just mulling through the concepts here while i get the best possible grip on how the scene works.

thanks for the profiler tip:)

Mesh colliders are still expensive because you do eventually collide with them, and that takes higher CPU usage. Having a bunch of mesh colliders that you’re far away from and not intersecting the bounding boxes should theoretically not be particularly worse than having a bunch of box colliders, but you’d want to profile to be sure.

–Eric