use bevy_ecs::prelude::*;
use bevy_reflect::prelude::*;
use bevy_render::prelude::*;
use bevy_transform::prelude::*;
use crate::prelude::*;
use self::motion::CurrentMotion;
#[derive(Debug, Clone, Reflect)]
pub struct PerspectiveSettings {
pub near_clip_limits: std::ops::Range<f32>,
pub near_clip_multiplier: f32,
}
impl Default for PerspectiveSettings {
fn default() -> Self {
Self {
near_clip_limits: f32::MIN..0.1,
near_clip_multiplier: 0.05,
}
}
}
pub fn update_perspective(mut cameras: Query<(&EditorCam, &mut Projection)>) {
for (editor_cam, mut projection) in cameras.iter_mut() {
let Projection::Perspective(ref mut perspective) = *projection else {
continue;
};
let limits = editor_cam.perspective.near_clip_limits.clone();
let multiplier = editor_cam.perspective.near_clip_multiplier;
perspective.near = (editor_cam.last_anchor_depth.abs() as f32 * multiplier)
.clamp(limits.start, limits.end);
}
}
#[derive(Debug, Clone, Reflect)]
pub struct OrthographicSettings {
pub scale_to_near_clip: f32,
pub near_clip_limits: std::ops::Range<f32>,
pub far_clip_multiplier: f32,
}
impl Default for OrthographicSettings {
fn default() -> Self {
Self {
scale_to_near_clip: 1_000_000.0,
near_clip_limits: 1.0..1_000_000.0,
far_clip_multiplier: 1.0,
}
}
}
pub fn update_orthographic(mut cameras: Query<(&mut EditorCam, &mut Projection, &mut Transform)>) {
for (mut editor_cam, mut projection, mut cam_transform) in cameras.iter_mut() {
let Projection::Orthographic(ref mut orthographic) = *projection else {
continue;
};
let anchor_dist = editor_cam.last_anchor_depth().abs() as f32;
let target_dist = (editor_cam.orthographic.scale_to_near_clip * orthographic.scale).clamp(
editor_cam.orthographic.near_clip_limits.start,
editor_cam.orthographic.near_clip_limits.end,
);
let forward_amount = anchor_dist - target_dist;
let movement = cam_transform.forward() * forward_amount;
cam_transform.translation += movement;
editor_cam.last_anchor_depth += forward_amount as f64;
if let CurrentMotion::UserControlled { ref mut anchor, .. } = editor_cam.current_motion {
anchor.z += forward_amount as f64;
}
orthographic.near = 0.0;
orthographic.far = anchor_dist * (1.0 + editor_cam.orthographic.far_clip_multiplier);
}
}