- Home /
mesh collider instancing
I have noticed that creating a complex convex mesh collider produces a significant lag, even if i am instancing the same prefab with the collider multiple times. It looks like the concave->convex procedure is called for every instance. My question is if it's possible to pre-build(runtime) or bake(in editor) a convex mesh for mesh colliders so it doesn't have to be generated every time a collider is instanced.
Answer by Kryptos · Aug 08, 2012 at 02:37 PM
The best solution would be to NOT use any MeshCollider. The computation of the convex collider (convex hull closure) can be very expensive. And the resulting collider will also be very expensive.
My advice: create a compound collider with the primitive colliders (Box, Sphere, Capsule) and make a prefab with it. This will take a little more time during editiong, but will have much greater performance at runtime.
A quick (and also not 100% perfect) explanation to why MeshCollider are so expensive.
First, let's explain why the primitive collider are so cheap.
a BoxCollider just checks for bounds, so it is like using the < and > operators a limited number of times.
a SphereCollider is just a check for a distance from a point, which is not very expensive. It can be further optimized by using the squared distance (which is the case in most implementation).
a CapsuleCollider is also a distance check but along a line. So it requires first to project onto this line.
Therefore, the order of collider is (cheapest first): Box < Sphere < Capsule.
Now a MeshCollider is just a bunch of triangles. The engine has to check each triangles against collision. Thus: MeshCollider >> Capsule.
With a convex collider, there are some optimizations (not presented here). But basically in the worst case, the physics engine still has to check each triangle.
Thanks for your reply. I do have some experience with other physics engines and i know what benefits the primitive colliders have, but let's say i have to use mesh collider and my mesh is already convex. But unity is checking and recalculating a convex mesh for every single instance of prefab, which is very laggy. Is that really necessary? (runtime performace is good since mesh to mesh pairs/collisions are rare)
It seems that the generated mesh is not shared. There might be a design explanation, but this is beyond my skills and knowledge of Unity
Try to ask the Unity support. $$anonymous$$aybe this will change in the future. Ideally, it would be great to be able to flag a mesh as being convex so that recomputation is not necessary.
There's a flag "generate colliders" in the importer, but i don't see any performance boost
Your answer
Follow this Question
Related Questions
Bucket with apples on a terrain (Sphere Colliders + Mesh Collider + Terrain) 0 Answers
Adding multiple simple mesh colliders to be able to click on an objects parent 0 Answers
Pushing a mesh with mesh collider 1 Answer
Preventing Convex Mesh Colliders from Interpenetrating 2 Answers
Object with more than 255 polygons (with Mesh Collider(Convex - on)) is falling through a Terrain 2 Answers