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

Resolved Way to change MaterialMeshInfo inside IJobChunk?

Discussion in 'Graphics for ECS' started by MatanYamin, May 15, 2023.

  1. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    105
    Hey everyone,

    Like the title says, I need to change the material id of entity's MaterialMeshInfo.

    The problem is, I can't use the ref MaterialMeshInfo inside IJobChunk Execute.

    How do I pass a query with the data to IJobChunk and then modify it inside?

    (Note: I know I can do this inside IJobEntity but I need to use Chunks).

    Thanks!
     
  2. LeFlop2001

    LeFlop2001

    Joined:
    Jan 30, 2020
    Posts:
    11
    the array you get from chunk.GetNativeArray points to inside the chunk, so you simply have to get a value, modify it and reassign it at the same index
    Code (CSharp):
    1.  
    2.     [BurstCompile]
    3.     public struct UpdateTranslationFromVelocityJob : IJobChunk
    4.     {
    5.         public ComponentTypeHandle<MaterialMeshInfo> MMTypeHandle;
    6.  
    7.         [BurstCompile]
    8.         public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    9.         {
    10.             NativeArray<MaterialMeshInfo> mmInfo = chunk.GetNativeArray(ref MMTypeHandle);
    11.  
    12.             var enumerator = new ChunkEntityEnumerator(useEnabledMask, chunkEnabledMask, chunk.Count);
    13.             while (enumerator.NextEntityIndex(out var i))
    14.             {
    15.                 var info = mmInfo[i];
    16.  
    17.                 info.Material++;
    18.  
    19.                 mmInfo[i] = info;
    20.             }
    21.         }
    22.     }
     
    MatanYamin likes this.
  3. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    105
    Thank you so much! Just what I was looking for. Works great for me!