1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Debug Drawing library
use crate::pod::IntoPod;
use amethyst_core::{
    ecs::{Component, DenseVecStorage},
    math::{Point2, Point3, UnitQuaternion, Vector2, Vector3},
};
use palette::Srgba;
use rendy::mesh::{AsVertex, Color, PosColor, VertexFormat};

/// Debug lines are stored as a pair of position and color.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[repr(C)]
pub struct DebugLine {
    start: PosColor,
    end: PosColor,
}

impl AsVertex for DebugLine {
    fn vertex() -> VertexFormat {
        VertexFormat::new((PosColor::vertex(), PosColor::vertex()))
    }
}

impl DebugLine {
    fn new(start: PosColor, end: PosColor) -> Self {
        Self { start, end }
    }
}

/// Parameters for renderer of debug lines. The params affect all lines.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct DebugLinesParams {
    /// Width of lines in screen space pixels, default is 1.0 pixel
    pub line_width: f32,
}

impl Default for DebugLinesParams {
    fn default() -> Self {
        DebugLinesParams { line_width: 1.0 }
    }
}

/// Component that stores persistent debug lines to be rendered in DebugLinesPass draw pass.
/// The vector can only be cleared manually.
#[derive(Debug, Default)]
pub struct DebugLinesComponent {
    /// Lines to be rendered
    lines: Vec<DebugLine>,
}

impl Component for DebugLinesComponent {
    type Storage = DenseVecStorage<Self>;
}

impl DebugLinesComponent {
    /// Creates a new debug lines component with an empty DebugLine vector.
    pub fn new() -> DebugLinesComponent {
        Self::default()
    }

