LYGIA Shader Library

worley (lygia/generative/worley)

Worley noise. Returns vec2(F1, F2)

Dependencies:

Use:

<vec2> worley2(<vec2|vec3> pos)

Check it on Github



#ifndef FNC_WORLEY
#define FNC_WORLEY

#ifndef WORLEY_JITTER
#define WORLEY_JITTER 1.0
#endif

#ifndef WORLEY_DIST_FNC
#define WORLEY_DIST_FNC distEuclidean
#endif

vec2 worley2(vec2 p){
    vec2 n = floor( p );
    vec2 f = fract( p );

    float distF1 = 1.0;
    float distF2 = 1.0;
    vec2 off1 = vec2(0.0); 
    vec2 pos1 = vec2(0.0);
    vec2 off2 = vec2(0.0);
    vec2 pos2 = vec2(0.0);
    for( int j= -1; j <= 1; j++ ) {
        for( int i=-1; i <= 1; i++ ) {  
            vec2  g = vec2(i,j);
            vec2  o = random2( n + g ) * WORLEY_JITTER;
            vec2  p = g + o;
            float d = WORLEY_DIST_FNC(p, f);
            if (d < distF1) {
                distF2 = distF1;
                distF1 = d;
                off2 = off1;
                off1 = g;
                pos2 = pos1;
                pos1 = p;
            }
            else if (d < distF2) {
                distF2 = d;
                off2 = g;
                pos2 = p;
            }
        }
    }

    return vec2(distF1, distF2);
}

float worley(vec2 p){ return 1.0-worley2(p).x; }

vec2 worley2(vec3 p) {
    vec3 n = floor( p );
    vec3 f = fract( p );

    float distF1 = 1.0;
    float distF2 = 1.0;
    vec3 off1 = vec3(0.0);
    vec3 pos1 = vec3(0.0);
    vec3 off2 = vec3(0.0);
    vec3 pos2 = vec3(0.0);
    for( int k = -1; k <= 1; k++ ) {
        for( int j= -1; j <= 1; j++ ) {
            for( int i=-1; i <= 1; i++ ) {  
                vec3  g = vec3(i,j,k);
                vec3  o = random3( n + g ) * WORLEY_JITTER;
                vec3  p = g + o;
                float d = WORLEY_DIST_FNC(p, f);
                if (d < distF1) {
                    distF2 = distF1;
                    distF1 = d;
                    off2 = off1;
                    off1 = g;
                    pos2 = pos1;
                    pos1 = p;
                }
                else if (d < distF2) {
                    distF2 = d;
                    off2 = g;
                    pos2 = p;
                }
            }
        }
    }

    return vec2(distF1, distF2);
}

float worley(vec3 p){ return 1.0-worley2(p).x; }

#endif

Dependencies:

Use:

<float2> worley2(<float2|float3> pos)

Check it on Github



#ifndef FNC_WORLEY
#define FNC_WORLEY

#ifndef WORLEY_JITTER
#define WORLEY_JITTER 1.0
#endif

#ifndef WORLEY_DIST_FNC
#define WORLEY_DIST_FNC distEuclidean
#endif

float2 worley2(float2 p){
    float2 n = floor( p );
    float2 f = frac( p );

    float distF1 = 1.0;
    float distF2 = 1.0;
    float2 off1, pos1;
    float2 off2, pos2;
    for( int j= -1; j <= 1; j++ ) {
        for( int i=-1; i <= 1; i++ ) {  
            float2  g = float2(i,j);
            float2  o = random2( n + g ) * WORLEY_JITTER;
            float2  p = g + o;
            float d = WORLEY_DIST_FNC(p, f);
            if (d < distF1) {
                distF2 = distF1;
                distF1 = d;
                off2 = off1;
                off1 = g;
                pos2 = pos1;
                pos1 = p;
            }
            else if (d < distF2) {
                distF2 = d;
                off2 = g;
                pos2 = p;
            }
        }
    }

    return float2(distF1, distF2);
}

float worley(float2 p){
    return 1.0-worley2(p).x;
}

float2 worley2(float3 p) {
    float3 n = floor( p );
    float3 f = frac( p );

    float distF1 = 1.0;
    float distF2 = 1.0;
    float3 off1, pos1;
    float3 off2, pos2;
    for( int k = -1; k <= 1; k++ ) {
        for( int j= -1; j <= 1; j++ ) {
            for( int i=-1; i <= 1; i++ ) {  
                float3  g = float3(i,j,k);
                float3  o = random3( n + g ) * WORLEY_JITTER;
                float3  p = g + o;
                float d = WORLEY_DIST_FNC(p, f);
                if (d < distF1) {
                    distF2 = distF1;
                    distF1 = d;
                    off2 = off1;
                    off1 = g;
                    pos2 = pos1;
                    pos1 = p;
                }
                else if (d < distF2) {
                    distF2 = d;
                    off2 = g;
                    pos2 = p;
                }
            }
        }
    }

    return float2(distF1, distF2);
}

float worley(float3 p){
    return 1.0-worley2(p).x;
}

#endif

Examples

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