I’m searching for an efficient solution for rendering portals that reveal part of the scene from a different camera view (think Valve’s Portal). My current solution is to render the full screen from two separate cameras, layer them together with the portal camera beneath the standard camera, then punch a hole in the standard camera to reveal the portal scene.
Since I plan only ever to have one portal on screen at a time this works reasonably well, but it’s terribly inefficient. Even when the portal is taking up only a few pixels of screen space, two screenfuls of content still must be rendered. The obvious solution would be to use the stencil buffer, but Unity doesn’t seem to allow this.
Thanks, I’ll look into that. I think I may fundamentally misunderstand the way render textures work…
I forgot to mention one caveat: the portals are carved out by floating three-dimensional shapes. Do render textures have to wrap around an object, or can they simply render a flat screen-facing 1:1 screen pixel to texture pixel image?
That’s what I was thinking, though I’m uncertain how to get a render texture to match the viewport pixel-for-pixel. Provided that I overcome this hurdle, it seems that I’d ultimately be in the same situation. If the player has a screen resolution of, say, 1600x1200 then it seems that it’d need to be rendering a 2048x2048 texture at all times in order to avoid pixelation or a visible pop in resolution as the player passes through the portal. Do render textures only to record/draw the part of the texture that actually needs to be displayed?
Power of two is only used for texture compression. That’s not happening with a render texture. A render texture turns what a camera renders into a texture. If you only need to render a portion of the screen, then only render the camera there. But if you can model the shape of the portal with a mesh, then it’s not doing you any better to use a render texture versus just layering cameras.
A non-planar approach (like your sphere) could get you into trouble. The “hole” that you render will show objects that are closer than the faces of the mesh. This is relatively easily taken care of with an oblique camera projection, but only when you’re looking at a plane.
That actually brings up another question - is there a way to crop a camera’s viewport without stretching/scaling it? I was initially going to try writing a script that would figure out the mesh’s bounds, convert that to screen space, then clamp down the portal camera’s viewport accordingly.
Sorry about that, I didn’t realize the link you posted was demonstrating this exactly. I really need to work on my linear algebra, it seems that the solution to most of my problems lately has been “use a transformation matrix.”
I can safely say that I understand exactly none of this. But I can see my solution locked away in there. Maddening! I’ll poke and prod and see where it gets me. Thanks for pointing me in the right direction.