Implement Shadows #24

Merged
SeanOMik merged 28 commits from feat/shadow-maps into main 2024-08-10 03:10:30 +00:00
4 changed files with 28 additions and 13 deletions
Showing only changes of commit fefcf58765 - Show all commits

View File

@ -124,13 +124,6 @@ fn setup_scene_plugin(game: &mut Game) {
cube_gltf.wait_recurse_dependencies_load(); cube_gltf.wait_recurse_dependencies_load();
let cube_mesh = &cube_gltf.data_ref().unwrap().scenes[0]; 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 let palm_tree_platform_gltf = resman
.request::<Gltf>("../assets/shadows-platform-palmtree.glb") .request::<Gltf>("../assets/shadows-platform-palmtree.glb")
.unwrap(); .unwrap();
@ -189,6 +182,10 @@ fn setup_scene_plugin(game: &mut Game) {
color: Vec3::new(1.0, 0.95, 0.9), color: Vec3::new(1.0, 0.95, 0.9),
intensity: 0.9, intensity: 0.9,
}, },
ShadowCasterSettings {
filtering_mode: ShadowFilteringMode::Pcss,
..Default::default()
},
light_tran, light_tran,
)); ));

View File

@ -256,7 +256,8 @@ impl ShadowMapsPass {
has_shadow_settings, has_shadow_settings,
pcf_samples_num: u.pcf_samples_num, pcf_samples_num: u.pcf_samples_num,
pcss_blocker_search_samples: u.pcss_blocker_search_samples, 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); let uniform_index = self.light_uniforms_buffer.insert(queue, &u);
@ -348,7 +349,8 @@ impl ShadowMapsPass {
has_shadow_settings, has_shadow_settings,
pcf_samples_num: u.pcf_samples_num, pcf_samples_num: u.pcf_samples_num,
pcss_blocker_search_samples: u.pcss_blocker_search_samples, 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; indices[i] = uniform_i;
@ -594,6 +596,11 @@ impl Node for ShadowMapsPass {
} }
let settings = *world.get_resource::<ShadowCasterSettings>(); 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.render_meshes = world.try_get_resource_data::<RenderMeshes>();
self.transform_buffers = world.try_get_resource_data::<TransformBuffers>(); self.transform_buffers = world.try_get_resource_data::<TransformBuffers>();
self.mesh_buffers = world.try_get_resource_data::<RenderAssets<MeshBufferStorage>>(); self.mesh_buffers = world.try_get_resource_data::<RenderAssets<MeshBufferStorage>>();
@ -888,8 +895,15 @@ pub struct ShadowCasterSettings {
pub pcss_blocker_search_samples: u32, pub pcss_blocker_search_samples: u32,
pub near_plane: f32, pub near_plane: f32,
pub far_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 { impl Default for ShadowCasterSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -898,6 +912,8 @@ impl Default for ShadowCasterSettings {
pcss_blocker_search_samples: 25, pcss_blocker_search_samples: 25,
near_plane: 0.1, near_plane: 0.1,
far_plane: 45.0, 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, has_shadow_settings: u32,
pcf_samples_num: u32, pcf_samples_num: u32,
pcss_blocker_search_samples: 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. /// 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, has_shadow_settings: u32,
pcf_samples_num: u32, pcf_samples_num: u32,
pcss_blocker_search_samples: u32, pcss_blocker_search_samples: u32,
constant_depth_bias: f32,
} }
struct ShadowSettingsUniform { 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; let xy_remapped = proj_coords.xy * 0.5 + 0.5;
// use a bias to avoid shadow acne // 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 - map_data.constant_depth_bias;
let current_depth = proj_coords.z - bias;
// get settings // get settings
let settings = get_shadow_settings(map_data); 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); var current_depth = length(frag_to_light);
current_depth /= u.far_plane; 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 // get settings
let settings = get_shadow_settings(u); let settings = get_shadow_settings(u);

View File

@ -19,6 +19,7 @@ struct LightShadowMapUniform {
has_shadow_settings: u32, has_shadow_settings: u32,
pcf_samples_num: u32, pcf_samples_num: u32,
pcss_blocker_search_samples: u32, pcss_blocker_search_samples: u32,
constant_depth_bias: f32,
} }
@group(0) @binding(0) @group(0) @binding(0)