获取Depth的几种方法,分别有不同效果
1.
<span style="font-size:14px;"> float2 depth_uv = float2(i.uv_MainTex.x, 1-i.uv_MainTex.y); float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, depth_uv);</span>
2.
<span style="font-size:14px;"> float zx = SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, float4(depth_uv, 0, 0)); zx = Linear01Depth(zx);</span>
<span style="font-size:14px;"> v2f vert(appdata_full v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.screen = ComputeScreenPos(o.pos); COMPUTE_EYEDEPTH(o.screen.z); o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex); return o; }</span>
<span style="font-size:14px;"> float d2 = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screen)); d2 = LinearEyeDepth(d2);</span>
4.
<span style="font-size:14px;"> float3 normalValues; float depthValue; DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.screen.xy), depthValue, normalValues);</span>
需要在脚本中做如下代码
camera.depthTextureMode = DepthTextureMode.DepthNormals;
CameraDepthNormalsTexture 可以获取Normals和Depth
<span style="font-size:14px;">tex2D(_CameraNormalsTexture, i.uv_MainTex);</span>
---- by wolf96 http://blog.csdn.net/wolf96