Search Unity

Create Mesh at runtime

Discussion in 'Scripting' started by wandern3D, Oct 13, 2013.

  1. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
    Hi there,
    I wanna create a Mesh at runtime from x,y,z coordinates which I read from a file. I read all the float point into an Vector Array and then I maked a VERTICE Array like

    "vertices = new Vector3(x, y, z);"

    The next work is to make the triangles like this:

    var tri: int[] = new int[18];
    tri[0] = 0;
    tri[1] = 2;
    tri[2] = 1;

    tri[3] = 2;
    tri[4] = 3;
    tri[5] = 1;

    tri[6] = 1;
    tri[7] = 3;
    tri[8] = 4;

    tri[9] = 3;
    tri[10] = 5;
    tri[11] = 4;


    And that is the position I'm a little bit confused how to make this Array in a FOR i .....
    I attached a file where I draw what I mean. This concept function but to get this in a FOR ...NEXT
    I should have continued numbers, but only the beginning is for me confused.

    Have someone some infos or a tut which better explain how to make a mesh from coordinates x,y,z
     

    Attached Files:

  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I usually do it this way

    Code (csharp):
    1.  
    2. int numberOfQuads = ...;
    3.  
    4. var indices = new List<int>();
    5. for (int i = 0; i < numberOfQuads; i++)
    6. {
    7.     indices.Add(i * 2 + 0);
    8.     indices.Add(i * 2 + 1);
    9.     indices.Add(i * 2 + 2);
    10.  
    11.     indices.Add(i * 2 + 2);
    12.     indices.Add(i * 2 + 1);
    13.     indices.Add(i * 2 + 3);
    14. }
    or
    Code (csharp):
    1. int numberOfQuads = ...;
    2.  
    3. var indices = new int[numberOfQuads * 6];
    4. for (int i = 0; i < numberOfQuads; i++)
    5. {
    6.     indices[i * 6 + 0] = i * 2 + 0;
    7.     indices[i * 6 + 1] = i * 2 + 1;
    8.     indices[i * 6 + 2] = i * 2 + 2;
    9.     indices[i * 6 + 3] = i * 2 + 2;
    10.     indices[i * 6 + 4] = i * 2 + 1;
    11.     indices[i * 6 + 5] = i * 2 + 3;
    12. }
    $WP_000807.jpg
     
    Last edited: Oct 13, 2013
  3. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
    Thanks,
    better structered code, but one problem I have, cause the first 6 triangles must I declare manuel out of the FOR....
    cause If I take yours my meshes lays down in the x-z axis.
    So if I take this manually like above, my mesh lay in the x-y or z-y axis....see screenshot.
    So I wondering how to find out how I start with the Triangles.

    I have all the Points from your picture i xyz-coordinates,
    1 = (0,1,0)
    3 = (3,2,2)
    5 = (5,1,3)
    and so on...but I don't know the 2,4,6,8.....
    I wanna make the mesh in x-y / z-y
    The vertices I set so:

    // Set first 4 Vectors manually, I don't know why....
    vertices[0] = new Vector3(GPS_daten[0].x * 1,0,GPS_daten[0].z * 1);
    vertices[1] = new Vector3(GPS_daten[1].x * 1,0,GPS_daten[1].z * 1);
    vertices[2] = GPS_daten[0];
    vertices[3] = GPS_daten[1];

    for (var j : int = 4; j < MaxVertices+1; j++) {
    vertices[j] = new Vector3(GPS_daten[j/2].x * 1,0,GPS_daten[j/2].z * 1);
    j++;
    vertices[j] = GPS_daten[(j-1)/2];
    }
    mesh.vertices = vertices;

    That gives my screenshot I attached. Do you have some more informations on create meshes at runtime, a tutorial or something else an example?
    I think the first definition of the vertices and then the triangle gives the orientation of the mesh, I think..
     

    Attached Files:

  4. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    You can easily skip first 3 quads if you want:

    Code (csharp):
    1. int numberOfQuads = ...;
    2. var indices = new List<int>();
    3.  
    4. // add first 3 quads manually
    5.  
    6. for (int i = 3; i < numberOfQuads; i++)
    7. {
    8.     indices.Add(i * 2 + 0);
    9.     indices.Add(i * 2 + 1);
    10.     indices.Add(i * 2 + 2);
    11.  
    12.     indices.Add(i * 2 + 2);
    13.     indices.Add(i * 2 + 1);
    14.     indices.Add(i * 2 + 3);
    15. }
    Code (csharp):
    1. int numberOfQuads = ...;
    2. var indices = new int[numberOfQuads * 6];
    3.  
    4. // add first 3 quads manually
    5.  
    6. for (int i = 3; i < numberOfQuads; i++)
    7. {
    8.     indices[i * 6 + 0] = i * 2 + 0;
    9.     indices[i * 6 + 1] = i * 2 + 1;
    10.     indices[i * 6 + 2] = i * 2 + 2;
    11.  
    12.     indices[i * 6 + 3] = i * 2 + 2;
    13.     indices[i * 6 + 4] = i * 2 + 1;
    14.     indices[i * 6 + 5] = i * 2 + 3;
    15. }
     
  5. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
    Hi alexzzzz,
    Many thanks, i also find your way to create the mesh logical and I think its the better way.
    but in my scene it isn't functioned, so that I might set the first two triangles to
    tri[0] = 0;
    tri[1] = 2;
    tri[2] = 1;

    tri[3] = 2;
    tri[4] = 3;
    tri[5] = 1;

    tri[6] = 1;
    tri[7] = 3;
    tri[8] = 4;

    tri[9] = 3;
    tri[10] = 5;
    tri[11] = 4;

    not 0,1,2 and so on.....cause when I take 0,1,2.....then all my meshes lays down in the x-z axis.

    So now it function with my method above but Ican't figure out why. I think I should look at this detail in a few days. My brain is full of code and other thinks so maybe i can't see the other better solution.
     
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Are the vertices #1 and #2 on the picture attached to the first post are swapped on purpose or by accident?
     
  7. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
    Hi alex,
    no not really an accident. Its on purpose I changed the triangles cause if i don't do that I get meshes that layed dawn in the x-z axis.

    if I don't do this like i decriped I get this: see screenshot.
    And that is I wonder why. To understand create meshes at runtime I can't find any tut which explain me how to make correct way.
    I make an empty gameobject in the scene and attached my Java script

    Principle if I was UNITY I should ask one question: In which direction should I begin to draw the meshes of my user at the emptyGameObject.
     

    Attached Files: