LYGIA Shader Library

noised (lygia/generative/noised)

Returns 2D/3D value noise in the first channel and in the rest the derivatives. For more details read this nice article http://www.iquilezles.org/www/articles/gradientnoise/gradientnoise.htm

Dependencies:

Use:

noised(<vec2|vec3> space)

Check it on Github



#ifndef NOISED_RANDOM2_FNC
#define NOISED_RANDOM2_FNC srandom2
#endif

#ifndef NOISED_RANDOM3_FNC
#define NOISED_RANDOM3_FNC srandom3
#endif

#ifndef FNC_NOISED
#define FNC_NOISED

// return gradient noise (in x) and its derivatives (in yz)
vec3 noised (in vec2 p) {
    // grid
    vec2 i = floor( p );
    vec2 f = fract( p );

    // quintic interpolation
    vec2 u = f * f * f * (f * (f * 6. - 15.) + 10.);
    vec2 du = 30. * f * f * (f * (f - 2.) + 1.);

    vec2 ga = NOISED_RANDOM2_FNC(i + vec2(0., 0.));
    vec2 gb = NOISED_RANDOM2_FNC(i + vec2(1., 0.));
    vec2 gc = NOISED_RANDOM2_FNC(i + vec2(0., 1.));
    vec2 gd = NOISED_RANDOM2_FNC(i + vec2(1., 1.));

    float va = dot(ga, f - vec2(0., 0.));
    float vb = dot(gb, f - vec2(1., 0.));
    float vc = dot(gc, f - vec2(0., 1.));
    float vd = dot(gd, f - vec2(1., 1.));

    return vec3( va + u.x*(vb-va) + u.y*(vc-va) + u.x*u.y*(va-vb-vc+vd),   // value
                ga + u.x*(gb-ga) + u.y*(gc-ga) + u.x*u.y*(ga-gb-gc+gd) +  // derivatives
                du * (u.yx*(va-vb-vc+vd) + vec2(vb,vc) - va));
}

vec4 noised (in vec3 pos) {
    // grid
    vec3 p = floor(pos);
    vec3 w = fract(pos);

    // quintic interpolant
    vec3 u = w * w * w * ( w * (w * 6. - 15.) + 10. );
    vec3 du = 30.0 * w * w * ( w * (w - 2.) + 1.);

    // gradients
    vec3 ga = NOISED_RANDOM3_FNC(p + vec3(0., 0., 0.));
    vec3 gb = NOISED_RANDOM3_FNC(p + vec3(1., 0., 0.));
    vec3 gc = NOISED_RANDOM3_FNC(p + vec3(0., 1., 0.));
    vec3 gd = NOISED_RANDOM3_FNC(p + vec3(1., 1., 0.));
    vec3 ge = NOISED_RANDOM3_FNC(p + vec3(0., 0., 1.));
    vec3 gf = NOISED_RANDOM3_FNC(p + vec3(1., 0., 1.));
    vec3 gg = NOISED_RANDOM3_FNC(p + vec3(0., 1., 1.));
    vec3 gh = NOISED_RANDOM3_FNC(p + vec3(1., 1., 1.));

    // projections
    float va = dot(ga, w - vec3(0., 0., 0.));
    float vb = dot(gb, w - vec3(1., 0., 0.));
    float vc = dot(gc, w - vec3(0., 1., 0.));
    float vd = dot(gd, w - vec3(1., 1., 0.));
    float ve = dot(ge, w - vec3(0., 0., 1.));
    float vf = dot(gf, w - vec3(1., 0., 1.));
    float vg = dot(gg, w - vec3(0., 1., 1.));
    float vh = dot(gh, w - vec3(1., 1., 1.));

    // interpolations
    return vec4( va + u.x*(vb-va) + u.y*(vc-va) + u.z*(ve-va) + u.x*u.y*(va-vb-vc+vd) + u.y*u.z*(va-vc-ve+vg) + u.z*u.x*(va-vb-ve+vf) + (-va+vb+vc-vd+ve-vf-vg+vh)*u.x*u.y*u.z,    // value
                ga + u.x*(gb-ga) + u.y*(gc-ga) + u.z*(ge-ga) + u.x*u.y*(ga-gb-gc+gd) + u.y*u.z*(ga-gc-ge+gg) + u.z*u.x*(ga-gb-ge+gf) + (-ga+gb+gc-gd+ge-gf-gg+gh)*u.x*u.y*u.z +   // derivatives
                du * (vec3(vb,vc,ve) - va + u.yzx*vec3(va-vb-vc+vd,va-vc-ve+vg,va-vb-ve+vf) + u.zxy*vec3(va-vb-ve+vf,va-vb-vc+vd,va-vc-ve+vg) + u.yzx*u.zxy*(-va+vb+vc-vd+ve-vf-vg+vh) ));
}

