Skip to content

Instantly share code, notes, and snippets.

@EgoMoose
Created January 29, 2021 00:00
Show Gist options
  • Save EgoMoose/12dd556a363c4951160e9ed798f73ae6 to your computer and use it in GitHub Desktop.
Save EgoMoose/12dd556a363c4951160e9ed798f73ae6 to your computer and use it in GitHub Desktop.
Lua function for geting a surface cframe and size that is aligned to a surface gui
local UNIT_X = Vector3.new(1, 0, 0)
local UNIT_Y = Vector3.new(0, 1, 0)
local SPIN_Y = CFrame.fromEulerAnglesXYZ(0, -math.pi/2, 0)
local function getRotationBetween(u, v, axis)
local dot, uxv = u:Dot(v), u:Cross(v)
if dot < -0.99999 then return CFrame.fromAxisAngle(axis, math.pi) end
return CFrame.new(0, 0, 0, uxv.x, uxv.y, uxv.z, 1 + dot)
end
local function getSurfaceCFrame(part, enum)
local pSize = part.Size
local lnormal = Vector3.FromNormalId(enum)
local lrotation = getRotationBetween(UNIT_X, lnormal, UNIT_Y) * SPIN_Y
local components = {lrotation:GetComponents()}
for i = 4, 12 do
components[i] = math.floor(components[i] + 0.5)
end
lrotation = CFrame.new(unpack(components))
local size = Vector3.new(
math.abs(lrotation.XVector:Dot(pSize)),
math.abs(lrotation.YVector:Dot(pSize)),
math.abs(lrotation.ZVector:Dot(pSize))
)
return part.CFrame * CFrame.new(lnormal * pSize/2) * lrotation, size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment