-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - add globals to mesh view bind group #5409
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,67 @@ | ||
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>() | ||
.add_system_to_stage(RenderStage::Extract, extract_time) | ||
IceSentry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.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, 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. | ||
IceSentry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// It wraps to zero when it reaches the maximum value of a u32. | ||
frame_count: u32, | ||
} | ||
|
||
/// 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(); | ||
buffer.time = time.seconds_since_startup_wrapped_f32(); | ||
buffer.delta_time = time.delta_seconds(); | ||
buffer.frame_count = frame_count.0; | ||
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about 2d users? They may not depend on
bevy_pbr
, but it's possible they still want to access the globals binding.Possible solutions (imo):
bevy_sprite::mesh2d_view_bindings
mesh_view_bindings
frombevy_pbr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I'll address this in a follow up PR. I honestly didn't think of that. I'll probably add the bindings to
mesh2d_view_bindings
. Although, I wonder if we could have some sort of "core" bindings that are shared between both.