EDIT: I’m consolidating this thread to keep it short and simple.
THE ISSUE:
Creating a black and white image of a stencil buffer, for later use in a post-processing effect, requires too many blits with CommandBuffer.Blit().
Here’s the summary:
-
If you blit from the screen to a rendertexture, you can’t use a custom shader… or else the resulting render texture is blank. Also, it never copies the stencil to any render texture. I believe both these issues are bugs. (Case 834634)
-
If you blit from a render texture, to another render texture, the stencil can’t be access in the shader. Probably because as I said in #1, stencils aren’t copied to render textures. I believe this is a bug.
-
If you blit from a render texture to the screen, everything works including the stencil (weee!!), but now you just over-written the picture on the screen. You’d then have to fix that, I suppose with another blit to put the picture back again.
-
You can’t blit from screen to screen, or from one render texture to itself, unless your shader specifically uses a global texture (not _MainTex). Otherwise result is blank picture.
-
Any attempt to render the camera to a render texuture will result in no ability to access or use the stencil buffer, due to issue #1 above.
In the end it’s possible but it requires too many blits.
→ first blit is from screen to render texture, and you can’t use a shader, as I said in #1 above.
→ second blit is from that render texture to screen, to make the stencil image, since as I said in #2, you can’t blit from a RT to another RT, or else there is no stencil… but anyway now you just put the black and white picture on your screen so screen is screwed up.
→ third blit is from screen to render texture, again with no shader allowed (see #1 above), just to “save” the stencil image you made. Or else, if you skip this step, how are you gonna use it later?
→ fourth blit is from the original render texture, back to the screen again, with a shader to do some post processing effect. You’d have to set the stencil image to a global property, so that this shader would have it also to work with. This step is required for another reason also: to put the picture back on the screen, since you screwed it up on the second blit.
That’s a minimum of 4 blits just to make (and use) this stencil texture. I’d happily do it if it could be done with just one blit from screen to screen, but can’t (see #4 above).
Acceptable solutions:
A) It should be possible to blit from screen to render texture, and use a custom shader. Currently, this isn’t possible (see point #1 above)
B) Any blits from the screen to a render texture, should copy the stencil buffer to that render texture. Currently, it does not copy it, which probably is what results in point no. “C” below.
C) Any blits from render texture to another render texture should be able to access the stencil. Currently, it is not accessible.
D) It would be nice if it were possible to blit from the screen, to the screen, with a custom shader. This would cut down on the number of blits needed to do many post processing effects.