@margoengine/points 0.0.0-stage → 1.0.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 (62) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +876 -2
  4. package/dist/attributes-DNc-1pdr.js +256 -0
  5. package/dist/attributes.d.ts +48 -0
  6. package/dist/clip.d.ts +55 -0
  7. package/dist/clip.js +122 -0
  8. package/dist/cloud-B4O_G-Df.js +545 -0
  9. package/dist/cloud.d.ts +155 -0
  10. package/dist/color.d.ts +1 -0
  11. package/dist/copc-write.d.ts +95 -0
  12. package/dist/copc-write.js +318 -0
  13. package/dist/copc.d.ts +55 -0
  14. package/dist/copc.js +127 -0
  15. package/dist/depth-video.d.ts +98 -0
  16. package/dist/depth-video.js +164 -0
  17. package/dist/e57.d.ts +163 -0
  18. package/dist/e57.js +624 -0
  19. package/dist/frame.d.ts +14 -0
  20. package/dist/index.d.ts +16 -0
  21. package/dist/index.js +1246 -0
  22. package/dist/las.d.ts +213 -0
  23. package/dist/las.js +500 -0
  24. package/dist/laz-encode-CHXgHra_.js +446 -0
  25. package/dist/laz-encode.d.ts +14 -0
  26. package/dist/laz-model-DCphHLqH.js +385 -0
  27. package/dist/laz-model.d.ts +47 -0
  28. package/dist/laz-write.d.ts +46 -0
  29. package/dist/laz-write.js +75 -0
  30. package/dist/laz.d.ts +50 -0
  31. package/dist/laz.js +645 -0
  32. package/dist/material.d.ts +167 -0
  33. package/dist/math.d.ts +103 -0
  34. package/dist/octree.d.ts +182 -0
  35. package/dist/octree.js +360 -0
  36. package/dist/pick.d.ts +76 -0
  37. package/dist/pick.js +192 -0
  38. package/dist/plugin.d.ts +235 -0
  39. package/dist/potree-las-RC8sjbYU.js +1 -0
  40. package/dist/potree-las.d.ts +1 -0
  41. package/dist/potree.d.ts +70 -0
  42. package/dist/potree.js +451 -0
  43. package/dist/range-BQuJAjUM.js +23 -0
  44. package/dist/range.d.ts +4 -0
  45. package/dist/screen-door-CALlYs39.js +7 -0
  46. package/dist/shaders-DCnbvb0-.js +282 -0
  47. package/dist/shaders.d.ts +52 -0
  48. package/dist/shared/fog.d.ts +33 -0
  49. package/dist/shared/pass.d.ts +53 -0
  50. package/dist/shared/selection-mask.d.ts +52 -0
  51. package/dist/shared/style-expression.d.ts +18 -0
  52. package/dist/sprites.d.ts +78 -0
  53. package/dist/storage-limit.d.ts +6 -0
  54. package/dist/style.d.ts +124 -0
  55. package/dist/style.js +709 -0
  56. package/dist/surfels.d.ts +95 -0
  57. package/dist/surfels.js +538 -0
  58. package/dist/survey-DToZwkJv.js +46 -0
  59. package/dist/survey.d.ts +10 -0
  60. package/dist/tier-BcFbLPVT.js +91 -0
  61. package/dist/tier.d.ts +6 -0
  62. package/package.json +102 -4
package/dist/octree.js ADDED
@@ -0,0 +1,360 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { n as e, t } from "./cloud-B4O_G-Df.js";
5
+ import { MargoError as n } from "@margoengine/math/errors";
6
+ import { meshWorldMatrix as r } from "@margoengine/math/mesh";
7
+ //#region src/octree.ts
8
+ var i = class {
9
+ constructor(e, t, n, r, i = 3, a = {}, o) {
10
+ this.positions = n, this.colors = r, this.colorComponents = i, this.attributes = a, this.rowIds = o, this.root = e, this.indices = t;
11
+ }
12
+ *nodes() {
13
+ let e = [this.root];
14
+ for (let t = 0; t < e.length; t++) {
15
+ yield e[t];
16
+ for (let n of e[t].children) n && e.push(n);
17
+ }
18
+ }
19
+ load(e) {
20
+ let t = this.indices.get(e);
21
+ if (!t) throw TypeError(`Node ${e.id} is not part of this octree; pass a node reached from this octree's root`);
22
+ let n = new Float32Array(t.length * 3), r = this.colorComponents, i = this.colors ? this.colors instanceof Uint8Array || this.colors instanceof Uint8ClampedArray ? new Uint8Array(t.length * r) : new Float32Array(t.length * r) : null;
23
+ for (let e = 0; e < t.length; e++) {
24
+ let a = t[e];
25
+ if (n[e * 3] = this.positions[a * 3], n[e * 3 + 1] = this.positions[a * 3 + 1], n[e * 3 + 2] = this.positions[a * 3 + 2], i) for (let t = 0; t < r; t++) i[e * r + t] = this.colors[a * r + t];
26
+ }
27
+ let a = {};
28
+ for (let [e, n] of Object.entries(this.attributes)) {
29
+ let r = n.length / (this.positions.length / 3), i = new n.constructor(t.length * r);
30
+ for (let e = 0; e < t.length; e++) for (let a = 0; a < r; a++) i[e * r + a] = n[t[e] * r + a];
31
+ a[e] = i;
32
+ }
33
+ let o = this.rowIds ? Array.from(t, (e) => this.rowIds[e]) : t;
34
+ return {
35
+ positions: n,
36
+ colors: i,
37
+ colorComponents: i ? r : void 0,
38
+ attributes: a,
39
+ rowIds: o
40
+ };
41
+ }
42
+ };
43
+ function a(e, t = {}) {
44
+ let { positions: n } = e, r = n.length / 3, a = t.maxPointsPerNode ?? 2e4, o = t.grid ?? 128, s = t.maxDepth ?? 16;
45
+ if (!Number.isInteger(r)) throw RangeError("positions length must be a multiple of 3");
46
+ for (let [t, n] of Object.entries(e.attributes ?? {})) if (r ? !Number.isInteger(n.length / r) || n.length < r : n.length !== 0) throw RangeError(`Attribute ${t} must have a whole number of components per point`);
47
+ if (e.rowIds && e.rowIds.length !== r) throw RangeError("rowIds must have one key per point");
48
+ if (!(a >= 1)) throw RangeError("Invalid maxPointsPerNode; pass a number of 1 or more");
49
+ if (!Number.isInteger(s) || s < 0 || s > 64) throw RangeError("Invalid maxDepth; pass a whole number from 0 to 64");
50
+ if (!Number.isInteger(o) || o < 1 || o > 1024) throw RangeError("Invalid grid; pass a whole number from 1 to 1024");
51
+ let c = e.colorComponents ?? (e.colors && r ? e.colors.length / r : 3);
52
+ if (e.colors && c !== 3 && c !== 4) throw RangeError("colors must have 3 or 4 components per point");
53
+ let l = [
54
+ Infinity,
55
+ Infinity,
56
+ Infinity
57
+ ], u = [
58
+ -Infinity,
59
+ -Infinity,
60
+ -Infinity
61
+ ];
62
+ for (let e = 0; e < r * 3; e++) {
63
+ let t = n[e];
64
+ if (!Number.isFinite(t)) throw RangeError(`Point ${Math.floor(e / 3)} is not finite; remove or fix it before building`);
65
+ t < l[e % 3] && (l[e % 3] = t), t > u[e % 3] && (u[e % 3] = t);
66
+ }
67
+ r || (l.fill(0), u.fill(0));
68
+ let d = Math.max(u[0] - l[0], u[1] - l[1], u[2] - l[2]) || 1, f = new Uint32Array(r);
69
+ for (let e = 0; e < r; e++) f[e] = e;
70
+ let p = (t.seed ?? 1) >>> 0, m = () => {
71
+ p = p + 1831565813 >>> 0;
72
+ let e = p;
73
+ return e = Math.imul(e ^ e >>> 15, e | 1), e ^= e + Math.imul(e ^ e >>> 7, e | 61), ((e ^ e >>> 14) >>> 0) / 4294967296;
74
+ };
75
+ for (let e = r - 1; e > 0; e--) {
76
+ let t = Math.floor(m() * (e + 1)), n = f[e];
77
+ f[e] = f[t], f[t] = n;
78
+ }
79
+ let h = /* @__PURE__ */ new Map(), g = new Uint32Array(o * o * o), _ = 0, v = (e, t, n, r, i, a) => ({
80
+ id: e,
81
+ level: t,
82
+ bounds: Float64Array.of(n[0], n[1], n[2], n[0] + r, n[1] + r, n[2] + r),
83
+ spacing: r / o,
84
+ count: i,
85
+ parent: a,
86
+ children: [
87
+ null,
88
+ null,
89
+ null,
90
+ null,
91
+ null,
92
+ null,
93
+ null,
94
+ null
95
+ ]
96
+ }), y = v("r", 0, l, d, 0, null), b = [{
97
+ node: y,
98
+ points: f
99
+ }];
100
+ for (; b.length;) {
101
+ let { node: e, points: t } = b.pop(), r = e.bounds, i = r[3] - r[0];
102
+ if (t.length <= a || e.level >= s) {
103
+ h.set(e, t), e.count = t.length;
104
+ continue;
105
+ }
106
+ _++;
107
+ let c = [], l = [
108
+ [],
109
+ [],
110
+ [],
111
+ [],
112
+ [],
113
+ [],
114
+ [],
115
+ []
116
+ ], u = r[0] + i / 2, d = r[1] + i / 2, f = r[2] + i / 2, p = o / i;
117
+ for (let e = 0; e < t.length; e++) {
118
+ let i = t[e], a = n[i * 3], s = n[i * 3 + 1], m = n[i * 3 + 2], h = Math.min(o - 1, Math.floor((a - r[0]) * p)), v = Math.min(o - 1, Math.floor((s - r[1]) * p)), y = (Math.min(o - 1, Math.floor((m - r[2]) * p)) * o + v) * o + h;
119
+ g[y] === _ ? l[a >= u | (s >= d ? 2 : 0) | (m >= f ? 4 : 0)].push(i) : (g[y] = _, c.push(i));
120
+ }
121
+ h.set(e, Uint32Array.from(c)), e.count = c.length;
122
+ for (let t = 0; t < 8; t++) {
123
+ if (!l[t].length) continue;
124
+ let n = i / 2, a = [
125
+ r[0] + (t & 1 ? n : 0),
126
+ r[1] + (t & 2 ? n : 0),
127
+ r[2] + (t & 4 ? n : 0)
128
+ ], o = v(e.id + t, e.level + 1, a, n, 0, e);
129
+ e.children[t] = o, b.push({
130
+ node: o,
131
+ points: Uint32Array.from(l[t])
132
+ });
133
+ }
134
+ }
135
+ return new i(y, h, n, e.colors, c, e.attributes, e.rowIds);
136
+ }
137
+ var o = 5e5;
138
+ function s(e) {
139
+ let t = Math.max(e.spacing, (e.bounds[3] - e.bounds[0]) / Math.sqrt(Math.max(e.count, 1)));
140
+ return 1 / (t * t);
141
+ }
142
+ function c(e) {
143
+ let t = 0;
144
+ for (let n = e; n; n = n.parent) t += s(n);
145
+ return t;
146
+ }
147
+ var l = (e, t) => e.length === t.length && e.every((e, n) => e === t[n]), u = 0, d = class i {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
148
+ static attach(e, t, n, r) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
149
+ let a = new i(t, n, r);
150
+ return e.use(a), a;
151
+ }
152
+ constructor(t, n, r = {}) {
153
+ this.points = t, this.matrix = new Float32Array([
154
+ 1,
155
+ 0,
156
+ 0,
157
+ 0,
158
+ 0,
159
+ 1,
160
+ 0,
161
+ 0,
162
+ 0,
163
+ 0,
164
+ 1,
165
+ 0,
166
+ 0,
167
+ 0,
168
+ 0,
169
+ 1
170
+ ]), this.visible = [], this.stats = {
171
+ drawn: 0,
172
+ loaded: 0,
173
+ loading: 0,
174
+ failed: 0
175
+ }, this.disposed = !1, this.mesh = null, this.localPlanes = /* @__PURE__ */ new Float64Array(24), this.frame = 0, this.loaded = /* @__PURE__ */ new Map(), this.failed = /* @__PURE__ */ new Set(), this.sizeScale = 0, this.sizeFloor = 0, this.sizesPending = !1, this.name = `octree-cloud-${++u}`, this.source = n, this.pointBudget = r.pointBudget ?? 2e6, this.minNodePixels = r.minNodePixels ?? 150, this.maxLoadedPoints = r.maxLoadedPoints ?? this.pointBudget * 3;
176
+ let i = r.maxConcurrentLoads === void 0 ? 4 : r.maxConcurrentLoads, a = "Invalid OctreeCloud maxConcurrentLoads; pass a positive safe integer; see https://margoengine.com/errors#points";
177
+ if (typeof i != "number") throw TypeError(a);
178
+ if (!Number.isSafeInteger(i) || i <= 0) throw RangeError(a);
179
+ if (this.maxConcurrent = i, !(this.pointBudget > 0) || !(this.minNodePixels > 0) || !(this.maxLoadedPoints > 0)) throw RangeError("Invalid OctreeCloud option; pointBudget, minNodePixels and maxLoadedPoints must be above 0");
180
+ let o = r.material instanceof e ? r.material : new e(r.material);
181
+ if (this.material = o, this.cloudOptions = r.cloud, r.adaptiveSize) {
182
+ let e = r.adaptiveSize === !0 ? 1.5 : r.adaptiveSize.scale ?? 1.5;
183
+ if (!(e > 0) || !Number.isFinite(e)) throw RangeError("Invalid adaptiveSize.scale; pass a finite number above 0");
184
+ this.sizeScale = e, o.sizeUnits === "world" ? this.sizeFloor = o.size : o.minPixelSize = Math.min(Math.max(o.minPixelSize, o.size), o.maxPixelSize), o.sizeUnits = "world", o.size = 1;
185
+ }
186
+ }
187
+ setup(e) {
188
+ this.engine = e;
189
+ }
190
+ get attachedMesh() {
191
+ return this.mesh;
192
+ }
193
+ attach(e) {
194
+ this.mesh = e;
195
+ for (let t of this.loaded.values()) t.cloud?.attach(e);
196
+ return this;
197
+ }
198
+ onError(e) {
199
+ this.errorListener = e;
200
+ }
201
+ retry() {
202
+ this.failed.clear(), this.stats.failed = 0;
203
+ }
204
+ onPreUpdate() {
205
+ let e = this.engine;
206
+ e && !this.disposed && this.select({
207
+ position: e.camera.position,
208
+ planes: e.camera.planes,
209
+ screenHeight: e.canvas.height || 1,
210
+ fov: e.camera.fov
211
+ });
212
+ }
213
+ select(e) {
214
+ if (this.disposed) throw new n("disposed", "OctreeCloud is disposed; create a new one");
215
+ let t = this.mesh && this.engine ? r(this.engine, this.mesh) : this.matrix;
216
+ if (!t) return this.visible;
217
+ this.frame++;
218
+ let i = Math.max(Math.hypot(t[0], t[1], t[2]), Math.hypot(t[4], t[5], t[6]), Math.hypot(t[8], t[9], t[10]));
219
+ this.sizeScale && (this.material.size = i);
220
+ let a = e.screenHeight / (2 * Math.tan((e.fov ?? 60) * Math.PI / 180 / 2)), o = e.position, s = e.planes && this.planesInCloud(e.planes, t), c = (e) => {
221
+ let n = e.bounds, r = Math.hypot(n[3] - n[0], n[4] - n[1], n[5] - n[2]) / 2 * i, s = (n[0] + n[3]) / 2, c = (n[1] + n[4]) / 2, l = (n[2] + n[5]) / 2, u = Math.hypot(t[0] * s + t[4] * c + t[8] * l + t[12] - o[0], t[1] * s + t[5] * c + t[9] * l + t[13] - o[1], t[2] * s + t[6] * c + t[10] * l + t[14] - o[2]);
222
+ return u <= r ? Infinity : r * a / (u - r);
223
+ }, u = (e) => {
224
+ if (!s) return !0;
225
+ let t = e.bounds;
226
+ for (let e = 0; e < 6; e++) {
227
+ let n = s[e * 4], r = s[e * 4 + 1], i = s[e * 4 + 2], a = s[e * 4 + 3];
228
+ if (n * (n >= 0 ? t[3] : t[0]) + r * (r >= 0 ? t[4] : t[1]) + i * (i >= 0 ? t[5] : t[2]) + a < 0) return !1;
229
+ }
230
+ return !0;
231
+ }, d = [], f = [], p = [{
232
+ node: this.source.root,
233
+ size: c(this.source.root)
234
+ }], m = 0;
235
+ for (; p.length;) {
236
+ let e = 0;
237
+ for (let t = 1; t < p.length; t++) p[t].size > p[e].size && (e = t);
238
+ let { node: t, size: n } = p[e];
239
+ if (p[e] = p[p.length - 1], p.pop(), !u(t) || this.failed.has(t)) continue;
240
+ if (m + t.count > this.pointBudget) break;
241
+ let r = this.loaded.get(t);
242
+ if (!r?.cloud) f.push({
243
+ node: t,
244
+ size: n
245
+ });
246
+ else if (r.used = this.frame, d.push(t), m += t.count, n > this.minNodePixels) for (let e of t.children) e && p.push({
247
+ node: e,
248
+ size: c(e)
249
+ });
250
+ }
251
+ let h = new Set(d);
252
+ for (let [e, t] of this.loaded) t.cloud && (t.cloud.visible = h.has(e));
253
+ return this.sizeScale && (this.sizesPending || !l(d, this.visible)) && this.fitSizes(d, h), this.visible = d, this.stats.drawn = m, this.request(f), this.evict(), d;
254
+ }
255
+ planesInCloud(e, t) {
256
+ let n = this.localPlanes;
257
+ for (let r = 0; r < 24; r += 4) {
258
+ let i = e[r], a = e[r + 1], o = e[r + 2];
259
+ n[r] = i * t[0] + a * t[1] + o * t[2], n[r + 1] = i * t[4] + a * t[5] + o * t[6], n[r + 2] = i * t[8] + a * t[9] + o * t[10], n[r + 3] = i * t[12] + a * t[13] + o * t[14] + e[r + 3];
260
+ }
261
+ return n;
262
+ }
263
+ pointSize(e) {
264
+ return Math.max(this.sizeFloor, this.sizeScale / Math.sqrt(e));
265
+ }
266
+ fitSizes(e, t) {
267
+ let n = /* @__PURE__ */ new Map(), r = (e) => {
268
+ let i = "";
269
+ for (let n = 0; n < 8; n++) {
270
+ let a = e.children[n];
271
+ a && t.has(a) && (i += `${n}(${r(a)})`);
272
+ }
273
+ return n.set(e, i), i;
274
+ };
275
+ for (let n of e) (!n.parent || !t.has(n.parent)) && r(n);
276
+ let i = o;
277
+ this.sizesPending = !1;
278
+ for (let r of e) {
279
+ let e = this.loaded.get(r), a = n.get(r);
280
+ if (e.shape !== a) {
281
+ if (i <= 0) {
282
+ this.sizesPending = !0;
283
+ break;
284
+ }
285
+ i -= r.count, e.shape = a, this.sizeNode(r, e.cloud, t);
286
+ }
287
+ }
288
+ }
289
+ sizeNode(e, t, n) {
290
+ let { positions: r, sizes: i } = t, a = c(e);
291
+ if (i) {
292
+ for (let o = 0; o < t.count; o++) {
293
+ let t = r[o * 3], c = r[o * 3 + 1], l = r[o * 3 + 2], u = a, d = e;
294
+ for (;;) {
295
+ let e = d.bounds, r = +(t >= (e[0] + e[3]) / 2) | (c >= (e[1] + e[4]) / 2 ? 2 : 0) | (l >= (e[2] + e[5]) / 2 ? 4 : 0), i = d.children[r];
296
+ if (!i || !n.has(i)) break;
297
+ u += s(i), d = i;
298
+ }
299
+ i[o] = this.pointSize(u);
300
+ }
301
+ t.markDirty("sizes");
302
+ }
303
+ }
304
+ request(e) {
305
+ let t = new Set(e.map((e) => e.node));
306
+ for (let [e, n] of this.loaded) !n.cloud && n.controller && !t.has(e) && (n.controller.abort(new DOMException("Node no longer needed", "AbortError")), this.loaded.delete(e), this.stats.loading--);
307
+ e.sort((e, t) => t.size - e.size);
308
+ for (let { node: t } of e) {
309
+ if (this.stats.loading >= this.maxConcurrent) break;
310
+ if (this.loaded.has(t)) continue;
311
+ let e = new AbortController(), n = {
312
+ controller: e,
313
+ used: this.frame,
314
+ points: 0
315
+ };
316
+ this.loaded.set(t, n), this.stats.loading++, this.load(t, n, e.signal);
317
+ }
318
+ }
319
+ async load(e, n, r) {
320
+ try {
321
+ let i = await this.source.load(e, r);
322
+ if (r.throwIfAborted(), this.disposed) return;
323
+ let a = new t({
324
+ ...this.cloudOptions,
325
+ ...i,
326
+ material: this.material,
327
+ matrix: this.matrix,
328
+ sizes: this.sizeScale ? new Float32Array(i.positions.length / 3).fill(this.pointSize(c(e))) : void 0
329
+ });
330
+ a.visible = !1, a.attach(this.mesh), this.points.add(a), n.cloud = a, n.points = a.count, n.shape = "", this.stats.loaded += a.count;
331
+ } catch (t) {
332
+ if (r.aborted) return;
333
+ this.loaded.delete(e), this.failed.add(e), this.stats.failed++, this.errorListener ? this.errorListener(e, t) : this.engine?.onError(t);
334
+ } finally {
335
+ r.aborted || this.stats.loading--, n.controller = void 0;
336
+ }
337
+ }
338
+ evict() {
339
+ if (this.stats.loaded <= this.maxLoadedPoints) return;
340
+ let e = [...this.loaded].filter(([, e]) => e.cloud && e.used !== this.frame);
341
+ e.sort(([e, t], [n, r]) => t.used - r.used || n.level - e.level);
342
+ for (let [t, n] of e) {
343
+ if (this.stats.loaded <= this.maxLoadedPoints) break;
344
+ this.points.remove(n.cloud), this.stats.loaded -= n.points, this.loaded.delete(t);
345
+ }
346
+ }
347
+ dispose() {
348
+ if (this.disposed) return;
349
+ this.disposed = !0;
350
+ for (let e of this.loaded.values()) e.controller?.abort(new DOMException("OctreeCloud disposed", "AbortError")), e.cloud && this.points.remove(e.cloud);
351
+ this.loaded.clear();
352
+ let e = this.engine;
353
+ this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
354
+ }
355
+ [Symbol.dispose]() {
356
+ this.dispose();
357
+ }
358
+ };
359
+ //#endregion
360
+ export { i as MemoryOctree, d as OctreeCloud, a as buildOctree };
package/dist/pick.d.ts ADDED
@@ -0,0 +1,76 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { Camera, Mesh } from '@margoengine/core';
3
+ import type { PointCloud } from './cloud.js';
4
+ import type { PointsPlugin } from './plugin.js';
5
+ /** What `Picking` hands an overlay for one pick. */
6
+ export interface PointPickContext {
7
+ encoder: GPUCommandEncoder;
8
+ /** The r32uint pick tile, 3 x 3 pixels around the picked pixel. */
9
+ color: GPUTextureView;
10
+ /** Its depth32float depth, holding the meshes already drawn. */
11
+ depth: GPUTextureView;
12
+ /** The camera's view-projection matrix, cropped to the tile. */
13
+ matrix: Float32Array;
14
+ camera: Camera;
15
+ /** The picked view's rectangle in drawing-buffer pixels. */
16
+ rect: readonly number[];
17
+ /** The ID of this overlay's item 0. */
18
+ firstId: number;
19
+ }
20
+ /** A point hit: `hit.mesh` is the mesh the cloud follows, or an empty stand-in mesh (see `cloudOf`). */
21
+ export interface PointPickHit {
22
+ mesh: Mesh;
23
+ /** The cloud the point belongs to. */
24
+ cloud: PointCloud;
25
+ /** Index of the point in the cloud's arrays. */
26
+ point: number;
27
+ /** World position of the point. */
28
+ position: readonly [number, number, number];
29
+ }
30
+ /** What `PointPicking.attach` needs of `@margoengine/selection`'s Picking. */
31
+ export interface PointPickHost {
32
+ addOverlay(overlay: {
33
+ drawPick(context: PointPickContext): void;
34
+ pickHit(index: number): PointPickHit | null;
35
+ }): () => void;
36
+ }
37
+ /** The point shader with each point's slot carried to the fragment, and fragments that write IDs. */
38
+ export declare function pointPickShader(): string;
39
+ /**
40
+ * Makes the points of a `PointsPlugin` pickable by `@margoengine/selection`'s `Picking`, and so by `engine.pick` once
41
+ * Picking is attached. Points and meshes share one depth test, so a mesh in front blocks the pick; parts cut by clipping
42
+ * planes do not hit. The picked points are those last drawn in that view, at their level of detail and transform.
43
+ * `hit.cloud`, `hit.point` and `hit.position` name what was hit; `hit.mesh` is the mesh the cloud follows or else an
44
+ * empty stand-in mesh, as `engine.pick` can only answer meshes (`cloudOf` maps it back).
45
+ */
46
+ export declare class PointPicking {
47
+ readonly points: PointsPlugin;
48
+ private gpu?;
49
+ private entries;
50
+ private readonly standIns;
51
+ private readonly owners;
52
+ private readonly frameData;
53
+ private readonly view;
54
+ private remove?;
55
+ /**
56
+ * Registers `points` with `picking`; undo with `dispose`.
57
+ * @throws MargoError `state` when `points` is not attached to an engine yet.
58
+ */
59
+ static attach(picking: PointPickHost, points: PointsPlugin): PointPicking;
60
+ private constructor();
61
+ /** The cloud `mesh` stands in for, from a `hit.mesh` or an `engine.pick` answer; null for any other mesh. */
62
+ cloudOf(mesh: Mesh | null): PointCloud | null;
63
+ private resources;
64
+ private pipeline;
65
+ /** @internal A cloud joined: its stand-in is made now, because adding a mesh during a pick would void that pick's answer. */
66
+ added(cloud: PointCloud): void;
67
+ /** @internal A cloud left: its stand-in goes with it. */
68
+ removed(cloud: PointCloud): void;
69
+ /** @internal Picking's turn: draws the points last drawn in this view into the tile, with IDs from `firstId`. */
70
+ drawPick(c: PointPickContext): void;
71
+ /** @internal The hit of ID `index` of the last `drawPick`. */
72
+ pickHit(index: number): PointPickHit | null;
73
+ /** Leaves Picking and frees the stand-in meshes and GPU buffers. */
74
+ dispose(): void;
75
+ [Symbol.dispose](): void;
76
+ }
package/dist/pick.js ADDED
@@ -0,0 +1,192 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { _ as e, c as t } from "./shaders-DCnbvb0-.js";
5
+ import { MargoError as n } from "@margoengine/math/errors";
6
+ //#region ../shared/pick-base.js
7
+ function r(e, t, n) {
8
+ (t.base?.size ?? 0) >= n || (t.base?.destroy(), t.base = e.createBuffer({
9
+ size: n * 2,
10
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
11
+ }), t.baseGroup = e.createBindGroup({
12
+ layout: t.baseLayout,
13
+ entries: [{
14
+ binding: 0,
15
+ resource: {
16
+ buffer: t.base,
17
+ size: 16
18
+ }
19
+ }]
20
+ }));
21
+ }
22
+ function i(e, t) {
23
+ let n = e.encoder.beginRenderPass({
24
+ colorAttachments: [{
25
+ view: e.color,
26
+ loadOp: "load",
27
+ storeOp: "store"
28
+ }],
29
+ depthStencilAttachment: {
30
+ view: e.depth,
31
+ depthLoadOp: "load",
32
+ depthStoreOp: "store"
33
+ }
34
+ });
35
+ return n.setBindGroup(0, t), n;
36
+ }
37
+ //#endregion
38
+ //#region src/pick.ts
39
+ var a = 256, o = 2 ** 28, s = "\n@group(2) @binding(0) var<uniform> pickBase:vec4u;\nstruct SpherePick{@location(0) id:u32,@builtin(frag_depth) depth:f32}\n\n@fragment fn pick(i:VOut)->@location(0) u32{\n let s=surface(i);\n if(s.color.a<=0.0){discard;}\n return pickBase.x+i.slot;\n}\n@fragment fn pickSphere(i:VOut)->SpherePick{\n let s=surface(i);\n if(s.color.a<=0.0){discard;}\n return SpherePick(pickBase.x+i.slot,sphereDepth(i,s.nz));\n}";
40
+ function c() {
41
+ let e = "@location(3) @interpolate(flat) center:vec3f,", r = "out.center=world.xyz;";
42
+ if (!t.includes(e) || !t.includes(r)) throw new n("unsupported", "Point picking needs the shader it was written for; update its edits with it");
43
+ return t.replace(e, `${e}@location(5) @interpolate(flat) slot:u32,`).replace(r, `${r}out.slot=slot;`) + s;
44
+ }
45
+ var l = class t {
46
+ static attach(e, r) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
47
+ if (!r.engine) throw new n("state", "Attach PointsPlugin to the engine before PointPicking.attach");
48
+ let i = new t(r);
49
+ i.remove = e.addOverlay(i), r.watchers.add(i);
50
+ for (let e of r.pointClouds) i.added(e);
51
+ return i;
52
+ }
53
+ constructor(e) {
54
+ this.points = e, this.entries = [], this.standIns = /* @__PURE__ */ new Map(), this.owners = /* @__PURE__ */ new Map(), this.frameData = /* @__PURE__ */ new Float32Array(36), this.view = /* @__PURE__ */ new Float32Array(16);
55
+ }
56
+ cloudOf(e) {
57
+ return (e && this.owners.get(e)) ?? null;
58
+ }
59
+ resources(e, t) {
60
+ if (this.gpu?.host === t) return this.gpu;
61
+ this.gpu?.frame.destroy(), this.gpu?.base?.destroy();
62
+ let n = e.createBuffer({
63
+ size: 144,
64
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
65
+ }), r = e.createBindGroupLayout({ entries: [{
66
+ binding: 0,
67
+ visibility: GPUShaderStage.FRAGMENT,
68
+ buffer: {
69
+ type: "uniform",
70
+ hasDynamicOffset: !0,
71
+ minBindingSize: 16
72
+ }
73
+ }] });
74
+ return this.gpu = {
75
+ host: t,
76
+ frame: n,
77
+ frameGroup: e.createBindGroup({
78
+ layout: t.frameLayout,
79
+ entries: [{
80
+ binding: 0,
81
+ resource: { buffer: n }
82
+ }]
83
+ }),
84
+ baseLayout: r,
85
+ layout: e.createPipelineLayout({ bindGroupLayouts: [
86
+ t.frameLayout,
87
+ t.cloudLayout,
88
+ r
89
+ ] }),
90
+ module: e.createShaderModule({ code: c() }),
91
+ pipelines: /* @__PURE__ */ new Map()
92
+ };
93
+ }
94
+ pipeline(e, t, n, r) {
95
+ let i = `${n}|${r}`, a = t.pipelines.get(i);
96
+ return a || (a = e.createRenderPipeline({
97
+ layout: t.layout,
98
+ vertex: {
99
+ module: t.module,
100
+ entryPoint: "vertex"
101
+ },
102
+ fragment: {
103
+ module: t.module,
104
+ entryPoint: n ? "pickSphere" : "pick",
105
+ targets: [{ format: "r32uint" }]
106
+ },
107
+ primitive: {
108
+ topology: "triangle-strip",
109
+ cullMode: "none"
110
+ },
111
+ depthStencil: {
112
+ format: "depth32float",
113
+ depthWriteEnabled: !0,
114
+ depthCompare: r ? "greater-equal" : "always"
115
+ }
116
+ }), t.pipelines.set(i, a)), a;
117
+ }
118
+ added(e) {
119
+ if (this.standIns.has(e)) return;
120
+ let t = this.points.engine.createMesh();
121
+ this.standIns.set(e, t), this.owners.set(t, e);
122
+ }
123
+ removed(e) {
124
+ let t = this.standIns.get(e);
125
+ t && (this.standIns.delete(e), this.owners.delete(t), t.dispose());
126
+ }
127
+ drawPick(t) {
128
+ let n = this.points, s = n.device, c = n.gpu;
129
+ if (this.entries = [], !s || !c) return;
130
+ let l;
131
+ for (let e of n.pickViews.drawn()) e.rect.every((e, n) => e === t.rect[n]) && (l = e);
132
+ if (!l) return;
133
+ let u = [];
134
+ for (let e of n.pointClouds) {
135
+ let t = l.drawn.get(e);
136
+ t?.length && n.cloudGpu.get(e)?.group && u.push({
137
+ cloud: e,
138
+ list: t
139
+ });
140
+ }
141
+ if (!u.length) return;
142
+ let d = this.resources(s, c), f = new Uint32Array(u.length * a / 4);
143
+ r(s, d, f.byteLength);
144
+ let p = this.frameData;
145
+ e(p, this.view, t.camera, t.rect[2], t.rect[3], "rgba8unorm-srgb"), p.set(t.matrix, 0), p[16] = p[17] = 3, s.queue.writeBuffer(d.frame, 0, p);
146
+ let m = i(t, d.frameGroup), h = 0, g = 0;
147
+ for (let { cloud: e, list: r } of u) {
148
+ if (h + e.count > o) break;
149
+ let i = n.cloudGpu.get(e), c = e.material;
150
+ m.setPipeline(this.pipeline(s, d, c.shading === "sphere", c.depthTest)), f[g * a / 4] = t.firstId + h, this.entries.push({
151
+ cloud: e,
152
+ first: h,
153
+ model: i.model
154
+ }), m.setBindGroup(1, i.group), m.setBindGroup(2, d.baseGroup, [g * a]);
155
+ for (let t = 0; t < r.length; t += 2) m.draw(4, r[t + 1], 0, e.blocks.starts[r[t]]);
156
+ h += e.count, g++;
157
+ }
158
+ s.queue.writeBuffer(d.base, 0, f), m.end();
159
+ }
160
+ pickHit(e) {
161
+ let t = this.entries, n = 0, r = t.length - 1;
162
+ for (; n < r;) {
163
+ let i = n + r + 1 >> 1;
164
+ t[i].first <= e ? n = i : r = i - 1;
165
+ }
166
+ let i = t[n];
167
+ if (!i) return null;
168
+ let { cloud: a, model: o } = i, s = e - i.first, c = a.direct ? s : a.blocks.order[s], l = a.attachedMesh ?? this.standIns.get(a);
169
+ if (!l) return null;
170
+ let u = a.positions[c * 3], d = a.positions[c * 3 + 1], f = a.positions[c * 3 + 2];
171
+ return {
172
+ mesh: l,
173
+ cloud: a,
174
+ point: c,
175
+ position: [
176
+ o[0] * u + o[4] * d + o[8] * f + o[12],
177
+ o[1] * u + o[5] * d + o[9] * f + o[13],
178
+ o[2] * u + o[6] * d + o[10] * f + o[14]
179
+ ]
180
+ };
181
+ }
182
+ dispose() {
183
+ this.remove?.(), this.remove = void 0, this.points.watchers.delete(this);
184
+ for (let e of this.standIns.values()) e.dispose();
185
+ this.standIns.clear(), this.owners.clear(), this.entries = [], this.gpu?.frame.destroy(), this.gpu?.base?.destroy(), this.gpu = void 0;
186
+ }
187
+ [Symbol.dispose]() {
188
+ this.dispose();
189
+ }
190
+ };
191
+ //#endregion
192
+ export { l as PointPicking, c as pointPickShader };