s3m-three 0.3.5 → 0.3.6

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 (59) hide show
  1. package/dist/browser/assets/s3mWorker-hZY5KqWF.js +1 -0
  2. package/dist/browser/s3m-three.iife.ext-three.js +26 -20
  3. package/dist/browser/s3m-three.iife.js +26 -20
  4. package/dist/core/TextureDecode.d.ts +18 -0
  5. package/dist/core/TextureDecode.d.ts.map +1 -0
  6. package/dist/core/TextureDecode.js +130 -0
  7. package/dist/core/TextureDecode.js.map +1 -0
  8. package/dist/core/TextureFormat.d.ts +22 -2
  9. package/dist/core/TextureFormat.d.ts.map +1 -1
  10. package/dist/core/TextureFormat.js +22 -2
  11. package/dist/core/TextureFormat.js.map +1 -1
  12. package/dist/render/DatasetManager.d.ts +2 -0
  13. package/dist/render/DatasetManager.d.ts.map +1 -1
  14. package/dist/render/DatasetManager.js +1 -0
  15. package/dist/render/DatasetManager.js.map +1 -1
  16. package/dist/render/FeatureStyleManager.d.ts +4 -2
  17. package/dist/render/FeatureStyleManager.d.ts.map +1 -1
  18. package/dist/render/FeatureStyleManager.js +33 -1
  19. package/dist/render/FeatureStyleManager.js.map +1 -1
  20. package/dist/render/S3MViewer.d.ts +2 -0
  21. package/dist/render/S3MViewer.d.ts.map +1 -1
  22. package/dist/render/S3MViewer.js +5 -2
  23. package/dist/render/S3MViewer.js.map +1 -1
  24. package/dist/render/StyleMaterial.d.ts.map +1 -1
  25. package/dist/render/StyleMaterial.js +6 -0
  26. package/dist/render/StyleMaterial.js.map +1 -1
  27. package/dist/render/TextureFactory.d.ts +8 -19
  28. package/dist/render/TextureFactory.d.ts.map +1 -1
  29. package/dist/render/TextureFactory.js +39 -116
  30. package/dist/render/TextureFactory.js.map +1 -1
  31. package/dist/tileset/S3MTileset.d.ts +17 -0
  32. package/dist/tileset/S3MTileset.d.ts.map +1 -1
  33. package/dist/tileset/S3MTileset.js +32 -1
  34. package/dist/tileset/S3MTileset.js.map +1 -1
  35. package/dist/worker/S3MWorkerClient.d.ts +40 -0
  36. package/dist/worker/S3MWorkerClient.d.ts.map +1 -0
  37. package/dist/worker/S3MWorkerClient.js +120 -0
  38. package/dist/worker/S3MWorkerClient.js.map +1 -0
  39. package/dist/worker/s3mWorker.d.ts +2 -0
  40. package/dist/worker/s3mWorker.d.ts.map +1 -0
  41. package/dist/worker/s3mWorker.js +48 -0
  42. package/dist/worker/s3mWorker.js.map +1 -0
  43. package/dist/worker/transferables.d.ts +14 -0
  44. package/dist/worker/transferables.d.ts.map +1 -0
  45. package/dist/worker/transferables.js +61 -0
  46. package/dist/worker/transferables.js.map +1 -0
  47. package/docs/API.md +4 -0
  48. package/package.json +1 -1
  49. package/src/core/TextureDecode.ts +158 -0
  50. package/src/core/TextureFormat.ts +23 -2
  51. package/src/render/DatasetManager.ts +3 -0
  52. package/src/render/FeatureStyleManager.ts +31 -2
  53. package/src/render/S3MViewer.ts +8 -2
  54. package/src/render/StyleMaterial.ts +6 -0
  55. package/src/render/TextureFactory.ts +43 -122
  56. package/src/tileset/S3MTileset.ts +39 -1
  57. package/src/worker/S3MWorkerClient.ts +150 -0
  58. package/src/worker/s3mWorker.ts +58 -0
  59. package/src/worker/transferables.ts +58 -0
