Adding mesh to Entity

Hello guys!

I am having trouble to adding Mesh to Entity.
I have tried different methods, but nothing work.
I have a system that uses some prefab (without meshFilter and meshRenderer) to create Entity, and I would like to add specific mesh (lfor example square 1x1 or 2x2 or 1x2 and etc.).
I tried google it, but did not find nothing…
I tried following:

var ecb = new EntityCommandBuffer(Allocator.Temp);

...

var mesh = new Mesh();
mesh.vertices = new[] { Vector3.up * size, Vector3.left * size, Vector3.right * size };
mesh.triangles = new[] { 0, 1, 2, 0, 2, 1 };

ecb.SetComponent(newItem, new RenderMesh
{
    mesh = mesh,
    material = material,
});

or

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            var worldEntity = SystemAPI.GetSingletonEntity<WorldProperties>();
            var world = SystemAPI.GetAspect<WorldAspect>(worldEntity);
            var ecb = new EntityCommandBuffer(Allocator.Temp);
            var newMachine = ecb.Instantiate(world.MachinePrefab);

            var desc = new RenderMeshDescription(
                shadowCastingMode: ShadowCastingMode.Off,
                receiveShadows: false);

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

            var mesh = new Mesh();
            mesh.vertices = new[] { Vector3.up * 2, Vector3.left * 2, Vector3.right * 2 };
            mesh.triangles = new[] { 0, 1, 2, 0, 2, 1 };

            var renderMeshArray = new RenderMeshArray(
                new Material[]
                {
                    material,
                },
                new Mesh[]
                {
                    mesh
                }
            );

            RenderMeshUtility.AddComponents(
                newMachine,
                World.DefaultGameObjectInjectionWorld.EntityManager,
                desc,
                renderMeshArray,
                MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0)
            );

What the correct way?

Thanks,
Andrei.

The latter version looks mostly correct, but if you want to use an EntityCommandBuffer for the Instantiate, then you should Playback it before calling RenderMeshUtility.AddComponents. In this kind of use case it would probably be easier to just use World.DefaultGameObjectInjectionWorld.EntityManager.Instantiate instead.

Hi Jussi,

You’re a wizard!
Your solution to use World.DefaultGameObjectInjectionWorld.EntityManager.Instantiate worked, and finally I saw my damn triangle :slight_smile:
Working version is following:

        public void OnUpdate(ref SystemState state)
        {
            state.Enabled = false;

            var worldEntity = SystemAPI.GetSingletonEntity<WorldProperties>();
            var world = SystemAPI.GetAspect<WorldAspect>(worldEntity);
            var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
            var newMachine = entityManager.Instantiate(world.MachinePrefab);
            var desc = new RenderMeshDescription(
                shadowCastingMode: ShadowCastingMode.Off,
                receiveShadows: false);
            var material = new Material(Shader.Find("Universal Render Pipeline/Lit"));
            var mesh = new Mesh();
            mesh.vertices = new[] { Vector3.up * 2, Vector3.left * 2, Vector3.right * 2 };
            mesh.triangles = new[] { 0, 1, 2, 0, 2, 1 };
            var renderMeshArray = new RenderMeshArray(
                new Material[] { material },
                new Mesh[] { mesh });

            RenderMeshUtility.AddComponents(
                newMachine,
                entityManager,
                desc,
                renderMeshArray,
                MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0)
            );

            entityManager.AddComponentData(newMachine, new LocalToWorld());
        }

But now I have a few new questions:

  1. I’m encountering Burst error:
    ./Library/PackageCache/com.unity.entities@1.0.14/Unity.Entities/EntityDataAccess.cs(2113,13): Burst error BC1051: Invalid managed type found for the field m_Materials of the struct Unity.Rendering.RenderMeshArray.: the type UnityEngine.Material[ ] is a managed type and is not supported
    This error on the line with RenderMeshUtility.AddComponents. So I removed [BurstCompile] for OnUpdate(ref SystemState state) function in my System. But how can I resolve this issue that Burst start works?
  2. How I can use EntityCommandBuffer? I tried following:
...
            var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
           
            var ecb = new EntityCommandBuffer(Allocator.Temp);
            var newMachine = ecb.Instantiate(world.MachinePrefab);
            ecb.SetComponent(newMachine, new LocalTransform
            {
                Position = new float3 { x = 1.0f, y = 2.0f, z = 0.0f },
                Rotation = quaternion.identity,
                Scale = 1.0f,
            });

            ecb.Playback(entityManager);
            ecb.Dispose();

            var desc = new RenderMeshDescription(
                shadowCastingMode: ShadowCastingMode.Off,
                receiveShadows: false);
...

But this approach doesn’t work for me. I received error:
ArgumentException: All entities created using EntityCommandBuffer.CreateEntity must be realized via playback(). One of the entities is still deferred (Index: -1).

Please forgive me for my stupid questions. I’m a newbie in DOTS…

The RenderMeshArray type is not Burst compatible, because it contains Mesh and Material objects, which are managed class objects.

The whole reason why we introduced RenderMeshArray was so that code can be split into Burst compatible parts that can use integers (for example, the MaterialMeshInfo component), and Burst incompatible parts that have to deal with RenderMeshArray directly, but which usually are run infrequently.

Unfortunately, I’m not sure why the EntityCommandBuffer code does not work, so I’m not able to help you there.

Jussi, anyway, thanks for the help!

I still don’t know how it will be implemented :slight_smile:
For now, I see, that I will have system that will create entities. For example, if I need to create 2x2 house, then the system will create new entity with 2x2 mesh, will set some specific material, texture for scheder, and set component with health, resistances and etc…
But really I’m not sure that this good apprach.
I don’t know how mach types of building I’ll have. I can assume that there will be up to hundred of them, and the majority will be 1x1 or 2x2.
Maybe should I create separate prefab for each size? Or create one prefab that will contains all meshes?
Really I have not experience with it :frowning: