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
use amethyst_core::{
nalgebra::{Point3, Vector3},
specs::{Component, DenseVecStorage},
};
use crate::{color::Rgba, vertex::PosColorNorm};
/// Debug lines are stored as a position, a direction and a color.
///
/// Storing a direction instead of an end position may not be intuitive,
/// but is similar to other 'VertexFormat's.
pub type DebugLine = PosColorNorm;
/// 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
pub 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 {
DebugLinesComponent {
lines: Vec::<DebugLine>::new(),
}
}
/// Builder method to pre-allocate a number of lines.
pub fn with_capacity(mut self, capacity: usize) -> Self {
self.lines = Vec::<DebugLine>::with_capacity(capacity);
self
}
/// 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: Rgba) {
let vertex = DebugLine {
position: position.to_homogeneous().xyz().into(),
color: color.into(),
normal: direction.into(),
};
self.lines.push(vertex);
}
/// 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: Rgba) {
let vertex = DebugLine {
position: start.to_homogeneous().xyz().into(),
color: color.into(),
normal: (end - start).into(),
};
self.lines.push(vertex);
}
/// 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();
}
}
/// 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
pub lines: Vec<DebugLine>,
}
impl DebugLines {
/// Creates a new debug lines component with an empty DebugLine vector.
pub fn new() -> DebugLines {
DebugLines {
lines: Vec::<DebugLine>::new(),
}
}
/// Builder method to pre-allocate a number of lines.
pub fn with_capacity(mut self, capacity: usize) -> Self {
self.lines = Vec::<DebugLine>::with_capacity(capacity);
self
}
/// 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: Rgba) {
let vertex = DebugLine {
position: position.to_homogeneous().xyz().into(),
color: color.into(),
normal: direction.into(),
};
self.lines.push(vertex);
}
/// 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: Rgba) {
let vertex = DebugLine {
position: start.to_homogeneous().xyz().into(),
color: color.into(),
normal: (end - start).into(),
};
self.lines.push(vertex);
}
}