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

Question Run Mesh.SetVertices in parallel

Discussion in 'Physics for ECS' started by hauwing, Feb 14, 2021.

  1. hauwing

    hauwing

    Joined:
    Dec 13, 2017
    Posts:
    69
    Hello,

    I have a scene where there are multiple meshes, and there is a job system to update the vertices of these meshes using the Entities.ForEach and ScheduleParallel() manner, which is working fine.

    I also have another job to assign the updated vertices back to the meshes and recalc the normal as the following, but it only allows .WithoutBurst().Run(). Is there a way to run the mesh update job in a parallel way? thanks.



    Code (CSharp):
    1. public class UpdateMeshVertex_JobSystem : SystemBase
    2. {
    3.     protected override void OnUpdate()
    4.     {
    5.  
    6.         Entities.ForEach(
    7.         (
    8.          in DynamicBuffer<Vertex_Element> vertex_buffer,
    9.          in RenderMesh RM
    10.          ) =>
    11.         {
    12.  
    13.             RM.mesh.SetVertices(vertex_buffer.ToNativeArray(Allocator.Temp).Reinterpret<Vector3>());
    14.             RM.mesh.RecalculateNormals();
    15.  
    16.         })
    17.         .WithoutBurst().Run();
    18.       //.ScheduleParallel();
    19.       //error DC0019: Entities.ForEach uses ISharedComponentType RenderMesh. This is only supported when using .WithoutBurst() and  .Run()
    20.     }
    21. }
     
  2. GameDeveloper1111

    GameDeveloper1111

    Joined:
    Jul 24, 2020
    Posts:
    100
    See Mesh.MeshData for writing to vertex buffers from C# jobs: