Skip to content

Commit 25bfa80

Browse files
authored
Migrate cameras to required components (bevyengine#15641)
# Objective Yet another PR for migrating stuff to required components. This time, cameras! ## Solution As per the [selected proposal](https://hackmd.io/tsYID4CGRiWxzsgawzxG_g#Combined-Proposal-1-Selected), deprecate `Camera2dBundle` and `Camera3dBundle` in favor of `Camera2d` and `Camera3d`. Adding a `Camera` without `Camera2d` or `Camera3d` now logs a warning, as suggested by Cart [on Discord](https://discord.com/channels/691052431525675048/1264881140007702558/1291506402832945273). I would personally like cameras to work a bit differently and be split into a few more components, to avoid some footguns and confusing semantics, but that is more controversial, and shouldn't block this core migration. ## Testing I ran a few 2D and 3D examples, and tried cameras with and without render graphs. --- ## Migration Guide `Camera2dBundle` and `Camera3dBundle` have been deprecated in favor of `Camera2d` and `Camera3d`. Inserting them will now also insert the other components required by them automatically.
1 parent ac9b0c8 commit 25bfa80

File tree

241 files changed

+870
-969
lines changed

Some content is hidden

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

241 files changed

+870
-969
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ android_shared_stdcxx = ["bevy_internal/android_shared_stdcxx"]
346346
# Enable detailed trace event logging. These trace events are expensive even when off, thus they require compile time opt-in
347347
detailed_trace = ["bevy_internal/detailed_trace"]
348348

349-
# Include tonemapping Look Up Tables KTX2 files. If everything is pink, you need to enable this feature or change the `Tonemapping` method on your `Camera2dBundle` or `Camera3dBundle`.
349+
# Include tonemapping Look Up Tables KTX2 files. If everything is pink, you need to enable this feature or change the `Tonemapping` method for your `Camera2d` or `Camera3d`.
350350
tonemapping_luts = ["bevy_internal/tonemapping_luts", "ktx2", "zstd"]
351351

352352
# Include SMAA Look Up Tables KTX2 Files

crates/bevy_core_pipeline/src/core_2d/camera_2d.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(deprecated)]
2+
13
use crate::{
24
core_2d::graph::Core2d,
35
tonemapping::{DebandDither, Tonemapping},
@@ -17,12 +19,25 @@ use bevy_render::{
1719
};
1820
use bevy_transform::prelude::{GlobalTransform, Transform};
1921

22+
/// A 2D camera component. Enables the 2D render graph for a [`Camera`].
2023
#[derive(Component, Default, Reflect, Clone, ExtractComponent)]
2124
#[extract_component_filter(With<Camera>)]
2225
#[reflect(Component, Default)]
26+
#[require(
27+
Camera,
28+
DebandDither,
29+
CameraRenderGraph(|| CameraRenderGraph::new(Core2d)),
30+
OrthographicProjection(OrthographicProjection::default_2d),
31+
Frustum(|| OrthographicProjection::default_2d().compute_frustum(&GlobalTransform::from(Transform::default()))),
32+
Tonemapping(|| Tonemapping::None),
33+
)]
2334
pub struct Camera2d;
2435

