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.