• 摄像机设置Camera.depthTextureMode。让摄像机生成一张深度纹理;
  • 生成了纹理后,OnRenderImage把它传到shader sampler2D _CameraDepthTexture 中。
  • shader采样计算出深度值,渲染到屏幕中
  • 项目地址: GeWenL / DepthProj

    分为两部分,

  • C#脚本部分
  • Shader部分
  • C#脚本部分
  • 摄像机设置Camera.depthTextureMode
  • mCamera.depthTextureMode = DepthTextureMode.Depth;

    为了真正从Unity渲染器中抓取被渲染过的图像,我们需要使用Unity内置的OnRenderImage函数。下面的代码允许我们访问当前被渲染的图像:

     void OnRenderImage (RenderTexture source, RenderTexture destination){  
         if (null != mMat)
             Graphics.Blit(source, destination, mMat);
             Graphics.Blit(source, destination);
    

    这个函数负责从Unity渲染器中抓取当前的render texture,然后使用Graphics.Blit()函数再传递给Shader(通过sourceTexture参数),然后再返回一个处理后的图像再次传递回给Unity渲染器(通过destTexture参数)。

    这两个行数互相搭配是处理画面特效的很常见的方法。你可以在下面的连接中找到这两个函数更详细的信息:

    2.1 OnRenderImage:该摄像机上的任何脚本都可以收到这个回调(意味着你必须把它附到一个Camera上)。允许你修改最后的Render Texture。
    2.2 Graphics.Blit:sourceTexture会成为material的_CameraDepthTexture。

    完整PostProcessDepthGrayscale.cs 代码如下:

    using UnityEngine;
    using System.Collections;
    [RequireComponent(typeof (Camera))]
    public class PostProcessDepthGrayscale : MonoBehaviour {
        public Material mMat;  
        private Camera mCamera;
        void Start () {  
            mCamera = gameObject.GetComponent<Camera> ();
            //设置Camera的depthTextureMode,使得摄像机能生成深度图。
            if (mCamera) {
                mCamera.depthTextureMode = DepthTextureMode.Depth;  
        void OnRenderImage (RenderTexture source, RenderTexture destination){  
            if (null != mMat)
                Graphics.Blit(source, destination, mMat);
                Graphics.Blit(source, destination);
    
  • 把脚本赋给当前的主摄像机。
  • 深度贴图:sampler2D _CameraDepthTexture;
    通过Graphics.Blit()函数传入。深度图中记录的深度值:深度纹理中每个像素所记录的深度值是从0 到1 非线性分布的。
  • 顶点着色器Vertex Shader
    在顶点着色器中获取顶点在屏幕空间的位置,用做采样深度图的uv坐标:
  • v2f vert (appdata_base v){  
       v2f o;  
       o.pos = mul (UNITY_MATRIX_MVP, v.vertex);  
       o.scrPos=ComputeScreenPos(o.pos);  //将返回片段着色器的屏幕位置
       return o;  
    
  • Fragment Shader ,在像素着色器中采样深度贴图:
  • half4 frag (v2f i) : COLOR{  
       //计算当前像素深度
       float depthValue =Linear01Depth (tex2Dproj(_CameraDepthTexture,UNITY_PROJ_COORD(i.scrPos)).r);  
       return half4(depthValue,depthValue,depthValue,1);
    

    3.1 UNITY_PROJ_COORD:given a 4-component vector, return a texture coordinate suitable for projected texture reads. On most platforms this returns the given value directly.
    3.2 tex2Dproj:
    解释1. either by explicitly dividing x and y by w or by using tex2Dproj.
    tex2D(_ShadowTex , input.posProj.xy / input.posProj.w); 等价于
    tex2Dproj(_ShadowTex, input.posProj);
    解释2. Samples a 2D texture using a projective divide; the texture coordinate is divided by t.w before the lookup takes place.

    //Fragment Shader half4 frag (v2f i) : COLOR{ float depthValue =Linear01Depth (tex2Dproj(_CameraDepthTexture,UNITY_PROJ_COORD(i.scrPos)).r); return half4(depthValue,depthValue,depthValue,1); ENDCG FallBack "Diffuse"

    运行效果图: