euv-core 0.47.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use super::*;

/// Evaluates a smooth cubic Hermite ramp (`smoothstep`) in `[0.0, 1.0]`.
///
/// # Arguments
///
/// - `f32` - Lower edge threshold.
/// - `f32` - Upper edge threshold.
/// - `f32` - Input scalar value.
///
/// # Returns
///
/// - `f32` - Smoothly interpolated value in `[0.0, 1.0]`.
#[inline(always)]
#[must_use]
pub(crate) fn fluid_smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
    let denom: f32 = edge1 - edge0;
    if denom.abs() < 1.0e-7 {
        return if x >= edge1 { 1.0 } else { 0.0 };
    }
    let t: f32 = ((x - edge0) / denom).clamp(0.0, 1.0);
    t * t * (3.0 - 2.0 * t)
}

/// Fast rational approximation of `exp(-x)` for `x >= 0.0` accurate to `< 0.5%` for Beer-Lambert absorption.
///
/// # Arguments
///
/// - `f32` - Non-negative optical depth `σ * d`.
///
/// # Returns
///
/// - `f32` - Transmittance in `(0.0, 1.0]`.
#[inline(always)]
#[must_use]
pub(crate) fn fast_exp_neg(x: f32) -> f32 {
    if x <= 0.0 {
        return 1.0;
    }
    let d: f32 = 1.0 + x + 0.48 * x * x + 0.235 * x * x * x;
    1.0 / d
}

/// Converts a linear `[0.0, 1.0]` color channel to an 8-bit `u8` pixel channel with soft filmic rolloff.
///
/// # Arguments
///
/// - `f32` - Linear HDR intensity value.
///
/// # Returns
///
/// - `u8` - Clamped sRGB-like 8-bit byte.
#[inline(always)]
#[must_use]
pub(crate) fn tone_to_u8(v: f32) -> u8 {
    let clamped: f32 = v.max(0.0);
    let mapped: f32 = clamped / (1.0 + clamped * 0.22);
    (mapped.clamp(0.0, 1.0) * 255.0 + 0.5) as u8
}

/// Evaluates the crisp architectural tiled laboratory-pool underwater/tank studio backdrop at normalized coordinates `(u, v)`.
///
/// Straight tile grout lines and subtle lighting gradients make Snell's Law refraction and optical caustics immediately visible.
///
/// # Arguments
///
/// - `f32` - Horizontal UV coordinate in `[0.0, 1.0]`.
/// - `f32` - Vertical UV coordinate in `[0.0, 1.0]`.
/// - `f32` - Aspect ratio `width / height` for square tiles.
///
/// # Returns
///
/// - `[f32; 3]` - Linear RGB backdrop color.
#[inline(always)]
#[must_use]
pub(crate) fn eval_vessel_backdrop(u: f32, v: f32, aspect: f32) -> [f32; 3] {
    let tiles_y: f32 = 8.0;
    let tiles_x: f32 = (tiles_y * aspect).max(4.0);
    let gx: f32 = u * tiles_x;
    let gy: f32 = v * tiles_y;
    let ix: i32 = gx.floor() as i32;
    let iy: i32 = gy.floor() as i32;
    let fx: f32 = gx - (ix as f32);
    let fy: f32 = gy - (iy as f32);
    let dist_edge_x: f32 = fx.min(1.0 - fx);
    let dist_edge_y: f32 = fy.min(1.0 - fy);
    let edge_d: f32 = dist_edge_x.min(dist_edge_y);
    let grout: f32 = fluid_smoothstep(0.025, 0.068, edge_d);
    let checker: f32 = if ((ix + iy) & 1) == 0 { 1.0 } else { 0.935 };
    let dx_light: f32 = u - 0.36;
    let dy_light: f32 = v - 0.24;
    let radial: f32 = (1.0 - (dx_light * dx_light + dy_light * dy_light) * 0.62).clamp(0.45, 1.08);
    let vert_grad: f32 = 0.88 + 0.16 * (1.0 - v);
    let tile_lum: f32 = checker * radial * vert_grad;
    let tile_r: f32 = 0.145 * tile_lum + 0.025;
    let tile_g: f32 = 0.195 * tile_lum + 0.038;
    let tile_b: f32 = 0.265 * tile_lum + 0.055;
    let grout_r: f32 = 0.068 * radial;
    let grout_g: f32 = 0.105 * radial;
    let grout_b: f32 = 0.155 * radial;
    let mut r: f32 = grout_r + (tile_r - grout_r) * grout;
    let mut g: f32 = grout_g + (tile_g - grout_g) * grout;
    let mut b: f32 = grout_b + (tile_b - grout_b) * grout;
    let side_tick: bool = (u < 0.045 || u > 0.955) && ((v * 20.0).fract() < 0.11);
    if side_tick {
        r += 0.10;
        g += 0.16;
        b += 0.22;
    }
    [r, g, b]
}

