Skip to main content

euv_core/game/engine3d/world/
struct.rs

1use super::*;
2
3/// 3D world-space transform (translation, yaw/pitch/roll Euler rotation, and non-uniform scale).
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct Transform3D {
6    /// World-space translation `(x, y, z)`.
7    #[get(type(copy))]
8    pub(crate) position: Vec3,
9    /// Horizontal Y-axis yaw rotation in radians.
10    #[get(type(copy))]
11    pub(crate) yaw_rad: f32,
12    /// Vertical X-axis pitch rotation in radians.
13    #[get(type(copy))]
14    pub(crate) pitch_rad: f32,
15    /// Longitudinal Z-axis roll rotation in radians.
16    #[get(type(copy))]
17    pub(crate) roll_rad: f32,
18    /// Scale multiplier along `(x, y, z)`.
19    #[get(type(copy))]
20    pub(crate) scale: Vec3,
21}
22
23/// Fluent descriptor for spawning a `SceneObject3D` into `World3D`.
24#[derive(Clone, Data, Debug, PartialEq)]
25pub struct SpawnObjectDesc {
26    /// Human-readable object label.
27    pub(crate) name: String,
28    /// Registered mesh identifier from `World3D::register_mesh`.
29    #[get(type(copy))]
30    pub(crate) mesh_id: usize,
31    /// World-space TRS transform.
32    #[get(type(copy))]
33    pub(crate) transform: Transform3D,
34    /// Surface PBR material.
35    #[get(type(copy))]
36    pub(crate) material: PbrMaterial,
37    /// Whether this object casts ray-traced shadows and appears in BVH reflections.
38    #[get(type(copy))]
39    pub(crate) ray_trace_caster: bool,
40    /// Continuous spin speed in radians/sec.
41    #[get(type(copy))]
42    pub(crate) spin_speed: f32,
43    /// Whether this object moves dynamically and belongs in the incrementally refitted dynamic BVH.
44    #[get(type(copy))]
45    pub(crate) is_dynamic: bool,
46}
47
48/// Fluent descriptor for importing a 3D `.obj` or `.gltf` model into `World3D`.
49#[derive(Clone, Data, Debug, PartialEq)]
50pub struct ImportModelDesc {
51    /// Source model interchange format.
52    #[get(type(copy))]
53    pub(crate) format: ModelFormat,
54    /// Raw `.obj` or `.gltf` text content.
55    pub(crate) source: String,
56    /// World-space placement transform.
57    #[get(type(copy))]
58    pub(crate) transform: Transform3D,
59    /// Fallback PBR material when source omits material definitions.
60    #[get(type(copy))]
61    pub(crate) fallback_material: PbrMaterial,
62    /// Optional continuous Y-spin speed in radians/sec.
63    #[get(type(copy))]
64    pub(crate) spin_speed: f32,
65}
66
67/// Instanced 3D scene object placed inside an open-world chunk.
68#[derive(Clone, Data, Debug, PartialEq)]
69pub struct SceneObject3D {
70    /// Human-readable object label.
71    pub(crate) name: String,
72    /// Index into `World3D::mesh_library` enabling GPU instancing batching.
73    #[get(type(copy))]
74    pub(crate) mesh_id: usize,
75    /// World-space TRS transform.
76    #[get(type(copy))]
77    pub(crate) transform: Transform3D,
78    /// Material override (or default mesh material).
79    #[get(type(copy))]
80    pub(crate) material: PbrMaterial,
81    /// Cached world-space axis-aligned bounding box for O(1) frustum culling and chunk streaming.
82    #[get(type(copy))]
83    pub(crate) world_bounds: AxisAlignedBox3D,
84    /// Spatial open-world chunk coordinate `(chunk_x, chunk_z)`.
85    #[get(type(copy))]
86    pub(crate) chunk_coord: (i32, i32),
87    /// Whether this object casts ray-traced shadows and appears in BVH reflections.
88    #[get(type(copy))]
89    pub(crate) ray_trace_caster: bool,
90    /// Continuous spin speed in radians/sec for animated hero models or props.
91    #[get(type(copy))]
92    pub(crate) spin_speed: f32,
93    /// Whether this object belongs to the dynamic incrementally-refitted BVH rather than the static world BVH.
94    #[get(type(copy))]
95    pub(crate) is_dynamic: bool,
96    /// Discontinuity counter incremented on teleports so snapshot interpolation snaps cleanly.
97    #[get(type(copy))]
98    pub(crate) teleport_epoch: u32,
99}
100
101/// Primary sun/moon directional light and atmospheric sky parameters.
102#[derive(Clone, Copy, Data, Debug, PartialEq)]
103pub struct DirectionalLight3D {
104    /// Normalized direction pointing toward the sun.
105    #[get(type(copy))]
106    pub(crate) direction: Vec3,
107    /// Linear HDR sun radiance color.
108    #[get(type(copy))]
109    pub(crate) color: Vec3,
110    /// Ambient sky zenith color.
111    #[get(type(copy))]
112    pub(crate) sky_zenith: Vec3,
113    /// Ambient sky horizon and volumetric fog color.
114    #[get(type(copy))]
115    pub(crate) sky_horizon: Vec3,
116    /// Time of day in `[0.0, 24.0]` hours.
117    #[get(type(copy))]
118    pub(crate) time_of_day: f32,
119}
120
121/// Oriented bounding box in 3D space used for tight rotated collider narrow-phase tests.
122#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
123pub struct OrientedBox3D {
124    /// Box center position in target coordinate space.
125    #[get(type(copy))]
126    pub(crate) center: Vec3,
127    /// Orthonormal local X, Y, and Z basis axes.
128    #[get(type(copy))]
129    pub(crate) axes: [Vec3; 3],
130    /// Positive half-extents along each local axis.
131    #[get(type(copy))]
132    pub(crate) half_extents: Vec3,
133}
134
135/// Real-mesh narrow-phase collision contact reported against a solid `SceneObject3D` triangle.
136#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
137pub struct MeshCollisionHit3D {
138    /// Colliding obstacle index inside `World3D::objects`.
139    #[get(type(copy))]
140    pub(crate) object_index: usize,
141    /// Unit world-space separation normal pointing toward the moving collider.
142    #[get(type(copy))]
143    pub(crate) normal: Vec3,
144    /// Positive penetration depth along `normal` in meters.
145    #[get(type(copy))]
146    pub(crate) penetration: f32,
147    /// World-space contact point on the colliding triangle surface.
148    #[get(type(copy))]
149    pub(crate) contact_point: Vec3,
150}
151
152/// Object-space raytracing instance descriptor enabling exact interpolated dynamic shadows and reflections with zero BVH rebuilds per VSync.
153#[derive(Clone, Copy, Debug)]
154pub(crate) struct DynamicRayInstance {
155    /// Owning scene object index in `World3D::objects`.
156    pub(crate) obj_idx: usize,
157    /// Registered mesh index referencing `World3D::mesh_local_bvhs`.
158    pub(crate) mesh_id: usize,
159    /// World-space axis-aligned bounding box for O(1) miss rejection before transforming rays into object space.
160    pub(crate) world_bounds: AxisAlignedBox3D,
161    /// Exact analytical inverse model matrix (`S^-1 * R^T * T^-1`) mapping world rays to local space.
162    pub(crate) inv_model: Mat4,
163    /// Inverse-transpose 3x3 normal basis mapping local hit normals back to world space.
164    pub(crate) normal_basis: [Vec3; 3],
165    /// Surface PBR material override for this instance.
166    pub(crate) material: PbrMaterial,
167}
168
169/// Complete open-world 3D scene graph with mesh instancing library, chunk streaming, and BVH acceleration.
170#[derive(Clone, Data, Debug)]
171pub struct World3D {
172    /// Shared LOD mesh asset library referenced by `SceneObject3D::mesh_id`.
173    pub(crate) mesh_library: Vec<MeshLodChain>,
174    /// All world objects partitioned into open-world chunks.
175    pub(crate) objects: Vec<SceneObject3D>,
176    /// Active 3D perspective camera.
177    #[get(type(copy))]
178    pub(crate) camera: Camera3D,
179    /// Active directional sun/sky lighting.
180    #[get(type(copy))]
181    pub(crate) sun: DirectionalLight3D,
182    /// Flat BVH built over static world-space ray-trace casters (rebuilt only on static geometry edits).
183    pub(crate) bvh: Bvh3D,
184    /// Separate flat BVH over moving/dynamic ray-trace casters supporting O(N) incremental bottom-up refits.
185    pub(crate) dynamic_bvh: Bvh3D,
186    /// Lazily constructed local-space BVH4/BVH8 cache indexed by `mesh_id` for zero-rebuild object-space dynamic raytracing.
187    #[get(skip)]
188    #[get_mut(skip)]
189    #[set(skip)]
190    pub(crate) mesh_local_bvhs: Vec<Option<Bvh3D>>,
191    /// Active object-space dynamic raytracing instances updated via inverse interpolated `Mat4` without BVH rebuilds.
192    #[get(skip)]
193    #[get_mut(skip)]
194    #[set(skip)]
195    pub(crate) dynamic_instances: Vec<DynamicRayInstance>,
196    /// Dirty flag triggering static BVH rebuild when static casters are added or modified.
197    #[get(type(copy))]
198    pub(crate) bvh_dirty: bool,
199    /// Dirty flag triggering dynamic BVH incremental refit or rebuild when moving casters update.
200    #[get(type(copy))]
201    pub(crate) dynamic_bvh_dirty: bool,
202    /// Topology change flag forcing a fresh dynamic BVH SAH build rather than an in-place refit.
203    #[get(type(copy))]
204    pub(crate) dynamic_topology_dirty: bool,
205    /// Total static BVH full rebuilds executed.
206    #[get(type(copy))]
207    pub(crate) static_bvh_rebuilds: u64,
208    /// Total zero-allocation incremental dynamic BVH refits executed.
209    #[get(type(copy))]
210    pub(crate) dynamic_bvh_refits: u64,
211    /// Total full dynamic BVH rebuilds executed (topology changes or SAH quality resets).
212    #[get(type(copy))]
213    pub(crate) dynamic_bvh_rebuilds: u64,
214    /// Monotonic revision incremented only when 3D world transforms, camera, or lighting change (isolated from HUD dirtiness).
215    #[get(type(copy))]
216    pub(crate) world_revision: u64,
217    /// Reusable scratch buffer holding transformed dynamic caster triangles during incremental BVH refits.
218    #[get(skip)]
219    #[get_mut(skip)]
220    #[set(skip)]
221    pub(crate) dynamic_tris_scratch: Vec<BvhTriangle>,
222    /// Open-world chunk size in meters (`64.0m` per city block).
223    #[get(type(copy))]
224    pub(crate) chunk_size: f32,
225    /// Index of the drivable player vehicle object if spawned.
226    #[get(skip)]
227    pub(crate) player_vehicle_index: Option<usize>,
228    /// Total imported external 3D models (`.obj` / `.gltf`).
229    #[get(type(copy))]
230    pub(crate) imported_model_count: usize,
231    /// Active 3D camera mode index (`0=Chase`, `1=Hood`, `2=Orbit`, `3=Aerial`, `4=FreeFly`, `5=CinematicDolly`).
232    #[get(type(copy))]
233    pub(crate) camera_mode: u8,
234    /// Continuous orbit angle in radians used for `Orbit` and `CinematicDolly` modes.
235    #[get(type(copy))]
236    pub(crate) camera_orbit_rad: f32,
237    /// Dynamic world-space omnidirectional point lights binned by `LightClusterGrid3D`.
238    pub(crate) point_lights: Vec<PointLight3D>,
239    /// Dynamic world-space spotlights (vehicle headlights, streetlamps) binned by `LightClusterGrid3D`.
240    pub(crate) spot_lights: Vec<SpotLight3D>,
241    /// Zero-allocation 3D Niagara particle system for sparks, exhaust trails, and atmospheric effects.
242    pub(crate) niagara: NiagaraSystem3D,
243    /// Active atmospheric mood preset (`0=Sunset, 1=RainyNight, 2=Noon, 3=Midnight`).
244    #[get(type(copy))]
245    pub(crate) atmosphere_preset: u8,
246    /// Cruising AI traffic vehicles `(object_index, velocity_z_mps, z_min, z_max)` animated smoothly on outer boulevard lanes.
247    #[get(skip)]
248    #[get_mut(skip)]
249    #[set(skip)]
250    pub(crate) traffic_vehicles: Vec<(usize, f32, f32, f32)>,
251    /// Monotonic simulation clock in seconds driving continuous animated ocean waves, caustics, and shoreline foam.
252    #[get(type(copy))]
253    pub(crate) world_time_secs: f32,
254}