Skip to main content

euv_core/game/engine3d/raytrace/accel/
struct.rs

1use super::*;
2
3/// Local-space triangle storing vertices, smooth normals, texture coordinates, and precomputed edges for Möller–Trumbore intersection.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct RtTriangle {
6    /// First vertex position in local mesh space.
7    #[get(type(copy))]
8    pub(crate) v0: Vec3,
9    /// Precomputed edge vector `v1 - v0`.
10    #[get(type(copy))]
11    pub(crate) edge1: Vec3,
12    /// Precomputed edge vector `v2 - v0`.
13    #[get(type(copy))]
14    pub(crate) edge2: Vec3,
15    /// Smooth vertex normal at `v0`.
16    #[get(type(copy))]
17    pub(crate) n0: Vec3,
18    /// Smooth vertex normal at `v1`.
19    #[get(type(copy))]
20    pub(crate) n1: Vec3,
21    /// Smooth vertex normal at `v2`.
22    #[get(type(copy))]
23    pub(crate) n2: Vec3,
24    /// Primary texture coordinate at `v0`.
25    #[get(type(copy))]
26    pub(crate) uv0: Vec2,
27    /// Primary texture coordinate at `v1`.
28    #[get(type(copy))]
29    pub(crate) uv1: Vec2,
30    /// Primary texture coordinate at `v2`.
31    #[get(type(copy))]
32    pub(crate) uv2: Vec2,
33    /// Unit geometric face normal computed from `edge1 × edge2`.
34    #[get(type(copy))]
35    pub(crate) geometric_normal: Vec3,
36    /// Local-space axis-aligned bounding box enclosing the triangle.
37    #[get(type(copy))]
38    pub(crate) bounds: AxisAlignedBox3D,
39    /// Precomputed triangle centroid for SAH partitioning.
40    #[get(type(copy))]
41    pub(crate) centroid: Vec3,
42    /// Per-triangle sub-material slot index (`0` = primary instance material).
43    #[get(type(copy))]
44    pub(crate) material_slot: u16,
45}
46
47/// Flat SAH BVH node inside a `Blas3D` triangle acceleration structure.
48#[derive(Clone, Copy, Data, Debug, PartialEq)]
49pub struct BlasNode3D {
50    /// Local-space bounding box enclosing this node's subtree.
51    #[get(type(copy))]
52    pub(crate) bounds: AxisAlignedBox3D,
53    /// Left child node index (`0` for leaf nodes).
54    #[get(type(copy))]
55    pub(crate) left: u32,
56    /// Right child node index (`0` for leaf nodes).
57    #[get(type(copy))]
58    pub(crate) right: u32,
59    /// Starting triangle index in `Blas3D::triangles` for leaf nodes.
60    #[get(type(copy))]
61    pub(crate) first_tri: u32,
62    /// Number of triangles in this leaf (`0` for interior nodes).
63    #[get(type(copy))]
64    pub(crate) tri_count: u32,
65}
66
67/// Bottom-Level Acceleration Structure (BLAS) storing a SAH-binned triangle BVH in local mesh space.
68#[derive(Clone, Data, Debug, PartialEq)]
69pub struct Blas3D {
70    /// Flat contiguous array of BVH nodes with root at index `0`.
71    pub(crate) nodes: Vec<BlasNode3D>,
72    /// Partitioned local-space triangles referenced by leaf node ranges.
73    pub(crate) triangles: Vec<RtTriangle>,
74    /// Overall local-space bounding box enclosing all triangles in the BLAS.
75    #[get(type(copy))]
76    pub(crate) bounds: AxisAlignedBox3D,
77}
78
79/// Local-space ray-triangle intersection result returned by `Blas3D`.
80#[derive(Clone, Copy, Data, Debug, PartialEq)]
81pub struct RtLocalHit {
82    /// Ray parametric hit distance `t`.
83    #[get(type(copy))]
84    pub(crate) distance: f32,
85    /// Barycentric coordinate `u` along `edge1`.
86    #[get(type(copy))]
87    pub(crate) barycentric_u: f32,
88    /// Barycentric coordinate `v` along `edge2`.
89    #[get(type(copy))]
90    pub(crate) barycentric_v: f32,
91    /// Local-space intersection point.
92    #[get(type(copy))]
93    pub(crate) position: Vec3,
94    /// Local-space geometric face normal.
95    #[get(type(copy))]
96    pub(crate) geometric_normal: Vec3,
97    /// Local-space smoothly interpolated shading normal.
98    #[get(type(copy))]
99    pub(crate) shading_normal: Vec3,
100    /// Interpolated texture coordinate `(u, v)`.
101    #[get(type(copy))]
102    pub(crate) uv: Vec2,
103    /// Index of the intersected triangle inside `Blas3D::triangles`.
104    #[get(type(copy))]
105    pub(crate) triangle_index: u32,
106    /// Per-triangle sub-material slot index.
107    #[get(type(copy))]
108    pub(crate) material_slot: u16,
109}
110
111/// Instanced reference to a shared `Blas3D` with affine transform, cached inverse, normal matrix, and material binding.
112#[derive(Clone, Copy, Data, Debug, PartialEq)]
113pub struct RtInstance {
114    /// Index into the scene's `Blas3D` table.
115    #[get(type(copy))]
116    pub(crate) blas_id: u32,
117    /// Local-to-world 4x4 affine transformation matrix.
118    #[get(type(copy))]
119    pub(crate) transform: Mat4,
120    /// Cached inverse world-to-local 4x4 transformation matrix.
121    #[get(type(copy))]
122    pub(crate) inv_transform: Mat4,
123    /// Cached cofactor-transpose 3x3 normal matrix mapping local normals into world space.
124    #[get(type(copy))]
125    pub(crate) normal_matrix: [Vec3; 3],
126    /// Material index inside `RaytraceScene::materials`.
127    #[get(type(copy))]
128    pub(crate) material_id: u32,
129    /// World-space enclosing axis-aligned bounding box of this instance.
130    #[get(type(copy))]
131    pub(crate) world_bounds: AxisAlignedBox3D,
132    /// Whether this instance participates in ray intersection queries.
133    #[get(type(copy))]
134    pub(crate) visible: bool,
135    /// Sign of the linear transform determinant (`+1.0` or `-1.0`) for orientation preservation under mirrored scales.
136    #[get(type(copy))]
137    pub(crate) determinant_sign: f32,
138}
139
140/// Flat SAH BVH node inside a `Tlas3D` top-level instance acceleration structure.
141#[derive(Clone, Copy, Data, Debug, PartialEq)]
142pub struct TlasNode3D {
143    /// World-space bounding box enclosing all instances under this node.
144    #[get(type(copy))]
145    pub(crate) bounds: AxisAlignedBox3D,
146    /// Left child node index (`0` for leaf nodes).
147    #[get(type(copy))]
148    pub(crate) left: u32,
149    /// Right child node index (`0` for leaf nodes).
150    #[get(type(copy))]
151    pub(crate) right: u32,
152    /// First index into `Tlas3D::instance_order` for leaf nodes.
153    #[get(type(copy))]
154    pub(crate) first_instance: u32,
155    /// Number of instances stored in this leaf (`0` for interior nodes).
156    #[get(type(copy))]
157    pub(crate) instance_count: u32,
158}
159
160/// Top-Level Acceleration Structure (TLAS) over dynamic `RtInstance` objects supporting SAH rebuild and zero-alloc bottom-up refit.
161#[derive(Clone, Data, Debug, PartialEq)]
162pub struct Tlas3D {
163    /// Flat contiguous array of top-level BVH nodes with root at index `0`.
164    pub(crate) nodes: Vec<TlasNode3D>,
165    /// Stable table of scene instances addressed by caller-held `instance_id`.
166    pub(crate) instances: Vec<RtInstance>,
167    /// Indirection array partitioning `instance_id`s across leaf nodes without mutating stable IDs.
168    pub(crate) instance_order: Vec<u32>,
169    /// World-space bounding box enclosing the entire TLAS.
170    #[get(type(copy))]
171    pub(crate) bounds: AxisAlignedBox3D,
172    /// Total number of incremental bottom-up `refit()` operations executed.
173    #[get(type(copy))]
174    pub(crate) refit_count: u64,
175    /// Total number of full SAH `rebuild()` operations executed.
176    #[get(type(copy))]
177    pub(crate) rebuild_count: u64,
178}
179
180/// World-space ray intersection record unified across triangles, analytic primitives, and SDF sphere tracing.
181#[derive(Clone, Copy, Data, Debug, PartialEq)]
182pub struct RtHit {
183    /// World-space parametric hit distance `t` along the normalized ray.
184    #[get(type(copy))]
185    pub(crate) distance: f32,
186    /// World-space intersection position.
187    #[get(type(copy))]
188    pub(crate) point: Vec3,
189    /// Unit world-space geometric normal always oriented against the incoming ray.
190    #[get(type(copy))]
191    pub(crate) geometric_normal: Vec3,
192    /// Unit world-space shading normal oriented into the same hemisphere as `geometric_normal`.
193    #[get(type(copy))]
194    pub(crate) shading_normal: Vec3,
195    /// Surface texture coordinate `(u, v)`.
196    #[get(type(copy))]
197    pub(crate) uv: Vec2,
198    /// Material index inside `RaytraceScene::materials`.
199    #[get(type(copy))]
200    pub(crate) material_id: u32,
201    /// Intersected TLAS instance index, or `RT_PRIMITIVE_INSTANCE_ID` for analytic/SDF hits.
202    #[get(type(copy))]
203    pub(crate) instance_id: u32,
204    /// Triangle index within the BLAS, or analytic/SDF primitive index.
205    #[get(type(copy))]
206    pub(crate) primitive_index: u32,
207    /// `true` when the ray struck the exterior front face; `false` when exiting the interior.
208    #[get(type(copy))]
209    pub(crate) front_face: bool,
210}
211
212/// Bound analytic primitive pairing an `RtPrimitiveShape` with a material index.
213#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
214pub struct RtPrimitive {
215    /// Analytic primitive geometry.
216    #[get(type(copy))]
217    pub(crate) shape: RtPrimitiveShape,
218    /// Material index inside `RaytraceScene::materials`.
219    #[get(type(copy))]
220    pub(crate) material_id: u32,
221}
222
223/// Lipschitz sphere-tracing intersector for implicit SDF/CSG surfaces with analytic central-difference normals.
224#[derive(Clone, Data, Debug, PartialEq)]
225pub struct SdfSphereTracer {
226    /// Flat array of SDF primitive and CSG operator nodes evaluated iteratively with zero heap allocation.
227    pub(crate) ops: Vec<RtSdfOp>,
228    /// Root node index in `ops` evaluated as the surface distance field.
229    #[get(type(copy))]
230    pub(crate) root_index: u32,
231    /// Material index inside `RaytraceScene::materials`.
232    #[get(type(copy))]
233    pub(crate) material_id: u32,
234    /// Conservative world-space bounding box culling rays that miss the implicit volume.
235    #[get(type(copy))]
236    pub(crate) bounds: AxisAlignedBox3D,
237    /// Maximum sphere-marching iterations per ray.
238    #[get(type(copy))]
239    pub(crate) max_steps: u32,
240    /// Surface hit distance threshold in meters.
241    #[get(type(copy))]
242    pub(crate) hit_epsilon: f32,
243    /// Finite-difference step size for tetrahedral normal estimation.
244    #[get(type(copy))]
245    pub(crate) normal_epsilon: f32,
246    /// Maximum marching distance along a ray.
247    #[get(type(copy))]
248    pub(crate) max_distance: f32,
249    /// Conservative step multiplier (`<= 1.0`) for Lipschitz-continuous distance fields.
250    #[get(type(copy))]
251    pub(crate) lipschitz_factor: f32,
252}
253
254/// Surface Area Heuristic (SAH) bucket accumulator used during BVH binning.
255#[derive(Clone, Copy, Data, Debug, PartialEq)]
256pub(crate) struct SahBin {
257    /// Combined bounding box of all primitives mapped into this bin.
258    #[get(type(copy))]
259    pub(crate) bounds: AxisAlignedBox3D,
260    /// Number of primitives mapped into this bin.
261    #[get(type(copy))]
262    pub(crate) count: u32,
263}