Search Unity

Question Range of noise functions (e.g. noise.snoise)

Discussion in 'C# Job System' started by Leyren, Jun 3, 2023.

  1. Leyren

    Leyren

    Joined:
    Mar 23, 2016
    Posts:
    29
    I would've assumed the snoise (https://docs.unity3d.com/Packages/com.unity.mathematics@1.2/api/Unity.Mathematics.noise.snoise.html) to be in the range [-1, 1], but that doesn't seem the case.

    Tested using this Job:
    Code (CSharp):
    1. [BurstCompile, BurstCompatible]
    2. public struct NoiseTestJob : IJob
    3. {
    4.  
    5.     public NativeReference<float> min;
    6.  
    7.     public NativeReference<float> max;
    8.  
    9.     public uint seed;
    10.  
    11.     public void Execute()
    12.     {
    13.         Random random = new Random(seed);
    14.         float tmin = 1;
    15.         float tmax = 0;
    16.         for (int x = 0; x < 1000; x++)
    17.         {
    18.             for (int y = 0; y < 1000; y++)
    19.             {
    20.                 float value = noise.snoise(random.NextFloat2());
    21.                 tmin = math.min(value, tmin);
    22.                 tmax = math.max(value, tmax);
    23.             }
    24.         }
    25.         min.Value = math.min(tmin, min.Value);
    26.         max.Value = math.max(tmax, max.Value);
    27.     }
    28. }
    In 1000 runs through it with different seeds (so a total of 1 billion values), the range was:
    Min: -0.7880797 Max: 0.9636794

    Can anyone clarify what the actual range is, and how to properly account for it / scale it to [-1, 1]?
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    625
    It seems like you either didn't read the documentation page you linked or don't understand what's at play. It's simplex noise. The spacial properties of the input coordinates will determine what kind of output you get. Using such a limited range as you are doing can restrict the noise output to a box that happens to not have very good local minima / maxima. Just multiply your coordinates by 100 and you'll see better bounds.
     
  3. Leyren

    Leyren

    Joined:
    Mar 23, 2016
    Posts:
    29
    Thanks for your answer. It's not like the documentation page has much information to share, except for that it's Simplex noise.
    You are correct though, somehow I completely missed that. Thanks.