Box Blur
Gaussian Blur
O(n^2)
Discrete Sample
// discrete sample gaussian blur vs
attribute vec4 a_position;
attribute vec2 a_texcoord;
uniform vec2 blurSize;
varying vec2 vblurtexcoord[9];
void main()
{
gl_Position = a_position;
vec2 offset1 = 1.0 * blurSize;
vec2 offset2 = 2.0 * blurSize;
vec2 offset3 = 3.0 * blurSize;
vec2 offset4 = 4.0 * blurSize;
vblurtexcoord[0] = a_texcoord - offset4;
vblurtexcoord[1] = a_texcoord - offset3;
vblurtexcoord[2] = a_texcoord - offset2;
vblurtexcoord[3] = a_texcoord - offset1;
vblurtexcoord[4] = a_texcoord;
vblurtexcoord[5] = a_texcoord + offset1;
vblurtexcoord[6] = a_texcoord + offset2;
vblurtexcoord[7] = a_texcoord + offset3;
vblurtexcoord[8] = a_texcoord + offset4;
}
// discrete sample gaussian blur fs
precision mediump float;
uniform sampler2D inputTexture;
varying vec2 vblurtexcoord[9];
void main()
{
lowp vec3 sample = texture2D(inputTexture, vblurtexcoord[0]).rgb * 0.05;
sample += texture2D(inputTexture, vblurtexcoord[1]).rgb * 0.09;
sample += texture2D(inputTexture, vblurtexcoord[2]).rgb * 0.12;
sample += texture2D(inputTexture, vblurtexcoord[3]).rgb * 0.15;
sample += texture2D(inputTexture, vblurtexcoord[4]).rgb * 0.18;
sample += texture2D(inputTexture, vblurtexcoord[5]).rgb * 0.15;
sample += texture2D(inputTexture, vblurtexcoord[6]).rgb * 0.12;
sample += texture2D(inputTexture, vblurtexcoord[7]).rgb * 0.09;
sample += texture2D(inputTexture, vblurtexcoord[8]).rgb * 0.05;
gl_FragColor = vec4(sample, 1.0);
}
Linear Sample
// linear sample gaussian blur vs
attribute vec4 a_position;
attribute vec2 a_texcoord;
uniform vec2 blurSize;
varying vec2 vblurtexcoord[5];
void main()
{
gl_Position = a_position;
vec2 firstOffset = 1.3846153846 * blurSize;
vec2 secondOffset = 3.2307692308 * blurSize;
vblurtexcoord[0] = a_texcoord - secondOffset;
vblurtexcoord[1] = a_texcoord - firstOffset;
vblurtexcoord[2] = a_texcoord;
vblurtexcoord[3] = a_texcoord + firstOffset;
vblurtexcoord[4] = a_texcoord + secondOffset;
}
// linear sample gaussian blur fs
precision mediump float;
uniform sampler2D inputTexture;
varying vec2 vblurtexcoord[5];
void main()
{
lowp vec3 sample = texture2D(inputTexture, vblurtexcoord[0]).rgb * 0.0702702703;
sample += texture2D(inputTexture, vblurtexcoord[1]).rgb * 0.3162162162;
sample += texture2D(inputTexture, vblurtexcoord[2]).rgb * 0.2270270270;
sample += texture2D(inputTexture, vblurtexcoord[3]).rgb * 0.3162162162;
sample += texture2D(inputTexture, vblurtexcoord[4]).rgb * 0.0702702703;
gl_FragColor = vec4(sample, 1.0);
}
Stack Blur
Reference