Skip to content

Commit

Permalink
Add globals struct to mesh2d (#6222)
Browse files Browse the repository at this point in the history
See commit message.
I noticed I couldn't use `globals.time` when using `Material2d`.

I copied the solution from 8073362 , and now `Material2d` works for me.

Perhaps some of these struct definitions could be shared in the future, but for now I've just copy pasted it (it looked like the `View` struct was done that way).

Ping @IceSentry , I saw a comment on the linked commit that you intended to do this work at some point in the future.
  • Loading branch information
torsteingrindvik committed Oct 10, 2022
1 parent 740ae9a commit 55d126c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
31 changes: 26 additions & 5 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Expand Up @@ -8,6 +8,7 @@ use bevy_math::{Mat4, Vec2};
use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::{
extract_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
globals::{GlobalsBuffer, GlobalsUniform},
mesh::{GpuBufferInfo, Mesh, MeshVertexBufferLayout},
render_asset::RenderAssets,
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
Expand Down Expand Up @@ -176,6 +177,16 @@ impl FromWorld for Mesh2dPipeline {
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(GlobalsUniform::min_size()),
},
count: None,
},
],
label: Some("mesh2d_view_layout"),
});
Expand Down Expand Up @@ -429,14 +440,24 @@ pub fn queue_mesh2d_view_bind_groups(
mesh2d_pipeline: Res<Mesh2dPipeline>,
view_uniforms: Res<ViewUniforms>,
views: Query<Entity, With<ExtractedView>>,
globals_buffer: Res<GlobalsBuffer>,
) {
if let Some(view_binding) = view_uniforms.uniforms.binding() {
if let (Some(view_binding), Some(globals)) = (
view_uniforms.uniforms.binding(),
globals_buffer.buffer.binding(),
) {
for entity in &views {
let view_bind_group = render_device.create_bind_group(&BindGroupDescriptor {
entries: &[BindGroupEntry {
binding: 0,
resource: view_binding.clone(),
}],
entries: &[
BindGroupEntry {
binding: 0,
resource: view_binding.clone(),
},
BindGroupEntry {
binding: 1,
resource: globals.clone(),
},
],
label: Some("mesh2d_view_bind_group"),
layout: &mesh2d_pipeline.view_layout,
});
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl
Expand Up @@ -4,3 +4,6 @@

@group(0) @binding(0)
var<uniform> view: View;

@group(0) @binding(1)
var<uniform> globals: Globals;
11 changes: 11 additions & 0 deletions crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl
Expand Up @@ -11,3 +11,14 @@ struct View {
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
};

struct Globals {
// 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,
}

0 comments on commit 55d126c

Please sign in to comment.