Skip to main content

euv_core/game/engine3d/lighting/
struct.rs

1use super::*;
2
3/// Dynamic physically based omnidirectional 3D point light for UE5 Clustered Deferred Shading.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct PointLight3D {
6    /// World-space light center position in meters.
7    #[get(type(copy))]
8    pub(crate) position: Vec3,
9    /// Linear HDR RGB emission color.
10    #[get(type(copy))]
11    pub(crate) color: Vec3,
12    /// Luminous intensity multiplier.
13    #[get(type(copy))]
14    pub(crate) intensity: f32,
15    /// Maximum attenuation influence radius in meters.
16    #[get(type(copy))]
17    pub(crate) radius: f32,
18    /// Whether this light casts local shadows when enabled.
19    #[get(type(copy))]
20    pub(crate) cast_shadows: bool,
21}
22
23/// Dynamic physically based 3D spotlight (vehicle headlights, streetlamps, volumetric beams) for Clustered Deferred Shading.
24#[derive(Clone, Copy, Data, Debug, PartialEq)]
25pub struct SpotLight3D {
26    /// World-space spotlight origin in meters.
27    #[get(type(copy))]
28    pub(crate) position: Vec3,
29    /// Unit world-space beam direction pointing away from the emitter.
30    #[get(type(copy))]
31    pub(crate) direction: Vec3,
32    /// Linear HDR RGB emission color.
33    #[get(type(copy))]
34    pub(crate) color: Vec3,
35    /// Luminous intensity multiplier.
36    #[get(type(copy))]
37    pub(crate) intensity: f32,
38    /// Maximum beam attenuation range in meters.
39    #[get(type(copy))]
40    pub(crate) range: f32,
41    /// Cosine of inner full-brightness half-angle.
42    #[get(type(copy))]
43    pub(crate) inner_cos: f32,
44    /// Cosine of outer cutoff half-angle.
45    #[get(type(copy))]
46    pub(crate) outer_cos: f32,
47}
48
49/// Cache-compact 64-byte unified local light record stored contiguously inside `LightClusterGrid3D`.
50#[repr(C, align(16))]
51#[derive(Clone, Copy, Debug, Default, PartialEq)]
52pub(crate) struct PackedLocalLight {
53    /// World position `(x, y, z)` and inverse squared radius `1 / r^2`.
54    pub(crate) pos_inv_r2: [f32; 4],
55    /// Pre-scaled linear HDR radiance `(r, g, b)` and radius `r`.
56    pub(crate) radiance_radius: [f32; 4],
57    /// Unit beam direction `(dx, dy, dz)` and `outer_cos` (`-2.0` marks omnidirectional point light).
58    pub(crate) dir_outer_cos: [f32; 4],
59    /// Reciprocal cone transition `1 / (inner_cos - outer_cos)` and padding.
60    pub(crate) inv_cone_span: [f32; 4],
61}
62
63/// UE5-style 3D Froxel Clustered Deferred Lighting grid binning up to 256 dynamic point and spot lights with zero per-frame heap allocations.
64#[derive(Clone, Data, Debug)]
65pub struct LightClusterGrid3D {
66    /// Near frustum depth bound in meters for logarithmic Z slicing.
67    #[get(type(copy))]
68    pub(crate) near_plane: f32,
69    /// Far frustum depth bound in meters for logarithmic Z slicing.
70    #[get(type(copy))]
71    pub(crate) far_plane: f32,
72    /// Precomputed `LIGHT_CLUSTER_GRID_Z / ln(far / near)` mapping camera depth to Z slice in O(1).
73    #[get(type(copy))]
74    pub(crate) log_depth_scale: f32,
75    /// Precomputed upper depth boundary in meters for each of the 16 logarithmic Z slices (zero `ln()` calls during shading).
76    #[get(skip)]
77    #[get_mut(skip)]
78    #[set(skip)]
79    pub(crate) depth_slice_max: [f32; LIGHT_CLUSTER_GRID_Z],
80    /// Active packed local light count for the current frame.
81    #[get(type(copy))]
82    pub(crate) active_light_count: usize,
83    /// Total light-to-froxel assignments recorded during the latest cluster build.
84    #[get(type(copy))]
85    pub(crate) total_assignments: usize,
86    /// Contiguous packed point and spot light records.
87    #[get(skip)]
88    #[get_mut(skip)]
89    #[set(skip)]
90    pub(crate) packed_lights: Vec<PackedLocalLight>,
91    /// Number of lights assigned to each froxel (`len == 16 * 9 * 16`).
92    #[get(skip)]
93    #[get_mut(skip)]
94    #[set(skip)]
95    pub(crate) cluster_counts: Vec<u16>,
96    /// Flat froxel light index table (`len == 16 * 9 * 16 * 16`).
97    #[get(skip)]
98    #[get_mut(skip)]
99    #[set(skip)]
100    pub(crate) cluster_indices: Vec<u16>,
101}
102
103/// Single orthographic directional shadow cascade inside `VirtualShadowMap` with static page caching.
104#[derive(Clone, Data, Debug)]
105pub struct VsmCascade {
106    /// Cascade texture edge resolution in texels.
107    #[get(type(copy))]
108    pub(crate) resolution: usize,
109    /// World-space half-extent covered by this cascade in meters.
110    #[get(type(copy))]
111    pub(crate) half_extent_m: f32,
112    /// Snapped world-space cascade center preventing sub-texel shadow swimming.
113    #[get(type(copy))]
114    pub(crate) snapped_center: Vec3,
115    /// Light-space right basis vector.
116    #[get(type(copy))]
117    pub(crate) light_right: Vec3,
118    /// Light-space up basis vector.
119    #[get(type(copy))]
120    pub(crate) light_up: Vec3,
121    /// Light-space forward direction pointing toward the sun.
122    #[get(type(copy))]
123    pub(crate) light_forward: Vec3,
124    /// World-space projection depth range `(min_z, max_z)` along `light_forward`.
125    #[get(type(copy))]
126    pub(crate) depth_min: f32,
127    /// Maximum light-space depth along `light_forward`.
128    #[get(type(copy))]
129    pub(crate) depth_max: f32,
130    /// Whether the cached static page depth buffer is clean and valid.
131    #[get(type(copy))]
132    pub(crate) static_valid: bool,
133    /// Cached static geometry shadow depth map (`0.0..=1.0`, `1.0` = far).
134    #[get(skip)]
135    #[get_mut(skip)]
136    #[set(skip)]
137    pub(crate) static_depth: Vec<f32>,
138    /// Composite active shadow depth map combining static cache plus dynamic casters.
139    #[get(skip)]
140    #[get_mut(skip)]
141    #[set(skip)]
142    pub(crate) active_depth: Vec<f32>,
143}
144
145/// UE5-style 2-Cascade Virtual Shadow Map with texel-snapped static page caching and 4-tap PCF soft filtering.
146#[derive(Clone, Data, Debug)]
147pub struct VirtualShadowMap {
148    /// Near high-resolution shadow cascade (`0..36m`).
149    pub(crate) cascade_near: VsmCascade,
150    /// Far city-wide shadow cascade (`36..120m`).
151    pub(crate) cascade_far: VsmCascade,
152    /// Cached sun direction validating static shadow pages.
153    #[get(type(copy))]
154    pub(crate) cached_sun_dir: Vec3,
155    /// Cached `World3D::static_bvh_rebuilds` counter validating static shadow pages.
156    #[get(type(copy))]
157    pub(crate) cached_static_epoch: u64,
158    /// Total static cascade page cache hits (zero-re-rasterize frames).
159    #[get(type(copy))]
160    pub(crate) static_cache_hits: u64,
161    /// Total static cascade rebuilds executed.
162    #[get(type(copy))]
163    pub(crate) static_rebuilds: u64,
164}