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
use crate::*;
use emath::*;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
pub struct Vertex {
pub pos: Pos2, pub uv: Pos2, pub color: Color32, }
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Mesh {
pub indices: Vec<u32>,
pub vertices: Vec<Vertex>,
pub texture_id: TextureId,
}
impl Mesh {
pub fn with_texture(texture_id: TextureId) -> Self {
Self {
texture_id,
..Default::default()
}
}
pub fn clear(&mut self) {
self.indices.clear();
self.vertices.clear();
self.vertices = Default::default();
}
pub fn bytes_used(&self) -> usize {
std::mem::size_of::<Self>()
+ self.vertices.len() * std::mem::size_of::<Vertex>()
+ self.indices.len() * std::mem::size_of::<u32>()
}
pub fn is_valid(&self) -> bool {
if let Ok(n) = u32::try_from(self.vertices.len()) {
self.indices.iter().all(|&i| i < n)
} else {
false
}
}
pub fn is_empty(&self) -> bool {
self.indices.is_empty() && self.vertices.is_empty()
}
pub fn calc_bounds(&self) -> Rect {
let mut bounds = Rect::NOTHING;
for v in &self.vertices {
bounds.extend_with(v.pos);
}
bounds
}
pub fn append(&mut self, other: Mesh) {
crate::epaint_assert!(other.is_valid());
if self.is_empty() {
*self = other;
} else {
self.append_ref(&other);
}
}
pub fn append_ref(&mut self, other: &Mesh) {
crate::epaint_assert!(other.is_valid());
if !self.is_empty() {
assert_eq!(
self.texture_id, other.texture_id,
"Can't merge Mesh using different textures"
);
} else {
self.texture_id = other.texture_id;
}
let index_offset = self.vertices.len() as u32;
self.indices
.extend(other.indices.iter().map(|index| index + index_offset));
self.vertices.extend(other.vertices.iter());
}
#[inline(always)]
pub fn colored_vertex(&mut self, pos: Pos2, color: Color32) {
crate::epaint_assert!(self.texture_id == TextureId::default());
self.vertices.push(Vertex {
pos,
uv: WHITE_UV,
color,
});
}
#[inline(always)]
pub fn add_triangle(&mut self, a: u32, b: u32, c: u32) {
self.indices.push(a);
self.indices.push(b);
self.indices.push(c);
}
#[inline(always)]
pub fn reserve_triangles(&mut self, additional_triangles: usize) {
self.indices.reserve(3 * additional_triangles);
}
#[inline(always)]
pub fn reserve_vertices(&mut self, additional: usize) {
self.vertices.reserve(additional);
}
pub fn add_rect_with_uv(&mut self, rect: Rect, uv: Rect, color: Color32) {
#![allow(clippy::identity_op)]
let idx = self.vertices.len() as u32;
self.add_triangle(idx + 0, idx + 1, idx + 2);
self.add_triangle(idx + 2, idx + 1, idx + 3);
self.vertices.push(Vertex {
pos: rect.left_top(),
uv: uv.left_top(),
color,
});
self.vertices.push(Vertex {
pos: rect.right_top(),
uv: uv.right_top(),
color,
});
self.vertices.push(Vertex {
pos: rect.left_bottom(),
uv: uv.left_bottom(),
color,
});
self.vertices.push(Vertex {
pos: rect.right_bottom(),
uv: uv.right_bottom(),
color,
});
}
#[inline(always)]
pub fn add_colored_rect(&mut self, rect: Rect, color: Color32) {
crate::epaint_assert!(self.texture_id == TextureId::default());
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color);
}
pub fn split_to_u16(self) -> Vec<Mesh16> {
crate::epaint_assert!(self.is_valid());
const MAX_SIZE: u32 = 1 << 16;
if self.vertices.len() < MAX_SIZE as usize {
return vec![Mesh16 {
indices: self.indices.iter().map(|&i| i as u16).collect(),
vertices: self.vertices,
texture_id: self.texture_id,
}];
}
let mut output = vec![];
let mut index_cursor = 0;
while index_cursor < self.indices.len() {
let span_start = index_cursor;
let mut min_vindex = self.indices[index_cursor];
let mut max_vindex = self.indices[index_cursor];
while index_cursor < self.indices.len() {
let (mut new_min, mut new_max) = (min_vindex, max_vindex);
for i in 0..3 {
let idx = self.indices[index_cursor + i];
new_min = new_min.min(idx);
new_max = new_max.max(idx);
}
if new_max - new_min < MAX_SIZE {
min_vindex = new_min;
max_vindex = new_max;
index_cursor += 3;
} else {
break;
}
}
assert!(
index_cursor > span_start,
"One triangle spanned more than {} vertices",
MAX_SIZE
);
let mesh = Mesh16 {
indices: self.indices[span_start..index_cursor]
.iter()
.map(|vi| u16::try_from(vi - min_vindex).unwrap())
.collect(),
vertices: self.vertices[(min_vindex as usize)..=(max_vindex as usize)].to_vec(),
texture_id: self.texture_id,
};
crate::epaint_assert!(mesh.is_valid());
output.push(mesh);
}
output
}
pub fn translate(&mut self, delta: Vec2) {
for v in &mut self.vertices {
v.pos += delta;
}
}
pub fn rotate(&mut self, rot: Rot2, origin: Pos2) {
for v in &mut self.vertices {
v.pos = origin + rot * (v.pos - origin);
}
}
}
pub struct Mesh16 {
pub indices: Vec<u16>,
pub vertices: Vec<Vertex>,
pub texture_id: TextureId,
}
impl Mesh16 {
pub fn is_valid(&self) -> bool {
if let Ok(n) = u16::try_from(self.vertices.len()) {
self.indices.iter().all(|&i| i < n)
} else {
false
}
}
}