Search Unity

Return indexed data IJobForEach

Discussion in 'Entity Component System' started by ADNCG, Dec 3, 2019.

  1. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I have a system where the user can carve into a 2D mesh. Thing is, I don't use RenderMesh because of unrelated reasons.

    Since I can't access UnityEngine mesh data outside of the main thread, I'd at least like to build the lists of vertices and triangles for all the chunks in a job, and then rebuild the meshes from that data on the main thread.

    How do I return that data and bind it to its respective chunk? Nested native containers aren't currently supported I've read.

    I'd like to do something like that :
    Code (CSharp):
    1. protected override JobHandle OnUpdate(JobHandle inputDependencies)
    2. {
    3.  
    4.     NativeHashMap<int, NativeList<float3>> vertices = new NativeHashMap<int, NativeList<float3>>(480, Allocator.Temp);
    5.     NativeHashMap<int, NativeList<int>> triangles = new NativeHashMap<int, NativeList<int>>(480, Allocator.Temp);
    6.  
    7.     MapMeshDataJob mapMeshDataJob = new MapMeshDataJob
    8.     {
    9.         MapBuffer = GetBufferFromEntity<IntBufferElement>(true),
    10.         Vertices = vertices
    11.         Triangles = triangles
    12.     };
    13.  
    14.     JobHandle jobHandle = mapMeshDataJob.Schedule(inputDependencies);
    15.     jobHandle.Complete();
    16.  
    17.     RebuildMeshes(vertices, triangles);
    18.  
    19.     vertices.Dispose();
    20.     triangles.Dispose();
    21.  
    22.     return inputDependencies;
    23. }
    I'm trying to make sense of the DOTS and feeling pretty dense right now. If there's anything wrong with the implementation as well, please let me know.
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    I recommend you to read this post. It handles building a mesh like your describe but UI/Text usage and the source code is available.
     
    ADNCG likes this.
  3. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Thank you for sharing. I found what I needed in his repo.

    If anybody else stumbles upon this, wondering the same thing, I'm going with the following solution :

    Instead of returning the data from the job, I'm just going to store the data in additional components and just read from them, post-job.