How to render an object to the stencil buffer #168
-
Hi! How can I render an object to the stencil buffer? Do I need to change some parameters in the pipeline state to start writing to the stencil? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 5 replies
-
You need to enable stencil and set the necessary stencil properties for the front and back faces. |
Beta Was this translation helpful? Give feedback.
-
There is also a depth test. You may need to disable it |
Beta Was this translation helpful? Give feedback.
-
Which stencil reference value do you write? You need to set it with SetStencilRef |
Beta Was this translation helpful? Give feedback.
-
You can find the information on stencil operation on the Web. That one seems to be reasonable. Stencil works similarly in all graphics APIs. |
Beta Was this translation helpful? Give feedback.
-
I don't know if it's a bug or not, but I have done a lot of trial and error, but it doesn't work. Finally, I tried to hide the object completely by setting stencil comparison function to NEVER but it still doesn't work.
And before drawing the mesh I specified the stencil reference value:
Tested on Mac (MoltenVk) and Windows (Direct3D11) If you want, I can share with you my demo project in which the stencil doesn't work, and help to compile it as well. What if it really is a bug? |
Beta Was this translation helpful? Give feedback.
-
What are you trying to do with stencil? Stencil testing is used in atmospheric scattering effect in particular and everything works properly. |
Beta Was this translation helpful? Give feedback.
-
First of all, you will need to use a depth buffer with stencil (e.g. D32F_S8 or D24U_S8). On the other hand, you don't really need stencil buffer at all to make sure primitives do not overlap. Simply use regular depth buffer - use depth function LESS and write some fixed depth from your shader (e.g. 0.5 or 0.0). This way every pixel will only be written by a single primitive |
Beta Was this translation helpful? Give feedback.
You need to enable stencil and set the necessary stencil properties for the front and back faces.