Razaekel / noise-rs

Procedural noise generation library for Rust.
Apache License 2.0
855 stars 120 forks source link

Analytical derivitives for SuperSimplex noise #167

Closed tayloraswift closed 3 years ago

tayloraswift commented 7 years ago

Does anyone know how to compute the analytic derivative of SuperSimplex noise? The algorithm is so poorly documented I couldn’t find any information anywhere.

mystise commented 7 years ago

Checkout the eval_D2C1 and eval_D3C1 functions from SuperSimplexNoise.java, those provide what appears to be dx, dy, dxdy, and all the other various derivatives.

Edit: I just realized that gives you the numerical derivatives, not the analytical ones. I have the analytical equations, but the derivatives aren't super helpful unfortunately.

    // Calculation of maximum value:
    // x => real_rel_coords[0], y => real_rel_coords[1]
    // a-h, components of gradient vectors for 4 closest points
    // One contribution: (a*x + b*y) * (2/3 - x^2 - y^2)^4
    // Limit per contribution: 0 <= x^2 + y^2 < 2/3
    // skew = ((1 / sqrt(2 + 1) - 1) / 2)
    // (a*x + b*y) * (2/3 - x^2 - y^2)^4 + (c*(x - 1 - 2 * skew) + d*(y - 1 - 2 * skew)) * (2/3 - (x - 1 - 2 * skew)^2 - (y - 1 - 2 * skew)^2)^4 + (e*(x - skew) + f*(y - 1 - skew)) * (2/3 - (x - skew)^2 - (y - 1 - skew)^2)^4 + (g*(x - 1 - skew) + h*(y - skew)) * (2/3 - (x - 1 - skew)^2 - (y - skew)^2)^4
    // 0 <= x^2 + y^2 < 2/3 && 0 <= (x - 1 - 2 * skew)^2 + (y - 1 - 2 * skew)^2 < 2/3 && 0 <= (x - skew)^2 + (y - 1 - skew)^2 < 2/3 && 0 <= (x - 1 - skew)^2 + (y - skew)^2 < 2/3
    // a^2 + b^2 == 1 && c^2 + d^2 == 1 && e^2 + f^2 == 1 && g^2 + h^2 == 1

If you plug that into Mathematica you can take derivatives Dx and Dy, which end up as screen shot 2017-05-29 at 18 28 30 screen shot 2017-05-29 at 18 28 37

tayloraswift commented 7 years ago

yikes

mystise commented 7 years ago

The derivatives for only one contribution (so you can just add them together after accounting for the different x and y positions, as in the eval_D2C1 and eval_D3C1 functions) are:

screen shot 2017-05-29 at 23 05 48

tayloraswift commented 7 years ago

are x and y relative to the gradient point

mystise commented 7 years ago

Yes