2536
#[derive(Bundle, Clone)]
37+
#[deprecated(
38+
since = "0.15.0",
39+
note = "Use the `Camera2d` component instead. Inserting it will now also insert the other components required by it automatically."
40+
)]
2641
pub struct Camera2dBundle {
2742
pub camera: Camera,
2843
pub camera_render_graph: CameraRenderGraph,

crates/bevy_core_pipeline/src/core_3d/camera_3d.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(deprecated)]
2+
13
use crate::{
24
core_3d::graph::Core3d,
35
tonemapping::{DebandDither, Tonemapping},
@@ -15,12 +17,22 @@ use bevy_render::{
1517
use bevy_transform::prelude::{GlobalTransform, Transform};
1618
use serde::{Deserialize, Serialize};
1719

18-
/// Configuration for the "main 3d render graph".
19-
/// The camera coordinate space is right-handed x-right, y-up, z-back.
20+
/// A 3D camera component. Enables the main 3D render graph for a [`Camera`].
21+
///
22+
/// The camera coordinate space is right-handed X-right, Y-up, Z-back.
2023
/// This means "forward" is -Z.
2124
#[derive(Component, Reflect, Clone, ExtractComponent)]
2225
#[extract_component_filter(With<Camera>)]
2326
#[reflect(Component, Default)]
27+
#[require(
28+
Camera,
29+
DebandDither(|| DebandDither::Enabled),
30+
CameraRenderGraph(|| CameraRenderGraph::new(Core3d)),
31+
Projection,
32+
Tonemapping,
33+
ColorGrading,
34+
Exposure
35+
)]
2436
pub struct Camera3d {
2537
/// The depth clear operation to perform for the main 3d pass.
2638
pub depth_load_op: Camera3dDepthLoadOp,
@@ -139,6 +151,10 @@ pub enum ScreenSpaceTransmissionQuality {
139151
/// The camera coordinate space is right-handed x-right, y-up, z-back.
140152
/// This means "forward" is -Z.
141153
#[derive(Bundle, Clone)]
154+
#[deprecated(
155+
since = "0.15.0",
156+
note = "Use the `Camera3d` component instead. Inserting it will now also insert the other components required by it automatically."
157+
)]
142158
pub struct Camera3dBundle {
143159
pub camera: Camera,
144160
pub camera_render_graph: CameraRenderGraph,

crates/bevy_core_pipeline/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub mod experimental {
4545
/// The core pipeline prelude.
4646
///
4747
/// This includes the most common types in this crate, re-exported for your convenience.
48+
#[expect(deprecated)]
4849
pub mod prelude {
4950
#[doc(hidden)]
5051
pub use crate::{

crates/bevy_core_pipeline/src/motion_blur/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ pub struct MotionBlurBundle {
5959
/// camera.
6060
///
6161
/// ```
62-
/// # use bevy_core_pipeline::{core_3d::Camera3dBundle, motion_blur::MotionBlur};
62+
/// # use bevy_core_pipeline::{core_3d::Camera3d, motion_blur::MotionBlur};
6363
/// # use bevy_ecs::prelude::*;
6464
/// # fn test(mut commands: Commands) {
6565
/// commands.spawn((
66-
/// Camera3dBundle::default(),
66+
/// Camera3d::default(),
6767
/// MotionBlur::default(),
6868
/// ));
6969
/// # }

crates/bevy_core_pipeline/src/tonemapping/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl SpecializedRenderPipeline for TonemappingPipeline {
263263
error!(
264264
"AgX tonemapping requires the `tonemapping_luts` feature.
265265
Either enable the `tonemapping_luts` feature for bevy in `Cargo.toml` (recommended),
266-
or use a different `Tonemapping` method in your `Camera2dBundle`/`Camera3dBundle`."
266+
or use a different `Tonemapping` method for your `Camera2d`/`Camera3d`."
267267
);
268268
shader_defs.push("TONEMAP_METHOD_AGX".into());
269269
}
@@ -275,7 +275,7 @@ impl SpecializedRenderPipeline for TonemappingPipeline {
275275
error!(
276276
"TonyMcMapFace tonemapping requires the `tonemapping_luts` feature.
277277
Either enable the `tonemapping_luts` feature for bevy in `Cargo.toml` (recommended),
278-
or use a different `Tonemapping` method in your `Camera2dBundle`/`Camera3dBundle`."
278+
or use a different `Tonemapping` method for your `Camera2d`/`Camera3d`."
279279
);
280280
shader_defs.push("TONEMAP_METHOD_TONY_MC_MAPFACE".into());
281281
}
@@ -284,7 +284,7 @@ impl SpecializedRenderPipeline for TonemappingPipeline {
284284
error!(
285285
"BlenderFilmic tonemapping requires the `tonemapping_luts` feature.
286286
Either enable the `tonemapping_luts` feature for bevy in `Cargo.toml` (recommended),
287-
or use a different `Tonemapping` method in your `Camera2dBundle`/`Camera3dBundle`."
287+
or use a different `Tonemapping` method for your `Camera2d`/`Camera3d`."
288288
);
289289
shader_defs.push("TONEMAP_METHOD_BLENDER_FILMIC".into());
290290
}

crates/bevy_dev_tools/src/ui_debug_overlay/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::any::{Any, TypeId};
44
use bevy_app::{App, Plugin, PostUpdate};
55
use bevy_color::Hsla;
66
use bevy_core::Name;
7-
use bevy_core_pipeline::core_2d::Camera2dBundle;
7+
use bevy_core_pipeline::core_2d::Camera2d;
88
use bevy_ecs::{prelude::*, system::SystemParam};
99
use bevy_gizmos::{config::GizmoConfigStore, prelude::Gizmos, AppGizmoBuilder};
1010
use bevy_hierarchy::{Children, Parent};
@@ -88,17 +88,15 @@ fn update_debug_camera(
8888
} else {
8989
let spawn_cam = || {
9090
cmds.spawn((
91-
Camera2dBundle {
92-
projection: OrthographicProjection {
93-
far: 1000.0,
94-
viewport_origin: Vec2::new(0.0, 0.0),
95-
..OrthographicProjection::default_3d()
96-
},
97-
camera: Camera {
98-
order: LAYOUT_DEBUG_CAMERA_ORDER,
99-
clear_color: ClearColorConfig::None,
100-
..default()
101-
},
91+
Camera2d,
92+
OrthographicProjection {
93+
far: 1000.0,
94+
viewport_origin: Vec2::new(0.0, 0.0),
95+
..OrthographicProjection::default_3d()
96+
},
97+
Camera {
98+
order: LAYOUT_DEBUG_CAMERA_ORDER,
99+
clear_color: ClearColorConfig::None,
102100
..default()
103101
},
104102
LAYOUT_DEBUG_LAYERS.clone(),

crates/bevy_ecs/src/bundle.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ use core::{any::TypeId, ptr::NonNull};
5555
/// would create incoherent behavior.
5656
/// This would be unexpected if bundles were treated as an abstraction boundary, as
5757
/// the abstraction would be unmaintainable for these cases.
58-
/// For example, both `Camera3dBundle` and `Camera2dBundle` contain the `CameraRenderGraph`
59-
/// component, but specifying different render graphs to use.
60-
/// If the bundles were both added to the same entity, only one of these two bundles would work.
6158
///
6259
/// For this reason, there is intentionally no [`Query`] to match whether an entity
6360
/// contains the components of a bundle.

crates/bevy_gltf/src/loader.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bevy_asset::{
99
};
1010
use bevy_color::{Color, LinearRgba};
1111
use bevy_core::Name;
12-
use bevy_core_pipeline::prelude::Camera3dBundle;
12+
use bevy_core_pipeline::prelude::Camera3d;
1313
use bevy_ecs::{
1414
entity::{Entity, EntityHashMap},
1515
world::World,
@@ -1413,15 +1413,15 @@ fn load_node(
14131413
Projection::Perspective(perspective_projection)
14141414
}
14151415
};
1416-
node.insert(Camera3dBundle {
1416+
node.insert((
1417+
Camera3d::default(),
14171418
projection,
14181419
transform,
1419-
camera: Camera {
1420+
Camera {
14201421
is_active: !*active_camera_found,
14211422
..Default::default()
14221423
},
1423-
..Default::default()
1424-
});
1424+
));
14251425

14261426
*active_camera_found = true;
14271427
}

crates/bevy_pbr/src/fog.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ use bevy_render::{extract_component::ExtractComponent, prelude::Camera};
2929
/// # fn system(mut commands: Commands) {
3030
/// commands.spawn((
3131
/// // Setup your camera as usual
32-
/// Camera3dBundle {
33-
/// // ... camera options
34-
/// # ..Default::default()
35-
/// },
32+
/// Camera3d::default(),
3633
/// // Add fog to the same entity
3734
/// DistanceFog {
3835
/// color: Color::WHITE,

0 commit comments

Comments
 (0)