Skip to content

Commit 63d13bb

Browse files
committed
linestrip
1 parent 1aadc09 commit 63d13bb

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

crates/bevy_debug_draw/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
99
keywords = ["bevy"]
1010

1111
[dependencies]
12+
# Bevy
1213
bevy_pbr = { path = "../bevy_pbr", version = "0.9.0-dev", optional = true }
1314
bevy_sprite = { path = "../bevy_sprite", version = "0.9.0-dev", optional = true }
1415
bevy_app = { path = "../bevy_app", version = "0.9.0-dev" }

crates/bevy_debug_draw/src/debug_draw.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@ impl DebugDraw {
5353
]);
5454
}
5555

56+
#[inline]
57+
pub fn linestrip(&mut self, positions: impl IntoIterator<Item = Vec3>, color: Color) {
58+
let mut count = 0;
59+
let positions = positions
60+
.into_iter()
61+
.map(|value| {
62+
count += 1;
63+
value.to_array()
64+
})
65+
.chain(iter::once([f32::NAN; 3]));
66+
67+
self.strip_positions.extend(positions);
68+
self.add_strip_color(color, count);
69+
}
70+
71+
#[inline]
72+
pub fn linestrip_gradient(&mut self, points: impl IntoIterator<Item = (Vec3, Color)>) {
73+
let points = points.into_iter();
74+
let (lower, _) = points.size_hint();
75+
self.strip_colors.reserve(lower + 1);
76+
self.strip_positions.reserve(lower + 1);
77+
78+
for (position, color) in points {
79+
self.strip_positions.push(position.to_array());
80+
self.strip_colors.push(color.as_linear_rgba_f32());
81+
}
82+
self.strip_positions.push([f32::NAN; 3]);
83+
self.strip_colors.push([f32::NAN; 4]);
84+
}
85+
5686
/// Draw a line from `start` to `start + vector`.
5787
#[inline]
5888
pub fn ray(&mut self, start: Vec3, vector: Vec3, color: Color) {
@@ -152,6 +182,20 @@ impl DebugDraw {
152182
self.line_gradient(start.extend(0.), end.extend(0.), start_color, end_color);
153183
}
154184

185+
#[inline]
186+
pub fn linestrip_2d(&mut self, positions: impl IntoIterator<Item = Vec2>, color: Color) {
187+
self.linestrip(positions.into_iter().map(|vec2| vec2.extend(0.)), color);
188+
}
189+
190+
#[inline]
191+
pub fn linestrip_gradient_2d(&mut self, positions: impl IntoIterator<Item = (Vec2, Color)>) {
192+
self.linestrip_gradient(
193+
positions
194+
.into_iter()
195+
.map(|(vec2, color)| (vec2.extend(0.), color)),
196+
);
197+
}
198+
155199
/// Draw a line from `start` to `start + vector`.
156200
#[inline]
157201
pub fn ray_2d(&mut self, start: Vec2, vector: Vec2, color: Color) {

examples/2d/2d_debug_draw.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,28 @@ fn setup(mut commands: Commands) {
1515
}
1616

1717
fn system(mut draw: ResMut<DebugDraw>, time: Res<Time>) {
18-
draw.line_2d(Vec2::ZERO, Vec2::new(-200., 300.), Color::RED);
19-
draw.line_2d(Vec2::ZERO, Vec2::ONE * 300., Color::GREEN);
18+
let sin = time.elapsed_seconds().sin() * 50.;
19+
draw.line_2d(Vec2::Y * -sin, Vec2::splat(-80.), Color::RED);
20+
draw.ray_2d(Vec2::Y * sin, Vec2::splat(80.), Color::GREEN);
21+
22+
// Triangle
23+
draw.linestrip_gradient_2d([
24+
(Vec2::Y * 300., Color::BLUE),
25+
(Vec2::new(-255., -155.), Color::RED),
26+
(Vec2::new(255., -155.), Color::GREEN),
27+
(Vec2::Y * 300., Color::BLUE),
28+
]);
2029

2130
draw.rect_2d(
2231
Vec2::ZERO,
23-
time.elapsed_seconds(),
24-
Vec2::ONE * 300.,
32+
time.elapsed_seconds() / 3.,
33+
Vec2::splat(300.),
2534
Color::BLACK,
2635
);
27-
// The circles have 24 line-segments by default.
36+
// The circles have 32 line-segments by default.
2837
draw.circle_2d(Vec2::ZERO, 120., Color::BLACK);
2938
// You may want to increase this for larger circles.
3039
draw.circle_segments = 64;
31-
draw.circle_2d(Vec2::ZERO, 250., Color::NAVY);
32-
draw.circle_segments = 24;
40+
draw.circle_2d(Vec2::ZERO, 300., Color::NAVY);
41+
draw.circle_segments = 32;
3342
}

examples/3d/3d_debug_draw.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ fn system(mut draw: ResMut<DebugDraw>, time: Res<Time>) {
2929
draw.rect(
3030
Vec3::new(time.elapsed_seconds().cos() * 2.5, 1., 0.),
3131
Quat::from_rotation_y(PI / 2.),
32-
Vec2::ONE * 2.,
32+
Vec2::splat(2.),
3333
Color::GREEN,
3434
);
3535

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

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

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

0 commit comments

Comments
 (0)