Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Simple MeshRender

Discussion in 'Graphics for ECS' started by andywatts, Dec 16, 2019.

  1. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    112
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Entities;
    3. using Unity.Transforms;
    4. using Unity.Rendering;
    5.  
    6. public class MyMono : MonoBehaviour
    7. {
    8.  
    9.     void Start()
    10.     {
    11.         // Get cube mesh from a standard cube.
    12.         var primativeGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
    13.         primativeGO.GetComponent<MeshRenderer>().enabled = false;
    14.         Mesh mesh = primativeGO.GetComponent<MeshFilter>().mesh;
    15.  
    16.         Material material = Resources.Load("MyMaterial", typeof(Material)) as Material;
    17.  
    18.         // Create entity, with RenderMesh and LocalToWorld
    19.         EntityManager em = World.Active.EntityManager;
    20.         Entity e = em.CreateEntity();
    21.         em.AddComponent<LocalToWorld>(e);
    22.         em.AddSharedComponentData(e, new RenderMesh { mesh = mesh, material = material });
    23.         Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0);
    24.     }
    25.  
    26.  
    27. }
     
  2. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    Did you include the hybridrenderer package to your unity project?
     
  3. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    112
    Yup.
     
  4. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    125
    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

     
    andywatts likes this.
  5. axxessdenied

    axxessdenied

    Joined:
    Nov 29, 2016
    Posts:
    33
    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()

    Code (CSharp):
    1. public class TestingGround : MonoBehaviour
    2.     {
    3.         [SerializeField] private Mesh mesh;
    4.         [SerializeField] private Material material;
    5.         EntityManager entityManager;
    6.         private void Awake()
    7.         {
    8.             entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    9.         }
    10.  
    11.         private void Start()
    12.         {
    13.             var entityArchetype = entityManager
    14.                 .CreateArchetype(
    15.                     typeof(Position),
    16.                     typeof(RigidBody),
    17.                     typeof(Collider),
    18.                     typeof(GenerateGUID),
    19.                     typeof(RenderMesh),
    20.                     typeof(LocalToWorld),
    21.                     typeof(PlayerInput),
    22.                     typeof(PlayerMovement),
    23.                     typeof(Translation)
    24.                 );
    25.  
    26.             var entityArchetype2 = entityManager
    27.                 .CreateArchetype(
    28.                     typeof(Position),
    29.                     typeof(Collider),
    30.                     typeof(GenerateGUID)
    31.                 );
    32.  
    33.             var entity = entityManager.CreateEntity(entityArchetype);
    34.  
    35.             entityManager.SetComponentData(entity, new Position { Value = new float2(2,100)  });
    36.             entityManager.SetComponentData(entity, new Collider { Size = 2 });
    37.             entityManager.SetComponentData(entity, new PlayerMovement { Speed = 6 });
    38.             entityManager.SetSharedComponentData(entity, new RenderMesh { mesh = mesh, material = material });
    39.             var entity2 = entityManager.CreateEntity(entityArchetype2);
    40.  
    41.             entityManager.SetComponentData(entity2, new Position { Value = new float2(2,0)  });
    42.             entityManager.SetComponentData(entity2, new Collider { Size = 2 });
    43.         }
    44.     }