Perlin noise and its use in unity game engine to generate procedural terrain

perlin noise is a kind of gradient noise developed by ken perlin in 1983 after he was frustrated with the machine-Like look of computer graphics at that time. He first described his findings in the SIGGRAPH paper in 1985  named “An image Synthesizer“. Ken perlin was also awarded an academy award in 1987 for creating this algorithm.

 

Uses of perlin noise :

Perlin noise is a procedural texture primitive. It is a type of gradient noise which is used by visual effect artists to increase realism in computer graphics. The perlin noise function has a pseudo random appearance though all of its visual details are of same size. This property allows perlin noise to be really controllable and by controllable, i mean that multiple scaled copy of perlin noise can be inserted into mathematical expressions to create a great variety of procedural textures. Synthetic textures made using perlin noise are often used in CGI(computer generated imagery) to make computer generated visual elements such as worlds, fire, smoke, clouds, etc and they appear more natural by imitating the controlled random appearances of textures in the nature. It is also extremely important for video games development. In video games development, perlin noise can be used for any sort of wave like undulating texture or material. For example it can be used for procedural generation of terrain(Minecraft like terrain can be drawn using perlin noise function and i will also be giving out script to generate procedural terrain inside of unity in this blog), fire effects, water, clouds, and much more. These sorts of effects mostly represent perlin noise in second and third dimensions, but it can be extended into the fourth dimensions too. Additionally it can also be used in one dimension only for purpose such as side-scrolling terrains (as in like that of games Terrari or star-bound) to create illusion of handwritten lines.

to read more about perlin noise please click here (it’s the best article that i found over the net on perlin noise)

 

Using perlin noise inside of unity game engine to create procedural terrain:

Here i have attached a script that will create a random height-maps in your terrain when attached to it inside of unity game engine. You just need to tweak the value of depth and scale of height-maps to your liking and when you are satisfied, please change the update method inside the script into the start method. You can also go inside the script and make all private variables to public to have control over the size of terrain (length and width.) and you are food to go. and if you have a basic knowledge of C# programming language inside of unity, I am pretty sure that you can understand every single line of code very easily.

Script:

 

using UnityEngine;
public class PerlinNoise : MonoBehaviour {
    //this script was created by praphul pokharel  
  // we will be using old terrain engine of unity   
 public int depth = 1    ;   
 private int Width = 1000;  
  private int height = 1000;  
  public float scale = 20f;  
  public float offsetX = 100f; 
   public float offsetY = 100f;


  void  Start() {    
    offsetX = Random.Range(offsetX, 99999f);  
      offsetY = Random.Range(offsetY, 99999f);    
    // the lines below are just for animating the terrain, you can go on and delete these lines        
 offsetX += Time.deltaTime * 5f;   
 }
   

     void Update() {  
     Terrain terrain =  GetComponent<Terrain>();     
   terrain.terrainData = GenerateTerrain(terrain.terrainData); 
   }
    TerrainData GenerateTerrain(TerrainData terrainData)   
 {        terrainData.heightmapResolution = Width + 1;
        terrainData.size = new Vector3(Width,depth,  height);    
    terrainData.SetHeights(0, 0, GenerateHeights());   
     return terrainData;
    }   


 float[,] GenerateHeights() 
{        float[,] heights = new float[Width, height]; 
       for (int x = 0; x < Width; x++) { 
           for (int y = 0; y < height; y++) {     
           heights[x, y] = calculateHeight(x, y);
            }
        }      
  return heights;

    }


    float calculateHeight(int x, int y) {   
     float xCoord = (float)x / Width * scale+ offsetX;    
    float yCoord = (float)y / height*scale+ offsetY;    
    return Mathf.PerlinNoise(xCoord, yCoord);
    }
 }

click here to download script

Demo video on effect of scripts:

 

if you have any question regarding the script or if you find it difficult to understand something in this scripts please leave a comment or contact me personally at praphulgerrard1@gmail.com, I will be more than happy to assist you personally, or you can also go to menu tab, click on facebook link and leave me a message there, i will respond to you as soon as i will see the message.

 

Thank you,

 

 


 

 

 

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s