/// Evaluates the overhead studio & sky reflection color reflected off the liquid free surface via Schlick Fresnel.
///
/// # Arguments
///
/// - `f32` - Surface normal x-component.
/// - `f32` - Surface normal y-component.
/// - `f32` - Surface normal z-component.
/// - `f32` - Gravity-aligned up-vector x-component.
/// - `f32` - Gravity-aligned up-vector y-component.
///
/// # Returns
///
/// - `[f32; 3]` - Linear RGB sky/studio reflection color.
#[inline(always)]
#[must_use]
pub(crate) fn eval_studio_sky_reflection(
    nx: f32,
    ny: f32,
    nz: f32,
    up_x: f32,
    up_y: f32,
) -> [f32; 3] {
    let rx: f32 = 2.0 * nz * nx;
    let ry: f32 = 2.0 * nz * ny;
    let up_align: f32 = (rx * up_x + ry * up_y * 0.85 + 0.45).clamp(0.0, 1.0);
    let softbox: f32 = fluid_smoothstep(0.68, 0.96, up_align);
    let horizon: f32 = (1.0 - up_align).clamp(0.0, 1.0);
    let r: f32 = 0.20 * horizon + 0.56 * up_align + 0.65 * softbox;
    let g: f32 = 0.34 * horizon + 0.78 * up_align + 0.72 * softbox;
    let b: f32 = 0.50 * horizon + 0.95 * up_align + 0.78 * softbox;
    [r, g, b]
}

/// Evaluates signed distance to a rounded rectangle centered at `(0, 0)` with half-extents `(hx, hy)` and corner radius `r`.
///
/// # Arguments
///
/// - `f32` - Local x-offset from center.
/// - `f32` - Local y-offset from center.
/// - `f32` - Box half-width.
/// - `f32` - Box half-height.
/// - `f32` - Corner bevel radius.
///
/// # Returns
///
/// - `f32` - Signed distance (`< 0` inside, `> 0` outside).
#[inline(always)]
#[must_use]
pub(crate) fn fluid_rounded_box_sdf(px: f32, py: f32, hx: f32, hy: f32, r: f32) -> f32 {
    let qx: f32 = px.abs() - (hx - r);
    let qy: f32 = py.abs() - (hy - r);
    let out_x: f32 = qx.max(0.0);
    let out_y: f32 = qy.max(0.0);
    (out_x * out_x + out_y * out_y).sqrt() + qx.max(qy).min(0.0) - r
}

