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
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.
isOnScreen
Checks if a position is on screen on a camera