LYGIA Shader Library

alphaFill (lygia/morphological/alphaFill)

fill alpha with edge colors

Dependencies:

Use:

<vec4> fillAlpha(<SAMPLER_TYPE> texture, <vec2> st, <vec2> pixel, <int> passes)

Check it on Github



#ifndef ALPHAFILL_RADIUS
#define ALPHAFILL_RADIUS 2.0
#endif

#ifndef ALPHAFILL_SAMPLE_FNC
#define ALPHAFILL_SAMPLE_FNC(TEX, UV) SAMPLER_FNC(TEX, UV)
#endif

#ifndef FNC_ALPHAFILL
#define FNC_ALPHAFILL
vec4 alphaFill(SAMPLER_TYPE tex, vec2 st, vec2 pixel, int passes) {
    vec4 accum = vec4(0.0, 0.0, 0.0, 0.0);
    float max_dist = sqrt(ALPHAFILL_RADIUS * ALPHAFILL_RADIUS);

    #if defined(PLATFORM_WEBGL)
    for (int s = 0; s < 100; s++) {   
        if (s >= passes)
            break;
    #else 
    for (int s = 0; s < passes; s++) {    
    #endif
        vec2 spiral = vec2(sin(float(s)*GOLDEN_ANGLE), cos(float(s)*GOLDEN_ANGLE));
        float dist = sqrt(ALPHAFILL_RADIUS * float(s));
        spiral *= dist;
        vec4 sampled_pixel = ALPHAFILL_SAMPLE_FNC(tex, st + spiral * pixel);
        sampled_pixel.rgb *= sampled_pixel.a;
        accum += sampled_pixel * (1.0 / (1.0 + dist));
        if (accum.a >= 1.0) 
            break;
    }

    return accum.rgba / max(0.0001, accum.a);
}
#endif

Dependencies:

Use:

<float4> fillAlpha(<SAMPLER_TYPE> texture, <float2> st, <float2> pixel, <int> passes)

Check it on Github



#ifndef ALPHAFILL_RADIUS
#define ALPHAFILL_RADIUS 2.0
#endif

#ifndef FNC_ALPHAFILL
#define FNC_ALPHAFILL

float4 alphaFill(SAMPLER_TYPE tex, float2 st, float2 pixel, int passes) {
    float4 accum = float4(0.0, 0.0, 0.0, 0.0);
    float max_dist = sqrt(ALPHAFILL_RADIUS * ALPHAFILL_RADIUS);
    for (int s = 0; s < passes; s++) {    
        float2 spiral = float2(sin(float(s)*GOLDEN_ANGLE), cos(float(s)*GOLDEN_ANGLE));
        float dist = sqrt(ALPHAFILL_RADIUS * float(s));
        spiral *= dist;
        float4 sampled_pixel = SAMPLER_FNC(tex, st + spiral * pixel);
        sampled_pixel.rgb *= sampled_pixel.a;
        accum += sampled_pixel * (1.0 / (1.0 + dist));
        if (accum.a >= 1.0) 
            break;
    }

    return accum.rgba / max(0.0001, accum.a);
}

#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