Overview
The portal system I built is inspired by Portal 2 and aims to recreate that iconic mechanic where two linked surfaces act as seamless gateways. The goal was to combine clever rendering techniques and physics tricks to make portals feel both visually convincing and fun to play with. From the shader-based rendering and recursive visuals to momentum preservation and smooth transitions, the system brings together multiple technical pieces to deliver the magic of stepping through one world and instantly emerging in another.
Steps
The very first step was getting the portal to actually look like a portal. I used an HLSL shader to project the view from the exit portal’s camera right onto the entrance surface, basically turning it into window to another place. To make sure that view only shows up inside the exact shape of the portal (and not all over the screen), I relied on the stencil buffer as a neat little mask.
The second step was all about making the portals feel right when you jump through them. Just like in Portal 2, I had to carry over the player’s momentum so the gameplay stays smooth and satisfying. The trick was to take the velocity vector when entering a portal, then transform it to match the orientation of the exit portal. That way, launching yourself into a portal at high speed shoots you out the other side with the same energy, no matter the angle. It’s basically portal-powered physics — and it makes every jump feel super dynamic!
The third step was tackling the mind-bending part: portal recursion. This is what happens when you point one portal at the other and suddenly see an endless tunnel of worlds inside worlds. To pull this off, I rendered the view from the exit portal’s camera, then recursively re-projected it back through the entrance, repeating the process a few times. Each pass creates a smaller “portal within a portal” effect. Of course, I had to limit the recursion depth so the GPU wouldn’t melt, but even with just a handful of layers, you get that trippy, infinite hallway look that feels really perfect.
The fourth step was all about polishing the portal transition. At first, going through a portal felt a bit janky — like a mini teleport glitch where you’d still see the entrance portal’s view for a split second. It totally broke the illusion! This hiccup probably came from Unity’s rendering pipeline, and there are a few ways to patch it. My fix was to place a simple box behind the portal, perfectly matching its surface, and render the exit camera’s view onto it. That little trick smooths out the handoff between portals, making the transition feel seamless and way more natural.
Special Thanks
Huge thanks to the authors whose articles inspired this portal system:Daniel Ilett – Intro to Portals and Thomas – Rendering Recursive Portals with OpenGL.