8 releases (4 breaking)

new 0.5.0 Jan 24, 2025
0.4.1 Dec 10, 2024
0.3.0 Aug 9, 2024
0.2.2 Aug 5, 2024
0.1.1 Jul 23, 2024

#165 in Game dev

Download history 3/week @ 2024-10-08 2/week @ 2024-10-15 2/week @ 2024-10-22 3/week @ 2024-10-29 9/week @ 2024-11-05 4/week @ 2024-11-19 7/week @ 2024-11-26 1/week @ 2024-12-03 274/week @ 2024-12-10 2/week @ 2024-12-17 115/week @ 2025-01-21

115 downloads per month

MIT license

48KB
1K SLoC

Rust 1K SLoC // 0.0% comments WebGPU Shader Language 196 SLoC // 0.1% comments

bevy_lit

bevy_lit is a simple and easy-to-use 2D lighting library for Bevy, designed to work seamlessly with a single camera setup. The library provides basic lighting functionalities through the types: Lighting2dSettings, AmbientLight2d, LightOccluder2d, and PointLight2d.

bevy_lit demo

Features

  • Lighting2dSettings: Controls lighting parameters such as shadow softness
  • AmbientLight2d: Provides a general light source that illuminates the entire scene uniformly.
  • PointLight2d: Emits light from a specific point, simulating light sources like lamps or torches.
  • LightOccluder2d: Creates shadows and blocks light from PointLight2d.
  • Web support both for WebGPU

Getting Started

Installation

Install it using the CLI:

cargo add bevy_lit

Or add bevy_lit to your Cargo.lock:

[dependencies]
bevy_lit = "*"

Usage

Below is a basic example demonstrating how to set up and use bevy_lit in your Bevy project:

use bevy::prelude::*;
use bevy_lit::prelude::*;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, Lighting2dPlugin))
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn((
        Camera2d,
        Lighting2dSettings::default(),
    ));

    commands.spawn(PointLight2d {
        color: Color::rgb(1.0, 1.0, 1.0),
        intensity: 3.0,
        radius: 200.0,
        falloff: 2.0,
        ..default(),
    });

    commands.spawn((
        LightOccluder2d::new(Vec2::splat(50.0)),
        Transform::from_xyz(0.0, 200.0, 0.0)
    ));
}

Implementation

bevy_lit uses signed distance fields (SDFs) to compute the occluders' distances. To soften the shadows, a blur is applied. This approach is not ideal and might have limitations in terms of performance and visual accuracy, but it provides a starting point for basic 2D lighting effects.

Compatibility

bevy bevy_lit
0.15 0.4..0.5
0.14 0.3

Acknowledgement

This library took heavy inspiration from the work of other developers. I learned a lot about lighting and Bevy development by reading the source code of the following crates:

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

bevy_lit is licensed under the MIT License. See LICENSE for more details.

Dependencies

~44–81MB
~1.5M SLoC