5
\$\begingroup\$

I am reading through a book called "The Nature of Coding" and I am working on porting the examples from Processing.js to Regular old Java. The example I am on uses the following method call in processing...

noise(x)

I noticed this class provided by Perlin himself, however, it is three dimensional. Can I just do something like this to make it one dimensional (seems too easy)...

static public double noise(double x) {
    return noise(x, 0, 0);
}

Or is there some other changes I have to make?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Yes, this is fine. \$\endgroup\$ Jan 11, 2013 at 17:42

2 Answers 2

7
\$\begingroup\$

No, you should avoid doing that. 3D Perlin noise is 7 or 8 times as expensive as 1D Perlin noise. You'd be better off reimplementing the 1D function, because it's very simple. If I'm not mistaken:

static public double noise(double x) {
   int X = (int)Math.floor(x) & 255;
   x -= Math.floor(x);
   double u = fade(x);
   return lerp(u, grad(p[X  ], x  ),
                  grad(p[X+1], x-1));
}

static double grad(int hash, double x) {
   return ((hash&1) == 0 ? x : -x);
}
\$\endgroup\$
2
  • \$\begingroup\$ So I do notice some values in the previous version that look like they merge values, When I move to 2D do I have to create those merged values as well I am refering basically to...int A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z, // HASH COORDINATES // OF B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z; // THE 8 CUBE // CORNERS, \$\endgroup\$
    – Jackie
    Jan 12, 2013 at 6:19
  • \$\begingroup\$ The purpose of these p[...p[...]] constructs is to shuffle X, Y and Z in order to generate a pseudo-random hash number. In 1D you only have X so p[X] and p[X+1] are enough to generate the hash. \$\endgroup\$ Jan 12, 2013 at 13:54
10
\$\begingroup\$

Noise along any of the axes will be consistent noise. Just imagine you're flying through a cloud of noise sampling a single line of data.

So yes, return noise(x, 0, 0); is exactly like traveling along a single line of noise inside a cloud of noise. You'll be traveling along the line that represents the x axis. You could even do return noise(x, x, x); and travel diagonally through the noise if you wanted to. The point being, as long as you're moving linearly through the noise you'll get consistent noise with consistent values of x.

\$\endgroup\$
7
  • \$\begingroup\$ Can you expand this a little bit? For example the noise method in Processing does return a constant based on the x value passed to it, but the value does in fact change as x changes. Also you never really said if my implementation is correct with the 0s or not. \$\endgroup\$
    – Jackie
    Jan 11, 2013 at 17:37
  • 2
    \$\begingroup\$ The word wasn't constant, it was consistent - what Byte56 is saying is that any 1-dimensional 'slice' of a proper 3-dimensional noise function will be equivalent to a 1-dimensional noise function. \$\endgroup\$ Jan 11, 2013 at 23:01
  • \$\begingroup\$ @StevenStadnicki Thanks, I was actually confused by that "constant" comment and your comment makes it clear now. \$\endgroup\$
    – House
    Jan 11, 2013 at 23:03
  • \$\begingroup\$ (Also, I should note that 'noise along any of the axes will be consistent' is a goal, but not always a reality - IIRC there are some bad implementations of Perlin noise out there that show some very distinct axial inhomogeneity.) \$\endgroup\$ Jan 11, 2013 at 23:26
  • \$\begingroup\$ Note that noise(x, x, x) will return noise with a frequency sqrt(3) larger than expected. And even when accounting for that, you will not get the same noise as noise(x, 0, 0) because of Perlin noise’s inherent anisotropy. \$\endgroup\$ Jan 12, 2013 at 2:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .