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 AABB for O(1) frustum culling and chunk streaming.
82    #[get(type(copy))]
83    pub(crate) world_bounds: Aabb3D,
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/// Object-space raytracing instance descriptor enabling exact interpolated dynamic shadows and reflections with zero BVH rebuilds per VSync.
122#[derive(Clone, Copy, Debug)]
123pub(crate) struct DynamicRayInstance {
124    /// Owning scene object index in `World3D::objects`.
125    pub(crate) obj_idx: usize,
126    /// Registered mesh index referencing `World3D::mesh_local_bvhs`.
127    pub(crate) mesh_id: usize,
128    /// World-space AABB for O(1) miss rejection before transforming rays into object space.
129    pub(crate) world_bounds: Aabb3D,
130    /// Exact analytical inverse model matrix (`S^-1 * R^T * T^-1`) mapping world rays to local space.
131    pub(crate) inv_model: Mat4,
132    /// Inverse-transpose 3x3 normal basis mapping local hit normals back to world space.
133    pub(crate) normal_basis: [Vec3; 3],
134    /// Surface PBR material override for this instance.
135    pub(crate) material: PbrMaterial,
136}
137
138/// Complete open-world 3D scene graph with mesh instancing library, chunk streaming, and BVH acceleration.
139#[derive(Clone, Data, Debug)]
140pub struct World3D {
141    /// Shared LOD mesh asset library referenced by `SceneObject3D::mesh_id`.
142    pub(crate) mesh_library: Vec<MeshLodChain>,
143    /// All world objects partitioned into open-world chunks.
144    pub(crate) objects: Vec<SceneObject3D>,
145    /// Active 3D perspective camera.
146    #[get(type(copy))]
147    pub(crate) camera: Camera3D,
148    /// Active directional sun/sky lighting.
149    #[get(type(copy))]
150    pub(crate) sun: DirectionalLight3D,
151    /// Flat BVH built over static world-space ray-trace casters (rebuilt only on static geometry edits).
152    pub(crate) bvh: Bvh3D,
153    /// Separate flat BVH over moving/dynamic ray-trace casters supporting O(N) incremental bottom-up refits.
154    pub(crate) dynamic_bvh: Bvh3D,
155    /// Lazily constructed local-space BVH4/BVH8 cache indexed by `mesh_id` for zero-rebuild object-space dynamic raytracing.
156    #[get(skip)]
157    #[get_mut(skip)]
158    #[set(skip)]
159    pub(crate) mesh_local_bvhs: Vec<Option<Bvh3D>>,
160    /// Active object-space dynamic raytracing instances updated via inverse interpolated `Mat4` without BVH rebuilds.
161    #[get(skip)]
162    #[get_mut(skip)]
163    #[set(skip)]
164    pub(crate) dynamic_instances: Vec<DynamicRayInstance>,
165    /// Dirty flag triggering static BVH rebuild when static casters are added or modified.
166    #[get(type(copy))]
167    pub(crate) bvh_dirty: bool,
168    /// Dirty flag triggering dynamic BVH incremental refit or rebuild when moving casters update.
169    #[get(type(copy))]
170    pub(crate) dynamic_bvh_dirty: bool,
171    /// Topology change flag forcing a fresh dynamic BVH SAH build rather than an in-place refit.
172    #[get(type(copy))]
173    pub(crate) dynamic_topology_dirty: bool,
174    /// Total static BVH full rebuilds executed.
175    #[get(type(copy))]
176    pub(crate) static_bvh_rebuilds: u64,
177    /// Total zero-allocation incremental dynamic BVH refits executed.
178    #[get(type(copy))]
179    pub(crate) dynamic_bvh_refits: u64,
180    /// Total full dynamic BVH rebuilds executed (topology changes or SAH quality resets).
181    #[get(type(copy))]
182    pub(crate) dynamic_bvh_rebuilds: u64,
183    /// Monotonic revision incremented only when 3D world transforms, camera, or lighting change (isolated from HUD dirtiness).
184    #[get(type(copy))]
185    pub(crate) world_revision: u64,
186    /// Reusable scratch buffer holding transformed dynamic caster triangles during incremental BVH refits.
187    #[get(skip)]
188    #[get_mut(skip)]
189    #[set(skip)]
190    pub(crate) dynamic_tris_scratch: Vec<BvhTriangle>,
191    /// Open-world chunk size in meters (`64.0m` per city block).
192    #[get(type(copy))]
193    pub(crate) chunk_size: f32,
194    /// Index of the drivable player vehicle object if spawned.
195    #[get(skip)]
196    pub(crate) player_vehicle_index: Option<usize>,
197    /// Total imported external 3D models (`.obj` / `.gltf`).
198    #[get(type(copy))]
199    pub(crate) imported_model_count: usize,
200    /// Active 3D camera mode index (`0=Chase`, `1=Hood`, `2=Orbit`, `3=Aerial`, `4=FreeFly`, `5=CinematicDolly`).
201    #[get(type(copy))]
202    pub(crate) camera_mode: u8,
203    /// Continuous orbit angle in radians used for `Orbit` and `CinematicDolly` modes.
204    #[get(type(copy))]
205    pub(crate) camera_orbit_rad: f32,
206}