pix_engine/
lighting.rs

1//! [Light] source functions.
2
3use crate::prelude::*;
4#[cfg(feature = "serde")]
5use serde::{de::DeserializeOwned, Deserialize, Serialize};
6
7/// Source of [Light].
8#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10#[cfg_attr(feature = "serde", serde(bound = "T: Serialize + DeserializeOwned"))]
11pub enum LightSource<T = f64, const N: usize = 3> {
12    /// Ambient light.
13    Ambient,
14    /// Light from a specific point.
15    Point(Point<T, N>),
16    /// Light from a specific direction.
17    Direction(Vector<T, N>),
18}
19
20/// `Light` representation including `source` and `intensity`.
21#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
22#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
23#[cfg_attr(feature = "serde", serde(bound = "T: Serialize + DeserializeOwned"))]
24pub struct Light<T = f64, const N: usize = 3> {
25    /// Source of light.
26    pub source: LightSource<T, N>,
27    /// Light intensity.
28    pub intensity: T,
29}
30
31impl<T, const N: usize> Light<T, N> {
32    /// Constructs a new `Light`.
33    pub const fn new(source: LightSource<T, N>, intensity: T) -> Self {
34        Self { source, intensity }
35    }
36
37    /// Constructs a `Light` with `source` as [`LightSource::Ambient`].
38    pub const fn ambient(intensity: T) -> Self {
39        Self::new(LightSource::Ambient, intensity)
40    }
41
42    /// Constructs a `Light` with `source` as [`LightSource::Point`].
43    pub fn point<P>(intensity: T, position: P) -> Self
44    where
45        P: Into<Point<T, N>>,
46    {
47        Self::new(LightSource::Point(position.into()), intensity)
48    }
49
50    /// Constructs a `Light` with source as [`LightSource::Direction`].
51    pub fn direction<V>(intensity: T, direction: V) -> Self
52    where
53        V: Into<Vector<T, N>>,
54    {
55        Self::new(LightSource::Direction(direction.into()), intensity)
56    }
57}
58
59impl<T, const N: usize> Default for Light<T, N>
60where
61    T: Default,
62{
63    fn default() -> Self {
64        Self::new(LightSource::Ambient, T::default())
65    }
66}