#Creating meshes at runtime on multiple entities.

1 messages · Page 1 of 1 (latest)

weary crater
#

Hi, I've been trying to create a mesh to render terrain chunks, but I can not for the life of me figure out how to achieve this. From what I got from the docs, you have to use RenderMeshUtility. However when i try to use that in my current implementation, it does not work and complains about not being able to process structural changing while iterating through a query: ```cs

    foreach (var (chunk, transform, chunkEntity) in SystemAPI.Query<RefRW<Chunk>, RefRO<Carbon.Components.Transform>>().WithEntityAccess()) {
        var desc = new RenderMeshDescription(shadowCastingMode: UnityEngine.Rendering.ShadowCastingMode.Off, receiveShadows: false);
        var renderMeshArray = new RenderMeshArray(new Material[] {placeholderMaterial}, new Mesh[] {placeholderMesh});
        RenderMeshUtility.AddComponents(chunkEntity, World.DefaultGameObjectInjectionWorld.EntityManager, desc, renderMeshArray, MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0)); // This line
        ecb.AddComponent(chunkEntity, new LocalToWorld());
    }``` I want to have a mesh for each chunk that can be accessed via the entity.
rugged geode
#

if you use the query and get nativearrays of the entities, and any component data needed, then you can do structural changes with RenderMeshUtility. alternatively you could add the necessary data to those lists and then after foreach, iterate your lists and use RenderMeshUtility

#

finally you could use Entities.ForEach & .WithStructuralChanges(), but it will be removed in 2.0(but who knows when that will be)

void cloud
#

Yeah, like baron says, just don't use SystemAPI.Query and just get the arrays from a query instead.
Also don't use World.DefaultGameObjectInjectionWorld from inside a system, just use the entity manager provided by the system directly