December 19, 2011

GLSL depth of field with bokeh v2.4

here is a new update on the depth of field shader.
• again totally redone the depth of field calculation with 2 possible options:

- Physically accurate DoF simulation calculated from "focalDepth" ,"focalLength", "f-stop" and "CoC" parameters.
- Manual - artist controlled DoF simulation calculated only from "focalDepth" and individual controls for near and far blur planes.

• added "circe of confusion" (CoC) parameter in mm to accurately simulate DoF with different camera sensor or film sizes.
• optical lens vignetting factor based on f-stop value and user defined properties.
• cleaned up the code
• some optimization

Shader is HERE
Blend is HERE

Mouse+WASD - look/movement
LMB - drag the focal point object
1 - enables DoF rendering
spacebar - toggles debug view
Up/Down arrowkeys - zoom in/out (changes focal length)
Left/Right arrowkeys - changes f-stop value


  1. Hi Martinsh..

    First of all. Amazing work! Love it! Thanks for sharing.

    Copied 2.4 shader to 2.3 .blend. 2.4 bokeh seems disabled while moving, bokeh reappeared during stop. 2.3 bokeh works nicely while moving and stop (ATI4650).


  2. Hey, I have updated the blend also. Yeah the python file has also changed significantly.

  3. Now its working properly (ATI HD4650). Thanks man. :D

  4. Amazing work Martins, thank you very much for sharing!

  5. Hey Martin, just questions:


    what range of these values ? what are they equal to ?

    1. i start game in blender and do not see any dof also

    2. and why *1000?

      and which pdf did you read to achieve this dof ?

    3. hi Andrey,
      so it does not work when you press "1" in Blender, does it show anything in console?

      - focalDepth is the focal point or actually focal plane in meters from the camera.
      in other words it is the area where will be no blur. range can be from (0.0 - to infinite)

      - focalLength is sort of the optical power of the lens which changes the field of view (zoom of lens). In shader it will not zoom in or out but this value changes the depth of field (blur) significantly. The value is in milimeters and range depends on the lens you are trying to simulate. For example I have a lens on my camera of Focal Length ranging from 18mm to 200mm, basically this means that with 18mm I can make a wide angle photo but depth of field will be minimal, but 200mm will zoom in alot and depth of field will be very noticeable.

      fStop (or "f-number" or "relative aperture") is maybe the most important value that will affect the DoF. It sort of simulates the diameter of the aperture - entrance pupil. Smaller the diameter, bigger the fStop value - less depth of field.

      You can read these basic concepts of photography in wikipedia. And from there I have based my implementation.

    4. This comment has been removed by the author.

    5. This comment has been removed by the author.

    6. This comment has been removed by the author.

    7. thx

      1) do i calculate values right ? it seems ok.... like in blender


      camera_s *cam = world->current_cam;
      float zfar = cam->zfar;
      float znear = cam->znear;

      ///////////////////////////////// calc ////////////////////////////////

      // project mouseXY along Z axis
      vec3_t start,end;

      // find intersection point with scene from mouseVector in world coords
      trees_collider trace;


      // project to 2d
      float focalDepth = 0; // m - focus point depth (focal plane origin)... is ignored if 'autofocus' is on
      vec3_t screenPosition;

      screenPosition[1] = TSystem::tvars.v_height->fvalue-screenPosition[1];


      // linearize depth
      focalDepth = -zfar * znear / (screenPosition[2] * (zfar - znear) - zfar);

      // it must change FOV but does not affect...
      static float focalLUserVal = 20;
      if(input->GetKeyState(ID_KEYBOARD, KEY_DELETE)== KeyPressed)
      if(input->GetKeyState(ID_KEYBOARD, KEY_INSERT)== KeyPressed)
      float focalLength = _clampf2(focalLUserVal,6,600)*1000; // mm - default is 20 (userval)

      static float stopUserVal = 1; // default is 1 (userval)
      if(input->GetKeyState(ID_KEYBOARD, KEY_ADD)== KeyPressed)
      if(input->GetKeyState(ID_KEYBOARD, KEY_SUBTRACT)== KeyPressed)

      float stop = _clampf2(stopUserVal,-1,16);
      float fStop = powf(sqrtf(2.0),stop) * 1000; // mm - no phyton round here...




      if i need to make different dof variants (to blur near/ not to blur far ; to blur near only; to blur far ony) ..
      i need to tweak these values,i guess

      one more problem here:
      float penta(vec2 coords) //pentagonal shape
      this causes crash on geforce gtx580....

      what is in gl_TexCoord[3].xy ? ))

      i just mainly try to adapt shader into my app,not blender

    8. Hi, :)
      Okay I will answer the easier parts first:

      "if i need to make different dof variants (to blur near/ not to blur far ; to blur near only; to blur far ony) ..
      i need to tweak these values,i guess"

      For that case play with "manualdof":
      - first set "bool manualdof" to "true".

      then if you want near dof only:

      float ndofstart = 0.0; <-- near dof will start from focal plane
      float ndofdist = 2.0; <-- near dof will falloff for 2 meters

      float fdofstart = 999.0; <-- far dof will start somewhere in infinity..
      float fdofdist = 999.0; <-- far dof will extend even more distant

      - I have to make it more intuitive.. like adding "bool neardof" and "bool fardof" to enable/disable near/far dof completely. And I actually have to write a document of the shader and general use :)

      2) Well it was experimental anyway, so better to delete the pentagonal part completely. I can do that for you and make more simplified shader.

      3) gl_TexCoord[3] was the canvas coordinates in Blender, but now it is the same as gl_TexCoord[0], I have to fix that.

    9. i had not understood about manualDof also/.
      how to calculate those values

    10. ok cool )))) thx.... i played with shader and tried to optimize that, so check this out:

      i guess i made all that right...what do u think ?

      ...................... app code part ......................

      CVector3f DepthBlurOffsetKernel[9]; // xy - offset; z - kernel

      float dbsize = 1.25; // depthblursize
      CVector2f texel(1.0/(float)postRender2dMapRes,1.0/(float)postRender2dMapRes);
      CVector2f wh = texel * dbsize;

      DepthBlurOffsetKernel[1].SetXY( 0.0, -wh[1]);
      DepthBlurOffsetKernel[2].SetXY( wh[0], -wh[1]);

      DepthBlurOffsetKernel[3].SetXY(-wh[0], 0.0);
      DepthBlurOffsetKernel[4].SetXY( 0.0, 0.0);
      DepthBlurOffsetKernel[5].SetXY( wh[0], 0.0);

      DepthBlurOffsetKernel[6].SetXY(-wh[0], wh[1]);
      DepthBlurOffsetKernel[7].SetXY( 0.0, wh[1]);
      DepthBlurOffsetKernel[8].SetXY( wh[0], wh[1]);

      DepthBlurOffsetKernel[0][2] = 1.0/16.0; DepthBlurOffsetKernel[1][2] = 2.0/16.0; DepthBlurOffsetKernel[2][2] = 1.0/16.0;
      DepthBlurOffsetKernel[3][2] = 2.0/16.0; DepthBlurOffsetKernel[4][2] = 4.0/16.0; DepthBlurOffsetKernel[5][2] = 2.0/16.0;
      DepthBlurOffsetKernel[6][2] = 1.0/16.0; DepthBlurOffsetKernel[7][2] = 2.0/16.0; DepthBlurOffsetKernel[8][2] = 1.0/16.0;

      for(int i=0;i<9;i++)

    11. yup that last code seems to be correct, but it is not vital for DoF, it just blurs depth map a little.

      here is my simplified GLSL shader for DoF:

      also in the python file "" there is a lot of stuff that is confusing and unnecessary :P

      So more about the DoF values you need to get:

      you only need 3 - focalDepth, focalLength and fStop

      - As I understand for the "focalDepth" you will read a pixel from rendered depth map, right?
      That is how I do the autofocus in shader - I just get middle pixel of the screen and focus on it.

      - "focalLength" should also change together with your camera lens.
      In Blender it is simple - camera.lens = focalLength
      I guess in your implementation you need to specify FOV right? Or change the frustum matrix?
      anyway here is the math to get FOV in degrees from focalLength:

      x = 35.0; //35mm camera film/sensor
      FOV = (2 * atan(x / (2 * focalLength)) * 180 / PI);

      fStop - it ranges from 0.7 to 256.0
      if you don`t need physically correct steps, just use any value in range of it.

      btw. there are many much more simple depth of field shaders out there :D
      This one I have made is physically correct though.

    12. physically correct - it is big cool thing)

      i thought,that it is not correct to allow postfx to change camera matrix, i will think over fov changing, but thanks. probably i will left fov untouched (but term will be added)

      thanks, for feedback - i will play with that.. you are a guru

    13. hm....

      does not change anything.... manualdof is on

  6. This comment has been removed by the author.

  7. Hi there!
    Did you know you can improve the quality of the blur by mip-mapping the screen texture? I wonder if this is possible with a python script.
    It is possible when you have access to the OpenGL functions so maybe you can simply implement this into the Candy branch?

  8. Does not appear to be working on BGE 2.77