Skip to main content

CameraUtils

Utility methods for cameras. These are great for viewport frames.

-- Sample viewport frame fitting of a model
local viewportFrame = ...
local camera = viewportFrame.CurrentCamera
local model = viewportFrame:FindFirstChildWhichIsA("Model")

RunService.RenderStepped:Connect(function()
	local cframe, size = model:GetBoundingBox()
	local size = viewportFrame.AbsoluteSize
	local aspectRatio = size.X/size.Y
	local dist = CameraUtils.fitBoundingBoxToCamera(size, camera.FieldOfView, aspectRatio)
	camera.CFrame = cframe.Position + CFrame.Angles(0, math.pi*os.clock() % math.pi, -math.pi/8)
		:vectorToWorldSpace(Vector3.new(0, 0, -dist))
end)

Functions​

getCubeoidDiameter​

CameraUtils.getCubeoidDiameter(size: Vector3) → number

Computes the diameter of a cubeid

fitBoundingBoxToCamera​

CameraUtils.fitBoundingBoxToCamera(
size: Vector3,--

Size of the bounding box

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number--

Aspect ratio of the screen

) → number--

Distance to move the camera back from the bounding box

Use spherical bounding box to calculate how far back to move a camera See: https://community.khronos.org/t/zoom-to-fit-screen/59857/12

fitBoundingBoxToCameraRect​

CameraUtils.fitBoundingBoxToCameraRect(
size: Vector3,--

Size of the bounding box

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number,--

Aspect ratio of the screen

rect: Rect--

Normalized viewport rect (0 to 1, top-left origin)

) → Vector3--

Camera position relative to the bounding box center, in camera space

Like CameraUtils.fitBoundingBoxToCamera, but frames the box in a sub-rect of the viewport. See CameraUtils.fitSphereToCameraRect.

-- Keep a hat clear of a label along the bottom of a viewport frame
local viewportSize = viewportFrame.AbsoluteSize
local labelTop = label.AbsolutePosition.Y - viewportFrame.AbsolutePosition.Y
local visibleRect = Rect.new(0, 0, 1, labelTop / viewportSize.Y)

local boxCFrame, size = hat:GetBoundingBox()
local offset = CameraUtils.fitBoundingBoxToCameraRect(size, camera.FieldOfView, viewportSize.X / viewportSize.Y, visibleRect)
camera.CFrame = CFrame.new(boxCFrame.Position) * rotation * CFrame.new(offset)

fitBoundingBoxToCameraRectExact​

CameraUtils.fitBoundingBoxToCameraRectExact(
boxCFrame: CFrame,--

CFrame of the bounding box

size: Vector3,--

Size of the bounding box

rotation: CFrame,--

Camera rotation. Position is ignored.

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number,--

Aspect ratio of the screen

subrect: Rect--

Normalized viewport rect (0 to 1, top-left origin)

) → Vector3--

Camera position relative to the bounding box center, in camera space

Like CameraUtils.fitBoundingBoxToCameraRect, but fits the box's corners exactly for the given camera rotation instead of its bounding sphere. Much tighter for boxes that aren't cube shaped, but the distance changes as the camera rotates around the box.

local boxCFrame, size = model:GetBoundingBox()
local offset = CameraUtils.fitBoundingBoxToCameraRectExact(boxCFrame, size, rotation, camera.FieldOfView, aspectRatio, visibleRect)
camera.CFrame = CFrame.new(boxCFrame.Position) * rotation * CFrame.new(offset)

fitSphereToCamera​

CameraUtils.fitSphereToCamera(
radius: number,--

Radius of the sphere

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number--

Aspect ratio of the screen

) → number--

Distance to move the camera back from the bounding box

Fits a sphere to the camera, computing how far back to zoom the camera from the center of the sphere.

fitSphereToCameraRect​

CameraUtils.fitSphereToCameraRect(
radius: number,--

Radius of the sphere

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number,--

Aspect ratio of the screen

subrect: Rect--

Normalized viewport rect (0 to 1, top-left origin)

) → Vector3--

Camera position relative to the sphere center, in camera space

Like CameraUtils.fitSphereToCamera, but frames the sphere in a sub-rect of the viewport, such as the space left visible by UI. Rotation is unchanged.

Returns both the distance and the sideways shift that centers the sphere in the rect. The distance alone would center the sphere on the full viewport, under the UI.

Off-center spheres project stretched away from the viewport center, so the sphere's outline is centered in the rect rather than its center point. It touches both edges on the limiting axis.