#endif

Dependencies:

Use:

noised(<float2|float3> space)

Check it on Github



#ifndef NOISED_RANDOM2_FNC
#define NOISED_RANDOM2_FNC srandom2
#endif

#ifndef NOISED_RANDOM3_FNC
#define NOISED_RANDOM3_FNC srandom3
#endif

#ifndef FNC_NOISED
#define FNC_NOISED

float3 noised(in float2 p) {
    // grid
    float2 i = floor( p );
    float2 f = frac( p );

    // quintic interpolation
    float2 u = f * f * f * (f * (f * 6. - 15.) + 10.);
    float2 du = 30. * f * f * (f * (f - 2.) + 1.);

    float2 ga = NOISED_RANDOM2_FNC(i + float2(0., 0.));
    float2 gb = NOISED_RANDOM2_FNC(i + float2(1., 0.));
    float2 gc = NOISED_RANDOM2_FNC(i + float2(0., 1.));
    float2 gd = NOISED_RANDOM2_FNC(i + float2(1., 1.));

    float va = dot(ga, f - float2(0., 0.));
    float vb = dot(gb, f - float2(1., 0.));
    float vc = dot(gc, f - float2(0., 1.));
    float vd = dot(gd, f - float2(1., 1.));

    return float3(va + u.x*(vb-va) + u.y*(vc-va) + u.x*u.y*(va-vb-vc+vd),   // value
                    ga + u.x*(gb-ga) + u.y*(gc-ga) + u.x*u.y*(ga-gb-gc+gd) +  // derivatives
                    du * (u.yx*(va-vb-vc+vd) + float2(vb,vc) - va));
}

float4 noised(in float3 pos) {
    // grid
    float3 p = floor(pos);
    float3 w = frac(pos);

    // quintic interpolant
    float3 u = w * w * w * ( w * (w * 6. - 15.) + 10. );
    float3 du = 30.0 * w * w * ( w * (w - 2.) + 1.);

    // gradients
    float3 ga = NOISED_RANDOM3_FNC(p + float3(0., 0., 0.));
    float3 gb = NOISED_RANDOM3_FNC(p + float3(1., 0., 0.));
    float3 gc = NOISED_RANDOM3_FNC(p + float3(0., 1., 0.));
    float3 gd = NOISED_RANDOM3_FNC(p + float3(1., 1., 0.));
    float3 ge = NOISED_RANDOM3_FNC(p + float3(0., 0., 1.));
    float3 gf = NOISED_RANDOM3_FNC(p + float3(1., 0., 1.));
    float3 gg = NOISED_RANDOM3_FNC(p + float3(0., 1., 1.));
    float3 gh = NOISED_RANDOM3_FNC(p + float3(1., 1., 1.));

    // projections
    float va = dot(ga, w - float3(0., 0., 0.));
    float vb = dot(gb, w - float3(1., 0., 0.));
    float vc = dot(gc, w - float3(0., 1., 0.));
    float vd = dot(gd, w - float3(1., 1., 0.));
    float ve = dot(ge, w - float3(0., 0., 1.));
    float vf = dot(gf, w - float3(1., 0., 1.));
    float vg = dot(gg, w - float3(0., 1., 1.));
    float vh = dot(gh, w - float3(1., 1., 1.));

    // interpolations
    return float4( va + u.x*(vb-va) + u.y*(vc-va) + u.z*(ve-va) + u.x*u.y*(va-vb-vc+vd) + u.y*u.z*(va-vc-ve+vg) + u.z*u.x*(va-vb-ve+vf) + (-va+vb+vc-vd+ve-vf-vg+vh)*u.x*u.y*u.z,    // value
                ga + u.x*(gb-ga) + u.y*(gc-ga) + u.z*(ge-ga) + u.x*u.y*(ga-gb-gc+gd) + u.y*u.z*(ga-gc-ge+gg) + u.z*u.x*(ga-gb-ge+gf) + (-ga+gb+gc-gd+ge-gf-gg+gh)*u.x*u.y*u.z +   // derivatives
                du * (float3(vb,vc,ve) - va + u.yzx*float3(va-vb-vc+vd,va-vc-ve+vg,va-vb-ve+vf) + u.zxy*float3(va-vb-ve+vf,va-vb-vc+vd,va-vc-ve+vg) + u.yzx*u.zxy*(-va+vb+vc-vd+ve-vf-vg+vh) ));
}

