Developer knowledge network · moderated exchange

UnreliableCode Topluluğu

Geliştirici Araştırması, Tersine Mühendislik ve Kodlama Topluluğu

Knowledge indexCanlı
4Categories
919Threads
2.8KGönderiler
Tutorial

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

raycast_ryan
Ballistics Dev
MEMBER
Temsilci: 72
Katılım Tarihi: Sep 2019
Gönderiler: 32
Teşekkürler: 16
1 ay önce · 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
Temsilci: 105
Katılım Tarihi: Aug 2018
Gönderiler: 52
Teşekkürler: 41
1 ay önce · 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
Temsilci: 159
Katılım Tarihi: Aug 2020
Gönderiler: 17
Teşekkürler: 26
1 ay önce · Jul 7, 2026 2:17 PM
#3

Works faster than calling native engine method on 100 entities.