euv-core 0.57.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Local-space triangle storing vertices, smooth normals, texture coordinates, and precomputed edges for Möller–Trumbore intersection.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct RtTriangle {
    /// First vertex position in local mesh space.
    #[get(type(copy))]
    pub(crate) v0: Vec3,
    /// Precomputed edge vector `v1 - v0`.
    #[get(type(copy))]
    pub(crate) edge1: Vec3,
    /// Precomputed edge vector `v2 - v0`.
    #[get(type(copy))]
    pub(crate) edge2: Vec3,
    /// Smooth vertex normal at `v0`.
    #[get(type(copy))]
    pub(crate) n0: Vec3,
    /// Smooth vertex normal at `v1`.
    #[get(type(copy))]
    pub(crate) n1: Vec3,
    /// Smooth vertex normal at `v2`.
    #[get(type(copy))]
    pub(crate) n2: Vec3,
    /// Primary texture coordinate at `v0`.
    #[get(type(copy))]
    pub(crate) uv0: Vec2,
    /// Primary texture coordinate at `v1`.
    #[get(type(copy))]
    pub(crate) uv1: Vec2,
    /// Primary texture coordinate at `v2`.
    #[get(type(copy))]
    pub(crate) uv2: Vec2,
    /// Unit geometric face normal computed from `edge1 × edge2`.
    #[get(type(copy))]
    pub(crate) geometric_normal: Vec3,
    /// Local-space axis-aligned bounding box enclosing the triangle.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
    /// Precomputed triangle centroid for SAH partitioning.
    #[get(type(copy))]
    pub(crate) centroid: Vec3,
    /// Per-triangle sub-material slot index (`0` = primary instance material).
    #[get(type(copy))]
    pub(crate) material_slot: u16,
}

/// Flat SAH BVH node inside a `Blas3D` triangle acceleration structure.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct BlasNode3D {
    /// Local-space bounding box enclosing this node's subtree.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
    /// Left child node index (`0` for leaf nodes).
    #[get(type(copy))]
    pub(crate) left: u32,
    /// Right child node index (`0` for leaf nodes).
    #[get(type(copy))]
    pub(crate) right: u32,
    /// Starting triangle index in `Blas3D::triangles` for leaf nodes.
    #[get(type(copy))]
    pub(crate) first_tri: u32,
    /// Number of triangles in this leaf (`0` for interior nodes).
    #[get(type(copy))]
    pub(crate) tri_count: u32,
}

/// Bottom-Level Acceleration Structure (BLAS) storing a SAH-binned triangle BVH in local mesh space.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct Blas3D {
    /// Flat contiguous array of BVH nodes with root at index `0`.
    pub(crate) nodes: Vec<BlasNode3D>,
    /// Partitioned local-space triangles referenced by leaf node ranges.
    pub(crate) triangles: Vec<RtTriangle>,
    /// Overall local-space bounding box enclosing all triangles in the BLAS.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
}

/// Local-space ray-triangle intersection result returned by `Blas3D`.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct RtLocalHit {
    /// Ray parametric hit distance `t`.
    #[get(type(copy))]
    pub(crate) distance: f32,
    /// Barycentric coordinate `u` along `edge1`.
    #[get(type(copy))]
    pub(crate) barycentric_u: f32,
    /// Barycentric coordinate `v` along `edge2`.
    #[get(type(copy))]
    pub(crate) barycentric_v: f32,
    /// Local-space intersection point.
    #[get(type(copy))]
    pub(crate) position: Vec3,
    /// Local-space geometric face normal.
    #[get(type(copy))]
    pub(crate) geometric_normal: Vec3,
    /// Local-space smoothly interpolated shading normal.
    #[get(type(copy))]
    pub(crate) shading_normal: Vec3,
    /// Interpolated texture coordinate `(u, v)`.
    #[get(type(copy))]
    pub(crate) uv: Vec2,
    /// Index of the intersected triangle inside `Blas3D::triangles`.
    #[get(type(copy))]
    pub(crate) triangle_index: u32,
    /// Per-triangle sub-material slot index.
    #[get(type(copy))]
    pub(crate) material_slot: u16,
}

/// Instanced reference to a shared `Blas3D` with affine transform, cached inverse, normal matrix, and material binding.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct RtInstance {
    /// Index into the scene's `Blas3D` table.
    #[get(type(copy))]
    pub(crate) blas_id: u32,
    /// Local-to-world 4x4 affine transformation matrix.
    #[get(type(copy))]
    pub(crate) transform: Mat4,
    /// Cached inverse world-to-local 4x4 transformation matrix.
    #[get(type(copy))]
    pub(crate) inv_transform: Mat4,
    /// Cached cofactor-transpose 3x3 normal matrix mapping local normals into world space.
    #[get(type(copy))]
    pub(crate) normal_matrix: [Vec3; 3],
    /// Material index inside `RaytraceScene::materials`.
    #[get(type(copy))]
    pub(crate) material_id: u32,
    /// World-space enclosing axis-aligned bounding box of this instance.
    #[get(type(copy))]
    pub(crate) world_bounds: AxisAlignedBox3D,
    /// Whether this instance participates in ray intersection queries.
    #[get(type(copy))]
    pub(crate) visible: bool,
    /// Sign of the linear transform determinant (`+1.0` or `-1.0`) for orientation preservation under mirrored scales.
    #[get(type(copy))]
    pub(crate) determinant_sign: f32,
}

