voxel-framework 0.13.0

A reusable voxel engine framework built on Bevy
Documentation
//! Optional lighting plugins: a fixed scene sun with a fill light, a headlamp
//! carried by an entity the game marks, and lights placed in the world.
//!
//! The three plugins are independent Default Implementation plugins. None of
//! them is part of [`VoxelCorePlugin`](crate::VoxelCorePlugin) or
//! [`VoxelPlugin`](crate::VoxelPlugin); a game adds the ones it wants. Every
//! color, intensity, angle and budget is supplied by the game.
//!
//! | Plugin | What it manages |
//! |---|---|
//! | [`SceneLightingPlugin`] | A fixed directional sun, a shadowless fill light and the global ambient light, from [`SceneLightingConfig`] |
//! | [`HeadlampPlugin`] | A spot light child for every entity that carries a [`Headlamp`] component |
//! | [`PlacedLightsPlugin`] | Point lights recorded by voxel position in [`PlacedLights`], with a nearest-N budget ([`PlacedLightBudget`]) |
//!
//! # Relationship with the day cycle
//!
//! [`DayCyclePlugin`](crate::world::DayCyclePlugin) animates its own sun
//! (tagged [`SunLight`](crate::world::SunLight)) and writes the global ambient
//! brightness every frame. [`SceneLightingPlugin`] uses different marker
//! components ([`SceneSunLight`], [`SceneFillLight`]), so the two never edit
//! each other's entities. A game normally picks one of them for the sun. To
//! add a fill light to an animated day cycle, leave
//! [`SceneLightingConfig::sun`] and [`SceneLightingConfig::ambient`] as `None`
//! and set only [`SceneLightingConfig::fill`] with an explicit direction.
//!
//! # Example
//!
//! ```no_run
//! use bevy::prelude::*;
//! use voxel_framework::lighting::{
//!     FillLightConfig, Headlamp, HeadlampPlugin, PlacedLight, PlacedLights,
//!     PlacedLightsPlugin, SceneLightingConfig, SceneLightingPlugin, SunConfig,
//! };
//!
//! fn setup(mut commands: Commands, mut lights: ResMut<PlacedLights>) {
//!     // The game decides which entity carries the headlamp.
//!     commands.spawn((Camera3d::default(), Headlamp::default()));
//!     // A light recorded at a voxel position.
//!     lights.insert(IVec3::new(4, 10, -3), PlacedLight::default());
//! }
//!
//! App::new()
//!     .add_plugins(DefaultPlugins)
//!     .insert_resource(SceneLightingConfig {
//!         sun: Some(SunConfig {
//!             direction: Vec3::new(-0.4, -1.0, -0.3),
//!             shadows_enabled: true,
//!             ..default()
//!         }),
//!         fill: Some(FillLightConfig::default()),
//!         ambient: None,
//!     })
//!     .add_plugins((SceneLightingPlugin, HeadlampPlugin, PlacedLightsPlugin))
//!     .add_systems(Startup, setup)
//!     .run();
//! ```

use bevy::prelude::*;

pub mod headlamp;
pub mod placed;
pub mod scene;

pub use headlamp::{Headlamp, HeadlampLight, HeadlampPlugin};
pub use placed::{
    PlacedLight, PlacedLightBudget, PlacedLightSource, PlacedLights, PlacedLightsPlugin,
    placed_light_chunk, placed_light_voxel, select_active_lights,
};
pub use scene::{
    AmbientConfig, FillLightConfig, SceneFillLight, SceneLightingConfig, SceneLightingPlugin,
    SceneSunLight, SunConfig, light_rotation,
};

/// System sets of the lighting plugins, all in the `Update` schedule.
///
/// Each plugin puts its systems in its own set, so a game can order its
/// systems around them or gate them with a run condition.
///
/// ```no_run
/// use bevy::prelude::*;
/// use voxel_framework::lighting::{Headlamp, HeadlampPlugin, LightingSystems};
///
/// fn toggle_headlamp(keys: Res<ButtonInput<KeyCode>>, mut lamps: Query<&mut Headlamp>) {
///     if keys.just_pressed(KeyCode::KeyF) {
///         for mut lamp in &mut lamps {
///             lamp.toggle();
///         }
///     }
/// }
///
/// App::new()
///     .add_plugins(DefaultPlugins)
///     .add_plugins(HeadlampPlugin)
///     // The change is applied to the spot light in the same frame.
///     .add_systems(Update, toggle_headlamp.before(LightingSystems::Headlamp))
///     .run();
/// ```
#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LightingSystems {
    /// [`SceneLightingPlugin`]: applies [`SceneLightingConfig`] when it changes.
    Scene,
    /// [`HeadlampPlugin`]: creates, updates and removes headlamp spot lights.
    Headlamp,
    /// [`PlacedLightsPlugin`]: selects the active placed lights and spawns or
    /// despawns their point lights.
    Placed,
}