Unity干货Unity分享征服Unity3d

Unity 深度缓冲区之查看深度图

2017-11-05  本文已影响231人  云木unity

深度图是展示深度缓冲区的纹理,是一个包含有场景里物体离照相机的距离值的一张纹理图;通过查看深度图可以对深度缓冲区存的数据有个直观的认识。

unity查看深度图.png

为了查看深度图:

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

具体操作

分为两部分,

  1. C#脚本部分
  2. Shader部分
C#脚本部分
  1. 摄像机设置Camera.depthTextureMode

mCamera.depthTextureMode = DepthTextureMode.Depth;

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

     void OnRenderImage (RenderTexture source, RenderTexture destination){  
         if (null != mMat)
         {
             Graphics.Blit(source, destination, mMat);
         }
         else
         {
             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);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    } 
}
  1. 把脚本赋给当前的主摄像机。
添加脚本.png

注意Far Clip Plane,摄像机的Far Clip Plane设置太大,看到的深度图可能是全黑的 或者 深度值变化不明显。

  1. 创建材质 与 设置shader
创建材质.png 材质选择shader.png
Shader的编写
  1. 深度贴图:sampler2D _CameraDepthTexture;
    通过Graphics.Blit()函数传入。深度图中记录的深度值:深度纹理中每个像素所记录的深度值是从0 到1 非线性分布的。
  2. 顶点着色器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;  
}  
  1. 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.

tex2Dproj.png

完整的shader DepthGrayscale.shader如下:

Shader "Custom/DepthGrayscale" {  
    SubShader {  
        Tags { "RenderType"="Opaque" }  
          
        Pass{  
            CGPROGRAM  
            #pragma vertex vert  
            #pragma fragment frag  
            #include "UnityCG.cginc"  
              
            sampler2D _CameraDepthTexture;  
              
            struct v2f {  
               float4 pos : SV_POSITION;  
               float4 scrPos:TEXCOORD1;  
            };  
              
            //Vertex Shader  
            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);   
            }  
            ENDCG  
        }  
    }  
    FallBack "Diffuse"  
}  

运行效果图:

效果图.png

如果看到的深度图是全黑的,可能摄像机的Far Clip Plane设置太大。

最后:
完整项目工程地址:GeWenL/DepthProj

相关文章

Unity RenderTexture的应用

参考

  1. Unity 深度图片获取
  2. 【Unity Shaders】使用Unity Render Textures实现画面特效——建立画面特效脚本系统
  3. Unity Water Shader
  4. unity,UNITY_PROJ_COORD和tex2Dproj
上一篇下一篇

猜你喜欢

热点阅读