- Home /
Difficulty with Perlin Noise based Terrain Generation
First off I know that this sort of question has been asked 101 times already on here, but I've honestly read a good 80% of them, and none offer me anymore information than I currently have.
I'm prototyping out a game which requires a really simple low-res terrain, and I eventually stumbled onto a few Perlin Noice tutorials. This is the function I have currently:
public void GenerateTerrain(float tileSize){
TerrainData TD = selfTerrain.terrainData;
//Higher = More Hills/Mountains
float HM = Random.Range(0,10);
//Lower = Higher Mountains/Hills
float divRange = Random.Range(12,25);
int height = TD.heightmapHeight;
int width = TD.heightmapWidth;
float[,] heights = new float[width,height];
for(int i = 0; i < width; i++){
for(int k = 0; k < height; k++){
heights[i,k] = Mathf.PerlinNoise((i/width) * tileSize, (k/height) * tileSize) / divRange;
}
}
Debug.Log("DivRange: " + divRange + " , " + "HTiling: " + HM);
TD.SetHeights(0,0,heights);
}
but instead of making me some nice ripply terrain it applies the same height to the whole terrain object, essentially just moving it up and down. The happens event when I use Random.Range to just put a random height onto every location in the heightmap.
If its something stupid, please don't rub it in, I've been up for probably a bit too long.
Cheers in Advance
Answer by incorrect · Jan 20, 2016 at 02:38 AM
Actually the problem is that maths for integers and floats work differently. Dividing int by int will not get you a float you need. Keep it in mind next time.
heights[i,k] = Mathf.PerlinNoise((1f*i/width) * tileSize, (1f*k/height) * tileSize) / divRange;
This will work like expected. Multiplying and int by 1f (float 1) will make it a float and you will get your expected result. Enjoy your noises! :)
And here is a bit of info about division. General program$$anonymous$$g is boring, but necessary.
Answer by cjdev · Jan 20, 2016 at 01:26 AM
I'm assuming tileSize is an int here, but the problem is likely that Perlin Noise doesn't work when it's input is a perfect whole number, an integer. Try multiplying your x and y by some random float value.
It works as you expect with the float because of floating point imprecision, which is why it's no longer a perfect whole number. The formula for Perlin Noise is using a modulo operation of 1 with the float value coordinates where the remainder is also used to provide the "seed" for a semi-random lookup table.
Your answer
Follow this Question
Related Questions
perlin noise terrain is spikey 0 Answers
Multiple Cars not working 1 Answer
Perlin noise gives the same result every time 1 Answer
Distribute terrain in zones 3 Answers