The Ocean Seen from Below the Surface
snell's window
Snell's Window: The Ocean Seen from Below
Note: Most of the code in this article was written by AI.
A human contributed some experience and additions, but the mathematical expressions and most of the code were written by AI.
Look up from underwater and the whole 180° of sky folds into a cone with half-angle . Outside this cone relative to the local normal, no light from the air reaches the eye, and the interface turns into a mirror held up by total internal reflection. A rippling surface bends and blurs that boundary, but the law itself holds. The local critical angle simply applies again at every wave slope.
Physical Model: Measured Constants and Explicit Model Coefficients
Parameter | Value | Source |
|---|---|---|
(seawater, ‰, 20 °C, 589.3 nm) | 1.33938 | Quan & Fry 1995; critical angle 48.30°, window diameter 96.60° |
Absorption (680/550/440 nm) | (0.4524, 0.0654, 0.0083) m⁻¹ | Pope & Fry 1997 values tabulated in NASA Ocean Optics Protocols |
Total molecular scattering (680/550/440 nm) | (0.0006, 0.0015, 0.0038) m⁻¹ | Morel 1974 values tabulated in NASA Ocean Optics Protocols |
Sky | Nishita 1993 single scattering | Standard Rayleigh/Mie β |
- Parameter
(seawater, ‰, 20 °C, 589.3 nm)
- Value
1.33938
- Source
Quan & Fry 1995; critical angle 48.30°, window diameter 96.60°
- Parameter
Absorption (680/550/440 nm)
- Value
(0.4524, 0.0654, 0.0083) m⁻¹
- Source
Pope & Fry 1997 values tabulated in NASA Ocean Optics Protocols
- Parameter
Total molecular scattering (680/550/440 nm)
- Value
(0.0006, 0.0015, 0.0038) m⁻¹
- Source
Morel 1974 values tabulated in NASA Ocean Optics Protocols
- Parameter
Sky
- Value
Nishita 1993 single scattering
- Source
Standard Rayleigh/Mie β
The implementation is a clear-water hybrid. Interface index follows the seawater conditions specified for it, while spectral attenuation uses measured absorption and molecular scattering of pure water only. Dissolved matter, phytoplankton and suspended-particle scattering from any particular ocean stay outside the model.
The first AI implementation used for blue absorption and a scalar B_BACK=0.0025. The first value sits much closer to the cyan wavelengths beyond 440 nm, and the second, though named as a backscattering coefficient, was in practice used as a total extinction value. Both have been corrected. Beam extinction now follows the explicit relation .
Mathematical Notes: Exact Checks and Range-Limited Approximations
The entire hemisphere fits exactly inside the cone. Sample zenith angles from 0–90° and refract each one into the water: the largest reachable angle lands on the critical angle to within machine precision.
The bright rim is not drawn. The last 5° of sky before the horizon takes up 0.51% of the angular radius, so a full 360° horizon panorama is packed into a thin ring. In a perspective view the radius depends on the projection. So 0.51% is not a universal pixel-width ratio; it is a figure that holds for a given projection.
Fresnel. The numerically computed matches the closed form , and holds at the critical angle.
Interface radiance. The Fresnel power ratio satisfies . The radiance conversion adds one more step. Transmitted skylight also obeys the invariant, so air-side radiance picks up a factor of on its way into the water.
The caustic focal distance checks out. Surface ripples of amplitude a and wavenumber k focus refracted sunlight at f = 1/(0.25339·a·k²). The swell used here (λ = 0.8–1.8 m, a = 8–30 mm) focuses at 7.7–10.8 m, which matches the distance between the surface and the seafloor. Without those ripples, swell alone would focus beyond 100 m and no caustics would appear at all.
Caustic estimator. The gain comes from a finite-difference Jacobian of the map that lands refracted sunlight on the floor. On flat water the raw value is 1, and curvature concentrates light. The value that goes to the screen adds fold clamping and empirical normalization on top, so it can stray from 1 even over flat water. FLOOR_NORM and BEAM_NORM are empirical correction factors, not a proof of global energy conservation.
Shortcuts Tried and Discarded
In the first AI pass the caustic Jacobian arrived as one monolithic block. It is expensive, because every sample evaluates the surface normal three times. An analytic version built on the Hessian of the wave field and the normal-incidence factor cost less, but it drifted far from the finite-difference forward map: the incoming refracted sunlight is oblique, not normal.
The current estimator starts from a guess inverted off the mean plane, evaluates one local branch only, clamps fold singularities and applies an offline normalization. That is an approximation, not an exact inverse caustic solution. At a fold it takes a single preimage.
The ray estimator deliberately uses a wide stencil and two octaves of surface. Octaves smaller than that stencil were dropped as an explicit band-limiting approximation. With no reproducible sampling artifact to compare against, the choice is hard to call a verified physical equivalent.
From Equations to Production Code
The equations below are paired with the code that currently runs. The visual model is owned by shaders/snells-window.frag; browser-side auditing, loading, interaction and audio state are owned by main.js.
Each runtime file has exactly one owner.
shaders/vertex.vert— full-screen WebGL vertex stageshaders/snells-window.frag— all optics, surface, caustics, and water GLSLmain.js— explicit shader URLs, loading, compilation, controls, and auditingunderwatersound.mp3— optional original audio recording
There is no inline shader fallback. If the GLSL file is missing, an error surfaces visibly instead of an old JavaScript copy quietly becoming a second source of truth.
const SHADER_URLS = Object.freeze({
vertex: new URL('./shaders/vertex.vert', import.meta.url),
fragment: new URL('./shaders/snells-window.frag', import.meta.url)
});1. Snell's Cone and the Critical Angle
With the refractive index of air and that of water,
The air-side horizon sits at , so the image seen from underwater is
Implementation — main.js, runAudit()
const thetaC = Math.asin(1 / N_W);
let maxSeen = 0;
for (let d = 0; d <= 90; d += 0.5) {
maxSeen = Math.max(
maxSeen,
Math.asin(Math.sin(d / DEG) / N_W)
);
}The shader traces backward camera rays, from water toward air. That is why refract(I,N,eta) in GLSL takes .
vec3 rr0 = refract(rd, -n, N_W);refract() returns a zero vector when the value inside the square root goes negative. That is the total-internal-reflection branch outside the local cone.
2. Exact Unpolarized Dielectric Fresnel
With the cosine of the incidence angle on the water side,
At normal incidence,
Implementation — shaders/snells-window.frag, fresnelWA()
float fresnelWA(float cosW) {
float s = sqrt(max(0.0, 1.0 - cosW * cosW)) * N_W;
if (s >= 1.0) return 1.0;
float ca = sqrt(1.0 - s * s);
float rs = (N_W * cosW - ca) / (N_W * cosW + ca);
float rp = (N_W * ca - cosW) / (N_W * ca + cosW);
return clamp(0.5 * (rs * rs + rp * rp), 0.0, 1.0);
}The sign of the rp amplitude in the code is opposite to the convention above. It is squared in the power term, though, so itself comes out the same.
3. Fresnel Coverage and the Radiance Law
At a lossless refractive interface,
Applying Fresnel transmittance,
The shader approximates sub-pixel coverage with three surface-normal samples, averaging transmittance and refraction direction together and then evaluating the sky once along the weighted direction.
Implementation — shaders/snells-window.frag, main()
float T0 = 1.0 - R0, T1 = 1.0 - R1, T2 = 1.0 - R2;
vec3 rr0 = refract(rd, -n, N_W);
vec3 rr1 = refract(rd, -nA, N_W);
vec3 rr2 = refract(rd, -nB, N_W);
vec3 transmittedDirection = rr0 * T0 + rr1 * T1 + rr2 * T2;
float transmission = (T0 + T1 + T2) / 3.0;
float R = 1.0 - transmission;
if (dot(transmittedDirection, transmittedDirection) > 1e-6) {
above = skyColor(normalize(transmittedDirection), sd, 1.0)
* (N_W * N_W);
}
col = above * (1.0 - R) + mirrored * R;This is coverage anti-aliasing, not full supersampling. Three refracted sky rays collapse into one weighted direction, while the reflection branch still leans on a single coarse-scale normal.
4. Water Absorption, Scattering, and Beam Extinction
The abbreviated water-column model keeps absorption , total scattering and beam extinction apart.
The 8-step single-scattering approximation is as follows.
Implementation — shaders/snells-window.frag, constants and lookDown(), main()
const vec3 A_W = vec3(0.4524, 0.0654, 0.0083);
const vec3 B_W = vec3(0.0006, 0.0015, 0.0038);
const vec3 C_W = A_W + B_W;
vec3 sunHere = SUN_I * sunT
* exp(-C_W * (-p.y) / max(-dMean.y, 0.2));
inSc += B_W * (sunHere * 0.13 * gg + vec3(0.10))
* exp(-C_W * tt) * ds;
col = col * exp(-C_W * min(pathLen, 60.0)) + inSc;0.13 is an effective phase and source weighting, not a measured volume scattering function. What comes out is a self-contained abbreviated single-scattering closure; a spectral oceanic radiative transfer equation (RTE) solution sits outside its scope.
5. Mean-Centered Surface and the First-Intersection Equation
The procedural octave function returns positive values only. Without mean correction the surface averaged roughly , so the control labeled "depth below mean surface" was off by exactly that much. The corrected field is
Here is the measured spatial mean for each LOD. The first root of the equation below gives the point where the camera ray meets the surface.
Implementation — shaders/snells-window.frag, seaHeight3() and traceSurface()
const float SEA_MEAN_3 = 0.4107;
float seaHeight3(vec2 xz, float t) {
float freq = 0.22, amp = 0.32, choppy = 3.0;
vec2 uv = vec2(xz.x * 0.75, xz.y);
float h = 0.0;
for (int i = 0; i < 3; i++) {
h += (seaOct((uv + t) * freq, choppy)
+ seaOct((uv - t) * freq, choppy)) * amp;
uv = OCTM * uv;
freq *= 1.9;
amp *= 0.22;
choppy = mix(choppy, 1.0, 0.2);
}
return h - SEA_MEAN_3 + chop(xz, t) * uChop;
}
for (int i = 1; i <= 24; i++) {
float ti = mix(tLo, tHi, float(i) / 24.0);
vec3 p = ro + rd * ti;
float h = p.y - seaHeight3(p.xz, t);
if (h * hPrev <= 0.0) {
float a = tPrev, b = ti, ha = hPrev;
for (int j = 0; j < 8; j++) {
float m = 0.5 * (a + b);
vec3 pm = ro + rd * m;
float hm = pm.y - seaHeight3(pm.xz, t);
if (hm * ha <= 0.0) {
b = m;
} else {
a = m;
ha = hm;
}
}
return 0.5 * (a + b);
}
tPrev = ti;
hPrev = h;
}Unlike the old silent fallback, the function returns -1.0 when no sign change is found and no intersection can be located. Inventing an intersection on the top search plane is gone.
6. Caustic Landing Map and Finite-Difference Jacobian
With the source coordinate on the surface, the refracted sun direction, the surface height and the floor height, the landing map is
The geometric-optics raw local gain is approximated by
The value that actually reaches the screen is given by
Here , and are correction and normalization terms rather than optical constants. The raw map is what returns 1 on flat water; what reaches the screen has passed through these coefficients, so the value there may land somewhere other than 1.
Implementation — shaders/snells-window.frag, causticGainFloor()
float surfaceY = seaHeight4(sp, t);
vec3 d = refract(-sd, seaNormal4(sp, 0.05, t), 1.0 / N_W);
vec2 land = sp + d.xz * ((surfaceY - p.y) / (-d.y));
vec2 jx = (Lx - L0) / eps;
vec2 jz = (Lz - L0) / eps;
return min(
1.0 / max(abs(jx.x * jz.y - jz.x * jx.y), 0.02),
FLOOR_GMAX
) * FLOOR_NORM;The earlier code used -p.y as the travel height, which implicitly placed every source point at . It now includes the computed wave height. The estimator structure is the one described above, and FLOOR_GMAX and FLOOR_NORM are explicit normalization and correction constants.
7. Focal Length Under the Small-Slope Approximation
For near-normal incidence with , the deflection of small-slope refracted rays produces a focal length of
Implementation — shaders/snells-window.frag, chop()
return 0.030 * sin(3.5 * (xz.x * 0.92 + xz.y * 0.39) + t * 1.7)
+ 0.014 * sin(5.5 * (xz.x * 0.31 - xz.y * 0.95) - t * 2.1)
+ 0.008 * sin(8.0 * (xz.x * 0.71 + xz.y * 0.70) + t * 2.7);At the default uChop=1 these modes focus at roughly 10.7, 9.3 and 7.8 m. The relation is only an estimate, and it holds strictly for small slopes at near-normal incidence. On screen, the caustics come from the finite-difference map instead.
Problems That Arose While Working with AI
Near the critical angle the Fresnel reflectance climbs from 0.2 to 1.0 inside about 2°. One sample per pixel therefore undersamples this real glint and turns it into speckle.
The bug produced wrong output about three times before anyone measured it, and the first three fixes all produced pixel-identical output. Only rendering the intermediate values as debug images settled it: the surface normals were perfectly smooth, and was a clean binary 0/1 at the fractal boundary. The normals were not the problem. The Fresnel term at that point was behaving like a step function.
An earlier attempt that averaged alone failed too. The stencil came from the pixel footprint, ts * pixAng / max(rd.y, 0.12) ≈ 3.6 cm, while the normals vary over several meters. What finally worked was widening the stencil to the scale on which the normals live.
float nEps = clamp(0.35 + ts * 0.03, 0.35, 1.6); // metres, was clamp(ts*0.012, 0.05, 0.9)Seafloor caustics and rays hit the same class of error, separately. Applying pixel-footprint LOD (lodC, lodA) fixed it, so caustic gain fades toward 1 as the footprint grows.
Some speckle survives at the edge of the window. Fixing it properly needs supersampling, and that part is still open. The water column still runs on single scattering; multiple scattering stays outside the model.
The current 3-tap interface filter averages transmittance coverage and refraction direction together.
The AI-written code averaged alone. When the center tap is in total internal reflection and a neighbor tap transmits, the center refract() returns a zero vector, yet still came out above zero after averaging. Interface energy was silently thrown away. (This part is my own fix.)
The scene renders with white balance switched off. Red really does vanish within a few meters. With the corrected coefficients the absorption-only lengths are 2.21 / 15.29 / 120.48 m for R/G/B, and the direct beam extinction lengths are 2.21 / 14.95 / 82.64 m. That is why the result looks blue, and why divers carry red filters.
A personal note: working with AI, I find that if you extract and record the values from the reference papers underlying the implementation, it works much faster than I could.
Playing around with GLSL like this is something I always wanted to do since I was young, once I became a programmer. But because I genuinely lacked the talent, I was mostly limited to ray-marching techniques and tweaking the colors and shapes of things others had written. It feels like the world has changed radically.
It's enjoyable to be able to do things I couldn't do before.
References
E. O. Hulburt, Optics of distilled and natural water, JOSA 35 (1945) 698.
X. Quan, E. S. Fry, Empirical equation for the index of refraction of seawater, Applied Optics 34 (1995) 3477–3480. DOI: 10.1364/AO.34.003477
R. M. Pope, E. S. Fry, Absorption spectrum (380–700 nm) of pure water, Applied Optics 36 (1997) 8710–8723. DOI: 10.1364/AO.36.008710
A. Morel, Optical properties of pure water and pure sea water, in
Optical Aspects of Oceanography (1974), 1–24.
NASA, Ocean Optics Protocols for Satellite Ocean Color Sensor Validation, Rev. 4, Vol. IV, Table 1.1 (Pope–Fry absorption and Morel scattering values). NASA Ocean Optics Protocols PDF
R. W. Preisendorfer, Hydrologic Optics, Vol. II: Foundations, §2.6,
radiance law. NOAA Repository
T. Nishita et al., Display of the Earth taking into account atmospheric scattering, SIGGRAPH 1993. DOI: 10.1145/166117.166140
C. D. Mobley, Light and Water: Radiative Transfer in Natural Waters, Academic Press, 1994.