roamwright 0.4.0 → 0.20.1

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 (139) hide show
  1. package/CONTRIBUTING.md +3 -1
  2. package/README.md +11 -34
  3. package/geometry/bedding.js +141 -0
  4. package/geometry/builder-cells.js +42 -0
  5. package/geometry/builder-frame.js +47 -0
  6. package/geometry/builder.js +189 -177
  7. package/geometry/cell-mask.js +25 -0
  8. package/geometry/creased-sheet.js +34 -34
  9. package/geometry/ear.js +36 -36
  10. package/geometry/erosion.js +65 -42
  11. package/geometry/face-grid.js +65 -65
  12. package/geometry/foliage.js +10 -10
  13. package/geometry/hair-gathers.js +41 -41
  14. package/geometry/hair-locks.js +41 -41
  15. package/geometry/hair-patch.js +18 -18
  16. package/geometry/hair-shell.js +31 -31
  17. package/geometry/head-profile.js +15 -15
  18. package/geometry/head-shape.js +77 -77
  19. package/geometry/heightfield.js +27 -18
  20. package/geometry/index-parts.js +2 -2
  21. package/geometry/instance-visibility.js +12 -12
  22. package/geometry/instances.js +15 -15
  23. package/geometry/outline-grid.js +7 -7
  24. package/geometry/part-ranges.js +3 -3
  25. package/geometry/path-field.js +63 -33
  26. package/geometry/placement.js +52 -52
  27. package/geometry/point-buckets.js +50 -0
  28. package/geometry/primitives.js +63 -47
  29. package/geometry/sagging-tube.js +27 -27
  30. package/geometry/sampled-surface.js +109 -77
  31. package/geometry/scatter-grid.js +46 -6
  32. package/geometry/sculpt.js +93 -93
  33. package/geometry/shore-grid.js +77 -34
  34. package/geometry/strata-field.js +92 -92
  35. package/geometry/strata-tiles.js +91 -91
  36. package/geometry/stream.js +70 -48
  37. package/geometry/terrain-chunks.js +87 -82
  38. package/geometry/terrain-extension.js +6 -6
  39. package/index.js +5 -0
  40. package/math/cell-edge.js +16 -16
  41. package/math/cell-key.js +6 -0
  42. package/math/circle-clearances.js +19 -0
  43. package/math/cloth-textures.js +6 -6
  44. package/math/cloud-textures.js +20 -20
  45. package/math/color.js +14 -14
  46. package/math/curve.js +22 -22
  47. package/math/mountain-ranges.js +216 -165
  48. package/math/noise.js +101 -49
  49. package/math/particle-noise.js +28 -28
  50. package/math/periodic.js +5 -5
  51. package/math/random.js +24 -15
  52. package/math/rectangles.js +52 -0
  53. package/math/rock-texture.js +7 -7
  54. package/math/scalar.js +5 -5
  55. package/math/surface-textures.js +54 -54
  56. package/motion/arm-reach.js +24 -24
  57. package/motion/ballistic-arc.js +11 -0
  58. package/motion/biped-cycle.js +44 -0
  59. package/motion/biped-leg.js +37 -0
  60. package/motion/biped-stride.js +98 -122
  61. package/motion/contact-transition.js +33 -0
  62. package/motion/crowd-alarms.js +39 -39
  63. package/motion/crowd-routine.js +94 -65
  64. package/motion/daily-clock.js +9 -9
  65. package/motion/deck-surface.js +38 -0
  66. package/motion/ground-lookahead.js +5 -5
  67. package/motion/instance-sight.js +8 -8
  68. package/motion/player.js +36 -28
  69. package/motion/quadruped.js +155 -155
  70. package/motion/spaces.js +32 -32
  71. package/motion/weather.js +73 -24
  72. package/motion/wind.js +32 -13
  73. package/package.json +7 -3
  74. package/rendering/caster-motion.js +5 -5
  75. package/rendering/cell-lod.js +138 -82
  76. package/rendering/cloud-dome.js +28 -28
  77. package/rendering/cloud-field.js +2 -2
  78. package/rendering/compilation-schedule.js +68 -11
  79. package/rendering/contact-shadows.js +2 -2
  80. package/rendering/context.js +12 -0
  81. package/rendering/crowd-skinning.js +13 -13
  82. package/rendering/crust.js +18 -12
  83. package/rendering/daylight.js +3 -3
  84. package/rendering/detail-range.js +28 -0
  85. package/rendering/dispose-meshes.js +18 -0
  86. package/rendering/dof.js +9 -9
  87. package/rendering/eye-adapt.js +14 -14
  88. package/rendering/far-shadow.js +8 -8
  89. package/rendering/flocks.js +78 -78
  90. package/rendering/garment-material.js +17 -17
  91. package/rendering/glow-lines.js +13 -13
  92. package/rendering/hair-strands.js +20 -20
  93. package/rendering/height-probe.js +149 -19
  94. package/rendering/label-layout.js +46 -0
  95. package/rendering/label-range.js +38 -0
  96. package/rendering/light-budget.js +52 -52
  97. package/rendering/light-shafts.js +23 -23
  98. package/rendering/local-lights.js +15 -4
  99. package/rendering/material-lod.js +49 -49
  100. package/rendering/particle-emitters.js +90 -90
  101. package/rendering/particles.js +78 -67
  102. package/rendering/piece-batch.js +17 -6
  103. package/rendering/planar-reflection.js +38 -38
  104. package/rendering/post-shaders.js +17 -13
  105. package/rendering/post.js +71 -71
  106. package/rendering/rain-streaks.js +67 -0
  107. package/rendering/road-surface.js +11 -11
  108. package/rendering/sea-shaders.js +11 -11
  109. package/rendering/shader-warmup.js +29 -16
  110. package/rendering/shadow-alignment.js +4 -4
  111. package/rendering/shadow-depth.js +7 -7
  112. package/rendering/sky-occlusion.js +16 -16
  113. package/rendering/sky.js +16 -6
  114. package/rendering/stage-lights.js +6 -6
  115. package/rendering/static-point-shadow.js +3 -3
  116. package/rendering/still-casters.js +1 -1
  117. package/rendering/strata-material.js +10 -10
  118. package/rendering/stream-flow.js +10 -10
  119. package/rendering/taa.js +26 -26
  120. package/rendering/talus.js +15 -15
  121. package/rendering/terrain-rock.js +7 -7
  122. package/rendering/texture-fills.js +8 -8
  123. package/rendering/textures.js +28 -28
  124. package/rendering/wakes.js +24 -24
  125. package/rendering/water-material.js +36 -23
  126. package/rendering/world-matrices.js +38 -38
  127. package/runtime/diagnostic-log.js +2 -2
  128. package/runtime/first-use.js +33 -28
  129. package/runtime/frame-pacing.js +33 -0
  130. package/runtime/gpu-timing.js +7 -7
  131. package/runtime/grid-cache.js +1 -1
  132. package/runtime/grid-rows.js +44 -5
  133. package/runtime/hand-over.js +41 -23
  134. package/runtime/pool.js +58 -16
  135. package/runtime/quality-fit.js +1 -1
  136. package/runtime/search.js +22 -22
  137. package/runtime/spatial-audio.js +13 -13
  138. package/runtime/withdrawable-task.js +35 -0
  139. package/runtime/worker.js +7 -0
