- Home /
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:
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:
I'm not used to shaders so I don't know what to do now. Any hints ?
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?
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.
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.
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:
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)
Render your cullable objects with multiple passes
In first pass do what you do now and write 1 into Stencil buffer for objects which are rendered
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.
Your answer
Follow this Question
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