Port DrawGrid/DrawGridCustom*, DrawCubes, DrawAxes, ViewManipulate; add Roblox helpers, example, README

This commit is contained in:
4DBug committed 2026-10-17 00:59:07 -05:00
1 parent 8a3ad019f9
commit 625fd44c73
4 files changed
+717 -4

No files matched your search

+546 -1
View File
@@ -585,7 +585,6 @@ return function(G)
end
end
-- [Port] ViewManipulateState / mViewManipulateStates not ported yet (ViewManipulate is TODO)
local gContext = {
mDrawList = nil,
mStyle = Style(),
@@ -683,6 +682,9 @@ return function(G)
mEditingID = -1,
mOperation = 0,
-- per-id ViewManipulate widget states (see ViewManipulateState)
mViewManipulateStates = {}, -- [Port] map id -> state instead of ImVector + linear search
mAllowAxisFlip = true,
mGizmoSizeClipSpace = 0.1,
}
@@ -2413,6 +2415,549 @@ return function(G)
gContext.mGizmoSizeClipSpace = value
end
do -- [Port] scoped to keep the factory under the 200-locals limit
local function GetViewManipulateState()
local id = GetCurrentID()
local state = gContext.mViewManipulateStates[id]
if state == nil then
state = { mID = id, mIsDragging = false, mIsClicking = false, mInterpolationFrames = 0,
mInterpolationUp = makeVect(0, 0), mInterpolationDir = makeVect(0, 0) }
gContext.mViewManipulateStates[id] = state
end
return state
end
-- [Port] frustum is a 1-based array of 6 vec_t; clip is a 1-based m16
local function ComputeFrustumPlanes(clip)
local c = clip
local frustum = {
makeVect(c[4] - c[1], c[8] - c[5], c[12] - c[9], c[16] - c[13]),
makeVect(c[4] + c[1], c[8] + c[5], c[12] + c[9], c[16] + c[13]),
makeVect(c[4] + c[2], c[8] + c[6], c[12] + c[10], c[16] + c[14]),
makeVect(c[4] - c[2], c[8] - c[6], c[12] - c[10], c[16] - c[14]),
makeVect(c[4] - c[3], c[8] - c[7], c[12] - c[11], c[16] - c[15]),
makeVect(c[4] + c[3], c[8] + c[7], c[12] + c[11], c[16] + c[15]),
}
for i = 1, 6 do
frustum[i]:Normalize()
end
return frustum
end
-- [Port] matrices: flat 1-based array of matrixCount * 16 numbers (or an array of 16-arrays)
local function GetMatrixAt(matrices, i)
if type(matrices[1]) == "table" then
return matrix_t.new(matrices[i + 1])
end
local m = matrix_t.new()
for k = 1, 16 do m[k] = matrices[i * 16 + k] end
return m
end
function ImGuizmo.DrawAxes(view, projection, matrices, matrixCount)
local viewM = matrix_t.new(view)
local projM = matrix_t.new(projection)
local viewProj = viewM * projM
-- build frustum
local frustum = ComputeFrustumPlanes(viewProj)
local axes = {
{ dir = makeVect(1, 0, 0), color = IM_COL32(255, 0, 0, 255) }, -- X - red
{ dir = makeVect(0, 1, 0), color = IM_COL32(0, 255, 0, 255) }, -- Y - green
{ dir = makeVect(0, 0, 1), color = IM_COL32(0, 0, 255, 255) }, -- Z - blue
}
for i = 0, matrixCount - 1 do
local model = GetMatrixAt(matrices, i)
local mvp = model * viewProj
-- world-space origin
local origin = makeVect(0, 0)
origin:TransformPoint(makeVect(0, 0, 0), model)
for a = 1, 3 do
local endLocal = axes[a].dir
-- world-space endpoint
local _end = makeVect(0, 0)
_end:TransformPoint(endLocal, model)
-- frustum test
local visible = true
for f = 1, 6 do
local d0 = DistanceToPlane(origin, frustum[f])
local d1 = DistanceToPlane(_end, frustum[f])
-- both outside - reject
if d0 < 0 and d1 < 0 then
visible = false
break
end
end
if visible then
-- project to screen
local p0 = worldToPos(makeVect(0, 0, 0), mvp)
local p1 = worldToPos(endLocal, mvp)
-- reject behind camera (clip space)
local clip0, clip1 = makeVect(0, 0), makeVect(0, 0)
clip0:TransformPoint(makeVect(0, 0, 0), mvp)
clip1:TransformPoint(endLocal, mvp)
if not (clip0.w <= 0 and clip1.w <= 0) then
-- draw
gContext.mDrawList:AddLine(p0, p1, axes[a].color, 2.0)
end
end
end
end
end
function ImGuizmo.DrawCubes(view, projection, matrices, matrixCount)
local viewInverse = matrix_t.new()
viewInverse:Inverse(matrix_t.new(view))
local faces = {} -- [Port] array of { z, faceCoordsScreen, color } instead of _malloca
local viewProjection = matrix_t.new(view) * matrix_t.new(projection)
local frustum = ComputeFrustumPlanes(viewProjection)
for cube = 0, matrixCount - 1 do
local matrix = GetMatrixAt(matrices, cube)
local res = matrix * viewProjection
for iFace = 0, 5 do
local normalIndex = (iFace % 3)
local perpXIndex = (normalIndex + 1) % 3
local perpYIndex = (normalIndex + 2) % 3
local invert = 1
if iFace > 2 then invert = -1 end
local dn, dx, dy = directionUnaryV[normalIndex], directionUnaryV[perpXIndex], directionUnaryV[perpYIndex]
local faceCoords = { dn + dx + dy, dn + dx - dy, dn - dx - dy, dn - dx + dy }
-- clipping
-- [Port] upstream's commented-out camSpacePosition skip omitted
local centerPositionVP = makeVect(0, 0)
centerPositionVP:TransformPoint(dn * 0.5 * invert, res)
local inFrustum = true
for iFrustum = 1, 6 do
local plane = frustum[iFrustum]
local allOutside = true
for iCoord = 1, 4 do
local worldPos = makeVect(0, 0)
worldPos:TransformPoint(faceCoords[iCoord] * 0.5 * invert, matrix)
if DistanceToPlane(worldPos, plane) >= 0 then
allOutside = false
break
end
end
-- [Port] upstream `continue`s here without clearing inFrustum (no culling); kept as-is
end
if inFrustum then
-- 3D->2D
local faceCoordsScreen = {}
for iCoord = 1, 4 do
faceCoordsScreen[iCoord] = worldToPos(faceCoords[iCoord] * 0.5 * invert, res)
end
local directionColor = GetColorU32(COLOR.DIRECTION_X + normalIndex)
table.insert(faces, {
z = centerPositionVP.z / centerPositionVP.w,
faceCoordsScreen = faceCoordsScreen,
color = bor(directionColor, IM_COL32(0x80, 0x80, 0x80, 0)),
})
end
end
end
table.sort(faces, function(a, b) return a.z > b.z end)
-- draw face with lighter color
for iFace = 1, #faces do
local cubeFace = faces[iFace]
gContext.mDrawList:AddConvexPolyFilled(cubeFace.faceCoordsScreen, 4, cubeFace.color)
end
end
-- [Port] clips ptA/ptB against the frustum in place; returns visible (shared by DrawGrid/DrawGridCustomColor)
local function ClipGridLine(frustum, ptA, ptB)
for i = 1, 6 do
local dA = DistanceToPlane(ptA, frustum[i])
local dB = DistanceToPlane(ptB, frustum[i])
if dA < 0 and dB < 0 then
return false
end
if not (dA > 0 and dB > 0) then
if dA < 0 then
local len = abs(dA - dB)
local t = abs(dA) / len
ptA:Lerp(ptB, t)
end
if dB < 0 then
local len = abs(dB - dA)
local t = abs(dB) / len
ptB:Lerp(ptA, t)
end
end
end
return true
end
local function GridLine(dir, f, gridSize)
if dir == 1 then
return makeVect(-gridSize, 0, f), makeVect(gridSize, 0, f)
end
return makeVect(f, 0, -gridSize), makeVect(f, 0, gridSize)
end
function ImGuizmo.DrawGrid(view, projection, matrix, gridSize)
local viewProjection = matrix_t.new(view) * matrix_t.new(projection)
local frustum = ComputeFrustumPlanes(viewProjection)
local res = matrix_t.new(matrix) * viewProjection
local f = -gridSize
while f <= gridSize do
for dir = 0, 1 do
local ptA, ptB = GridLine(dir, f, gridSize)
if ClipGridLine(frustum, ptA, ptB) then
local col = IM_COL32(0x80, 0x80, 0x80, 0xFF)
local thickness = 1.0
if math.fmod(abs(f), 10) < FLT_EPSILON then
col = IM_COL32(0x90, 0x90, 0x90, 0xFF)
thickness = 1.5
end
if abs(f) < FLT_EPSILON then
col = IM_COL32(0x40, 0x40, 0x40, 0xFF)
thickness = 2.3
end
gContext.mDrawList:AddLine(worldToPos(ptA, res), worldToPos(ptB, res), col, thickness)
end
end
f += 1
end
end
function ImGuizmo.DrawGridCustomColor(view, projection, matrix, gridSize, majorStep, subdivision, majorCol, minorCol, centerCol)
-- Must have at least 1 subdivision
IM_ASSERT(subdivision > 0, "At least 1 segment required!")
local viewProjection = matrix_t.new(view) * matrix_t.new(projection)
local frustum = ComputeFrustumPlanes(viewProjection)
local res = matrix_t.new(matrix) * viewProjection
local minorStep = majorStep / subdivision
local lineCount = math.ceil(gridSize / minorStep)
for i = -lineCount, lineCount do
local f = i * minorStep
local isMajor = math.fmod(i, subdivision) == 0 -- [Port] C++ % truncates like fmod
local isCenter = (i == 0)
-- Styling
local col = minorCol
local thickness = 1.0
if isMajor then col = majorCol; thickness = 1.5 end
if isCenter then col = centerCol; thickness = 2.3 end
for dir = 0, 1 do
local ptA, ptB = GridLine(dir, f, gridSize)
if ClipGridLine(frustum, ptA, ptB) then
gContext.mDrawList:AddLine(worldToPos(ptA, res), worldToPos(ptB, res), col, thickness)
end
end
end
end
function ImGuizmo.DrawGridCustom(view, projection, matrix, gridSize, majorStep, subdivision)
local majorCol = IM_COL32(0x80, 0x80, 0x80, 0xFF)
local minorCol = IM_COL32(0x90, 0x90, 0x90, 0xFF)
local centerCol = IM_COL32(0x40, 0x40, 0x40, 0xFF)
ImGuizmo.DrawGridCustomColor(view, projection, matrix, gridSize, majorStep, subdivision, majorCol, minorCol, centerCol)
end
-- panels
local panelPosition = { ImVec2(0.75, 0.75), ImVec2(0.25, 0.75), ImVec2(0, 0.75),
ImVec2(0.75, 0.25), ImVec2(0.25, 0.25), ImVec2(0, 0.25),
ImVec2(0.75, 0), ImVec2(0.25, 0), ImVec2(0, 0) }
local panelSize = { ImVec2(0.25, 0.25), ImVec2(0.5, 0.25), ImVec2(0.25, 0.25),
ImVec2(0.25, 0.5), ImVec2(0.5, 0.5), ImVec2(0.25, 0.5),
ImVec2(0.25, 0.25), ImVec2(0.5, 0.25), ImVec2(0.25, 0.25) }
local overBox = -1 -- [Port] function-level static in upstream
local function ViewManipulateCube(view, length, position, size, backgroundColor)
-- State is bound to the current id (PushID/PopID) so multiple view cubes are independent.
local vms = GetViewManipulateState()
local referenceUp = makeVect(0, 1, 0)
-- Recompute hovering for this widget's own window instead of relying on the last
-- Manipulate() call, otherwise only the most recently drawn view would react.
gContext.mbMouseOver = IsHoveringWindow()
local svgView = matrix_t.new(gContext.mViewMat)
local svgProjection = matrix_t.new(gContext.mProjectionMat)
local io = ImGui.GetIO()
local mp = io.MousePos
local viewManipulateHovered = gContext.mbMouseOver and mp.x >= position.x and mp.y >= position.y
and mp.x < position.x + size.x and mp.y < position.y + size.y -- [Port] ImRect::Contains
gContext.mDrawList:AddRectFilled(position, position + size, backgroundColor)
local viewInverse = matrix_t.new()
viewInverse:Inverse(matrix_t.new(view))
local rightHanded = gContext.mProjectionMat[12] < 0 -- m[2][3]
local handednessSign = -1
if rightHanded then handednessSign = 1 end
local camTarget = viewInverse:GetRow(3) - viewInverse:GetRow(2) * handednessSign * length
-- view/projection matrices
local distance = 3
local cubeProjection, cubeView = matrix_t.new(), matrix_t.new()
local fov = math.acos(distance / (sqrt(distance * distance + 3))) * RAD2DEG
Perspective(fov / sqrt(2), size.x / size.y, 0.01, 1000, cubeProjection, rightHanded)
local dir = makeVect(viewInverse[9], viewInverse[10], viewInverse[11])
local up = makeVect(viewInverse[5], viewInverse[6], viewInverse[7])
local eye = dir * handednessSign * distance
LookAt({ eye.x, eye.y, eye.z }, { 0, 0, 0 }, { up.x, up.y, up.z }, cubeView, rightHanded)
-- set context
gContext.mViewMat = cubeView
gContext.mProjectionMat = cubeProjection
gContext.mRayOrigin, gContext.mRayVector = ComputeCameraRay(position, size)
local res = cubeView * cubeProjection
-- tag faces
local boxes = {} -- [Port] 0-based bool[27], nil == false
for iPass = 0, 1 do
for iFace = 0, 5 do
local normalIndex = (iFace % 3)
local perpXIndex = (normalIndex + 1) % 3
local perpYIndex = (normalIndex + 2) % 3
local invert = 1
if iFace > 2 then invert = -1 end
local indexVectorX = directionUnaryV[perpXIndex] * invert
local indexVectorY = directionUnaryV[perpYIndex] * invert
local boxOrigin = directionUnaryV[normalIndex] * -invert - indexVectorX - indexVectorY
-- plan local space
local n = directionUnaryV[normalIndex] * invert
local viewSpaceNormal = n:Copy()
local viewSpacePoint = n * 0.5
viewSpaceNormal:TransformVector(cubeView)
viewSpaceNormal:Normalize()
viewSpacePoint:TransformPoint(cubeView)
local viewSpaceFacePlan = BuildPlan(viewSpacePoint, viewSpaceNormal)
-- back face culling
if viewSpaceFacePlan.w <= 0 then
local facePlan = BuildPlan(n * 0.5, n)
local len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, facePlan)
local posOnPlan = gContext.mRayOrigin + gContext.mRayVector * len - (n * 0.5)
local localx = Dot(directionUnaryV[perpXIndex], posOnPlan) * invert + 0.5
local localy = Dot(directionUnaryV[perpYIndex], posOnPlan) * invert + 0.5
-- panels
local dx = directionUnaryV[perpXIndex]
local dy = directionUnaryV[perpYIndex]
local origin = directionUnaryV[normalIndex] - dx - dy
for iPanel = 0, 8 do
local boxCoord = boxOrigin + indexVectorX * (iPanel % 3) + indexVectorY * (iPanel // 3) + makeVect(1, 1, 1)
local p = panelPosition[iPanel + 1] * 2
local s = panelSize[iPanel + 1] * 2
local faceCoordsScreen = {}
local panelPos = { dx * p.x + dy * p.y,
dx * p.x + dy * (p.y + s.y),
dx * (p.x + s.x) + dy * (p.y + s.y),
dx * (p.x + s.x) + dy * p.y }
for iCoord = 1, 4 do
faceCoordsScreen[iCoord] = worldToPos((panelPos[iCoord] + origin) * 0.5 * invert, res, position, size)
end
local c0 = panelPosition[iPanel + 1]
local c1 = c0 + panelSize[iPanel + 1]
local insidePanel = localx > c0.x and localx < c1.x and localy > c0.y and localy < c1.y
local boxCoordInt = math.floor(boxCoord.x * 9 + boxCoord.y * 3 + boxCoord.z + 0.5) -- [Port] round: int() of an exact float sum
IM_ASSERT(boxCoordInt < 27)
boxes[boxCoordInt] = boxes[boxCoordInt] or (insidePanel and (not vms.mIsDragging) and gContext.mbMouseOver)
-- draw face with lighter color
if iPass == 1 then
local directionColor = GetColorU32(COLOR.DIRECTION_X + normalIndex)
local hoverTint = 0
if viewManipulateHovered then hoverTint = IM_COL32(0x08, 0x08, 0x08, 0) end
gContext.mDrawList:AddConvexPolyFilled(faceCoordsScreen, 4, bor(directionColor, IM_COL32(0x80, 0x80, 0x80, 0x80), hoverTint))
if boxes[boxCoordInt] then
local selectionColor = GetColorU32(COLOR.SELECTION)
gContext.mDrawList:AddConvexPolyFilled(faceCoordsScreen, 4, selectionColor)
if ImGui.IsMouseDown(0) and not vms.mIsClicking and not vms.mIsDragging and ImGui.GetCurrentContext().ActiveId == 0 then
overBox = boxCoordInt
vms.mIsClicking = true
vms.mIsDragging = true
end
end
end
end
end
end
end
if vms.mInterpolationFrames ~= 0 then
vms.mInterpolationFrames -= 1
-- [Port] upstream's discarded newDir/newUp lerps dropped: newUp = interpolationUp and the
-- eye uses newDir, so keep newDir only
local newDir = viewInverse:GetRow(2) * handednessSign
newDir:Lerp(vms.mInterpolationDir * handednessSign, 0.2)
newDir:Normalize()
local newUp = vms.mInterpolationUp
local newEye = camTarget + newDir * length
LookAt({ newEye.x, newEye.y, newEye.z }, { camTarget.x, camTarget.y, camTarget.z }, { newUp.x, newUp.y, newUp.z }, view, rightHanded)
end
gContext.mIsViewManipulatorHovered = gContext.mIsViewManipulatorHovered or viewManipulateHovered
if ImGui.IsMouseDown(0) and (abs(io.MouseDelta.x) ~= 0 or abs(io.MouseDelta.y) ~= 0) and vms.mIsClicking then
vms.mIsClicking = false
end
if not ImGui.IsMouseDown(0) then
if vms.mIsClicking then
-- apply new view direction
local cx = overBox // 9
local cy = (overBox - cx * 9) // 3
local cz = overBox % 3
vms.mInterpolationDir = makeVect(1 - cx, 1 - cy, 1 - cz) * handednessSign
vms.mInterpolationDir:Normalize()
if abs(Dot(vms.mInterpolationDir, referenceUp)) > 1.0 - 0.01 then
local right = viewInverse:GetRow(0)
if abs(right.x) > abs(right.z) then
right.z = 0
else
right.x = 0
end
right:Normalize()
vms.mInterpolationUp = Cross(vms.mInterpolationDir * handednessSign, right)
vms.mInterpolationUp:Normalize()
else
vms.mInterpolationUp = referenceUp
end
vms.mInterpolationFrames = 40
end
vms.mIsClicking = false
vms.mIsDragging = false
end
if vms.mIsDragging then
local rx, ry = matrix_t.new(), matrix_t.new()
rx:RotationAxis(referenceUp, -io.MouseDelta.x * 0.01 * handednessSign)
ry:RotationAxis(viewInverse:GetRow(0), -io.MouseDelta.y * 0.01 * handednessSign)
local roll = rx * ry
local newDir = viewInverse:GetRow(2)
newDir:TransformVector(roll)
newDir:Normalize()
-- clamp
local planDir = Cross(viewInverse:GetRow(0), referenceUp)
planDir.y = 0
planDir:Normalize()
local dt = Dot(planDir, newDir)
if dt < 0 then
newDir = newDir + planDir * dt
newDir:Normalize()
end
local newEye = camTarget + newDir * handednessSign * length
LookAt({ newEye.x, newEye.y, newEye.z }, { camTarget.x, camTarget.y, camTarget.z }, { 0, 1, 0 }, view, rightHanded)
end
local thisUsingViewManipulate = (vms.mInterpolationFrames ~= 0) or vms.mIsDragging
gContext.mbUsingViewManipulate = gContext.mbUsingViewManipulate or thisUsingViewManipulate
if vms.mIsClicking or thisUsingViewManipulate or viewManipulateHovered then
ImGui.SetNextFrameWantCaptureMouse(true)
end
-- restore view/projection because it was used to compute ray
ComputeContext(svgView, svgProjection, gContext.mModelSource, gContext.mMode)
end
-- [Port] overloads merged, dispatched on the 2nd argument:
-- ViewManipulate(view, length, position, size, backgroundColor)
-- ViewManipulate(view, projection, operation, mode, matrix, length, position, size, backgroundColor)
-- view is mutated in place.
function ImGuizmo.ViewManipulate(view, a2, ...)
if type(a2) == "number" then
ViewManipulateCube(view, a2, ...)
return
end
local projection = a2
local operation, mode, matrix, length, position, size, backgroundColor = ...
-- Scale is always local or matrix will be skewed when applying world scale or oriented matrix
local ctxMode = mode
if band(operation, OPERATION.SCALE) ~= 0 then ctxMode = ImGuizmo.MODE.LOCAL end
ComputeContext(view, projection, matrix, ctxMode)
ViewManipulateCube(view, length, position, size, backgroundColor)
end
ImGuizmo._ComputeFrustumPlanes = ComputeFrustumPlanes
end
---------------------------------------------------------------------------
-- [Port] Roblox helpers (not in upstream). Matrices are 1-based 16-arrays in ImGuizmo's layout:
-- rows right/up/back/position, translation at [13..15]. Roblox is right-handed, -Z forward.
do
local Roblox = {}
-- CFrame -> 16-array. GetComponents rotation is row-major; its columns are right/up/back.
function Roblox.MatrixFromCFrame(cf)
local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents()
return { R00, R10, R20, 0, R01, R11, R21, 0, R02, R12, R22, 0, x, y, z, 1 }
end
-- 16-array -> CFrame, scaleVector3. The basis rows are normalized; their lengths are the scale.
function Roblox.CFrameFromMatrix(m)
local rows = {}
local scale = {}
for r = 0, 2 do
local v = makeVect(m[r * 4 + 1], m[r * 4 + 2], m[r * 4 + 3])
scale[r + 1] = v:Length()
v:Normalize()
rows[r + 1] = v
end
local rt, up, bk = rows[1], rows[2], rows[3]
local cf = CFrame.new(m[13], m[14], m[15], rt.x, up.x, bk.x, rt.y, up.y, bk.y, rt.z, up.z, bk.z)
return cf, Vector3.new(scale[1], scale[2], scale[3])
end
-- camera.CFrame inverse, as a view matrix
function Roblox.ViewFromCamera(camera)
local view = matrix_t.new()
view:Inverse(matrix_t.new(Roblox.MatrixFromCFrame(camera.CFrame)))
local out = {}
for i = 1, 16 do out[i] = view[i] end
return out
end
-- camera.FieldOfView is the vertical FOV in degrees; [Port] upstream Perspective takes the half angle
function Roblox.ProjFromCamera(camera, near, far)
local vp = camera.ViewportSize
local proj = {}
Perspective(camera.FieldOfView * 0.5, vp.X / vp.Y, near or 0.1, far or 1000, proj, true)
return proj
end
ImGuizmo.Roblox = Roblox
end
-- [Port] internals exposed for tests
ImGuizmo._context = gContext
ImGuizmo._worldToPos = worldToPos
+29 -1
View File
@@ -5,13 +5,41 @@
Luau port of [ImGuizmo](https://github.com/CedricGuillemet/ImGuizmo) for [imgui-luau](https://git.bug.tools/bug/imgui-luau) ([GitHub](https://github.com/4DBug/imgui-luau)).
Mirrors: [git.bug.tools](https://git.bug.tools/bug/imguizmo-luau) / [GitHub](https://github.com/4DBug/imguizmo-luau).
**Status: work in progress.**
## Usage
```lua
local G = require(ReplicatedStorage.ImGui)
local ImGuizmo = require(ReplicatedStorage.ImGuizmo)(G)
local Roblox = ImGuizmo.Roblox
-- each frame, after ImGui.NewFrame():
ImGuizmo.BeginFrame()
ImGuizmo.SetDrawlist(G.ImGui.GetForegroundDrawList())
local Camera = workspace.CurrentCamera
ImGuizmo.SetRect(0, 0, Camera.ViewportSize.X, Camera.ViewportSize.Y)
local View = Roblox.ViewFromCamera(Camera)
local Proj = Roblox.ProjFromCamera(Camera) -- near 0.1, far 1000
local Matrix = Roblox.MatrixFromCFrame(Part.CFrame) -- 16-array, translation at [13..15]
if ImGuizmo.Manipulate(View, Proj, ImGuizmo.OPERATION.TRANSLATE, ImGuizmo.MODE.WORLD, Matrix) then
Part.CFrame = Roblox.CFrameFromMatrix(Matrix) -- also returns the basis scale as a Vector3
end
ImGuizmo.ViewManipulate(View, 10, G.ImVec2(Camera.ViewportSize.X - 128, 0), G.ImVec2(128, 128), 0x10101010)
```
Matrices are plain 1-based 16-number arrays in upstream order; `matrix`, `deltaMatrix` and `view` are mutated in place.
Full example: `examples/roblox/main.client.luau`.
## Status
Ported (upstream @105e4fd): math (LookAt/Perspective/Frustum, Decompose/Recompose), context/style/IDs,
`Manipulate` (translate, rotate, scale, universal, snapping, local bounds, deltaMatrix), `DrawGrid`,
`DrawGridCustom`, `DrawGridCustomColor`, `DrawCubes`, `DrawAxes`, `ViewManipulate` (both overloads),
`IsOver` (all overloads), `ComputeMouseRay`, `SetAlternativeWindow`, axis mask/limits, `Roblox` helpers.
Not ported: `Ortho` (not in this upstream revision); `PushID(int)`/`PushID(ptr)` are merged into one `PushID`
(numbers hash as ints, other values via `tostring`).
## Development
```sh
+56 -2
View File
@@ -2,14 +2,68 @@ local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local G = require(RS:WaitForChild("ImGui"))
local ImGuizmo = require(RS:WaitForChild("ImGuizmo"))(G)
local ImGui = G.ImGui
local ImGui, ImVec2 = G.ImGui, G.ImVec2
local OP, MODE, Roblox = ImGuizmo.OPERATION, ImGuizmo.MODE, ImGuizmo.Roblox
local Part = workspace:FindFirstChild("GizmoPart")
if not Part then
Part = Instance.new("Part")
Part.Name = "GizmoPart"
Part.Anchored = true
Part.Size = Vector3.new(4, 4, 4)
Part.CFrame = CFrame.new(0, 5, 0)
Part.Parent = workspace
end
local Operation, Mode = OP.TRANSLATE, MODE.WORLD
local Camera = workspace.CurrentCamera
ImGui.CreateContext()
G.ImGui_ImplRoblox.Init(game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"))
RunService.RenderStepped:Connect(function()
G.ImGui_ImplRoblox.NewFrame()
ImGui.NewFrame()
-- ImGuizmo example goes here
ImGuizmo.BeginFrame()
ImGui.Begin("Gizmo")
if ImGui.RadioButton("Translate", Operation == OP.TRANSLATE) then Operation = OP.TRANSLATE end
ImGui.SameLine()
if ImGui.RadioButton("Rotate", Operation == OP.ROTATE) then Operation = OP.ROTATE end
ImGui.SameLine()
if ImGui.RadioButton("Scale", Operation == OP.SCALE) then Operation = OP.SCALE end
if ImGui.RadioButton("Local", Mode == MODE.LOCAL) then Mode = MODE.LOCAL end
ImGui.SameLine()
if ImGui.RadioButton("World", Mode == MODE.WORLD) then Mode = MODE.WORLD end
ImGui.End()
local Viewport = Camera.ViewportSize
ImGuizmo.SetDrawlist(ImGui.GetForegroundDrawList())
ImGuizmo.SetRect(0, 0, Viewport.X, Viewport.Y)
local View = Roblox.ViewFromCamera(Camera)
local Proj = Roblox.ProjFromCamera(Camera)
-- Part.Size is baked into the basis so SCALE edits it
local Matrix = Roblox.MatrixFromCFrame(Part.CFrame)
local Size = Part.Size
for I = 1, 3 do
Matrix[I] *= Size.X
Matrix[4 + I] *= Size.Y
Matrix[8 + I] *= Size.Z
end
if ImGuizmo.Manipulate(View, Proj, Operation, Mode, Matrix) then
local NewCFrame, NewSize = Roblox.CFrameFromMatrix(Matrix)
Part.CFrame = NewCFrame
Part.Size = NewSize
end
-- ViewManipulate edits View in place; the camera is its inverse
ImGuizmo.ViewManipulate(View, 10, ImVec2(Viewport.X - 128, 0), ImVec2(128, 128), 0x10101010)
if ImGuizmo.IsUsingViewManipulate() then
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = Roblox.CFrameFromMatrix(View):Inverse()
end
ImGui.Render()
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
end)
+86
View File
@@ -0,0 +1,86 @@
-- 50 frames of DrawGrid + DrawCubes + DrawAxes + ViewManipulate (both overloads), then Roblox helper round trips.
local RS = game:GetService("ReplicatedStorage")
local G = require(RS:WaitForChild("ImGui"))
local ImGuizmo = require(RS:WaitForChild("ImGuizmo"))(G)
local ImGui, ImVec2 = G.ImGui, G.ImVec2
local M = ImGuizmo._math
local R = ImGuizmo.Roblox
ImGui.CreateContext()
G.ImGui_ImplRoblox.Init()
local io = ImGui.GetIO()
local W, H = 800, 600
local view, proj = {}, {}
M.LookAt({ 3, 4, 8 }, { 0, 0, 0 }, { 0, 1, 0 }, view)
M.Perspective(27, W / H, 0.1, 100, proj)
local identity = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
local cubes = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, -1, 1 }
local matrix = table.clone(identity)
local function near(a, b, eps) return math.abs(a - b) < (eps or 1e-4) end
-- Roblox helpers (CFrame mock: CFrame.new returns its args as a list)
local function fakeCFrame(...)
local c = { ... }
return { GetComponents = function() return table.unpack(c) end }
end
local s2 = math.sqrt(0.5)
-- 45 deg about Y: right = (s2, 0, -s2), up = (0,1,0), back = (s2, 0, s2)
local cfc = { 1, 2, 3, s2, 0, s2, 0, 1, 0, -s2, 0, s2 }
local cf = fakeCFrame(table.unpack(cfc))
local m = R.MatrixFromCFrame(cf)
assert(near(m[13], 1) and near(m[14], 2) and near(m[15], 3), "translation not at 13..15")
assert(near(m[1], s2) and near(m[3], -s2), "right row wrong")
-- scale the basis, round trip must normalize and report the scale
for k = 1, 4 do m[k] *= 2 end
for k = 9, 12 do m[k] *= 3 end
local back, scale = R.CFrameFromMatrix(m)
for i = 1, 12 do assert(near(back[i], cfc[i]), "CFrame round trip component " .. i) end
assert(near(scale.X, 2) and near(scale.Y, 1) and near(scale.Z, 3), "scale wrong")
local camera = { CFrame = cf, FieldOfView = 70, ViewportSize = { X = W, Y = H } }
local v = M.matrix_t.new(R.ViewFromCamera(camera)) * M.matrix_t.new(R.MatrixFromCFrame(camera.CFrame))
for i = 1, 16 do assert(near(v[i], identity[i]), "view * camera ~= identity at " .. i) end
local p = R.ProjFromCamera(camera)
assert(near(p[6], 1 / math.tan(math.rad(35)), 1e-3), "proj fov wrong: " .. p[6])
assert(near(p[1] * W / H, p[6], 1e-3), "proj aspect wrong")
local frame = 0
local WF = G.ImGuiWindowFlags
game:GetService("RunService").Stepped:Connect(function()
if frame >= 50 then return end
frame += 1
G.ImGui_ImplRoblox.NewFrame()
io.DeltaTime = 1 / 60
-- drag across the view cube for a few frames
if frame == 5 then io:AddMousePosEvent(W - 64, 64) end
if frame == 8 then io:AddMouseButtonEvent(0, true) end
if frame > 8 and frame < 20 then io:AddMousePosEvent(W - 64 + (frame - 8) * 3, 64) end
if frame == 22 then io:AddMouseButtonEvent(0, false) end
ImGui.NewFrame()
ImGuizmo.BeginFrame()
ImGui.SetNextWindowPos(ImVec2(0, 0))
ImGui.SetNextWindowSize(ImVec2(W, H))
ImGui.Begin("Viewport", nil, bit32.bor(WF.NoTitleBar, WF.NoMove, WF.NoResize))
ImGuizmo.SetDrawlist()
ImGuizmo.SetRect(0, 0, W, H)
ImGuizmo.DrawGrid(view, proj, identity, 10)
ImGuizmo.DrawGridCustom(view, proj, identity, 5, 2, 4)
ImGuizmo.DrawCubes(view, proj, cubes, 2)
ImGuizmo.DrawAxes(view, proj, cubes, 2)
ImGuizmo.Manipulate(view, proj, ImGuizmo.OPERATION.TRANSLATE, ImGuizmo.MODE.LOCAL, matrix)
if frame % 2 == 0 then
ImGuizmo.ViewManipulate(view, 8, ImVec2(W - 128, 0), ImVec2(128, 128), 0x10101010)
else
ImGuizmo.ViewManipulate(view, proj, ImGuizmo.OPERATION.TRANSLATE, ImGuizmo.MODE.LOCAL, matrix, 8, ImVec2(W - 128, 0), ImVec2(128, 128), 0x10101010)
end
ImGui.End()
ImGui.Render()
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
for i = 1, 16 do assert(view[i] == view[i], "view has NaN at frame " .. frame) end
if frame == 50 then
print("OK")
end
end)