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