@@ -1,21 +1,21 @@
1
1
  // The noise foam is torn by, shared by every water (the sea's surf, a wake) and the beach.
2
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);
3
+ float seaHash(vec2 cell) { cell = mod(cell, 289.0); return fract(sin(dot(cell, vec2(127.1, 311.7))) * 43758.5453); }
4
+ float seaNoise(vec2 samplePosition) {
5
+ vec2 cell = floor(samplePosition), blend = fract(samplePosition); blend = blend * blend * (3.0 - 2.0 * blend);
6
+ return mix(mix(seaHash(cell), seaHash(cell + vec2(1.0, 0.0)), blend.x), mix(seaHash(cell + vec2(0.0, 1.0)), seaHash(cell + vec2(1.0, 1.0)), blend.x), blend.y);
7
7
  }
8
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
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);
10
+ float seaLace(vec2 waterPosition, float elapsedTime, float metresPerPixel) {
11
+ vec2 coarsePosition = waterPosition * 0.55 + vec2(0.07, 0.04) * elapsedTime;
12
+ float coarseNoise = seaNoise(coarsePosition + 1.6 * vec2(seaNoise(coarsePosition * 0.43 - 0.03 * elapsedTime), seaNoise(coarsePosition * 0.43 + 5.2)));
13
+ float fineNoise = seaNoise(waterPosition * 1.7 - vec2(0.05, 0.11) * elapsedTime);
14
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
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)));
16
+ float coarse = mix(0.2, smoothstep(0.72, 0.93, 1.0 - abs(2.0 * coarseNoise - 1.0)), 1.0 - smoothstep(0.03, 0.1, metresPerPixel));
17
+ float fine = mix(0.08, smoothstep(0.78, 0.95, 1.0 - abs(2.0 * fineNoise - 1.0)), 1.0 - smoothstep(0.01, 0.035, metresPerPixel));
18
+ float gathered = mix(0.35, 1.0, smoothstep(0.3, 0.7, seaNoise(waterPosition * 0.09 + 3.1)));
19
19
  return max(coarse, 0.7 * fine) * gathered;
