log_diagnostics/
log_diagnostics.rs

1//! Shows different built-in plugins that logs diagnostics, like frames per second (FPS), to the console.
2
3use bevy::{
4    diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
5    prelude::*,
6};
7
8fn main() {
9    App::new()
10        .add_plugins((
11            // The diagnostics plugins need to be added after DefaultPlugins as they use e.g. the time plugin for timestamps.
12            DefaultPlugins,
13            // Adds a system that prints diagnostics to the console.
14            // The other diagnostics plugins can still be used without this if you want to use them in an ingame overlay for example.
15            LogDiagnosticsPlugin::default(),
16            // Adds frame time, FPS and frame count diagnostics.
17            FrameTimeDiagnosticsPlugin::default(),
18            // Adds an entity count diagnostic.
19            bevy::diagnostic::EntityCountDiagnosticsPlugin,
20            // Adds cpu and memory usage diagnostics for systems and the entire game process.
21            bevy::diagnostic::SystemInformationDiagnosticsPlugin,
22            // Forwards various diagnostics from the render app to the main app.
23            // These are pretty verbose but can be useful to pinpoint performance issues.
24            bevy_render::diagnostic::RenderDiagnosticsPlugin,
25        ))
26        // No rendering diagnostics are emitted unless something is drawn to the screen,
27        // so we spawn a small scene.
28        .add_systems(Startup, setup)
29        .run();
30}
31
32/// set up a simple 3D scene
33fn setup(
34    mut commands: Commands,
35    mut meshes: ResMut<Assets<Mesh>>,
36    mut materials: ResMut<Assets<StandardMaterial>>,
37) {
38    // circular base
39    commands.spawn((
40        Mesh3d(meshes.add(Circle::new(4.0))),
41        MeshMaterial3d(materials.add(Color::WHITE)),
42        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
43    ));
44    // cube
45    commands.spawn((
46        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
47        MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
48        Transform::from_xyz(0.0, 0.5, 0.0),
49    ));
50    // light
51    commands.spawn((
52        PointLight {
53            shadows_enabled: true,
54            ..default()
55        },
56        Transform::from_xyz(4.0, 8.0, 4.0),
57    ));
58    // camera
59    commands.spawn((
60        Camera3d::default(),
61        Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
62    ));
63}