Intro
Similar to the analytical derivatives of Value Noise, Gradient Noise (the name used for variations and generalizations of Perlin Noise) accept analytic computation of derivatives. Just like with Value Noise derivatives, this allows for much faster lighting computations or any other computation that requires gradients/normals based on the noise since we no longer need to approximate it though numerical methods that involve taking with multiple samples of the noise.
The code
Assuming we have some standard Gradient Noise implementation like the code on the left, the computation of the derivatives involves only a few more computations, as shown in the right.
1 | // returns 3D value noise |
1 | // returns 3D value noise (in .x) and its derivatives (in .yzw) |
You can find a reference implementation here: Shadertoy
In the case of 2D, the code gets naturally smaller:
1 | // returns 3D value noise (in .x) and its derivatives (in .yz) |
An implementation can be found here: Shadertoy