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
@@ -5,20 +5,20 @@ const SCATTER = 'fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056,
5
5
  // when it is let go. One copy to a material and share (a land's own stone has its own); all share one program.
6
6
  const fadings = new WeakMap();
7
7
  export function fadingOf(base, share) {
8
- let of = fadings.get(base);
9
- if (!of) fadings.set(base, (of = new Map()));
10
- let m = of.get(share);
11
- if (m) return m;
12
- of.set(share, (m = base.clone()));
13
- m.onBeforeCompile = (sh, renderer) => {
14
- base.onBeforeCompile(sh, renderer);
15
- sh.uniforms.uShare = share;
16
- sh.fragmentShader = sh.fragmentShader
8
+ let variants = fadings.get(base);
9
+ if (!variants) fadings.set(base, (variants = new Map()));
10
+ let variant = variants.get(share);
11
+ if (variant) return variant;
12
+ variants.set(share, (variant = base.clone()));
13
+ variant.onBeforeCompile = (shader, renderer) => {
14
+ base.onBeforeCompile(shader, renderer);
15
+ shader.uniforms.uShare = share;
16
+ shader.fragmentShader = shader.fragmentShader
17
17
  .replace('#include <common>', '#include <common>\nuniform float uShare;')
18
18
  .replace('#include <clipping_planes_fragment>', `#include <clipping_planes_fragment>\nif (${SCATTER} > uShare) discard;`);
19
19
  };
20
- m.customProgramCacheKey = () => base.customProgramCacheKey() + ':dissolving';
21
- return m;
20
+ variant.customProgramCacheKey = () => base.customProgramCacheKey() + ':dissolving';
21
+ return variant;
22
22
  }
23
23
  // A copy of a material that draws only what lies nearer the eye than `reach.value` metres or, with `beyond`, only what lies
24
24
  // further: a thing's near version and its far one hand over pixel by pixel across a band `band` metres deep about that distance,
@@ -27,25 +27,25 @@ export function fadingOf(base, share) {
27
27
  // and band; the two sides share one program each.
28
28
  const reaching = new WeakMap();
29
29
  export function reachOf(base, reach, beyond = false, band = 1) {
30
- let of = reaching.get(base);
31
- if (!of) reaching.set(base, (of = new Map()));
30
+ let variants = reaching.get(base);
31
+ if (!variants) reaching.set(base, (variants = new Map()));
32
32
  const key = `${beyond}:${band}`;
33
- let sides = of.get(reach);
34
- if (!sides) of.set(reach, (sides = {}));
33
+ let sides = variants.get(reach);
34
+ if (!sides) variants.set(reach, (sides = {}));
35
35
  if (sides[key]) return sides[key];
36
- const m = (sides[key] = base.clone());
37
- m.onBeforeCompile = (sh, renderer) => {
38
- base.onBeforeCompile(sh, renderer);
39
- sh.uniforms.uReach = reach;
40
- sh.fragmentShader = sh.fragmentShader
36
+ const variant = (sides[key] = base.clone());
37
+ variant.onBeforeCompile = (shader, renderer) => {
38
+ base.onBeforeCompile(shader, renderer);
39
+ shader.uniforms.uReach = reach;
40
+ shader.fragmentShader = shader.fragmentShader
41
41
  .replace('#include <common>', '#include <common>\nuniform float uReach;')
42
42
  .replace(
43
43
  '#include <clipping_planes_fragment>',
44
44
  `#include <clipping_planes_fragment>\n${handedOver(beyond, nearShare('length(vViewPosition)', band))}`,
45
45
  );
46
46
  };
47
- m.customProgramCacheKey = () => base.customProgramCacheKey() + (beyond ? ':beyond-reach:' : ':within-reach:') + band;
48
- return m;
47
+ variant.customProgramCacheKey = () => base.customProgramCacheKey() + (beyond ? ':beyond-reach:' : ':within-reach:') + band;
48
+ return variant;
49
49
  }
50
50
  // The same for the instances of an InstancedMesh, each handed over whole and in its own time, using its geometry's `aNear` (an instanced attribute, 0 to 1) is the near version's share of the instance's
51
51
  // pixels, which its builder moves from the one end to the other over a moment once the viewer comes within or goes beyond its
@@ -54,25 +54,25 @@ export function reachOf(base, reach, beyond = false, band = 1) {
54
54
  // shadows, of one shape in two levels of detail, lie over each other.
55
55
  const handing = new WeakMap();
56
56
  export function handOverOf(base, beyond = false) {
57
- let of = handing.get(base);
58
- if (!of) handing.set(base, (of = {}));
59
- if (of[beyond]) return of[beyond];
60
- const m = (of[beyond] = base.clone());
61
- m.onBeforeCompile = (sh, renderer) => {
62
- base.onBeforeCompile(sh, renderer);
63
- handOverVertices(sh, beyond);
64
- sh.fragmentShader = sh.fragmentShader
57
+ let variants = handing.get(base);
58
+ if (!variants) handing.set(base, (variants = {}));
59
+ if (variants[beyond]) return variants[beyond];
60
+ const variant = (variants[beyond] = base.clone());
61
+ variant.onBeforeCompile = (shader, renderer) => {
62
+ base.onBeforeCompile(shader, renderer);
63
+ handOverVertices(shader, beyond);
64
+ shader.fragmentShader = shader.fragmentShader
65
65
  .replace('#include <common>', '#include <common>\nvarying float vNearShare;')
66
66
  .replace('#include <clipping_planes_fragment>', `#include <clipping_planes_fragment>\n${handedOver(beyond, 'vNearShare')}`);
67
67
  };
68
- m.customProgramCacheKey = () => base.customProgramCacheKey() + (beyond ? ':handed-far' : ':handed-near');
69
- return m;
68
+ variant.customProgramCacheKey = () => base.customProgramCacheKey() + (beyond ? ':handed-far' : ':handed-near');
69
+ return variant;
70
70
  }
71
71
  export function handOverShadow(beyond = false) {
72
- const m = new THREE.MeshDepthMaterial({ depthPacking: THREE.RGBADepthPacking });
73
- m.onBeforeCompile = sh => handOverVertices(sh, beyond);
74
- m.customProgramCacheKey = () => (beyond ? 'handed-far-depth' : 'handed-near-depth');
75
- return m;
72
+ const variant = new THREE.MeshDepthMaterial({ depthPacking: THREE.RGBADepthPacking });
73
+ variant.onBeforeCompile = shader => handOverVertices(shader, beyond);
74
+ variant.customProgramCacheKey = () => (beyond ? 'handed-far-depth' : 'handed-near-depth');
75
+ return variant;
76
76
  }
77
77
  // The near version's share, 1 short of the band about uReach and 0 past it, at a distance (a GLSL expression) from the eye
78
78
  const nearShare = (distance, band) => `1.0 - smoothstep(uReach - ${(band / 2).toFixed(2)}, uReach + ${(band / 2).toFixed(2)}, ${distance})`;
@@ -80,8 +80,8 @@ const nearShare = (distance, band) => `1.0 - smoothstep(uReach - ${(band / 2).to
80
80
  const handedOver = (beyond, share) => `if (${SCATTER} ${beyond ? '<' : '>='} ${share}) discard;`;
81
81
  // Hands an instance's share (aNear) to the fragments (vNearShare), and puts the vertices of an instance its version does not draw
82
82
  // at all out of sight.
83
- function handOverVertices(sh, beyond) {
84
- sh.vertexShader = sh.vertexShader
83
+ function handOverVertices(shader, beyond) {
84
+ shader.vertexShader = shader.vertexShader
85
85
  .replace('#include <common>', '#include <common>\nattribute float aNear;\nvarying float vNearShare;')
86
86
  .replace(
87
87
  '#include <project_vertex>',
@@ -94,17 +94,17 @@ function handOverVertices(sh, beyond) {
94
94
  // one kind to the other, a few times every frame.
95
95
  const twins = new WeakMap();
96
96
  export function twinMaterial(base, kind) {
97
- let of = twins.get(base);
98
- if (!of) twins.set(base, (of = {}));
99
- if (!of[kind]) {
100
- const m = (of[kind] = base.clone());
101
- m.onBeforeCompile = (sh, renderer) => base.onBeforeCompile(sh, renderer);
102
- m.customProgramCacheKey = () => base.customProgramCacheKey();
97
+ let variants = twins.get(base);
98
+ if (!variants) twins.set(base, (variants = {}));
99
+ if (!variants[kind]) {
100
+ const variant = (variants[kind] = base.clone());
101
+ variant.onBeforeCompile = (shader, renderer) => base.onBeforeCompile(shader, renderer);
102
+ variant.customProgramCacheKey = () => base.customProgramCacheKey();
103
103
  }
104
- return of[kind];
104
+ return variants[kind];
105
105
  }
106
- export function glowMesh(geo, hex, intensity, parent) {
107
- const m = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ color: new THREE.Color(hex).multiplyScalar(intensity) }));
108
- if (parent) parent.add(m);
109
- return m;
106
+ export function glowMesh(geometry, hex, intensity, parent) {
107
+ const mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: new THREE.Color(hex).multiplyScalar(intensity) }));
108
+ if (parent) parent.add(mesh);
109
+ return mesh;
110
110
  }
@@ -16,33 +16,33 @@ import { TAU, clamp } from '../math/scalar.js';
16
16
  * not a burning building's.
17
17
  */
18
18
  export function createParticleEmitters({ spawn, put, random = Math.random, hearth = 4 }) {
19
- const rnd = random;
19
+ const randomValue = random;
20
20
 
21
21
  /**
22
22
  * One wisp of mist lying low over a disc of radius `site.r` about (site.x, site.z): broad pale veils, thickest at night (`day`
23
23
  * 0 at night, 1 by day), drifting slowly. `groundAt(x, z)` is the ground's height there. site.size: the wisp's size.
24
24
  */
25
25
  function mist(site, groundAt, day, pool) {
26
- const a = rnd() * TAU,
27
- d = Math.sqrt(rnd()) * site.r,
28
- x = site.x + Math.cos(a) * d,
29
- z = site.z + Math.sin(a) * d,
30
- g = 0.62 + rnd() * 0.12,
26
+ const angle = randomValue() * TAU,
27
+ radialDistance = Math.sqrt(randomValue()) * site.r,
28
+ x = site.x + Math.cos(angle) * radialDistance,
29
+ z = site.z + Math.sin(angle) * radialDistance,
30
+ brightness = 0.62 + randomValue() * 0.12,
31
31
  thick = 0.45 + 0.55 * (1 - day);
32
32
  spawn.x = x;
33
- spawn.y = groundAt(x, z) + 0.25 + rnd() * 0.6;
33
+ spawn.y = groundAt(x, z) + 0.25 + randomValue() * 0.6;
34
34
  spawn.z = z;
35
- spawn.vx = (rnd() - 0.5) * 0.25;
36
- spawn.vy = 0.02 + rnd() * 0.04;
37
- spawn.vz = (rnd() - 0.5) * 0.25;
38
- spawn.life = 12 + rnd() * 8;
39
- spawn.size = site.size * (0.7 + rnd() * 0.6);
40
- spawn.r = g;
41
- spawn.g = g * 1.02;
42
- spawn.b = g * 1.06;
35
+ spawn.vx = (randomValue() - 0.5) * 0.25;
36
+ spawn.vy = 0.02 + randomValue() * 0.04;
37
+ spawn.vz = (randomValue() - 0.5) * 0.25;
38
+ spawn.life = 12 + randomValue() * 8;
39
+ spawn.size = site.size * (0.7 + randomValue() * 0.6);
40
+ spawn.r = brightness;
41
+ spawn.g = brightness * 1.02;
42
+ spawn.b = brightness * 1.06;
43
43
  spawn.drag = 0.02;
44
44
  spawn.grav = 0;
45
- spawn.e1 = (0.1 + rnd() * 0.08) * thick;
45
+ spawn.e1 = (0.1 + randomValue() * 0.08) * thick;
46
46
  spawn.e2 = 0;
47
47
  spawn.e3 = 0.12;
48
48
  put(pool);
@@ -56,46 +56,46 @@ export function createParticleEmitters({ spawn, put, random = Math.random, heart
56
56
  function smoke(site, pool) {
57
57
  const S = site.size,
58
58
  small = 1 - clamp(S / hearth, 0, 1),
59
- g = (0.1 + rnd() * 0.17) * (site.dark ?? 1) * (1 + 1.8 * small),
59
+ brightness = (0.1 + randomValue() * 0.17) * (site.dark ?? 1) * (1 + 1.8 * small),
60
60
  brief = 1 - 0.6 * small,
61
61
  j = S * 0.16,
62
62
  heat = site.heat ?? 0.08,
63
63
  up = Math.sqrt(S / 8),
64
64
  thin = site.thin ?? 1;
65
- if (rnd() < 0.24) {
66
- spawn.x = site.x + (rnd() - 0.5) * j;
65
+ if (randomValue() < 0.24) {
66
+ spawn.x = site.x + (randomValue() - 0.5) * j;
67
67
  spawn.y = site.y + S * 0.2;
68
- spawn.z = site.z + (rnd() - 0.5) * j;
69
- spawn.vx = (rnd() - 0.5) * 0.6;
70
- spawn.vy = (2.3 + rnd() * 1.5) * up;
71
- spawn.vz = (rnd() - 0.5) * 0.6;
72
- spawn.life = (14 + rnd() * 9) * brief;
73
- spawn.size = S * (0.4 + rnd() * 0.3);
74
- spawn.r = g * 1.3;
75
- spawn.g = g * 1.3;
76
- spawn.b = g * 1.36;
68
+ spawn.z = site.z + (randomValue() - 0.5) * j;
69
+ spawn.vx = (randomValue() - 0.5) * 0.6;
70
+ spawn.vy = (2.3 + randomValue() * 1.5) * up;
71
+ spawn.vz = (randomValue() - 0.5) * 0.6;
72
+ spawn.life = (14 + randomValue() * 9) * brief;
73
+ spawn.size = S * (0.4 + randomValue() * 0.3);
74
+ spawn.r = brightness * 1.3;
75
+ spawn.g = brightness * 1.3;
76
+ spawn.b = brightness * 1.36;
77
77
  spawn.drag = 0.1;
78
78
  spawn.grav = 0.1;
79
- spawn.e1 = (0.34 + rnd() * 0.2) * thin * brief;
79
+ spawn.e1 = (0.34 + randomValue() * 0.2) * thin * brief;
80
80
  spawn.e2 = heat * 0.5;
81
- spawn.e3 = 1.1 + rnd() * 0.4;
81
+ spawn.e3 = 1.1 + randomValue() * 0.4;
82
82
  } else {
83
- spawn.x = site.x + (rnd() - 0.5) * j;
83
+ spawn.x = site.x + (randomValue() - 0.5) * j;
84
84
  spawn.y = site.y;
85
- spawn.z = site.z + (rnd() - 0.5) * j;
86
- spawn.vx = (rnd() - 0.5) * 0.9;
87
- spawn.vy = (2.0 + rnd() * 1.7) * up;
88
- spawn.vz = (rnd() - 0.5) * 0.9;
89
- spawn.life = 5.5 + rnd() * 4.5;
90
- spawn.size = S * (0.22 + rnd() * 0.2);
91
- spawn.r = g;
92
- spawn.g = g;
93
- spawn.b = g * 1.05;
85
+ spawn.z = site.z + (randomValue() - 0.5) * j;
86
+ spawn.vx = (randomValue() - 0.5) * 0.9;
87
+ spawn.vy = (2.0 + randomValue() * 1.7) * up;
88
+ spawn.vz = (randomValue() - 0.5) * 0.9;
89
+ spawn.life = 5.5 + randomValue() * 4.5;
90
+ spawn.size = S * (0.22 + randomValue() * 0.2);
91
+ spawn.r = brightness;
92
+ spawn.g = brightness;
93
+ spawn.b = brightness * 1.05;
94
94
  spawn.drag = 0.12;
95
95
  spawn.grav = 0.12;
96
- spawn.e1 = (0.5 + rnd() * 0.3) * thin;
96
+ spawn.e1 = (0.5 + randomValue() * 0.3) * thin;
97
97
  spawn.e2 = heat;
98
- spawn.e3 = 0.9 + rnd() * 0.6;
98
+ spawn.e3 = 0.9 + randomValue() * 0.6;
99
99
  }
100
100
  put(pool);
101
101
  }
@@ -109,16 +109,16 @@ export function createParticleEmitters({ spawn, put, random = Math.random, heart
109
109
  const z = site.size,
110
110
  sq = Math.sqrt(z),
111
111
  hot = site.hot ?? 1;
112
- spawn.x = site.x + (rnd() - 0.5) * z * 0.9;
113
- spawn.y = site.y + rnd() * 0.12 * z;
114
- spawn.z = site.z + (rnd() - 0.5) * z * 0.9;
115
- spawn.vx = (rnd() - 0.5) * 0.35;
116
- spawn.vy = (0.5 + rnd() * 0.9) * sq;
117
- spawn.vz = (rnd() - 0.5) * 0.35;
118
- spawn.life = 0.45 + rnd() * 0.5;
119
- spawn.size = z * (0.6 + rnd() * 0.6);
112
+ spawn.x = site.x + (randomValue() - 0.5) * z * 0.9;
113
+ spawn.y = site.y + randomValue() * 0.12 * z;
114
+ spawn.z = site.z + (randomValue() - 0.5) * z * 0.9;
115
+ spawn.vx = (randomValue() - 0.5) * 0.35;
116
+ spawn.vy = (0.5 + randomValue() * 0.9) * sq;
117
+ spawn.vz = (randomValue() - 0.5) * 0.35;
118
+ spawn.life = 0.45 + randomValue() * 0.5;
119
+ spawn.size = z * (0.6 + randomValue() * 0.6);
120
120
  spawn.r = 1.3 * hot;
121
- spawn.g = (0.42 + rnd() * 0.2) * hot;
121
+ spawn.g = (0.42 + randomValue() * 0.2) * hot;
122
122
  spawn.b = 0.065 * hot;
123
123
  spawn.drag = 0.6;
124
124
  spawn.grav = 0.8;
@@ -126,15 +126,15 @@ export function createParticleEmitters({ spawn, put, random = Math.random, heart
126
126
  spawn.e2 = 0;
127
127
  spawn.e3 = 0.12;
128
128
  put(add);
129
- if (rnd() < (site.sparks ?? 0.32)) {
130
- spawn.x = site.x + (rnd() - 0.5) * z * 0.6;
129
+ if (randomValue() < (site.sparks ?? 0.32)) {
130
+ spawn.x = site.x + (randomValue() - 0.5) * z * 0.6;
131
131
  spawn.y = site.y + 0.5 * z;
132
- spawn.z = site.z + (rnd() - 0.5) * z * 0.6;
133
- spawn.vx = (rnd() - 0.5) * 1.6 * sq;
134
- spawn.vy = (1.8 + rnd() * 2.6) * sq;
135
- spawn.vz = (rnd() - 0.5) * 1.6 * sq;
136
- spawn.life = 1.4 + rnd() * 2.6;
137
- spawn.size = (0.035 + rnd() * 0.04) * sq;
132
+ spawn.z = site.z + (randomValue() - 0.5) * z * 0.6;
133
+ spawn.vx = (randomValue() - 0.5) * 1.6 * sq;
134
+ spawn.vy = (1.8 + randomValue() * 2.6) * sq;
135
+ spawn.vz = (randomValue() - 0.5) * 1.6 * sq;
136
+ spawn.life = 1.4 + randomValue() * 2.6;
137
+ spawn.size = (0.035 + randomValue() * 0.04) * sq;
138
138
  spawn.r = 4.6;
139
139
  spawn.g = 1.7;
140
140
  spawn.b = 0.36;
@@ -145,14 +145,14 @@ export function createParticleEmitters({ spawn, put, random = Math.random, heart
145
145
  spawn.e3 = 0.7;
146
146
  put(add);
147
147
  }
148
- if (rnd() < (site.halo ?? 0.1)) {
148
+ if (randomValue() < (site.halo ?? 0.1)) {
149
149
  spawn.x = site.x;
150
150
  spawn.y = site.y + z * 1.7;
151
151
  spawn.z = site.z;
152
152
  spawn.vx = 0;
153
153
  spawn.vy = 0.4;
154
154
  spawn.vz = 0;
155
- spawn.life = 0.5 + rnd() * 0.5;
155
+ spawn.life = 0.5 + randomValue() * 0.5;
156
156
  spawn.size = z * 3.2;
157
157
  spawn.r = 0.3 * hot;
158
158
  spawn.g = 0.09 * hot;
@@ -161,41 +161,41 @@ export function createParticleEmitters({ spawn, put, random = Math.random, heart
161
161
  spawn.grav = 0;
162
162
  put(add);
163
163
  }
164
- if (rnd() < (site.soot ?? 0.14)) {
164
+ if (randomValue() < (site.soot ?? 0.14)) {
165
165
  if (site.wood) {
166
- const g = 0.36 + rnd() * 0.16;
167
- spawn.x = site.x + (rnd() - 0.5) * z * 0.4;
166
+ const brightness = 0.36 + randomValue() * 0.16;
167
+ spawn.x = site.x + (randomValue() - 0.5) * z * 0.4;
168
168
  spawn.y = site.y + z * 1.3;
169
- spawn.z = site.z + (rnd() - 0.5) * z * 0.4;
170
- spawn.vx = (rnd() - 0.5) * 0.3;
171
- spawn.vy = (1.3 + rnd() * 0.9) * sq;
172
- spawn.vz = (rnd() - 0.5) * 0.3;
173
- spawn.life = (6 + rnd() * 5) * Math.sqrt(sq);
174
- spawn.size = z * (0.9 + rnd() * 0.8);
175
- spawn.r = g;
176
- spawn.g = g * 1.02;
177
- spawn.b = g * 1.08;
169
+ spawn.z = site.z + (randomValue() - 0.5) * z * 0.4;
170
+ spawn.vx = (randomValue() - 0.5) * 0.3;
171
+ spawn.vy = (1.3 + randomValue() * 0.9) * sq;
172
+ spawn.vz = (randomValue() - 0.5) * 0.3;
173
+ spawn.life = (6 + randomValue() * 5) * Math.sqrt(sq);
174
+ spawn.size = z * (0.9 + randomValue() * 0.8);
175
+ spawn.r = brightness;
176
+ spawn.g = brightness * 1.02;
177
+ spawn.b = brightness * 1.08;
178
178
  spawn.drag = 0.15;
179
179
  spawn.grav = 0.15;
180
- spawn.e1 = 0.13 + rnd() * 0.11;
180
+ spawn.e1 = 0.13 + randomValue() * 0.11;
181
181
  spawn.e2 = 0.35;
182
182
  spawn.e3 = 1.3;
183
183
  } else {
184
- const g = 0.04 + rnd() * 0.04;
185
- spawn.x = site.x + (rnd() - 0.5) * z * 0.5;
184
+ const brightness = 0.04 + randomValue() * 0.04;
185
+ spawn.x = site.x + (randomValue() - 0.5) * z * 0.5;
186
186
  spawn.y = site.y + z * 1.5;
187
- spawn.z = site.z + (rnd() - 0.5) * z * 0.5;
188
- spawn.vx = (rnd() - 0.5) * 0.4;
189
- spawn.vy = (1.6 + rnd() * 1.2) * sq;
190
- spawn.vz = (rnd() - 0.5) * 0.4;
191
- spawn.life = (4 + rnd() * 3.5) * Math.sqrt(sq);
192
- spawn.size = z * (0.8 + rnd() * 0.7);
193
- spawn.r = g;
194
- spawn.g = g * 0.95;
195
- spawn.b = g * 0.9;
187
+ spawn.z = site.z + (randomValue() - 0.5) * z * 0.5;
188
+ spawn.vx = (randomValue() - 0.5) * 0.4;
189
+ spawn.vy = (1.6 + randomValue() * 1.2) * sq;
190
+ spawn.vz = (randomValue() - 0.5) * 0.4;
191
+ spawn.life = (4 + randomValue() * 3.5) * Math.sqrt(sq);
192
+ spawn.size = z * (0.8 + randomValue() * 0.7);
193
+ spawn.r = brightness;
194
+ spawn.g = brightness * 0.95;
195
+ spawn.b = brightness * 0.9;
196
196
  spawn.drag = 0.2;
197
197
  spawn.grav = 0.2;
198
- spawn.e1 = 0.42 + rnd() * 0.28;
198
+ spawn.e1 = 0.42 + randomValue() * 0.28;
199
199
  spawn.e2 = 0.6;
200
200
  spawn.e3 = 1.2;
201
201
  }
@@ -213,11 +213,11 @@ export function createParticleEmitters({ spawn, put, random = Math.random, heart
213
213
  * strength.
214
214
  */
215
215
  export function shakeOffset(out, strength, left, duration, reach = 0.18) {
216
- const a = strength * reach * Math.max(0, left / duration) ** 1.5,
216
+ const amplitude = strength * reach * Math.max(0, left / duration) ** 1.5,
217
217
  u = left;
218
218
  return out.set(
219
- a * (Math.sin(u * 13.1) * 0.6 + Math.sin(u * 19.7 + 1.3) * 0.4),
220
- a * (Math.sin(u * 11.3 + 2.1) * 0.6 + Math.sin(u * 21.9) * 0.4) * 0.7,
221
- a * (Math.sin(u * 15.5 + 0.7) * 0.6 + Math.sin(u * 9.7 + 2.9) * 0.4),
219
+ amplitude * (Math.sin(u * 13.1) * 0.6 + Math.sin(u * 19.7 + 1.3) * 0.4),
220
+ amplitude * (Math.sin(u * 11.3 + 2.1) * 0.6 + Math.sin(u * 21.9) * 0.4) * 0.7,
221
+ amplitude * (Math.sin(u * 15.5 + 0.7) * 0.6 + Math.sin(u * 9.7 + 2.9) * 0.4),
222
222
  );
223
223
  }
@@ -65,35 +65,35 @@ export function createParticleSystem({ scene, pointScale, sceneDepth, elapsed, n
65
65
  // Each particle is one camera-facing quad (instanced), moved entirely in the vertex shader from its spawn data.
66
66
  // The alpha pool draws smoke, dust and shadow as lit, billowing puffs; the additive pool draws glows (kind 0),
67
67
  // flame tongues that stand upright and lick upward (kind 1) and embers (kind 2).
68
- function makeParticles(cap, additive) {
69
- if (!Number.isInteger(cap) || cap < 1) throw new RangeError('Particle capacity must be a positive integer');
68
+ function makeParticles(capacity, additive) {
69
+ if (!Number.isInteger(capacity) || capacity < 1) throw new RangeError('Particle capacity must be a positive integer');
70
70
  if (!fxU.uNoise.value) fxU.uNoise.value = noiseTexture();
71
- const g = new THREE.InstancedBufferGeometry(),
72
- q = new THREE.PlaneGeometry(1, 1);
73
- g.setIndex(q.index);
74
- g.setAttribute('position', q.attributes.position);
75
- const mk = n => {
76
- const a = new THREE.InstancedBufferAttribute(new Float32Array(cap * n), n);
77
- a.setUsage(THREE.DynamicDrawUsage);
78
- return a;
71
+ const particleGeometry = new THREE.InstancedBufferGeometry(),
72
+ quadGeometry = new THREE.PlaneGeometry(1, 1);
73
+ particleGeometry.setIndex(quadGeometry.index);
74
+ particleGeometry.setAttribute('position', quadGeometry.attributes.position);
75
+ const mk = itemSize => {
76
+ const attribute = new THREE.InstancedBufferAttribute(new Float32Array(capacity * itemSize), itemSize);
77
+ attribute.setUsage(THREE.DynamicDrawUsage);
78
+ return attribute;
79
79
  };
80
80
  const pos = mk(3),
81
81
  vel = mk(3),
82
82
  dat = mk(4),
83
83
  col = mk(4),
84
84
  sed = mk(4);
85
- for (let i = 0; i < cap; i++) {
85
+ for (let i = 0; i < capacity; i++) {
86
86
  dat.array[i * 4] = -1e5;
87
87
  dat.array[i * 4 + 1] = 1;
88
88
  }
89
- g.setAttribute('aPos', pos);
90
- g.setAttribute('aVel', vel);
91
- g.setAttribute('aData', dat);
92
- g.setAttribute('aCol', col);
93
- g.setAttribute('aSeed', sed);
94
- g.instanceCount = cap;
95
- g.boundingSphere = new THREE.Sphere(new THREE.Vector3(), 1e7);
96
- const mat = new THREE.ShaderMaterial({
89
+ particleGeometry.setAttribute('aPos', pos);
90
+ particleGeometry.setAttribute('aVel', vel);
91
+ particleGeometry.setAttribute('aData', dat);
92
+ particleGeometry.setAttribute('aCol', col);
93
+ particleGeometry.setAttribute('aSeed', sed);
94
+ particleGeometry.instanceCount = 0;
95
+ particleGeometry.boundingSphere = new THREE.Sphere(new THREE.Vector3(), 1e7);
96
+ const material = new THREE.ShaderMaterial({
97
97
  transparent: true,
98
98
  depthWrite: false,
99
99
  blending: additive ? THREE.AdditiveBlending : THREE.NormalBlending,
@@ -213,15 +213,17 @@ export function createParticleSystem({ scene, pointScale, sceneDepth, elapsed, n
213
213
  #include <colorspace_fragment>
214
214
  }`,
215
215
  });
216
- const pts = new THREE.Mesh(g, mat);
217
- pts.frustumCulled = false;
218
- pts.renderOrder = additive ? additiveOrder : alphaOrder;
219
- pts.layers.set(layer);
220
- scene.add(pts);
216
+ const mesh = new THREE.Mesh(particleGeometry, material);
217
+ mesh.frustumCulled = false;
218
+ mesh.renderOrder = additive ? additiveOrder : alphaOrder;
219
+ mesh.layers.set(layer);
220
+ scene.add(mesh);
221
221
  return {
222
- cap,
222
+ cap: capacity,
223
223
  add: additive,
224
224
  head: 0,
225
+ cycleEnd: -Infinity,
226
+ previousCycleEnd: -Infinity,
225
227
  pos: pos.array,
226
228
  vel: vel.array,
227
229
  dat: dat.array,
@@ -233,7 +235,7 @@ export function createParticleSystem({ scene, pointScale, sceneDepth, elapsed, n
233
235
  dirty: false,
234
236
  wrapped: false,
235
237
  until: 0, // when the last particle sent out dies: until then the pool is drawn
236
- pts,
238
+ pts: mesh,
237
239
  };
238
240
  }
239
241
  // The particle pSpawn sends out next, set field by field by its caller: pSpawn is too long for the engine to take into its callers,
@@ -244,53 +246,62 @@ export function createParticleSystem({ scene, pointScale, sceneDepth, elapsed, n
244
246
  // hard the wind pushes it.
245
247
  const spawn = { x: 0, y: 0, z: 0, vx: 0, vy: 0, vz: 0, life: 0, size: 0, r: 0, g: 0, b: 0, drag: 0, grav: 0, e1: NaN, e2: 0, e3: 0 };
246
248
  // Sends out the particle set in `spawn` from pool s, and puts its e1 (NaN: the pool's own, 0 additive, 0.5 alpha), e2 and e3 back.
247
- function pSpawn(s) {
248
- const p = spawn,
249
- i = s.head,
249
+ function pSpawn(system) {
250
+ const particle = spawn,
251
+ i = system.head,
250
252
  i3 = i * 3,
251
253
  i4 = i * 4;
252
- s.head = i + 1 === s.cap ? 0 : i + 1;
253
- if (s.head === 0) s.wrapped = true;
254
- s.pos[i3] = p.x;
255
- s.pos[i3 + 1] = p.y;
256
- s.pos[i3 + 2] = p.z;
257
- s.vel[i3] = p.vx;
258
- s.vel[i3 + 1] = p.vy;
259
- s.vel[i3 + 2] = p.vz;
254
+ system.head = i + 1 === system.cap ? 0 : i + 1;
255
+ if (system.head === 0) system.wrapped = true;
256
+ system.pos[i3] = particle.x;
257
+ system.pos[i3 + 1] = particle.y;
258
+ system.pos[i3 + 2] = particle.z;
259
+ system.vel[i3] = particle.vx;
260
+ system.vel[i3 + 1] = particle.vy;
261
+ system.vel[i3 + 2] = particle.vz;
260
262
  const time = elapsed();
261
- s.dat[i4] = time;
262
- s.dat[i4 + 1] = p.life;
263
- if (time + p.life > s.until) s.until = time + p.life;
264
- s.dat[i4 + 2] = p.size;
265
- s.dat[i4 + 3] = p.grav;
266
- s.col[i4] = p.r;
267
- s.col[i4 + 1] = p.g;
268
- s.col[i4 + 2] = p.b;
269
- s.col[i4 + 3] = p.drag;
270
- s.sed[i4] = random();
271
- s.sed[i4 + 1] = Number.isNaN(p.e1) ? (s.add ? 0 : 0.5) : p.e1;
272
- s.sed[i4 + 2] = p.e2;
273
- s.sed[i4 + 3] = p.e3;
274
- p.e1 = NaN;
275
- p.e2 = p.e3 = 0;
276
- if (i < s.lo) s.lo = i;
277
- if (i > s.hi) s.hi = i;
278
- s.dirty = true;
263
+ system.dat[i4] = time;
264
+ system.dat[i4 + 1] = particle.life;
265
+ // Use the birth and lifetime the shader receives, including float32 rounding.
266
+ system.cycleEnd = Math.max(system.cycleEnd, system.dat[i4] + system.dat[i4 + 1]);
267
+ if (system.head === 0) {
268
+ system.previousCycleEnd = system.cycleEnd;
269
+ system.cycleEnd = -Infinity;
270
+ }
271
+ if (time + particle.life > system.until) system.until = time + particle.life;
272
+ system.dat[i4 + 2] = particle.size;
273
+ system.dat[i4 + 3] = particle.grav;
274
+ system.col[i4] = particle.r;
275
+ system.col[i4 + 1] = particle.g;
276
+ system.col[i4 + 2] = particle.b;
277
+ system.col[i4 + 3] = particle.drag;
278
+ system.sed[i4] = random();
279
+ system.sed[i4 + 1] = Number.isNaN(particle.e1) ? (system.add ? 0 : 0.5) : particle.e1;
280
+ system.sed[i4 + 2] = particle.e2;
281
+ system.sed[i4 + 3] = particle.e3;
282
+ particle.e1 = NaN;
283
+ particle.e2 = particle.e3 = 0;
284
+ if (i < system.lo) system.lo = i;
285
+ if (i > system.hi) system.hi = i;
286
+ system.dirty = true;
279
287
  }
280
288
  // Uploads the parts of a particle system's buffers that changed this frame, and draws the system only while a particle lives.
281
- function pFlush(s) {
282
- s.pts.visible = elapsed() < s.until;
283
- if (!s.dirty) return;
284
- for (let k = 0; k < s.attrs.length; k++) {
285
- const a = s.attrs[k];
286
- a.clearUpdateRanges();
287
- if (!s.wrapped) a.addUpdateRange(s.lo * a.itemSize, (s.hi - s.lo + 1) * a.itemSize);
288
- a.needsUpdate = true;
289
+ function pFlush(system) {
290
+ system.pts.visible = elapsed() < system.until;
291
+ // All slots at or beyond the write head are from the previous lap of the ring.
292
+ // Keep their original blend order until even its longest-lived particle has expired.
293
+ system.pts.geometry.instanceCount = Math.fround(fxU.uTime.value) <= system.previousCycleEnd ? system.cap : system.head;
294
+ if (!system.dirty) return;
295
+ for (let k = 0; k < system.attrs.length; k++) {
296
+ const attribute = system.attrs[k];
297
+ attribute.clearUpdateRanges();
298
+ if (!system.wrapped) attribute.addUpdateRange(system.lo * attribute.itemSize, (system.hi - system.lo + 1) * attribute.itemSize);
299
+ attribute.needsUpdate = true;
289
300
  }
290
- s.dirty = false;
291
- s.wrapped = false;
292
- s.lo = 1e9;
293
- s.hi = -1;
301
+ system.dirty = false;
302
+ system.wrapped = false;
303
+ system.lo = 1e9;
304
+ system.hi = -1;
294
305
  }
295
306
 
296
307
  return { emberMaterial, fxU, makeParticles, pFlush, pSpawn, spawn };