Saturday, July 9, 2016

unity - TopDown Island Generation


I am trying to generate an island using Simplex Noise. To actually make a island shape i used Amitp's answers to the following questions:


1.Fast, simple procedural 2d island generation


2.Generating random Pools or lakes


Using them i have snippet of code for generation:(width/height is 128)


for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
// amitp's solution
Vector2 vec = new Vector2(2 * x / WIDTH - 1, 2 * y / HEIGHT - 2);

float d = Mathf.Sqrt(vec.x * vec.x + vec.y * vec.y);
//
float noise = Noise.Generate((float)x / WIDTH * 10, (float)y / HEIGHT * 10);

// amitp's solution again
mapData[x, y] = noise > 0.3 + 0.4*d*d ? noise * 10 : 0.0f;
//

colors[x + y * WIDTH] = new Color(mapData[x, y], mapData[x, y], mapData[x, y]);
}

}

This gives me the following unwanted result:


Wrong


How do i get fix it to generate properly?


Lastly i'm using Unity.


***EDIT:


Fixed/Changed a couple of things and now i am getting this(still not what i want):


changes/fixes


with this changed code:



for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
// amitp's solution
Vector2 vec = new Vector2(((2 * x) / WIDTH) - 1, ((2 * y) / HEIGHT) - 1);
float d = Mathf.Sqrt((vec.x * vec.x) + (vec.y * vec.y));
//
float noise = Noise.Generate((float)x / WIDTH, (float)y / HEIGHT);

// amitp's solution again
mapData[x, y] = noise > 0.3 + 0.4*d*d ? noise * 10 : 0.0f;

//

colors[x + y * WIDTH] = new Color(mapData[x, y], mapData[x, y], mapData[x, y]);
}
}

Answer



I suggest this aproach. Let fs(x,y) be your simplex noise function. Let's introduce a second function : f(x,y) = ((float)Math.Sin(((float)x/(float)WIDTH) * Math.PI) ) * ((float)Math.Sin(((float)y / (float)WIDTH) * Math.PI) ) or any function that rassemble the following and that gives values from 0 to 1:


enter image description here


at this point take your simplex noise fs(x,y) :


enter image description here



and multiply per f(x,y) you get something like this :


enter image description here


finaly apply a colorgradient to obtain something like :


enter image description here


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...