1use crate::sim::FxInstance;
30
31#[derive(Clone, Copy, Debug, PartialEq)]
34pub struct FxCam {
35 pub view_proj: [f32; 16],
36 pub size_scale: f32,
37}
38
39impl FxCam {
40 fn project(&self, p: &[f32; 3], w_px: f32, h_px: f32, size: f32) -> Option<(f32, f32, f32)> {
42 let m = &self.view_proj;
43 let cx = m[0] * p[0] + m[4] * p[1] + m[8] * p[2] + m[12];
44 let cy = m[1] * p[0] + m[5] * p[1] + m[9] * p[2] + m[13];
45 let cw = m[3] * p[0] + m[7] * p[1] + m[11] * p[2] + m[15];
46 if cw <= 1e-4 || size <= 0.0 {
47 return None;
48 }
49 let px = (cx / cw * 0.5 + 0.5) * w_px;
50 let py = (0.5 - cy / cw * 0.5) * h_px;
51 let r = size * self.size_scale / cw;
52 if r <= 0.0 {
53 return None;
54 }
55 Some((px, py, r))
56 }
57}
58
59pub fn splat(dst: &mut [f32], w: u32, h: u32, cam: &FxCam, charge: &[FxInstance]) {
63 debug_assert_eq!(dst.len(), (w as usize) * (h as usize) * 4, "Zielgröße");
64 let (wf, hf) = (w as f32, h as f32);
65 for p in charge {
66 let Some((px, py, r)) = cam.project(&p.pos, wf, hf, p.size) else {
67 continue;
68 };
69 let x0 = (px - r).floor().max(0.0) as u32;
70 let x1 = (px + r).ceil().min(wf) as u32;
71 let y0 = (py - r).floor().max(0.0) as u32;
72 let y1 = (py + r).ceil().min(hf) as u32;
73 let (cr, cg, cb, ca) = (p.color[0], p.color[1], p.color[2], p.color[3]);
74 for y in y0..y1 {
75 let dy = y as f32 + 0.5 - py;
76 let zeile = ((y * w) as usize) * 4;
77 for x in x0..x1 {
78 let dx = x as f32 + 0.5 - px;
79 let d = (dx * dx + dy * dy).sqrt() / r;
80 if d >= 1.0 {
81 continue;
82 }
83 let s = 1.0 - d * d;
84 let a = s * s * ca;
85 let i = zeile + (x as usize) * 4;
86 dst[i] += cr * a;
87 dst[i + 1] += cg * a;
88 dst[i + 2] += cb * a;
89 dst[i + 3] += a;
90 }
91 }
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 fn cam_front(d: f32, h: u32) -> FxCam {
102 let near = 0.1f32;
103 let far = 4000.0f32;
104 let pz = (far + near) / (near - far);
105 let pz2 = 2.0 * far * near / (near - far);
106 FxCam {
108 view_proj: [
109 1.0,
110 0.0,
111 0.0,
112 0.0,
113 0.0,
114 1.0,
115 0.0,
116 0.0,
117 0.0,
118 0.0,
119 pz,
120 -1.0,
121 0.0,
122 0.0,
123 pz2 - d * pz,
124 d,
125 ],
126 size_scale: 1.0 * h as f32 * 0.25,
127 }
128 }
129
130 fn teilchen(pos: [f32; 3], size: f32, color: [f32; 4]) -> FxInstance {
131 FxInstance { pos, size, color }
132 }
133
134 fn puffer(w: u32, h: u32) -> Vec<f32> {
135 vec![0.0; (w as usize) * (h as usize) * 4]
136 }
137
138 #[test]
139 fn splat_analytik_am_mittelpunkt() {
140 let (w, h) = (64u32, 64u32);
141 let cam = cam_front(4.0, h);
142 let mut dst = puffer(w, h);
144 splat(
145 &mut dst,
146 w,
147 h,
148 &cam,
149 &[teilchen([0.0, 0.0, 0.0], 1.0, [1.0, 0.5, 0.0, 0.8])],
150 );
151 let d = ((0.5f32 * 0.5 + 0.5 * 0.5).sqrt()) / 4.0;
153 let s = 1.0 - d * d;
154 let a = s * s * 0.8;
155 let i = (32 * w as usize + 32) * 4;
156 assert!((dst[i] - a).abs() < 1e-6, "Rot");
157 assert!((dst[i + 1] - 0.5 * a).abs() < 1e-6, "Grün skaliert");
158 assert_eq!(dst[i + 2], 0.0, "Blau trägt nichts bei");
159 assert!((dst[i + 3] - a).abs() < 1e-6, "Alpha summiert mit");
160 assert_eq!(dst[(32 * w as usize + 40) * 4], 0.0, "jenseits d ≥ 1");
162 assert_eq!(dst[(27 * w as usize + 32) * 4], 0.0, "Zeile 27: d ≥ 1");
163 assert!(dst[(32 * w as usize + 33) * 4] > 0.0);
165 }
166
167 #[test]
168 fn splat_ist_additiv_und_reihenfolge_gleichgueltig() {
169 let (w, h) = (32u32, 32u32);
170 let cam = cam_front(4.0, h);
171 let a = teilchen([-0.2, 0.0, 0.0], 0.8, [1.0, 0.0, 0.0, 0.5]);
172 let b = teilchen([0.2, 0.0, 0.0], 0.8, [0.0, 0.0, 1.0, 0.5]);
173 let mut eine = puffer(w, h);
174 splat(&mut eine, w, h, &cam, &[a, b]);
175 let mut andere = puffer(w, h);
176 splat(&mut andere, w, h, &cam, &[b]);
177 splat(&mut andere, w, h, &cam, &[a]);
178 let dmax = eine
180 .iter()
181 .zip(&andere)
182 .fold(0.0f32, |m, (x, y)| m.max((x - y).abs()));
183 assert!(dmax < 1e-6, "Addition muss vertauschbar sein ({dmax})");
184 }
185
186 #[test]
187 fn splat_hinter_der_kamera_und_verhungert_ist_stumm() {
188 let (w, h) = (16u32, 16u32);
189 let cam = cam_front(4.0, h);
190 let mut dst = puffer(w, h);
191 let stumme = [
192 teilchen([0.0, 0.0, 5.0], 1.0, [1.0, 1.0, 1.0, 1.0]), teilchen([0.0, 0.0, 4.0], 1.0, [1.0, 1.0, 1.0, 1.0]), teilchen([0.0, 0.0, 0.0], 0.0, [1.0, 1.0, 1.0, 1.0]), teilchen([0.0, 0.0, 0.0], -1.0, [1.0, 1.0, 1.0, 1.0]), ];
197 splat(&mut dst, w, h, &cam, &stumme);
198 assert!(dst.iter().all(|v| *v == 0.0), "nichts darf treffen");
199 }
200
201 #[test]
202 fn splat_am_rand_klemmt_ohne_panik() {
203 let (w, h) = (32u32, 32u32);
204 let cam = cam_front(4.0, h);
205 let weit = [
207 teilchen([4.0, 0.0, 0.0], 2.0, [1.0, 0.0, 0.0, 1.0]), teilchen([-4.0, 3.0, 0.0], 2.0, [0.0, 1.0, 0.0, 1.0]), teilchen([0.0, -3.9, 0.0], 3.0, [0.0, 0.0, 1.0, 1.0]), ];
211 let mut dst = puffer(w, h);
212 splat(&mut dst, w, h, &cam, &weit);
213 assert!(dst.iter().all(|v| *v >= 0.0), "nur Addition");
214 assert!(
215 dst.iter().any(|v| *v > 0.0),
216 "Rand-Teilchen treffen sichtbar"
217 );
218 }
219
220 #[test]
221 fn winziges_teilchen_trifft_genau_ein_pixel() {
222 let (w, h) = (16u32, 16u32);
223 let cam = cam_front(4.0, h);
224 let mut dst = puffer(w, h);
228 splat(
229 &mut dst,
230 w,
231 h,
232 &cam,
233 &[teilchen([0.25, -0.25, 0.0], 0.5, [1.0, 1.0, 1.0, 1.0])],
234 );
235 let leuchtend = dst.chunks_exact(4).filter(|p| p[0] > 0.0).count();
236 assert_eq!(leuchtend, 1, "exakt ein Pixel wie nach Kantenregel der GPU");
237 assert!((dst[(8 * 16 + 8) * 4] - 1.0).abs() < 1e-6);
238 }
239}