Depth of Field

I’m sorry I couldn’t find this somewhere else, but could someone tip me off on where to look for information on using ‘depth of field’ in the game engine? I’ve seen it can be done, as in the Bathroom Demo. Thanks a ton!

unfortunately looks like the links are broken. But you can download my bathroom demo and take the DoF from there.

Thank you so much Martinsh. Your bathroom demo is outstanding.


Alright, I think I’m on the right track now. Thanks a ton.

I’ve never managed to make it work with ATI though… Mpan said he had a fix, but he hadn’t post it. Too bad, I’d have love to try this Dof script. It really looks wonderful.

Vicmax, yeah, that’s what I’ve heard. I’ll bet there are some things out there that only work on ATI though. Hash’s Animation:Master is supposedly optimized for ATI.

Martinsh, if you could clarify one point for me though, where is the Sensor for the DoF script? I see the script, but what links the script with the ‘1’ button to turn it on? Thanks

Find the FilterController empty outside of the bathroom.
I think that it’s the default selected object at the opening of the file.

There are Three sensors (one, two, and three on the keyboard).

I attached a new DoF .blend with blurred depthmap to remove sharp artifacts.

DoF_zblur.blend (694 KB)

here is the comparison images before/after

F - starts the filter
mousewheel: change focal length,

  • / - :max blur size
    [ / ] :focus radius

to enable autofocus, in FocusU and FocusV scripts erase “//” before “focus = texture2D(bgl_DepthTexture, vec2(0.5,0.5)).r;”

All credit goes to Mike Pan (mpan3)


DoF_update.blend (694 KB)

Cray, that’s exactly what I needed to know. Thanks man.

Martinsh, that’s excellent. Thanks for the demo.

I see three scripts in the file that pertain to focus; FocusSet, FocusU, and FocusV. Could you explain what each one does? These are exactly from Mike Pan, or have you changed them? Thanks dude

There are comments in each script, but basically:
“Focus set” - lets you control the focal length with mousewheel and then sends the value to property named “focaltarget”
“FocusU” and “FocusV” are diognal directional blur passes - i could actually try to put both of them in 1 script for ease of use.

They are Mike Pan`s scripts, but i added a depthblur

I’m getting 2d filter - shader compile error in the console :frowning:

good work martinsh! :slight_smile:
i’ve attached a version of your scripts that doesn’t give a shader compile error on ati cards.


DoF_update_ATI.blend (715 KB)

I still can’t get it to work… :frowning:

Cool that one works.

Is this a larger performance hit with the blurred depth map than without?

Martinsh, thank you very much for the ZBlur test file, but I’ve been having trouble with it.

I basically copied the three scripts in that file over to my file and linked them into an ‘Empty.’ The DoF effect turns on right when I want it to, but it seems to apply an equal blur to everything the camera sees.

Here is the scene.

And here is the game engine test–

I need the camera to be orthographic; does that cause problems with DoF?

I have added the 4 properties to the Empty;
focus: 0.900
max: .001
focaltarget: 0.900
radus: 0.300

I’ve tinkered with the focaltarget and focus values, but they don’t seem to have any effect on the DoF effect.
I’d greatly appreciate any thoughts.


Neither of those blends work on my computer, Imac with an ATI Radeon HD 2600 Pro.

CausticAgnostic, could you please give me the blend file, it will be much easier to fix it myself. Srry for late reply.

No worries dude. Thanks for helping me so much, I really appreciate it.

The blend file is set up so that the depth of field triggers when you hold down the spacebar. The depth of field effect turns off when you let go of the spacebar.

I was hoping to have the depth of field effect tuned to a specified ‘depth,’ so I wasn’t going to use your inventive ‘mouse’ script, but it is included in the blend file for testing purposes.

That rocky texture is printed on the vertical plane in the back of the scene; makes it easier to evaluation the depth of field effect.

Thanks very much


depth of field test.blend (203 KB)

i have the best idea for this and it will revolutionize blender, heh heh, if you were to kill depth and just have a very low setting of blur it could be a cheap but effective way of acheiving anti-aliasing in the game engine:evilgrin:.