render: make shadow depth bias configurable per light source

This commit is contained in:
SeanOMik 2024-07-21 21:53:02 -04:00
parent b0a6d30afc
commit fefcf58765
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
4 changed files with 28 additions and 13 deletions

View File

@ -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::<Gltf>("../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::<Gltf>("../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,
));

View File

@ -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::<ShadowCasterSettings>();
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::<RenderMeshes>();
self.transform_buffers = world.try_get_resource_data::<TransformBuffers>();
self.mesh_buffers = world.try_get_resource_data::<RenderAssets<MeshBufferStorage>>();
@ -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.

View File

@ -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<f32>, world_normal: vec3<f32>, 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<f32>, world_normal: vec3<f32>, 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);

View File

@ -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)