Asked in ‘answer’ forum but they suggested reposting here:
I need to render a bunch of objects but ‘mask’ them off if they are behind a ‘window’. That is, I need a rectangular ‘hole’ through which objects behind it can be seen as if through a window, objects in front are rendered normally. Like if I had a ‘wall’ but the wall itself is transparent. I hope that made sense?
Reference: How to cull/render to through a 'window'? - Questions & Answers - Unity Discussions
Note that the ‘window’ may be a quad or a hole in a quad, which ever will work best.
I think what you’re asking is pretty straightforward. Just render an invisible wall where you want it to be, but make it write to the Zbuffer. Here’s something I put together:
The important parts of the “Geometry-1”. This makes sure the invisible wall renders before all the solid geometry; if it’s mixed in with the regular geometry, there is no guarantee in what order it will be drawn with respect to all other geometry.
“Zwrite On” is where the magic happens. This will write the depth of the object to the z-buffer. When the real geometry is rendered, it will test the depth against this value, and everything farther away will not be drawn.
“ColorMask 0” is also important. This masks the RGBA output of the shader so nothing gets visibly rendered to the screen. Without this the quad will probably come out as black. Or white. Or possibly some other solid color. I didn’t try it.
The color property is included because it seems like the shader doesn’t want to compile without it. It’s not used at all.
I fixed it by creating a dummy camera (culling mask set to “Nothing”) with a lower depth that the main camera. Clear the dummy camera to “Skybox” and set the main camera to “Don’t Clear”.
Many thanks. Have not tried yet (just got home) but thinking about this some more, may be more complex than this. Consider a glancing view (the window will look like a rhombus I guess). Now, if something is ‘in front of the wall’ but closer to the camera than the window, it will render it in view, right? Part of this can be solved by setting the near clipping plane to the nearest world-point of the window. I thought maybe setting the far clipping plane to the far point of the window, but that’s not good, as one may see ‘infinitely’ far through the window. So what I (also?) need is a way to clip to the frustum formed from the camera through the window, if that made sense.
I’m not quite sure yet what you’re trying to accomplish, but I was reading your unity answers question a bit more. You are trying to make a kind of dimensional window effect?
That makes it a bit more complicated. It will be a similar technique, but you’ll need two cameras. First camera renders the wall. Second camera first renders the “window” (by using a shader like the one I posted that essentially clears the depth buffer by writing a large value to it), and then renders your regular objects. Since you “punched” a hole in the depth buffer, the “window” part of the wall will be over-written by any objects at that location whether it is far or near.
Maybe closer to what you’re trying to do?
If you posted a couple screenshots of what you’ve tried pointing out specific failure cases (what is or isn’t visible when it should or shouldn’t be), then maybe someone can suggest a more exact solution.
Added screenshot in original post. The sphere is just ‘behind’ the window. The blue cylinder penetrates the wall, and only part of it should be seen. The white cylinder is in front of the wall/window and is completely seen.
After trying these shaders, I’m pretty sure what I really need is a something like a Stencil Buffer, unless your 2-camera idea would accomplish this? I just want to render what the camera sees through the window, and object that are completely in front of the window (not penetrating the wall) (unless part of them can be seen through the window)
This is what I’m seeing. The boat and water protrudes through the wall. I don’t want to see that part, just what’s through the window (outlined in green for illustration).
I know it’s been a couple months, but I did go with these two shaders (ZMask and TestCull) for a while, both seem to do the same thing. And yes, both look fine in the Editor Scene window, but the Game (and running) windows still cull out the geometry which is IN FRONT of the shader, as stated. Trying the ‘dummy camera’ did not work for me. Might this be because I’m rendering to texture?