Skip to main content

scenix_light/
directional.rs

1use scenix_core::Color;
2use scenix_math::Vec3;
3
4use crate::ShadowConfig;
5
6/// Directional light with optional shadow configuration.
7#[derive(Clone, Copy, Debug, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct DirectionalLight {
10    /// Direction the light shines toward.
11    pub direction: Vec3,
12    /// Light color in linear RGB.
13    pub color: Color,
14    /// Scalar intensity.
15    pub intensity: f32,
16    /// Optional shadow configuration.
17    pub shadow: Option<ShadowConfig>,
18}
19
20impl DirectionalLight {
21    /// Creates a directional light. Zero directions fall back to negative Z.
22    #[inline]
23    pub fn new(direction: Vec3, color: Color, intensity: f32) -> Self {
24        let direction = direction.normalize();
25        Self {
26            direction: if direction == Vec3::ZERO {
27                Vec3::NEG_Z
28            } else {
29                direction
30            },
31            color,
32            intensity,
33            shadow: None,
34        }
35    }
36
37    /// Returns this light with shadow configuration.
38    #[inline]
39    pub const fn shadow(mut self, shadow: ShadowConfig) -> Self {
40        self.shadow = Some(shadow);
41        self
42    }
43}
44
45impl Default for DirectionalLight {
46    #[inline]
47    fn default() -> Self {
48        Self::new(Vec3::NEG_Z, Color::WHITE, 1.0)
49    }
50}