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/// Physically based Blender rectangular or disk area light evaluated with representative-point GGX and horizon-clamped irradiance.
50#[derive(Clone, Copy, Data, Debug, PartialEq)]
51pub struct AreaLight3D {
52    /// World-space emitter center position in meters.
53    #[get(type(copy))]
54    pub(crate) position: Vec3,
55    /// Unit emitting surface normal pointing outward from the light plane.
56    #[get(type(copy))]
57    pub(crate) direction: Vec3,
58    /// Unit tangent axis along emitter width.
59    #[get(type(copy))]
60    pub(crate) right: Vec3,
61    /// Unit bitangent axis along emitter height.
62    #[get(type(copy))]
63    pub(crate) up: Vec3,
64    /// Linear HDR RGB emission color.
65    #[get(type(copy))]
66    pub(crate) color: Vec3,
67    /// Luminous radiance intensity multiplier.
68    #[get(type(copy))]
69    pub(crate) intensity: f32,
70    /// Emitter aperture width in meters.
71    #[get(type(copy))]
72    pub(crate) width: f32,
73    /// Emitter aperture height in meters.
74    #[get(type(copy))]
75    pub(crate) height: f32,
76    /// Smooth attenuation cutoff range in meters (`KHR_lights_punctual` style window).
77    #[get(type(copy))]
78    pub(crate) range: f32,
79    /// Aperture shape (`Rectangle` or `Disk`).
80    #[get(type(copy))]
81    pub(crate) shape: AreaLightShape3D,
82    /// Whether the emitter radiates from both front and back faces.
83    #[get(type(copy))]
84    pub(crate) two_sided: bool,
85}
86
87/// Cache-compact 64-byte unified local light record stored contiguously inside `LightClusterGrid3D`.
88#[repr(C, align(16))]
89#[derive(Clone, Copy, Debug, Default, PartialEq)]
90pub(crate) struct PackedLocalLight {
91    /// World position `(x, y, z)` and inverse squared radius `1 / r^2`.
92    pub(crate) pos_inv_r2: [f32; 4],
93    /// Pre-scaled linear HDR radiance `(r, g, b)` and radius `r`.
94    pub(crate) radiance_radius: [f32; 4],
95    /// Unit beam direction `(dx, dy, dz)` and `outer_cos` (`-2.0` marks omnidirectional point light).
96    pub(crate) dir_outer_cos: [f32; 4],
97    /// Reciprocal cone transition `1 / (inner_cos - outer_cos)` and padding.
98    pub(crate) inv_cone_span: [f32; 4],
99}
100
101/// UE5-style 3D Froxel Clustered Deferred Lighting grid binning up to 256 dynamic point and spot lights with zero per-frame heap allocations.
102#[derive(Clone, Data, Debug)]
103pub struct LightClusterGrid3D {
104    /// Near frustum depth bound in meters for logarithmic Z slicing.
105    #[get(type(copy))]
106    pub(crate) near_plane: f32,
107    /// Far frustum depth bound in meters for logarithmic Z slicing.
108    #[get(type(copy))]
109    pub(crate) far_plane: f32,
110    /// Precomputed `LIGHT_CLUSTER_GRID_Z / ln(far / near)` mapping camera depth to Z slice in O(1).
111    #[get(type(copy))]
112    pub(crate) log_depth_scale: f32,
113    /// Precomputed upper depth boundary in meters for each of the 16 logarithmic Z slices (zero `ln()` calls during shading).
114    #[get(skip)]
115    #[get_mut(skip)]
116    #[set(skip)]
117    pub(crate) depth_slice_max: [f32; LIGHT_CLUSTER_GRID_Z],
118    /// Active packed local light count for the current frame.
119    #[get(type(copy))]
120    pub(crate) active_light_count: usize,
121    /// Total light-to-froxel assignments recorded during the latest cluster build.
122    #[get(type(copy))]
123    pub(crate) total_assignments: usize,
124    /// Contiguous packed point and spot light records.
125    #[get(skip)]
126    #[get_mut(skip)]
127    #[set(skip)]
128    pub(crate) packed_lights: Vec<PackedLocalLight>,
129    /// Number of lights assigned to each froxel (`len == 16 * 9 * 16`).
130    #[get(skip)]
131    #[get_mut(skip)]
132    #[set(skip)]
133    pub(crate) cluster_counts: Vec<u16>,
134    /// Flat froxel light index table (`len == 16 * 9 * 16 * 16`).
135    #[get(skip)]
136    #[get_mut(skip)]
137    #[set(skip)]
138    pub(crate) cluster_indices: Vec<u16>,
139}
140
141/// Single orthographic directional shadow cascade inside `VirtualShadowMap` with static page caching.
142#[derive(Clone, Data, Debug)]
143pub struct VsmCascade {
144    /// Cascade texture edge resolution in texels.
145    #[get(type(copy))]
146    pub(crate) resolution: usize,
147    /// World-space half-extent covered by this cascade in meters.
148    #[get(type(copy))]
149    pub(crate) half_extent_m: f32,
150    /// Snapped world-space cascade center preventing sub-texel shadow swimming.
151    #[get(type(copy))]
152    pub(crate) snapped_center: Vec3,
153    /// Light-space right basis vector.
154    #[get(type(copy))]
155    pub(crate) light_right: Vec3,
156    /// Light-space up basis vector.
157    #[get(type(copy))]
158    pub(crate) light_up: Vec3,
159    /// Light-space forward direction pointing toward the sun.
160    #[get(type(copy))]
161    pub(crate) light_forward: Vec3,
162    /// World-space projection depth range `(min_z, max_z)` along `light_forward`.
163    #[get(type(copy))]
164    pub(crate) depth_min: f32,
165    /// Maximum light-space depth along `light_forward`.
166    #[get(type(copy))]
167    pub(crate) depth_max: f32,
168    /// Whether the cached static page depth buffer is clean and valid.
169    #[get(type(copy))]
170    pub(crate) static_valid: bool,
171    /// Cached static geometry shadow depth map (`0.0..=1.0`, `1.0` = far).
172    #[get(skip)]
173    #[get_mut(skip)]
174    #[set(skip)]
175    pub(crate) static_depth: Vec<f32>,
176    /// Composite active shadow depth map combining static cache plus dynamic casters.
177    #[get(skip)]
178    #[get_mut(skip)]
179    #[set(skip)]
180    pub(crate) active_depth: Vec<f32>,
181}
182
183/// 4-Cascade Practical Split Cascaded Shadow Map with texel-snapped static page caching, real-mesh rasterization, and rotated 16-tap Vogel-disk PCF.
184#[derive(Clone, Data, Debug)]
185pub struct VirtualShadowMap {
186    /// Four Practical-Split directional cascades ordered from finest near contact (`[0]`) to farthest skyline (`[3]`).
187    pub(crate) cascades: [VsmCascade; VSM_CASCADE_COUNT],
188    /// Camera-space far split distance in meters for each of the four cascades.
189    #[get(type(copy))]
190    pub(crate) cascade_split_depths: [f32; VSM_CASCADE_COUNT],
191    /// Cached sun direction validating static shadow pages.
192    #[get(type(copy))]
193    pub(crate) cached_sun_dir: Vec3,
194    /// Cached `World3D::static_bvh_rebuilds` counter validating static shadow pages.
195    #[get(type(copy))]
196    pub(crate) cached_static_epoch: u64,
197    /// Total static cascade page cache hits (zero-re-rasterize frames).
198    #[get(type(copy))]
199    pub(crate) static_cache_hits: u64,
200    /// Total static cascade rebuilds executed.
201    #[get(type(copy))]
202    pub(crate) static_rebuilds: u64,
203}