roamwright 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CONTRIBUTING.md +39 -8
  2. package/README.md +53 -24
  3. package/examples/viaduct-camera.js +40 -0
  4. package/examples/viaduct-landscape.js +81 -0
  5. package/examples/viaduct.css +75 -0
  6. package/examples/viaduct.html +47 -0
  7. package/examples/viaduct.js +332 -0
  8. package/geometry/builder.js +11 -14
  9. package/geometry/creased-sheet.js +127 -0
  10. package/geometry/ear.js +97 -0
  11. package/geometry/erosion.js +106 -0
  12. package/geometry/face-grid.js +143 -0
  13. package/geometry/garment-tags.js +11 -0
  14. package/geometry/hair-gathers.js +174 -0
  15. package/geometry/hair-locks.js +156 -0
  16. package/geometry/hair-patch.js +42 -0
  17. package/geometry/hair-shell.js +186 -0
  18. package/geometry/hair-tags.js +6 -0
  19. package/geometry/head-profile.js +138 -0
  20. package/geometry/head-shape.js +499 -0
  21. package/geometry/heightfield.js +68 -0
  22. package/geometry/outline-grid.js +46 -0
  23. package/geometry/path-field.js +65 -0
  24. package/geometry/sagging-tube.js +58 -0
  25. package/geometry/sampled-surface.js +101 -0
  26. package/geometry/scatter-grid.js +32 -0
  27. package/geometry/sculpt.js +263 -0
  28. package/geometry/shore-grid.js +67 -0
  29. package/geometry/strata-field.js +339 -0
  30. package/geometry/strata-tiles.js +215 -0
  31. package/geometry/stream.js +94 -0
  32. package/geometry/terrain-chunks.js +300 -0
  33. package/geometry/terrain-extension.js +44 -0
  34. package/math/cell-edge.js +44 -0
  35. package/math/cloth-textures.js +83 -0
  36. package/math/cloud-textures.js +112 -0
  37. package/math/color.js +29 -0
  38. package/math/curve.js +3 -4
  39. package/math/mountain-ranges.js +463 -0
  40. package/math/particle-noise.js +48 -0
  41. package/math/periodic.js +20 -0
  42. package/math/rock-texture.js +56 -0
  43. package/math/surface-textures.js +241 -0
  44. package/motion/arm-reach.js +74 -0
  45. package/motion/biped-stride.js +276 -0
  46. package/motion/crowd-alarms.js +127 -0
  47. package/motion/crowd-routine.js +183 -0
  48. package/motion/daily-clock.js +59 -0
  49. package/motion/instance-sight.js +84 -0
  50. package/motion/quadruped.js +332 -0
  51. package/motion/weather.js +59 -0
  52. package/motion/wind.js +27 -0
  53. package/package.json +6 -4
  54. package/rendering/caster-motion.js +79 -0
  55. package/rendering/cell-lod.js +175 -0
  56. package/rendering/cloud-dome.js +371 -0
  57. package/rendering/cloud-field.js +83 -0
  58. package/rendering/compilation-schedule.js +34 -0
  59. package/rendering/contact-shadows.js +79 -0
  60. package/rendering/crowd-skinning.js +133 -0
  61. package/rendering/crust.js +28 -0
  62. package/rendering/daylight.js +50 -0
  63. package/rendering/far-shadow.js +186 -0
  64. package/rendering/flocks.js +323 -0
  65. package/rendering/garment-material.js +110 -0
  66. package/rendering/glow-lines.js +74 -0
  67. package/rendering/glow-shell.js +90 -0
  68. package/rendering/hair-strands.js +67 -0
  69. package/rendering/height-haze.js +129 -0
  70. package/rendering/height-probe.js +91 -0
  71. package/rendering/interior-mapping.js +19 -0
  72. package/rendering/light-budget.js +222 -0
  73. package/rendering/light-points.js +51 -0
  74. package/rendering/light-shafts.js +131 -0
  75. package/rendering/local-lights.js +28 -0
  76. package/rendering/low-light.js +13 -0
  77. package/rendering/material-lod.js +110 -0
  78. package/rendering/particle-emitters.js +223 -0
  79. package/rendering/particles.js +297 -0
  80. package/rendering/planar-reflection.js +175 -0
  81. package/rendering/post-shaders.js +21 -1
  82. package/rendering/post.js +8 -5
  83. package/rendering/rain-rings.js +22 -0
  84. package/rendering/road-surface.js +63 -0
  85. package/rendering/sea-shaders.js +230 -0
  86. package/rendering/shader-warmup.js +197 -0
  87. package/rendering/shadow-alignment.js +28 -0
  88. package/rendering/shadow-depth.js +129 -0
  89. package/rendering/sky-occlusion.js +187 -0
  90. package/rendering/sky.js +169 -0
  91. package/rendering/stage-lights.js +209 -0
  92. package/rendering/static-point-shadow.js +119 -0
  93. package/rendering/still-casters.js +6 -11
  94. package/rendering/strata-material.js +93 -0
  95. package/rendering/stream-flow.js +36 -0
  96. package/rendering/surface-light.js +22 -0
  97. package/rendering/surface-projection.js +69 -0
  98. package/rendering/talus.js +26 -0
  99. package/rendering/terrain-rock.js +48 -0
  100. package/rendering/texture-fills.js +68 -0
  101. package/rendering/viewport.js +9 -0
  102. package/rendering/wakes.js +99 -0
  103. package/rendering/water-material.js +288 -0
  104. package/rendering/world-matrices.js +4 -6
  105. package/runtime/device-quality.js +27 -0
  106. package/runtime/diagnostic-log.js +80 -0
  107. package/runtime/events.js +14 -0
  108. package/runtime/first-use.js +3 -1
  109. package/runtime/frame-yield.js +14 -0
  110. package/runtime/grid-cache.js +88 -0
  111. package/runtime/grid-rows.js +9 -0
  112. package/runtime/module-preload.js +15 -0
  113. package/runtime/quality-fit.js +5 -9
  114. package/runtime/search.js +69 -0
  115. package/runtime/spatial-audio.js +57 -0
  116. package/scripts/serve.py +26 -0
