Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Physical Shadow Softness Parameter #16698

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
36 changes: 27 additions & 9 deletions crates/bevy_pbr/src/cluster/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use bevy_utils::{prelude::default, tracing::warn};

use crate::{
prelude::EnvironmentMapLight, ClusterConfig, ClusterFarZMode, Clusters, ExtractedPointLight,
GlobalVisibleClusterableObjects, LightProbe, PointLight, SpotLight, ViewClusterBindings,
VisibleClusterableObjects, VolumetricLight, CLUSTERED_FORWARD_STORAGE_BUFFER_COUNT,
MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS,
GlobalVisibleClusterableObjects, LightProbe, ShadowsStyle, PointLight, SpotLight,
ViewClusterBindings, VisibleClusterableObjects, VolumetricLight,
CLUSTERED_FORWARD_STORAGE_BUFFER_COUNT, MAX_UNIFORM_BUFFER_CLUSTERABLE_OBJECTS,
};

use super::ClusterableObjectOrderData;
Expand Down Expand Up @@ -124,11 +124,11 @@ impl ClusterableObjectType {
match point_light.spot_light_angles {
Some((_, outer_angle)) => ClusterableObjectType::SpotLight {
outer_angle,
shadows_enabled: point_light.shadows_enabled,
shadows_enabled: point_light.shadows.enabled(),
volumetric: point_light.volumetric,
},
None => ClusterableObjectType::PointLight {
shadows_enabled: point_light.shadows_enabled,
shadows_enabled: point_light.shadows.enabled(),
volumetric: point_light.volumetric,
},
}
Expand All @@ -154,6 +154,7 @@ pub(crate) fn assign_objects_to_clusters(
Entity,
&GlobalTransform,
&PointLight,
&ShadowsStyle,
Option<&RenderLayers>,
Option<&VolumetricLight>,
&ViewVisibility,
Expand All @@ -162,6 +163,7 @@ pub(crate) fn assign_objects_to_clusters(
Entity,
&GlobalTransform,
&SpotLight,
&ShadowsStyle,
Option<&RenderLayers>,
Option<&VolumetricLight>,
&ViewVisibility,
Expand All @@ -187,13 +189,21 @@ pub(crate) fn assign_objects_to_clusters(
.iter()
.filter(|(.., visibility)| visibility.get())
.map(
|(entity, transform, point_light, maybe_layers, volumetric, _visibility)| {
|(
entity,
transform,
point_light,
point_light_shadows,
maybe_layers,
volumetric,
_visibility,
)| {
ClusterableObjectAssignmentData {
entity,
transform: GlobalTransform::from_translation(transform.translation()),
range: point_light.range,
object_type: ClusterableObjectType::PointLight {
shadows_enabled: point_light.shadows_enabled,
shadows_enabled: point_light_shadows.enabled(),
volumetric: volumetric.is_some(),
},
render_layers: maybe_layers.unwrap_or_default().clone(),
Expand All @@ -206,14 +216,22 @@ pub(crate) fn assign_objects_to_clusters(
.iter()
.filter(|(.., visibility)| visibility.get())
.map(
|(entity, transform, spot_light, maybe_layers, volumetric, _visibility)| {
|(
entity,
transform,
spot_light,
spot_light_shadows,
maybe_layers,
volumetric,
_visibility,
)| {
ClusterableObjectAssignmentData {
entity,
transform: *transform,
range: spot_light.range,
object_type: ClusterableObjectType::SpotLight {
outer_angle: spot_light.outer_angle,
shadows_enabled: spot_light.shadows_enabled,
shadows_enabled: spot_light_shadows.enabled(),
volumetric: volumetric.is_some(),
},
render_layers: maybe_layers.unwrap_or_default().clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub mod prelude {
SpotLightBundle,
},
fog::{DistanceFog, FogFalloff},
light::{light_consts, AmbientLight, DirectionalLight, PointLight, SpotLight},
light::{light_consts, AmbientLight, DirectionalLight, PointLight, SpotLight, ShadowsStyle},
light_probe::{
environment_map::{EnvironmentMapLight, ReflectionProbeBundle},
LightProbe,
Expand Down
41 changes: 17 additions & 24 deletions crates/bevy_pbr/src/light/directional_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use super::*;
CascadesFrusta,
CascadeShadowConfig,
CascadesVisibleEntities,
LightShadows,
Transform,
Visibility
)]
Expand All @@ -73,30 +74,23 @@ pub struct DirectionalLight {
/// area.
pub illuminance: f32,

/// Whether this light casts shadows.
/// The angular size of this light in radians. This must be a value in the
/// range [0, π).
///
/// Note that shadows are rather expensive and become more so with every
/// light that casts them. In general, it's best to aggressively limit the
/// number of lights with shadows enabled to one or two at most.
pub shadows_enabled: bool,

/// Whether soft shadows are enabled, and if so, the size of the light.
///
/// Soft shadows, also known as *percentage-closer soft shadows* or PCSS,
/// cause shadows to become blurrier (i.e. their penumbra increases in
/// radius) as they extend away from objects. The blurriness of the shadow
/// depends on the size of the light; larger lights result in larger
/// penumbras and therefore blurrier shadows.
/// The angular size of an object is how large it appears in one's field
/// of view. For example, if viewed from the surface of the Earth, if one
/// were to draw a line (specifically a *great circle*]) across the sky
/// through the center of the Sun, the Sun would cover roughly 0.5 degrees
/// of that circle, or 8.73e-3 radians.
///
/// Currently, soft shadows are rather noisy if not using the temporal mode.
/// If you enable soft shadows, consider choosing
/// [`ShadowFilteringMethod::Temporal`] and enabling temporal antialiasing
/// (TAA) to smooth the noise out over time.
/// This value controls the radius of soft shadow penumbras, as well as
/// some volumetric lighting effects. See [`LightShadows`] for more
/// information on soft shadows.
///
/// Note that soft shadows are significantly more expensive to render than
/// hard shadows.
#[cfg(feature = "experimental_pbr_pcss")]
pub soft_shadow_size: Option<f32>,
/// Note that this is not the same thing as the *solid angle* (or "angular
/// area", roughly) that this light covers in the sky. That is a separate
/// measurement, with units of *steradians* rather than radians.
pub angular_size: f32,

/// A value that adjusts the tradeoff between self-shadowing artifacts and
/// proximity of shadows to their casters.
Expand All @@ -120,16 +114,15 @@ impl Default for DirectionalLight {
DirectionalLight {
color: Color::WHITE,
illuminance: light_consts::lux::AMBIENT_DAYLIGHT,
shadows_enabled: false,
angular_size: Self::SUN_ANGULAR_SIZE,
shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,
shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,
#[cfg(feature = "experimental_pbr_pcss")]
soft_shadow_size: None,
}
}
}

impl DirectionalLight {
pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 1.8;
pub const SUN_ANGULAR_SIZE: f32 = 8.72665e-3;
}
Loading
Loading