Search Unity

Resolved How to Update MaterialMeshInfo from IJobEntity

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

  1. Wompscepter

    Wompscepter

    Joined:
    Feb 7, 2020
    Posts:
    10
    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);
    6.  
    7.         // ... Other code to generate meshIDs and MaterialID
    8.  
    9.         new ApplyMeshJob
    10.         {
    11.             ecb = ecb,
    12.             meshIDs = meshIDs,
    13.             materialID = materialID
    14.         }.Run();
    15.  
    16.         ecb.Playback(EntityManager);
    17.     }
    18. }
    19.  
    20. public partial struct ApplyMeshJob : IJobEntity
    21. {
    22.     public EntityCommandBuffer ecb;
    23.     public NativeArray<BatchMeshID> meshIDs;
    24.     public BatchMaterialID materialID;
    25.  
    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 (https://forum.unity.com/threads/rendermeshutility-will-not-accept-entitycommandbuffer.1370682/)

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

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    351
    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

    Wompscepter

    Joined:
    Feb 7, 2020
    Posts:
    10
    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: https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Mesh.MeshData.html) 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:
    upload_2023-2-1_12-44-20.png
     
  4. Wompscepter

    Wompscepter

    Joined:
    Feb 7, 2020
    Posts:
    10
    Update:
    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.