@thatopen/services 0.34.5 → 0.35.1

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 (118) hide show
  1. package/README.md +66 -66
  2. package/dist/cli.js +54 -12
  3. package/dist/cli.js.map +1 -1
  4. package/dist/core/app-cache.d.ts +32 -0
  5. package/dist/core/app-cache.test.d.ts +1 -0
  6. package/dist/core/client.d.ts +9 -1
  7. package/dist/index.cjs.js +5 -5
  8. package/dist/index.cjs.js.map +1 -1
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.es.js +974 -917
  11. package/dist/index.es.js.map +1 -1
  12. package/dist/types/context.d.ts +6 -0
  13. package/dist/vite-env.d.ts +1 -1
  14. package/docs/ai-quickstart.md +548 -548
  15. package/docs/builtin/paths.json +130 -130
  16. package/docs/cli/create.md +29 -29
  17. package/docs/cli/local-server.md +38 -38
  18. package/docs/cli/login.md +32 -32
  19. package/docs/cli/mcp.md +46 -46
  20. package/docs/cli/publish.md +31 -31
  21. package/docs/cli/run.md +26 -26
  22. package/docs/cli/serve.md +43 -43
  23. package/docs/cli/swap.md +30 -30
  24. package/docs/cloud/GaussianSplatConverter.md +43 -43
  25. package/docs/cloud/PointCloudConverter.md +53 -53
  26. package/docs/flow-architecture.md +393 -393
  27. package/docs/flow-plugin-guide.md +354 -354
  28. package/docs/platform-token.md +81 -81
  29. package/docs/rate-limits.md +133 -133
  30. package/docs/recipes-guide.md +243 -243
  31. package/docs/revit-collab-quickstart.md +445 -445
  32. package/docs/rhino-interop-quickstart.md +387 -387
  33. package/docs/sheet-sets-guide.md +69 -69
  34. package/package.json +4 -4
  35. package/resources/AGENTS.md +57 -57
  36. package/src/built-in/CDEManager/example.ts +214 -214
  37. package/src/built-in/CDEManager/examples/FileEditing/example.ts +226 -226
  38. package/src/built-in/CDEManager/examples/FileMoves/example.ts +186 -186
  39. package/src/built-in/CDEManager/examples/FileOperations/example.ts +295 -295
  40. package/src/built-in/CDEManager/examples/FileTransfers/example.ts +210 -210
  41. package/src/built-in/CDEManager/examples/FolderManagement/example.ts +230 -230
  42. package/src/built-in/CDEManager/examples/MetadataSchema/example.ts +230 -230
  43. package/src/built-in/CDEManager/examples/MetadataViews/example.ts +166 -166
  44. package/src/built-in/CDEManager/examples/ProjectConfig/example.ts +177 -177
  45. package/src/built-in/ClashesManager/example.ts +283 -283
  46. package/src/built-in/GISManager/example.ts +355 -355
  47. package/src/built-in/ProjectManager/example.ts +124 -124
  48. package/src/built-in/UIManager/src/app/example.ts +97 -97
  49. package/src/built-in/UIManager/src/app/examples/AppState/example.ts +140 -140
  50. package/src/built-in/UIManager/src/app/examples/Banner/example.ts +159 -159
  51. package/src/built-in/UIManager/src/app/examples/Contexts/example.ts +123 -123
  52. package/src/built-in/UIManager/src/app/examples/FileDrop/example.ts +124 -124
  53. package/src/built-in/UIManager/src/app/examples/FloatingLayout/example.ts +90 -90
  54. package/src/built-in/UIManager/src/app/examples/Modal/example.ts +163 -163
  55. package/src/built-in/UIManager/src/app/examples/Notifications/example.ts +133 -133
  56. package/src/built-in/UIManager/src/app/examples/ProjectFileStorage/example.ts +150 -150
  57. package/src/built-in/UIManager/src/app/examples/Sidebar/example.ts +105 -105
  58. package/src/built-in/UIManager/src/clashes-panel/example.ts +183 -183
  59. package/src/built-in/UIManager/src/gis-panel/example.ts +163 -163
  60. package/src/built-in/UIManager/src/gis-properties-panel/example.ts +168 -168
  61. package/src/built-in/UIManager/src/git-history-panel/example.ts +70 -70
  62. package/src/built-in/UIManager/src/map-panel/example.ts +189 -189
  63. package/src/built-in/UIManager/src/models-list/example.ts +181 -181
  64. package/src/built-in/UIManager/src/settings-panel/example.ts +303 -303
  65. package/src/built-in/UIManager/src/topics-panel/example.ts +170 -170
  66. package/src/built-in/UIManager/src/viewer/example.ts +182 -182
  67. package/src/built-in/UIManager/src/viewer-toolbar/example.ts +376 -376
  68. package/src/built-in/index.ts +2995 -2995
  69. package/src/cli/templates/app/README.md +250 -250
  70. package/src/cli/templates/app/index.html +35 -35
  71. package/src/cli/templates/app/package.json +32 -32
  72. package/src/cli/templates/app/src/app.ts +37 -37
  73. package/src/cli/templates/app/src/assets/tool-placeholder.ts +50 -50
  74. package/src/cli/templates/app/src/bim-components/CloudRunner/index.ts +95 -95
  75. package/src/cli/templates/app/src/bim-components/CloudRunner/src/index.ts +1 -1
  76. package/src/cli/templates/app/src/bim-components/CloudRunner/src/types.ts +5 -5
  77. package/src/cli/templates/app/src/bim-components/index.ts +1 -1
  78. package/src/cli/templates/app/src/globals.ts +1 -1
  79. package/src/cli/templates/app/src/main.history.ts +191 -191
  80. package/src/cli/templates/app/src/main.ts +226 -226
  81. package/src/cli/templates/app/src/setups/channel.ts +76 -76
  82. package/src/cli/templates/app/src/setups/reality-capture/lib/hidden-tiles-plugin.ts +88 -88
  83. package/src/cli/templates/app/src/setups/reality-capture/lib/point-tile-plugin.ts +166 -166
  84. package/src/cli/templates/app/src/setups/reality-capture/lib/render-frame.ts +31 -31
  85. package/src/cli/templates/app/src/setups/reality-capture/lib/splat-tile-plugin.ts +51 -51
  86. package/src/cli/templates/app/src/setups/reality-capture-viewer.ts +1383 -1383
  87. package/src/cli/templates/app/src/ui-components/app-info-section/index.ts +26 -26
  88. package/src/cli/templates/app/src/ui-components/app-info-section/src/index.ts +1 -1
  89. package/src/cli/templates/app/src/ui-components/app-info-section/src/types.ts +15 -15
  90. package/src/cli/templates/app/src/ui-components/cloud-runner-section/index.ts +37 -37
  91. package/src/cli/templates/app/src/ui-components/cloud-runner-section/src/index.ts +1 -1
  92. package/src/cli/templates/app/src/ui-components/cloud-runner-section/src/types.ts +14 -14
  93. package/src/cli/templates/app/src/ui-components/index.ts +2 -2
  94. package/src/cli/templates/app/tsconfig.json +16 -16
  95. package/src/cli/templates/app/vite.config.js +57 -57
  96. package/src/cli/templates/cloud-component/README.md +205 -205
  97. package/src/cli/templates/cloud-component/_thatopen +5 -5
  98. package/src/cli/templates/cloud-component/declarations.json +4 -4
  99. package/src/cli/templates/cloud-component/package.json +21 -21
  100. package/src/cli/templates/cloud-component/src/main.ts +70 -70
  101. package/src/cli/templates/cloud-component/tsconfig.json +16 -16
  102. package/src/cli/templates/cloud-component/vite.config.js +46 -46
  103. package/src/cli/templates/shared/AGENTS.md +8 -8
  104. package/src/cli/templates/shared/CLAUDE.md +1 -1
  105. package/src/cli/templates/shared/_gitignore +5 -5
  106. package/src/core/examples/.env.example +7 -7
  107. package/src/core/examples/apps.ts +67 -67
  108. package/src/core/examples/batch-reads.ts +99 -99
  109. package/src/core/examples/channel.ts +65 -65
  110. package/src/core/examples/components.ts +91 -91
  111. package/src/core/examples/execution.ts +75 -75
  112. package/src/core/examples/file-metadata.ts +94 -94
  113. package/src/core/examples/files.ts +81 -81
  114. package/src/core/examples/folders.ts +82 -82
  115. package/src/core/examples/hidden-files.ts +88 -88
  116. package/src/core/examples/history.ts +103 -103
  117. package/src/core/examples/icons.ts +68 -68
  118. package/src/core/examples/permissions.ts +87 -87
