From 01675a0475b56a903f8d351c850321eb1653e788 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Wed, 26 Jul 2023 18:22:57 +0200 Subject: [PATCH] many_cubes: Add stats overlay UI and make prepass and shadows toggleable --- examples/stress_tests/many_cubes.rs | 175 ++++++++++++++++++++++------ 1 file changed, 141 insertions(+), 34 deletions(-) diff --git a/examples/stress_tests/many_cubes.rs b/examples/stress_tests/many_cubes.rs index 41507dcd54bfa..b131053f06ee8 100644 --- a/examples/stress_tests/many_cubes.rs +++ b/examples/stress_tests/many_cubes.rs @@ -13,7 +13,8 @@ use std::f64::consts::PI; use bevy::{ - diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + core_pipeline::prepass::DepthPrepass, + diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}, math::{DVec2, DVec3}, pbr::NotShadowCaster, prelude::*, @@ -32,10 +33,9 @@ fn main() { ..default() }), FrameTimeDiagnosticsPlugin, - LogDiagnosticsPlugin::default(), )) .add_systems(Startup, setup) - .add_systems(Update, (toggle_shadows, move_camera, print_mesh_count)) + .add_systems(Update, (move_camera, update_state)) .run(); } @@ -54,6 +54,9 @@ fn setup( ..default() }); + // Initialized below + let camera_entity; + match std::env::args().nth(1).as_deref() { Some("cube") => { // NOTE: This pattern is good for demonstrating that frustum culling is working correctly @@ -96,10 +99,12 @@ fn setup( } } // camera - commands.spawn(Camera3dBundle { - transform: Transform::from_xyz(WIDTH as f32, HEIGHT as f32, WIDTH as f32), - ..default() - }); + camera_entity = commands + .spawn(Camera3dBundle { + transform: Transform::from_xyz(WIDTH as f32, HEIGHT as f32, WIDTH as f32), + ..default() + }) + .id(); } _ => { // NOTE: This pattern is good for testing performance of culling as it provides roughly @@ -121,10 +126,15 @@ fn setup( } // camera - commands.spawn(Camera3dBundle::default()); + camera_entity = commands.spawn(Camera3dBundle::default()).id(); } } + let depth_prepass_enabled = std::env::args().nth(2).as_deref() == Some("prepass-enabled"); + if depth_prepass_enabled { + commands.entity(camera_entity).insert(DepthPrepass); + } + commands.spawn(( PbrBundle { mesh: mesh.clone_weak(), @@ -157,7 +167,73 @@ fn setup( ..default() }); - commands.spawn(DirectionalLightBundle { ..default() }); + let directional_light_shadows_enabled = + std::env::args().nth(3).as_deref() == Some("shadows-enabled"); + commands.spawn(DirectionalLightBundle { + directional_light: DirectionalLight { + shadows_enabled: directional_light_shadows_enabled, + ..default() + }, + ..default() + }); + + let style = TextStyle { + font_size: 32.0, + ..default() + }; + commands.spawn(( + TextBundle::from_sections([ + TextSection::new(depth_prepass_string(depth_prepass_enabled), style.clone()), + TextSection::new( + directional_light_shadows_string(directional_light_shadows_enabled), + style.clone(), + ), + TextSection::new(mesh_stats_string(0, 0), style.clone()), + TextSection::new(frame_stats_string(0.0, 0.0), style), + ]), + UiState, + )); +} + +#[derive(Component)] +struct UiState; + +fn toggle_state_string(v: bool) -> &'static str { + if v { + "enabled" + } else { + "disabled" + } +} + +fn depth_prepass_string(depth_prepass_enabled: bool) -> String { + format!( + "Depth prepass: {} (toggle by pressing 'p')\n", + toggle_state_string(depth_prepass_enabled) + ) +} + +fn directional_light_shadows_string(directional_light_shadows_enabled: bool) -> String { + format!( + "Directional light shadows: {} (toggle by pressing 's')\n", + toggle_state_string(directional_light_shadows_enabled) + ) +} + +fn mesh_stats_string(total_meshes: usize, visible_meshes: usize) -> String { + format!( + "Meshes: {}\nVisible Meshes {} ({:5.1}%)\n", + total_meshes, + visible_meshes, + 100.0 * (visible_meshes as f32 / total_meshes as f32), + ) +} + +fn frame_stats_string(fps: f32, frame_time_ms: f32) -> String { + format!( + "Frame rate: {:>6.1} fps\nFrame time: {:>6.3} ms\n", + fps, frame_time_ms + ) } // NOTE: This epsilon value is apparently optimal for optimizing for the average @@ -187,36 +263,67 @@ fn move_camera(time: Res