forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_pipeline.rs
68 lines (64 loc) · 2.16 KB
/
text_pipeline.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
//! Text pipeline benchmark.
//!
//! Continuously recomputes a large `Text` component with 100 sections.
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
text::{BreakLineOn, Text2dBounds},
window::{PresentMode, WindowPlugin},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
present_mode: PresentMode::Immediate,
..default()
}),
..default()
}))
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin::default())
.add_systems(Startup, spawn)
.add_systems(Update, update_text_bounds)
.run();
}
fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
let sections = (1..=50)
.flat_map(|i| {
[
TextSection {
value: "text".repeat(i),
style: TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: (4 + i % 10) as f32,
color: Color::BLUE,
},
},
TextSection {
value: "pipeline".repeat(i),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: (4 + i % 11) as f32,
color: Color::YELLOW,
},
},
]
})
.collect::<Vec<_>>();
commands.spawn(Text2dBundle {
text: Text {
sections,
alignment: TextAlignment::Center,
linebreak_behavior: BreakLineOn::AnyCharacter,
},
..Default::default()
});
}
// changing the bounds of the text will cause a recomputation
fn update_text_bounds(time: Res<Time>, mut text_bounds_query: Query<&mut Text2dBounds>) {
let width = (1. + time.elapsed_seconds().sin()) * 600.0;
for mut text_bounds in text_bounds_query.iter_mut() {
text_bounds.size.x = width;
}
}