First of all, I would not recommend just setting the reference value to 128. Instead you should keep the reference value to the default value of 255 and set the ReadMask and WriteMask to 128. This will give you an actual check on a single bit.
In the current case you are writing and checking 10000000 binary. In the latter case you are writing and checking 1xxxxxxx, where the x’s are ignored.
Unity probably also uses the stencil buffer here and there and you will need to try and coexist with that. So now that you are actually only looking at a single bit instead of all eight, you can try each bit to see whether that works. (1, 2, 4, 8, 16, 32, 64 and 128.)
Hi jvo3dc, thanks for your reply and recommendations.
I’ve already checked each bit and result is the same.
Today I was able to achieve same result with command buffer in unity 5.6.0f3.
But currently I’m facing new problem cause this effect is used to detect if object is visible through opaque enviroment by reading pixels to a texture and checking their color. After pixels are read I need to return to image without any effects and I have no idea on how to achieve this kind of functionality using command buffer (I’m new to it).
So currently I need to understand if its a bug in unity that will be fixed in next versions or they’ve changed the API and added more steps for this kind of functionality in OnRenderImage or maybe completely removed it.
It could well be a “bug”. On the other hand, it’s not really clear which parts of the stencil buffer are used when by Unity itself and which are free for us to use.
If you can get in between before it is cleared using a CommandBuffer you would have a good workaround, but they indeed require some getting used to.
Found something interesting. If MSAA is turned ON on the rendering camera you’ll get black texture. From my point of view this functionality isn’t connected with MSAA. So its very odd. And now I need to solve this some how
Well, MSAA does also need to be applied to the depth/stencil buffer besides the color target. So at 4x MSAA, the depth/stencil buffer should also be able to store 4 samples per pixel.