Perlin noise gradient function
perlin-noise
Solution
... 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);
Problem
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. - `0`: `x + y` - `1`: `-x + y` - `2`: `x - y` - `3`: `-x - y` - `4`: `x + z` - `5`: `-x + z` - `6`: `x - z` - `7`: `-x - z` - `8`: `y + z` - `9`: `-y + z` - `10`: `y - z` - `11`: `-y - z` - `12`: `y + x` - `13`: `-y + z` - `14`: `y - x` - `15`: `-y - z` 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?