mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
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.
This commit is contained in:
parent
23fcdfae56
commit
18195bfa91
4 changed files with 28 additions and 6 deletions
|
@ -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);
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<int(NumLights.x) && i<MAX_LIGHTS; ++i) {
|
||||
Light light = SceneLights[i];
|
||||
// compute Lambertian diffuse term
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
light::{Light, LightRaw},
|
||||
light::{AmbientLight, Light, LightRaw},
|
||||
render_graph::uniform,
|
||||
};
|
||||
use bevy_core::{AsBytes, Byteable};
|
||||
|
@ -78,6 +78,7 @@ 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)>,
|
||||
|
@ -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::<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;
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue