Unity and terrain generation

So here it is. Terrain generation is back, now in Unity. I started learning Unity (from basics, how it works etc.) over week ago. And it’s amazing! It has very good documentation and helpful community. Here we have my terrain generator made in Unity.

[youtube http://www.youtube.com/watch?v=hziuLRpN2fg]

Terrain generation

Unity made it easy. I could write my own function using, for example Fractional Brownian motion with some noise function, but I was too lazy to do that. I used Mathf.PerlinNoise:

// Javascript (Unityscript);
function GetPerlinNoise(x : int, y : int, seed : int) {
 var NoiseSize : float = 128.0; // It depends on blocksize and this if you want to have more flat terrain or not;
 var NoiseVal : float = Mathf.PerlinNoise(seed+ x/NoiseSize, seed+ y/NoiseSize );

 return NoiseVal; // Returns value from 0.0 to 1.0;

function GetTerrainHeight(x : int, y : int, seed : int) {
 var ReturnVal : float = 0.0;

 ReturnVal = GetPerlinNoise(x,y,seed);

 // Height;
 ReturnVal = ReturnVal * TerrainMaxHeight;

 // Blocky like terrain; Comment this if you want to get smooth terrain;
 ReturnVal = Mathf.Floor(ReturnVal);

 return ReturnVal; // This return real terrain height from 0.0 to TerrainMaxHeight;

And yes, I know seed doesn’t work that way but this was the easiest way to make terrain generation dependent on seed.


After clicking button chunks are generated. After generating chunk it creates thread which builds triangles for terrain and water. Threads are made with System.Threading. Unfortunately it’s still slow and laggy during generation and building triangles but at least it is made ‘in background’. After building triangles there is made GameObject with MeshFilterMeshCollider and MeshRenderer. There is no water physics, objects just fall through it, but it’s transparent. There is no blocks, it’s just something like Terrain that comes with Unity.

Drawing height map

To get color of terrain from value 0.0-1.0 I used idea from this site. Basically this is the whole thing:

Here’s my code:

// Javascript (Unityscript);
TempH = TerrainHeight*4.0; // Where TerrainHeight is value from 0.0 to 1.0;
if((TempH>=0.0)&&(TempH<1.0)) { ColorR=0.0; ColorG=TempH; ColorB=1.0; }
if((TempH>=1.0)&&(TempH<2.0)) { ColorR=0.0; ColorG=1.0; ColorB=2.0-TempH; }
if((TempH>=2.0)&&(TempH<3.0)) { ColorR=TempH-2.0; ColorG=1.0; ColorB=0.0; }
if((TempH>=3.0)&&(TempH<=4.0)) { ColorR=1.0; ColorG=4.0-TempH; ColorB=0.0; }
// We get color: Color(ColorR,ColorG,ColorB);

And it looks like this:So that’s all about this terrain generator. I will probably get back to this idea someday adding for example water physics (buoyancy) and more!


One thought on “Unity and terrain generation

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s