core.engine.screen_to_world

Client

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


Syntax

local position = core.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 = core.engine.get_resolution()
local center = {resolution[1]*0.5, resolution[2]*0.5}
local position = core.engine.screen_to_world(center, 100)

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

core.engine.print("info", "Near:", util.table.unpack(near))
core.engine.print("info", "Far:", util.table.unpack(far))

On this page