Skip to main content

three_rs/cameras/
array_camera.rs

1//! Port of `three.js/src/cameras/ArrayCamera.js`.
2
3use std::ops::{Deref, DerefMut};
4
5use super::{PerspectiveCamera, RenderCamera};
6use crate::core::Layers;
7use crate::math::{CoordinateSystem, Matrix4};
8
9/// `ArrayCamera` — a `PerspectiveCamera` holding sub-cameras that one
10/// `render()` call draws the scene through, each into its own
11/// [`viewport`](PerspectiveCamera::viewport).
12///
13/// three.js' `ArrayCamera extends PerspectiveCamera`; the port derefs to the
14/// base camera, which is what the render list's sort and the frustum array's
15/// fallback read. The sub-cameras are not its children: their matrices are
16/// the application's to update, as `webgpu_camera_array` does with
17/// `subcamera.updateMatrixWorld()`.
18///
19/// `isMultiViewCamera` (WebXR's `OVR_multiview2`) is not ported.
20#[derive(Clone)]
21pub struct ArrayCamera {
22    /// The base camera: `ArrayCamera extends PerspectiveCamera`, held by value
23    /// since Rust has no inheritance.
24    pub camera: PerspectiveCamera,
25    /// `camera.cameras`.
26    pub cameras: Vec<PerspectiveCamera>,
27}
28
29impl ArrayCamera {
30    /// `new ArrayCamera( array )` — the base camera is `new
31    /// PerspectiveCamera()`'s defaults.
32    pub fn new(cameras: Vec<PerspectiveCamera>) -> Self {
33        Self {
34            camera: PerspectiveCamera::new(50.0, 1.0, 0.1, 2000.0),
35            cameras,
36        }
37    }
38}
39
40impl Deref for ArrayCamera {
41    type Target = PerspectiveCamera;
42    fn deref(&self) -> &PerspectiveCamera {
43        &self.camera
44    }
45}
46
47impl DerefMut for ArrayCamera {
48    fn deref_mut(&mut self) -> &mut PerspectiveCamera {
49        &mut self.camera
50    }
51}
52
53impl RenderCamera for ArrayCamera {
54    fn far(&self) -> f64 {
55        self.camera.far
56    }
57    fn near(&self) -> f64 {
58        self.camera.near
59    }
60    fn is_perspective_camera(&self) -> bool {
61        true
62    }
63    fn update_matrix_world(&mut self) {
64        self.camera.update_matrix_world();
65    }
66    /// `ArrayCamera` inherits `PerspectiveCamera.setViewOffset`, so the
67    /// offset lands on the outer camera; three's sub-cameras keep their own
68    /// projections, and so do these.
69    fn set_view_offset(
70        &mut self,
71        full_width: f64,
72        full_height: f64,
73        x: f64,
74        y: f64,
75        width: f64,
76        height: f64,
77    ) {
78        self.camera
79            .set_view_offset(full_width, full_height, x, y, width, height);
80    }
81    fn clear_view_offset(&mut self) {
82        self.camera.clear_view_offset();
83    }
84    fn projection_matrix(&self) -> Matrix4 {
85        self.camera.projection_matrix
86    }
87    fn projection_matrix_inverse(&self) -> Matrix4 {
88        self.camera.projection_matrix_inverse
89    }
90    fn matrix_world_inverse(&self) -> Matrix4 {
91        self.camera.matrix_world_inverse
92    }
93    fn coordinate_system(&self) -> CoordinateSystem {
94        self.camera.coordinate_system
95    }
96    fn matrix_world(&self) -> Matrix4 {
97        self.camera.node.borrow().matrix_world
98    }
99    fn layers(&self) -> Layers {
100        self.camera.node.borrow().layers
101    }
102    fn id(&self) -> u32 {
103        self.camera.node.borrow().id
104    }
105    fn sub_cameras(&self) -> &[PerspectiveCamera] {
106        &self.cameras
107    }
108}