I am attempting to duplicate the Player’s ScreenGui onto a plane with a SurfaceGui X distance from the camera.
The plane has the same AbsoluteSize as the ScreenGui, with the topbars inset taken into consideration.
This is how we should look - Black text being part of the ScreenGui, and Yellow being part of the SurfaceGui.
So we work at position 0,0. Great! However, once we move the position of our text object;
Uh oh! Suddenly we aren’t in the same position anymore!
Ideally this would only deal with centered UI elements, but I’d like to know if there was a way of using non-centered ones too.
tl;dr I’m trying to clone a ScreenGUI to a SurfaceGUI at the distance X, while maintaining near-perfect replication on size/position so they’re indistinguishable.
Here is my code
local Player = game.Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local RunService = game:GetService("RunService")
local GUISERV = game:GetService("GuiService")
local TOPBARINSET = GUISERV:GetGuiInset().Y
local FrustumPart = Instance.new("Part")
FrustumPart.Name = "AAAA1"
FrustumPart.Anchored = true
FrustumPart.CanCollide = false
FrustumPart.Transparency = .3
FrustumPart.Size = Vector3.new(.1, .1, 0)
FrustumPart.Parent = workspace
local FrustumCloning = Instance.new("ScreenGui")
FrustumCloning.Parent = Player.PlayerGui
local SurfaceClone = Instance.new("SurfaceGui")
SurfaceClone.SizingMode = "FixedSize"
SurfaceClone.Parent = FrustumPart
SurfaceClone.Face = "Back"
local ScreenActual = Instance.new("Frame")
ScreenActual.Parent = SurfaceClone
ScreenActual.Position = UDim2.new(0, 0, 0, TOPBARINSET/2)
ScreenActual.BackgroundTransparency = 1
ScreenActual.Size = UDim2.new(1, 0, 1, 0)
local Texty = Instance.new("TextLabel")
Texty.Parent = FrustumCloning
Texty.Text = "TRYING SOMETHING SOMETHING"
Texty.Size = UDim2.new(1,0,1,0)
Texty.BackgroundTransparency = 1
Texty.Position = UDim2.new(.2, 0, .2, 0)
local Clone = Texty:Clone()
Clone.Parent = ScreenActual
Clone.TextColor3 = Color3.fromRGB(255, 217, 0)
function CalculateFrustumAtDistance(distance)
FOV = Camera.FieldOfView
NewPosition = Camera.CFrame * CFrame.new(0, 0, -(distance + FrustumPart.Size.Z/2) )
--Apply NewPosition to FrustumPart
FrustumPart.CFrame = NewPosition
--Find New Size
local HeightAttempt = 2 * distance * (math.tan(math.rad(FOV * .5)))
local WindowSize = FrustumCloning.AbsoluteSize
local Ratio = (WindowSize.Y + TOPBARINSET) / WindowSize.X
local WidthAttempt = HeightAttempt / Ratio
FrustumPart.Size = Vector3.new(WidthAttempt, HeightAttempt, FrustumPart.Size.Z)
SurfaceClone.CanvasSize = Vector2.new(WindowSize.X, WindowSize.Y + TOPBARINSET)
end