Simple MeshRender

I’m trying to build the simplest MeshRender example.

I’m creating an entity with MeshRender and LocalToWorld.
I pass a standard cube mesh and material to the MeshRender.
Entity debugger shows the entity with above components and [World|Chunk]RenderBounds components.

Not seeing expected cube in game view.

using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;

public class MyMono : MonoBehaviour
{

    void Start()
    {
        // Get cube mesh from a standard cube.
        var primativeGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
        primativeGO.GetComponent<MeshRenderer>().enabled = false;
        Mesh mesh = primativeGO.GetComponent<MeshFilter>().mesh;

        Material material = Resources.Load("MyMaterial", typeof(Material)) as Material;

        // Create entity, with RenderMesh and LocalToWorld
        EntityManager em = World.Active.EntityManager;
        Entity e = em.CreateEntity();
        em.AddComponent<LocalToWorld>(e);
        em.AddSharedComponentData(e, new RenderMesh { mesh = mesh, material = material });
        Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0);
    }


}

Did you include the hybridrenderer package to your unity project?

Yup.

Graphics.DrawMesh(); only renders for a single frame, so you need to call it on every Update

The RenderMesh system takes care of that but you need to either fill up the LocalToWorld yourself or add a Translation component

1 Like

Here’s a quick example of how I did it last night to test out the hybrid renderer and it seems to work just fine. I did this on 2019.3.0f3 with the latest packages installed.

The sprite renders no problem without having to call Graphics.DrawMesh()

public class TestingGround : MonoBehaviour
    {
        [SerializeField] private Mesh mesh;
        [SerializeField] private Material material;
        EntityManager entityManager;
        private void Awake()
        {
            entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
        }

        private void Start()
        {
            var entityArchetype = entityManager
                .CreateArchetype(
                    typeof(Position),
                    typeof(RigidBody),
                    typeof(Collider),
                    typeof(GenerateGUID),
                    typeof(RenderMesh),
                    typeof(LocalToWorld),
                    typeof(PlayerInput),
                    typeof(PlayerMovement),
                    typeof(Translation)
                );

            var entityArchetype2 = entityManager
                .CreateArchetype(
                    typeof(Position),
                    typeof(Collider),
                    typeof(GenerateGUID)
                );

            var entity = entityManager.CreateEntity(entityArchetype);

            entityManager.SetComponentData(entity, new Position { Value = new float2(2,100)  });
            entityManager.SetComponentData(entity, new Collider { Size = 2 });
            entityManager.SetComponentData(entity, new PlayerMovement { Speed = 6 });
            entityManager.SetSharedComponentData(entity, new RenderMesh { mesh = mesh, material = material });
            var entity2 = entityManager.CreateEntity(entityArchetype2);

            entityManager.SetComponentData(entity2, new Position { Value = new float2(2,0)  });
            entityManager.SetComponentData(entity2, new Collider { Size = 2 });
        }
    }