diff --git a/examples/shadows/src/main.rs b/examples/shadows/src/main.rs index ebf9987..ba929aa 100644 --- a/examples/shadows/src/main.rs +++ b/examples/shadows/src/main.rs @@ -124,13 +124,6 @@ fn setup_scene_plugin(game: &mut Game) { cube_gltf.wait_recurse_dependencies_load(); let cube_mesh = &cube_gltf.data_ref().unwrap().scenes[0]; - let platform_gltf = resman - .request::("../assets/wood-platform.glb") - .unwrap(); - - platform_gltf.wait_recurse_dependencies_load(); - let platform_mesh = &platform_gltf.data_ref().unwrap().scenes[0]; - let palm_tree_platform_gltf = resman .request::("../assets/shadows-platform-palmtree.glb") .unwrap(); @@ -189,6 +182,10 @@ fn setup_scene_plugin(game: &mut Game) { color: Vec3::new(1.0, 0.95, 0.9), intensity: 0.9, }, + ShadowCasterSettings { + filtering_mode: ShadowFilteringMode::Pcss, + ..Default::default() + }, light_tran, )); diff --git a/lyra-game/src/render/graph/passes/shadows.rs b/lyra-game/src/render/graph/passes/shadows.rs index 8ff4ef3..3e207a9 100644 --- a/lyra-game/src/render/graph/passes/shadows.rs +++ b/lyra-game/src/render/graph/passes/shadows.rs @@ -256,7 +256,8 @@ impl ShadowMapsPass { has_shadow_settings, pcf_samples_num: u.pcf_samples_num, pcss_blocker_search_samples: u.pcss_blocker_search_samples, - _padding2: [0; 2], + constant_depth_bias: DEFAULT_CONSTANT_DEPTH_BIAS * shadow_settings.constant_depth_bias_scale, + _padding2: 0, }; let uniform_index = self.light_uniforms_buffer.insert(queue, &u); @@ -348,7 +349,8 @@ impl ShadowMapsPass { has_shadow_settings, pcf_samples_num: u.pcf_samples_num, pcss_blocker_search_samples: u.pcss_blocker_search_samples, - _padding2: [0; 2], + constant_depth_bias: DEFAULT_CONSTANT_DEPTH_BIAS * shadow_settings.constant_depth_bias_scale, + _padding2: 0, }, ); indices[i] = uniform_i; @@ -594,6 +596,11 @@ impl Node for ShadowMapsPass { } let settings = *world.get_resource::(); + if settings.use_back_faces { + // TODO: shadow maps rendering with back faces + todo!("render with back faces"); + } + self.render_meshes = world.try_get_resource_data::(); self.transform_buffers = world.try_get_resource_data::(); self.mesh_buffers = world.try_get_resource_data::>(); @@ -888,8 +895,15 @@ pub struct ShadowCasterSettings { pub pcss_blocker_search_samples: u32, pub near_plane: f32, pub far_plane: f32, + /// The scale of the constant shadow depth bias. + /// + /// This scale will be multiplied by the default constant depth bias value of 0.001. + pub constant_depth_bias_scale: f32, + pub use_back_faces: bool, } +const DEFAULT_CONSTANT_DEPTH_BIAS: f32 = 0.001; + impl Default for ShadowCasterSettings { fn default() -> Self { Self { @@ -898,6 +912,8 @@ impl Default for ShadowCasterSettings { pcss_blocker_search_samples: 25, near_plane: 0.1, far_plane: 45.0, + constant_depth_bias_scale: 1.0, + use_back_faces: false, } } } @@ -917,7 +933,8 @@ pub struct LightShadowUniform { has_shadow_settings: u32, pcf_samples_num: u32, pcss_blocker_search_samples: u32, - _padding2: [u32; 2], + constant_depth_bias: f32, + _padding2: u32, } /// A component that stores the ID of a shadow map in the shadow map atlas for the entities. diff --git a/lyra-game/src/render/shaders/base.wgsl b/lyra-game/src/render/shaders/base.wgsl index 6103938..2bab59e 100755 --- a/lyra-game/src/render/shaders/base.wgsl +++ b/lyra-game/src/render/shaders/base.wgsl @@ -123,6 +123,7 @@ struct LightShadowMapUniform { has_shadow_settings: u32, pcf_samples_num: u32, pcss_blocker_search_samples: u32, + constant_depth_bias: f32, } struct ShadowSettingsUniform { @@ -277,8 +278,7 @@ fn calc_shadow_dir_light(world_pos: vec3, world_normal: vec3, light_di let xy_remapped = proj_coords.xy * 0.5 + 0.5; // use a bias to avoid shadow acne - let bias = 0.005;//max(0.05 * (1.0 - dot(normal, light_dir)), 0.005); - let current_depth = proj_coords.z - bias; + let current_depth = proj_coords.z - map_data.constant_depth_bias; // get settings let settings = get_shadow_settings(map_data); @@ -427,7 +427,7 @@ fn calc_shadow_point_light(world_pos: vec3, world_normal: vec3, light_ var current_depth = length(frag_to_light); current_depth /= u.far_plane; - current_depth -= 0.005; // TODO: find a better way to calculate bias + current_depth -= u.constant_depth_bias; // get settings let settings = get_shadow_settings(u); diff --git a/lyra-game/src/render/shaders/shadows.wgsl b/lyra-game/src/render/shaders/shadows.wgsl index 4e9bf16..58be2c5 100644 --- a/lyra-game/src/render/shaders/shadows.wgsl +++ b/lyra-game/src/render/shaders/shadows.wgsl @@ -19,6 +19,7 @@ struct LightShadowMapUniform { has_shadow_settings: u32, pcf_samples_num: u32, pcss_blocker_search_samples: u32, + constant_depth_bias: f32, } @group(0) @binding(0)