I have 2 cameras, first one is the main camera and it draws all the geometry as usual, and the second one is a light camera that draws some 2d lights. And then i have a post effect that Blits those two textures.
So the problem is, i need to render some transparent geometry (some 2d light) into a render texture, lets call it ‘LightRT’. But i need to occlude it with the depth buffer values from what was rendered by main camera, which renders into RenderTexture.active.
I’m trying to do it like this, in the image effect:
protected void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.SetRenderTarget(LightRT.colorBuffer, source.depthBuffer);
ImageEffectCamera.Render();
Graphics.SetRenderTarget(source.colorBuffer, source.depthBuffer);
Material.SetTexture("_Overlay", LightRT);
Graphics.Blit(source, destination, Material);
}
and in the shader of the light i set ZTest to LEqual, which should occlude the light, right?
Light shader:
Shader "01DEVS/Light 2D/Screen"
{
Properties
{
_Color("Color (RGBA)",Color) = (1,1,1,1)
_MainTex ("Lightmap (RGBA)", 2D) = "white" {}
[Enum(UnityEngine.Rendering.CompareFunction)]_StencilComp("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
[Enum(UnityEngine.Rendering.StencilOp)]_StencilOp ("Stencil Operation", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"RenderType"="Light"
}
Pass
{
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
}
Name "BASE"
Cull Back
Lighting Off
ZWrite Off
ZTest LEqual
ColorMask RGBA
Blend OneMinusDstColor One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
half4 _MainTex_ST;
fixed4 _Color;
struct appdata_mine {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
v2f vert( appdata_mine v ){
v2f o;
o.vertex = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = TRANSFORM_TEX( v.texcoord, _MainTex );
o.color = v.color * _Color;
return o;
}
half4 frag( v2f i ) : COLOR
{
return i.color;
}
ENDCG
}
}
}
The light is not occluded, and i have no idea why, can anyone explain why?