forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::{ | ||
extract_resource::ExtractResource, | ||
render_resource::{ShaderType, UniformBuffer}, | ||
renderer::{RenderDevice, RenderQueue}, | ||
Extract, RenderApp, RenderStage, | ||
}; | ||
use bevy_app::{App, Plugin}; | ||
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>() | ||
.add_system_to_stage(RenderStage::Extract, extract_time) | ||
.add_system_to_stage(RenderStage::Prepare, prepare_globals_buffer); | ||
} | ||
} | ||
} | ||
|
||
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, ExtractResource, Reflect, ShaderType)] | ||
#[reflect(Resource)] | ||
pub struct GlobalsUniform { | ||
/// The time since startup in seconds | ||
time: f32, | ||
/// The duration of the last frame in seconds | ||
delta_time: f32, | ||
} | ||
|
||
/// The buffer containing the [`GlobalsUniform`] | ||
#[derive(Component, 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>, | ||
) { | ||
let buffer = globals_buffer.buffer.get_mut(); | ||
buffer.time = time.seconds_since_startup() as f32; | ||
buffer.delta_time = time.delta_seconds(); | ||
|
||
globals_buffer | ||
.buffer | ||
.write_buffer(&render_device, &render_queue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters