LYGIA Shader Library

2D (lygia/filter/gaussianBlur/2D)

two dimension Gaussian Blur to be applied in only one passes

Dependencies:

Use:

gaussianBlur2D(<SAMPLER_TYPE> texture, <vec2> st, <vec2> pixel_direction , const int kernelSize)

Check it on Github



#ifndef GAUSSIANBLUR2D_TYPE
#ifdef GAUSSIANBLUR_TYPE
#define GAUSSIANBLUR2D_TYPE GAUSSIANBLUR_TYPE
#else
#define GAUSSIANBLUR2D_TYPE vec4
#endif
#endif

#ifndef GAUSSIANBLUR2D_SAMPLER_FNC
#ifdef GAUSSIANBLUR_SAMPLER_FNC
#define GAUSSIANBLUR2D_SAMPLER_FNC(TEX, UV) GAUSSIANBLUR_SAMPLER_FNC(TEX, UV)
#else
#define GAUSSIANBLUR2D_SAMPLER_FNC(TEX, UV) sampleClamp2edge(TEX, UV)
#endif
#endif

#ifndef FNC_GAUSSIANBLUR2D
#define FNC_GAUSSIANBLUR2D
GAUSSIANBLUR2D_TYPE gaussianBlur2D(in SAMPLER_TYPE tex, in vec2 st, in vec2 offset, const int kernelSize) {
    GAUSSIANBLUR2D_TYPE accumColor = GAUSSIANBLUR2D_TYPE(0.);

    #ifndef GAUSSIANBLUR2D_KERNELSIZE

        #if defined(PLATFORM_WEBGL)
            #define GAUSSIANBLUR2D_KERNELSIZE 20
            float kernelSizef = float(kernelSize);
        #else
            #define GAUSSIANBLUR2D_KERNELSIZE kernelSize
            float kernelSizef = float(GAUSSIANBLUR2D_KERNELSIZE);
        #endif

    #else
        float kernelSizef = float(GAUSSIANBLUR2D_KERNELSIZE);
    #endif

    float accumWeight = 0.;
    const float k = 0.15915494; // 1 / (2*PI)
    vec2 xy = vec2(0.0);
    for (int j = 0; j < GAUSSIANBLUR2D_KERNELSIZE; j++) {
        #if defined(PLATFORM_WEBGL)
        if (j >= kernelSize)
            break;
        #endif
        xy.y = -.5 * (kernelSizef - 1.) + float(j);
        for (int i = 0; i < GAUSSIANBLUR2D_KERNELSIZE; i++) {
            #if defined(PLATFORM_WEBGL)
            if (i >= kernelSize)
                break;
            #endif
            xy.x = -0.5 * (kernelSizef - 1.) + float(i);
            float weight = (k / kernelSizef) * gaussian(xy, kernelSizef);
            accumColor += weight * GAUSSIANBLUR2D_SAMPLER_FNC(tex, st + xy * offset);
            accumWeight += weight;
        }
    }
    return accumColor / accumWeight;
}
#endif

Dependencies:

Use:

gaussianBlur2D(<SAMPLER_TYPE> texture, <float2> st, <float2> pixel_direction , const int kernelSize)

Check it on Github



#ifndef GAUSSIANBLUR2D_TYPE
#ifdef GAUSSIANBLUR_TYPE
#define GAUSSIANBLUR2D_TYPE GAUSSIANBLUR_TYPE
#else
#define GAUSSIANBLUR2D_TYPE float4
#endif
#endif

#ifndef GAUSSIANBLUR2D_SAMPLER_FNC
#ifdef GAUSSIANBLUR_SAMPLER_FNC
#define GAUSSIANBLUR2D_SAMPLER_FNC(TEX, UV) GAUSSIANBLUR_SAMPLER_FNC(TEX, UV)
#else
#define GAUSSIANBLUR2D_SAMPLER_FNC(TEX, UV) SAMPLER_FNC(TEX, UV)
#endif
#endif

#ifndef GAUSSIANBLUR2D_KERNEL_MAXSIZE
#define GAUSSIANBLUR2D_KERNEL_MAXSIZE 12
#endif

#ifndef FNC_GAUSSIANBLUR2D
#define FNC_GAUSSIANBLUR2D
GAUSSIANBLUR2D_TYPE gaussianBlur2D(in SAMPLER_TYPE tex, in float2 st, in float2 offset, const int kernelSize) {
    GAUSSIANBLUR2D_TYPE accumColor = float4(0.0, 0.0, 0.0, 0.0);

    float accumWeight = 0.;
    const float k = .15915494; // 1 / (2*PI)
    float kernelSizef = float(kernelSize);
    float kernelSize2 = kernelSizef * kernelSizef;

    for (int j = 0; j < GAUSSIANBLUR2D_KERNEL_MAXSIZE; j++) {
        if (j >= kernelSize)
            break;
        float y = -.5 * (kernelSize2 - 1.) + float(j);
        for (int i = 0; i < GAUSSIANBLUR2D_KERNEL_MAXSIZE; i++) {
            if (i >= kernelSize)
                break;

            float x = -.5 * (kernelSize2 - 1.) + float(i);
            float weight = (k / kernelSize2) * exp(-(x * x + y * y) / (2. * kernelSize2));

            accumColor += weight * GAUSSIANBLUR2D_SAMPLER_FNC(tex, st + float2(x, y) * offset);
            accumWeight += weight;
        }
    }
    return accumColor / accumWeight;
}
#endif

Examples

License

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