20
20
  }`;
21
21
 
@@ -29,7 +29,7 @@ function representatives(root) {
29
29
  const picked = new Map();
30
30
  root.traverse(object => {
31
31
  if (!(object.isMesh || object.isPoints || object.isLine || object.isSprite) || !object.material) return;
32
- const materials = Array.isArray(object.material) ? object.material.map(m => m.uuid).join(',') : object.material.uuid;
32
+ const materials = Array.isArray(object.material) ? object.material.map(material => material.uuid).join(',') : object.material.uuid;
33
33
  const key = materials + ':' + meshKind(object);
34
34
  if (!picked.has(key)) picked.set(key, object);
35
35
  });
@@ -60,26 +60,37 @@ export function createShaderWarmup({renderer, scene, camera, postMaterials = ()
60
60
  }
61
61
  if (entry.programs.size) continue;
62
62
  pending.delete(entry);
63
- entry.resolve();
63
+ entry.finish();
64
64
  }
65
65
  timer = pending.size ? setTimeout(poll, pollIntervalMs) : undefined;
66
66
  }
67
67
 
68
- function begin(traverse, root, view, lit, yieldIfReady = false) {
68
+ function begin(traverse, root, view, lit, yieldIfReady = false, signal) {
69
69
  const materials = renderer.compile({
70
70
  traverse,
71
71
  traverseVisible: visit => { if (lit !== root) root.traverseVisible(visit); }
72
72
  }, view, lit);
73
73
  const programs = new Set();
74
- for (const m of materials) {
75
- const variants = renderer.properties.get(m).programs;
74
+ for (const material of materials) {
75
+ const variants = renderer.properties.get(material).programs;
76
76
  if (variants) for (const program of variants.values()) {
77
77
  if (!ready.has(program)) programs.add(program);
78
78
  }
79
79
  }
80
80
  if (!programs.size && !yieldIfReady) return Promise.resolve();
81
- return new Promise(resolve => {
82
- pending.add({programs, resolve});
81
+ return new Promise((resolve, reject) => {
82
+ const entry = { programs, finish() {
83
+ signal?.removeEventListener('abort', abort);
84
+ resolve();
85
+ }};
86
+ function abort() {
87
+ pending.delete(entry);
88
+ if (!pending.size) { clearTimeout(timer); timer = undefined; }
89
+ reject(signal.reason);
90
+ }
91
+ if (signal?.aborted) { abort(); return; }
92
+ signal?.addEventListener('abort', abort, { once: true });
93
+ pending.add(entry);
83
94
  if (timer === undefined) timer = setTimeout(poll, 0);
84
95
  });
85
96
  }
@@ -98,9 +109,11 @@ export function createShaderWarmup({renderer, scene, camera, postMaterials = ()
98
109
  return begin(visit => objects.forEach(visit), root, view, lit);
99
110
  }
100
111
 
101
- async function compiledInTurn(root, view = camera, lit = scene, into = null) {
112
+ async function compiledInTurn(root, view = camera, lit = scene, into = null, signal) {
113
+ signal?.throwIfAborted();
102
114
  for (const object of representatives(root)) {
103
- await withTarget(into, () => begin(visit => visit(object), root, view, lit, true));
115
+ signal?.throwIfAborted();
116
+ await withTarget(into, () => begin(visit => visit(object), root, view, lit, true, signal));
104
117
  }
105
118
  }
106
119
 
@@ -109,9 +122,9 @@ export function createShaderWarmup({renderer, scene, camera, postMaterials = ()
109
122
  root.traverse(object => {
110
123
  if (!(object.isMesh || object.isPoints || object.isLine) || !object.material) return;
111
124
  if (!includeMaterialArrays && Array.isArray(object.material)) return;
112
- const m = standIn(object);
113
- if (!m) return;
114
- const key = [m.uuid, m.side, !!m.map, !!m.alphaMap, m.alphaTest > 0, meshKind(object)].join(':');
125
+ const standInMaterial = standIn(object);
126
+ if (!standInMaterial) return;
127
+ const key = [standInMaterial.uuid, standInMaterial.side, !!standInMaterial.map, !!standInMaterial.alphaMap, standInMaterial.alphaTest > 0, meshKind(object)].join(':');
115
128
  if (!picked.has(key)) picked.set(key, object);
116
129
  });
117
130
  return withTarget(into, () => {
@@ -160,10 +173,10 @@ export function createShaderWarmup({renderer, scene, camera, postMaterials = ()
160
173
 
161
174
  function warmShadowPass(root, {waitForPoint = true} = {}) {
162
175
  if (!renderer.shadowMap.enabled) return Promise.resolve();
163
- const depth = warmStandIns(root, o => o.castShadow && shadowMaterial(o, false), camera, target, null);
176
+ const depth = warmStandIns(root, object => object.castShadow && shadowMaterial(object, false), camera, target, null);
164
177
  let point;
165
- scene.traverseVisible(o => { if (o.isPointLight && o.castShadow) point = true; });
166
- const distance = point && warmStandIns(root, o => o.castShadow && shadowMaterial(o, true), camera, target, null);
178
+ scene.traverseVisible(object => { if (object.isPointLight && object.castShadow) point = true; });
179
+ const distance = point && warmStandIns(root, object => object.castShadow && shadowMaterial(object, true), camera, target, null);
167
180
  for (const twin of twins.values()) {
168
181
  twin.map = twin.alphaMap = twin.displacementMap = twin.clippingPlanes = null;
169
182
  }
@@ -186,7 +199,7 @@ export function createShaderWarmup({renderer, scene, camera, postMaterials = ()
186
199
  function dispose() {
187
200
  clearTimeout(timer);
188
201
  timer = undefined;
189
- for (const entry of pending) entry.resolve();
202
+ for (const entry of pending) entry.finish();
190
203
  pending.clear();
191
204
  quad.geometry.dispose();
192
205
  target.dispose();
@@ -10,10 +10,10 @@ export function createShadowSnap() {
10
10
  if (right.lengthSq() < 1e-8) right.set(1, 0, 0);
11
11
  right.normalize();
12
12
  up.crossVectors(direction, right);
13
- const a = Math.round((x * right.x + y * right.y + z * right.z) / texelWidth) * texelWidth;
14
- const u = Math.round((x * up.x + y * up.y + z * up.z) / texelHeight) * texelHeight;
15
- const d = x * direction.x + y * direction.y + z * direction.z;
16
- return out.set(0, 0, 0).addScaledVector(right, a).addScaledVector(up, u).addScaledVector(direction, d);
13
+ const snappedRight = Math.round((x * right.x + y * right.y + z * right.z) / texelWidth) * texelWidth;
14
+ const snappedUp = Math.round((x * up.x + y * up.y + z * up.z) / texelHeight) * texelHeight;
15
+ const depth = x * direction.x + y * direction.y + z * direction.z;
16
+ return out.set(0, 0, 0).addScaledVector(right, snappedRight).addScaledVector(up, snappedUp).addScaledVector(direction, depth);
17
17
  };
18
18
  }
19
19
 
@@ -15,11 +15,11 @@ function after(renderer, object, camera, shadowCamera, geometry, material, group
15
15
  previousAfter.call(this, renderer, object, camera, shadowCamera, geometry, material, group);
16
16
  }
17
17
 
18
- function kindOf(o) {
19
- const morph = o.geometry.morphAttributes;
18
+ function kindOf(object) {
19
+ const morph = object.geometry.morphAttributes;
20
20
  if (morph.position || morph.normal || morph.color) return null;
21
- if (o.isSkinnedMesh) return 'skinned';
22
- if (o.isInstancedMesh) return o.instanceColor ? 'tinted' : 'instanced';
21
+ if (object.isSkinnedMesh) return 'skinned';
22
+ if (object.isInstancedMesh) return object.instanceColor ? 'tinted' : 'instanced';
23
23
  return null;
24
24
  }
25
25
 
@@ -112,11 +112,11 @@ export function installShadowDepth(renderer) {
112
112
  handlers.set(renderer, {begin, end});
113
113
  function dispose(root) {
114
114
  if (!handlers.delete(renderer)) return;
115
- root?.traverse(o => {
116
- const entry = entries.get(o);
115
+ root?.traverse(object => {
116
+ const entry = entries.get(object);
117
117
  if (!entry) return;
118
118
  for (const field of ['customDepthMaterial','customDistanceMaterial']) {
119
- if (entry[field] && o[field] === entry[field]) o[field] = undefined;
119
+ if (entry[field] && object[field] === entry[field]) object[field] = undefined;
120
120
  }
121
121
  });
122
122
  for (const group of Object.values(materials)) for (const material of Object.values(group)) material.dispose();
@@ -22,9 +22,9 @@ export function createSkyOcclusionShader({minimum, underCover}) {
22
22
  return mix(1.0, ${minimum.toFixed(2)} + ${(1 - minimum).toFixed(2)} * open, uSkyOn * reach);
23
23
  }
24
24
  `;
