Skip to main content

concinnity_asset/
point_light.rs

1// Point-light schema.
2
3/// A spherical point light with quadratic distance attenuation.
4///
5/// The forward renderer lights every surface from all declared point lights (up
6/// to a large per-scene budget). Secondary effects (volumetric fog, SDF
7/// raymarching, and reflection-probe capture) still consider only the first 8.
8///
9/// ```rust
10/// # use concinnity_asset::PointLight;
11/// PointLight {
12///     position: [2.0, 2.5, -3.0],
13///     color: [1.0, 0.8, 0.5],
14///     intensity: 8.0,
15///     range: 6.0,
16///     ..Default::default()
17/// };
18/// ```
19#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
20#[serde(default)]
21pub struct PointLight {
22    /// World-space position of the light source.
23    pub position: [f32; 3],
24    /// Linear-space RGB colour of the light.
25    pub color: [f32; 3],
26    /// Intensity multiplier applied to the colour.
27    pub intensity: f32,
28    /// Maximum reach in world units; attenuation is zero at this distance.
29    pub range: f32,
30}
31
32impl Default for PointLight {
33    fn default() -> Self {
34        Self {
35            position: [0.0, 2.5, 0.0],
36            color: [1.0, 1.0, 1.0],
37            intensity: 8.0,
38            range: 6.0,
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn the_default_lamp_hangs_above_head_height_with_a_room_sized_reach() {
49        let l = PointLight::default();
50        assert_eq!(l.position, [0.0, 2.5, 0.0]);
51        assert_eq!(l.color, [1.0, 1.0, 1.0]);
52        assert_eq!(l.intensity, 8.0);
53        assert_eq!(l.range, 6.0);
54    }
55
56    #[test]
57    fn an_authored_lamp_parses_and_round_trips_through_postcard() {
58        let l: PointLight = serde_json::from_str(
59            r#"{"position":[2,2.5,-3],"color":[1,0.8,0.5],"intensity":12,"range":9}"#,
60        )
61        .unwrap();
62        assert_eq!(l.position, [2.0, 2.5, -3.0]);
63        assert_eq!(l.color, [1.0, 0.8, 0.5]);
64
65        let bytes = postcard::to_allocvec(&l).unwrap();
66        let back: PointLight = postcard::from_bytes(&bytes).unwrap();
67        assert_eq!(back.intensity, 12.0);
68        assert_eq!(back.range, 9.0);
69    }
70}