From dcc4e15d621d19338be9aa602bf5739e1447299c Mon Sep 17 00:00:00 2001 From: "David M. Lary" Date: Sat, 23 Sep 2023 13:14:24 -0500 Subject: [PATCH 1/7] Split OrthographicProjection::default into 2d & 3d 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 was discussion about other options in the discord [0], but this seemed like the lowest cost approach. This commit splits `OrthographicProjection::default` into `default_2d` and `default_3d`. [0]: https://discord.com/channels/691052431525675048/1154114310042292325 --- .../src/core_2d/camera_2d.rs | 8 ++----- crates/bevy_gltf/src/loader.rs | 2 +- crates/bevy_render/src/camera/projection.rs | 23 ++++++++++++++++--- examples/3d/orthographic.rs | 2 +- examples/3d/pbr.rs | 2 +- examples/stress_tests/many_lights.rs | 2 +- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs b/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs index 857c2202164ab..f252f187d2e98 100644 --- a/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs +++ b/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs @@ -41,11 +41,7 @@ pub struct Camera2dBundle { impl Default for Camera2dBundle { fn default() -> Self { - let projection = OrthographicProjection { - far: 1000., - near: -1000., - ..Default::default() - }; + let projection = OrthographicProjection::default_2d(); let transform = Transform::default(); let frustum = projection.compute_frustum(&GlobalTransform::from(transform)); Self { @@ -77,7 +73,7 @@ impl Camera2dBundle { // the camera's translation by far and use a right handed coordinate system let projection = OrthographicProjection { far, - ..Default::default() + ..OrthographicProjection::default_2d() }; let transform = Transform::from_xyz(0.0, 0.0, far - 0.1); let frustum = projection.compute_frustum(&GlobalTransform::from(transform)); diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 010e80f659c23..e2c193e110284 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -1256,7 +1256,7 @@ fn load_node( far: orthographic.zfar(), scaling_mode: ScalingMode::FixedHorizontal(1.0), scale: xmag, - ..Default::default() + ..OrthographicProjection::default_3d() }; Projection::Orthographic(orthographic_projection) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 0a6c3ca00afab..249d524b903a8 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -338,7 +338,7 @@ impl DivAssign for ScalingMode { /// }); /// ``` #[derive(Component, Debug, Clone, Reflect)] -#[reflect(Component, Default)] +#[reflect(Component)] pub struct OrthographicProjection { /// The distance of the near clipping plane in world units. /// @@ -479,8 +479,25 @@ impl CameraProjection for OrthographicProjection { } } -impl Default for OrthographicProjection { - fn default() -> Self { +impl FromWorld for OrthographicProjection { + fn from_world(_world: &mut World) -> Self { + OrthographicProjection::default_3d() + } +} + +impl OrthographicProjection { + pub fn default_2d() -> Self { + OrthographicProjection { + scale: 1.0, + near: -1000.0, + far: 1000.0, + viewport_origin: Vec2::new(0.5, 0.5), + scaling_mode: ScalingMode::WindowSize(1.0), + area: Rect::new(-1.0, -1.0, 1.0, 1.0), + } + } + + pub fn default_3d() -> Self { OrthographicProjection { scale: 1.0, near: 0.0, diff --git a/examples/3d/orthographic.rs b/examples/3d/orthographic.rs index a56e4f972fefe..f257ab35f8e67 100644 --- a/examples/3d/orthographic.rs +++ b/examples/3d/orthographic.rs @@ -20,7 +20,7 @@ fn setup( projection: OrthographicProjection { // 6 world units per window height. scaling_mode: ScalingMode::FixedVertical(6.0), - ..default() + ..OrthographicProjection::default_3d() } .into(), transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), diff --git a/examples/3d/pbr.rs b/examples/3d/pbr.rs index 3c7a76211903f..a20adb8edf409 100644 --- a/examples/3d/pbr.rs +++ b/examples/3d/pbr.rs @@ -121,7 +121,7 @@ fn setup( transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::default(), Vec3::Y), projection: OrthographicProjection { scale: 0.01, - ..default() + ..OrthographicProjection::default_3d() } .into(), ..default() diff --git a/examples/stress_tests/many_lights.rs b/examples/stress_tests/many_lights.rs index 196bea143d8b0..33244a99b9d4a 100644 --- a/examples/stress_tests/many_lights.rs +++ b/examples/stress_tests/many_lights.rs @@ -96,7 +96,7 @@ fn setup( projection: OrthographicProjection { scale: 20.0, scaling_mode: ScalingMode::FixedHorizontal(1.0), - ..default() + ..OrthographicProjection::default_3d() } .into(), ..default() From 882337248d8e31c6610f9ccb6eacbdfefc9f7ec0 Mon Sep 17 00:00:00 2001 From: Azorlogh Date: Sat, 7 Sep 2024 02:07:04 +0200 Subject: [PATCH 2/7] fixes --- crates/bevy_dev_tools/src/ui_debug_overlay/mod.rs | 2 +- crates/bevy_render/src/camera/projection.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_dev_tools/src/ui_debug_overlay/mod.rs b/crates/bevy_dev_tools/src/ui_debug_overlay/mod.rs index 6382cfa9038fb..88af56210a9e3 100644 --- a/crates/bevy_dev_tools/src/ui_debug_overlay/mod.rs +++ b/crates/bevy_dev_tools/src/ui_debug_overlay/mod.rs @@ -92,7 +92,7 @@ fn update_debug_camera( projection: OrthographicProjection { far: 1000.0, viewport_origin: Vec2::new(0.0, 0.0), - ..default() + ..OrthographicProjection::default_3d() }, camera: Camera { order: LAYOUT_DEBUG_CAMERA_ORDER, diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 249d524b903a8..0cb8b549623ca 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -237,7 +237,7 @@ impl Default for PerspectiveProjection { /// # use bevy_render::camera::{OrthographicProjection, Projection, ScalingMode}; /// let projection = Projection::Orthographic(OrthographicProjection { /// scaling_mode: ScalingMode::FixedVertical(2.0), -/// ..OrthographicProjection::default() +/// ..OrthographicProjection::default_2d() /// }); /// ``` #[derive(Debug, Clone, Copy, Reflect, Serialize, Deserialize)] @@ -334,7 +334,7 @@ impl DivAssign for ScalingMode { /// # use bevy_render::camera::{OrthographicProjection, Projection, ScalingMode}; /// let projection = Projection::Orthographic(OrthographicProjection { /// scaling_mode: ScalingMode::WindowSize(100.0), -/// ..OrthographicProjection::default() +/// ..OrthographicProjection::default_2d() /// }); /// ``` #[derive(Component, Debug, Clone, Reflect)] From 8d8ead798c6efecbaf623fa0f17427d5bd9ed8cb Mon Sep 17 00:00:00 2001 From: Alix Bott Date: Sat, 7 Sep 2024 15:17:20 +0200 Subject: [PATCH 3/7] Reduce code duplication in `OrthographicProjection::default_2d` Co-authored-by: Jan Hohenheim --- crates/bevy_render/src/camera/projection.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 0cb8b549623ca..c91110f0e0a10 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -488,12 +488,8 @@ impl FromWorld for OrthographicProjection { impl OrthographicProjection { pub fn default_2d() -> Self { OrthographicProjection { - scale: 1.0, near: -1000.0, - far: 1000.0, - viewport_origin: Vec2::new(0.5, 0.5), - scaling_mode: ScalingMode::WindowSize(1.0), - area: Rect::new(-1.0, -1.0, 1.0, 1.0), + ..OrthographicProjection::default_3d() } } From e596b9f75fec308a2c4cffc531694182f6a6beef Mon Sep 17 00:00:00 2001 From: Azorlogh Date: Sat, 7 Sep 2024 15:29:23 +0200 Subject: [PATCH 4/7] add docs to default_2d and default_3d, remove outdated doc --- crates/bevy_core_pipeline/src/core_2d/camera_2d.rs | 4 ---- crates/bevy_render/src/camera/projection.rs | 8 ++++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs b/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs index f252f187d2e98..b45f668c9943f 100644 --- a/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs +++ b/crates/bevy_core_pipeline/src/core_2d/camera_2d.rs @@ -23,10 +23,6 @@ pub struct Camera2d; pub struct Camera2dBundle { pub camera: Camera, pub camera_render_graph: CameraRenderGraph, - /// Note: default value for `OrthographicProjection.near` is `0.0` - /// which makes objects on the screen plane invisible to 2D camera. - /// `Camera2dBundle::default()` sets `near` to negative value, - /// so be careful when initializing this field manually. pub projection: OrthographicProjection, pub visible_entities: VisibleEntities, pub frustum: Frustum, diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index c91110f0e0a10..a8c155b38447d 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -486,6 +486,10 @@ impl FromWorld for OrthographicProjection { } impl OrthographicProjection { + /// Returns the default orthographic projection in a 2d context. + /// + /// The near plane is set to a negative value so that the camera can still + /// render the scene when using positive z coordinates for foreground elements. pub fn default_2d() -> Self { OrthographicProjection { near: -1000.0, @@ -493,6 +497,10 @@ impl OrthographicProjection { } } + /// Returns the default orthographic projection in a 3d context. + /// + /// The near plane is set to 0.0 so that the camera doesn't render + /// objects that are behind it. pub fn default_3d() -> Self { OrthographicProjection { scale: 1.0, From e265fe44b61fb151c01c6d079e6342c028cd0376 Mon Sep 17 00:00:00 2001 From: Alix Bott Date: Sat, 7 Sep 2024 20:42:38 +0200 Subject: [PATCH 5/7] Update crates/bevy_render/src/camera/projection.rs Co-authored-by: Jan Hohenheim --- crates/bevy_render/src/camera/projection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index a8c155b38447d..274530a4cf3c3 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -486,7 +486,7 @@ impl FromWorld for OrthographicProjection { } impl OrthographicProjection { - /// Returns the default orthographic projection in a 2d context. + /// Returns the default orthographic projection for a 2D context. /// /// The near plane is set to a negative value so that the camera can still /// render the scene when using positive z coordinates for foreground elements. From 855f9b95154f8be00573b6cbdc48f26fa0ba5568 Mon Sep 17 00:00:00 2001 From: Alix Bott Date: Sat, 7 Sep 2024 20:42:51 +0200 Subject: [PATCH 6/7] Update crates/bevy_render/src/camera/projection.rs Co-authored-by: Jan Hohenheim --- crates/bevy_render/src/camera/projection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 274530a4cf3c3..16310ec382f83 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -489,7 +489,7 @@ impl OrthographicProjection { /// Returns the default orthographic projection for a 2D context. /// /// The near plane is set to a negative value so that the camera can still - /// render the scene when using positive z coordinates for foreground elements. + /// render the scene when using positive z coordinates to order foreground elements. pub fn default_2d() -> Self { OrthographicProjection { near: -1000.0, From c946c95f4249d1c46e04265636a369486cd5a28d Mon Sep 17 00:00:00 2001 From: Alix Bott Date: Sat, 7 Sep 2024 20:43:02 +0200 Subject: [PATCH 7/7] Update crates/bevy_render/src/camera/projection.rs Co-authored-by: Jan Hohenheim --- crates/bevy_render/src/camera/projection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 16310ec382f83..2aa5f8acc0d07 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -497,7 +497,7 @@ impl OrthographicProjection { } } - /// Returns the default orthographic projection in a 3d context. + /// Returns the default orthographic projection for a 3D context. /// /// The near plane is set to 0.0 so that the camera doesn't render /// objects that are behind it.