having a camera render an object only if it appears after a plane

Hi,

I am attempting to make a portal effect using render textures and a secondary camera and I’m running into a bit of an issue. Essentially, I have it all set up where the second camera matches the main camera’s position relative to the portal object, which is then rendered onto a plane and such. Fairly simple stuff right now. The problem is when an object may exist between the portal exit and secondary camera, it’s rendered onto the portal plane, breaking the illusion. I’m still new to the whole shader stuff, so forgive me if this is something super easy, but what I was wondering is if there’s a way to have a camera not render any objects that would appear between the portal exit and itself?

Essentially having a sort of distance check to each thing the camera renders, or maybe even checking if it’s within the viewing window of the portal exit and the camera? I think I’ve seen stencil shaders do a similar thing, but they apply to objects themselves as opposed to a camera which seems to be a bit better for my situation considering the two cameras should renderer things a bit differently.

Thanks for any help!

One thing you could certainly do is to test the world position of fragments against that plane in your shaders and use the clip() function to omit them if they fail it. But this would need a modification of all the shaders you are using. If you go that route I guess you could then use the OnPreRender callback to distinctively set those plane parameters for all you shaders per camera using the Shader.SetGlobalXY() methods.