#bevy #sdf #illumination #global #light #screen #space

bevy_magic_light_2d

Experimental dynamic 2D global illumination system for Bevy, based on SDF ray-marching and screen space irradiance cache probes

3 unstable releases

0.8.1 Sep 4, 2024
0.8.0 Aug 25, 2024
0.7.0 Jul 7, 2024

#120 in Game dev

Download history 67/week @ 2024-07-01 50/week @ 2024-07-08 4/week @ 2024-07-29 80/week @ 2024-08-19 54/week @ 2024-08-26 148/week @ 2024-09-02 7/week @ 2024-09-09 7/week @ 2024-09-16 7/week @ 2024-09-23

172 downloads per month

Custom license

6MB
2.5K SLoC

Rust 2K SLoC // 0.0% comments WebGPU Shader Language 892 SLoC // 0.0% comments

🔮 Magic Light 2D – experimental dynamic 2D global illumination system for Bevy Engine.

Build status dependency status

Discord (ping me if it expires)

🚧 Beware! This repo is heavily under construction and a lot of things may change.

Experimental dynamic 2D global illumination system for Bevy, based on SDF ray-marching and screen space irradiance cache probes.

Demo

cargo run --example minimal
cargo run --example krypta
cargo run --example movement

Bevy Magic Light 2D – Demo

Implementation is based on several approaches:

  • First pass computes SDF for all occluders and stores it in a texture with one channel.
  • Second pass computes irradiance from direct light. We check amount of light received by probe taking into account occlusion which checked using SDF. The final contribution for each light source is computed using square falloff.
  • Third pass computes secondary bounced light. The approach is similar to the second pass, but instead of direct light, it uses irradiance is sampled from the probes. We use exponential sampling to check how much of reflected light is received by probe. Same as in the second pass SDF is used to check occlusion and the final contribution is computed using square falloff.
  • Finally, we combine results from the second and third passes combining cache from the previous eight frames. The we optionally filter the result using a edge-aware smoothing filter and apply Gamma correction.

The main performance gain is coming from computing value of only 1 / 64 number of pixels (for 8x8 probe size). The rest of the pixels are interpolated from the nearest probes.

Usage

cargo run --example krypta
  • WASD to control camera.
  • SHIFT+LMC to place a light source.
  • RMC to change color of light source.

TODOs

Optimizations

  • Use jump flood algorithm for calculating SDF.
  • Precomputed noise.
  • Guided sampling for secondary light.

Features

  • Light can bounce from occluders.
  • Arbitrary number of bounces via configuration.
  • Handle camera scale and rotation.
  • Support multiple layers.
  • Expose settings instead of hardcoding them.
  • Support resize of targets.
  • Support transparent occluders.
  • Support color transfer from occluders.
  • Add inspector for GI settings.
  • Add support for emissive materials and other types of light sources.

Address limitations

  • SDF for offscreen occluders.

Others

  • Add examples and HUD explaining how to use example.

License

Copyright 2022, 2023 Vladimir Zaytsev <vladimir@xyzw.io>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Dependencies

~41–77MB
~1.5M SLoC