use super::*;
#[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)
}
#[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
}
#[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
}
#[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]
}
#[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]
}
#[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
}
#[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]
}
}
}
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;
}
}