roamwright 0.1.1 → 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 (111) 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/creased-sheet.js +127 -0
  9. package/geometry/ear.js +97 -0
  10. package/geometry/erosion.js +106 -0
  11. package/geometry/face-grid.js +143 -0
  12. package/geometry/garment-tags.js +11 -0
  13. package/geometry/hair-gathers.js +174 -0
  14. package/geometry/hair-locks.js +156 -0
  15. package/geometry/hair-patch.js +42 -0
  16. package/geometry/hair-shell.js +186 -0
  17. package/geometry/hair-tags.js +6 -0
  18. package/geometry/head-profile.js +138 -0
  19. package/geometry/head-shape.js +499 -0
  20. package/geometry/heightfield.js +68 -0
  21. package/geometry/outline-grid.js +46 -0
  22. package/geometry/path-field.js +65 -0
  23. package/geometry/sagging-tube.js +58 -0
  24. package/geometry/sampled-surface.js +101 -0
  25. package/geometry/scatter-grid.js +32 -0
  26. package/geometry/sculpt.js +263 -0
  27. package/geometry/shore-grid.js +67 -0
  28. package/geometry/strata-field.js +339 -0
  29. package/geometry/strata-tiles.js +215 -0
  30. package/geometry/stream.js +94 -0
  31. package/geometry/terrain-chunks.js +300 -0
  32. package/geometry/terrain-extension.js +44 -0
  33. package/math/cell-edge.js +44 -0
  34. package/math/cloth-textures.js +83 -0
  35. package/math/cloud-textures.js +112 -0
  36. package/math/color.js +29 -0
  37. package/math/mountain-ranges.js +463 -0
  38. package/math/particle-noise.js +48 -0
  39. package/math/periodic.js +20 -0
  40. package/math/rock-texture.js +56 -0
  41. package/math/surface-textures.js +241 -0
  42. package/motion/arm-reach.js +74 -0
  43. package/motion/biped-stride.js +276 -0
  44. package/motion/crowd-alarms.js +127 -0
  45. package/motion/crowd-routine.js +183 -0
  46. package/motion/daily-clock.js +59 -0
  47. package/motion/instance-sight.js +84 -0
  48. package/motion/quadruped.js +332 -0
  49. package/motion/weather.js +59 -0
  50. package/motion/wind.js +27 -0
  51. package/package.json +6 -4
  52. package/rendering/caster-motion.js +79 -0
  53. package/rendering/cell-lod.js +175 -0
  54. package/rendering/cloud-dome.js +371 -0
  55. package/rendering/cloud-field.js +83 -0
  56. package/rendering/compilation-schedule.js +34 -0
  57. package/rendering/contact-shadows.js +79 -0
  58. package/rendering/crowd-skinning.js +133 -0
  59. package/rendering/crust.js +28 -0
  60. package/rendering/daylight.js +50 -0
  61. package/rendering/far-shadow.js +186 -0
  62. package/rendering/flocks.js +323 -0
  63. package/rendering/garment-material.js +110 -0
  64. package/rendering/glow-lines.js +74 -0
  65. package/rendering/glow-shell.js +90 -0
  66. package/rendering/hair-strands.js +67 -0
  67. package/rendering/height-haze.js +129 -0
  68. package/rendering/height-probe.js +91 -0
  69. package/rendering/interior-mapping.js +19 -0
  70. package/rendering/light-budget.js +222 -0
  71. package/rendering/light-points.js +51 -0
  72. package/rendering/light-shafts.js +131 -0
  73. package/rendering/local-lights.js +28 -0
  74. package/rendering/low-light.js +13 -0
  75. package/rendering/material-lod.js +110 -0
  76. package/rendering/particle-emitters.js +223 -0
  77. package/rendering/particles.js +297 -0
  78. package/rendering/planar-reflection.js +175 -0
  79. package/rendering/post-shaders.js +21 -1
  80. package/rendering/post.js +4 -0
  81. package/rendering/rain-rings.js +22 -0
  82. package/rendering/road-surface.js +63 -0
  83. package/rendering/sea-shaders.js +230 -0
  84. package/rendering/shader-warmup.js +197 -0
  85. package/rendering/shadow-alignment.js +28 -0
  86. package/rendering/shadow-depth.js +129 -0
  87. package/rendering/sky-occlusion.js +187 -0
  88. package/rendering/sky.js +169 -0
  89. package/rendering/stage-lights.js +209 -0
  90. package/rendering/static-point-shadow.js +119 -0
  91. package/rendering/strata-material.js +93 -0
  92. package/rendering/stream-flow.js +36 -0
  93. package/rendering/surface-light.js +22 -0
  94. package/rendering/surface-projection.js +69 -0
  95. package/rendering/talus.js +26 -0
  96. package/rendering/terrain-rock.js +48 -0
  97. package/rendering/texture-fills.js +68 -0
  98. package/rendering/viewport.js +9 -0
  99. package/rendering/wakes.js +99 -0
  100. package/rendering/water-material.js +288 -0
  101. package/runtime/device-quality.js +27 -0
  102. package/runtime/diagnostic-log.js +80 -0
  103. package/runtime/events.js +14 -0
  104. package/runtime/first-use.js +3 -1
  105. package/runtime/frame-yield.js +14 -0
  106. package/runtime/grid-cache.js +88 -0
  107. package/runtime/grid-rows.js +9 -0
  108. package/runtime/module-preload.js +15 -0
  109. package/runtime/search.js +69 -0
  110. package/runtime/spatial-audio.js +57 -0
  111. package/scripts/serve.py +26 -0
