@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/index.js ADDED
@@ -0,0 +1,1246 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { a as e, c as t, d as n, l as r, o as i, r as a, s as o, t as s, u as c } from "./tier-BcFbLPVT.js";
5
+ import { _ as l, a as u, c as d, d as f, f as p, g as m, h, i as g, l as _, m as v, n as y, o as b, p as x, r as S, s as C, t as w, u as T, v as E, y as D } from "./cloud-B4O_G-Df.js";
6
+ import { _ as ee, c as O, d as k, f as A, g as te, h as ne, i as j, l as re, n as ie, o as M, p as N, r as P, t as F, u as I } from "./shaders-DCnbvb0-.js";
7
+ import { a as L, c as R, d as z, i as B, l as V, n as H, o as U, r as W, s as G, t as K, u as q } from "./attributes-DNc-1pdr.js";
8
+ import { MargoError as ae } from "@margoengine/math/errors";
9
+ import { meshWorldMatrix as J } from "@margoengine/math/mesh";
10
+ import { multiplyMatrices as oe } from "@margoengine/math/mat4";
11
+ //#region src/sprites.ts
12
+ var se = 16, Y = class {
13
+ constructor(e = {}) {
14
+ this.visible = !0, this.matrix = new Float32Array([
15
+ 1,
16
+ 0,
17
+ 0,
18
+ 0,
19
+ 0,
20
+ 1,
21
+ 0,
22
+ 0,
23
+ 0,
24
+ 0,
25
+ 1,
26
+ 0,
27
+ 0,
28
+ 0,
29
+ 0,
30
+ 1
31
+ ]), this.structure = 0, this.dirty = [], this.used = 0, this.mesh = null, this.name = e.name ?? "", this.material = e.material instanceof S ? e.material : new S(e.material);
32
+ let t = e.capacity ?? 64;
33
+ if (!Number.isInteger(t) || t < 1) throw RangeError("capacity must be a positive integer");
34
+ this.data = new Float32Array(t * 16);
35
+ for (let t of e.sprites ?? []) this.add(t);
36
+ }
37
+ get count() {
38
+ return this.used;
39
+ }
40
+ get capacity() {
41
+ return this.data.length / 16;
42
+ }
43
+ get attachedMesh() {
44
+ return this.mesh;
45
+ }
46
+ attach(e) {
47
+ return this.mesh = e, this;
48
+ }
49
+ add(e = {}) {
50
+ if (this.used === this.capacity) {
51
+ let e = new Float32Array(this.data.length * 2);
52
+ e.set(this.data), this.data = e, this.structure++;
53
+ }
54
+ let t = this.used;
55
+ return this.write(t, e, !0), this.used++, t;
56
+ }
57
+ set(e, t) {
58
+ return this.check(e), this.write(e, t, !1), this;
59
+ }
60
+ remove(e) {
61
+ this.check(e);
62
+ let t = --this.used;
63
+ return e === t ? -1 : (this.data.copyWithin(e * 16, t * 16, (t + 1) * 16), this.dirty.push([e, e + 1]), t);
64
+ }
65
+ clear() {
66
+ return this.used = 0, this;
67
+ }
68
+ markDirty(e = 0, t = this.used - e) {
69
+ if (!Number.isInteger(e) || !Number.isInteger(t) || e < 0 || t < 0 || e + t > this.used) throw RangeError("The sprite range must lie inside the used sprites; pass integer start and count within 0..used.");
70
+ return this.dirty.push([e, e + t]), this;
71
+ }
72
+ dispose() {
73
+ this.owner?.remove(this);
74
+ }
75
+ [Symbol.dispose ?? Symbol()]() {
76
+ this.dispose();
77
+ }
78
+ bind(e) {
79
+ if (e && this.owner && this.owner !== e) throw new ae("attached", "The batch already belongs to another plugin; remove it from that plugin first");
80
+ this.owner = e;
81
+ }
82
+ check(e) {
83
+ if (!Number.isInteger(e) || e < 0 || e >= this.used) throw RangeError(`No sprite ${e}; pass an integer index below the sprite count.`);
84
+ }
85
+ write(e, t, n) {
86
+ let r = this.data, i = e * 16, a = /* @__PURE__ */ new Float32Array(16);
87
+ if (a.set(n ? [
88
+ 0,
89
+ 0,
90
+ 0,
91
+ 0,
92
+ 1,
93
+ 1,
94
+ .5,
95
+ .5,
96
+ 0,
97
+ 0,
98
+ 1,
99
+ 1,
100
+ 1,
101
+ 1,
102
+ 1,
103
+ 1
104
+ ] : r.subarray(i, i + 16)), t.position && a.set(t.position.slice(0, 3), 0), t.rotation !== void 0 && (a[3] = t.rotation), t.size !== void 0) {
105
+ let e = typeof t.size == "number" ? [t.size, t.size] : t.size;
106
+ if (a[4] = e[0], a[5] = e[1], !(e[0] >= 0 && e[1] >= 0)) throw RangeError("Sprite size must be non-negative");
107
+ }
108
+ if (t.center && (a[6] = t.center[0], a[7] = t.center[1]), t.uv && a.set(t.uv.slice(0, 4), 8), t.color !== void 0 && g(t.color, a, 12), t.opacity !== void 0) {
109
+ if (!(t.opacity >= 0 && t.opacity <= 1)) throw RangeError("Sprite opacity must be in [0, 1]");
110
+ a[15] = t.opacity;
111
+ }
112
+ for (let e of a) if (!Number.isFinite(e)) throw RangeError("Sprite values must be finite");
113
+ r.set(a, i), this.dirty.push([e, e + 1]);
114
+ }
115
+ };
116
+ //#endregion
117
+ //#region ../shared/borrowed.js
118
+ function ce(e, t, n) {
119
+ return e.value = t, e.device = typeof t == "function" ? void 0 : n, e.made = void 0, e.told = !1, e;
120
+ }
121
+ var le = (e, t, n, r) => ce({
122
+ name: t,
123
+ setter: n
124
+ }, e, r);
125
+ function ue(e, t, n) {
126
+ let { value: i } = e;
127
+ if (typeof i == "function") return e.device !== t && (e.made = i(e.device = t)), e.made;
128
+ if (i != null) {
129
+ if (e.device ??= t, e.device === t) return i;
130
+ e.told || (e.told = !0, n?.onError(r("state", `${e.name} lost with the device; pass a function or call ${e.setter}`, "borrowed-lost")));
131
+ }
132
+ }
133
+ //#endregion
134
+ //#region ../shared/mipmaps.js
135
+ var de = (e) => `
136
+ @vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {
137
+ return vec4f(array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i],0,1);
138
+ }
139
+ @group(0) @binding(0) var image:texture_2d<f32>;
140
+ @fragment fn fragment(@builtin(position) clip:vec4f)->@location(0) vec4f {
141
+ let src=textureDimensions(image);
142
+ let dst=max(src/2u,vec2u(1u));let g=vec2u(clip.xy);
143
+ let a=vec2f(g*src)/vec2f(dst);let b=vec2f((g+1u)*src)/vec2f(dst);let end=min(src,((g+1u)*src+dst-1u)/dst);
144
+ var sum=vec4f(0.0);var sc=vec3f(0.0);
145
+ for(var y=g.y*src.y/dst.y;y<end.y;y++){
146
+ let wy=min(b.y,f32(y+1u))-max(a.y,f32(y));
147
+ for(var x=g.x*src.x/dst.x;x<end.x;x++){
148
+ let w=(min(b.x,f32(x+1u))-max(a.x,f32(x)))*wy;let t=textureLoad(image,vec2u(x,y),0);
149
+ sum+=w*t;sc+=(w*t.a)*t.rgb;
150
+ }
151
+ }
152
+ let area=(b.x-a.x)*(b.y-a.y);
153
+ ${e ? "return vec4f(select(sum.rgb/area,sc/sum.a,sum.a>0.0),sum.a/area);" : "return sum/area;"}
154
+ }
155
+ `;
156
+ function fe(e, t, n, r, i = 1, a = "plain") {
157
+ if (n < 2) return;
158
+ let o = t.format, s = a === "weighted", c = s ? o + ":weighted" : o, l = r.get(c);
159
+ if (!l) {
160
+ let t = e.createShaderModule({ code: de(s) });
161
+ r.set(c, l = e.createRenderPipeline({
162
+ layout: "auto",
163
+ vertex: {
164
+ module: t,
165
+ entryPoint: "vertex"
166
+ },
167
+ fragment: {
168
+ module: t,
169
+ entryPoint: "fragment",
170
+ targets: [{ format: o }]
171
+ }
172
+ }));
173
+ }
174
+ let u = e.createCommandEncoder();
175
+ for (let r = 0; r < i; r++) for (let a = 1; a < n; a++) {
176
+ let n = (e) => t.createView(i > 1 ? {
177
+ format: o,
178
+ dimension: "2d",
179
+ baseMipLevel: e,
180
+ mipLevelCount: 1,
181
+ baseArrayLayer: r,
182
+ arrayLayerCount: 1
183
+ } : {
184
+ baseMipLevel: e,
185
+ mipLevelCount: 1
186
+ }), s = e.createBindGroup({
187
+ layout: l.getBindGroupLayout(0),
188
+ entries: [{
189
+ binding: 0,
190
+ resource: n(a - 1)
191
+ }]
192
+ }), c = u.beginRenderPass({ colorAttachments: [{
193
+ view: n(a),
194
+ loadOp: "clear",
195
+ storeOp: "store",
196
+ clearValue: {
197
+ r: 0,
198
+ g: 0,
199
+ b: 0,
200
+ a: 0
201
+ }
202
+ }] });
203
+ c.setPipeline(l), c.setBindGroup(0, s), c.draw(3), c.end();
204
+ }
205
+ e.queue.submit([u.finish()]);
206
+ }
207
+ //#endregion
208
+ //#region ../shared/copy-external.js
209
+ var X = /* @__PURE__ */ new WeakSet(), Z = /* @__PURE__ */ new WeakMap();
210
+ function pe(e) {
211
+ return !!e && typeof e == "object" && ("videoWidth" in e || "displayWidth" in e);
212
+ }
213
+ var me = (e) => [e.videoWidth ?? e.displayWidth ?? e.naturalWidth ?? e.width ?? 0, e.videoHeight ?? e.displayHeight ?? e.naturalHeight ?? e.height ?? 0];
214
+ function he(e, t, n, r) {
215
+ let i = e.queue, a = t.source, o = pe(a);
216
+ if (o && !X.has(i)) try {
217
+ return i.copyExternalImageToTexture(t, n, r);
218
+ } catch (e) {
219
+ if (!(e instanceof TypeError)) throw e;
220
+ X.add(i);
221
+ }
222
+ if (!o) return i.copyExternalImageToTexture(t, n, r);
223
+ let [s, c] = me(a), l = Z.get(a);
224
+ if (!l || l.w !== s || l.h !== c) {
225
+ let e = typeof OffscreenCanvas == "function" ? new OffscreenCanvas(s, c) : document.createElement("canvas");
226
+ e.width = s, e.height = c, Z.set(a, l = {
227
+ canvas: e,
228
+ w: s,
229
+ h: c
230
+ });
231
+ }
232
+ l.canvas.getContext("2d").drawImage(a, 0, 0), i.copyExternalImageToTexture({
233
+ ...t,
234
+ source: l.canvas
235
+ }, n, r);
236
+ }
237
+ //#endregion
238
+ //#region ../shared/projection.js
239
+ function ge(e, t) {
240
+ if (!e || e[3] || e[7] || e[11]) return null;
241
+ let n = (n) => e[n] * t[0] + e[4 + n] * t[1] + e[8 + n] * t[2] + e[12 + n], r = Math.hypot(e[2], e[6], e[10]), i = n(2);
242
+ return {
243
+ sx: Math.hypot(e[0], e[4], e[8]),
244
+ sy: Math.hypot(e[1], e[5], e[9]),
245
+ sz: r,
246
+ ox: n(0),
247
+ oy: n(1),
248
+ near: (i - 1) / r,
249
+ far: i / r
250
+ };
251
+ }
252
+ //#endregion
253
+ //#region src/plugin.ts
254
+ function Q(e, t, n, r) {
255
+ if (!(t < 2)) {
256
+ for (let r = 0; r < t; r++) n[r] = e[r];
257
+ n.length = t, n.sort(r);
258
+ for (let r = 0; r < t; r++) e[r] = n[r];
259
+ n.length = 0;
260
+ }
261
+ }
262
+ var $ = (e) => typeof e == "function" || typeof e.createView == "function", _e = class r {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
263
+ memoryFor(e) {
264
+ let t = this.cloudGpu.get(e);
265
+ return t ? {
266
+ cpu: [
267
+ t.floats,
268
+ t.uints,
269
+ t.written,
270
+ t.model,
271
+ t.drawn
272
+ ],
273
+ gpu: [
274
+ t.uniform,
275
+ t.positions,
276
+ t.colors,
277
+ t.sizes,
278
+ t.order,
279
+ t.mask
280
+ ].filter((e) => e !== null)
281
+ } : {
282
+ cpu: [],
283
+ gpu: []
284
+ };
285
+ }
286
+ static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
287
+ let n = new r(t);
288
+ return e.use(n), n;
289
+ }
290
+ constructor(t = {}) {
291
+ if (this.stats = {
292
+ clouds: 0,
293
+ blocks: 0,
294
+ blocksVisible: 0,
295
+ pointsVisible: 0,
296
+ pointsDrawn: 0,
297
+ sprites: 0,
298
+ drawCalls: 0,
299
+ uploads: 0,
300
+ uploadBytes: 0
301
+ }, this.clouds = /* @__PURE__ */ new Set(), this.batches = /* @__PURE__ */ new Set(), this.cloudGpu = /* @__PURE__ */ new Map(), this.batchGpu = /* @__PURE__ */ new Map(), this.owned = /* @__PURE__ */ new Map(), this.borrowed = /* @__PURE__ */ new WeakMap(), this.borrowedViews = /* @__PURE__ */ new WeakMap(), this.pipelines = /* @__PURE__ */ new Map(), this.edlLayout = null, this.mipCache = /* @__PURE__ */ new Map(), this.watchers = /* @__PURE__ */ new Set(), this.frameData = /* @__PURE__ */ new Float32Array(36), this.frameBits = new Uint32Array(this.frameData.buffer), this.frameWritten = /* @__PURE__ */ new Uint32Array(36), this.frameValid = !1, this.fogSource = null, this.fogUsed = !1, this.fogData = /* @__PURE__ */ new Float32Array(12), this.edlData = /* @__PURE__ */ new Float32Array(4), this.orthographic = !1, this.view = /* @__PURE__ */ new Float32Array(16), this.pickViews = new e(() => ({
302
+ viewProj: /* @__PURE__ */ new Float32Array(16),
303
+ rect: [
304
+ 0,
305
+ 0,
306
+ 0,
307
+ 0
308
+ ],
309
+ canvas: !0,
310
+ orthographic: !1,
311
+ drawn: /* @__PURE__ */ new WeakMap()
312
+ })), this.box = /* @__PURE__ */ new Float64Array(6), this.pickMvp = /* @__PURE__ */ new Float64Array(16), this.pickRanges = [], this.pickWindow = [], this.entryBlock = [], this.entryCount = [], this.entryWeight = [], this.entryDistance = [], this.blockAllotment = /* @__PURE__ */ new Uint32Array(), this.blockOrder = [], this.cloudDraws = [], this.cloudDrawScratch = [], this.spriteDraws = [], this.cloudCount = 0, this.cloudEnd = 0, this.spriteCount = 0, this.spriteEnd = 0, this.transparency = A(), this.hookLayouts = /* @__PURE__ */ new Map(), this.spriteDrawScratch = [], this.pointColor = {
313
+ view: void 0,
314
+ loadOp: "load",
315
+ storeOp: "store"
316
+ }, this.pointColors = [this.pointColor], this.edlColor = {
317
+ view: void 0,
318
+ loadOp: "clear",
319
+ storeOp: "store",
320
+ clearValue: {
321
+ r: 0,
322
+ g: 0,
323
+ b: 0,
324
+ a: 0
325
+ }
326
+ }, this.sceneDepth = {
327
+ view: void 0,
328
+ depthLoadOp: "load",
329
+ depthStoreOp: "store"
330
+ }, this.prepare = (e) => {
331
+ let t = this.stats;
332
+ if (this.cloudCount = this.cloudEnd = this.spriteCount = this.spriteEnd = 0, t.clouds = t.blocks = t.blocksVisible = t.pointsVisible = t.pointsDrawn = t.sprites = t.drawCalls = t.uploads = t.uploadBytes = 0, e.backend !== "webgpu" || !this.device || !this.gpu || !this.engine || e.device !== this.device || !this.clouds.size && !this.batches.size) return;
333
+ let n = this.engine, r = this.device, i = this.gpu, a = e.camera;
334
+ this.writeFrame(e);
335
+ let o = this.pixelScale?.(e) ?? te(n.canvas, e.width), s = this.frameData[18], c = this.frameData[16], l = this.frameData[17];
336
+ if (this.fogUsed = !1, this.fogSource) {
337
+ for (let e of this.clouds) this.fogUsed ||= e.visible && e.material.fog;
338
+ for (let e of this.batches) this.fogUsed ||= e.visible && e.material.fog;
339
+ this.fogUsed && (ne(this.fogSource, a.position, this.fogData), r.queue.writeBuffer(this.fogResources().buffer, 0, this.fogData));
340
+ }
341
+ let u = this.entryBlock, f = this.entryCount, p = this.entryWeight, m = this.entryDistance;
342
+ u.length = 0, f.length = 0, p.length = 0, m.length = 0;
343
+ let h = 0;
344
+ for (let e of this.clouds) {
345
+ e.stats.blocksVisible = e.stats.pointsVisible = e.stats.pointsDrawn = 0;
346
+ let r = this.pickView.drawn.get(e);
347
+ if (r || this.pickView.drawn.set(e, r = []), r.length = 0, !e.visible || !e.count) continue;
348
+ let g = e.attachedMesh, _ = g ? J(n, g) : e.matrix;
349
+ if (!_) continue;
350
+ e.material.validate();
351
+ let v = this.ensureCloud(e);
352
+ v.drawn = r, this.flushCloud(e, v);
353
+ let y = this.texture(e.material);
354
+ this.writeCloudUniform(e, v, _, !!y && e.material.map !== null, o), v.model.set(_), t.clouds++;
355
+ let b = e.blocks, x = u.length;
356
+ t.blocks += b.length;
357
+ for (let t = 0; t < b.length; t++) {
358
+ if (!b.counts[t] || (D(b.bounds, t, _, this.box), !d(this.box, a.planes, e.cullMargin))) continue;
359
+ let n = this.box;
360
+ u.push(t), f.push(b.counts[t]), p.push(E(n, a.position, s, c, l, this.orthographic)), m.push(Math.hypot(n[0] - a.position[0], n[1] - a.position[1], n[2] - a.position[2])), e.stats.blocksVisible++, e.stats.pointsVisible += b.counts[t];
361
+ }
362
+ if (t.blocksVisible += e.stats.blocksVisible, t.pointsVisible += e.stats.pointsVisible, u.length > x) {
363
+ let t = this.cloudDraws[h], n = y ?? i.whiteView, r = u.length;
364
+ t ? (t.cloud = e, t.r = v, t.view = n, t.first = x, t.end = r, t.mapped = !!y && e.material.map !== null) : this.cloudDraws[h] = t = {
365
+ cloud: e,
366
+ r: v,
367
+ view: n,
368
+ first: x,
369
+ end: r,
370
+ mapped: !!y && e.material.map !== null
371
+ }, h++;
372
+ }
373
+ }
374
+ let g = this.blockAllotment;
375
+ g.length < u.length && (this.blockAllotment = g = new Uint32Array(Math.max(16, u.length, g.length * 2))), t.pointsDrawn = b(f, p, u.length, this.pointBudget, g);
376
+ let _ = this.cloudDraws, v = 0;
377
+ for (let e = 0; e < h; e++) {
378
+ let t = _[e], n = t.cloud.material.adaptiveSize;
379
+ if (n > 1) {
380
+ let e = 0;
381
+ for (let n = t.first; n < t.end; n++) e += g[n];
382
+ let r = t.cloud.stats.pointsVisible;
383
+ e > 0 && e < r && this.writeCloudUniform(t.cloud, t.r, t.r.model, t.mapped, o, Math.min(n, Math.sqrt(r / e)));
384
+ }
385
+ if (this.takes(t.cloud.material)) for (let e = t.first; e < t.end; e++) g[e] && (t.r.drawn.push(u[e], g[e]), t.cloud.stats.pointsDrawn += g[e]);
386
+ else _[e] = _[v], _[v++] = t;
387
+ }
388
+ this.cloudCount = v, this.cloudEnd = h;
389
+ let y = 0;
390
+ for (let e of this.batches) {
391
+ if (!e.visible || !e.count) continue;
392
+ let a = e.attachedMesh, s = a ? J(n, a) : e.matrix;
393
+ if (!s) continue;
394
+ let c = e.material;
395
+ c.validate();
396
+ let l = this.ensureBatch(e);
397
+ if (e.dirty.length) {
398
+ for (let [n, i] of x(e.dirty)) {
399
+ let a = Math.min(i, e.count);
400
+ a <= n || (r.queue.writeBuffer(l.instances, n * 16 * 4, e.data, n * 16, (a - n) * 16), t.uploads++, t.uploadBytes += (a - n) * 16 * 4);
401
+ }
402
+ e.dirty.length = 0;
403
+ }
404
+ let u = this.texture(c) ?? i.whiteView, d = l.floats;
405
+ d.set(s, 0), d.set(c.color, 16), d[19] = c.opacity, d[20] = c.rotation, d[21] = c.alphaTest, d[22] = c.sizeUnits === "pixels" ? o : 0;
406
+ let f = c.clipping;
407
+ d[23] = f.length / 4 * (c.clipIntersection ? -1 : 1), d.set(f, 24), d.fill(0, 24 + f.length);
408
+ let p = !1;
409
+ for (let e = 0; e < d.length; e++) if (!Object.is(l.written[e], d[e])) {
410
+ p = !0;
411
+ break;
412
+ }
413
+ p && (r.queue.writeBuffer(l.uniform, 0, d), l.written.set(d));
414
+ let m = this.spriteDraws[y], h = this.batchGroup(l, u, c.mapFilter === "nearest" ? i.nearest : i.linear);
415
+ m ? (m.batch = e, m.group = h) : this.spriteDraws[y] = m = {
416
+ batch: e,
417
+ group: h
418
+ }, y++;
419
+ }
420
+ let S = 0;
421
+ for (let e = 0; e < y; e++) {
422
+ let n = this.spriteDraws[e];
423
+ this.takes(n.batch.material) ? t.sprites += n.batch.count : (this.spriteDraws[e] = this.spriteDraws[S], this.spriteDraws[S++] = n);
424
+ }
425
+ this.spriteCount = S, this.spriteEnd = y;
426
+ }, this.execute = (e) => {
427
+ if (e.backend !== "webgpu") return;
428
+ let t = this.cloudCount, n = this.spriteCount;
429
+ if (!t && !n) return;
430
+ let r = this.stats, o = this.gpu, s = this.device, c = e.camera, l = this.cloudDraws, u = this.blockAllotment, d = this.entryBlock, f = this.entryDistance, p = !1;
431
+ for (let e = 0; e < t; e++) l[e].cloud.material.edl && (p = !0);
432
+ let m = p && a(s);
433
+ for (let n = 0; n < t; n++) {
434
+ let t = l[n];
435
+ t.r.transparent = t.cloud.material.transparent, t.r.pipeline = this.pointPipeline(e, t.cloud.material, p && !m);
436
+ }
437
+ if (Q(l, t, this.cloudDrawScratch, (e, t) => +e.r.transparent - t.r.transparent), t) {
438
+ let n = e.descriptor.depthStencil.format, a = this.pointColors;
439
+ if (this.pointColor.view = e.color, p) {
440
+ let t = e.descriptor.multisample?.count ?? 1, n = this.edlTarget(e.width, e.height, t, this.edlPipelines(e.format, t)[0]);
441
+ this.edlColor.view = n.view, a[1] = this.edlColor, a.length = m ? 1 : 2;
442
+ } else a.length = 1;
443
+ let h = this.sceneDepth;
444
+ h.view = e.depth, n.includes("stencil") ? h.stencilReadOnly = !0 : delete h.stencilReadOnly;
445
+ let g = this.blockOrder, _ = e.encoder.beginRenderPass({
446
+ colorAttachments: a,
447
+ depthStencilAttachment: h
448
+ });
449
+ i(_, e), _.setBindGroup(0, o.frameGroup), this.fogUsed && _.setBindGroup(2, this.fogGpu.group);
450
+ for (let e = 0; e < t; e++) {
451
+ let t = l[e], { cloud: n, r: i } = t, a = n.blocks, s = n.material.mapFilter === "nearest" ? o.nearest : o.linear;
452
+ _.setPipeline(i.pipeline), _.setBindGroup(1, this.cloudGroup(i, t.view, s)), g.length = 0;
453
+ for (let e = t.first; e < t.end; e++) u[e] && g.push(e);
454
+ g.sort((e, t) => i.transparent ? f[t] - f[e] : f[e] - f[t]);
455
+ for (let e of g) {
456
+ let t = d[e], o = u[e];
457
+ _.draw(4, o, 0, a.starts[t]), i.drawn.push(t, o), n.stats.pointsDrawn += o, r.drawCalls++;
458
+ }
459
+ }
460
+ if (_.end(), m) {
461
+ let n = e.encoder.beginRenderPass({
462
+ colorAttachments: [null, this.edlColor],
463
+ depthStencilAttachment: h
464
+ });
465
+ i(n, e), n.setBindGroup(0, o.frameGroup), this.fogUsed && n.setBindGroup(2, this.fogGpu.group);
466
+ for (let i = 0; i < t; i++) {
467
+ let { cloud: t, r: a, view: s } = l[i];
468
+ n.setPipeline(this.pointPipeline(e, t.material, "alone")), n.setBindGroup(1, this.cloudGroup(a, s, t.material.mapFilter === "nearest" ? o.nearest : o.linear));
469
+ for (let e = 0; e < a.drawn.length; e += 2) n.draw(4, a.drawn[e + 1], 0, t.blocks.starts[a.drawn[e]]), r.drawCalls++;
470
+ }
471
+ n.end();
472
+ }
473
+ if (p && this.edl) {
474
+ this.edlData[0] = this.edlStrength, this.edlData[1] = this.edlRadius, this.edlData[2] = this.orthographic ? this.frameData[18] * 2 / this.frameData[17] * Math.tan(c.fov * Math.PI / 360) * Math.LOG2E : 0, s.queue.writeBuffer(o.edlParams, 0, this.edlData), this.pointColors.length = 1;
475
+ let t = e.encoder.beginRenderPass({ colorAttachments: this.pointColors });
476
+ i(t, e), t.setBindGroup(0, this.edl.group);
477
+ for (let n of this.edlPipelines(e.format, e.descriptor.multisample?.count ?? 1)) t.setPipeline(n), t.draw(3), r.drawCalls++;
478
+ t.end();
479
+ }
480
+ }
481
+ if (n) {
482
+ Q(this.spriteDraws, n, this.spriteDrawScratch, (e, t) => +e.batch.material.transparent - t.batch.material.transparent);
483
+ let t = this.sceneDepth;
484
+ t.view = e.depth, e.descriptor.depthStencil.format.includes("stencil") ? t.stencilReadOnly = !0 : delete t.stencilReadOnly, this.pointColor.view = e.color, this.pointColors.length = 1;
485
+ let a = e.encoder.beginRenderPass({
486
+ colorAttachments: this.pointColors,
487
+ depthStencilAttachment: t
488
+ });
489
+ i(a, e), a.setBindGroup(0, o.frameGroup), this.fogUsed && a.setBindGroup(2, this.fogGpu.group);
490
+ for (let t = 0; t < n; t++) {
491
+ let { batch: n, group: i } = this.spriteDraws[t];
492
+ a.setPipeline(this.spritePipeline(e, n.material)), a.setBindGroup(1, i), a.draw(4, n.count), r.sprites += n.count, r.drawCalls++;
493
+ }
494
+ a.end();
495
+ }
496
+ }, this.name = t.name ?? "points", this.order = t.order ?? 10, this.pointBudget = t.pointBudget ?? 1e6, this.edlStrength = t.edlStrength ?? 1, this.edlRadius = t.edlRadius ?? 1.4, !this.name || typeof this.name != "string" || !Number.isFinite(this.order)) throw TypeError("PointsPlugin needs a non-empty string name and a finite order; fix the options.");
497
+ if (!(this.pointBudget >= 0) || !(this.edlStrength >= 0) || !(this.edlRadius > 0)) throw RangeError("pointBudget, edlStrength and edlRadius must be non-negative");
498
+ }
499
+ get pointClouds() {
500
+ return this.clouds;
501
+ }
502
+ get spriteBatches() {
503
+ return this.batches;
504
+ }
505
+ get fog() {
506
+ return this.fogSource;
507
+ }
508
+ setFog(e) {
509
+ return this.fogSource = N(e), this;
510
+ }
511
+ add(e) {
512
+ if (e instanceof w) {
513
+ e.bind(this), this.clouds.add(e);
514
+ for (let t of this.watchers) t.added(e);
515
+ } else if (e instanceof Y) e.bind(this), this.batches.add(e);
516
+ else throw TypeError("Expected a PointCloud or SpriteBatch");
517
+ return e;
518
+ }
519
+ remove(e) {
520
+ if (e instanceof w && this.clouds.delete(e)) {
521
+ this.releaseCloud(e), e.bind(void 0), this.releaseUnusedTexture(e.material);
522
+ for (let t of this.watchers) t.removed(e);
523
+ } else e instanceof Y && this.batches.delete(e) && (this.releaseBatch(e), e.bind(void 0), this.releaseUnusedTexture(e.material));
524
+ }
525
+ clear() {
526
+ for (let e of [...this.clouds, ...this.batches]) this.remove(e);
527
+ }
528
+ setup(e) {
529
+ n(e, "PointsPlugin"), this.engine = e;
530
+ let t = this.device = e.gpu, r = GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, i = GPUShaderStage.VERTEX, a = GPUShaderStage.FRAGMENT, o = (e) => ({
531
+ binding: e,
532
+ visibility: i,
533
+ buffer: { type: "read-only-storage" }
534
+ }), s = t.createBindGroupLayout({ entries: [{
535
+ binding: 0,
536
+ visibility: r,
537
+ buffer: {
538
+ type: "uniform",
539
+ minBindingSize: 144
540
+ }
541
+ }] }), c = t.createBindGroupLayout({ entries: [
542
+ {
543
+ binding: 0,
544
+ visibility: r,
545
+ buffer: {
546
+ type: "uniform",
547
+ minBindingSize: 224
548
+ }
549
+ },
550
+ o(1),
551
+ o(2),
552
+ o(3),
553
+ o(4),
554
+ {
555
+ binding: 5,
556
+ visibility: a,
557
+ texture: { sampleType: "float" }
558
+ },
559
+ {
560
+ binding: 6,
561
+ visibility: a,
562
+ sampler: { type: "filtering" }
563
+ },
564
+ o(7)
565
+ ] }), l = t.createBindGroupLayout({ entries: [
566
+ {
567
+ binding: 0,
568
+ visibility: r,
569
+ buffer: {
570
+ type: "uniform",
571
+ minBindingSize: 192
572
+ }
573
+ },
574
+ o(1),
575
+ {
576
+ binding: 2,
577
+ visibility: a,
578
+ texture: { sampleType: "float" }
579
+ },
580
+ {
581
+ binding: 3,
582
+ visibility: a,
583
+ sampler: { type: "filtering" }
584
+ }
585
+ ] }), u = t.createBindGroupLayout({ entries: [{
586
+ binding: 0,
587
+ visibility: a,
588
+ texture: { sampleType: "unfilterable-float" }
589
+ }, {
590
+ binding: 1,
591
+ visibility: a,
592
+ buffer: {
593
+ type: "uniform",
594
+ minBindingSize: 16
595
+ }
596
+ }] }), d = [], f = (e, n) => {
597
+ let r = t.createBuffer({
598
+ size: e,
599
+ usage: n
600
+ });
601
+ return d.push(r), r;
602
+ };
603
+ try {
604
+ let e = f(144, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST), n = t.createTexture({
605
+ size: [1, 1],
606
+ format: "rgba8unorm",
607
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
608
+ });
609
+ d.push(n), t.queue.writeTexture({ texture: n }, new Uint8Array([
610
+ 255,
611
+ 255,
612
+ 255,
613
+ 255
614
+ ]), { bytesPerRow: 4 }, [1, 1]), this.gpu = {
615
+ pointModule: t.createShaderModule({ code: O }),
616
+ spriteModule: t.createShaderModule({ code: re }),
617
+ edlModule: t.createShaderModule({ code: P }),
618
+ frameLayout: s,
619
+ cloudLayout: c,
620
+ spriteLayout: l,
621
+ edlLayout: u,
622
+ pointPipelineLayout: t.createPipelineLayout({ bindGroupLayouts: [s, c] }),
623
+ spritePipelineLayout: t.createPipelineLayout({ bindGroupLayouts: [s, l] }),
624
+ edlPipelineLayout: t.createPipelineLayout({ bindGroupLayouts: [u] }),
625
+ frame: e,
626
+ frameGroup: t.createBindGroup({
627
+ layout: s,
628
+ entries: [{
629
+ binding: 0,
630
+ resource: { buffer: e }
631
+ }]
632
+ }),
633
+ dummy: f(16, GPUBufferUsage.STORAGE),
634
+ white: n,
635
+ whiteView: n.createView(),
636
+ linear: t.createSampler({
637
+ magFilter: "linear",
638
+ minFilter: "linear",
639
+ mipmapFilter: "linear"
640
+ }),
641
+ nearest: t.createSampler({
642
+ magFilter: "nearest",
643
+ minFilter: "nearest"
644
+ }),
645
+ edlParams: f(16, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST)
646
+ }, this.frameValid = !1;
647
+ } catch (e) {
648
+ for (let e of d) e.destroy();
649
+ throw e;
650
+ }
651
+ }
652
+ registerPasses(e) {
653
+ e.add({
654
+ name: `${this.name}:prepare`,
655
+ order: Math.min(this.order, -6),
656
+ execute: this.prepare
657
+ }), e.add({
658
+ name: `${this.name}:draw`,
659
+ order: this.order,
660
+ execute: this.execute
661
+ });
662
+ }
663
+ onDeviceRestored(e) {
664
+ this.releaseGpu(), this.setup(e);
665
+ }
666
+ onPreUpdate(e) {
667
+ e > 0 && this.pickViews.prune();
668
+ }
669
+ dispose() {
670
+ let e = this.engine;
671
+ this.transparency.released?.(), this.releaseGpu(), this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
672
+ }
673
+ [Symbol.dispose ?? Symbol()]() {
674
+ this.dispose();
675
+ }
676
+ pick(e, t, n = {}) {
677
+ if (!Number.isFinite(e) || !Number.isFinite(t)) throw RangeError("pick() needs finite x and y pixel coordinates; pass finite numbers.");
678
+ let r = n.radius ?? 6;
679
+ if (!(r >= 0)) throw RangeError("radius must be non-negative");
680
+ let i = this.engine;
681
+ if (!i) return null;
682
+ let a = i.canvas, o = a.clientWidth ? a.width / a.clientWidth : 1, s = e * o, c = t * o, l = (e) => s >= e[0] && c >= e[1] && s < e[0] + e[2] && c < e[1] + e[3], u;
683
+ if (n.view === void 0) for (let e of this.pickViews.drawn()) e.canvas && l(e.rect) && (u = e);
684
+ else u = this.pickViews.find(n.view);
685
+ if (!u || !l(u.rect)) return null;
686
+ let d = u.viewProj, [, , f, p] = u.rect, m = s - u.rect[0], g = c - u.rect[1], v = r * o, y = null, b = Infinity, x = Infinity, S = this.pickMvp, C = this.pickRanges, w = this.pickWindow;
687
+ for (let e of this.clouds) {
688
+ let t = this.cloudGpu.get(e), n = u.drawn.get(e);
689
+ if (!t || !n?.length) continue;
690
+ oe(d, t.model, S);
691
+ let r = 0;
692
+ for (let i = 0; i < n.length; i += 2) {
693
+ let a = n[i];
694
+ D(e.blocks.bounds, a, t.model, this.box);
695
+ let o = _(this.box, d, f, p);
696
+ if (o && (m < o[0] - v - 1 || m > o[2] + v + 1 || g < o[1] - v - 1 || g > o[3] + v + 1)) continue;
697
+ let s = C[r];
698
+ s || (C[r] = s = {
699
+ start: 0,
700
+ count: 0
701
+ }), s.start = e.blocks.starts[a], s.count = n[i + 1], w[r] = s, r++;
702
+ }
703
+ w.length = r;
704
+ let i = h(e.positions, e.direct ? null : e.blocks.order, w, S, f, p, m, g, v, e.mask && e.maskStyle.mode === "filter" ? (t) => !e.isSelected(t) : void 0);
705
+ if (i && (i.distance < b - .5 || Math.abs(i.distance - b) <= .5 && i.depth < x)) {
706
+ let n = i.index * 3, r = t.model, a = e.positions[n], s = e.positions[n + 1], c = e.positions[n + 2], l = [
707
+ 0,
708
+ 1,
709
+ 2
710
+ ].map((e) => r[e] * a + r[4 + e] * s + r[8 + e] * c + r[12 + e]);
711
+ y = {
712
+ cloud: e,
713
+ index: i.index,
714
+ position: l,
715
+ distance: i.distance / o,
716
+ depth: u.orthographic ? i.depth / Math.hypot(d[2], d[6], d[10]) : i.depth
717
+ }, b = i.distance, x = i.depth;
718
+ }
719
+ }
720
+ return w.length = 0, y;
721
+ }
722
+ texture(e) {
723
+ let t = e.map, n = this.device;
724
+ if (!t) return this.gpu.whiteView;
725
+ if ($(t)) {
726
+ let e = this.borrowed.get(t);
727
+ e || this.borrowed.set(t, e = le(t, "Points material map", "the map setter"));
728
+ let r = ue(e, n, this.engine);
729
+ if (!r) return null;
730
+ let i = this.borrowedViews.get(r);
731
+ return i || this.borrowedViews.set(r, i = r.createView()), i;
732
+ }
733
+ let r = this.owned.get(e);
734
+ if (r && r.source === t && r.version === e.mapVersion && r.mipmaps === e.mipmaps) return r.view;
735
+ r && (r.texture.destroy(), this.owned.delete(e));
736
+ let i = t, a = i.naturalWidth || i.videoWidth || i.width || 0, o = i.naturalHeight || i.videoHeight || i.height || 0;
737
+ if (!a || !o) throw RangeError("The texture source has no size yet; wait until the image or video has loaded before using it.");
738
+ let s = e.mipmaps ? Math.floor(Math.log2(Math.max(a, o))) + 1 : 1, c = n.createTexture({
739
+ size: [a, o],
740
+ mipLevelCount: s,
741
+ format: "rgba8unorm-srgb",
742
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
743
+ });
744
+ try {
745
+ typeof ImageData < "u" && t instanceof ImageData ? n.queue.writeTexture({ texture: c }, t.data, { bytesPerRow: a * 4 }, [a, o]) : he(n, { source: t }, { texture: c }, [a, o]), fe(n, c, s, this.mipCache), r = {
746
+ source: t,
747
+ version: e.mapVersion,
748
+ mipmaps: e.mipmaps,
749
+ texture: c,
750
+ view: c.createView()
751
+ };
752
+ } catch (e) {
753
+ throw c.destroy(), e;
754
+ }
755
+ return this.owned.set(e, r), r.view;
756
+ }
757
+ releaseUnusedTexture(e) {
758
+ let t = this.owned.get(e);
759
+ if (t) {
760
+ for (let t of [...this.clouds, ...this.batches]) if (t.material === e) return;
761
+ t.texture.destroy(), this.owned.delete(e);
762
+ }
763
+ }
764
+ ensureCloud(e) {
765
+ let t = this.cloudGpu.get(e);
766
+ if (t && t.structure === e.structure) return t;
767
+ t && this.releaseCloud(e);
768
+ let n = this.device;
769
+ c(e, n.limits, 0);
770
+ let r = [], i = (e, t) => {
771
+ let i = n.createBuffer({
772
+ size: Math.max(16, Math.ceil(e / 4) * 4),
773
+ usage: t
774
+ });
775
+ return r.push(i), i;
776
+ }, a = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST;
777
+ try {
778
+ let n = i(224, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST), r = i(e.count * 12, a), o = e.colors ? i(e.count * 4 * e.colorStride, a) : null, s = e.sizes ? i(e.count * 4, a) : null, c = e.direct ? null : i(e.count * 4, a), l = e.mask ? i(e.mask.byteLength, a) : null, u = /* @__PURE__ */ new ArrayBuffer(224);
779
+ t = {
780
+ structure: e.structure,
781
+ uniform: n,
782
+ positions: r,
783
+ colors: o,
784
+ sizes: s,
785
+ order: c,
786
+ mask: l,
787
+ maskVersion: -1,
788
+ group: null,
789
+ groupView: null,
790
+ groupSampler: null,
791
+ floats: new Float32Array(u),
792
+ uints: new Uint32Array(u),
793
+ written: (/* @__PURE__ */ new Float32Array(56)).fill(NaN),
794
+ model: /* @__PURE__ */ new Float32Array(16),
795
+ drawn: [],
796
+ pipeline: null,
797
+ transparent: !1
798
+ };
799
+ } catch (e) {
800
+ for (let e of r) e.destroy();
801
+ throw e;
802
+ }
803
+ let o = n.queue, s = e.count;
804
+ return s && (o.writeBuffer(t.positions, 0, e.positions, 0, s * 3), t.colors && o.writeBuffer(t.colors, 0, e.colors, 0, s * e.colorStride), t.sizes && o.writeBuffer(t.sizes, 0, e.sizes, 0, s), t.order && o.writeBuffer(t.order, 0, e.blocks.order, 0, s), this.stats.uploads += 1 + +!!t.colors + +!!t.sizes + +!!t.order, this.stats.uploadBytes += s * (12 + (t.colors ? 4 * e.colorStride : 0) + (t.sizes ? 4 : 0) + (t.order ? 4 : 0))), e.dirty.positions.length = e.dirty.colors.length = e.dirty.sizes.length = 0, this.cloudGpu.set(e, t), this.flushMask(e, t), t;
805
+ }
806
+ flushCloud(e, t) {
807
+ let n = this.device.queue, r = [
808
+ [
809
+ "positions",
810
+ t.positions,
811
+ e.positions,
812
+ 3
813
+ ],
814
+ [
815
+ "colors",
816
+ t.colors,
817
+ e.colors,
818
+ e.colorStride
819
+ ],
820
+ [
821
+ "sizes",
822
+ t.sizes,
823
+ e.sizes,
824
+ 1
825
+ ]
826
+ ];
827
+ for (let [t, i, a, o] of r) {
828
+ let r = e.dirty[t];
829
+ if (r.length) {
830
+ if (i && a) for (let [e, t] of x(r)) n.writeBuffer(i, e * o * 4, a, e * o, (t - e) * o), this.stats.uploads++, this.stats.uploadBytes += (t - e) * o * 4;
831
+ r.length = 0;
832
+ }
833
+ }
834
+ this.flushMask(e, t);
835
+ }
836
+ flushMask(e, t) {
837
+ t.mask && e.mask && t.maskVersion !== e.maskVersion && (this.device.queue.writeBuffer(t.mask, 0, e.mask), t.maskVersion = e.maskVersion, this.stats.uploads++, this.stats.uploadBytes += e.mask.byteLength);
838
+ }
839
+ releaseCloud(e) {
840
+ let t = this.cloudGpu.get(e);
841
+ t && (t.uniform.destroy(), t.positions.destroy(), t.colors?.destroy(), t.sizes?.destroy(), t.order?.destroy(), t.mask?.destroy(), this.cloudGpu.delete(e));
842
+ }
843
+ ensureBatch(e) {
844
+ let t = this.batchGpu.get(e);
845
+ if (t && t.structure === e.structure) return t;
846
+ t && this.releaseBatch(e);
847
+ let n = this.device, r = n.createBuffer({
848
+ size: 192,
849
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
850
+ }), i;
851
+ try {
852
+ i = n.createBuffer({
853
+ size: e.capacity * 16 * 4,
854
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
855
+ });
856
+ } catch (e) {
857
+ throw r.destroy(), e;
858
+ }
859
+ return t = {
860
+ structure: e.structure,
861
+ capacity: e.capacity,
862
+ uniform: r,
863
+ instances: i,
864
+ group: null,
865
+ groupView: null,
866
+ groupSampler: null,
867
+ floats: /* @__PURE__ */ new Float32Array(48),
868
+ written: (/* @__PURE__ */ new Float32Array(48)).fill(NaN)
869
+ }, e.count && (n.queue.writeBuffer(i, 0, e.data, 0, e.count * 16), this.stats.uploads++, this.stats.uploadBytes += e.count * 16 * 4), e.dirty.length = 0, this.batchGpu.set(e, t), t;
870
+ }
871
+ releaseBatch(e) {
872
+ let t = this.batchGpu.get(e);
873
+ t && (t.uniform.destroy(), t.instances.destroy(), this.batchGpu.delete(e));
874
+ }
875
+ releaseGpu() {
876
+ for (let e of [...this.cloudGpu.keys()]) this.releaseCloud(e);
877
+ for (let e of [...this.batchGpu.keys()]) this.releaseBatch(e);
878
+ for (let e of this.owned.values()) e.texture.destroy();
879
+ this.owned.clear(), this.edl?.texture.destroy(), this.edl = void 0, this.gpu && (this.gpu.frame.destroy(), this.gpu.dummy.destroy(), this.gpu.white.destroy(), this.gpu.edlParams.destroy()), this.fogGpu?.buffer.destroy(), this.fogGpu = void 0, this.gpu = void 0, this.device = void 0, this.pickViews.clear(), this.pickView = void 0, this.pipelines.clear(), this.edlLayout = null, this.hookLayouts.clear(), this.cloudCount = this.cloudEnd = this.spriteCount = this.spriteEnd = 0, this.mipCache.clear();
880
+ }
881
+ blend(e) {
882
+ return e === "additive" ? {
883
+ color: {
884
+ srcFactor: "src-alpha",
885
+ dstFactor: "one",
886
+ operation: "add"
887
+ },
888
+ alpha: {
889
+ srcFactor: "zero",
890
+ dstFactor: "one",
891
+ operation: "add"
892
+ }
893
+ } : k;
894
+ }
895
+ fogged(e) {
896
+ return this.fogSource !== null && e.fog;
897
+ }
898
+ fogResources() {
899
+ if (this.fogGpu) return this.fogGpu;
900
+ let e = this.device, t = this.gpu, n = e.createBindGroupLayout({ entries: [{
901
+ binding: 0,
902
+ visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
903
+ buffer: {
904
+ type: "uniform",
905
+ minBindingSize: 48
906
+ }
907
+ }] }), r = e.createBuffer({
908
+ size: 48,
909
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
910
+ });
911
+ return this.fogGpu = {
912
+ buffer: r,
913
+ layout: n,
914
+ group: e.createBindGroup({
915
+ layout: n,
916
+ entries: [{
917
+ binding: 0,
918
+ resource: { buffer: r }
919
+ }]
920
+ }),
921
+ pointLayout: e.createPipelineLayout({ bindGroupLayouts: [
922
+ t.frameLayout,
923
+ t.cloudLayout,
924
+ n
925
+ ] }),
926
+ spriteLayout: e.createPipelineLayout({ bindGroupLayouts: [
927
+ t.frameLayout,
928
+ t.spriteLayout,
929
+ n
930
+ ] }),
931
+ modules: /* @__PURE__ */ new Map()
932
+ };
933
+ }
934
+ fogModule(e, t) {
935
+ let n = this.fogResources().modules, r = e + t.blending, i = n.get(r);
936
+ return i || (i = this.device.createShaderModule({ code: j(e, t.blending === "additive") }), n.set(r, i)), i;
937
+ }
938
+ pointPipeline(e, t, n) {
939
+ let r = e.descriptor.depthStencil.format, i = t.shading === "sphere", a = this.fogged(t), o = `p|${e.descriptor.multisample?.count}|${e.format}|${r}|${n}|${i}|${t.writesDepth}|${t.depthTest}|${t.blending}${a ? "|fog" : ""}`, s = this.pipelines.get(o);
940
+ if (!s) {
941
+ let c = this.gpu, l = n && this.packedEdl(), u = n === "alone" || l ? this.device.createShaderModule({ code: M(a ? j("point", t.blending === "additive") : O) }) : a ? this.fogModule("point", t) : c.pointModule, d = [n === "alone" ? null : {
942
+ format: e.format,
943
+ blend: this.blend(t.blending)
944
+ }];
945
+ n && d.push({ format: l ? "rgba8unorm" : "r32float" }), s = this.device.createRenderPipeline({
946
+ layout: a ? this.fogResources().pointLayout : c.pointPipelineLayout,
947
+ vertex: {
948
+ module: u,
949
+ entryPoint: "vertex"
950
+ },
951
+ fragment: {
952
+ module: u,
953
+ entryPoint: `fragment${i ? "Sphere" : ""}${n ? "Edl" : ""}`,
954
+ targets: d
955
+ },
956
+ primitive: {
957
+ topology: "triangle-strip",
958
+ cullMode: "none"
959
+ },
960
+ multisample: e.descriptor.multisample,
961
+ depthStencil: {
962
+ format: r,
963
+ depthWriteEnabled: n !== "alone" && t.writesDepth,
964
+ depthCompare: t.depthTest ? "greater-equal" : "always"
965
+ }
966
+ }), this.pipelines.set(o, s);
967
+ }
968
+ return s;
969
+ }
970
+ spritePipeline(e, t) {
971
+ let n = e.descriptor.depthStencil.format, r = this.fogged(t), i = `s|${e.descriptor.multisample?.count}|${e.format}|${n}|${t.writesDepth}|${t.depthTest}|${t.blending}${r ? "|fog" : ""}`, a = this.pipelines.get(i);
972
+ if (!a) {
973
+ let o = this.gpu, s = r ? this.fogModule("sprite", t) : o.spriteModule;
974
+ a = this.device.createRenderPipeline({
975
+ layout: r ? this.fogResources().spriteLayout : o.spritePipelineLayout,
976
+ vertex: {
977
+ module: s,
978
+ entryPoint: "vertex"
979
+ },
980
+ fragment: {
981
+ module: s,
982
+ entryPoint: "fragment",
983
+ targets: [{
984
+ format: e.format,
985
+ blend: this.blend(t.blending)
986
+ }]
987
+ },
988
+ primitive: {
989
+ topology: "triangle-strip",
990
+ cullMode: "none"
991
+ },
992
+ multisample: e.descriptor.multisample,
993
+ depthStencil: {
994
+ format: n,
995
+ depthWriteEnabled: t.writesDepth,
996
+ depthCompare: t.depthTest ? "greater-equal" : "always"
997
+ }
998
+ }), this.pipelines.set(i, a);
999
+ }
1000
+ return a;
1001
+ }
1002
+ takes(e) {
1003
+ let t = this.transparency.hook;
1004
+ return !!t && e.transparent && !e.writesDepth && e.depthTest && (e.blending === "normal" || !!t.blends);
1005
+ }
1006
+ hookLayout(e) {
1007
+ let t = this.hookLayouts.get(e.layout);
1008
+ return t || this.hookLayouts.set(e.layout, t = this.device.createBindGroupLayout({ entries: [...e.layout] })), t;
1009
+ }
1010
+ surfacePipeline(e, t, n, r) {
1011
+ let i = n.blending === "additive", a = this.fogged(n), o = r.surface(3), s = e.descriptor.depthStencil.format, c = `t|${t}|${e.descriptor.multisample?.count}|${s}|${i}|${a}|${o}`, l = this.pipelines.get(c);
1012
+ if (!l) {
1013
+ let n = this.device, u = this.gpu, d = n.createShaderModule({ code: I(t, o, a ? i : void 0) });
1014
+ l = n.createRenderPipeline({
1015
+ layout: n.createPipelineLayout({ bindGroupLayouts: [
1016
+ u.frameLayout,
1017
+ t === "point" ? u.cloudLayout : u.spriteLayout,
1018
+ this.fogResources().layout,
1019
+ this.hookLayout(r)
1020
+ ] }),
1021
+ vertex: {
1022
+ module: d,
1023
+ entryPoint: "vertex"
1024
+ },
1025
+ fragment: {
1026
+ module: d,
1027
+ entryPoint: "transparent",
1028
+ constants: r.blends ? { margoKind: +i } : void 0,
1029
+ targets: [...r.targets]
1030
+ },
1031
+ primitive: {
1032
+ topology: "triangle-strip",
1033
+ cullMode: "none"
1034
+ },
1035
+ multisample: e.descriptor.multisample,
1036
+ depthStencil: {
1037
+ format: s,
1038
+ depthWriteEnabled: !1,
1039
+ depthCompare: "greater-equal"
1040
+ }
1041
+ }), this.pipelines.set(c, l);
1042
+ }
1043
+ return l;
1044
+ }
1045
+ setTransparency(e, t) {
1046
+ return this.transparency.set(e, t);
1047
+ }
1048
+ drawTransparent(e, t) {
1049
+ if (this.cloudEnd === this.cloudCount && this.spriteEnd === this.spriteCount) return;
1050
+ let n = this.gpu, r = this.stats, i = e, a = t.begin();
1051
+ a.setBindGroup(0, n.frameGroup), a.setBindGroup(2, this.fogResources().group), a.setBindGroup(3, this.device.createBindGroup({
1052
+ layout: this.hookLayout(t),
1053
+ entries: t.bindings()
1054
+ }));
1055
+ for (let e = this.cloudCount; e < this.cloudEnd; e++) {
1056
+ let { cloud: o, r: s, view: c, first: l, end: u } = this.cloudDraws[e];
1057
+ a.setPipeline(this.surfacePipeline(i, "point", o.material, t)), a.setBindGroup(1, this.cloudGroup(s, c, o.material.mapFilter === "nearest" ? n.nearest : n.linear));
1058
+ for (let e = l; e < u; e++) {
1059
+ let t = this.blockAllotment[e];
1060
+ t && (a.draw(4, t, 0, o.blocks.starts[this.entryBlock[e]]), r.drawCalls++);
1061
+ }
1062
+ }
1063
+ for (let e = this.spriteCount; e < this.spriteEnd; e++) {
1064
+ let { batch: n, group: o } = this.spriteDraws[e];
1065
+ a.setPipeline(this.surfacePipeline(i, "sprite", n.material, t)), a.setBindGroup(1, o), a.draw(4, n.count), r.drawCalls++;
1066
+ }
1067
+ }
1068
+ packedEdl() {
1069
+ return a(this.device) || s(this.device);
1070
+ }
1071
+ edlPipelines(e, t) {
1072
+ return a(this.device) ? Array.from({ length: t }, (n, r) => this.edlPipeline(e, t, r)) : [this.edlPipeline(e, t, null)];
1073
+ }
1074
+ edlSampledLayout() {
1075
+ let e = this.device;
1076
+ return this.edlLayout ??= e.createPipelineLayout({ bindGroupLayouts: [e.createBindGroupLayout({ entries: [{
1077
+ binding: 0,
1078
+ visibility: GPUShaderStage.FRAGMENT,
1079
+ texture: {
1080
+ sampleType: "unfilterable-float",
1081
+ multisampled: !0
1082
+ }
1083
+ }, {
1084
+ binding: 1,
1085
+ visibility: GPUShaderStage.FRAGMENT,
1086
+ buffer: { type: "uniform" }
1087
+ }] })] });
1088
+ }
1089
+ edlPipeline(e, t, n) {
1090
+ let r = `e|${e}|${t}|${n}`, i = this.pipelines.get(r);
1091
+ if (!i) {
1092
+ let a = this.gpu, o = n !== null || this.packedEdl(), s = P.replace("texture_2d<f32>", "texture_multisampled_2d<f32>"), c = t === 1 ? P : n === null ? s.replace("position:vec4f)", "position:vec4f,@builtin(sample_index) sample:u32)") : s, l = n === null ? t === 1 ? "0" : "i32(sample)" : String(n), u = !o && t === 1 ? a.edlModule : this.device.createShaderModule({ code: o ? c.replace(/textureLoad\(depths,([pq]),0\)\.r/g, `unpackDepth(textureLoad(depths,$1,${l}))`) + ie : c.replace(/textureLoad\(depths,([pq]),0\)/g, `textureLoad(depths,$1,${l})`) });
1093
+ i = this.device.createRenderPipeline({
1094
+ layout: t === 1 ? a.edlPipelineLayout : this.edlSampledLayout(),
1095
+ vertex: {
1096
+ module: u,
1097
+ entryPoint: "vertex"
1098
+ },
1099
+ fragment: {
1100
+ module: u,
1101
+ entryPoint: "fragment",
1102
+ targets: [{
1103
+ format: e,
1104
+ blend: {
1105
+ color: {
1106
+ srcFactor: "zero",
1107
+ dstFactor: "src",
1108
+ operation: "add"
1109
+ },
1110
+ alpha: {
1111
+ srcFactor: "zero",
1112
+ dstFactor: "one",
1113
+ operation: "add"
1114
+ }
1115
+ }
1116
+ }]
1117
+ },
1118
+ primitive: { topology: "triangle-list" },
1119
+ multisample: n === null ? { count: t } : {
1120
+ count: t,
1121
+ mask: 1 << n
1122
+ }
1123
+ }), this.pipelines.set(r, i);
1124
+ }
1125
+ return i;
1126
+ }
1127
+ edlTarget(e, t, n, r) {
1128
+ if (this.edl && this.edl.width === e && this.edl.height === t && this.edl.pipeline === r) return this.edl;
1129
+ this.edl?.texture.destroy();
1130
+ let i = this.device, a = this.gpu, o = i.createTexture({
1131
+ size: [e, t],
1132
+ sampleCount: n,
1133
+ format: this.packedEdl() ? "rgba8unorm" : "r32float",
1134
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
1135
+ }), s = o.createView(), c = i.createBindGroup({
1136
+ layout: r.getBindGroupLayout(0),
1137
+ entries: [{
1138
+ binding: 0,
1139
+ resource: s
1140
+ }, {
1141
+ binding: 1,
1142
+ resource: { buffer: a.edlParams }
1143
+ }]
1144
+ });
1145
+ return this.edl = {
1146
+ texture: o,
1147
+ view: s,
1148
+ group: c,
1149
+ width: e,
1150
+ height: t,
1151
+ pipeline: r
1152
+ };
1153
+ }
1154
+ writeFrame(e) {
1155
+ let n = this.frameData, r = e.camera.matrix, [i, a] = t(e);
1156
+ this.orthographic = ee(n, this.view, e.camera, i, a, e.format), n[27] = this.transparency.hook ? ge(e.camera.matrix, e.camera.position)?.near ?? 0 : 0;
1157
+ let s = !this.frameValid;
1158
+ for (let e = 0; !s && e < this.frameBits.length; e++) s = this.frameBits[e] !== this.frameWritten[e];
1159
+ s && (this.device.queue.writeBuffer(this.gpu.frame, 0, n), this.frameWritten.set(this.frameBits), this.frameValid = !0);
1160
+ let c = this.pickView = this.pickViews.get(e);
1161
+ c.viewProj.set(r), c.rect = [...o(e) ?? [
1162
+ 0,
1163
+ 0,
1164
+ i,
1165
+ a
1166
+ ]], c.canvas = !e.view?.target, c.orthographic = this.orthographic;
1167
+ }
1168
+ writeCloudUniform(e, t, n, r, i = 1, a = 1) {
1169
+ let o = e.material, s = t.floats, c = t.uints;
1170
+ s.set(n, 0), s.set(o.color, 16), s[19] = o.opacity, s[20] = (o.sizeUnits === "pixels" ? o.size * i : o.size) * a, s[21] = o.minPixelSize * i * a, s[22] = o.maxPixelSize * i * a, s[23] = o.alphaTest, c[24] = (e.colors && o.vertexColors ? F.colors : 0) | (e.sizes ? F.sizes : 0) | (o.sizeUnits === "world" ? F.world : 0) | (o.shape === "round" ? F.round : 0) | (r ? F.map : 0) | (o.shading === "sphere" ? F.sphere : 0) | (e.direct ? F.direct : 0) | (e.colorSpace === "srgb" ? F.srgbColors : 0) | (e.colorFormat === "float" ? F.floatColors : 0) | (e.mask ? F.mask : 0);
1171
+ let l = o.clipping;
1172
+ c[25] = l.length / 4 + (o.clipIntersection ? 65536 : 0), s[26] = o.ditherFade?.[0] ?? 0, s[27] = o.ditherFade?.[1] ?? 1, s.set(l, 32), s.fill(0, 32 + l.length);
1173
+ let u = !1;
1174
+ for (let e = 0; e < s.length; e++) if (!Object.is(t.written[e], s[e])) {
1175
+ u = !0;
1176
+ break;
1177
+ }
1178
+ u && (this.device.queue.writeBuffer(t.uniform, 0, s), t.written.set(s));
1179
+ }
1180
+ cloudGroup(e, t, n) {
1181
+ if (e.group && e.groupView === t && e.groupSampler === n) return e.group;
1182
+ let r = this.gpu, i = r.dummy;
1183
+ return e.group = this.device.createBindGroup({
1184
+ layout: r.cloudLayout,
1185
+ entries: [
1186
+ {
1187
+ binding: 0,
1188
+ resource: { buffer: e.uniform }
1189
+ },
1190
+ {
1191
+ binding: 1,
1192
+ resource: { buffer: e.positions }
1193
+ },
1194
+ {
1195
+ binding: 2,
1196
+ resource: { buffer: e.colors ?? i }
1197
+ },
1198
+ {
1199
+ binding: 3,
1200
+ resource: { buffer: e.sizes ?? i }
1201
+ },
1202
+ {
1203
+ binding: 4,
1204
+ resource: { buffer: e.order ?? i }
1205
+ },
1206
+ {
1207
+ binding: 5,
1208
+ resource: t
1209
+ },
1210
+ {
1211
+ binding: 6,
1212
+ resource: n
1213
+ },
1214
+ {
1215
+ binding: 7,
1216
+ resource: { buffer: e.mask ?? i }
1217
+ }
1218
+ ]
1219
+ }), e.groupView = t, e.groupSampler = n, e.group;
1220
+ }
1221
+ batchGroup(e, t, n) {
1222
+ return e.group && e.groupView === t && e.groupSampler === n || (e.group = this.device.createBindGroup({
1223
+ layout: this.gpu.spriteLayout,
1224
+ entries: [
1225
+ {
1226
+ binding: 0,
1227
+ resource: { buffer: e.uniform }
1228
+ },
1229
+ {
1230
+ binding: 1,
1231
+ resource: { buffer: e.instances }
1232
+ },
1233
+ {
1234
+ binding: 2,
1235
+ resource: t
1236
+ },
1237
+ {
1238
+ binding: 3,
1239
+ resource: n
1240
+ }
1241
+ ]
1242
+ }), e.groupView = t, e.groupSampler = n), e.group;
1243
+ }
1244
+ };
1245
+ //#endregion
1246
+ export { K as ASPRS_CLASSES, H as CLASSIFICATION_COLORS, W as ELEVATION_STOPS, u as NO_BLOCK, w as PointCloud, y as PointsMaterial, _e as PointsPlugin, se as SPRITE_STRIDE, Y as SpriteBatch, S as SpriteMaterial, b as allocateBudget, C as atlasRect, d as boxInFrustum, _ as boxScreenRect, T as buildBlocks, B as classificationColors, f as cullBlocks, p as expandBlockBounds, L as measureAngle, U as measureArea, G as measureHeight, R as measurePath, V as measurePointDistance, x as mergeRanges, v as packColors, g as parseColor, h as pickPoint, m as projectToPixels, q as rampColors, l as random, z as rgb16ToRgba8, E as screenArea, D as worldBox };