Vital.sandbox
Engine

engine.screen_to_world

Client

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


Syntax

local position = engine.screen_to_world(position, depth = 1)

Parameters

TypeNameDescription
vector2position2D screen-space pixel coordinates to convert to 3D world-space coordinates
floatdepthDistance in world units along the camera ray to retrieve the world position

Returns

TypeNameDescription
vector3position3D world-space position at the given depth or {-1, -1, -1} if off-screen

Examples

Convert screen center to world position
local resolution = engine.get_resolution()
local position = engine.screen_to_world({resolution[1]*0.5, resolution[2]*0.5}, 100)

engine.print("info", position[1], position[2], position[3])
Retrieve world positions at near and far depths
local resolution = engine.get_resolution()
local near = engine.screen_to_world({resolution[1]*0.5, resolution[2]*0.5}, 10)
local far = engine.screen_to_world({resolution[1]*0.5, resolution[2]*0.5}, 500)

engine.print("info", "Near:", near[1], near[2], near[3])
engine.print("info", "Far:",  far[1],  far[2],  far[3])

On this page