Search Unity

creating mesh from list of vector 3's

Discussion in 'Scripting' started by Dazz500, Aug 20, 2019.

  1. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Hi all, so I've got 2 lists of vector 3's, one for the left hand side of the mesh and one for the right, when I run it all I end up creating is a mesh with the last 2 points of each list.
    Any help appreciated.

    Code (CSharp):
    1. for (int i = 0; i < leftPos.Count; i++) {
    2.  
    3.             if (i > 0) {
    4.  
    5.                 current_CL_LEFT = leftPos [i];
    6.                 current_CL_RIGHT = rightPos [i];
    7.                 previous_CL_LEFT = leftPos [i - 1];
    8.                 previous_CL_RIGHT = rightPos [i - 1];
    9.  
    10.                 Vector3[] vertices = {
    11.  
    12.                     previous_CL_RIGHT,    // 0
    13.                     previous_CL_LEFT,    // 1
    14.                     current_CL_LEFT,     // 2
    15.                     current_CL_RIGHT,    // 3                            
    16.                
    17.                 };
    18.  
    19.                 int[] triangles = {
    20.                     0, 1, 2,
    21.                     2, 3, 0
    22.  
    23.                 };
    24.  
    25.                 Vector2 []uv = {
    26.                     new Vector2 (0, 0),
    27.                     new Vector2 (1, 0),
    28.                     new Vector2 (1, 1),
    29.                     new Vector2 (0, 1),
    30.                 };
    31.  
    32.                 Vector3[] norms = {
    33.  
    34.                     Vector3.up,
    35.                     Vector3.up,
    36.                     Vector3.up,
    37.                     Vector3.up
    38.                 };
    39.  
    40.                 Mesh mesh = new Mesh ();
    41.                 mesh.vertices = vertices;
    42.                 mesh.triangles = triangles;
    43.                 mesh.uv = uv;
    44.                 mesh.normals = norms;
    45.  
    46.  
    47.                 mesh_filter.mesh = mesh;
    48.  
    49.  
    50.             }
    51.        
    52.                
    53.        
    54.         }    //
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Every time you do

    Code (CSharp):
    1. mesh.vertices = vertices;
    2.                 mesh.triangles = triangles;
    3.                 mesh.uv = uv;
    4.                 mesh.normals = norms;
    you override values you putted there on previous iteration. Only last vertices triangles, uvs and normals remains. You need to add, not override.
     
  3. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Hi thanks for the reply, sorry if a basic question but how do I add instead of override ?
     
  4. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Hi thanks but i now get the following error from this line : mesh.vertices += vertices;
    error CS0019: Operator `+=' cannot be applied to operands of type `UnityEngine.Vector3[]' and `UnityEngine.Vector3[]'
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    He's misguided you. Operator += is not intended for arrays, that's why you get that error. You need to
    Code (CSharp):
    1.  
    2. List<Vector3> vertices = new List<Vector3>();
    3. Mesh mesh = meshFilter.mesh;
    4.  
    5. mesh.Clear();
    6.  
    7. for(...) {
    8.    vertices.AddRange(<your list of vertices for 1 element>);
    9. }
    10.  
    11. mesh.vertices = vertices.ToArray();
    12.  
     
  6. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    Try this one:

    Code (CSharp):
    1. List<Vector3> Vertices = new List<Vector3>();
    2.         List<int> Triangles = new List<int>();
    3.         List<Vector2> UV = new List<Vector2>();
    4.         List<Vector3> Normals = new List<Vector3>();
    5.  
    6.  
    7.         for (int i = 1; i < leftPos.Count; i++)
    8.         {
    9.  
    10.                 current_CL_LEFT = leftPos[i];
    11.                 current_CL_RIGHT = rightPos[i];
    12.                 previous_CL_LEFT = leftPos[i - 1];
    13.                 previous_CL_RIGHT = rightPos[i - 1];
    14.  
    15.                 Vector3[] vertices = {
    16.  
    17.                     previous_CL_RIGHT,    // 0
    18.                     previous_CL_LEFT,    // 1
    19.                     current_CL_LEFT,     // 2
    20.                     current_CL_RIGHT,    // 3                        
    21.            
    22.                 };
    23.  
    24.                 int j = (i - 1)* 4;
    25.  
    26.                 int[] triangles = {
    27.                     0+j, 1+j, 2+j,
    28.                     2+j, 3+j, 0+j
    29.  
    30.                 };
    31.  
    32.                 Vector2[] uv = {
    33.                     new Vector2 (0, 0),
    34.                     new Vector2 (1, 0),
    35.                     new Vector2 (1, 1),
    36.                     new Vector2 (0, 1),
    37.                 };
    38.  
    39.                 Vector3[] norms = {
    40.  
    41.                     Vector3.up,
    42.                     Vector3.up,
    43.                     Vector3.up,
    44.                     Vector3.up
    45.                 };
    46.  
    47.                 Vertices.AddRange(vertices);
    48.                 Triangles.AddRange(triangles);
    49.                 UV.AddRange(uv);
    50.                 Normals.AddRange(norms);
    51.         }
    52.         Mesh mesh = new Mesh();
    53.         mesh.vertices = Vertices.ToArray();
    54.         mesh.triangles = Triangles.ToArray();
    55.         mesh.uv = UV.ToArray();
    56.         mesh.normals = Normals.ToArray();
    57.         mesh_filter.mesh = mesh;
     
    Last edited: Aug 27, 2019
  7. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Thanks guys, will try tomorrow now
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    There are some helper classes in Unity that you might find useful for winding your own geometry, such as the UnityEngine.UI.VertexHelper class. Procedural Geometry in Unity is a amazingly easy compared to most other general purpose APIs.

    If you want to see some more handy examples of procedural generation, check out my little "makegeo" project, which is just an ad-hoc collection of random procgen routines.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo
     
    palex-nx likes this.