Skip to content

Commit d836ece

Browse files
authored
Moving structs PointLight, SpotLight, and DirectionalLight out of light/mod.rs (#12656)
# Objective Follow up from PR #12369 to extract lighting structs from light/mod.rs into their own file. Part of the Purdue Refactoring Team's goals issue #12349 ## Solution - Moved PointLight from light/mod.rs to light/point_light.rs - Moved SpotLight from light/mod.rs to light/spot_light.rs - Moved DirectionalLight from light/mod.rs to light/directional_light.rs
1 parent d80f05c commit d836ece

File tree

4 files changed

+202
-193
lines changed

4 files changed

+202
-193
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use super::*;
2+
3+
/// A Directional light.
4+
///
5+
/// Directional lights don't exist in reality but they are a good
6+
/// approximation for light sources VERY far away, like the sun or
7+
/// the moon.
8+
///
9+
/// The light shines along the forward direction of the entity's transform. With a default transform
10+
/// this would be along the negative-Z axis.
11+
///
12+
/// Valid values for `illuminance` are:
13+
///
14+
/// | Illuminance (lux) | Surfaces illuminated by |
15+
/// |-------------------|------------------------------------------------|
16+
/// | 0.0001 | Moonless, overcast night sky (starlight) |
17+
/// | 0.002 | Moonless clear night sky with airglow |
18+
/// | 0.05–0.3 | Full moon on a clear night |
19+
/// | 3.4 | Dark limit of civil twilight under a clear sky |
20+
/// | 20–50 | Public areas with dark surroundings |
21+
/// | 50 | Family living room lights |
22+
/// | 80 | Office building hallway/toilet lighting |
23+
/// | 100 | Very dark overcast day |
24+
/// | 150 | Train station platforms |
25+
/// | 320–500 | Office lighting |
26+
/// | 400 | Sunrise or sunset on a clear day. |
27+
/// | 1000 | Overcast day; typical TV studio lighting |
28+
/// | 10,000–25,000 | Full daylight (not direct sun) |
29+
/// | 32,000–100,000 | Direct sunlight |
30+
///
31+
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lux)
32+
///
33+
/// ## Shadows
34+
///
35+
/// To enable shadows, set the `shadows_enabled` property to `true`.
36+
///
37+
/// Shadows are produced via [cascaded shadow maps](https://developer.download.nvidia.com/SDK/10.5/opengl/src/cascaded_shadow_maps/doc/cascaded_shadow_maps.pdf).
38+
///
39+
/// To modify the cascade set up, such as the number of cascades or the maximum shadow distance,
40+
/// change the [`CascadeShadowConfig`] component of the [`DirectionalLightBundle`].
41+
///
42+
/// To control the resolution of the shadow maps, use the [`DirectionalLightShadowMap`] resource:
43+
///
44+
/// ```
45+
/// # use bevy_app::prelude::*;
46+
/// # use bevy_pbr::DirectionalLightShadowMap;
47+
/// App::new()
48+
/// .insert_resource(DirectionalLightShadowMap { size: 2048 });
49+
/// ```
50+
#[derive(Component, Debug, Clone, Reflect)]
51+
#[reflect(Component, Default)]
52+
pub struct DirectionalLight {
53+
pub color: Color,
54+
/// Illuminance in lux (lumens per square meter), representing the amount of
55+
/// light projected onto surfaces by this light source. Lux is used here
56+
/// instead of lumens because a directional light illuminates all surfaces
57+
/// more-or-less the same way (depending on the angle of incidence). Lumens
58+
/// can only be specified for light sources which emit light from a specific
59+
/// area.
60+
pub illuminance: f32,
61+
pub shadows_enabled: bool,
62+
pub shadow_depth_bias: f32,
63+
/// A bias applied along the direction of the fragment's surface normal. It is scaled to the
64+
/// shadow map's texel size so that it is automatically adjusted to the orthographic projection.
65+
pub shadow_normal_bias: f32,
66+
}
67+
68+
impl Default for DirectionalLight {
69+
fn default() -> Self {
70+
DirectionalLight {
71+
color: Color::WHITE,
72+
illuminance: light_consts::lux::AMBIENT_DAYLIGHT,
73+
shadows_enabled: false,
74+
shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,
75+
shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,
76+
}
77+
}
78+
}
79+
80+
impl DirectionalLight {
81+
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;
82+
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 1.8;
83+
}

