Hi, I'm looking to make some adjustments to this Portal code - At the moment, it works by displaying the Parts behind a single Portal.
I'm looking to make it work with two Portals, so looking into Portal1 will show Portal2. How could I do this by editing the code below as changing the Values to accept Portal2 causes the portal to glitch for me.
local RunService = game:GetService("RunService")
local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local UNIT_Y = Vector3.new(0,1,0)
local UNIT_ZM = Vector3.new(0,0,-1)
local XZ = Vector3.new(1,0,1)
local YZ = Vector3.new(0,1,1)
local camera = game.Workspace.CurrentCamera
-- Setup viewport frame and camera
local surfaceGui = PlayerGui:WaitForChild("SurfaceGui")
local vpf = surfaceGui:WaitForChild("ViewportFrame")
local vpfCam = camera:Clone()
vpfCam.Parent = surfaceGui
vpf.CurrentCamera = vpfCam
-- Folder of portal parts we don't copy into the VPF
local portalFolder = game.Workspace:WaitForChild("Portal_NO_COPY")
-- Portal location, corners, edge centers, face center
local portal = portalFolder:WaitForChild("Portal")
local portal2 = portalFolder:WaitForChild("Portal2")
local tc = portal.CFrame.p + 0.5 * portal.Size * YZ
local bc = portal.CFrame.p + 0.5 * portal.Size * Vector3.new(0,-1,1)
local p = portal.CFrame.p - portal.Size.Z * 0.5 * portal.CFrame.LookVector -- Portal face center, not part center (SurfaceGui is on Back face)
-- Unoptimized render loop. Much redundant calcs, wow.
RunService.RenderStepped:Connect(function(dt)
local camCF = camera.CFrame
local c2p = p - camCF.p
local c2pXZ = (c2p * XZ)
local c2pYZ = (c2p * YZ)
local levelCamCF = CFrame.fromMatrix(camCF.p, camCF.RightVector, Vector3.new(0,1,0), camCF.RightVector:Cross(Vector3.new(0,1,0)))
local csbc = levelCamCF:Inverse() * bc
local cstc = levelCamCF:Inverse() * tc
local v1 = (csbc*YZ).Unit
local v2 = (cstc*YZ).Unit
local alpha = math.sign(v1.Y) * math.acos( v1:Dot(UNIT_ZM))
local beta = math.sign(v2.Y) * math.acos( v2:Dot(UNIT_ZM))
local fh = 2.0 * math.tan(math.rad(camera.FieldOfView) * 0.5)
local hPrime = math.tan(beta) - math.tan(alpha)
local refHeight = hPrime / fh
local dpX = c2pXZ.Unit:Dot(-portal2.CFrame.LookVector)
local camXZ = (camCF.LookVector * XZ)
local scale = math.abs(camXZ.Unit:Dot(c2pXZ.Unit)) / (math.abs(portal.CFrame.LookVector:Dot(c2pXZ.Unit)))
local tanarccos = math.sqrt(1-dpX*dpX)/dpX
local sc = scale * refHeight
local w = 1
local h = 1
local dx = math.sign(c2p.X * c2p.Z)*tanarccos
local dy = -c2pYZ.Y/c2pYZ.Z
local d = sc
-- Camera CFrame won't take values too much larger than 1, so normalizing the unprojection matrix by the largest component
local max = math.max(w,h,math.abs(dx),math.abs(dy),d)
w, h, dx, dy, d = w / max, h / max, dx / max, dy / max, d / max
vpfCam.CFrame = CFrame.new(0,0,0, w, 0, 0, 0, h, 0, dx, dy, d) + camCF.p
end)