@@ -0,0 +1,127 @@
1
+ import { sstep } from '../math/scalar.js';
2
+
3
+ /**
4
+ * Alarms that scatter a crowd, and a stage it turns to watch. Everyone within reach of a fresh alarm runs away from it, round
5
+ * whatever stands in the way; once it has been quiet for a while they walk back to where their own routine has them. A person's
6
+ * flight is kept on them as an offset from that routine, so the routine itself never changes.
7
+ *
8
+ * A person is a plain record the crowd keeps: `fx`, `fz` (the flight's offset, metres), `fy` (its heading), `ft` (the clock at
9
+ * their last step, -1 before the first), `lean` (the side they turned to last, tried first next time) and `ph` (their own
10
+ * phase, which staggers when each starts to run). Positions come and go in records filled in place, and no frame allocates.
11
+ *
12
+ * @param {object} options
13
+ * @param {(x: number, z: number, want: number, step: number, radius: number, lean?: number) => number|undefined} options.steer
14
+ * A heading clear of obstacles for a step from (x, z), or undefined when hemmed in (`createSpaces().steer`).
15
+ * @param {number} [options.run=4.2] Metres a second of a person running from an alarm.
16
+ * @param {number} [options.walkBack=0.9] Metres a second of a person walking back to their routine.
17
+ * @param {number} [options.life=7] Seconds an alarm lasts after it was last raised.
18
+ * @param {number} [options.body=0.3] Metres: a runner's radius against obstacles.
19
+ * @param {number} [options.merge=3] Metres within which a new alarm renews an old one instead of adding another.
20
+ * @param {number} [options.reach=11] Metres an alarm carries unless it says.
21
+ * @param {number} [options.watchWithin=18] Metres from a stage within which an idle person turns to it,
22
+ * @param {number} [options.watchBeyond=2.5] unless they stand in the middle of it.
23
+ */
24
+ export function createCrowdAlarms({
25
+ steer,
26
+ run = 4.2,
27
+ walkBack = 0.9,
28
+ life = 7,
29
+ body = 0.3,
30
+ merge = 3,
31
+ reach = 11,
32
+ watchWithin = 18,
33
+ watchBeyond = 2.5,
34
+ } = {}) {
35
+ const alarms = []; // { x, z, t, r }: where, when last raised, and how far it carries
36
+ const stage = { on: false, x: 0, z: 0 };
37
+
38
+ // Raises an alarm at (x, z) at clock t, or renews the one already there.
39
+ function raise(x, z, t, r = reach) {
40
+ const near = alarms.find(a => Math.hypot(a.x - x, a.z - z) < merge);
41
+ if (near) {
42
+ near.t = t;
43
+ near.r = Math.max(near.r, r);
44
+ } else alarms.push({ x, z, t, r });
45
+ }
46
+ // Drops the alarms that have been quiet for `life` seconds at clock t (or were raised after it: the clock was turned back).
47
+ function expire(t) {
48
+ for (let i = alarms.length - 1; i >= 0; i--) if (t - alarms[i].t > life || t < alarms[i].t) alarms.splice(i, 1);
49
+ }
50
+ // A stage at (x, z) that idle people near it turn to watch; called with nothing when it closes.
51
+ function watch(x, z) {
52
+ stage.on = x !== undefined;
53
+ if (stage.on) {
54
+ stage.x = x;
55
+ stage.z = z;
56
+ }
57
+ }
58
+ // Turns someone standing at `at` ({ x, z, yaw }) to the stage, if there is one near enough.
59
+ function watchStage(at) {
60
+ if (!stage.on) return;
61
+ const dx = stage.x - at.x,
62
+ dz = stage.z - at.z,
63
+ d = Math.sqrt(dx * dx + dz * dz);
64
+ if (d < watchWithin && d > watchBeyond) at.yaw = Math.atan2(dx, dz);
65
+ }
66
+ // Whether g may be in flight: an alarm is up, or g is still away from their routine.
67
+ const alarmed = g => alarms.length > 0 || !!g.fx || !!g.fz;
68
+
69
+ // One step of g at clock t: `at` ({ x, z, yaw, speed, travel, focus }) holds where g's routine puts them, and is moved to where
70
+ // their flight has them. False when they stand where their routine puts them.
71
+ function flee(g, at, t) {
72
+ const hx = at.x,
73
+ hz = at.z,
74
+ dt = g.ft < 0 ? 0 : Math.min(0.1, Math.max(0, t - g.ft));
75
+ g.ft = t;
76
+ const x = hx + (g.fx || 0),
77
+ z = hz + (g.fz || 0);
78
+ let near = null,
79
+ nd = Infinity;
80
+ for (let i = 0; i < alarms.length; i++) {
81
+ const a = alarms[i],
82
+ ax = x - a.x,
83
+ az = z - a.z,
84
+ d = Math.sqrt(ax * ax + az * az);
85
+ if (d < a.r && d < nd) {
86
+ nd = d;
87
+ near = a;
88
+ }
89
+ }
90
+ let speed;
91
+ if (near) {
92
+ const away = nd < 0.01 ? g.ph || 1 : Math.atan2(x - near.x, z - near.z);
93
+ const k = sstep(0, 0.6, t - near.t + 0.6 - (g.ph % 1) * 0.5); // each starts at their own moment, not all on one beat
94
+ speed = go(g, x, z, hx, hz, away, run * k * dt, run * k);
95
+ } else {
96
+ const fx = g.fx || 0,
97
+ fz = g.fz || 0,
98
+ o = Math.sqrt(fx * fx + fz * fz);
99
+ if (o < 0.02) {
100
+ g.fx = g.fz = 0;
101
+ return false;
102
+ }
103
+ speed = go(g, x, z, hx, hz, Math.atan2(-g.fx, -g.fz), Math.min(o, walkBack * dt), walkBack);
104
+ }
105
+ if (!speed && !g.fx && !g.fz) return false; // hemmed in away from their routine, they stand there rather than leap back to it
106
+ at.x += g.fx;
107
+ at.z += g.fz;
108
+ at.yaw = g.fy;
109
+ at.speed = speed;
110
+ at.travel = t * speed + g.ph;
111
+ at.focus = undefined;
112
+ return true;
113
+ }
114
+ function go(g, x, z, hx, hz, want, step, speed) {
115
+ const a = steer(x, z, want, step, body, g.lean);
116
+ g.fy = a ?? want;
117
+ if (a === undefined) return 0; // hemmed in: they stand where they are
118
+ const turn = Math.sin(a - want);
119
+ if (turn > 0.1) g.lean = 1;
120
+ else if (turn < -0.1) g.lean = -1;
121
+ g.fx = x + Math.sin(a) * step - hx;
122
+ g.fz = z + Math.cos(a) * step - hz;
123
+ return speed;
124
+ }
125
+
126
+ return { alarms, raise, expire, watch, watchStage, alarmed, flee };
127
+ }
@@ -0,0 +1,183 @@
1
+ import { sstep } from '../math/scalar.js';
2
+
3
+ /**
4
+ * Where a crowd's people are, from routines laid out once: legs walked on a loop of the clock, a circle, or a way walked to and
5
+ * fro; and those routines fitted to the colliders of their space as the walls they stand among are built. A person is a plain
6
+ * record their crowd keeps; no frame allocates.
7
+ *
8
+ * Routine fields on a person `g`:
9
+ * - `legs`: [{ t0, tw, x0, z0, dx, dz, dir, len, travel, start, stop, hide, yaw, role, rest }], walked over `T` seconds from
10
+ * phase `ph` at `walkSpeed`, each eased in where it `start`s and out where it `stop`s; a leg that `hide`s ends indoors.
11
+ * - `speed`: moved by a keeper of its own, which sets `speed`, `travel` and `sway` (a turn off the line) each frame.
12
+ * - `r`, `w`: a circle of radius r about (ax, az) at w radians a second.
13
+ * - `sp`, `len`, `dx`, `dz`, `pathYaw`: a way of len metres from (ax, az) along (dx, dz), walked to and fro at sp metres a second.
14
+ * - otherwise they stand at (ax, az) facing `yaw`, `role` naming what they do and `graze` lowering a beast's head.
15
+ */
16
+
17
+ /**
18
+ * Where g's routine puts them at clock t, into `at` ({ x, z, yaw, speed, travel, focus, head, rest }). False while they are
19
+ * indoors at the end of a leg. `headAtHalt(leg, held, left)` gives a beast's head at a halt (1 down), from the seconds it has
20
+ * been held there and those left before the next leg; without it the head is down while grazing.
21
+ */
22
+ export function followRoutine(g, t, at, headAtHalt) {
23
+ at.x = g.ax;
24
+ at.z = g.az;
25
+ at.yaw = g.yaw || 0;
26
+ at.travel = 0;
27
+ at.speed = 0;
28
+ at.focus = g.role;
29
+ at.head = g.graze ? 1 : 0;
30
+ at.rest = 0;
31
+ if (g.legs) {
32
+ const tt = (t + g.ph) % g.T;
33
+ let l = g.legs[0],
34
+ next = null;
35
+ for (let i = 0; i < g.legs.length; i++) {
36
+ const q = g.legs[i];
37
+ if (q.t0 <= tt) l = q;
38
+ else {
39
+ next = q;
40
+ break;
41
+ }
42
+ }
43
+ const local = tt - l.t0,
44
+ ease = Math.min(0.23, l.tw * 0.5);
45
+ let u = Math.min(1, local / Math.max(l.tw, 1e-3));
46
+ if (u === 1 && l.hide) return false;
47
+ if (u < 1) {
48
+ let speed = g.walkSpeed;
49
+ if (ease > 0 && l.start && local < ease) {
50
+ const f = local / ease;
51
+ u *= f * (2 - f);
52
+ speed *= f * (4 - 3 * f);
53
+ } else if (ease > 0 && l.stop && l.tw - local < ease) {
54
+ const f = (l.tw - local) / ease;
55
+ u = 1 - (1 - u) * f * (2 - f);
56
+ speed *= f * (4 - 3 * f);
57
+ }
58
+ at.speed = speed;
59
+ } else {
60
+ if (l.role) at.focus = l.role;
61
+ if (headAtHalt) at.head = headAtHalt(l, local - l.tw, (next ? next.t0 : g.T) - tt);
62
+ at.rest = l.rest | 0;
63
+ }
64
+ at.x = l.x0 + l.dx * u;
65
+ at.z = l.z0 + l.dz * u;
66
+ at.yaw = l.yaw !== undefined && u === 1 ? l.yaw : l.dir;
67
+ at.travel = l.travel + l.len * u;
68
+ } else if (g.speed) {
69
+ at.speed = g.speed;
70
+ at.travel = g.travel;
71
+ at.yaw += g.sway || 0;
72
+ } else if (g.r) {
73
+ const a = t * g.w + g.ph,
74
+ speed = Math.abs(g.w) * g.r;
75
+ at.x = g.ax + Math.cos(a) * g.r;
76
+ at.z = g.az + Math.sin(a) * g.r;
77
+ at.yaw = -a + (g.w > 0 ? 0 : Math.PI);
78
+ at.speed = speed;
79
+ at.travel = t * speed + g.ph;
80
+ } else if (g.sp > 0 && g.len > 0.5) {
81
+ const travel = t * g.sp + g.ph,
82
+ f = (travel / g.len) % 2,
83
+ u = f < 1 ? f : 2 - f;
84
+ at.travel = travel;
85
+ at.x += g.dx * u;
86
+ at.z += g.dz * u;
87
+ at.yaw = g.pathYaw + (f < 1 ? 0 : Math.PI);
88
+ at.speed = g.sp * sstep(0, 0.23, Math.min(u, 1 - u) * g.len);
89
+ }
90
+ return true;
91
+ }
92
+
93
+ /**
94
+ * Fits routines to the walls round them. A builder lays a walker's way, or a stander's spot, from its plan of streets; the houses
95
+ * and stalls there may be built later. A stander inside a collider is set outside it, and a walker's way ends a little short of
96
+ * the first wall on it (or runs the other way from its start, where that is longer).
97
+ *
98
+ * @param {object} spaces What `createSpaces` returns: `collideIn`, `hit`, `onDeck` and `worldSpace` are read.
99
+ * @param {object} [options]
100
+ * @param {number} [options.body=0.35] Metres: a walker's half width.
101
+ * @param {number} [options.step=0.5] Metres between the spots tested along a way.
102
+ * @param {number} [options.short=0.9] Metres short of a wall a walker turns back.
103
+ * @param {number} [options.least=0.5] Metres: a way shorter than this is no walk, and they stand.
104
+ * @returns {(g: object, space: object) => void} Fits g, whose builder gave it (ox, oz) and, for a walker, the way's far end
105
+ * (bx, bz), to the colliders of `space` as they stand now: sets ax, az, dx, dz, len, pathYaw or yaw, and `fit` (the
106
+ * space's collider count it was fitted to). People on decks or roofs, on a keeper's leash (`steady`), on legs or circles keep
107
+ * what their builder gave them.
108
+ */
109
+ export function createWalkFit({ collideIn, hit, onDeck, worldSpace }, { body = 0.35, step = 0.5, short = 0.9, least = 0.5 } = {}) {
110
+ const end = { x: 0, z: 0, len: 0 };
111
+ // How far a body may go from (x, z) along (ux, uz) up to len metres before a collider stops it, less `short`; into end.
112
+ function clearRun(grid, x, z, ux, uz, len) {
113
+ let free = len;
114
+ for (let t = step; t < len + step; t += step) {
115
+ const d = Math.min(t, len);
116
+ if (collideIn(grid, x + ux * d, z + uz * d, body)) {
117
+ free = Math.max(0, d - step - short);
118
+ break;
119
+ }
120
+ }
121
+ end.len = free;
122
+ end.x = ux * free;
123
+ end.z = uz * free;
124
+ }
125
+ return function fitWalk(g, sp) {
126
+ g.fit = sp.colliders;
127
+ if (!sp.grid || g.steady || g.legs || g.r || (g.ground !== undefined && !g.onGround)) return;
128
+ let x = g.ox,
129
+ z = g.oz;
130
+ if (sp === worldSpace && onDeck(x, z)) return;
131
+ if (collideIn(sp.grid, x, z, body)) {
132
+ x = hit.x;
133
+ z = hit.z;
134
+ }
135
+ g.ax = x;
136
+ g.az = z;
137
+ if (g.onGround) g.ground = sp.ground(x, z);
138
+ if (!(g.sp > 0)) return;
139
+ const dx = g.bx - x,
140
+ dz = g.bz - z,
141
+ len = Math.hypot(dx, dz);
142
+ if (len < least) return;
143
+ const ux = dx / len,
144
+ uz = dz / len;
145
+ clearRun(sp.grid, x, z, ux, uz, len);
146
+ let run = end.len,
147
+ sx = end.x,
148
+ sz = end.z;
149
+ if (run < len * 0.5) {
150
+ clearRun(sp.grid, x, z, -ux, -uz, len);
151
+ if (end.len > run) {
152
+ run = end.len;
153
+ sx = end.x;
154
+ sz = end.z;
155
+ }
156
+ }
157
+ g.dx = sx;
158
+ g.dz = sz;
159
+ g.len = run;
160
+ if (run >= least) g.pathYaw = Math.atan2(sx, sz);
161
+ else g.yaw = Math.atan2(dx, dz); // no room to walk: they stand, facing the way they would have gone
162
+ };
163
+ }
164
+
165
+ /**
166
+ * A level of detail chosen with hysteresis: 3 near, 2 medium, 1 light, 0 past them. `near`, `medium` and `light` are each level's
167
+ * outer edge in metres at a lens of 1, as [for one outside it, for one already inside it]: an edge lies a little further out for
168
+ * one inside, so that nobody standing on it changes every frame. `d2` is the distance squared, `previous` the level last frame,
169
+ * `lens` how much nearer the lens brings things (edges move out with it).
170
+ */
171
+ export function detailLevel(d2, previous, lens, near, medium, light) {
172
+ const n = (previous === 3 ? near[1] : near[0]) * lens,
173
+ m = (previous >= 2 ? medium[1] : medium[0]) * lens,
174
+ l = (previous >= 1 ? light[1] : light[0]) * lens;
175
+ return d2 < n * n ? 3 : d2 < m * m ? 2 : d2 < l * l ? 1 : 0;
176
+ }
177
+
178
+ /** Turns a heading `dir` towards `want` at `rate` a second over `gap` seconds, at once after a gap of more than `snap` seconds. */
179
+ export function easeHeading(dir, want, gap, rate, snap = 0.2) {
180
+ if (gap > snap || gap < 0) return want;
181
+ const turn = want - dir;
182
+ return dir + Math.atan2(Math.sin(turn), Math.cos(turn)) * Math.min(1, gap * rate);
183
+ }
@@ -0,0 +1,59 @@
1
+ export function createDailyClock({ getFrame, getHour, jumpHours = 0.05 }) {
2
+ const clock = { frame: -1, hour: 0, cut: true };
3
+
4
+ // The hour shown this frame, worked out once a frame, and whether the clock was turned since the last (a cut: what follows the clock
5
+ // may then change in view).
6
+ function now() {
7
+ const frame = getFrame();
8
+ if (clock.frame !== frame) {
9
+ const hour = getHour();
10
+ let moved = Math.abs(hour - clock.hour);
11
+ if (moved > 12) moved = 24 - moved;
12
+ clock.cut = clock.frame < 0 || moved > jumpHours;
13
+ clock.hour = hour;
14
+ clock.frame = frame;
15
+ }
16
+ return clock;
17
+ }
18
+ const hourNow = () => now().hour;
19
+ const clockTurned = () => now().cut;
20
+
21
+ // A span of the day from one ramp to another, as the hours of the one that comes first and goes first (rank 0) and of the last
22
+ // (rank 1); one that runs over midnight is counted on into the next day.
23
+ function spanOf(from, to, h, rank) {
24
+ const a = from[0] + rank * (from[1] - from[0]);
25
+ let b = to[0] + rank * (to[1] - to[0]);
26
+ if (b < a) {
27
+ b += 24;
28
+ if (h < a) h += 24;
29
+ }
30
+ return h >= a && h < b;
31
+ }
32
+ // Whether one of rank `rank` is out now in a span [from, to] of ramps, and will still be out `ahead` hours from now (one that
33
+ // needs that long to get in goes in so much sooner).
34
+ function isOut(from, to, rank, ahead = 0) {
35
+ const h = hourNow();
36
+ return spanOf(from, to, h, rank) && spanOf(from, to, (h + ahead) % 24, rank);
37
+ }
38
+ // The share of a kind that is out at hour h: from none before `from` begins to all once it ends, and back to none over `to`.
39
+ function shareOut(from, to, h = hourNow()) {
40
+ const up = from[1] - from[0] || 1e-3,
41
+ down = to[1] - to[0] || 1e-3;
42
+ let t = h,
43
+ end = to[0];
44
+ if (end < from[0]) {
45
+ end += 24;
46
+ if (t < from[0]) t += 24;
47
+ }
48
+ const came = Math.min(1, Math.max(0, (t - from[0]) / up)),
49
+ gone = Math.min(1, Math.max(0, (t - end) / down));
50
+ return came - gone;
51
+ }
52
+
53
+ // The hours between two hours of the day, the short way round.
54
+ const hoursApart = (a, b) => {
55
+ const d = Math.abs(a - b) % 24;
56
+ return d > 12 ? 24 - d : d;
57
+ };
58
+ return { hourNow, clockTurned, isOut, shareOut, hoursApart };
59
+ }
@@ -0,0 +1,84 @@
1
+ // An index of upright instanced obstacles. Instance transforms, camera and subject
2
+ // use the same coordinate frame; register meshes before releasing CPU geometry.
3
+ export function createInstanceSight({ widthFraction = 0.7, nearSubject = 0.6 } = {}) {
4
+ const bySpace = new Map(), // space id -> [{ mesh, body }]
5
+ bodies = new WeakMap(); // a model's position attribute -> its body
6
+
7
+ // The body of a model, read off its bounds: `from`, the share of its height where its crown begins (0 for a bush or a rock);
8
+ // `trunk`, the trunk's radius in the model's units. Worked out while the model's arrays are there (before its first draw), and
9
+ // from its bounding sphere if they are gone.
10
+ function bodyOf(geometry, from, trunk) {
11
+ const key = geometry.attributes.position,
12
+ known = bodies.get(key);
13
+ if (known && known.frac === from && known.trunk === trunk) return known;
14
+ let x0, x1, y0, y1, z0, z1;
15
+ if (key.array) {
16
+ if (!geometry.boundingBox) geometry.computeBoundingBox();
17
+ ({
18
+ min: { x: x0, y: y0, z: z0 },
19
+ max: { x: x1, y: y1, z: z1 },
20
+ } = geometry.boundingBox);
21
+ } else {
22
+ const { center: c, radius: r } = geometry.boundingSphere;
23
+ [x0, x1, y0, y1, z0, z1] = [c.x - r * 0.7, c.x + r * 0.7, 0, c.y + r * 0.7, c.z - r * 0.7, c.z + r * 0.7];
24
+ }
25
+ const top = Math.max(y1, 0),
26
+ body = {
27
+ frac: from,
28
+ x: (x0 + x1) / 2,
29
+ z: (z0 + z1) / 2,
30
+ r: (widthFraction * Math.max(x1 - x0, z1 - z0)) / 2,
31
+ from: Math.max(y0, 0) + from * (top - Math.max(y0, 0)),
32
+ top,
33
+ trunk,
34
+ };
35
+ bodies.set(key, body);
36
+ return body;
37
+ }
38
+ // Files instanced mesh `mesh` (its bounding sphere worked out) as plants that can stand in a line of sight in space `spaceId`.
39
+ function addSightPlants(spaceId, mesh, from = 0, trunk = 0) {
40
+ if (!mesh.boundingSphere) mesh.computeBoundingSphere();
41
+ const list = bySpace.get(spaceId) ?? bySpace.set(spaceId, []).get(spaceId);
42
+ list.push({ mesh, body: bodyOf(mesh.geometry, from, trunk) });
43
+ }
44
+
45
+ // True if a plant of space `spaceId` stands in the middle of the picture a lens at p takes of its subject at `at` (points with
46
+ // x, y, z): across the line of sight, or within `spread` of it (a tangent: the share of the picture round its middle that must
47
+ // be clear, as the angle it takes from the lens), or round the lens itself. A bush beside the line a few metres from the lens
48
+ // fills the foreground as surely as one on it.
49
+ function plantInTheWay(spaceId, p, at, spread) {
50
+ const list = bySpace.get(spaceId);
51
+ if (!list) return false;
52
+ const dx = at.x - p.x,
53
+ dy = at.y - p.y,
54
+ dz = at.z - p.z,
55
+ flat2 = dx * dx + dz * dz,
56
+ flat = Math.sqrt(flat2),
57
+ reach = Math.sqrt(flat2 + dy * dy),
58
+ mx = (p.x + at.x) / 2,
59
+ my = (p.y + at.y) / 2,
60
+ mz = (p.z + at.z) / 2;
61
+ for (let k = 0; k < list.length; k++) {
62
+ const { mesh, body } = list[k],
63
+ s = mesh.boundingSphere;
64
+ if (Math.hypot(s.center.x - mx, s.center.y - my, s.center.z - mz) > s.radius + reach * (0.5 + spread)) continue; // the batch is nowhere near
65
+ const m = mesh.instanceMatrix.array;
66
+ for (let i = 0; i < mesh.count * 16; i += 16) {
67
+ const wide = Math.hypot(m[i], m[i + 1], m[i + 2]),
68
+ tall = Math.hypot(m[i + 4], m[i + 5], m[i + 6]),
69
+ x = m[i + 12] + m[i] * body.x + m[i + 8] * body.z,
70
+ z = m[i + 14] + m[i + 2] * body.x + m[i + 10] * body.z,
71
+ u = flat2 > 1e-6 ? Math.min(1, Math.max(0, ((x - p.x) * dx + (z - p.z) * dz) / flat2)) : 0;
72
+ if ((1 - u) * flat < nearSubject) continue;
73
+ const room = u * flat * spread, // how far the middle of the picture reaches round the line of sight there, in metres
74
+ aside = Math.hypot(p.x + dx * u - x, p.z + dz * u - z) - room,
75
+ rise = p.y + dy * u - m[i + 13], // the line's height over the plant's foot where it passes nearest
76
+ crown = body.from * tall;
77
+ if (aside < body.r * wide && rise + room > crown && rise - room < body.top * tall) return true;
78
+ if (aside < body.trunk * wide && rise - room < crown && rise + room > 0) return true;
79
+ }
80
+ }
81
+ return false;
82
+ }
83
+ return { add: addSightPlants, obstructed: plantInTheWay, clear: spaceId => bySpace.delete(spaceId) };
84
+ }