Matthew Piziak
Matthew Piziak

Reputation: 3520

Perlin noise gradient function

I'm looking to adapt the 3D Perlin noise algorithm to lower dimensions, but I'm having trouble with the gradient function, since I don't fully understand the reasoning.

The original Perlin gradient function takes four arguments: a hash and a three-dimensional coordinate (x, y, z). The result of the function is returned based on the value of hash mod 16, as listed below.

The return values from 0 to 11 make a kind of pattern, since every combination is represented once. The last four, however, are duplicates. Why were they chosen to fit the last four return values? And what would be the analagous cases with two (x, y) and one (x) dimensions?

Upvotes: 9

Views: 5208

Answers (1)

cube
cube

Reputation: 3948

... is late answer better than none? ;-)

The grad function in the "improved noise" implementation calculates a dot product between the vector x, y, z and a pseudo random gradient vector.

In this implementation, the gradient vector is selected from 12 options. They drop uniformity of the selection and add numbers 12 to 15, because it is faster to do hash & 15 than hash % 12

For a 2D perlin noise I have used only 4 gradient vectors without any visible problems like this:

return ((hash & 1) ? x : -x) + ((hash & 2) ? y : -y);

Upvotes: 13

Related Questions