- Home /
Stencil Buffer in Image Effect not updating?
I'm trying to make use of the stencil buffer to selectively apply an image effect to pixels in a post-process image effect. The problem I'm having is that the stencil buffer doesn't seem to be getting properly cleared/re-populated, or perhaps OnRenderImage is not getting called, which means that the stencil mask I create never gets changed if the frame changes after the effect is first applied.
I've created a simple reproduction case of the problem, which can be achieved as follows:
Create a shader that writes a value "5" to the stencil buffer for every pixel rendered:
Shader "Write5ToStencil" {
SubShader {
Pass {
Stencil {
Ref 5
Pass Replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 vert (float4 vertex : POSITION) : SV_POSITION{
return mul(UNITY_MATRIX_MVP, vertex);
}
fixed4 frag () : SV_Target {
return fixed4(0.5, 0.5, 1, 1);
}
ENDCG
}
}
}
Create a material that uses this shader, and place it on a few objects in the scene.
Now create another shader that will be used as an image effect. It'll have two passes that will test the value in the stencil buffer and draw white if there is something, or black if not:
Shader "Hidden/Is5InTheStencilBuffer" {
SubShader {
// If the stencil buffer value is NOT 5, draw black
Pass {
Cull Off ZWrite Off ZTest Always
Stencil {
Ref 5
Comp NotEqual
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 vert (float4 vertex : POSITION) : SV_POSITION{
return mul(UNITY_MATRIX_MVP, vertex);
}
fixed4 frag () : SV_Target {
return fixed4(0, 0, 0, 1);
}
ENDCG
}
// If the stencil buffer value IS 5, draw white
Pass {
Cull Off ZWrite Off ZTest Always
Stencil {
Ref 5
Comp Equal
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 vert (float4 vertex : POSITION) : SV_POSITION{
return mul(UNITY_MATRIX_MVP, vertex);
}
fixed4 frag () : SV_Target {
return fixed4(1, 1, 1, 1);
}
ENDCG
}
}
}
Create a material from this shader, and use it in a Blit() of the OnRenderImage function of a script attached to the camera, just like any regular image effect:
[ExecuteInEditMode]
public class ApplyImageEffect : MonoBehaviour {
public Material mat;
void OnRenderImage(RenderTexture src, RenderTexture dst){
Graphics.Blit(src, dst, mat);
}
}
The behaviour I get (using Unity 5.2.3) from this setup is as follows - the image effect shows the stencil buffer gets created correctly on the first frame after it is activated, but then doesn't get updated again, no matter how much I change the camera or other objects in the scene? (note that I've put [ExecuteInEditMode] on the image effect script, but the exact same problem happens whether I'm in Play mode or Scene mode). (I've also tried placing a full-screen quad at the back of the scene that fills the stencil buffer with "0" just to see if that made a difference but it did not) Disabling and re-enabling the component or the camera itself updates the stencil buffer again. But then, weirdly, resizing the game view window appears to completely clear the buffer - making the screen all black.
Does anyone have any insights or can explain what I'm doing wrong?
I will mention that I've already read and tried suggestions in http://answers.unity3d.com/questions/621279/using-the-stencil-buffer-in-a-post-process.html , but I don't think they apply, since it's obvious my stencil buffer is getting written to at least once.
Hmm, have you read about the deferred rendering pass on the stencil page? Since Image effects are applied after all rendering has finished you might have some interference with Unity's lighting pass(es). Though you don't seem to specify a renderqueue nor do we know what lighting mode you're using.
I only have played around a little bit with stencil operations in Unity so i've never come across such a problem. I've once implemented stencil shadows (Carmack's reverse) but not in Unity but in plain DX in a C++ application (as an assignment back then ^^).
Thanks for the reply @Bunny83 - sorry should have mentioned that I'm using Forward rendering so, to my knowledge, the buffer should not be being interfered with by any lighting calculations.
As I understand, the stencil buffer is stored using 8 bits appended to the depth texture. So I've tried setting Camera.depthTexture$$anonymous$$ode = DepthTexture$$anonymous$$ode.depth to ensure that the depth buffer was being written, and fiddled with 32 bit depth texture on/off, but it made no difference.
Your answer
Follow this Question
Related Questions
Stencil Buffers With SpriteRenderer 1 Answer
Stencil Shaders with depth testing 0 Answers
Help with Stencil shaders please 0 Answers