@@ -0,0 +1,175 @@
1
+ import * as THREE from 'three';
2
+
3
+ export function createPlanarReflection({ camera, renderer, scene, drawnSize, waterCells, cellSize, scale = 0.5, minSize = 256, hiddenInterval = 4 }) {
4
+ const mirror = {
5
+ rt: null,
6
+ cam: new THREE.PerspectiveCamera(),
7
+ on: false,
8
+ plane: new THREE.Plane(),
9
+ clip: new THREE.Vector4(),
10
+ q: new THREE.Vector4(),
11
+ frustum: new THREE.Frustum(),
12
+ n: new THREE.Vector3(0, 1, 0),
13
+ o: new THREE.Vector3(),
14
+ a: new THREE.Vector3(),
15
+ b: new THREE.Vector3(),
16
+ rot: new THREE.Matrix4(),
17
+ m: new THREE.Matrix4(),
18
+ };
19
+ const WAVE_X = 0.1, // the waves bend the reflection sideways and up or down by up to this much of the picture's half-size
20
+ WAVE_Y = 0.16,
21
+ IN_FRONT = 0.05, // a corner nearer the mirror camera's plane than this is cut off
22
+ NOTHING = new THREE.Color(-1, -1, -1), // what the mirror holds outside the part drawn: the water takes the plain sky there
23
+ _colour = new THREE.Color(),
24
+ _corner = [new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4()],
25
+ _cut = new THREE.Vector4(),
26
+ part = { x0: 0, x1: 0, y0: 0, y1: 0 }; // the part of the picture where water shows, from -1 to 1 each way
27
+
28
+ function findWaterPart(view, level) {
29
+ const M = mirror,
30
+ cells = (M.cells ??= waterCells());
31
+ let x0 = 1,
32
+ x1 = -1,
33
+ y0 = 1,
34
+ y1 = -1;
35
+ for (let c = 0; c < cells.length; c += 2) {
36
+ let left = true, // is the whole cell off one side of the picture?
37
+ right = true,
38
+ below = true,
39
+ above = true,
40
+ ax = 1, // the cell's own box
41
+ bx = -1,
42
+ ay = 1,
43
+ by = -1;
44
+ const take = p => {
45
+ const x = p.x / p.w,
46
+ y = p.y / p.w;
47
+ left &&= x < -1;
48
+ right &&= x > 1;
49
+ below &&= y < -1;
50
+ above &&= y > 1;
51
+ ax = Math.min(ax, x);
52
+ bx = Math.max(bx, x);
53
+ ay = Math.min(ay, y);
54
+ by = Math.max(by, y);
55
+ };
56
+ for (let k = 0; k < 4; k++)
57
+ _corner[k].set(cells[c] + (k === 1 || k === 2 ? cellSize : 0), level, cells[c + 1] + (k > 1 ? cellSize : 0), 1).applyMatrix4(view);
58
+ for (let k = 0; k < 4; k++) {
59
+ const a = _corner[k],
60
+ b = _corner[(k + 1) % 4];
61
+ if (a.w > IN_FRONT) take(a);
62
+ if (a.w > IN_FRONT !== b.w > IN_FRONT) take(_cut.lerpVectors(a, b, (IN_FRONT - a.w) / (b.w - a.w))); // where the edge passes the camera's plane
63
+ }
64
+ if (bx < ax || left || right || below || above) continue;
65
+ x0 = Math.min(x0, ax);
66
+ x1 = Math.max(x1, bx);
67
+ y0 = Math.min(y0, ay);
68
+ y1 = Math.max(y1, by);
69
+ }
70
+ part.x0 = Math.max(-1, x0 - WAVE_X);
71
+ part.x1 = Math.min(1, x1 + WAVE_X);
72
+ part.y0 = Math.max(-1, y0 - WAVE_Y);
73
+ part.y1 = Math.min(1, y1 + WAVE_Y);
74
+ return x1 > x0 && y1 > y0;
75
+ }
76
+ // Fits the mirror's camera and target to `part`: whole pixels of the target, and a projection that fills just those, so each
77
+ // reflected thing lands on the pixel it would have in the whole picture.
78
+ function fitToPart(projection, target) {
79
+ const w = target.width,
80
+ h = target.height,
81
+ px0 = Math.floor(((part.x0 + 1) / 2) * w),
82
+ px1 = Math.ceil(((part.x1 + 1) / 2) * w),
83
+ py0 = Math.floor(((part.y0 + 1) / 2) * h),
84
+ py1 = Math.ceil(((part.y1 + 1) / 2) * h),
85
+ x0 = (2 * px0) / w - 1,
86
+ x1 = (2 * px1) / w - 1,
87
+ y0 = (2 * py0) / h - 1,
88
+ y1 = (2 * py1) / h - 1,
89
+ e = projection.elements;
90
+ for (let col = 0; col < 16; col += 4) {
91
+ e[col] = (2 * e[col] - (x0 + x1) * e[col + 3]) / (x1 - x0);
92
+ e[col + 1] = (2 * e[col + 1] - (y0 + y1) * e[col + 3]) / (y1 - y0);
93
+ }
94
+ target.viewport.set(px0, py0, px1 - px0, py1 - py0);
95
+ target.scissor.set(px0, py0, px1 - px0, py1 - py0);
96
+ }
97
+ function sizeMirror() {
98
+ // half the size the world is drawn at each way: where the quality fit draws it smaller and upscales it, the mirror shrinks with it
99
+ const [w, h] = drawnSize();
100
+ if (mirror.rt) mirror.rt.setSize(Math.max(minSize, Math.round(w * scale)), Math.max(minSize, Math.round(h * scale)));
101
+ }
102
+ function renderMirror(u, wl, k, unseen, frame) {
103
+ const M = mirror, cp = camera.position;
104
+ if (M.off) k = 0; // debug switch, for measuring what the mirror costs
105
+ u.uReflOn.value = k;
106
+ M.on = k > 0.01;
107
+ if (!M.on || (unseen && M.rt && frame % hiddenInterval)) return;
108
+ if (!M.rt) {
109
+ M.rt = new THREE.WebGLRenderTarget(4, 4, { type: THREE.HalfFloatType, depthBuffer: true });
110
+ u.uRefl.value = M.rt.texture;
111
+ sizeMirror();
112
+ }
113
+ const c = M.cam,
114
+ o = M.o.set(0, wl, 0);
115
+ M.a.subVectors(o, cp).reflect(M.n).negate().add(o);
116
+ c.position.copy(M.a);
117
+ M.rot.extractRotation(camera.matrixWorld);
118
+ M.b.set(0, 0, -1).applyMatrix4(M.rot).add(cp);
119
+ M.a.subVectors(o, M.b).reflect(M.n).negate().add(o);
120
+ c.up.set(0, 1, 0).applyMatrix4(M.rot).reflect(M.n);
121
+ c.lookAt(M.a);
122
+ c.near = camera.near;
123
+ c.far = camera.far;
124
+ c.updateMatrixWorld();
125
+ c.projectionMatrix.copy(camera.projectionMatrix);
126
+ u.uReflM.value
127
+ .set(0.5, 0, 0, 0.5, 0, 0.5, 0, 0.5, 0, 0, 0.5, 0.5, 0, 0, 0, 1)
128
+ .multiply(c.projectionMatrix)
129
+ .multiply(c.matrixWorldInverse);
130
+ const whole = !findWaterPart(M.m.multiplyMatrices(c.projectionMatrix, c.matrixWorldInverse), wl); // no cell found where water was seen: draw it all
131
+ // tilt the near plane onto the water, so nothing under the surface gets into the mirror
132
+ M.plane.setFromNormalAndCoplanarPoint(M.n, M.o.set(0, wl - 0.05, 0)).applyMatrix4(c.matrixWorldInverse);
133
+ const cl = M.clip.set(M.plane.normal.x, M.plane.normal.y, M.plane.normal.z, M.plane.constant),
134
+ pe = c.projectionMatrix.elements,
135
+ q = M.q;
136
+ q.x = (Math.sign(cl.x) + pe[8]) / pe[0];
137
+ q.y = (Math.sign(cl.y) + pe[9]) / pe[5];
138
+ q.z = -1;
139
+ q.w = (1 + pe[10]) / pe[14];
140
+ cl.multiplyScalar(2 / cl.dot(q));
141
+ pe[2] = cl.x;
142
+ pe[6] = cl.y;
143
+ pe[10] = cl.z + 1;
144
+ pe[14] = cl.w;
145
+ const su = renderer.shadowMap.autoUpdate,
146
+ alpha = renderer.getClearAlpha();
147
+ const previous = renderer.getRenderTarget(), face = renderer.getActiveCubeFace(), mip = renderer.getActiveMipmapLevel();
148
+ renderer.getClearColor(_colour);
149
+ renderer.shadowMap.autoUpdate = false;
150
+ try {
151
+ M.rt.scissorTest = false;
152
+ M.rt.viewport.set(0, 0, M.rt.width, M.rt.height);
153
+ renderer.setRenderTarget(M.rt);
154
+ renderer.setClearColor(NOTHING, 1);
155
+ renderer.clear();
156
+ renderer.setClearColor(_colour, alpha);
157
+ if (!whole) {
158
+ fitToPart(c.projectionMatrix, M.rt);
159
+ M.rt.scissorTest = true;
160
+ renderer.setRenderTarget(M.rt);
161
+ }
162
+ renderer.render(scene, c);
163
+ } finally {
164
+ renderer.setClearColor(_colour, alpha);
165
+ renderer.setRenderTarget(previous, face, mip);
166
+ renderer.shadowMap.autoUpdate = su;
167
+ }
168
+ }
169
+ function dispose() {
170
+ mirror.rt?.dispose();
171
+ mirror.rt = mirror.cells = null;
172
+ mirror.on = false;
173
+ }
174
+ return { mirror, sizeMirror, renderMirror, dispose };
175
+ }
@@ -14,7 +14,7 @@ const RAY_FS = `/* glsl */ uniform sampler2D tMask; uniform vec2 uSun; uniform f
14
14
  gl_FragColor = vec4(acc / 30.0, 0.0, 0.0, 1.0); }`;
