Search code examples
c#unity-game-engineperlin-noise

How to fix script only generating numbers between 0.1 and 0.95


When I try to use Perlin noise in my program, it only seems to generate values between 0.1 and 0.95. I checked the documentation and did research online to try and fix my issue, and I believe the issue might be due to the way I'm iterating x and y within the for loops, but I am not sure.

Here is a MRE:

float width = tiles[0].transform.localScale.x; // this value is 1
float height = tiles[0].transform.localScale.z; // this value is 1

for (float x = 0; x < map_width; x++) // map_width is 100
{
    for (float y = 0; y < map_height; y++) // map_height is 100
    {
        var perlin = Mathf.PerlinNoise(x / 10, y / 10);
        Debug.Log(perlin); // values between 0.1 and 0.95
    }
}

When I try this code, the values displayed by Debug.Log(perlin); never appear to be smaller than 0.1 or larger than 0.95. Why?


Solution

  • You are doing everything right. ALL noises are like this and it is not related to algorithm they are using, its just their nature, they are not normalized. For them to fit something, their seed must be perfect - but it defeats the purpose, cause you need randomness.

    More examples of noises: Simplex, Perlin, Worley (which also depends on distance function like Manhattan or Chebyshev), Value noise, etc - they all generate somewhat bounded values which not necessarily fit 0-1.

    For my world generation I usually do algebraic fitting to 0-1 or -1 to 1. Shift, multiply, then clamp values to 0:1, -1:1 and hardcode it somewhere in helper class.

    When I combine octaves from different or same noise (different frequency, amplitude) I usually do pre-calculation fitting (look for optimization algorithms), running 1000 iterations to eliminate sequential 1 or 0 so they don't generate flat surfaces and save those modifiers in cache, but this might be overkill for you, because I usually port algorithm to CUDA/GLSL/HLSL.