GLSL depth of field with bokeh v2.4 (update)

(martinsh) #1

I am really lazy so I will just paste the text from my dev-blog here:

This is my second attempt to create a depth of field shader with bokeh. the first one is here:
And I am really glad that it got popular quite fast. It has been even used in Google Chrome’s ROME “3 DREAMS OF BLACK” hehe.

This one is much more flexible and I have added few new features.

• variable sample count to increase quality/performance
• option to blur depth buffer to reduce hard edges
• option to dither the samples with noise or pattern
• bokeh chromatic aberration/fringing
• bokeh bias to bring out bokeh edges
• image thresholding to bring out highlights when image is out of focus
• pentagonal bokeh shape (experimental)

yet to do

• add multi-shape bokeh (does anyone knows how to make procedural pentagon or hexagon?)
• bokeh vignetting at screen edges

The shader is made on 2006 iMac with Mobility Radeon card so it should work on any hardware supporting OpenGL.

Screenshots are captured with high sample count, but the point is to show the features and capabilities of the shader.

some of the main features explained with images:

threshold & gain:
brings out highlights by “threshold” value and enhances them with “gain”
as you can see this is the main part that makes the distinct look of the bokeh blur. Without bringing out highlights, the blur looks like just a regular circular blur.

adds chromatic aberration for the blur (small overlay image shows r,g,b color offset of the sample)

shifts the weights of the samples on the bokeh edges

pentagon shape of the bokeh (still needs some work)

and the GLSL 2D Filter is HERE
version 2.2 is HERE
version 2.4 is HERE (newest!)
I am trying to comment it as simple as I can so anyone can understand it.

and blend is HERE
new blend (2.4) is HERE
I will be glad if you guys will find this useful. I would also like to know how it works for you.

(SolarLune) #2


That’s really, quite good. Very much so in fact… EXCELLENT.

(JohnnyBlack) #3

One question : Is there any way to achieve Distance Blur?

(martinsh) #4

Thanks, SolarLune!

BlendingBGE - distance blur? like a blur that blurs the scene only in specified distance?

(mataii) #5

Thanks Martinsh, screenshots looks great! :slight_smile:

(rolo_gl) #6

Wow, those bokeh blur look beautiful!

(skykooler) #7

Ooh, pretty!

(Dhaher) #8

That is just fantastic! Typical of Martinsh work.:yes:

(JohnnyBlack) #9

The blur should appear on a specified distance.

(Karakasimov) #10

Excellent work indeed!

(martinsh) #11

Thank you for the nice replies.

here you go. it should be really easy to use. You have to specify the distance where the blur starts and the distance how far it fades in. All the distances are in Blender Units.

(JohnnyBlack) #12

amazing Martinsh! Thank you. Very nice!

(MrPutuLips) #13

This is awesome. When you make the blend, please make a version that works the same as your old DoF ( just centring to the screen so it can be used for a moving camera). Also, is it possible to make the chromatic aberration affect the objects?

Thanks for sharing this script :slight_smile:

I forgot to add: it seems pretty noisy even at 512 samples :frowning:

(leonnn) #14

Amazing as usual thanks for share this!

(leonnn) #15

Martinsh I was playing with the DOf with the bokeh effects and its amazing what we can do here is a little presentation with your filter, thanks!

(Josip Kladaric) #16

martinsh, as always, awesome !!!

(mills332) #17

I don’t know why… but if I add a filter as a 2d filter actuator, it never works for me, unless I download the pre-made .blend sample.

What am I doing wrong?

(leonnn) #18

Some filters have properties that are floating values and those need to be replicated in order to them to work, for example, the vignette need the vigneting size or something like that and a valuefrom -1, to 1 ( at least on the one that Im using, but it was modificated) so you need to set it up. Other filters may need a auxiliar python script in order to make them work, like the HDR that need the get light script or the DOF that needs an auxiliar like autofocus or manual focus for example.

And there are other things that may differ from scene to scene, the size for example every scene needs an Z-far and a Z-near for the SSAO to work properly, but all the filters have specific configurations that need to be changed in order to make them work, the templates that martinsh share with us are the best for the scene that cames with the blender but they need to be changed if you want better results for your scene.

I hope that it helps!

(martinsh) #19

leonnn is right,
My shaders are lacking a detailed documentation how to properly set them up, but I try to fix that with the new shaders.
Shaders that uses depth texture input as SSAO and DoF are very dependent to camera range so also for scene scaling . I usually use 1 BlenderUnit = 1meter and camera depth range from 0.1 --> 100.0. Well 100meters is not much, especially for outdoors scenes, but the values can be cleverly tweaked to work for large scale areas. The reason is the depth texture precision on older ATI/AMD cards with large scene sizes, they tend to produce artifacts that looks like stripes.
Thankfully the shaders are very customizable :), it is a matter of tweaking to acquire the best results for your needs.

(martinsh) #20

Here is a blend file and with that - a major shader update.
the new shader v2.2:

•WASD+mouse = move&look
•1 - enable depth of field shader
•left mouse button = set focal target
•spacebar toggles a debug visualization of focus point and focal range (red line = focal point, green area = focal range)
debug focus visualization enabled: