@yaks/stage 0.0.0-stage → 0.2.36

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 (79) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +73 -2
  3. package/_dist/aim.d.ts +14 -0
  4. package/_dist/aim.d.ts.map +1 -0
  5. package/_dist/air.d.ts +23 -0
  6. package/_dist/air.d.ts.map +1 -0
  7. package/_dist/bus.d.ts +8 -0
  8. package/_dist/bus.d.ts.map +1 -0
  9. package/_dist/cam.d.ts +75 -0
  10. package/_dist/cam.d.ts.map +1 -0
  11. package/_dist/ears.d.ts +71 -0
  12. package/_dist/ears.d.ts.map +1 -0
  13. package/_dist/flames.d.ts +10 -0
  14. package/_dist/flames.d.ts.map +1 -0
  15. package/_dist/fx.d.ts +27 -0
  16. package/_dist/fx.d.ts.map +1 -0
  17. package/_dist/halo.d.ts +4 -0
  18. package/_dist/halo.d.ts.map +1 -0
  19. package/_dist/instances.d.ts +24 -0
  20. package/_dist/instances.d.ts.map +1 -0
  21. package/_dist/mod.d.ts +7 -0
  22. package/_dist/mod.d.ts.map +1 -0
  23. package/_dist/pace.d.ts +19 -0
  24. package/_dist/pace.d.ts.map +1 -0
  25. package/_dist/render.d.ts +7 -0
  26. package/_dist/render.d.ts.map +1 -0
  27. package/_dist/samples.d.ts +27 -0
  28. package/_dist/samples.d.ts.map +1 -0
  29. package/_dist/scene.d.ts +28 -0
  30. package/_dist/scene.d.ts.map +1 -0
  31. package/_dist/sound.d.ts +13 -0
  32. package/_dist/sound.d.ts.map +1 -0
  33. package/aim.js +63 -0
  34. package/aim.js.map +1 -0
  35. package/aim.ts +71 -0
  36. package/air.js +40 -0
  37. package/air.js.map +1 -0
  38. package/air.ts +66 -0
  39. package/bus.js +36 -0
  40. package/bus.js.map +1 -0
  41. package/bus.ts +51 -0
  42. package/cam.js +110 -0
  43. package/cam.js.map +1 -0
  44. package/cam.ts +181 -0
  45. package/deno.json +1 -0
  46. package/ears.js +123 -0
  47. package/ears.js.map +1 -0
  48. package/ears.ts +131 -0
  49. package/flames.js +61 -0
  50. package/flames.js.map +1 -0
  51. package/flames.ts +69 -0
  52. package/fx.js +235 -0
  53. package/fx.js.map +1 -0
  54. package/fx.ts +330 -0
  55. package/halo.js +22 -0
  56. package/halo.js.map +1 -0
  57. package/halo.ts +23 -0
  58. package/instances.js +131 -0
  59. package/instances.js.map +1 -0
  60. package/instances.ts +166 -0
  61. package/mod.js +5 -0
  62. package/mod.js.map +1 -0
  63. package/mod.ts +7 -0
  64. package/pace.js +63 -0
  65. package/pace.js.map +1 -0
  66. package/pace.ts +86 -0
  67. package/package.json +73 -4
  68. package/render.js +50 -0
  69. package/render.js.map +1 -0
  70. package/render.ts +68 -0
  71. package/samples.js +162 -0
  72. package/samples.js.map +1 -0
  73. package/samples.ts +204 -0
  74. package/scene.js +80 -0
  75. package/scene.js.map +1 -0
  76. package/scene.ts +112 -0
  77. package/sound.js +117 -0
  78. package/sound.js.map +1 -0
  79. package/sound.ts +175 -0
package/fx.js ADDED
@@ -0,0 +1,235 @@
1
+ // What flies about for a moment: bits knocked off a creature, dust at a
2
+ // runner's heels, embers off the fire, fireflies at dusk, and the numbers
3
+ // that float up from a blow. None of it is state; it is drawn and forgotten.
4
+ // Also the labels that ride above heads (names, health), which live in the
5
+ // page's DOM and are placed over the scene every frame.
6
+ // @ts-types="npm:@types/three@^0.186.0"
7
+ import * as THREE from "three";
8
+ import { halo } from "./halo.js";
9
+ /** A pool of little cubes: `lit` ones are shaded like the world, the others
10
+ * glow. */ export let bits = (scene, lit, most = 500)=>{
11
+ let material = lit ? new THREE.MeshLambertMaterial({
12
+ color: 0xffffff
13
+ }) : new THREE.MeshBasicMaterial({
14
+ color: 0xffffff,
15
+ transparent: true,
16
+ blending: THREE.AdditiveBlending,
17
+ depthWrite: false,
18
+ fog: false
19
+ });
20
+ let mesh = new THREE.InstancedMesh(new THREE.BoxGeometry(1, 1, 1), material, most);
21
+ mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
22
+ // The first emitted bit must not change this mesh's shader variant.
23
+ mesh.setColorAt(0, new THREE.Color(0xffffff));
24
+ mesh.frustumCulled = false;
25
+ mesh.count = 0;
26
+ mesh.castShadow = false;
27
+ scene.add(mesh);
28
+ let flames = lit ? null : new THREE.InstancedMesh(new THREE.ConeGeometry(0.5, 1, 5), material, Math.min(most, 80));
29
+ if (flames) {
30
+ flames.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
31
+ flames.setColorAt(0, new THREE.Color(0xffffff));
32
+ flames.frustumCulled = false;
33
+ flames.count = 0;
34
+ scene.add(flames);
35
+ }
36
+ let halos = lit ? null : new THREE.InstancedMesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshBasicMaterial({
37
+ map: halo(),
38
+ transparent: true,
39
+ blending: THREE.AdditiveBlending,
40
+ depthWrite: false,
41
+ fog: false,
42
+ toneMapped: false,
43
+ side: THREE.DoubleSide
44
+ }), Math.min(most, 64));
45
+ if (halos) {
46
+ halos.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
47
+ halos.setColorAt(0, new THREE.Color(0xffffff));
48
+ halos.frustumCulled = false;
49
+ halos.count = 0;
50
+ scene.add(halos);
51
+ }
52
+ let live = [];
53
+ let m = new THREE.Matrix4();
54
+ let q = new THREE.Quaternion();
55
+ let s = new THREE.Vector3();
56
+ let spin = new THREE.Euler();
57
+ let tint = new THREE.Color();
58
+ return {
59
+ /** `n` bits from `at`, flung up to `speed` metres a second */ emit: (at, color, n, o = {})=>{
60
+ let c = new THREE.Color(color);
61
+ for(let i = 0; i < n; i++){
62
+ if (live.length >= most) live.shift();
63
+ let a = Math.random() * Math.PI * 2, sp = (o.speed ?? 3) * (0.4 + Math.random() * 0.6), flame = !!o.flame;
64
+ let p = at.clone();
65
+ if (flame) {
66
+ p.add(new THREE.Vector3(Math.cos(a) * Math.random() * 0.18, 0, Math.sin(a) * Math.random() * 0.18));
67
+ }
68
+ live.push({
69
+ p,
70
+ v: new THREE.Vector3(Math.cos(a) * sp * (flame ? 0.15 : 1), (o.up ?? 3) * (flame ? 0.8 + Math.random() * 0.6 : 0.5 + Math.random()), Math.sin(a) * sp * (flame ? 0.15 : 1)),
71
+ born: performance.now(),
72
+ life: (o.life ?? 0.7) * (0.7 + Math.random() * 0.6),
73
+ size: (o.size ?? 0.12) * (0.7 + Math.random() * 0.6),
74
+ fall: o.fall ?? (flame ? -0.4 : 12),
75
+ color: flame ? new THREE.Color(i % 3 == 0 ? 0xffe28a : i % 3 == 1 ? 0xffa334 : 0xff5525) : c.clone().offsetHSL(0, 0, (Math.random() - 0.5) * 0.12),
76
+ halo: !!o.halo || flame && i % 3 == 0,
77
+ flame
78
+ });
79
+ }
80
+ },
81
+ tick: (dt, camera)=>{
82
+ let t = performance.now();
83
+ live = live.filter((b)=>(t - b.born) / 1000 < b.life);
84
+ let cubes = 0, tongues = 0;
85
+ for(let i = live.length - 1; i >= 0; i--){
86
+ let b = live[i];
87
+ b.v.y -= b.fall * dt;
88
+ b.p.addScaledVector(b.v, dt);
89
+ let k = 1 - (t - b.born) / 1000 / b.life;
90
+ spin.set(b.flame ? 0 : b.born % 7 + t * 0.003, b.flame ? 0 : b.born % 5 + t * 0.002, b.flame ? Math.sin(t * 0.012 + b.born) * 0.2 : 0);
91
+ q.setFromEuler(spin);
92
+ let fade = Math.min(1, k * 2.5);
93
+ s.setScalar(b.size * fade);
94
+ if (b.flame) s.y *= 2.8;
95
+ m.compose(b.p, q, s);
96
+ if (b.flame) {
97
+ if (flames && tongues < flames.instanceMatrix.count) {
98
+ flames.setMatrixAt(tongues, m);
99
+ flames.setColorAt(tongues++, b.color);
100
+ }
101
+ } else {
102
+ mesh.setMatrixAt(cubes, m);
103
+ mesh.setColorAt(cubes++, b.color);
104
+ }
105
+ }
106
+ mesh.count = cubes;
107
+ mesh.instanceMatrix.needsUpdate = true;
108
+ if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
109
+ if (flames) {
110
+ flames.count = tongues;
111
+ flames.instanceMatrix.needsUpdate = true;
112
+ if (flames.instanceColor) flames.instanceColor.needsUpdate = true;
113
+ }
114
+ if (halos) {
115
+ let shown = 0;
116
+ q.copy(camera?.quaternion ?? mesh.quaternion);
117
+ for(let i = live.length - 1; i >= 0 && shown < halos.instanceMatrix.count; i--){
118
+ let b = live[i];
119
+ if (!b.halo) continue;
120
+ let k = 1 - (t - b.born) / 1000 / b.life;
121
+ s.setScalar(b.size * (b.flame ? 5 : 4) * Math.min(1, k * 2.5));
122
+ m.compose(b.p, q, s);
123
+ halos.setMatrixAt(shown, m);
124
+ halos.setColorAt(shown++, tint.copy(b.color).multiplyScalar(k));
125
+ }
126
+ halos.count = shown;
127
+ halos.instanceMatrix.needsUpdate = true;
128
+ if (halos.instanceColor) halos.instanceColor.needsUpdate = true;
129
+ }
130
+ }
131
+ };
132
+ };
133
+ /** The labels over the scene: plates that follow someone, and numbers that
134
+ * float up and fade. A label that would be `under` the glass is not shown. */ export let overlay = (layer, camera, under = ()=>false)=>{
135
+ let at = new THREE.Vector3();
136
+ let floats = [];
137
+ let plates = new Map();
138
+ let byBody = new WeakMap();
139
+ let sized = new ResizeObserver((entries)=>{
140
+ for (let entry of entries){
141
+ let pl = byBody.get(entry.target);
142
+ if (!pl) continue;
143
+ pl.w = entry.borderBoxSize[0]?.inlineSize ?? entry.contentRect.width;
144
+ pl.h = entry.borderBoxSize[0]?.blockSize ?? entry.contentRect.height;
145
+ }
146
+ });
147
+ let screen = (p)=>{
148
+ at.copy(p).project(camera);
149
+ if (at.z > 1 || at.x < -1.2 || at.x > 1.2 || at.y < -1.2 || at.y > 1.2) {
150
+ return null;
151
+ }
152
+ return [
153
+ (at.x + 1) / 2 * innerWidth,
154
+ (1 - at.y) / 2 * innerHeight
155
+ ];
156
+ };
157
+ // Put `el` over `p`, unless `p` is off the screen or what `el` shows, `w`
158
+ // by `h` and standing on the point, would be under the glass.
159
+ let place = (el, p, w = 0, h = 0)=>{
160
+ let s = screen(p);
161
+ let shown = !!s && !under(s[0] - w / 2, s[1] - h, w, h);
162
+ // Keep plates laid out while occluded: a display:none plate measures 0x0
163
+ // in ResizeObserver, so its next frame tests a different overlap and
164
+ // alternates between hidden and shown at the edge of the glass.
165
+ el.style.visibility = shown ? '' : 'hidden';
166
+ if (s && shown) {
167
+ el.style.transform = `translate(${s[0].toFixed(1)}px, ${s[1].toFixed(1)}px)`;
168
+ }
169
+ };
170
+ return {
171
+ float: (text, p, kind)=>{
172
+ let el = document.createElement('div');
173
+ el.className = `Float Float-${kind}`;
174
+ el.textContent = text;
175
+ layer.append(el);
176
+ let jitter = new THREE.Vector3((Math.random() - 0.5) * 0.6, 0, (Math.random() - 0.5) * 0.6);
177
+ floats.push({
178
+ el,
179
+ p: p.clone().add(jitter),
180
+ born: performance.now()
181
+ });
182
+ },
183
+ /** keep a plate over `p` this frame, holding `html` */ plate: (key, p, html, cls = 'Plate')=>{
184
+ let pl = plates.get(key);
185
+ if (!pl) {
186
+ let el = document.createElement('div');
187
+ el.className = cls;
188
+ let body = document.createElement('div');
189
+ body.className = 'Plate_In';
190
+ el.append(body);
191
+ layer.append(el);
192
+ pl = {
193
+ el,
194
+ body,
195
+ html: '',
196
+ seen: true,
197
+ w: 0,
198
+ h: 0
199
+ };
200
+ plates.set(key, pl);
201
+ byBody.set(body, pl);
202
+ sized.observe(body);
203
+ }
204
+ if (pl.html != html) {
205
+ pl.body.innerHTML = html;
206
+ pl.html = html;
207
+ }
208
+ pl.seen = true;
209
+ place(pl.el, p, pl.w, pl.h);
210
+ },
211
+ tick: ()=>{
212
+ let t = performance.now();
213
+ floats = floats.filter((f)=>{
214
+ let age = (t - f.born) / 1000;
215
+ if (age > 1.1) {
216
+ f.el.remove();
217
+ return false;
218
+ }
219
+ f.p.y += 0.018;
220
+ place(f.el, f.p);
221
+ f.el.style.opacity = String(Math.min(1, (1.1 - age) * 3));
222
+ return true;
223
+ });
224
+ for (let [key, pl] of plates){
225
+ if (!pl.seen) {
226
+ sized.unobserve(pl.body);
227
+ pl.el.remove();
228
+ plates.delete(key);
229
+ }
230
+ pl.seen = false;
231
+ }
232
+ }
233
+ };
234
+ };
235
+ //# sourceMappingURL=fx.js.map
package/fx.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fx.js","sources":["./fx.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,0EAA0E;AAC1E,6EAA6E;AAC7E,2EAA2E;AAC3E,wDAAwD;AACxD,wCAAwC;AACxC,YAAY,mBAA+B;AAC3C,SAAS,IAAI,oBAAmB;AAmChC;SACS,GACT,OAAO,IAAI,OAAO,CAAC,OAAoB,KAAc,OAAO,GAAG;EAC7D,IAAI,WAAW,MACX,IAAI,MAAM,mBAAmB,CAAC;IAAE,OAAO;EAAS,KAChD,IAAI,MAAM,iBAAiB,CAAC;IAC5B,OAAO;IACP,aAAa;IACb,UAAU,MAAM,gBAAgB;IAChC,YAAY;IACZ,KAAK;EACP;EACF,IAAI,OAAO,IAAI,MAAM,aAAa,CAChC,IAAI,MAAM,WAAW,CAAC,GAAG,GAAG,IAC5B,UACA;EAEF,KAAK,cAAc,CAAC,QAAQ,CAAC,MAAM,gBAAgB;EACnD,oEAAoE;EACpE,KAAK,UAAU,CAAC,GAAG,IAAI,MAAM,KAAK,CAAC;EACnC,KAAK,aAAa,GAAG;EACrB,KAAK,KAAK,GAAG;EACb,KAAK,UAAU,GAAG;EAClB,MAAM,GAAG,CAAC;EACV,IAAI,SAAS,MAAM,OAAO,IAAI,MAAM,aAAa,CAC/C,IAAI,MAAM,YAAY,CAAC,KAAK,GAAG,IAC/B,UACA,KAAK,GAAG,CAAC,MAAM;EAEjB,IAAI,QAAQ;IACV,OAAO,cAAc,CAAC,QAAQ,CAAC,MAAM,gBAAgB;IACrD,OAAO,UAAU,CAAC,GAAG,IAAI,MAAM,KAAK,CAAC;IACrC,OAAO,aAAa,GAAG;IACvB,OAAO,KAAK,GAAG;IACf,MAAM,GAAG,CAAC;EACZ;E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GAAG,SAAS,GAAG;QACf,IAAI,OAAO,SAAS,aAAa,CAAC;QAClC,KAAK,SAAS,GAAG;QACjB,GAAG,MAAM,CAAC;QACV,MAAM,MAAM,CAAC;QACb,KAAK;UAAE;UAAI;UAAM,MAAM;UAAI,MAAM;UAAM,GAAG;UAAG,GAAG;QAAE;QAClD,OAAO,GAAG,CAAC,KAAK;QAChB,OAAO,GAAG,CAAC,MAAM;QACjB,MAAM,OAAO,CAAC;MAChB;MACA,IAAI,GAAG,IAAI,IAAI,MAAM;QACnB,GAAG,IAAI,CAAC,SAAS,GAAG;QACpB,GAAG,IAAI,GAAG;MACZ;MACA,GAAG,IAAI,GAAG;MACV,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,EAAE,GAAG,CAAC;IAC5B;IACA,MAAM;MACJ,IAAI,IAAI,YAAY,GAAG;MACvB,SAAS,OAAO,MAAM,CAAC,CAAC;QACtB,IAAI,MAAM,CAAC,IAAI,EAAE,IAAI,IAAI;QACzB,IAAI,MAAM,KAAK;UACb,EAAE,EAAE,CAAC,MAAM;UACX,OAAO;QACT;QACA,EAAE,CAAC,CAAC,CAAC,IAAI;QACT,MAAM,EAAE,EAAE,EAAE,EAAE,CAAC;QACf,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,GAAG,OAAO,KAAK,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,IAAI;QACtD,OAAO;MACT;MACA,KAAK,IAAI,CAAC,KAAK,GAAG,IAAI,OAAQ;QAC5B,IAAI,CAAC,GAAG,IAAI,EAAE;UACZ,MAAM,SAAS,CAAC,GAAG,IAAI;UACvB,GAAG,EAAE,CAAC,MAAM;UACZ,OAAO,MAAM,CAAC;QAChB;QACA,GAAG,IAAI,GAAG;MACZ;IACF;EACF;AACF,EAAC"}
package/fx.ts ADDED
@@ -0,0 +1,330 @@
1
+ // What flies about for a moment: bits knocked off a creature, dust at a
2
+ // runner's heels, embers off the fire, fireflies at dusk, and the numbers
3
+ // that float up from a blow. None of it is state; it is drawn and forgotten.
4
+ // Also the labels that ride above heads (names, health), which live in the
5
+ // page's DOM and are placed over the scene every frame.
6
+ // @ts-types="npm:@types/three@^0.186.0"
7
+ import * as THREE from 'three'
8
+ import { halo } from './halo.js'
9
+
10
+ type Bit = {
11
+ p: THREE.Vector3
12
+ v: THREE.Vector3
13
+ born: number
14
+ life: number
15
+ size: number
16
+ fall: number
17
+ color: THREE.Color
18
+ halo: boolean
19
+ flame: boolean
20
+ }
21
+
22
+ /** Emission values for a burst of particles. */
23
+ export type Emission = {
24
+ speed?: number
25
+ up?: number
26
+ life?: number
27
+ size?: number
28
+ fall?: number
29
+ halo?: boolean
30
+ flame?: boolean
31
+ }
32
+
33
+ export type Bits = {
34
+ emit: (
35
+ at: THREE.Vector3,
36
+ color: THREE.ColorRepresentation,
37
+ n: number,
38
+ options?: Emission,
39
+ ) => void
40
+ tick: (dt: number, camera?: THREE.Camera) => void
41
+ }
42
+
43
+ /** A pool of little cubes: `lit` ones are shaded like the world, the others
44
+ * glow. */
45
+ export let bits = (scene: THREE.Scene, lit: boolean, most = 500): Bits => {
46
+ let material = lit
47
+ ? new THREE.MeshLambertMaterial({ color: 0xffffff })
48
+ : new THREE.MeshBasicMaterial({
49
+ color: 0xffffff,
50
+ transparent: true,
51
+ blending: THREE.AdditiveBlending,
52
+ depthWrite: false,
53
+ fog: false,
54
+ })
55
+ let mesh = new THREE.InstancedMesh(
56
+ new THREE.BoxGeometry(1, 1, 1),
57
+ material,
58
+ most,
59
+ )
60
+ mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage)
61
+ // The first emitted bit must not change this mesh's shader variant.
62
+ mesh.setColorAt(0, new THREE.Color(0xffffff))
63
+ mesh.frustumCulled = false
64
+ mesh.count = 0
65
+ mesh.castShadow = false
66
+ scene.add(mesh)
67
+ let flames = lit ? null : new THREE.InstancedMesh(
68
+ new THREE.ConeGeometry(0.5, 1, 5),
69
+ material,
70
+ Math.min(most, 80),
71
+ )
72
+ if (flames) {
73
+ flames.instanceMatrix.setUsage(THREE.DynamicDrawUsage)
74
+ flames.setColorAt(0, new THREE.Color(0xffffff))
75
+ flames.frustumCulled = false
76
+ flames.count = 0
77
+ scene.add(flames)
78
+ }
79
+ let halos = lit ? null : new THREE.InstancedMesh(
80
+ new THREE.PlaneGeometry(1, 1),
81
+ new THREE.MeshBasicMaterial({
82
+ map: halo(),
83
+ transparent: true,
84
+ blending: THREE.AdditiveBlending,
85
+ depthWrite: false,
86
+ fog: false,
87
+ toneMapped: false,
88
+ side: THREE.DoubleSide,
89
+ }),
90
+ Math.min(most, 64),
91
+ )
92
+ if (halos) {
93
+ halos.instanceMatrix.setUsage(THREE.DynamicDrawUsage)
94
+ halos.setColorAt(0, new THREE.Color(0xffffff))
95
+ halos.frustumCulled = false
96
+ halos.count = 0
97
+ scene.add(halos)
98
+ }
99
+ let live: Bit[] = []
100
+ let m = new THREE.Matrix4()
101
+ let q = new THREE.Quaternion()
102
+ let s = new THREE.Vector3()
103
+ let spin = new THREE.Euler()
104
+ let tint = new THREE.Color()
105
+ return {
106
+ /** `n` bits from `at`, flung up to `speed` metres a second */
107
+ emit: (
108
+ at: THREE.Vector3,
109
+ color: THREE.ColorRepresentation,
110
+ n: number,
111
+ o: Emission = {},
112
+ ) => {
113
+ let c = new THREE.Color(color)
114
+ for (let i = 0; i < n; i++) {
115
+ if (live.length >= most) live.shift()
116
+ let a = Math.random() * Math.PI * 2,
117
+ sp = (o.speed ?? 3) * (0.4 + Math.random() * 0.6),
118
+ flame = !!o.flame
119
+ let p = at.clone()
120
+ if (flame) {
121
+ p.add(
122
+ new THREE.Vector3(
123
+ Math.cos(a) * Math.random() * 0.18,
124
+ 0,
125
+ Math.sin(a) * Math.random() * 0.18,
126
+ ),
127
+ )
128
+ }
129
+ live.push({
130
+ p,
131
+ v: new THREE.Vector3(
132
+ Math.cos(a) * sp * (flame ? 0.15 : 1),
133
+ (o.up ?? 3) *
134
+ (flame ? 0.8 + Math.random() * 0.6 : 0.5 + Math.random()),
135
+ Math.sin(a) * sp * (flame ? 0.15 : 1),
136
+ ),
137
+ born: performance.now(),
138
+ life: (o.life ?? 0.7) * (0.7 + Math.random() * 0.6),
139
+ size: (o.size ?? 0.12) * (0.7 + Math.random() * 0.6),
140
+ fall: o.fall ?? (flame ? -0.4 : 12),
141
+ color: flame
142
+ ? new THREE.Color(
143
+ i % 3 == 0 ? 0xffe28a : i % 3 == 1 ? 0xffa334 : 0xff5525,
144
+ )
145
+ : c.clone().offsetHSL(0, 0, (Math.random() - 0.5) * 0.12),
146
+ halo: !!o.halo || (flame && i % 3 == 0),
147
+ flame,
148
+ })
149
+ }
150
+ },
151
+ tick: (dt: number, camera?: THREE.Camera) => {
152
+ let t = performance.now()
153
+ live = live.filter((b) => (t - b.born) / 1000 < b.life)
154
+ let cubes = 0, tongues = 0
155
+ for (let i = live.length - 1; i >= 0; i--) {
156
+ let b = live[i]
157
+ b.v.y -= b.fall * dt
158
+ b.p.addScaledVector(b.v, dt)
159
+ let k = 1 - (t - b.born) / 1000 / b.life
160
+ spin.set(
161
+ b.flame ? 0 : b.born % 7 + t * 0.003,
162
+ b.flame ? 0 : b.born % 5 + t * 0.002,
163
+ b.flame ? Math.sin(t * 0.012 + b.born) * 0.2 : 0,
164
+ )
165
+ q.setFromEuler(spin)
166
+ let fade = Math.min(1, k * 2.5)
167
+ s.setScalar(b.size * fade)
168
+ if (b.flame) s.y *= 2.8
169
+ m.compose(b.p, q, s)
170
+ if (b.flame) {
171
+ if (flames && tongues < flames.instanceMatrix.count) {
172
+ flames.setMatrixAt(tongues, m)
173
+ flames.setColorAt(tongues++, b.color)
174
+ }
175
+ } else {
176
+ mesh.setMatrixAt(cubes, m)
177
+ mesh.setColorAt(cubes++, b.color)
178
+ }
179
+ }
180
+ mesh.count = cubes
181
+ mesh.instanceMatrix.needsUpdate = true
182
+ if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true
183
+ if (flames) {
184
+ flames.count = tongues
185
+ flames.instanceMatrix.needsUpdate = true
186
+ if (flames.instanceColor) flames.instanceColor.needsUpdate = true
187
+ }
188
+ if (halos) {
189
+ let shown = 0
190
+ q.copy(camera?.quaternion ?? mesh.quaternion)
191
+ for (
192
+ let i = live.length - 1;
193
+ i >= 0 && shown < halos.instanceMatrix.count;
194
+ i--
195
+ ) {
196
+ let b = live[i]
197
+ if (!b.halo) continue
198
+ let k = 1 - (t - b.born) / 1000 / b.life
199
+ s.setScalar(b.size * (b.flame ? 5 : 4) * Math.min(1, k * 2.5))
200
+ m.compose(b.p, q, s)
201
+ halos.setMatrixAt(shown, m)
202
+ halos.setColorAt(shown++, tint.copy(b.color).multiplyScalar(k))
203
+ }
204
+ halos.count = shown
205
+ halos.instanceMatrix.needsUpdate = true
206
+ if (halos.instanceColor) halos.instanceColor.needsUpdate = true
207
+ }
208
+ },
209
+ }
210
+ }
211
+
212
+ /** Whether a box on the screen, `w` by `h` from its top left at `x`, `y`,
213
+ * lies under the glass, where the scene's labels do not show. */
214
+ export type Under = (x: number, y: number, w: number, h: number) => boolean
215
+
216
+ export type Overlay = {
217
+ float: (text: string, p: THREE.Vector3, kind: string) => void
218
+ plate: (key: string, p: THREE.Vector3, html: string, cls?: string) => void
219
+ tick: () => void
220
+ }
221
+
222
+ /** The labels over the scene: plates that follow someone, and numbers that
223
+ * float up and fade. A label that would be `under` the glass is not shown. */
224
+ export let overlay = (
225
+ layer: HTMLElement,
226
+ camera: THREE.Camera,
227
+ under: Under = () => false,
228
+ ): Overlay => {
229
+ let at = new THREE.Vector3()
230
+ let floats: { el: HTMLElement; p: THREE.Vector3; born: number }[] = []
231
+ // Sizes arrive after layout, so drawing a plate never asks for layout.
232
+ type Plate = {
233
+ el: HTMLElement
234
+ body: HTMLElement
235
+ html: string
236
+ seen: boolean
237
+ w: number
238
+ h: number
239
+ }
240
+ let plates = new Map<string, Plate>()
241
+ let byBody = new WeakMap<Element, Plate>()
242
+ let sized = new ResizeObserver((entries) => {
243
+ for (let entry of entries) {
244
+ let pl = byBody.get(entry.target)
245
+ if (!pl) continue
246
+ pl.w = entry.borderBoxSize[0]?.inlineSize ?? entry.contentRect.width
247
+ pl.h = entry.borderBoxSize[0]?.blockSize ?? entry.contentRect.height
248
+ }
249
+ })
250
+ let screen = (p: THREE.Vector3): [number, number] | null => {
251
+ at.copy(p).project(camera)
252
+ if (at.z > 1 || at.x < -1.2 || at.x > 1.2 || at.y < -1.2 || at.y > 1.2) {
253
+ return null
254
+ }
255
+ return [(at.x + 1) / 2 * innerWidth, (1 - at.y) / 2 * innerHeight]
256
+ }
257
+ // Put `el` over `p`, unless `p` is off the screen or what `el` shows, `w`
258
+ // by `h` and standing on the point, would be under the glass.
259
+ let place = (el: HTMLElement, p: THREE.Vector3, w = 0, h = 0) => {
260
+ let s = screen(p)
261
+ let shown = !!s && !under(s[0] - w / 2, s[1] - h, w, h)
262
+ // Keep plates laid out while occluded: a display:none plate measures 0x0
263
+ // in ResizeObserver, so its next frame tests a different overlap and
264
+ // alternates between hidden and shown at the edge of the glass.
265
+ el.style.visibility = shown ? '' : 'hidden'
266
+ if (s && shown) {
267
+ el.style.transform = `translate(${s[0].toFixed(1)}px, ${
268
+ s[1].toFixed(1)
269
+ }px)`
270
+ }
271
+ }
272
+ return {
273
+ float: (text: string, p: THREE.Vector3, kind: string) => {
274
+ let el = document.createElement('div')
275
+ el.className = `Float Float-${kind}`
276
+ el.textContent = text
277
+ layer.append(el)
278
+ let jitter = new THREE.Vector3(
279
+ (Math.random() - 0.5) * 0.6,
280
+ 0,
281
+ (Math.random() - 0.5) * 0.6,
282
+ )
283
+ floats.push({ el, p: p.clone().add(jitter), born: performance.now() })
284
+ },
285
+ /** keep a plate over `p` this frame, holding `html` */
286
+ plate: (key: string, p: THREE.Vector3, html: string, cls = 'Plate') => {
287
+ let pl = plates.get(key)
288
+ if (!pl) {
289
+ let el = document.createElement('div')
290
+ el.className = cls
291
+ let body = document.createElement('div')
292
+ body.className = 'Plate_In'
293
+ el.append(body)
294
+ layer.append(el)
295
+ pl = { el, body, html: '', seen: true, w: 0, h: 0 }
296
+ plates.set(key, pl)
297
+ byBody.set(body, pl)
298
+ sized.observe(body)
299
+ }
300
+ if (pl.html != html) {
301
+ pl.body.innerHTML = html
302
+ pl.html = html
303
+ }
304
+ pl.seen = true
305
+ place(pl.el, p, pl.w, pl.h)
306
+ },
307
+ tick: () => {
308
+ let t = performance.now()
309
+ floats = floats.filter((f) => {
310
+ let age = (t - f.born) / 1000
311
+ if (age > 1.1) {
312
+ f.el.remove()
313
+ return false
314
+ }
315
+ f.p.y += 0.018
316
+ place(f.el, f.p)
317
+ f.el.style.opacity = String(Math.min(1, (1.1 - age) * 3))
318
+ return true
319
+ })
320
+ for (let [key, pl] of plates) {
321
+ if (!pl.seen) {
322
+ sized.unobserve(pl.body)
323
+ pl.el.remove()
324
+ plates.delete(key)
325
+ }
326
+ pl.seen = false
327
+ }
328
+ },
329
+ }
330
+ }
package/halo.js ADDED
@@ -0,0 +1,22 @@
1
+ // One soft radial texture for lamps and other glowing objects. The scene makes coloured, additive sprites from this shared map.
2
+ // @ts-types="npm:@types/three@^0.186.0"
3
+ import * as THREE from "three";
4
+ let map;
5
+ export let halo = ()=>{
6
+ if (map) return map;
7
+ let size = 64, pixels = new Uint8Array(size * size * 4);
8
+ for(let y = 0; y < size; y++){
9
+ for(let x = 0; x < size; x++){
10
+ let r = Math.min(1, Math.hypot(x - 31.5, y - 31.5) / 31.5);
11
+ let alpha = r < 0.35 ? 0.9 - 0.55 * r / 0.35 : 0.35 * (1 - r) / 0.65;
12
+ let i = (y * size + x) * 4;
13
+ pixels[i] = pixels[i + 1] = pixels[i + 2] = 255;
14
+ pixels[i + 3] = Math.round(255 * alpha);
15
+ }
16
+ }
17
+ map = new THREE.DataTexture(pixels, size, size, THREE.RGBAFormat);
18
+ map.minFilter = map.magFilter = THREE.LinearFilter;
19
+ map.needsUpdate = true;
20
+ return map;
21
+ };
22
+ //# sourceMappingURL=halo.js.map
package/halo.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"halo.js","sources":["./halo.ts"],"names":[],"mappings":"AAAA,gIAAgI;AAChI,wCAAwC;AACxC,YAAY,mBAA+B;AAE3C,IAAI;AAEJ,OAAO,IAAI,OAAO;EAChB,IAAI,KAAK,OAAO;EAChB,IAAI,OAAO,IAAI,SAAS,IAAI,WAAW,OAAO,OAAO;EACrD,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,IAAK;IAC7B,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,IAAK;MAC7B,IAAI,IAAI,KAAK,GAAG,CAAC,GAAG,KAAK,KAAK,CAAC,IAAI,MAAM,IAAI,QAAQ;MACrD,IAAI,QAAQ,IAAI,OAAO,MAAM,OAAO,IAAI,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI;MAChE,IAAI,IAAI,CAAC,IAAI,OAAO,CAAC,IAAI;MACzB,MAAM,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,GAAG;MAC5C,MAAM,CAAC,IAAI,EAAE,GAAG,KAAK,KAAK,CAAC,MAAM;IACnC;EACF;EACA,MAAM,IAAI,MAAM,WAAW,CAAC,QAAQ,MAAM,MAAM,MAAM,UAAU;EAChE,IAAI,SAAS,GAAG,IAAI,SAAS,GAAG,MAAM,YAAY;EAClD,IAAI,WAAW,GAAG;EAClB,OAAO;AACT,EAAC"}
package/halo.ts ADDED
@@ -0,0 +1,23 @@
1
+ // One soft radial texture for lamps and other glowing objects. The scene makes coloured, additive sprites from this shared map.
2
+ // @ts-types="npm:@types/three@^0.186.0"
3
+ import * as THREE from 'three'
4
+
5
+ let map: THREE.DataTexture | undefined
6
+
7
+ export let halo = (): THREE.DataTexture => {
8
+ if (map) return map
9
+ let size = 64, pixels = new Uint8Array(size * size * 4)
10
+ for (let y = 0; y < size; y++) {
11
+ for (let x = 0; x < size; x++) {
12
+ let r = Math.min(1, Math.hypot(x - 31.5, y - 31.5) / 31.5)
13
+ let alpha = r < 0.35 ? 0.9 - 0.55 * r / 0.35 : 0.35 * (1 - r) / 0.65
14
+ let i = (y * size + x) * 4
15
+ pixels[i] = pixels[i + 1] = pixels[i + 2] = 255
16
+ pixels[i + 3] = Math.round(255 * alpha)
17
+ }
18
+ }
19
+ map = new THREE.DataTexture(pixels, size, size, THREE.RGBAFormat)
20
+ map.minFilter = map.magFilter = THREE.LinearFilter
21
+ map.needsUpdate = true
22
+ return map
23
+ }