Skip to content

Commit

Permalink
use rh coordinate system in 2d
Browse files Browse the repository at this point in the history
z = 0 is now "farthest back" and z=1000 "farthest forward"
  • Loading branch information
cart committed Jul 20, 2020
1 parent cadbb4c commit 726eb37
Show file tree
Hide file tree
Showing 25 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 14 additions & 8 deletions crates/bevy_render/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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(),
}
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ pub fn ui_focus_system(
})
.collect::<Vec<_>>();

// 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"
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ui/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 });
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/load_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/parenting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/z_sort_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/asset/asset_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/asset/hot_asset_reloading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/shader_custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/shader_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, 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)),
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
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)),
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/ui_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn placement_system(
}

fn setup(mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
commands.spawn(OrthographicCameraComponents::default());
commands.spawn(Camera2dComponents::default());

let mut prev = Vec2::default();
let count = 1000;
Expand Down
4 changes: 2 additions & 2 deletions examples/window/multiple_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down

0 comments on commit 726eb37

Please sign in to comment.