From 18195bfa91d0e294f28b51e46709ed54482a6dd0 Mon Sep 17 00:00:00 2001 From: Jackson Lango <5734630+no1hitjam@users.noreply.github.com> Date: Sun, 15 Nov 2020 11:34:55 -0800 Subject: [PATCH] Controllable ambient light color (#852) Control ambient light color via resource The AmbientLight resource now controls the ambient light color in the pbr fragment shader. --- crates/bevy_pbr/src/lib.rs | 3 ++- crates/bevy_pbr/src/light.rs | 14 ++++++++++++++ .../src/render_graph/forward_pipeline/forward.frag | 4 ++-- crates/bevy_pbr/src/render_graph/lights_node.rs | 13 ++++++++++--- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index 8270b46859558..4381a3863b2db 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -32,7 +32,8 @@ impl Plugin for PbrPlugin { .add_system_to_stage( stage::POST_UPDATE, shader::asset_shader_defs_system::.system(), - ); + ) + .init_resource::(); let resources = app.resources(); let mut render_graph = resources.get_mut::().unwrap(); add_pbr_graph(&mut render_graph, resources); diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index e9d7c4427ac74..40f81403846ad 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -53,3 +53,17 @@ impl LightRaw { } } } + +// Ambient light color. +#[derive(Debug)] +pub struct AmbientLight { + pub color: Color, +} + +impl Default for AmbientLight { + fn default() -> Self { + Self { + color: Color::rgb(0.05, 0.05, 0.05), + } + } +} diff --git a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag b/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag index 9c869f29e7129..0b8242a98d410 100644 --- a/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag +++ b/crates/bevy_pbr/src/render_graph/forward_pipeline/forward.frag @@ -19,6 +19,7 @@ layout(set = 0, binding = 0) uniform Camera { }; layout(set = 1, binding = 0) uniform Lights { + vec3 AmbientColor; uvec4 NumLights; Light SceneLights[MAX_LIGHTS]; }; @@ -42,9 +43,8 @@ void main() { # ifdef STANDARDMATERIAL_SHADED vec3 normal = normalize(v_Normal); - vec3 ambient = vec3(0.05, 0.05, 0.05); // accumulate color - vec3 color = ambient; + vec3 color = AmbientColor; for (int i=0; i, render_resource_context: Res>, + ambient_light_resource: Res, // TODO: this write on RenderResourceBindings will prevent this system from running in parallel with other systems that do the same mut render_resource_bindings: ResMut, query: Query<(&Light, &GlobalTransform)>, @@ -85,9 +86,11 @@ pub fn lights_node_system( let state = &mut state; let render_resource_context = &**render_resource_context; + let ambient_light: [f32; 4] = ambient_light_resource.color.into(); + let ambient_light_size = std::mem::size_of::<[f32; 4]>(); let light_count = query.iter().count(); let size = std::mem::size_of::(); - let light_count_size = std::mem::size_of::(); + let light_count_size = ambient_light_size + std::mem::size_of::(); let light_array_size = size * light_count; let light_array_max_size = size * state.max_lights; let current_light_uniform_size = light_count_size + light_array_size; @@ -128,8 +131,12 @@ pub fn lights_node_system( staging_buffer, 0..current_light_uniform_size as u64, &mut |data, _renderer| { + // ambient light + data[0..ambient_light_size].copy_from_slice(ambient_light.as_bytes()); + // light count - data[0..light_count_size].copy_from_slice([light_count as u32, 0, 0, 0].as_bytes()); + data[ambient_light_size..light_count_size] + .copy_from_slice([light_count as u32, 0, 0, 0].as_bytes()); // light array for ((light, global_transform), slot) in query