crates/bevy_pbr/src/light/mod.rs

Lines changed: 6 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ use crate::*;
2222

2323
mod ambient_light;
2424
pub use ambient_light::AmbientLight;
25+
mod point_light;
26+
pub use point_light::PointLight;
27+
mod spot_light;
28+
pub use spot_light::SpotLight;
29+
mod directional_light;
30+
pub use directional_light::DirectionalLight;
2531

2632
/// Constants for operating with the light units: lumens, and lux.
2733
pub mod light_consts {
@@ -80,61 +86,6 @@ pub mod light_consts {
8086
}
8187
}
8288

83-
/// A light that emits light in all directions from a central point.
84-
///
85-
/// Real-world values for `intensity` (luminous power in lumens) based on the electrical power
86-
/// consumption of the type of real-world light are:
87-
///
88-
/// | Luminous Power (lumen) (i.e. the intensity member) | Incandescent non-halogen (Watts) | Incandescent halogen (Watts) | Compact fluorescent (Watts) | LED (Watts |
89-
/// |------|-----|----|--------|-------|
90-
/// | 200 | 25 | | 3-5 | 3 |
91-
/// | 450 | 40 | 29 | 9-11 | 5-8 |
92-
/// | 800 | 60 | | 13-15 | 8-12 |
93-
/// | 1100 | 75 | 53 | 18-20 | 10-16 |
94-
/// | 1600 | 100 | 72 | 24-28 | 14-17 |
95-
/// | 2400 | 150 | | 30-52 | 24-30 |
96-
/// | 3100 | 200 | | 49-75 | 32 |
97-
/// | 4000 | 300 | | 75-100 | 40.5 |
98-
///
99-
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lumen_(unit)#Lighting)
100-
#[derive(Component, Debug, Clone, Copy, Reflect)]
101-
#[reflect(Component, Default)]
102-
pub struct PointLight {
103-
pub color: Color,
104-
/// Luminous power in lumens, representing the amount of light emitted by this source in all directions.
105-
pub intensity: f32,
106-
pub range: f32,
107-
pub radius: f32,
108-
pub shadows_enabled: bool,
109-
pub shadow_depth_bias: f32,
110-
/// A bias applied along the direction of the fragment's surface normal. It is scaled to the
111-
/// shadow map's texel size so that it can be small close to the camera and gets larger further
112-
/// away.
113-
pub shadow_normal_bias: f32,
114-
}
115-
116-
impl Default for PointLight {
117-
fn default() -> Self {
118-
PointLight {
119-
color: Color::WHITE,
120-
// 1,000,000 lumens is a very large "cinema light" capable of registering brightly at Bevy's
121-
// default "very overcast day" exposure level. For "indoor lighting" with a lower exposure,
122-
// this would be way too bright.
123-
intensity: 1_000_000.0,
124-
range: 20.0,
125-
radius: 0.0,
126-
shadows_enabled: false,
127-
shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,
128-
shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,
129-
}
130-
}
131-
}
132-
133-
impl PointLight {
134-
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;
135-
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 0.6;
136-
}
137-
13889
#[derive(Resource, Clone, Debug, Reflect)]
13990
#[reflect(Resource)]
14091
pub struct PointLightShadowMap {
@@ -147,144 +98,6 @@ impl Default for PointLightShadowMap {
14798
}
14899
}
149100

