4
$\begingroup$

I'm making an FPS game, and for some of my guns, i want to be able to zoom in so that i can see farther. This isn't the problem. What i really want is to make the rest of the screen (The part not looking through the scope) blurry so that it looks like the character is focusing through the scope.

I want everything that is not seen directly through the scope to be blurred, for example, refer to the scopes of Crysis 3.

$\endgroup$
4
  • 1
    $\begingroup$ You can use a 2Dfilter to accomplish this. Probably also with a few custom uniforms so you can pass the information to the shader about when and where to blur. $\endgroup$
    – Mike Pan
    Commented Aug 5, 2014 at 18:34
  • 1
    $\begingroup$ Are you looking to simulate DOF (Depth of Field), or do you specifically want to blur everything not in scope, by the same factor, regardless of distance from focus point? $\endgroup$ Commented Oct 12, 2014 at 4:07
  • $\begingroup$ Does this help at all? $\endgroup$
    – J Sargent
    Commented Jan 10, 2015 at 2:54
  • $\begingroup$ @GoranMilovanovic I want to blur everything not in the scope $\endgroup$
    – christai
    Commented Jan 27, 2015 at 9:40

1 Answer 1

2
$\begingroup$

Extending the sample shader linked by @NoviceInDisguise:

How it works: the shader gets the rendered image as a texture its x/y coordinates range from 0 to 1.0

The distance from center is calculated by sqrt(x*x+y*y) > .3 to exclude a circular region from blur.

uniform sampler2D bgl_RenderedTexture;

void main(void)
{
  vec4 texcolor = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
  float x =  gl_TexCoord[0].st.x - .5;
  float y =  gl_TexCoord[0].st.y - .5;   

  if ( sqrt(x*x+y*y) > .3 ) { 
       float value = 0.0015;
       vec4 color = texture2D(bgl_RenderedTexture, vec2(gl_TexCoord[0].st.x + value, gl_TexCoord[0].st.y + value)); // Sample area around current pixel
       color += texture2D(bgl_RenderedTexture, vec2(gl_TexCoord[0].st.x - value, gl_TexCoord[0].st.y - value));
       color += texture2D(bgl_RenderedTexture, vec2(gl_TexCoord[0].st.x + value, gl_TexCoord[0].st.y - value));
       color += texture2D(bgl_RenderedTexture, vec2(gl_TexCoord[0].st.x - value, gl_TexCoord[0].st.y + value));
       color /= 4.0;        
       gl_FragColor = color;
   }
   else {
       gl_FragColor =texcolor;
   }
}

enter image description here

Node setup: Add a Filter 2D configure it as Custom Shader and select the above script.

enter image description here Sample render

Disclaimer: A decent blur needs to incorporate values from a 3x3, 5x5 or bigger matrix, the sample code only uses the left and right neighbor.

$\endgroup$
1
  • $\begingroup$ Gosh this was a while back, sorry for not confirming your answer yet :/ Is the float value the blur amount? And how would I incorporate a matrix? $\endgroup$
    – christai
    Commented Feb 24, 2017 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .