bevy/examples/3d
Patrick Walton 83d6600267
Implement minimal reflection probes (fixed macOS, iOS, and Android). (#11366)
This pull request re-submits #10057, which was backed out for breaking
macOS, iOS, and Android. I've tested this version on macOS and Android
and on the iOS simulator.

# Objective

This pull request implements *reflection probes*, which generalize
environment maps to allow for multiple environment maps in the same
scene, each of which has an axis-aligned bounding box. This is a
standard feature of physically-based renderers and was inspired by [the
corresponding feature in Blender's Eevee renderer].

## Solution

This is a minimal implementation of reflection probes that allows
artists to define cuboid bounding regions associated with environment
maps. For every view, on every frame, a system builds up a list of the
nearest 4 reflection probes that are within the view's frustum and
supplies that list to the shader. The PBR fragment shader searches
through the list, finds the first containing reflection probe, and uses
it for indirect lighting, falling back to the view's environment map if
none is found. Both forward and deferred renderers are fully supported.

A reflection probe is an entity with a pair of components, *LightProbe*
and *EnvironmentMapLight* (as well as the standard *SpatialBundle*, to
position it in the world). The *LightProbe* component (along with the
*Transform*) defines the bounding region, while the
*EnvironmentMapLight* component specifies the associated diffuse and
specular cubemaps.

A frequent question is "why two components instead of just one?" The
advantages of this setup are:

1. It's readily extensible to other types of light probes, in particular
*irradiance volumes* (also known as ambient cubes or voxel global
illumination), which use the same approach of bounding cuboids. With a
single component that applies to both reflection probes and irradiance
volumes, we can share the logic that implements falloff and blending
between multiple light probes between both of those features.

2. It reduces duplication between the existing *EnvironmentMapLight* and
these new reflection probes. Systems can treat environment maps attached
to cameras the same way they treat environment maps applied to
reflection probes if they wish.

Internally, we gather up all environment maps in the scene and place
them in a cubemap array. At present, this means that all environment
maps must have the same size, mipmap count, and texture format. A
warning is emitted if this restriction is violated. We could potentially
relax this in the future as part of the automatic mipmap generation
work, which could easily do texture format conversion as part of its
preprocessing.

An easy way to generate reflection probe cubemaps is to bake them in
Blender and use the `export-blender-gi` tool that's part of the
[`bevy-baked-gi`] project. This tool takes a `.blend` file containing
baked cubemaps as input and exports cubemap images, pre-filtered with an
embedded fork of the [glTF IBL Sampler], alongside a corresponding
`.scn.ron` file that the scene spawner can use to recreate the
reflection probes.

Note that this is intentionally a minimal implementation, to aid
reviewability. Known issues are:

* Reflection probes are basically unsupported on WebGL 2, because WebGL
2 has no cubemap arrays. (Strictly speaking, you can have precisely one
reflection probe in the scene if you have no other cubemaps anywhere,
but this isn't very useful.)

* Reflection probes have no falloff, so reflections will abruptly change
when objects move from one bounding region to another.

* As mentioned before, all cubemaps in the world of a given type
(diffuse or specular) must have the same size, format, and mipmap count.

Future work includes:

* Blending between multiple reflection probes.

* A falloff/fade-out region so that reflected objects disappear
gradually instead of vanishing all at once.

* Irradiance volumes for voxel-based global illumination. This should
reuse much of the reflection probe logic, as they're both GI techniques
based on cuboid bounding regions.

* Support for WebGL 2, by breaking batches when reflection probes are
used.

These issues notwithstanding, I think it's best to land this with
roughly the current set of functionality, because this patch is useful
as is and adding everything above would make the pull request
significantly larger and harder to review.

---

## Changelog

### Added

* A new *LightProbe* component is available that specifies a bounding
region that an *EnvironmentMapLight* applies to. The combination of a
*LightProbe* and an *EnvironmentMapLight* offers *reflection probe*
functionality similar to that available in other engines.

[the corresponding feature in Blender's Eevee renderer]:
https://docs.blender.org/manual/en/latest/render/eevee/light_probes/reflection_cubemaps.html

[`bevy-baked-gi`]: https://github.com/pcwalton/bevy-baked-gi

[glTF IBL Sampler]: https://github.com/KhronosGroup/glTF-IBL-Sampler
2024-01-19 07:33:52 +00:00
..
3d_gizmos.rs Multiple Configurations for Gizmos (#10342) 2024-01-18 15:52:50 +00:00
3d_scene.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
3d_shapes.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
3d_viewport_to_world.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
anti_aliasing.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
atmospheric_fog.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
blend_modes.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
bloom_3d.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
deferred_rendering.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
deterministic.rs Cleanup deterministic example (#11416) 2024-01-19 06:08:19 +00:00
fog.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
generate_custom_mesh.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
lighting.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
lightmaps.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
lines.rs Use impl Into<A> for Assets::add (#10878) 2024-01-08 22:14:43 +00:00
load_gltf.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
orthographic.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
parallax_mapping.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
parenting.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
pbr.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
reflection_probes.rs Implement minimal reflection probes (fixed macOS, iOS, and Android). (#11366) 2024-01-19 07:33:52 +00:00
render_to_texture.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
shadow_biases.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
shadow_caster_receiver.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
skybox.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
spherical_area_lights.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
split_screen.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
spotlight.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
ssao.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
texture.rs Use impl Into<A> for Assets::add (#10878) 2024-01-08 22:14:43 +00:00
tonemapping.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
transmission.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
transparency_3d.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
two_passes.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
update_gltf_scene.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
vertex_colors.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00
wireframe.rs Exposure settings (adopted) (#11347) 2024-01-16 14:53:21 +00:00