Skip to content

Commit de888a3

Browse files
authored
Migrate lights to required components (bevyengine#15554)
# Objective Another step in the migration to required components: lights! Note that this does not include `EnvironmentMapLight` or reflection probes yet, because their API hasn't been fully chosen yet. ## Solution As per the [selected proposals](https://hackmd.io/@bevy/required_components/%2FLLnzwz9XTxiD7i2jiUXkJg): - Deprecate `PointLightBundle` in favor of the `PointLight` component - Deprecate `SpotLightBundle` in favor of the `PointLight` component - Deprecate `DirectionalLightBundle` in favor of the `DirectionalLight` component ## Testing I ran some examples with lights. --- ## Migration Guide `PointLightBundle`, `SpotLightBundle`, and `DirectionalLightBundle` have been deprecated. Use the `PointLight`, `SpotLight`, and `DirectionalLight` components instead. Adding them will now insert the other components required by them automatically.
1 parent 383c2e5 commit de888a3

File tree

104 files changed

+538
-720
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+538
-720
lines changed

crates/bevy_gltf/src/loader.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use bevy_ecs::{
1717
use bevy_hierarchy::{BuildChildren, ChildBuild, WorldChildBuilder};
1818
use bevy_math::{Affine2, Mat4, Vec3};
1919
use bevy_pbr::{
20-
DirectionalLight, DirectionalLightBundle, PbrBundle, PointLight, PointLightBundle, SpotLight,
21-
SpotLightBundle, StandardMaterial, UvChannel, MAX_JOINTS,
20+
DirectionalLight, PbrBundle, PointLight, SpotLight, StandardMaterial, UvChannel, MAX_JOINTS,
2221
};
2322
use bevy_render::{
2423
alpha::AlphaMode,
@@ -1523,14 +1522,11 @@ fn load_node(
15231522
if let Some(light) = gltf_node.light() {
15241523
match light.kind() {
15251524
gltf::khr_lights_punctual::Kind::Directional => {
1526-
let mut entity = parent.spawn(DirectionalLightBundle {
1527-
directional_light: DirectionalLight {
1528-
color: Color::srgb_from_array(light.color()),
1529-
// NOTE: KHR_punctual_lights defines the intensity units for directional
1530-
// lights in lux (lm/m^2) which is what we need.
1531-
illuminance: light.intensity(),
1532-
..Default::default()
1533-
},
1525+
let mut entity = parent.spawn(DirectionalLight {
1526+
color: Color::srgb_from_array(light.color()),
1527+
// NOTE: KHR_punctual_lights defines the intensity units for directional
1528+
// lights in lux (lm/m^2) which is what we need.
1529+
illuminance: light.intensity(),
15341530
..Default::default()
15351531
});
15361532
if let Some(name) = light.name() {
@@ -1543,17 +1539,14 @@ fn load_node(
15431539
}
15441540
}
15451541
gltf::khr_lights_punctual::Kind::Point => {
1546-
let mut entity = parent.spawn(PointLightBundle {
1547-
point_light: PointLight {
1548-
color: Color::srgb_from_array(light.color()),
1549-
// NOTE: KHR_punctual_lights defines the intensity units for point lights in
1550-
// candela (lm/sr) which is luminous intensity and we need luminous power.
1551-
// For a point light, luminous power = 4 * pi * luminous intensity
1552-
intensity: light.intensity() * core::f32::consts::PI * 4.0,
1553-
range: light.range().unwrap_or(20.0),
1554-
radius: 0.0,
1555-
..Default::default()
1556-
},
1542+
let mut entity = parent.spawn(PointLight {
1543+
color: Color::srgb_from_array(light.color()),
1544+
// NOTE: KHR_punctual_lights defines the intensity units for point lights in
1545+
// candela (lm/sr) which is luminous intensity and we need luminous power.
1546+
// For a point light, luminous power = 4 * pi * luminous intensity
1547+
intensity: light.intensity() * core::f32::consts::PI * 4.0,
1548+
range: light.range().unwrap_or(20.0),
1549+
radius: 0.0,
15571550
..Default::default()
15581551
});
15591552
if let Some(name) = light.name() {
@@ -1569,19 +1562,16 @@ fn load_node(
15691562
inner_cone_angle,
15701563
outer_cone_angle,
15711564
} => {
1572-
let mut entity = parent.spawn(SpotLightBundle {
1573-
spot_light: SpotLight {
1574-
color: Color::srgb_from_array(light.color()),
1575-
// NOTE: KHR_punctual_lights defines the intensity units for spot lights in
1576-
// candela (lm/sr) which is luminous intensity and we need luminous power.
1577-
// For a spot light, we map luminous power = 4 * pi * luminous intensity
1578-
intensity: light.intensity() * core::f32::consts::PI * 4.0,
1579-
range: light.range().unwrap_or(20.0),
1580-
radius: light.range().unwrap_or(0.0),
1581-
inner_angle: inner_cone_angle,
1582-
outer_angle: outer_cone_angle,
1583-
..Default::default()
1584-
},
1565+
let mut entity = parent.spawn(SpotLight {
1566+
color: Color::srgb_from_array(light.color()),
1567+
// NOTE: KHR_punctual_lights defines the intensity units for spot lights in
1568+
// candela (lm/sr) which is luminous intensity and we need luminous power.
1569+
// For a spot light, we map luminous power = 4 * pi * luminous intensity
1570+
intensity: light.intensity() * core::f32::consts::PI * 4.0,
1571+
range: light.range().unwrap_or(20.0),
1572+
radius: light.range().unwrap_or(0.0),
1573+
inner_angle: inner_cone_angle,
1574+
outer_angle: outer_cone_angle,
15851575
..Default::default()
15861576
});
15871577
if let Some(name) = light.name() {

crates/bevy_pbr/src/bundle.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(deprecated)]
2+
13
use crate::{
24
CascadeShadowConfig, Cascades, DirectionalLight, Material, PointLight, SpotLight,
35
StandardMaterial,
@@ -97,6 +99,10 @@ pub struct CascadesVisibleEntities {
9799

98100
/// A component bundle for [`PointLight`] entities.
99101
#[derive(Debug, Bundle, Default, Clone)]
102+
#[deprecated(
103+
since = "0.15.0",
104+
note = "Use the `PointLight` component instead. Inserting it will now also insert the other components required by it automatically."
105+
)]
100106
pub struct PointLightBundle {
101107
pub point_light: PointLight,
102108
pub cubemap_visible_entities: CubemapVisibleEntities,
@@ -115,6 +121,10 @@ pub struct PointLightBundle {
115121

116122
/// A component bundle for spot light entities
117123
#[derive(Debug, Bundle, Default, Clone)]
124+
#[deprecated(
125+
since = "0.15.0",
126+
note = "Use the `SpotLight` component instead. Inserting it will now also insert the other components required by it automatically."
127+
)]
118128
pub struct SpotLightBundle {
119129
pub spot_light: SpotLight,
120130
pub visible_entities: VisibleMeshEntities,
@@ -133,6 +143,10 @@ pub struct SpotLightBundle {
133143

134144
/// A component bundle for [`DirectionalLight`] entities.
135145
#[derive(Debug, Bundle, Default, Clone)]
146+
#[deprecated(
147+
since = "0.15.0",
148+
note = "Use the `DirectionalLight` component instead. Inserting it will now also insert the other components required by it automatically."
149+
)]
136150
pub struct DirectionalLightBundle {
137151
pub directional_light: DirectionalLight,
138152
pub frusta: CascadesFrusta,

crates/bevy_pbr/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub use volumetric_fog::{
6868
/// The PBR prelude.
6969
///
7070
/// This includes the most common types in this crate, re-exported for your convenience.
71+
#[expect(deprecated)]
7172
pub mod prelude {
7273
#[doc(hidden)]
7374
pub use crate::{

crates/bevy_pbr/src/light/directional_light.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use bevy_render::{view::Visibility, world_sync::SyncToRenderWorld};
2+
13
use super::*;
24

35
/// A Directional light.
@@ -36,8 +38,8 @@ use super::*;
3638
///
3739
/// 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).
3840
///
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+
/// To modify the cascade setup, such as the number of cascades or the maximum shadow distance,
42+
/// change the [`CascadeShadowConfig`] component of the entity with the [`DirectionalLight`].
4143
///
4244
/// To control the resolution of the shadow maps, use the [`DirectionalLightShadowMap`] resource:
4345
///
@@ -49,6 +51,15 @@ use super::*;
4951
/// ```
5052
#[derive(Component, Debug, Clone, Reflect)]
5153
#[reflect(Component, Default, Debug)]
54+
#[require(
55+
Cascades,
56+
CascadesFrusta,
57+
CascadeShadowConfig,
58+
CascadesVisibleEntities,
59+
Transform,
60+
Visibility,
61+
SyncToRenderWorld
62+
)]
5263
pub struct DirectionalLight {
5364
/// The color of the light.
5465
///

crates/bevy_pbr/src/light/point_light.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use bevy_render::{view::Visibility, world_sync::SyncToRenderWorld};
2+
13
use super::*;
24

35
/// A light that emits light in all directions from a central point.
@@ -19,6 +21,13 @@ use super::*;
1921
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lumen_(unit)#Lighting)
2022
#[derive(Component, Debug, Clone, Copy, Reflect)]
2123
#[reflect(Component, Default, Debug)]
24+
#[require(
25+
CubemapFrusta,
26+
CubemapVisibleEntities,
27+
Transform,
28+
Visibility,
29+
SyncToRenderWorld
30+
)]
2231
pub struct PointLight {
2332
/// The color of this light source.
2433
pub color: Color,

crates/bevy_pbr/src/light/spot_light.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use bevy_render::{view::Visibility, world_sync::SyncToRenderWorld};
2+
13
use super::*;
24

35
/// A light that emits light in a given direction from a central point.
@@ -7,6 +9,7 @@ use super::*;
79
/// the transform, and can be specified with [`Transform::looking_at`](Transform::looking_at).
810
#[derive(Component, Debug, Clone, Copy, Reflect)]
911
#[reflect(Component, Default, Debug)]
12+
#[require(Frustum, VisibleMeshEntities, Transform, Visibility, SyncToRenderWorld)]
1013
pub struct SpotLight {
1114
/// The color of the light.
1215
///

examples/3d/3d_scene.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ fn setup(
3030
..default()
3131
});
3232
// light
33-
commands.spawn(PointLightBundle {
34-
point_light: PointLight {
33+
commands.spawn((
34+
PointLight {
3535
shadows_enabled: true,
3636
..default()
3737
},
38-
transform: Transform::from_xyz(4.0, 8.0, 4.0),
39-
..default()
40-
});
38+
Transform::from_xyz(4.0, 8.0, 4.0),
39+
));
4140
// camera
4241
commands.spawn(Camera3dBundle {
4342
transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),

examples/3d/3d_shapes.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,16 @@ fn setup(
116116
));
117117
}
118118

119-
commands.spawn(PointLightBundle {
120-
point_light: PointLight {
119+
commands.spawn((
120+
PointLight {
121121
shadows_enabled: true,
122122
intensity: 10_000_000.,
123123
range: 100.0,
124124
shadow_depth_bias: 0.2,
125125
..default()
126126
},
127-
transform: Transform::from_xyz(8.0, 16.0, 8.0),
128-
..default()
129-
});
127+
Transform::from_xyz(8.0, 16.0, 8.0),
128+
));
130129

131130
// ground plane
132131
commands.spawn(PbrBundle {

examples/3d/3d_viewport_to_world.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ fn setup(
6666
));
6767

6868
// light
69-
commands.spawn(DirectionalLightBundle {
70-
transform: Transform::from_translation(Vec3::ONE).looking_at(Vec3::ZERO, Vec3::Y),
71-
..default()
72-
});
69+
commands.spawn((
70+
DirectionalLight::default(),
71+
Transform::from_translation(Vec3::ONE).looking_at(Vec3::ZERO, Vec3::Y),
72+
));
7373

7474
// camera
7575
commands.spawn(Camera3dBundle {

examples/3d/anisotropy.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,24 +252,18 @@ fn add_skybox_and_environment_map(
252252

253253
/// Spawns a rotating directional light.
254254
fn spawn_directional_light(commands: &mut Commands) {
255-
commands.spawn(DirectionalLightBundle {
256-
directional_light: DirectionalLight {
257-
color: WHITE.into(),
258-
illuminance: 3000.0,
259-
..default()
260-
},
255+
commands.spawn(DirectionalLight {
256+
color: WHITE.into(),
257+
illuminance: 3000.0,
261258
..default()
262259
});
263260
}
264261

265262
/// Spawns a rotating point light.
266263
fn spawn_point_light(commands: &mut Commands) {
267-
commands.spawn(PointLightBundle {
268-
point_light: PointLight {
269-
color: WHITE.into(),
270-
intensity: 200000.0,
271-
..default()
272-
},
264+
commands.spawn(PointLight {
265+
color: WHITE.into(),
266+
intensity: 200000.0,
273267
..default()
274268
});
275269
}

0 commit comments

Comments
 (0)