use rh coordinate system in 2d

z = 0 is now "farthest back" and z=1000 "farthest forward"
This commit is contained in:
Carter Anderson 2020-07-19 17:00:08 -07:00
parent cadbb4c1b0
commit 726eb37198
25 changed files with 41 additions and 36 deletions

View file

@ -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,

View file

@ -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(),
}

View file

@ -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"

View file

@ -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 });
}
}

View file

@ -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()

View file

@ -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),

View file

@ -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)]

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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),

View file

@ -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()),

View file

@ -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),

View file

@ -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),

View file

@ -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,

View file

@ -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)),

View file

@ -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)),

View file

@ -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()),

View file

@ -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;

View file

@ -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),