Search Unity

Adding caves to Perlin Noise generation?

Discussion in '2D' started by Deanford, Aug 22, 2017.

  1. Deanford

    Deanford

    Joined:
    Oct 17, 2014
    Posts:
    93
    Hi,

    I have a procedural perlin noise generation in my game that generates the world in chunks and I want to add caves to the generation, could someone help with that?

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class level_gen : MonoBehaviour {
    6.  
    7.     //Basic blocks
    8.     public GameObject moonrockTile;
    9.     public GameObject moonstoneTile;
    10.     public GameObject mudTile;
    11.     public GameObject moonsandTile;
    12.  
    13.     //Ores
    14.     public GameObject tileGold;
    15.     public GameObject tileCopper;
    16.     public GameObject tileSilver;
    17.  
    18.     public int width;
    19.     public float heightMultiplier;
    20.     public int heightAddition;
    21.  
    22.     public float smoothness;
    23.  
    24.     public float seed;
    25.  
    26.     public float chanceGold;
    27.     public float chanceCopper;
    28.     public float chanceSilver;
    29.  
    30.     void Start ()
    31.     {
    32.         Generate ();
    33.         seed = Random.Range (-10000, 10000);
    34.     }
    35.  
    36.     public void Generate ()
    37.     {
    38.         for(int i = 0; i < width; i++)
    39.         {
    40.             int h = Mathf.RoundToInt (Mathf.PerlinNoise (seed, (i +transform.position.x) / smoothness) * heightMultiplier) + heightAddition;
    41.  
    42.             for (int j = 0; j < h; j++)
    43.             {
    44.                 GameObject selectedTile;
    45.  
    46.                 //Layers of tiles here:
    47.                 if (j < h - 8)
    48.                 {
    49.                     selectedTile = moonstoneTile;
    50.                 }
    51.                 else if (j < h -4)
    52.                 {
    53.                     selectedTile = mudTile;
    54.                 }
    55.                 else if (j < h - 2)
    56.                 {
    57.                     selectedTile = moonsandTile;
    58.                 }
    59.                 else
    60.                 {
    61.                     selectedTile = moonrockTile;
    62.                 }
    63.  
    64.                 //Spawn new tiles and add to Generate gameobject
    65.                 GameObject newTile = Instantiate (selectedTile, Vector3.zero, Quaternion.identity) as GameObject;
    66.  
    67.                 newTile.transform.parent = this.gameObject.transform;
    68.                 newTile.transform.localPosition = new Vector3 (i, j);
    69.             }
    70.         }
    71.  
    72.         GenOre ();
    73.     }
    74.  
    75.     public void GenOre()
    76.     {
    77.         foreach(GameObject t in GameObject.FindGameObjectsWithTag("tile_stone"))
    78.         {
    79.             if (t.transform.parent == this.gameObject.transform)
    80.             {
    81.                 float r = Random.Range (0f, 100f);
    82.                 GameObject selectedTile = null;
    83.  
    84.                 if (r < chanceGold)
    85.                 {
    86.                     selectedTile = tileGold;
    87.                 }
    88.                 else if (r < chanceSilver)
    89.                 {
    90.                     selectedTile = tileSilver;
    91.                 }
    92.                 else if(r < chanceCopper)
    93.                 {
    94.                     selectedTile = tileCopper;
    95.                 }
    96.  
    97.                 if(selectedTile != null)
    98.                 {
    99.                     GameObject newResourceTile = Instantiate (selectedTile, t.transform.position, Quaternion.identity) as GameObject;
    100.                     newResourceTile.transform.parent = transform;
    101.                     Destroy (t);
    102.                 }
    103.             }
    104.         }
    105.     }
    106. }
    107.  

    Also a quick question; How would I create a black fade on tiles far away like Terraria

    Example: http://imgur.com/a/zbJxo

    Help would be really appreciated!

    Thanks peeps!

    Dean
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Generate a second field of perlin noise and subtract it from the first
     
  3. Deanford

    Deanford

    Joined:
    Oct 17, 2014
    Posts:
    93
    Hi,

    thanks for the reply, for the cave generation or the black fade (lighting) on the tiles?
     
  4. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    Do something like this
    Code (csharp):
    1.     float noiseValue = Mathf.PerlinNoise(xFloat, yFloat);
    2.                 if (noiseValue > .6f)
    3.                      // it's a cave
     
  5. Deanford

    Deanford

    Joined:
    Oct 17, 2014
    Posts:
    93
    Ah okay, im not well known with purlin noise, I have the script below to make the terrain, so regarding your post above how would I implement that into my script below?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class level_gen : MonoBehaviour {
    6.  
    7.     //Basic blocks
    8.     public GameObject moonrockTile;
    9.     public GameObject moonstoneTile;
    10.     public GameObject mudTile;
    11.     public GameObject moonsandTile;
    12.  
    13.     //Ores
    14.     public GameObject tileGold;
    15.     public GameObject tileCopper;
    16.     public GameObject tileSilver;
    17.  
    18.     public int width;
    19.     public float heightMultiplier;
    20.     public int heightAddition;
    21.  
    22.     public float smoothness;
    23.  
    24.     public float seed;
    25.  
    26.     public float chanceGold;
    27.     public float chanceCopper;
    28.     public float chanceSilver;
    29.  
    30.     void Start ()
    31.     {
    32.         Generate ();
    33.         seed = Random.Range (-10000, 10000);
    34.     }
    35.  
    36.     public void Generate ()
    37.     {
    38.         for(int i = 0; i < width; i++)
    39.         {
    40.             int h = Mathf.RoundToInt (Mathf.PerlinNoise (seed, (i +transform.position.x) / smoothness) * heightMultiplier) + heightAddition;
    41.  
    42.             for (int j = 0; j < h; j++)
    43.             {
    44.                 GameObject selectedTile;
    45.  
    46.                 //Layers of tiles here:
    47.                 if (j < h - 8)
    48.                 {
    49.                     selectedTile = moonstoneTile;
    50.                 }
    51.                 else if (j < h -4)
    52.                 {
    53.                     selectedTile = mudTile;
    54.                 }
    55.                 else if (j < h - 2)
    56.                 {
    57.                     selectedTile = moonsandTile;
    58.                 }
    59.                 else
    60.                 {
    61.                     selectedTile = moonrockTile;
    62.                 }
    63.  
    64.                 //Spawn new tiles and add to Generate gameobject
    65.                 GameObject newTile = Instantiate (selectedTile, Vector3.zero, Quaternion.identity) as GameObject;
    66.  
    67.                 newTile.transform.parent = this.gameObject.transform;
    68.                 newTile.transform.localPosition = new Vector3 (i, j);
    69.             }
    70.         }
    71.  
    72.  
    73.  
    74.         GenOre ();
    75.     }
    76.  
    77.     public void GenOre()
    78.     {
    79.         foreach(GameObject t in GameObject.FindGameObjectsWithTag("tile_stone"))
    80.         {
    81.             if (t.transform.parent == this.gameObject.transform)
    82.             {
    83.                 float r = Random.Range (0f, 100f);
    84.                 GameObject selectedTile = null;
    85.  
    86.                 if (r < chanceGold)
    87.                 {
    88.                     selectedTile = tileGold;
    89.                 }
    90.                 else if (r < chanceSilver)
    91.                 {
    92.                     selectedTile = tileSilver;
    93.                 }
    94.                 else if(r < chanceCopper)
    95.                 {
    96.                     selectedTile = tileCopper;
    97.                 }
    98.  
    99.                 if(selectedTile != null)
    100.                 {
    101.                     GameObject newResourceTile = Instantiate (selectedTile, t.transform.position, Quaternion.identity) as GameObject;
    102.                     newResourceTile.transform.parent = transform;
    103.                     Destroy (t);
    104.                 }
    105.             }
    106.         }
    107.     }
    108. }
    109.  
     
  6. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    Mathf.PerlinNoise returns values between 0 and 1. Rounding it to integer does not make sense. If you have no idea what you are doing start here.