- Home /
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 http://gamedev.stackexchange.com/questions/54276/a-simple-method-to-create-island-map-mask 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 http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/ 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.
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;
}
}
It's not clear on exactly what you are stuck. The links you have in your question are answered, with code samples, that are in c#. Please be a bit more specific.
Can you show us what your code is? Have you attempted to even try writing code to fix this?
Yes sorry about that, I cannot figure out how to implement the segments of code from the examples.. There are 3 functions and one bit I'm not sure where that fits.. I tried searching then for ways to apply a mask to a height map with no solution.. So I'm stuck. Any workaround or context of how to implement the sampled code would be greatly appreciated.
considering the code in those links is in c#, you should be able to just directly copy most of it?
and what do you mean with applying a mask to a height map? Set all heights outside (or would that be inside?) the mask area to a predeter$$anonymous$$ed value?
ins$$anonymous$$d of setting your height to what make$$anonymous$$ask returns, multiply the current height with that result.
Answer by siaran · May 07, 2015 at 09:34 PM
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.
Answer by 4t0m1c · May 08, 2015 at 11:50 PM
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;
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
[BEGINNER] Generating Terrain with Perlin noise flat and nothing happens ? 1 Answer
Procedual Terrain 1 Answer
Attempting terrain generation 1 Answer
perlin noise terrain is spikey 0 Answers