Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HackerFoo committed Aug 14, 2022
1 parent 0b3230a commit c6d6874
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
7 changes: 6 additions & 1 deletion assets/shaders/custom_material.wgsl
@@ -1,3 +1,5 @@
#import bevy_pbr::mesh_view_bindings

struct CustomMaterial {
color: vec4<f32>,
};
Expand All @@ -13,5 +15,8 @@ var base_color_sampler: sampler;
fn fragment(
#import bevy_pbr::mesh_vertex_output
) -> @location(0) vec4<f32> {
return material.color * textureSample(base_color_texture, base_color_sampler, uv);
let time = sin(globals.time) * 0.5 + 0.5;
return material.color
* textureSample(base_color_texture, base_color_sampler, uv)
* vec4<f32>(time, time, time, 1.0);
}
20 changes: 19 additions & 1 deletion crates/bevy_pbr/src/render/mesh.rs
Expand Up @@ -13,6 +13,7 @@ use bevy_math::{Mat4, Vec2};
use bevy_reflect::TypeUuid;
use bevy_render::{
extract_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
globals::{GlobalsBuffer, GlobalsUniform},
mesh::{
skinning::{SkinnedMesh, SkinnedMeshInverseBindposes},
GpuBufferInfo, Mesh, MeshVertexBufferLayout,
Expand Down Expand Up @@ -377,6 +378,16 @@ impl FromWorld for MeshPipeline {
},
count: None,
},
BindGroupLayoutEntry {
binding: 9,
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("mesh_view_layout"),
});
Expand Down Expand Up @@ -463,6 +474,7 @@ impl FromWorld for MeshPipeline {
),
}
};

MeshPipeline {
view_layout,
mesh_layout,
Expand Down Expand Up @@ -757,11 +769,13 @@ pub fn queue_mesh_view_bind_groups(
global_light_meta: Res<GlobalLightMeta>,
view_uniforms: Res<ViewUniforms>,
views: Query<(Entity, &ViewShadowBindings, &ViewClusterBindings)>,
globals_buffer: Res<GlobalsBuffer>,
) {
if let (Some(view_binding), Some(light_binding), Some(point_light_binding)) = (
if let (Some(view_binding), Some(light_binding), Some(point_light_binding), Some(globals)) = (
view_uniforms.uniforms.binding(),
light_meta.view_gpu_lights.binding(),
global_light_meta.gpu_point_lights.binding(),
globals_buffer.buffer.binding(),
) {
for (entity, view_shadow_bindings, view_cluster_bindings) in &views {
let view_bind_group = render_device.create_bind_group(&BindGroupDescriptor {
Expand Down Expand Up @@ -808,6 +822,10 @@ pub fn queue_mesh_view_bind_groups(
binding: 8,
resource: view_cluster_bindings.offsets_and_counts_binding().unwrap(),
},
BindGroupEntry {
binding: 9,
resource: globals.clone(),
},
],
label: Some("mesh_view_bind_group"),
layout: &mesh_pipeline.view_layout,
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_pbr/src/render/mesh_view_bindings.wgsl
Expand Up @@ -40,3 +40,6 @@ var<storage> cluster_light_index_lists: ClusterLightIndexLists;
@group(0) @binding(8)
var<storage> cluster_offsets_and_counts: ClusterOffsetsAndCounts;
#endif

@group(0) @binding(9)
var<uniform> globals: Globals;
7 changes: 7 additions & 0 deletions crates/bevy_pbr/src/render/mesh_view_types.wgsl
Expand Up @@ -85,3 +85,10 @@ struct ClusterOffsetsAndCounts {
data: array<vec4<u32>>,
};
#endif

struct Globals {
// The time since startup in seconds
time: f32,
// The delta time of the previous frame in seconds
delta_time: f32,
}
60 changes: 60 additions & 0 deletions crates/bevy_render/src/globals.rs
@@ -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);
}
5 changes: 4 additions & 1 deletion crates/bevy_render/src/lib.rs
Expand Up @@ -5,6 +5,7 @@ pub mod color;
pub mod extract_component;
mod extract_param;
pub mod extract_resource;
pub mod globals;
pub mod mesh;
pub mod primitives;
pub mod rangefinder;
Expand Down Expand Up @@ -33,6 +34,7 @@ pub mod prelude {
};
}

use globals::GlobalsPlugin;
pub use once_cell;

use crate::{
Expand Down Expand Up @@ -321,7 +323,8 @@ impl Plugin for RenderPlugin {
.add_plugin(MeshPlugin)
// NOTE: Load this after renderer initialization so that it knows about the supported
// compressed texture formats
.add_plugin(ImagePlugin);
.add_plugin(ImagePlugin)
.add_plugin(GlobalsPlugin);
}
}

Expand Down

0 comments on commit c6d6874

Please sign in to comment.