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
0
Question by khemistry · Jan 23, 2017 at 01:01 PM · c#shader programmingstencil

Stencil Shaders with depth testing

Hi, I'm building a unity level that takes advantage of Stencil Buffer shaders to mask objects. I have a shader that is puts a value in the stencil buffer, and another shader that only renders when that value is in the stencil buffer. This part is all working.

My problems arise when using multiple masks with differing values. If I have a mask that sets the value to 1 (Mask 1) and a mask that sets the value to 2 (Mask 2), and Mask 2 precedes Mask 1 in the Rendering Order (they use the same shader, so are in the same queue) then any areas of overlap on screen will set the Buffer to 2, even if 1 is in front. This is in part because the stencil buffer shader does not write to the z buffer (which it kinda can't so the the correct geometry can render behind it later). I'm also trying to prevent geometry that only renders when the buffer is set to #value from rendering when the mask that sets the buffer to #value is behind the object. I have two rough ideas for solutions, but lack the knowledge of shader coding to implement them / know if their stupid ideas.

  1. Change the render order of the masks so that the furthest mask from the camera renders first, and the render order moves forward. This would make the overwriting work in my favor. I believe at the moment the render order (within the same point in the queue) is defined by the order of mesh creation? This solution would break down if the masks ever intersection since it would be a per object or per fragment operation but this case won't arise in my project.

  2. Recycle the depth buffer. Have an early pass than runs on all my masks and all my geometry and writes to the depth buffer, then run all my Stencil buffer masks. Then (somehow?) clear the depth buffer, then render all my geometry. This would also mean objects in front of masks would prevent those areas from being effected by the mask. This sounds like it might be very inefficient, and I have no idea how to clear the depth buffer at a specific point in the queue (or at all).

Do either of these ideas sound viable? and can anyone help me out with understanding the pieces of shader code I'm missing? Or am I doing this all wrong and you've got a totally better way of doing it.

Comment
Add comment · Show 2
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
avatar image andrewgotow · Jan 23, 2017 at 03:00 PM 0
Share

Hmm, do you want to know if an object is within mask 2 and/or mask 1, or would you like mask 2 to fully overwrite mask 1, but with depth testing?

avatar image khemistry andrewgotow · Jan 23, 2017 at 11:31 PM 0
Share

I would like the masks to fully overwrite each other, based on depth testing. So 2 will fully overwrite 1 if 2 is closer to the camera.

I could build an additive mask with an incremental stencil shader, like the circles in the documentation I think?

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

In Unity 2D how can I make a shader aware of the Z coordinate (depth) of the object it is about to draw over? 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

My object keeps rendering the object in the back first using stencil buffer 0 Answers


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