Http://blog.csdn.net/xoyojank/article/details/1883520
What is the depth of field effect?The depth of field effect (DOF) is common in human eyes and optical camera devices. For example:
In short, the close and distant scenes are blurred, while the objects near the focal point are very clear. As for why this effect occurs, I am too lazy to say: P
So how to achieve this effect?Figure:
Starting from the camera, the camera is divided into three parts by distance: close-range fuzzy, Focus range (clear), and distance fuzzy rendering based on depth (distance, it is clear within the focus range, otherwise it will be blurred.
|
The entire process is divided into three pass: 1. render the scenario to a rendertarget as a clear version. 2. blur the rendertarget obtained in the previous step to obtain the bluredrt (Fuzzy version ). 3. merging. determine whether the image should be blurred based on the distance between the image and data. If the image is not in the focus range, draw the bluredrt; otherwise, draw the rendertarget. so there is another question: how to calculate the depth value? Formula: wdepth = depth/far_z_clip. Here, I render the depth value into rendertarget In the alpha channel. In this way, we can directly obtain color. a In the last step to make a deep judgment.(The picture on the right shows the alpha channel, which stores the depth value) |
Shader
And final effect
Sampler rendertarget; sampler bluredrt; // focus range float fneardis; float ffardis; float4 ps_main (float2 texcoord: Regular): color0 {float4 color = tex2d (rendertarget, texcoord ); if (color. a> fneardis & color. A <ffardis) return color; else return tex2d (bluredrt, texcoord );} |
Does it seem unnatural? Why? Because the implementation of the dof at the junction of clarity and blur is too rigid, it is like dividing it into three parts -_-!
So we can add two more transitions?Like this:
Sampler rendertarget; sampler vertex; // focus range float vertex; float ffardis; float vertex; float ffarrange; float4 ps_main (float2 texcoord: vertex): color0 {float4 sharp = tex2d (rendertarget, texcoord); float4 blur = tex2d (bluredrt, texcoord); float percent = max (saturate (1-(sharp. a-fneardis)/fnearrange), saturate (sharp. a-(ffardis-ffarrange)/ffarrange); Return lerp (sharp, blur, percent );} |