Skip to main content

many_gizmos/
many_gizmos.rs

1//! Test rendering of many gizmos.
2
3use std::f32::consts::TAU;
4
5use bevy::{
6    diagnostic::{Diagnostic, DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
7    prelude::*,
8    window::{PresentMode, WindowResolution},
9    winit::WinitSettings,
10};
11
12const SYSTEM_COUNT: u32 = 10;
13
14fn 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::continuous())
30    .insert_resource(Config {
31        line_count: 50_000,
32        fancy: false,
33    })
34    .add_systems(Startup, setup)
35    .add_systems(Update, (input, ui_system));
36
37    for _ in 0..SYSTEM_COUNT {
38        app.add_systems(Update, system);
39    }
40
41    app.run();
42}
43
44#[derive(Resource, Debug)]
45struct Config {
46    line_count: u32,
47    fancy: bool,
48}
49
50fn input(mut config: ResMut<Config>, input: Res<ButtonInput<KeyCode>>) {
51    if input.just_pressed(KeyCode::ArrowUp) {
52        config.line_count += 10_000;
53    }
54    if input.just_pressed(KeyCode::ArrowDown) {
55        config.line_count = config.line_count.saturating_sub(10_000);
56    }
57    if input.just_pressed(KeyCode::Space) {
58        config.fancy = !config.fancy;
59    }
60}
61
62fn system(config: Res<Config>, time: Res<Time>, mut draw: Gizmos) {
63    if !config.fancy {
64        for _ in 0..(config.line_count / SYSTEM_COUNT) {
65            draw.line(Vec3::NEG_Y, Vec3::Y, Color::BLACK);
66        }
67    } else {
68        for i in 0..(config.line_count / SYSTEM_COUNT) {
69            let angle = i as f32 / (config.line_count / SYSTEM_COUNT) as f32 * TAU;
70
71            let vector = Vec2::from(ops::sin_cos(angle)).extend(ops::sin(time.elapsed_secs()));
72            let start_color = LinearRgba::rgb(vector.x, vector.z, 0.5);
73            let end_color = LinearRgba::rgb(-vector.z, -vector.y, 0.5);
74
75            draw.line_gradient(vector, -vector, start_color, end_color);
76        }
77    }
78}
79
80fn setup(mut commands: Commands) {
81    warn!(include_str!("warning_string.txt"));
82
83    commands.spawn((
84        Camera3d::default(),
85        Transform::from_xyz(3., 1., 5.).looking_at(Vec3::ZERO, Vec3::Y),
86    ));
87
88    commands.spawn((
89        Text::default(),
90        Node {
91            position_type: PositionType::Absolute,
92            top: px(12),
93            left: px(12),
94            ..default()
95        },
96    ));
97}
98
99fn ui_system(mut text: Single<&mut Text>, config: Res<Config>, diag: Res<DiagnosticsStore>) {
100    let Some(fps) = diag
101        .get(&FrameTimeDiagnosticsPlugin::FPS)
102        .and_then(Diagnostic::smoothed)
103    else {
104        return;
105    };
106
107    text.0 = format!(
108        "Line count: {}\n\
109        FPS: {:.0}\n\n\
110        Controls:\n\
111        Up/Down: Raise or lower the line count.\n\
112        Spacebar: Toggle fancy mode.",
113        config.line_count, fps,
114    );
115}