Depth Of Field

Depth of Field is a visual effect that simulate camera optics by making certain portions of the screen in-focus or out-of-focus depending on their distance from the camera. It can be used to make picture more cinematic and artistic. To achieve Depth Of Field effect, you need:

1. Render scene depth info to a texture

Add a Back Buffer Aligned render target to scene, then use a Shot with  Not Affect Camera style to render scene to texture. Then firt create two Scripts:



depthon DoConsoleCommand('r_scene 2')
depthoff DoConsoleCommand('r_scene 1')

then create two Actions for shot's 'Render to Texture':




Render Texture Begin Do Script depthon
Render Texture End Do Script depthoff

The console variable r_scene control Normal Rendering (1) or Depth Rendering (2). When do depth rendering :

1. First clear render target use Depth Back Color. The Depth Back Color is indicated by the console variable r_depth_bkcolor ( value range is 0~255 ), it's default value is 255.

2. Take the distance of vertex to camera, then map it to 0~1 by Zmin & Zmax, here:

Zmin   = Near Clipping plane * r_depth_nearscale;
Zmax  = Far Clipping plane * r_depth_farscale;

the r_depth_nearscale & r_depth_farscale is console variable.


2. Blurring the scene image to a texture

First use a use a none shader Post copy current scene image to a 256 x 256 texture, then use the gaussblur5x5_256.fx shader to blur it.
(use 512 x 512 texture can get more good picture quality, but need multiple blurring.)

3. Combine the clear scene image and blurred image by the depth texture


Use the depth_of_field.fx shader to compose final result, set the blurred image to the 2nd texure, and depth image to the 3rd texture.

Tips: Blur depth map before combining gets better result.