Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with procedural mesh generation. Please!

Discussion in 'Scripting' started by ytrew, Mar 11, 2017.

  1. ytrew

    ytrew

    Joined:
    Feb 22, 2013
    Posts:
    20
    Okay, so for the purposes of being succinct. I have several points that make up a circle and several circles evenly spaced and align with on another. I am trying to create a mesh, by getting and assigning the points to vertex array. I then create an array to store calculated triangle data (It's kind of funky, but given how the data is laid out in the vertex array, it's necessary to parse it out that way in order to get the correct indices for the triangle array). Now the problem, I can generate a mesh, but for some reason it is not complete, as you can see for them image further below.

    Code (CSharp):
    1.  
    2. Mesh mesh = GetComponent<MeshFilter>().mesh;
    3.  
    4. // get vertices
    5. List<Vector3> vertices = new List<Vector3>();
    6. for (int i = 1; i < pathSegments * segmentLength + 1; i++)
    7. {
    8.     vertices.AddRange(nodeList[i]);
    9. }
    10.  
    11. int[] triangles = new int[vertices.Count * 3];
    12.  
    13. int a = 0;
    14. int b = 1;
    15. int c = 6;
    16.  
    17. int e = 1;
    18. int f = 7;
    19. int g = 6;
    20.  
    21. // easiest way I know how to get the alternating triangle indices.
    22. for (int i = 0; i < triangles.Length; i = i + 3)
    23. {
    24.     if (i % 2 == 0)
    25.     {
    26.         triangles[i] = a;
    27.         triangles[i + 1] = b;
    28.         triangles[i + 2] = c;
    29.         a++;
    30.         b++;
    31.         c++;
    32.     }
    33.     else
    34.     {
    35.         triangles[i] = e;
    36.         triangles[i + 1] = f;
    37.         triangles[i + 2] = g;
    38.         e++;
    39.         f++;
    40.         g++;
    41.     }
    42. }
    43.  
    44. // finally judgement time!
    45. mesh.vertices = vertices.ToArray();
    46. mesh.triangles = triangles;
    47.  
    This is the resulting mesh:
    Capture.PNG

    Any help would be greatly appreciated :)
     
  2. ytrew

    ytrew

    Joined:
    Feb 22, 2013
    Posts:
    20
    Nailed it!
    Capture.PNG
     
  3. Krish-Vikram

    Krish-Vikram

    Joined:
    Aug 17, 2012
    Posts:
    8
  4. PhilippUmschaden

    PhilippUmschaden

    Joined:
    Jul 15, 2017
    Posts:
    1
    I am trying to create something similar. Could you please share the code?