import*asTHREEfrom'three';import{N,CASCADE_LENGTHS}from'./OceanSim.js';import{ rawPass }from'../engine/fullscreen.js';// Wave-driven caustics by area-ratio splatting (Evan Wallace's WebGL water): a grid over// the 12 m cascade's period is refracted through its slopes onto planes at three depths,// and every landed triangle adds (undisturbed area / landed area) to that depth's channel.// Output, mipmapped + repeat, tiling every 12 m: intensity relative to flat water at the// slice depths, mean 1 by construction (the mesh is watertight, so the splatted energy is// the tile area), so it multiplies direct sun light. Two targets, sized by the sun-disc// blur at each depth (~0.7% of the depth): near R16F 512² (2.3 cm) with 1.5 m, deep// RG16F 256² with 4 m and 10 m.// The landing offset of flat water is subtracted, registering the pattern to the surface// entry point: look up at uv = entryXZ / 12 (see CAUSTICS_GLSL).constL=CASCADE_LENGTHS[2];constETA
x
s
_color
renderer
sim
res res
format
mat
target
depth cells cells
margin cells channel
span cells margin
mesh span span span span mat
mesh
targetmesh
up sunDir
up
up
r
prevRT r face r mip r
prevAlpha r
r_color
sunDir
rup
i up
rrt
r
ri
rprevRT face mip
r_color prevAlpha
// firefly clamp (calibration knob; energy above it is dropped)
// Fold slivers are far thinner than a texel: point-sampled they hit texel centres only
// now and then and the lines break into dots, so both targets are MSAA (each sliver adds
// by coverage).
const
TARGETS
=
[
{
res
:
512
,
format
:
THREE
.
RedFormat
}
,
{
res
:
256
,
format
:
THREE
.
RGFormat
}
,
]
;
// Per slice: target + channel, grid cells per tile, and a margin of cells beyond each
// tile edge that must cover the largest landing offset (wrap-around, instead of drawing
// 3×3 tiles). The cost goes with cells × target res (sliver quad overshading), and the
// B-spline slopes keep coarse grids smooth. Deeper slices are focused by longer waves,
// so they take coarser slope mips.
const
SLICES
=
[
{
depth
:
1.5
,
target
:
0
,
channel
:
0
,
cells
:
128
,
margin
:
6
}
,
// 0.38 m
{
depth
:
4
,
target
:
1
,
channel
:
0
,
cells
:
64
,
margin
:
7
}
,
// 0.94 m
{
depth
:
10
,
target
:
1
,
channel
:
1
,
cells
:
32
,
margin
:
8
}
,
// 2.25 m
]
;
const
f
=
(
x
)
=>
.
toPrecision
(
9
)
;
// GLSL float literal
const
[
D0
,
D1
,
D2
]
=
SLICES
.
map
(
(
s
)
=>
f
(
.
depth
)
)
;
const
VERT
=
/* glsl */
`
precision highp float;
in vec3 position; // PlaneGeometry vertex: cell coords - uCells / 2
uniform highp sampler2D uSlope; // cascade 2 (sx, sz, ...)
uniform vec3 uSun; // towards the sun
uniform float uDepth, uCells, uLod, uMargin, uRes;
out vec2 vOld;
// cubic B-spline from 4 bilinear taps (GPU Gems 2, ch. 20). The landed intensity goes
// with the slope's derivative, which bilinear makes piecewise constant: every slope texel
// would land as its own bead along the caustic lines.
vec2 slopeBSpline(vec2 uv, float lod) {
vec2 size = vec2(textureSize(uSlope, int(lod)));
vec2 st = uv * size - 0.5, i = floor(st), f = st - i;
vec2 f2 = f * f, f3 = f2 * f;
vec2 w0 = (1.0 - 3.0 * f + 3.0 * f2 - f3) / 6.0, w1 = (4.0 - 6.0 * f2 + 3.0 * f3) / 6.0;
vec2 w2 = (1.0 + 3.0 * f + 3.0 * f2 - 3.0 * f3) / 6.0, w3 = f3 / 6.0;
vec2 g0 = w0 + w1, g1 = w2 + w3;
vec2 h0 = (i - 0.5 + w1 / g0) / size, h1 = (i + 1.5 + w3 / g1) / size;
return g0.y * (g0.x * textureLod(uSlope, h0, lod).xy + g1.x * textureLod(uSlope, vec2(h1.x, h0.y), lod).xy)
+ g1.y * (g0.x * textureLod(uSlope, vec2(h0.x, h1.y), lod).xy + g1.x * textureLod(uSlope, h1, lod).xy);
}
void main() {
vec2 t = (position.xy + 0.5 * uCells + 0.5) / uCells; // tile uv, at a texel centre of mip uLod
vec2 s = slopeBSpline(t, uLod);
vec3 r = refract(-uSun, normalize(vec3(-s.x, 1.0, -s.y)), ${f(ETA)});
vec3 rf = refract(-uSun, vec3(0.0, 1.0, 0.0), ${f(ETA)});
vec2 d = (r.xz / max(-r.y, 0.05) - rf.xz / -rf.y) * (uDepth / ${f(L)});
// ponytail: offsets past the margin (steep facets at low sun) are clamped, widen the margin if it shows
d = clamp(d, -uMargin, uMargin);
vOld = t * uRes;
gl_Position = vec4((t + d) * 2.0 - 1.0, 0.0, 1.0);
}`
;
const
FRAG
=
/* glsl */
`
precision highp float;
uniform vec3 uMask;
in vec2 vOld;
out vec4 outColor;
void main() {
// raster space is the landed space at one texel per pixel, so the landed area is 1 and
// the ratio is the undisturbed area (in texels) per pixel
vec2 a = dFdx(vOld), b = dFdy(vOld);
outColor = vec4(uMask * min(abs(a.x * b.y - a.y * b.x), ${f(MAX_RATIO)}), 0.0);
}`
;
// waveCaustic(entryXZ, depth, lod) -> direct-sun multiplier, mean ~1 (uniforms from
// WaveCaustics.uniforms).
// entryXZ: world xz (m) where the flat-water refracted sun ray through the shaded point