@margoengine/points 0.0.0-stage → 0.1.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 (52) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +794 -3
  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.d.ts +55 -0
  12. package/dist/copc.js +130 -0
  13. package/dist/depth-video.d.ts +98 -0
  14. package/dist/depth-video.js +164 -0
  15. package/dist/e57.d.ts +163 -0
  16. package/dist/e57.js +624 -0
  17. package/dist/frame.d.ts +9 -0
  18. package/dist/index.d.ts +16 -0
  19. package/dist/index.js +1246 -0
  20. package/dist/las.d.ts +213 -0
  21. package/dist/las.js +500 -0
  22. package/dist/laz.d.ts +50 -0
  23. package/dist/laz.js +1025 -0
  24. package/dist/material.d.ts +167 -0
  25. package/dist/math.d.ts +103 -0
  26. package/dist/octree.d.ts +182 -0
  27. package/dist/octree.js +360 -0
  28. package/dist/pick.d.ts +76 -0
  29. package/dist/pick.js +192 -0
  30. package/dist/plugin.d.ts +235 -0
  31. package/dist/potree.d.ts +70 -0
  32. package/dist/potree.js +451 -0
  33. package/dist/range-BQuJAjUM.js +23 -0
  34. package/dist/range.d.ts +4 -0
  35. package/dist/screen-door-CALlYs39.js +7 -0
  36. package/dist/shaders-Deu8H36P.js +278 -0
  37. package/dist/shaders.d.ts +52 -0
  38. package/dist/shared/fog.d.ts +33 -0
  39. package/dist/shared/pass.d.ts +53 -0
  40. package/dist/shared/selection-mask.d.ts +52 -0
  41. package/dist/shared/style-expression.d.ts +18 -0
  42. package/dist/sprites.d.ts +78 -0
  43. package/dist/storage-limit.d.ts +6 -0
  44. package/dist/style.d.ts +124 -0
  45. package/dist/style.js +708 -0
  46. package/dist/surfels.d.ts +95 -0
  47. package/dist/surfels.js +538 -0
  48. package/dist/survey-DToZwkJv.js +46 -0
  49. package/dist/survey.d.ts +10 -0
  50. package/dist/tier-BcFbLPVT.js +91 -0
  51. package/dist/tier.d.ts +6 -0
  52. package/package.json +96 -6
