bevy/examples/3d
Patrick Walton 2ae5a21009
Implement percentage-closer soft shadows (PCSS). (#13497)
[*Percentage-closer soft shadows*] are a technique from 2004 that allow
shadows to become blurrier farther from the objects that cast them. It
works by introducing a *blocker search* step that runs before the normal
shadow map sampling. The blocker search step detects the difference
between the depth of the fragment being rasterized and the depth of the
nearby samples in the depth buffer. Larger depth differences result in a
larger penumbra and therefore a blurrier shadow.

To enable PCSS, fill in the `soft_shadow_size` value in
`DirectionalLight`, `PointLight`, or `SpotLight`, as appropriate. This
shadow size value represents the size of the light and should be tuned
as appropriate for your scene. Higher values result in a wider penumbra
(i.e. blurrier shadows).

When using PCSS, temporal shadow maps
(`ShadowFilteringMethod::Temporal`) are recommended. If you don't use
`ShadowFilteringMethod::Temporal` and instead use
`ShadowFilteringMethod::Gaussian`, Bevy will use the same technique as
`Temporal`, but the result won't vary over time. This produces a rather
noisy result. Doing better would likely require downsampling the shadow
map, which would be complex and slower (and would require PR #13003 to
land first).

In addition to PCSS, this commit makes the near Z plane for the shadow
map configurable on a per-light basis. Previously, it had been hardcoded
to 0.1 meters. This change was necessary to make the point light shadow
map in the example look reasonable, as otherwise the shadows appeared
far too aliased.

A new example, `pcss`, has been added. It demonstrates the
percentage-closer soft shadow technique with directional lights, point
lights, spot lights, non-temporal operation, and temporal operation. The
assets are my original work.

Both temporal and non-temporal shadows are rather noisy in the example,
and, as mentioned before, this is unavoidable without downsampling the
depth buffer, which we can't do yet. Note also that the shadows don't
look particularly great for point lights; the example simply isn't an
ideal scene for them. Nevertheless, I felt that the benefits of the
ability to do a side-by-side comparison of directional and point lights
outweighed the unsightliness of the point light shadows in that example,
so I kept the point light feature in.

Fixes #3631.

[*Percentage-closer soft shadows*]:
https://developer.download.nvidia.com/shaderlibrary/docs/shadow_PCSS.pdf

## Changelog

### Added

* Percentage-closer soft shadows (PCSS) are now supported, allowing
shadows to become blurrier as they stretch away from objects. To use
them, set the `soft_shadow_size` field in `DirectionalLight`,
`PointLight`, or `SpotLight`, as applicable.

* The near Z value for shadow maps is now customizable via the
`shadow_map_near_z` field in `DirectionalLight`, `PointLight`, and
`SpotLight`.

## Screenshots

PCSS off:
![Screenshot 2024-05-24
120012](https://github.com/bevyengine/bevy/assets/157897/0d35fe98-245b-44fb-8a43-8d0272a73b86)

PCSS on:
![Screenshot 2024-05-24
115959](https://github.com/bevyengine/bevy/assets/157897/83397ef8-1317-49dd-bfb3-f8286d7610cd)

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com>
2024-09-18 18:07:17 +00:00
..
3d_scene.rs Swapping back to using From<Color> for StandardMaterial in examples (#13566) 2024-05-29 13:50:28 +00:00
3d_shapes.rs Disabled usage of the POLYGON_MODE_LINE gpu feature in the examples (#14402) 2024-07-29 23:40:39 +00:00
3d_viewport_to_world.rs Return Results from Camera's world/viewport conversion methods (#14989) 2024-09-03 19:45:15 +00:00
animated_material.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
anisotropy.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
anti_aliasing.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
atmospheric_fog.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
auto_exposure.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
blend_modes.rs Fix common capitalization errors in documentation (#14562) 2024-07-31 21:16:05 +00:00
bloom_3d.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
clearcoat.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
color_grading.rs Fixing text sizes for examples (#15190) 2024-09-16 23:14:37 +00:00
deferred_rendering.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
depth_of_field.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
fog.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
fog_volumes.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
generate_custom_mesh.rs Fix a few "repeated word" typos (#13955) 2024-06-20 21:35:20 +00:00
irradiance_volumes.rs style: simplify string formatting for readability (#15033) 2024-09-03 23:35:49 +00:00
lighting.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
lightmaps.rs glTF labels: add enum to avoid misspelling and keep up-to-date list documented (#13586) 2024-05-31 23:25:57 +00:00
lines.rs Highlight dependency on shader files in examples (#13824) 2024-06-12 14:16:01 +00:00
load_gltf.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
load_gltf_extras.rs Fixing text sizes for examples (#15190) 2024-09-16 23:14:37 +00:00
meshlet.rs More triangles/vertices per meshlet (#15023) 2024-09-08 17:55:57 +00:00
motion_blur.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
orthographic.rs Split OrthographicProjection::default into 2d & 3d (Adopted) (#15073) 2024-09-09 15:51:28 +00:00
parallax_mapping.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
parenting.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
pbr.rs Fixing text sizes for examples (#15190) 2024-09-16 23:14:37 +00:00
pcss.rs Implement percentage-closer soft shadows (PCSS). (#13497) 2024-09-18 18:07:17 +00:00
post_processing.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
reflection_probes.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
render_to_texture.rs Simplify render_to_texture examples (#14855) 2024-08-25 14:15:11 +00:00
rotate_environment_map.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
scrolling_fog.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
shadow_biases.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00
shadow_caster_receiver.rs Remove redundant imports (#12817) 2024-04-01 19:59:08 +00:00
skybox.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
spherical_area_lights.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
split_screen.rs Fix num_cascades in split_screen exmample for WebGL (#14601) 2024-08-04 13:57:22 +00:00
spotlight.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
ssao.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
ssr.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
texture.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
tonemapping.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
transmission.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
transparency_3d.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
two_passes.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
update_gltf_scene.rs Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
vertex_colors.rs Migrate from LegacyColor to bevy_color::Color (#12163) 2024-02-29 19:35:12 +00:00
visibility_range.rs Add support for environment map transformation (#14290) 2024-07-19 15:00:50 +00:00
volumetric_fog.rs Rename rendering components for improved consistency and clarity (#15035) 2024-09-10 01:11:46 +00:00
wireframe.rs Updates default Text font size to 24px (#13603) 2024-05-31 16:41:27 +00:00