Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cc37bca

Browse filesBrowse files
committed
map screen to world, tested and works
1 parent e4962ca commit cc37bca
Copy full SHA for cc37bca

File tree

Expand file treeCollapse file tree

1 file changed

+35
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+35
-1
lines changed

‎fastplotlib/layouts/_base.py

Copy file name to clipboardExpand all lines: fastplotlib/layouts/_base.py
+35-1Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from warnings import warn
21
from typing import *
32
import weakref
43

54
import numpy as np
65

76
from pygfx import Scene, OrthographicCamera, PerspectiveCamera, PanZoomController, OrbitController, \
87
Viewport, WgpuRenderer
8+
from pygfx.linalg import Vector3
99
from wgpu.gui.auto import WgpuCanvas
1010

1111
from ..graphics._base import Graphic, GraphicCollection
@@ -146,6 +146,40 @@ def get_rect(self) -> Tuple[float, float, float, float]:
146146
"""allows setting the region occupied by the viewport w.r.t. the parent"""
147147
raise NotImplementedError("Must be implemented in subclass")
148148

149+
def map_screen_to_world(self, pos: Tuple[float, float]) -> Vector3:
150+
"""
151+
Map screen position to world position
152+
153+
Parameters
154+
----------
155+
pos: (float, float)
156+
(x, y) screen coordinates
157+
158+
"""
159+
160+
if not self.viewport.is_inside(*pos):
161+
return None
162+
163+
vs = self.viewport.logical_size
164+
165+
# get position relative to viewport
166+
pos_rel = (
167+
pos[0] - self.viewport.rect[0],
168+
pos[1] - self.viewport.rect[1],
169+
)
170+
171+
# convert screen position to NDC
172+
pos_ndc = Vector3(
173+
pos_rel[0] / vs[0] * 2 - 1,
174+
-(pos_rel[1] / vs[1] * 2 - 1),
175+
0
176+
)
177+
178+
# get world position
179+
pos_world = self.camera.position.clone().project(self.camera).add(pos_ndc).unproject(self.camera)
180+
181+
return pos_world
182+
149183
def set_viewport_rect(self, *args):
150184
self.viewport.rect = self.get_rect()
151185

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.