Portals using Custom Stencil / Custom Depth?

Hey all, I recently found an interesting potential solution to working with portals. The idea is that there are 2 sets of geometry. One that is rendered on one render layer (stencil value of say 0), and this layer is the “main” layer that is generally seen. What I want is to create another set of geometry that is in the same place as the main layer, but is only visible through a stencil portal.

The trouble I’m running into is displaying something in the portal (just a mesh with the material I’ve made) and NOT displaying it in the world. I am able to sort of pull it off, but the parts that are not in the portal remain black.

My question is, is it possible to make multiple layers so that I can mask the main layer and reveal the other layer? I can’t seem to get both working, and only end up masking one or the other, but not both. I know there’s more math I must be missing, I’m just not sure what that must be. Maybe some IF statements to dictate portal vs external rendering?

102954-portal_003.png

I’m working on an approach in which the “main” layer is going to be on say layer 1, and the rest is not layered, so that the main layer can always render atop anything that is not in a layer. I then have a mesh with layer 2 that is cutting out anything on layer 1 to reveal the unlayered meshes behind it.

The remaining question is: How can I render Layer 1 as it should look, rather than as the grey that I’m testing with? Trying to connect the SceneTexture:PostProcessInput0 reveals everything, including the unlayered elements.

This is actually only one of many steps closer to my goal. The interior of the portal is still revealing things meant to remain external.