
# Objective I have an application where I'd like to measure average frame rate over the entire life of the application, and it would be handy if I could just configure this on the existing `FrameTimeDiagnosticsPlugin`. Probably fixes #10948? ## Solution Add `max_history_length` to `FrameTimeDiagnosticsPlugin`, and because `smoothing_factor` seems to be based on history length, add that too. ## Discussion I'm not totally sure that `DEFAULT_MAX_HISTORY_LENGTH` is a great default for `FrameTimeDiagnosticsPlugin` (or any diagnostic?). That's 1/3 of a second at typical game frame rates. Moreover, the default print interval for `LogDiagnosticsPlugin` is 1 second. So when the two are combined, you are printing the average over the last third of the duration between now and the previous print, which seems a bit wonky. (related: #11429) I'm pretty sure this default value discussed and the current value wasn't totally arbitrary though. Maybe it would be nice for `Diagnostic` to have a `with_max_history_length_and_also_calculate_a_good_default_smoothing_factor` method? And then make an explicit smoothing factor in `FrameTimeDiagnosticsPlugin` optional? Or add a `new(max_history_length: usize)` method to `FrameTimeDiagnosticsPlugin` that sets a reasonable default `smoothing_factor`? edit: This one seems like a no-brainer, doing it. ## Alternatives It's really easy to roll your own `FrameTimeDiagnosticsPlugin`, but that might not be super interoperable with, for example, third party FPS overlays. Still, might be the right call. ## Testing `cargo run --example many_sprites` (modified to use a custom `max_history_length`) ## Migration Guide `FrameTimeDiagnosticsPlugin` now contains two fields. Use `FrameTimeDiagnosticsPlugin::default()` to match Bevy's previous behavior or, for example, `FrameTimeDiagnosticsPlugin::new(60)` to configure it.
130 lines
4.1 KiB
Rust
130 lines
4.1 KiB
Rust
//! Renders a lot of sprites to allow performance testing.
|
|
//! See <https://github.com/bevyengine/bevy/pull/1492>
|
|
//!
|
|
//! This example sets up many sprites in different sizes, rotations, and scales in the world.
|
|
//! It also moves the camera over them to see how well frustum culling works.
|
|
//!
|
|
//! Add the `--colored` arg to run with color tinted sprites. This will cause the sprites to be rendered
|
|
//! in multiple batches, reducing performance but useful for testing.
|
|
|
|
use bevy::{
|
|
color::palettes::css::*,
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
prelude::*,
|
|
window::{PresentMode, WindowResolution},
|
|
winit::{UpdateMode, WinitSettings},
|
|
};
|
|
|
|
use rand::Rng;
|
|
|
|
const CAMERA_SPEED: f32 = 1000.0;
|
|
|
|
const COLORS: [Color; 3] = [Color::Srgba(BLUE), Color::Srgba(WHITE), Color::Srgba(RED)];
|
|
|
|
#[derive(Resource)]
|
|
struct ColorTint(bool);
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(ColorTint(
|
|
std::env::args().nth(1).unwrap_or_default() == "--colored",
|
|
))
|
|
// Since this is also used as a benchmark, we want it to display performance data.
|
|
.add_plugins((
|
|
LogDiagnosticsPlugin::default(),
|
|
FrameTimeDiagnosticsPlugin::default(),
|
|
DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
present_mode: PresentMode::AutoNoVsync,
|
|
resolution: WindowResolution::new(1920.0, 1080.0)
|
|
.with_scale_factor_override(1.0),
|
|
..default()
|
|
}),
|
|
..default()
|
|
}),
|
|
))
|
|
.insert_resource(WinitSettings {
|
|
focused_mode: UpdateMode::Continuous,
|
|
unfocused_mode: UpdateMode::Continuous,
|
|
})
|
|
.add_systems(Startup, setup)
|
|
.add_systems(
|
|
Update,
|
|
(print_sprite_count, move_camera.after(print_sprite_count)),
|
|
)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands, assets: Res<AssetServer>, color_tint: Res<ColorTint>) {
|
|
warn!(include_str!("warning_string.txt"));
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let tile_size = Vec2::splat(64.0);
|
|
let map_size = Vec2::splat(320.0);
|
|
|
|
let half_x = (map_size.x / 2.0) as i32;
|
|
let half_y = (map_size.y / 2.0) as i32;
|
|
|
|
let sprite_handle = assets.load("branding/icon.png");
|
|
|
|
// Spawns the camera
|
|
|
|
commands.spawn(Camera2d);
|
|
|
|
// Builds and spawns the sprites
|
|
let mut sprites = vec![];
|
|
for y in -half_y..half_y {
|
|
for x in -half_x..half_x {
|
|
let position = Vec2::new(x as f32, y as f32);
|
|
let translation = (position * tile_size).extend(rng.gen::<f32>());
|
|
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
|
|
let scale = Vec3::splat(rng.gen::<f32>() * 2.0);
|
|
|
|
sprites.push((
|
|
Sprite {
|
|
image: sprite_handle.clone(),
|
|
custom_size: Some(tile_size),
|
|
color: if color_tint.0 {
|
|
COLORS[rng.gen_range(0..3)]
|
|
} else {
|
|
Color::WHITE
|
|
},
|
|
..default()
|
|
},
|
|
Transform {
|
|
translation,
|
|
rotation,
|
|
scale,
|
|
},
|
|
));
|
|
}
|
|
}
|
|
commands.spawn_batch(sprites);
|
|
}
|
|
|
|
// System for rotating and translating the camera
|
|
fn move_camera(time: Res<Time>, mut camera_transform: Single<&mut Transform, With<Camera>>) {
|
|
camera_transform.rotate_z(time.delta_secs() * 0.5);
|
|
**camera_transform = **camera_transform
|
|
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_secs());
|
|
}
|
|
|
|
#[derive(Deref, DerefMut)]
|
|
struct PrintingTimer(Timer);
|
|
|
|
impl Default for PrintingTimer {
|
|
fn default() -> Self {
|
|
Self(Timer::from_seconds(1.0, TimerMode::Repeating))
|
|
}
|
|
}
|
|
|
|
// System for printing the number of sprites on every tick of the timer
|
|
fn print_sprite_count(time: Res<Time>, mut timer: Local<PrintingTimer>, sprites: Query<&Sprite>) {
|
|
timer.tick(time.delta());
|
|
|
|
if timer.just_finished() {
|
|
info!("Sprites: {}", sprites.iter().count());
|
|
}
|
|
}
|