Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_gizmos.rs
6592 views
1
//! Test rendering of many gizmos.
2
3
use std::f32::consts::TAU;
4
5
use bevy::{
6
diagnostic::{Diagnostic, DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
7
prelude::*,
8
window::{PresentMode, WindowResolution},
9
winit::{UpdateMode, WinitSettings},
10
};
11
12
const SYSTEM_COUNT: u32 = 10;
13
14
fn main() {
15
let mut app = App::new();
16
app.add_plugins((
17
DefaultPlugins.set(WindowPlugin {
18
primary_window: Some(Window {
19
title: "Many Debug Lines".to_string(),
20
present_mode: PresentMode::AutoNoVsync,
21
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
22
..default()
23
}),
24
..default()
25
}),
26
FrameTimeDiagnosticsPlugin::default(),
27
LogDiagnosticsPlugin::default(),
28
))
29
.insert_resource(WinitSettings {
30
focused_mode: UpdateMode::Continuous,
31
unfocused_mode: UpdateMode::Continuous,
32
})
33
.insert_resource(Config {
34
line_count: 50_000,
35
fancy: false,
36
})
37
.add_systems(Startup, setup)
38
.add_systems(Update, (input, ui_system));
39
40
for _ in 0..SYSTEM_COUNT {
41
app.add_systems(Update, system);
42
}
43
44
app.run();
45
}
46
47
#[derive(Resource, Debug)]
48
struct Config {
49
line_count: u32,
50
fancy: bool,
51
}
52
53
fn input(mut config: ResMut<Config>, input: Res<ButtonInput<KeyCode>>) {
54
if input.just_pressed(KeyCode::ArrowUp) {
55
config.line_count += 10_000;
56
}
57
if input.just_pressed(KeyCode::ArrowDown) {
58
config.line_count = config.line_count.saturating_sub(10_000);
59
}
60
if input.just_pressed(KeyCode::Space) {
61
config.fancy = !config.fancy;
62
}
63
}
64
65
fn system(config: Res<Config>, time: Res<Time>, mut draw: Gizmos) {
66
if !config.fancy {
67
for _ in 0..(config.line_count / SYSTEM_COUNT) {
68
draw.line(Vec3::NEG_Y, Vec3::Y, Color::BLACK);
69
}
70
} else {
71
for i in 0..(config.line_count / SYSTEM_COUNT) {
72
let angle = i as f32 / (config.line_count / SYSTEM_COUNT) as f32 * TAU;
73
74
let vector = Vec2::from(ops::sin_cos(angle)).extend(ops::sin(time.elapsed_secs()));
75
let start_color = LinearRgba::rgb(vector.x, vector.z, 0.5);
76
let end_color = LinearRgba::rgb(-vector.z, -vector.y, 0.5);
77
78
draw.line_gradient(vector, -vector, start_color, end_color);
79
}
80
}
81
}
82
83
fn setup(mut commands: Commands) {
84
warn!(include_str!("warning_string.txt"));
85
86
commands.spawn((
87
Camera3d::default(),
88
Transform::from_xyz(3., 1., 5.).looking_at(Vec3::ZERO, Vec3::Y),
89
));
90
91
commands.spawn((
92
Text::default(),
93
Node {
94
position_type: PositionType::Absolute,
95
top: px(12),
96
left: px(12),
97
..default()
98
},
99
));
100
}
101
102
fn ui_system(mut text: Single<&mut Text>, config: Res<Config>, diag: Res<DiagnosticsStore>) {
103
let Some(fps) = diag
104
.get(&FrameTimeDiagnosticsPlugin::FPS)
105
.and_then(Diagnostic::smoothed)
106
else {
107
return;
108
};
109
110
text.0 = format!(
111
"Line count: {}\n\
112
FPS: {:.0}\n\n\
113
Controls:\n\
114
Up/Down: Raise or lower the line count.\n\
115
Spacebar: Toggle fancy mode.",
116
config.line_count, fps,
117
);
118
}
119
120