use crate::core::Object3D;
use crate::math::{Box3, CoordinateSystem, Matrix4, Vector3};
use super::CameraView;
#[derive(Clone)]
pub struct OrthographicCamera {
pub object: Object3D,
pub left: f64,
pub right: f64,
pub top: f64,
pub bottom: f64,
pub near: f64,
pub far: f64,
pub zoom: f64,
pub view: Option<CameraView>,
pub coordinate_system: CoordinateSystem,
pub projection_matrix: Matrix4,
pub projection_matrix_inverse: Matrix4,
pub matrix_world_inverse: Matrix4,
}
impl OrthographicCamera {
pub fn new(left: f64, right: f64, top: f64, bottom: f64, near: f64, far: f64) -> Self {
let mut camera = Self {
object: Object3D::default(),
left,
right,
top,
bottom,
near,
far,
zoom: 1.0,
view: None,
coordinate_system: CoordinateSystem::WebGpu,
projection_matrix: Matrix4::identity(),
projection_matrix_inverse: Matrix4::identity(),
matrix_world_inverse: Matrix4::identity(),
};
camera.update_projection_matrix();
camera.update_matrix_world();
camera
}
pub fn set_view_offset(
&mut self,
full_width: f64,
full_height: f64,
x: f64,
y: f64,
width: f64,
height: f64,
) {
self.view = Some(CameraView {
enabled: true,
full_width,
full_height,
offset_x: x,
offset_y: y,
width,
height,
});
self.update_projection_matrix();
}
pub fn clear_view_offset(&mut self) {
if let Some(view) = self.view.as_mut() {
view.enabled = false;
}
self.update_projection_matrix();
}
pub fn update_projection_matrix(&mut self) {
let dx = (self.right - self.left) / (2.0 * self.zoom);
let dy = (self.top - self.bottom) / (2.0 * self.zoom);
let cx = (self.right + self.left) / 2.0;
let cy = (self.top + self.bottom) / 2.0;
let mut left = cx - dx;
let mut right = cx + dx;
let mut top = cy + dy;
let mut bottom = cy - dy;
if let Some(view) = self.view.filter(|view| view.enabled) {
let scale_w = (self.right - self.left) / view.full_width / self.zoom;
let scale_h = (self.top - self.bottom) / view.full_height / self.zoom;
left += scale_w * view.offset_x;
right = left + scale_w * view.width;
top -= scale_h * view.offset_y;
bottom = top - scale_h * view.height;
}
self.projection_matrix.make_orthographic(
left,
right,
top,
bottom,
self.near,
self.far,
self.coordinate_system,
);
self.projection_matrix_inverse = self.projection_matrix;
self.projection_matrix_inverse.invert();
}
pub fn fit(&mut self, box_: &Box3, margin: f64) {
if box_.is_empty() {
return;
}
self.update_matrix_world();
let (right, up, forward) = super::view_basis(&self.object.matrix_world);
let offsets = super::corner_offsets(box_, &right, &up, &forward);
let mut half_width = 0.0_f64;
let mut half_height = 0.0_f64;
let mut near_depth = f64::INFINITY;
let mut far_depth = f64::NEG_INFINITY;
for offset in &offsets {
half_width = half_width.max(offset.x.abs());
half_height = half_height.max(offset.y.abs());
near_depth = near_depth.min(offset.z);
far_depth = far_depth.max(offset.z);
}
let slack = (1.0 - margin).clamp(f64::EPSILON, 1.0);
half_width /= slack;
half_height /= slack;
let aspect = if self.top != self.bottom {
(self.right - self.left) / (self.top - self.bottom)
} else {
1.0
};
if half_width < half_height * aspect {
half_width = half_height * aspect;
} else if aspect != 0.0 {
half_height = half_width / aspect;
}
self.left = -half_width * self.zoom;
self.right = half_width * self.zoom;
self.top = half_height * self.zoom;
self.bottom = -half_height * self.zoom;
let clearance = {
let diagonal = box_.get_size().length();
if diagonal > 0.0 {
diagonal
} else {
1.0
}
};
let distance = clearance - near_depth;
self.near = clearance;
self.far = distance + far_depth;
if self.far <= self.near {
self.far = self.near * 2.0;
}
self.update_projection_matrix();
let center = box_.get_center();
let mut position = Vector3::ZERO;
position
.copy(&forward)
.multiply_scalar(-distance)
.add(¢er);
self.object.position = position;
self.update_matrix_world();
}
pub fn look_at(&mut self, target: &Vector3) {
let mut m = Matrix4::identity();
m.look_at(&self.object.position, target, &self.object.up);
self.object.quaternion.set_from_rotation_matrix(&m);
self.object.sync_rotation_from_quaternion();
}
pub fn update_matrix_world(&mut self) {
self.object.update_matrix_world(None);
self.matrix_world_inverse = self.object.matrix_world;
self.matrix_world_inverse.invert();
}
}