@@ -0,0 +1,95 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
3
+ import type { EngineKernel } from '@margoengine/core';
4
+ import { type FogLike } from './shared/fog.js';
5
+ import { PointCloud } from './cloud.js';
6
+ /** Surfel shader: one disc per point, read from the same storage buffers as the point shader. */
7
+ export declare const surfelShader: string;
8
+ /** Resolve shader: the weighted colour sum over its weight. `EDGE` weight or more is fully opaque. */
9
+ export declare const resolveShader: string;
10
+ /** Options of `SurfelPlugin`. An empty name or non-finite order throws a TypeError, a value out of range a RangeError. */
11
+ export interface SurfelPluginOptions {
12
+ /** Plugin name, unique per engine. Default 'surfels'. */
13
+ name?: string;
14
+ /** Frame graph order. Default 10, as `PointsPlugin`: after the scene, before fat lines and post-processing. */
15
+ order?: number;
16
+ /** Most points drawn per frame across all clouds. Visible blocks share it by screen area. Default 1,000,000. */
17
+ pointBudget?: number;
18
+ /** Disc diameter as a multiple of the point's size: discs must overlap to blend. Above 0. Default 1.5. */
19
+ overlap?: number;
20
+ /** Depth behind the front surface that still blends, as a multiple of the disc's radius. Above 0. Default 1. */
21
+ blendDepth?: number;
22
+ }
23
+ /**
24
+ * Draws `PointCloud`s as blended surfels over the scene colour and depth (WebGPU), so a scan looks like a continuous
25
+ * surface. A cloud goes to one plugin: add it here instead of to a `PointsPlugin`. `OctreeCloud` takes this plugin
26
+ * in place of `PointsPlugin`. On the WebGL2 backend it warns once and draws nothing.
27
+ */
28
+ export declare class SurfelPlugin implements EnginePlugin {
29
+ readonly name: string;
30
+ readonly order: number;
31
+ pointBudget: number;
32
+ overlap: number;
33
+ blendDepth: number;
34
+ /** Counters of the latest frame. */
35
+ readonly stats: {
36
+ clouds: number;
37
+ blocksVisible: number;
38
+ pointsDrawn: number;
39
+ drawCalls: number;
40
+ };
41
+ private readonly clouds;
42
+ private readonly cloudGpu;
43
+ private readonly pipelines;
44
+ private readonly resolves;
45
+ private fogSource;
46
+ private engine?;
47
+ private device?;
48
+ private gpu?;
49
+ private targets?;
50
+ private readonly frameData;
51
+ private readonly fogData;
52
+ private readonly view;
53
+ private readonly box;
54
+ private readonly entryBlock;
55
+ private readonly entryCount;
56
+ private readonly entryWeight;
57
+ private allotment;
58
+ private readonly draws;
59
+ /** Same as `engine.use(new SurfelPlugin(...))`; returns the plugin. */
60
+ static attach(engine: EngineKernel, options?: SurfelPluginOptions): SurfelPlugin;
61
+ constructor(options?: SurfelPluginOptions);
62
+ /** Clouds drawn by this plugin. */
63
+ get pointClouds(): ReadonlySet<PointCloud>;
64
+ /** The fog clouds are drawn in (see `setFog`), or null. */
65
+ get fog(): FogLike | null;
66
+ /**
67
+ * Fogs every cloud whose material has `fog` true, as `PointsPlugin.setFog` does: `@margoengine/environment`'s `Fog` or
68
+ * any object with its fields, read every frame. A point takes the fog at its centre. `null` draws without fog.
69
+ * @throws TypeError when `fog` is neither an object nor null; RangeError or TypeError for an invalid field.
70
+ */
71
+ setFog(fog: FogLike | null): this;
72
+ /** Adds a cloud and returns it. */
73
+ add<T extends PointCloud>(cloud: T): T;
74
+ /** Removes a cloud and frees its GPU buffers. */
75
+ remove(cloud: unknown): void;
76
+ clear(): void;
77
+ setup(engine: Engine): void;
78
+ registerPasses(graph: FrameGraph): void;
79
+ onDeviceRestored(engine: Engine): void;
80
+ /** Releases every GPU resource. Clouds stay registered, so the plugin can be used again. */
81
+ dispose(): void;
82
+ [Symbol.dispose](): void;
83
+ private ensure;
84
+ private flush;
85
+ private release;
86
+ private releaseGpu;
87
+ private releaseTargets;
88
+ /** A sampled-texture layout of `count` depth or sum targets; multisampled ones are unfilterable by definition. */
89
+ private textureLayout;
90
+ private frameTargets;
91
+ /** The depth, blend and resolve pipelines of one target setup; on native WebGPU at 4x, one resolve per sample. */
92
+ private passes;
93
+ private writeCloud;
94
+ private readonly execute;
95
+ }
@@ -0,0 +1,538 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { c as e, d as t, i as n, n as r, o as i, r as a, t as o, u as s } from "./tier-BcFbLPVT.js";
5
+ import { c, o as l, p as u, t as d, v as f, y as p } from "./cloud-B4O_G-Df.js";
6
+ import { a as m, d as h, g, h as _, m as v, n as y, p as b, s as x, t as S } from "./shaders-Deu8H36P.js";
7
+ import { meshWorldMatrix as C } from "@margoengine/math/mesh";
8
+ //#region src/surfels.ts
9
+ var w = 512, T = 1024, E = 224, D = 3e38, O = ((e) => ({
10
+ r: e[0] / 255,
11
+ g: e[1] / 255,
12
+ b: e[2] / 255,
13
+ a: e[3] / 255
14
+ }))(new Uint8Array(new Float32Array([D]).buffer)), k = (e, t = e) => m + (e ? y : "") + v(3, 0) + `
15
+ struct Cloud{model:mat4x4f,color:vec4f,size:vec4f,flags:vec4u,blend:vec4f,planes:array<vec4f,6>}
16
+ @group(1) @binding(0) var<uniform> cloud:Cloud;
17
+ @group(1) @binding(1) var<storage,read> positions:array<f32>;
18
+ @group(1) @binding(2) var<storage,read> colors:array<u32>;
19
+ @group(1) @binding(3) var<storage,read> sizes:array<f32>;
20
+ @group(1) @binding(4) var<storage,read> order:array<u32>;
21
+ @group(1) @binding(5) var<storage,read> normals:array<f32>;
22
+ @group(2) @binding(0) var front:texture_2d<f32>;
23
+ struct VOut{
24
+ @builtin(position) clip:vec4f,
25
+ @location(0) @interpolate(flat) color:vec4f,
26
+ @location(1) quad:vec2f,
27
+ @location(2) world:vec3f,
28
+ @location(3) @interpolate(flat) reach:f32,
29
+ }
30
+ fn has(bit:u32)->bool{return (cloud.flags.x&bit)!=0u;}
31
+ ${x}
32
+ fn disc(vi:u32,slot:u32,pushed:bool)->VOut{
33
+ var out:VOut;
34
+ var p=slot;
35
+ if(!has(${S.direct}u)){p=order[slot];}
36
+ let center=cloud.model*vec4f(positions[p*3u],positions[p*3u+1u],positions[p*3u+2u],1.0);
37
+ let middle=frame.viewProj*center;
38
+ var color=vec4f(cloud.color.rgb,1.0);
39
+ if(has(${S.colors}u)){color*=pointColor(p);}
40
+
41
+ if(!(middle.w>1e-6)||color.a<=0.0||color.a<cloud.color.w){out.clip=vec4f(0.0,0.0,-1.0,1.0);return out;}
42
+ if(has(${T}u)){color=vec4f(mix(color.rgb,margoFog.color.rgb,fogAmount(center.xyz)),color.a);}
43
+ var size=cloud.size.x;
44
+ if(has(${S.sizes}u)){size*=sizes[p];}
45
+ let perUnit=frame.viewport.z/middle.w;
46
+ let px=clamp(select(size,size*perUnit,has(${S.world}u)),cloud.size.y,cloud.size.z);
47
+ let radius=0.5*px*cloud.size.w/perUnit;
48
+ var right=frame.right.xyz;
49
+ var up=frame.up.xyz;
50
+ if(has(${w}u)){
51
+ let m=(cloud.model*vec4f(normals[p*3u],normals[p*3u+1u],normals[p*3u+2u],0.0)).xyz;
52
+ if(dot(m,m)>1e-12){
53
+ let n=normalize(m);
54
+ right=normalize(cross(n,select(vec3f(0.0,1.0,0.0),vec3f(1.0,0.0,0.0),abs(n.y)>0.9)));
55
+ up=cross(n,right);
56
+ }
57
+ }
58
+ let corner=CORNERS[vi];
59
+ out.world=center.xyz+(right*corner.x+up*corner.y)*radius;
60
+ out.clip=frame.viewProj*vec4f(out.world,1.0);
61
+ out.reach=cloud.blend.x*radius;
62
+ if(pushed){
63
+ let deep=frame.viewProj*vec4f(out.world-frame.back.xyz*out.reach,1.0);
64
+ out.clip.z=deep.z/deep.w*out.clip.w;
65
+ }
66
+ out.color=color;out.quad=corner;
67
+ return out;
68
+ }
69
+ @vertex fn vertexDepth(@builtin(vertex_index) vi:u32,@builtin(instance_index) slot:u32)->VOut{return disc(vi,slot,true);}
70
+ @vertex fn vertexBlend(@builtin(vertex_index) vi:u32,@builtin(instance_index) slot:u32)->VOut{return disc(vi,slot,false);}
71
+
72
+ fn cut(i:VOut)->bool{
73
+ if(dot(i.quad,i.quad)>1.0){return true;}
74
+ let count=cloud.flags.y&0xffffu;
75
+ var behind=0u;
76
+ for(var k=0u;k<count;k++){if(dot(cloud.planes[k].xyz,i.world)+cloud.planes[k].w<0.0){behind++;}}
77
+ return count>0u&&select(behind>0u,behind==count,(cloud.flags.y&65536u)!=0u);
78
+ }
79
+ @fragment fn fragmentDepth(i:VOut)->@location(0) ${e ? "vec4f" : "f32"}{
80
+ if(cut(i)){discard;}
81
+ return ${e ? "packDepth(-dot(i.world,frame.back.xyz))" : "-dot(i.world,frame.back.xyz)"};
82
+ }
83
+ ${t ? "struct Sums{@location(0) rg:vec4f,@location(1) ba:vec4f}" : ""}
84
+ @fragment fn fragmentBlend(i:VOut)->${t ? "Sums" : "@location(0) vec4f"}{
85
+ if(cut(i)){discard;}
86
+ let behind=max(-dot(i.world,frame.back.xyz)-${e ? "unpackDepth(textureLoad(front,vec2i(i.clip.xy),0))" : "textureLoad(front,vec2i(i.clip.xy),0).r"},0.0);
87
+
88
+ let w=exp(-2.0*dot(i.quad,i.quad))*(1.0-smoothstep(0.0,max(i.reach,1e-20),behind))*i.color.a;
89
+ let c=i.color.rgb*w;
90
+ return ${t ? "Sums(vec4f(c.rg,0.0,0.0),vec4f(c.b,w,0.0,0.0))" : "vec4f(c,w)"};
91
+ }
92
+ `, A = k(!1), j = (e, t = !1) => m + `
93
+ @group(1) @binding(0) var sums:texture_2d<f32>;
94
+ ${t ? "@group(1) @binding(1) var rest:texture_2d<f32>;" : ""}
95
+ const EDGE=0.25;
96
+ @vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f{
97
+ return vec4f(f32(i&1u)*4.0-1.0,f32(i>>1u)*4.0-1.0,0.0,1.0);
98
+ }
99
+ @fragment fn fragment(@builtin(position) position:vec4f)->@location(0) vec4f{
100
+ let q=vec2i(position.xy);
101
+ let sum=${e === void 0 ? "textureLoad(sums,q,0)" : t ? `vec4f(textureLoad(sums,q,${e}).rg,textureLoad(rest,q,${e}).rg)` : `textureLoad(sums,q,${e})`};
102
+ if(sum.a<=0.0){discard;}
103
+ return encode(vec4f(sum.rgb/sum.a,clamp(sum.a/EDGE,0.0,1.0)));
104
+ }
105
+ `, M = j(), N = class m {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
106
+ static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
107
+ let n = new m(t);
108
+ return e.use(n), n;
109
+ }
110
+ constructor(t = {}) {
111
+ if (this.stats = {
112
+ clouds: 0,
113
+ blocksVisible: 0,
114
+ pointsDrawn: 0,
115
+ drawCalls: 0
116
+ }, this.clouds = /* @__PURE__ */ new Set(), this.cloudGpu = /* @__PURE__ */ new Map(), this.pipelines = /* @__PURE__ */ new Map(), this.resolves = /* @__PURE__ */ new Map(), this.fogSource = null, this.frameData = /* @__PURE__ */ new Float32Array(36), this.fogData = /* @__PURE__ */ new Float32Array(12), this.view = /* @__PURE__ */ new Float32Array(16), this.box = /* @__PURE__ */ new Float64Array(6), this.entryBlock = [], this.entryCount = [], this.entryWeight = [], this.allotment = /* @__PURE__ */ new Uint32Array(), this.draws = [], this.execute = (t) => {
117
+ let n = this.stats;
118
+ if (n.clouds = n.blocksVisible = n.pointsDrawn = n.drawCalls = 0, t.backend !== "webgpu" || !this.device || !this.gpu || !this.engine || t.device !== this.device || !this.clouds.size) return;
119
+ let r = this.engine, a = this.device, o = this.gpu, s = t.camera, u = this.frameData, [d, m] = e(t), h = g(u, this.view, s, d, m, t.format);
120
+ a.queue.writeBuffer(o.frame, 0, u), this.fogSource && (_(this.fogSource, s.position, this.fogData), a.queue.writeBuffer(o.fog, 0, this.fogData));
121
+ let v = this.entryBlock, y = this.entryCount, b = this.entryWeight, x = this.draws;
122
+ v.length = y.length = b.length = 0;
123
+ let S = 0;
124
+ for (let e of this.clouds) {
125
+ if (!e.visible || !e.count) continue;
126
+ let t = e.attachedMesh, i = t ? C(r, t) : e.matrix;
127
+ if (!i) continue;
128
+ e.material.validate();
129
+ let a = this.ensure(e);
130
+ this.flush(e, a), this.writeCloud(e, a, i), n.clouds++;
131
+ let o = e.blocks, l = v.length;
132
+ for (let t = 0; t < o.length; t++) o.counts[t] && (p(o.bounds, t, i, this.box), c(this.box, s.planes, e.cullMargin) && (v.push(t), y.push(o.counts[t]), b.push(f(this.box, s.position, u[18], u[16], u[17], h))));
133
+ n.blocksVisible += v.length - l, v.length > l && (x[S++] = {
134
+ cloud: e,
135
+ r: a,
136
+ first: l,
137
+ end: v.length
138
+ });
139
+ }
140
+ if (!S) return;
141
+ this.allotment.length < v.length && (this.allotment = new Uint32Array(Math.max(16, v.length, this.allotment.length * 2)));
142
+ let w = this.allotment;
143
+ n.pointsDrawn = l(y, b, v.length, this.pointBudget, w);
144
+ let T = t, E = this.frameTargets(T), [k, A, j] = this.passes(T, E), M = t.descriptor.depthStencil.format.includes("stencil"), N = {
145
+ view: t.depth,
146
+ depthLoadOp: "load",
147
+ depthStoreOp: "store",
148
+ ...M ? { stencilReadOnly: !0 } : {}
149
+ }, P = (e) => {
150
+ i(e, t), e.setBindGroup(0, o.frameGroup), e.setBindGroup(3, o.fogGroup);
151
+ for (let t = 0; t < S; t++) {
152
+ let { cloud: r, r: i, first: a, end: o } = x[t];
153
+ e.setBindGroup(1, i.group);
154
+ for (let t = a; t < o; t++) w[t] && (e.draw(4, w[t], 0, r.blocks.starts[v[t]]), n.drawCalls++);
155
+ }
156
+ e.end();
157
+ }, F = t.encoder.beginRenderPass({
158
+ colorAttachments: [{
159
+ view: E.frontView,
160
+ loadOp: "clear",
161
+ storeOp: "store",
162
+ clearValue: E.packed ? O : {
163
+ r: D,
164
+ g: 0,
165
+ b: 0,
166
+ a: 0
167
+ }
168
+ }],
169
+ depthStencilAttachment: N
170
+ });
171
+ F.setPipeline(k), F.setBindGroup(2, E.depthGroup), P(F);
172
+ let I = t.encoder.beginRenderPass({
173
+ colorAttachments: E.sumsViews.map((e) => ({
174
+ view: e,
175
+ loadOp: "clear",
176
+ storeOp: "store",
177
+ clearValue: {
178
+ r: 0,
179
+ g: 0,
180
+ b: 0,
181
+ a: 0
182
+ }
183
+ })),
184
+ depthStencilAttachment: N
185
+ });
186
+ I.setPipeline(A), I.setBindGroup(2, E.frontGroup), P(I);
187
+ let L = t.encoder.beginRenderPass({ colorAttachments: [{
188
+ view: t.color,
189
+ loadOp: "load",
190
+ storeOp: "store"
191
+ }] });
192
+ i(L, t), L.setBindGroup(0, o.frameGroup), L.setBindGroup(1, E.sumsGroup);
193
+ for (let e of j) L.setPipeline(e), L.draw(3), n.drawCalls++;
194
+ L.end();
195
+ }, this.name = t.name ?? "surfels", this.order = t.order ?? 10, this.pointBudget = t.pointBudget ?? 1e6, this.overlap = t.overlap ?? 1.5, this.blendDepth = t.blendDepth ?? 1, !this.name || typeof this.name != "string" || !Number.isFinite(this.order)) throw TypeError("SurfelPlugin needs a non-empty string name and a finite order; fix the options.");
196
+ if (!(this.pointBudget >= 0) || !(this.overlap > 0) || !(this.blendDepth > 0)) throw RangeError("Invalid SurfelPlugin option; pointBudget must be 0 or more, overlap and blendDepth above 0");
197
+ }
198
+ get pointClouds() {
199
+ return this.clouds;
200
+ }
201
+ get fog() {
202
+ return this.fogSource;
203
+ }
204
+ setFog(e) {
205
+ return this.fogSource = b(e), this;
206
+ }
207
+ add(e) {
208
+ if (!(e instanceof d)) throw TypeError("Expected a PointCloud; surfels draw point clouds only");
209
+ return e.bind(this), this.clouds.add(e), e;
210
+ }
211
+ remove(e) {
212
+ e instanceof d && this.clouds.delete(e) && (this.release(e), e.bind(void 0));
213
+ }
214
+ clear() {
215
+ for (let e of [...this.clouds]) this.remove(e);
216
+ }
217
+ setup(e) {
218
+ t(e, "SurfelPlugin"), n(e.gpu, "SurfelPlugin weighted sums", "half-float"), this.engine = e;
219
+ let r = this.device = e.gpu, i = GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, a = (e) => r.createBuffer({
220
+ size: e,
221
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
222
+ }), o = (e) => ({
223
+ binding: e,
224
+ visibility: GPUShaderStage.VERTEX,
225
+ buffer: { type: "read-only-storage" }
226
+ }), s = r.createBindGroupLayout({ entries: [{
227
+ binding: 0,
228
+ visibility: i,
229
+ buffer: {
230
+ type: "uniform",
231
+ minBindingSize: 144
232
+ }
233
+ }] }), c = r.createBindGroupLayout({ entries: [{
234
+ binding: 0,
235
+ visibility: i,
236
+ buffer: {
237
+ type: "uniform",
238
+ minBindingSize: 48
239
+ }
240
+ }] }), l = a(144), u = a(48);
241
+ this.gpu = {
242
+ module: r.createShaderModule({ code: A }),
243
+ frame: l,
244
+ frameLayout: s,
245
+ frameGroup: r.createBindGroup({
246
+ layout: s,
247
+ entries: [{
248
+ binding: 0,
249
+ resource: { buffer: l }
250
+ }]
251
+ }),
252
+ cloudLayout: r.createBindGroupLayout({ entries: [
253
+ {
254
+ binding: 0,
255
+ visibility: i,
256
+ buffer: {
257
+ type: "uniform",
258
+ minBindingSize: E
259
+ }
260
+ },
261
+ o(1),
262
+ o(2),
263
+ o(3),
264
+ o(4),
265
+ o(5)
266
+ ] }),
267
+ fog: u,
268
+ fogLayout: c,
269
+ fogGroup: r.createBindGroup({
270
+ layout: c,
271
+ entries: [{
272
+ binding: 0,
273
+ resource: { buffer: u }
274
+ }]
275
+ }),
276
+ dummy: r.createBuffer({
277
+ size: 16,
278
+ usage: GPUBufferUsage.STORAGE
279
+ })
280
+ };
281
+ }
282
+ registerPasses(e) {
283
+ e.add({
284
+ name: `${this.name}:draw`,
285
+ order: this.order,
286
+ execute: this.execute
287
+ });
288
+ }
289
+ onDeviceRestored(e) {
290
+ this.releaseGpu(), this.setup(e);
291
+ }
292
+ dispose() {
293
+ let e = this.engine;
294
+ this.releaseGpu(), this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
295
+ }
296
+ [Symbol.dispose]() {
297
+ this.dispose();
298
+ }
299
+ ensure(e) {
300
+ let t = this.cloudGpu.get(e);
301
+ if (t && t.structure === e.structure) return t;
302
+ t && this.release(e);
303
+ let n = this.device, r = this.gpu, i = e.count;
304
+ s(e, n.limits, e.normals ? 12 : 0);
305
+ let a = (e, t) => {
306
+ if (!e) return null;
307
+ let r = n.createBuffer({
308
+ size: Math.max(16, t),
309
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
310
+ });
311
+ return i && n.queue.writeBuffer(r, 0, e, 0, t / 4), r;
312
+ }, o = a(e.positions, i * 12), c = a(e.colors, i * 4 * e.colorStride), l = a(e.sizes, i * 4), u = a(e.direct ? null : e.blocks.order, i * 4), d = a(e.normals, i * 12), f = n.createBuffer({
313
+ size: E,
314
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
315
+ }), p = /* @__PURE__ */ new ArrayBuffer(E);
316
+ return t = {
317
+ structure: e.structure,
318
+ uniform: f,
319
+ positions: o,
320
+ colors: c,
321
+ sizes: l,
322
+ order: u,
323
+ normals: d,
324
+ group: n.createBindGroup({
325
+ layout: r.cloudLayout,
326
+ entries: [
327
+ {
328
+ binding: 0,
329
+ resource: { buffer: f }
330
+ },
331
+ {
332
+ binding: 1,
333
+ resource: { buffer: o }
334
+ },
335
+ {
336
+ binding: 2,
337
+ resource: { buffer: c ?? r.dummy }
338
+ },
339
+ {
340
+ binding: 3,
341
+ resource: { buffer: l ?? r.dummy }
342
+ },
343
+ {
344
+ binding: 4,
345
+ resource: { buffer: u ?? r.dummy }
346
+ },
347
+ {
348
+ binding: 5,
349
+ resource: { buffer: d ?? r.dummy }
350
+ }
351
+ ]
352
+ }),
353
+ floats: new Float32Array(p),
354
+ uints: new Uint32Array(p)
355
+ }, e.dirty.positions.length = e.dirty.colors.length = e.dirty.sizes.length = 0, this.cloudGpu.set(e, t), t;
356
+ }
357
+ flush(e, t) {
358
+ let n = this.device.queue, r = [
359
+ [
360
+ "positions",
361
+ t.positions,
362
+ e.positions,
363
+ 3
364
+ ],
365
+ [
366
+ "colors",
367
+ t.colors,
368
+ e.colors,
369
+ e.colorStride
370
+ ],
371
+ [
372
+ "sizes",
373
+ t.sizes,
374
+ e.sizes,
375
+ 1
376
+ ]
377
+ ];
378
+ for (let [t, i, a, o] of r) {
379
+ let r = e.dirty[t];
380
+ if (r.length) {
381
+ if (i && a) for (let [e, t] of u(r)) n.writeBuffer(i, e * o * 4, a, e * o, (t - e) * o);
382
+ r.length = 0;
383
+ }
384
+ }
385
+ }
386
+ release(e) {
387
+ let t = this.cloudGpu.get(e);
388
+ if (t) {
389
+ for (let e of [
390
+ t.uniform,
391
+ t.positions,
392
+ t.colors,
393
+ t.sizes,
394
+ t.order,
395
+ t.normals
396
+ ]) e?.destroy();
397
+ this.cloudGpu.delete(e);
398
+ }
399
+ }
400
+ releaseGpu() {
401
+ for (let e of [...this.cloudGpu.keys()]) this.release(e);
402
+ if (this.releaseTargets(), this.gpu) for (let e of [
403
+ this.gpu.frame,
404
+ this.gpu.fog,
405
+ this.gpu.dummy
406
+ ]) e.destroy();
407
+ this.gpu = void 0, this.device = void 0, this.pipelines.clear(), this.resolves.clear();
408
+ }
409
+ releaseTargets() {
410
+ for (let e of this.targets?.textures ?? []) e.destroy();
411
+ this.targets = void 0;
412
+ }
413
+ textureLayout(e, t = 1) {
414
+ return this.device.createBindGroupLayout({ entries: Array.from({ length: t }, (t, n) => ({
415
+ binding: n,
416
+ visibility: GPUShaderStage.FRAGMENT,
417
+ texture: {
418
+ sampleType: "unfilterable-float",
419
+ multisampled: e > 1
420
+ }
421
+ })) });
422
+ }
423
+ frameTargets(e) {
424
+ let t = e.descriptor.multisample?.count ?? 1, { width: n, height: i } = e, s = this.targets;
425
+ if (s && s.width === n && s.height === i && s.samples === t) return s;
426
+ let c = this.device, l = t > 1 && a(c), u = t > 1 && r(c) === "native", d = l || o(c);
427
+ this.releaseTargets();
428
+ let f = this.textureLayout(t), p = (e) => c.createTexture({
429
+ size: [n, i],
430
+ sampleCount: t,
431
+ format: e,
432
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
433
+ }), m = [p(d ? "rgba8unorm" : "r32float"), ...l ? [p("rg16float"), p("rg16float")] : [p("rgba16float")]], [h, ...g] = m.map((e) => e.createView()), _ = (e, t = f) => c.createBindGroup({
434
+ layout: t,
435
+ entries: e.map((e, t) => ({
436
+ binding: t,
437
+ resource: e
438
+ }))
439
+ }), v = l ? _(g, this.textureLayout(t, 2)) : _(g);
440
+ return this.targets = {
441
+ width: n,
442
+ height: i,
443
+ samples: t,
444
+ split: l,
445
+ masked: u,
446
+ packed: d,
447
+ textures: m,
448
+ frontView: h,
449
+ sumsViews: g,
450
+ frontGroup: _([h]),
451
+ sumsGroup: v,
452
+ depthGroup: l ? _(g.slice(0, 1)) : v
453
+ };
454
+ }
455
+ passes(e, t) {
456
+ let { samples: n, split: r, masked: i, packed: a } = t, o = e.descriptor.depthStencil.format, s = `${n}|${+r}|${+i}|${+a}|${e.format}|${o}`, c = this.resolves.get(s);
457
+ if (c) return [
458
+ this.pipelines.get(`d${s}`),
459
+ this.pipelines.get(`b${s}`),
460
+ c
461
+ ];
462
+ let l = this.device, u = this.gpu, d = l.createPipelineLayout({ bindGroupLayouts: [
463
+ u.frameLayout,
464
+ u.cloudLayout,
465
+ this.textureLayout(n),
466
+ u.fogLayout
467
+ ] }), f = e.descriptor.multisample, p = n > 1 || a ? l.createShaderModule({ code: P(k(a, r), n, !1) }) : u.module, m = (e, t, n, r) => l.createRenderPipeline({
468
+ layout: d,
469
+ vertex: {
470
+ module: p,
471
+ entryPoint: e
472
+ },
473
+ fragment: {
474
+ module: p,
475
+ entryPoint: t,
476
+ targets: n
477
+ },
478
+ primitive: {
479
+ topology: "triangle-strip",
480
+ cullMode: "none"
481
+ },
482
+ multisample: f,
483
+ depthStencil: {
484
+ format: o,
485
+ depthWriteEnabled: r,
486
+ depthCompare: "greater-equal"
487
+ }
488
+ }), g = m("vertexDepth", "fragmentDepth", [{ format: a ? "rgba8unorm" : "r32float" }], !0), _ = {
489
+ srcFactor: "one",
490
+ dstFactor: "one",
491
+ operation: "add"
492
+ }, v = (e) => ({
493
+ format: e,
494
+ blend: {
495
+ color: _,
496
+ alpha: _
497
+ }
498
+ }), y = m("vertexBlend", "fragmentBlend", r ? [v("rg16float"), v("rg16float")] : [v("rgba16float")], !1), b = l.createPipelineLayout({ bindGroupLayouts: [u.frameLayout, this.textureLayout(n, r ? 2 : 1)] }), x = (t, n) => {
499
+ let r = l.createShaderModule({ code: t });
500
+ return l.createRenderPipeline({
501
+ layout: b,
502
+ vertex: {
503
+ module: r,
504
+ entryPoint: "vertex"
505
+ },
506
+ fragment: {
507
+ module: r,
508
+ entryPoint: "fragment",
509
+ targets: [{
510
+ format: e.format,
511
+ blend: h
512
+ }]
513
+ },
514
+ primitive: { topology: "triangle-list" },
515
+ multisample: n
516
+ });
517
+ }, S = i ? Array.from({ length: n }, (e, t) => x(P(j(t, r), n, !1), {
518
+ count: n,
519
+ mask: 1 << t
520
+ })) : [x(P(M, n, !0), { count: n })];
521
+ return this.pipelines.set(`d${s}`, g), this.pipelines.set(`b${s}`, y), this.resolves.set(s, S), [
522
+ g,
523
+ y,
524
+ S
525
+ ];
526
+ }
527
+ writeCloud(e, t, n) {
528
+ let r = e.material, i = t.floats, a = t.uints, o = r.clipping;
529
+ i.set(n, 0), i.set(r.color, 16), i[19] = r.alphaTest, i[20] = r.size, i[21] = r.minPixelSize, i[22] = r.maxPixelSize, i[23] = this.overlap, a[24] = (e.colors && r.vertexColors ? S.colors : 0) | (e.sizes ? S.sizes : 0) | (r.sizeUnits === "world" ? S.world : 0) | (e.direct ? S.direct : 0) | (e.colorSpace === "srgb" ? S.srgbColors : 0) | (e.colorFormat === "float" ? S.floatColors : 0) | (t.normals ? w : 0) | (this.fogSource && r.fog ? T : 0), a[25] = o.length / 4 + (r.clipIntersection ? 65536 : 0), a[26] = a[27] = 0, i[28] = this.blendDepth, i.set(o, 32), i.fill(0, 32 + o.length), this.device.queue.writeBuffer(t.uniform, 0, i);
530
+ }
531
+ };
532
+ function P(e, t, n) {
533
+ if (t === 1) return e;
534
+ let r = e.replaceAll("texture_2d<f32>", "texture_multisampled_2d<f32>");
535
+ return n ? r.replace("position:vec4f)", "position:vec4f,@builtin(sample_index) sample:u32)").replace("sums,q,0)", "sums,q,i32(sample))") : r;
536
+ }
537
+ //#endregion
538
+ export { N as SurfelPlugin, M as resolveShader, A as surfelShader };
@@ -0,0 +1,46 @@
1
+ import { t as e } from "./cloud-B4O_G-Df.js";
2
+ //#region src/survey.ts
3
+ function t(t) {
4
+ if (!t?.positions || t.positions.length % 3) throw TypeError("Pass xyz positions with a length divisible by three");
5
+ if (t instanceof e && t.attachedMesh) throw TypeError("For an attached cloud, pass {positions, matrix: worldMatrix} explicitly");
6
+ if (t.matrix && (t.matrix.length !== 16 || !Array.from(t.matrix).every(Number.isFinite))) throw TypeError("Pass a finite column-major matrix of 16 numbers");
7
+ }
8
+ function n(e, t, n) {
9
+ let r = e[t * 3], i = e[t * 3 + 1], a = e[t * 3 + 2];
10
+ if (!n) return [
11
+ r,
12
+ i,
13
+ a
14
+ ];
15
+ let o = n[3] * r + n[7] * i + n[11] * a + n[15];
16
+ return [
17
+ (n[0] * r + n[4] * i + n[8] * a + n[12]) / o,
18
+ (n[1] * r + n[5] * i + n[9] * a + n[13]) / o,
19
+ (n[2] * r + n[6] * i + n[10] * a + n[14]) / o
20
+ ];
21
+ }
22
+ function r(t, n) {
23
+ let r = (e, t) => {
24
+ let r = new Float32Array(n.length * t);
25
+ for (let i = 0; i < n.length; i++) for (let a = 0; a < t; a++) r[i * t + a] = e[n[i] * t + a];
26
+ return r;
27
+ }, i = new e({
28
+ positions: r(t.positions, 3),
29
+ normals: t.normals ? r(t.normals, 3) : null,
30
+ sizes: t.sizes ? r(t.sizes, 1) : null,
31
+ colors: t.floatColors ? r(t.floatColors, 4) : t.colors ? new Uint8Array(n.length * 4) : null,
32
+ colorComponents: 4,
33
+ colorFormat: t.colorFormat,
34
+ colorSpace: t.colorSpace,
35
+ matrix: t.matrix,
36
+ material: t.material,
37
+ blockSize: t.blockSize,
38
+ seed: t.seed,
39
+ cullMargin: t.cullMargin,
40
+ name: t.name
41
+ });
42
+ if (t.colors && !t.floatColors) for (let e = 0; e < n.length; e++) i.colors[e] = t.colors[n[e]];
43
+ return i.attach(t.attachedMesh), i;
44
+ }
45
+ //#endregion
46
+ export { r as n, n as r, t };
@@ -0,0 +1,10 @@
1
+ import { PointCloud } from './cloud.js';
2
+ /** Positions in local space and an optional column-major local-to-world transform. */
3
+ export interface SurveyPoints {
4
+ positions: ArrayLike<number>;
5
+ matrix?: ArrayLike<number>;
6
+ }
7
+ export declare function checkPoints(data: SurveyPoints): void;
8
+ export declare function transformPoint(p: ArrayLike<number>, i: number, m?: ArrayLike<number>): [number, number, number];
9
+ /** Copy selected local point records, preserving material, HDR colours, normals and source transform. */
10
+ export declare function subsetCloud(source: PointCloud, indices: ArrayLike<number>): PointCloud;