Hi all,
(Cross-post from the forum - didn’t get any response there!)
I’m trying to do a graphical effect where a couple of objects in the scene write to the stencil buffer, and then a post-fx reads the stencil buffer to do a full-screen effect.
The part where the objects write to the stencil buffer is definitely working.
Let’s say I just want to write green pixels where the stencil buffer has been written to earlier. Here’s my shader:
Shader “Green” {
SubShader {
Tags { “RenderType”=“Opaque” “Queue”=“Geometry+1”}
Pass {
Stencil {
Ref 1
Comp equal
Pass keep
ZFail keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR {
return half4(0,1,0,1);
}
ENDCG
}
}
}
If I have my OnRenderImage function blitting straight from source to dest using this shader, everything works as expected - I get solid green on screen only where I wrote to the stencil:
void OnRenderImage (RenderTexture source, RenderTexture dest)
{
Graphics.Blit (source, dest, greenMat);
}
But, if I try to use an intermediate buffer, it seems to ignore the Stencil section of the pass, and just write green to every pixel:
void OnRenderImage (RenderTexture source, RenderTexture dest)
{
RenderTexture buffer = RenderTexture.GetTemporary(source.width, source.height, 0);
Graphics.Blit (source, buffer, greenMat);
Graphics.Blit(buffer, dest);
}
This just results in a completely green screen.
I’ve noticed that there’s a RenderTexture.SupportsStencil method, which returns false for my buffer, but I can’t find any documentation on why, or how to change it. Is that even relevant? I only want to use the main screen’s stencil buffer.
This is on PC.
Any help would be much appreciated,
Rob.