- Home /
Why is perlin noise generating flat terrain?
I am trying to generate terrain using perlin noise however the terrain being generated is a plateau. The script I'm using is as follows:
var xlength = 65.0;
var ylength = 65.0;
var scale = 4.1f;
var heights = new float[xlength, ylength];
function Start () {
for (var i = 0; i < xlength; i++)
for (var j = 0; j < ylength;j++)
heights[i,j] = Mathf.PerlinNoise(Time.time * i / xlength * scale/1000,Time.time * j / ylength * scale/1000)*i*j;
gameObject.GetComponent(Terrain).terrainData.SetHeights(0,0,heights);
}
Any help that could be given would be appreciated! :)
Answer by tanoshimi · Oct 16, 2016 at 10:15 AM
As documented, the inputs for Mathf.PerlinNoise should be between 0.0 and 1.0. Although you can supply higher values seemingly without trouble, only the fractional part is considered.
You haven't typed any of your variables but, from the look of it, xlength and ylength are doubles, scale is a float, i and j are integers. I think you are then always supplying a whole integer value as the parameters to PerlinNoise (they're certainly not in the range 0.0 - 1.0), which is why you're always getting the same output.
This is one of the many reasons Javascript is sometimes frowned upon - its lack of strict typecasting makes it far too easy to write code that doesn't do what you expect. I recommend you always explicitly declare a type for your variables, e.g.: var ylength : float = 65.0;
(if you intended ylength to be a float) or var ylength : int = 65;
(if you didn't), then you wouldn't accidentally end up doing integer math when you didn't expect it. And to solve your immediate problem, ensure you do your calculation only with floats.
Where in the documentation does it state that the input should be between 0-1. The first example they give is between 0-10 (Perlin noise sampled in the range 0..10).