2022-09-28 04:20:27 +00:00
|
|
|
use crate::{
|
|
|
|
extract_resource::ExtractResource,
|
|
|
|
render_resource::{ShaderType, UniformBuffer},
|
|
|
|
renderer::{RenderDevice, RenderQueue},
|
|
|
|
Extract, RenderApp, RenderStage,
|
|
|
|
};
|
|
|
|
use bevy_app::{App, Plugin};
|
|
|
|
use bevy_core::FrameCount;
|
|
|
|
use bevy_ecs::prelude::*;
|
|
|
|
use bevy_reflect::Reflect;
|
|
|
|
use bevy_time::Time;
|
|
|
|
|
|
|
|
pub struct GlobalsPlugin;
|
|
|
|
|
|
|
|
impl Plugin for GlobalsPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
|
|
|
|
render_app
|
|
|
|
.init_resource::<GlobalsBuffer>()
|
|
|
|
.init_resource::<Time>()
|
2022-11-21 13:19:41 +00:00
|
|
|
.add_system_to_stage(RenderStage::Extract, extract_frame_count)
|
2022-09-28 04:20:27 +00:00
|
|
|
.add_system_to_stage(RenderStage::Extract, extract_time)
|
|
|
|
.add_system_to_stage(RenderStage::Prepare, prepare_globals_buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:19:41 +00:00
|
|
|
fn extract_frame_count(mut commands: Commands, frame_count: Extract<Res<FrameCount>>) {
|
|
|
|
commands.insert_resource(**frame_count);
|
|
|
|
}
|
|
|
|
|
2022-09-28 04:20:27 +00:00
|
|
|
fn extract_time(mut commands: Commands, time: Extract<Res<Time>>) {
|
|
|
|
commands.insert_resource(time.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Contains global values useful when writing shaders.
|
|
|
|
/// Currently only contains values related to time.
|
|
|
|
#[derive(Default, Clone, Resource, ExtractResource, Reflect, ShaderType)]
|
|
|
|
#[reflect(Resource)]
|
|
|
|
pub struct GlobalsUniform {
|
|
|
|
/// The time since startup in seconds.
|
|
|
|
/// Wraps to 0 after 1 hour.
|
|
|
|
time: f32,
|
|
|
|
/// The delta time since the previous frame in seconds
|
|
|
|
delta_time: f32,
|
|
|
|
/// Frame count since the start of the app.
|
|
|
|
/// It wraps to zero when it reaches the maximum value of a u32.
|
|
|
|
frame_count: u32,
|
2022-11-07 19:44:14 +00:00
|
|
|
/// WebGL2 structs must be 16 byte aligned.
|
|
|
|
#[cfg(feature = "webgl")]
|
|
|
|
_wasm_padding: f32,
|
2022-09-28 04:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The buffer containing the [`GlobalsUniform`]
|
|
|
|
#[derive(Resource, Default)]
|
|
|
|
pub struct GlobalsBuffer {
|
|
|
|
pub buffer: UniformBuffer<GlobalsUniform>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_globals_buffer(
|
|
|
|
render_device: Res<RenderDevice>,
|
|
|
|
render_queue: Res<RenderQueue>,
|
|
|
|
mut globals_buffer: ResMut<GlobalsBuffer>,
|
|
|
|
time: Res<Time>,
|
|
|
|
frame_count: Res<FrameCount>,
|
|
|
|
) {
|
|
|
|
let buffer = globals_buffer.buffer.get_mut();
|
2022-10-22 18:52:29 +00:00
|
|
|
buffer.time = time.elapsed_seconds_wrapped();
|
2022-09-28 04:20:27 +00:00
|
|
|
buffer.delta_time = time.delta_seconds();
|
|
|
|
buffer.frame_count = frame_count.0;
|
|
|
|
|
|
|
|
globals_buffer
|
|
|
|
.buffer
|
|
|
|
.write_buffer(&render_device, &render_queue);
|
|
|
|
}
|