-
-
Save cxmeel/5e03ef47d6c38caa60ccf67d605d9184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--!strict | |
local RunService = game:GetService("RunService") | |
local Camera = workspace.CurrentCamera | |
local Module = {} | |
Module.Position = UDim2.new(0, 0, 0, 0) | |
Module.Size = UDim2.new(1, 0, 1, 0) | |
local function UDim2Absolute(udim2: UDim2) | |
local viewSize = Camera.ViewportSize | |
return Vector2.new( | |
(udim2.X.Scale * viewSize.X) + udim2.X.Offset, | |
(udim2.Y.Scale * viewSize.Y) + udim2.Y.Offset | |
) | |
end | |
--- Compute offset | |
function Module._computePosition() | |
local viewSize = Camera.ViewportSize | |
local aspectRatio = viewSize.X / viewSize.Y | |
local offset = UDim2Absolute(Module.Position) - (viewSize / 2) | |
local position = offset / viewSize | |
-- Taken from ScreenSpace | |
local hFactor = math.tan(math.rad(Camera.FieldOfView) / 2) | |
local wFactor = hFactor * aspectRatio | |
return -position.X * wFactor * 2, position.Y * hFactor * 2 | |
end | |
function Module._computeSize() | |
local size = UDim2Absolute(Module.Size) / Camera.ViewportSize | |
return size.X, size.Y | |
end | |
function Module._getOffset() | |
local x, y = Module._computePosition() | |
local w, h = Module._computeSize() | |
return CFrame.new( | |
0, 0, 0, | |
w, 0, 0, | |
0, h, 0, | |
x, y, 1 | |
) | |
end | |
--- Handler | |
function Module.Start() | |
RunService:BindToRenderStep("ViewportResizer", Enum.RenderPriority.Camera.Value + 1, function() | |
Camera.CFrame = Camera.CFrame * Module._getOffset() | |
end) | |
end | |
function Module.Stop() | |
RunService:UnbindFromRenderStep("ViewportResizer") | |
end | |
return Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment