GLSL - make effect only on one texture

I wish to blur my background, with the following fragment shader:

 varying vec4 vColor;
    varying vec2 vTexCoord;

    uniform vec2 screenSize;

    uniform sampler2D u_texture;
    uniform vec4 v_time;

    const float RADIUS = 0.75;

    const float SOFTNESS = 0.6;

    const float blurSize = 1.0/1000.0;

    void main() {

	    vec4 texColor = vec4(0.0); // texture2D(u_texture, vTexCoord)
	    texColor += texture2D(u_texture, vTexCoord - 4.0*blurSize) * 0.05;
   	    texColor += texture2D(u_texture, vTexCoord - 3.0*blurSize) * 0.09;
  	    texColor += texture2D(u_texture, vTexCoord - 2.0*blurSize) * 0.12;
   	    texColor += texture2D(u_texture, vTexCoord - blurSize) * 0.15;
   	    texColor += texture2D(u_texture, vTexCoord) * 0.16;
   	    texColor += texture2D(u_texture, vTexCoord + blurSize) * 0.15;
   	    texColor += texture2D(u_texture, vTexCoord + 2.0*blurSize) * 0.12;
   	    texColor += texture2D(u_texture, vTexCoord + 3.0*blurSize) * 0.09;
   	    texColor += texture2D(u_texture, vTexCoord + 4.0*blurSize) * 0.05;

        vec4 timedColor = (vColor + v_time);

	    vec2 position = (gl_FragCoord.xy / screenSize.xy) - vec2(0.5);
	    float len = length(position);

	    float vignette = smoothstep(RADIUS, RADIUS-SOFTNESS, len);

	    texColor.rgb = mix(texColor.rgb, texColor.rgb * vignette, 0.5);
		
	    gl_FragColor = vec4(texColor.rgb * timedColor.rgb, texColor.a);
    }

But the problem being is that the shader blurs all screen… and i want it to blur only the background texture… what should i write to make it focus on my background texture instead of all the screen?

You will have to bind this when you draw your background, and then bind another shader that doesn’t blur when you draw everything else.

As davedes tutorials say, which I think you are following correct me if I’m wrong, a two pass gaussian blur would be much more performant than the one pass thing you have here. However, for a simple background in a 2D game it shouldn’t make much of a difference. Just a suggestion :slight_smile:

I didn’t even think about unbinding and binding shader programs while rendering the game… correct me if i understood you wrong.
It won’t hurt the performance or something? i mean unbinding and binding different shader programs while rendering 60 times a sec won’t hurt performance?

But I will absolutely try it out, thanks :slight_smile:

Changing the current shader program isn’t something you want to do 10,000 times per frame, but a few times is fine ;D

Thanks for pointing this out :smiley: