I'm writing a portal renderer in Unity, for a VR game. The idea is that a complex mesh can be used as a 'window' into an alternate world. I have set things up to render the portals with three ordered shaders currently:
- The first shader renders portals to the stencil buffer.
- The second shader is a modified standard shader, that renders a world within the stencil mask.
- The third shader renders portals to the depth buffer, so that the 'outside' world doesn't render inside the portal in a later pass.
The problem is that objects inside the portal occasionally show up in front of the portal when they shouldn't. I can't think of any way to clip them to the portal easily. If the portals were flat, I could use oblique near-plane clipping. For a complex mesh, though, I thought maybe I could leverage a second depth buffer, and only draw on z-fail for the world behind the portal?
I'm not sure how to go about creating a second depth buffer though, or if there's a better way.
Any thoughts?