Skip to main content

euv_core/game/fluid/renderer/
struct.rs

1use super::*;
2
3/// Persistent zero-allocation photorealistic ocean & origami paper boat renderer producing RGBA8 frames with perspective stormy-sunset sky, multi-scale Gerstner ocean waves, dual-lobe GGX sun glitter, emerald subsurface scattering, 3D floating origami boat with wave-warped reflection, and quarter-res HDR bloom.
4#[derive(Clone, Data, Debug)]
5pub struct LiquidOpticalRenderer {
6    /// Active scalar field grid width in cells for PBF interactive wake transfer.
7    #[get(type(copy))]
8    pub(crate) field_w: usize,
9    /// Active scalar field grid height in cells for PBF interactive wake transfer.
10    #[get(type(copy))]
11    pub(crate) field_h: usize,
12    /// Smoothed PBF horizontal flow velocity wake field.
13    pub(crate) flow_u: Vec<f32>,
14    /// Smoothed PBF vertical flow velocity wake field.
15    pub(crate) flow_v: Vec<f32>,
16    /// Smoothed PBF wave-crest foam intensity wake field.
17    pub(crate) foam: Vec<f32>,
18    /// Separable horizontal smoothing scratch buffer.
19    pub(crate) wake_tmp: Vec<f32>,
20    /// Persistent packed RGBA8 PBF wake field (`field_w * field_h * 4`) eliminating per-frame heap allocation in `build_gpu_ocean_pass`.
21    #[get(skip)]
22    #[get_mut(skip)]
23    #[set(skip)]
24    pub(crate) wake_rgba8: Vec<u8>,
25    /// Persistent linear HDR RGB frame scratch buffer (`len == out_w * out_h * 3`) feeding filmic tone-mapping and HDR sun/glitter bloom.
26    pub(crate) hdr_rgb: Vec<f32>,
27    /// Quarter-resolution HDR bloom extraction and vertical blur buffer.
28    pub(crate) bloom_a: Vec<[f32; 3]>,
29    /// Quarter-resolution HDR bloom horizontal blur scratch buffer.
30    pub(crate) bloom_b: Vec<[f32; 3]>,
31    /// Pre-baked `SKY_LUT_W x SKY_LUT_H` spherical stormy-sunset sky dome radiance LUT (excluding full-res sun disk).
32    pub(crate) sky_lut: Vec<[f32; 3]>,
33    /// Pre-baked `REFL_LUT_W x REFL_LUT_H` spherical ocean-reflection sky radiance LUT.
34    pub(crate) refl_lut: Vec<[f32; 3]>,
35    /// Precomputed 1025-entry L1-resident ACES filmic tone-mapping + sRGB gamma LUT eliminating per-pixel `powf`.
36    pub(crate) aces_lut: Vec<u8>,
37    /// Simulation timestamp at which `sky_lut` and `refl_lut` were last baked.
38    #[get(type(copy))]
39    pub(crate) last_sky_lut_time: f32,
40    /// Last rendered output width in pixels.
41    #[get(type(copy))]
42    pub(crate) last_width: u32,
43    /// Last rendered output height in pixels.
44    #[get(type(copy))]
45    pub(crate) last_height: u32,
46    /// Whether persistent dt-invariant camera and boat damping state has been initialized from a live snapshot.
47    #[get(type(copy))]
48    pub(crate) cam_initialized: bool,
49    /// Previous snapshot simulation timestamp in seconds driving dt-invariant camera damping across 60/120/180Hz+ renders.
50    #[get(type(copy))]
51    pub(crate) cam_last_sim_time: f32,
52    /// Exponentially damped camera horizon roll angle in radians.
53    #[get(type(copy))]
54    pub(crate) cam_roll_rad: f32,
55    /// Exponentially damped normalized screen horizon Y coordinate decoupled from transient PBF slosh.
56    #[get(type(copy))]
57    pub(crate) cam_horizon_v: f32,
58    /// Exponentially damped ocean wave agitation multiplier preventing single-frame wave-height twitching.
59    #[get(type(copy))]
60    pub(crate) cam_agitation: f32,
61    /// Exponentially damped horizontal sailboat sway offset derived from buoyant physics.
62    #[get(type(copy))]
63    pub(crate) cam_sway_x: f32,
64    /// Exponentially damped sailboat angular sway offset derived from buoyant physics.
65    #[get(type(copy))]
66    pub(crate) cam_sway_angle: f32,
67}
68
69/// Pre-projected screen-space 3D origami paper boat triangle ready for per-pixel barycentric depth shading and wave-warped water reflection.
70#[derive(Clone, Copy, Data, Debug, Default)]
71pub(crate) struct ProjectedBoatTri {
72    /// Projected screen X coordinates `(x0, x1, x2)` in viewport pixels.
73    #[get(pub(crate), type(copy))]
74    #[set(pub(crate))]
75    pub(crate) sx: [f32; 3],
76    /// Projected screen Y coordinates `(y0, y1, y2)` in viewport pixels.
77    #[get(pub(crate), type(copy))]
78    #[set(pub(crate))]
79    pub(crate) sy: [f32; 3],
80    /// View-space depth Z at each vertex `(z0, z1, z2)` in meters.
81    #[get(pub(crate), type(copy))]
82    #[set(pub(crate))]
83    pub(crate) depth: [f32; 3],
84    /// Local height above boat waterline `(h0, h1, h2)` in meters for wet-paper capillary absorption.
85    #[get(pub(crate), type(copy))]
86    #[set(pub(crate))]
87    pub(crate) local_y: [f32; 3],
88    /// Reciprocal signed 2D screen area for fast barycentric interpolation.
89    #[get(pub(crate), type(copy))]
90    #[set(pub(crate))]
91    pub(crate) inv_area: f32,
92    /// Reciprocal screen pixel lengths `(1/|v1v2|, 1/|v2v0|, 1/|v0v1|)` for analytical 1.35px edge anti-aliasing.
93    #[get(pub(crate), type(copy))]
94    #[set(pub(crate))]
95    pub(crate) edge_inv_len: [f32; 3],
96    /// Sign of the signed 2D screen area (`+1.0` or `-1.0`) orienting signed pixel edge distances inward.
97    #[get(pub(crate), type(copy))]
98    #[set(pub(crate))]
99    pub(crate) area_sign: f32,
100    /// Precomputed linear HDR diffuse + translucent paper color `[r, g, b]` for this facet.
101    #[get(pub(crate), type(copy))]
102    #[set(pub(crate))]
103    pub(crate) base_rgb: [f32; 3],
104    /// Screen-space bounding box `(min_x, max_x, min_y, max_y)` in pixels.
105    #[get(pub(crate), type(copy))]
106    #[set(pub(crate))]
107    pub(crate) bbox: [f32; 4],
108    /// Origami facet classification (`0=outer_wing`, `1=center_flap`, `2=sail_peak`, `3=inner_cavity`).
109    #[get(pub(crate), type(copy))]
110    #[set(pub(crate))]
111    pub(crate) facet_kind: u8,
112    /// Whether this triangle is valid and visible in front of the camera.
113    #[get(pub(crate), type(copy))]
114    #[set(pub(crate))]
115    pub(crate) valid: bool,
116}
117
118/// Immutable per-frame ocean, sky, sun, gyro-tilt, and projected 3D origami boat parameters shared across parallel scanline workers.
119#[derive(Clone, Copy, Data, Debug)]
120pub(crate) struct OceanShadeParams {
121    /// Output framebuffer width in pixels.
122    #[get(pub(crate), type(copy))]
123    #[set(pub(crate))]
124    pub(crate) out_w: usize,
125    /// Output framebuffer height in pixels.
126    #[get(pub(crate), type(copy))]
127    #[set(pub(crate))]
128    pub(crate) out_h: usize,
129    /// PBF wake field width in cells.
130    #[get(pub(crate), type(copy))]
131    #[set(pub(crate))]
132    pub(crate) field_w: usize,
133    /// PBF wake field height in cells.
134    #[get(pub(crate), type(copy))]
135    #[set(pub(crate))]
136    pub(crate) field_h: usize,
137    /// Monotonic simulation time in seconds driving continuous ocean swells and drifting storm clouds.
138    #[get(pub(crate), type(copy))]
139    #[set(pub(crate))]
140    pub(crate) sim_time: f32,
141    /// Dynamic wave choppiness multiplier amplified by PBF kinetic energy, shaking, and stirring (`1.0..=2.0`).
142    #[get(pub(crate), type(copy))]
143    #[set(pub(crate))]
144    pub(crate) wave_agitation: f32,
145    /// Effective normalized screen horizon Y coordinate (`~0.545`, subtly shifted by fill level).
146    #[get(pub(crate), type(copy))]
147    #[set(pub(crate))]
148    pub(crate) horizon_v: f32,
149    /// Cosine of active camera horizon roll angle derived from gravity/gyroscope.
150    #[get(pub(crate), type(copy))]
151    #[set(pub(crate))]
152    pub(crate) roll_cos: f32,
153    /// Sine of active camera horizon roll angle derived from gravity/gyroscope.
154    #[get(pub(crate), type(copy))]
155    #[set(pub(crate))]
156    pub(crate) roll_sin: f32,
157    /// Normalized 3D sun direction vector `[lx, ly, lz]` in ocean camera coordinates.
158    #[get(pub(crate), type(copy))]
159    #[set(pub(crate))]
160    pub(crate) sun_dir: [f32; 3],
161    /// Projected screen X coordinate of the origami paper boat center in pixels.
162    #[get(pub(crate), type(copy))]
163    #[set(pub(crate))]
164    pub(crate) boat_screen_x: f32,
165    /// Projected screen Y coordinate of the origami paper boat waterline in pixels.
166    #[get(pub(crate), type(copy))]
167    #[set(pub(crate))]
168    pub(crate) boat_waterline_y: f32,
169    /// Overall projected screen bounding box `[min_x, max_x, min_y, max_y]` of the origami paper boat in pixels.
170    #[get(pub(crate), type(copy))]
171    #[set(pub(crate))]
172    pub(crate) boat_bbox: [f32; 4],
173    /// Pre-projected origami paper boat triangles ready for per-pixel shading and water reflection.
174    #[get(pub(crate), type(copy))]
175    #[set(pub(crate))]
176    pub(crate) boat_tris: [ProjectedBoatTri; BOAT_TRI_COUNT],
177    /// Precomputed per-frame wave temporal phase offsets `sim_time * speed[i]` for all 8 wave trains.
178    #[get(pub(crate), type(copy))]
179    #[set(pub(crate))]
180    pub(crate) wave_time_phases: [f32; 8],
181    /// Whether the PBF wake field contains non-negligible interactive stirring or foam requiring bilinear sampling.
182    #[get(pub(crate), type(copy))]
183    #[set(pub(crate))]
184    pub(crate) has_active_wake: bool,
185}
186
187/// Read-only borrowed slice bundle of interactive PBF wake fields and pre-baked spherical sky LUTs for parallel ocean shading.
188#[derive(Clone, Copy, Data, Debug)]
189pub(crate) struct FluidWakeViews<'a> {
190    /// Smoothed PBF horizontal flow velocity wake field.
191    #[get(pub(crate), type(copy))]
192    #[set(pub(crate))]
193    pub(crate) flow_u: &'a [f32],
194    /// Smoothed PBF vertical flow velocity wake field.
195    #[get(pub(crate), type(copy))]
196    #[set(pub(crate))]
197    pub(crate) flow_v: &'a [f32],
198    /// Smoothed PBF foam intensity wake field.
199    #[get(pub(crate), type(copy))]
200    #[set(pub(crate))]
201    pub(crate) foam: &'a [f32],
202    /// Pre-baked `SKY_LUT_W x SKY_LUT_H` primary storm-cloud dome radiance LUT.
203    #[get(pub(crate), type(copy))]
204    #[set(pub(crate))]
205    pub(crate) sky_lut: &'a [[f32; 3]],
206    /// Pre-baked `REFL_LUT_W x REFL_LUT_H` ocean-reflection sky radiance LUT.
207    #[get(pub(crate), type(copy))]
208    #[set(pub(crate))]
209    pub(crate) refl_lut: &'a [[f32; 3]],
210}