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.
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.
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,
NativeArray<int> meshIdsNative = new NativeArray<int>(meshIds, Allocator.Persistent);
bakeMeshJobBatches = new BakeMeshJobBatches()
{
meshInstanceIDs = meshIdsNative
};
bakeMeshBatchesJobHandle = bakeMeshJobBatches.Schedule(meshIdsNative.Length, 1);
bakeMeshBatchesJobHandle.Complete();
for(int i = 0; i < meshColliders.Count; i++)
{
meshColliders[i].sharedMesh = meshFilters[i].sharedMesh;
}
meshIdsNative.Dispose();
The Job Struct
[BurstCompile]
public struct BakeMeshJobBatches : IJobParallelFor
{
public NativeArray<int> meshInstanceIDs;
public void Execute(int index)
{
Physics.BakeMesh(meshInstanceIDs[index], false);
}
}
Any help?
