dreamwell_engine/
lighting.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
7pub struct DirectionalLightDesc {
8 pub direction: [f32; 3],
10 pub color: [f32; 3],
12 pub intensity_lux: f32,
14}
15
16impl Default for DirectionalLightDesc {
17 fn default() -> Self {
18 Self {
19 direction: [0.0, -1.0, 0.0],
20 color: [1.0, 1.0, 1.0],
21 intensity_lux: 100_000.0,
22 }
23 }
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
28pub struct PointLightDesc {
29 pub position: [f32; 3],
31 pub color: [f32; 3],
33 pub intensity_lumens: f32,
35 pub range: f32,
37}
38
39impl Default for PointLightDesc {
40 fn default() -> Self {
41 Self {
42 position: [0.0, 3.0, 0.0],
43 color: [1.0, 1.0, 1.0],
44 intensity_lumens: 800.0,
45 range: 10.0,
46 }
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
52pub struct SpotLightDesc {
53 pub position: [f32; 3],
55 pub direction: [f32; 3],
57 pub color: [f32; 3],
59 pub intensity_lumens: f32,
61 pub range: f32,
63 pub inner_cos: f32,
65 pub outer_cos: f32,
67}
68
69impl Default for SpotLightDesc {
70 fn default() -> Self {
71 Self {
72 position: [0.0, 3.0, 0.0],
73 direction: [0.0, -1.0, 0.0],
74 color: [1.0, 1.0, 1.0],
75 intensity_lumens: 800.0,
76 range: 10.0,
77 inner_cos: 0.9659, outer_cos: 0.8660, }
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn directional_default() {
89 let d = DirectionalLightDesc::default();
90 assert_eq!(d.direction, [0.0, -1.0, 0.0]);
91 assert!(d.intensity_lux > 0.0);
92 }
93
94 #[test]
95 fn point_default() {
96 let p = PointLightDesc::default();
97 assert!(p.range > 0.0);
98 assert!(p.intensity_lumens > 0.0);
99 }
100
101 #[test]
102 fn spot_cone_angles() {
103 let s = SpotLightDesc::default();
104 assert!(s.inner_cos > s.outer_cos, "inner cone must be tighter than outer");
105 }
106
107 #[test]
108 fn serde_roundtrip() {
109 let d = DirectionalLightDesc::default();
110 let json = serde_json::to_string(&d).unwrap();
111 let back: DirectionalLightDesc = serde_json::from_str(&json).unwrap();
112 assert_eq!(d, back);
113 }
114}