Problem with rendering order of passes it seems

Hi,

I am trying to set up a shader which writes to the depthmask at a second pass (since I want the depth of the object written before it). However this seems to cause problems.
It seems as there is some fundamental drawing order of the passes, which I would like to alter if that is possible.

One can reproduce the scenario with the shader below, applying it to an object and duplicating that object. The Z sorting will not work right when overlapping the two objects.

Shader "Custom/ZWriteTest" {
	Properties {
	}
	SubShader {
		LOD 200
	    
		Pass {
			ZWrite Off
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			
			struct v2f {
			    float4 pos : SV_POSITION;
			    float3 color : COLOR0;
			};
			
			v2f vert (appdata_base v)
			{
			    v2f o;
			    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
			    o.color = v.normal * 0.5 + 0.5;
			    return o;
			}
			
			half4 frag (v2f i) : COLOR
			{
			    return half4 (i.color, 1);
			}
			ENDCG

   		}
   		
		Pass {
	        ZWrite On
	        ColorMask 0
	    }
	} 
	FallBack Off
}

Can anyone explain this and/or help me out?

I think your problem is in the ZWrite Off in your shader

I need the ZWrite in the second pass. Theoretically I think this shouldn’t be an issue, at least that’s my understanding of it.

Also, it seems that objects without this shader interact (Z-buffer wise) perfectly with an object with this shader. It’s just that when two objects with this shader are aligned behind each other that the wrong Z buffer can be shown.