Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Entity Component System' started by TheGameNewBie, Nov 22, 2020.

  1. TheGameNewBie

    TheGameNewBie

    Joined:
    Jul 27, 2017
    Posts:
    92
    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
    Physics.BakeMesh()
    to bake mesh colliders on multiple threads.

    https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Physics.BakeMesh.html

    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);
    2.  
    3.         bakeMeshJobBatches = new BakeMeshJobBatches()
    4.         {
    5.             meshInstanceIDs = meshIdsNative
    6.         };
    7.  
    8.         bakeMeshBatchesJobHandle = bakeMeshJobBatches.Schedule(meshIdsNative.Length, 1);
    9.         bakeMeshBatchesJobHandle.Complete();
    10.  
    11.         for(int i = 0; i < meshColliders.Count; i++)
    12.         {
    13.             meshColliders[i].sharedMesh = meshFilters[i].sharedMesh;
    14.         }
    15.  
    16.         meshIdsNative.Dispose();
    The Job Struct
    Code (CSharp):
    1. [BurstCompile]
    2.     public struct BakeMeshJobBatches : IJobParallelFor
    3.     {
    4.         public NativeArray<int> meshInstanceIDs;
    5.  
    6.         public void Execute(int index)
    7.         {
    8.             Physics.BakeMesh(meshInstanceIDs[index], false);
    9.         }
    10.     }
    Any help?
     
    Last edited: Nov 22, 2020
  2. Morbeavus

    Morbeavus

    Joined:
    Feb 21, 2016
    Posts:
    3
    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.