25
- function patch(sh, more = '') {
26
- Object.assign(sh.uniforms, uniforms);
27
- sh.fragmentShader = sh.fragmentShader
25
+ function patch(shader, more = '') {
26
+ Object.assign(shader.uniforms, uniforms);
27
+ shader.fragmentShader = shader.fragmentShader
28
28
  .replace('#include <common>', '#include <common>\n' + SKY_OPEN)
29
29
  .replace(
30
30
  '#include <lights_fragment_begin>',
@@ -37,7 +37,7 @@ export function createSkyOcclusionShader({minimum, underCover}) {
37
37
  // Two passes: record overhead caster height, then integrate the horizon in eight directions.
38
38
  // The first map is available immediately. Subsequent height and horizon passes alternate frames.
39
39
  export function createSkyOcclusion({renderer, scene, camera, uniforms, config, samples, ground, strength,
40
- occludes = o => o.isMesh && o.castShadow, warmup}) {
40
+ occludes = object => object.isMesh && object.castShadow, warmup}) {
41
41
  const {compiled, warmStandIns, compilingNow} = warmup;
42
42
  const heights = new THREE.WebGLRenderTarget(config.size, config.size, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter });
43
43
  const openness = new THREE.WebGLRenderTarget(config.size, config.size, {
@@ -72,7 +72,7 @@ export function createSkyOcclusion({renderer, scene, camera, uniforms, config, s
72
72
  float a = float(i) * 0.7853982;
73
73
  vec2 dir = vec2(cos(a), sin(a)) / (uTexel * ${config.size.toFixed(1)});
74
74
  float rise = 0.0, top = h0;
75
- ${samples.map(d => `{ float h = heightAt(vUv + dir * ${d.toFixed(2)}); rise = max(rise, (h - h0) / ${d.toFixed(2)}); top = max(top, h); }`).join('\n ')}
75
+ ${samples.map(sampleDistance => `{ float h = heightAt(vUv + dir * ${sampleDistance.toFixed(2)}); rise = max(rise, (h - h0) / ${sampleDistance.toFixed(2)}); top = max(top, h); }`).join('\n ')}
76
76
  sky += 1.0 / (1.0 + rise * rise);
77
77
  tops += top;
78
78
  }
@@ -84,7 +84,7 @@ export function createSkyOcclusion({renderer, scene, camera, uniforms, config, s
84
84
  const passScene = new THREE.Scene().add(openPass),
85
85
  passCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
86
86
 
87
- const depthFor = o => (!occludes(o) ? null : o.isInstancedMesh ? (o.instanceColor ? depthOf.tinted : depthOf.instanced) : depthOf.plain);
87
+ const depthFor = object => (!occludes(object) ? null : object.isInstancedMesh ? (object.instanceColor ? depthOf.tinted : depthOf.instanced) : depthOf.plain);
88
88
  const pending = Object.assign(new THREE.Vector4(), { due: false }),
89
89
  hidden = [],
90
90
  dressed = [],
@@ -101,17 +101,17 @@ export function createSkyOcclusion({renderer, scene, camera, uniforms, config, s
101
101
  const shadows = renderer.shadowMap.autoUpdate, clearAlpha = renderer.getClearAlpha(), fog = scene.fog;
102
102
  renderer.getClearColor(_clear);
103
103
  try {
104
- scene.traverse(o => {
105
- if (!o.visible || !(o.isMesh || o.isPoints || o.isLine || o.isSprite)) return;
106
- const depth = depthFor(o);
104
+ scene.traverse(object => {
105
+ if (!object.visible || !(object.isMesh || object.isPoints || object.isLine || object.isSprite)) return;
106
+ const depth = depthFor(object);
107
107
  if (!depth) {
108
- o.visible = false;
109
- hidden.push(o);
108
+ object.visible = false;
109
+ hidden.push(object);
110
110
  return;
111
111
  }
112
- dressed.push(o);
113
- worn.push(o.material);
114
- o.material = depth;
112
+ dressed.push(object);
113
+ worn.push(object.material);
114
+ object.material = depth;
115
115
  });
116
116
  renderer.shadowMap.autoUpdate = false;
117
117
  renderer.setRenderTarget(heights);
@@ -152,7 +152,7 @@ export function createSkyOcclusion({renderer, scene, camera, uniforms, config, s
152
152
  }
153
153
 
154
154
  const drawn = { x: NaN, z: NaN, age: 0 };
155
- function update(dt) {
155
+ function update(deltaSeconds) {
156
156
  const x = camera.position.x,
157
157
  z = camera.position.z,
158
158
  high = camera.position.y - ground(x, z),
@@ -163,7 +163,7 @@ export function createSkyOcclusion({renderer, scene, camera, uniforms, config, s
163
163
  workOutOpenness();
164
164
  return;
165
165
  }
166
- drawn.age += dt;
166
+ drawn.age += deltaSeconds;
167
167
  if (Math.hypot(x - drawn.x, z - drawn.z) < config.step && drawn.age < config.every) return;
168
168
  const first = Number.isNaN(drawn.x);
169
169
  drawHeights(x, z);
package/rendering/sky.js CHANGED
@@ -1,5 +1,15 @@
1
+ /**
2
+ * Render the sky and its depth mask from a cloud field and a cloud dome.
3
+ * @module
4
+ */
5
+
1
6
  import * as THREE from 'three';
2
7
 
8
+ /**
9
+ * Combine a cloud field and dome into a sky mesh and a mask for effects passes.
10
+ * Both meshes share geometry. Call the returned `dispose()` when finished with the pair.
11
+ * @param options - Outputs from `createCloudField` and `createCloudDome`, plus an optional moon angular radius in radians.
12
+ */
3
13
  export function createSky({ field, dome, moonRadius: MOON_RADIUS = 0.0347 }) {
4
14
  const {
5
15
  fieldGLSL: CLOUD_FIELD_PARS,
@@ -52,16 +62,16 @@ export function createSky({ field, dome, moonRadius: MOON_RADIUS = 0.0347 }) {
52
62
  const float SHEET = ${(FLARE / CLOUD_SHEET).toFixed(6)}; // the sheet's share of a unit of the layer
53
63
  const float MANY = 0.86; // how much of the light scattered many times in a cloud comes out of it,
54
64
  const float ONCE = 0.5; // and of the light scattered once
55
- float hash12(vec2 p){ vec3 p3 = fract(vec3(p.xyx) * .1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }
56
- float hash13(vec3 p){ p = fract(p * .1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }
57
- float vn(vec2 p){ vec2 i = floor(p), f = fract(p); f = f*f*(3.-2.*f);
58
- return mix(mix(hash12(i), hash12(i+vec2(1,0)), f.x), mix(hash12(i+vec2(0,1)), hash12(i+vec2(1,1)), f.x), f.y); }
65
+ float hash12(vec2 samplePosition){ vec3 mixed = fract(vec3(samplePosition.xyx) * .1031); mixed += dot(mixed, mixed.yzx + 33.33); return fract((mixed.x + mixed.y) * mixed.z); }
66
+ float hash13(vec3 samplePosition){ samplePosition = fract(samplePosition * .1031); samplePosition += dot(samplePosition, samplePosition.zyx + 31.32); return fract((samplePosition.x + samplePosition.y) * samplePosition.z); }
67
+ float vn(vec2 samplePosition){ vec2 cell = floor(samplePosition), blend = fract(samplePosition); blend = blend*blend*(3.-2.*blend);
68
+ return mix(mix(hash12(cell), hash12(cell+vec2(1,0)), blend.x), mix(hash12(cell+vec2(0,1)), hash12(cell+vec2(1,1)), blend.x), blend.y); }
59
69
  // where a direction meets the clouds' base, on the sheet
60
- vec2 sheetAt(vec3 d){ return d.xz / (max(d.y, 0.0) + ${DIP}) * SHEET + uCloudAt; }
70
+ vec2 sheetAt(vec3 viewDirection){ return viewDirection.xz / (max(viewDirection.y, 0.0) + ${DIP}) * SHEET + uCloudAt; }
61
71
  // how much of a cloud is drawn at a direction's height: none low in the sky, over the roofs and hills (cloud-field.js)
62
72
  float aloft(float y){ return smoothstep(${LOWEST}, ${WHOLE}, y); }
63
73
  // how much of the light a drop sends on at an angle from the way it came (c the cosine): g > 0 mostly onwards
64
- float sentOn(float c, float g){ float k = 1.0 + g * g - 2.0 * g * c; return (1.0 - g * g) / (k * sqrt(k)); }
74
+ float sentOn(float cosAngle, float anisotropy){ float phaseTerm = 1.0 + anisotropy * anisotropy - 2.0 * anisotropy * cosAngle; return (1.0 - anisotropy * anisotropy) / (phaseTerm * sqrt(phaseTerm)); }
65
75
  // the clouds in a direction: rgb the light they send to the eye, a how much of the sky behind them they hide
66
76
  vec4 clouds(vec3 d, vec3 behind){
67
77
  vec4 dome = cloudDome(d);
@@ -197,13 +197,13 @@ export function createNarrowFraming({
197
197
  export function cameraKeyAt(keys, t, out) {
198
198
  let i = 0;
199
199
  while (i < keys.length - 2 && keys[i + 1][0] <= t) i++;
200
- const a = keys[i],
201
- b = keys[i + 1],
202
- cutNext = !!b[7];
203
- let u = cutNext ? 0 : clamp((t - a[0]) / (b[0] - a[0]), 0, 1);
200
+ const currentKey = keys[i],
201
+ nextKey = keys[i + 1],
202
+ cutNext = !!nextKey[7];
203
+ let u = cutNext ? 0 : clamp((t - currentKey[0]) / (nextKey[0] - currentKey[0]), 0, 1);
204
204
  u = u * u * (3 - 2 * u);
205
- out.src = cutNext && t >= b[0] ? b : a;
206
- out.dst = cutNext ? out.src : b;
205
+ out.src = cutNext && t >= nextKey[0] ? nextKey : currentKey;
206
+ out.dst = cutNext ? out.src : nextKey;
207
207
  out.u = u;
208
208
  return out;
209
209
  }
@@ -14,8 +14,8 @@ export function createStaticPointShadow(renderer) {
14
14
  let previousClear;
15
15
 
16
16
  function framebuffer(target, face) {
17
- const fb = renderer.properties.get(target).__webglFramebuffer;
18
- return target.isWebGLCubeRenderTarget ? fb[face] : fb;
17
+ const framebuffer = renderer.properties.get(target).__webglFramebuffer;
18
+ return target.isWebGLCubeRenderTarget ? framebuffer[face] : framebuffer;
19
19
  }
20
20
 
21
21
  function copyFace(source, destination, face) {
@@ -56,7 +56,7 @@ export function createStaticPointShadow(renderer) {
56
56
  const target = renderer.getRenderTarget(), face = renderer.getActiveCubeFace(), level = renderer.getActiveMipmapLevel();
57
57
  try {
58
58
  renderer.setRenderTarget(map);
59
- for (let f = 0; f < (map.isWebGLCubeRenderTarget ? 6 : 1); f++) copyFace(map, picture, f);
59
+ for (let faceIndex = 0; faceIndex < (map.isWebGLCubeRenderTarget ? 6 : 1); faceIndex++) copyFace(map, picture, faceIndex);
60
60
  } finally { renderer.setRenderTarget(target, face, level); }
61
61
  origin.copy(current);
62
62
  cachedNear = shadow.camera.near;
@@ -10,7 +10,7 @@ const still = { kept: false, sum: 0, round: 0 }; // round: which drawing of the
10
10
  const markStill = object => object.layers.enable(STILL_LAYER);
11
11
  const isStill = object => (object.layers.mask & STILL_BIT) !== 0;
12
12
  // Where a mesh stands, how it is turned (about any axis) and its size, as one number
13
- const placeOf = e => e[12] * 1.37 + e[13] * 2.11 + e[14] * 0.59 + e[0] * 5.3 + e[1] * 3.3 + e[2] * 4.1 + e[5] * 1.9 + e[6] * 2.7;
13
+ const placeOf = elements => elements[12] * 1.37 + elements[13] * 2.11 + elements[14] * 0.59 + elements[0] * 5.3 + elements[1] * 3.3 + elements[2] * 4.1 + elements[5] * 1.9 + elements[6] * 2.7;
14
14
 
15
15
  // Record a caster during cache generation. On subsequent draws, skip it only
16
16
  // when its cached revision still matches the caller-supplied transform or batch revision.
@@ -59,35 +59,35 @@ const FULL_LIGHT = 'float rockShade(vec2 xz) { return 1.0; }';
59
59
 
60
60
  export function createStrataMaterial({ texture, palette, snowline = 1e7, shading = FULL_LIGHT, vertex = STATIC_VERTEX, uniforms = {} }) {
61
61
  const head = vertex + '\nvarying vec3 vWP, vWN; varying vec4 vRock; varying float vYY;';
62
- const m = new MeshStandardMaterial({ vertexColors: true, roughness: 0.93 });
62
+ const material = new MeshStandardMaterial({ vertexColors: true, roughness: 0.93 });
63
63
  const colors = {};
64
64
  for (const key of ['dark', 'light', 'iron', 'shale', 'dust', 'lichen', 'crust', 'snow']) {
65
65
  const rgb = palette[key];
66
66
  if (!rgb || rgb.length !== 3 || !rgb.every(Number.isFinite)) throw new TypeError('Expected a linear RGB triple for ' + key);
67
67
  colors['u' + key[0].toUpperCase() + key.slice(1)] = { value: new Color(...rgb) };
68
68
  }
69
- m.onBeforeCompile = sh => {
70
- Object.assign(sh.uniforms, uniforms, colors, { uRockTex: { value: texture }, uSnowline: { value: snowline } });
71
- sh.vertexShader = sh.vertexShader
69
+ material.onBeforeCompile = shader => {
70
+ Object.assign(shader.uniforms, uniforms, colors, { uRockTex: { value: texture }, uSnowline: { value: snowline } });
71
+ shader.vertexShader = shader.vertexShader
72
72
  .replace('#include <common>', '#include <common>\n' + head)
73
73
  .replace('#include <beginnormal_vertex>', 'cliffMorph(); vec3 objectNormal = cN;')
74
74
  .replace('#include <begin_vertex>', 'vec3 transformed = cP; vWP = cP; vWN = cN; vRock = aRock; vYY = aYY;')
75
75
  .replace('#include <project_vertex>', '#include <project_vertex>\nif (cHide > 0.5) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);');
76
- sh.fragmentShader = sh.fragmentShader
76
+ shader.fragmentShader = shader.fragmentShader
77
77
  .replace('#include <common>', '#include <common>\n' + shading + '\n' + CLIFF_FRAG_HEAD + '\n' + terrainReliefGLSL)
78
78
  .replace('#include <color_fragment>', CLIFF_FRAG)
79
79
  .replace('#include <roughnessmap_fragment>', '#include <roughnessmap_fragment>\nroughnessFactor = mix(roughnessFactor, 0.62, gSnow);')
80
80
  .replace('#include <normal_fragment_maps>', CLIFF_BUMP);
81
81
  };
82
- m.customProgramCacheKey = () => 'strata:' + vertex + ':' + shading;
82
+ material.customProgramCacheKey = () => 'strata:' + vertex + ':' + shading;
83
83
  const depth = new MeshDepthMaterial({ depthPacking: RGBADepthPacking });
84
- depth.onBeforeCompile = sh => {
85
- Object.assign(sh.uniforms, uniforms);
86
- sh.vertexShader = sh.vertexShader
84
+ depth.onBeforeCompile = shader => {
85
+ Object.assign(shader.uniforms, uniforms);
86
+ shader.vertexShader = shader.vertexShader
87
87
  .replace('#include <common>', '#include <common>\n' + vertex)
88
88
  .replace('#include <begin_vertex>', 'cliffMorph(); vec3 transformed = cP;')
89
89
  .replace('#include <project_vertex>', '#include <project_vertex>\nif (cHide > 0.5) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);');
90
90
  };
91
91
  depth.customProgramCacheKey = () => 'strata-depth:' + vertex;
92
- return { material: m, depth };
92
+ return { material: material, depth };
93
93
  }
@@ -6,21 +6,21 @@ export const STREAM_FLOW_GLSL = /* glsl */ `
6
6
  }
7
7
 
8
8
  vec4 streamFlow(vec3 flow, float acrossMetres, float depth, float travel, float distanceToEye, float footprint) {
9
- vec2 q = vec2(acrossMetres, flow.y - travel);
10
- footprint = max(footprint, max(length(dFdx(q)), length(dFdy(q))));
9
+ vec2 flowPosition = vec2(acrossMetres, flow.y - travel);
10
+ footprint = max(footprint, max(length(dFdx(flowPosition)), length(dFdy(flowPosition))));
11
11
  vec2 broad = vec2(0.78, 0.24), middle = vec2(-1.35, 0.43), fine = vec2(2.4, 1.22);
12
12
  vec2 broadWarp = vec2(0.33, -0.22), middleWarp = vec2(0.38, 0.28), fineWarp = vec2(-0.58, 0.63);
13
- float a = dot(q, broadWarp), b = dot(q, middleWarp), c = dot(q, fineWarp);
14
- float broadPhase = dot(q, broad) + 1.55 * sin(a);
15
- float middlePhase = dot(q, middle) + 1.35 * cos(b);
16
- float finePhase = dot(q, fine) + 0.9 * sin(c);
17
- vec2 broadRate = broad + 1.55 * cos(a) * broadWarp;
18
- vec2 middleRate = middle - 1.35 * sin(b) * middleWarp;
19
- vec2 fineRate = fine + 0.9 * cos(c) * fineWarp;
13
+ float broadWarpPhase = dot(flowPosition, broadWarp), middleWarpPhase = dot(flowPosition, middleWarp), fineWarpPhase = dot(flowPosition, fineWarp);
14
+ float broadPhase = dot(flowPosition, broad) + 1.55 * sin(broadWarpPhase);
15
+ float middlePhase = dot(flowPosition, middle) + 1.35 * cos(middleWarpPhase);
16
+ float finePhase = dot(flowPosition, fine) + 0.9 * sin(fineWarpPhase);
17
+ vec2 broadRate = broad + 1.55 * cos(broadWarpPhase) * broadWarp;
18
+ vec2 middleRate = middle - 1.35 * sin(middleWarpPhase) * middleWarp;
19
+ vec2 fineRate = fine + 0.9 * cos(fineWarpPhase) * fineWarp;
20
20
  float broadDetail = streamWaveDetail(footprint, length(broadRate));
21
21
  float middleDetail = streamWaveDetail(footprint, length(middleRate));
22
22
  float fineDetail = streamWaveDetail(footprint, length(fineRate));
23
- float packet = 0.5 + 0.5 * sin(dot(q, vec2(0.23, -0.19)) + 0.9 * sin(b));
23
+ float packet = 0.5 + 0.5 * sin(dot(flowPosition, vec2(0.23, -0.19)) + 0.9 * sin(middleWarpPhase));
24
24
  vec2 slope = 0.085 * broadRate * cos(broadPhase) * broadDetail * (0.38 + 0.62 * packet)
25
25
  + 0.048 * middleRate * cos(middlePhase) * middleDetail * (1.0 - 0.55 * packet)
26
26
  + 0.016 * fineRate * cos(finePhase) * fineDetail;
package/rendering/taa.js CHANGED
@@ -24,13 +24,13 @@ const JITTER_POINTS = 8, // the Halton points the lens steps through, then again
24
24
  STALE = 1000; // milliseconds between two frames that start it afresh (a page that was hidden, a long pause)
25
25
 
26
26
  const halton = (i, base) => {
27
- let f = 1,
28
- r = 0;
27
+ let digitWeight = 1,
28
+ sequenceValue = 0;
29
29
  for (; i > 0; i = Math.floor(i / base)) {
30
- f /= base;
31
- r += f * (i % base);
30
+ digitWeight /= base;
31
+ sequenceValue += digitWeight * (i % base);
32
32
  }
33
- return r;
33
+ return sequenceValue;
34
34
  };
35
35
  // The lens's offsets, in pixels from the pixel's centre.
36
36
  const JITTER = Array.from({ length: JITTER_POINTS }, (_, i) => [halton(i + 1, 2) - 0.5, halton(i + 1, 3) - 0.5]);
@@ -64,8 +64,8 @@ export function createTemporalAA(camera) {
64
64
  for (const t of taa.history) t.dispose();
65
65
  taa.history = [];
66
66
  }
67
- function sizeHistory(w, h) {
68
- for (const t of taa.history) t.setSize(w, h);
67
+ function sizeHistory(width, height) {
68
+ for (const t of taa.history) t.setSize(width, height);
69
69
  taa.fresh = true;
70
70
  }
71
71
  // The running picture starts afresh on the next frame: a cut, a crossing, a new size.
@@ -80,34 +80,34 @@ export function createTemporalAA(camera) {
80
80
  }
81
81
  const historyNow = () => taa.history[taa.flip];
82
82
 
83
- // Moves the lens by this frame's offset for a picture w x h pixels, after noting how the lens moved since the last frame.
84
- function jitterLens(w, h) {
83
+ // Moves the lens by this frame's offset for a viewport of the given width and height in pixels, after noting how the lens moved since the last frame.
84
+ function jitterLens(width, height) {
85
85
  const now = performance.now(),
86
- p = camera.projectionMatrix,
87
- e = p.elements;
86
+ projectionMatrix = camera.projectionMatrix,
87
+ elements = projectionMatrix.elements;
88
88
  if (now - taa.last > STALE || camera.position.distanceToSquared(lastLens) > JUMP * JUMP) taa.fresh = true;
89
89
  taa.last = now;
90
90
  lastLens.copy(camera.position);
91
- viewProj.multiplyMatrices(p, camera.matrixWorldInverse);
91
+ viewProj.multiplyMatrices(projectionMatrix, camera.matrixWorldInverse);
92
92
  taaUniforms.uReproj.value.copy(viewProj).invert().premultiply(lastViewProj);
93
93
  lastViewProj.copy(viewProj);
94
94
  taaUniforms.uFresh.value = taa.fresh ? 1 : 0;
95
95
  taa.fresh = false;
96
96
  const point = JITTER[taa.index++ % JITTER_POINTS], // read by index: no iterator made in a frame
97
- dx = (2 * point[0]) / w,
98
- dy = (2 * point[1]) / h;
97
+ dx = (2 * point[0]) / width,
98
+ dy = (2 * point[1]) / height;
99
99
  taaUniforms.uJit.value.set(dx, dy);
100
- taa.saved[0] = e[8];
101
- taa.saved[1] = e[9];
102
- e[8] -= dx; // a point's clip x gains -dx times its view depth, which is dx once divided by w = -depth
103
- e[9] -= dy;
104
- camera.projectionMatrixInverse.copy(p).invert();
100
+ taa.saved[0] = elements[8];
101
+ taa.saved[1] = elements[9];
102
+ elements[8] -= dx; // a point's clip x gains -dx times its view depth, which is dx once divided by w = -depth
103
+ elements[9] -= dy;
104
+ camera.projectionMatrixInverse.copy(projectionMatrix).invert();
105
105
  }
106
106
  // Puts the lens back where it stands, for everything drawn after the scene.
107
107
  function steadyLens() {
108
- const e = camera.projectionMatrix.elements;
109
- e[8] = taa.saved[0];
110
- e[9] = taa.saved[1];
108
+ const elements = camera.projectionMatrix.elements;
109
+ elements[8] = taa.saved[0];
110
+ elements[9] = taa.saved[1];
111
111
  camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert();
112
112
  }
113
113
  // The running pictures while they exist, for the reckoning of memory.
@@ -128,14 +128,14 @@ export function createTemporalAA(camera) {
128
128
  }
129
129
 
130
130
  // Exposed light squashed into 0 to 1 and back: the running picture's form.
131
- const SQUASH = `/* glsl */ vec3 squash(vec3 c){ return c / (1.0 + max(c.r, max(c.g, c.b))); }
132
- vec3 unsquash(vec3 c){ return c / max(1.0 - max(c.r, max(c.g, c.b)), 1e-3); }`;
131
+ const SQUASH = `/* glsl */ vec3 squash(vec3 hdrColor){ return hdrColor / (1.0 + max(hdrColor.r, max(hdrColor.g, hdrColor.b))); }
132
+ vec3 unsquash(vec3 compressedColor){ return compressedColor / max(1.0 - max(compressedColor.r, max(compressedColor.g, compressedColor.b)), 1e-3); }`;
133
133
  // The final pass's part (post-shaders.js, under TAA): steady(cur, mean, spread) returns the running picture at this pixel (squashed)
134
134
  // from this frame's exposed light cur and its neighbourhood's mean and spread (standard deviation) in YCoCg. It needs the final
135
135
  // pass's tDepth and uPx.
136
136
  const TAA_FS = `/* glsl */ uniform sampler2D tHistory; uniform mat4 uReproj; uniform vec2 uJit; uniform float uFresh; ${SQUASH}
137
- vec3 toYCoCg(vec3 c){ return vec3(0.25 * c.r + 0.5 * c.g + 0.25 * c.b, 0.5 * c.r - 0.5 * c.b, -0.25 * c.r + 0.5 * c.g - 0.25 * c.b); }
138
- vec3 fromYCoCg(vec3 y){ return vec3(y.x + y.y - y.z, y.x + y.z, y.x - y.y - y.z); }
137
+ vec3 toYCoCg(vec3 rgbColor){ return vec3(0.25 * rgbColor.r + 0.5 * rgbColor.g + 0.25 * rgbColor.b, 0.5 * rgbColor.r - 0.5 * rgbColor.b, -0.25 * rgbColor.r + 0.5 * rgbColor.g - 0.25 * rgbColor.b); }
138
+ vec3 fromYCoCg(vec3 lumaChroma){ return vec3(lumaChroma.x + lumaChroma.y - lumaChroma.z, lumaChroma.x + lumaChroma.z, lumaChroma.x - lumaChroma.y - lumaChroma.z); }
139
139
  // where this pixel's point stood in the last frame's picture: carried by the nearest of five depths round it, so an edge moves
140
140
  // with what stands in front of it
141
141
  vec2 lastUv(vec2 uv){
@@ -3,24 +3,24 @@
3
3
  export const talusGLSL = /* glsl */ `
4
4
  vec2 talusFragment(vec2 metres, vec2 dx, vec2 dy, float spacing, float seed) {
5
5
  mat2 basis = mat2(0.868, -0.497, 0.497, 0.868) / spacing;
6
- vec2 q = basis * metres;
6
+ vec2 gridPosition = basis * metres;
7
7
  vec2 footprint = abs(basis * dx) + abs(basis * dy);
8
8
  float resolved = 1.0 - smoothstep(0.12, 0.45, max(footprint.x, footprint.y));
9
- q.x += 0.5 * mod(floor(q.y), 2.0);
10
- vec2 cell = floor(q), p = fract(q) - 0.5;
11
- vec3 h = fract(vec3(cell.xyx) * vec3(0.1031, 0.11369, 0.13787) + seed);
12
- h += dot(h, h.yzx + 19.19);
13
- h = fract((h.xxy + h.yzz) * h.zyx);
14
- p -= (h.xy - 0.5) * 0.14;
15
- float angle = h.z * 6.28318;
16
- p = mat2(cos(angle), -sin(angle), sin(angle), cos(angle)) * p;
17
- float halfWidth = mix(0.13, 0.32, h.y);
18
- vec2 shape = p / vec2(halfWidth, halfWidth * mix(0.5, 0.9, h.x));
9
+ gridPosition.x += 0.5 * mod(floor(gridPosition.y), 2.0);
10
+ vec2 cell = floor(gridPosition), cellPosition = fract(gridPosition) - 0.5;
11
+ vec3 cellHash = fract(vec3(cell.xyx) * vec3(0.1031, 0.11369, 0.13787) + seed);
12
+ cellHash += dot(cellHash, cellHash.yzx + 19.19);
13
+ cellHash = fract((cellHash.xxy + cellHash.yzz) * cellHash.zyx);
14
+ cellPosition -= (cellHash.xy - 0.5) * 0.14;
15
+ float angle = cellHash.z * 6.28318;
16
+ cellPosition = mat2(cos(angle), -sin(angle), sin(angle), cos(angle)) * cellPosition;
17
+ float halfWidth = mix(0.13, 0.32, cellHash.y);
18
+ vec2 shape = cellPosition / vec2(halfWidth, halfWidth * mix(0.5, 0.9, cellHash.x));
19
19
  // Two chipped corners and a slanted top distinguish a fragment from a round pebble.
20
20
  float edge = max(max(abs(shape.x), abs(shape.y)), max(dot(shape, vec2(0.77, 0.88)), dot(shape, vec2(-0.74, -0.80))));
21
- float aa = min(0.8, max(0.025, max(footprint.x, footprint.y) / halfWidth));
22
- float inside = (1.0 - smoothstep(1.0 - aa, 1.0 + aa, edge)) * resolved;
21
+ float filterWidth = min(0.8, max(0.025, max(footprint.x, footprint.y) / halfWidth));
22
+ float inside = (1.0 - smoothstep(1.0 - filterWidth, 1.0 + filterWidth, edge)) * resolved;
23
23
  float crest = max(0.0, 1.0 - edge);
24
- float facet = mix(-0.32, 0.36, smoothstep(-aa, aa, shape.x * 0.8 + shape.y * 0.2 + h.z - 0.5));
25
- return inside * vec2(facet + (h.x - 0.5) * 0.34, 0.28 + 0.72 * crest);
24
+ float facet = mix(-0.32, 0.36, smoothstep(-filterWidth, filterWidth, shape.x * 0.8 + shape.y * 0.2 + cellHash.z - 0.5));
25
+ return inside * vec2(facet + (cellHash.x - 0.5) * 0.34, 0.28 + 0.72 * crest);
26
26
  }`;
@@ -1,22 +1,22 @@
1
1
  // World-space rock for a heightfield. The horizontal components of the surface normal
2
2
  // weight fixed vertical projections; changing the normal never changes the coordinates.
3
3
  export const terrainRockGLSL = /* glsl */ `
4
- vec3 rockFaceSample(sampler2D grain, vec3 p, float xWeight) {
5
- return mix(texture2D(grain, p.xy).rgb, texture2D(grain, p.zy + vec2(0.37, 0.19)).rgb, xWeight);
4
+ vec3 rockFaceSample(sampler2D grain, vec3 texturePosition, float xWeight) {
5
+ return mix(texture2D(grain, texturePosition.xy).rgb, texture2D(grain, texturePosition.zy + vec2(0.37, 0.19)).rgb, xWeight);
6
6
  }
7
7
  float rockBed(float height, float frequency, float phase, float footprint) {
8
8
  return 0.5 + 0.5 * sin(height * frequency + phase) *
9
9
  (1.0 - smoothstep(0.2, 0.5, footprint * frequency));
10
10
  }
11
- vec4 terrainRock(sampler2D grain, vec3 p, vec3 normal, vec3 darkBed, vec3 lightBed, vec3 iron, vec3 lime) {
11
+ vec4 terrainRock(sampler2D grain, vec3 worldPosition, vec3 normal, vec3 darkBed, vec3 lightBed, vec3 iron, vec3 lime) {
12
12
  vec2 weights = normal.xz * normal.xz;
13
13
  weights *= weights;
14
14
  float across = weights.x / max(weights.x + weights.y, 0.00001);
15
- vec3 broad = rockFaceSample(grain, p * 0.021, across);
16
- vec3 broken = rockFaceSample(grain, p * 0.13, across);
17
- vec3 fine = rockFaceSample(grain, p * 0.68, across);
15
+ vec3 broad = rockFaceSample(grain, worldPosition * 0.021, across);
16
+ vec3 broken = rockFaceSample(grain, worldPosition * 0.13, across);
17
+ vec3 fine = rockFaceSample(grain, worldPosition * 0.68, across);
18
18
  // Warped beds follow the height through corners. Individual grains keep roughly the same size in both axes.
19
- float height = p.y + (texture2D(grain, p.xz * 0.0031).r - 0.5) * 22.0
19
+ float height = worldPosition.y + (texture2D(grain, worldPosition.xz * 0.0031).r - 0.5) * 22.0
20
20
  + (broad.g - 0.5) * 3.5 + (broken.b - 0.5) * 1.5;
21
21
  float footprint = fwidth(height) * 0.15915;
22
22
  float major = rockBed(height, 0.83, 0.0, footprint), minor = rockBed(height, 2.57, 1.3, footprint);