Skip to content

Commit 750e150

Browse files
torsteingrindvikPietrek14
authored andcommitted
Add globals struct to mesh2d (bevyengine#6222)
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.
1 parent a43c020 commit 750e150

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

crates/bevy_sprite/src/mesh2d/mesh.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bevy_math::{Mat4, Vec2};
88
use bevy_reflect::{Reflect, TypeUuid};
99
use bevy_render::{
1010
extract_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
11+
globals::{GlobalsBuffer, GlobalsUniform},
1112
mesh::{GpuBufferInfo, Mesh, MeshVertexBufferLayout},
1213
render_asset::RenderAssets,
1314
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
@@ -176,6 +177,16 @@ impl FromWorld for Mesh2dPipeline {
176177
},
177178
count: None,
178179
},
180+
BindGroupLayoutEntry {
181+
binding: 1,
182+
visibility: ShaderStages::VERTEX_FRAGMENT,
183+
ty: BindingType::Buffer {
184+
ty: BufferBindingType::Uniform,
185+
has_dynamic_offset: false,
186+
min_binding_size: Some(GlobalsUniform::min_size()),
187+
},
188+
count: None,
189+
},
179190
],
180191
label: Some("mesh2d_view_layout"),
181192
});
@@ -429,14 +440,24 @@ pub fn queue_mesh2d_view_bind_groups(
429440
mesh2d_pipeline: Res<Mesh2dPipeline>,
430441
view_uniforms: Res<ViewUniforms>,
431442
views: Query<Entity, With<ExtractedView>>,
443+
globals_buffer: Res<GlobalsBuffer>,
432444
) {
433-
if let Some(view_binding) = view_uniforms.uniforms.binding() {
445+
if let (Some(view_binding), Some(globals)) = (
446+
view_uniforms.uniforms.binding(),
447+
globals_buffer.buffer.binding(),
448+
) {
434449
for entity in &views {
435450
let view_bind_group = render_device.create_bind_group(&BindGroupDescriptor {
436-
entries: &[BindGroupEntry {
437-
binding: 0,
438-
resource: view_binding.clone(),
439-
}],
451+
entries: &[
452+
BindGroupEntry {
453+
binding: 0,
454+
resource: view_binding.clone(),
455+
},
456+
BindGroupEntry {
457+
binding: 1,
458+
resource: globals.clone(),
459+
},
460+
],
440461
label: Some("mesh2d_view_bind_group"),
441462
layout: &mesh2d_pipeline.view_layout,
442463
});

crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44

55
@group(0) @binding(0)
66
var<uniform> view: View;
7+
8+
@group(0) @binding(1)
9+
var<uniform> globals: Globals;

crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl

+11
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ struct View {
1111
// viewport(x_origin, y_origin, width, height)
1212
viewport: vec4<f32>,
1313
};
14+
15+
struct Globals {
16+
// The time since startup in seconds
17+
// Wraps to 0 after 1 hour.
18+
time: f32,
19+
// The delta time since the previous frame in seconds
20+
delta_time: f32,
21+
// Frame count since the start of the app.
22+
// It wraps to zero when it reaches the maximum value of a u32.
23+
frame_count: u32,
24+
}

0 commit comments

Comments
 (0)