1use crate::cameras::{OrthographicCamera, PerspectiveCamera};
6use crate::math::{Matrix4, Vector2, Vector3, RAD2DEG};
7use crate::nodes::NodeRef;
8use crate::textures::CubeDepthTexture;
9
10use super::shadow_filter::ShadowFilterFn;
11
12#[derive(Clone)]
15#[allow(clippy::large_enum_variant)] pub enum ShadowCamera {
17 Perspective(PerspectiveCamera),
19 Orthographic(OrthographicCamera),
21}
22
23impl ShadowCamera {
24 pub fn near(&self) -> f64 {
26 match self {
27 ShadowCamera::Perspective(c) => c.near,
28 ShadowCamera::Orthographic(c) => c.near,
29 }
30 }
31
32 pub fn set_near(&mut self, near: f64) {
34 match self {
35 ShadowCamera::Perspective(c) => c.near = near,
36 ShadowCamera::Orthographic(c) => c.near = near,
37 }
38 }
39
40 pub fn far(&self) -> f64 {
42 match self {
43 ShadowCamera::Perspective(c) => c.far,
44 ShadowCamera::Orthographic(c) => c.far,
45 }
46 }
47
48 pub fn set_far(&mut self, far: f64) {
50 match self {
51 ShadowCamera::Perspective(c) => c.far = far,
52 ShadowCamera::Orthographic(c) => c.far = far,
53 }
54 }
55
56 pub fn set_bounds(&mut self, left: f64, right: f64, top: f64, bottom: f64) {
59 match self {
60 ShadowCamera::Perspective(_) => {
61 panic!("three-rs: a perspective shadow camera has no ortho bounds")
62 }
63 ShadowCamera::Orthographic(c) => {
64 c.left = left;
65 c.right = right;
66 c.top = top;
67 c.bottom = bottom;
68 }
69 }
70 }
71
72 pub fn is_orthographic(&self) -> bool {
76 matches!(self, ShadowCamera::Orthographic(_))
77 }
78
79 pub fn projection_matrix(&self) -> Matrix4 {
81 match self {
82 ShadowCamera::Perspective(c) => c.projection_matrix,
83 ShadowCamera::Orthographic(c) => c.projection_matrix,
84 }
85 }
86
87 pub fn matrix_world(&self) -> Matrix4 {
89 match self {
90 ShadowCamera::Perspective(c) => c.node.borrow().matrix_world,
91 ShadowCamera::Orthographic(c) => c.object.matrix_world,
92 }
93 }
94
95 pub fn matrix_world_inverse(&self) -> Matrix4 {
97 match self {
98 ShadowCamera::Perspective(c) => c.matrix_world_inverse,
99 ShadowCamera::Orthographic(c) => c.matrix_world_inverse,
100 }
101 }
102
103 pub fn update_projection_matrix(&mut self) {
105 match self {
106 ShadowCamera::Perspective(c) => c.update_projection_matrix(),
107 ShadowCamera::Orthographic(c) => c.update_projection_matrix(),
108 }
109 }
110
111 fn place(&mut self, position: Vector3, target: &Vector3) {
114 match self {
115 ShadowCamera::Perspective(c) => {
116 c.node.borrow_mut().position = position;
117 c.look_at(target);
118 c.update_matrix_world();
119 }
120 ShadowCamera::Orthographic(c) => {
121 c.object.position = position;
122 c.look_at(target);
123 c.update_matrix_world();
124 }
125 }
126 }
127}
128
129pub struct LightShadow {
133 pub camera: ShadowCamera,
136 pub intensity: f64,
138 pub bias: f64,
140 pub normal_bias: f64,
142 pub radius: f64,
144 pub blur_samples: usize,
146 pub map_size: Vector2,
148 pub matrix: Matrix4,
151 pub focus: f64,
153 pub aspect: f64,
155 pub filter_node: Option<ShadowFilterFn>,
158 pub shadow_node: Option<NodeRef>,
161 pub(crate) point_depth_texture: Option<CubeDepthTexture>,
167}
168
169impl Clone for LightShadow {
174 fn clone(&self) -> Self {
175 Self {
176 camera: self.camera.clone(),
177 intensity: self.intensity,
178 bias: self.bias,
179 normal_bias: self.normal_bias,
180 radius: self.radius,
181 blur_samples: self.blur_samples,
182 map_size: self.map_size,
183 matrix: self.matrix,
184 focus: self.focus,
185 aspect: self.aspect,
186 filter_node: self.filter_node.clone(),
187 shadow_node: self.shadow_node.clone(),
188 point_depth_texture: None,
189 }
190 }
191}
192
193impl LightShadow {
194 pub fn spot() -> Self {
196 Self::new(ShadowCamera::Perspective(PerspectiveCamera::new(
197 50.0, 1.0, 0.5, 500.0,
198 )))
199 }
200
201 pub fn point() -> Self {
203 Self::new(ShadowCamera::Perspective(PerspectiveCamera::new(
204 90.0, 1.0, 0.5, 500.0,
205 )))
206 }
207
208 pub fn directional() -> Self {
211 Self::new(ShadowCamera::Orthographic(OrthographicCamera::new(
212 -5.0, 5.0, 5.0, -5.0, 0.5, 500.0,
213 )))
214 }
215
216 fn new(camera: ShadowCamera) -> Self {
217 Self {
218 camera,
219 intensity: 1.0,
220 bias: 0.0,
221 normal_bias: 0.0,
222 radius: 1.0,
223 blur_samples: 8,
224 map_size: Vector2::new(512.0, 512.0),
225 matrix: Matrix4::identity(),
226 focus: 1.0,
227 aspect: 1.0,
228 filter_node: None,
229 shadow_node: None,
230 point_depth_texture: None,
231 }
232 }
233
234 pub fn point_depth_texture(&mut self) -> CubeDepthTexture {
244 let size = self.map_size.x as u32;
245 self.point_depth_texture
246 .get_or_insert_with(|| CubeDepthTexture::new(size))
247 .clone()
248 }
249
250 pub(crate) fn update_spot_projection(&mut self, angle: f64, distance: f64) {
253 let fov = RAD2DEG * 2.0 * angle * self.focus;
254 let aspect = (self.map_size.x / self.map_size.y) * self.aspect;
255 let far = if distance != 0.0 {
256 distance
257 } else {
258 self.camera.far()
259 };
260
261 if let ShadowCamera::Perspective(camera) = &mut self.camera {
262 if fov != camera.fov || aspect != camera.aspect || far != camera.far {
263 camera.fov = fov;
264 camera.aspect = aspect;
265 camera.far = far;
266 camera.update_projection_matrix();
267 }
268 }
269 }
270
271 pub(crate) fn update_point_matrices(&mut self, light_position_world: Vector3, distance: f64) {
276 let far = if distance != 0.0 {
277 distance
278 } else {
279 self.camera.far()
280 };
281 if far != self.camera.far() {
282 self.camera.set_far(far);
283 self.camera.update_projection_matrix();
284 }
285 let mut matrix = Matrix4::identity();
286 matrix.make_translation(
287 -light_position_world.x,
288 -light_position_world.y,
289 -light_position_world.z,
290 );
291 self.matrix = matrix;
292 }
293
294 pub fn update_matrices(&mut self, light_position_world: Vector3, look_target: Vector3) {
297 self.camera.place(light_position_world, &look_target);
298 self.update_matrix();
299 }
300
301 fn update_matrix(&mut self) {
305 let mut proj_screen_matrix = self.camera.projection_matrix();
306 proj_screen_matrix.multiply(&self.camera.matrix_world_inverse());
307
308 let mut shadow_matrix = Matrix4::identity();
309 shadow_matrix.set(
310 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
314 );
315 shadow_matrix.multiply(&proj_screen_matrix);
316
317 self.matrix = shadow_matrix;
318 }
319}
320
321#[cfg(test)]
322mod tests {
323 use super::*;
324
325 #[test]
326 fn clone_does_not_share_the_point_depth_texture() {
327 let mut shadow = LightShadow::point();
328 shadow.point_depth_texture();
329 let copy = shadow.clone();
330 assert!(shadow.point_depth_texture.is_some());
331 assert!(copy.point_depth_texture.is_none());
332 }
333}