Skip to main content

euv_core/game/engine3d/bvh/
struct.rs

1use super::*;
2
3/// World-space triangle stored in the flat BVH leaf array for ray-traced shadows, reflections, and GI.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct BvhTriangle {
6    /// First vertex position in world space.
7    #[get(type(copy))]
8    pub(crate) v0: Vec3,
9    /// Edge vector `v1 - v0`.
10    #[get(type(copy))]
11    pub(crate) edge1: Vec3,
12    /// Edge vector `v2 - v0`.
13    #[get(type(copy))]
14    pub(crate) edge2: Vec3,
15    /// Unit face normal.
16    #[get(type(copy))]
17    pub(crate) normal: Vec3,
18    /// Precomputed triangle centroid for O(N log N) BVH partitioning.
19    #[get(type(copy))]
20    pub(crate) centroid: Vec3,
21    /// Precomputed triangle bounding box.
22    #[get(type(copy))]
23    pub(crate) bounds: Aabb3D,
24    /// Surface PBR material referenced on reflection/GI hits.
25    #[get(type(copy))]
26    pub(crate) material: PbrMaterial,
27}
28
29/// Cache-friendly flat BVH tree node (interior node or leaf triangle range).
30#[derive(Clone, Copy, Data, Debug, PartialEq)]
31pub struct BvhNode3D {
32    /// World-space AABB enclosing this subtree.
33    #[get(type(copy))]
34    pub(crate) bounds: Aabb3D,
35    /// Left child node index (`0` for leaves).
36    #[get(type(copy))]
37    pub(crate) left: u32,
38    /// Right child node index (`0` for leaves).
39    #[get(type(copy))]
40    pub(crate) right: u32,
41    /// First triangle index in `Bvh3D::triangles` for leaves.
42    #[get(type(copy))]
43    pub(crate) first_tri: u32,
44    /// Triangle count (`> 0` denotes a leaf node).
45    #[get(type(copy))]
46    pub(crate) tri_count: u32,
47}
48
49/// Closest ray-triangle hit result returned by `Bvh3D::trace_closest`.
50#[derive(Clone, Copy, Data, Debug, PartialEq)]
51pub struct RayHit3D {
52    /// Ray parametric hit distance `t`.
53    #[get(type(copy))]
54    pub(crate) distance: f32,
55    /// World-space hit position.
56    #[get(type(copy))]
57    pub(crate) point: Vec3,
58    /// Unit surface normal at the hit point.
59    #[get(type(copy))]
60    pub(crate) normal: Vec3,
61    /// Surface PBR material at the hit point.
62    #[get(type(copy))]
63    pub(crate) material: PbrMaterial,
64}
65
66/// Cache-compact 36-byte hot geometry triangle for L1-dense Möller–Trumbore ray intersections.
67#[derive(Clone, Copy, Debug, PartialEq)]
68pub(crate) struct BvhGeomTri {
69    /// First vertex `(x, y, z)`.
70    pub(crate) v0: [f32; 3],
71    /// First edge `(v1 - v0)`.
72    pub(crate) e1: [f32; 3],
73    /// Second edge `(v2 - v0)`.
74    pub(crate) e2: [f32; 3],
75}
76
77/// L1-cache-aligned 32-byte DFS pre-order BVH node (2 nodes per 64-byte cache line; left child is `idx + 1`).
78#[derive(Clone, Copy, Debug, PartialEq)]
79pub(crate) struct BvhFlatNode {
80    /// Minimum AABB corner `(x, y, z)`.
81    pub(crate) min: [f32; 3],
82    /// Right child node index when `tri_count == 0`, or first triangle index when `tri_count > 0`.
83    pub(crate) right_or_first: u32,
84    /// Maximum AABB corner `(x, y, z)`.
85    pub(crate) max: [f32; 3],
86    /// Leaf triangle count (`0` for interior nodes).
87    pub(crate) tri_count: u32,
88}
89
90/// Flat-array Bounding Volume Hierarchy accelerating ray-traced shadows, reflections, and GI in O(log N).
91#[derive(Clone, Data, Debug, Default)]
92pub struct Bvh3D {
93    /// Contiguous BVH nodes with root at index `0`.
94    pub(crate) nodes: Vec<BvhNode3D>,
95    /// Partitioned world-space triangle metadata and shading materials.
96    pub(crate) triangles: Vec<BvhTriangle>,
97    /// Hot 36-byte packed geometry mirror aligned 1:1 with `triangles` for fast ray intersection.
98    #[get(skip)]
99    #[get_mut(skip)]
100    #[set(skip)]
101    pub(crate) geom: Vec<BvhGeomTri>,
102    /// Hot 32-byte DFS-ordered BVH mirror for branchless cache-dense traversal.
103    #[get(skip)]
104    #[get_mut(skip)]
105    #[set(skip)]
106    pub(crate) flat_nodes: Vec<BvhFlatNode>,
107}