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
Computes the diameter of a cubeid
fitBoundingBoxToCamera
CameraUtils.fitBoundingBoxToCamera(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(fovDeg: number,--
Field of view in degrees (vertical)
aspectRatio: number,--
Aspect ratio of the screen
) → 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(fovDeg: number,--
Field of view in degrees (vertical)
aspectRatio: number,--
Aspect ratio of the screen
) → 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
) → 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(fovDeg: number,--
Field of view in degrees (vertical)
aspectRatio: number,--
Aspect ratio of the screen
) → 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(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
) → 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
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
) → 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
Checks if a position is on screen on a camera