Developer knowledge network · moderated exchange

Сообщество UnreliableCode

Сообщество разработчиков, обратного проектирования и кодирования

Tutorial

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

raycast_ryan
Ballistics Dev
MEMBER
Представитель: 72
Дата присоединения: Sep 2019
Сообщения: 32
Спасибо: 16
1 месяцев назад · 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
Представитель: 105
Дата присоединения: Aug 2018
Сообщения: 52
Спасибо: 41
1 месяцев назад · 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
Представитель: 159
Дата присоединения: Aug 2020
Сообщения: 17
Спасибо: 26
1 месяцев назад · Jul 7, 2026 2:17 PM
#3

Works faster than calling native engine method on 100 entities.