/// Flat SAH BVH node inside a `Tlas3D` top-level instance acceleration structure.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct TlasNode3D {
    /// World-space bounding box enclosing all instances under this node.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
    /// Left child node index (`0` for leaf nodes).
    #[get(type(copy))]
    pub(crate) left: u32,
    /// Right child node index (`0` for leaf nodes).
    #[get(type(copy))]
    pub(crate) right: u32,
    /// First index into `Tlas3D::instance_order` for leaf nodes.
    #[get(type(copy))]
    pub(crate) first_instance: u32,
    /// Number of instances stored in this leaf (`0` for interior nodes).
    #[get(type(copy))]
    pub(crate) instance_count: u32,
}

/// Top-Level Acceleration Structure (TLAS) over dynamic `RtInstance` objects supporting SAH rebuild and zero-alloc bottom-up refit.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct Tlas3D {
    /// Flat contiguous array of top-level BVH nodes with root at index `0`.
    pub(crate) nodes: Vec<TlasNode3D>,
    /// Stable table of scene instances addressed by caller-held `instance_id`.
    pub(crate) instances: Vec<RtInstance>,
    /// Indirection array partitioning `instance_id`s across leaf nodes without mutating stable IDs.
    pub(crate) instance_order: Vec<u32>,
    /// World-space bounding box enclosing the entire TLAS.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
    /// Total number of incremental bottom-up `refit()` operations executed.
    #[get(type(copy))]
    pub(crate) refit_count: u64,
    /// Total number of full SAH `rebuild()` operations executed.
    #[get(type(copy))]
    pub(crate) rebuild_count: u64,
}

/// World-space ray intersection record unified across triangles, analytic primitives, and SDF sphere tracing.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct RtHit {
    /// World-space parametric hit distance `t` along the normalized ray.
    #[get(type(copy))]
    pub(crate) distance: f32,
    /// World-space intersection position.
    #[get(type(copy))]
    pub(crate) point: Vec3,
    /// Unit world-space geometric normal always oriented against the incoming ray.
    #[get(type(copy))]
    pub(crate) geometric_normal: Vec3,
    /// Unit world-space shading normal oriented into the same hemisphere as `geometric_normal`.
    #[get(type(copy))]
    pub(crate) shading_normal: Vec3,
    /// Surface texture coordinate `(u, v)`.
    #[get(type(copy))]
    pub(crate) uv: Vec2,
    /// Material index inside `RaytraceScene::materials`.
    #[get(type(copy))]
    pub(crate) material_id: u32,
    /// Intersected TLAS instance index, or `RT_PRIMITIVE_INSTANCE_ID` for analytic/SDF hits.
    #[get(type(copy))]
    pub(crate) instance_id: u32,
    /// Triangle index within the BLAS, or analytic/SDF primitive index.
    #[get(type(copy))]
    pub(crate) primitive_index: u32,
    /// `true` when the ray struck the exterior front face; `false` when exiting the interior.
    #[get(type(copy))]
    pub(crate) front_face: bool,
}

/// Bound analytic primitive pairing an `RtPrimitiveShape` with a material index.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct RtPrimitive {
    /// Analytic primitive geometry.
    #[get(type(copy))]
    pub(crate) shape: RtPrimitiveShape,
    /// Material index inside `RaytraceScene::materials`.
    #[get(type(copy))]
    pub(crate) material_id: u32,
}

/// Lipschitz sphere-tracing intersector for implicit SDF/CSG surfaces with analytic central-difference normals.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct SdfSphereTracer {
    /// Flat array of SDF primitive and CSG operator nodes evaluated iteratively with zero heap allocation.
    pub(crate) ops: Vec<RtSdfOp>,
    /// Root node index in `ops` evaluated as the surface distance field.
    #[get(type(copy))]
    pub(crate) root_index: u32,
    /// Material index inside `RaytraceScene::materials`.
    #[get(type(copy))]
    pub(crate) material_id: u32,
    /// Conservative world-space bounding box culling rays that miss the implicit volume.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
    /// Maximum sphere-marching iterations per ray.
    #[get(type(copy))]
    pub(crate) max_steps: u32,
    /// Surface hit distance threshold in meters.
    #[get(type(copy))]
    pub(crate) hit_epsilon: f32,
    /// Finite-difference step size for tetrahedral normal estimation.
    #[get(type(copy))]
    pub(crate) normal_epsilon: f32,
    /// Maximum marching distance along a ray.
    #[get(type(copy))]
    pub(crate) max_distance: f32,
    /// Conservative step multiplier (`<= 1.0`) for Lipschitz-continuous distance fields.
    #[get(type(copy))]
    pub(crate) lipschitz_factor: f32,
}

/// Surface Area Heuristic (SAH) bucket accumulator used during BVH binning.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub(crate) struct SahBin {
    /// Combined bounding box of all primitives mapped into this bin.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
    /// Number of primitives mapped into this bin.
    #[get(type(copy))]
    pub(crate) count: u32,
}