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
  3. Dismiss Notice

Question Issue with generating mesh plane with high vertex count

Discussion in 'Scripting' started by rj191951, Dec 12, 2020.

  1. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    11
    I'm able to generate mesh plane by providing vertex count along x & z-axis. i.e,
    x_count for x-vertices and z_count for z-vertices. So, total vertices in mesh equal to x_count*z_count.

    But for a high value of vertices like values of x & z count above 300 or 500, it gives weird results.

    Here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MeshGen : MonoBehaviour
    4. {
    5.     public Material mat;
    6.  
    7.     public int x_cnt = 2;
    8.     public int z_cnt = 2;
    9.     public float x_scale = 1f;
    10.     public float z_scale = 1f;
    11.  
    12.     Vector3[] vert;
    13.     int[] tri;
    14.     //Vector2[] uvs;
    15.  
    16.     void Start()//public void Generate()
    17.     {
    18.         if (x_scale <= 0f)
    19.         {
    20.             x_scale = 1f;
    21.             Debug.Log("Xscale less than or equal to 0");
    22.         }
    23.         if (z_scale <= 0f)
    24.         {
    25.             z_scale = 1f;
    26.             Debug.Log("Zscale less than or equal to 0");
    27.         }
    28.  
    29.         if(x_cnt >= 2 && z_cnt >= 2)
    30.         {
    31.             MakePlane();
    32.         }
    33.     }
    34.  
    35.     //make mesh with uv mapping...
    36.     void MeshMake(ref Vector3[] v, ref int[] t, ref Vector2[] u, Material m)
    37.     {
    38.         Mesh me = new Mesh();
    39.         me.vertices = v;
    40.         me.triangles = t;
    41.         me.uv = u;
    42.         me.RecalculateNormals();
    43.         me.Optimize();
    44.         transform.gameObject.AddComponent<MeshFilter>();
    45.         transform.gameObject.AddComponent<MeshRenderer>();
    46.         transform.GetComponent<MeshFilter>().mesh = me;
    47.         transform.gameObject.GetComponent<MeshRenderer>().material = m;
    48.     }
    49.  
    50.     //make mesh without uv mapping...
    51.     void MeshMake(ref Vector3[] v, ref int[] t, Material m)
    52.     {
    53.         Mesh me = new Mesh();
    54.         if(!transform.GetComponent<MeshFilter>() || !transform.GetComponent<MeshRenderer>())
    55.         {
    56.             transform.gameObject.AddComponent<MeshFilter>();
    57.             transform.gameObject.AddComponent<MeshRenderer>();
    58.         }
    59.         transform.GetComponent<MeshFilter>().mesh = me;
    60.         me.vertices = v;
    61.         me.triangles = t;
    62.         me.RecalculateNormals();
    63.         transform.gameObject.GetComponent<MeshRenderer>().material = m;
    64.     }
    65.  
    66.     void MakePlane()
    67.     {
    68.         //define vertices...
    69.         vert = new Vector3[x_cnt * z_cnt];
    70.         for (int i = 0; i < z_cnt; i++)
    71.         {
    72.             for (int j = 0; j < x_cnt; j++)
    73.             {
    74.                 int idx = i * x_cnt + j;
    75.                 vert[idx] = new Vector3(j * x_scale, 0f, i * z_scale);
    76.             }
    77.         }
    78.  
    79.         //define triangles...
    80.         tri = new int[(x_cnt - 1) * (z_cnt - 1) * 6];
    81.         int id = 0;
    82.         for(int i = 0; i < z_cnt-1; i++)
    83.         {
    84.             for(int j =0; j < x_cnt-1; j++)
    85.             {
    86.                 //first triangle..
    87.                 tri[id] = i * x_cnt + j;
    88.                 tri[id + 1] = tri[id] + x_cnt;
    89.                 tri[id + 2] = tri[id+1] + 1;
    90.  
    91.                 //second triangle..
    92.                 tri[id + 3] = tri[id];
    93.                 tri[id + 4] = tri[id+2];
    94.                 tri[id + 5] = tri[id]+1;
    95.  
    96.                 id += 6;
    97.             }
    98.         }
    99.  
    100.         //define uvs...
    101.         /*uvs = new Vector2[x_cnt * z_cnt];
    102.         for (int i = 0; i < z_cnt; i++)
    103.         {
    104.             for (int j = 0; j < x_cnt; j++)
    105.             {
    106.                 int idx = i * x_cnt + j;
    107.                 uvs[idx] = new Vector2(j/(x_cnt-1),i/(z_cnt-1));
    108.             }
    109.         }*/
    110.  
    111.         MeshMake(ref vert, ref tri, mat);  //mesh without uv mappin..
    112.     }
    113. }
    114.  
    Here's my results:

    for x_count = 200 & z_count = 200
    pl1.JPG

    for x_count = 500 & z_count = 500
    pl2.JPG
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Are you sure it's the mesh and not the rendering? this kinda looks like shadow acne. (edit: actually looks like artefacts from the wireframe?)
    if you divide it into multiple meshes of the same total size does this still happen?
     
  3. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    11
    It's in shaded wireframe mode.
    I was using it to check if mesh is generated correctly or not. (like culling issue, triangles generated..)
     
  4. kalkuzoguzhann

    kalkuzoguzhann

    Joined:
    Apr 5, 2021
    Posts:
    1
    The issue is with the integer bit size that exceeds the mesh vertices array length limit. Integers used here are 16 bit which makes them 2^16 = 65536. As you create 2-dimensional plane here with with lets say width=256 and height=256 and which makes the vertices count equal to w*h which is 256*256=65536, any width or height increase after this limit will result in the loss of vertices. For solving this issue, try to make more planes connected to each other, which is called 'chunks' in the terrain generation concept, or set the indexFormat to 32 bits using:
    mesh.indexFormat = IndexFormat.UInt32;
    .
     
    Last edited: Aug 14, 2022
    Bunny83 likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    2^32 is not 65536.

    2^16 is 65536.
    2^32 is 4294967296.