
Co-authored-by: Robert Swain <robert.swain@gmail.com> # Objective Implements cascaded shadow maps for directional lights, which produces better quality shadows without needing excessively large shadow maps. Fixes #3629 Before  After  ## Solution Rather than rendering a single shadow map for directional light, the view frustum is divided into a series of cascades, each of which gets its own shadow map. The correct cascade is then sampled for shadow determination. --- ## Changelog Directional lights now use cascaded shadow maps for improved shadow quality. ## Migration Guide You no longer have to manually specify a `shadow_projection` for a directional light, and these settings should be removed. If customization of how cascaded shadow maps work is desired, modify the `CascadeShadowConfig` component instead.
121 lines
4.1 KiB
Rust
121 lines
4.1 KiB
Rust
use crate::{
|
|
CascadeShadowConfig, Cascades, DirectionalLight, Material, PointLight, SpotLight,
|
|
StandardMaterial,
|
|
};
|
|
use bevy_asset::Handle;
|
|
use bevy_ecs::{bundle::Bundle, component::Component, prelude::Entity, reflect::ReflectComponent};
|
|
use bevy_reflect::Reflect;
|
|
use bevy_render::{
|
|
mesh::Mesh,
|
|
primitives::{CascadesFrusta, CubemapFrusta, Frustum},
|
|
view::{ComputedVisibility, Visibility, VisibleEntities},
|
|
};
|
|
use bevy_transform::components::{GlobalTransform, Transform};
|
|
use bevy_utils::HashMap;
|
|
|
|
/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
|
|
pub type PbrBundle = MaterialMeshBundle<StandardMaterial>;
|
|
|
|
/// A component bundle for entities with a [`Mesh`] and a [`Material`].
|
|
#[derive(Bundle, Clone)]
|
|
pub struct MaterialMeshBundle<M: Material> {
|
|
pub mesh: Handle<Mesh>,
|
|
pub material: Handle<M>,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// User indication of whether an entity is visible
|
|
pub visibility: Visibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub computed_visibility: ComputedVisibility,
|
|
}
|
|
|
|
impl<M: Material> Default for MaterialMeshBundle<M> {
|
|
fn default() -> Self {
|
|
Self {
|
|
mesh: Default::default(),
|
|
material: Default::default(),
|
|
transform: Default::default(),
|
|
global_transform: Default::default(),
|
|
visibility: Default::default(),
|
|
computed_visibility: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Clone, Debug, Default, Reflect)]
|
|
#[reflect(Component)]
|
|
pub struct CubemapVisibleEntities {
|
|
#[reflect(ignore)]
|
|
data: [VisibleEntities; 6],
|
|
}
|
|
|
|
impl CubemapVisibleEntities {
|
|
pub fn get(&self, i: usize) -> &VisibleEntities {
|
|
&self.data[i]
|
|
}
|
|
|
|
pub fn get_mut(&mut self, i: usize) -> &mut VisibleEntities {
|
|
&mut self.data[i]
|
|
}
|
|
|
|
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &VisibleEntities> {
|
|
self.data.iter()
|
|
}
|
|
|
|
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut VisibleEntities> {
|
|
self.data.iter_mut()
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Clone, Debug, Default, Reflect)]
|
|
#[reflect(Component)]
|
|
pub struct CascadesVisibleEntities {
|
|
/// Map of view entity to the visible entities for each cascade frustum.
|
|
#[reflect(ignore)]
|
|
pub entities: HashMap<Entity, Vec<VisibleEntities>>,
|
|
}
|
|
|
|
/// A component bundle for [`PointLight`] entities.
|
|
#[derive(Debug, Bundle, Default)]
|
|
pub struct PointLightBundle {
|
|
pub point_light: PointLight,
|
|
pub cubemap_visible_entities: CubemapVisibleEntities,
|
|
pub cubemap_frusta: CubemapFrusta,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// Enables or disables the light
|
|
pub visibility: Visibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub computed_visibility: ComputedVisibility,
|
|
}
|
|
|
|
/// A component bundle for spot light entities
|
|
#[derive(Debug, Bundle, Default)]
|
|
pub struct SpotLightBundle {
|
|
pub spot_light: SpotLight,
|
|
pub visible_entities: VisibleEntities,
|
|
pub frustum: Frustum,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// Enables or disables the light
|
|
pub visibility: Visibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub computed_visibility: ComputedVisibility,
|
|
}
|
|
|
|
/// A component bundle for [`DirectionalLight`] entities.
|
|
#[derive(Debug, Bundle, Default)]
|
|
pub struct DirectionalLightBundle {
|
|
pub directional_light: DirectionalLight,
|
|
pub frusta: CascadesFrusta,
|
|
pub cascades: Cascades,
|
|
pub cascade_shadow_config: CascadeShadowConfig,
|
|
pub visible_entities: CascadesVisibleEntities,
|
|
pub transform: Transform,
|
|
pub global_transform: GlobalTransform,
|
|
/// Enables or disables the light
|
|
pub visibility: Visibility,
|
|
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
|
|
pub computed_visibility: ComputedVisibility,
|
|
}
|