nightshade 0.14.0

A cross-platform data-oriented game engine.
Documentation
//! Light component definitions.

use serde::{Deserialize, Serialize};

/// Type of light source following glTF KHR_lights_punctual.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum LightType {
    /// Infinitely distant light (like the sun). Uses entity's -Z direction.
    #[default]
    Directional,
    /// Omnidirectional point light. Uses entity's position.
    Point,
    /// Cone-shaped spotlight. Uses entity's position and -Z direction.
    Spot,
}

/// Light source component.
///
/// Attach to an entity with a transform to illuminate the scene.
/// The transform's -Z axis defines the light direction (for directional and spot lights).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Light {
    /// The type of light (directional, point, or spot).
    pub light_type: LightType,
    /// Light color as linear RGB.
    pub color: nalgebra_glm::Vec3,
    /// Light intensity (lumens for point/spot, lux for directional).
    pub intensity: f32,
    /// Maximum range for point/spot lights (0 = unlimited).
    pub range: f32,
    /// Inner cone angle in radians for spot lights (full intensity).
    pub inner_cone_angle: f32,
    /// Outer cone angle in radians for spot lights (zero intensity).
    pub outer_cone_angle: f32,
    /// Whether this light casts shadows.
    pub cast_shadows: bool,
    /// Depth bias to reduce shadow acne artifacts.
    pub shadow_bias: f32,
    /// Per-light shadow map resolution. 0 falls back to the renderer default.
    #[serde(default)]
    pub shadow_resolution: u32,
    /// Maximum distance at which this light casts shadows. 0 = unlimited.
    #[serde(default)]
    pub shadow_distance: f32,
    /// Optional cookie texture path (IES profile or projected mask).
    #[serde(default)]
    pub cookie_texture: Option<String>,
}

impl Default for Light {
    fn default() -> Self {
        Self {
            light_type: LightType::default(),
            color: nalgebra_glm::Vec3::new(0.0, 0.0, 0.0),
            intensity: 0.0,
            range: 0.0,
            inner_cone_angle: 0.0,
            outer_cone_angle: 0.0,
            cast_shadows: false,
            shadow_bias: 0.0,
            shadow_resolution: 0,
            shadow_distance: 0.0,
            cookie_texture: None,
        }
    }
}

impl Light {
    /// Enables shadow casting with the specified depth bias.
    pub fn with_shadows(mut self, bias: f32) -> Self {
        self.cast_shadows = true;
        self.shadow_bias = bias;
        self
    }
}