Procedural Island Terrain Generation

Edit: Rewrote my question after trying a few things and made it more specific.

Hi, so I’m creating a mobile RTS game with procedurally generated maps. I’ve worked out how to create a terrain with a basic perlin noise on it, and tried to integrate c# - A Simple Method To Create Island Map Mask - Game Development Stack Exchange method to creating an island procedurally. This is the result so far:

As you can see, the edges are great but the main area of the island is completely maxed at 1 and completely un-random in shape.

The image below from Polygonal Map Generation for Games shows the kind of terrain I’m after. The tutorial there is great but would be too intensive, thus the post.

I want the Random Shaped island with Perlin noise generated land mass, surrounded by water.

This is the kind of shaping I want

edit: I’ve narrowed the problem down to the somewhere in the last 3 functions and I don’t quite understand it enough to figure it out.

Here is my code. A script attached to a null with a button to activate Begin():

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Gen_Perlin : MonoBehaviour {

	public float Tiling = 3.0f;
	public float min = 100000.0f;
	public float max = -100000.0f;
	private bool active = false;
	public int mapHeight = 50;
	
	public void Begin()
	{
		if (active == false) {
			TerrainData terrainData = new TerrainData ();
			const int size = 513;
			terrainData.heightmapResolution = size;
			terrainData.size = new Vector3 (2000, mapHeight, 2000);
		
			terrainData.heightmapResolution = 513;
			terrainData.baseMapResolution = 1024;
			terrainData.SetDetailResolution (1024, 1024);

			Terrain.CreateTerrainGameObject (terrainData);
			GameObject obj = GameObject.Find ("Terrain");
			obj.transform.parent = this.transform;

			if (obj.GetComponent<Terrain> ()) {
				Debug.Log ("Gen_Begin- " + obj);
				GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
			}
		} else {
			GameObject obj = GameObject.Find ("Terrain");
			if (obj.GetComponent<Terrain> ()) {
				Debug.Log ("Gen_Begin- " + obj);
				GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
			}
		}
	}
	
	public void GenerateHeights(Terrain terrain, float tileSize)
	{
		Debug.Log ("Start_Height_Gen");
		float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];
		
		for (int i = 0; i < terrain.terrainData.heightmapWidth; i++)
		{
			for (int k = 0; k < terrain.terrainData.heightmapHeight; k++)
			{
				heights[i, k] = Mathf.PerlinNoise(((float)i / (float)terrain.terrainData.heightmapWidth) * tileSize, ((float)k / (float)terrain.terrainData.heightmapHeight) * tileSize);
				if (heights[i, k] > max) max = heights[i, k];
				if (heights[i, k] < min) min = heights[i, k];
				heights[i, k] = makeMask( terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight, i, k, heights[i, k] );
			}
		}

		Debug.Log ("Set_Height");
		terrain.terrainData.SetHeights(0, 0, heights);
		Debug.Log ("Done");
		//this.gameObject.AddComponent <Gen_Prefabs>();
	}

	public static float makeMask( int width, int height, int posX, int posY, float oldValue ) {
		int minVal = ( ( ( height + width ) / 2 ) / 100 * 2 );
		int maxVal = ( ( ( height + width ) / 2 ) / 100 * 20 );
		if( getDistanceToEdge( posX, posY, width, height ) <= minVal ) {
			return 0;
		} else if( getDistanceToEdge( posX, posY, width, height ) >= maxVal ) {
			return 1;
		} else {
			float factor = getFactor( getDistanceToEdge( posX, posY, width, height ), minVal, maxVal );
			return ( oldValue + oldValue ) * factor;
		}
	}
	
	private static float getFactor( int val, int min, int max ) {
		int full = max - min;
		int part = val - min;
		float factor = (float)part / (float)full;
		return factor;
	}
	
	public static int getDistanceToEdge( int x, int y, int width, int height ) {
		int[] distances = new int[]{ y, x, ( width - x ), ( height - y ) };
		int min = distances[ 0 ];
		foreach( var val in distances ) {
			if( val < min ) {
				min = val;
			}
		}
		return min;
	}
}

from what I can tell, I think (not sure) that the normalisation loop he’s talking about is just part of the code he’s written to generate his perlin noise for him, and he’s putting his makemask method inside that loop so that he doesn’t need to go through it again.

As for applying a mask to a heightmap, that seems quite simple then:

(assuming your mask has the same dimensions as your heightmap, which it really should or it doesn’t make sense)

just loop through the indices of your heightmap, and check your mask at those same indices (same size so they exist), and if that part of your height mask is the ‘black’ part of it you set the heightmap to 0 and if it’s not you leave it as it is. Looking at his code that’s actually basically what the MakeMask method does, except it uses a factor to slightly change the height at the ‘edges’ of the generated mask.

So thanks to @siaran, the working version of the basic generation with 1st phase Perlin noise is as follows:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Gen_Perlin : MonoBehaviour {

public float Tiling = 0.5f;
private bool active = false;
public int mapHeight = 10;

public void Begin()
{
	if (active == false) {
		TerrainData terrainData = new TerrainData ();
		const int size = 513;
		terrainData.heightmapResolution = size;
		terrainData.size = new Vector3 (2000, mapHeight, 2000);
	
		terrainData.heightmapResolution = 513;
		terrainData.baseMapResolution = 1024;
		terrainData.SetDetailResolution (1024, 1024);

		Terrain.CreateTerrainGameObject (terrainData);
		GameObject obj = GameObject.Find ("Terrain");
		obj.transform.parent = this.transform;

		if (obj.GetComponent<Terrain> ()) {
			GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
		}
	} else {
		GameObject obj = GameObject.Find ("Terrain");
		if (obj.GetComponent<Terrain> ()) {
			GenerateHeights (obj.GetComponent<Terrain> (), Tiling);
		}
	}
}

public void GenerateHeights(Terrain terrain, float tileSize)
{
	Debug.Log ("Start_Height_Gen");
	float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];
	
	for (int i = 0; i < terrain.terrainData.heightmapWidth; i++)
	{
		for (int k = 0; k < terrain.terrainData.heightmapHeight; k++)
		{
			heights[i, k] = 0.25f + Mathf.PerlinNoise(((float)i / (float)terrain.terrainData.heightmapWidth) * tileSize, ((float)k / (float)terrain.terrainData.heightmapHeight) * tileSize);
			heights[i, k] *= makeMask( terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight, i, k, heights[i, k] );
		}
	}
	terrain.terrainData.SetHeights(0, 0, heights);
}

public static float makeMask( int width, int height, int posX, int posY, float oldValue ) {
	int minVal = ( ( ( height + width ) / 2 ) / 100 * 2 );
	int maxVal = ( ( ( height + width ) / 2 ) / 100 * 10 );
	if( getDistanceToEdge( posX, posY, width, height ) <= minVal ) {
		return 0;
	} else if( getDistanceToEdge( posX, posY, width, height ) >= maxVal ) {
		return oldValue;
	} else {
		float factor = getFactor( getDistanceToEdge( posX, posY, width, height ), minVal, maxVal );
		return oldValue * factor;
	}
}

private static float getFactor( int val, int min, int max ) {
	int full = max - min;
	int part = val - min;
	float factor = (float)part / (float)full;
	return factor;
}

public static int getDistanceToEdge( int x, int y, int width, int height ) {
	int[] distances = new int[]{ y, x, ( width - x ), ( height - y ) };
	int min = distances[ 0 ];
	foreach( var val in distances ) {
		if( val < min ) {
			min = val;
		}
	}
	return min;
}

}