Search Unity

How to use the RenderMesh component in Pure ECS inside of a ComponentSystem?

Discussion in 'Graphics for ECS' started by Matt_De_Boss_Developer, May 25, 2019.

  1. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    So at the current moment I have been using @tertle 's code to copy my DynamicBuffers into a Mesh class, then using that Mesh class as shown below to draw to the scene. However, is there any documentation on how the RenderMesh component can be used in order to render a DynamicBuffer of vertices, normals, and indices? Code Below:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Unity.Entities;
    4. using Unity.Collections;
    5. using Entities.Extensions;
    6. using Unity.Jobs;
    7. using Unity.Rendering;
    8.  
    9. [UpdateAfter(typeof(NormalGenerationSystem))]
    10. public class RenderChunkMesh : JobComponentSystem
    11. {
    12.  
    13.  
    14.     EntityQuery eq;
    15.     List<Vector3> verticesList;
    16.     List<int> trianglesList;
    17.     List<Vector3> normalsList;
    18.     Mesh mesh;
    19.     Material material;
    20.  
    21.  
    22.     protected override void OnCreate()
    23.     {
    24.         eq = World.Active.EntityManager.CreateEntityQuery(typeof(shouldRender));
    25.         mesh = new Mesh();
    26.         verticesList = new List<Vector3>();
    27.         trianglesList = new List<int>();
    28.         normalsList = new List<Vector3>();
    29.         material = Resources.Load("Test", typeof(Material)) as Material;
    30.     }
    31.  
    32.  
    33.  
    34.  
    35.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    36.     {
    37.         //throw new System.NotImplementedException();
    38.  
    39.         NativeArray<Entity> entities = eq.ToEntityArray(Allocator.TempJob);
    40.  
    41.  
    42.         if (entities.Length == 0)
    43.         {
    44.             return inputDeps;
    45.         }
    46.  
    47.         World.Active.EntityManager.AddComponent(entities[0], typeof(completeChunk));
    48.         World.Active.EntityManager.RemoveComponent(entities[0], typeof(shouldRender));
    49.        
    50.  
    51.  
    52.         SetMesh(mesh, GetBufferFromEntity<filteredVerticesArray>(false)[entities[0]].Reinterpret<Vector3>(),
    53.                       GetBufferFromEntity<normalArray>(false)[entities[0]].Reinterpret<Vector3>(),
    54.                       GetBufferFromEntity<indicesArray>(false)[entities[0]].Reinterpret<int>());
    55.  
    56.         World.Active.EntityManager.SetSharedComponentData(entities[0], new RenderMesh {
    57.             mesh = mesh,
    58.             material = material
    59.         });
    60.         //Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0);
    61.  
    62.         //RenderMesh rM = World.Active.EntityManager.GetComponentObject<RenderMesh>(entities[0]);
    63.  
    64.  
    65.         entities.Dispose();
    66.  
    67.         return inputDeps;
    68.     }
    69.  
    70.     private void SetMesh(
    71.             Mesh mesh,
    72.             DynamicBuffer<Vector3> vertices,
    73.             DynamicBuffer<Vector3> normals,
    74.             DynamicBuffer<int> triangles)
    75.     {
    76.         mesh.Clear();
    77.  
    78.         if (vertices.Length == 0)
    79.         {
    80.             return;
    81.         }
    82.        
    83.         verticesList.AddRange(vertices);
    84.         normalsList.AddRange(normals);
    85.         trianglesList.AddRange(triangles);
    86.  
    87.  
    88.  
    89.  
    90.  
    91.         mesh.SetVertices(this.verticesList);
    92.         mesh.SetNormals(this.normalsList);
    93.         mesh.SetTriangles(this.trianglesList, 0);
    94.  
    95.         verticesList.Clear();
    96.         normalsList.Clear();
    97.         trianglesList.Clear();
    98.     }
    99.  
    100. }
    101.  
    EDIT: So now I have it kind of working with a MeshRenderer Component, however, all of the meshes are now the same for some reason (I have seen other people use the MeshRenderer Component without have the same mesh for all entities).
     
    Last edited: May 25, 2019
    pardod479 likes this.
  2. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    967
    I think the problem is that you are using the same single mesh reference from the system in the shared components.
    Once you change it, you overwrite the other meshes.
    Try to clone or copy when you set the shared component data to avoid setting the same objects.
     
  3. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    Thats exactly what was happening. Thanks so much for the help!