From 7ed1ba8766e968f9b860c95c9640a32aee85b030 Mon Sep 17 00:00:00 2001 From: Brian Schwind Date: Sun, 21 Jan 2024 19:15:08 +0900 Subject: [PATCH] Toggle hidden-line drawing with the X key in the viewer app (#162) --- crates/viewer/src/edge_drawer.rs | 29 +++++++++++++++++++---------- crates/viewer/src/main.rs | 8 +++++++- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/crates/viewer/src/edge_drawer.rs b/crates/viewer/src/edge_drawer.rs index 7d328bbd..51f7c496 100644 --- a/crates/viewer/src/edge_drawer.rs +++ b/crates/viewer/src/edge_drawer.rs @@ -29,6 +29,7 @@ pub struct EdgeDrawer { bind_groups: BindGroups, screen_width: u32, screen_height: u32, + draw_back_edges: bool, } impl EdgeDrawer { @@ -61,6 +62,7 @@ impl EdgeDrawer { bind_groups, screen_width, screen_height, + draw_back_edges: false, } } @@ -69,6 +71,10 @@ impl EdgeDrawer { self.screen_height = screen_height; } + pub fn toggle_back_edge_drawing(&mut self) { + self.draw_back_edges = !self.draw_back_edges; + } + #[allow(clippy::too_many_arguments)] pub fn draw( &self, @@ -125,19 +131,22 @@ impl EdgeDrawer { timestamp_writes: None, }); - // Render dashed line strips - render_pass.set_pipeline(&self.dashed_line_strip_pipeline); render_pass.set_vertex_buffer(0, self.buffers.round_strip_geometry.slice(..)); render_pass.set_vertex_buffer(1, rendered_line.vertex_buf.slice(..)); - render_pass.set_bind_group(0, &self.bind_groups.dashed_vertex_uniform, &[]); - - let mut offset = 0usize; - let vertex_count = self.buffers.round_strip_geometry_len as u32; - for line_strip_size in &rendered_line.line_sizes { - let range = (offset as u32)..(offset + line_strip_size - 1) as u32; - offset += line_strip_size; - render_pass.draw(0..vertex_count, range); + // Render dashed line strips + if self.draw_back_edges { + render_pass.set_pipeline(&self.dashed_line_strip_pipeline); + render_pass.set_bind_group(0, &self.bind_groups.dashed_vertex_uniform, &[]); + + let mut offset = 0usize; + let vertex_count = self.buffers.round_strip_geometry_len as u32; + + for line_strip_size in &rendered_line.line_sizes { + let range = (offset as u32)..(offset + line_strip_size - 1) as u32; + offset += line_strip_size; + render_pass.draw(0..vertex_count, range); + } } // Render solid line strips diff --git a/crates/viewer/src/main.rs b/crates/viewer/src/main.rs index f09565b7..aff88373 100644 --- a/crates/viewer/src/main.rs +++ b/crates/viewer/src/main.rs @@ -290,12 +290,18 @@ impl GameApp for ViewerApp { self.camera.zoom(-zoom_delta); }, WindowEvent::KeyboardInput { - event: KeyEvent { physical_key: PhysicalKey::Code(key_code), .. }, + event: + KeyEvent { + physical_key: PhysicalKey::Code(key_code), + state: ElementState::Pressed, + .. + }, .. } => match key_code { KeyCode::Escape => window_target.exit(), KeyCode::KeyP => self.camera.use_perspective(), KeyCode::KeyO => self.camera.use_orthographic(), + KeyCode::KeyX => self.line_drawer.toggle_back_edge_drawing(), _ => {}, }, _ => {},