1
- use std:: f32:: consts:: TAU ;
2
-
3
1
use bevy_app:: { CoreStage , Plugin } ;
4
- use bevy_asset:: { load_internal_asset, Assets , Handle , HandleUntyped } ;
2
+ use bevy_asset:: { load_internal_asset, Assets , HandleUntyped } ;
5
3
use bevy_ecs:: {
6
- prelude:: { Component , Entity } ,
4
+ component:: Component ,
5
+ entity:: Entity ,
7
6
query:: With ,
8
7
system:: { Commands , Query , Res , ResMut , Resource } ,
9
8
} ;
10
- use bevy_math:: { vec3, Quat , Vec2 , Vec3 } ;
11
9
use bevy_reflect:: TypeUuid ;
12
10
use bevy_render:: {
13
- prelude:: { Color , Mesh , SpatialBundle } ,
11
+ prelude:: { Mesh , SpatialBundle } ,
14
12
render_phase:: AddRenderCommand ,
15
13
render_resource:: { PrimitiveTopology , Shader , SpecializedMeshPipelines } ,
16
14
Extract , RenderApp , RenderStage ,
@@ -21,15 +19,19 @@ use bevy_pbr::{NotShadowCaster, NotShadowReceiver};
21
19
#[ cfg( feature = "bevy_sprite" ) ]
22
20
use bevy_sprite:: Mesh2dHandle ;
23
21
22
+ pub mod debug_draw;
23
+
24
24
#[ cfg( feature = "bevy_sprite" ) ]
25
25
pub mod pipeline_2d;
26
26
#[ cfg( feature = "bevy_pbr" ) ]
27
27
pub mod pipeline_3d;
28
28
29
+ use crate :: debug_draw:: DebugDraw ;
30
+
29
31
/// The `bevy_debug_draw` prelude.
30
32
pub mod prelude {
31
33
#[ doc( hidden) ]
32
- pub use crate :: { DebugDraw , DebugDrawConfig , DebugDrawPlugin } ;
34
+ pub use crate :: { debug_draw :: DebugDraw , DebugDrawConfig , DebugDrawPlugin } ;
33
35
}
34
36
35
37
pub const SHADER_HANDLE : HandleUntyped =
@@ -94,173 +96,8 @@ impl Default for DebugDrawConfig {
94
96
}
95
97
}
96
98
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
-
262
99
#[ derive( Component ) ]
263
- pub struct DebugDrawMesh ;
100
+ struct DebugDrawMesh ;
264
101
265
102
pub ( crate ) fn update (
266
103
config : Res < DebugDrawConfig > ,
@@ -298,7 +135,7 @@ pub(crate) fn update(
298
135
}
299
136
}
300
137
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.
302
139
pub ( crate ) fn extract (
303
140
mut commands : Commands ,
304
141
query : Extract < Query < Entity , With < DebugDrawMesh > > > ,
0 commit comments