1uniform sampler2D tex; 2uniform sampler2D dtex; 3 4out vec4 FragColor; 5 6float focalDepth = 10.; 7float maxblur = 1.; 8float range = 100.; 9 10void main() 11{ 12 vec2 uv = gl_FragCoord.xy / u_screen; 13 float curdepth = texture(dtex, uv).x; 14 vec4 FragPos = u_inverse_projection_matrix * (2.0 * vec4(uv, curdepth, 1.0) - 1.0); 15 FragPos /= FragPos.w; 16 17 float depth = FragPos.z; 18 float blur = clamp(abs(depth - focalDepth) / range, -maxblur, maxblur); 19 20 vec2 offset = 10. / u_screen; 21 22 vec4 col = texture(tex, uv); 23 vec4 colOriginal = col; 24 // Weight from here http://artmartinsh.blogspot.fr/2010/02/glsl-lens-blur-filter-with-bokeh.html 25 26 col += texture(tex, uv + (vec2(0.0, 0.4) * offset) * blur); 27 col += texture(tex, uv + (vec2(0.15, 0.37) * offset) * blur); 28 col += texture(tex, uv + (vec2(0.29,0.29) * offset) * blur); 29 col += texture(tex, uv + (vec2(-0.37,0.15) * offset) * blur); 30 col += texture(tex, uv + (vec2(0.4, 0.0) * offset) * blur); 31 col += texture(tex, uv + (vec2(0.37, -0.15) * offset) * blur); 32 col += texture(tex, uv + (vec2(0.29, -0.29) * offset) * blur); 33 col += texture(tex, uv + (vec2(-0.15, -0.37) * offset) * blur); 34 col += texture(tex, uv + (vec2(0.0, -0.4) * offset) * blur); 35 col += texture(tex, uv + (vec2(-0.15, 0.37) * offset) * blur); 36 col += texture(tex, uv + (vec2(-0.29, 0.29) * offset) * blur); 37 col += texture(tex, uv + (vec2(0.37, 0.15) * offset) * blur); 38 col += texture(tex, uv + (vec2(-0.4, 0.0) * offset) * blur); 39 col += texture(tex, uv + (vec2(-0.37, -0.15) * offset) * blur); 40 col += texture(tex, uv + (vec2(-0.29, -0.29) * offset) * blur); 41 col += texture(tex, uv + (vec2(0.15, -0.37) * offset) * blur); 42 43 col += texture(tex, uv + (vec2(0.15, 0.37) * offset) * blur * 0.9); 44 col += texture(tex, uv + (vec2(-0.37, 0.15) * offset) * blur * 0.9); 45 col += texture(tex, uv + (vec2(0.37, -0.15) * offset) * blur * 0.9); 46 col += texture(tex, uv + (vec2(-0.15, -0.37) * offset) * blur * 0.9); 47 col += texture(tex, uv + (vec2(-0.15, 0.37) * offset) * blur * 0.9); 48 col += texture(tex, uv + (vec2(0.37, 0.15) * offset) * blur * 0.9); 49 col += texture(tex, uv + (vec2(-0.37, -0.15) * offset) * blur * 0.9); 50 col += texture(tex, uv + (vec2(0.15, -0.37) * offset) * blur * 0.9); 51 52 col += texture(tex, uv + (vec2(0.29, 0.29) * offset) * blur * 0.7); 53 col += texture(tex, uv + (vec2(0.4, 0.0) * offset) * blur * 0.7); 54 col += texture(tex, uv + (vec2(0.29, -0.29) * offset) * blur * 0.7); 55 col += texture(tex, uv + (vec2(0.0, -0.4) * offset) * blur * 0.7); 56 col += texture(tex, uv + (vec2(-0.29, 0.29) * offset) * blur * 0.7); 57 col += texture(tex, uv + (vec2(-0.4, 0.0) * offset) * blur * 0.7); 58 col += texture(tex, uv + (vec2(-0.29, -0.29) * offset) * blur * 0.7); 59 col += texture(tex, uv + (vec2(0.0, 0.4) * offset) * blur *0.7); 60 61 col += texture(tex, uv + (vec2(0.29, 0.29) * offset) * blur * 0.4); 62 col += texture(tex, uv + (vec2(0.4, 0.0) * offset) * blur * 0.4); 63 col += texture(tex, uv + (vec2(0.29, -0.29) * offset) * blur * 0.4); 64 col += texture(tex, uv + (vec2(0.0, -0.4) * offset) * blur * 0.4); 65 col += texture(tex, uv + (vec2(-0.29, 0.29) * offset) * blur * 0.4); 66 col += texture(tex, uv + (vec2(-0.4, 0.0) * offset) * blur * 0.4); 67 col += texture(tex, uv + (vec2(-0.29, -0.29) * offset) * blur * 0.4); 68 col += texture(tex, uv + (vec2(0.0, 0.4) * offset) * blur * 0.4); 69 70 col = vec4(col.rgb / 41.0, col.a); 71 depth = clamp(max(1.1666 - (FragPos.z/240.0), FragPos.z - 2000.0), 0., 1.); 72 73 vec3 final = colOriginal.rgb * depth + col.rgb * (1. - depth); 74 75 FragColor = vec4(final, colOriginal.a); 76} 77