sepblur.glsl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Adapted from:
  2. // http://callumhay.blogspot.com/2010/09/gaussian-blur-shader-glsl.html
  3. #ifdef GL_ES
  4. precision mediump float;
  5. precision mediump int;
  6. #endif
  7. #define PROCESSING_TEXTURE_SHADER
  8. uniform sampler2D texture;
  9. // The inverse of the texture dimensions along X and Y
  10. uniform vec2 texOffset;
  11. varying vec4 vertColor;
  12. varying vec4 vertTexCoord;
  13. uniform int blurSize;
  14. uniform int horizontalPass; // 0 or 1 to indicate vertical or horizontal pass
  15. uniform float sigma; // The sigma value for the gaussian function: higher value means more blur
  16. // A good value for 9x9 is around 3 to 5
  17. // A good value for 7x7 is around 2.5 to 4
  18. // A good value for 5x5 is around 2 to 3.5
  19. // ... play around with this based on what you need :)
  20. const float pi = 3.14159265;
  21. void main() {
  22. float numBlurPixelsPerSide = float(blurSize / 2);
  23. vec2 blurMultiplyVec = 0 < horizontalPass ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
  24. // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
  25. vec3 incrementalGaussian;
  26. incrementalGaussian.x = 1.0 / (sqrt(2.0 * pi) * sigma);
  27. incrementalGaussian.y = exp(-0.5 / (sigma * sigma));
  28. incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y;
  29. vec4 avgValue = vec4(0.0, 0.0, 0.0, 0.0);
  30. float coefficientSum = 0.0;
  31. // Take the central sample first...
  32. avgValue += texture2D(texture, vertTexCoord.st) * incrementalGaussian.x;
  33. coefficientSum += incrementalGaussian.x;
  34. incrementalGaussian.xy *= incrementalGaussian.yz;
  35. // Go through the remaining 8 vertical samples (4 on each side of the center)
  36. for (float i = 1.0; i <= numBlurPixelsPerSide; i++) {
  37. avgValue += texture2D(texture, vertTexCoord.st - i * texOffset * blurMultiplyVec) * incrementalGaussian.x;
  38. avgValue += texture2D(texture, vertTexCoord.st + i * texOffset * blurMultiplyVec) * incrementalGaussian.x;
  39. coefficientSum += 2.0 * incrementalGaussian.x;
  40. incrementalGaussian.xy *= incrementalGaussian.yz;
  41. }
  42. gl_FragColor = avgValue / coefficientSum;
  43. }