Vital.sandbox
Engine

engine.world_to_screen

Client

Projects a 3D world position to 2D screen-space coordinates via the active viewport camera


Syntax

local position, distance = engine.world_to_screen(position, padding = 0)

Parameters

TypeNameDescription
vector3position3D world position to convert to screen-space coordinates
floatpaddingExtra pixel boundary beyond screen edges before treating the point as off-screen

Returns

TypeNameDescription
vector2position2D screen-space pixel coordinates of the projected point or {-1, -1} if off-screen
floatdistanceDistance in world units from the camera to the point or -1 if off-screen

Examples

Draw a rectangle at a world position projected to screen
local position, distance = engine.world_to_screen({0, 0, 0})
local size = {10, 10}

network:fetch("vital.sandbox:draw", true):on(function()
    local position, distance = engine.world_to_screen({0, 0, 0}, size*2)
    if position[1] == -1 then return false end

    engine.draw_rectangle(
        {position[1] - size[1]*0.5, position[2] - size[2]*0.5},
        size,
        {1, 0, 0, 1}
    )
end)

On this page