Async Mesh Collider Cooking

I have many complex mesh “tiles” loaded in at runtime from a file. These tiles require collision of the player as well as raycasting.

If these tile meshes are generated at runtime without colliders, there is no performance lag in creating them. But if I finish the mesh generation with adding a collider there is a substantial lag in the program.

I have found using the profiler that the main issue is Unity “cooking” the collider information from the mesh. This cooking happens synchronously. So for any large mesh, the initial creation of its mesh collider is done in one frame.

I am asking, is there a way to have runtime mesh collider cooking done asynchronously? Where once a mesh has been created and added to a gameobject’s meshrenderer the process then to add a mesh collider can be done over multiple frames? Maybe manually cook the mesh collider through some api?

This isn’t possible with the default PhysX physics engine Unity’s using. The only bits of library exposed for baking a mesh collider is Physics.BakeMesh() which will do the same thing you’re doing pretty much. However, if you’re willing to migrate to Unity Physics, then you can create those colliders in a job, which can be treated as asynchronous for your purposes. Last time I checked, it’s not very beginner friendly as it’s mostly just composed of API and example scenes, so you’ll probably need days to weeks just to get comfortable with it.

There is a PhysX plugin available as well which gives more control just over what is going on on the lower level, however, when i tried it about half a year ago it was crashing a lot. Also the guy who posted noted that it is only supported on Windows x64.

Thanks for the reply! I have some experience with Unity Physics for ECS. Mainly for raycasting entities. I don’t know if I would be ready to convert all terrain meshes into entities and run collision and raycasting through ECS.

Is there any way for PhysX colliders to be processed in a job using Unity Physics. I’m imagining no since they are two completely different engines.

I will take a look.

@crysicle Do you have a link for this PhysX plugin?

I found this repo with similar functionality for Unreal. Seems like this allows for the collision cooking to be done over multiple frames.

Thanks! I have found that you can move the mesh baking in another thread. Physics.BakeMesh

I found this thread answer and example for Baking off the main thread:

Do you no if there are more examples of threading individual processes like this on unity? What I mean is to run a process that is not a batch.

Oh wow, I spent a day looking for threaded baking and you pointed me in the right direction! Thanks :slight_smile: I will note that it’s even easier than the referenced code in the that post. You can just use Task.Run to run lambdas on a threadpool thread instead of having to manage your own thread starts, handlers, delegates, events, etc. For example:

Task.Run(() => Physics.BakeMesh(meshId, false)).ContinueWith((task) => NewMeshBakeReady = true);