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(sizeVector3) → number

Computes the diameter of a cubeid

fitBoundingBoxToCamera

CameraUtils.fitBoundingBoxToCamera(
sizeVector3,--

Size of the bounding box

fovDegnumber,--

Field of view in degrees (vertical)

aspectRationumber--

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

fitSphereToCamera

CameraUtils.fitSphereToCamera(
radiusnumber,--

Radius of the sphere

fovDegnumber,--

Field of view in degrees (vertical)

aspectRationumber--

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.

isOnScreen

CameraUtils.isOnScreen(
cameraCamera,
positionVector3
) → 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": 31,
                "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": 44,
                "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": 58,
                "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": 74,
                "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": 22,
        "path": "src/camera/src/Client/CameraUtils.lua"
    }
}