many_text2d/
many_text2d.rs

1//! Renders a lot of `Text2d`s
2
3use std::ops::RangeInclusive;
4
5use bevy::{
6    camera::visibility::NoFrustumCulling,
7    diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
8    prelude::*,
9    text::FontAtlasSets,
10    window::{PresentMode, WindowResolution},
11    winit::WinitSettings,
12};
13
14use argh::FromArgs;
15use rand::{
16    seq::{IndexedRandom, IteratorRandom},
17    Rng, SeedableRng,
18};
19use rand_chacha::ChaCha8Rng;
20
21const CAMERA_SPEED: f32 = 1000.0;
22
23// Some code points for valid glyphs in `FiraSans-Bold.ttf`
24const 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
34struct 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)]
57struct FontHandle(Handle<Font>);
58impl FromWorld for FontHandle {
59    fn from_world(world: &mut World) -> Self {
60        Self(world.load_asset("fonts/FiraSans-Bold.ttf"))
61    }
62}
63
64fn 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::continuous())
86    .init_resource::<FontHandle>()
87    .add_systems(Startup, setup)
88    .add_systems(Update, (move_camera, print_counts));
89
90    if args.recompute {
91        app.add_systems(Update, recompute);
92    }
93
94    app.insert_resource(args).run();
95}
96
97#[derive(Deref, DerefMut)]
98struct PrintingTimer(Timer);
99
100impl Default for PrintingTimer {
101    fn default() -> Self {
102        Self(Timer::from_seconds(1.0, TimerMode::Repeating))
103    }
104}
105
106fn setup(mut commands: Commands, font: Res<FontHandle>, args: Res<Args>) {
107    warn!(include_str!("warning_string.txt"));
108
109    let mut rng = ChaCha8Rng::seed_from_u64(42);
110
111    let tile_size = Vec2::splat(64.0);
112    let map_size = Vec2::splat(640.0);
113
114    let half_x = (map_size.x / 4.0) as i32;
115    let half_y = (map_size.y / 4.0) as i32;
116
117    // Spawns the camera
118
119    commands.spawn(Camera2d);
120
121    // Builds and spawns the `Text2d`s, distributing them in a way that ensures a
122    // good distribution of on-screen and off-screen entities.
123    let mut text2ds = vec![];
124    for y in -half_y..half_y {
125        for x in -half_x..half_x {
126            let position = Vec2::new(x as f32, y as f32);
127            let translation = (position * tile_size).extend(rng.random::<f32>());
128            let rotation = Quat::from_rotation_z(rng.random::<f32>());
129            let scale = Vec3::splat(rng.random::<f32>() * 2.0);
130            let color = Hsla::hsl(rng.random_range(0.0..360.0), 0.8, 0.8);
131
132            text2ds.push((
133                Text2d(random_text(&mut rng, &args)),
134                random_text_font(&mut rng, &args, font.0.clone()),
135                TextColor(color.into()),
136                TextLayout::new_with_justify(if args.center {
137                    Justify::Center
138                } else {
139                    Justify::Left
140                }),
141                Transform {
142                    translation,
143                    rotation,
144                    scale,
145                },
146            ));
147        }
148    }
149
150    if args.no_frustum_culling {
151        let bundles = text2ds.into_iter().map(|bundle| (bundle, NoFrustumCulling));
152        commands.spawn_batch(bundles);
153    } else {
154        commands.spawn_batch(text2ds);
155    }
156}
157
158// System for rotating and translating the camera
159fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
160    let Ok(mut camera_transform) = camera_query.single_mut() else {
161        return;
162    };
163    camera_transform.rotate_z(time.delta_secs() * 0.5);
164    *camera_transform =
165        *camera_transform * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());
166}
167
168// System for printing the number of texts on every tick of the timer
169fn print_counts(
170    time: Res<Time>,
171    mut timer: Local<PrintingTimer>,
172    texts: Query<&ViewVisibility, With<Text2d>>,
173    atlases: Res<FontAtlasSets>,
174    font: Res<FontHandle>,
175) {
176    timer.tick(time.delta());
177    if !timer.just_finished() {
178        return;
179    }
180
181    let num_atlases = atlases
182        .get(font.0.id())
183        .map(|set| set.iter().map(|atlas| atlas.1.len()).sum())
184        .unwrap_or(0);
185
186    let visible_texts = texts.iter().filter(|visibility| visibility.get()).count();
187
188    info!(
189        "Texts: {} Visible: {} Atlases: {}",
190        texts.iter().count(),
191        visible_texts,
192        num_atlases
193    );
194}
195
196fn random_text_font(rng: &mut ChaCha8Rng, args: &Args, font: Handle<Font>) -> TextFont {
197    let font_size = if args.many_font_sizes {
198        *[10.0, 20.0, 30.0, 40.0, 50.0, 60.0].choose(rng).unwrap()
199    } else {
200        60.0
201    };
202
203    TextFont {
204        font_size,
205        font,
206        ..default()
207    }
208}
209
210fn random_text(rng: &mut ChaCha8Rng, args: &Args) -> String {
211    if !args.many_glyphs {
212        return "Bevy".to_string();
213    }
214
215    CODE_POINT_RANGES
216        .choose(rng)
217        .unwrap()
218        .clone()
219        .choose_multiple(rng, 4)
220        .into_iter()
221        .map(|cp| char::from_u32(cp).unwrap())
222        .collect::<String>()
223}
224
225fn recompute(mut query: Query<&mut Text2d>) {
226    for mut text2d in &mut query {
227        text2d.set_changed();
228    }
229}