Archive | March 2013

Perlin Mountains

Perlin Mountains
Solo project
Unity3D – C#
December 2012

perlin01 perlin02 perlin03

This program was created as a simple test of randomly generating terrain in Unity. The landscape is created from code, rather than being hand-sculpted. This was done to test the viability of such as system for use in larger projects, such as Roadtrip.

Unity includes a heightmap-based terrain system which can be sculpted in the editor. The challenge of this program was in implementing an algorithm for generating 2D Perlin noise in C#. A user-defined seed is used to initialize a random number generator for consistent results. Noise output is used to build a heightmap for the terrain object. The resulting terrain is then painted with different textures based on slope and elevation, and grass detail sprites are added to flatter areas.


Code sample:

public void BuildTerrain()
     _terrain = GetComponent<Terrain>();
     _terrainData = _terrain.terrainData;

     int w = _terrainData.heightmapWidth;
     float[,] heights = _terrainData.GetHeights(0, 0, w, w);

     for (int y = 0; y < w; ++y)
          for (int x = 0; x < w; ++x)
               heights[y, x] = PerlinNoise(x/64f, y/64f);

     _terrainData.SetHeights(0, 0, heights);