Skip to main content

bevy_light/
ambient_light.rs

1use bevy_camera::Camera;
2use bevy_color::Color;
3use bevy_ecs::prelude::*;
4use bevy_reflect::prelude::*;
5
6/// An ambient light, which lights the entire scene equally.
7///
8/// It can be added to a camera to override [`GlobalAmbientLight`], which is the default that is otherwise used.
9#[derive(Component, Clone, Debug, Reflect)]
10#[reflect(Component, Debug, Default, Clone)]
11#[require(Camera)]
12pub struct AmbientLight {
13    /// The color of the ambient light.
14    pub color: Color,
15
16    /// A direct scale factor multiplied with `color` before being passed to the shader.
17    ///
18    /// After applying this multiplier, the resulting value should be in units of [cd/m^2].
19    ///
20    /// [cd/m^2]: https://en.wikipedia.org/wiki/Candela_per_square_metre
21    pub brightness: f32,
22
23    /// Whether this ambient light has an effect on meshes with lightmaps.
24    ///
25    /// Set this to false if your lightmap baking tool bakes the ambient light
26    /// into the lightmaps, to avoid rendering that light twice.
27    ///
28    /// By default, this is set to true.
29    pub affects_lightmapped_meshes: bool,
30}
31
32impl Default for AmbientLight {
33    fn default() -> Self {
34        Self {
35            color: Color::WHITE,
36            brightness: 80.0,
37            affects_lightmapped_meshes: true,
38        }
39    }
40}
41
42/// The global ambient light, which lights the entire scene equally.
43///
44/// This resource is inserted by the [`LightPlugin`] and by default it is set to a low ambient light.
45/// Inserting an [`AmbientLight`] on a camera will override this default.
46///
47/// # Examples
48///
49/// Make ambient light slightly brighter:
50///
51/// ```
52/// # use bevy_ecs::system::ResMut;
53/// # use bevy_light::GlobalAmbientLight;
54/// fn setup_ambient_light(mut ambient_light: ResMut<GlobalAmbientLight>) {
55///    ambient_light.brightness = 100.0;
56/// }
57/// ```
58///
59/// [`LightPlugin`]: crate::LightPlugin
60#[derive(Resource, Clone, Debug, Reflect)]
61#[reflect(Resource, Debug, Default, Clone)]
62pub struct GlobalAmbientLight {
63    /// The color of the ambient light.
64    pub color: Color,
65
66    /// A direct scale factor multiplied with `color` before being passed to the shader.
67    ///
68    /// After applying this multiplier, the resulting value should be in units of [cd/m^2].
69    ///
70    /// [cd/m^2]: https://en.wikipedia.org/wiki/Candela_per_square_metre
71    pub brightness: f32,
72
73    /// Whether this ambient light has an effect on meshes with lightmaps.
74    ///
75    /// Set this to false if your lightmap baking tool bakes the ambient light
76    /// into the lightmaps, to avoid rendering that light twice.
77    ///
78    /// By default, this is set to true.
79    pub affects_lightmapped_meshes: bool,
80}
81
82impl Default for GlobalAmbientLight {
83    fn default() -> Self {
84        Self {
85            color: Color::WHITE,
86            brightness: 80.0,
87            affects_lightmapped_meshes: true,
88        }
89    }
90}
91
92impl GlobalAmbientLight {
93    /// Convenience constant for turning off global ambient light.
94    pub const NONE: GlobalAmbientLight = GlobalAmbientLight {
95        color: Color::WHITE,
96        brightness: 0.0,
97        affects_lightmapped_meshes: true,
98    };
99}