Skip to main content

concinnity_asset/
rect_area_light.rs

1// Rectangular area-light schema.
2
3/// A rectangular area light: a glowing panel that lights the scene from its
4/// whole surface rather than from a single point.
5///
6/// Unlike a [PointLight](#pointlight) or [SpotLight](#spotlight), the softness of
7/// the shadow terminator and the shape of the specular highlight follow the
8/// panel's real dimensions, so a wide softbox wraps light around a surface and
9/// leaves a stretched rectangular reflection on glossy materials. Use it for
10/// windows, ceiling panels, screens, and practical lights.
11///
12/// The panel is positioned by `centre`, oriented by `normal` (the direction it
13/// emits), and sized by `half_size`, matching [GlassPanel](#glasspanel).
14///
15/// ```rust
16/// # use concinnity_asset::RectAreaLight;
17/// RectAreaLight {
18///     centre: [0.0, 3.0, -4.0],
19///     normal: [0.0, 0.0, 1.0],
20///     half_size: [1.5, 1.0],
21///     color: [1.0, 0.95, 0.85],
22///     intensity: 12.0,
23///     range: 18.0,
24///     ..Default::default()
25/// };
26/// ```
27#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
28#[serde(default)]
29pub struct RectAreaLight {
30    /// World-space position of the panel's centre.
31    pub centre: [f32; 3],
32    /// Direction the panel emits. Normalised on load; defaults to `+Z` when
33    /// degenerate.
34    pub normal: [f32; 3],
35    /// Half-width and half-height of the panel, in world units.
36    pub half_size: [f32; 2],
37    /// Linear-space RGB colour of the light.
38    pub color: [f32; 3],
39    /// Intensity multiplier applied to the colour.
40    pub intensity: f32,
41    /// Maximum reach in world units; attenuation is zero at this distance.
42    pub range: f32,
43    /// When true the panel emits from both faces. A one-sided panel lights only
44    /// the half-space its `normal` points into.
45    pub two_sided: bool,
46}
47
48impl Default for RectAreaLight {
49    fn default() -> Self {
50        Self {
51            centre: [0.0, 3.0, 0.0],
52            normal: [0.0, -1.0, 0.0],
53            half_size: [1.0, 1.0],
54            color: [1.0, 1.0, 1.0],
55            intensity: 12.0,
56            range: 18.0,
57            two_sided: false,
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn the_default_panel_is_a_ceiling_light_facing_down() {
68        let l = RectAreaLight::default();
69        assert_eq!(l.centre, [0.0, 3.0, 0.0]);
70        assert_eq!(l.normal, [0.0, -1.0, 0.0]);
71        assert_eq!(l.half_size, [1.0, 1.0]);
72        assert_eq!(l.intensity, 12.0);
73        assert_eq!(l.range, 18.0);
74        // One-sided: the back of the panel emits nothing.
75        assert!(!l.two_sided);
76    }
77
78    #[test]
79    fn an_authored_panel_parses_and_round_trips_through_postcard() {
80        let l: RectAreaLight = serde_json::from_str(
81            r#"{"centre":[0,1.5,-4],"normal":[0,0,1],"half_size":[2,0.5],
82                "color":[1,0.95,0.9],"intensity":30,"range":25,"two_sided":true}"#,
83        )
84        .unwrap();
85        assert!(l.two_sided);
86        assert_eq!(l.normal, [0.0, 0.0, 1.0]);
87
88        let bytes = postcard::to_allocvec(&l).unwrap();
89        let back: RectAreaLight = postcard::from_bytes(&bytes).unwrap();
90        assert_eq!(back.centre, [0.0, 1.5, -4.0]);
91        assert_eq!(back.half_size, [2.0, 0.5]);
92        assert_eq!(back.color, [1.0, 0.95, 0.9]);
93        assert_eq!(back.intensity, 30.0);
94        assert_eq!(back.range, 25.0);
95    }
96}