diff options
-rw-r--r-- | client/src/ui.rs | 70 |
1 files changed, 45 insertions, 25 deletions
diff --git a/client/src/ui.rs b/client/src/ui.rs index ffd840d..be206b5 100644 --- a/client/src/ui.rs +++ b/client/src/ui.rs @@ -19,7 +19,7 @@ use egui::{ Context, Event, ImageData, TextureId, ViewportId, ViewportInfo, epaint::{ImageDelta, Primitive, Vertex}, }; -use glam::{Affine3A, Mat2, Mat3, Mat4, Vec2, vec2}; +use glam::{Affine3A, Mat2, Mat3, Mat4, Vec2, Vec3Swizzles, Vec4Swizzles, vec2, vec4}; use log::info; use rand::random; use std::{ @@ -309,23 +309,7 @@ impl UiRenderer { rpass.set_pipeline(&self.pipeline); - let _screen_size = vec2( - surface_configuration.width as f32, - surface_configuration.height as f32, - ); - let mut raw_input = egui::RawInput::default(); - if input_state.cursor_pos != self.last_pointer { - raw_input.events.push(Event::PointerMoved(egui::Pos2::new( - input_state.cursor_pos.x, - input_state.cursor_pos.y, - ))); - self.last_pointer = input_state.cursor_pos; - } - raw_input - .events - .extend(input_state.egui_events.iter().cloned()); - raw_input.viewport_id = surfaces.keys().next().copied().unwrap(); raw_input.viewports = surfaces .keys() @@ -339,6 +323,50 @@ impl UiRenderer { let mut surfaces_closed = Vec::new(); for (viewport_id, surf) in surfaces.iter_mut() { + let scale = 0.03; + let projection = projection + * Mat4::from_translation(surf.transform.translation.into()) + * Mat4::from_mat3a(surf.transform.matrix3) + * Mat4::from_mat3(Mat3::from_mat2(Mat2::from_cols_array(&[ + scale, 0., 0., -scale, + ]))); + + let screen_size = vec2( + surface_configuration.width as f32, + surface_configuration.height as f32, + ); + + let mouse_xy_clip = (input_state.cursor_pos / screen_size) * 2. - 1.; + + let iproject = projection.inverse(); + + let mut mouse_world_1 = iproject * vec4(mouse_xy_clip.x, mouse_xy_clip.y, 0.0, 1.0); + let mut mouse_world_2 = iproject * vec4(mouse_xy_clip.x, mouse_xy_clip.y, 1.0, 1.0); + + mouse_world_1 /= mouse_world_1.w; + mouse_world_2 /= mouse_world_2.w; + + let mouse_world_1 = mouse_world_1.xyz(); + let mouse_world_2 = mouse_world_2.xyz(); + + let ray_norm = (mouse_world_2 - mouse_world_1).normalize(); + let ray_t = mouse_world_1.z / ray_norm.z; + let ray_hit = mouse_world_1 + ray_norm * ray_t; + + let cursor_pos = ray_hit.xy(); + + let mut raw_input = raw_input.clone(); + if cursor_pos != self.last_pointer { + raw_input.events.push(Event::PointerMoved(egui::Pos2::new( + cursor_pos.x, + cursor_pos.y, + ))); + self.last_pointer = cursor_pos; + } + raw_input + .events + .extend(input_state.egui_events.iter().cloned()); + let mut close = false; let full_output = self.ctx.run(raw_input.clone(), |ctx| { close = !(surf.content)(ctx); @@ -439,14 +467,6 @@ impl UiRenderer { assert_eq!(index_count, index_offset); assert_eq!(vertex_count, vertex_offset); - let scale = 0.03; - let projection = projection - * Mat4::from_translation(surf.transform.translation.into()) - * Mat4::from_mat3a(surf.transform.matrix3) - * Mat4::from_mat3(Mat3::from_mat2(Mat2::from_cols_array(&[ - scale, 0., 0., -scale, - ]))); - let projection = projection.to_cols_array().map(|v| v.to_le_bytes()); rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); |