Skip to content

Commit

Permalink
Add world_to_window function (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
dri-richard committed Dec 1, 2023
1 parent 71c5415 commit 1fc8e62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ Translate world coordinates to [screen coordinates](#screen-coordinates), based
**RETURN**
* `screen_coords` (vector3) Screen coordinates

### camera.world_to_window(camera_id, world)
Translate world coordinates to [window coordinates](#window-coordinates), based on the view and projection of the camera.

**PARAMETER**
* `camera_id` (hash|url|nil) nil for the first camera
* `world` (vector3) World coordinates to convert

**RETURN**
* `window_coords` (vector3) Window coordinates


### camera.unproject(view, projection, screen)
Translate [screen coordinates](#screen-coordinates) to world coordinates using the specified view and projection.
Expand Down
15 changes: 15 additions & 0 deletions orthographic/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,21 @@ function M.world_to_screen(camera_id, world, adjust_mode)
return vmath.vector3(screen.x, screen.y, screen.z)
end

--- Convert world coordinates to window coordinates based
-- on a specific camera's view and projection
-- Window coordinates are the non-scaled coordinates provided by action.screen_x
-- and action.screen_y in on_input()
-- @param camera_id
-- @param world World coordinates as a vector3
-- @return window coordinates
function M.world_to_window(camera_id, world)
local view = cameras[camera_id].view or MATRIX4
local projection = cameras[camera_id].projection or MATRIX4
local screen = M.project(view, projection, vmath.vector3(world))
local scale_x = screen.x / (dpi_ratio * DISPLAY_WIDTH / WINDOW_WIDTH)
local scale_y = screen.y / (dpi_ratio * DISPLAY_HEIGHT / WINDOW_HEIGHT)
return vmath.vector3(scale_x, scale_y, 0)
end

--- Translate world coordinates to screen coordinates given a
-- view and projection matrix
Expand Down

0 comments on commit 1fc8e62

Please sign in to comment.