Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Mouton · Jan 15, 2015 at 03:22 PM · shaderstencilglslzdepth

Make a shader that only renders objects behind another object

My question is very similar to this one: http://answers.unity3d.com/questions/590800/how-to-cullrender-to-through-a-window.html

I need the same effect except that I don't want to draw objects before the object.

Here you can see the culprit box: alt text

I tried to set the stencil quad's 'Zwrite' set to 'on', then the culled object's 'ZTest' to 'GEqual'. ZTest is working as excepted, only objects behind the quad are rendered but faces are mixed up.

Here's what I got: alt text

I'm not used to shaders so I don't know what to do now. Any hints ?

stencil-shader.png (149.5 kB)
stencil-shader2.png (105.5 kB)
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 Tsuole · Mar 19, 2016 at 10:04 AM 0
Share

Essentially the question is whether or not you can have a stencil window only work if you're looking THROUGH it, not AT it.

I really hope there is a way around this problem?

avatar image Bunny83 Tsuole · Mar 19, 2016 at 12:01 PM 1
Share

I don't quite get what you mean by that. If you look at the geometry that renders into the stencil buffer you are looking through it as well. For "normal" window masking the stencil geometry itself should do a ztest but should not write to the z buffer itself, only the stencil buffer.

In the shader for the masked objects you can do a stencil test and only render things if they the pixel does have a stencil value greater than 0.

This question however was about something different. He not just wanted a window to look through, but a window that hides everything before the window and only renders things behind it. For that the masking geometry must not do a ztest (otherwise it wouldn't be rendered if something is in front of it)

A usecase could be when viewing a house from the outside. You move the masking geometry into the house so it cuts out a hole into the outer walls and only renders what's inside the house at this hole. So a way for a third person view to look into a house for example.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mouton · Jan 16, 2015 at 03:16 AM

I was able to answer my own question during moderation.

The trick is to change 'Pass keep' with 'Pass invert' in the culled object shader.

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
avatar image
0

Answer by Paulius-Liekis · Jan 15, 2015 at 06:46 PM

I don't think you can achieve it this way. Problem is that in order to render triangles of object in correct order it uses ZTest. But you need inverted ZTest for the trick that you're doing. I don't think you'll be able to combine these two things.

Maybe you can achieve it by using multiple passes and Stencil buffer (http://docs.unity3d.com/Manual/SL-Stencil.html). It's allows you to write and read bit-mask per pixel.

Something like this:

  1. Your window object must be rendered first (make sure it's Queue value in shader is less than in the shader of the cullable object)

  2. Render your cullable objects with multiple passes

  3. In first pass do what you do now and write 1 into Stencil buffer for objects which are rendered

  4. In second pass set ZTest to normal one and check that 1 is written in Stencil buffer

Anyway I'm sure you will have some issues with this, because Unity batches objects with multiple passes in a weird way (at least that was my experience) and I was having problem with this approach, but maybe you can figure something out in this direction.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Use stencil buffer to hide objects with same shader? 1 Answer

Shader Fails to Compile on iOS: GL_EXT_frag_depth 2 Answers

Using stencil buffer as form of zwrite? 0 Answers

Looking for Stencil Buffer examples/tutorials 1 Answer

Showing hidden units - using stencil buffer with standard shader and deferred rendering 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