Portals
Insipired by the portal series, I challenged myself to create portals.
You can see through portals and they teleport both the player and objects passing through them.

Cameras and players
To sell the illusion, a camera is set up to mimic the players movement relative to both portals, the players relative location and rotation to
portal A needs to replicated in relation to portal B.
This is also how the teleportation works, when the player steps over the "portal line" they are teleported to the same location relative to the other portal, their look rotation is also rotated to fit the new relative portal.
If this is set up correctly, the teleportation should feel seemless.
The camera view is rendered to a texture and displayed by the portal.


Portals should ideally look and work just like a doorway

Topdown look at how the cameras follow the player
It is important that the camera view out of the portal is the same as the players
view in to it


Banana Juice
An issue I encountered with this method of using cameras is that they will render everything, even if its behind the portal, since the camera also is behind it. What we actually want is just the view out of the portal. After researching this issue I found that the devs for portal call this issue "Banana Juice".
The fix for it is quite simple, but requires the cameras to have individual custom clip planes that are centered to the portal its looking out of, and that the normal of the clip plane is aligned with the portal.
.png)
Camera renders everything, even the things behind the portal

Custom clip plane cull everything between the camera and the portal
Objects through portals
In order to teleport objects, and for it to look believable, we need to partially teleport objects pixel by pixel.
Atleast that is the illusion I was aiming to achieve.
In reality, the effect is cheated by spawning a copy of the object to act as the "emerging side" from the portal. When an object comes near a portal, it spawns a copy of itself that mimics its movements relative to each portal, just like the cameras above. When the objects pivot point passes through the portal, it swaps places with the copy. The real
object is responsible for logic and physics, while the copy just mimics the position of the real one.
Visually we need to hide the copy while it is behind the portal. This is similiar to the clip plane from above as well, but on a object by object basis. Each object that can pass through a portal needs a certain shader that is supplied a point in space and a normal. The shader then calculates and masks out the pixels that are not on the front side of the point.

Object going through and being teleported to connected portal

Debug material, showing the mask that hides the copy behind the portal