lygia
/v1.1.6
/filter
/gaussianBlur
/1D
)one dimension Gaussian Blur to be applied in two passes
Dependencies:
Use:
gaussianBlur1D(<SAMPLER_TYPE> texture, <vec2> st, <vec2> pixel_direction , const int kernelSize)
#ifndef GAUSSIANBLUR1D_TYPE
#ifdef GAUSSIANBLUR_TYPE
#define GAUSSIANBLUR1D_TYPE GAUSSIANBLUR_TYPE
#else
#define GAUSSIANBLUR1D_TYPE vec4
#endif
#endif
#ifndef GAUSSIANBLUR1D_SAMPLER_FNC
#ifdef GAUSSIANBLUR_SAMPLER_FNC
#define GAUSSIANBLUR1D_SAMPLER_FNC(TEX, UV) GAUSSIANBLUR_SAMPLER_FNC(TEX, UV)
#else
#define GAUSSIANBLUR1D_SAMPLER_FNC(TEX, UV) sampleClamp2edge(TEX, UV)
#endif
#endif
#ifndef FNC_GAUSSIANBLUR1D
#define FNC_GAUSSIANBLUR1D
#ifdef PLATFORM_WEBGL
GAUSSIANBLUR1D_TYPE gaussianBlur1D(in SAMPLER_TYPE tex,in vec2 st,in vec2 offset,const int kernelSize){
GAUSSIANBLUR1D_TYPE accumColor = GAUSSIANBLUR1D_TYPE(0.0);
float kernelSizef = float(kernelSize);
float accumWeight = 0.0;
const float k = 0.39894228;// 1 / sqrt(2*PI)
for (int i = 0; i < 16; i++) {
if( i >= kernelSize)
break;
float x = -0.5 * (float(kernelSize) - 1.0)+float(i);
float weight = (k/float(kernelSize)) * gaussian(x, kernelSizef);
GAUSSIANBLUR1D_TYPE tex = GAUSSIANBLUR1D_SAMPLER_FNC(tex, st + x * offset);
accumColor += weight * tex;
accumWeight += weight;
}
return accumColor/accumWeight;
}
#else
GAUSSIANBLUR1D_TYPE gaussianBlur1D(in SAMPLER_TYPE tex,in vec2 st,in vec2 offset,const int kernelSize){
GAUSSIANBLUR1D_TYPE accumColor=GAUSSIANBLUR1D_TYPE(0.);
float kernelSizef = float(kernelSize);
float accumWeight = 0.0;
const float k = 0.39894228;// 1 / sqrt(2*PI)
for (int i = 0; i < kernelSize; i++) {
float x = -0.5 * ( kernelSizef -1.0) + float(i);
float weight = (k / kernelSizef) * gaussian(x, kernelSizef);
GAUSSIANBLUR1D_TYPE tex = GAUSSIANBLUR1D_SAMPLER_FNC(tex, st + x * offset);
accumColor += weight * tex;
accumWeight += weight;
}
return accumColor/accumWeight;
}
#endif
#endif
Dependencies:
Use:
gaussianBlur1D(<SAMPLER_TYPE> texture, <float2> st, <float2> pixel_direction , const int kernelSize)
#ifndef GAUSSIANBLUR1D_TYPE
#ifdef GAUSSIANBLUR_TYPE
#define GAUSSIANBLUR1D_TYPE GAUSSIANBLUR_TYPE
#else
#define GAUSSIANBLUR1D_TYPE float4
#endif
#endif
#ifndef GAUSSIANBLUR1D_SAMPLER_FNC
#ifdef GAUSSIANBLUR_SAMPLER_FNC
#define GAUSSIANBLUR1D_SAMPLER_FNC(TEX, UV) GAUSSIANBLUR_SAMPLER_FNC(TEX, UV)
#else
#define GAUSSIANBLUR1D_SAMPLER_FNC(TEX, UV) SAMPLER_FNC(TEX, UV)
#endif
#endif
#ifndef FNC_GAUSSIANBLUR1D
#define FNC_GAUSSIANBLUR1D
GAUSSIANBLUR1D_TYPE gaussianBlur1D(in SAMPLER_TYPE tex,in float2 st,in float2 offset,const int kernelSize){
GAUSSIANBLUR1D_TYPE accumColor = float4(0.0, 0.0, 0.0, 0.0);
float accumWeight = 0.0;
const float k = 0.39894228;// 1 / sqrt(2*PI)
float kernelSize2 = float(kernelSize)*float(kernelSize);
for(int i = 0; i < 16; i++){
if( i >= kernelSize)
break;
float x = -0.5 * (float(kernelSize) - 1.0)+float(i);
float weight = (k/float(kernelSize)) * exp(-(x*x)/(2.0*kernelSize2));
accumColor += weight * GAUSSIANBLUR1D_SAMPLER_FNC(tex, st + x * offset);
accumWeight += weight;
}
return accumColor/accumWeight;
}
#endif
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.
Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository