From 726eb371985eb17e9a159fdfe3f504bcd85808fb Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sun, 19 Jul 2020 17:00:08 -0700 Subject: [PATCH] use rh coordinate system in 2d z = 0 is now "farthest back" and z=1000 "farthest forward" --- crates/bevy_render/src/camera/projection.rs | 2 +- crates/bevy_render/src/entity.rs | 22 ++++++++++++------- crates/bevy_ui/src/focus.rs | 3 +-- crates/bevy_ui/src/update.rs | 6 ++--- examples/2d/sprite.rs | 2 +- examples/2d/sprite_sheet.rs | 2 +- examples/2d/texture_atlas.rs | 2 +- examples/3d/3d_scene.rs | 2 +- examples/3d/load_model.rs | 2 +- examples/3d/parenting.rs | 2 +- examples/3d/spawner.rs | 2 +- examples/3d/texture.rs | 2 +- examples/3d/z_sort_debug.rs | 2 +- .../example_plugin/src/lib.rs | 2 +- examples/asset/asset_loading.rs | 2 +- examples/asset/hot_asset_reloading.rs | 2 +- examples/game/breakout.rs | 2 +- examples/shader/shader_custom_material.rs | 2 +- examples/shader/shader_defs.rs | 2 +- examples/ui/button.rs | 2 +- examples/ui/font_atlas_debug.rs | 2 +- examples/ui/text.rs | 2 +- examples/ui/ui.rs | 2 +- examples/ui/ui_bench.rs | 2 +- examples/window/multiple_windows.rs | 4 ++-- 25 files changed, 41 insertions(+), 36 deletions(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 43053a5d1a..51fdf2bb22 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -55,7 +55,7 @@ pub struct OrthographicProjection { impl CameraProjection for OrthographicProjection { fn get_projection_matrix(&self) -> Mat4 { - Mat4::orthographic_lh( + Mat4::orthographic_rh( self.left, self.right, self.bottom, diff --git a/crates/bevy_render/src/entity.rs b/crates/bevy_render/src/entity.rs index 15eee827fa..88a177a787 100644 --- a/crates/bevy_render/src/entity.rs +++ b/crates/bevy_render/src/entity.rs @@ -20,7 +20,7 @@ pub struct MeshComponents { } #[derive(Bundle)] -pub struct PerspectiveCameraComponents { +pub struct Camera3dComponents { pub camera: Camera, pub perspective_projection: PerspectiveProjection, pub visible_entities: VisibleEntities, @@ -30,9 +30,9 @@ pub struct PerspectiveCameraComponents { pub scale: Scale, } -impl Default for PerspectiveCameraComponents { +impl Default for Camera3dComponents { fn default() -> Self { - PerspectiveCameraComponents { + Camera3dComponents { camera: Camera { name: Some(base::camera::CAMERA3D.to_string()), ..Default::default() @@ -48,7 +48,7 @@ impl Default for PerspectiveCameraComponents { } #[derive(Bundle)] -pub struct OrthographicCameraComponents { +pub struct Camera2dComponents { pub camera: Camera, pub orthographic_projection: OrthographicProjection, pub visible_entities: VisibleEntities, @@ -58,17 +58,23 @@ pub struct OrthographicCameraComponents { pub scale: Scale, } -impl Default for OrthographicCameraComponents { +impl Default for Camera2dComponents { fn default() -> Self { - OrthographicCameraComponents { + // we want 0 to be "closest" and +far to be "farthest" in 2d, so we offset + // the camera's translation by far and use a right handed coordinate system + let far = 1000.0; + Camera2dComponents { camera: Camera { name: Some(base::camera::CAMERA2D.to_string()), ..Default::default() }, - orthographic_projection: Default::default(), + orthographic_projection: OrthographicProjection { + far, + ..Default::default() + }, visible_entities: Default::default(), transform: Default::default(), - translation: Default::default(), + translation: Translation::new(0.0, 0.0, far - 0.1), rotation: Default::default(), scale: Default::default(), } diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index dedc9badba..8b5c2e7ba4 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -106,8 +106,7 @@ pub fn ui_focus_system( }) .collect::>(); - // TODO: sort by negative when we move back to a right handed coordinate system - moused_over_z_sorted_nodes.sort_by_key(|(_, _, _, z)| *z); + moused_over_z_sorted_nodes.sort_by_key(|(_, _, _, z)| -*z); for (focus_policy, click, hover, _) in moused_over_z_sorted_nodes { if mouse_clicked { // only consider nodes with ClickState "clickable" diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index 39cf798d6c..6ef70d1661 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -37,7 +37,7 @@ pub fn ui_update_system( }; let mut previous_sibling_result = Some(Rect { - z: 999.0, + z: 0.0, size: window_size, }); for entity in orphan_nodes { @@ -66,8 +66,8 @@ fn update_node_entity( z = previous_rect.z }; - z -= UI_Z_STEP; - node.update(&mut translation, z - parent_rect.z, parent_rect.size); + z += UI_Z_STEP; + node.update(&mut translation, z + parent_rect.z, parent_rect.size); return Some(Rect { size: node.size, z }); } } diff --git a/examples/2d/sprite.rs b/examples/2d/sprite.rs index ab5fd69b47..d91caefc21 100644 --- a/examples/2d/sprite.rs +++ b/examples/2d/sprite.rs @@ -14,7 +14,7 @@ fn setup( ) { let texture_handle = asset_server.load("assets/branding/icon.png").unwrap(); commands - .spawn(OrthographicCameraComponents::default()) + .spawn(Camera2dComponents::default()) .spawn(SpriteComponents { material: materials.add(texture_handle.into()), ..Default::default() diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index a3786e7c88..5fb37661b6 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -37,7 +37,7 @@ fn setup( let texture_atlas = TextureAtlas::from_grid(texture_handle, texture.size, 7, 1); let texture_atlas_handle = texture_atlases.add(texture_atlas); commands - .spawn(OrthographicCameraComponents::default()) + .spawn(Camera2dComponents::default()) .spawn(SpriteSheetComponents { texture_atlas: texture_atlas_handle, scale: Scale(6.0), diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index 08a813ba93..6436efedd2 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -27,7 +27,7 @@ fn setup( rpg_sprite_handles.handles = asset_server .load_asset_folder("assets/textures/rpg") .unwrap(); - commands.spawn(OrthographicCameraComponents::default()); + commands.spawn(Camera2dComponents::default()); } #[derive(Default)] diff --git a/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index 858c0c223b..292e370138 100644 --- a/examples/3d/3d_scene.rs +++ b/examples/3d/3d_scene.rs @@ -48,7 +48,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, 8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/load_model.rs b/examples/3d/load_model.rs index 008223abf7..fb6a81b298 100644 --- a/examples/3d/load_model.rs +++ b/examples/3d/load_model.rs @@ -37,7 +37,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(-2.0, 2.0, 6.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index 6057ca0570..82126e0036 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -53,7 +53,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(5.0, 10.0, 10.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/spawner.rs b/examples/3d/spawner.rs index a82f8e52a4..a99b6b3da5 100644 --- a/examples/3d/spawner.rs +++ b/examples/3d/spawner.rs @@ -62,7 +62,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/texture.rs b/examples/3d/texture.rs index 20c5fd6374..4305ae0e30 100644 --- a/examples/3d/texture.rs +++ b/examples/3d/texture.rs @@ -91,7 +91,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/3d/z_sort_debug.rs b/examples/3d/z_sort_debug.rs index b14530f81a..a2851977ad 100644 --- a/examples/3d/z_sort_debug.rs +++ b/examples/3d/z_sort_debug.rs @@ -84,7 +84,7 @@ fn setup( }); }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(5.0, 10.0, 10.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs b/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs index 2f0458b959..3e7090735b 100644 --- a/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs +++ b/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs @@ -33,7 +33,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, 8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/asset/asset_loading.rs b/examples/asset/asset_loading.rs index 91fe1823e0..c80a5ef203 100644 --- a/examples/asset/asset_loading.rs +++ b/examples/asset/asset_loading.rs @@ -70,7 +70,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(0.0, 3.0, 10.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/asset/hot_asset_reloading.rs b/examples/asset/hot_asset_reloading.rs index d58e2dc96a..d900787630 100644 --- a/examples/asset/hot_asset_reloading.rs +++ b/examples/asset/hot_asset_reloading.rs @@ -45,7 +45,7 @@ fn setup( ..Default::default() }) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(2.0, 2.0, 6.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/game/breakout.rs b/examples/game/breakout.rs index 803e235eaf..222bef5250 100644 --- a/examples/game/breakout.rs +++ b/examples/game/breakout.rs @@ -40,7 +40,7 @@ fn setup( // Add the game's entities to our world commands // camera - .spawn(OrthographicCameraComponents::default()) + .spawn(Camera2dComponents::default()) // paddle .spawn(SpriteComponents { material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()), diff --git a/examples/shader/shader_custom_material.rs b/examples/shader/shader_custom_material.rs index 5f23faf4d4..acb48d02b6 100644 --- a/examples/shader/shader_custom_material.rs +++ b/examples/shader/shader_custom_material.rs @@ -109,7 +109,7 @@ fn setup( }) .with(material) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/shader/shader_defs.rs b/examples/shader/shader_defs.rs index f93cf5e813..c76e3ebda6 100644 --- a/examples/shader/shader_defs.rs +++ b/examples/shader/shader_defs.rs @@ -152,7 +152,7 @@ fn setup( }) .with(blue_material) // camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(3.0, 5.0, -8.0), Vec3::new(0.0, 0.0, 0.0), diff --git a/examples/ui/button.rs b/examples/ui/button.rs index 6d837d6251..96d8027788 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -92,7 +92,7 @@ fn setup( ) { commands // ui camera - .spawn(OrthographicCameraComponents::default()) + .spawn(Camera2dComponents::default()) .spawn(ButtonComponents { node: Node::new(Anchors::CENTER, Margins::new(-75.0, 75.0, -35.0, 35.0)), material: button_materials.normal, diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index b7fbddd4fe..367429d115 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -65,7 +65,7 @@ fn setup(mut commands: Commands, asset_server: Res, mut state: ResM state.handle = font_handle; commands // 2d camera - .spawn(OrthographicCameraComponents::default()) + .spawn(Camera2dComponents::default()) // texture .spawn(LabelComponents { node: Node::new(Anchors::TOP_LEFT, Margins::new(0.0, 250.0, 0.0, 60.0)), diff --git a/examples/ui/text.rs b/examples/ui/text.rs index 9f2f3e9fb5..5fcec3887c 100644 --- a/examples/ui/text.rs +++ b/examples/ui/text.rs @@ -26,7 +26,7 @@ fn setup(mut commands: Commands, asset_server: Res) { let font_handle = asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap(); commands // 2d camera - .spawn(OrthographicCameraComponents::default()) + .spawn(Camera2dComponents::default()) // texture .spawn(LabelComponents { node: Node::new(Anchors::TOP_LEFT, Margins::new(0.0, 250.0, 0.0, 60.0)), diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 6930afdc0c..fe5231b1cb 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -23,7 +23,7 @@ fn setup( commands // ui camera - .spawn(OrthographicCameraComponents::default()) + .spawn(Camera2dComponents::default()) // root node .spawn(NodeComponents { node: Node::new(Anchors::FULL, Margins::default()), diff --git a/examples/ui/ui_bench.rs b/examples/ui/ui_bench.rs index 560c94dde1..e65c3c2dee 100644 --- a/examples/ui/ui_bench.rs +++ b/examples/ui/ui_bench.rs @@ -22,7 +22,7 @@ fn placement_system( } fn setup(mut commands: Commands, mut materials: ResMut>) { - commands.spawn(OrthographicCameraComponents::default()); + commands.spawn(Camera2dComponents::default()); let mut prev = Vec2::default(); let count = 1000; diff --git a/examples/window/multiple_windows.rs b/examples/window/multiple_windows.rs index d45185fafb..a6d75b9925 100644 --- a/examples/window/multiple_windows.rs +++ b/examples/window/multiple_windows.rs @@ -136,7 +136,7 @@ fn setup( ..Default::default() }) // main camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( Vec3::new(0.0, 0.0, 6.0), Vec3::new(0.0, 0.0, 0.0), @@ -145,7 +145,7 @@ fn setup( ..Default::default() }) // second window camera - .spawn(PerspectiveCameraComponents { + .spawn(Camera3dComponents { camera: Camera { name: Some("Secondary".to_string()), window: WindowReference::Id(window_id),