Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do you create a mesh in code using DOTS?

Discussion in 'Entity Component System' started by Manablight, Feb 14, 2020.

  1. Manablight

    Manablight

    Joined:
    Aug 11, 2014
    Posts:
    4
    I'd like to create a mesh in code, lets say a quad. With a GameObject I could do it with the following Monobehavior. the new entities and components can only use primitive types? How would I go about doing this with the new DOTS setup?

    Code (CSharp):
    1. Mesh mesh = new Mesh();
    2.  
    3.     Vector3[] vertices = new Vector3[4];
    4.     Vector3[] normals = new Vector3[4];
    5.     Vector2[] uvs = new Vector2[4];
    6.     int[] triangles = new int[6];
    7.  
    8.     Vector2 uv00 = new Vector2( 0f, 0f );
    9.     Vector2 uv10 = new Vector2( 1f, 0f );
    10.     Vector2 uv01 = new Vector2( 0f, 1f );
    11.     Vector2 uv11 = new Vector2( 1f, 1f );
    12.  
    13.     Vector3 p0 = new Vector3( -0.5f,  -0.5f,  0.5f );
    14.     Vector3 p1 = new Vector3(  0.5f,  -0.5f,  0.5f );
    15.     Vector3 p2 = new Vector3(  0.5f,  -0.5f, -0.5f );
    16.     Vector3 p3 = new Vector3( -0.5f,  -0.5f, -0.5f );      
    17.     Vector3 p4 = new Vector3( -0.5f,   0.5f,  0.5f );
    18.     Vector3 p5 = new Vector3(  0.5f,   0.5f,  0.5f );
    19.     Vector3 p6 = new Vector3(  0.5f,   0.5f, -0.5f );
    20.     Vector3 p7 = new Vector3( -0.5f,   0.5f, -0.5f );
    21.  
    22.  
    23.     vertices = new Vector3[] {p4, p5, p1, p0};
    24.     normals = new Vector3[] {Vector3.forward,
    25.                              Vector3.forward,
    26.                              Vector3.forward,
    27.                              Vector3.forward};
    28.  
    29.     uvs = new Vector2[] {uv11, uv01, uv00, uv10};
    30.     triangles = new int[] {3, 1, 0, 3, 2, 1};
    31.  
    32.     mesh.vertices = vertices;
    33.     mesh.normals = normals;
    34.     mesh.uv = uvs;
    35.     mesh.triangles = triangles;
    36.    
    37.     mesh.RecalculateBounds();
    38.  
    39.     GameObject quad = new GameObject("quad");
    40.     quad.transform.parent = this.gameObject.transform;
    41.      MeshFilter meshFilter = (MeshFilter) quad.AddComponent(typeof(MeshFilter));
    42.     meshFilter.mesh = mesh;
    43.     MeshRenderer renderer = quad.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
    44.     renderer.material = cubeMaterial;
     
    AndrewHerrera1998 likes this.
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    If the quad is always the same for many entities, I would store mesh data in a renderer system and add a tag component to the entities for which you want to render the quad. Or you can store the mesh data in a sharedcomponent that can contain reference types. Then loop over these entities to call Graphics.DrawMeshInstanced for each of them.

    This is basically what the hybridrenderer package does.
     
  3. Manablight

    Manablight

    Joined:
    Aug 11, 2014
    Posts:
    4
    Thanks for the reply