mod directional;
pub use directional::DirectionalShadow;
use crate::context::WgpuContext;
use crate::core::DepthTexture;
use glam::Mat4;
#[derive(Debug, Clone, Copy)]
pub struct ShadowConfig {
pub resolution: u32,
pub bias: f32,
pub normal_bias: f32,
pub pcf_enabled: bool,
pub pcf_radius: u32,
}
impl Default for ShadowConfig {
fn default() -> Self {
Self {
resolution: 2048,
bias: 0.005,
normal_bias: 0.02,
pcf_enabled: true,
pcf_radius: 1,
}
}
}
pub struct ShadowMap {
pub depth_texture: DepthTexture,
pub light_matrix: Mat4,
pub config: ShadowConfig,
}
impl ShadowMap {
pub fn new(ctx: &WgpuContext, config: ShadowConfig) -> Self {
let depth_texture = DepthTexture::new(
ctx,
config.resolution,
config.resolution,
Some("shadow map"),
);
Self {
depth_texture,
light_matrix: Mat4::IDENTITY,
config,
}
}
pub fn depth_view(&self) -> &wgpu::TextureView {
self.depth_texture.view()
}
pub fn uniform(&self) -> ShadowUniform {
ShadowUniform {
light_matrix: self.light_matrix.to_cols_array_2d(),
bias: self.config.bias,
normal_bias: self.config.normal_bias,
pcf_radius: self.config.pcf_radius as f32,
shadow_map_size: self.config.resolution as f32,
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ShadowUniform {
pub light_matrix: [[f32; 4]; 4],
pub bias: f32,
pub normal_bias: f32,
pub pcf_radius: f32,
pub shadow_map_size: f32,
}