Skip to main content

ursus_core/math/
frustum.rs

1use crate::assets::mesh::Aabb;
2use glam::{Mat4, Vec4};
3
4pub fn extract_planes(view_proj: Mat4) -> [Vec4; 6] {
5    let m = view_proj.transpose();
6    let rows = [m.x_axis, m.y_axis, m.z_axis, m.w_axis];
7
8    [
9        rows[3] + rows[0], // left
10        rows[3] - rows[0], // right
11        rows[3] + rows[1], // bottom
12        rows[3] - rows[1], // top
13        rows[3] + rows[2], // near
14        rows[3] - rows[2], // far
15    ]
16}
17
18pub fn transform_aabb(aabb: &Aabb, model: Mat4) -> Aabb {
19    let corners = [
20        glam::Vec3::new(aabb.min.x, aabb.min.y, aabb.min.z),
21        glam::Vec3::new(aabb.max.x, aabb.min.y, aabb.min.z),
22        glam::Vec3::new(aabb.min.x, aabb.max.y, aabb.min.z),
23        glam::Vec3::new(aabb.max.x, aabb.max.y, aabb.min.z),
24        glam::Vec3::new(aabb.min.x, aabb.min.y, aabb.max.z),
25        glam::Vec3::new(aabb.max.x, aabb.min.y, aabb.max.z),
26        glam::Vec3::new(aabb.min.x, aabb.max.y, aabb.max.z),
27        glam::Vec3::new(aabb.max.x, aabb.max.y, aabb.max.z),
28    ];
29
30    let mut min = glam::Vec3::splat(f32::MAX);
31    let mut max = glam::Vec3::splat(f32::MIN);
32    for c in &corners {
33        let world = model.transform_point3(*c);
34        min = min.min(world);
35        max = max.max(world);
36    }
37    Aabb { min, max }
38}