Question

I am currently trying to implement perlin noise on HLSL in 2D. I watched Ken's improved Perlin Noise, but i don't understand how the conversion from the permuation-array into Vectors works. I know that i can get the hash-code like this

int g00 = p[floorX + p[floorY]],
    g10 = p[floorX + 1 + p[floorY]],
    g01 = p[floorX + p[floorY + 1]],
    g11 = p[floorX + 1 + p[floorY + 1]];

(floorX and floorY are the floored x,y coordinates broken down to 8 bits.)

from here, but i still don't understand it. I also don't understand how the "grad(...)"- method from Ken's implementation works. Can anyone explain me how that works?

Was it helpful?

Solution

Ken Perlin's actual java implementation is really pseudo-code for his GPU implementation, which means some parts (especially the one you are stuck on) are downright impenetrable (using some optimizing theory).

For readability, try Stefan Gustavson's reference java implementation from Simplex Noise Demystified' (2005) in pdf, it is much more legible as it was designed partly as a teaching tool.

The long set of bit-operations and conditionals (using the trinary short hand operator) in his grad() function are there to choose a pseud-random Unit Vector. In particular, it should choose from a set that are equally spread around the unit circle (in 2D), or sphere (approximated by midpoints of the sides of a cube, in 3D), etc., and do so with an equal probability of each one. If the lowest 4 bits are sufficiently scrambled by the hashing routine, those 16 choices can be mapped to the 12 best 3D vectors (of length sqrt2) quickly - that is what it's doing (and, imho, why Perlin noise is rarely ever implemented up to the standard he set forth in the accompanying paper).

In particular, he sums two axis unit vectors (x, y, and z)-pseudo-randomly chosen, with pseudo-randomly determined signs each.

Since you want 2D noise, may I suggest using a lookup table for the gradients themselves, but please make them all the same length (it looks a little better than the shortcut Perlin left in the appendix that's been copied EveryWhere, and the vectors are usually floats already). And you can actually choose 8 good basis gradients (so the bitwise scramble works without deformation!).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top