15
15
 
16
16
  // Ambient occlusion from the depth buffer alone: corners, eaves, doorways and the ground under things darken. Stored with the depth it was taken at.
17
- const AO_FS = `/* glsl */ uniform sampler2D tDepth; uniform vec2 uNF, uTan, uPx; uniform float uAspect, uAO; varying vec2 vUv;
17
+ const AO_FS = `/* glsl */ uniform sampler2D tDepth; uniform vec2 uNF, uTan, uPx; uniform float uAspect, uAO; uniform vec2 uContact; varying vec2 vUv;
18
18
  vec3 vp(vec2 uv){ uv = (floor(uv / uPx) + 0.5) * uPx; // every lookup is moved onto the centre of a depth texel, so the position and the depth it is built from always belong together (otherwise thin dark lines cross flat ground, worst on phones)
19
19
  float z = uNF.x * uNF.y / ((uNF.y - uNF.x) * texture2D(tDepth, uv).x - uNF.y); return vec3((uv * 2.0 - 1.0) * uTan * -z, z); }
20
20
  void main(){ vec2 uv = (floor(vUv / uPx) + 0.5) * uPx; // a half-size pixel's centre is the corner of four depth texels: step onto one, or rounding picks a different one from row to row and draws lines
@@ -31,6 +31,26 @@ const AO_FS = `/* glsl */ uniform sampler2D tDepth; uniform vec2 uNF, uTan, uPx;
31
31
  }
32
32
  float reach = sr * dist * 2.0 * uTan.y; // how far the samples really reach in the world once the screen's cap has bitten (metres)
33
33
  ao = mix(clamp(1.0 - uAO * occ * rad / 14.0 * 2.1, 0.0, 1.0), 1.0, max(smoothstep(120.0, 240.0, dist), 1.0 - smoothstep(0.04, 0.12, reach))); // under a hand's breadth it would shade a nose's own curve: a face seen close was smeared dark beside the nose
34
+ // Four nearer samples resolve local contacts that can fall inside the first spiral tap.
35
+ // Skip subpixel contacts, and vary spacing by depth so the radius remains in world units.
36
+ if (uContact.y > 0.0 && dist > 1.5 && dist < 45.0) {
37
+ float r = uContact.x, screenRadius = min(r / (dist * 2.0 * uTan.y), 0.06);
38
+ float pixels = screenRadius / uPx.y;
39
+ if (pixels > 1.5) {
40
+ float contact = 0.0;
41
+ vec2 direction = vec2(0.923880, 0.382683);
42
+ for (int j = 0; j < 4; j++) {
43
+ vec2 dUV = direction * vec2(1.0 / uAspect, 1.0) * screenRadius;
44
+ direction = vec2(-direction.y, direction.x);
45
+ vec3 delta = vp(uv + dUV) - P;
46
+ float separation = length(delta);
47
+ float horizon = clamp((dot(N, delta) - r * 0.045) / max(separation, r * 0.12), 0.0, 1.0);
48
+ contact += horizon * (1.0 - smoothstep(r * 0.4, r * 2.0, separation));
49
+ }
50
+ float scale = smoothstep(1.5, 3.0, pixels) * smoothstep(1.5, 3.0, dist) * (1.0 - smoothstep(32.0, 45.0, dist));
51
+ ao *= 1.0 - clamp(uContact.y * 0.25 * contact * scale, 0.0, 0.55);
52
+ }
53
+ }
34
54
  }
35
55
  gl_FragColor = vec4(ao, dist, 0.0, 1.0); }`;
36
56
 
package/rendering/post.js CHANGED
@@ -11,7 +11,7 @@ import { AO_FS, BLOOM, BLUR_FS, EASU_FS, FINAL_FS, GRADE, PRESENT_FS, QUAD_VS, R
11
11
  // shafts are soft, so they are worked out at half size; bloom works at half size and below. None of them writes a full-size copy of the frame.
12
12
  // Where a device cannot hold its preset at the picture's size, the world is drawn smaller (post.scale of each side, set by the quality
13
13
  // fit) and brought up to the picture's size after the final pass, by an edge-adaptive upscale and a sharpening pass (the method of
14
- // AMD's FSR 1). Names over people and places are drawn after all of it, at the picture's own size (drawnAtFullSize).
14
+ // AMD's FSR 1). Overlay sprites render at the output resolution (drawnAtFullSize).
15
15
  // With temporal anti-aliasing (taa.js) the scene is drawn with the lens moved a little each frame, the final pass keeps a running
16
16
  // picture of it, tone-mapped, and a third full-size pass grades that onto the screen.
17
17
  const DOF_EASE = 0.08; // how much of depth of field comes in or goes out a frame
@@ -44,6 +44,8 @@ export function createPostProcessing({
44
44
  referenceLogLuminance = 0,
45
45
  expectedLogLuminance = () => referenceLogLuminance,
46
46
  adaptationStrength = 0.35,
47
+ contactRadius = 0.25,
48
+ contactStrength = () => 0,
47
49
  maxExposureStops = 0.8,
48
50
  eyeAdaptation = createEyeAdaptation(),
49
51
  depthOfField = createDepthOfField(),
@@ -107,7 +109,7 @@ export function createPostProcessing({
107
109
  uTime: grade.uTime,
108
110
  },
109
111
  });
110
- // What is drawn after the picture, at the screen's own size and onto its depth: the names over people and places.
112
+ // Render overlays at output resolution with depth testing against the main scene.
111
113
  const overlay = new THREE.Scene();
112
114
  // A scene of one quad that covers the screen, for a post-processing pass.
113
115
  function fullScreen(material) {
@@ -218,6 +220,7 @@ export function createPostProcessing({
218
220
  uPx: { value: new THREE.Vector2(0.001, 0.001) },
219
221
  uAspect: { value: 1 },
220
222
  uAO: { value: 1 },
223
+ uContact: { value: new THREE.Vector2(contactRadius, 0) },
221
224
  },
222
225
  });
223
226
  post.rayScene = fullScreen(post.rayMat);
@@ -254,6 +257,7 @@ export function createPostProcessing({
254
257
  a.uTan.value.copy(u.uTan.value);
255
258
  a.uAspect.value = u.uAspect.value;
256
259
  a.uAO.value = u.uAO.value;
260
+ a.uContact.value.y = contactStrength();
257
261
  rn.setRenderTarget(post.aoRT);
258
262
  rn.render(post.aoScene, post.aoCam);
259
263
  }
@@ -480,9 +484,8 @@ export function createPostProcessing({
480
484
  }
481
485
  // The upscale's targets while they exist, for the reckoning of memory.
482
486
  const upscaleTargets = () => [upscale.low, upscale.full];
483
- // A sprite drawn after the picture, at the screen's own size, tested against the scene's depth, and graded as the picture is: a name
484
- // over a person or a place stays sharp where the world is drawn smaller. It is laid over the screen's encoded colour, where the
485
- // picture's own pass laid it over light: its cover is raised to what it hid there (exact over black, near enough over the rest).
487
+ // Composite a depth-tested sprite at output resolution. Adjust its alpha for
488
+ // blending over encoded colour after the main grading pass.
486
489
  function drawnAtFullSize(sprite) {
487
490
  const material = sprite.material;
488
491
  material.onBeforeCompile = shader => {
@@ -0,0 +1,22 @@
1
+ const RAIN_PARS = `/* glsl */
2
+ uniform float uRain;`;
3
+
4
+ // In the water's main, after its waves (g): adds the rings' slope to g. Reads vW, dist, g, uTime, seaHash and OW.
5
+ const RAIN_GLSL = `/* glsl */
6
+ if (uRain > 0.01 && dist < 40.0) {
7
+ float showers = uRain * (1.0 - smoothstep(22.0, 40.0, dist)) * OW(30.0);
8
+ for (int layer = 0; layer < 2; layer++) {
9
+ float cellsAMetre = layer == 0 ? 1.6 : 2.3; // two grids of different size, so neither shows
10
+ vec2 q = vW.xz * cellsAMetre + float(layer) * vec2(0.5, 0.37), cell = floor(q), f = fract(q) - 0.5;
11
+ float h = seaHash(cell + float(layer) * 91.7), h2 = seaHash(cell.yx + 17.3), h3 = fract(h * 31.7 + h2 * 7.3);
12
+ float age = fract(uTime * (0.8 + 0.7 * h2) + h * 7.0); // a drop strikes the cell about once a second
13
+ float reach = 0.14 + 0.16 * h3; // how far its ring spreads, in cells
14
+ vec2 d = f - (vec2(h, h2) - 0.5) * (0.8 - 2.0 * reach); // from where it struck, so the ring stays in its cell
15
+ float r = length(d) + 1e-4, x = (r - age * reach) * 18.0 * 1.6 / cellsAMetre; // across the ring: 18 radians a cell of the first grid
16
+ float ring = sin(x) * exp(-x * x * 0.06) * (1.0 - age) * (1.0 - age) * (0.6 + 0.4 * h3);
17
+ float struck = step(fract(h * 53.1 + float(layer) * 0.5), uRain * (layer == 0 ? 0.8 : 0.6)); // a lighter shower, fewer drops
18
+ g += d / r * (0.4 * ring * struck * showers);
19
+ }
20
+ }`;
21
+
22
+ export { RAIN_GLSL, RAIN_PARS };
@@ -0,0 +1,63 @@
1
+ import * as THREE from 'three';
2
+
3
+ // Packed earth, wheel ruts and rain-dependent puddles on a road strip.
4
+ const ROAD_COMMON = `/* glsl */ varying vec4 vRoad; varying vec3 vRW; float gPud = 0.0;
5
+ float rh(vec2 p){ vec3 q = fract(vec3(p.xyx) * .1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }
6
+ float rn(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);
7
+ return mix(mix(rh(i), rh(i + vec2(1, 0)), f.x), mix(rh(i + vec2(0, 1)), rh(i + vec2(1, 1)), f.x), f.y); }
8
+ varying vec3 vVerge; uniform sampler2D uGrit; uniform float uRoadDamp;`;
9
+ const ROAD_SURFACE = `/* glsl */
10
+ float x = vRoad.x, s = vRoad.y, hw = vRoad.z, fine = smoothstep(160.0, 45.0, length(vViewPosition)); // fine grain fades out before it can shimmer
11
+ float nw = mix(0.5, rn(vRW.xz * 1.9), fine), nb = rn(vRW.xz * 0.37), ns = rn(vRW.xz * 0.11 + 7.0);
12
+ float side = x > 0.0 ? 3.1 : 9.4; // each edge meanders on its own
13
+ float edge = hw + (rn(vec2(s * 0.018, side + 1.0)) - 0.5) * 1.4 + (rn(vec2(s * 0.06, side)) - 0.5) * 1.2 + (rn(vec2(s * 0.24, side + 2.0)) - 0.5) * 0.5 + (rn(vec2(s * 0.9, side + 4.0)) - 0.5) * 0.3 * fine - abs(x); // metres inside the travelled edge
14
+ float drift = (rn(vec2(s * 0.02, 1.3)) - 0.5) * 1.3 + (rn(vec2(s * 0.09, 2.3)) - 0.5) * 0.3, gauge = 0.78 + (rn(vec2(s * 0.05, 5.1)) - 0.5) * 0.14; // the ruts wander as a pair
15
+ float c = x - drift, r1 = c - gauge, r2 = c + gauge, rw = 0.08 + 0.08 * ns, deep = 0.18 + 0.3 * rn(vec2(s * 0.07, 6.6));
16
+ float rut = (exp(-r1 * r1 / rw) + exp(-r2 * r2 / rw)) * smoothstep(-0.3, 0.6, edge);
17
+ float crown = (1.0 - smoothstep(gauge * 0.3, gauge * 0.72, abs(c))) * vRoad.w;
18
+ float grass = clamp(crown * smoothstep(0.38, 0.66, nb * 0.75 + nw * 0.25) + (1.0 - smoothstep(-0.2, 0.9, edge)) * smoothstep(0.42, 0.68, nb) * 0.7, 0.0, 1.0);
19
+ gPud = smoothstep(0.08, 0.45, uRoadDamp) * smoothstep(0.35, 0.8, rut) * smoothstep(0.7, 0.76, rn(vec2(s * 0.16, c * 0.6 + 4.0)) * 0.75 + ns * 0.25) * (1.0 - grass); // water lies in the deeper stretches of rut
20
+ float worn = smoothstep(-0.2, 0.9, edge); // the travelled way, packed and dry, against its damp margin
21
+ float near = smoothstep(70.0, 6.0, length(vViewPosition)); // grit, pebbles and dried cracks, at walking distance
22
+ vec3 g1 = texture2D(uGrit, vRW.xz * 0.45).rgb, g2 = texture2D(uGrit, vRW.xz * 0.13 + 0.37).rgb;
23
+ float grit = mix(1.0, (0.72 + 0.56 * g1.r) * (0.86 + 0.28 * g2.r) * (1.0 + 0.28 * g1.g * g2.r * worn) * (1.0 - 0.22 * g1.b * worn * (1.0 - min(rut, 1.0))), near);
24
+ vec3 soil = diffuseColor.rgb * grit * (0.86 + 0.28 * ns) * (0.9 + 0.2 * nw) * mix(0.72, 1.06, worn) * (1.0 - deep * min(rut, 1.0)); // ruts are compacted, wetter and shadowed
25
+ diffuseColor.rgb = mix(soil, vVerge * (0.8 + 0.4 * nw), grass) * (1.0 - 0.45 * gPud);
26
+ diffuseColor.a *= smoothstep(-0.9, 0.5, edge + (nw - 0.5) * 0.8);`;
27
+ const ROAD_SHEEN = `/* glsl */
28
+ #if NUM_HEMI_LIGHTS > 0
29
+ if (gPud > 0.0) { float fr = 0.04 + 0.96 * pow(1.0 - clamp(dot(normal, normalize(vViewPosition)), 0.0, 1.0), 5.0); outgoingLight += gPud * fr * hemisphereLights[0].skyColor * 0.6; }
30
+ #endif
31
+ #include <opaque_fragment>`;
32
+
33
+ export function createRoadMaterial({ grit, dampness = { value: 0 } }) {
34
+ const mat = new THREE.MeshStandardMaterial({
35
+ transparent: true,
36
+ depthWrite: false,
37
+ vertexColors: true,
38
+ roughness: 0.95,
39
+ polygonOffset: true,
40
+ polygonOffsetFactor: -2,
41
+ polygonOffsetUnits: -2,
42
+ });
43
+ mat.onBeforeCompile = sh => {
44
+ sh.uniforms.uGrit = { value: grit };
45
+ sh.uniforms.uRoadDamp = dampness;
46
+ sh.vertexShader = sh.vertexShader
47
+ .replace(
48
+ '#include <common>',
49
+ '#include <common>\nattribute vec4 road; attribute vec3 verge; varying vec4 vRoad; varying vec3 vRW, vVerge;',
50
+ )
51
+ .replace(
52
+ '#include <begin_vertex>',
53
+ '#include <begin_vertex>\nvRoad = road; vVerge = verge; vRW = (modelMatrix * vec4(position, 1.0)).xyz;',
54
+ );
55
+ sh.fragmentShader = sh.fragmentShader
56
+ .replace('#include <common>', '#include <common>\n' + ROAD_COMMON)
57
+ .replace('#include <color_fragment>', '#include <color_fragment>\n' + ROAD_SURFACE)
58
+ .replace('#include <roughnessmap_fragment>', 'float roughnessFactor = mix(roughness, 0.08, gPud);')
59
+ .replace('#include <opaque_fragment>', ROAD_SHEEN);
60
+ };
61
+ mat.customProgramCacheKey = () => 'road-earth';
62
+ return mat;
63
+ }
@@ -0,0 +1,230 @@
1
+ // The noise foam is torn by, shared by every water (the sea's surf, a wake) and the beach.
2
+ export const FOAM_GLSL = `/* glsl */
3
+ float seaHash(vec2 c) { c = mod(c, 289.0); return fract(sin(dot(c, vec2(127.1, 311.7))) * 43758.5453); }
4
+ float seaNoise(vec2 x) {
5
+ vec2 i = floor(x), f = fract(x); f = f * f * (3.0 - 2.0 * f);
6
+ return mix(mix(seaHash(i), seaHash(i + vec2(1.0, 0.0)), f.x), mix(seaHash(i + vec2(0.0, 1.0)), seaHash(i + vec2(1.0, 1.0)), f.x), f.y);
7
+ }
8
+ // foam's lace, 0 to 1: threads of foam round holes of clear water, gathered in patches tens of metres across; once the
9
+ // threads would be finer than a pixel, the share of the water they cover (px: metres to a pixel)
10
+ float seaLace(vec2 q, float t, float px) {
11
+ vec2 a = q * 0.55 + vec2(0.07, 0.04) * t;
12
+ float n = seaNoise(a + 1.6 * vec2(seaNoise(a * 0.43 - 0.03 * t), seaNoise(a * 0.43 + 5.2)));
13
+ float n2 = seaNoise(q * 1.7 - vec2(0.05, 0.11) * t);
14
+ // a thread is about a tenth of a metre wide, the finer net's a third of that: each fades to its share of the water
15
+ // while it is still a pixel or two wide, or it crawls and sparkles as the foam drifts
16
+ float coarse = mix(0.2, smoothstep(0.72, 0.93, 1.0 - abs(2.0 * n - 1.0)), 1.0 - smoothstep(0.03, 0.1, px));
17
+ float fine = mix(0.08, smoothstep(0.78, 0.95, 1.0 - abs(2.0 * n2 - 1.0)), 1.0 - smoothstep(0.01, 0.035, px));
18
+ float gathered = mix(0.35, 1.0, smoothstep(0.3, 0.7, seaNoise(q * 0.09 + 3.1)));
19
+ return max(coarse, 0.7 * fine) * gathered;
20
+ }`;
21
+
22
+ // Shared by the water and the beach, so a breaking wave and the swash it sends up the sand keep time: the swell's phase at
23
+ // the water's edge, and its sets. Positions are measured from the land's corner (uSeaO), so the numbers stay small out here,
24
+ // 14 km from the world's middle.
25
+ export const SWELL_GLSL = `/* glsl */
26
+ const float SWELL_W = 0.785; // radians a second: a wave comes in every eight seconds
27
+ const float SHOAL_K = 8.0; // the swell's phase per root metre of depth: its crests close up as the water shoals
28
+ const float SEA_TAU = 6.2831853;
29
+ // how far a crest is put back along the shore, so the waves break in peaks and sections, never in one line
30
+ float swellShift(vec2 q) { return 1.9 * sin(q.x * 0.0123 + 1.3 * sin(q.y * 0.0171)) + 1.2 * sin(q.y * 0.0219 - q.x * 0.0083 + 2.0); }
31
+ // the swell's phase over the shoals, depth metres deep: a crest where it is a whole turn; at the water's edge (depth 0)
32
+ // a crest is a bore reaching the sand
33
+ float swellPhase(vec2 q, float depth, float t) { return SHOAL_K * sqrt(max(depth, 0.0) + 0.03) + t * SWELL_W + swellShift(q); }
34
+ // the height of the set, 0.48 to 1.08: every seventh wave or so is a big one
35
+ float swellSet(vec2 q, float t) { return 0.78 + 0.3 * sin(t * SWELL_W / 7.0 + (q.x + q.y) * 0.0021); }`;
36
+
37
+ // The water's own: its uniforms, and how far a point lies from the nearest wall and how far the moles shelter it.
38
+ export const seaParameters = ({ moleCount, quayCount, moleReach, moleWidth }) => `/* glsl */
39
+ uniform vec4 uSeaO; uniform vec2 uSwellDir;
40
+ uniform vec4 uMole[${moleCount}], uMoleF[${moleCount}], uQuay[${quayCount}];
41
+ ${SWELL_GLSL}
42
+ // (metres from p to the face of the nearest mole or quay, the share of the swell the moles let reach p, how far p lies
43
+ // among them: there they, not the city's sheltered water, say how still it is)
44
+ vec3 seaWalls(vec2 p) {
45
+ float d = 1e4, open = 1.0, among = 0.0;
46
+ for (int i = 0; i < ${moleCount}; i++) {
47
+ vec4 M = uMole[i], F = uMoleF[i]; // centre x, z, radius, mouth; the city's turn (sin, cos), half the mouth's angle, in use
48
+ vec2 rel = p - M.xy; float r = length(rel);
49
+ if (F.w < 0.5 || r > M.z + 240.0) continue;
50
+ float a = atan(dot(rel, vec2(F.y, -F.x)), dot(rel, F.xy)); // radians round from straight out to sea
51
+ if (abs(a) > ${moleReach.toFixed(2)}) continue;
52
+ among = max(among, smoothstep(M.z + 240.0, M.z + 120.0, r));
53
+ float pastMouth = abs(a - M.w) - F.z; // radians beyond the mouth's edge: below 0 in the mouth
54
+ if (pastMouth > 0.0) d = min(d, abs(r - M.z) - ${(moleWidth / 2).toFixed(1)});
55
+ if (r < M.z) open *= mix(1.0, 0.35, smoothstep(0.0, 0.1, pastMouth)); // behind the mole, but for what comes in by its mouth
56
+ }
57
+ for (int i = 0; i < ${quayCount}; i++) {
58
+ vec4 Q = uQuay[i]; vec2 ab = Q.zw - Q.xy, ap = p - Q.xy;
59
+ d = min(d, length(ap - ab * clamp(dot(ap, ab) / dot(ab, ab), 0.0, 1.0)));
60
+ }
61
+ return vec3(max(d, 0.0), open, among);
62
+ }`;
63
+
64
+ // In the water's main: bends its waves (g) by the swell and gives the white water (rapid) and how churned the water is (surf).
65
+ // Reads the water's own: vW, bed, dep, dist, g, fp, OW, wv, uDepth, uDepthR, uTime.
66
+ export const SEA_WAVES = `/* glsl */
67
+ vec2 sq = vW.xz - uSeaO.xy;
68
+ float seaPx = length(fwidth(sq)), seaAfar = smoothstep(0.05, 0.3, seaPx); // metres to a pixel; and how far that is from near
69
+ vec3 walls = dist < 2500.0 ? seaWalls(vW.xz) : vec3(1e4, 1.0, 0.0);
70
+ float exposed = mix(clamp(1.0 - bed.b * 1.15, 0.0, 1.0), 1.0, walls.z) * walls.y; // the bed's B is sheltered water (0.84 at most): a harbour lies still
71
+ float setH = swellSet(sq, uTime), offshore = smoothstep(5.0, 7.8, dep);
72
+ // offshore, two trains of long swell running before the wind
73
+ vec2 swell2 = mat2(0.9, 0.435, -0.435, 0.9) * uSwellDir;
74
+ g += (wv(sq, -uSwellDir, 0.074, 0.85, 0.34 * setH) * OW(0.074) + wv(sq, -swell2, 0.11, 1.04, 0.18) * OW(0.11)) * offshore * exposed;
75
+ // over the shoals the crests follow the depth, closing up as it shallows: the phase is SHOAL_K times its root, which
76
+ // spaces them as waves are spaced over a steady slope. Offshore and in still water none of this is worked out.
77
+ float shoals = (1.0 - offshore) * exposed, surf = 0.0, white = 0.0, seaLaceK = 0.0;
78
+ vec2 seaward = -uSwellDir;
79
+ if (shoals > 0.0) {
80
+ vec2 bedUv = uDepthR.xy + vW.xz * uDepthR.zw; // (no mipmaps: the level is always 0)
81
+ vec2 deepens = (vec2(textureLod(uDepth, bedUv + vec2(uDepthR.z * uSeaO.z, 0.0), 0.0).r, textureLod(uDepth, bedUv + vec2(0.0, uDepthR.w * uSeaO.z), 0.0).r) - bed.r) * (10.0 / uSeaO.z);
82
+ float slope = length(deepens), h = max(dep, 0.0), rootH = sqrt(h + 0.03);
83
+ if (slope > 0.002) seaward = deepens / slope;
84
+ float psi = swellPhase(sq, h, uTime);
85
+ float sharp = mix(1.0, 4.5, smoothstep(4.0, 0.8, h)); // round swell, peaked as it shoals
86
+ float crest = exp(sharp * (cos(psi) - 1.0));
87
+ float dpsi = SHOAL_K * min(slope, 0.25) / (2.0 * rootH); // radians a metre, seaward
88
+ float breakAt = (1.6 + 0.6 * sin(sq.x * 0.029 + sq.y * 0.023)) * setH; // the depth the crests break at: deeper for a set's big waves, and in peaks
89
+ surf = smoothstep(breakAt, breakAt * 0.5, h) * shoals;
90
+ float swellH = 0.3 * setH * shoals * mix(1.0, 0.45, surf) * smoothstep(0.0, 0.3, h); // a broken wave runs on as a lower bore
91
+ g -= seaward * (swellH * sharp * sin(psi) * crest * min(dpsi, 2.5)) * OW(dpsi * sharp);
92
+ // white water: the lip as a crest curls, the bore it leaves rolling in, and lace torn from it lying between
93
+ if (surf > 0.0) {
94
+ seaLaceK = seaLace(sq + seaward * (uTime * 0.6), uTime, seaPx);
95
+ float behind = fract(psi / SEA_TAU);
96
+ white = surf * (exp(-behind * 10.0) * (0.65 + 0.35 * seaLaceK) + exp(-behind * 2.2) * seaLaceK * 0.9 + 0.35 * seaLaceK + smoothstep(0.92, 1.0, behind) * 0.5);
97
+ // from afar the breakers' white lines, each a pixel or two, come and go along the whole coast at once: there the white
98
+ // water is held at what it is on average over a wave, as the beach's swash is (SHORE_GLSL)
99
+ white = mix(white, surf * (0.085 + 0.745 * seaLaceK), seaAfar);
100
+ }
101
+ }
102
+ // at a mole or a quay's foot: a ring of foam, bursting out as each crest of the swell strikes; a lap of it in a harbour
103
+ if (walls.x < 60.0) {
104
+ if (surf <= 0.0) seaLaceK = seaLace(sq + seaward * (uTime * 0.6), uTime, seaPx);
105
+ float strike = mix(pow(0.5 + 0.5 * cos(-0.074 * dot(uSwellDir, sq) + 0.85 * uTime), 4.0), 0.27, seaAfar); // its mean, from afar
106
+ float reach = mix(1.2, 13.0, exposed * (0.3 + 0.7 * strike)); // metres the foam spreads from the wall's foot
107
+ float burst = exp(-walls.x / reach) * (0.6 + 0.4 * seaLaceK) * (0.3 + 0.7 * exposed * (0.45 + 0.55 * strike));
108
+ white = max(white, burst + exp(-walls.x / 22.0) * exposed * seaLaceK * 0.8); // and the lace it leaves spreading round it
109
+ }
110
+ rapid = clamp(white, 0.0, 1.0) * mix(1.0, 0.7, 1.0 - OW(1.5));`;
111
+
112
+ // The beach's own colour, in the ground's shader as soon as its baked colour (base) is read: the bake fades the foot of the
113
+ // beach into the grey-green of a lake's bed over the 1.6 m about the sea's level, which on the
114
+ // sea's shore laid a green band along every beach. There the sea's bed is drawn instead: the beach's pale sand into the
115
+ // shallows, darkening and greening with weed only as it deepens. Reads vWP, landUv, uLandR, uSea, uShore; sets base, and past the
116
+ // map's edge under the sea tint's salt and cover too.
117
+ export const shoreBed = ({ beach, lakeBed, weed }) => `/* glsl */
118
+ if (uShore > 0.5 && vWP.y < uSea + 1.4 && texture2D(uSeaDepth, landUv).a > 0.5) { // the sea's bed, never a lake's sunk below the sea (the map's A)
119
+ const vec3 BEACH = vec3(${beach.join(', ')}), LAKE_BED = vec3(${lakeBed.join(', ')}), WEED = vec3(${weed.join(', ')}); // palette values in linear light
120
+ const float OFF_MAP_FADE = 150.0; // metres past the map's edge over which its own bed gives way
121
+ float baked = 1.0 - smoothstep(uSea - 0.2, uSea + 1.4, vWP.y); // how much of the bed's tone the bake laid here
122
+ float under = uSea - vWP.y; // metres of water over the bed
123
+ // wet sand in the shallows, weed on the middle slope, and the bed's own tone by 7 m, the plain bed material's
124
+ vec3 seaBed = mix(BEACH * 0.72, mix(LAKE_BED, WEED, 0.4 * (1.0 - smoothstep(4.5, 7.0, under))), smoothstep(1.2, 6.0, under));
125
+ // past the map's edge its maps are read at the edge, so a cape that meets it ran on as a pale band of sand under the sea
126
+ // to the horizon: under water out there the bed takes the tone the bake lays under all the sea, crust and cover none
127
+ vec2 pastUv = max(-landUv, landUv - 1.0);
128
+ float offMap = smoothstep(0.0, OFF_MAP_FADE, max(pastUv.x / uLandR.z, pastUv.y / uLandR.w)) * (1.0 - smoothstep(uSea - 0.3, uSea + 0.3, vWP.y));
129
+ base = mix(base, LAKE_BED, offMap); tint.a *= 1.0 - offMap; cover *= 1.0 - offMap;
130
+ base += (seaBed - LAKE_BED) * baked;
131
+ }`;
132
+
133
+ // The beach, in the ground's shader after its colour and relief (diffuseColor, gTH) are worked out: where the sea meets the
134
+ // sand, the swash runs up and drains back with a band of foam at its front, the sand under it dark and glassy; below the
135
+ // highest reach of a set's swash the sand lies wet, darker and glossier the lower it is, and at that reach a line of the
136
+ // foam's last lace and the wrack it left. shoreGloss (glassy) and shoreDamp (wet) are for the roughness. Reads vWP, landUv,
137
+ // vd, gWN, uSea, uTime, uLandR, uShore, uSeaDepth (the water's depth map, its B sheltered water), uWind, uNight; a shore with no
138
+ // depth map defines SHORE_EXPOSED, how open to the swell it is (0 to 1), instead.
139
+ export const SHORE_GLSL = `/* glsl */
140
+ #ifndef SHORE_EXPOSED
141
+ #define SHORE_EXPOSED clamp(1.0 - texture2D(uSeaDepth, landUv).b * 1.15, 0.0, 1.0)
142
+ #define SHORE_AT_SEA (texture2D(uSeaDepth, landUv).a > 0.5)
143
+ #else
144
+ #define SHORE_AT_SEA true
145
+ #endif
146
+ float shoreGloss = 0.0, shoreDamp = 0.0;
147
+ float rise = vWP.y - uSea; // metres above the still sea
148
+ if (uShore > 0.5 && rise < 1.0 && rise > -0.3 && SHORE_AT_SEA) { // the sea's swash, never on a lake's bed sunk below it
149
+ // the swash's marks fade out over the last of the block, under the still sea, rather than stop at a line along the shore
150
+ vec3 dry = diffuseColor.rgb; float flatTH = gTH, inSwash = smoothstep(-0.3, -0.1, rise);
151
+ vec2 sq = vWP.xz - uLandR.xy;
152
+ float exposed = SHORE_EXPOSED, px = length(fwidth(sq)); // metres to a pixel
153
+ // seen from afar the swash's running edge is a pixel or less across and its sheet sweeps metres of sand at a time:
154
+ // there its edge is softened and the sheet held nearer its average, so the coast does not pulse
155
+ float afar = smoothstep(0.05, 0.3, px);
156
+ float setH = swellSet(sq, uTime);
157
+ float runUp = 0.05 + 0.42 * setH * exposed; // how high the swash climbs: a lap of a few centimetres in a harbour
158
+ float tau = fract(swellPhase(sq, 0.0, uTime) / SEA_TAU); // how far through its wave: 0 as the bore reaches the sand
159
+ float up = tau < 0.3 ? 1.0 - (1.0 - tau / 0.3) * (1.0 - tau / 0.3) : 1.0 - pow((tau - 0.3) / 0.7, 1.4); // a quick rush up, a slow drain back
160
+ float front = runUp * up + 0.03 * (seaNoise(sq * 0.31) - 0.5) - 0.02, fw = fwidth(rise) + 0.003 + 0.06 * afar * runUp;
161
+ float sheet = 1.0 - smoothstep(front - fw, front + fw, rise); // the sheet of water as it is now; the foam rides its front
162
+ float film = mix(sheet, 1.0 - smoothstep(-0.02, runUp * 0.75, rise), afar * 0.85); // and as it darkens the sand: from afar, the time it covers it
163
+ float lace = seaLace(sq, uTime, px);
164
+ // the foam rides the front up the sand, thick in the rush and thinning as it drains; what it strands lies as lace
165
+ float rush = 1.0 - smoothstep(0.15, 0.55, tau);
166
+ float edge = sheet * smoothstep(front - 0.05 - 0.05 * rush - fw, front - fw * 0.5, rise) * (0.35 + 0.65 * rush) * (0.6 + 0.4 * lace);
167
+ float stranded = sheet * lace * 0.4 * (1.0 - rush) * (1.0 - smoothstep(0.0, 0.6, rise / max(front, 0.05))); // lying low, where the drain left it
168
+ // from afar the foam riding up and down the sand is a white line sweeping the beach every eight seconds, which reads as the
169
+ // whole coast flickering (the owner): there it is held at where it lies on average, a faint band over the swash's middle
170
+ float meanFoam = exp(-abs(rise - 0.5 * runUp) / max(0.4 * runUp, fw)) * (0.15 + 0.15 * lace);
171
+ float foam = clamp(mix(max(edge, stranded), meanFoam, afar) * (0.3 + 0.7 * exposed), 0.0, 1.0);
172
+ // the tide line: as high as a set's big waves reach, ragged along the shore; the sand below it is wet, darkest and
173
+ // glossiest at the bottom, and glassy for a moment where the backwash has just drained off it
174
+ float reach = 0.07 + 0.46 * exposed * (0.9 + 0.2 * seaNoise(sq * 0.045)) + 0.09 * (seaNoise(sq * 0.11 + 7.0) - 0.5) + 0.03 * (seaNoise(sq * 0.6) - 0.5); // meandering
175
+ float wet = 1.0 - smoothstep(reach - 0.1, reach + fw, rise), low = 1.0 - smoothstep(0.0, reach, rise);
176
+ float drained = (1.0 - smoothstep(front, front + 0.1, rise)) * (1.0 - sheet) * (1.0 - rush) * (1.0 - afar);
177
+ // the foot of the beach is already the wet sand of the bed (SHORE_BED): darkened again here, and again under the still sea,
178
+ // it lay along every shore as a dark outline between the dry sand and the shallows, metres wide on a gentle beach
179
+ float notUnder = smoothstep(-0.15, 0.05, rise);
180
+ diffuseColor.rgb *= 1.0 - wet * mix(0.08, 0.16, low) * notUnder;
181
+ shoreDamp = wet * mix(0.6, 1.0, low);
182
+ // the foam's last reach: a line of its dried lace a hand wide, with dark wrack caught in it, gone to a faint line
183
+ // before it is finer than a pixel
184
+ // (measured across the sand, not up it: on a flat beach the rise hardly changes from pixel to pixel however far off)
185
+ float lineW = 0.012 + fw, atLine = exp(-abs(rise - reach - 0.01) / lineW), sharpLine = (1.0 - smoothstep(0.012, 0.04, fw)) * (1.0 - smoothstep(0.02, 0.06, px));
186
+ float wrack = smoothstep(0.66, 0.82, seaNoise(sq * 6.5 + 3.0)) * smoothstep(0.4, 0.7, seaNoise(sq * 0.5)) * sharpLine; // in flecks, gathered in stretches
187
+ float dried = atLine * mix(0.35, 0.4 + 0.6 * smoothstep(0.1, 0.5, seaLace(sq * 1.7, 0.0, px * 1.7)), sharpLine) * exposed;
188
+ diffuseColor.rgb = mix(diffuseColor.rgb, vec3(0.7, 0.69, 0.64), dried * 0.75);
189
+ diffuseColor.rgb *= 1.0 - atLine * wrack * 0.45 * exposed;
190
+ diffuseColor.rgb *= 1.0 - 0.14 * film * notUnder; // sand under a sheet of water is darker,
191
+ shoreGloss = max(film, drained * 0.7); // and glassy, and so for a moment where it has drained
192
+ gTH *= 1.0 - 0.7 * film - 0.25 * wet; // water smooths the grain over
193
+ diffuseColor.rgb = mix(diffuseColor.rgb, vec3(0.78, 0.8, 0.78), foam);
194
+ diffuseColor.rgb = mix(dry, diffuseColor.rgb, inSwash);
195
+ gTH = mix(flatTH, gTH, inSwash);
196
+ shoreGloss *= inSwash;
197
+ shoreDamp *= inSwash;
198
+ }
199
+ // the bed under the shallows: ripples the waves raise in the sand, running along the shore a hand's breadth high, and the
200
+ // sun's light gathered by the waves into a moving net; each gone before it is finer than a pixel
201
+ if (uShore > 0.5 && rise < 0.05 && rise > -4.5 && vd < 70.0 && SHORE_AT_SEA) {
202
+ vec2 sq = vWP.xz - uLandR.xy, down = length(gWN.xz) > 0.004 ? normalize(gWN.xz) : uWind; // down the bed's slope: out to sea
203
+ float across = dot(sq, down), along = dot(sq, vec2(-down.y, down.x));
204
+ float phase = across * 8.5 + 1.8 * sin(along * 0.9 + across * 0.3) + 0.9 * sin(along * 2.3 - across * 0.5);
205
+ float seenR = (1.0 - smoothstep(0.4, 1.2, fwidth(phase))) * (1.0 - smoothstep(30.0, 70.0, vd)) * smoothstep(0.02, 0.4, -rise);
206
+ float ripple = sin(phase) + 0.3 * sin(2.0 * phase + 1.1);
207
+ gTH += ripple * seenR * 0.2;
208
+ diffuseColor.rgb *= 1.0 + 0.07 * ripple * seenR; // silt settles in the troughs
209
+ vec2 cq = sq * 0.9;
210
+ float lines = abs(sin(cq.x + sin(cq.y * 1.3 + uTime * 0.9)) + sin(cq.y * 1.1 + sin(cq.x * 0.8 - uTime * 0.7)));
211
+ float lines2 = abs(sin(cq.x * 1.7 - cq.y * 0.6 + sin(cq.y * 1.9 - uTime * 1.1)) + sin(cq.y * 1.5 + cq.x * 0.4 + sin(cq.x * 1.4 + uTime * 0.8)));
212
+ float caustic = (1.0 - smoothstep(0.0, 0.3, lines)) + (1.0 - smoothstep(0.0, 0.3, lines2)) * 0.7;
213
+ float seenC = (1.0 - smoothstep(0.05, 0.15, length(fwidth(cq)))) * smoothstep(0.03, 0.3, -rise) * (1.0 - smoothstep(1.5, 4.5, -rise));
214
+ diffuseColor.rgb *= 1.0 + caustic * seenC * 0.55 * (1.0 - uNight);
215
+ }`;
216
+
217
+ // The colour of the sea's body: clear and green-blue over pale sand in the shallows, churned where the waves break, dark
218
+ // where it is deep. Reads waterline, surf, uDeep, uLum.
219
+ export const SEA_BODY = `/* glsl */
220
+ body = mix(uDeep, vec3(0.028, 0.15, 0.142) * uLum, exp(-waterline * 0.38));
221
+ body = mix(body, vec3(0.11, 0.15, 0.14) * uLum, surf * 0.45);`;
222
+ // How much of the bed the sea hides: less than the lake does, so the sand shows through the shallows; and all of it once
223
+ // the water is deep. The bed is drawn in two materials that meet along the cells of the ground at 9 m (the plain bed,
224
+ // the submerged bed), and the depth map reads no deeper than 8 m: where the water still let a tenth of the bed through, that seam
225
+ // showed from the air as stair steps along every coast. So the bed fades out from 6 m and is gone just short of 8 m, where
226
+ // every texel of the map round the seam reads (a harbour's basin, 6 m deep, shows its floor as before).
227
+ export const SEA_OPACITY = `/* glsl */
228
+ opacity = mix(0.06, 0.94, 1.0 - exp(-waterline * 0.3 / max(V.y, 0.3)));
229
+ opacity = mix(opacity, 1.0, smoothstep(6.0, 7.8, waterline));`;
230
+