@@ -1,88 +1,88 @@
1
- // 3DTilesRendererJS plugin: serve a STANDARD loose 3D Tiles dataset whose tiles
2
- // are stored as platform HIDDEN files, streamed one-file-per-tile.
3
- //
4
- // Seam: the renderer's `fetchData(url, options)` hook (intercepts BOTH the root
5
- // tileset and every tile content fetch). Point the TilesRenderer at
6
- // `<baseUrl>/tileset.json`; relative content URIs then resolve to
7
- // `<baseUrl>/<relUri>` (e.g. "mem://t/tiles/n12_d3.spz"), which we strip back to
8
- // the dataset-relative path (`relUri`) and look up in the `hiddenFiles` map to
9
- // find the platform hidden-file id to download.
10
- //
11
- // SHARED CONTRACT (matches the converters): the main visible file is a standard
12
- // `tileset.json`; each tile `content.uri` is a relative path under "tiles/".
13
- // tileset.json carries a top-level `hiddenFiles: { "<content.uri>": "<id>" }`
14
- // map. We fetch a tile blob with `client.downloadHiddenFile(id)` -> Response.
15
- //
16
- // Per-tile streaming = one `downloadHiddenFile` per visible tile; the controller's
17
- // Q1 LRU/count-budget governor bounds how many are resident/loading at once.
18
-
19
- // Structural type for the platform client: download a normal file by id (the
20
- // tileset main file) and a hidden file by id (each tile). PlatformClient /
21
- // EngineServicesClient satisfy this.
22
- export interface HiddenTilesClient {
23
- downloadFile(fileId: string, params?: any): Promise<Response>;
24
- downloadHiddenFile(hiddenId: string): Promise<Response>;
25
- }
26
-
27
- export interface HiddenTilesOptions {
28
- baseUrl: string; // synthetic base the TilesRenderer is pointed at, e.g. "mem://t"
29
- tilesetBytes: Uint8Array; // the already-downloaded tileset.json bytes
30
- hiddenFiles: Record<string, string>; // content.uri -> hidden file id
31
- client: HiddenTilesClient;
32
- }
33
-
34
- export class HiddenTilesPlugin {
35
- name = "HIDDEN_TILES_PLUGIN";
36
- private baseUrl: string;
37
- private tilesetBytes: Uint8Array;
38
- private hiddenFiles: Record<string, string>;
39
- private client: HiddenTilesClient;
40
-
41
- constructor({ baseUrl, tilesetBytes, hiddenFiles, client }: HiddenTilesOptions) {
42
- this.baseUrl = baseUrl.replace(/\/$/, "");
43
- this.tilesetBytes = tilesetBytes;
44
- this.hiddenFiles = hiddenFiles;
45
- this.client = client;
46
- }
47
-
48
- // dataset-relative path for URLs under our synthetic base, else null.
49
- private rel(url: string): string | null {
50
- const prefix = this.baseUrl + "/";
51
- return url.startsWith(prefix) ? url.slice(prefix.length) : null;
52
- }
53
-
54
- // The renderer fetch hook. Return a Response, or null to let the default fetch
55
- // run (URLs that aren't under our base).
56
- async fetchData(url: string, _options: any): Promise<Response | null> {
57
- const rel = this.rel(url);
58
- if (rel === null) return null;
59
-
60
- // Root tileset request: serve the bytes we already downloaded.
61
- if (rel === "tileset.json") {
62
- // copy out into a standalone ArrayBuffer for the Response
63
- const b = this.tilesetBytes;
64
- const ab = (b.byteOffset === 0 && b.byteLength === b.buffer.byteLength
65
- ? b.buffer
66
- : b.slice().buffer) as ArrayBuffer;
67
- return new Response(ab, {
68
- status: 200,
69
- headers: { "Content-Type": "application/json" },
70
- });
71
- }
72
-
73
- // Content (tile) request: strip query/fragment, look up the hidden id.
74
- const relUri = decodeURIComponent(rel.split("?")[0].split("#")[0]);
75
- const hiddenId = this.hiddenFiles[relUri];
76
- if (!hiddenId) {
77
- // not a known tile -> 404 so the renderer treats it as a failed fetch.
78
- return new Response(null, { status: 404, statusText: "Not Found" });
79
- }
80
-
81
- const res = await this.client.downloadHiddenFile(hiddenId);
82
- const buf = await res.arrayBuffer();
83
- return new Response(buf, {
84
- status: 200,
85
- headers: { "Content-Type": "application/octet-stream" },
86
- });
87
- }
88
- }
1
+ // 3DTilesRendererJS plugin: serve a STANDARD loose 3D Tiles dataset whose tiles
2
+ // are stored as platform HIDDEN files, streamed one-file-per-tile.
3
+ //
4
+ // Seam: the renderer's `fetchData(url, options)` hook (intercepts BOTH the root
5
+ // tileset and every tile content fetch). Point the TilesRenderer at
6
+ // `<baseUrl>/tileset.json`; relative content URIs then resolve to
7
+ // `<baseUrl>/<relUri>` (e.g. "mem://t/tiles/n12_d3.spz"), which we strip back to
8
+ // the dataset-relative path (`relUri`) and look up in the `hiddenFiles` map to
9
+ // find the platform hidden-file id to download.
10
+ //
11
+ // SHARED CONTRACT (matches the converters): the main visible file is a standard
12
+ // `tileset.json`; each tile `content.uri` is a relative path under "tiles/".
13
+ // tileset.json carries a top-level `hiddenFiles: { "<content.uri>": "<id>" }`
14
+ // map. We fetch a tile blob with `client.downloadHiddenFile(id)` -> Response.
15
+ //
16
+ // Per-tile streaming = one `downloadHiddenFile` per visible tile; the controller's
17
+ // Q1 LRU/count-budget governor bounds how many are resident/loading at once.
18
+
19
+ // Structural type for the platform client: download a normal file by id (the
20
+ // tileset main file) and a hidden file by id (each tile). PlatformClient /
21
+ // EngineServicesClient satisfy this.
22
+ export interface HiddenTilesClient {
23
+ downloadFile(fileId: string, params?: any): Promise<Response>;
24
+ downloadHiddenFile(hiddenId: string): Promise<Response>;
25
+ }
26
+
27
+ export interface HiddenTilesOptions {
28
+ baseUrl: string; // synthetic base the TilesRenderer is pointed at, e.g. "mem://t"
29
+ tilesetBytes: Uint8Array; // the already-downloaded tileset.json bytes
30
+ hiddenFiles: Record<string, string>; // content.uri -> hidden file id
31
+ client: HiddenTilesClient;
32
+ }
33
+
34
+ export class HiddenTilesPlugin {
35
+ name = "HIDDEN_TILES_PLUGIN";
36
+ private baseUrl: string;
37
+ private tilesetBytes: Uint8Array;
38
+ private hiddenFiles: Record<string, string>;
39
+ private client: HiddenTilesClient;
40
+
41
+ constructor({ baseUrl, tilesetBytes, hiddenFiles, client }: HiddenTilesOptions) {
42
+ this.baseUrl = baseUrl.replace(/\/$/, "");
43
+ this.tilesetBytes = tilesetBytes;
44
+ this.hiddenFiles = hiddenFiles;
45
+ this.client = client;
46
+ }
47
+
48
+ // dataset-relative path for URLs under our synthetic base, else null.
49
+ private rel(url: string): string | null {
50
+ const prefix = this.baseUrl + "/";
51
+ return url.startsWith(prefix) ? url.slice(prefix.length) : null;
52
+ }
53
+
54
+ // The renderer fetch hook. Return a Response, or null to let the default fetch
55
+ // run (URLs that aren't under our base).
56
+ async fetchData(url: string, _options: any): Promise<Response | null> {
57
+ const rel = this.rel(url);
58
+ if (rel === null) return null;
59
+
60
+ // Root tileset request: serve the bytes we already downloaded.
61
+ if (rel === "tileset.json") {
62
+ // copy out into a standalone ArrayBuffer for the Response
63
+ const b = this.tilesetBytes;
64
+ const ab = (b.byteOffset === 0 && b.byteLength === b.buffer.byteLength
65
+ ? b.buffer
66
+ : b.slice().buffer) as ArrayBuffer;
67
+ return new Response(ab, {
68
+ status: 200,
69
+ headers: { "Content-Type": "application/json" },
70
+ });
71
+ }
72
+
73
+ // Content (tile) request: strip query/fragment, look up the hidden id.
74
+ const relUri = decodeURIComponent(rel.split("?")[0].split("#")[0]);
75
+ const hiddenId = this.hiddenFiles[relUri];
76
+ if (!hiddenId) {
77
+ // not a known tile -> 404 so the renderer treats it as a failed fetch.
78
+ return new Response(null, { status: 404, statusText: "Not Found" });
79
+ }
80
+
81
+ const res = await this.client.downloadHiddenFile(hiddenId);
82
+ const buf = await res.arrayBuffer();
83
+ return new Response(buf, {
84
+ status: 200,
85
+ headers: { "Content-Type": "application/octet-stream" },
86
+ });
87
+ }
88
+ }
@@ -1,166 +1,166 @@
1
- import * as THREE from "three";
2
- import { frame, placeAtAnchor } from "./render-frame";
3
-
4
- // ---- decode worker pool -----------------------------------------------------
5
- // Dequantizes node blobs off the main thread (grimoire: worker-zero-copy decode).
6
- //
7
- // INLINE worker: the decode logic is bundled as a source STRING -> Blob -> object
8
- // URL classic worker. NO `new URL("./worker.ts", import.meta.url)` — that breaks
9
- // under esbuild bundling + the platform's sandboxed iframe (Invalid URL). This
10
- // blob-URL form works in every bundler/sandbox (same approach Spark uses).
11
- // Blob: f64 anchor[3] | f32 scale[3] | u32 count | (u16 x,y,z + u8 r,g,b)*count.
12
- const WORKER_SRC = `
13
- self.onmessage = function (e) {
14
- var id = e.data.id, buffer = e.data.buffer;
15
- var dv = new DataView(buffer);
16
- var ax = dv.getFloat64(0, true), ay = dv.getFloat64(8, true), az = dv.getFloat64(16, true);
17
- var sx = dv.getFloat32(24, true), sy = dv.getFloat32(28, true), sz = dv.getFloat32(32, true);
18
- var count = dv.getUint32(36, true);
19
- var positions = new Float32Array(count * 3);
20
- var colors = new Uint8Array(count * 3);
21
- var o = 40;
22
- for (var k = 0; k < count; k++) {
23
- positions[k * 3] = dv.getUint16(o, true) * sx;
24
- positions[k * 3 + 1] = dv.getUint16(o + 2, true) * sy;
25
- positions[k * 3 + 2] = dv.getUint16(o + 4, true) * sz;
26
- colors[k * 3] = dv.getUint8(o + 6);
27
- colors[k * 3 + 1] = dv.getUint8(o + 7);
28
- colors[k * 3 + 2] = dv.getUint8(o + 8);
29
- o += 9;
30
- }
31
- self.postMessage({ id: id, positions: positions, colors: colors, count: count, anchor: [ax, ay, az] }, [positions.buffer, colors.buffer]);
32
- };
33
- `;
34
- let _blobURL: string | null = null;
35
- function workerURL(): string {
36
- return (_blobURL ??= URL.createObjectURL(new Blob([WORKER_SRC], { type: "text/javascript" })));
37
- }
38
-
39
- type Decoded = { positions: Float32Array; colors: Uint8Array; count: number; anchor: [number, number, number] };
40
- class DecodePool {
41
- private workers: Worker[] = [];
42
- private idle: Worker[] = [];
43
- private queue: { id: number; buffer: ArrayBuffer; resolve: (d: Decoded) => void }[] = [];
44
- private pending = new Map<number, (d: Decoded) => void>();
45
- private nextId = 0;
46
- constructor(n: number) {
47
- for (let i = 0; i < n; i++) {
48
- const w = new Worker(workerURL()); // classic blob worker (no import.meta.url, no module type)
49
- w.onmessage = (e: MessageEvent<Decoded & { id: number }>) => {
50
- const r = this.pending.get(e.data.id);
51
- this.pending.delete(e.data.id);
52
- this.idle.push(w);
53
- r?.(e.data);
54
- this.drain();
55
- };
56
- this.workers.push(w); this.idle.push(w);
57
- }
58
- }
59
- decode(buffer: ArrayBuffer): Promise<Decoded> {
60
- return new Promise((resolve) => { this.queue.push({ id: this.nextId++, buffer, resolve }); this.drain(); });
61
- }
62
- private drain() {
63
- while (this.idle.length && this.queue.length) {
64
- const w = this.idle.pop()!; const job = this.queue.shift()!;
65
- this.pending.set(job.id, job.resolve);
66
- w.postMessage({ id: job.id, buffer: job.buffer }, [job.buffer]); // transfer in (zero-copy)
67
- }
68
- }
69
- }
70
- // LAZY: workers are created on the FIRST decode (a points tile), never at module
71
- // load — so merely importing this module (e.g. for the splat path) spins up nothing.
72
- let _pool: DecodePool | null = null;
73
- function getPool(): DecodePool {
74
- return (_pool ??= new DecodePool(Math.min(16, Math.max(3, (navigator.hardwareConcurrency || 4) - 1))));
75
- }
76
- export function decodeStats() {
77
- const p = _pool as any;
78
- return p ? { workers: p.workers.length, idle: p.idle.length, queued: p.queue.length, pending: p.pending.size }
79
- : { workers: 0, idle: 0, queued: 0, pending: 0 };
80
- }
81
-
82
- // 3DTilesRendererJS plugin: decodes our quantized point-node format into a
83
- // node-local THREE.Points, tagged with its float64 world anchor for RTC.
84
- //
85
- // Node blob (sliced out of cloud.bin by byte range):
86
- // header: f64 anchor[3] | f32 scale[3] | u32 count (40 B)
87
- // body: per point u16 x,y,z + u8 r,g,b (9 B)
88
- // Decoded position = stored * scale (node-local, small, float32-safe).
89
- //
90
- // Same `parseToMesh` seam the experiment proved with Spark: return a
91
- // THREE.Object3D and the renderer owns transform/cull/cache/eviction. We must
92
- // NOT be async at the top level (invokeOnePlugin treats a Promise as truthy and
93
- // stops iterating), so non-matching content returns null synchronously.
94
-
95
- // Shared material for all point nodes. Writes rgb + view-space log-depth (alpha)
96
- // so the EDL post pass gets its depth field for free (no second target).
97
- const pointUniforms = { uPointSize: { value: frame.pointSize } };
98
-
99
- const pointMaterial = new THREE.ShaderMaterial({
100
- uniforms: pointUniforms,
101
- vertexShader: /* glsl */ `
102
- attribute vec3 color;
103
- uniform float uPointSize;
104
- varying vec3 vColor;
105
- varying float vLogZ;
106
- void main() {
107
- vColor = color;
108
- vec4 mv = modelViewMatrix * vec4(position, 1.0);
109
- vLogZ = log2(max(1e-6, -mv.z));
110
- gl_Position = projectionMatrix * mv;
111
- gl_PointSize = uPointSize;
112
- }
113
- `,
114
- fragmentShader: /* glsl */ `
115
- varying vec3 vColor;
116
- varying float vLogZ;
117
- void main() {
118
- gl_FragColor = vec4(vColor, vLogZ);
119
- }
120
- `,
121
- });
122
-
123
- export function setPointSize(px: number) {
124
- pointUniforms.uPointSize.value = px;
125
- }
126
-
127
- export class PointTilePlugin {
128
- name = "POINT_TILE_PLUGIN";
129
- tiles: any = null;
130
-
131
- init(tiles: any) {
132
- this.tiles = tiles;
133
- }
134
-
135
- // parseToMesh: hand the blob to the decode WORKER POOL (off-thread dequant +
136
- // zero-copy transfer back) so tile loads never hitch the main thread. Must
137
- // return null synchronously for non-matching content (a Promise is truthy and
138
- // would stop plugin iteration); matching content returns a Promise<Points>.
139
- parseToMesh(
140
- buffer: ArrayBuffer,
141
- _tile: any,
142
- _extension: string,
143
- uri: string,
144
- signal: AbortSignal
145
- ) {
146
- if (!uri.includes(".pnt")) return null;
147
- if (signal.aborted) return null;
148
- return getPool().decode(buffer).then((d) => {
149
- if (signal.aborted) return null;
150
- const geom = new THREE.BufferGeometry();
151
- geom.setAttribute("position", new THREE.BufferAttribute(d.positions, 3));
152
- geom.setAttribute("color", new THREE.BufferAttribute(d.colors, 3, true));
153
- const points = new THREE.Points(geom, pointMaterial);
154
- points.matrixAutoUpdate = false;
155
- points.frustumCulled = false; // renderer culls per tile already
156
- points.userData.worldAnchor = d.anchor; // float64 world anchor for RTC
157
- placeAtAnchor(points, d.anchor);
158
- return points;
159
- });
160
- }
161
-
162
- disposeTile(tile: any) {
163
- const scene = tile.engineData?.scene;
164
- if (scene && scene.geometry) scene.geometry.dispose();
165
- }
166
- }
1
+ import * as THREE from "three";
2
+ import { frame, placeAtAnchor } from "./render-frame";
3
+
4
+ // ---- decode worker pool -----------------------------------------------------
5
+ // Dequantizes node blobs off the main thread (grimoire: worker-zero-copy decode).
6
+ //
7
+ // INLINE worker: the decode logic is bundled as a source STRING -> Blob -> object
8
+ // URL classic worker. NO `new URL("./worker.ts", import.meta.url)` — that breaks
9
+ // under esbuild bundling + the platform's sandboxed iframe (Invalid URL). This
10
+ // blob-URL form works in every bundler/sandbox (same approach Spark uses).
11
+ // Blob: f64 anchor[3] | f32 scale[3] | u32 count | (u16 x,y,z + u8 r,g,b)*count.
12
+ const WORKER_SRC = `
13
+ self.onmessage = function (e) {
14
+ var id = e.data.id, buffer = e.data.buffer;
15
+ var dv = new DataView(buffer);
16
+ var ax = dv.getFloat64(0, true), ay = dv.getFloat64(8, true), az = dv.getFloat64(16, true);
17
+ var sx = dv.getFloat32(24, true), sy = dv.getFloat32(28, true), sz = dv.getFloat32(32, true);
18
+ var count = dv.getUint32(36, true);
19
+ var positions = new Float32Array(count * 3);
20
+ var colors = new Uint8Array(count * 3);
21
+ var o = 40;
22
+ for (var k = 0; k < count; k++) {
23
+ positions[k * 3] = dv.getUint16(o, true) * sx;
24
+ positions[k * 3 + 1] = dv.getUint16(o + 2, true) * sy;
25
+ positions[k * 3 + 2] = dv.getUint16(o + 4, true) * sz;
26
+ colors[k * 3] = dv.getUint8(o + 6);
27
+ colors[k * 3 + 1] = dv.getUint8(o + 7);
28
+ colors[k * 3 + 2] = dv.getUint8(o + 8);
29
+ o += 9;
30
+ }
31
+ self.postMessage({ id: id, positions: positions, colors: colors, count: count, anchor: [ax, ay, az] }, [positions.buffer, colors.buffer]);
32
+ };
33
+ `;
34
+ let _blobURL: string | null = null;
35
+ function workerURL(): string {
36
+ return (_blobURL ??= URL.createObjectURL(new Blob([WORKER_SRC], { type: "text/javascript" })));
37
+ }
38
+
39
+ type Decoded = { positions: Float32Array; colors: Uint8Array; count: number; anchor: [number, number, number] };
40
+ class DecodePool {
41
+ private workers: Worker[] = [];
42
+ private idle: Worker[] = [];
43
+ private queue: { id: number; buffer: ArrayBuffer; resolve: (d: Decoded) => void }[] = [];
44
+ private pending = new Map<number, (d: Decoded) => void>();
45
+ private nextId = 0;
46
+ constructor(n: number) {
47
+ for (let i = 0; i < n; i++) {
48
+ const w = new Worker(workerURL()); // classic blob worker (no import.meta.url, no module type)
49
+ w.onmessage = (e: MessageEvent<Decoded & { id: number }>) => {
50
+ const r = this.pending.get(e.data.id);
51
+ this.pending.delete(e.data.id);
52
+ this.idle.push(w);
53
+ r?.(e.data);
54
+ this.drain();
55
+ };
56
+ this.workers.push(w); this.idle.push(w);
57
+ }
58
+ }
59
+ decode(buffer: ArrayBuffer): Promise<Decoded> {
60
+ return new Promise((resolve) => { this.queue.push({ id: this.nextId++, buffer, resolve }); this.drain(); });
61
+ }
62
+ private drain() {
63
+ while (this.idle.length && this.queue.length) {
64
+ const w = this.idle.pop()!; const job = this.queue.shift()!;
65
+ this.pending.set(job.id, job.resolve);
66
+ w.postMessage({ id: job.id, buffer: job.buffer }, [job.buffer]); // transfer in (zero-copy)
67
+ }
68
+ }
69
+ }
70
+ // LAZY: workers are created on the FIRST decode (a points tile), never at module
71
+ // load — so merely importing this module (e.g. for the splat path) spins up nothing.
72
+ let _pool: DecodePool | null = null;
73
+ function getPool(): DecodePool {
74
+ return (_pool ??= new DecodePool(Math.min(16, Math.max(3, (navigator.hardwareConcurrency || 4) - 1))));
75
+ }
76
+ export function decodeStats() {
77
+ const p = _pool as any;
78
+ return p ? { workers: p.workers.length, idle: p.idle.length, queued: p.queue.length, pending: p.pending.size }
79
+ : { workers: 0, idle: 0, queued: 0, pending: 0 };
80
+ }
81
+
82
+ // 3DTilesRendererJS plugin: decodes our quantized point-node format into a
83
+ // node-local THREE.Points, tagged with its float64 world anchor for RTC.
84
+ //
85
+ // Node blob (sliced out of cloud.bin by byte range):
86
+ // header: f64 anchor[3] | f32 scale[3] | u32 count (40 B)
87
+ // body: per point u16 x,y,z + u8 r,g,b (9 B)
88
+ // Decoded position = stored * scale (node-local, small, float32-safe).
89
+ //
90
+ // Same `parseToMesh` seam the experiment proved with Spark: return a
91
+ // THREE.Object3D and the renderer owns transform/cull/cache/eviction. We must
92
+ // NOT be async at the top level (invokeOnePlugin treats a Promise as truthy and
93
+ // stops iterating), so non-matching content returns null synchronously.
94
+
95
+ // Shared material for all point nodes. Writes rgb + view-space log-depth (alpha)
96
+ // so the EDL post pass gets its depth field for free (no second target).
97
+ const pointUniforms = { uPointSize: { value: frame.pointSize } };
98
+
99
+ const pointMaterial = new THREE.ShaderMaterial({
100
+ uniforms: pointUniforms,
101
+ vertexShader: /* glsl */ `
102
+ attribute vec3 color;
103
+ uniform float uPointSize;
104
+ varying vec3 vColor;
105
+ varying float vLogZ;
106
+ void main() {
107
+ vColor = color;
108
+ vec4 mv = modelViewMatrix * vec4(position, 1.0);
109
+ vLogZ = log2(max(1e-6, -mv.z));
110
+ gl_Position = projectionMatrix * mv;
111
+ gl_PointSize = uPointSize;
112
+ }
113
+ `,
114
+ fragmentShader: /* glsl */ `
115
+ varying vec3 vColor;
116
+ varying float vLogZ;
117
+ void main() {
118
+ gl_FragColor = vec4(vColor, vLogZ);
119
+ }
120
+ `,
121
+ });
122
+
123
+ export function setPointSize(px: number) {
124
+ pointUniforms.uPointSize.value = px;
125
+ }
126
+
127
+ export class PointTilePlugin {
128
+ name = "POINT_TILE_PLUGIN";
129
+ tiles: any = null;
130
+
131
+ init(tiles: any) {
132
+ this.tiles = tiles;
133
+ }
134
+
135
+ // parseToMesh: hand the blob to the decode WORKER POOL (off-thread dequant +
136
+ // zero-copy transfer back) so tile loads never hitch the main thread. Must
137
+ // return null synchronously for non-matching content (a Promise is truthy and
138
+ // would stop plugin iteration); matching content returns a Promise<Points>.
139
+ parseToMesh(
140
+ buffer: ArrayBuffer,
141
+ _tile: any,
142
+ _extension: string,
143
+ uri: string,
144
+ signal: AbortSignal
145
+ ) {
146
+ if (!uri.includes(".pnt")) return null;
147
+ if (signal.aborted) return null;
148
+ return getPool().decode(buffer).then((d) => {
149
+ if (signal.aborted) return null;
150
+ const geom = new THREE.BufferGeometry();
151
+ geom.setAttribute("position", new THREE.BufferAttribute(d.positions, 3));
152
+ geom.setAttribute("color", new THREE.BufferAttribute(d.colors, 3, true));
153
+ const points = new THREE.Points(geom, pointMaterial);
154
+ points.matrixAutoUpdate = false;
155
+ points.frustumCulled = false; // renderer culls per tile already
156
+ points.userData.worldAnchor = d.anchor; // float64 world anchor for RTC
157
+ placeAtAnchor(points, d.anchor);
158
+ return points;
159
+ });
160
+ }
161
+
162
+ disposeTile(tile: any) {
163
+ const scene = tile.engineData?.scene;
164
+ if (scene && scene.geometry) scene.geometry.dispose();
165
+ }
166
+ }
@@ -1,31 +1,31 @@
1
- import * as THREE from "three";
2
-
3
- // Shared floating-origin (RTC) state for the whole viewer.
4
- //
5
- // The precision stack: authoritative coordinates live in float64 (plain JS
6
- // numbers). `origin` is a float64 world point near the camera. Every renderable
7
- // (point node, splat) is placed at worldAnchor - origin computed in float64,
8
- // then assigned to a float32 Object3D.position — which is small and safe. When
9
- // the camera travels far, we rebase `origin` and shift everything, so GPU
10
- // coordinates never grow large no matter the absolute world position.
11
-
12
- export const POINT_LAYER = 1;
13
- export const SPLAT_LAYER = 2;
14
-
15
- export const frame = {
16
- // float64 render origin in world space
17
- origin: [0, 0, 0] as [number, number, number],
18
- pointSize: 3.0,
19
- edlStrength: 0.35,
20
- edlRadius: 1.4,
21
- edlOn: true,
22
- };
23
-
24
- // Place a point node at its FULL float64 world anchor. The recentering (origin
25
- // subtraction) is done ONCE by pointContainer.position = -origin each frame — so
26
- // world = -origin + anchor + localOffset = (worldPoint - origin), small + precise.
27
- // (Do NOT subtract origin here too, or it gets subtracted twice.)
28
- export function placeAtAnchor(obj: THREE.Object3D, anchor: [number, number, number]) {
29
- obj.position.set(anchor[0], anchor[1], anchor[2]);
30
- obj.updateMatrix();
31
- }
1
+ import * as THREE from "three";
2
+
3
+ // Shared floating-origin (RTC) state for the whole viewer.
4
+ //
5
+ // The precision stack: authoritative coordinates live in float64 (plain JS
6
+ // numbers). `origin` is a float64 world point near the camera. Every renderable
7
+ // (point node, splat) is placed at worldAnchor - origin computed in float64,
8
+ // then assigned to a float32 Object3D.position — which is small and safe. When
9
+ // the camera travels far, we rebase `origin` and shift everything, so GPU
10
+ // coordinates never grow large no matter the absolute world position.
11
+
12
+ export const POINT_LAYER = 1;
13
+ export const SPLAT_LAYER = 2;
14
+
15
+ export const frame = {
16
+ // float64 render origin in world space
17
+ origin: [0, 0, 0] as [number, number, number],
18
+ pointSize: 3.0,
19
+ edlStrength: 0.35,
20
+ edlRadius: 1.4,
21
+ edlOn: true,
22
+ };
23
+
24
+ // Place a point node at its FULL float64 world anchor. The recentering (origin
25
+ // subtraction) is done ONCE by pointContainer.position = -origin each frame — so
26
+ // world = -origin + anchor + localOffset = (worldPoint - origin), small + precise.
27
+ // (Do NOT subtract origin here too, or it gets subtracted twice.)
28
+ export function placeAtAnchor(obj: THREE.Object3D, anchor: [number, number, number]) {
29
+ obj.position.set(anchor[0], anchor[1], anchor[2]);
30
+ obj.updateMatrix();
31
+ }