mod array_camera;
mod cube_camera;
mod orthographic_camera;
mod perspective_camera;
mod stereo_camera;
pub use array_camera::ArrayCamera;
pub use cube_camera::CubeCamera;
pub use orthographic_camera::OrthographicCamera;
pub use perspective_camera::{CameraView, PerspectiveCamera};
pub use stereo_camera::StereoCamera;
use crate::core::Layers;
use crate::math::{Box3, CoordinateSystem, Matrix4, Vector3};
mod sealed {
pub trait Sealed {}
impl Sealed for super::ArrayCamera {}
impl Sealed for super::OrthographicCamera {}
impl Sealed for super::PerspectiveCamera {}
}
#[doc(hidden)]
pub enum CameraMut<'a> {
Borrowed(&'a mut PerspectiveCamera),
Shared(std::cell::RefMut<'a, PerspectiveCamera>),
}
impl std::ops::Deref for CameraMut<'_> {
type Target = PerspectiveCamera;
fn deref(&self) -> &PerspectiveCamera {
match self {
Self::Borrowed(camera) => camera,
Self::Shared(camera) => camera,
}
}
}
impl std::ops::DerefMut for CameraMut<'_> {
fn deref_mut(&mut self) -> &mut PerspectiveCamera {
match self {
Self::Borrowed(camera) => camera,
Self::Shared(camera) => camera,
}
}
}
impl<'a> From<&'a mut PerspectiveCamera> for CameraMut<'a> {
fn from(camera: &'a mut PerspectiveCamera) -> Self {
Self::Borrowed(camera)
}
}
impl<'a> From<std::cell::RefMut<'a, PerspectiveCamera>> for CameraMut<'a> {
fn from(camera: std::cell::RefMut<'a, PerspectiveCamera>) -> Self {
Self::Shared(camera)
}
}
pub trait RenderCamera: sealed::Sealed {
fn update_matrix_world(&mut self);
fn projection_matrix(&self) -> Matrix4;
fn projection_matrix_inverse(&self) -> Matrix4;
fn matrix_world_inverse(&self) -> Matrix4;
fn coordinate_system(&self) -> CoordinateSystem;
fn matrix_world(&self) -> Matrix4;
fn layers(&self) -> Layers;
fn id(&self) -> u32;
fn far(&self) -> f64;
fn near(&self) -> f64;
fn set_view_offset(
&mut self,
full_width: f64,
full_height: f64,
x: f64,
y: f64,
width: f64,
height: f64,
);
fn clear_view_offset(&mut self);
fn is_perspective_camera(&self) -> bool {
false
}
fn is_orthographic_camera(&self) -> bool {
false
}
fn sub_cameras(&self) -> &[PerspectiveCamera] {
&[]
}
fn node(&self) -> Option<&crate::core::ObjectRef> {
None
}
}
impl RenderCamera for PerspectiveCamera {
fn node(&self) -> Option<&crate::core::ObjectRef> {
Some(&self.node)
}
fn set_view_offset(
&mut self,
full_width: f64,
full_height: f64,
x: f64,
y: f64,
width: f64,
height: f64,
) {
self.set_view_offset(full_width, full_height, x, y, width, height);
}
fn clear_view_offset(&mut self) {
self.clear_view_offset();
}
fn far(&self) -> f64 {
self.far
}
fn near(&self) -> f64 {
self.near
}
fn is_perspective_camera(&self) -> bool {
true
}
fn update_matrix_world(&mut self) {
self.update_matrix_world();
}
fn projection_matrix(&self) -> Matrix4 {
self.projection_matrix
}
fn projection_matrix_inverse(&self) -> Matrix4 {
self.projection_matrix_inverse
}
fn matrix_world_inverse(&self) -> Matrix4 {
self.matrix_world_inverse
}
fn coordinate_system(&self) -> CoordinateSystem {
self.coordinate_system
}
fn matrix_world(&self) -> Matrix4 {
self.node.borrow().matrix_world
}
fn layers(&self) -> Layers {
self.node.borrow().layers
}
fn id(&self) -> u32 {
self.node.borrow().id
}
}
impl RenderCamera for OrthographicCamera {
fn set_view_offset(
&mut self,
full_width: f64,
full_height: f64,
x: f64,
y: f64,
width: f64,
height: f64,
) {
self.set_view_offset(full_width, full_height, x, y, width, height);
}
fn clear_view_offset(&mut self) {
self.clear_view_offset();
}
fn far(&self) -> f64 {
self.far
}
fn near(&self) -> f64 {
self.near
}
fn is_orthographic_camera(&self) -> bool {
true
}
fn update_matrix_world(&mut self) {
self.update_matrix_world();
}
fn projection_matrix(&self) -> Matrix4 {
self.projection_matrix
}
fn projection_matrix_inverse(&self) -> Matrix4 {
self.projection_matrix_inverse
}
fn matrix_world_inverse(&self) -> Matrix4 {
self.matrix_world_inverse
}
fn coordinate_system(&self) -> CoordinateSystem {
self.coordinate_system
}
fn matrix_world(&self) -> Matrix4 {
self.object.matrix_world
}
fn layers(&self) -> Layers {
self.object.layers
}
fn id(&self) -> u32 {
self.object.id
}
}
fn view_basis(matrix_world: &Matrix4) -> (Vector3, Vector3, Vector3) {
let mut right = Vector3::ZERO;
let mut up = Vector3::ZERO;
let mut forward = Vector3::ZERO;
matrix_world.extract_basis(&mut right, &mut up, &mut forward);
right.normalize();
up.normalize();
forward.normalize().negate();
(right, up, forward)
}
fn corner_offsets(box_: &Box3, right: &Vector3, up: &Vector3, forward: &Vector3) -> [Vector3; 8] {
let center = box_.get_center();
let mut offsets = [Vector3::ZERO; 8];
for (i, offset) in offsets.iter_mut().enumerate() {
let corner = Vector3::new(
if i & 4 == 0 { box_.min.x } else { box_.max.x },
if i & 2 == 0 { box_.min.y } else { box_.max.y },
if i & 1 == 0 { box_.min.z } else { box_.max.z },
);
let mut d = Vector3::ZERO;
d.sub_vectors(&corner, ¢er);
offset.set(d.dot(right), d.dot(up), d.dot(forward));
}
offsets
}