I am trying to render procedural meshes using RenderMeshUnmanaged and RenderBounds. First I create the entity:
entitymanager.AddComponentData(chunkEntity, new LocalTransform
{
Position = c.ChunkOriginInWorldSpace,
Rotation = quaternion.identity,
Scale = 1,
});
entitymanager.AddComponent(chunkEntity, typeof(LocalToWorld));
entitymanager.AddComponent(chunkEntity, typeof(RenderMeshUnmanaged));
entitymanager.AddComponentData(chunkEntity, new RenderBounds());
Then I generate the mesh and update the components here:
entitymanager.SetComponentData(chunkEntity, new RenderMeshUnmanaged(mesh, mat));
entitymanager.SetComponentData(chunkEntity, new RenderBounds { Value = new() { Center = mesh.bounds.center, Extents = mesh.bounds.extents } });
And yes, I have verified that the same exact meshes render with the same exact material as GameObjects.
RenderMeshUnmanaged is bake-time only, and baking systems iterate over it to add the real required components. It doesn’t do anything at runtime. I think what you are looking for is RenderMeshUtility.
Ah I have used RenderMeshUtility for creating entities. How do I update an existing mesh? It only have AddComponents. Do I need to maintain a global renderMeshArray, change the mesh in its array or something?
MaterialMeshInfo is what you want if you are looking to swap out meshes and materials at runtime. If the mesh is newly-generated, you will need to register it to get a runtime ID.
Yes. You can deregister a mesh the same way you register it. But also, you do not need to deregister and reregister a mesh if all you do is modify it. You modify it with the Mesh API, just like GameObjects.
I see. So that implies that I should be able to initialize an entity with a new Mesh() in the RenderMeshArray, assign it using the RenderMeshUtility, and then at runtime just update the mesh data without having to change the MaterialMeshInfo mesh index or register a new mesh, right?