use bevy_asset::Handle;1use bevy_camera::{2primitives::Frustum,3visibility::{self, Visibility, VisibilityClass, VisibleMeshEntities},4};5use bevy_color::Color;6use bevy_ecs::prelude::*;7use bevy_image::Image;8use bevy_math::{Affine3A, Dir3, Mat3, Mat4, Vec3};9use bevy_reflect::prelude::*;10use bevy_transform::components::{GlobalTransform, Transform};1112use crate::cluster::{ClusterVisibilityClass, GlobalVisibleClusterableObjects};1314/// A light that emits light in a given direction from a central point.15///16/// Behaves like a point light in a perfectly absorbent housing that17/// shines light only in a given direction. The direction is taken from18/// the transform, and can be specified with [`Transform::looking_at`](Transform::looking_at).19///20/// To control the resolution of the shadow maps, use the [`crate::DirectionalLightShadowMap`] resource.21#[derive(Component, Debug, Clone, Copy, Reflect)]22#[reflect(Component, Default, Debug, Clone)]23#[require(Frustum, VisibleMeshEntities, Transform, Visibility, VisibilityClass)]24#[component(on_add = visibility::add_visibility_class::<ClusterVisibilityClass>)]25pub struct SpotLight {26/// The color of the light.27///28/// By default, this is white.29pub color: Color,3031/// Luminous power in lumens, representing the amount of light emitted by this source in all directions.32pub intensity: f32,3334/// Range in meters that this light illuminates.35///36/// Note that this value affects resolution of the shadow maps; generally, the37/// higher you set it, the lower-resolution your shadow maps will be.38/// Consequently, you should set this value to be only the size that you need.39pub range: f32,4041/// Simulates a light source coming from a spherical volume with the given42/// radius.43///44/// This affects the size of specular highlights created by this light, as45/// well as the soft shadow penumbra size. Because of this, large values may46/// not produce the intended result -- for example, light radius does not47/// affect shadow softness or diffuse lighting.48pub radius: f32,4950/// Whether this light casts shadows.51///52/// Note that shadows are rather expensive and become more so with every53/// light that casts them. In general, it's best to aggressively limit the54/// number of lights with shadows enabled to one or two at most.55pub shadows_enabled: bool,5657/// Whether soft shadows are enabled.58///59/// Soft shadows, also known as *percentage-closer soft shadows* or PCSS,60/// cause shadows to become blurrier (i.e. their penumbra increases in61/// radius) as they extend away from objects. The blurriness of the shadow62/// depends on the [`SpotLight::radius`] of the light; larger lights result in larger63/// penumbras and therefore blurrier shadows.64///65/// Currently, soft shadows are rather noisy if not using the temporal mode.66/// If you enable soft shadows, consider choosing67/// [`ShadowFilteringMethod::Temporal`] and enabling temporal antialiasing68/// (TAA) to smooth the noise out over time.69///70/// Note that soft shadows are significantly more expensive to render than71/// hard shadows.72///73/// [`ShadowFilteringMethod::Temporal`]: crate::ShadowFilteringMethod::Temporal74#[cfg(feature = "experimental_pbr_pcss")]75pub soft_shadows_enabled: bool,7677/// Whether this spot light contributes diffuse lighting to meshes with78/// lightmaps.79///80/// Set this to false if your lightmap baking tool bakes the direct diffuse81/// light from this directional light into the lightmaps in order to avoid82/// counting the radiance from this light twice. Note that the specular83/// portion of the light is always considered, because Bevy currently has no84/// means to bake specular light.85///86/// By default, this is set to true.87pub affects_lightmapped_mesh_diffuse: bool,8889/// A value that adjusts the tradeoff between self-shadowing artifacts and90/// proximity of shadows to their casters.91///92/// This value frequently must be tuned to the specific scene; this is93/// normal and a well-known part of the shadow mapping workflow. If set too94/// low, unsightly shadow patterns appear on objects not in shadow as95/// objects incorrectly cast shadows on themselves, known as *shadow acne*.96/// If set too high, shadows detach from the objects casting them and seem97/// to "fly" off the objects, known as *Peter Panning*.98pub shadow_depth_bias: f32,99100/// A bias applied along the direction of the fragment's surface normal. It is scaled to the101/// shadow map's texel size so that it can be small close to the camera and gets larger further102/// away.103pub shadow_normal_bias: f32,104105/// The distance from the light to the near Z plane in the shadow map.106///107/// Objects closer than this distance to the light won't cast shadows.108/// Setting this higher increases the shadow map's precision.109///110/// This only has an effect if shadows are enabled.111pub shadow_map_near_z: f32,112113/// Angle defining the distance from the spot light direction to the outer limit114/// of the light's cone of effect.115/// `outer_angle` should be < `PI / 2.0`.116/// `PI / 2.0` defines a hemispherical spot light, but shadows become very blocky as the angle117/// approaches this limit.118pub outer_angle: f32,119120/// Angle defining the distance from the spot light direction to the inner limit121/// of the light's cone of effect.122/// Light is attenuated from `inner_angle` to `outer_angle` to give a smooth falloff.123/// `inner_angle` should be <= `outer_angle`124pub inner_angle: f32,125}126127impl SpotLight {128pub const DEFAULT_SHADOW_DEPTH_BIAS: f32 = 0.02;129pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 1.8;130pub const DEFAULT_SHADOW_MAP_NEAR_Z: f32 = 0.1;131}132133impl Default for SpotLight {134fn default() -> Self {135// a quarter arc attenuating from the center136Self {137color: Color::WHITE,138// 1,000,000 lumens is a very large "cinema light" capable of registering brightly at Bevy's139// default "very overcast day" exposure level. For "indoor lighting" with a lower exposure,140// this would be way too bright.141intensity: 1_000_000.0,142range: 20.0,143radius: 0.0,144shadows_enabled: false,145affects_lightmapped_mesh_diffuse: true,146shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,147shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,148shadow_map_near_z: Self::DEFAULT_SHADOW_MAP_NEAR_Z,149inner_angle: 0.0,150outer_angle: core::f32::consts::FRAC_PI_4,151#[cfg(feature = "experimental_pbr_pcss")]152soft_shadows_enabled: false,153}154}155}156157/// Constructs a right-handed orthonormal basis from a given unit Z vector.158///159/// This method of constructing a basis from a [`Vec3`] is used by [`bevy_math::Vec3::any_orthonormal_pair`]160// we will also construct it in the fragment shader and need our implementations to match exactly,161// so we reproduce it here to avoid a mismatch if glam changes.162// See bevy_render/maths.wgsl:orthonormalize163pub fn orthonormalize(z_basis: Dir3) -> Mat3 {164let sign = 1f32.copysign(z_basis.z);165let a = -1.0 / (sign + z_basis.z);166let b = z_basis.x * z_basis.y * a;167let x_basis = Vec3::new(1681.0 + sign * z_basis.x * z_basis.x * a,169sign * b,170-sign * z_basis.x,171);172let y_basis = Vec3::new(b, sign + z_basis.y * z_basis.y * a, -z_basis.y);173Mat3::from_cols(x_basis, y_basis, z_basis.into())174}175/// Constructs a right-handed orthonormal basis with translation, using only the forward direction and translation of a given [`GlobalTransform`].176///177/// This is a version of [`orthonormalize`] which also includes translation.178pub fn spot_light_world_from_view(transform: &GlobalTransform) -> Affine3A {179// the matrix z_local (opposite of transform.forward())180let fwd_dir = transform.back();181182let basis = orthonormalize(fwd_dir);183Affine3A::from_mat3_translation(basis, transform.translation())184}185186pub fn spot_light_clip_from_view(angle: f32, near_z: f32) -> Mat4 {187// spot light projection FOV is 2x the angle from spot light center to outer edge188Mat4::perspective_infinite_reverse_rh(angle * 2.0, 1.0, near_z)189}190191/// Add to a [`SpotLight`] to add a light texture effect.192/// A texture mask is applied to the light source to modulate its intensity,193/// simulating patterns like window shadows, gobo/cookie effects, or soft falloffs.194#[derive(Clone, Component, Debug, Reflect)]195#[reflect(Component, Debug)]196#[require(SpotLight)]197pub struct SpotLightTexture {198/// The texture image. Only the R channel is read.199/// Note the border of the image should be entirely black to avoid leaking light.200pub image: Handle<Image>,201}202203pub fn update_spot_light_frusta(204global_lights: Res<GlobalVisibleClusterableObjects>,205mut views: Query<206(Entity, &GlobalTransform, &SpotLight, &mut Frustum),207Or<(Changed<GlobalTransform>, Changed<SpotLight>)>,208>,209) {210for (entity, transform, spot_light, mut frustum) in &mut views {211// The frusta are used for culling meshes to the light for shadow mapping212// so if shadow mapping is disabled for this light, then the frusta are213// not needed.214// Also, if the light is not relevant for any cluster, it will not be in the215// global lights set and so there is no need to update its frusta.216if !spot_light.shadows_enabled || !global_lights.entities.contains(&entity) {217continue;218}219220// ignore scale because we don't want to effectively scale light radius and range221// by applying those as a view transform to shadow map rendering of objects222let view_backward = transform.back();223224let spot_world_from_view = spot_light_world_from_view(transform);225let spot_clip_from_view =226spot_light_clip_from_view(spot_light.outer_angle, spot_light.shadow_map_near_z);227let clip_from_world = spot_clip_from_view * spot_world_from_view.inverse();228229*frustum = Frustum::from_clip_from_world_custom_far(230&clip_from_world,231&transform.translation(),232&view_backward,233spot_light.range,234);235}236}237238239