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

**URL:** <https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276>\
**Category:** Game Engine Resources\
**Created:** [November 22, 2011, 9:16am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276 "2011-11-22T09:16:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinsh](https://blenderartists.org/user_avatar/blenderartists.org/martinsh/32/589035_2.png) [@martinsh](https://blenderartists.org/u/martinsh)\
**Post date:** [November 22, 2011, 9:16am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/1 "2011-11-22T09:16:03Z")

</div>

Hi,  
I am really lazy so I will just paste the text from my [dev-blog](http://devlog-martinsh.blogspot.com/) here:

This is my second attempt to create a depth of field shader with bokeh. the first one is here:  
[http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html](http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html)  
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”](http://www.youtube.com/watch?v=ReH7zzj5GPc) 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.

![http://dl.dropbox.com/u/11542084/highlights0.jpg](http://dl.dropbox.com/u/11542084/highlights0.jpg)  
 ![http://dl.dropbox.com/u/11542084/highlights1.jpg](http://dl.dropbox.com/u/11542084/highlights1.jpg)  
 ![http://dl.dropbox.com/u/11542084/highlights2.jpg](http://dl.dropbox.com/u/11542084/highlights2.jpg)  
 ![http://dl.dropbox.com/u/11542084/highlights3.jpg](http://dl.dropbox.com/u/11542084/highlights3.jpg)

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

![http://dl.dropbox.com/u/11542084/fringe.jpg](http://dl.dropbox.com/u/11542084/fringe.jpg)

bias:  
shifts the weights of the samples on the bokeh edges

![http://dl.dropbox.com/u/11542084/bias1.jpg](http://dl.dropbox.com/u/11542084/bias1.jpg)  
 ![http://dl.dropbox.com/u/11542084/bias2.jpg](http://dl.dropbox.com/u/11542084/bias2.jpg)

pentagon:  
pentagon shape of the bokeh (still needs some work)

![http://dl.dropbox.com/u/11542084/penta1.jpg](http://dl.dropbox.com/u/11542084/penta1.jpg)  
 ![http://dl.dropbox.com/u/11542084/penta2.jpg](http://dl.dropbox.com/u/11542084/penta2.jpg)

and the GLSL 2D Filter is [HERE](http://dl.dropbox.com/u/11542084/DoF_bokeh_2.1)  
version 2.2 is [HERE](http://dl.dropbox.com/u/11542084/DoF_bokeh_2.2)  
version 2.4 is [HERE](http://dl.dropbox.com/u/11542084/DoF_bokeh_2.4) (newest!)  
I am trying to comment it as simple as I can so anyone can understand it.

and blend is [HERE](http://dl.dropbox.com/u/11542084/DoF_playground.blend)  
new blend (2.4) is [HERE](http://dl.dropbox.com/u/11542084/DoF_2.4_playground.blend)  
I will be glad if you guys will find this useful. I would also like to know how it works for you.

---

<div class="post-metadata">

**Author:** ![SolarLune](https://blenderartists.org/user_avatar/blenderartists.org/solarlune/32/7258_2.png) [@SolarLune](https://blenderartists.org/u/SolarLune)\
**Post date:** [November 22, 2011, 9:56am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/2 "2011-11-22T09:56:46Z")

</div>

😃

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

---

<div class="post-metadata">

**Author:** ![JohnnyBlack](https://blenderartists.org/user_avatar/blenderartists.org/johnnyblack/32/11611_2.png) [@JohnnyBlack](https://blenderartists.org/u/JohnnyBlack)\
**Post date:** [November 22, 2011, 10:50am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/3 "2011-11-22T10:50:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martinsh](https://blenderartists.org/user_avatar/blenderartists.org/martinsh/32/589035_2.png) [@martinsh](https://blenderartists.org/u/martinsh)\
**Post date:** [November 22, 2011, 2:02pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/4 "2011-11-22T14:02:49Z")

</div>

Thanks, SolarLune!

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

---

<div class="post-metadata">

**Author:** ![mataii](https://blenderartists.org/user_avatar/blenderartists.org/mataii/32/238_2.png) [@mataii](https://blenderartists.org/u/mataii)\
**Post date:** [November 22, 2011, 2:49pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/5 "2011-11-22T14:49:18Z")

</div>

Thanks Martinsh, screenshots looks great! 🙂

---

<div class="post-metadata">

**Author:** ![rolo\_gl](https://blenderartists.org/letter_avatar_proxy/v4/letter/r/ba8739/32.png) [@rolo\_gl](https://blenderartists.org/u/rolo_gl)\
**Post date:** [November 22, 2011, 3:04pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/6 "2011-11-22T15:04:15Z")

</div>

Wow, those bokeh blur look beautiful!

---

<div class="post-metadata">

**Author:** ![skykooler](https://blenderartists.org/user_avatar/blenderartists.org/skykooler/32/9381_2.png) [@skykooler](https://blenderartists.org/u/skykooler)\
**Post date:** [November 22, 2011, 6:06pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/7 "2011-11-22T18:06:18Z")

</div>

Ooh, pretty!

---

<div class="post-metadata">

**Author:** ![Dhaher](https://blenderartists.org/user_avatar/blenderartists.org/dhaher/32/10823_2.png) [@Dhaher](https://blenderartists.org/u/Dhaher)\
**Post date:** [November 22, 2011, 6:45pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/8 "2011-11-22T18:45:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JohnnyBlack](https://blenderartists.org/user_avatar/blenderartists.org/johnnyblack/32/11611_2.png) [@JohnnyBlack](https://blenderartists.org/u/JohnnyBlack)\
**Post date:** [November 22, 2011, 9:49pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/9 "2011-11-22T21:49:47Z")

</div>

> [@martinsh](#):
>
> Thanks, SolarLune!
> 
> BlendingBGE - distance blur? like a blur that blurs the scene only in specified distance?

Yes.  
The blur should appear on a specified distance.

---

<div class="post-metadata">

**Author:** ![Karakasimov](https://blenderartists.org/user_avatar/blenderartists.org/karakasimov/32/14043_2.png) [@Karakasimov](https://blenderartists.org/u/Karakasimov)\
**Post date:** [November 22, 2011, 11:35pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/10 "2011-11-22T23:35:07Z")

</div>

Excellent work indeed!

---

<div class="post-metadata">

**Author:** ![martinsh](https://blenderartists.org/user_avatar/blenderartists.org/martinsh/32/589035_2.png) [@martinsh](https://blenderartists.org/u/martinsh)\
**Post date:** [November 23, 2011, 5:30am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/11 "2011-11-23T05:30:36Z")

</div>

Thank you for the nice replies.

> [@BlendingBGE](#):
>
> Yes.  
> The blur should appear on a specified distance.

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.  
[http://dl.dropbox.com/u/11542084/distance\_blur](http://dl.dropbox.com/u/11542084/distance_blur)

---

<div class="post-metadata">

**Author:** ![JohnnyBlack](https://blenderartists.org/user_avatar/blenderartists.org/johnnyblack/32/11611_2.png) [@JohnnyBlack](https://blenderartists.org/u/JohnnyBlack)\
**Post date:** [November 23, 2011, 6:00am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/12 "2011-11-23T06:00:19Z")

</div>

> [@martinsh](#):
>
> 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.  
> [http://dl.dropbox.com/u/11542084/distance\_blur](http://dl.dropbox.com/u/11542084/distance_blur)

amazing Martinsh! Thank you. Very nice!

---

<div class="post-metadata">

**Author:** ![MrPutuLips](https://blenderartists.org/user_avatar/blenderartists.org/mrputulips/32/14792_2.png) [@MrPutuLips](https://blenderartists.org/u/MrPutuLips)\
**Post date:** [November 23, 2011, 6:41am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/13 "2011-11-23T06:41:35Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![leonnn](https://blenderartists.org/user_avatar/blenderartists.org/leonnn/32/9115_2.png) [@leonnn](https://blenderartists.org/u/leonnn)\
**Post date:** [December 4, 2011, 8:53am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/14 "2011-12-04T08:53:10Z")

</div>

Amazing as usual thanks for share this!

---

<div class="post-metadata">

**Author:** ![leonnn](https://blenderartists.org/user_avatar/blenderartists.org/leonnn/32/9115_2.png) [@leonnn](https://blenderartists.org/u/leonnn)\
**Post date:** [December 5, 2011, 12:28pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/15 "2011-12-05T12:28:33Z")

</div>

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!

[![](https://blenderartists.org/uploads/default/original/4X/1/7/e/17e3ef6477dcd6ad47eebe89dcb7b0c928a60b22.jpeg "Sunny finding") ](https://www.youtube.com/watch?v=BMJR0ZIPDYo)

---

<div class="post-metadata">

**Author:** ![Josip\_Kladaric](https://blenderartists.org/user_avatar/blenderartists.org/josip_kladaric/32/948953_2.png) [@Josip\_Kladaric](https://blenderartists.org/u/Josip_Kladaric)\
**Post date:** [December 6, 2011, 2:50pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/16 "2011-12-06T14:50:41Z")

</div>

martinsh, as always, awesome !!!

---

<div class="post-metadata">

**Author:** ![mills332](https://blenderartists.org/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mills332](https://blenderartists.org/u/mills332)\
**Post date:** [December 7, 2011, 9:45am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/17 "2011-12-07T09:45:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![leonnn](https://blenderartists.org/user_avatar/blenderartists.org/leonnn/32/9115_2.png) [@leonnn](https://blenderartists.org/u/leonnn)\
**Post date:** [December 7, 2011, 10:44am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/18 "2011-12-07T10:44:36Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![martinsh](https://blenderartists.org/user_avatar/blenderartists.org/martinsh/32/589035_2.png) [@martinsh](https://blenderartists.org/u/martinsh)\
**Post date:** [December 8, 2011, 3:08am UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/19 "2011-12-08T03:08:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![martinsh](https://blenderartists.org/user_avatar/blenderartists.org/martinsh/32/589035_2.png) [@martinsh](https://blenderartists.org/u/martinsh)\
**Post date:** [December 8, 2011, 12:32pm UTC](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276/20 "2011-12-08T12:32:43Z")

</div>

Here is a blend file and with that - a major shader update.  
blend: [http://dl.dropbox.com/u/11542084/DoF\_playground.blend](http://dl.dropbox.com/u/11542084/DoF_playground.blend)  
the new shader v2.2: [http://dl.dropbox.com/u/11542084/DoF\_bokeh\_2.2](http://dl.dropbox.com/u/11542084/DoF_bokeh_2.2)

controls:  
•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)

![http://dl.dropbox.com/u/11542084/focus1.jpg](http://dl.dropbox.com/u/11542084/focus1.jpg)  
debug focus visualization enabled:  
 ![http://dl.dropbox.com/u/11542084/focus2.jpg](http://dl.dropbox.com/u/11542084/focus2.jpg)

[Next page](https://blenderartists.org/t/glsl-depth-of-field-with-bokeh-v2-4-update/525276.md?page=2)
