# How can I make my algorithm make cloudy heightmaps?

My game relies heavily on my terrain generation algorithm.
The only problem is, is that my terrain is jagged. Suddenly, a mountain starts.
And then it is perfectly flat. I want flat ground, but INTERESTING flat ground.
and my mountain aren’t mountainous. The heightmap is below.

Seriously blocky.
My algorithm is this.
(Yes, I’ve seen a thread with an algorithm JUST like this one.
I did not steal. Hand on heart.)

``````function Start () {
var res = 513;
var tData = GetComponent(Terrain).terrainData;
tData.heightmapResolution = res;
var heights = new float[res, res];
for (var i = 0; i < res*res; i++) heights[i%res, i/res] = Random.Range(.51, .519);
tData.SetHeights (0, 0, heights);
var flat = new float[50, 50];
for (i = 0; i < 50*50; i++) flat[i%50, i/50] = .5;
for (i = 0; i < 75; i++) {
tData.SetHeights (Random.Range(0, res-50), Random.Range(0, res-50), flat);
}
}
``````

Yes, this is my first game. Any support would be delightful.

The reason you have jaggies is because you are using such a large range for each sample. Perlin Noise interpolates.

Here is a script that takes two samples of perlin, then combines them.

This modifies terrain, so be sure to use a New Terrain. I won’t be held responsible for lost terrain.

Step 1 : Backup your terrain ! Right click it, and hit Export Package…

Create a new scene, create a New Terrain, attach this script to the terrain or an empty gameObject.

Hit play, then Left-Mouse-Click to add noise to the terrain.

Modify the values of one of the octaves, then click LMB.

Then start playing around with the offsets and click LMB.

``````// April 6th 2013
// TwoSamplePerlinTerrain.js by Jay Kay (Alucard Jay)
#pragma strict

public var terrain : Terrain;
private var terrainData : TerrainData;
private var heightmapWidth : int;
private var heightmapHeight : int;

public var sampleOneOctave : float = 2.0;
public var sampleTwoOctave : float = 5.0;

public var sampleOneOffset : Vector2 = Vector2.zero;
public var sampleTwoOffset : Vector2 = Vector2.zero;

function Start()
{
if ( !terrain )
{
terrain = Terrain.activeTerrain;
}

terrainData = terrain.terrainData;

heightmapWidth = terrain.terrainData.heightmapWidth;
heightmapHeight = terrain.terrainData.heightmapHeight;
}

function Update()
{
if ( Input.GetMouseButtonDown(0) )
{
GeneratePerlinTerrain();
}
}

function GeneratePerlinTerrain()
{
var heightmapData : float[,] = terrainData.GetHeights( 0, 0, heightmapWidth, heightmapHeight );

for ( var y : int = 0; y < heightmapHeight; y ++ )
{
for ( var x : int = 0; x < heightmapWidth; x ++ )
{
var perlinSampleOne : Vector2 = new Vector2( ( ( sampleOneOctave / parseFloat( heightmapWidth ) ) * parseFloat( x ) ) + sampleOneOffset.x, ( ( sampleOneOctave / parseFloat( heightmapHeight ) ) * parseFloat( y ) ) + sampleOneOffset.y );
var perlinHeightOne : float = Mathf.PerlinNoise( perlinSampleOne.x, perlinSampleOne.y );

var perlinSampleTwo : Vector2 = new Vector2( ( ( sampleTwoOctave / parseFloat( heightmapWidth ) ) * parseFloat( x ) ) + sampleTwoOffset.x, ( ( sampleTwoOctave / parseFloat( heightmapHeight ) ) * parseFloat( y ) ) + sampleTwoOffset.y );
var perlinHeightTwo : float = Mathf.PerlinNoise( perlinSampleTwo.x, perlinSampleTwo.y );

heightmapData[y,x] = ( perlinHeightOne + perlinHeightTwo ) * 0.5;
}
}

terrainData.SetHeights( 0, 0, heightmapData );
}
``````

There are more examples out there. Some good suggestions on the answer to this question : Is it possible to do this for a random terrain approach? - Questions & Answers - Unity Discussions

Also there is a script in the Unity Wiki.

I agree with alucardj. You should use perlin noise. It works very well for terrain generation. I don’t like Mathf.PerlinNoise. it never worked for me. I wrote a perlin noise algorithm that I can send you if you’d like. It works very well