Search Unity

Some questions about drawing portals with stencil buffer

Discussion in 'General Graphics' started by Sharlatan, Jan 15, 2016.

  1. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Hey everyone!

    I'm trying to get familiar with the stencil buffer and since it's often mentioned in the context of portals, I'd though that might be an interesting project to learn the basics.

    I've found this site which gives me a good idea about how to approach the problem in general: http://th0mas.nl/2013/05/19/rendering-recursive-portals-with-opengl/

    I also found this Unity thread/project and some other examples for the stencil shader: http://forum.unity3d.com/threads/unity-4-2-stencils-for-portal-rendering.191890/

    My current problem is that I get how the Unity project linked above was done but it doesn't quiet do what I'd probably need it to do. Like every other Unity stencil buffer example I found, this project only uses one camera. It appears like different rooms are shown, depending from which side you look into the box, but in reality all the objects inside the boxes are placed "on top of each other" and the stencil buffer is only used to mask out the objects that should(n't) be drawn.

    If I analyzed the problem correctly though, I'd have to have multiple cameras to make "real" portals where I see into another room or at least the same room from another perspective. As far as I understand it, I'd roughly have to

    1. Draw the portal's frame from the player perspective (camera 1)
    2. Draw the scene from the portals "out" camera (camera 2) but only on the screenspace where the portal frame in step 1 was drawn (using the stencil buffer)
    3. Draw the whole scene with the player camera (camera 1) but not render the portal itself (only objects that might be in front of it) because it's already been rendered correctly by camera 1 in step 2. I assume I'll again have to use a stencil buffer to do so..?

    But I'm a bit stumped at how this whole "render with multiple cameras in combination with masks" thing works. It's hard to explain where exactly I'm stuck at. It's probably a lack of understanding in general of how to approach this.
    For example.. I assume I'll have to use the "depth" value of the cameras to set the rendering order. But in step 2 (rendering only the portal contents) I will have to draw only on the portal, while in step 3 (rendering the scene from the player perspective) I will have to draw everywhere else except the portal (unless there are objects in front of the portal but I'll worry about that later on). Doesn't this mean I'll have to use the same mask for multiple cameras but mask stuff in/out depending on which camera is currently rendering? How do I do that?
    And step 2 is drawn from another perspective/camera than step 1 but I'd need the portal frame and perspective of step 1 as mask... would I have to do something like this at step 2?

    1. (In OnPreRender()) Duplicate the portal frame mesh from step 1
    2. (In OnPreRender()) Use a translation matrix to move the duplicate so it takes up the exact same screen space as the original did when the portal was originally rendered
    3. Render the scene with camera 2, using the portal frame copy as mask
    4. (In OnPostRender()) delete the duplicate.
    Am I totally off track here or does my approach sound at least somewhat reasonable?

    And if portals would be positioned facing each other, I'd have to do this all recursive tho get the "mirror in mirror" effect. If I got it right, this would imply a camera renders multiple times for one frame. Is that even possible? Or would I have to stack multiple cameras with identical settings -except depth-?

    I hope my question make sense to at least some degree. I'll be happy to answer any question you might have if something's not clear.

    Thanks a lot !