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
| Type | Name | Description |
|---|---|---|
vector3 | position | 3D world position to convert to screen-space coordinates |
float | padding | Extra pixel boundary beyond screen edges before treating the point as off-screen |
Returns
| Type | Name | Description |
|---|---|---|
vector2 | position | 2D screen-space pixel coordinates of the projected point or {-1, -1} if off-screen |
float | distance | Distance in world units from the camera to the point or -1 if off-screen |
Examples
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)