Compare commits

..

No commits in common. "fefcf587658194d13934317b205138d70de52d61" and "fef709d5f102798a81ea9dfbee18c3d9b8084ba1" have entirely different histories.

4 changed files with 30 additions and 51 deletions

View File

@ -124,6 +124,13 @@ 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();
@ -182,10 +189,6 @@ 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,8 +256,7 @@ impl ShadowMapsPass {
has_shadow_settings,
pcf_samples_num: u.pcf_samples_num,
pcss_blocker_search_samples: u.pcss_blocker_search_samples,
constant_depth_bias: DEFAULT_CONSTANT_DEPTH_BIAS * shadow_settings.constant_depth_bias_scale,
_padding2: 0,
_padding2: [0; 2],
};
let uniform_index = self.light_uniforms_buffer.insert(queue, &u);
@ -349,8 +348,7 @@ impl ShadowMapsPass {
has_shadow_settings,
pcf_samples_num: u.pcf_samples_num,
pcss_blocker_search_samples: u.pcss_blocker_search_samples,
constant_depth_bias: DEFAULT_CONSTANT_DEPTH_BIAS * shadow_settings.constant_depth_bias_scale,
_padding2: 0,
_padding2: [0; 2],
},
);
indices[i] = uniform_i;
@ -596,11 +594,6 @@ 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>>();
@ -895,15 +888,8 @@ 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 {
@ -912,8 +898,6 @@ 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,
}
}
}
@ -933,8 +917,7 @@ pub struct LightShadowUniform {
has_shadow_settings: u32,
pcf_samples_num: u32,
pcss_blocker_search_samples: u32,
constant_depth_bias: f32,
_padding2: u32,
_padding2: [u32; 2],
}
/// A component that stores the ID of a shadow map in the shadow map atlas for the entities.

View File

