Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I pass buffers from other entities to an IJobEntity

Discussion in 'Entity Component System' started by leryss, May 17, 2023.

  1. leryss

    leryss

    Joined:
    May 4, 2019
    Posts:
    5
    Im trying to generate chunked terrain meshes, each chunk entity has a buffer of data from which the mesh is created.
    The problem is that I also need this data from neighboring chunk entities so i can calculate normals & have the chunk geometry merge seamlessly

    Is it somehow possible to pass buffers from other entities of the same archetype to a IJobEntity or do i have to implement this using plain IJobs ?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,976
    Does BufferLookup not solve your problem?
     
    bb8_1 likes this.
  3. FootSteps

    FootSteps

    Joined:
    Aug 19, 2014
    Posts:
    12
    Here is an example. You can pass in BufferLookup<BUFFER TYPE> into your job. It's going to be a list of all dynamic buffers that exist keyed by Entity they belong to.

    Code (CSharp):
    1.  
    2. public partial struct SomeSystem : ISystem
    3. {
    4.     private BufferLookup<NeighborBufferElement> neighborLookup;
    5.  
    6.     void OnCreate(ref SystemState state)
    7.     {
    8.         neighborLookup = state.GetBufferLookup<NeighborBufferElement>(true);
    9.     }
    10.     void OnUpdate(ref SystemState state)
    11.     {
    12.         neighborLookup.Update(ref state);
    13.         SomeJob job2 = new SomeJob
    14.         {
    15.             neighborLookup = neighborLookup,
    16.         };
    17.     }
    18. }
    19.  
    20. partial struct SomeJob : IJobEntity
    21. {
    22.     [ReadOnly] public BufferLookup<NeighborBufferElement> neighborLookup;
    23.  
    24.     public void Execute(.....)
    25.     {
    26.         Entity someEntity;
    27.  
    28.         DynamicBuffer<NeighborBufferElement> getBuffer = neighborLookup[someEntity];
    29.         //USE THE BUFFER
    30.     }
    31.  
    32. }
     
    OndrejP likes this.