Ambient component (#17343)

# Objective

allow setting ambient light via component on cameras. 
arguably fixes #7193
note i chose to use a component rather than an entity since it was not
clear to me how to manage multiple ambient sources for a single
renderlayer, and it makes for a very small changeset.

## Solution

- make ambient light a component as well as a resource
- extract it
- use the component if present on a camera, fallback to the resource

## Testing

i added 
```rs
        if index == 1 {
            commands.entity(camera).insert(AmbientLight{
                color: Color::linear_rgba(1.0, 0.0, 0.0, 1.0),
                brightness: 1000.0,
                ..Default::default()                
            });
        }
```
at line 84 of the split_screen example

---------

Co-authored-by: François Mockers <mockersf@gmail.com>
This commit is contained in:
robtfm 2025-01-14 08:10:15 +00:00 committed by GitHub
parent d34803f5f4
commit 47d25c13d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 3 deletions

View File

@ -341,6 +341,7 @@ impl Plugin for PbrPlugin {
SyncComponentPlugin::<DirectionalLight>::default(),
SyncComponentPlugin::<PointLight>::default(),
SyncComponentPlugin::<SpotLight>::default(),
ExtractComponentPlugin::<AmbientLight>::default(),
))
.configure_sets(
PostUpdate,

View File

@ -4,6 +4,8 @@ use super::*;
///
/// This resource is inserted by the [`PbrPlugin`] and by default it is set to a low ambient light.
///
/// It can also be added to a camera to override the resource (or default) ambient for that camera only.
///
/// # Examples
///
/// Make ambient light slightly brighter:
@ -15,8 +17,9 @@ use super::*;
/// ambient_light.brightness = 100.0;
/// }
/// ```
#[derive(Resource, Clone, Debug, ExtractResource, Reflect)]
#[reflect(Resource, Debug, Default)]
#[derive(Resource, Component, Clone, Debug, ExtractResource, ExtractComponent, Reflect)]
#[reflect(Resource, Component, Debug, Default)]
#[require(Camera)]
pub struct AmbientLight {
pub color: Color,

View File

@ -705,6 +705,7 @@ pub fn prepare_lights(
&ExtractedClusterConfig,
Option<&RenderLayers>,
Has<NoIndirectDrawing>,
Option<&AmbientLight>,
),
With<Camera3d>,
>,
@ -1115,7 +1116,14 @@ pub fn prepare_lights(
let mut live_views = EntityHashSet::with_capacity(views_count);
// set up light data for each view
for (entity, extracted_view, clusters, maybe_layers, no_indirect_drawing) in sorted_cameras
for (
entity,
extracted_view,
clusters,
maybe_layers,
no_indirect_drawing,
maybe_ambient_override,
) in sorted_cameras
.0
.iter()
.filter_map(|sorted_camera| views.get(sorted_camera.entity).ok())
@ -1138,6 +1146,7 @@ pub fn prepare_lights(
);
let n_clusters = clusters.dimensions.x * clusters.dimensions.y * clusters.dimensions.z;
let ambient_light = maybe_ambient_override.unwrap_or(&ambient_light);
let mut gpu_lights = GpuLights {
directional_lights: gpu_directional_lights,
ambient_color: Vec4::from_slice(&LinearRgba::from(ambient_light.color).to_f32_array())