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

Procedural landscape generation with mesh generator don't scale up

Discussion in 'Scripting' started by Underbrine, Apr 6, 2021.

  1. Underbrine

    Underbrine

    Joined:
    Apr 6, 2021
    Posts:
    5
    Hello dear community,

    I am trying to make a procedural generated landscape using the mesh generator tutorial made by Brackeys but, after adding a few modifications i can't achieve to scale up the world. I have a 200x200 map now

    But when i'm scaling it up i finish with this kind of result, like if my triangles are connected to the beggining ones...


    Here is my code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(MeshFilter))]
    6. public class MeshGenerator : MonoBehaviour
    7. {
    8.     Mesh mesh; //mesh
    9.     Vector3[] vertices; //vertices array
    10.     int[] triangles; //Triangle array
    11.  
    12.     //Size of the noise grid
    13.     public int xSize = 200;
    14.     public int zSize = 200;
    15.  
    16.     public bool showGizmos = false;
    17.  
    18.     //Offset of the noise grid
    19.     public float xOffset = 0;
    20.     public float zOffset = 0;
    21.  
    22.     //Frequencies of the perlin noise octaves
    23.     public float frequency1 = .1f;
    24.     public float frequency2 = .5f;
    25.     public float frequency3 = 1f;
    26.  
    27.     private float minTerrainHeight;
    28.     private float maxTerrainHeight;
    29.  
    30.     //height of the perlin noise octaves
    31.     public float terrainHeight1 = 2f;
    32.     public float terrainHeight2 = 1f;
    33.     public float terrainHeight3 = .5f;
    34.  
    35.     //Gradient for the map coloring
    36.     public Gradient gradient;
    37.  
    38.     Color[] colors;
    39.    
    40.     // Start is called before the first frame update
    41.     void Start()
    42.     {
    43.         mesh = new Mesh();
    44.         GetComponent<MeshFilter>().mesh = mesh;
    45.         CreateShape();
    46.         UpdateMesh();
    47.        
    48.     }
    49.     void Update()
    50.     {
    51.         CreateShape();
    52.         UpdateMesh();
    53.     }
    54.     void CreateShape()
    55.     {
    56.         vertices = new Vector3[(xSize + 1) * (zSize + 1)];  //Vertices array
    57.  
    58.         for (int i = 0, z = 0; z <= zSize; z++)
    59.         {
    60.             for (int x = 0; x <= xSize; x++) //Loop for all the vertices
    61.             {
    62.                 //Creating the height of the vertex with multiple perlin noise values
    63.                 float y = (Mathf.PerlinNoise(x * frequency1 + xOffset, z * frequency1 + zOffset) * terrainHeight1)
    64.                      + (Mathf.PerlinNoise(x * frequency2 + xOffset, z * frequency2 + zOffset) * terrainHeight2) +
    65.                     (Mathf.PerlinNoise(x * frequency3 + xOffset, z * frequency3 + zOffset) * terrainHeight3);
    66.                 if (y < 0)
    67.                 {
    68.                     y = 0;
    69.                 }
    70.                 vertices[i] = new Vector3(x, y, z); //Creating the vertex
    71.                
    72.                 if(y > maxTerrainHeight)
    73.                 {
    74.                     maxTerrainHeight = y;
    75.                 }
    76.                 if (y < minTerrainHeight)
    77.                 {
    78.                     minTerrainHeight = y;
    79.                 }
    80.                 i++;
    81.             }
    82.  
    83.         }
    84.  
    85.         triangles = new int[xSize * zSize * 6]; //Triangles array
    86.  
    87.         int vert = 0;
    88.         int tris = 0;
    89.  
    90.         for (int z = 0; z < zSize; z++)
    91.         {
    92.             for (int x = 0; x < xSize; x++) //Creating triangles from vertices
    93.             {
    94.                 triangles[tris + 0] = vert + 0;
    95.                 triangles[tris + 1] = vert + xSize + 1;
    96.                 triangles[tris + 2] = vert + 1;
    97.                 triangles[tris + 3] = vert + 1;
    98.                 triangles[tris + 4] = vert + xSize + 1;
    99.                 triangles[tris + 5] = vert + xSize + 2;
    100.                 vert++;
    101.                 tris += 6;
    102.             }
    103.             vert++;
    104.         }
    105.  
    106.         colors = new Color[vertices.Length]; //Colors array
    107.  
    108.         for (int i = 0, z = 0; z <= zSize; z++)
    109.         {
    110.             for (int x = 0; x <= xSize; x++) //Applying colors
    111.             {
    112.                 float height = Mathf.InverseLerp(minTerrainHeight,maxTerrainHeight,vertices[i].y);
    113.                 colors[i] = gradient.Evaluate(height);
    114.                 i++;
    115.             }
    116.  
    117.         }
    118.     }
    119.  
    120.     void UpdateMesh()
    121.     {
    122.         mesh.Clear(); //Clear the mesh vertices and triangle
    123.  
    124.         //Applying vertices, triangles and colors
    125.         mesh.vertices = vertices;
    126.         mesh.triangles = triangles;
    127.         mesh.colors = colors;
    128.  
    129.         mesh.RecalculateNormals(); //Calculating the normals
    130.  
    131.         //Calculating the colliders
    132.         mesh.RecalculateBounds();
    133.         MeshCollider meshCollider = gameObject.GetComponent<MeshCollider>();
    134.         meshCollider.sharedMesh = mesh;
    135.     }
    136.  
    137.     // Show Vertices
    138.     private void OnDrawGizmos()
    139.     {
    140.         if (showGizmos)
    141.         {
    142.             if (vertices == null)
    143.             {
    144.                 return;
    145.             }
    146.             for (int i = 0; i < vertices.Length; i++)
    147.             {
    148.                 Gizmos.DrawSphere(vertices[i], .1f);
    149.             }
    150.         }
    151.        
    152.     }
    153. }
    154.  
    If someone have the power to help me, it would be cool, thanks :)
     
  2. Underbrine

    Underbrine

    Joined:
    Apr 6, 2021
    Posts:
    5
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Underbrine likes this.
  4. Underbrine

    Underbrine

    Joined:
    Apr 6, 2021
    Posts:
    5
    I Think i can try this. Is it just a variable to change in my script ? How do i use it ?

    Thanks for your fast reply !