Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Forked from Reselim/distort.lua
Created January 22, 2024 15:44
Show Gist options
  • Save cxmeel/5e03ef47d6c38caa60ccf67d605d9184 to your computer and use it in GitHub Desktop.
Save cxmeel/5e03ef47d6c38caa60ccf67d605d9184 to your computer and use it in GitHub Desktop.
--!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