-
Notifications
You must be signed in to change notification settings - Fork 16
/
mesh_data_generator.rs
256 lines (212 loc) · 7.47 KB
/
mesh_data_generator.rs
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
use bevy::prelude::*;
use ttf2mesh::{TTFFile, Value};
use crate::{
mesh_cache::{CacheKey, MeshCache},
text_mesh::{FontStyle, TextMesh},
};
pub(crate) struct MeshData {
pub vertices: Vec<[f32; 3]>,
pub normals: Vec<[f32; 3]>,
pub indices: Vec<u32>,
pub uvs: Vec<[f32; 2]>,
}
// FIXME: add validator, that validates all .unwrap's() at addition time
// now crashes might occur
//
// TODO: optimization possibility - take char mesh up to modified char
// from the existing mesh
pub(crate) fn generate_text_mesh(
text_mesh: &TextMesh,
font: &mut TTFFile,
cache: Option<&mut MeshCache>,
) -> MeshData {
trace!("Generate text mesh: {:?}", text_mesh.text);
let mut internal_cache;
let cache = match cache {
Some(cache) => cache,
None => {
internal_cache = Some(MeshCache::default());
internal_cache.as_mut().unwrap()
}
};
// TODO performance: pre-allocate capacity
let mut vertices = Vec::new(); //with_capacity(4308); // TODO: allocate opportunistically
let mut normals = Vec::new(); //with_capacity(4308); // TODO: allocate opportunistically
let mut indices = Vec::new(); //with_capacity(8520);
let mut vertices_offset: usize = 0;
let depth = 0.08;
let text = if text_mesh.style.font_style.contains(FontStyle::UPPERCASE) {
text_mesh.text.to_uppercase()
} else if text_mesh.style.font_style.contains(FontStyle::LOWERCASE) {
text_mesh.text.to_lowercase()
} else {
text_mesh.text.clone() // TODO performance - extra allocation
};
let scalar = match text_mesh.style.font_size.as_scalar() {
Some(scalar) => scalar,
None => todo!("Font automatic sizing has not been implemented yet"),
};
let spacing = Vec2::new(0.08, 0.1) * scalar;
let mut scaled_offset = Vec2::ZERO;
let mut scaled_row_y_max_height = 0.;
//println!("scalar={}, spacing={}", scalar, spacing);
for char in text.chars() {
//println!("{} offset={}", char, scaled_offset);
if char == ' ' {
scaled_offset.x += 0.2 * scalar + spacing.x;
continue;
} else if char == '\n' {
scaled_offset.x = 0.;
scaled_offset.y -= scaled_row_y_max_height + spacing.y;
continue;
}
let key = CacheKey::new_3d(char, depth);
let mesh = match cache.meshes.get(&key) {
Some(mesh) => mesh,
None => {
let glyph = font.glyph_from_char(char);
let mut glyph = match glyph {
Ok(glyph) => glyph,
Err(_) => {
println!("Glyph {} not found", char);
font.glyph_from_char('?').unwrap()
}
};
let mesh = match &text_mesh.size.depth {
Some(unit) => glyph
.to_3d_mesh(text_mesh.style.mesh_quality, unit.as_scalar().unwrap())
.unwrap(),
None => todo!("2d glyphs are not implemented yet. Define depth"),
};
cache.meshes.insert(key.clone(), mesh);
cache.meshes.get(&key).unwrap()
}
};
let (mut xmin, mut xmax) = (f32::MAX, f32::MIN);
let (mut ymin, mut ymax) = (f32::MAX, f32::MIN);
for vertex in mesh.iter_vertices() {
let (x, y, _z) = vertex.val();
// optimization possibility: calculate per-glyph min/max when caching
if x < xmin {
xmin = x;
}
if x > xmax {
xmax = x;
}
if y < ymin {
ymin = y;
}
if y > ymax {
ymax = y;
}
}
let y_diff = (ymax - ymin) * scalar;
if scaled_row_y_max_height < y_diff {
scaled_row_y_max_height = y_diff;
}
for vertex in mesh.iter_vertices() {
let (x, y, z) = vertex.val();
vertices.push([
x * scalar + scaled_offset.x - xmin * scalar,
y * scalar + scaled_offset.y,
z * scalar,
]);
}
/*
println!(
" - x({:.3} - {:.3})={:.3}, y({:.3} - {:.3})={:.3}",
xmin * scalar,
xmax * scalar,
(xmax - xmin) * scalar,
ymin * scalar,
ymax * scalar,
(ymax - ymin) * scalar
);
*/
// 13 microsecs
for normal in mesh.iter_normals().unwrap() {
let (x, y, z) = normal.val();
normals.push([x, y, z]);
}
// total = 24ms
for face in mesh.iter_faces() {
let val = face.val();
indices.extend_from_slice(&[
(val.0) as u32 + vertices_offset as u32,
(val.1) as u32 + vertices_offset as u32,
(val.2) as u32 + vertices_offset as u32,
]);
}
// 30 microsecs
vertices_offset += mesh.vertices_len();
scaled_offset.x += (xmax - xmin) * scalar + spacing.x;
if text_mesh.size.wrapping
&& scaled_offset.x + scalar + spacing.x > text_mesh.size.width.as_scalar().unwrap()
{
scaled_offset.x = 0.;
scaled_offset.y -= scaled_row_y_max_height + spacing.y;
}
//println!("");
}
let uvs = vertices.iter().map(|_vert| [0., 1.]).collect::<Vec<_>>();
MeshData {
vertices,
normals,
indices,
uvs,
}
}
#[cfg(test)]
mod tests {
use crate::{
mesh_data_generator::generate_text_mesh, text_mesh::TextMesh, SizeUnit, TextMeshSize,
TextMeshStyle,
};
use super::*;
pub(crate) fn get_font_bytes() -> Vec<u8> {
std::fs::read("./assets/fonts/FiraMono-Medium.ttf").unwrap()
}
#[test]
fn test_generate_mesh() {
let mut mesh_cache = MeshCache::default();
let mut font = ttf2mesh::TTFFile::from_buffer_vec(get_font_bytes()).unwrap();
let text_mesh = TextMesh {
text: "hello world!".to_string(),
size: TextMeshSize {
width: SizeUnit::NonStandard(36. * 2.),
height: SizeUnit::NonStandard(36. * 5.),
..Default::default()
},
style: TextMeshStyle {
font_size: SizeUnit::NonStandard(18.),
..Default::default()
},
..Default::default()
};
let _ = generate_text_mesh(&text_mesh, &mut font, Some(&mut mesh_cache));
}
}
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use super::*;
use test::Bencher;
#[bench]
fn bench_get_glyph_cached(b: &mut Bencher) {
let mut mesh_cache = MeshCache::default();
let mut font = ttf2mesh::TTFFile::from_buffer_vec(tests::get_font_bytes()).unwrap();
let text_mesh = TextMesh::new_no_font("hello world!".to_string());
let _ = generate_text_mesh(&text_mesh, &mut font, Some(&mut mesh_cache));
b.iter(|| {
let _ = generate_text_mesh(&text_mesh, &mut font, Some(&mut mesh_cache));
});
}
#[bench]
fn bench_get_glyph_no_cache(b: &mut Bencher) {
let mut font = ttf2mesh::TTFFile::from_buffer_vec(tests::get_font_bytes()).unwrap();
let text_mesh = TextMesh::new_no_font("hello world!".to_string());
b.iter(|| {
let _ = generate_text_mesh(&text_mesh, &mut font, None);
});
}
}