1use glam::{Mat4, Vec3};
19
20#[derive(Clone, Copy, Debug, PartialEq)]
22pub struct Plane {
23 pub n: Vec3,
24 pub d: f32,
25}
26
27impl Plane {
28 pub fn signed_distance(&self, p: Vec3) -> f32 {
30 self.n.dot(p) + self.d
31 }
32}
33
34#[derive(Clone, Copy, Debug, PartialEq)]
36pub struct Frustum {
37 pub planes: [Plane; 6],
38}
39
40fn row(m: &Mat4, i: usize) -> glam::Vec4 {
42 glam::Vec4::new(m.x_axis[i], m.y_axis[i], m.z_axis[i], m.w_axis[i])
43}
44
45impl Frustum {
46 pub fn from_view_proj(view_proj: &Mat4) -> Self {
50 let r0 = row(view_proj, 0);
51 let r1 = row(view_proj, 1);
52 let r2 = row(view_proj, 2);
53 let r3 = row(view_proj, 3);
54 let raw = [r3 + r0, r3 - r0, r3 + r1, r3 - r1, r3 + r2, r3 - r2];
56 let planes = raw.map(|v| {
57 let n = Vec3::new(v.x, v.y, v.z);
58 let len = n.length().max(1e-9);
59 Plane {
60 n: n / len,
61 d: v.w / len,
62 }
63 });
64 Self { planes }
65 }
66
67 pub fn intersects_sphere(&self, center: Vec3, radius: f32) -> bool {
70 self.planes
71 .iter()
72 .all(|p| p.signed_distance(center) >= -radius)
73 }
74
75 pub fn intersects_aabb(&self, center: Vec3, half: Vec3) -> bool {
79 self.planes.iter().all(|p| {
80 let abs_n = Vec3::new(p.n.x.abs(), p.n.y.abs(), p.n.z.abs());
81 p.signed_distance(center) >= -abs_n.dot(half)
82 })
83 }
84}
85
86#[derive(Clone, Debug, PartialEq)]
92pub struct HiZPyramid {
93 pub width: u32,
94 pub height: u32,
95 pub levels: Vec<Vec<f32>>,
97}
98
99impl HiZPyramid {
100 pub fn new(width: u32, height: u32) -> Self {
103 let mut levels = Vec::new();
104 let (mut w, mut h) = (width.max(1), height.max(1));
105 loop {
106 levels.push(vec![1.0; (w as usize) * (h as usize)]);
107 if w == 1 && h == 1 {
108 break;
109 }
110 w = w.div_ceil(2).max(1);
111 h = h.div_ceil(2).max(1);
112 }
113 Self {
114 width: width.max(1),
115 height: height.max(1),
116 levels,
117 }
118 }
119
120 pub fn level_count(&self) -> u32 {
122 self.levels.len() as u32
123 }
124
125 pub fn build_from_depth(&mut self, depth: &[f32]) {
128 assert_eq!(
129 depth.len(),
130 (self.width * self.height) as usize,
131 "Depth-Buffer passt nicht zur Pyramiden-Grundfläche"
132 );
133 self.levels[0].copy_from_slice(depth);
134 for l in 1..self.levels.len() {
135 let prev_w = self.level_width((l - 1) as u32) as usize;
136 let prev_h = self.level_height((l - 1) as u32) as usize;
137 let w = self.level_width(l as u32) as usize;
138 let h = self.level_height(l as u32) as usize;
139 for y in 0..h {
140 for x in 0..w {
141 let mut m = f32::MAX;
142 for dy in 0..2 {
143 for dx in 0..2 {
144 let (sx, sy) = (x * 2 + dx, y * 2 + dy);
145 if sx < prev_w && sy < prev_h {
146 m = m.min(self.levels[l - 1][sy * prev_w + sx]);
147 }
148 }
149 }
150 self.levels[l][y * w + x] = m;
151 }
152 }
153 }
154 }
155
156 pub fn level_width(&self, l: u32) -> u32 {
157 self.width.div_ceil(1u32 << l.min(31)).max(1)
160 }
161
162 pub fn level_height(&self, l: u32) -> u32 {
163 self.height.div_ceil(1u32 << l.min(31)).max(1)
164 }
165
166 pub fn level_for_size(w: u32, h: u32) -> u32 {
169 let m = w.max(h).max(1);
170 (u32::BITS - m.leading_zeros() - 1) + (m.count_ones() > 1) as u32
171 }
172
173 pub fn min_depth_in(&self, level: u32, x0: u32, y0: u32, x1: u32, y1: u32) -> f32 {
176 let l = (level as usize).min(self.levels.len() - 1);
177 let lx = self.level_width(l as u32);
178 let ly = self.level_height(l as u32);
179 let w = lx as usize;
180 let (xa, xb) = ((x0.min(lx)) as usize, (x1.max(x0 + 1).min(lx)) as usize);
181 let (ya, yb) = ((y0.min(ly)) as usize, (y1.max(y0 + 1).min(ly)) as usize);
182 let mut m = f32::MAX;
183 for y in ya..yb {
184 for x in xa..xb {
185 m = m.min(self.levels[l][y * w + x]);
186 }
187 }
188 m
189 }
190
191 pub fn occludes(&self, level: u32, tx: u32, ty: u32, near_depth: f32, epsilon: f32) -> bool {
198 self.min_depth_in(level, tx, ty, tx + 1, ty + 1) <= near_depth + epsilon
199 }
200}
201
202#[cfg(test)]
203mod tests {
204 use super::*;
205
206 fn persp(aspect: f32) -> Mat4 {
207 Mat4::perspective_rh(std::f32::consts::FRAC_PI_2, aspect, 0.1, 100.0)
209 }
210
211 #[test]
212 fn frustum_punkte_richtig() {
213 let f = Frustum::from_view_proj(&persp(1.0));
214 assert!(
215 f.intersects_sphere(Vec3::new(0.0, 0.0, -5.0), 0.0),
216 "vorne Mitte"
217 );
218 assert!(
219 !f.intersects_sphere(Vec3::new(0.0, 0.0, 5.0), 0.0),
220 "hinter Kamera"
221 );
222 assert!(
223 !f.intersects_sphere(Vec3::new(0.0, 0.0, -200.0), 0.5),
224 "hinter fern"
225 );
226 assert!(
227 !f.intersects_sphere(Vec3::new(0.0, 0.0, -0.02), 0.0),
228 "vor nah"
229 );
230 assert!(
231 !f.intersects_sphere(Vec3::new(50.0, 0.0, -5.0), 0.5),
232 "rechts vorbei"
233 );
234 assert!(f.intersects_sphere(Vec3::new(3.9, 0.0, -5.0), 0.0));
236 assert!(f.intersects_sphere(Vec3::new(0.0, -3.9, -5.0), 0.0));
237 }
238
239 #[test]
240 fn kugel_am_rand_schneidet() {
241 let f = Frustum::from_view_proj(&persp(1.0));
242 let center = Vec3::new(5.2, 0.0, -5.0);
244 assert!(!f.intersects_sphere(center, 0.0));
245 assert!(
246 f.intersects_sphere(center, 1.5),
247 "Radius muss reinschneiden"
248 );
249 }
250
251 #[test]
252 fn aabb_test_konservativ() {
253 let f = Frustum::from_view_proj(&persp(1.0));
254 assert!(f.intersects_aabb(Vec3::new(6.0, 0.0, -5.0), Vec3::new(3.0, 1.0, 1.0)));
256 assert!(!f.intersects_aabb(Vec3::new(50.0, 0.0, -5.0), Vec3::new(1.0, 1.0, 1.0)));
258 assert!(f.intersects_aabb(Vec3::ZERO, Vec3::new(0.5, 0.5, 0.5)));
260 assert!(f.intersects_aabb(Vec3::new(0.0, 0.0, -10.0), Vec3::new(1.0, 1.0, 1.0)));
262 }
263
264 #[test]
265 fn pyramiden_geometrie() {
266 let p = HiZPyramid::new(8, 4);
267 assert_eq!(p.level_count(), 4); assert_eq!(p.level_width(0), 8);
269 assert_eq!(p.level_height(2), 1);
270 assert_eq!(p.levels[3].len(), 1);
271 assert_eq!(HiZPyramid::level_for_size(1, 1), 0);
273 assert_eq!(HiZPyramid::level_for_size(8, 8), 3);
274 assert_eq!(HiZPyramid::level_for_size(1024, 1024), 10);
275 assert_eq!(HiZPyramid::level_for_size(5, 3), 3); }
277
278 #[test]
279 fn pyramide_nicht_pot_konsistent_mit_level_geometrie() {
280 let mut p = HiZPyramid::new(1280, 720);
283 for (l, lv) in p.levels.iter().enumerate() {
284 assert_eq!(
285 lv.len(),
286 (p.level_width(l as u32) * p.level_height(l as u32)) as usize,
287 "Level {l} Geometrie"
288 );
289 }
290 let depth = vec![0.5; 1280 * 720];
293 p.build_from_depth(&depth);
294 for lv in &p.levels {
295 assert!(lv.iter().all(|&d| d == 0.5), "veraltete Texel nach build");
296 }
297 }
298
299 #[test]
300 fn min_reduktion_findet_naechste_tiefe() {
301 let mut p = HiZPyramid::new(4, 4);
302 let mut depth = vec![1.0; 16];
303 depth[2 * 4 + 3] = 0.25; p.build_from_depth(&depth);
305 assert!(approx_eq(p.levels[1][3], 0.25)); assert!(approx_eq(p.min_depth_in(2, 0, 0, 1, 1), 0.25)); assert!(approx_eq(p.min_depth_in(1, 0, 0, 1, 1), 1.0)); }
309
310 #[test]
311 fn okklusion_wand_verdeckt_dahinter() {
312 let mut p = HiZPyramid::new(8, 8);
314 let mut depth = vec![1.0; 64];
315 for y in 0..8 {
316 for x in 0..4 {
317 depth[y * 8 + x] = 0.5;
318 }
319 }
320 p.build_from_depth(&depth);
321 assert!(p.occludes(1, 0, 0, 0.6, 1e-4), "hinter Wand verdeckt");
323 assert!(!p.occludes(1, 0, 0, 0.4, 1e-4), "vor Wand sichtbar");
325 assert!(!p.occludes(1, 2, 0, 0.6, 1e-4), "neben Wand sichtbar");
328 }
329
330 #[test]
331 fn leere_pyramide_verdeckt_nichts() {
332 let p = HiZPyramid::new(16, 16);
333 assert!(!p.occludes(2, 1, 1, 0.9, 1e-4), "1.0 fern > 0.9");
334 assert!(p.occludes(2, 1, 1, 1.0, 1e-4), "Grenzfall 1.0 <= 1.0");
335 }
336
337 fn approx_eq(a: f32, b: f32) -> bool {
338 (a - b).abs() < 1e-6
339 }
340}