@@ -1,32 +1,18 @@
1
1
  /**
2
2
  * S3M 纹理 → three.js Texture
3
3
  *
4
- * S3M 的纹理是「裸块」:没有容器头,只有 width/height/compressType/dataSize/pixelFormat + 原始字节。
5
- * 该走哪条解码路径由 compressType 决定(见 core/TextureFormat.ts):DXT 族直接解块,
6
- * CRN(26 / 27 / 38001 / 50000)先用 crunch wasm 转码回 DXT,WEBP 交给浏览器,
7
- * RGB 系逐像素拷贝。
4
+ * 解码本体已下沉到 core/TextureDecode.ts(three 无关,主线程 / Worker 共用),
5
+ * 本文件只负责两件事:
6
+ * 1. 优先把解码派给 Worker(setTextureOffThreadDecode 注入,S3MViewer 装配),
7
+ * Worker 不可用时用同一份 decodeTextureData 在主线程兜底;
8
+ * 2. 把纯数据解码产物包成 three Texture 并按 atlas / 普通 分别配置采样参数。
8
9
  */
9
10
  import * as THREE from 'three';
10
- import DXTTextureDecode from '../core/DXTTextureDecode.js';
11
- import { transcodeCRN } from '../core/CRNDecode.js';
12
- import { dxtPixelFormatOf, resolveTextureKind } from '../core/TextureFormat.js';
13
11
  import type { S3MTexture } from '../core/types.js';
12
+ import { decodeTextureData } from '../core/TextureDecode.js';
13
+ import type { DecodedTextureData } from '../core/TextureDecode.js';
14
14
 
15
- export const PixelFormat = {
16
- RGB: 10,
17
- BGR: 11,
18
- ARGB: 12,
19
- ABGR: 13,
20
- BGRA: 14,
21
- RGBA: 28,
22
- WEBP: 25,
23
- DXT1: 17,
24
- DXT3: 19,
25
- DXT4: 20,
26
- DXT5: 21,
27
- CRN_DXT5: 26,
28
- STANDARD_CRN: 27,
29
- } as const;
15
+ export { PixelFormat } from '../core/TextureFormat.js';
30
16
 
31
17
  /** 由 S3MLayer 在拿到 GL 上下文后注入,用于斜视角下保持纹理清晰 */
32
18
  let maxAnisotropy = 1;
@@ -41,6 +27,26 @@ export interface DecodedTexture {
41
27
  decoded: string;
42
28
  }
43
29
 
