Skip to content

Commit

Permalink
Controllable ambient light color (#852)
Browse files Browse the repository at this point in the history
Control ambient light color via resource

The AmbientLight resource now controls the ambient light color in the
pbr fragment shader.
  • Loading branch information
no1hitjam authored Nov 15, 2020
1 parent 23fcdfa commit 18195bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl Plugin for PbrPlugin {
.add_system_to_stage(
stage::POST_UPDATE,
shader::asset_shader_defs_system::<StandardMaterial>.system(),
);
)
.init_resource::<AmbientLight>();
let resources = app.resources();
let mut render_graph = resources.get_mut::<RenderGraph>().unwrap();
add_pbr_graph(&mut render_graph, resources);
Expand Down
14 changes: 14 additions & 0 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
Expand All @@ -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<int(NumLights.x) && i<MAX_LIGHTS; ++i) {
Light light = SceneLights[i];
// compute Lambertian diffuse term
Expand Down
13 changes: 10 additions & 3 deletions crates/bevy_pbr/src/render_graph/lights_node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
light::{Light, LightRaw},
light::{AmbientLight, Light, LightRaw},
render_graph::uniform,
};
use bevy_core::{AsBytes, Byteable};
Expand Down Expand Up @@ -78,16 +78,19 @@ pub struct LightsNodeSystemState {
pub fn lights_node_system(
mut state: Local<LightsNodeSystemState>,
render_resource_context: Res<Box<dyn RenderResourceContext>>,
ambient_light_resource: Res<AmbientLight>,
// 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<RenderResourceBindings>,
query: Query<(&Light, &GlobalTransform)>,
) {
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::<LightRaw>();
let light_count_size = std::mem::size_of::<LightCount>();
let light_count_size = ambient_light_size + std::mem::size_of::<LightCount>();
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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 18195bf

Please sign in to comment.