Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_text2d.rs
6592 views
1
//! Renders a lot of `Text2d`s
2
3
use std::ops::RangeInclusive;
4
5
use bevy::{
6
camera::visibility::NoFrustumCulling,
7
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
8
prelude::*,
9
text::FontAtlasSets,
10
window::{PresentMode, WindowResolution},
11
winit::{UpdateMode, WinitSettings},
12
};
13
14
use argh::FromArgs;
15
use rand::{
16
seq::{IndexedRandom, IteratorRandom},
17
Rng, SeedableRng,
18
};
19
use rand_chacha::ChaCha8Rng;
20
21
const CAMERA_SPEED: f32 = 1000.0;
22
23
// Some code points for valid glyphs in `FiraSans-Bold.ttf`
24
const CODE_POINT_RANGES: [RangeInclusive<u32>; 5] = [
25
0x20..=0x7e,
26
0xa0..=0x17e,
27
0x180..=0x2b2,
28
0x3f0..=0x479,
29
0x48a..=0x52f,
30
];
31
32
#[derive(FromArgs, Resource)]
33
/// `many_text2d` stress test
34
struct Args {
35
/// whether to use many different glyphs to increase the amount of font atlas textures used.
36
#[argh(switch)]
37
many_glyphs: bool,
38
39
/// whether to use many different font sizes to increase the amount of font atlas textures used.
40
#[argh(switch)]
41
many_font_sizes: bool,
42
43
/// whether to force the text to recompute every frame by triggering change detection.
44
#[argh(switch)]
45
recompute: bool,
46
47
/// whether to disable all frustum culling.
48
#[argh(switch)]
49
no_frustum_culling: bool,
50
51
/// whether the text should use `Justify::Center`.
52
#[argh(switch)]
53
center: bool,
54
}
55
56
#[derive(Resource)]
57
struct FontHandle(Handle<Font>);
58
impl FromWorld for FontHandle {
59
fn from_world(world: &mut World) -> Self {
60
Self(world.load_asset("fonts/FiraSans-Bold.ttf"))
61
}
62
}
63
64
fn main() {
65
// `from_env` panics on the web
66
#[cfg(not(target_arch = "wasm32"))]
67
let args: Args = argh::from_env();
68
#[cfg(target_arch = "wasm32")]
69
let args = Args::from_args(&[], &[]).unwrap();
70
71
let mut app = App::new();
72
73
app.add_plugins((
74
FrameTimeDiagnosticsPlugin::default(),
75
LogDiagnosticsPlugin::default(),
76
DefaultPlugins.set(WindowPlugin {
77
primary_window: Some(Window {
78
present_mode: PresentMode::AutoNoVsync,
79
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
80
..default()
81
}),
82
..default()
83
}),
84
))
85
.insert_resource(WinitSettings {
86
focused_mode: UpdateMode::Continuous,
87
unfocused_mode: UpdateMode::Continuous,
88
})
89
.init_resource::<FontHandle>()
90
.add_systems(Startup, setup)
91
.add_systems(Update, (move_camera, print_counts));
92
93
if args.recompute {
94
app.add_systems(Update, recompute);
95
}
96
97
app.insert_resource(args).run();
98
}
99
100
#[derive(Deref, DerefMut)]
101
struct PrintingTimer(Timer);
102
103
impl Default for PrintingTimer {
104
fn default() -> Self {
105
Self(Timer::from_seconds(1.0, TimerMode::Repeating))
106
}
107
}
108
109
fn setup(mut commands: Commands, font: Res<FontHandle>, args: Res<Args>) {
110
warn!(include_str!("warning_string.txt"));
111
112
let mut rng = ChaCha8Rng::seed_from_u64(42);
113
114
let tile_size = Vec2::splat(64.0);
115
let map_size = Vec2::splat(640.0);
116
117
let half_x = (map_size.x / 4.0) as i32;
118
let half_y = (map_size.y / 4.0) as i32;
119
120
// Spawns the camera
121
122
commands.spawn(Camera2d);
123
124
// Builds and spawns the `Text2d`s, distributing them in a way that ensures a
125
// good distribution of on-screen and off-screen entities.
126
let mut text2ds = vec![];
127
for y in -half_y..half_y {
128
for x in -half_x..half_x {
129
let position = Vec2::new(x as f32, y as f32);
130
let translation = (position * tile_size).extend(rng.random::<f32>());
131
let rotation = Quat::from_rotation_z(rng.random::<f32>());
132
let scale = Vec3::splat(rng.random::<f32>() * 2.0);
133
let color = Hsla::hsl(rng.random_range(0.0..360.0), 0.8, 0.8);
134
135
text2ds.push((
136
Text2d(random_text(&mut rng, &args)),
137
random_text_font(&mut rng, &args, font.0.clone()),
138
TextColor(color.into()),
139
TextLayout::new_with_justify(if args.center {
140
Justify::Center
141
} else {
142
Justify::Left
143
}),
144
Transform {
145
translation,
146
rotation,
147
scale,
148
},
149
));
150
}
151
}
152
153
if args.no_frustum_culling {
154
let bundles = text2ds.into_iter().map(|bundle| (bundle, NoFrustumCulling));
155
commands.spawn_batch(bundles);
156
} else {
157
commands.spawn_batch(text2ds);
158
}
159
}
160
161
// System for rotating and translating the camera
162
fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
163
let Ok(mut camera_transform) = camera_query.single_mut() else {
164
return;
165
};
166
camera_transform.rotate_z(time.delta_secs() * 0.5);
167
*camera_transform =
168
*camera_transform * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());
169
}
170
171
// System for printing the number of texts on every tick of the timer
172
fn print_counts(
173
time: Res<Time>,
174
mut timer: Local<PrintingTimer>,
175
texts: Query<&ViewVisibility, With<Text2d>>,
176
atlases: Res<FontAtlasSets>,
177
font: Res<FontHandle>,
178
) {
179
timer.tick(time.delta());
180
if !timer.just_finished() {
181
return;
182
}
183
184
let num_atlases = atlases
185
.get(font.0.id())
186
.map(|set| set.iter().map(|atlas| atlas.1.len()).sum())
187
.unwrap_or(0);
188
189
let visible_texts = texts.iter().filter(|visibility| visibility.get()).count();
190
191
info!(
192
"Texts: {} Visible: {} Atlases: {}",
193
texts.iter().count(),
194
visible_texts,
195
num_atlases
196
);
197
}
198
199
fn random_text_font(rng: &mut ChaCha8Rng, args: &Args, font: Handle<Font>) -> TextFont {
200
let font_size = if args.many_font_sizes {
201
*[10.0, 20.0, 30.0, 40.0, 50.0, 60.0].choose(rng).unwrap()
202
} else {
203
60.0
204
};
205
206
TextFont {
207
font_size,
208
font,
209
..default()
210
}
211
}
212
213
fn random_text(rng: &mut ChaCha8Rng, args: &Args) -> String {
214
if !args.many_glyphs {
215
return "Bevy".to_string();
216
}
217
218
CODE_POINT_RANGES
219
.choose(rng)
220
.unwrap()
221
.clone()
222
.choose_multiple(rng, 4)
223
.into_iter()
224
.map(|cp| char::from_u32(cp).unwrap())
225
.collect::<String>()
226
}
227
228
fn recompute(mut query: Query<&mut Text2d>) {
229
for mut text2d in &mut query {
230
text2d.set_changed();
231
}
232
}
233
234