-- Frame a sphere left of a panel covering the right 40% of the screen
local offset = CameraUtils.fitSphereToCameraRect(radius, camera.FieldOfView, aspectRatio, Rect.new(0, 0, 0.6, 1))
camera.CFrame = CFrame.new(center) * rotation * CFrame.new(offset)

fitPointsToCameraRect​

CameraUtils.fitPointsToCameraRect(
points: {Vector3},--

Points in camera space (x right, y up, looking down -z), relative to an origin

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number,--

Aspect ratio of the screen

subrect: Rect--

Normalized viewport rect (0 to 1, top-left origin)

) → Vector3--

Camera position relative to the origin, in camera space

Fits points into a sub-rect of the viewport exactly, without rotating the camera. Returns the closest camera position that keeps every point inside the rect. On the limiting axis the points touch both edges, and on the other axis they're centered between the edges.

For a fixed rotation, keeping a point inside an edge is linear in the camera position, so the closest distance has a closed form per axis.

getPointsCameraSubRectShift​

CameraUtils.getPointsCameraSubRectShift(
points: {Vector3},--

Points in camera space (x right, y up, looking down -z), relative to an origin

depth: number,--

Distance from the camera to the origin along the look vector

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number,--

Aspect ratio of the screen

subrect: Rect--

Normalized viewport rect (0 to 1, top-left origin)

) → Vector3--

Camera-space offset

Computes the camera-space shift that centers points in a sub-rect of the viewport at the given depth, without rotating the camera. Matches CameraUtils.fitPointsToCameraRect at its depth.

Use this when the distance comes from elsewhere, such as a spring. Unlike CameraUtils.getCameraSubRectShift, this centers the points' projected outline.

boundingBoxPoints​

CameraUtils.boundingBoxPoints(
boxCFrame: CFrame,--

CFrame of the bounding box

size: Vector3--

Size of the bounding box

) → {Vector3}

Returns the 8 corners of a bounding box in world space.

getCameraSubRectShift​

CameraUtils.getCameraSubRectShift(
depth: number,--

Distance from the camera to the subject along the look vector

fovDeg: number,--

Field of view in degrees (vertical)

aspectRatio: number,--

Aspect ratio of the screen

subrect: Rect--

Normalized viewport rect (0 to 1, top-left origin)

) → Vector3--

Camera-space offset

Computes the camera-space shift that centers a subject at depth in a sub-rect of the viewport, without rotating the camera.

The subject is placed on the angle bisector of the rect's edges rather than its screen-space center, since off-center objects project stretched away from the viewport center. This centers the outline of a sphere at any depth and radius.

Use this when the distance comes from elsewhere, such as a spring. Otherwise prefer CameraUtils.fitSphereToCameraRect.

-- Center the subject above a dialog box covering the bottom 30% of the screen
local shift = CameraUtils.getCameraSubRectShift(distance, camera.FieldOfView, aspectRatio, Rect.new(0, 0, 1, 0.7))
camera.CFrame = CFrame.new(subjectPosition) * rotation * CFrame.new(shift + Vector3.new(0, 0, distance))

isOnScreen​

CameraUtils.isOnScreen(
camera: Camera,
position: Vector3
) → boolean

Checks if a position is on screen on a camera

