Skip to content

Commit

Permalink
linestrip
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-blackbird committed Nov 14, 2022
1 parent 1aadc09 commit 63d13bb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
1 change: 1 addition & 0 deletions crates/bevy_debug_draw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
44 changes: 44 additions & 0 deletions crates/bevy_debug_draw/src/debug_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ impl DebugDraw {
]);
}

#[inline]
pub fn linestrip(&mut self, positions: impl IntoIterator<Item = Vec3>, 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<Item = (Vec3, Color)>) {
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) {
Expand Down Expand Up @@ -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<Item = Vec2>, color: Color) {
self.linestrip(positions.into_iter().map(|vec2| vec2.extend(0.)), color);
}

#[inline]
pub fn linestrip_gradient_2d(&mut self, positions: impl IntoIterator<Item = (Vec2, Color)>) {
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) {
Expand Down
23 changes: 16 additions & 7 deletions examples/2d/2d_debug_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@ fn setup(mut commands: Commands) {
}

fn system(mut draw: ResMut<DebugDraw>, time: Res<Time>) {
draw.line_2d(Vec2::ZERO, Vec2::new(-200., 300.), Color::RED);
draw.line_2d(Vec2::ZERO, Vec2::ONE * 300., Color::GREEN);
let sin = time.elapsed_seconds().sin() * 50.;
draw.line_2d(Vec2::Y * -sin, Vec2::splat(-80.), Color::RED);
draw.ray_2d(Vec2::Y * sin, Vec2::splat(80.), Color::GREEN);

// Triangle
draw.linestrip_gradient_2d([
(Vec2::Y * 300., Color::BLUE),
(Vec2::new(-255., -155.), Color::RED),
(Vec2::new(255., -155.), Color::GREEN),
(Vec2::Y * 300., Color::BLUE),
]);

draw.rect_2d(
Vec2::ZERO,
time.elapsed_seconds(),
Vec2::ONE * 300.,
time.elapsed_seconds() / 3.,
Vec2::splat(300.),
Color::BLACK,
);
// The circles have 24 line-segments by default.
// The circles have 32 line-segments by default.
draw.circle_2d(Vec2::ZERO, 120., Color::BLACK);
// You may want to increase this for larger circles.
draw.circle_segments = 64;
draw.circle_2d(Vec2::ZERO, 250., Color::NAVY);
draw.circle_segments = 24;
draw.circle_2d(Vec2::ZERO, 300., Color::NAVY);
draw.circle_segments = 32;
}
14 changes: 7 additions & 7 deletions examples/3d/3d_debug_draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ fn system(mut draw: ResMut<DebugDraw>, time: Res<Time>) {
draw.rect(
Vec3::new(time.elapsed_seconds().cos() * 2.5, 1., 0.),
Quat::from_rotation_y(PI / 2.),
Vec2::ONE * 2.,
Vec2::splat(2.),
Color::GREEN,
);

draw.sphere(Vec3::new(1., 0.5, 0.), 0.5, Color::RED);
let vector = Vec3::new(-3., (time.elapsed_seconds() * 3.).sin(), 0.);
draw.ray(Vec3::new(1., 0., 0.), vector, Color::BLUE);
draw.ray(Vec3::new(1., 0.5, 0.), vector, Color::BLUE);
draw.ray(Vec3::new(1., 1., 0.), vector, Color::BLUE);
for f in [0., 0.5, 1.] {
draw.ray(Vec3::new(1., f, 0.), vector, Color::BLUE);
}

// The circles have 24 line-segments by default.
// The circles have 32 line-segments by default.
draw.circle(Vec3::ZERO, Vec3::Y, 3., Color::BLACK);
// You may want to increase this for larger circles/spheres.
// You may want to increase this for larger circles or spheres.
draw.circle_segments = 64;
draw.circle(Vec3::ZERO, Vec3::Y, 3.1, Color::NAVY);
draw.sphere(Vec3::ZERO, 3.2, Color::BLACK);
draw.circle_segments = 24;
draw.circle_segments = 32;
}

fn rotate_camera(mut query: Query<&mut Transform, With<Camera>>, time: Res<Time>) {
Expand Down

0 comments on commit 63d13bb

Please sign in to comment.