From 63d13bb770e0b68450af4643bcd4f32cd5b84653 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Mon, 14 Nov 2022 23:33:46 +0100 Subject: [PATCH] linestrip --- crates/bevy_debug_draw/Cargo.toml | 1 + crates/bevy_debug_draw/src/debug_draw.rs | 44 ++++++++++++++++++++++++ examples/2d/2d_debug_draw.rs | 23 +++++++++---- examples/3d/3d_debug_draw.rs | 14 ++++---- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/crates/bevy_debug_draw/Cargo.toml b/crates/bevy_debug_draw/Cargo.toml index 8e305b8a120cae..10d21250ef442f 100644 --- a/crates/bevy_debug_draw/Cargo.toml +++ b/crates/bevy_debug_draw/Cargo.toml @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0" keywords = ["bevy"] [dependencies] +# Bevy bevy_pbr = { path = "../bevy_pbr", version = "0.9.0-dev", optional = true } bevy_sprite = { path = "../bevy_sprite", version = "0.9.0-dev", optional = true } bevy_app = { path = "../bevy_app", version = "0.9.0-dev" } diff --git a/crates/bevy_debug_draw/src/debug_draw.rs b/crates/bevy_debug_draw/src/debug_draw.rs index 103a8af536b15d..3472f4e38e5f15 100644 --- a/crates/bevy_debug_draw/src/debug_draw.rs +++ b/crates/bevy_debug_draw/src/debug_draw.rs @@ -53,6 +53,36 @@ impl DebugDraw { ]); } + #[inline] + pub fn linestrip(&mut self, positions: impl IntoIterator, color: Color) { + let mut count = 0; + let positions = positions + .into_iter() + .map(|value| { + count += 1; + value.to_array() + }) + .chain(iter::once([f32::NAN; 3])); + + self.strip_positions.extend(positions); + self.add_strip_color(color, count); + } + + #[inline] + pub fn linestrip_gradient(&mut self, points: impl IntoIterator) { + let points = points.into_iter(); + let (lower, _) = points.size_hint(); + self.strip_colors.reserve(lower + 1); + self.strip_positions.reserve(lower + 1); + + for (position, color) in points { + self.strip_positions.push(position.to_array()); + self.strip_colors.push(color.as_linear_rgba_f32()); + } + self.strip_positions.push([f32::NAN; 3]); + self.strip_colors.push([f32::NAN; 4]); + } + /// Draw a line from `start` to `start + vector`. #[inline] pub fn ray(&mut self, start: Vec3, vector: Vec3, color: Color) { @@ -152,6 +182,20 @@ impl DebugDraw { self.line_gradient(start.extend(0.), end.extend(0.), start_color, end_color); } + #[inline] + pub fn linestrip_2d(&mut self, positions: impl IntoIterator, color: Color) { + self.linestrip(positions.into_iter().map(|vec2| vec2.extend(0.)), color); + } + + #[inline] + pub fn linestrip_gradient_2d(&mut self, positions: impl IntoIterator) { + self.linestrip_gradient( + positions + .into_iter() + .map(|(vec2, color)| (vec2.extend(0.), color)), + ); + } + /// Draw a line from `start` to `start + vector`. #[inline] pub fn ray_2d(&mut self, start: Vec2, vector: Vec2, color: Color) { diff --git a/examples/2d/2d_debug_draw.rs b/examples/2d/2d_debug_draw.rs index f69cc4dfeff621..024c44d6c85571 100644 --- a/examples/2d/2d_debug_draw.rs +++ b/examples/2d/2d_debug_draw.rs @@ -15,19 +15,28 @@ fn setup(mut commands: Commands) { } fn system(mut draw: ResMut, time: Res