Skip to content

Commit 82e416d

Browse files
Azorloghdmlaryjanhohenheim
authored
Split OrthographicProjection::default into 2d & 3d (Adopted) (#15073)
Adopted PR from dmlary, all credit to them! #9915 Original description: # Objective The default value for `near` in `OrthographicProjection` should be different for 2d & 3d. For 2d using `near = -1000` allows bevy users to build up scenes using background `z = 0`, and foreground elements `z > 0` similar to css. However in 3d `near = -1000` results in objects behind the camera being rendered. Using `near = 0` works for 3d, but forces 2d users to assign `z <= 0` for rendered elements, putting the background at some arbitrary negative value. There is no common value for `near` that doesn't result in a footgun or usability issue for either 2d or 3d, so they should have separate values. There was discussion about other options in the discord [0](https://discord.com/channels/691052431525675048/1154114310042292325), but splitting `default()` into `default_2d()` and `default_3d()` seemed like the lowest cost approach. Related/past work #9138, #9214, #9310, #9537 (thanks to @Selene-Amanita for the list) ## Solution This commit splits `OrthographicProjection::default` into `default_2d` and `default_3d`. ## Migration Guide - In initialization of `OrthographicProjection`, change `..default()` to `..OrthographicProjection::default_2d()` or `..OrthographicProjection::default_3d()` Example: ```diff --- a/examples/3d/orthographic.rs +++ b/examples/3d/orthographic.rs @@ -20,7 +20,7 @@ fn setup( projection: OrthographicProjection { scale: 3.0, scaling_mode: ScalingMode::FixedVertical(2.0), - ..default() + ..OrthographicProjection::default_3d() } .into(), transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), ``` --------- Co-authored-by: David M. Lary <dmlary@gmail.com> Co-authored-by: Jan Hohenheim <jan@hohenheim.ch>
1 parent 8460cfa commit 82e416d

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

crates/bevy_core_pipeline/src/core_2d/camera_2d.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ pub struct Camera2d;
2323
pub struct Camera2dBundle {
2424
pub camera: Camera,
2525
pub camera_render_graph: CameraRenderGraph,
26-
/// Note: default value for `OrthographicProjection.near` is `0.0`
27-
/// which makes objects on the screen plane invisible to 2D camera.
28-
/// `Camera2dBundle::default()` sets `near` to negative value,
29-
/// so be careful when initializing this field manually.
3026
pub projection: OrthographicProjection,
3127
pub visible_entities: VisibleEntities,
3228
pub frustum: Frustum,
@@ -41,11 +37,7 @@ pub struct Camera2dBundle {
4137

4238
impl Default for Camera2dBundle {
4339
fn default() -> Self {
44-
let projection = OrthographicProjection {
45-
far: 1000.,
46-
near: -1000.,
47-
..Default::default()
48-
};
40+
let projection = OrthographicProjection::default_2d();
4941
let transform = Transform::default();
5042
let frustum = projection.compute_frustum(&GlobalTransform::from(transform));
5143
Self {
@@ -77,7 +69,7 @@ impl Camera2dBundle {
7769
// the camera's translation by far and use a right handed coordinate system
7870
let projection = OrthographicProjection {
7971
far,
80-
..Default::default()
72+
..OrthographicProjection::default_2d()
8173
};
8274
let transform = Transform::from_xyz(0.0, 0.0, far - 0.1);
8375
let frustum = projection.compute_frustum(&GlobalTransform::from(transform));

crates/bevy_dev_tools/src/ui_debug_overlay/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn update_debug_camera(
9292
projection: OrthographicProjection {
9393
far: 1000.0,
9494
viewport_origin: Vec2::new(0.0, 0.0),
95-
..default()
95+
..OrthographicProjection::default_3d()
9696
},
9797
camera: Camera {
9898
order: LAYOUT_DEBUG_CAMERA_ORDER,

crates/bevy_gltf/src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ fn load_node(
12561256
far: orthographic.zfar(),
12571257
scaling_mode: ScalingMode::FixedHorizontal(1.0),
12581258
scale: xmag,
1259-
..Default::default()
1259+
..OrthographicProjection::default_3d()
12601260
};
12611261

12621262
Projection::Orthographic(orthographic_projection)

crates/bevy_render/src/camera/projection.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl Default for PerspectiveProjection {
237237
/// # use bevy_render::camera::{OrthographicProjection, Projection, ScalingMode};
238238
/// let projection = Projection::Orthographic(OrthographicProjection {
239239
/// scaling_mode: ScalingMode::FixedVertical(2.0),
240-
/// ..OrthographicProjection::default()
240+
/// ..OrthographicProjection::default_2d()
241241
/// });
242242
/// ```
243243
#[derive(Debug, Clone, Copy, Reflect, Serialize, Deserialize)]
@@ -334,11 +334,11 @@ impl DivAssign<f32> for ScalingMode {
334334
/// # use bevy_render::camera::{OrthographicProjection, Projection, ScalingMode};
335335
/// let projection = Projection::Orthographic(OrthographicProjection {
336336
/// scaling_mode: ScalingMode::WindowSize(100.0),
337-
/// ..OrthographicProjection::default()
337+
/// ..OrthographicProjection::default_2d()
338338
/// });
339339
/// ```
340340
#[derive(Component, Debug, Clone, Reflect)]
341-
#[reflect(Component, Default)]
341+
#[reflect(Component)]
342342
pub struct OrthographicProjection {
343343
/// The distance of the near clipping plane in world units.
344344
///
@@ -479,8 +479,29 @@ impl CameraProjection for OrthographicProjection {
479479
}
480480
}
481481

482-
impl Default for OrthographicProjection {
483-
fn default() -> Self {
482+
impl FromWorld for OrthographicProjection {
483+
fn from_world(_world: &mut World) -> Self {
484+
OrthographicProjection::default_3d()
485+
}
486+
}
487+
488+
impl OrthographicProjection {
489+
/// Returns the default orthographic projection for a 2D context.
490+
///
491+
/// The near plane is set to a negative value so that the camera can still
492+
/// render the scene when using positive z coordinates to order foreground elements.
493+
pub fn default_2d() -> Self {
494+
OrthographicProjection {
495+
near: -1000.0,
496+
..OrthographicProjection::default_3d()
497+
}
498+
}
499+
500+
/// Returns the default orthographic projection for a 3D context.
501+
///
502+
/// The near plane is set to 0.0 so that the camera doesn't render
503+
/// objects that are behind it.
504+
pub fn default_3d() -> Self {
484505
OrthographicProjection {
485506
scale: 1.0,
486507
near: 0.0,

examples/3d/orthographic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn setup(
2020
projection: OrthographicProjection {
2121
// 6 world units per window height.
2222
scaling_mode: ScalingMode::FixedVertical(6.0),
23-
..default()
23+
..OrthographicProjection::default_3d()
2424
}
2525
.into(),
2626
transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),

examples/3d/pbr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn setup(
121121
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::default(), Vec3::Y),
122122
projection: OrthographicProjection {
123123
scale: 0.01,
124-
..default()
124+
..OrthographicProjection::default_3d()
125125
}
126126
.into(),
127127
..default()

examples/stress_tests/many_lights.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn setup(
9696
projection: OrthographicProjection {
9797
scale: 20.0,
9898
scaling_mode: ScalingMode::FixedHorizontal(1.0),
99-
..default()
99+
..OrthographicProjection::default_3d()
100100
}
101101
.into(),
102102
..default()

0 commit comments

Comments
 (0)