#endif

Dependencies:

Use:

noised(<float2|float3> space)

Check it on Github



#ifndef NOISED_RANDOM2_FNC
#define NOISED_RANDOM2_FNC srandom2
#endif

#ifndef NOISED_RANDOM3_FNC
#define NOISED_RANDOM3_FNC srandom3
#endif

#ifndef FNC_NOISED
#define FNC_NOISED

// return gradient noise (x) and its derivatives (yz)
float3 noised (float2 p) {
    // grid
    float2 i = floor( p );
    float2 f = fract( p );

    // quintic interpolation
    float2 u = f * f * f * (f * (f * 6. - 15.) + 10.);
    float2 du = 30. * f * f * (f * (f - 2.) + 1.);

    float2 ga = NOISED_RANDOM2_FNC(i + float2(0., 0.));
    float2 gb = NOISED_RANDOM2_FNC(i + float2(1., 0.));
    float2 gc = NOISED_RANDOM2_FNC(i + float2(0., 1.));
    float2 gd = NOISED_RANDOM2_FNC(i + float2(1., 1.));

    float va = dot(ga, f - float2(0., 0.));
    float vb = dot(gb, f - float2(1., 0.));
    float vc = dot(gc, f - float2(0., 1.));
    float vd = dot(gd, f - float2(1., 1.));

    return float3( va + u.x*(vb-va) + u.y*(vc-va) + u.x*u.y*(va-vb-vc+vd),   // value
                ga + u.x*(gb-ga) + u.y*(gc-ga) + u.x*u.y*(ga-gb-gc+gd) +  // derivatives
                du * (u.yx*(va-vb-vc+vd) + float2(vb,vc) - va));
}

float4 noised (float3 pos) {
    // grid
    float3 p = floor(pos);
    float3 w = fract(pos);

    // quintic interpolant
    float3 u = w * w * w * ( w * (w * 6. - 15.) + 10. );
    float3 du = 30.0 * w * w * ( w * (w - 2.) + 1.);

    // gradients
    float3 ga = NOISED_RANDOM3_FNC(p + float3(0., 0., 0.));
    float3 gb = NOISED_RANDOM3_FNC(p + float3(1., 0., 0.));
    float3 gc = NOISED_RANDOM3_FNC(p + float3(0., 1., 0.));
    float3 gd = NOISED_RANDOM3_FNC(p + float3(1., 1., 0.));
    float3 ge = NOISED_RANDOM3_FNC(p + float3(0., 0., 1.));
    float3 gf = NOISED_RANDOM3_FNC(p + float3(1., 0., 1.));
    float3 gg = NOISED_RANDOM3_FNC(p + float3(0., 1., 1.));
    float3 gh = NOISED_RANDOM3_FNC(p + float3(1., 1., 1.));

    // projections
    float va = dot(ga, w - float3(0., 0., 0.));
    float vb = dot(gb, w - float3(1., 0., 0.));
    float vc = dot(gc, w - float3(0., 1., 0.));
    float vd = dot(gd, w - float3(1., 1., 0.));
    float ve = dot(ge, w - float3(0., 0., 1.));
    float vf = dot(gf, w - float3(1., 0., 1.));
    float vg = dot(gg, w - float3(0., 1., 1.));
    float vh = dot(gh, w - float3(1., 1., 1.));

    // interpolations
    return float4( va + u.x*(vb-va) + u.y*(vc-va) + u.z*(ve-va) + u.x*u.y*(va-vb-vc+vd) + u.y*u.z*(va-vc-ve+vg) + u.z*u.x*(va-vb-ve+vf) + (-va+vb+vc-vd+ve-vf-vg+vh)*u.x*u.y*u.z,    // value
                ga + u.x*(gb-ga) + u.y*(gc-ga) + u.z*(ge-ga) + u.x*u.y*(ga-gb-gc+gd) + u.y*u.z*(ga-gc-ge+gg) + u.z*u.x*(ga-gb-ge+gf) + (-ga+gb+gc-gd+ge-gf-gg+gh)*u.x*u.y*u.z +   // derivatives
                du * (float3(vb,vc,ve) - va + u.yzx*float3(va-vb-vc+vd,va-vc-ve+vg,va-vb-ve+vf) + u.zxy*float3(va-vb-ve+vf,va-vb-vc+vd,va-vc-ve+vg) + u.yzx*u.zxy*(-va+vb+vc-vd+ve-vf-vg+vh) ));
}

