use crate::cameras::{OrthographicCamera, PerspectiveCamera};
use crate::math::{Matrix4, Vector2, Vector3, RAD2DEG};
use crate::nodes::NodeRef;
use crate::textures::CubeDepthTexture;
use super::shadow_filter::ShadowFilterFn;
#[derive(Clone)]
#[allow(clippy::large_enum_variant)] pub enum ShadowCamera {
Perspective(PerspectiveCamera),
Orthographic(OrthographicCamera),
}
impl ShadowCamera {
pub fn near(&self) -> f64 {
match self {
ShadowCamera::Perspective(c) => c.near,
ShadowCamera::Orthographic(c) => c.near,
}
}
pub fn set_near(&mut self, near: f64) {
match self {
ShadowCamera::Perspective(c) => c.near = near,
ShadowCamera::Orthographic(c) => c.near = near,
}
}
pub fn far(&self) -> f64 {
match self {
ShadowCamera::Perspective(c) => c.far,
ShadowCamera::Orthographic(c) => c.far,
}
}
pub fn set_far(&mut self, far: f64) {
match self {
ShadowCamera::Perspective(c) => c.far = far,
ShadowCamera::Orthographic(c) => c.far = far,
}
}
pub fn set_bounds(&mut self, left: f64, right: f64, top: f64, bottom: f64) {
match self {
ShadowCamera::Perspective(_) => {
panic!("three-rs: a perspective shadow camera has no ortho bounds")
}
ShadowCamera::Orthographic(c) => {
c.left = left;
c.right = right;
c.top = top;
c.bottom = bottom;
}
}
}
pub fn is_orthographic(&self) -> bool {
matches!(self, ShadowCamera::Orthographic(_))
}
pub fn projection_matrix(&self) -> Matrix4 {
match self {
ShadowCamera::Perspective(c) => c.projection_matrix,
ShadowCamera::Orthographic(c) => c.projection_matrix,
}
}
pub fn matrix_world(&self) -> Matrix4 {
match self {
ShadowCamera::Perspective(c) => c.node.borrow().matrix_world,
ShadowCamera::Orthographic(c) => c.object.matrix_world,
}
}
pub fn matrix_world_inverse(&self) -> Matrix4 {
match self {
ShadowCamera::Perspective(c) => c.matrix_world_inverse,
ShadowCamera::Orthographic(c) => c.matrix_world_inverse,
}
}
pub fn update_projection_matrix(&mut self) {
match self {
ShadowCamera::Perspective(c) => c.update_projection_matrix(),
ShadowCamera::Orthographic(c) => c.update_projection_matrix(),
}
}
fn place(&mut self, position: Vector3, target: &Vector3) {
match self {
ShadowCamera::Perspective(c) => {
c.node.borrow_mut().position = position;
c.look_at(target);
c.update_matrix_world();
}
ShadowCamera::Orthographic(c) => {
c.object.position = position;
c.look_at(target);
c.update_matrix_world();
}
}
}
}
pub struct LightShadow {
pub camera: ShadowCamera,
pub intensity: f64,
pub bias: f64,
pub normal_bias: f64,
pub radius: f64,
pub blur_samples: usize,
pub map_size: Vector2,
pub matrix: Matrix4,
pub focus: f64,
pub aspect: f64,
pub filter_node: Option<ShadowFilterFn>,
pub shadow_node: Option<NodeRef>,
pub(crate) point_depth_texture: Option<CubeDepthTexture>,
}
impl Clone for LightShadow {
fn clone(&self) -> Self {
Self {
camera: self.camera.clone(),
intensity: self.intensity,
bias: self.bias,
normal_bias: self.normal_bias,
radius: self.radius,
blur_samples: self.blur_samples,
map_size: self.map_size,
matrix: self.matrix,
focus: self.focus,
aspect: self.aspect,
filter_node: self.filter_node.clone(),
shadow_node: self.shadow_node.clone(),
point_depth_texture: None,
}
}
}
impl LightShadow {
pub fn spot() -> Self {
Self::new(ShadowCamera::Perspective(PerspectiveCamera::new(
50.0, 1.0, 0.5, 500.0,
)))
}
pub fn point() -> Self {
Self::new(ShadowCamera::Perspective(PerspectiveCamera::new(
90.0, 1.0, 0.5, 500.0,
)))
}
pub fn directional() -> Self {
Self::new(ShadowCamera::Orthographic(OrthographicCamera::new(
-5.0, 5.0, 5.0, -5.0, 0.5, 500.0,
)))
}
fn new(camera: ShadowCamera) -> Self {
Self {
camera,
intensity: 1.0,
bias: 0.0,
normal_bias: 0.0,
radius: 1.0,
blur_samples: 8,
map_size: Vector2::new(512.0, 512.0),
matrix: Matrix4::identity(),
focus: 1.0,
aspect: 1.0,
filter_node: None,
shadow_node: None,
point_depth_texture: None,
}
}
pub fn point_depth_texture(&mut self) -> CubeDepthTexture {
let size = self.map_size.x as u32;
self.point_depth_texture
.get_or_insert_with(|| CubeDepthTexture::new(size))
.clone()
}
pub(crate) fn update_spot_projection(&mut self, angle: f64, distance: f64) {
let fov = RAD2DEG * 2.0 * angle * self.focus;
let aspect = (self.map_size.x / self.map_size.y) * self.aspect;
let far = if distance != 0.0 {
distance
} else {
self.camera.far()
};
if let ShadowCamera::Perspective(camera) = &mut self.camera {
if fov != camera.fov || aspect != camera.aspect || far != camera.far {
camera.fov = fov;
camera.aspect = aspect;
camera.far = far;
camera.update_projection_matrix();
}
}
}
pub(crate) fn update_point_matrices(&mut self, light_position_world: Vector3, distance: f64) {
let far = if distance != 0.0 {
distance
} else {
self.camera.far()
};
if far != self.camera.far() {
self.camera.set_far(far);
self.camera.update_projection_matrix();
}
let mut matrix = Matrix4::identity();
matrix.make_translation(
-light_position_world.x,
-light_position_world.y,
-light_position_world.z,
);
self.matrix = matrix;
}
pub fn update_matrices(&mut self, light_position_world: Vector3, look_target: Vector3) {
self.camera.place(light_position_world, &look_target);
self.update_matrix();
}
fn update_matrix(&mut self) {
let mut proj_screen_matrix = self.camera.projection_matrix();
proj_screen_matrix.multiply(&self.camera.matrix_world_inverse());
let mut shadow_matrix = Matrix4::identity();
shadow_matrix.set(
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,
);
shadow_matrix.multiply(&proj_screen_matrix);
self.matrix = shadow_matrix;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clone_does_not_share_the_point_depth_texture() {
let mut shadow = LightShadow::point();
shadow.point_depth_texture();
let copy = shadow.clone();
assert!(shadow.point_depth_texture.is_some());
assert!(copy.point_depth_texture.is_none());
}
}