s3m-three 0.4.15 → 0.4.16

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.
@@ -25,9 +25,10 @@ import { usableGeoBounds } from '../tileset/S3MTileset.js';
25
25
  import { setMaxAnisotropy, setCompressedTexturesSupported, setTextureOffThreadDecode, setMaxTextureSize as setTextureSizeCap } from './TextureFactory.js';
26
26
  import { setMaterialQuality } from './StyleMaterial.js';
27
27
  import { decodeTextureWithWorker } from '../worker/S3MWorkerClient.js';
28
- import { STYLE, STYLE_LABELS, SELECT_MODE, DEFAULT_STYLE, createSharedUniforms, createOutlineMaterial, } from './StyleMaterial.js';
28
+ import { STYLE, STYLE_LABELS, SELECT_MODE, DEFAULT_STYLE, createSharedUniforms, createOutlineMaterial, resolveEdgeModes, sameEdgeModes, } from './StyleMaterial.js';
29
29
  import { applyPointCloudStyle, isOwnedPointCloudMaterial } from './PointCloudMaterial.js';
30
30
  import { Picker } from './Picker.js';
31
+ import { GpuEdgePass } from './GpuEdgePass.js';
31
32
  import { Animator } from '../anim/Animator.js';
32
33
  import { Path } from '../anim/Path.js';
33
34
  import { GPUPathManager } from './GPUPathManager.js';
@@ -82,6 +83,8 @@ const STYLE_SCALAR_KEYS = [
82
83
  'ambientGain',
83
84
  'edgeWidth',
84
85
  'edgeOpacity',
86
+ 'edgeOffset',
87
+ 'edgeHullWidth',
85
88
  'outlineWidth',
86
89
  ];
87
90
  /** 能逐帧插值的颜色键(在线性空间插值,与 shader 消费端一致) */
