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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Dynamic point lights for runtime illumination.
//!
//! Point lights are evaluated at face/vertex granularity (not per-pixel) to
//! keep cost tractable on microcontrollers. A typical embedded scene uses
//! 4–16 lights; the engine stores up to 16 internally.
//!
//! # Example
//! ```
//! use embedded_3dgfx::lights::{PointLight, PointLightSet};
//! use nalgebra::Point3;
//! use embedded_graphics_core::pixelcolor::Rgb565;
//!
//! let mut lights: PointLightSet<8> = PointLightSet::new();
//! lights.add(PointLight::new(Point3::new(0.0, 3.0, 0.0), Rgb565::new(31, 63, 31), 5.0));
//! let tint = lights.accumulate(Point3::new(0.0, 0.0, 0.0));
//! ```
use embedded_graphics_core::pixelcolor::{Rgb565, RgbColor};
use nalgebra::Point3;
/// A dynamic point light in world space.
///
/// Attenuation uses a squared-distance falloff that avoids a `sqrt` call:
/// `factor = (1 − d²/r²) × intensity`
/// giving a smooth curve from full brightness at the source to zero at
/// `radius`.
#[derive(Debug, Clone, Copy)]
pub struct PointLight {
/// World-space position of the light source.
pub position: Point3<f32>,
/// Light colour. Channels are scaled by `intensity` at sample time.
pub color: Rgb565,
/// Influence radius in world units. Surfaces at or beyond this distance
/// receive no contribution.
pub radius: f32,
/// Brightness multiplier. `1.0` = full colour at the source centre.
pub intensity: f32,
}
impl PointLight {
/// Construct a new point light with `intensity = 1.0`.
pub fn new(position: Point3<f32>, color: Rgb565, radius: f32) -> Self {
Self {
position,
color,
radius,
intensity: 1.0,
}
}
/// Builder-style intensity override.
pub fn with_intensity(mut self, intensity: f32) -> Self {
self.intensity = intensity;
self
}
/// Compute the additive RGB565 contribution of this light at `world_pos`.
///
/// Returns `Rgb565::new(0, 0, 0)` when `world_pos` is outside the
/// influence radius.
#[inline]
pub fn contribution_at(&self, world_pos: Point3<f32>) -> Rgb565 {
let diff = world_pos - self.position;
let dist_sq = diff.dot(&diff);
let r_sq = self.radius * self.radius;
if dist_sq >= r_sq {
return Rgb565::new(0, 0, 0);
}
let t = 1.0 - dist_sq / r_sq;
let factor = t * self.intensity;
let r = ((self.color.r() as f32) * factor).min(31.0) as u8;
let g = ((self.color.g() as f32) * factor).min(63.0) as u8;
let b = ((self.color.b() as f32) * factor).min(31.0) as u8;
Rgb565::new(r, g, b)
}
}
/// A fixed-capacity collection of [`PointLight`]s.
///
/// `N` is the maximum number of simultaneous lights (8–16 is typical for
/// embedded targets).
pub struct PointLightSet<const N: usize> {
pub lights: heapless::Vec<PointLight, N>,
}
impl<const N: usize> PointLightSet<N> {
/// Create an empty set.
pub const fn new() -> Self {
Self {
lights: heapless::Vec::new(),
}
}
/// Add a light. Returns `true` on success, `false` if the set is full.
pub fn add(&mut self, light: PointLight) -> bool {
self.lights.push(light).is_ok()
}
/// Remove all lights.
pub fn clear(&mut self) {
self.lights.clear();
}
/// Number of lights currently in the set.
pub fn len(&self) -> usize {
self.lights.len()
}
/// `true` if no lights are registered.
pub fn is_empty(&self) -> bool {
self.lights.is_empty()
}
/// Accumulate the additive RGB565 contribution of all lights at
/// `world_pos`. Each channel is summed and saturated to its maximum
/// (R: 31, G: 63, B: 31).
pub fn accumulate(&self, world_pos: Point3<f32>) -> Rgb565 {
let mut r = 0u32;
let mut g = 0u32;
let mut b = 0u32;
for light in &self.lights {
let c = light.contribution_at(world_pos);
r += c.r() as u32;
g += c.g() as u32;
b += c.b() as u32;
}
Rgb565::new(r.min(31) as u8, g.min(63) as u8, b.min(31) as u8)
}
}
impl<const N: usize> Default for PointLightSet<N> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
extern crate std;
use super::*;
use embedded_graphics_core::pixelcolor::WebColors;
#[test]
fn test_point_light_full_at_center() {
let light = PointLight::new(Point3::new(0.0, 0.0, 0.0), Rgb565::CSS_WHITE, 5.0);
let tint = light.contribution_at(Point3::new(0.0, 0.0, 0.0));
assert_eq!(tint.r(), 31);
assert_eq!(tint.g(), 63);
assert_eq!(tint.b(), 31);
}
#[test]
fn test_point_light_zero_outside_radius() {
let light = PointLight::new(Point3::new(0.0, 0.0, 0.0), Rgb565::CSS_WHITE, 1.0);
let tint = light.contribution_at(Point3::new(2.0, 0.0, 0.0));
assert_eq!(tint.r(), 0);
assert_eq!(tint.g(), 0);
assert_eq!(tint.b(), 0);
}
#[test]
fn test_point_light_falloff() {
let light = PointLight::new(Point3::new(0.0, 0.0, 0.0), Rgb565::CSS_WHITE, 10.0);
let near = light.contribution_at(Point3::new(1.0, 0.0, 0.0));
let far = light.contribution_at(Point3::new(5.0, 0.0, 0.0));
assert!(near.r() > far.r());
}
#[test]
fn test_point_light_set_accumulates() {
let mut set: PointLightSet<4> = PointLightSet::new();
set.add(PointLight::new(
Point3::new(0.0, 0.0, 0.0),
Rgb565::new(10, 20, 10),
5.0,
));
set.add(PointLight::new(
Point3::new(0.0, 0.0, 0.0),
Rgb565::new(5, 10, 5),
5.0,
));
let tint = set.accumulate(Point3::new(0.0, 0.0, 0.0));
assert!(tint.r() >= 10);
}
#[test]
fn test_point_light_set_empty() {
let set: PointLightSet<4> = PointLightSet::new();
let tint = set.accumulate(Point3::new(0.0, 0.0, 0.0));
assert_eq!(tint.r(), 0);
assert_eq!(tint.g(), 0);
assert_eq!(tint.b(), 0);
}
#[test]
fn test_point_light_set_saturation() {
let mut set: PointLightSet<4> = PointLightSet::new();
// Two max-brightness lights at the same point saturate all channels
set.add(PointLight::new(
Point3::new(0.0, 0.0, 0.0),
Rgb565::CSS_WHITE,
5.0,
));
set.add(PointLight::new(
Point3::new(0.0, 0.0, 0.0),
Rgb565::CSS_WHITE,
5.0,
));
let tint = set.accumulate(Point3::new(0.0, 0.0, 0.0));
assert_eq!(tint.r(), 31);
assert_eq!(tint.g(), 63);
assert_eq!(tint.b(), 31);
}
}