Code Monkey home page Code Monkey logo

bevy_lit's Introduction

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: AmbientLight2d, LightOccluder2d, and PointLight2d.

bevy_lit demo

Features

  • AmbientLight2d: Provides a general light source that illuminates the entire scene uniformly.
  • LightOccluder2d: Creates shadows and blocks light from PointLight2d.
  • PointLight2d: Emits light from a specific point, simulating light sources like lamps or torches.

Disclaimer

Please note that bevy_lit is in an early development stage. Users will find bugs and incomplete features. Additionally, the documentation is currently lacking. I appreciate any feedback and contributions to help improve the library.

Getting Started

Installation

To use it, you can clone the repository directly from GitHub - if you want to fine tune the library to your needs. Or add it to your Cargo.lock:

[dependencies]
bevy_lit = "0.1"

Demo

cargo run --example basic

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 {
                ambient_light: AmbientLight2d {
                    brightness: 0.2,
                    color: Color::Srgba(Srgba::hex("#C09AFE").unwrap()),
                },
                shadow_softness: 32.0,
            },
        ))
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2dBundle::default());

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

    commands.spawn(LightOccluder2dBundle {
        light_occluder: LightOccluder2d::new(Vec2::new(50.0, 50.0)),
        transform: Transform::from_xyz(0.0, 200.0, 0.0),
        ..default()
    });
}

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.

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.

bevy_lit's People

Contributors

malbernaz avatar

Stargazers

 avatar  avatar

Watchers

Lucian avatar  avatar

bevy_lit's Issues

Panics on wasm

Had this panic when running on wasm:

.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.20.1/src/backend/wgpu_core.rs:2996:5:
wgpu error: Validation Error

Caused by:
    In Device::create_bind_group_layout
      note: label = `sdf_bind_group_layout`
    Too many bindings of type StorageTextures in Stage ShaderStages(COMPUTE), limit is 0, count was 1. Check the limit `max_storage_textures_per_shader_stage` passed to `Adapter::request_device`



Stack:

