|
| 1 | +from typing import TYPE_CHECKING |
| 2 | +from typing_extensions import override |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from pyglet.event import EVENT_HANDLE_STATE |
| 7 | + |
| 8 | +from .ray import Plane, Ray, RayHit |
| 9 | +from .vec3 import Vec3 |
| 10 | +from .viewer_interaction_base import ViewerInteractionBase |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from genesis.engine.scene import Scene |
| 14 | + from genesis.ext.pyrender.node import Node |
| 15 | + |
| 16 | + |
| 17 | +class ViewerInteraction(ViewerInteractionBase): |
| 18 | + """Functionalities to be implemented: |
| 19 | + - mouse picking |
| 20 | + - mouse dragging |
| 21 | + """ |
| 22 | + |
| 23 | + camera: 'Node' |
| 24 | + scene: 'Scene' |
| 25 | + viewport_size: tuple[int, int] |
| 26 | + camera_yfov: float |
| 27 | + |
| 28 | + tan_half_fov: float |
| 29 | + prev_mouse_pos: tuple[int, int] |
| 30 | + |
| 31 | + def __init__(self, |
| 32 | + camera: 'Node', |
| 33 | + scene: 'Scene', |
| 34 | + viewport_size: tuple[int, int], |
| 35 | + camera_yfov: float, |
| 36 | + log_events: bool = False, |
| 37 | + camera_fov: float = 60.0, |
| 38 | + ): |
| 39 | + super().__init__(log_events) |
| 40 | + self.camera = camera |
| 41 | + self.scene = scene |
| 42 | + self.viewport_size = viewport_size |
| 43 | + self.camera_yfov = camera_yfov |
| 44 | + |
| 45 | + self.tan_half_fov = np.tan(0.5 * self.camera_yfov) |
| 46 | + self.prev_mouse_pos = tuple(np.array(viewport_size) / 2) |
| 47 | + |
| 48 | + @override |
| 49 | + def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> EVENT_HANDLE_STATE: |
| 50 | + super().on_mouse_motion(x, y, dx, dy) |
| 51 | + self.prev_mouse_pos = (x, y) |
| 52 | + |
| 53 | + @override |
| 54 | + def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int) -> EVENT_HANDLE_STATE: |
| 55 | + super().on_mouse_drag(x, y, dx, dy, buttons, modifiers) |
| 56 | + self.prev_mouse_pos = (x, y) |
| 57 | + |
| 58 | + @override |
| 59 | + def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> EVENT_HANDLE_STATE: |
| 60 | + super().on_mouse_press(x, y, button, modifiers) |
| 61 | + mouse_ray = self.screen_position_to_ray(x, y) |
| 62 | + # print(f"mouse_ray: {mouse_ray}") |
| 63 | + |
| 64 | + @override |
| 65 | + def on_draw(self) -> None: |
| 66 | + super().on_draw() |
| 67 | + if self.scene._visualizer is not None and self.scene._visualizer.viewer_lock is not None: |
| 68 | + self.scene.clear_debug_objects() |
| 69 | + |
| 70 | + ray_hit = self._raycast_against_ground(self.screen_position_to_ray(*self.prev_mouse_pos)) |
| 71 | + if ray_hit.is_hit: |
| 72 | + self.scene.draw_debug_sphere(ray_hit.position.v, 0.01, (0, 1, 0, 1)) |
| 73 | + self._draw_arrow(ray_hit.position, ray_hit.normal, (0, 1, 0, 1)) |
| 74 | + |
| 75 | + def screen_position_to_ray(self, x: float, y: float) -> Ray: |
| 76 | + # convert screen position to ray |
| 77 | + if True: |
| 78 | + x = x - 0.5 * self.viewport_size[0] |
| 79 | + y = y - 0.5 * self.viewport_size[1] |
| 80 | + x = 2.0 * x / self.viewport_size[1] * self.tan_half_fov |
| 81 | + y = 2.0 * y / self.viewport_size[1] * self.tan_half_fov |
| 82 | + else: |
| 83 | + # alternative way |
| 84 | + projection_matrix = self.camera.camera.get_projection_matrix(*self.viewport_size) |
| 85 | + x = x - 0.5 * self.viewport_size[0] |
| 86 | + y = y - 0.5 * self.viewport_size[1] |
| 87 | + x = 2.0 * x / self.viewport_size[0] / projection_matrix[0, 0] |
| 88 | + y = 2.0 * y / self.viewport_size[1] / projection_matrix[1, 1] |
| 89 | + |
| 90 | + # Note: ignoring pixel aspect ratio |
| 91 | + |
| 92 | + mtx = self.camera.matrix |
| 93 | + position = Vec3.from_float64(mtx[:3, 3]) |
| 94 | + forward = Vec3.from_float64(-mtx[:3, 2]) |
| 95 | + right = Vec3.from_float64(mtx[:3, 0]) |
| 96 | + up = Vec3.from_float64(mtx[:3, 1]) |
| 97 | + |
| 98 | + direction = forward + right * x + up * y |
| 99 | + return Ray(position, direction) |
| 100 | + |
| 101 | + def get_camera_ray(self) -> Ray: |
| 102 | + mtx = self.camera.matrix |
| 103 | + position = Vec3.from_float64(mtx[:3, 3]) |
| 104 | + forward = Vec3.from_float64(-mtx[:3, 2]) |
| 105 | + return Ray(position, forward) |
| 106 | + |
| 107 | + def _raycast_against_ground(self, ray: Ray) -> RayHit: |
| 108 | + ground_plane = Plane(Vec3.from_xyz(0, 0, 1), Vec3.zero()) |
| 109 | + return ground_plane.raycast(ray) |
| 110 | + |
| 111 | + def _draw_arrow( |
| 112 | + self, pos: Vec3, dir: Vec3, color: tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0), |
| 113 | + ) -> None: |
| 114 | + self.scene.draw_debug_arrow(pos.v, dir.v, color=color) # Only draws arrowhead -- bug? |
| 115 | + self.scene.draw_debug_line(pos.v, pos.v + dir.v, color=color) |
0 commit comments