@ -123,7 +123,6 @@ struct LightShadowMapUniform {
has_shadow_settings: u32,
pcf_samples_num: u32,
pcss_blocker_search_samples: u32,
constant_depth_bias: f32,
}
struct ShadowSettingsUniform {
@ -184,7 +183,7 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let shadow_u: LightShadowMapUniform = u_light_shadow[light.light_shadow_uniform_index[0]];
let frag_pos_light_space = shadow_u.light_space_matrix * vec4<f32>(in.world_position, 1.0);
let shadow = calc_shadow_dir_light(in.world_position, in.world_normal, light_dir, light);
let shadow = calc_shadow_dir_light(in.world_normal, light_dir, frag_pos_light_space, atlas_dimensions, shadow_u);
light_res += blinn_phong_dir_light(in.world_position, in.world_normal, light, u_material, specular_color, shadow);
} else if (light.light_ty == LIGHT_TY_POINT) {
let shadow = calc_shadow_point_light(in.world_position, in.world_normal, light_dir, light, atlas_dimensions);
@ -266,10 +265,7 @@ fn get_shadow_settings(shadow_u: LightShadowMapUniform) -> vec2<u32> {
}
}
fn calc_shadow_dir_light(world_pos: vec3<f32>, world_normal: vec3<f32>, light_dir: vec3<f32>, light: Light) -> f32 {
let map_data: LightShadowMapUniform = u_light_shadow[light.light_shadow_uniform_index[0]];
let frag_pos_light_space = map_data.light_space_matrix * vec4<f32>(world_pos, 1.0);
fn calc_shadow_dir_light(normal: vec3<f32>, light_dir: vec3<f32>, frag_pos_light_space: vec4<f32>, atlas_dimensions: vec2<i32>, shadow_u: LightShadowMapUniform) -> f32 {
var proj_coords = frag_pos_light_space.xyz / frag_pos_light_space.w;
// for some reason the y component is flipped after transforming
proj_coords.y = -proj_coords.y;
@ -278,31 +274,32 @@ 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 current_depth = proj_coords.z - map_data.constant_depth_bias;
let bias = 0.005;//max(0.05 * (1.0 - dot(normal, light_dir)), 0.005);
let current_depth = proj_coords.z - bias;
// get settings
let settings = get_shadow_settings(map_data);
let settings = get_shadow_settings(shadow_u);
let pcf_samples_num = settings.x;
let pcss_blocker_search_samples = settings.y;
var shadow = 0.0;
// hardware 2x2 PCF via camparison sampler
if pcf_samples_num == 2u {
let region_coords = to_atlas_frame_coords(map_data, xy_remapped, false);
let region_coords = to_atlas_frame_coords(shadow_u, xy_remapped);
shadow = textureSampleCompareLevel(t_shadow_maps_atlas, s_shadow_maps_atlas_compare, region_coords, current_depth);
}
// PCSS
else if pcf_samples_num > 0u && pcss_blocker_search_samples > 0u {
shadow = pcss_dir_light(xy_remapped, current_depth, map_data);
shadow = pcss_dir_light(xy_remapped, current_depth, shadow_u);
}
// only PCF
else if pcf_samples_num > 0u {
let texel_size = 1.0 / f32(map_data.atlas_frame.width);
shadow = pcf_dir_light(xy_remapped, current_depth, map_data, texel_size);
let texel_size = 1.0 / f32(shadow_u.atlas_frame.width);
shadow = pcf_dir_light(xy_remapped, current_depth, shadow_u, texel_size);
}
// no filtering
else {
let region_coords = to_atlas_frame_coords(map_data, xy_remapped, false);
let region_coords = to_atlas_frame_coords(shadow_u, xy_remapped);
let closest_depth = textureSampleLevel(t_shadow_maps_atlas, s_shadow_maps_atlas, region_coords, 0.0);
shadow = select(1.0, 0.0, current_depth > closest_depth);
}
@ -326,10 +323,7 @@ fn search_width(light_near: f32, uv_light_size: f32, receiver_depth: f32) -> f32
}
/// Convert texture coords to be texture coords of an atlas frame.
///
/// If `safety_offset` is true, the frame will be shrank by a tiny amount to avoid bleeding
/// into adjacent frames from fiiltering.
fn to_atlas_frame_coords(shadow_u: LightShadowMapUniform, coords: vec2<f32>, safety_offset: bool) -> vec2<f32> {
fn to_atlas_frame_coords(shadow_u: LightShadowMapUniform, coords: vec2<f32>) -> vec2<f32> {
let atlas_dimensions = textureDimensions(t_shadow_maps_atlas);
// get the rect of the frame as a vec4
@ -338,9 +332,9 @@ fn to_atlas_frame_coords(shadow_u: LightShadowMapUniform, coords: vec2<f32>, saf
// put the frame rect in atlas UV space
region_rect /= f32(atlas_dimensions.x);
// if safety_offset is true, calculate a relatively tiny offset to avoid getting the end of
// the frame and causing linear or nearest filtering to bleed to the adjacent frame.
let texel_size = select(0.0, (1.0 / f32(shadow_u.atlas_frame.x)) * 4.0, safety_offset);
// calculate a relatively tiny offset to avoid getting the end of the frame and causing
// linear or nearest filtering to bleed to the adjacent frame.
let texel_size = (1.0 / f32(shadow_u.atlas_frame.x)) * 4.0;
// lerp input coords
let region_coords = vec2<f32>(
@ -360,7 +354,7 @@ fn find_blocker_distance_dir_light(tex_coords: vec2<f32>, receiver_depth: f32, b
let samples = i32(u_shadow_settings.pcss_blocker_search_samples);
for (var i = 0; i < samples; i++) {
let offset_coords = tex_coords + u_pcss_poisson_disc[i] * search_width;
let new_coords = to_atlas_frame_coords(shadow_u, offset_coords, false);
let new_coords = to_atlas_frame_coords(shadow_u, offset_coords);
let z = textureSampleLevel(t_shadow_maps_atlas, s_shadow_maps_atlas, new_coords, 0.0);
if z < (receiver_depth - bias) {
@ -396,7 +390,7 @@ fn pcf_dir_light(tex_coords: vec2<f32>, test_depth: f32, shadow_u: LightShadowMa
let samples_num = i32(u_shadow_settings.pcf_samples_num);
for (var i = 0; i < samples_num; i++) {
let offset = tex_coords + u_pcf_poisson_disc[i] * uv_radius;
let new_coords = to_atlas_frame_coords(shadow_u, offset, false);
let new_coords = to_atlas_frame_coords(shadow_u, offset);
shadow += textureSampleCompare(t_shadow_maps_atlas, s_shadow_maps_atlas_compare, new_coords, test_depth);
}
@ -427,7 +421,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 -= u.constant_depth_bias;
current_depth -= 0.005; // TODO: find a better way to calculate bias
// get settings
let settings = get_shadow_settings(u);
@ -437,7 +431,7 @@ fn calc_shadow_point_light(world_pos: vec3<f32>, world_normal: vec3<f32>, light_
var shadow = 0.0;
// hardware 2x2 PCF via camparison sampler
if pcf_samples_num == 2u {
let region_coords = to_atlas_frame_coords(u, coords_2d, true);
let region_coords = to_atlas_frame_coords(u, coords_2d);
shadow = textureSampleCompareLevel(t_shadow_maps_atlas, s_shadow_maps_atlas_compare, region_coords, current_depth);
}
// PCSS
@ -452,7 +446,7 @@ fn calc_shadow_point_light(world_pos: vec3<f32>, world_normal: vec3<f32>, light_
}
// no filtering
else {
let region_coords = to_atlas_frame_coords(u, coords_2d, true);
let region_coords = to_atlas_frame_coords(u, coords_2d);
let closest_depth = textureSampleLevel(t_shadow_maps_atlas, s_shadow_maps_atlas, region_coords, 0.0);
shadow = select(1.0, 0.0, current_depth > closest_depth);
}
@ -473,7 +467,7 @@ fn pcf_point_light(tex_coords: vec3<f32>, test_depth: f32, shadow_us: array<Ligh
coords_2d += u_pcf_poisson_disc[i] * uv_radius;
let new_coords = to_atlas_frame_coords(shadow_u, coords_2d, true);
let new_coords = to_atlas_frame_coords(shadow_u, coords_2d);
shadow += textureSampleCompare(t_shadow_maps_atlas, s_shadow_maps_atlas_compare, new_coords, test_depth);
}
shadow /= f32(samples_num);

View File

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