1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Directional-light schema.
/// An infinitely distant directional light (sun, moon, or sky fill).
///
/// Up to 4 directional lights may be declared; extras beyond 4 are silently ignored.
/// When no directional light is present, a built-in warm sun is used as a fallback.
///
/// ```rust
/// # use concinnity_core::components::DirectionalLight;
/// DirectionalLight {
/// direction: [-0.3, 0.85, 0.4],
/// color: [1.0, 0.95, 0.8],
/// intensity: 1.0,
/// ..Default::default()
/// };
/// ```
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct DirectionalLight {
/// Direction pointing toward the light source. Does not need to be
/// normalised.
pub direction: [f32; 3],
/// Linear-space RGB colour of the light.
pub color: [f32; 3],
/// Intensity multiplier applied to the colour.
pub intensity: f32,
}
impl DirectionalLight {
/// A light contributing nothing, used to pad a fixed-size set.
pub const ZERO: Self = Self {
direction: [0.0; 3],
color: [0.0; 3],
intensity: 0.0,
};
}
impl Default for DirectionalLight {
fn default() -> Self {
Self {
direction: [-0.3, 0.85, 0.4],
color: [1.0, 1.0, 1.0],
intensity: 1.0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_sun_points_down_from_above() {
// The direction is toward the light, so a positive Y component is what
// makes the default read as an overhead sun rather than an uplight.
let l = DirectionalLight::default();
assert!(l.direction[1] > 0.0);
assert_eq!(l.color, [1.0, 1.0, 1.0]);
assert_eq!(l.intensity, 1.0);
}
#[test]
fn an_authored_sun_parses_and_round_trips_through_postcard() {
let l: DirectionalLight =
serde_json::from_str(r#"{"direction":[0,1,0],"color":[1,0.9,0.7],"intensity":3}"#)
.unwrap();
assert_eq!(l.direction, [0.0, 1.0, 0.0]);
assert_eq!(l.color, [1.0, 0.9, 0.7]);
assert_eq!(l.intensity, 3.0);
let bytes = postcard::to_allocvec(&l).unwrap();
let back: DirectionalLight = postcard::from_bytes(&bytes).unwrap();
assert_eq!(back.color, [1.0, 0.9, 0.7]);
assert_eq!(back.intensity, 3.0);
}
}