PixSpace/src/viewer.rs

79 lines
2.2 KiB
Rust
Raw Normal View History

use bevy::{prelude::*, transform, math::Vec4Swizzles};
use crate::{screeninfo::ScreenInfo, offaxis::OffAxisProjection, projection::create_offaxis_matrices};
2022-12-06 15:44:16 +01:00
#[derive(Component, Default)]
2022-12-06 15:44:16 +01:00
pub struct Viewer {
pub position: Vec3,
pub orientation: Quat,
2022-12-06 15:44:16 +01:00
pub alpha: f32,
}
2022-12-06 15:44:16 +01:00
impl Viewer {
pub fn new(position: Vec3) -> Self {
Self {
position,
orientation: Quat::IDENTITY,
alpha: 0.0,
}
}
pub fn default() -> Self {
2022-12-06 15:44:16 +01:00
Self {
position: Vec3::ZERO,
orientation: Quat::IDENTITY,
alpha: 0.0,
2022-12-06 15:44:16 +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)| {
let eye = viewer.position;
let (lower_left,lower_right,upper_left,_) = screen_info.corner_points();
// 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);
// 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);
let (view, projection) = create_offaxis_matrices(lower_left, lower_right, upper_left, eye, 1000.0f32);
offaxis.projection_matrix = projection;
*transform = Transform::from_matrix(view.inverse());
2022-12-11 23:41:22 +01:00
// *transform = Transform::from_translation(eye).looking_at(Vec3::ZERO, Vec3::Y);
});
}
2022-12-13 23:18:18 +01:00
pub fn simulate_viewer(mut query: Query<&mut Viewer>,time: Res<Time>) {
for mut v in query.iter_mut() {
//v.position += Vec3::Y * 0.005;
v.alpha += 1.5 * time.delta_seconds();
let radius = 1.0;
2022-12-11 23:41:22 +01:00
let z_distance = 3.0_f32;
v.position = Vec3::new(
v.alpha.sin() * radius,
v.alpha.cos() * radius,
2022-12-11 23:41:22 +01:00
z_distance + v.alpha.sin(),
);
info!("Viewer {:?}",v.position);
}
}