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:
public partial class SectorMeshingSystem : SystemBase
{
protected override void OnUpdate()
{
var ecb = new EntityCommandBuffer(Allocator.TempJob);
// ... Other code to generate meshIDs and MaterialID
new ApplyMeshJob
{
ecb = ecb,
meshIDs = meshIDs,
materialID = materialID
}.Run();
ecb.Playback(EntityManager);
}
}
public partial struct ApplyMeshJob : IJobEntity
{
public EntityCommandBuffer ecb;
public NativeArray<BatchMeshID> meshIDs;
public BatchMaterialID materialID;
private void Execute([EntityIndexInQuery] int i, SectorMeshAspect sectorMeshAspect)
{
var materialMeshInfo = new MaterialMeshInfo {
MaterialID = materialID, MeshID = meshIDs[i]
};
ecb.SetComponent<MaterialMeshInfo>(sectorMeshAspect.Entity, materialMeshInfo);
}
}
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 ( RenderMeshUtility will not accept EntityCommandBuffer? )
Do I need to use RenderMeshUtility.AddComponents(…) for this anyways?
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?
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: Unity - Scripting API: MeshData) 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)
It seems like the MaterialMeshInfo is also being properly updated:

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.
Hey Jussi, In Unity 2022 with older versions of entities and entites.graphics I was able to set mmi.MaterialID and mmi.MeshID to an invalid number such as 888 and it would simply not display the entity (for the last LOD level). It worked great and kept the custom LOD code super simple. Once I upgraded to Unity 6 with the latest libraries, it started spitting out an error instead. What is the new method of hiding the entity?
A BatchDrawCommand was submitted with an invalid Batch, Mesh, or Material ID. Shader pass: ALWAYS, BatchDrawCommand index: 9, (index in range: 2), BatchDrawRange index: 5
This is not supported when rendering with a BatchRendererGroup (or Entities Graphics). MaterialID: 888 (“”), MeshID: 888 (“”), BatchID: 3.
MaterialMeshInfo is an IEnableableComponent. Disable it to get the effect you want.
Thanks DreamingImLatios! That certainly worked. It made the IJobEntity a lot more complicated because trying to enable/disable a component at the same time as writing to it requires using EntityQuery/QueryBuilder, but I finally got it.