Gaussian Blur Shader Issue

Hello all, I’m new to the forums and fairly new to JMonkey Engine. In the midst of working on my latest game I came across an issue with the included Common/MatDefs/Blur/VGaussianBlur.frag and Common/MatDefs/Blur/HGaussianBlur.frag shaders.

sum += texture2D(m_Texture, vec2(texCoord.x- 4.0*blurSize, texCoord.y )) * 0.05;
sum += texture2D(m_Texture, vec2(texCoord.x- 3.0*blurSize, texCoord.y )) * 0.09;
sum += texture2D(m_Texture, vec2(texCoord.x - 2.0*blurSize, texCoord.y)) * 0.12;
sum += texture2D(m_Texture, vec2(texCoord.x- blurSize, texCoord.y )) * 0.15;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y)) * 0.16;
sum += texture2D(m_Texture, vec2(texCoord.x+ blurSize, texCoord.y )) * 0.15;
sum += texture2D(m_Texture, vec2(texCoord.x+ 2.0*blurSize, texCoord.y )) * 0.12;
sum += texture2D(m_Texture, vec2(texCoord.x+ 3.0*blurSize, texCoord.y )) * 0.09;
sum += texture2D(m_Texture, vec2(texCoord.x+ 4.0*blurSize, texCoord.y )) * 0.05;

The first and last value of the above code from the HGaussianBlur fragmentation script should be 0.06 rather than 0.05. As it is using these shaders for blurring an image causes a loss of color. Combined the percentage values should add to 1, but as written they add to 0.98. In example, if you were to apply this to a purely white image the resulting blurred image would be slightly grey, having been reduced from vec4(1, 1, 1, 1) to vec4(0.98, 0.98, 0.98, 0.98).

Again the rewritten code should appear as:
HGaussianBlur:

sum += texture2D(m_Texture, vec2(texCoord.x- 4.0*blurSize, texCoord.y )) * 0.06;
sum += texture2D(m_Texture, vec2(texCoord.x- 3.0*blurSize, texCoord.y )) * 0.09;
sum += texture2D(m_Texture, vec2(texCoord.x - 2.0*blurSize, texCoord.y)) * 0.12;
sum += texture2D(m_Texture, vec2(texCoord.x- blurSize, texCoord.y )) * 0.15;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y)) * 0.16;
sum += texture2D(m_Texture, vec2(texCoord.x+ blurSize, texCoord.y )) * 0.15;
sum += texture2D(m_Texture, vec2(texCoord.x+ 2.0*blurSize, texCoord.y )) * 0.12;
sum += texture2D(m_Texture, vec2(texCoord.x+ 3.0*blurSize, texCoord.y )) * 0.09;
sum += texture2D(m_Texture, vec2(texCoord.x+ 4.0*blurSize, texCoord.y )) * 0.06;

VGaussianBlur:

sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y - 4.0*blurSize)) * 0.06;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y - 3.0*blurSize)) * 0.09;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y - 2.0*blurSize)) * 0.12;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y - blurSize)) * 0.15;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y)) * 0.16;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y + blurSize)) * 0.15;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y + 2.0*blurSize)) * 0.12;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y + 3.0*blurSize)) * 0.09;
sum += texture2D(m_Texture, vec2(texCoord.x, texCoord.y + 4.0*blurSize)) * 0.06;

Anyway just wanted to get this out there in the event anyone else was having trouble with their blurring methods :slight_smile:

2 Likes

Hey thanks! will fix this

No problem, thanks for JMonkey Engine :slight_smile:

I figure this was an easy thing to miss when just using the blur shaders for blooms and glows, but one of the things I’m using them for is blurring a black and white texture for use as an alpha mask mapped with world coordinates and I noticed that things that should’ve been opaque were slightly transparent.

Being able to find that and re-write it certainly highlighted one of the open source benefits.