Perlin noise value range

1

I used perlin noise to generate a 2D height map. At first i tried some parameters manually and found a good combination of amplitude, persistence,... for my job.

Now that i'm developing the program, i added the feature for user to change the map parameters and make a new map for himself but now i see that for certain parameters (Mostly octaves and frequency) the values are not in the range i used to see. I thought that if a set Amplitude = 20, the values(heights) i get from it will be in e.g [0,20] or [-10,10] or [-20,20] ranges but now i see that Amplitude is not the only parameter that controls output range.

My question is: Is there an exact mathematical formula (a function of Amplitude, Octaves, Frequency and persistence) to compute the range or i should take a lot of samples (like 100,000) and check minimum and maximum values of them to guess the aproximate range?

Note: The following code is an implementation of perlin noise that one of stackoverflow guys worte it in C and i ported it to java.

PerlinNoiseParameters.java

public class PerlinNoiseParameters {

    public double persistence;
    public double frequency;
    public double amplitude;
    public int octaves;
    public int randomseed;

    public PerlinNoiseParameters(double persistence, double frequency, double amplitude, int octaves, int randomseed) {
        this.ChangeParameters(persistence, frequency, amplitude, octaves, randomseed);
    }

    public void ChangeParameters(double persistence, double frequency, double amplitude, int octaves, int randomseed) {
        this.persistence = persistence;
        this.frequency = frequency;
        this.amplitude = amplitude;
        this.octaves = octaves;
        this.randomseed = 2 + randomseed * randomseed;
    }
}

PerlinNoiseGenerator.java

public class PerlinNoiseGenerator {

    PerlinNoiseParameters parameters;

    public PerlinNoiseGenerator() {
    }

    public PerlinNoiseGenerator(PerlinNoiseParameters parameters) {
        this.parameters = parameters;
    }

    public void ChangeParameters(double persistence, double frequency, double amplitude, int octaves, int randomseed) {
        parameters.ChangeParameters(persistence, frequency, amplitude, octaves, randomseed);
    }

    public void ChangeParameters(PerlinNoiseParameters newParams) {
        parameters = newParams;
    }

    public double get(double x, double y) {
        return parameters.amplitude * Total(x, y);
    }

    private double Total(double i, double j) {
        double t = 0.0f;
        double _amplitude = 1;
        double freq = parameters.frequency;

        for (int k = 0; k < parameters.octaves; k++) {
            t += GetValue(j * freq + parameters.randomseed, i * freq + parameters.randomseed)
                    * _amplitude;
            _amplitude *= parameters.persistence;
            freq *= 2;
        }

        return t;
    }

    private double GetValue(double x, double y) {
        int Xint = (int) x;
        int Yint = (int) y;

        double Xfrac = x - Xint;
        double Yfrac = y - Yint;

        double n01 = Noise(Xint - 1, Yint - 1);
        double n02 = Noise(Xint + 1, Yint - 1);
        double n03 = Noise(Xint - 1, Yint + 1);
        double n04 = Noise(Xint + 1, Yint + 1);
        double n05 = Noise(Xint - 1, Yint);
        double n06 = Noise(Xint + 1, Yint);
        double n07 = Noise(Xint, Yint - 1);
        double n08 = Noise(Xint, Yint + 1);
        double n09 = Noise(Xint, Yint);
        double n12 = Noise(Xint + 2, Yint - 1);
        double n14 = Noise(Xint + 2, Yint + 1);
        double n16 = Noise(Xint + 2, Yint);
        double n23 = Noise(Xint - 1, Yint + 2);
        double n24 = Noise(Xint + 1, Yint + 2);
        double n28 = Noise(Xint, Yint + 2);
        double n34 = Noise(Xint + 2, Yint + 2);

        double x0y0 = 0.0625 * (n01 + n02 + n03 + n04) + 0.1250
                * (n05 + n06 + n07 + n08) + 0.2500 * n09;

        double x1y0 = 0.0625 * (n07 + n12 + n08 + n14) + 0.1250
                * (n09 + n16 + n02 + n04) + 0.2500 * n06;

        double x0y1 = 0.0625 * (n05 + n06 + n23 + n24) + 0.1250
                * (n03 + n04 + n09 + n28) + 0.2500 * n08;

        double x1y1 = 0.0625 * (n09 + n16 + n28 + n34) + 0.1250
                * (n08 + n14 + n06 + n24) + 0.2500 * n04;

        double v1 = Interpolate(x0y0, x1y0, Xfrac);
        double v2 = Interpolate(x0y1, x1y1, Xfrac);

        double fin = Interpolate(v1, v2, Yfrac);

        return fin;
    }

    private double Interpolate(double x, double y, double a) {
        double negA = 1.0 - a;
        double negASqr = negA * negA;
        double fac1 = 3.0 * (negASqr) - 2.0 * (negASqr * negA);
        double aSqr = a * a;
        double fac2 = 3.0 * aSqr - 2.0 * (aSqr * a);

        return x * fac1 + y * fac2;
    }

    private double Noise(int x, int y) {
        int n = x + y * 57;
        n = (n << 13) ^ n;
        int t = (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff;
        return 1.0 - (double) t * 0.931322574615478515625e-9;
    }
}
java
perlin-noise

2 Answers

1

This is not a problem with octaves and frequency affecting amplitude, not directly at least. It is a problem with integer overflow. Because you introduce your random seed by adding it to the the x and y co-ordinates (which is unusual, I don't think this is the usual implimentation)

t += GetValue(j * freq + parameters.randomseed, i * freq + parameters.randomseed)* _amplitude;

And random seed could be huge (possibly the near full size of the int) because

this.randomseed = 2 + randomseed * randomseed;

So if you input large values for j and i you end up with the doubles that are passed through at GetValue(double x, double y) being larger than the maximum size of int, at that point when you call

int Xint = (int) x;
int Yint = (int) y;

Xint and YInt won't be anything like x and y (because x and y could be huge!) and so

double Xfrac = x - Xint;
double Yfrac = y - Yint;

could be much much larger that 1, allowing values not between -1 and 1 to be returned.

Using reasonable and small values my ranges using your code are between -1 and 1 (for amplitude 1)


As an asside, in java usually method names are methodName, not MethodName

If its useful please find annother java implimentation of perlin noise here: http://mrl.nyu.edu/~perlin/noise/

answered on Stack Overflow Jul 2, 2013 by Richard Tingle
0

The range of a single perlin noise step is: http://digitalfreepen.com/2017/06/20/range-perlin-noise.html

-sqrt(N/4), sqrt(N/4)

With N being the amount of dimensions. 2 in your case.

Octaves, persistence and amplitude add on top of that:

double range = 0.0;
double _amplitude = parameters.;
for (int k = 0; k < parameters.octaves; k++) {
    range += sqrt(N/4) * _amplitude;
    _amplitude *= parameters.persistence;
}
return range;

There might be some way to do this as a single mathematical expression. Involving pow(), but by brain fails me right now.

answered on Stack Overflow Aug 15, 2019 by Daid Braam

User contributions licensed under CC BY-SA 3.0