    /// Builder method to pre-allocate a number of lines.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            lines: Vec::with_capacity(capacity),
        }
    }

    /// Adds a line to be rendered by giving a position and a direction.
    pub fn add_direction(&mut self, position: Point3<f32>, direction: Vector3<f32>, color: Srgba) {
        self.add_line(position, position + direction, color);
    }

    /// Adds a line to be rendered by giving a start and an end position.
    pub fn add_line(&mut self, start: Point3<f32>, end: Point3<f32>, color: Srgba) {
        self.add_gradient_line(start, end, color, color);
    }

    /// Adds a line to be rendered by giving a start and an end position with separate start and end colors.
    pub fn add_gradient_line(
        &mut self,
        start: Point3<f32>,
        end: Point3<f32>,
        start_color: Srgba,
        end_color: Srgba,
    ) {
        let vertex = DebugLine::new(
            PosColor {
                position: start.to_homogeneous().xyz().into(),
                color: Color(start_color.into_pod()),
            },
            PosColor {
                position: end.to_homogeneous().xyz().into(),
                color: Color(end_color.into_pod()),
            },
        );
        self.lines.push(vertex);
    }

    /// Adds multiple lines that form a rectangle to be rendered by giving a Z coordinate, a min and a max position.
    ///
    /// This rectangle is aligned to the XY plane.
    pub fn add_rectangle_2d(&mut self, min: Point2<f32>, max: Point2<f32>, z: f32, color: Srgba) {
        self.add_line(
            [min[0], min[1], z].into(),
            [max[0], min[1], z].into(),
            color,
        );
        self.add_line(
            [min[0], min[1], z].into(),
            [min[0], max[1], z].into(),
            color,
        );
        self.add_line(
            [max[0], min[1], z].into(),
            [max[0], max[1], z].into(),
            color,
        );
        self.add_line(
            [min[0], max[1], z].into(),
            [max[0], max[1], z].into(),
            color,
        );
    }

    /// Adds multiple lines that form a rotated rectangle to be rendered by giving a Z coordinate, a rotation, a min and a max position.
    pub fn add_rotated_rectangle(
        &mut self,
        min: Point2<f32>,
        max: Point2<f32>,
        z: f32,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        let center = (min + Vector2::new(max[0], max[1])) / 2.0;
        let center = Vector3::new(center[0], center[1], z);

        let top_left = Point3::new(min[0], min[1], z);
        let top_right = Point3::new(min[0], max[1], z);
        let bottom_left = Point3::new(max[0], min[1], z);
        let bottom_right = Point3::new(max[0], max[1], z);

        let top_left = rotation * (top_left - center) + center;
        let top_right = rotation * (top_right - center) + center;
        let bottom_left = rotation * (bottom_left - center) + center;
        let bottom_right = rotation * (bottom_right - center) + center;

        self.add_line(top_left, top_right, color);
        self.add_line(top_left, bottom_left, color);
        self.add_line(top_right, bottom_right, color);
        self.add_line(bottom_left, bottom_right, color);
    }

    /// Adds multiple lines that form a box to be rendered by giving a min and a max position.
    ///
    /// This box is an axis aligned box.
    pub fn add_box(&mut self, min: Point3<f32>, max: Point3<f32>, color: Srgba) {
        self.add_rectangle_2d(min.xy(), max.xy(), min[2], color);
        self.add_rectangle_2d(min.xy(), max.xy(), max[2], color);
        self.add_line(min, [min[0], min[1], max[2]].into(), color);
        self.add_line(
            [max[0], min[1], min[2]].into(),
            [max[0], min[1], max[2]].into(),
            color,
        );
        self.add_line(
            [min[0], max[1], min[2]].into(),
            [min[0], max[1], max[2]].into(),
            color,
        );
        self.add_line([max[0], max[1], min[2]].into(), max, color);
    }

    /// Adds multiple lines that form a rotated box to be rendered by giving a rotation, a min and a max position.
    pub fn add_rotated_box(
        &mut self,
        min: Point3<f32>,
        max: Point3<f32>,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        let center = (min + Vector3::from([max[0], max[1], max[2]])) / 2.0;
        let center = Vector3::new(center[0], center[1], center[2]);

        let top_left_back = Point3::new(min[0], min[1], min[2]);
        let top_right_back = Point3::new(min[0], max[1], min[2]);
        let bottom_left_back = Point3::new(max[0], min[1], min[2]);
        let bottom_right_back = Point3::new(max[0], max[1], min[2]);

        let top_left_back = rotation * (top_left_back - center) + center;
        let top_right_back = rotation * (top_right_back - center) + center;
        let bottom_left_back = rotation * (bottom_left_back - center) + center;
        let bottom_right_back = rotation * (bottom_right_back - center) + center;

        let top_left_front = Point3::new(min[0], min[1], max[2]);
        let top_right_front = Point3::new(min[0], max[1], max[2]);
        let bottom_left_front = Point3::new(max[0], min[1], max[2]);
        let bottom_right_front = Point3::new(max[0], max[1], max[2]);

        let top_left_front = rotation * (top_left_front - center) + center;
        let top_right_front = rotation * (top_right_front - center) + center;
        let bottom_left_front = rotation * (bottom_left_front - center) + center;
        let bottom_right_front = rotation * (bottom_right_front - center) + center;

        self.add_line(top_left_back, top_right_back, color);
        self.add_line(top_left_back, bottom_left_back, color);
        self.add_line(top_left_back, top_left_front, color);
        self.add_line(top_right_back, bottom_right_back, color);
        self.add_line(top_right_back, top_right_front, color);
        self.add_line(bottom_left_back, bottom_right_back, color);
        self.add_line(bottom_left_back, bottom_left_front, color);
        self.add_line(top_left_front, top_right_front, color);
        self.add_line(top_left_front, bottom_left_front, color);
        self.add_line(bottom_right_front, top_right_front, color);
        self.add_line(bottom_right_front, bottom_left_front, color);
        self.add_line(bottom_right_front, bottom_right_back, color);
    }

    /// Adds multiple lines that form a circle to be rendered by giving a center, a radius and an amount of points.
    ///
    /// This circle is aligned to the XY plane.
    pub fn add_circle_2d(&mut self, center: Point3<f32>, radius: f32, points: u32, color: Srgba) {
        let mut prev = None;

        for i in 0..=points {
            let a = std::f32::consts::PI * 2.0 / (points as f32) * (i as f32);
            let x = radius * a.cos();
            let y = radius * a.sin();
            let point = [center[0] + x, center[1] + y, center[2]].into();

            if let Some(prev) = prev {
                self.add_line(prev, point, color);
            }

            prev = Some(point);
        }
    }

    /// Adds multiple lines that form a rotated circle to be rendered by giving a center, a radius, an amount of points and a rotation.
    pub fn add_rotated_circle(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        points: u32,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        let mut prev = None;

        for i in 0..=points {
            let a = std::f32::consts::PI * 2.0 / (points as f32) * (i as f32);
            let x = radius * a.cos();
            let y = radius * a.sin();
            let point = Vector3::new(x, y, 0.0);
            let point = center + rotation * point;

            if let Some(prev) = prev {
                self.add_line(prev, point, color);
            }

            prev = Some(point);
        }
    }

    /// Adds multiple lines that form a sphere to be rendered by giving a center, a radius, an amount of vertical points and an amount of horizontal points.
    pub fn add_sphere(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        horizontal_points: u32,
        vertical_points: u32,
        color: Srgba,
    ) {
        let mut prev_row = Vec::new();

        for i in 0..=horizontal_points {
            let lon = std::f32::consts::PI / (horizontal_points as f32) * (i as f32);
            let mut new_prev_row = Vec::new();

            for j in 0..=vertical_points {
                let lat = std::f32::consts::PI * 2.0 / (vertical_points as f32) * (j as f32);
                let x = radius * lon.sin() * lat.cos();
                let y = radius * lon.sin() * lat.sin();
                let z = radius * lon.cos();
                let point = center + Vector3::new(x, y, z);

                if !new_prev_row.is_empty() {
                    self.add_line(new_prev_row[(j - 1) as usize], point, color);
                }

                if !prev_row.is_empty() {
                    self.add_line(prev_row[j as usize], point, color);
                }

                new_prev_row.push(point);
            }

            prev_row = new_prev_row;
        }
    }

    /// Adds multiple lines that form a cylinder to be rendered by giving a center, a radius, a height and an amount of points.
    ///
    /// This cylinder is aligned to the y axis.
    pub fn add_cylinder(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        height: f32,
        points: u32,
        color: Srgba,
    ) {
        let mut prev: Option<(Point3<f32>, Point3<f32>)> = None;

        for i in 0..=points {
            let a = std::f32::consts::PI * 2.0 / (points as f32) * (i as f32);
            let x = radius * a.cos();
            let z = radius * a.sin();
            let point1: Point3<f32> =
                [center[0] + x, center[1] - height / 2.0, center[2] + z].into();
            let point2: Point3<f32> = [point1[0], point1[1] + height, point1[2]].into();

            self.add_line(point1, point2, color);

            if let Some(prev) = prev {
                self.add_line(prev.0, point1, color);
                self.add_line(prev.1, point2, color);
            }

            prev = Some((point1, point2));
        }
    }

    /// Adds multiple lines that form a rotated cylinder to be rendered by giving a center, a radius, a height, an amount of points and a rotation.
    pub fn add_rotated_cylinder(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        height: f32,
        points: u32,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        let mut prev: Option<(Point3<f32>, Point3<f32>)> = None;
        let center = Vector3::new(center[0], center[1], center[2]);

        for i in 0..=points {
            let a = std::f32::consts::PI * 2.0 / (points as f32) * (i as f32);
            let x = radius * a.cos();
            let z = radius * a.sin();
            let point1: Point3<f32> =
                [center[0] + x, center[1] - height / 2.0, center[2] + z].into();
            let point2: Point3<f32> = [point1[0], point1[1] + height, point1[2]].into();
            let point1 = rotation * (point1 - center) + center;
            let point2 = rotation * (point2 - center) + center;

            self.add_line(point1, point2, color);

            if let Some(prev) = prev {
                self.add_line(prev.0, point1, color);
                self.add_line(prev.1, point2, color);
            }

            prev = Some((point1, point2));
        }
    }

    /// Clears lines buffer.
    ///
    /// As lines are persistent, it's necessary to use this function for updating or deleting lines.
    pub fn clear(&mut self) {
        self.lines.clear();
    }

    pub(crate) fn lines(&self) -> &[DebugLine] {
        &self.lines
    }
}

/// Resource that stores non-persistent debug lines to be rendered in DebugLinesPass draw pass.
/// The vector is automatically cleared after being rendered.
#[derive(Debug, Default)]
pub struct DebugLines {
    /// Lines to be rendered
    inner: DebugLinesComponent,
}

impl DebugLines {
    /// Creates a new debug lines component with an empty DebugLine vector.
    pub fn new() -> DebugLines {
        Self {
            inner: Default::default(),
        }
    }

    /// Submits a line to be rendered by giving a position and a direction.
    pub fn draw_direction(&mut self, position: Point3<f32>, direction: Vector3<f32>, color: Srgba) {
        self.inner.add_direction(position, direction, color);
    }

    /// Submits a line to be rendered by giving a start and an end position with separate start and end colors.
    pub fn draw_gradient_line(
        &mut self,
        start: Point3<f32>,
        end: Point3<f32>,
        start_color: Srgba,
        end_color: Srgba,
    ) {
        self.inner
            .add_gradient_line(start, end, start_color, end_color);
    }

    /// Submits a line to be rendered by giving a start and an end position.
    pub fn draw_line(&mut self, start: Point3<f32>, end: Point3<f32>, color: Srgba) {
        self.inner.add_line(start, end, color);
    }

    /// Submits multiple lines that form a rectangle to be rendered by giving a Z coordinate, a min and a max position.
    ///
    /// This rectangle is aligned to the XY plane.
    pub fn draw_rectangle(&mut self, min: Point2<f32>, max: Point2<f32>, z: f32, color: Srgba) {
        self.inner.add_rectangle_2d(min, max, z, color);
    }

