concinnity_core/components/
directional_light.rs1#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
18#[serde(default)]
19pub struct DirectionalLight {
20 pub direction: [f32; 3],
23 pub color: [f32; 3],
25 pub intensity: f32,
27}
28
29impl DirectionalLight {
30 pub const ZERO: Self = Self {
32 direction: [0.0; 3],
33 color: [0.0; 3],
34 intensity: 0.0,
35 };
36}
37
38impl Default for DirectionalLight {
39 fn default() -> Self {
40 Self {
41 direction: [-0.3, 0.85, 0.4],
42 color: [1.0, 1.0, 1.0],
43 intensity: 1.0,
44 }
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn the_default_sun_points_down_from_above() {
54 let l = DirectionalLight::default();
57 assert!(l.direction[1] > 0.0);
58 assert_eq!(l.color, [1.0, 1.0, 1.0]);
59 assert_eq!(l.intensity, 1.0);
60 }
61
62 #[test]
63 fn an_authored_sun_parses_and_round_trips_through_postcard() {
64 let l: DirectionalLight =
65 serde_json::from_str(r#"{"direction":[0,1,0],"color":[1,0.9,0.7],"intensity":3}"#)
66 .unwrap();
67 assert_eq!(l.direction, [0.0, 1.0, 0.0]);
68 assert_eq!(l.color, [1.0, 0.9, 0.7]);
69 assert_eq!(l.intensity, 3.0);
70
71 let bytes = postcard::to_allocvec(&l).unwrap();
72 let back: DirectionalLight = postcard::from_bytes(&bytes).unwrap();
73 assert_eq!(back.color, [1.0, 0.9, 0.7]);
74 assert_eq!(back.intensity, 3.0);
75 }
76}