0

Iv been trying to understand noise for a very long time and I think I finally found someplace that can explain it to me. I have looked at the complex algorithms for too long, so I decided to go off the bare, English explanation of how noise is formed.

http://flafla2.github.io/2014/08/09/perlinnoise.html

https://www.youtube.com/watch?v=MJ3bvCkHJtE

These are the two sources that I used.

My problem is that the noise seems to be just an interpolation between whatever two points I give(when both points are under one) and when the points are above one, the numbers quickly grow into the trillions.

My input are things like, (.95, 02) or (10.38,39.04)

Iv tried to implement a very basic version, so it would be easy to understand. Here is my code:

package perlinNoise;

public class Perlin {
    int grads[][] = {{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
    public double perlin (double x, double y) {
        int unitX = (int)x & 255; // decide unit square
        int unitY = (int)y & 255; // decide unit square

        double relX = x-Math.floor(x); // relative x position
        double relY = y-Math.floor(y); // relative y position

        // bad pseudorandom gradient
        int[] gradTL = grads[(unitX+unitY)%8];
        int[] gradTR = grads[((unitX+1)+unitY)%8];
        int[] gradBL = grads[(unitX+(unitY+1))%8];
        int[] gradBR = grads[((unitX+1)+(unitY+1))%8];

        // distance from edges to point, relative x and y inside the unit square
        double[] vecTL = {relX,relY};
        double[] vecTR = {1-relX,relY};
        double[] vecBL = {relX,1-relY};
        double[] vecBR = {1-relX,1-relY};

        double tl = dot(gradTL,vecTL);
        double tr = dot(gradTR,vecTR);
        double bl = dot(gradBL,vecBL);
        double br = dot(gradBR,vecBR);

        double u = fade(relX);
        double v = fade(relY);

        double x1 = lerp(tl,tr, u);
        double y1 = lerp(bl,br, u);

        return lerp(x1,y1, v);
    }

    public double dot(int[] grad, double[] dist) {
        return (grad[0]*dist[0] + grad[1]*dist[1]);
    }

    public double lerp(double start, double end, double rate){
         return start+rate*(end-start);
    }
    public double fade(double t) {
        return t*t*t*(t*(t*6-15)+10);
    }
}

If anyone with more experience could offer some advice, it would be greatly appreciated.

Sprockget
  • 1
  • 4
  • `noise seems to be just an interpolation between whatever two points` Yeah that's a start. Perlin noise is also smooth (it has no discontinuities or "inflection points") and also "coherent", which means the interpolating function produces noise that has a narrow range of dominant frequencies. (I think that's the right way of describing it). – markspace Mar 01 '18 at 19:04
  • There is a rather good (and technical) explanation of Perlin noise here: http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf – markspace Mar 01 '18 at 19:08
  • Thank you, but that is simplex noise and while I know Ken Perlin has said it is better, I am just trying to make classical perlin noise. – Sprockget Mar 01 '18 at 19:16
  • Why are the definitions of relX and relY identical? – dmuir Mar 01 '18 at 19:26
  • Wow, thats a huge mistake, I just changed it back. It has not fixed the problem though. – Sprockget Mar 01 '18 at 19:28

1 Answers1

0

The reason you get huge values for numbers > 1 is due to your fade function:

public double fade(double t) {
        return t*t*t*(t*(t*6-15)+10);
}

I'm not entirely sure what you intend it to do with values > 1, but as written it gives the following for values 0 to 10:

0.0
1.0
32.0
513.0
2944.0
10625.0
29376.0
68257.0
140288.0
263169.0
460000.0
Pikalek
  • 926
  • 7
  • 21
  • I fixed my code above, there is a different problem now. The solution to my previous problem was that I lerped by a number greater then 1. This threw off the purpose of the formula and made the numbers huge. The current problem is that the noise becomes an exact copy and has obvious tiling lines when I try to go to higher frequencies. – Sprockget Mar 13 '18 at 17:13
  • @Sprockget That sounds like a different problem & thus should be in a different question. – Pikalek Mar 13 '18 at 17:54