mirror of
https://github.com/bevyengine/bevy
synced 2024-12-25 12:33:07 +00:00
c19aa5939d
# Objective - Add Time-Adjusted Rolling EMA-based smoothing to diagnostics. - Closes #4983; see that issue for more more information. ## Terms - EMA - [Exponential Moving Average](https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average) - SMA - [Simple Moving Average](https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average) ## Solution - We use a fairly standard approximation of a true EMA where $EMA_{\text{frame}} = EMA_{\text{previous}} + \alpha \left( x_{\text{frame}} - EMA_{\text{previous}} \right)$ where $\alpha = \Delta t / \tau$ and $\tau$ is an arbitrary smoothness factor. (See #4983 for more discussion of the math.) - The smoothness factor is here defaulted to $2 / 21$; this was chosen fairly arbitrarily as supposedly related to the existing 20-bucket SMA. - The smoothness factor can be set on a per-diagnostic basis via `Diagnostic::with_smoothing_factor`. --- ## Changelog ### Added - `Diagnostic::smoothed` - provides an exponentially smoothed view of a recorded diagnostic, to e.g. reduce jitter in frametime readings. ### Changed - `LogDiagnosticsPlugin` now records the smoothed value rather than the raw value. - For diagnostics recorded less often than every 0.1 seconds, this change to defaults will have no visible effect. - For discrete diagnostics where this smoothing is not desirable, set a smoothing factor of 0 to disable smoothing. - The average of the recent history is still shown when available.
48 lines
1.7 KiB
Rust
48 lines
1.7 KiB
Rust
use crate::{Diagnostic, DiagnosticId, Diagnostics};
|
|
use bevy_app::prelude::*;
|
|
use bevy_core::FrameCount;
|
|
use bevy_ecs::system::{Res, ResMut};
|
|
use bevy_time::Time;
|
|
|
|
/// Adds "frame time" diagnostic to an App, specifically "frame time", "fps" and "frame count"
|
|
#[derive(Default)]
|
|
pub struct FrameTimeDiagnosticsPlugin;
|
|
|
|
impl Plugin for FrameTimeDiagnosticsPlugin {
|
|
fn build(&self, app: &mut bevy_app::App) {
|
|
app.add_startup_system(Self::setup_system)
|
|
.add_system(Self::diagnostic_system);
|
|
}
|
|
}
|
|
|
|
impl FrameTimeDiagnosticsPlugin {
|
|
pub const FPS: DiagnosticId = DiagnosticId::from_u128(288146834822086093791974408528866909483);
|
|
pub const FRAME_COUNT: DiagnosticId =
|
|
DiagnosticId::from_u128(54021991829115352065418785002088010277);
|
|
pub const FRAME_TIME: DiagnosticId =
|
|
DiagnosticId::from_u128(73441630925388532774622109383099159699);
|
|
|
|
pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
|
|
diagnostics.add(Diagnostic::new(Self::FRAME_TIME, "frame_time", 20).with_suffix("ms"));
|
|
diagnostics.add(Diagnostic::new(Self::FPS, "fps", 20));
|
|
diagnostics
|
|
.add(Diagnostic::new(Self::FRAME_COUNT, "frame_count", 1).with_smoothing_factor(0.0));
|
|
}
|
|
|
|
pub fn diagnostic_system(
|
|
mut diagnostics: ResMut<Diagnostics>,
|
|
time: Res<Time>,
|
|
frame_count: Res<FrameCount>,
|
|
) {
|
|
diagnostics.add_measurement(Self::FRAME_COUNT, || frame_count.0 as f64);
|
|
|
|
let delta_seconds = time.raw_delta_seconds_f64();
|
|
if delta_seconds == 0.0 {
|
|
return;
|
|
}
|
|
|
|
diagnostics.add_measurement(Self::FRAME_TIME, || delta_seconds * 1000.0);
|
|
|
|
diagnostics.add_measurement(Self::FPS, || 1.0 / delta_seconds);
|
|
}
|
|
}
|