150-
/// A light that emits light in a given direction from a central point.
151-
/// Behaves like a point light in a perfectly absorbent housing that
152-
/// shines light only in a given direction. The direction is taken from
153-
/// the transform, and can be specified with [`Transform::looking_at`](Transform::looking_at).
154-
#[derive(Component, Debug, Clone, Copy, Reflect)]
155-
#[reflect(Component, Default)]
156-
pub struct SpotLight {
157-
pub color: Color,
158-
/// Luminous power in lumens, representing the amount of light emitted by this source in all directions.
159-
pub intensity: f32,
160-
pub range: f32,
161-
pub radius: f32,
162-
pub shadows_enabled: bool,
163-
pub shadow_depth_bias: f32,
164-
/// A bias applied along the direction of the fragment's surface normal. It is scaled to the
165-
/// shadow map's texel size so that it can be small close to the camera and gets larger further
166-
/// away.
167-
pub shadow_normal_bias: f32,
168-
/// Angle defining the distance from the spot light direction to the outer limit
169-
/// of the light's cone of effect.
170-
/// `outer_angle` should be < `PI / 2.0`.
171-
/// `PI / 2.0` defines a hemispherical spot light, but shadows become very blocky as the angle
172-
/// approaches this limit.
173-
pub outer_angle: f32,
174-
/// Angle defining the distance from the spot light direction to the inner limit
175-
/// of the light's cone of effect.
176-
/// Light is attenuated from `inner_angle` to `outer_angle` to give a smooth falloff.
177-
/// `inner_angle` should be <= `outer_angle`
178-
pub inner_angle: f32,
179-
}
180-
181-
impl SpotLight {
182-
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;
183-
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 1.8;
184-
}
185-
186-
impl Default for SpotLight {
187-
fn default() -> Self {
188-
// a quarter arc attenuating from the center
189-
Self {
190-
color: Color::WHITE,
191-
// 1,000,000 lumens is a very large "cinema light" capable of registering brightly at Bevy's
192-
// default "very overcast day" exposure level. For "indoor lighting" with a lower exposure,
193-
// this would be way too bright.
194-
intensity: 1_000_000.0,
195-
range: 20.0,
196-
radius: 0.0,
197-
shadows_enabled: false,
198-
shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,
199-
shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,
200-
inner_angle: 0.0,
201-
outer_angle: std::f32::consts::FRAC_PI_4,
202-
}
203-
}
204-
}
205-
206-
/// A Directional light.
207-
///
208-
/// Directional lights don't exist in reality but they are a good
209-
/// approximation for light sources VERY far away, like the sun or
210-
/// the moon.
211-
///
212-
/// The light shines along the forward direction of the entity's transform. With a default transform
213-
/// this would be along the negative-Z axis.
214-
///
215-
/// Valid values for `illuminance` are:
216-
///
217-
/// | Illuminance (lux) | Surfaces illuminated by |
218-
/// |-------------------|------------------------------------------------|
219-
/// | 0.0001 | Moonless, overcast night sky (starlight) |
220-
/// | 0.002 | Moonless clear night sky with airglow |
221-
/// | 0.05–0.3 | Full moon on a clear night |
222-
/// | 3.4 | Dark limit of civil twilight under a clear sky |
223-
/// | 20–50 | Public areas with dark surroundings |
224-
/// | 50 | Family living room lights |
225-
/// | 80 | Office building hallway/toilet lighting |
226-
/// | 100 | Very dark overcast day |
227-
/// | 150 | Train station platforms |
228-
/// | 320–500 | Office lighting |
229-
/// | 400 | Sunrise or sunset on a clear day. |
230-
/// | 1000 | Overcast day; typical TV studio lighting |
231-
/// | 10,000–25,000 | Full daylight (not direct sun) |
232-
/// | 32,000–100,000 | Direct sunlight |
233-
///
234-
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lux)
235-
///
236-
/// ## Shadows
237-
///
238-
/// To enable shadows, set the `shadows_enabled` property to `true`.
239-
///
240-
/// Shadows are produced via [cascaded shadow maps](https://developer.download.nvidia.com/SDK/10.5/opengl/src/cascaded_shadow_maps/doc/cascaded_shadow_maps.pdf).
241-
///
242-
/// To modify the cascade set up, such as the number of cascades or the maximum shadow distance,
243-
/// change the [`CascadeShadowConfig`] component of the [`DirectionalLightBundle`].
244-
///
245-
/// To control the resolution of the shadow maps, use the [`DirectionalLightShadowMap`] resource:
246-
///
247-
/// ```
248-
/// # use bevy_app::prelude::*;
249-
/// # use bevy_pbr::DirectionalLightShadowMap;
250-
/// App::new()
251-
/// .insert_resource(DirectionalLightShadowMap { size: 2048 });
252-
/// ```
253-
#[derive(Component, Debug, Clone, Reflect)]
254-
#[reflect(Component, Default)]
255-
pub struct DirectionalLight {
256-
pub color: Color,
257-
/// Illuminance in lux (lumens per square meter), representing the amount of
258-
/// light projected onto surfaces by this light source. Lux is used here
259-
/// instead of lumens because a directional light illuminates all surfaces
260-
/// more-or-less the same way (depending on the angle of incidence). Lumens
261-
/// can only be specified for light sources which emit light from a specific
262-
/// area.
263-
pub illuminance: f32,
264-
pub shadows_enabled: bool,
265-
pub shadow_depth_bias: f32,
266-
/// A bias applied along the direction of the fragment's surface normal. It is scaled to the
267-
/// shadow map's texel size so that it is automatically adjusted to the orthographic projection.
268-
pub shadow_normal_bias: f32,
269-
}
270-
271-
impl Default for DirectionalLight {
272-
fn default() -> Self {
273-
DirectionalLight {
274-
color: Color::WHITE,
275-
illuminance: light_consts::lux::AMBIENT_DAYLIGHT,
276-
shadows_enabled: false,
277-
shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,
278-
shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,
279-
}
280-
}
281-
}
282-
283-
impl DirectionalLight {
284-
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;
285-
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 1.8;
286-
}
287-
288101
/// Controls the resolution of [`DirectionalLight`] shadow maps.
289102
#[derive(Resource, Clone, Debug, Reflect)]
290103
#[reflect(Resource)]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use super::*;
2+
3+
/// A light that emits light in all directions from a central point.
4+
///
5+
/// Real-world values for `intensity` (luminous power in lumens) based on the electrical power
6+
/// consumption of the type of real-world light are:
7+
///
8+
/// | Luminous Power (lumen) (i.e. the intensity member) | Incandescent non-halogen (Watts) | Incandescent halogen (Watts) | Compact fluorescent (Watts) | LED (Watts |
9+
/// |------|-----|----|--------|-------|
10+
/// | 200 | 25 | | 3-5 | 3 |
11+
/// | 450 | 40 | 29 | 9-11 | 5-8 |
12+
/// | 800 | 60 | | 13-15 | 8-12 |
13+
/// | 1100 | 75 | 53 | 18-20 | 10-16 |
14+
/// | 1600 | 100 | 72 | 24-28 | 14-17 |
15+
/// | 2400 | 150 | | 30-52 | 24-30 |
16+
/// | 3100 | 200 | | 49-75 | 32 |
17+
/// | 4000 | 300 | | 75-100 | 40.5 |
18+
///
19+
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lumen_(unit)#Lighting)
20+
#[derive(Component, Debug, Clone, Copy, Reflect)]
21+
#[reflect(Component, Default)]
22+
pub struct PointLight {
23+
pub color: Color,
24+
/// Luminous power in lumens, representing the amount of light emitted by this source in all directions.
25+
pub intensity: f32,
26+
pub range: f32,
27+
pub radius: f32,
28+
pub shadows_enabled: bool,
29+
pub shadow_depth_bias: f32,
30+
/// A bias applied along the direction of the fragment's surface normal. It is scaled to the
31+
/// shadow map's texel size so that it can be small close to the camera and gets larger further
32+
/// away.
33+
pub shadow_normal_bias: f32,
34+
}
35+
36+
impl Default for PointLight {
37+
fn default() -> Self {
38+
PointLight {
39+
color: Color::WHITE,
40+
// 1,000,000 lumens is a very large "cinema light" capable of registering brightly at Bevy's
41+
// default "very overcast day" exposure level. For "indoor lighting" with a lower exposure,
42+
// this would be way too bright.
43+
intensity: 1_000_000.0,
44+
range: 20.0,
45+
radius: 0.0,
46+
shadows_enabled: false,
47+
shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,
48+
shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,
49+
}
50+
}
51+
}
52+
53+
impl PointLight {
54+
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;
55+
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 0.6;
56+
}

0 commit comments

Comments
 (0)