I’m developing a glow effect, having this test scene:
I have a secondary Camera rendering to a Target texture, only objects on Glow
layer:
Now I want to cull fragments covered by Eiffel Tower. Of course I don’t want to draw all the 1M Eiffel Tower triangles again here. The idea is to reuse the depth texture of the Main Camera (I can get a sample from it using Unity - Scripting API: DepthTextureMode.Depth):
But I can’t see how to cull fragments in the secondary camera shader. The shader code is bare simple, my question is how to use _CameraDepthTexture in my shader:
Shader "eppz!/Glow/Camera"
{
Properties
{
_MainColor ("Main Color", Color) = (1,1,1,1)
_GlowColor ("Glow Color", Color) = (1,1,1,1)
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
fixed4 _MainColor;
fixed4 _GlowColor;
sampler2D_CameraDepthTexture;
float4 vert(float4 v:POSITION) : SV_POSITION
{
// Sample from depth texture.
// Compare to current vertex depth (But how?).
// Discard fragment if Z-culled.
return mul (UNITY_MATRIX_MVP, v);
}
fixed4 frag() : COLOR
{
// Sample here at least, then output black pixel if fragment is Z-culled.
return _GlowColor;
}
ENDCG
}
}
}