#endif

Dependencies:

Use:

<vec3f> noised2(<vec2f>)
<vec4f> noised3(<vec3f>)

Check it on Github



// return gradient noise (in x) and its derivatives (in yz)
fn noised2 (p: vec2f) -> vec3f {
    // grid
    let i = floor( p );
    let f = fract( p );

    // quintic interpolation
    let u = f * f * f * (f * (f * 6. - 15.) + 10.);
    let du = 30. * f * f * (f * (f - 2.) + 1.);

    let ga = srandom22(i + vec2(0., 0.));
    let gb = srandom22(i + vec2(1., 0.));
    let gc = srandom22(i + vec2(0., 1.));
    let gd = srandom22(i + vec2(1., 1.));

    let va = dot(ga, f - vec2(0., 0.));
    let vb = dot(gb, f - vec2(1., 0.));
    let vc = dot(gc, f - vec2(0., 1.));
    let vd = dot(gd, f - vec2(1., 1.));

    return vec3( va + u.x*(vb-va) + u.y*(vc-va) + u.x*u.y*(va-vb-vc+vd),   // value
                ga + u.x*(gb-ga) + u.y*(gc-ga) + u.x*u.y*(ga-gb-gc+gd) +  // derivatives
                du * (u.yx*(va-vb-vc+vd) + vec2(vb,vc) - va));
}

fn noised3 (pos: vec3f) -> vec4f {
    // grid
    let p = floor(pos);
    let w = fract(pos);

    // quintic interpolant
    let u = w * w * w * ( w * (w * 6. - 15.) + 10. );
    let du = 30.0 * w * w * ( w * (w - 2.) + 1.);

    // gradients
    let ga = srandom33(p + vec3(0., 0., 0.));
    let gb = srandom33(p + vec3(1., 0., 0.));
    let gc = srandom33(p + vec3(0., 1., 0.));
    let gd = srandom33(p + vec3(1., 1., 0.));
    let ge = srandom33(p + vec3(0., 0., 1.));
    let gf = srandom33(p + vec3(1., 0., 1.));
    let gg = srandom33(p + vec3(0., 1., 1.));
    let gh = srandom33(p + vec3(1., 1., 1.));

    // projections
    let va = dot(ga, w - vec3(0., 0., 0.));
    let vb = dot(gb, w - vec3(1., 0., 0.));
    let vc = dot(gc, w - vec3(0., 1., 0.));
    let vd = dot(gd, w - vec3(1., 1., 0.));
    let ve = dot(ge, w - vec3(0., 0., 1.));
    let vf = dot(gf, w - vec3(1., 0., 1.));
    let vg = dot(gg, w - vec3(0., 1., 1.));
    let vh = dot(gh, w - vec3(1., 1., 1.));

    // interpolations
    return vec4( va + u.x*(vb-va) + u.y*(vc-va) + u.z*(ve-va) + u.x*u.y*(va-vb-vc+vd) + u.y*u.z*(va-vc-ve+vg) + u.z*u.x*(va-vb-ve+vf) + (-va+vb+vc-vd+ve-vf-vg+vh)*u.x*u.y*u.z,    // value
                ga + u.x*(gb-ga) + u.y*(gc-ga) + u.z*(ge-ga) + u.x*u.y*(ga-gb-gc+gd) + u.y*u.z*(ga-gc-ge+gg) + u.z*u.x*(ga-gb-ge+gf) + (-ga+gb+gc-gd+ge-gf-gg+gh)*u.x*u.y*u.z +   // derivatives
                du * (vec3(vb,vc,ve) - va + u.yzx*vec3(va-vb-vc+vd,va-vc-ve+vg,va-vb-ve+vf) + u.zxy*vec3(va-vb-ve+vf,va-vb-vc+vd,va-vc-ve+vg) + u.yzx*u.zxy*(-va+vb+vc-vd+ve-vf-vg+vh) ));
}

Examples

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository