# 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.
147 lines
4.6 KiB
Rust
147 lines
4.6 KiB
Rust
//! Module containing logic for FPS overlay.
|
|
|
|
use bevy_app::{Plugin, Startup, Update};
|
|
use bevy_asset::Handle;
|
|
use bevy_color::Color;
|
|
use bevy_diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
|
|
use bevy_ecs::{
|
|
change_detection::DetectChangesMut,
|
|
component::Component,
|
|
entity::Entity,
|
|
query::With,
|
|
schedule::{common_conditions::resource_changed, IntoSystemConfigs},
|
|
system::{Commands, Query, Res, Resource},
|
|
};
|
|
use bevy_hierarchy::{BuildChildren, ChildBuild};
|
|
use bevy_render::view::Visibility;
|
|
use bevy_text::{Font, TextColor, TextFont, TextSpan};
|
|
use bevy_ui::{
|
|
widget::{Text, TextUiWriter},
|
|
GlobalZIndex, Node, PositionType,
|
|
};
|
|
|
|
/// [`GlobalZIndex`] used to render the fps overlay.
|
|
///
|
|
/// We use a number slightly under `i32::MAX` so you can render on top of it if you really need to.
|
|
pub const FPS_OVERLAY_ZINDEX: i32 = i32::MAX - 32;
|
|
|
|
/// A plugin that adds an FPS overlay to the Bevy application.
|
|
///
|
|
/// This plugin will add the [`FrameTimeDiagnosticsPlugin`] if it wasn't added before.
|
|
///
|
|
/// Note: It is recommended to use native overlay of rendering statistics when possible for lower overhead and more accurate results.
|
|
/// The correct way to do this will vary by platform:
|
|
/// - **Metal**: setting env variable `MTL_HUD_ENABLED=1`
|
|
#[derive(Default)]
|
|
pub struct FpsOverlayPlugin {
|
|
/// Starting configuration of overlay, this can be later be changed through [`FpsOverlayConfig`] resource.
|
|
pub config: FpsOverlayConfig,
|
|
}
|
|
|
|
impl Plugin for FpsOverlayPlugin {
|
|
fn build(&self, app: &mut bevy_app::App) {
|
|
// TODO: Use plugin dependencies, see https://github.com/bevyengine/bevy/issues/69
|
|
if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() {
|
|
app.add_plugins(FrameTimeDiagnosticsPlugin::default());
|
|
}
|
|
app.insert_resource(self.config.clone())
|
|
.add_systems(Startup, setup)
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
(customize_text, toggle_display).run_if(resource_changed::<FpsOverlayConfig>),
|
|
update_text,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Configuration options for the FPS overlay.
|
|
#[derive(Resource, Clone)]
|
|
pub struct FpsOverlayConfig {
|
|
/// Configuration of text in the overlay.
|
|
pub text_config: TextFont,
|
|
/// Color of text in the overlay.
|
|
pub text_color: Color,
|
|
/// Displays the FPS overlay if true.
|
|
pub enabled: bool,
|
|
}
|
|
|
|
impl Default for FpsOverlayConfig {
|
|
fn default() -> Self {
|
|
FpsOverlayConfig {
|
|
text_config: TextFont {
|
|
font: Handle::<Font>::default(),
|
|
font_size: 32.0,
|
|
..Default::default()
|
|
},
|
|
text_color: Color::WHITE,
|
|
enabled: true,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct FpsText;
|
|
|
|
fn setup(mut commands: Commands, overlay_config: Res<FpsOverlayConfig>) {
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
// We need to make sure the overlay doesn't affect the position of other UI nodes
|
|
position_type: PositionType::Absolute,
|
|
..Default::default()
|
|
},
|
|
// Render overlay on top of everything
|
|
GlobalZIndex(FPS_OVERLAY_ZINDEX),
|
|
))
|
|
.with_children(|p| {
|
|
p.spawn((
|
|
Text::new("FPS: "),
|
|
overlay_config.text_config.clone(),
|
|
TextColor(overlay_config.text_color),
|
|
FpsText,
|
|
))
|
|
.with_child((TextSpan::default(), overlay_config.text_config.clone()));
|
|
});
|
|
}
|
|
|
|
fn update_text(
|
|
diagnostic: Res<DiagnosticsStore>,
|
|
query: Query<Entity, With<FpsText>>,
|
|
mut writer: TextUiWriter,
|
|
) {
|
|
for entity in &query {
|
|
if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
|
if let Some(value) = fps.smoothed() {
|
|
*writer.text(entity, 1) = format!("{value:.2}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn customize_text(
|
|
overlay_config: Res<FpsOverlayConfig>,
|
|
query: Query<Entity, With<FpsText>>,
|
|
mut writer: TextUiWriter,
|
|
) {
|
|
for entity in &query {
|
|
writer.for_each_font(entity, |mut font| {
|
|
*font = overlay_config.text_config.clone();
|
|
});
|
|
writer.for_each_color(entity, |mut color| color.0 = overlay_config.text_color);
|
|
}
|
|
}
|
|
|
|
fn toggle_display(
|
|
overlay_config: Res<FpsOverlayConfig>,
|
|
mut query: Query<&mut Visibility, With<FpsText>>,
|
|
) {
|
|
for mut visibility in &mut query {
|
|
visibility.set_if_neq(match overlay_config.enabled {
|
|
true => Visibility::Visible,
|
|
false => Visibility::Hidden,
|
|
});
|
|
}
|
|
}
|