How to create the ZDepth Accurate?

I wanna to compare two ZDepth,one is rendered into a renderTexture,another is compute in Shader,the porblem is the previous rendered ZDepth texture seems dont have enough Accurate ,so is result like this:


It shouldnt be,I wanna something like this:

here is the shader code to render ZDepth to a texture:

SubShader {
	    Tags { "RenderType"="Opaque" }
	    Pass {
	        Fog { Mode Off }
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			
			struct v2f {
			    float4 pos : SV_POSITION;
			    float2 depth : TEXCOORD0;
			};
			
			v2f vert (appdata_base v) {
			    v2f o;
			    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
			    o.depth.xy=o.pos.zw;
			    return o;
			}
			
			float4 frag(v2f i) : COLOR {
			    float d=i.depth.x/i.depth.y;
			    float4 c=EncodeFloatRGBA(d);
			    return c;
			}
			ENDCG
			}//endpass
	}

here is the shader code to compare them in the same sapce:

SubShader {
		Tags { "RenderType"="Opaque" }
		pass{
		//Zwrite off
		//ZTest Always
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "UnityCG.cginc"
		
		sampler2D _myShadow;
		float4x4 _litMVP;
		
		struct vertOut {
			float4 pos:SV_POSITION;
			float4 litPos;
		};
		vertOut vert(appdata_base v)
		{
			vertOut o;
			o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
			float4 wVertex=mul(_Object2World ,v.vertex);
			o.litPos=mul(_litMVP,wVertex);
			return o;
		}
		float4 frag(vertOut i):COLOR
		{
			float2 shadowTexc=0.5*i.litPos.xy/i.litPos.w+float2(0.5,0.5);
			float litZ=i.litPos.z/i.litPos.w;
			float4 c=tex2D(_myShadow,shadowTexc);
			float t=DecodeFloatRGBA(c);
			if(litZ>t)
			return 0.3;
			else
			return 0.7;
		}
		ENDCG
		}//endpass
	}

what can i improve to access enough Accurate ZDepth

Why don’t you use _CameraDepthTexture?

_CameraDepthTexture only have the current ZDepth in its space,I have two sapce,one is light ,another is the camera,I need to transfer OBJ in Camera Space into the light Space’s ZDepth,so a compareable ZDepth tex is wanted,i wanna to reproduce the ShadowMapping process,apparently i caught in trouble,how did unity do it…

What about having 2 cameras:

  1. From light’s point of view
  2. From player’s point of view

Now you render the first camera into RenderTexture using renderTexture.format = RenderTextureFormat.Depth;

This should give you a depth texture from light’s point of view.

That is what i have done now,the porblem is the accurate of ZDepth will lose after come through these space transfer