I must recalculate often a MeshCollider.sharedMesh but unfortunately it is really slow. It seems really not well optimised. I can generate a procedural mesh with all the triangles, normals and uv in ~0.12 sec but the sharedMesh will take around 1.2 sec!!!
I tried to use a coroutine to -at least- put the calculation in background but it doesn’t seem to work: the application still stalls during the calculation.
Is my code wrong?
Or am I right in supposing that the recalculation of the shared mesh cannot be put in background?
As I don’t need a collider 100% precise can calculation be speed up by generating a “simplified” collider?
Any ideas how I could speed up this?
Alternatively -if we can’t speed up the collider update- is there a non-collider based raycast that can detect the triangles in a mesh?
Coroutines aren’t separate threads. You can use real threads with System.Threading, but only .net classes, nothing Unity-specific, because Unity functions are not thread-safe and will crash randomly. (Which is too bad, because recalculating a mesh collider in a separate thread works very nicely, except for the times when it crashes…)
Not sure what either of those have to do with what I said. See attached pic…when the high-res mesh (blue) is deformed at runtime, the low-res mesh (green, the actual collider) has the same code applied to it. Both were modeled in a 3D app.
Yes, but just .vertices, because the triangles don’t change. Also I’m not using .sharedMesh, just .mesh, because I don’t want to modify everything that uses that mesh, just that particular instance.
MeshColliders have to do a great deal of processing before they can be used for physics calculations. When you directly set mc.sharedMesh = mesh, Unity will detect a change, and will re-generate some behind-the-scenes data. If you just set the vertices however, this will not be the case. That’s why when you individually set mesh.vertices = vertices, etc. It runs slower, and doesn’t effect the collision volume.
I’ve been messing with mesh deformation for a school project of mine, and I ran into the same issue. Hope this answers the question for anyone who’s curious in the future!