/// Evaluates optical shading and coverage `(r, g, b, alpha)` of a buoyant floating orb or translucent ice cube at vessel meter coordinates `(wx, wy)`.
///
/// # Arguments
///
/// - `&BuoyantBody` - Buoyant rigid body state.
/// - `f32` - Sample x-coordinate in vessel meters.
/// - `f32` - Sample y-coordinate in vessel meters.
/// - `f32` - Local pixel scale in meters per pixel for crisp anti-aliasing.
///
/// # Returns
///
/// - `[f32; 4]` - Linear `(r, g, b, coverage)` color tuple (`coverage == 0.0` outside body).
#[inline(always)]
#[must_use]
pub(crate) fn shade_buoyant_body(
    body: &BuoyantBody,
    wx: f32,
    wy: f32,
    meter_per_px: f32,
) -> [f32; 4] {
    let dx: f32 = wx - body.get_x();
    let dy: f32 = wy - body.get_y();
    let rad: f32 = body.get_radius();
    let bound: f32 = rad + meter_per_px * 2.5;
    if dx.abs() > bound || dy.abs() > bound {
        return [0.0, 0.0, 0.0, 0.0];
    }
    match body.get_kind() {
        BuoyantBodyKind::FloatingOrb => {
            let d2: f32 = dx * dx + dy * dy;
            let r2: f32 = rad * rad;
            if d2 > bound * bound {
                return [0.0, 0.0, 0.0, 0.0];
            }
            let dist: f32 = d2.sqrt();
            let cov: f32 = 1.0 - fluid_smoothstep(rad - meter_per_px, rad + meter_per_px, dist);
            if cov <= 0.001 {
                return [0.0, 0.0, 0.0, 0.0];
            }
            let nx: f32 = (dx / rad).clamp(-1.0, 1.0);
            let ny: f32 = (dy / rad).clamp(-1.0, 1.0);
            let nz: f32 = (1.0 - (d2 / r2).min(1.0)).sqrt();
            let ndl: f32 = (nx * (-0.42) + ny * (-0.64) + nz * 0.64).max(0.0);
            let ndh: f32 = (nx * (-0.26) + ny * (-0.40) + nz * 0.88).max(0.0);
            let ndh2: f32 = ndh * ndh;
            let ndh4: f32 = ndh2 * ndh2;
            let spec: f32 = ndh4 * ndh4 * ndh4 * 1.15;
            let rim: f32 = (1.0 - nz) * (1.0 - nz) * 0.42;
            let base_r: f32 = 0.96;
            let base_g: f32 = 0.44;
            let base_b: f32 = 0.24;
            let diff: f32 = 0.30 + 0.72 * ndl;
            let r: f32 = base_r * diff + spec + rim * 0.75;
            let g: f32 = base_g * diff + spec + rim * 0.85;
            let b: f32 = base_b * diff + spec + rim * 0.95;
            [r, g, b, cov]
        }
        BuoyantBodyKind::IceCube => {
            let ca: f32 = body.get_angle().cos();
            let sa: f32 = body.get_angle().sin();
            let lx: f32 = dx * ca + dy * sa;
            let ly: f32 = -dx * sa + dy * ca;
            let half: f32 = rad * 0.84;
            let bevel: f32 = rad * 0.22;
            let sdf: f32 = fluid_rounded_box_sdf(lx, ly, half, half, bevel);
            let cov: f32 = 1.0 - fluid_smoothstep(-meter_per_px, meter_per_px, sdf);
            if cov <= 0.001 {
                return [0.0, 0.0, 0.0, 0.0];
            }
            let edge_frost: f32 = fluid_smoothstep(-half * 0.45, -meter_per_px * 0.3, sdf);
            let inner_facet: f32 = 0.5 + 0.5 * ((lx * 0.65 - ly * 0.76) / half).clamp(-1.0, 1.0);
            let glint: f32 = fluid_smoothstep(0.72, 0.98, inner_facet) * 0.48;
            let r: f32 = 0.68 + 0.26 * edge_frost + glint;
            let g: f32 = 0.88 + 0.14 * edge_frost + glint;
            let b: f32 = 0.98 + 0.08 * edge_frost + glint;
            [r, g, b, cov * 0.84]
        }
    }
}

/// Shades a single output scanline `[y]` of the photorealistic liquid optical renderer into `row_rgba`.
///
/// # Arguments
///
/// - `&FluidShadeParams` - Immutable frame parameters and buoyant body snapshot.
/// - `&FluidFieldViews<'_>` - Read-only precomputed optical scalar field views.
/// - `usize` - Scanline index `y` in `[0, out_h)`.
/// - `&mut [u8]` - Destination RGBA8 byte slice for scanline `y` (`len == out_w * 4`).
pub(crate) fn shade_fluid_scanline(
    params: &FluidShadeParams,
    fields: &FluidFieldViews<'_>,
    y: usize,
    row_rgba: &mut [u8],
) {
    let phi: &[f32] = fields.phi;
    let thickness: &[f32] = fields.thickness;
    let foam: &[f32] = fields.foam;
    let grad_x: &[f32] = fields.grad_x;
    let grad_y: &[f32] = fields.grad_y;
    let caustic: &[f32] = fields.caustic;
    let out_w: usize = params.out_w;
    let out_h: usize = params.out_h;
    let fw: usize = params.field_w;
    let fh: usize = params.field_h;
    let inv_w: f32 = 1.0 / (out_w.max(1) as f32);
    let inv_h: f32 = 1.0 / (out_h.max(1) as f32);
    let aspect: f32 = (out_w as f32) * inv_h;
    let f_scale_x: f32 = (fw.saturating_sub(1)) as f32 * inv_w;
    let f_scale_y: f32 = (fh.saturating_sub(1)) as f32 * inv_h;
    let py_f: f32 = y as f32 + 0.5;
    let v: f32 = py_f * inv_h;
    let fy: f32 = (py_f * f_scale_y).clamp(0.0, (fh.saturating_sub(1)) as f32);
    let fy0: usize = fy as usize;
    let fy1: usize = (fy0 + 1).min(fh.saturating_sub(1));
    let ty: f32 = fy - (fy0 as f32);
    let omty: f32 = 1.0 - ty;
    let row0: usize = fy0 * fw;
    let row1: usize = fy1 * fw;
    let max_fx: f32 = (fw.saturating_sub(1)) as f32;
    let max_x_idx: usize = fw.saturating_sub(1);
    let wy: f32 = v * params.vessel_h;
    let meter_per_px: f32 = (params.vessel_w * inv_w).max(params.vessel_h * inv_h);
    let half_w: f32 = (out_w as f32) * 0.5;
    let half_h: f32 = (out_h as f32) * 0.5;
    let cy_px: f32 = py_f - half_h;
    let inner_hx: f32 = (half_w - params.bezel_px).max(4.0);
    let inner_hy: f32 = (half_h - params.bezel_px).max(4.0);
    let inner_cr: f32 = (params.corner_px - params.bezel_px * 0.6).max(2.0);
    let mut row_has_body: bool = false;
    let body_margin: f32 = meter_per_px * 3.0 + 0.005;
    for body in params.bodies.iter().take(params.body_count) {
        if (wy - body.get_y()).abs() <= body.get_radius() * 1.25 + body_margin {
            row_has_body = true;
            break;
        }
    }
    let sh_y: usize = fy0.saturating_sub(1) * fw;
    for x in 0..out_w {
        let px_f: f32 = x as f32 + 0.5;
        let u: f32 = px_f * inv_w;
        let fx: f32 = (px_f * f_scale_x).clamp(0.0, max_fx);
        let fx0: usize = fx as usize;
        let fx1: usize = (fx0 + 1).min(max_x_idx);
        let tx: f32 = fx - (fx0 as f32);
        let omtx: f32 = 1.0 - tx;
        let w00: f32 = omtx * omty;
        let w10: f32 = tx * omty;
        let w01: f32 = omtx * ty;
        let w11: f32 = tx * ty;
        let i00: usize = row0 + fx0;
        let i10: usize = row0 + fx1;
        let i01: usize = row1 + fx0;
        let i11: usize = row1 + fx1;
        let wx: f32 = u * params.vessel_w;
        let cx_px: f32 = px_f - half_w;
        let outer_sdf: f32 =
            fluid_rounded_box_sdf(cx_px, cy_px, half_w - 1.0, half_h - 1.0, params.corner_px);
        let dst: usize = x * 4;
        if outer_sdf > 1.2 {
            let bg_v: f32 = v * 0.65;
            row_rgba[dst] = tone_to_u8(0.032 + 0.015 * bg_v);
            row_rgba[dst + 1] = tone_to_u8(0.048 + 0.022 * bg_v);
            row_rgba[dst + 2] = tone_to_u8(0.082 + 0.038 * bg_v);
            row_rgba[dst + 3] = 255;
            continue;
        }
        let phi_val: f32 = phi[i00] * w00 + phi[i10] * w10 + phi[i01] * w01 + phi[i11] * w11;
        let water_cov: f32 = fluid_smoothstep(FLUID_ISO_LOW, FLUID_ISO_HIGH, phi_val);
        let mut air_r: f32 = 0.0;
        let mut air_g: f32 = 0.0;
        let mut air_b: f32 = 0.0;
        if water_cov < 0.999 {
            let bg_air: [f32; 3] = eval_vessel_backdrop(u, v, aspect);
            let sh_x: usize = fx0.saturating_sub(1);
            let shadow_phi: f32 = phi[sh_y + sh_x];
            let shadow_cov: f32 =
                fluid_smoothstep(FLUID_ISO_LOW * 0.85, FLUID_ISO_HIGH, shadow_phi);
            let air_shadow: f32 = 1.0 - shadow_cov * 0.22;
            air_r = bg_air[0] * air_shadow;
            air_g = bg_air[1] * air_shadow;
            air_b = bg_air[2] * air_shadow;
        }
        let mut r_out: f32 = air_r;
        let mut g_out: f32 = air_g;
        let mut b_out: f32 = air_b;
        if water_cov > 0.001 {
            let thick_val: f32 = thickness[i00] * w00
                + thickness[i10] * w10
                + thickness[i01] * w01
                + thickness[i11] * w11;
            let foam_val: f32 =
                foam[i00] * w00 + foam[i10] * w10 + foam[i01] * w01 + foam[i11] * w11;
            let gx_val: f32 =
                grad_x[i00] * w00 + grad_x[i10] * w10 + grad_x[i01] * w01 + grad_x[i11] * w11;
            let gy_val: f32 =
                grad_y[i00] * w00 + grad_y[i10] * w10 + grad_y[i01] * w01 + grad_y[i11] * w11;
            let caust_val: f32 =
                caustic[i00] * w00 + caustic[i10] * w10 + caustic[i01] * w01 + caustic[i11] * w11;
            let slope_boost: f32 = 1.35
                + 1.55 * (1.0 - fluid_smoothstep(FLUID_ISO_HIGH, FLUID_ISO_HIGH + 0.70, phi_val));
            let nx_raw: f32 = -gx_val * slope_boost;
            let ny_raw: f32 = -gy_val * slope_boost;
            let n2_xy: f32 = nx_raw * nx_raw + ny_raw * ny_raw;
            let nz_raw: f32 = 0.72;
            let inv_n: f32 = 1.0 / (n2_xy + nz_raw * nz_raw).sqrt();
            let nx: f32 = nx_raw * inv_n;
            let ny: f32 = ny_raw * inv_n;
            let nz: f32 = nz_raw * inv_n;
            let cos_i: f32 = nz;
            let sin2_t: f32 = WATER_INV_IOR * WATER_INV_IOR * (1.0 - cos_i * cos_i).max(0.0);
            let cos_t: f32 = (1.0 - sin2_t).max(0.04).sqrt();
            let refr_k: f32 =
                (WATER_INV_IOR * cos_i - cos_t) * (0.048 + 0.052 * thick_val.min(1.8));
            let du: f32 = nx * refr_k;
            let dv: f32 = ny * refr_k;
            let bg_refr: [f32; 3] =
                eval_vessel_backdrop((u + du).clamp(0.0, 1.0), (v + dv).clamp(0.0, 1.0), aspect);
            let disp: f32 = (du.abs() + dv.abs()) * 0.45;
            let mut sub_r: f32 = (bg_refr[0] - disp * 0.05).max(0.0);
            let mut sub_g: f32 = bg_refr[1];
            let mut sub_b: f32 = bg_refr[2] + disp * 0.08;
            let caustic_energy: f32 = caust_val.min(1.15);
            sub_r += caustic_energy * 0.40;
            sub_g += caustic_energy * 0.66;
            sub_b += caustic_energy * 0.76;
            if row_has_body {
                for body in params.bodies.iter().take(params.body_count) {
                    let sh_dx: f32 = (wx - body.get_x()) - 0.0025;
                    let sh_dy: f32 = (wy - body.get_y()) - 0.0032;
                    let sh_r: f32 = body.get_radius() * 1.15;
                    let sh_d2: f32 = sh_dx * sh_dx + sh_dy * sh_dy;
                    if sh_d2 < sh_r * sh_r {
                        let s_fac: f32 = 1.0 - (1.0 - sh_d2 / (sh_r * sh_r)) * 0.32;
                        sub_r *= s_fac;
                        sub_g *= s_fac;
                        sub_b *= s_fac;
                    }
                    let refr_wx: f32 = wx + du * params.vessel_w * 0.55;
                    let refr_wy: f32 = wy + dv * params.vessel_h * 0.55;
                    let b_col: [f32; 4] = shade_buoyant_body(body, refr_wx, refr_wy, meter_per_px);
                    if b_col[3] > 0.001 {
                        let a_b: f32 = b_col[3];
                        sub_r = sub_r * (1.0 - a_b) + b_col[0] * a_b;
                        sub_g = sub_g * (1.0 - a_b) + b_col[1] * a_b;
                        sub_b = sub_b * (1.0 - a_b) + b_col[2] * a_b;
                    }
                }
            }
            let opt_depth: f32 = (0.16 + thick_val * 0.78).clamp(0.10, 2.4);
            let tr_r: f32 = fast_exp_neg(BEER_LAMBERT_SIGMA_R * opt_depth);
            let tr_g: f32 = fast_exp_neg(BEER_LAMBERT_SIGMA_G * opt_depth);
            let tr_b: f32 = fast_exp_neg(BEER_LAMBERT_SIGMA_B * opt_depth);
            let scatter_r: f32 = 0.025;
            let scatter_g: f32 = 0.34;
            let scatter_b: f32 = 0.58;
            let abs_r: f32 = sub_r * tr_r + scatter_r * (1.0 - tr_r);
            let abs_g: f32 = sub_g * tr_g + scatter_g * (1.0 - tr_g);
            let abs_b: f32 = sub_b * tr_b + scatter_b * (1.0 - tr_b);
            let omnv: f32 = (1.0 - nz).clamp(0.0, 1.0);
            let omnv2: f32 = omnv * omnv;
            let fresnel: f32 = (WATER_FRESNEL_F0 + (1.0 - WATER_FRESNEL_F0) * omnv2 * omnv2 * omnv)
                .clamp(WATER_FRESNEL_F0, 0.85);
            let sky: [f32; 3] = eval_studio_sky_reflection(nx, ny, nz, params.up_x, params.up_y);
            let mut wat_r: f32 = abs_r * (1.0 - fresnel) + sky[0] * fresnel;
            let mut wat_g: f32 = abs_g * (1.0 - fresnel) + sky[1] * fresnel;
            let mut wat_b: f32 = abs_b * (1.0 - fresnel) + sky[2] * fresnel;
            let ndh1: f32 = (nx * (-0.28) + ny * (-0.56) + nz * 0.78).max(0.0);
            let ndh1_2: f32 = ndh1 * ndh1;
            let ndh1_4: f32 = ndh1_2 * ndh1_2;
            let ndh1_8: f32 = ndh1_4 * ndh1_4;
            let ndh1_16: f32 = ndh1_8 * ndh1_8;
            let spec_sharp: f32 = ndh1_16 * ndh1_16 * ndh1_16 * 1.65;
            let spec_broad: f32 = ndh1_8 * 0.22;
            let ndh2: f32 = (nx * 0.42 + ny * (-0.34) + nz * 0.84).max(0.0);
            let ndh2_4: f32 = ndh2 * ndh2 * ndh2 * ndh2;
            let spec_rim: f32 = ndh2_4 * ndh2_4 * 0.24;
            wat_r += spec_sharp * 0.96 + spec_broad * 0.45 + spec_rim * 0.35;
            wat_g += spec_sharp * 1.00 + spec_broad * 0.72 + spec_rim * 0.62;
            wat_b += spec_sharp * 1.05 + spec_broad * 0.90 + spec_rim * 0.82;
            let surface_rim: f32 = fluid_smoothstep(FLUID_ISO_LOW, FLUID_ISO_HIGH + 0.08, phi_val)
                * (1.0 - fluid_smoothstep(FLUID_ISO_HIGH + 0.04, FLUID_ISO_HIGH + 0.34, phi_val));
            let meniscus_glow: f32 = surface_rim * (0.36 + 0.28 * (1.0 - nz));
            wat_r += meniscus_glow * 0.65;
            wat_g += meniscus_glow * 0.94;
            wat_b += meniscus_glow * 1.04;
            let foam_cov: f32 = (foam_val * (0.45 + 0.55 * surface_rim)).clamp(0.0, 0.85);
            if foam_cov > 0.005 {
                wat_r = wat_r * (1.0 - foam_cov) + 0.92 * foam_cov;
                wat_g = wat_g * (1.0 - foam_cov) + 0.97 * foam_cov;
                wat_b = wat_b * (1.0 - foam_cov) + 1.00 * foam_cov;
            }
            r_out = air_r * (1.0 - water_cov) + wat_r * water_cov;
            g_out = air_g * (1.0 - water_cov) + wat_g * water_cov;
            b_out = air_b * (1.0 - water_cov) + wat_b * water_cov;
        }
        if row_has_body && water_cov < 0.98 {
            let dry_weight: f32 = 1.0 - water_cov;
            for body in params.bodies.iter().take(params.body_count) {
                let b_col: [f32; 4] = shade_buoyant_body(body, wx, wy, meter_per_px);
                if b_col[3] > 0.001 {
                    let a_dry: f32 = b_col[3] * dry_weight;
                    r_out = r_out * (1.0 - a_dry) + b_col[0] * a_dry;
                    g_out = g_out * (1.0 - a_dry) + b_col[1] * a_dry;
                    b_out = b_out * (1.0 - a_dry) + b_col[2] * a_dry;
                }
            }
        }
        let inner_sdf: f32 = fluid_rounded_box_sdf(cx_px, cy_px, inner_hx, inner_hy, inner_cr);
        if inner_sdf > -1.5 {
            let glass_wall: f32 = fluid_smoothstep(-1.5, 1.0, inner_sdf);
            let outer_edge: f32 = 1.0 - fluid_smoothstep(-0.8, 1.2, outer_sdf);
            let rim_band: f32 = glass_wall * outer_edge;
            let bevel_lit: f32 =
                (0.5 - 0.35 * (cx_px / half_w) - 0.45 * (cy_px / half_h)).clamp(0.15, 1.0);
            let water_tint: f32 = 0.25 + 0.75 * water_cov;
            let wall_r: f32 = 0.16 * bevel_lit + 0.06 * water_tint;
            let wall_g: f32 = 0.26 * bevel_lit + 0.20 * water_tint;
            let wall_b: f32 = 0.36 * bevel_lit + 0.32 * water_tint;
            let spec_edge: f32 = (1.0
                - (inner_sdf - params.bezel_px * 0.35).abs() / (params.bezel_px * 0.45).max(1.0))
            .max(0.0);
            let rim_highlight: f32 = spec_edge * spec_edge * bevel_lit * 0.48;
            let blend: f32 = (rim_band * 0.76).clamp(0.0, 0.92);
            r_out = r_out * (1.0 - blend) + (wall_r + rim_highlight) * blend;
            g_out = g_out * (1.0 - blend) + (wall_g + rim_highlight * 1.05) * blend;
            b_out = b_out * (1.0 - blend) + (wall_b + rim_highlight * 1.12) * blend;
            if outer_edge < 0.99 {
                let bg_r: f32 = 0.032 + 0.015 * v * 0.65;
                let bg_g: f32 = 0.048 + 0.022 * v * 0.65;
                let bg_b: f32 = 0.082 + 0.038 * v * 0.65;
                r_out = bg_r * (1.0 - outer_edge) + r_out * outer_edge;
                g_out = bg_g * (1.0 - outer_edge) + g_out * outer_edge;
                b_out = bg_b * (1.0 - outer_edge) + b_out * outer_edge;
            }
        }
        row_rgba[dst] = tone_to_u8(r_out);
        row_rgba[dst + 1] = tone_to_u8(g_out);
        row_rgba[dst + 2] = tone_to_u8(b_out);
        row_rgba[dst + 3] = 255;
    }
}