- Home /
Question by
RexBaribal · Nov 25, 2015 at 09:00 PM ·
shaderrendertextureimage effectsstencil
RenderTexture clears after Blit
Hi, I'm trying to render my scenes stencilbuffer into a texture so i can use it for an image effect shader.
However, the texture i render to is always white but when i render it to the screen instead it renders correctly.
here is the Code:
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (PostprocessMaterial == null)
{
Graphics.Blit(source, destination);
return;
}
RenderTexture temp = RenderTexture.GetTemporary(source.width, source.height, 0);
Graphics.Blit(source, temp, StencilMask);
PostprocessMaterial.SetTexture("_OutlineTex", temp);
Graphics.Blit(source, destination, PostprocessMaterial);
RenderTexture.active = null;RenderTexture.ReleaseTemporary(temp);
}
and the shader:
Shader "Hidden/RenderStencilMask"
{
SubShader
{
Pass
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 frag(v2f_img i) : SV_Target
{
return fixed4(0,0,0,0);
}
ENDCG
}
Pass
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Stencil
{
Ref 255
Comp equal
}
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 frag(v2f_img i) : SV_Target
{
return fixed4(1,1,1,1);
}
ENDCG
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612050356im_/https://answers.unity.com/themes/thub/images/avi.jpg)