I am trying to improve this code without having to make the terrain “borring”, so I would need some way to optimize this code to make it run faster, because right now it takes 0.9 seconds to generate a heightmap for 3 tiles. Which is way to much when the terrain is infinite and generated on the run.
Here is how I run the code:
if (perlin == null) {
perlin = new PerlinNoise(Seed);
}
mountains = Mathf.Max(0f, perlin.FractalNoise2D(worldPosX+250, worldPosZ+250, Octaves, mountainFrq, 0.5f) - 0.1f) * 1.5;
plain = perlin.FractalNoise2D(worldPosX-250, worldPosZ-250, 1, groundFrq, 0.4f) + 0.3f;
Here is the function:
public float FractalNoise2D(float x, float y, int octNum, float frq, float amp)
{
float gain = 1.0f;
float sum = 0.0f;
for(int i = 0; i < octNum; i++)
{
sum += Noise2D(x*gain/frq, y*gain/frq) * amp/gain;
gain *= 2.0f;
}
return sum;
}
Full code noise code: New Perlin Noise - Pastebin.com