2022-12-14 22:55:26 +01:00
|
|
|
use bevy::{math::Vec4Swizzles, prelude::*, transform};
|
2022-12-09 21:30:47 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
use crate::{
|
|
|
|
offaxis::OffAxisProjection, projection::create_offaxis_matrices, screeninfo::ScreenInfo,
|
|
|
|
};
|
2022-12-06 15:44:16 +01:00
|
|
|
|
2022-12-11 12:41:34 +01:00
|
|
|
#[derive(Component, Default)]
|
2022-12-06 15:44:16 +01:00
|
|
|
pub struct Viewer {
|
2022-12-06 21:12:15 +01:00
|
|
|
pub position: Vec3,
|
|
|
|
pub orientation: Quat,
|
2022-12-06 15:44:16 +01:00
|
|
|
|
2022-12-06 21:12:15 +01:00
|
|
|
pub alpha: f32,
|
|
|
|
}
|
2022-12-06 15:44:16 +01:00
|
|
|
|
|
|
|
impl Viewer {
|
2022-12-11 12:41:34 +01:00
|
|
|
pub fn new(position: Vec3) -> Self {
|
2022-12-06 21:12:15 +01:00
|
|
|
Self {
|
|
|
|
position,
|
|
|
|
orientation: Quat::IDENTITY,
|
|
|
|
alpha: 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default() -> Self {
|
2022-12-06 15:44:16 +01:00
|
|
|
Self {
|
|
|
|
position: Vec3::ZERO,
|
2022-12-06 21:12:15 +01:00
|
|
|
orientation: Quat::IDENTITY,
|
2022-12-11 12:41:34 +01:00
|
|
|
alpha: 0.0,
|
2022-12-06 15:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-06 21:12:15 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
pub fn apply_viewer_to_projections(
|
|
|
|
mut query: Query<(&Viewer, &ScreenInfo, &mut OffAxisProjection, &mut Transform)>,
|
|
|
|
) {
|
|
|
|
query.for_each_mut(|(viewer, screen_info, mut offaxis, mut transform)| {
|
2022-12-11 21:12:08 +01:00
|
|
|
let eye = viewer.position;
|
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
let (lower_left, lower_right, upper_left, _) = screen_info.corner_points();
|
2022-12-11 21:12:08 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
// let lower_left = Vec3::new(screen_info.center.x - screen_info.width / 2.0,screen_info.center.y - screen_info.height / 2.0,screen_info.center.z);
|
|
|
|
// let upper_left = Vec3::new(screen_info.center.x - screen_info.width / 2.0,screen_info.center.y + screen_info.height / 2.0,screen_info.center.z);
|
2022-12-11 21:12:08 +01:00
|
|
|
|
|
|
|
// let lower_right = Vec3::new(screen_info.center.x + screen_info.width / 2.0,screen_info.center.y - screen_info.height / 2.0,screen_info.center.z);
|
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
let (view, projection) =
|
|
|
|
create_offaxis_matrices(lower_left, lower_right, upper_left, eye, 1000.0f32);
|
2022-12-11 21:12:08 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
offaxis.projection_matrix = projection;
|
2022-12-11 21:12:08 +01:00
|
|
|
|
|
|
|
*transform = Transform::from_matrix(view.inverse());
|
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
info!("Viewer {:?}", viewer.position);
|
2022-12-11 23:41:22 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
// *transform = Transform::from_translation(eye).looking_at(Vec3::ZERO, Vec3::Y);
|
2022-12-11 21:12:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:57:35 +01:00
|
|
|
pub fn simulate_viewer_with_circle(mut query: Query<&mut Viewer>, time: Res<Time>) {
|
2022-12-11 12:41:34 +01:00
|
|
|
for mut v in query.iter_mut() {
|
2022-12-09 21:30:47 +01:00
|
|
|
|
2022-12-17 20:57:35 +01:00
|
|
|
let radius = 1.3;
|
|
|
|
let offset = Vec3::new(0.0,0.0,8f32);
|
|
|
|
let speed = 1.0f32;
|
|
|
|
|
|
|
|
v.alpha += speed * time.delta_seconds();
|
2022-12-11 21:12:08 +01:00
|
|
|
|
2022-12-09 21:30:47 +01:00
|
|
|
|
2022-12-11 12:41:34 +01:00
|
|
|
v.position = Vec3::new(
|
|
|
|
v.alpha.sin() * radius,
|
2022-12-11 21:12:08 +01:00
|
|
|
v.alpha.cos() * radius,
|
2022-12-17 20:57:35 +01:00
|
|
|
0.0,
|
|
|
|
) + offset;
|
2022-12-07 20:41:17 +01:00
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
// info!("Viewer {:?}", v.position);
|
2022-12-06 21:12:15 +01:00
|
|
|
}
|
2022-12-11 12:41:34 +01:00
|
|
|
}
|
2022-12-14 22:55:26 +01:00
|
|
|
|
|
|
|
pub fn simulate_viewer_with_keyboard(
|
|
|
|
mut query: Query<&mut Viewer>,
|
|
|
|
input: Res<Input<KeyCode>>,
|
|
|
|
time: Res<Time>,
|
|
|
|
) {
|
2022-12-19 10:09:15 +01:00
|
|
|
|
|
|
|
let offset = Vec3::new(0.0,0.0,8f32);
|
|
|
|
|
2022-12-14 22:55:26 +01:00
|
|
|
for mut viewer in query.iter_mut() {
|
|
|
|
if input.pressed(KeyCode::W) {
|
|
|
|
viewer.position += Vec3::Z * time.delta_seconds();
|
2022-12-20 17:13:17 +01:00
|
|
|
} else if input.pressed(KeyCode::S) {
|
2022-12-14 22:55:26 +01:00
|
|
|
viewer.position -= Vec3::Z * time.delta_seconds();
|
2022-12-19 10:09:15 +01:00
|
|
|
} else if input.pressed(KeyCode::A) {
|
|
|
|
viewer.position -= Vec3::X * time.delta_seconds();
|
|
|
|
} else if input.pressed(KeyCode::D) {
|
|
|
|
viewer.position += Vec3::X * time.delta_seconds();
|
2022-12-19 22:49:24 +01:00
|
|
|
} else if input.pressed(KeyCode::Up) {
|
|
|
|
viewer.position += Vec3::Y * time.delta_seconds();
|
|
|
|
} else if input.pressed(KeyCode::Down) {
|
|
|
|
viewer.position -= Vec3::Y * time.delta_seconds();
|
2022-12-14 22:55:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|