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.

Resolved How to Update MaterialMeshInfo from IJobEntity

Discussion in 'Graphics for ECS' started by Wompscepter, Feb 1, 2023.

  1. Wompscepter


    Feb 7, 2020
    I am trying to dynamically generate modifiable terrain within a system. I have a general outline to generate the meshes using the MeshDataAPI, create a MaterialMeshInfo from each meshID, and then update the MaterialMeshInfo IComponentData of the entity in question using the ECB.

    After generating the NativeArray of meshIDs, I run this job:

    Code (CSharp):
    1. public partial class SectorMeshingSystem : SystemBase
    2. {
    3.     protected override void OnUpdate()
    4.     {
    5.         var ecb = new EntityCommandBuffer(Allocator.TempJob);
    7.         // ... Other code to generate meshIDs and MaterialID
    9.         new ApplyMeshJob
    10.         {
    11.             ecb = ecb,
    12.             meshIDs = meshIDs,
    13.             materialID = materialID
    14.         }.Run();
    16.         ecb.Playback(EntityManager);
    17.     }
    18. }
    20. public partial struct ApplyMeshJob : IJobEntity
    21. {
    22.     public EntityCommandBuffer ecb;
    23.     public NativeArray<BatchMeshID> meshIDs;
    24.     public BatchMaterialID materialID;
    26.     private void Execute([EntityIndexInQuery] int i, SectorMeshAspect sectorMeshAspect)
    27.     {
    28.         var materialMeshInfo = new MaterialMeshInfo {
    29.             MaterialID = materialID, MeshID = meshIDs[i]
    30.         };
    31.         ecb.SetComponent<MaterialMeshInfo>(sectorMeshAspect.Entity, materialMeshInfo);
    32.     }
    33. }
    I don't know where I'm going wrong. I read from another post that you should be able to update the MaterialMeshInfo component directly in runtime and update the mesh, though I could be misunderstanding what JussiKnuuttila was saying (

    Do I need to use RenderMeshUtility.AddComponents(...) for this anyways?
  2. JussiKnuuttila


    Unity Technologies

    Jun 7, 2019
    It should be possible to modify the material and mesh IDs like this. What is going wrong? Are the entities not rendering at all, or is their mesh not changing?
    Wompscepter likes this.
  3. Wompscepter


    Feb 7, 2020
    I have the entities set to initially have a standard cube mesh, which I then update with this. The cube mesh is correctly disappearing, but the new mesh I'm applying (the Tetrahedron mesh described here: is not showing.
    So, technically it seems the entities are not rendering at all.
    I have verified that the Mesh object is indeed populated with the correct vertices and indices.

    The transform is correct as well, I gave it a scale of 12 and positioned it (0, 0, -12) upload_2023-2-1_12-42-0.png

    It seems like the MaterialMeshInfo is also being properly updated:
  4. Wompscepter


    Feb 7, 2020
    I found out that my issue with the above situation was solely because I was using a bad material.

    Bad material:
    Material newMaterial = new Material(Shader.Find("Specular"));

    Working material:
    Material newMaterial = new Material(Shader.Find("Universal Render Pipeline/Lit"));

    Thank you so much JussiKnuuttila for helping me isolate the problem. It's great to have a sanity check that I was doing the new process correctly.
    JussiKnuuttila likes this.