use bevy_asset::Handle;1use bevy_camera::visibility::Visibility;2use bevy_ecs::prelude::*;3use bevy_image::Image;4use bevy_math::{Quat, UVec2};5use bevy_reflect::prelude::*;6use bevy_transform::components::Transform;78/// A marker component for a light probe, which is a cuboid region that provides9/// global illumination to all fragments inside it.10///11/// Note that a light probe will have no effect unless the entity contains some12/// kind of illumination, which can either be an [`EnvironmentMapLight`] or an13/// [`IrradianceVolume`].14///15/// The light probe range is conceptually a unit cube (1×1×1) centered on the16/// origin. The [`Transform`] applied to this entity can scale, rotate, or translate17/// that cube so that it contains all fragments that should take this light probe into account.18///19/// When multiple sources of indirect illumination can be applied to a fragment,20/// the highest-quality one is chosen. Diffuse and specular illumination are21/// considered separately, so, for example, Bevy may decide to sample the22/// diffuse illumination from an irradiance volume and the specular illumination23/// from a reflection probe. From highest priority to lowest priority, the24/// ranking is as follows:25///26/// | Rank | Diffuse | Specular |27/// | ---- | -------------------- | -------------------- |28/// | 1 | Lightmap | Lightmap |29/// | 2 | Irradiance volume | Reflection probe |30/// | 3 | Reflection probe | View environment map |31/// | 4 | View environment map | |32///33/// Note that ambient light is always added to the diffuse component and does34/// not participate in the ranking. That is, ambient light is applied in35/// addition to, not instead of, the light sources above.36///37/// A terminology note: Unfortunately, there is little agreement across game and38/// graphics engines as to what to call the various techniques that Bevy groups39/// under the term *light probe*. In Bevy, a *light probe* is the generic term40/// that encompasses both *reflection probes* and *irradiance volumes*. In41/// object-oriented terms, *light probe* is the superclass, and *reflection42/// probe* and *irradiance volume* are subclasses. In other engines, you may see43/// the term *light probe* refer to an irradiance volume with a single voxel, or44/// perhaps some other technique, while in Bevy *light probe* refers not to a45/// specific technique but rather to a class of techniques. Developers familiar46/// with other engines should be aware of this terminology difference.47#[derive(Component, Debug, Clone, Copy, Default, Reflect)]48#[reflect(Component, Default, Debug, Clone)]49#[require(Transform, Visibility)]50pub struct LightProbe;5152impl LightProbe {53/// Creates a new light probe component.54#[inline]55pub fn new() -> Self {56Self57}58}5960/// A pair of cubemap textures that represent the surroundings of a specific61/// area in space.62///63/// See `bevy_pbr::environment_map` for detailed information.64#[derive(Clone, Component, Reflect)]65#[reflect(Component, Default, Clone)]66pub struct EnvironmentMapLight {67/// The blurry image that represents diffuse radiance surrounding a region.68pub diffuse_map: Handle<Image>,6970/// The typically-sharper, mipmapped image that represents specular radiance71/// surrounding a region.72pub specular_map: Handle<Image>,7374/// Scale factor applied to the diffuse and specular light generated by this component.75///76/// After applying this multiplier, the resulting values should77/// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).78///79/// See also <https://google.github.io/filament/Filament.html#lighting/imagebasedlights/iblunit>.80pub intensity: f32,8182/// World space rotation applied to the environment light cubemaps.83/// This is useful for users who require a different axis, such as the Z-axis, to serve84/// as the vertical axis.85pub rotation: Quat,8687/// Whether the light from this environment map contributes diffuse lighting88/// to meshes with lightmaps.89///90/// Set this to false if your lightmap baking tool bakes the diffuse light91/// from this environment light into the lightmaps in order to avoid92/// counting the radiance from this environment map twice.93///94/// By default, this is set to true.95pub affects_lightmapped_mesh_diffuse: bool,96}9798impl Default for EnvironmentMapLight {99fn default() -> Self {100EnvironmentMapLight {101diffuse_map: Handle::default(),102specular_map: Handle::default(),103intensity: 0.0,104rotation: Quat::IDENTITY,105affects_lightmapped_mesh_diffuse: true,106}107}108}109110/// A generated environment map that is filtered at runtime.111///112/// See `bevy_pbr::light_probe::generate` for detailed information.113#[derive(Clone, Component, Reflect)]114#[reflect(Component, Default, Clone)]115pub struct GeneratedEnvironmentMapLight {116/// Source cubemap to be filtered on the GPU, size must be a power of two.117pub environment_map: Handle<Image>,118119/// Scale factor applied to the diffuse and specular light generated by this120/// component. Expressed in cd/m² (candela per square meter).121pub intensity: f32,122123/// World-space rotation applied to the cubemap.124pub rotation: Quat,125126/// Whether this light contributes diffuse lighting to meshes that already127/// have baked lightmaps.128pub affects_lightmapped_mesh_diffuse: bool,129}130131impl Default for GeneratedEnvironmentMapLight {132fn default() -> Self {133GeneratedEnvironmentMapLight {134environment_map: Handle::default(),135intensity: 0.0,136rotation: Quat::IDENTITY,137affects_lightmapped_mesh_diffuse: true,138}139}140}141142/// Lets the atmosphere contribute environment lighting (reflections and ambient diffuse) to your scene.143///144/// Attach this to a [`Camera3d`](bevy_camera::Camera3d) to light the entire view, or to a145/// [`LightProbe`] to light only a specific region.146/// Behind the scenes, this generates an environment map from the atmosphere for image-based lighting147/// and inserts a corresponding [`GeneratedEnvironmentMapLight`].148///149/// For HDRI-based lighting, use a preauthored [`EnvironmentMapLight`] or filter one at runtime with150/// [`GeneratedEnvironmentMapLight`].151#[derive(Component, Clone)]152pub struct AtmosphereEnvironmentMapLight {153/// Controls how bright the atmosphere's environment lighting is.154/// Increase this value to brighten reflections and ambient diffuse lighting.155///156/// The default is `1.0` so that the generated environment lighting matches157/// the light intensity of the atmosphere in the scene.158pub intensity: f32,159/// Whether the diffuse contribution should affect meshes that already have lightmaps.160pub affects_lightmapped_mesh_diffuse: bool,161/// Cubemap resolution in pixels (must be a power-of-two).162pub size: UVec2,163}164165impl Default for AtmosphereEnvironmentMapLight {166fn default() -> Self {167Self {168intensity: 1.0,169affects_lightmapped_mesh_diffuse: true,170size: UVec2::new(512, 512),171}172}173}174175/// The component that defines an irradiance volume.176///177/// See `bevy_pbr::irradiance_volume` for detailed information.178///179/// This component requires the [`LightProbe`] component, and is typically used with180/// [`bevy_transform::components::Transform`] to place the volume appropriately.181#[derive(Clone, Reflect, Component, Debug)]182#[reflect(Component, Default, Debug, Clone)]183#[require(LightProbe)]184pub struct IrradianceVolume {185/// The 3D texture that represents the ambient cubes, encoded in the format186/// described in `bevy_pbr::irradiance_volume`.187pub voxels: Handle<Image>,188189/// Scale factor applied to the diffuse and specular light generated by this component.190///191/// After applying this multiplier, the resulting values should192/// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).193///194/// See also <https://google.github.io/filament/Filament.html#lighting/imagebasedlights/iblunit>.195pub intensity: f32,196197/// Whether the light from this irradiance volume has an effect on meshes198/// with lightmaps.199///200/// Set this to false if your lightmap baking tool bakes the light from this201/// irradiance volume into the lightmaps in order to avoid counting the202/// irradiance twice. Frequently, applications use irradiance volumes as a203/// lower-quality alternative to lightmaps for capturing indirect204/// illumination on dynamic objects, and such applications will want to set205/// this value to false.206///207/// By default, this is set to true.208pub affects_lightmapped_meshes: bool,209}210211impl Default for IrradianceVolume {212#[inline]213fn default() -> Self {214IrradianceVolume {215voxels: Handle::default(),216intensity: 0.0,217affects_lightmapped_meshes: true,218}219}220}221222223