[Zz] depth of field (depth of field), pass1 renders the scene to a rendertarget as a clear version, pass2: bluredrt, pass3: wdepth = depth/far_z_clip

Source: Internet
Author: User
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 );}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.