Show raw api
{
    "functions": [
        {
            "name": "getCubeoidDiameter",
            "desc": "Computes the diameter of a cubeid",
            "params": [
                {
                    "name": "size",
                    "desc": "",
                    "lua_type": "Vector3"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 51,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "fitBoundingBoxToCamera",
            "desc": "Use spherical bounding box to calculate how far back to move a camera\nSee: https://community.khronos.org/t/zoom-to-fit-screen/59857/12",
            "params": [
                {
                    "name": "size",
                    "desc": "Size of the bounding box",
                    "lua_type": "Vector3"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "Distance to move the camera back from the bounding box",
                    "lua_type": "number"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 68,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "fitBoundingBoxToCameraRect",
            "desc": "Like [CameraUtils.fitBoundingBoxToCamera], but frames the box in a sub-rect of the viewport.\nSee [CameraUtils.fitSphereToCameraRect].\n\n```lua\n-- Keep a hat clear of a label along the bottom of a viewport frame\nlocal viewportSize = viewportFrame.AbsoluteSize\nlocal labelTop = label.AbsolutePosition.Y - viewportFrame.AbsolutePosition.Y\nlocal visibleRect = Rect.new(0, 0, 1, labelTop / viewportSize.Y)\n\nlocal boxCFrame, size = hat:GetBoundingBox()\nlocal offset = CameraUtils.fitBoundingBoxToCameraRect(size, camera.FieldOfView, viewportSize.X / viewportSize.Y, visibleRect)\ncamera.CFrame = CFrame.new(boxCFrame.Position) * rotation * CFrame.new(offset)\n```",
            "params": [
                {
                    "name": "size",
                    "desc": "Size of the bounding box",
                    "lua_type": "Vector3"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                },
                {
                    "name": "rect",
                    "desc": "Normalized viewport rect (0 to 1, top-left origin)",
                    "lua_type": "Rect"
                }
            ],
            "returns": [
                {
                    "desc": "Camera position relative to the bounding box center, in camera space",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 94,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "fitBoundingBoxToCameraRectExact",
            "desc": "Like [CameraUtils.fitBoundingBoxToCameraRect], but fits the box's corners exactly for the given\ncamera rotation instead of its bounding sphere. Much tighter for boxes that aren't cube shaped,\nbut the distance changes as the camera rotates around the box.\n\n```lua\nlocal boxCFrame, size = model:GetBoundingBox()\nlocal offset = CameraUtils.fitBoundingBoxToCameraRectExact(boxCFrame, size, rotation, camera.FieldOfView, aspectRatio, visibleRect)\ncamera.CFrame = CFrame.new(boxCFrame.Position) * rotation * CFrame.new(offset)\n```",
            "params": [
                {
                    "name": "boxCFrame",
                    "desc": "CFrame of the bounding box",
                    "lua_type": "CFrame"
                },
                {
                    "name": "size",
                    "desc": "Size of the bounding box",
                    "lua_type": "Vector3"
                },
                {
                    "name": "rotation",
                    "desc": "Camera rotation. Position is ignored.",
                    "lua_type": "CFrame"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                },
                {
                    "name": "subrect",
                    "desc": "Normalized viewport rect (0 to 1, top-left origin)",
                    "lua_type": "Rect"
                }
            ],
            "returns": [
                {
                    "desc": "Camera position relative to the bounding box center, in camera space",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 118,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "fitSphereToCamera",
            "desc": "Fits a sphere to the camera, computing how far back to zoom the camera from\nthe center of the sphere.",
            "params": [
                {
                    "name": "radius",
                    "desc": "Radius of the sphere",
                    "lua_type": "number"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "Distance to move the camera back from the bounding box",
                    "lua_type": "number"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 143,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "fitSphereToCameraRect",
            "desc": "Like [CameraUtils.fitSphereToCamera], but frames the sphere in a sub-rect of the viewport,\nsuch as the space left visible by UI. Rotation is unchanged.\n\nReturns both the distance and the sideways shift that centers the sphere in the rect.\nThe distance alone would center the sphere on the full viewport, under the UI.\n\nOff-center spheres project stretched away from the viewport center, so the sphere's outline\nis centered in the rect rather than its center point. It touches both edges on the limiting axis.\n\n```lua\n-- Frame a sphere left of a panel covering the right 40% of the screen\nlocal offset = CameraUtils.fitSphereToCameraRect(radius, camera.FieldOfView, aspectRatio, Rect.new(0, 0, 0.6, 1))\ncamera.CFrame = CFrame.new(center) * rotation * CFrame.new(offset)\n```",
            "params": [
                {
                    "name": "radius",
                    "desc": "Radius of the sphere",
                    "lua_type": "number"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                },
                {
                    "name": "subrect",
                    "desc": "Normalized viewport rect (0 to 1, top-left origin)",
                    "lua_type": "Rect"
                }
            ],
            "returns": [
                {
                    "desc": "Camera position relative to the sphere center, in camera space",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 174,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "fitPointsToCameraRect",
            "desc": "Fits points into a sub-rect of the viewport exactly, without rotating the camera. Returns the\nclosest camera position that keeps every point inside the rect. On the limiting axis the points\ntouch both edges, and on the other axis they're centered between the edges.\n\nFor a fixed rotation, keeping a point inside an edge is linear in the camera position, so the\nclosest distance has a closed form per axis.",
            "params": [
                {
                    "name": "points",
                    "desc": "Points in camera space (x right, y up, looking down -z), relative to an origin",
                    "lua_type": "{ Vector3 }"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                },
                {
                    "name": "subrect",
                    "desc": "Normalized viewport rect (0 to 1, top-left origin)",
                    "lua_type": "Rect"
                }
            ],
            "returns": [
                {
                    "desc": "Camera position relative to the origin, in camera space",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 202,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "getPointsCameraSubRectShift",
            "desc": "Computes the camera-space shift that centers points in a sub-rect of the viewport at the given\ndepth, without rotating the camera. Matches [CameraUtils.fitPointsToCameraRect] at its depth.\n\nUse this when the distance comes from elsewhere, such as a spring. Unlike\n[CameraUtils.getCameraSubRectShift], this centers the points' projected outline.",
            "params": [
                {
                    "name": "points",
                    "desc": "Points in camera space (x right, y up, looking down -z), relative to an origin",
                    "lua_type": "{ Vector3 }"
                },
                {
                    "name": "depth",
                    "desc": "Distance from the camera to the origin along the look vector",
                    "lua_type": "number"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                },
                {
                    "name": "subrect",
                    "desc": "Normalized viewport rect (0 to 1, top-left origin)",
                    "lua_type": "Rect"
                }
            ],
            "returns": [
                {
                    "desc": "Camera-space offset",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 232,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "boundingBoxPoints",
            "desc": "Returns the 8 corners of a bounding box in world space.",
            "params": [
                {
                    "name": "boxCFrame",
                    "desc": "CFrame of the bounding box",
                    "lua_type": "CFrame"
                },
                {
                    "name": "size",
                    "desc": "Size of the bounding box",
                    "lua_type": "Vector3"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ Vector3 }"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 251,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "getCameraSubRectShift",
            "desc": "Computes the camera-space shift that centers a subject at `depth` in a sub-rect of the\nviewport, without rotating the camera.\n\nThe subject is placed on the angle bisector of the rect's edges rather than its screen-space\ncenter, since off-center objects project stretched away from the viewport center. This centers\nthe outline of a sphere at any depth and radius.\n\nUse this when the distance comes from elsewhere, such as a spring. Otherwise prefer\n[CameraUtils.fitSphereToCameraRect].\n\n```lua\n-- Center the subject above a dialog box covering the bottom 30% of the screen\nlocal shift = CameraUtils.getCameraSubRectShift(distance, camera.FieldOfView, aspectRatio, Rect.new(0, 0, 1, 0.7))\ncamera.CFrame = CFrame.new(subjectPosition) * rotation * CFrame.new(shift + Vector3.new(0, 0, distance))\n```",
            "params": [
                {
                    "name": "depth",
                    "desc": "Distance from the camera to the subject along the look vector",
                    "lua_type": "number"
                },
                {
                    "name": "fovDeg",
                    "desc": "Field of view in degrees (vertical)",
                    "lua_type": "number"
                },
                {
                    "name": "aspectRatio",
                    "desc": "Aspect ratio of the screen",
                    "lua_type": "number"
                },
                {
                    "name": "subrect",
                    "desc": "Normalized viewport rect (0 to 1, top-left origin)",
                    "lua_type": "Rect"
                }
            ],
            "returns": [
                {
                    "desc": "Camera-space offset",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 326,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        },
        {
            "name": "isOnScreen",
            "desc": "Checks if a position is on screen on a camera",
            "params": [
                {
                    "name": "camera",
                    "desc": "",
                    "lua_type": "Camera"
                },
                {
                    "name": "position",
                    "desc": "",
                    "lua_type": "Vector3"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 358,
                "path": "src/camera/src/Client/CameraUtils.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "CameraUtils",
    "desc": "Utility methods for cameras. These are great for viewport frames.\n\n```lua\n-- Sample viewport frame fitting of a model\nlocal viewportFrame = ...\nlocal camera = viewportFrame.CurrentCamera\nlocal model = viewportFrame:FindFirstChildWhichIsA(\"Model\")\n\nRunService.RenderStepped:Connect(function()\n\tlocal cframe, size = model:GetBoundingBox()\n\tlocal size = viewportFrame.AbsoluteSize\n\tlocal aspectRatio = size.X/size.Y\n\tlocal dist = CameraUtils.fitBoundingBoxToCamera(size, camera.FieldOfView, aspectRatio)\n\tcamera.CFrame = cframe.Position + CFrame.Angles(0, math.pi*os.clock() % math.pi, -math.pi/8)\n\t\t:vectorToWorldSpace(Vector3.new(0, 0, -dist))\nend)\n```",
    "source": {
        "line": 23,
        "path": "src/camera/src/Client/CameraUtils.lua"
    }
}