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

Bug My falloff map isn't working with randomly generated terrain.

Discussion in 'Editor & General Support' started by JonteErik, Jun 4, 2023.

  1. JonteErik

    JonteErik

    Joined:
    Jan 29, 2022
    Posts:
    5
    So i made this random terrain generator but i want it to be islands so i tried to make a falloff map for it. However when i play it nothing is being generated and i get an error saying "Object reference not set to an instance of an object". Here's my code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MeshGenerator : MonoBehaviour
    6. {
    7.     public Mesh mesh;
    8.     Vector3[] vertices;
    9.     Color[] colors;
    10.     int[] triangles;
    11.     public Gradient gradient;
    12.  
    13.     public int XSize = 20;
    14.     public int ZSize = 20;
    15.     public int octaves;
    16.     float minTerrainHeight;
    17.     float maxTerrainHeight;
    18.  
    19.     [Range(0.0f, 100.0f)]
    20.     [SerializeField] private float amplitude_1;
    21.     [Range(0.0f, 100.0f)]
    22.     [SerializeField] private float scale_1;
    23.     [Range(0.0f, 1.0f)]
    24.     [SerializeField] private float amplitude_2;
    25.     [Range(0.0f, 5.0f)]
    26.     [SerializeField] private float scale_2;
    27.     [Range(0.0f, 1.0f)]
    28.     [SerializeField] private float amplitude_3;
    29.     [Range(0.0f, 1.0f)]
    30.     [SerializeField] private float scale_3;
    31.  
    32.     private float CalculateNoise(float x, float z)
    33.     {
    34.         float noise;
    35.         noise = Mathf.PerlinNoise(x, z) * 5;
    36.         noise += Mathf.PerlinNoise(x * amplitude_1, z * amplitude_1) * scale_1;
    37.         noise -= Mathf.PerlinNoise(x * amplitude_2, z * amplitude_2) * scale_2;
    38.         noise += Mathf.PerlinNoise(x * amplitude_3, z * amplitude_3) * scale_3 * 2;
    39.  
    40.         return noise;
    41.     }
    42.  
    43.     void Start()
    44.     {
    45.         mesh = new Mesh();
    46.         GetComponent<MeshFilter>().mesh = mesh;
    47.         GetComponent<MeshCollider>().sharedMesh = mesh;
    48.         CreateFalloffMap();
    49.         CreateShape();
    50.     }
    51.  
    52.     private void Update()
    53.     {
    54.         UpdateMesh();
    55.     }
    56.  
    57.     void CreateShape()
    58.     {
    59.         vertices = new Vector3[(XSize + 1) * (ZSize + 1)];
    60.  
    61.         for (int i = 0, z = 0; z <= ZSize; z++)
    62.         {
    63.             for (int x = 0; x <= XSize; x++)
    64.             {
    65.                 float yWithNoise = CalculateNoise(x, z);
    66.                 vertices[i] = new Vector3(x, yWithNoise, z);
    67.  
    68.                 if (yWithNoise > maxTerrainHeight)
    69.                 {
    70.                     maxTerrainHeight = yWithNoise;
    71.                 }
    72.                 if (yWithNoise < minTerrainHeight)
    73.                 {
    74.                     minTerrainHeight = yWithNoise;
    75.                 }
    76.                 i++;
    77.             }
    78.         }
    79.  
    80.         triangles = new int[XSize * ZSize * 6];
    81.  
    82.         int vert = 0;
    83.         int tris = 0;
    84.  
    85.         for (int z = 0; z < ZSize; z++)
    86.         {
    87.             for (int x = 0; x < XSize; x++)
    88.             {
    89.                 triangles[tris + 0] = vert + 0;
    90.                 triangles[tris + 1] = vert + XSize + 1;
    91.                 triangles[tris + 2] = vert + 1;
    92.                 triangles[tris + 3] = vert + 1;
    93.                 triangles[tris + 4] = vert + XSize + 1;
    94.                 triangles[tris + 5] = vert + XSize + 2;
    95.  
    96.                 vert++;
    97.                 tris += 6;
    98.             }
    99.             vert++;
    100.         }
    101.  
    102.         colors = new Color[vertices.Length];
    103.         for (int i = 0, z = 0; z <= ZSize; z++)
    104.         {
    105.             for (int x = 0; x <= XSize; x++)
    106.             {
    107.                 float height = Mathf.InverseLerp(minTerrainHeight, maxTerrainHeight, vertices[i].y);
    108.                 colors[i] = gradient.Evaluate(height);
    109.                 i++;
    110.             }
    111.         }
    112.     }
    113.  
    114.     void CreateFalloffMap()
    115.     {
    116.         float[,] falloffMap = new float[XSize, ZSize];
    117.         for (int y = 0; y < ZSize; y++)
    118.         {
    119.             for (int x = 0; x < XSize; x++)
    120.             {
    121.                 float xv = x / (float)XSize * 2 - 1;
    122.                 float yv = y / (float)ZSize * 2 - 1;
    123.                 float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv));
    124.                 falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f));
    125.             }
    126.         }
    127.  
    128.         // Apply falloff map to vertices
    129.         for (int i = 0, z = 0; z <= ZSize; z++)
    130.         {
    131.             for (int x = 0; x <= XSize; x++)
    132.             {
    133.                 vertices[i].y *= falloffMap[x, z];
    134.                 i++;
    135.             }
    136.         }
    137.     }
    138.  
    139.     void UpdateMesh()
    140.     {
    141.         mesh.Clear();
    142.         mesh.vertices = vertices;
    143.         mesh.triangles = triangles;
    144.  
    145.         mesh.RecalculateNormals();
    146.         mesh.RecalculateBounds();
    147.         MeshCollider meshCollider = gameObject.GetComponent<MeshCollider>();
    148.         meshCollider.sharedMesh = mesh;
    149.         mesh.colors = colors;
    150.     }
    151. }
    Any help on how to solve the issue is appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,194