LYGIA Shader Library

jumpFlood (lygia/morphological/jumpFlood)

Algorithmic paradigm, that approximate other algorithms in constant-time performance. Does not always compute the correct result for every pixel, although in practice errors are few and the magnitude of errors is generally small Based on the work of Alexander Griffis in this project which were introduce by Rong Guodong at an ACM symposium in 2006


<float> jumpFloodCalcIterTotal(<vec2> res)
<float> jumpFloodCalcIter(<float> iterTotal, <float> frame)
<vec4>  jumpFloodEncode(<sampler2D> tex, <vec2> st)
<float> jumpFloodSdf(<sampler2D> tex, <vec2> st)
<vec4>  jumpFloodIterate(<sampler2D> tex, <vec2> st, <vec2> pixel, <float> iterTotal, <float> iterN)

Check it on Github

#define JUMPFLOOD_TYPE vec4


#define JUMPFLOOD_ENCODE_FNC(TEX, UV) vec4(UV * SAMPLER_FNC(TEX, UV).a, 0.0, 1.0)


JUMPFLOOD_TYPE jumpFloodEncode(SAMPLER_TYPE tex, vec2 st) { return JUMPFLOOD_ENCODE_FNC(tex, st); }
float jumpFloodSdf(SAMPLER_TYPE tex, vec2 st) { return distance(st, JUMPFLOOD_SAMPLE_FNC(tex, st).xy); }

float jumpFloodCalcIterTotal(vec2 res) { return ceil(log2(max(res.x, res.y)) / log2(2.0)); }
float jumpFloodCalcIter(float iterTotal, float frame) { return mod(frame, iterTotal); }

JUMPFLOOD_TYPE jumpFloodIterate(SAMPLER_TYPE tex, vec2 st, vec2 pixel, float iterTotal, float iterN) {
    float jDist = pow(2.0, iterTotal - iterN - 1.0);

    vec2 offsets[9];
    offsets[0] = vec2(-1.0, -1.0);
    offsets[1] = vec2(-1.0, 0.0);
    offsets[2] = vec2(-1.0, 1.0);
    offsets[3] = vec2(0.0, -1.0);
    offsets[4] = vec2(0.0, 0.0);
    offsets[5] = vec2(0.0, 1.0);
    offsets[6] = vec2(1.0, -1.0);
    offsets[7] = vec2(1.0, 0.0);
    offsets[8] = vec2(1.0, 1.0);

    float closest_dist = 9999999.9;
    vec2 closest_pos = vec2(0.0);
    JUMPFLOOD_TYPE closest_data = JUMPFLOOD_TYPE(0.0);

    for(int i = 0; i < 9; i++) {
        vec2 jump = st + (offsets[i] * jDist * pixel);
        JUMPFLOOD_TYPE seed = JUMPFLOOD_SAMPLE_FNC(tex, jump);
        vec2 seedpos = seed.xy;
        float dist = distance(seedpos, st);
        if (seedpos != vec2(0.0) && dist <= closest_dist) {
            closest_dist = dist;
            closest_data = seed;

    return closest_data;




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