Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controllable ambient light color #852

Merged
merged 4 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use bevy_render::{prelude::Color, render_graph::RenderGraph, shader};
use bevy_type_registry::RegisterType;
use light::Light;
use material::StandardMaterial;
use render_graph::add_pbr_graph;
use render_graph::{add_pbr_graph, AmbientLight};

/// NOTE: this isn't PBR yet. consider this name "aspirational" :)
#[derive(Default)]
Expand All @@ -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
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
23 changes: 21 additions & 2 deletions crates/bevy_pbr/src/render_graph/lights_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy_render::{
BufferId, BufferInfo, BufferUsage, RenderContext, RenderResourceBinding,
RenderResourceBindings, RenderResourceContext,
},
color::Color,
};
use bevy_transform::prelude::*;

Expand Down Expand Up @@ -75,19 +76,34 @@ pub struct LightsNodeSystemState {
max_lights: usize,
}

// Ambient color.
#[derive(Debug)]
pub struct AmbientLight {
no1hitjam marked this conversation as resolved.
Show resolved Hide resolved
pub color: Color,
}

impl Default for AmbientLight {
fn default() -> Self {
Self{ color: Color::rgb(0.05, 0.05, 0.05) }
}
}

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 +144,11 @@ 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