7. 2d noise algorithm

according to the original shader code, this was based on an algorithm by Morgan McGuire.

<<static_funcdefs>>=
static float noise(vec2 st);
<<funcs>>=
static float noise(vec2 st)
{
    vec2 i;
    vec2 f;

    float a, b, c, d;

    vec2 u;

    i.x = floor(st.x);
    i.y = floor(st.y);

    /* TODO: derive from i instead of fract */
    f.x = fract(st.x);
    f.y = fract(st.y);

    a = random(i);
    b = random(add(i, mkvec2(1.0, 0.0)));
    c = random(add(i, mkvec2(0.0, 1.0)));
    d = random(add(i, mkvec2(1.0, 1.0)));

    /* f * f * (3.0 - 2.0 * f) */
    /* f * f */
    u = mul(f, f);
    /* -2f + (3) */
    u = mul(u, adds(muls(f, -2), 3));
    return mix(a, b, u.x) +
            (c - a)* u.y * (1.0 - u.x) +
            (d - b) * u.x * u.y;
}



prev | home | next