@@ -120,6 +123,8 @@ export class S3MViewer {
120
123
  _edgeStyleUniforms;
121
124
  /** 选中棱线的外观 uniform */
122
125
  _selEdgeStyleUniforms;
126
+ /** 全局外扩轮廓的外观 uniform:逐网格轮廓壳共享这些对象 */
127
+ _hullStyleUniforms;
123
128
  /** 3D 物理地表面 / 基坑开挖管理器 */
124
129
  groundManager;
125
130
  /** 多数据集生命周期与锚点管理器 */
@@ -157,6 +162,12 @@ export class S3MViewer {
157
162
  _selectionEdges = null;
158
163
  _selectionEdgeMeshes = [];
159
164
  _edgesEnabled = false;
165
+ /** 当前生效的棱线画法(line / offset / hull / gpu),决定要不要建 / 拆哪种装饰 */
166
+ _edgeModes = resolveEdgeModes(false);
167
+ /** GPU 描边后处理(深度 + 法线),只在 edges 含 'gpu' 时按帧执行 */
168
+ _gpuEdge = null;
169
+ /** 给 GPU 描边传色用的临时 Color,避免每帧 new */
170
+ _gpuEdgeColor = new THREE.Color();
160
171
  /** 瓦片淡入通道的自增序号(不用 pickId:pickId 会被回收复用) */
161
172
  _tileFadeSeq = 0;
162
173
  /** setMode 编排的代次,用来作废被新调用顶掉的旧编排 */
@@ -281,6 +292,7 @@ export class S3MViewer {
281
292
  uColor: { value: new THREE.Color(this.style.edgeColor) },
282
293
  uWidth: { value: Math.max(0.1, this.style.edgeWidth ?? 1.5) },
283
294
  uOpacity: { value: 0.6 },
295
+ uOffset: { value: this.style.edgeOffset ?? 0.15 },
284
296
  };
285
297
  this.edgeMaterial = createThickLineMaterial(this.lineShared, {}, this._edgeStyleUniforms);
286
298
  this._selEdgeStyleUniforms = {
@@ -290,10 +302,16 @@ export class S3MViewer {
290
302
  uElevation: { value: this.style.selectElevation ?? 0 },
291
303
  };
292
304
  this.selectionEdgeMaterial = createThickLineMaterial(this.lineShared, {}, this._selEdgeStyleUniforms);
305
+ // 全局外扩轮廓的外观 uniform:逐网格轮廓壳复用这几个对象,改一次全场景生效
306
+ this._hullStyleUniforms = {
307
+ uColor: { value: new THREE.Color(this.style.edgeColor) },
308
+ uWidth: { value: Math.max(0, this.style.edgeHullWidth ?? 0.9) },
309
+ uOpacity: { value: 0.6 },
310
+ };
293
311
  // 关键:style.edges 是个开关,但构造函数从没据此启用过棱线,导致
294
312
  // `style: { edges: true }` 形同虚设、_edgesEnabled 永远是 false。
295
313
  // 这里按 style 实际值调一次 setEdges,让开关真正生效(与 setEdges 后来的手工调用一致)。
296
- this.setEdges(!!this.style.edges);
314
+ this.setEdges(this.style.edges);
297
315
  // 初始化图层整体不透明度:确保共享 uniform uOpacity、材质混合开关与棱线外观与初始 style.opacity 严格一致
298
316
  this._applyOpacity();
299
317
  this._fps.last = performance.now();
@@ -371,6 +389,9 @@ export class S3MViewer {
371
389
  this._syncTextureCapabilities(gl);
372
390
  this.picker?.dispose();
373
391
  this.picker = new Picker(this.renderer);
392
+ // GPU 描边的 RT / 材质绑定在旧上下文上,整体丢弃,下一帧按需重建
393
+ this._gpuEdge?.dispose();
394
+ this._gpuEdge = null;
374
395
  // 共享 uniform / 材质引用不变,three 重新编译着色器时会自然接管;
375
396
  // 这里只确保下一帧尽快驱动一次渲染。
376
397
  this._requestRepaint();
@@ -384,6 +405,8 @@ export class S3MViewer {
384
405
  this.picker?.dispose();
385
406
  this.picker = null;
386
407
  this.outlineMaterial.dispose();
408
+ this._gpuEdge?.dispose();
409
+ this._gpuEdge = null;
387
410
  this.edgeMaterial.dispose();
388
411
  this.selectionEdgeMaterial.dispose();
389
412
  this.scene?.clear();
@@ -1302,11 +1325,16 @@ export class S3MViewer {
1302
1325
  entry.root.add(node.group);
1303
1326
  node.group.updateMatrixWorld(true);
1304
1327
  const hasNativeEdges = node.group.userData.hasNativeEdges === true;
1328
+ // forceCpuEdges:无视数据自带的棱线包,改由 CPU 现算。native 是 1px 的
1329
+ // LineBasicMaterial,edgeWidth / 外扩 / 单体跟随全都吃不上。
1330
+ // gpu 画法由 shader 自己描边,native 线段包同样不能留
1331
+ const useNativeEdges = hasNativeEdges && this.style.forceCpuEdges !== true && !this._edgeModes.gpu;
1305
1332
  for (const child of node.group.children) {
1306
1333
  // 数据自带的棱线包:只跟着 edges 开关显示,不参与拾取、不参与着色
1307
1334
  if (child.isLineSegments && child.userData.isEdgeLines) {
1308
- child.visible = this._edgesEnabled;
1335
+ // 一律登记进 _nativeEdgeLines:forceCpuEdges 之后又关掉时还要能把它们放出来
1309
1336
  this._nativeEdgeLines.add(child);
1337
+ child.visible = useNativeEdges && this._edgesEnabled;
1310
1338
  this._styleEdgeLine(child);
1311
1339
  continue;
1312
1340
  }
@@ -1320,7 +1348,8 @@ export class S3MViewer {
1320
1348
  const ud = mesh.userData;
1321
1349
  ud.pickId = this._allocPickId();
1322
1350
  ud.datasetId = entry.id;
1323
- ud.nativeEdges = hasNativeEdges;
1351
+ ud.nativeEdges = useNativeEdges;
1352
+ ud.nativeEdgesRaw = hasNativeEdges;
1324
1353
  this._pickables.set(ud.pickId, {
1325
1354
  datasetId: entry.id,
1326
1355
  mesh,
@@ -1332,8 +1361,13 @@ export class S3MViewer {
1332
1361
  // 点云没有「棱」:给一堆孤立点建 EdgesGeometry 又慢又没意义
1333
1362
  // 点云没有「棱」:给一堆孤立点建 EdgesGeometry 又慢又没意义。
1334
1363
  // 自动描边受三道开关控制:全局 style.edges / 数据集 enableEdges / 地形绿地等关键字跳过。
1335
- if (!hasNativeEdges && !isPoints && this._datasetEdgesEnabled(entry) && !this._shouldSkipAutoEdges(ud, entry.name)) {
1336
- this._ensureEdges(mesh);
1364
+ if (!useNativeEdges && !isPoints && this._datasetEdgesEnabled(entry) && !this._shouldSkipAutoEdges(ud, entry.name)) {
1365
+ // 数据集强制开启(enableEdges: true)时全局画法可能还是关的,退回默认折边线;
1366
+ // 但 gpu 画法本来就不建几何,别在这里多此一举
1367
+ if (this._edgeModes.line || this._edgeModes.hull)
1368
+ this._ensureEdgeDecoration(mesh);
1369
+ else if (!this._edgeModes.gpu)
1370
+ this._ensureEdges(mesh);
1337
1371
  }
1338
1372
  // 新瓦片要继承当前的不透明度与着色方式
1339
1373
  if (isPoints) {
@@ -1571,7 +1605,7 @@ export class S3MViewer {
1571
1605
  if ('edges' in patch) {
1572
1606
  const { edges, ...others } = patch;
1573
1607
  rest = others;
1574
- this.setEdges(!!edges, wantAnim ? transition : undefined);
1608
+ this.setEdges(edges, wantAnim ? transition : undefined);
1575
1609
  if (Object.keys(rest).length === 0) {
1576
1610
  this._requestRepaint();
1577
1611
  return;
@@ -1682,7 +1716,11 @@ export class S3MViewer {
1682
1716
  return;
1683
1717
  }
1684
1718
  this._applyStyleUniforms();
1685
- if (key === 'edgeColor' || key === 'edgeWidth' || key === 'edgeOpacity') {
1719
+ if (key === 'edgeColor' ||
1720
+ key === 'edgeWidth' ||
1721
+ key === 'edgeOpacity' ||
1722
+ key === 'edgeOffset' ||
1723
+ key === 'edgeHullWidth') {
1686
1724
  this._refreshEdgeLines();
1687
1725
  }
1688
1726
  }
@@ -1717,11 +1755,17 @@ export class S3MViewer {
1717
1755
  // 临界角变了要重建 CPU 棱线(EdgesGeometry 的参数),开 / 关走 setEdges 整体刷新。
1718
1756
  // 必须先 _disposeEdges():setEdges 只挑「还没有棱线」的网格来建,
1719
1757
  // 不先拆掉的话已经建好的会被全部跳过,临界角改了等于没改。
1720
- if ('edgeThreshold' in patch && this._edgesEnabled) {
1758
+ if (('edgeThreshold' in patch || 'forceCpuEdges' in patch) && this._edgesEnabled) {
1721
1759
  this._disposeEdges();
1722
- this.setEdges(true);
1723
- }
1724
- else if ('edgeColor' in patch || 'edgeWidth' in patch || 'opacity' in patch || 'edgeOpacity' in patch) {
1760
+ this.setEdges(this.style.edges);
1761
+ }
1762
+ else if ('edgeColor' in patch ||
1763
+ 'edgeWidth' in patch ||
1764
+ 'opacity' in patch ||
1765
+ 'edgeOpacity' in patch ||
1766
+ 'edgeOffset' in patch ||
1767
+ 'edgeHullWidth' in patch) {
1768
+ // 颜色 / 粗细 / 外推距离 / 外扩厚度都只是 uniform,拖着滑块逐帧改也不重建几何
1725
1769
  this._refreshEdgeLines();
1726
1770
  }
1727
1771
  if (this._selection) {
@@ -2004,8 +2048,15 @@ export class S3MViewer {
2004
2048
  /**
2005
2049
  * 全局棱线开关。
2006
2050
  *
2007
- * 数据自带棱线包(primitiveType 13)时直接用,成本为零;
2008
- * 没有的话才用 EdgesGeometry 现算 —— 那是 CPU 端的活,几十万面一次性建会卡住主线程
2051
+ * 取值是 `boolean | EdgeStyle | EdgeStyle[]`(见 resolveEdgeModes):
2052
+ * - `true` / `'line'` 折边线(旧口径)
2053
+ * - `'offset'` 折边线 + 沿边法线外推 `edgeOffset` 米
2054
+ * - `'hull'` 背面外扩轮廓(选中侧 outlineStyle:'hull' 的整场景版)
2055
+ * - `'gpu'` 纯片元描边:零几何、零额外 pass,线宽约 1 像素
2056
+ * - 数组 叠加,例如 `['line', 'hull']`
2057
+ *
2058
+ * 数据自带棱线包(primitiveType 13)时直接用,成本为零;`style.forceCpuEdges`
2059
+ * 可以无视它,改由 CPU 现算 —— 那是 CPU 端的活,几十万面一次性建会卡住主线程
2009
2060
  * (实测 20 个网格 1.9s),所以按 8ms 一帧切片,边建边出。
2010
2061
  *
2011
2062
  * 棱线本身用「屏幕空间等宽线」画(见 EdgeLines.ts):WebGL 的 lineWidth 基本无效,
@@ -2015,23 +2066,33 @@ export class S3MViewer {
2015
2066
  * 开:立刻置位 + 开始分片建线,同时 `uEdgeFade` 0→1 —— 建好一块亮一块,视觉上是渐显;
2016
2067
  * 关:`uEdgeFade` 1→0,淡完才 dispose,避免棱线「啪」一下消失。
2017
2068
  */
2018
- setEdges(enabled, transition) {
2069
+ setEdges(edges, transition) {
2019
2070
  const wantAnim = !!transition && ((transition.duration ?? 0) > 0 || !!transition.spring);
2020
- this._edgesEnabled = enabled;
2021
- this.style.edges = enabled;
2022
- this._styleTarget.edges = enabled;
2023
- // 数据自带的棱线:直接切可见性
2024
- for (const entry of this.datasets.values()) {
2025
- entry.root.traverse((o) => {
2026
- const ud = o.userData;
2027
- if (ud.isEdgeLines) {
2028
- o.visible = enabled;
2029
- this._styleEdgeLine(o);
2030
- }
2031
- });
2071
+ const modes = resolveEdgeModes(edges);
2072
+ // 换画法(line ↔ hull ↔ offset)是结构变更:旧装饰必须拆掉再建,
2073
+ // 否则会出现「切到 hull 了折边线还留着」这种叠影。
2074
+ // 同一画法重复调用(滑块逐帧 setEdges(true))不重建,几何是复用的。
2075
+ const modesChanged = !sameEdgeModes(this._edgeModes, modes);
2076
+ this._edgeModes = modes;
2077
+ this._edgesEnabled = modes.enabled;
2078
+ this.style.edges = edges;
2079
+ this._styleTarget.edges = edges;
2080
+ // 数据自带的棱线:直接切可见性(forceCpuEdges 时永远收起,改由 CPU 现算)。
2081
+ // 只遍历注册时收集好的那批 LineSegments —— 全数据集 traverse 在几千节点下太贵。
2082
+ // gpu 模式下 shader 自己描边,native 线段包必须收起,否则叠成双线
2083
+ const nativeVisible = modes.enabled && !modes.gpu && this.style.forceCpuEdges !== true;
2084
+ for (const lines of this._nativeEdgeLines) {
2085
+ lines.visible = nativeVisible;
2086
+ this._styleEdgeLine(lines);
2032
2087
  }
2033
2088
  this._syncEdgeStyle();
2034
- if (!enabled) {
2089
+ // 关掉 gpu 画法就把后处理资源还回去(整屏 RT 白占显存没意义)。
2090
+ // 必须放在「整体关闭」的早退之前,否则关棱线时永远轮不到它。
2091
+ if (!modes.gpu && this._gpuEdge) {
2092
+ this._gpuEdge.dispose();
2093
+ this._gpuEdge = null;
2094
+ }
2095
+ if (!modes.enabled) {
2035
2096
  if (wantAnim) {
2036
2097
  this.animator.to('style.edgeFade', 0, {
2037
2098
  ...transition,
@@ -2049,18 +2110,25 @@ export class S3MViewer {
2049
2110
  this._requestRepaint();
2050
2111
  return;
2051
2112
  }
2113
+ if (modesChanged)
2114
+ this._disposeEdges();
2052
2115
  const pending = [...this._pickables.values()]
2053
2116
  .map((p) => p.mesh)
2054
2117
  .filter((m) => {
2055
2118
  const ud = m.userData;
2056
2119
  // 点云没有棱可描
2057
- return !ud.edges && !ud.nativeEdges && !isPointCloudObject(m);
2120
+ if (isPointCloudObject(m))
2121
+ return false;
2122
+ // native 棱线还算不算数,取决于**当前**的 forceCpuEdges(它可以在运行中切换),
2123
+ // 所以这里按注册时记下的原始标记重算,而不是沿用注册那一刻的结论。
2124
+ ud.nativeEdges = ud.nativeEdgesRaw === true && this.style.forceCpuEdges !== true;
2125
+ return !ud.nativeEdges && this._needsEdgeDecoration(ud);
2058
2126
  });
2059
2127
  let i = 0;
2060
2128
  const step = () => {
2061
2129
  const t0 = performance.now();
2062
2130
  while (i < pending.length && performance.now() - t0 < 8)
2063
- this._ensureEdges(pending[i++]);
2131
+ this._ensureEdgeDecoration(pending[i++]);
2064
2132
  if (i < pending.length)
2065
2133
  setTimeout(step, 0);
2066
2134
  else
@@ -2133,10 +2201,11 @@ export class S3MViewer {
2133
2201
  this._requestRepaint();
2134
2202
  }
2135
2203
  /**
2136
- * 让全局棱线材质跟上 style.edgeColor / edgeWidth / edgeOpacity。
2204
+ * 让全局棱线材质跟上 style.edgeColor / edgeWidth / edgeOpacity / edgeOffset。
2137
2205
  *
2138
- * 全程 O(1):只有一份共享 edgeMaterial(所有逐 mesh 棱线都用它)+ 一份预制缓存,
2139
- * 所以棱线淡入淡出只要改一个 uniform,新建瓦片自动继承当前进度。
2206
+ * 全程 O(1):只有一份共享 edgeMaterial(所有逐 mesh 棱线都用它)+ 一份预制缓存
2207
+ * + 一批共享外观对象的轮廓壳,所以棱线淡入淡出只要改一个 uniform,
2208
+ * 新建瓦片自动继承当前进度。
2140
2209
  */
2141
2210
  _syncEdgeStyle() {
2142
2211
  const s = this.style;
@@ -2148,6 +2217,14 @@ export class S3MViewer {
2148
2217
  u.uColor.value.set(s.edgeColor);
2149
2218
  u.uWidth.value = Math.max(0.1, s.edgeWidth ?? 1.5);
2150
2219
  u.uOpacity.value = opacity;
2220
+ // 外推距离只在 offset 画法下非 0;其它画法把线钉在面上(与改动前一致)
2221
+ u.uOffset.value = this._edgeModes.offset ? Math.max(0, s.edgeOffset ?? 0.15) : 0;
2222
+ // 外扩轮廓壳与折边线共用颜色 / 透明度,厚度是米制的单独一项
2223
+ const h = this._hullStyleUniforms;
2224
+ h.uColor.value.set(s.edgeColor);
2225
+ h.uWidth.value = Math.max(0, s.edgeHullWidth ?? 0.9);
2226
+ h.uOpacity.value = opacity;
2227
+ // GPU 描边(edges: 'gpu')是后处理,参数每帧从 style 现取,这里不用同步 uniform
2151
2228
  // 预制棱线缓存(LineBasicMaterial)同步颜色 / 不透明度
2152
2229
  if (this._creaseLines) {
2153
2230
  const m = this._creaseLines.material;
@@ -2205,6 +2282,25 @@ export class S3MViewer {
2205
2282
  const hay = `${ud.skeletonName ?? ''} ${datasetName}`.toLowerCase();
2206
2283
  return S3MViewer._EDGE_SKIP_KEYWORDS.some((k) => hay.includes(k));
2207
2284
  }
2285
+ /**
2286
+ * 这块网格还缺哪种棱线装饰(缺就建,齐了就不动)。
2287
+ *
2288
+ * setEdges 的分片建线靠它筛「还没建完的」,注册新瓦片也靠它决定要不要动手。
2289
+ */
2290
+ _needsEdgeDecoration(ud) {
2291
+ if (this._edgeModes.line && !ud.edges)
2292
+ return true;
2293
+ if (this._edgeModes.hull && !ud.edgeHull)
2294
+ return true;
2295
+ return false;
2296
+ }
2297
+ /** 按当前画法补齐这块网格的棱线装饰 */
2298
+ _ensureEdgeDecoration(mesh) {
2299
+ if (this._edgeModes.line)
2300
+ this._ensureEdges(mesh);
2301
+ if (this._edgeModes.hull)
2302
+ this._ensureHull(mesh);
2303
+ }
2208
2304
  _ensureEdges(mesh) {
2209
2305
  const ud = mesh.userData;
2210
2306
  if (ud.edges || ud.nativeEdges || !mesh.geometry)
@@ -2225,11 +2321,23 @@ export class S3MViewer {
2225
2321
  const welded = mergeVerticesByPosition(mesh.geometry, 0.005);
2226
2322
  // 2. 调大临界角(默认 55°,可在 style.edgeThreshold 调),过滤浅坡与曲面细分,只保留真正的棱。
2227
2323
  const thresholdAngle = this.style.edgeThreshold ?? 55;
2228
- const source = new THREE.EdgesGeometry(welded, thresholdAngle);
2324
+ // 'offset' 画法要额外带一条「边法线」才知道往哪推,所以走本地实现(它本来就算好了
2325
+ // 相邻两面法线);其余画法保持 EdgesGeometry 这条老路,几何输出与改动前逐字一致。
2326
+ let segments;
2327
+ let normals;
2328
+ if (this._edgeModes.offset) {
2329
+ const raw = [];
2330
+ segments = buildFeatureEdgeSegments(welded, null, thresholdAngle, raw);
2331
+ if (raw.length > 0)
2332
+ normals = new Float32Array(raw);
2333
+ }
2334
+ else {
2335
+ const source = new THREE.EdgesGeometry(welded, thresholdAngle);
2336
+ const attr = source.getAttribute('position');
2337
+ segments = attr ? attr.array : null;
2338
+ source.dispose();
2339
+ }
2229
2340
  welded.dispose();
2230
- const attr = source.getAttribute('position');
2231
- const segments = attr ? attr.array : null;
2232
- source.dispose();
2233
2341
  if (!segments || segments.length < 6)
2234
2342
  return;
2235
2343
  // 3. 回填每段棱线所属的单体 batchId。
@@ -2247,7 +2355,7 @@ export class S3MViewer {
2247
2355
  batchMap.get(positionQuantKey(segments[s], segments[s + 1], segments[s + 2], 1 / TOL)) ?? -1;
2248
2356
  }
2249
2357
  }
2250
- const lines = new THREE.Mesh(buildThickLineGeometry(segments, batchIds), this._createEdgeMaterial(mesh));
2358
+ const lines = new THREE.Mesh(buildThickLineGeometry(segments, batchIds, normals), this._createEdgeMaterial(mesh));
2251
2359
  lines.renderOrder = 2;
2252
2360
  // 屏幕空间外扩后可能超出原包围球,关掉剔除更稳
2253
2361
  lines.frustumCulled = false;
@@ -2256,6 +2364,44 @@ export class S3MViewer {
2256
2364
  ud.edges = lines;
2257
2365
  mesh.add(lines); // 作为子节点,自动继承 mesh 的世界变换
2258
2366
  }
2367
+ /**
2368
+ * 给一块网格挂「背面外扩轮廓」(整场景版)。
2369
+ *
2370
+ * 复用选中侧那套 `createOutlineMaterial`:顶点沿法线鼓 `edgeHullWidth` 米、只画背面,
2371
+ * 于是被本体挡住的部分不显示,露出来的那圈就是描边。与折边线的差别在于——
2372
+ * 这里**不建任何新几何**,成本只是把同一块网格多画一遍,折边判定那套 CPU 活全省了。
2373
+ *
2374
+ * 实例化几何拿不到逐实例变换(轮廓壳是 mesh 的子节点,不吃 instanceMatrix),
2375
+ * 直接跳过:宁可这栋没描边,也不要所有实例的壳叠在原点。
2376
+ */
2377
+ _ensureHull(mesh) {
2378
+ const ud = mesh.userData;
2379
+ if (ud.edgeHull || !mesh.geometry)
2380
+ return;
2381
+ if (mesh.isInstancedMesh)
2382
+ return;
2383
+ // 地形 / 绿地 / 道路等不参与自动描边(即使全局开棱线也要跳过)
2384
+ if (this._shouldSkipAutoEdges(ud, ''))
2385
+ return;
2386
+ // 逐网格一份材质(各绑各的单体状态纹理),外观 uniform 复用共享对象
2387
+ const mat = createOutlineMaterial(this.uniforms, this.style.edgeHullWidth ?? 0.9, this._hullStyleUniforms, true);
2388
+ // 整块几何都要外扩:选中侧靠 uSelBatch 只扩一栋,全局必须置 whole
2389
+ mat.uniforms.uSelWhole.value = 1;
2390
+ mat.uniforms.uSelBatch.value = -1;
2391
+ const styled = styledMaterialOf(mesh);
2392
+ if (styled?.userData?.uBatchTex) {
2393
+ mat.uniforms.uBatchTex.value = styled.userData.uBatchTex.value;
2394
+ mat.uniforms.uBatchTexWidth.value = styled.userData.uBatchTexWidth.value;
2395
+ mat.uniforms.uBatchTexActive.value = styled.userData.uBatchTexActive.value;
2396
+ }
2397
+ const hull = new THREE.Mesh(mesh.geometry, mat);
2398
+ hull.renderOrder = 2;
2399
+ hull.frustumCulled = false;
2400
+ hull.raycast = () => { };
2401
+ hull.userData.isEdgeHull = true;
2402
+ ud.edgeHull = hull;
2403
+ mesh.add(hull); // 作为子节点,自动继承 mesh 的世界变换
2404
+ }
2259
2405
  /**
2260
2406
  * 给一块网格造专属的棱线材质。
2261
2407
  *
@@ -2302,19 +2448,30 @@ export class S3MViewer {
2302
2448
  _removeEdges(mesh) {
2303
2449
  const ud = mesh.userData;
2304
2450
  const lines = ud.edges;
2305
- if (!lines)
2306
- return;
2307
- mesh.remove(lines);
2308
- lines.geometry?.dispose();
2309
- // 棱线材质现在是逐网格一份(要绑各自的 batch 纹理),得跟着释放
2310
- const mat = lines.material;
2311
- if (Array.isArray(mat)) {
2312
- mat.forEach((m) => m?.dispose?.());
2313
- }
2314
- else {
2315
- mat?.dispose?.();
2451
+ if (lines) {
2452
+ mesh.remove(lines);
2453
+ lines.geometry?.dispose();
2454
+ // 棱线材质现在是逐网格一份(要绑各自的 batch 纹理),得跟着释放
2455
+ const mat = lines.material;
2456
+ if (Array.isArray(mat)) {
2457
+ mat.forEach((m) => m?.dispose?.());
2458
+ }
2459
+ else {
2460
+ mat?.dispose?.();
2461
+ }
2462
+ ud.edges = null;
2463
+ }
2464
+ // 外扩壳的 geometry 是**复用主网格的**,只能释放材质,不能 dispose 几何
2465
+ const hull = ud.edgeHull;
2466
+ if (hull) {
2467
+ mesh.remove(hull);
2468
+ const hmat = hull.material;
2469
+ if (Array.isArray(hmat))
2470
+ hmat.forEach((m) => m?.dispose?.());
2471
+ else
2472
+ hmat?.dispose?.();
2473
+ ud.edgeHull = null;
2316
2474
  }
2317
- ud.edges = null;
2318
2475
  }
2319
2476
  /* ---------------------------------------------------------------- 拾取 */
2320
2477
  /** 获取指定屏幕像素对应的场景射线(用于高精度求交与穿透拾取) */
@@ -3001,6 +3158,35 @@ export class S3MViewer {
3001
3158
  this._selectionEdges = null;
3002
3159
  }
3003
3160
  /* ---------------------------------------------------------------- 渲染 */
3161
+ /**
3162
+ * 执行一帧 GPU 描边(法线 + 深度后处理)。
3163
+ *
3164
+ * 参数全部来自 style,不缓存:`edgeWidth` 在这里是「采样偏移」,线宽 ≈ 2×它;
3165
+ * `edgeThreshold` 是折边敏感度(夹角小于它不描),和 CPU 折边线同一口径。
3166
+ */
3167
+ _renderGpuEdges(canvas) {
3168
+ if (!this.renderer || !this.scene || !this.camera)
3169
+ return;
3170
+ const s = this.style;
3171
+ const fade = this.uniforms.uEdgeFade?.value ?? 1;
3172
+ const opacity = Math.min(1, Math.max(0, s.edgeOpacity ?? 0.6)) * clampOpacity(s.opacity) * fade;
3173
+ if (opacity <= 0.004)
3174
+ return;
3175
+ if (!this._gpuEdge)
3176
+ this._gpuEdge = new GpuEdgePass();
3177
+ if (!this._gpuEdge.supported(this.renderer))
3178
+ return;
3179
+ // 度数 → 归一化法线差(|Δn| = 2·sin(θ/2)),乘 0.6 让阈值落在「刚好能描出」的位置
3180
+ const deg = Math.min(179, Math.max(1, s.edgeThreshold ?? 55));
3181
+ const normalThreshold = 2 * Math.sin((deg * Math.PI) / 360) * 0.6;
3182
+ this._gpuEdge.render(this.renderer, this.scene, this.camera, canvas.width, canvas.height, {
3183
+ color: this._gpuEdgeColor.set(s.edgeColor),
3184
+ opacity,
3185
+ radius: Math.max(1, s.edgeWidth ?? 1.5),
3186
+ normalThreshold,
3187
+ depthThreshold: 0.08,
3188
+ });
3189
+ }
3004
3190
  /** 等宽线要按设备像素换算线宽:uResolution 用绘图缓冲尺寸,uDpr 是缓冲 / CSS 的比值 */
3005
3191
  _updateLineResolution(canvas) {
3006
3192
  const w = canvas.width || 1;
@@ -3223,6 +3409,10 @@ export class S3MViewer {
3223
3409
  // 这里跳过的是「本层的绘制」,宿主自己的清屏 / 底图照旧。
3224
3410
  if ((this.uniforms.uOpacity?.value ?? 1) > OPACITY_EPS) {
3225
3411
  this.renderer.render(this.scene, this.camera);
3412
+ // GPU 描边必须画在主场景之后:它的全屏 quad 参与宿主深度测试,
3413
+ // 依赖本帧已经写好的深度(被地形挡住的地方不该描边)
3414
+ if (this._edgeModes.gpu)
3415
+ this._renderGpuEdges(canvas);
3226
3416
  }
3227
3417
  this._frame++;
3228
3418
  const now = performance.now();