Search Unity

Async Mesh Collider Cooking

Discussion in 'World Building' started by ajg5629, Aug 12, 2020.

  1. ajg5629

    ajg5629

    Joined:
    Jan 8, 2020
    Posts:
    4
    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?
     
  2. crysicle

    crysicle

    Joined:
    Oct 24, 2018
    Posts:
    95
    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.
     
  3. ajg5629

    ajg5629

    Joined:
    Jan 8, 2020
    Posts:
    4
    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.
     
  4. ajg5629

    ajg5629

    Joined:
    Jan 8, 2020
    Posts:
    4
  5. crysicle

    crysicle

    Joined:
    Oct 24, 2018
    Posts:
    95
  6. ajg5629

    ajg5629

    Joined:
    Jan 8, 2020
    Posts:
    4
  7. jessdecker1

    jessdecker1

    Joined:
    Oct 20, 2020
    Posts:
    1
    Oh wow, I spent a day looking for threaded baking and you pointed me in the right direction! Thanks :) 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:
    Code (CSharp):
    1.  
    2. Task.Run(() => Physics.BakeMesh(meshId, false)).ContinueWith((task) => NewMeshBakeReady = true);
    3.