Skip to main content

euv_core/game/fluid_kit/ocean/
struct.rs

1use super::*;
2
3/// Single directional Gerstner wave train synthesized from a JONSWAP sea-state cascade.
4#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
5pub struct GerstnerWaveComponent {
6    /// Unit horizontal propagation direction `(dx, dz)`.
7    #[get(type(copy))]
8    pub(crate) direction: Vec2,
9    /// Spatial wavelength in meters (`lambda > 0`).
10    #[get(type(copy))]
11    pub(crate) wavelength_m: f32,
12    /// Vertical wave amplitude in meters.
13    #[get(type(copy))]
14    pub(crate) amplitude_m: f32,
15    /// Dimensionless horizontal steepness contribution `k * H_i` (`sum <= OCEAN_MAX_TOTAL_STEEPNESS`).
16    #[get(type(copy))]
17    pub(crate) steepness: f32,
18    /// Deterministic initial phase offset in radians.
19    #[get(type(copy))]
20    pub(crate) phase_offset: f32,
21    /// Dispersion angular frequency multiplier.
22    #[get(type(copy))]
23    pub(crate) speed_multiplier: f32,
24    /// Cascade tier index (`0` = macro swell, `1` = mid gravity, `2` = fine chop).
25    #[get(type(copy))]
26    pub(crate) cascade_index: u8,
27}
28
29/// Physical optical shading parameters for ocean Beer-Lambert absorption, Fresnel, SSS, and seabed caustics.
30#[derive(Clone, Copy, Data, Debug, PartialEq)]
31pub struct OceanOpticalConfig {
32    /// Linear RGB Beer-Lambert extinction coefficients in `m^-1`.
33    #[get(type(copy))]
34    pub(crate) absorption_coeff: Vec3,
35    /// Deep abyssal water scattering color in linear RGB.
36    #[get(type(copy))]
37    pub(crate) deep_scatter_color: Vec3,
38    /// Shallow sunlit coastal scatter tint in linear RGB.
39    #[get(type(copy))]
40    pub(crate) shallow_tint_color: Vec3,
41    /// Backlit emerald crest subsurface scattering tint in linear RGB.
42    #[get(type(copy))]
43    pub(crate) sss_color: Vec3,
44    /// Aerated crest whitecap foam linear RGB color.
45    #[get(type(copy))]
46    pub(crate) foam_color: Vec3,
47    /// Schlick Fresnel normal-incidence reflectance (`0.02` for water).
48    #[get(type(copy))]
49    pub(crate) fresnel_f0: f32,
50    /// Backlit crest subsurface scattering intensity gain.
51    #[get(type(copy))]
52    pub(crate) sss_strength: f32,
53    /// Seabed projected caustics intensity multiplier.
54    #[get(type(copy))]
55    pub(crate) caustics_strength: f32,
56    /// Jacobian surface-folding determinant threshold below which whitecap foam emerges.
57    #[get(type(copy))]
58    pub(crate) foam_jacobian_threshold: f32,
59}
60
61/// Complete declarative configuration for `OceanSurface3D`.
62#[derive(Clone, Copy, Data, Debug, PartialEq)]
63pub struct OceanConfig {
64    /// Base sea-state preset controlling default spectral energy and optics.
65    #[get(type(copy))]
66    pub(crate) preset: OceanPreset,
67    /// Wind speed at 10 m reference elevation in meters per second.
68    #[get(type(copy))]
69    pub(crate) wind_speed_mps: f32,
70    /// Dominant wind propagation direction `(dx, dz)` (normalized internally).
71    #[get(type(copy))]
72    pub(crate) wind_direction: Vec2,
73    /// Wind fetch distance over open water in kilometers for JONSWAP peak frequency scaling.
74    #[get(type(copy))]
75    pub(crate) fetch_km: f32,
76    /// Global horizontal Gerstner choppiness multiplier in `[0.0, 1.5]`.
77    #[get(type(copy))]
78    pub(crate) choppiness: f32,
79    /// Mean resting sea-surface elevation along world `+Y` in meters.
80    #[get(type(copy))]
81    pub(crate) mean_water_level_y: f32,
82    /// Gravitational acceleration in `m/s^2` driving deep-water dispersion `omega = sqrt(g * k)`.
83    #[get(type(copy))]
84    pub(crate) gravity_mps2: f32,
85    /// Number of directional wave cascades (`1..=4`).
86    #[get(type(copy))]
87    pub(crate) cascade_count: u8,
88    /// Deterministic PRNG seed for wave phases and directional jitter.
89    #[get(type(copy))]
90    pub(crate) seed: u64,
91    /// Physical optical parameters for absorption, Fresnel, SSS, and caustics.
92    #[get(type(copy))]
93    pub(crate) optics: OceanOpticalConfig,
94}
95
96/// Analytical geometric evaluation of `OceanSurface3D` at a single undisplaced horizontal coordinate.
97#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
98pub struct OceanSurfaceSample {
99    /// Displaced world-space surface position `(x + dx, mean_y + dy, z + dz)`.
100    #[get(type(copy))]
101    pub(crate) displaced_position: Vec3,
102    /// Pure 3D wave displacement vector `(dx, dy, dz)` relative to `(x, mean_y, z)`.
103    #[get(type(copy))]
104    pub(crate) displacement: Vec3,
105    /// Exact analytical upward-pointing unit surface normal `(dP/dz × dP/dx).normalize()`.
106    #[get(type(copy))]
107    pub(crate) normal: Vec3,
108    /// Exact analytical partial derivative `dP/dx`.
109    #[get(type(copy))]
110    pub(crate) tangent_x: Vec3,
111    /// Exact analytical partial derivative `dP/dz`.
112    #[get(type(copy))]
113    pub(crate) tangent_z: Vec3,
114    /// Horizontal surface-folding Jacobian determinant (`< 1.0` compressed crest, `> 1.0` stretched trough).
115    #[get(type(copy))]
116    pub(crate) jacobian: f32,
117    /// Whitecap crest foam coverage mask in `[0.0, 1.0]`.
118    #[get(type(copy))]
119    pub(crate) foam_mask: f32,
120}
121
122/// Physical optical shading evaluation for an `OceanSurfaceSample`.
123#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
124pub struct OceanOpticalSample {
125    /// Beer-Lambert depth transmittance `exp(-absorption_coeff * depth_m)` in `[0.0, 1.0]^3`.
126    #[get(type(copy))]
127    pub(crate) transmittance: Vec3,
128    /// Schlick Fresnel reflection coefficient in `[fresnel_f0, 1.0]`.
129    #[get(type(copy))]
130    pub(crate) fresnel: f32,
131    /// Backlit emerald subsurface scattering linear RGB radiance contribution.
132    #[get(type(copy))]
133    pub(crate) subsurface_scatter: Vec3,
134    /// Analytical seabed caustics projection intensity (`>= 0.0`).
135    #[get(type(copy))]
136    pub(crate) caustics_intensity: f32,
137    /// Physically combined linear RGB water surface radiance.
138    #[get(type(copy))]
139    pub(crate) water_color: Vec3,
140}
141
142/// Descriptor for building a projected-grid / LOD ocean `Mesh3D` patch.
143#[derive(Clone, Copy, Data, Debug, PartialEq)]
144pub struct OceanPatchDesc {
145    /// World-space horizontal center `(cx, cz)` of the patch (typically camera anchor).
146    #[get(type(copy))]
147    pub(crate) center_xz: Vec2,
148    /// Total patch width along world X in meters.
149    #[get(type(copy))]
150    pub(crate) extent_x: f32,
151    /// Total patch depth along world Z in meters.
152    #[get(type(copy))]
153    pub(crate) extent_z: f32,
154    /// Grid subdivision count along X (`2..=256`).
155    #[get(type(copy))]
156    pub(crate) subdivisions_x: usize,
157    /// Grid subdivision count along Z (`2..=256`).
158    #[get(type(copy))]
159    pub(crate) subdivisions_z: usize,
160    /// Radial LOD non-linearity (`0.0` = uniform grid, `> 0.0` = denser triangles near center).
161    #[get(type(copy))]
162    pub(crate) lod_radial_falloff: f32,
163    /// Reference seabed elevation along Y for Beer-Lambert depth tinting in vertex colors.
164    #[get(type(copy))]
165    pub(crate) seabed_y: f32,
166}
167
168/// Reusable multi-cascade directional Gerstner + JONSWAP ocean surface synthesis and optics engine.
169#[derive(Clone, Data, Debug, PartialEq)]
170pub struct OceanSurface3D {
171    /// Sanitized ocean sea-state and optical configuration.
172    #[get(type(copy))]
173    pub(crate) config: OceanConfig,
174    /// Synthesized directional Gerstner wave components across all cascades.
175    pub(crate) waves: Vec<GerstnerWaveComponent>,
176    /// Current elapsed simulation time in seconds.
177    #[get(type(copy))]
178    pub(crate) sim_time_secs: f32,
179}
180
181/// Distributed hull buoyancy probe point expressed in local vessel coordinates.
182#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
183pub struct PontoonSamplePoint {
184    /// Local-space offset from hull center of mass in meters.
185    #[get(type(copy))]
186    pub(crate) local_offset: Vec3,
187    /// Maximum displaced water volume in `m^3` represented by this probe when fully submerged.
188    #[get(type(copy))]
189    pub(crate) submerged_volume_m3: f32,
190    /// Vertical draft transition half-height in meters controlling smooth submersion.
191    #[get(type(copy))]
192    pub(crate) sample_radius_m: f32,
193}
194
195/// Rigid-body hydrodynamic parameters for `BuoyantHull3D`.
196#[derive(Clone, Copy, Data, Debug, PartialEq)]
197pub struct BuoyantHullConfig {
198    /// Total hull rigid-body mass in kilograms.
199    #[get(type(copy))]
200    pub(crate) mass_kg: f32,
201    /// Principal rotational inertia tensor diagonal `(Ix, Iy, Iz)` in `kg*m^2`.
202    #[get(type(copy))]
203    pub(crate) inertia_diag: Vec3,
204    /// Ambient fluid density in `kg/m^3` (`1025.0` seawater, `1000.0` fresh water).
205    #[get(type(copy))]
206    pub(crate) fluid_density_kgm3: f32,
207    /// Gravitational acceleration in `m/s^2`.
208    #[get(type(copy))]
209    pub(crate) gravity_mps2: f32,
210    /// Linear viscous hydrodynamic drag coefficient (`N / (m/s)`).
211    #[get(type(copy))]
212    pub(crate) linear_drag: f32,
213    /// Quadratic hydrodynamic pressure drag coefficient (`N / (m/s)^2`).
214    #[get(type(copy))]
215    pub(crate) quadratic_drag: f32,
216    /// Angular water damping coefficient (`N*m / (rad/s)`).
217    #[get(type(copy))]
218    pub(crate) angular_drag: f32,
219    /// Restoring torque aligning hull up-axis with local wave surface normal.
220    #[get(type(copy))]
221    pub(crate) slope_alignment_torque: f32,
222    /// Maximum linear speed clamp in `m/s` preventing runaway energy injection.
223    #[get(type(copy))]
224    pub(crate) max_linear_speed: f32,
225    /// Maximum angular speed clamp in `rad/s`.
226    #[get(type(copy))]
227    pub(crate) max_angular_speed: f32,
228}
229
230/// Rigid-body 3D floating vessel/object simulator coupled to `OceanSurface3D` or `WaveHeightfield2D`.
231#[derive(Clone, Data, Debug, PartialEq)]
232pub struct BuoyantHull3D {
233    /// Sanitized hydrodynamic rigid-body configuration.
234    #[get(type(copy))]
235    pub(crate) config: BuoyantHullConfig,
236    /// World-space center of mass position in meters.
237    #[get(type(copy))]
238    pub(crate) position: Vec3,
239    /// World-space linear velocity in meters per second.
240    #[get(type(copy))]
241    pub(crate) velocity: Vec3,
242    /// World-space unit orientation quaternion.
243    #[get(type(copy))]
244    pub(crate) orientation: Quat,
245    /// World-space angular velocity vector in radians per second.
246    #[get(type(copy))]
247    pub(crate) angular_velocity: Vec3,
248    /// Distributed hull/pontoon buoyancy sample points in local coordinates.
249    pub(crate) pontoons: Vec<PontoonSamplePoint>,
250    /// Aggregate fraction of total pontoon volume currently submerged (`0.0..=1.0`).
251    #[get(type(copy))]
252    pub(crate) submerged_fraction: f32,
253    /// Net Archimedes upward buoyancy force vector from the most recent physics step.
254    #[get(type(copy))]
255    pub(crate) buoyancy_force: Vec3,
256}