Developer knowledge network · moderated exchange

Zajednica UnreliableCode

Zajednica za istraživanje, obrnuti inženjering i programiranje programera

Knowledge indexŽivjeti
4Categories
919Threads
2.8KPostovi
Guide

Reading Camera.CFrame & computing custom 3D to 2D projection in Luau

matrix_math_guy
Math Specialist
MEMBER
Rep: 105
Datum pridruživanja: Aug 2018
Postovi: 52
Hvala: 41
prije 1 mjeseci · Jul 2, 2026 11:30 PM
#1

How Roblox's Camera:WorldToViewportPoint works under the hood:

LUA
local function CustomWorldToScreen(worldPos)
    local camera = workspace.CurrentCamera
    local cframe = camera.CFrame
    local fov = camera.FieldOfView
    local viewport = camera.ViewportSize
    
    -- Transform world point to camera relative space
    local relPos = cframe:PointToObjectSpace(worldPos)
    if relPos.Z >= 0 then return Vector2.new(0, 0), false end -- Behind camera
    
    local aspect = viewport.X / viewport.Y
    local fovYRad = math.rad(fov)
    local fovXRad = 2 * math.atan(math.tan(fovYRad / 2) * aspect)
    
    local screenX = (viewport.X / 2) * (1 - (relPos.X / (-relPos.Z * math.tan(fovXRad / 2))))
    local screenY = (viewport.Y / 2) * (1 + (relPos.Y / (-relPos.Z * math.tan(fovYRad / 2))))
    return Vector2.new(screenX, screenY), true
end
luau_hacker
Luau Expert
MEMBER
Rep: 159
Datum pridruživanja: Aug 2020
Postovi: 17
Hvala: 26
prije 1 mjeseci · Jul 3, 2026 2:50 AM
#2

Mathematical projection matches Roblox's internal C++ implementation within 0.01 pixels. Great work!

raycast_ryan
Ballistics Dev
MEMBER
Rep: 72
Datum pridruživanja: Sep 2019
Postovi: 32
Hvala: 16
prije 1 mjeseci · Jul 3, 2026 1:46 PM
#3

PointToObjectSpace handles camera rotation matrix cleanly.