From fc8fbe2e8c64eef240455eed41d9ef0345375f4e Mon Sep 17 00:00:00 2001 From: Cameron <51241057+maniwani@users.noreply.github.com> Date: Sat, 22 Oct 2022 18:52:29 +0000 Subject: [PATCH] Add global time scaling (#5752) # Objective - Make `Time` API more consistent. - Support time accel/decel/pause. ## Solution This is just the `Time` half of #3002. I was told that part isn't controversial. - Give the "delta time" and "total elapsed time" methods `f32`, `f64`, and `Duration` variants with consistent naming. - Implement accelerating / decelerating the passage of time. - Implement stopping time. --- ## Changelog - Changed `time_since_startup` to `elapsed` because `time.time_*` is just silly. - Added `relative_speed` and `set_relative_speed` methods. - Added `is_paused`, `pause`, `unpause` , and methods. (I'd prefer `resume`, but `unpause` matches `Timer` API.) - Added `raw_*` variants of the "delta time" and "total elapsed time" methods. - Added `first_update` method because there's a non-zero duration between startup and the first update. ## Migration Guide - `time.time_since_startup()` -> `time.elapsed()` - `time.seconds_since_startup()` -> `time.elapsed_seconds_f64()` - `time.seconds_since_startup_wrapped_f32()` -> `time.elapsed_seconds_wrapped()` If you aren't sure which to use, most systems should continue to use "scaled" time (e.g. `time.delta_seconds()`). The realtime "unscaled" time measurements (e.g. `time.raw_delta_seconds()`) are mostly for debugging and profiling. --- .../src/frame_time_diagnostics_plugin.rs | 7 +- .../src/log_diagnostics_plugin.rs | 4 +- crates/bevy_render/src/globals.rs | 2 +- crates/bevy_time/src/time.rs | 654 +++++++++++++++--- examples/2d/text2d.rs | 8 +- examples/3d/load_gltf.rs | 6 +- examples/3d/skybox.rs | 6 +- examples/3d/spotlight.rs | 8 +- examples/3d/transparency_3d.rs | 2 +- examples/3d/update_gltf_scene.rs | 4 +- examples/animation/custom_skinned_mesh.rs | 5 +- examples/animation/gltf_skinned_mesh.rs | 4 +- examples/audio/audio_control.rs | 2 +- examples/ecs/component_change_detection.rs | 4 +- examples/ecs/fixed_timestep.rs | 30 +- examples/ecs/hierarchy.rs | 6 +- examples/ecs/removal_detection.rs | 2 +- examples/ecs/state.rs | 2 +- examples/ecs/system_sets.rs | 2 +- examples/games/alien_cake_addict.rs | 5 +- examples/scene/scene.rs | 2 +- examples/tools/scene_viewer.rs | 6 +- examples/ui/text.rs | 2 +- examples/window/window_settings.rs | 2 +- 24 files changed, 609 insertions(+), 166 deletions(-) diff --git a/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs index ec9ea2d9469c1e..c651e53a973bce 100644 --- a/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/frame_time_diagnostics_plugin.rs @@ -35,12 +35,13 @@ impl FrameTimeDiagnosticsPlugin { ) { diagnostics.add_measurement(Self::FRAME_COUNT, || frame_count.0 as f64); - if time.delta_seconds_f64() == 0.0 { + let delta_seconds = time.raw_delta_seconds_f64(); + if delta_seconds == 0.0 { return; } - diagnostics.add_measurement(Self::FRAME_TIME, || time.delta_seconds_f64() * 1000.); + diagnostics.add_measurement(Self::FRAME_TIME, || delta_seconds * 1000.0); - diagnostics.add_measurement(Self::FPS, || 1.0 / time.delta_seconds_f64()); + diagnostics.add_measurement(Self::FPS, || 1.0 / delta_seconds); } } diff --git a/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs index 9966157cb0cf5c..588d05013e0e18 100644 --- a/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs @@ -85,7 +85,7 @@ impl LogDiagnosticsPlugin { time: Res