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

Question Scale Selfmade mesh object up, lower mesh vertices. (LOD system)

Discussion in 'Scripting' started by Aviation_Simmer, Nov 1, 2022.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Hi there! I am working on a terrain generator. Now.. Since it needs performance inprovements, Level of detail is very helpful. My Idea: Scale the overal gameobject up, and remove some vertices. Sound simple.... but isnt..
    I think there is a better way to approach this.. eg. by just removing some vertices and make the triangles bigger. thats acctually the same but a solution without scaling. Anyway... maybe somebody can help me please! thanks.

    Code (CSharp):
    1.     [Range(1, 6)]
    2.     public int LevelOfDetail;
    Code (CSharp):
    1.         float distance = Vector3.Distance(Player.transform.position, this.transform.position);
    2.         if (distance > maxLODdistance) distance = maxLODdistance;
    3.         int lod = ((int)(distance / (maxLODdistance / 5)));
    4.         if (lod < 0) lod = 0;
    5.         LevelOfDetail = lod + 1;
    Code (CSharp):
    1.     public void CreateShape()
    2.     {
    3.         transform.localScale = new Vector3(LevelOfDetail, 1, LevelOfDetail);
    4.         vertices = new Vector3[(X_Size + 1) * (Z_Size + 1)];
    5.  
    6.         for (int i = 0, z = 0; z <= Z_Size / LevelOfDetail; z++)
    7.         {
    8.             for (int x = 0; x <= X_Size / LevelOfDetail; x++)
    9.             {
    10.                 vertices[i] = new Vector3(x, 0, z);
    11.                 i++;
    12.             }
    13.         }
    14.  
    15.         triangles = new int[X_Size * Z_Size * 6];
    16.         int vert = 0;
    17.         int tris = 0;
    18.         for (int z = 0; z < Z_Size / LevelOfDetail; z++)
    19.         {
    20.             for (int x = 0; x < X_Size / LevelOfDetail; x++)
    21.             {
    22.                 triangles[tris + 0] = vert + 0;
    23.                 triangles[tris + 1] = vert + X_Size + 1;
    24.                 triangles[tris + 2] = vert + 1;
    25.                 triangles[tris + 3] = vert + 1;
    26.                 triangles[tris + 4] = vert + X_Size + 1;
    27.                 triangles[tris + 5] = vert + X_Size + 2;
    28.                 vert++;
    29.                 tris += 6;
    30.             }
    31.             vert++;
    32.         }
    33.     }
     
  2. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Acctually.. I somehow made it! Nothing to see here :) but for improvements or better ideas, I'll take them! thanks upload_2022-11-1_18-36-6.png