30
+ /**
31
+ * 注入「离线解码」实现(S3MViewer 装配:走共享解析 Worker)。
32
+ * 返回 null = Worker 不可用 / 崩溃,createTexture 自动退回主线程 decodeTextureData。
33
+ */
34
+ let offThreadDecode: ((tex: S3MTexture) => Promise<DecodedTextureData | null>) | null = null;
35
+
36
+ export function setTextureOffThreadDecode(
37
+ fn: ((tex: S3MTexture) => Promise<DecodedTextureData | null>) | null
38
+ ): void {
39
+ offThreadDecode = fn;
40
+ }
41
+
42
+ async function decodeTexture(tex: S3MTexture): Promise<DecodedTextureData> {
43
+ if (offThreadDecode) {
44
+ const viaWorker = await offThreadDecode(tex);
45
+ if (viaWorker) return viaWorker;
46
+ }
47
+ return decodeTextureData(tex);
48
+ }
49
+
44
50
  function configure<T extends THREE.Texture>(tex: T, srgb = true): T {
45
51
  tex.wrapS = THREE.RepeatWrapping;
46
52
  tex.wrapT = THREE.RepeatWrapping;
@@ -74,117 +80,32 @@ function configureAtlas<T extends THREE.Texture>(tex: T): T {
74
80
  return tex;
75
81
  }
76
82
 
77
- /** 行序翻转(原地)。S3MBLOCK 图集按 GL 方向存储(v=0 在最后一行),解码后要翻一次行 */
78
- function flipRows(rgba: Uint8Array, width: number, height: number): void {
79
- const row = width * 4;
80
- const tmp = new Uint8Array(row);
81
- for (let y = 0; y < height >> 1; y++) {
82
- const a = y * row;
83
- const b = (height - 1 - y) * row;
84
- tmp.set(rgba.subarray(a, a + row));
85
- rgba.copyWithin(a, b, b + row);
86
- rgba.set(tmp, b);
87
- }
88
- }
89
-
90
- /** 该像素格式自带 alpha 通道(DXT1 的 1bit alpha 不算,历史上没开过 alphaTest) */
91
- function hasAlphaChannel(pixelFormat: number): boolean {
92
- return (
93
- pixelFormat === PixelFormat.DXT3 ||
94
- pixelFormat === PixelFormat.DXT4 ||
95
- pixelFormat === PixelFormat.DXT5 ||
96
- pixelFormat === PixelFormat.ARGB ||
97
- pixelFormat === PixelFormat.ABGR ||
98
- pixelFormat === PixelFormat.BGRA ||
99
- pixelFormat === PixelFormat.RGBA
100
- );
101
- }
102
-
103
- /** 解 DXT 到 RGBA8。DXT5 是 1 字节/像素,DXT1 是 0.5 字节/像素。 */
104
- /** DXT 解码只需要尺寸 / 格式 / 字节,CRN 转码出来的结果也走这条路 */
105
- function decodeDXT(tex: Pick<S3MTexture, 'width' | 'height' | 'pixelFormat' | 'data' | 'flipY'>): THREE.DataTexture {
106
- const { width, height, pixelFormat, data } = tex;
107
- const out = new Uint8Array(width * height * 4);
108
- DXTTextureDecode.decode(out, width, height, data, pixelFormat);
109
- if (tex.flipY) flipRows(out, width, height);
110
- const dt = new THREE.DataTexture(out, width, height, THREE.RGBAFormat, THREE.UnsignedByteType);
111
- if (hasAlphaChannel(pixelFormat)) dt.userData.s3mHasAlpha = true;
112
- return dt;
113
- }
114
-
115
- function fromRawRGB(tex: S3MTexture): THREE.DataTexture {
116
- const { width, height, pixelFormat, data } = tex;
117
- const out = new Uint8Array(width * height * 4);
118
- // RGBA / BGRA / ARGB / ABGR 都是 4 字节,前两个是「尾字节是 alpha」,后两个是「首字节是 alpha」
119
- const hasAlpha =
120
- pixelFormat === PixelFormat.RGBA ||
121
- pixelFormat === PixelFormat.BGRA ||
122
- pixelFormat === PixelFormat.ARGB ||
123
- pixelFormat === PixelFormat.ABGR;
124
- const swapRB = pixelFormat === PixelFormat.BGR || pixelFormat === PixelFormat.BGRA;
125
- const alphaFirst = pixelFormat === PixelFormat.ARGB || pixelFormat === PixelFormat.ABGR;
126
- const src = hasAlpha ? 4 : 3;
127
- for (let i = 0, s = 0, d = 0; i < width * height; i++, s += src, d += 4) {
128
- const p = alphaFirst ? s + 1 : s;
129
- out[d] = swapRB ? data[p + 2] : data[p];
130
- out[d + 1] = data[p + 1];
131
- out[d + 2] = swapRB ? data[p] : data[p + 2];
132
- out[d + 3] = hasAlpha ? data[alphaFirst ? s : s + 3] : 255;
133
- }
134
- if (tex.flipY) flipRows(out, width, height);
135
- const dt = new THREE.DataTexture(out, width, height, THREE.RGBAFormat, THREE.UnsignedByteType);
136
- if (hasAlphaChannel(pixelFormat)) dt.userData.s3mHasAlpha = true;
137
- return dt;
138
- }
139
-
140
83
  export async function createTexture(tex: S3MTexture | null | undefined): Promise<DecodedTexture> {
141
84
  if (!tex || !tex.width || !tex.height) return { texture: null, decoded: 'empty' };
142
85
 
143
86
  // 图集纹理(flipY 的原始 DXT 图集 / AtlasPack 拼出来的 RGBA 大图 / leaf 组合画布)都必须
144
87
  // 关 GPU mip,层级由 StyleMaterial.s3mAtlasUnit 的内联金字塔自己算。
145
88
  const isAtlas = tex.packed === true || tex.flipY === true || tex.composed === true;
146
- const asAtlas = <T extends THREE.Texture>(t: T): T => (isAtlas ? configureAtlas(t) : configure(t));
147
89
 
148
- // 判定只能看 compressType:v3.00 的 CRN 纹理 pixelFormat 是 13(ABGR)、
149
- // 行标的 CRN 是 32856(GL_RGBA8),都不是 26 / 27(见 core/TextureFormat.ts)
150
- const kind = resolveTextureKind(tex.compressType ?? 0, tex.pixelFormat);
90
+ const data = await decodeTexture(tex);
151
91
 
152
- if (kind === 'crn') {
153
- try {
154
- const crn = await transcodeCRN(tex.data);
155
- const decoded = decodeDXT({
156
- width: crn.width,
157
- height: crn.height,
158
- pixelFormat: crn.dxtFormat === 1 ? 17 : 21,
159
- data: crn.data,
160
- });
161
- return { texture: asAtlas(decoded), decoded: `crn->dxt${crn.dxtFormat}` };
162
- } catch (e) {
92
+ if (!data.rgba && !data.bitmap) {
93
+ if (data.error) {
163
94
  // 一张纹理坏了不该带崩整个瓦片,如实上报并降级成无贴图
164
- console.warn('[s3m] CRN 转码失败:', e);
165
- return { texture: null, decoded: 'crn(failed)' };
95
+ console.warn('[s3m] CRN 转码失败:', data.error);
166
96
  }
97
+ return { texture: null, decoded: data.kind };
167
98
  }
168
99
 
169
- if (kind === 'dxt1' || kind === 'dxt3' || kind === 'dxt4' || kind === 'dxt5') {
170
- const decoded = decodeDXT({ ...tex, pixelFormat: dxtPixelFormatOf(kind) });
171
- // flipY 为真即 S3MBLOCK 图集(见 core/types.ts),必须无 mip
172
- return { texture: asAtlas(decoded), decoded: kind };
100
+ let texture: THREE.Texture;
101
+ if (data.bitmap) {
102
+ texture = new THREE.Texture(data.bitmap);
103
+ } else {
104
+ const dt = new THREE.DataTexture(data.rgba!, data.width, data.height, THREE.RGBAFormat, THREE.UnsignedByteType);
105
+ if (data.hasAlpha) dt.userData.s3mHasAlpha = true;
106
+ texture = dt;
173
107
  }
174
-
175
- if (kind === 'webp') {
176
- const blob = new Blob([tex.data as BlobPart], { type: 'image/webp' });
177
- const bmp = await createImageBitmap(blob);
178
- const t = new THREE.Texture(bmp);
179
- t.needsUpdate = true;
180
- return { texture: asAtlas(t), decoded: 'webp' };
181
- }
182
-
183
- if (kind === 'raw') {
184
- const decoded = fromRawRGB(tex);
185
- return { texture: asAtlas(decoded), decoded: 'raw' };
186
- }
187
-
188
- // KTX2 / ETC 需要额外解码器,先如实上报而不是静默出错
189
- return { texture: null, decoded: `unsupported(${tex.compressType}/${tex.pixelFormat})` };
108
+ texture.needsUpdate = true;
109
+ // flipY 为真即 S3MBLOCK 图集(见 core/types.ts),必须无 mip
110
+ return { texture: isAtlas ? configureAtlas(texture) : configure(texture), decoded: data.kind };
190
111
  }
@@ -7,6 +7,7 @@
7
7
  * 命中阈值就请求 childTile,实现渐进式细化。
8
8
  */
9
9
  import { parseS3MB } from '../core/S3MBParser.js';
10
+ import { getSharedS3MWorker, S3MWorkerClient, WorkerDeadError } from '../worker/S3MWorkerClient.js';
10
11
  import {
11
12
  composeLeafAtlas,
12
13
  expandTexblockRequestNames,
@@ -235,6 +236,15 @@ export interface S3MTilesetOptions {
235
236
  /** 原始图像/子图集缓存上限(LRU),默认 128 */
236
237
  maxImageCache?: number;
237
238
  buildTile?: BuildTileFn | null;
239
+ /**
240
+ * 是否用 Web Worker 解析经典 `.s3mb` 瓦片(默认开)。
241
+ *
242
+ * Worker 里跑 parseS3MB 全链路(inflate / Draco / MeshOpt 内联 wasm 解码),
243
+ * 主线程只做 fetch 与几何组装,消除加载阶段的解析卡顿。
244
+ * Worker 创建失败或崩溃时自动**永久**降级回主线程解析,行为与关闭时完全一致。
245
+ * S3MBLOCK 路径(图集补齐与主线程缓存强耦合)暂不进 Worker,仍主线程解析。
246
+ */
247
+ useWorker?: boolean;
238
248
  uniforms?: StyleUniforms | null;
239
249
  onTile?: (node: TileNode) => void;
240
250
  /** 超过 maxTiles 被淘汰时回调,调用方要自己把场景里的对象和拾取表清掉 */
@@ -330,6 +340,8 @@ export class S3MTileset {
330
340
  private isS3MV1 = false;
331
341
  private _buildTile: BuildTileFn | null;
332
342
  private readonly _uniforms: StyleUniforms | null;
343
+ /** 经典 .s3mb 的 Worker 解析客户端;undefined = 尚未尝试创建,null = 不可用/已降级 */
344
+ private _workerClient: S3MWorkerClient | null | undefined; private readonly _useWorker: boolean;
333
345
  private _queue: string[] = [];
334
346
  private _active = 0;
335
347
 
@@ -346,6 +358,7 @@ export class S3MTileset {
346
358
  this._buildTile = options.buildTile ?? null;
347
359
  // 着色器共享 uniform,由 S3MLayer 持有并传进来
348
360
  this._uniforms = options.uniforms ?? null;
361
+ this._useWorker = options.useWorker !== false;
349
362
 
350
363
  // 并发与总量限制:iServer 公开服务上别把人家打爆
351
364
  this.maxConcurrent = options.maxConcurrent ?? 4;
@@ -677,6 +690,30 @@ export class S3MTileset {
677
690
  return this._buildTile;
678
691
  }
679
692
 
693
+ /**
694
+ * 经典 `.s3mb` 瓦片解析:优先走 Worker(把 inflate / Draco / MeshOpt 的 CPU 密集
695
+ * 解码搬离主线程),Worker 不可用或已崩溃时主线程兜底,两条路径输出完全一致。
696
+ */
697
+ private async _parseClassicTile(buffer: ArrayBuffer): Promise<ParsedTile> {
698
+ if (this._useWorker && this._workerClient === undefined) {
699
+ // 与纹理解码共用进程级单例,避免每个数据集各起一个 Worker
700
+ this._workerClient = getSharedS3MWorker();
701
+ }
702
+ if (this._workerClient) {
703
+ try {
704
+ return await this._workerClient.parseTile(buffer);
705
+ } catch (e) {
706
+ if (e instanceof WorkerDeadError) {
707
+ // 脚本加载失败 / 崩溃属于环境问题,永久退回主线程;瓦片本身的问题照常抛
708
+ this._workerClient = null;
709
+ } else {
710
+ throw e;
711
+ }
712
+ }
713
+ }
714
+ return parseS3MB(buffer);
715
+ }
716
+
680
717
  /**
681
718
  * 记住一个块文件里解析出来的全部 LOD 组。
682
719
  *
@@ -970,7 +1007,7 @@ export class S3MTileset {
970
1007
  groupNames = set.names;
971
1008
  tile = set.tiles[groupIndex] ?? set.tiles[0]!;
972
1009
  } else {
973
- tile = await parseS3MB(buffer);
1010
+ tile = await this._parseClassicTile(buffer);
974
1011
  }
975
1012
  }
976
1013
  if (isBlock) {
@@ -1269,6 +1306,7 @@ export class S3MTileset {
1269
1306
  }
1270
1307
 
1271
1308
  dispose(): void {
1309
+ this._workerClient?.dispose();
1272
1310
  for (const node of this.nodes.values()) {
1273
1311
  node.group.traverse((o) => {
1274
1312
  const mesh = o as {
@@ -0,0 +1,150 @@
1
+ /**
2
+ * 解析 Worker 的主线程客户端。
3
+ *
4
+ * 生命周期约定(由 S3MTileset 使用):
5
+ * - `create()` 返回 null = 环境不可用(无 Worker / 脚本同步创建失败),调用方直接走主线程解析;
6
+ * - `parseTile()` 抛 `WorkerDeadError` = Worker 脚本加载失败或已崩溃,调用方应**永久**
7
+ * 退回主线程解析(不要再给这个 client 发活,也别让每块瓦片都白白失败两轮重试);
8
+ * - 抛普通 Error = Worker 活着、但解析本身失败(网络字节坏了 / S3M1 容器永久错误等),
9
+ * 语义与主线程 `parseS3MB` 抛错完全一致,原样向上传。
10
+ */
11
+ import type { ParsedTile, S3MTexture } from '../core/types.js';
12
+ import type { DecodedTextureData } from '../core/TextureDecode.js';
13
+
14
+ /** Worker 崩溃 / 不可用 —— 与「瓦片数据本身有问题」区分开,调用方据此决定降级还是重试 */
15
+ export class WorkerDeadError extends Error {
16
+ constructor(message: string) {
17
+ super(message);
18
+ this.name = 'WorkerDeadError';
19
+ }
20
+ }
21
+
22
+ interface ParseOkResponse {
23
+ id: number;
24
+ ok: true;
25
+ tile: ParsedTile;
26
+ }
27
+
28
+ interface TexOkResponse {
29
+ id: number;
30
+ ok: true;
31
+ data: DecodedTextureData;
32
+ }
33
+
34
+ interface ErrResponse {
35
+ id: number;
36
+ ok: false;
37
+ message: string;
38
+ permanent: boolean;
39
+ }
40
+
41
+ type Response = ParseOkResponse | TexOkResponse | ErrResponse;
42
+
43
+ export class S3MWorkerClient {
44
+ private readonly _worker: Worker;
45
+ private _seq = 0;
46
+ private readonly _pending = new Map<number, { resolve: (v: unknown) => void; reject: (err: unknown) => void }>();
47
+ /** true = 已崩溃 / 已终止,后续请求直接拒绝 */
48
+ dead = false;
49
+
50
+ /** 创建失败返回 null(不抛),调用方据此走主线程解析 */
51
+ static create(): S3MWorkerClient | null {
52
+ if (typeof Worker === 'undefined') return null;
53
+ try {
54
+ // 路径锚定本模块的编译产物(dist/worker/s3mWorker.js);
55
+ // Vite 打包时会把这个 pattern 转成独立 worker 资产,IIFE 直引场景由死锁降级兜底
56
+ const worker = new Worker(new URL('./s3mWorker.js', import.meta.url), { type: 'module' });
57
+ return new S3MWorkerClient(worker);
58
+ } catch {
59
+ return null;
60
+ }
61
+ }
62
+
63
+ private constructor(worker: Worker) {
64
+ this._worker = worker;
65
+ worker.onmessage = (e: MessageEvent<Response>) => {
66
+ const msg = e.data;
67
+ const waiter = this._pending.get(msg.id);
68
+ if (!waiter) return;
69
+ this._pending.delete(msg.id);
70
+ if (!msg.ok) {
71
+ // 还原主线程 parseS3MB 的错误形状:permanent 标记驱动瓦片级的「永久失败」分支
72
+ const err = new Error(msg.message);
73
+ (err as { permanent?: boolean }).permanent = msg.permanent;
74
+ waiter.reject(err);
75
+ } else if ('tile' in msg) {
76
+ waiter.resolve(msg.tile);
77
+ } else {
78
+ waiter.resolve(msg.data);
79
+ }
80
+ };
81
+ worker.onerror = () => this._die('s3m 解析 Worker 加载失败/崩溃');
82
+ worker.onmessageerror = () => this._die('s3m 解析 Worker 消息无法反序列化');
83
+ }
84
+
85
+ /** 解析一块瓦片。buffer 会被 structured clone(不 transfer),主线程保留原 buffer 以便崩溃后降级重解 */
86
+ parseTile(buffer: ArrayBuffer): Promise<ParsedTile> {
87
+ if (this.dead) return Promise.reject(new WorkerDeadError('s3m 解析 Worker 已终止'));
88
+ const id = ++this._seq;
89
+ return new Promise<ParsedTile>((resolve, reject) => {
90
+ this._pending.set(id, { resolve: resolve as (v: unknown) => void, reject });
91
+ this._worker.postMessage({ type: 'parse', id, buffer });
92
+ });
93
+ }
94
+
95
+ /** 解码一张纹理(DXT / CRN / WEBP / RGB 系)。tex.data 走 structured clone,主线程保留原数据 */
96
+ decodeTexture(tex: S3MTexture): Promise<DecodedTextureData> {
97
+ if (this.dead) return Promise.reject(new WorkerDeadError('s3m 解析 Worker 已终止'));
98
+ const id = ++this._seq;
99
+ return new Promise<DecodedTextureData>((resolve, reject) => {
100
+ this._pending.set(id, { resolve: resolve as (v: unknown) => void, reject });
101
+ this._worker.postMessage({ type: 'tex', id, tex });
102
+ });
103
+ }
104
+
105
+ dispose(): void {
106
+ this._die('dispose');
107
+ }
108
+
109
+ private _die(reason: string): void {
110
+ if (this.dead) return;
111
+ this.dead = true;
112
+ try {
113
+ this._worker.terminate();
114
+ } catch {
115
+ /* 已死的 worker terminate 不会再抛 */
116
+ }
117
+ const err = new WorkerDeadError(`${reason},退回主线程解析`);
118
+ for (const waiter of this._pending.values()) waiter.reject(err);
119
+ this._pending.clear();
120
+ }
121
+ }
122
+
123
+ /* ------------------------------------------------------------------ 共享单例 */
124
+
125
+ /**
126
+ * 进程级共享的解析 Worker(解析 + 纹理解码共用一个线程,避免每个数据集 /
127
+ * 每个渲染层各起一个 Worker 把内存和调度打爆)。懒创建,崩溃后返回 null。
128
+ */
129
+ let sharedClient: S3MWorkerClient | null | undefined;
130
+
131
+ export function getSharedS3MWorker(): S3MWorkerClient | null {
132
+ if (sharedClient === undefined) sharedClient = S3MWorkerClient.create();
133
+ if (sharedClient && sharedClient.dead) return null;
134
+ return sharedClient;
135
+ }
136
+
137
+ /**
138
+ * 纹理解码的「Worker 优先」入口,供 TextureFactory.setTextureOffThreadDecode 装配。
139
+ * 永不 reject:Worker 不可用 / 崩溃 / 单张解码失败都返回 null,调用方主线程兜底
140
+ * (解码失败本身在 decodeTextureData 里也不抛错,走 crn(failed) 之类的标记)。
141
+ */
142
+ export async function decodeTextureWithWorker(tex: S3MTexture): Promise<DecodedTextureData | null> {
143
+ const client = getSharedS3MWorker();
144
+ if (!client) return null;
145
+ try {
146
+ return await client.decodeTexture(tex);
147
+ } catch {
148
+ return null;
149
+ }
150
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * S3M 解析 / 纹理解码 Worker 入口。
3
+ *
4
+ * 职责:把 CPU 密集的解码搬离主线程——
5
+ * - parse:`.s3mb`(含 S3M 1.x / Draco / MeshOpt 内联 wasm 解码)整块解析;
6
+ * - tex: 纹理解码(DXT 解块 / CRN wasm 转码 / WEBP createImageBitmap),
7
+ * 与主线程共用 core/TextureDecode.ts 的同一份实现。
8
+ * 抓取(fetch)仍由 S3MTileset 在主线程做——重试、退避、content-type 检查这些
9
+ * 语义都留在原处不动,Worker 只收字节、吐纯数据结果。
10
+ *
11
+ * 协议:
12
+ * { type:'parse', id, buffer } → { id, ok:true, tile } | { id, ok:false, message, permanent }
13
+ * { type:'tex', id, tex } → { id, ok:true, data } | { id, ok:false, message }
14
+ *
15
+ * 线程安全:两个入口都无模块级可变状态;每次调用用独立的 reader / 解码器实例。
16
+ */
17
+ import { parseS3MB } from '../core/S3MBParser.js';
18
+ import { decodeTextureData } from '../core/TextureDecode.js';
19
+ import type { ParsedTile, S3MTexture } from '../core/types.js';
20
+ import { collectTransferables } from './transferables.js';
21
+
22
+ type ParseRequest = { type: 'parse'; id: number; buffer: ArrayBuffer };
23
+ type TexRequest = { type: 'tex'; id: number; tex: S3MTexture };
24
+ type Request = ParseRequest | TexRequest;
25
+
26
+ type ParseResponse =
27
+ | { id: number; ok: true; tile: ParsedTile }
28
+ | { id: number; ok: false; message: string; permanent: boolean };
29
+
30
+ type TexResponse = { id: number; ok: true; data: Awaited<ReturnType<typeof decodeTextureData>> };
31
+
32
+ const ctx = self as unknown as Worker;
33
+
34
+ /** 纹理解码响应的传输清单:RGBA 像素 buffer + WEBP 的 ImageBitmap 都走零拷贝 */
35
+ function texTransferList(data: Awaited<ReturnType<typeof decodeTextureData>>): ArrayBuffer[] {
36
+ const list = collectTransferables(data);
37
+ if (data.bitmap) list.push(data.bitmap as unknown as ArrayBuffer);
38
+ return list;
39
+ }
40
+
41
+ ctx.onmessage = async (e: MessageEvent<Request>) => {
42
+ const msg = e.data;
43
+ const { id } = msg;
44
+ try {
45
+ if (msg.type === 'parse') {
46
+ const tile = await parseS3MB(msg.buffer);
47
+ // 响应整体过线(零拷贝);请求 buffer 是主线程 structured clone 过来的副本,无需回传
48
+ ctx.postMessage({ id, ok: true, tile } satisfies ParseResponse, collectTransferables(tile));
49
+ } else {
50
+ const data = await decodeTextureData(msg.tex);
51
+ ctx.postMessage({ id, ok: true, data } satisfies TexResponse, texTransferList(data));
52
+ }
53
+ } catch (err) {
54
+ const permanent = (err as { permanent?: boolean } | null)?.permanent === true;
55
+ const message = String((err as Error | null)?.message ?? err);
56
+ ctx.postMessage({ id, ok: false, message, permanent });
57
+ }
58
+ };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * 传输清单收集:递归扫出对象图里所有可 Transfer 的 ArrayBuffer(去重)。
3
+ *
4
+ * 用于 Worker 往返的零拷贝:postMessage 的 transferList 传这个清单后,
5
+ * 线程间共享同一块内存(structured clone 只拷贝「壳」,不拷字节)。
6
+ *
7
+ * 安全性约定:**要么全转,要么不转** —— 同一块 ArrayBuffer 上可能挂着多个视图
8
+ * (positions / normals 常是同一块顶点缓冲的 subarray),transferList 里出现它一次
9
+ * 之后所有视图共享的底层数据整体过线,视图本身仍然有效;但如果只转一半视图、
10
+ * 另一半留在原地,留在原地的会因 detach 变成空数据。所以只允许「把整个对象图
11
+ * 的 buffer 全部收集」或「完全不收集」两种用法,不支持挑着转。
12
+ */
13
+
14
+ /** 递归深度上限。ParsedTile 的层级是固定的(tile → geoPackage → geode → package),远用不到 12 层 */
15
+ const MAX_DEPTH = 12;
16
+
17
+ export function collectTransferables(root: unknown): ArrayBuffer[] {
18
+ const buffers = new Set<ArrayBuffer>();
19
+ const seen = new Set<object>();
20
+
21
+ const walk = (value: unknown, depth: number): void => {
22
+ if (value == null || depth > MAX_DEPTH) return;
23
+ if (ArrayBuffer.isView(value)) {
24
+ const b = (value as ArrayBufferView).buffer;
25
+ if (b instanceof ArrayBuffer) buffers.add(b);
26
+ return;
27
+ }
28
+ if (value instanceof ArrayBuffer) {
29
+ buffers.add(value);
30
+ return;
31
+ }
32
+ if (typeof value !== 'object') return;
33
+ const obj = value as object;
34
+ if (seen.has(obj)) return;
35
+ seen.add(obj);
36
+ if (Array.isArray(obj)) {
37
+ for (const item of obj) walk(item, depth + 1);
38
+ return;
39
+ }
40
+ if (obj instanceof Map) {
41
+ for (const [k, v] of obj) {
42
+ walk(k, depth + 1);
43
+ walk(v, depth + 1);
44
+ }
45
+ return;
46
+ }
47
+ if (obj instanceof Set) {
48
+ for (const item of obj) walk(item, depth + 1);
49
+ return;
50
+ }
51
+ for (const key of Object.keys(obj)) {
52
+ walk((obj as Record<string, unknown>)[key], depth + 1);
53
+ }
54
+ };
55
+
56
+ walk(root, 0);
57
+ return [...buffers];
58
+ }