import*asTHREEfrom'three';import{ mergeVertices }from'three/addons/utils/BufferGeometryUtils.js';import{ buildTerrain }from'./terrainGeometry.js';import{ islandHeight }from'./islandHeight.js';import{ fbm }from'./noise.js';import{CAUSTICS_GLSL}from'../water/Caustics.js';import{FLOAT_SHADOW_GLSL}from'./floaterShadow.js';import{SHORE_GLSL,SWASH_GLSL
ground mode
mode material material
material
shader uniforms
shader shader
shader shader
ground
ground
ground
material mode
g
g
g
g g
p g v
i i p i
vp i
n v v v
facet v v v
v n facet
v v v
pi v v v
g
g
out
i i count out count i
a
r
r a r a r r
r
x a r z a r
h x z
h h
s
out x z h s s
k k out count k
ox x s oz z s
ss s
out ox oz ox oz ss ss
out
uniforms
floatShadow
k waterk
caustics
sky
absorption
sky
geometry
groundMat
groundMat uniforms
ground geometry groundMat
ground ground
seed
seed seed
spots rand
rockMat
rockMat uniforms
rocks rockMat spots
m qt e sc
spots
e
scs s s
m s s s qte sc
rocksi m
rocks
rocks rocks
group
groupground rocks
o ground rocks oreflectionLayer
group uniforms m uniforms
// sand ripples + grain bump in world space, faded out before they alias
const
RIPPLES
=
/* glsl */
`
#ifndef ROCK
{
vec2 fw = fwidth(vWPos.xz);
float px = max(fw.x, fw.y);
if (px < 0.5) { // both ripple bands are faded out beyond this
float sandMask = (1.0 - smoothstep(sandLine - 0.5, sandLine + 0.5, hgt)) * smoothstep(0.85, 0.95, flat_);
sandMask *= mix(1.0, 0.25, smoothstep(-0.3, 0.6, hgt)); // dry sand: faint wind ripples only
// long wave-formed ripples (~1.6 m) and short ones (~0.3 m), each fading in and out
// in patches
vec2 p = vWPos.xz;
vec2 d1 = normalize(vec2(0.8, 0.6) + (vec2(tn(p * 0.02), tn(p * 0.02 + 5.0)) - 0.5));
float a1 = 0.02 * (0.3 + 0.7 * smoothstep(0.25, 0.75, tn(p * 0.06 + 11.0)));
vec2 g = rippleSlope(p, d1, 3.9, 0.35, 0.8, a1) * (1.0 - smoothstep(0.15, 0.5, px));
if (px < 0.09) {
float a2 = 0.002 * (0.3 + 0.7 * smoothstep(0.25, 0.75, tn(p * 0.15 + 3.0)));
g += rippleSlope(p, normalize(d1 + vec2(-d1.y, d1.x) * 0.3), 21.0, 2.2, 0.1, a2) * (1.0 - smoothstep(0.03, 0.09, px));
}
vec3 nW = normalize(vWNrm - vec3(g.x, 0.0, g.y) * sandMask);
normal = normalize((viewMatrix * vec4(nW, 0.0)).xyz);
}
}
#endif
`
;
const
UNDERWATER
=
/* glsl */
`
float wdepth = max(-vWPos.y, 0.0);
// sun and sky light reaching the bed through the water column (the sun along its
// refracted path)
vec3 rsun = refract(-uSunDirT, vec3(0.0, 1.0, 0.0), 1.0 / 1.33);
float sunPath = wdepth / max(-rsun.y, 0.1);
diffuseColor.rgb *= exp(-uAbsorption * (sunPath * 0.6 + wdepth * 0.4));
`
;
// Direct sun (spliced into three's light loop, one directional light): floater shadows
// everywhere; below the surface the light arrives refracted (much steeper than the
// grazing sun outside), dimmed by the Fresnel transmission, and both shadow maps are
// read where it came through the surface, so the bed isn't shadowed along the