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

Unity generates two Vertecies instead of one

Discussion in 'Scripting' started by TheOtherUserName, Feb 28, 2021.

  1. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    So I used the Brackeys mesh generation tutorial as reference since I didnt work with meshes in a while and I stumbled upon a bug that I dont understand why it is there. As you can see I increment my verts value by one after finishing the inner loop but for some reason my code still generates polygons when switching lines. Surely I am overseeing something here but after trying to trouble shoot it still happens and I think I need a second opinion on my code
    Code (CSharp):
    1. //Generate mesh
    2. void GenerateMesh()
    3.     {
    4.         triangles = new int[size * size * 6];
    5.         vertices = new Vector3[(size + 1) * (size + 1)];
    6.  
    7.         for(int y = 0, i = 0; y < size; y++)
    8.         {
    9.             for(int x = 0; x < size; x++)
    10.             {
    11.                 vertices[i] = new Vector3(x, noise[x, y] * 15, y);
    12.                 i++;
    13.             }
    14.         }
    15.  
    16.         int verts = 0;
    17.         int tris = 0;
    18.  
    19.         for(int i = 0; i < size - 1; i++)
    20.         {
    21.             for(int j = 0; j < size - 1;j++)
    22.             {
    23.                 triangles[tris + 0] = verts + 0;
    24.                 triangles[tris + 1] = verts + size + 1;
    25.                 triangles[tris + 2] = verts + 1;
    26.                 triangles[tris + 3] = verts + 1;
    27.                 triangles[tris + 4] = verts + size + 1;
    28.                 triangles[tris + 5] = verts + size + 2;
    29.  
    30.                 tris += 6;
    31.                 verts++;
    32.             }
    33.             verts++;
    34.         }
    35.  
    36.     }
    37.  
    38. //Show mesh
    39.     void UpdateMesh()
    40.     {
    41.         mesh.Clear();
    42.  
    43.         mesh.vertices = vertices;
    44.         mesh.triangles = triangles;
    45.        
    46.         mesh.RecalculateNormals();
    47.     }
    Here you see the result (look from below):
    Screenshot (74).png
    Otherwise it works as intended
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Cut it way down to perhaps 3 x 3 verts and 2x2 (x2) triangles so you can debug what's going on by printing out indices and scribbling up a quick diagram of vert numbers, triangle numbers, etc.

    Also remove the noise term and make it zero so the mesh is completely flat.

    Get the basic bookkeeping for the grid mesh generation bulletproof solid first, THEN start layering interesting things on it.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,919
    You generate not enough vertices as you stop your x and y loop at size-1 but you need to go up to size.
     
    Kurt-Dekker likes this.