LYGIA Shader Library

gnoise (lygia/generative/gnoise)

Gradient Noise

Dependencies:

Use:

gnoise(<float> x)

Check it on Github



#ifndef FNC_GNOISE
#define FNC_GNOISE

float gnoise(float x) {
    float i = floor(x);  // integer
    float f = fract(x);  // fraction
    return mix(random(i), random(i + 1.0), smoothstep(0.,1.,f)); 
}

float gnoise(vec2 st) {
    vec2 i = floor(st);
    vec2 f = fract(st);
    float a = random(i);
    float b = random(i + vec2(1.0, 0.0));
    float c = random(i + vec2(0.0, 1.0));
    float d = random(i + vec2(1.0, 1.0));
    vec2 u = cubic(f);
    return mix( a, b, u.x) +
                (c - a)* u.y * (1.0 - u.x) +
                (d - b) * u.x * u.y;
}

float gnoise(vec3 p) {
    vec3 i = floor(p);
    vec3 f = fract(p);
    vec3 u = quintic(f);
    return -1.0 + 2.0 * mix( mix( mix( random(i + vec3(0.0,0.0,0.0)), 
                                        random(i + vec3(1.0,0.0,0.0)), u.x),
                                mix( random(i + vec3(0.0,1.0,0.0)), 
                                        random(i + vec3(1.0,1.0,0.0)), u.x), u.y),
                            mix( mix( random(i + vec3(0.0,0.0,1.0)), 
                                        random(i + vec3(1.0,0.0,1.0)), u.x),
                                mix( random(i + vec3(0.0,1.0,1.0)), 
                                        random(i + vec3(1.0,1.0,1.0)), u.x), u.y), u.z );
}

float gnoise(vec3 p, float tileLength) {
    vec3 i = floor(p);
    vec3 f = fract(p);

    vec3 u = quintic(f);

    return mix( mix( mix( dot( srandom3(i + vec3(0.0,0.0,0.0), tileLength), f - vec3(0.0,0.0,0.0)), 
                            dot( srandom3(i + vec3(1.0,0.0,0.0), tileLength), f - vec3(1.0,0.0,0.0)), u.x),
                    mix( dot( srandom3(i + vec3(0.0,1.0,0.0), tileLength), f - vec3(0.0,1.0,0.0)), 
                            dot( srandom3(i + vec3(1.0,1.0,0.0), tileLength), f - vec3(1.0,1.0,0.0)), u.x), u.y),
                mix( mix( dot( srandom3(i + vec3(0.0,0.0,1.0), tileLength), f - vec3(0.0,0.0,1.0)), 
                            dot( srandom3(i + vec3(1.0,0.0,1.0), tileLength), f - vec3(1.0,0.0,1.0)), u.x),
                    mix( dot( srandom3(i + vec3(0.0,1.0,1.0), tileLength), f - vec3(0.0,1.0,1.0)), 
                            dot( srandom3(i + vec3(1.0,1.0,1.0), tileLength), f - vec3(1.0,1.0,1.0)), u.x), u.y), u.z );
}

vec3 gnoise3(vec3 x) {
    return vec3(gnoise(x+vec3(123.456, 0.567, 0.37)),
                gnoise(x+vec3(0.11, 47.43, 19.17)),
                gnoise(x) );
}

#endif

Dependencies:

Use:

gnoise(<float> x)

Check it on Github



#ifndef FNC_GNOISE
#define FNC_GNOISE

float gnoise(float x) {
    float i = floor(x);  // integer
    float f = fract(x);  // fraction
    return mix(random(i), random(i + 1.0), smoothstep(0.,1.,f)); 
}

float gnoise(float2 st) {
    float2 i = floor(st);
    float2 f = fract(st);
    float a = random(i);
    float b = random(i + float2(1.0, 0.0));
    float c = random(i + float2(0.0, 1.0));
    float d = random(i + float2(1.0, 1.0));
    float2 u = cubic(f);
    return mix( a, b, u.x) +
                (c - a)* u.y * (1.0 - u.x) +
                (d - b) * u.x * u.y;
}

float gnoise(float3 p) {
    float3 i = floor(p);
    float3 f = fract(p);
    float3 u = quintic(f);
    return -1.0 + 2.0 * mix( mix( mix( random(i + float3(0.0,0.0,0.0)), 
                                        random(i + float3(1.0,0.0,0.0)), u.x),
                                mix( random(i + float3(0.0,1.0,0.0)), 
                                        random(i + float3(1.0,1.0,0.0)), u.x), u.y),
                            mix( mix( random(i + float3(0.0,0.0,1.0)), 
                                        random(i + float3(1.0,0.0,1.0)), u.x),
                                mix( random(i + float3(0.0,1.0,1.0)), 
                                        random(i + float3(1.0,1.0,1.0)), u.x), u.y), u.z );
}

float gnoise(float3 p, float tileLength) {
    float3 i = floor(p);
    float3 f = fract(p);

    float3 u = quintic(f);

    return mix( mix( mix( dot( srandom3(i + float3(0.0,0.0,0.0), tileLength), f - float3(0.0,0.0,0.0)), 
                            dot( srandom3(i + float3(1.0,0.0,0.0), tileLength), f - float3(1.0,0.0,0.0)), u.x),
                    mix( dot( srandom3(i + float3(0.0,1.0,0.0), tileLength), f - float3(0.0,1.0,0.0)), 
                            dot( srandom3(i + float3(1.0,1.0,0.0), tileLength), f - float3(1.0,1.0,0.0)), u.x), u.y),
                mix( mix( dot( srandom3(i + float3(0.0,0.0,1.0), tileLength), f - float3(0.0,0.0,1.0)), 
                            dot( srandom3(i + float3(1.0,0.0,1.0), tileLength), f - float3(1.0,0.0,1.0)), u.x),
                    mix( dot( srandom3(i + float3(0.0,1.0,1.0), tileLength), f - float3(0.0,1.0,1.0)), 
                            dot( srandom3(i + float3(1.0,1.0,1.0), tileLength), f - float3(1.0,1.0,1.0)), u.x), u.y), u.z );
}

float3 gnoise3(float3 x) {
    return float3(gnoise(x+float3(123.456, 0.567, 0.37)),
                gnoise(x+float3(0.11, 47.43, 19.17)),
                gnoise(x) );
}

#endif

Licenses

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