    /// Submits multiple lines that form a rotated rectangle to be rendered by giving a Z coordinate, a rotation, a min and a max position.
    pub fn draw_rotated_rectangle(
        &mut self,
        min: Point2<f32>,
        max: Point2<f32>,
        z: f32,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        self.inner
            .add_rotated_rectangle(min, max, z, rotation, color);
    }

    /// Submits multiple lines that form a box to be rendered by giving a min and a max position.
    ///
    /// This box is an axis aligned box.
    pub fn draw_box(&mut self, min: Point3<f32>, max: Point3<f32>, color: Srgba) {
        self.inner.add_box(min, max, color);
    }

    /// Submits multiple lines that form a rotated box to be rendered by giving a rotation, a min and a max position.
    ///
    /// This box is an axis aligned box.
    pub fn draw_rotated_box(
        &mut self,
        min: Point3<f32>,
        max: Point3<f32>,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        self.inner.add_rotated_box(min, max, rotation, color);
    }

    /// Submits multiple lines that form a circle to be rendered by giving a center, a radius and an amount of points.
    ///
    /// This circle is aligned to the XY plane.
    pub fn draw_circle(&mut self, center: Point3<f32>, radius: f32, points: u32, color: Srgba) {
        self.inner.add_circle_2d(center, radius, points, color);
    }

    /// Submits multiple lines that form a rotated circle to be rendered by giving a center, a radius, an amount of points and a rotation.
    pub fn draw_rotated_circle(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        points: u32,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        self.inner
            .add_rotated_circle(center, radius, points, rotation, color);
    }

    /// Submits multiple lines that form a sphere to be rendered by giving a center, a radius, an amount of vertical points and an amount of horizontal points.
    pub fn draw_sphere(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        horizontal_points: u32,
        vertical_points: u32,
        color: Srgba,
    ) {
        self.inner
            .add_sphere(center, radius, horizontal_points, vertical_points, color);
    }

    /// Submits multiple lines that form a cylinder to be rendered by giving a center, a radius, a height and an amount of points.
    ///
    /// This cylinder is aligned to the y axis.
    pub fn draw_cylinder(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        height: f32,
        points: u32,
        color: Srgba,
    ) {
        self.inner
            .add_cylinder(center, radius, height, points, color);
    }

    /// Adds multiple lines that form a rotated cylinder to be rendered by giving a center, a radius, a height, an amount of points and a rotation.
    pub fn draw_rotated_cylinder(
        &mut self,
        center: Point3<f32>,
        radius: f32,
        height: f32,
        points: u32,
        rotation: UnitQuaternion<f32>,
        color: Srgba,
    ) {
        self.inner
            .add_rotated_cylinder(center, radius, height, points, rotation, color);
    }

    pub(crate) fn drain<'a>(&'a mut self) -> impl Iterator<Item = DebugLine> + 'a {
        self.inner.lines.drain(..)
    }
}