Use shadow parameters in update_shadow
@@ -60,16 +60,17 @@ impl DirectionalLight {
pub fn clear_shadow_map(&mut self) {
self.shadow_texture = None;
+ self.shadow_matrix = Mat4::identity();
}
- pub fn generate_shadow_map(&mut self, geometries: &[impl Geometry]) -> ThreeDResult<()> {
- if let Some(ref shadow_parameters) = self.shadow {
+ pub fn generate_shadow_map(
+ &mut self,
+ texture_size: u32,
+ geometries: &[impl Geometry],
+ ) -> ThreeDResult<()> {
let up = compute_up_direction(self.direction);
- let viewport = Viewport::new_at_origo(
- shadow_parameters.texture_size,
- shadow_parameters.texture_size,
- );
+ let viewport = Viewport::new_at_origo(texture_size, texture_size);
let mut aabb = AxisAlignedBoundingBox::EMPTY;
for geometry in geometries {
aabb.expand_with_aabb(&geometry.aabb());
@@ -94,8 +95,8 @@ impl DirectionalLight {
)?;
let mut shadow_texture = DepthTargetTexture2D::new(
&self.context,
- shadow_parameters.texture_size,
- shadow_parameters.texture_size,
+ texture_size,
+ texture_size,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
DepthFormat::Depth32F,
@@ -122,10 +123,6 @@ impl DirectionalLight {
})?;
self.shadow_texture = Some(shadow_texture);
self.shadow_matrix = shadow_matrix(&shadow_camera);
- } else {
- self.shadow_texture = None;
- self.shadow_matrix = Mat4::identity();
- }
Ok(())
}
@@ -179,6 +176,11 @@ impl Light for DirectionalLight {
Ok(())
}
fn update_shadow(&mut self, geometries: &[impl Geometry]) -> ThreeDResult<()> {
- self.generate_shadow_map(geometries)
+ if let Some(shadow_parameters) = self.shadow {
+ self.generate_shadow_map(shadow_parameters.texture_size, geometries)
+ } else {
+ self.clear_shadow_map();
+ Ok(())
+ }
}
}
