Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Prinsessa · Jun 12, 2018 at 03:43 PM · renderinggraphicsglobalstencil

Set global stencil test regardless of shader (command buffer?)

Hey! I need to redraw the same scene a couple of times with a different stencil mask clipping everything in the scene each time. All directly to screen on top the previous render; no render textures or post effects or any such thing involved. I would like to set up a global stencil test to apply to absolutely everything before each render.


A few options that you might have in mind that I cannot use and why:


  • Custom pipeline: This is the only solution for which I've actually found a way in the documentation to directly set the stencil state, but I don't have the time, experience or desire to go with this when all I want to do is hook into the standard pipeline and do one little thing, and I think we're stuck on too old a version of Unity anyway.


  • Shaders: I know that I can set up a stencil test in a shader, but I'm new on a project that's already full of shaders, both regular and from ShaderForge, as well as ones that belong to various plugins such as MegaSplat, so I simply don't have the level of control over the shaders used this project that I can just go in and edit all of them anyway. Thus I cannot use shader global variables, because that would entail modifying every shader to add a stencil test that reads it. I also cannot use replacement shaders because then I would have to reimplement every shader anyway.


  • Post effect: Some people seem to have misunderstood my question and suggested various things related to this. Not sure what's wrong with my wording, but this has nothing to do with those, sorry. :p


If I was working on a pure OpenGL project I would just drop glStencilFunc() and glStencilOp() in there right before rendering the scene, so that's basically how I would like to solve this in Unity too, if at all possible, whether that be by going through the camera's OnPreRender() and OnPostRender() using some entry point in the Unity C# API to access the corresponding functions, or if I can somehow use command buffers to hook in there, but I haven't find any such option for those in the documentation…


Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–Ľ
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zarenityx · Jul 11, 2019 at 05:50 PM

To my knowledge, Unity doesn't allow this. There are numerous workarounds, although without editing any shaders the options for using the stencil buffer are rather limited. Unfortunately, these workarounds really aren't using the stencil buffer, but can be used in some cases to achieve a similar effect. Some of the workarounds I can think of:

Multiple Cameras
This is the quick, ugly solution, and has some severe limitations, but is the easiest to pull off. It's the same trick you might use when making a first person game to keep hands/weapons from clipping through walls. Just render the same scene multiple times from multiple cameras with different culling masks, where parts of the scene have different layers.
Pros:
Easy to setup and extend
Requires no additional shaders
Cons
Only works on a per-object level
May require clever level design to mask the many edge cases that break the illusion


Using the Depth Buffer
While not the exact same thing as the stencil buffer, these two buffers often perform similar roles. If you just want to mask off certain areas, you can take advantage of ZTest. This requires writing a single shader. Using the SV_Depth frag output semantic, you can set a custom depth. Writing a shader that renders after everything else, writes nothing to the color buffer (you probably have to use blending rather than discard/clip), and writes 1 into the depth buffer, essentially clears depth locally wherever that object is rendered. Then, when you do the next render, don't clear the depth buffer. Then, in the spaces where the depth buffer was overwritten, the new objects will always pass ZTest.
Pros:
Provides per-pixel masking
Can be used a bit more creatively/artistically
Cons:
Requires writing a shader
Doesn't allow super complicated operations like stencil does. Can only be used for masking.


I apologize for the fact that both of these workarounds have serious disadvantages. The fact is, without using a RenderTexture and PostFX to implement your own version of stencil, you can't really get all the functionality of stencil buffers. On the other hand, you can mix these two workarounds together to get effects that aren't even easily feasible with stencil. I've used the depth buffer trick to make nice portal effects in the past without needing a bunch of RTs (although it would require rendering multiple times to the same RT, which happened to pretty much just be the screen).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–Ľ
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using stencil buffer with OnRenderImage()/Blit 0 Answers

Custom bit size for Stencil buffer 0 Answers

Cutting hole in water plane using stencil buffer.Help making it visible from both sides. 0 Answers

Graphics.DrawTexture() not displaying in HDRP 0 Answers

How do I draw lines on the edges of meshes? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges