Skip to content

Commit ae414f3

Browse files
committed
Move DebugDraw to a separate file
1 parent 757796b commit ae414f3

File tree

3 files changed

+188
-175
lines changed

3 files changed

+188
-175
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
use std::f32::consts::TAU;
2+
3+
use bevy_asset::Handle;
4+
use bevy_ecs::system::Resource;
5+
use bevy_math::{vec3, Quat, Vec2, Vec3};
6+
use bevy_render::prelude::{Color, Mesh};
7+
8+
#[derive(Resource)]
9+
pub struct DebugDraw {
10+
positions: Vec<[f32; 3]>,
11+
colors: Vec<[f32; 4]>,
12+
pub(crate) mesh_handle: Option<Handle<Mesh>>,
13+
/// The amount of line segments to use when drawing a circle.
14+
///
15+
/// Defaults to `24`.
16+
pub circle_segments: u32,
17+
}
18+
19+
impl Default for DebugDraw {
20+
fn default() -> Self {
21+
DebugDraw {
22+
positions: Vec::new(),
23+
colors: Vec::new(),
24+
mesh_handle: None,
25+
circle_segments: 24,
26+
}
27+
}
28+
}
29+
30+
impl DebugDraw {
31+
/// Draw a line from `start` to `end`.
32+
pub fn line(&mut self, start: Vec3, end: Vec3, color: Color) {
33+
self.line_gradient(start, end, color, color);
34+
}
35+
36+
/// Draw a line from `start` to `end`.
37+
pub fn line_gradient(&mut self, start: Vec3, end: Vec3, start_color: Color, end_color: Color) {
38+
self.positions.extend([start.to_array(), end.to_array()]);
39+
self.colors.extend([
40+
start_color.as_linear_rgba_f32(),
41+
end_color.as_linear_rgba_f32(),
42+
]);
43+
}
44+
45+
/// Draw a line from `start` to `start + vector`.
46+
pub fn ray(&mut self, start: Vec3, vector: Vec3, color: Color) {
47+
self.ray_gradient(start, vector, color, color);
48+
}
49+
50+
/// Draw a line from `start` to `start + vector`.
51+
pub fn ray_gradient(
52+
&mut self,
53+
start: Vec3,
54+
vector: Vec3,
55+
start_color: Color,
56+
end_color: Color,
57+
) {
58+
self.line_gradient(start, start + vector, start_color, end_color);
59+
}
60+
61+
/// Draw a circle at `position` with the flat side facing `normal`.
62+
pub fn circle(&mut self, position: Vec3, normal: Vec3, radius: f32, color: Color) {
63+
let rotation = Quat::from_rotation_arc(Vec3::Z, normal);
64+
self.positions
65+
.extend((0..self.circle_segments).into_iter().flat_map(|i| {
66+
let mut angle = i as f32 * TAU / self.circle_segments as f32;
67+
let start = rotation * (Vec2::from(angle.sin_cos()) * radius).extend(0.) + position;
68+
69+
angle += TAU / self.circle_segments as f32;
70+
let end = rotation * (Vec2::from(angle.sin_cos()) * radius).extend(0.) + position;
71+
72+
[start.to_array(), end.to_array()]
73+
}));
74+
75+
self.colors.extend(
76+
std::iter::repeat(color.as_linear_rgba_f32()).take(self.circle_segments as usize * 2),
77+
);
78+
}
79+
80+
/// Draw a sphere.
81+
pub fn sphere(&mut self, position: Vec3, radius: f32, color: Color) {
82+
self.circle(position, Vec3::X, radius, color);
83+
self.circle(position, Vec3::Y, radius, color);
84+
self.circle(position, Vec3::Z, radius, color);
85+
}
86+
87+
/// Draw a rectangle.
88+
pub fn rect(&mut self, position: Vec3, rotation: Quat, size: Vec2, color: Color) {
89+
let half_size = size / 2.;
90+
let tl = (position + rotation * vec3(-half_size.x, half_size.y, 0.)).to_array();
91+
let tr = (position + rotation * vec3(half_size.x, half_size.y, 0.)).to_array();
92+
let bl = (position + rotation * vec3(-half_size.x, -half_size.y, 0.)).to_array();
93+
let br = (position + rotation * vec3(half_size.x, -half_size.y, 0.)).to_array();
94+
self.positions.extend([tl, tr, tr, br, br, bl, bl, tl]);
95+
self.colors
96+
.extend(std::iter::repeat(color.as_linear_rgba_f32()).take(8))
97+
}
98+
99+
/// Draw a box.
100+
pub fn cuboid(&mut self, position: Vec3, rotation: Quat, size: Vec3, color: Color) {
101+
let half_size = size / 2.;
102+
// Front
103+
let tlf = (position + rotation * vec3(-half_size.x, half_size.y, half_size.z)).to_array();
104+
let trf = (position + rotation * vec3(half_size.x, half_size.y, half_size.z)).to_array();
105+
let blf = (position + rotation * vec3(-half_size.x, -half_size.y, half_size.z)).to_array();
106+
let brf = (position + rotation * vec3(half_size.x, -half_size.y, half_size.z)).to_array();
107+
// Back
108+
let tlb = (position + rotation * vec3(-half_size.x, half_size.y, -half_size.z)).to_array();
109+
let trb = (position + rotation * vec3(half_size.x, half_size.y, -half_size.z)).to_array();
110+
let blb = (position + rotation * vec3(-half_size.x, -half_size.y, -half_size.z)).to_array();
111+
let brb = (position + rotation * vec3(half_size.x, -half_size.y, -half_size.z)).to_array();
112+
self.positions.extend([
113+
tlf, trf, trf, brf, brf, blf, blf, tlf, // Front
114+
tlb, trb, trb, brb, brb, blb, blb, tlb, // Back
115+
tlf, tlb, trf, trb, brf, brb, blf, blb, // Front to back
116+
]);
117+
self.colors
118+
.extend(std::iter::repeat(color.as_linear_rgba_f32()).take(24))
119+
}
120+
121+
/// Draw a line from `start` to `end`.
122+
pub fn line_2d(&mut self, start: Vec2, end: Vec2, color: Color) {
123+
self.line_gradient_2d(start, end, color, color);
124+
}
125+
126+
/// Draw a line from `start` to `end`.
127+
pub fn line_gradient_2d(
128+
&mut self,
129+
start: Vec2,
130+
end: Vec2,
131+
start_color: Color,
132+
end_color: Color,
133+
) {
134+
self.line_gradient(start.extend(0.), end.extend(0.), start_color, end_color);
135+
}
136+
137+
/// Draw a line from `start` to `start + vector`.
138+
pub fn ray_2d(&mut self, start: Vec2, vector: Vec2, color: Color) {
139+
self.ray(start.extend(0.), vector.extend(0.), color);
140+
}
141+
142+
// Draw a circle.
143+
pub fn circle_2d(&mut self, position: Vec2, radius: f32, color: Color) {
144+
self.circle(position.extend(0.), Vec3::Z, radius, color);
145+
}
146+
147+
/// Draw a rectangle.
148+
pub fn rect_2d(&mut self, position: Vec2, rotation: f32, size: Vec2, color: Color) {
149+
self.rect(
150+
position.extend(0.),
151+
Quat::from_rotation_z(rotation),
152+
size,
153+
color,
154+
);
155+
}
156+
157+
/// Clear everything drawn up to this point, this frame.
158+
pub fn clear(&mut self) {
159+
self.positions.clear();
160+
self.colors.clear();
161+
}
162+
163+
/// Take the positions and colors data from `self` and overwrite the `mesh`'s vertex positions and colors.
164+
pub fn update_mesh(&mut self, mesh: &mut Mesh) {
165+
mesh.insert_attribute(
166+
Mesh::ATTRIBUTE_POSITION,
167+
std::mem::take(&mut self.positions),
168+
);
169+
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, std::mem::take(&mut self.colors));
170+
}
171+
}

crates/bevy_debug_draw/src/lib.rs

Lines changed: 11 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use std::f32::consts::TAU;
2-
31
use bevy_app::{CoreStage, Plugin};
4-
use bevy_asset::{load_internal_asset, Assets, Handle, HandleUntyped};
2+
use bevy_asset::{load_internal_asset, Assets, HandleUntyped};
53
use bevy_ecs::{
6-
prelude::{Component, Entity},
4+
component::Component,
5+
entity::Entity,
76
query::With,
87
system::{Commands, Query, Res, ResMut, Resource},
98
};
10-
use bevy_math::{vec3, Quat, Vec2, Vec3};
119
use bevy_reflect::TypeUuid;
1210
use bevy_render::{
13-
prelude::{Color, Mesh, SpatialBundle},
11+
prelude::{Mesh, SpatialBundle},
1412
render_phase::AddRenderCommand,
1513
render_resource::{PrimitiveTopology, Shader, SpecializedMeshPipelines},
1614
Extract, RenderApp, RenderStage,
@@ -21,15 +19,19 @@ use bevy_pbr::{NotShadowCaster, NotShadowReceiver};
2119
#[cfg(feature = "bevy_sprite")]
2220
use bevy_sprite::Mesh2dHandle;
2321

22+
pub mod debug_draw;
23+
2424
#[cfg(feature = "bevy_sprite")]
2525
pub mod pipeline_2d;
2626
#[cfg(feature = "bevy_pbr")]
2727
pub mod pipeline_3d;
2828

29+
use crate::debug_draw::DebugDraw;
30+
2931
/// The `bevy_debug_draw` prelude.
3032
pub mod prelude {
3133
#[doc(hidden)]
32-
pub use crate::{DebugDraw, DebugDrawConfig, DebugDrawPlugin};
34+
pub use crate::{debug_draw::DebugDraw, DebugDrawConfig, DebugDrawPlugin};
3335
}
3436

3537
pub const SHADER_HANDLE: HandleUntyped =
@@ -94,173 +96,8 @@ impl Default for DebugDrawConfig {
9496
}
9597
}
9698

97-
#[derive(Resource)]
98-
pub struct DebugDraw {
99-
positions: Vec<[f32; 3]>,
100-
colors: Vec<[f32; 4]>,
101-
mesh_handle: Option<Handle<Mesh>>,
102-
/// The amount of line segments to use when drawing a circle.
103-
///
104-
/// Defaults to `24`.
105-
pub circle_segments: u32,
106-
}
107-
108-
impl Default for DebugDraw {
109-
fn default() -> Self {
110-
DebugDraw {
111-
positions: Vec::new(),
112-
colors: Vec::new(),
113-
mesh_handle: None,
114-
circle_segments: 24,
115-
}
116-
}
117-
}
118-
119-
impl DebugDraw {
120-
/// Draw a line from `start` to `end`.
121-
pub fn line(&mut self, start: Vec3, end: Vec3, color: Color) {
122-
self.line_gradient(start, end, color, color);
123-
}
124-
125-
/// Draw a line from `start` to `end`.
126-
pub fn line_gradient(&mut self, start: Vec3, end: Vec3, start_color: Color, end_color: Color) {
127-
self.positions.extend([start.to_array(), end.to_array()]);
128-
self.colors.extend([
129-
start_color.as_linear_rgba_f32(),
130-
end_color.as_linear_rgba_f32(),
131-
]);
132-
}
133-
134-
/// Draw a line from `start` to `start + vector`.
135-
pub fn ray(&mut self, start: Vec3, vector: Vec3, color: Color) {
136-
self.ray_gradient(start, vector, color, color);
137-
}
138-
139-
/// Draw a line from `start` to `start + vector`.
140-
pub fn ray_gradient(
141-
&mut self,
142-
start: Vec3,
143-
vector: Vec3,
144-
start_color: Color,
145-
end_color: Color,
146-
) {
147-
self.line_gradient(start, start + vector, start_color, end_color);
148-
}
149-
150-
/// Draw a circle at `position` with the flat side facing `normal`.
151-
pub fn circle(&mut self, position: Vec3, normal: Vec3, radius: f32, color: Color) {
152-
let rotation = Quat::from_rotation_arc(Vec3::Z, normal);
153-
self.positions
154-
.extend((0..self.circle_segments).into_iter().flat_map(|i| {
155-
let mut angle = i as f32 * TAU / self.circle_segments as f32;
156-
let start = rotation * (Vec2::from(angle.sin_cos()) * radius).extend(0.) + position;
157-
158-
angle += TAU / self.circle_segments as f32;
159-
let end = rotation * (Vec2::from(angle.sin_cos()) * radius).extend(0.) + position;
160-
161-
[start.to_array(), end.to_array()]
162-
}));
163-
164-
self.colors.extend(
165-
std::iter::repeat(color.as_linear_rgba_f32()).take(self.circle_segments as usize * 2),
166-
);
167-
}
168-
169-
/// Draw a sphere.
170-
pub fn sphere(&mut self, position: Vec3, radius: f32, color: Color) {
171-
self.circle(position, Vec3::X, radius, color);
172-
self.circle(position, Vec3::Y, radius, color);
173-
self.circle(position, Vec3::Z, radius, color);
174-
}
175-
176-
/// Draw a rectangle.
177-
pub fn rect(&mut self, position: Vec3, rotation: Quat, size: Vec2, color: Color) {
178-
let half_size = size / 2.;
179-
let tl = (position + rotation * vec3(-half_size.x, half_size.y, 0.)).to_array();
180-
let tr = (position + rotation * vec3(half_size.x, half_size.y, 0.)).to_array();
181-
let bl = (position + rotation * vec3(-half_size.x, -half_size.y, 0.)).to_array();
182-
let br = (position + rotation * vec3(half_size.x, -half_size.y, 0.)).to_array();
183-
self.positions.extend([tl, tr, tr, br, br, bl, bl, tl]);
184-
self.colors
185-
.extend(std::iter::repeat(color.as_linear_rgba_f32()).take(8))
186-
}
187-
188-
/// Draw a box.
189-
pub fn cuboid(&mut self, position: Vec3, rotation: Quat, size: Vec3, color: Color) {
190-
let half_size = size / 2.;
191-
// Front
192-
let tlf = (position + rotation * vec3(-half_size.x, half_size.y, half_size.z)).to_array();
193-
let trf = (position + rotation * vec3(half_size.x, half_size.y, half_size.z)).to_array();
194-
let blf = (position + rotation * vec3(-half_size.x, -half_size.y, half_size.z)).to_array();
195-
let brf = (position + rotation * vec3(half_size.x, -half_size.y, half_size.z)).to_array();
196-
// Back
197-
let tlb = (position + rotation * vec3(-half_size.x, half_size.y, -half_size.z)).to_array();
198-
let trb = (position + rotation * vec3(half_size.x, half_size.y, -half_size.z)).to_array();
199-
let blb = (position + rotation * vec3(-half_size.x, -half_size.y, -half_size.z)).to_array();
200-
let brb = (position + rotation * vec3(half_size.x, -half_size.y, -half_size.z)).to_array();
201-
self.positions.extend([
202-
tlf, trf, trf, brf, brf, blf, blf, tlf, // Front
203-
tlb, trb, trb, brb, brb, blb, blb, tlb, // Back
204-
tlf, tlb, trf, trb, brf, brb, blf, blb, // Front to back
205-
]);
206-
self.colors
207-
.extend(std::iter::repeat(color.as_linear_rgba_f32()).take(24))
208-
}
209-
210-
/// Draw a line from `start` to `end`.
211-
pub fn line_2d(&mut self, start: Vec2, end: Vec2, color: Color) {
212-
self.line_gradient_2d(start, end, color, color);
213-
}
214-
215-
/// Draw a line from `start` to `end`.
216-
pub fn line_gradient_2d(
217-
&mut self,
218-
start: Vec2,
219-
end: Vec2,
220-
start_color: Color,
221-
end_color: Color,
222-
) {
223-
self.line_gradient(start.extend(0.), end.extend(0.), start_color, end_color);
224-
}
225-
226-
/// Draw a line from `start` to `start + vector`.
227-
pub fn ray_2d(&mut self, start: Vec2, vector: Vec2, color: Color) {
228-
self.ray(start.extend(0.), vector.extend(0.), color);
229-
}
230-
231-
// Draw a circle.
232-
pub fn circle_2d(&mut self, position: Vec2, radius: f32, color: Color) {
233-
self.circle(position.extend(0.), Vec3::Z, radius, color);
234-
}
235-
236-
/// Draw a rectangle.
237-
pub fn rect_2d(&mut self, position: Vec2, rotation: f32, size: Vec2, color: Color) {
238-
self.rect(
239-
position.extend(0.),
240-
Quat::from_rotation_z(rotation),
241-
size,
242-
color,
243-
);
244-
}
245-
246-
/// Clear everything drawn up to this point, this frame.
247-
pub fn clear(&mut self) {
248-
self.positions.clear();
249-
self.colors.clear();
250-
}
251-
252-
/// Take the positions and colors data from `self` and overwrite the `mesh`'s vertex positions and colors.
253-
pub fn update_mesh(&mut self, mesh: &mut Mesh) {
254-
mesh.insert_attribute(
255-
Mesh::ATTRIBUTE_POSITION,
256-
std::mem::take(&mut self.positions),
257-
);
258-
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, std::mem::take(&mut self.colors));
259-
}
260-
}
261-
26299
#[derive(Component)]
263-
pub struct DebugDrawMesh;
100+
struct DebugDrawMesh;
264101

265102
pub(crate) fn update(
266103
config: Res<DebugDrawConfig>,
@@ -298,7 +135,7 @@ pub(crate) fn update(
298135
}
299136
}
300137

301-
/// Move the DebugDrawMesh marker Component and the DebugDrawConfig Resource to the render context.
138+
/// Move the [`DebugDrawMesh`] marker Component and the [`DebugDrawConfig`] Resource to the render context.
302139
pub(crate) fn extract(
303140
mut commands: Commands,
304141
query: Extract<Query<Entity, With<DebugDrawMesh>>>,

0 commit comments

Comments
 (0)