A difficult problem to express in words. The issue is that 2 objects in the scene sharing the same material are not writing depth correctly without instancing… phew.
Here’s a picture:
CameraDepthTexture is represented here as Red.
Scene on left, game on right.
You can immediately see the inconsistency. But that’s not really the major problem here. The issue is the vertex modification in the surface shader. Here’s the simple shader:
Shader "Custom/Flat" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
LOD 200
Cull Off
ZTest LEqual
CGPROGRAM
#pragma surface surf Lambert vertex:vert nolightmap addshadow
#pragma target 3.0
uniform sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void vert(inout appdata_base v) {
//Vertex comes in at local coords?
//vertex output expects local coords, so this should just flatten the vert in local space
//But it doesn't, it places the vert down to 0 in world space **** UNLESS the material uses instancing! ****
v.vertex.y = 0;
}
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
FallBack Off
}
All it does is take the local vertex.y and make it 0.
I’m under the assumption that the incoming vertex is local and the output expected is local.
Why then, when two objects exist, the vertex modification places the vertex at y=0 worldSpace?
I’m assuming that this is probably a dynamic batching situation where matrices are no longer per-object?
I say that because if I enable “use instancing” the objects render correctly.
Here’s the same scene with the cloned object disabled. Everything looks good.

