Developer knowledge network ยท moderated exchange

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Knowledge indexLive
4Categories
919Threads
2.8KPosts
Tutorial

Custom 3D to 2D Viewport Projection Math in Pure Luau Script [v2.4 Technical Discussion]

raycast_ryan
Ballistics Dev
MEMBER
Rep: 72
Join Date: Sep 2019
Posts: 32
Thanks: 16
1 months ago ยท Jul 6, 2026 6:20 PM
#1

Calculating screen coordinates without relying on Camera:WorldToViewportPoint:

LUA
local function WorldToScreen(worldPos, camera)
    local cframe = camera.CFrame
    local rel = cframe:PointToObjectSpace(worldPos)
    if rel.Z >= 0 then return nil end -- Behind camera
    
    local fov = math.rad(camera.FieldOfView)
    local h = math.tan(fov / 2)
    local aspect = camera.ViewportSize.X / camera.ViewportSize.Y
    local w = h * aspect
    
    local screenX = ((-rel.X / rel.Z) / w + 1) * 0.5 * camera.ViewportSize.X
    local screenY = ((rel.Y / rel.Z) / h + 1) * 0.5 * camera.ViewportSize.Y
    return Vector2.new(screenX, screenY)
end
matrix_math_guy
Math Specialist
MEMBER
Rep: 105
Join Date: Aug 2018
Posts: 52
Thanks: 41
1 months ago ยท Jul 7, 2026 2:17 AM
#2

The tangent aspect ratio calculation matches the internal camera projection matrix identically. Pure math at its finest.

luau_hacker
Luau Expert
MEMBER
Rep: 159
Join Date: Aug 2020
Posts: 17
Thanks: 26
1 months ago ยท Jul 7, 2026 2:17 PM
#3

Works faster than calling native engine method on 100 entities.