bevy_light/probe.rs
1use bevy_asset::{Assets, Handle, HandleTemplate, RenderAssetUsages};
2use bevy_camera::visibility::{self, ViewVisibility, Visibility, VisibilityClass};
3use bevy_color::{Color, ColorToComponents, LinearRgba};
4use bevy_ecs::prelude::*;
5use bevy_ecs::template::{FromTemplate, OptionTemplate};
6use bevy_image::Image;
7use bevy_math::{Quat, UVec2, Vec3};
8use bevy_reflect::prelude::*;
9use bevy_transform::components::Transform;
10use wgpu_types::{
11 Extent3d, TextureDimension, TextureFormat, TextureViewDescriptor, TextureViewDimension,
12};
13
14use crate::cluster::ClusterVisibilityClass;
15
16/// A marker component for a light probe, which is a cuboid region that provides
17/// global illumination to all fragments inside it.
18///
19/// Note that a light probe will have no effect unless the entity contains some
20/// kind of illumination, which can either be an [`EnvironmentMapLight`] or an
21/// [`IrradianceVolume`].
22///
23/// The light probe range is conceptually a unit cube (1×1×1) centered on the
24/// origin. The [`Transform`] applied to this entity can scale, rotate, or
25/// translate that cube so that it contains all fragments that should take this
26/// light probe into account.
27///
28/// Light probes may specify a *falloff* range over which their influence tapers
29/// off. The falloff range is expressed as a range from 0, representing
30/// infinitely-sharp falloff, to 1, representing the most gradual falloff,
31/// *inside* the 1×1×1 cube. So, for example, if you set the falloff to 0.5 on
32/// an axis, then any fragments with positions between 0.0 units to 0.25 units
33/// on that axis will receive 100% influence from the light probe, while
34/// fragments with positions between 0.25 units to 0.5 units on that axis will
35/// receive gradually-diminished influence, and fragments more than 0.5 units
36/// from the center of the light probe will receive no influence at all.
37///
38/// When multiple sources of indirect illumination can be applied to a fragment,
39/// the highest-quality ones are chosen. Diffuse and specular illumination are
40/// considered separately, so, for example, Bevy may decide to sample the
41/// diffuse illumination from an irradiance volume and the specular illumination
42/// from a reflection probe. From highest priority to lowest priority, the
43/// ranking is as follows:
44///
45/// | Rank | Diffuse | Specular |
46/// | ---- | -------------------- | -------------------- |
47/// | 1 | Lightmap | Lightmap |
48/// | 2 | Irradiance volume | Reflection probe |
49/// | 3 | Reflection probe | View environment map |
50/// | 4 | View environment map | |
51///
52/// Note that ambient light is always added to the diffuse component and does
53/// not participate in the ranking. That is, ambient light is applied in
54/// addition to, not instead of, the light sources above.
55///
56/// Multiple light probes of the same type can apply to a single fragment. By
57/// setting falloff regions appropriately, one can achieve a gradual blend from
58/// one reflection probe and/or irradiance volume to another as objects move
59/// between them.
60///
61/// A terminology note: Unfortunately, there is little agreement across game and
62/// graphics engines as to what to call the various techniques that Bevy groups
63/// under the term *light probe*. In Bevy, a *light probe* is the generic term
64/// that encompasses both *reflection probes* and *irradiance volumes*. In
65/// object-oriented terms, *light probe* is the superclass, and *reflection
66/// probe* and *irradiance volume* are subclasses. In other engines, you may see
67/// the term *light probe* refer to an irradiance volume with a single voxel, or
68/// perhaps some other technique, while in Bevy *light probe* refers not to a
69/// specific technique but rather to a class of techniques. Developers familiar
70/// with other engines should be aware of this terminology difference.
71#[derive(Component, Debug, Clone, Copy, Default, Reflect)]
72#[reflect(Component, Default, Debug, Clone)]
73#[require(Transform, ViewVisibility, Visibility, VisibilityClass)]
74#[component(on_add = visibility::add_visibility_class::<ClusterVisibilityClass>)]
75pub struct LightProbe {
76 /// The distance over which the effect of the light probe becomes weaker, on
77 /// each axis.
78 ///
79 /// This is specified as a ratio of the total distance on each axis. So, for
80 /// example, if you specify `Vec3::splat(0.25)` here, then the light probe
81 /// will consist of a 0.75×0.75×0.75 unit cube within which fragments
82 /// receive the maximum influence from the light probe, contained within a
83 /// 1×1×1 cube which influences fragments inside it in a manner that
84 /// diminishes as fragments get farther from its center.
85 ///
86 /// Falloff doesn't affect the influence range of the light probe itself;
87 /// it's still conceptually a 1×1×1 cube, regardless of the falloff setting.
88 /// In other words, falloff modifies the *interior* of the light probe cube
89 /// instead of increasing the *exterior* boundaries of the cube.
90 pub falloff: Vec3,
91}
92
93impl LightProbe {
94 /// Creates a new light probe component.
95 #[inline]
96 pub fn new() -> Self {
97 Self::default()
98 }
99}
100
101/// A pair of cubemap textures that represent the surroundings of a specific
102/// area in space.
103///
104/// See `bevy_pbr::environment_map` for detailed information.
105#[derive(Clone, Component, Reflect, FromTemplate)]
106#[reflect(Component, Default, Clone)]
107pub struct EnvironmentMapLight {
108 /// The blurry image that represents diffuse radiance surrounding a region.
109 pub diffuse_map: Handle<Image>,
110
111 /// The typically-sharper, mipmapped image that represents specular radiance
112 /// surrounding a region.
113 pub specular_map: Handle<Image>,
114
115 /// Scale factor applied to the diffuse and specular light generated by this component.
116 ///
117 /// After applying this multiplier, the resulting values should
118 /// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).
119 ///
120 /// See also <https://google.github.io/filament/Filament.md.html#lighting/imagebasedlights/iblunit>.
121 pub intensity: f32,
122
123 /// World space rotation applied to the environment light cubemaps.
124 /// This is useful for users who require a different axis, such as the Z-axis, to serve
125 /// as the vertical axis.
126 ///
127 /// Note: This only has an effect if attached to a view.
128 pub rotation: Quat,
129
130 /// Whether the light from this environment map contributes diffuse lighting
131 /// to meshes with lightmaps.
132 ///
133 /// Set this to false if your lightmap baking tool bakes the diffuse light
134 /// from this environment light into the lightmaps in order to avoid
135 /// counting the radiance from this environment map twice.
136 ///
137 /// By default, this is set to true.
138 pub affects_lightmapped_mesh_diffuse: bool,
139}
140
141impl EnvironmentMapLight {
142 /// An environment map with a uniform color, useful for uniform ambient lighting.
143 pub fn solid_color(assets: &mut Assets<Image>, color: impl Into<Color>) -> Self {
144 let color = color.into();
145 Self::hemispherical_gradient(assets, color, color, color)
146 }
147
148 /// An environment map with a hemispherical gradient, fading between the sky and ground colors
149 /// at the horizon. Useful as a very simple 'sky'.
150 pub fn hemispherical_gradient(
151 assets: &mut Assets<Image>,
152 top_color: impl Into<Color>,
153 mid_color: impl Into<Color>,
154 bottom_color: impl Into<Color>,
155 ) -> Self {
156 let handle = assets.add(Self::hemispherical_gradient_cubemap(
157 top_color.into(),
158 mid_color.into(),
159 bottom_color.into(),
160 ));
161
162 Self {
163 diffuse_map: handle.clone(),
164 specular_map: handle,
165 intensity: 1.0,
166 ..Default::default()
167 }
168 }
169
170 pub(crate) fn hemispherical_gradient_cubemap(
171 top_color: Color,
172 mid_color: Color,
173 bottom_color: Color,
174 ) -> Image {
175 let top_color: LinearRgba = top_color.into();
176 let mid_color: LinearRgba = mid_color.into();
177 let bottom_color: LinearRgba = bottom_color.into();
178 Image {
179 texture_view_descriptor: Some(TextureViewDescriptor {
180 dimension: Some(TextureViewDimension::Cube),
181 ..Default::default()
182 }),
183 ..Image::new(
184 Extent3d {
185 width: 1,
186 height: 1,
187 depth_or_array_layers: 6,
188 },
189 TextureDimension::D2,
190 [
191 mid_color,
192 mid_color,
193 top_color,
194 bottom_color,
195 mid_color,
196 mid_color,
197 ]
198 .into_iter()
199 .flat_map(|c| c.to_f32_array().map(half::f16::from_f32))
200 .flat_map(half::f16::to_le_bytes)
201 .collect(),
202 TextureFormat::Rgba16Float,
203 RenderAssetUsages::RENDER_WORLD,
204 )
205 }
206 }
207}
208
209impl Default for EnvironmentMapLight {
210 fn default() -> Self {
211 EnvironmentMapLight {
212 diffuse_map: Handle::default(),
213 specular_map: Handle::default(),
214 intensity: 0.0,
215 rotation: Quat::IDENTITY,
216 affects_lightmapped_mesh_diffuse: true,
217 }
218 }
219}
220
221/// Adds a skybox to a 3D camera, based on a cubemap texture.
222///
223/// Note that this component does not (currently) affect the scene's lighting.
224/// To do so, use [`EnvironmentMapLight`] alongside this component.
225///
226/// See also <https://en.wikipedia.org/wiki/Skybox_(video_games)>.
227#[derive(Component, Clone, Reflect, FromTemplate)]
228#[reflect(Component, Default, Clone)]
229pub struct Skybox {
230 /// The cubemap to use.
231 ///
232 /// If this is [`None`], the skybox will not be rendered, as if it does not exist.
233 /// This allows `Skybox` to implement [`Default`].
234 #[template(OptionTemplate<HandleTemplate<Image>>)]
235 pub image: Option<Handle<Image>>,
236
237 /// Scale factor applied to the skybox image.
238 /// After applying this multiplier to the image samples, the resulting values should
239 /// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).
240 pub brightness: f32,
241
242 /// View space rotation applied to the skybox cubemap.
243 /// This is useful for users who require a different axis, such as the Z-axis, to serve
244 /// as the vertical axis.
245 pub rotation: Quat,
246}
247
248impl Default for Skybox {
249 fn default() -> Self {
250 Skybox {
251 image: None,
252 brightness: 0.0,
253 rotation: Quat::IDENTITY,
254 }
255 }
256}
257
258/// A generated environment map that is filtered at runtime.
259///
260/// See `bevy_pbr::light_probe::generate` for detailed information.
261#[derive(Clone, Component, Reflect, FromTemplate)]
262#[reflect(Component, Default, Clone)]
263pub struct GeneratedEnvironmentMapLight {
264 /// Source cubemap to be filtered on the GPU, size must be a power of two.
265 pub environment_map: Handle<Image>,
266
267 /// Scale factor applied to the diffuse and specular light generated by this
268 /// component. Expressed in cd/m² (candela per square meter).
269 pub intensity: f32,
270
271 /// World-space rotation applied to the cubemap.
272 pub rotation: Quat,
273
274 /// Whether this light contributes diffuse lighting to meshes that already
275 /// have baked lightmaps.
276 pub affects_lightmapped_mesh_diffuse: bool,
277}
278
279impl Default for GeneratedEnvironmentMapLight {
280 fn default() -> Self {
281 GeneratedEnvironmentMapLight {
282 environment_map: Handle::default(),
283 intensity: 0.0,
284 rotation: Quat::IDENTITY,
285 affects_lightmapped_mesh_diffuse: true,
286 }
287 }
288}
289
290/// Lets the atmosphere contribute environment lighting (reflections and ambient diffuse) to your scene.
291///
292/// Attach this to a [`Camera3d`](bevy_camera::Camera3d) to light the entire view, or to a
293/// [`LightProbe`] to light only a specific region.
294/// Behind the scenes, this generates an environment map from the atmosphere for image-based lighting
295/// and inserts a corresponding [`GeneratedEnvironmentMapLight`].
296///
297/// For HDRI-based lighting, use a preauthored [`EnvironmentMapLight`] or filter one at runtime with
298/// [`GeneratedEnvironmentMapLight`].
299#[derive(Component, Clone)]
300pub struct AtmosphereEnvironmentMapLight {
301 /// Controls how bright the atmosphere's environment lighting is.
302 /// Increase this value to brighten reflections and ambient diffuse lighting.
303 ///
304 /// The default is `1.0` so that the generated environment lighting matches
305 /// the light intensity of the atmosphere in the scene.
306 pub intensity: f32,
307 /// Whether the diffuse contribution should affect meshes that already have lightmaps.
308 pub affects_lightmapped_mesh_diffuse: bool,
309 /// Cubemap resolution in pixels (must be a power-of-two).
310 pub size: UVec2,
311}
312
313impl Default for AtmosphereEnvironmentMapLight {
314 fn default() -> Self {
315 Self {
316 intensity: 1.0,
317 affects_lightmapped_mesh_diffuse: true,
318 size: UVec2::new(512, 512),
319 }
320 }
321}
322
323/// The component that defines an irradiance volume.
324///
325/// See `bevy_pbr::irradiance_volume` for detailed information.
326///
327/// This component requires the [`LightProbe`] component, and is typically used with
328/// [`bevy_transform::components::Transform`] to place the volume appropriately.
329#[derive(Clone, Reflect, Component, Debug, FromTemplate)]
330#[reflect(Component, Default, Debug, Clone)]
331#[require(LightProbe)]
332pub struct IrradianceVolume {
333 /// The 3D texture that represents the ambient cubes, encoded in the format
334 /// described in `bevy_pbr::irradiance_volume`.
335 pub voxels: Handle<Image>,
336
337 /// Scale factor applied to the diffuse and specular light generated by this component.
338 ///
339 /// After applying this multiplier, the resulting values should
340 /// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).
341 ///
342 /// See also <https://google.github.io/filament/Filament.md.html#lighting/imagebasedlights/iblunit>.
343 pub intensity: f32,
344
345 /// Whether the light from this irradiance volume has an effect on meshes
346 /// with lightmaps.
347 ///
348 /// Set this to false if your lightmap baking tool bakes the light from this
349 /// irradiance volume into the lightmaps in order to avoid counting the
350 /// irradiance twice. Frequently, applications use irradiance volumes as a
351 /// lower-quality alternative to lightmaps for capturing indirect
352 /// illumination on dynamic objects, and such applications will want to set
353 /// this value to false.
354 ///
355 /// By default, this is set to true.
356 pub affects_lightmapped_meshes: bool,
357}
358
359impl Default for IrradianceVolume {
360 #[inline]
361 fn default() -> Self {
362 IrradianceVolume {
363 voxels: Handle::default(),
364 intensity: 0.0,
365 affects_lightmapped_meshes: true,
366 }
367 }
368}
369
370/// Add this component to a reflection probe to customize *parallax correction*.
371///
372/// For environment maps added directly to a camera, Bevy renders the reflected
373/// scene that a cubemap captures as though it were infinitely far away. This is
374/// acceptable if the cubemap captures very distant objects, such as distant
375/// mountains in outdoor scenes. It's less ideal, however, if the cubemap
376/// reflects near objects, such as the interior of a room. Therefore, by default
377/// for reflection probes Bevy uses *parallax-corrected cubemaps* (PCCM), which
378/// causes Bevy to treat the reflected scene as though it coincided with the
379/// boundaries of the light probe.
380///
381/// As an example, for indoor scenes, it's common to place reflection probes
382/// inside each room and to make the boundaries of the reflection probe (as
383/// determined by the light probe's [`bevy_transform::components::Transform`])
384/// coincide with the walls of the room. That way, the reflection probes will
385/// (1) apply to the objects inside the room and (2) take the positions of those
386/// objects into account in order to create a realistic reflection.
387///
388/// Instead of having the simulated boundaries of the reflected area coincide
389/// with the boundaries of the light probe, it's also possible to specify
390/// *custom* parallax correction boundaries, so that the region of influence of
391/// the light probe doesn't correspond with the simulated boundaries used for
392/// parallax correction. This is commonly used when the boundaries of the light
393/// probe are slightly larger than the room that the light probe contains, for
394/// instance in order to avoid artifacts along the edges of the room that occur
395/// due to rounding error, or else when the *falloff* feature is used that
396/// blends reflection probes into adjacent ones.
397///
398/// Place this component on an entity that has a [`LightProbe`] and
399/// [`EnvironmentMapLight`] component in order to either (1) opt out of parallax
400/// correction via [`ParallaxCorrection::None`] or (2) specify custom parallax
401/// correction boundaries via [`ParallaxCorrection::Custom`]. If you don't
402/// manually place this component on a reflection probe, Bevy will automatically
403/// add a [`ParallaxCorrection::Auto`] component so that the boundaries of the
404/// light probe will coincide with the simulated boundaries used for parallax
405/// correction.
406///
407/// See the `pccm` example for an example of usage of parallax-corrected
408/// cubemaps and the `light_probe_blending` example for an example of use of
409/// custom parallax correction boundaries.
410#[derive(Clone, Copy, Default, Component, Reflect)]
411#[reflect(Clone, Default, Component)]
412pub enum ParallaxCorrection {
413 /// No parallax correction is used.
414 ///
415 /// This component causes Bevy to render the reflection as though the
416 /// reflected surface were infinitely distant.
417 None,
418
419 /// The parallax correction boundaries correspond with the boundaries of the
420 /// light probe.
421 ///
422 /// This is the default value. Bevy automatically adds this component value
423 /// to reflection probes that don't have a [`ParallaxCorrection`] component.
424 /// It's equivalent to `ParallaxCorrection::Custom(Vec3::splat(0.5))`.
425 #[default]
426 Auto,
427
428 /// The parallax correction boundaries are specified manually.
429 ///
430 /// The simulated reflection boundaries are specified as an axis-aligned
431 /// cube *in light probe space* with the given *half* extents. Thus, for
432 /// example, if you set the parallax correction boundaries to `vec3(0.5,
433 /// 1.0, 2.0)` and the scale of the light probe is `vec3(3.0, 3.0, 3.0)`,
434 /// then the simulated boundaries of the reflected area used for parallax
435 /// correction will be centered on the reflection probe with a width of 3.0
436 /// m, a height of 6.0 m, and a depth of 12.0 m.
437 Custom(Vec3),
438}
439
440/// A system that automatically adds a [`ParallaxCorrection::Auto`] component to
441/// any reflection probe that doesn't already have a [`ParallaxCorrection`]
442/// component.
443///
444/// A reflection probe is any entity with both an [`EnvironmentMapLight`] and a
445/// [`LightProbe`] component.
446pub fn automatically_add_parallax_correction_components(
447 mut commands: Commands,
448 query: Query<
449 Entity,
450 (
451 With<EnvironmentMapLight>,
452 With<LightProbe>,
453 Without<ParallaxCorrection>,
454 ),
455 >,
456) {
457 for entity in &query {
458 commands
459 .entity(entity)
460 .insert(ParallaxCorrection::default());
461 }
462}