Viewportframe portal maths

Heya, I’m working on a Portal effect, and I’ve watched a video explaining that I need the camera of the second portal to be relative to the world camera of the first camera. It’s a little bit confusing, but I get what it means (here’s an image)

note: the green camera is the world camera and the yellow camera is the viewportframe camera.
I would like to know the maths that I need to use to get this simple effect.
I’ve tried a couple of combinations of this:

local localspace = portal1.CFrame.Position - Camera.CFrame.Position
localspace = portal2.Position - localspace

but I can never seem to get it to work:image
Any suggestions are appreciated.

1 Like

This is quite a complicated script, and is 800 line script for me
Do you need the whole script or if there is any error tell me that error

Well I need to know the mathematics behind it, so not the whole script but just what calculations need to be performed to get a realistic result.

You can try checking out this open-source demo by the amazing @EgoMoose Teleportation portal demo - Roblox

if you want to transform a vector in one coordinate space (A) to another one in another coordinate space (B) you need to find the basis vectors of coordinate space B then multiply it by the matrix transformation using those basis vectors and the origin of the coordinate space. Since your cameras cframe is technically its own coordinate space (lookat matrix) you just do transformedAToB = B*A