Hi, I'm trying to sample a depth buffer inside a vertex shader to see if a particular vertex is hidden behind already drawn geometry. Than I'm planning to modify vertices according to the result. I've already got a working code sample from my other project - but it samples depth buffer in fragment shader, which is more common case. When I moved sampling part to the vertex shader, a strange flickering started to occur when object is drawn before other objects. When object is beind, everything seems fine. In the example below, if calculated depth is larger than one in buffer (so it's occluded by some other geometry) - quad is drawn in green. Otherwise it is red. Center of quad is the vertex that is sampled. Do I need to do something else to make it work? Code (CSharp): v2g vert(appdata_base v) { v2g o; o.pos = UnityObjectToClipPos(v.vertex); float4 screenPos = ComputeScreenPos(o.pos); o.sceneZ = LinearEyeDepth(tex2Dlod(_CameraDepthTexture, float4(screenPos.xy / screenPos.w, 0, 0))); COMPUTE_EYEDEPTH(o.pointZ); return o; } float4 frag(g2f i) : SV_Target { if (i.sceneZ > i.pointZ) return float4(1, 0, 0, 1); else return float4(0, 1, 0, 1); }
OK, I've got it as soon as I have posted the question There was a Fallback "Diffuse" part at the end of entire shader, which caused it (I guess) to write to depth buffer itself. Thus the flickering.