Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity Bakes Mesh Collision Data again despite running Physics.BakeMesh() earlier

Discussion in 'Data Oriented Technology Stack' started by TheGameNewBie, Nov 22, 2020.

  1. TheGameNewBie


    Jul 27, 2017
    I am trying to implement multi-threading in procedural generation where I am modifying chunks of terrain (along with Mesh Colliders) at runtime.
    I am using
    to bake mesh colliders on multiple threads.

    As the docs say, This function allows to bake meshes in advance so Unity does not bake them again when assigning to a mesh collider. But it does not seem to be the case.

    Profiler Mesh Baking.PNG

    As you can see, 'Bake Mesh Data' is called on the worker threads (as intended) and then it is again called on the main thread (when I'm assigning the meshes to mesh colliders).
    Which makes the threads useless.

    Is this a bug? Or am I missing something?

    Another question, Is there a way to know which mesh was baked in the profiler?
    For example, It only says, 'Mesh.Bake PhysX Collision Data' in the profiler, It doesn't say the name of the mesh or anything like that.

    Here's the code I'm using to run the job,
    Code (CSharp):
    1. NativeArray<int> meshIdsNative = new NativeArray<int>(meshIds, Allocator.Persistent);
    3.         bakeMeshJobBatches = new BakeMeshJobBatches()
    4.         {
    5.             meshInstanceIDs = meshIdsNative
    6.         };
    8.         bakeMeshBatchesJobHandle = bakeMeshJobBatches.Schedule(meshIdsNative.Length, 1);
    9.         bakeMeshBatchesJobHandle.Complete();
    11.         for(int i = 0; i < meshColliders.Count; i++)
    12.         {
    13.             meshColliders[i].sharedMesh = meshFilters[i].sharedMesh;
    14.         }
    16.         meshIdsNative.Dispose();
    The Job Struct
    Code (CSharp):
    1. [BurstCompile]
    2.     public struct BakeMeshJobBatches : IJobParallelFor
    3.     {
    4.         public NativeArray<int> meshInstanceIDs;
    6.         public void Execute(int index)
    7.         {
    8.             Physics.BakeMesh(meshInstanceIDs[index], false);
    9.         }
    10.     }
    Any help?
    Last edited: Nov 22, 2020
  2. Morbeavus


    Feb 21, 2016
    Hi, I ran into similar issues while back and had to turn off all flags when setting the mesh Vertices before running the job
    Code (CSharp):
    1. private const MeshUpdateFlags MESH_UPDATE_FLAGS = MeshUpdateFlags.DontValidateIndices | MeshUpdateFlags.DontNotifyMeshUsers | MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontResetBoneBounds;
    Nyanpas likes this.