__wbg_get_imports/imports.wbg.__wbg_new_abda76e883ba8a5f@http://127.0.0.1:1334/api/wasm.js:1864:13
srs_bjam5-e874cad49f4fc267.wasm.__wbg_new_abda76e883ba8a5f externref shim@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[577864]:0x9624569
srs_bjam5-e874cad49f4fc267.wasm.console_error_panic_hook::Error::new::h1f61cab1f500c269@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[362003]:0x89d50b6
srs_bjam5-e874cad49f4fc267.wasm.console_error_panic_hook::hook_impl::hd4fc3e2da1234d6e@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[78280]:0x5936900
srs_bjam5-e874cad49f4fc267.wasm.console_error_panic_hook::hook::h46988bcbd1b76001@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[505018]:0x934b9d2
srs_bjam5-e874cad49f4fc267.wasm.core::ops::function::Fn::call::hc4001c9b32cd8f25@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[448189]:0x9030b5a
srs_bjam5-e874cad49f4fc267.wasm.std::panicking::rust_panic_with_hook::h6731baa78621a747@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[154549]:0x6e4b4d4
srs_bjam5-e874cad49f4fc267.wasm.std::panicking::begin_panic_handler::{{closure}}::hb6cd8464ed39ae71@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[207432]:0x77fca42
srs_bjam5-e874cad49f4fc267.wasm.std::sys_common::backtrace::__rust_end_short_backtrace::hbdf3ddeb21a1e747@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[582468]:0x9633257
srs_bjam5-e874cad49f4fc267.wasm.rust_begin_unwind@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[461459]:0x9100567
srs_bjam5-e874cad49f4fc267.wasm.core::panicking::panic_fmt::h5c7ce52813e94bcd@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[483976]:0x923b435
srs_bjam5-e874cad49f4fc267.wasm.wgpu::backend::wgpu_core::default_error_handler::h1c0fc58be54099f7@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[61492]:0x5213eec
srs_bjam5-e874cad49f4fc267.wasm.core::ops::function::Fn::call::hf719e2aa777ba88d@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[240810]:0x7cf7af8
srs_bjam5-e874cad49f4fc267.wasm.<alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h18b5c0c1016bcdc8@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[207258]:0x77f5a01
srs_bjam5-e874cad49f4fc267.wasm.wgpu::backend::wgpu_core::ErrorSinkRaw::handle_error::h065e4b6bf8802a96@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[17609]:0x345f2e3
srs_bjam5-e874cad49f4fc267.wasm.wgpu::backend::wgpu_core::ContextWgpuCore::handle_error::ha59b319cf3960af1@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[7348]:0x25aefe8
srs_bjam5-e874cad49f4fc267.wasm.<wgpu::backend::wgpu_core::ContextWgpuCore as wgpu::context::Context>::device_create_bind_group_layout::h17f317da88700ea5@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[8332]:0x27a9d82
srs_bjam5-e874cad49f4fc267.wasm.<T as wgpu::context::DynContext>::device_create_bind_group_layout::h8d0b62c340313f6c@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[150458]:0x6d69429
srs_bjam5-e874cad49f4fc267.wasm.wgpu::Device::create_bind_group_layout::h8810113d2861d672@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[108994]:0x632d712
srs_bjam5-e874cad49f4fc267.wasm.bevy_render::renderer::render_device::RenderDevice::create_bind_group_layout::h6c2601e4ea3dd920@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[129042]:0x6874085
srs_bjam5-e874cad49f4fc267.wasm.<bevy_lit::pipeline::LightingPipelines as bevy_ecs::world::FromWorld>::from_world::hea260bfdba3e5a39@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[946]:0xc22f30
srs_bjam5-e874cad49f4fc267.wasm.bevy_ecs::world::World::init_resource::he7f57d06e9c4dad1@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[123986]:0x67315b6
srs_bjam5-e874cad49f4fc267.wasm.bevy_app::sub_app::SubApp::init_resource::hdc23cebf7f0d8473@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[463241]:0x911a7e6
srs_bjam5-e874cad49f4fc267.wasm.<bevy_lit::plugin::Lighting2dPlugin as bevy_app::plugin::Plugin>::finish::had24875c1b61722e@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[206086]:0x77c6380
srs_bjam5-e874cad49f4fc267.wasm.bevy_app::app::App::finish::h63b87fb92beee313@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[55031]:0x4edc9af
srs_bjam5-e874cad49f4fc267.wasm.<bevy_winit::state::WinitAppRunnerState<T> as winit::application::ApplicationHandler<T>>::new_events::h9498b6a75c328ce4@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[48444]:0x4b4e33a
srs_bjam5-e874cad49f4fc267.wasm.winit::event_loop::EventLoop<T>::run_app::{{closure}}::h3cccec1eba050fa8@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[24131]:0x3a61dc6
srs_bjam5-e874cad49f4fc267.wasm.winit::platform_impl::web::event_loop::EventLoop<T>::run::{{closure}}::h0eb8f3b7a54edc65@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[61714]:0x522e380
srs_bjam5-e874cad49f4fc267.wasm.<alloc::boxed::Box<F,A> as core::ops::function::FnMut<Args>>::call_mut::h2f44e43f0688fc8e@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[270229]:0x80a8263
srs_bjam5-e874cad49f4fc267.wasm.winit::platform_impl::web::event_loop::runner::Runner::handle_single_event::h531374782f3c9446@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[27307]:0x3d0fe76
srs_bjam5-e874cad49f4fc267.wasm.winit::platform_impl::web::event_loop::runner::Shared::handle_event::he6976b2d3357512c@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[6328]:0x2356114
srs_bjam5-e874cad49f4fc267.wasm.winit::platform_impl::web::event_loop::runner::Shared::run_until_cleared::h06a8121c1d5b5803@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[19217]:0x3600561
srs_bjam5-e874cad49f4fc267.wasm.winit::platform_impl::web::event_loop::runner::Shared::send_events::hf2aa87d054f27aae@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[7095]:0x252534f
srs_bjam5-e874cad49f4fc267.wasm.winit::platform_impl::web::event_loop::window_target::ActiveEventLoop::register::{{closure}}::h4963d81cb524463d@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[22533]:0x39001bf
srs_bjam5-e874cad49f4fc267.wasm.winit::platform_impl::web::web_sys::pointer::PointerHandler::on_cursor_enter::{{closure}}::h3f17f3c019b11afe@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[90126]:0x5d60577
srs_bjam5-e874cad49f4fc267.wasm.<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h9d474cbbba7df26f@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[153499]:0x6e115ff
srs_bjam5-e874cad49f4fc267.wasm.closure68897 externref shim@http://127.0.0.1:1334/api/wasm.wasm:wasm-function[576553]:0x961d21a
__wbg_adapter_60@http://127.0.0.1:1334/api/wasm.js:228:6
real@http://127.0.0.1:1334/api/wasm.js:181:32

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.