2019-11-26 08:38:53 +00:00
|
|
|
uniform float u_blur_radius;
|
|
|
|
uniform vec2 u_blur_size;
|
|
|
|
uniform vec2 u_blur_dir;
|
2017-11-25 07:56:50 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
const float PI = 3.14159265;
|
|
|
|
const float RADIUS_MULTIPLIER = 3.0;
|
2017-11-25 07:56:50 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
// blur_radius 0 is NOT supported and MUST be caught before.
|
2017-11-25 07:56:50 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
// Partially from http://callumhay.blogspot.com/2010/09/gaussian-blur-shader-glsl.html
|
|
|
|
void main() {
|
|
|
|
float sigma = u_blur_radius; // *shrug*
|
|
|
|
float blur_radius = u_blur_radius * RADIUS_MULTIPLIER;
|
|
|
|
vec3 incrementalGaussian;
|
|
|
|
incrementalGaussian.x = 1.0 / (sqrt(2.0 * PI) * sigma);
|
|
|
|
incrementalGaussian.y = exp(-0.5 / (sigma * sigma));
|
|
|
|
incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y;
|
2017-12-28 15:41:32 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
vec2 pixel_step = vec2(1.0) / u_blur_size;
|
2017-12-28 15:41:32 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
float coefficientSum = 0;
|
|
|
|
vec4 sum = Texture(u_source, vUv) * incrementalGaussian.x;
|
|
|
|
coefficientSum += incrementalGaussian.x;
|
|
|
|
incrementalGaussian.xy *= incrementalGaussian.yz;
|
2017-12-28 15:41:32 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
int pixels_per_side = int(floor(blur_radius / 2.0));
|
|
|
|
for (int i = 1; i <= pixels_per_side; i++) {
|
|
|
|
vec2 p = i * pixel_step * u_blur_dir;
|
2017-12-28 15:41:32 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
sum += Texture(u_source, vUv - p) * incrementalGaussian.x;
|
|
|
|
sum += Texture(u_source, vUv + p) * incrementalGaussian.x;
|
2017-12-28 15:41:32 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
coefficientSum += 2.0 * incrementalGaussian.x;
|
|
|
|
incrementalGaussian.xy *= incrementalGaussian.yz;
|
|
|
|
}
|
2017-11-25 07:56:50 +00:00
|
|
|
|
2019-11-26 08:38:53 +00:00
|
|
|
setOutputColor(sum / coefficientSum);
|
2017-11-25 07:56:50 +00:00
|
|
|
}
|