proof_engine/scene/
query.rs1use crate::glyph::GlyphId;
4use glam::Vec3;
5
6#[derive(Debug, Clone)]
8pub struct RaycastHit {
9 pub glyph_id: GlyphId,
10 pub distance: f32,
11 pub point: Vec3,
12 pub normal: Vec3,
13}
14
15#[derive(Debug, Clone)]
17pub struct SphereQuery {
18 pub center: Vec3,
19 pub radius: f32,
20 pub mask: u32,
21}
22
23#[derive(Debug, Clone)]
25pub struct FrustumQuery {
26 pub planes: [(Vec3, f32); 6],
28}
29
30impl FrustumQuery {
31 pub fn contains_aabb(&self, min: Vec3, max: Vec3) -> bool {
33 for (normal, d) in &self.planes {
34 let p = Vec3::new(
36 if normal.x >= 0.0 { max.x } else { min.x },
37 if normal.y >= 0.0 { max.y } else { min.y },
38 if normal.z >= 0.0 { max.z } else { min.z },
39 );
40 if normal.dot(p) + d < 0.0 { return false; }
41 }
42 true
43 }
44
45 pub fn contains_sphere(&self, center: Vec3, radius: f32) -> bool {
47 for (normal, d) in &self.planes {
48 if normal.dot(center) + d < -radius { return false; }
49 }
50 true
51 }
52}
53
54pub struct SceneQuery;
56
57impl SceneQuery {
58 pub fn frustum_from_vp(vp: &glam::Mat4) -> FrustumQuery {
60 let cols = vp.to_cols_array_2d();
61 let planes = [
63 (Vec3::new(cols[0][3] + cols[0][0], cols[1][3] + cols[1][0], cols[2][3] + cols[2][0]),
65 cols[3][3] + cols[3][0]),
66 (Vec3::new(cols[0][3] - cols[0][0], cols[1][3] - cols[1][0], cols[2][3] - cols[2][0]),
68 cols[3][3] - cols[3][0]),
69 (Vec3::new(cols[0][3] + cols[0][1], cols[1][3] + cols[1][1], cols[2][3] + cols[2][1]),
71 cols[3][3] + cols[3][1]),
72 (Vec3::new(cols[0][3] - cols[0][1], cols[1][3] - cols[1][1], cols[2][3] - cols[2][1]),
74 cols[3][3] - cols[3][1]),
75 (Vec3::new(cols[0][3] + cols[0][2], cols[1][3] + cols[1][2], cols[2][3] + cols[2][2]),
77 cols[3][3] + cols[3][2]),
78 (Vec3::new(cols[0][3] - cols[0][2], cols[1][3] - cols[1][2], cols[2][3] - cols[2][2]),
80 cols[3][3] - cols[3][2]),
81 ];
82 FrustumQuery { planes }
83 }
84}