@mesh3d/cesium-vectortile-gl 0.4.4 → 0.4.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.
- package/.gitattributes +11 -0
- package/.gitconfig +3 -0
- package/.husky/pre-commit +1 -0
- package/.prettierignore +5 -0
- package/.vscode/settings.json +25 -0
- package/LICENSE.md +203 -203
- package/README.md +202 -167
- package/Source/Cesium.d.ts +2692 -2691
- package/Source/VectorTileLOD.js +720 -532
- package/Source/VectorTileRenderList.js +70 -70
- package/Source/VectorTileset.js +473 -447
- package/Source/layers/BackgroundRenderLayer.js +91 -89
- package/Source/layers/FillRenderLayer.js +18 -18
- package/Source/layers/IRenderLayer.js +160 -152
- package/Source/layers/LineRenderLayer.js +104 -94
- package/Source/layers/SymbolRenderLayer.js +30 -31
- package/Source/layers/index.js +23 -16
- package/Source/layers/registerRenderLayer.js +24 -24
- package/Source/layers/visualizers/FillLayerVisualizer.js +542 -426
- package/Source/layers/visualizers/ILayerVisualizer.js +90 -94
- package/Source/layers/visualizers/LineLayerVisualizer.js +702 -571
- package/Source/layers/visualizers/SymbolLayerVisualizer.js +514 -244
- package/Source/sources/GeoJSONSource.js +53 -46
- package/Source/sources/ISource.js +39 -39
- package/Source/sources/VectorSource.js +94 -52
- package/Source/sources/granularitySettings.js +23 -20
- package/Source/sources/index.js +6 -11
- package/Source/sources/registerSource.js +17 -19
- package/Source/style/StyleLayer.js +43 -43
- package/Source/style/StyleLayerProperties.js +44 -43
- package/Source/style/index.js +2 -2
- package/Source/symbol/SymbolPlacements.js +117 -88
- package/Source/workers/VectorTileWorker.js +41 -0
- package/Source/workers/ellipsoid.js +47 -0
- package/Source/workers/processTileTask.js +329 -0
- package/Source/workers/styleEvaluator.js +168 -0
- package/benchmark.html +148 -0
- package/dist/cvt-gl-worker.js +9274 -0
- package/dist/cvt-gl-worker.js.map +1 -0
- package/dist/cvt-gl.js +2570 -2001
- package/dist/cvt-gl.js.map +1 -1
- package/dist/cvt-gl.min.js +3 -3
- package/dist/cvt-gl.min.js.map +1 -1
- package/eslint.config.mjs +58 -0
- package/index.js +9 -6
- package/mlt.html +26 -25
- package/package.json +64 -41
- package/prettier.config.mjs +30 -0
- package/vite.config.mjs +43 -0
- package/vite.worker.config.mjs +31 -0
- package/worker.html +26 -0
package/README.md
CHANGED
|
@@ -1,167 +1,202 @@
|
|
|
1
|
-
### 🌍 **cesium-vectortile-gl**
|
|
2
|
-
|
|
3
|
-
**cesium-vectortile-gl** 是专为 **CesiumJS** 设计的开源矢量瓦片渲染库。原生 Primitive 实现,**不依赖 ImageryProvider 和 第三方矢量瓦片渲染器**,支持 MLT/MVT/PBF 与 GeoJSON,兼容 MapLibre 样式规范,可渲染线/面/文字,支持虚线、贴地、合批优化与 GPU 剔除。
|
|
4
|
-
|
|
5
|
-
#### ✨ 核心特性
|
|
6
|
-
|
|
7
|
-
- ✅ **原生 Cesium 渲染**:使用 `Primitive`、`PolylineGeometry` 和自定义 `Appearance`,深度集成 Cesium 渲染管线
|
|
8
|
-
- ✅ **多源支持**:加载 **MLT/MVT (PBF)** 矢量瓦片 或 **GeoJSON** 数据(通过 `geojson-vt` 动态切片)
|
|
9
|
-
- ✅ **MapLibre 样式兼容**:完整解析 `maplibre-gl-style-spec` 的样式表达式(颜色、透明度、线型、文本等)
|
|
10
|
-
- ✅ **丰富图层类型**:支持 `background` / `fill` / `line` / `symbol` 四大基础图层,含**虚线**(`line-dasharray`)、本地字体、文字大小、显隐控制
|
|
11
|
-
- ✅ **三维场景融合**:可与 `Entity`、`Model`、贴地线/面等 Cesium 对象共存,实现真三维 GIS 可视化
|
|
12
|
-
- ✅ **高性能渲染**:按图层合批构建几何体,通过 `DrawCommand` 的 `offset/count` 复用 buffer,显著提升帧率
|
|
13
|
-
- ✅ **GPU 精准剔除**:利用 FBO + RTT 技术生成瓦片 ID 纹理,在 GPU 中高效剔除不可见矢量片段
|
|
14
|
-
- ✅ **高度可扩展**:提供清晰接口,轻松接入新数据源(如 TopoJSON)或自定义图层类型(如热力图、流线)
|
|
15
|
-
- ✅ **文字符号自动避让**:使用 `maplibre-gl` `GridIndex`类,结合 Cesium.Label 屏幕空间位置和包围盒计算API,实现符号碰撞检测(自动避让)
|
|
16
|
-
|
|
17
|
-
#### 🧠 技术栈亮点
|
|
18
|
-
|
|
19
|
-
- 解析:`@mapbox/vector-tile` + `maplibre-gl` + `@maplibre/vt-pbf`
|
|
20
|
-
- 切片:`geojson-vt`
|
|
21
|
-
- 样式:`@maplibre/maplibre-gl-style-spec`
|
|
22
|
-
- 投影与细分:复用 `maplibre-gl` 的线/面投影逻辑
|
|
23
|
-
- 文字渲染:`Cesium.LabelCollection`
|
|
24
|
-
- 渲染优化:自定义 `Appearance` + 合批 `DrawCommand` + GPU 剔除
|
|
25
|
-
|
|
26
|
-
---
|
|
27
|
-
|
|
28
|
-
> 📦 **Apache-2.0 许可证 · 欢迎贡献 · 由 [mesh-3d](https://github.com/mesh-3d) 社区维护** · **QQ交流群 1064447844**
|
|
29
|
-
|
|
30
|
-
---
|
|
31
|
-
|
|
32
|
-
#### 📜 许可证变更:MIT → Apache License 2.0
|
|
33
|
-
|
|
34
|
-
从 **0.3.0 版本**起,本项目已将许可证由 **MIT 许可证** 更改为 **Apache License 2.0**。
|
|
35
|
-
|
|
36
|
-
此次变更旨在:
|
|
37
|
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
1
|
+
### 🌍 **cesium-vectortile-gl**
|
|
2
|
+
|
|
3
|
+
**cesium-vectortile-gl** 是专为 **CesiumJS** 设计的开源矢量瓦片渲染库。原生 Primitive 实现,**不依赖 ImageryProvider 和 第三方矢量瓦片渲染器**,支持 MLT/MVT/PBF 与 GeoJSON,兼容 MapLibre 样式规范,可渲染线/面/文字,支持虚线、贴地、合批优化与 GPU 剔除。
|
|
4
|
+
|
|
5
|
+
#### ✨ 核心特性
|
|
6
|
+
|
|
7
|
+
- ✅ **原生 Cesium 渲染**:使用 `Primitive`、`PolylineGeometry` 和自定义 `Appearance`,深度集成 Cesium 渲染管线
|
|
8
|
+
- ✅ **多源支持**:加载 **MLT/MVT (PBF)** 矢量瓦片 或 **GeoJSON** 数据(通过 `geojson-vt` 动态切片)
|
|
9
|
+
- ✅ **MapLibre 样式兼容**:完整解析 `maplibre-gl-style-spec` 的样式表达式(颜色、透明度、线型、文本等)
|
|
10
|
+
- ✅ **丰富图层类型**:支持 `background` / `fill` / `line` / `symbol` 四大基础图层,含**虚线**(`line-dasharray`)、本地字体、文字大小、显隐控制
|
|
11
|
+
- ✅ **三维场景融合**:可与 `Entity`、`Model`、贴地线/面等 Cesium 对象共存,实现真三维 GIS 可视化
|
|
12
|
+
- ✅ **高性能渲染**:按图层合批构建几何体,通过 `DrawCommand` 的 `offset/count` 复用 buffer,显著提升帧率
|
|
13
|
+
- ✅ **GPU 精准剔除**:利用 FBO + RTT 技术生成瓦片 ID 纹理,在 GPU 中高效剔除不可见矢量片段
|
|
14
|
+
- ✅ **高度可扩展**:提供清晰接口,轻松接入新数据源(如 TopoJSON)或自定义图层类型(如热力图、流线)
|
|
15
|
+
- ✅ **文字符号自动避让**:使用 `maplibre-gl` `GridIndex`类,结合 Cesium.Label 屏幕空间位置和包围盒计算API,实现符号碰撞检测(自动避让)
|
|
16
|
+
|
|
17
|
+
#### 🧠 技术栈亮点
|
|
18
|
+
|
|
19
|
+
- 解析:`@mapbox/vector-tile` + `maplibre-gl` + `@maplibre/vt-pbf`
|
|
20
|
+
- 切片:`geojson-vt`
|
|
21
|
+
- 样式:`@maplibre/maplibre-gl-style-spec`
|
|
22
|
+
- 投影与细分:复用 `maplibre-gl` 的线/面投影逻辑
|
|
23
|
+
- 文字渲染:`Cesium.LabelCollection`
|
|
24
|
+
- 渲染优化:自定义 `Appearance` + 合批 `DrawCommand` + GPU 剔除
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
> 📦 **Apache-2.0 许可证 · 欢迎贡献 · 由 [mesh-3d](https://github.com/mesh-3d) 社区维护** · **QQ交流群 1064447844**
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
#### 📜 许可证变更:MIT → Apache License 2.0
|
|
33
|
+
|
|
34
|
+
从 **0.3.0 版本**起,本项目已将许可证由 **MIT 许可证** 更改为 **Apache License 2.0**。
|
|
35
|
+
|
|
36
|
+
此次变更旨在:
|
|
37
|
+
|
|
38
|
+
- 为贡献者和用户提供明确的**专利授权与保护**,
|
|
39
|
+
- 对**商业使用和再分发**提供更清晰的法律条款,
|
|
40
|
+
- 与相关地理空间项目(如 CesiumJS)的许可策略保持一致。
|
|
41
|
+
|
|
42
|
+
Apache 2.0 仍是一个**宽松且对商业友好的许可证**——您依然可以自由地使用、修改和分发本软件(包括用于专有产品),只需保留原始版权声明和免责声明即可。
|
|
43
|
+
|
|
44
|
+
本次变更后的新贡献均遵循 Apache 2.0 条款。此前发布的版本(MIT 许可)仍按其原始许可证条款继续有效。
|
|
45
|
+
|
|
46
|
+
详情请参见 [LICENSE](./LICENSE.md) 文件。
|
|
47
|
+
|
|
48
|
+
## 构建
|
|
49
|
+
|
|
50
|
+
安装 vite 等开发依赖项
|
|
51
|
+
|
|
52
|
+
```shell
|
|
53
|
+
npm install --save-dev
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
然后可以运行构建命令
|
|
57
|
+
|
|
58
|
+
```shell
|
|
59
|
+
npm run build
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
源码调试
|
|
63
|
+
|
|
64
|
+
```shell
|
|
65
|
+
npm run dev
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 安装
|
|
69
|
+
|
|
70
|
+
```shell
|
|
71
|
+
npm install @mesh3d/cesium-vectortile-gl
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 使用
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
import { VectorTileset } from '@mesh3d/cesium-vectortile-gl'
|
|
78
|
+
|
|
79
|
+
const tileset = new VectorTileset({
|
|
80
|
+
style: '/assets/demotiles/style.json'
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
viewer.scene.primitives.add(tileset)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 可选:Web Worker 多线程瓦片处理
|
|
87
|
+
|
|
88
|
+
将瓦片解析、坐标转换、几何构建放到子线程,减轻主线程卡顿。不传 `workerUrl` 时仍走主线程。
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import {
|
|
92
|
+
VectorTileset,
|
|
93
|
+
DEFAULT_WORKER_FILENAME
|
|
94
|
+
} from '@mesh3d/cesium-vectortile-gl'
|
|
95
|
+
|
|
96
|
+
// 与 cvt-gl.js 同目录的 Worker 脚本 URL
|
|
97
|
+
const workerUrl = new URL(
|
|
98
|
+
`./node_modules/@mesh3d/cesium-vectortile-gl/dist/${DEFAULT_WORKER_FILENAME}`,
|
|
99
|
+
import.meta.url
|
|
100
|
+
).href
|
|
101
|
+
|
|
102
|
+
const tileset = new VectorTileset({
|
|
103
|
+
style: '/assets/demotiles/style.json',
|
|
104
|
+
workerUrl,
|
|
105
|
+
maximumActiveTasks: 4
|
|
106
|
+
})
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
- 仅对 **vector** 源生效;若瓦片含 GeoJSON 源则自动回退到主线程。
|
|
110
|
+
- `maximumActiveTasks` 与 `maxLoading`/`maxInitializing` 配合,控制并发数。
|
|
111
|
+
- 本地示例:运行 `npm run dev` 后打开 [worker.html](worker.html) 可查看启用 Web Worker 的示例。
|
|
112
|
+
- **性能对比**:在浏览器中打开 [benchmark.html](benchmark.html),点击「运行性能对比」可测量主线程 vs Worker 的帧时间、长帧数等(无需 Vitest,需真实浏览器与 WebGL)。
|
|
113
|
+
|
|
114
|
+
**注意**:请确保通过`window.Cesium`能够访问到可用的 Cesium 包,例如:
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import * as Cesium from 'cesium'
|
|
118
|
+
window.Cesium = Cesium
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
或者在 html 中通过`script`标签引入 Cesium.js,例如
|
|
122
|
+
|
|
123
|
+
```html
|
|
124
|
+
<script src="libs/cesium/Build/CesiumUnminified/Cesium.js"></script>
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## 性能测试(Worker vs 主线程)
|
|
128
|
+
|
|
129
|
+
1. 运行 `npm run dev`,在浏览器打开 [benchmark.html](benchmark.html)。
|
|
130
|
+
2. 点击「运行性能对比(约 30 秒)」:先采集约 12 秒主线程模式帧时间,再采集约 12 秒 Worker 模式帧时间。
|
|
131
|
+
3. 页面与控制台会输出:帧数、平均/最大帧时间、P95/P99、长帧(≥50ms) 数量及两者对比。
|
|
132
|
+
|
|
133
|
+
## 扩展
|
|
134
|
+
|
|
135
|
+
可以通过实现统一的接口,对图层类型和数据源类型两大模块的进行自定义扩展,以支持更多 Maplibre 规范的数据源类型和图层样式。
|
|
136
|
+
|
|
137
|
+
### 扩展图层类型
|
|
138
|
+
|
|
139
|
+
扩展支持新的图层类型,有两种方式:
|
|
140
|
+
|
|
141
|
+
- **简单扩展** 只扩展**渲染图层类**,通过继承和重写关键方法的方式实现**IRenderLayer**接口,每个图层对应一个渲染图元(Primitive),这种方式适合确定图层实例极少的情况,是否复用缓冲区对性能的影响不大,可以参考 background 图层渲染类**BackgroundRenderLayer**实现;
|
|
142
|
+
|
|
143
|
+
- **高级扩展** 扩展**渲染图层类**和**图层渲染器**,实现**IRenderLayer**和**ILayerVisualizer**接口。**图层渲染器**负责瓦片内指定类型图层的合批几何体、批次表、绘图命令(DrawCommand)的构建,以及图层 DrawCommand 浅拷贝副本(shallow clone)的分配;**渲染图层类**只负责更新图层状态(例如同步图层样式)。如果图层实例数量不确定或者很大,每个图层实例独占一个顶点缓冲区和索引缓冲区,性能损耗将很大,**应该**采用这种方式进行扩展,确保**流畅渲染**。可以参考**FillRenderLayer**和**FillLayerVisualizer**的实现。
|
|
144
|
+
|
|
145
|
+
编写扩展类后,通过如下方式注册:
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
import { registerRenderLayer } from '@mesh3d/cesium-vectortile-gl'
|
|
149
|
+
//简单扩展
|
|
150
|
+
registerRenderLayer('layerType', XXXRenderLayer)
|
|
151
|
+
|
|
152
|
+
//高级扩展
|
|
153
|
+
registerRenderLayer('fill', FillRenderLayer, FillLayerVisualizer)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
- `第一个参数`为图层类型名称,如 circle,**必选**
|
|
157
|
+
- `第二个参数`为图层渲染类,**必选**
|
|
158
|
+
- `第三个参数`为图层渲染器类,**可选**,仅注册高级扩展时需要传递
|
|
159
|
+
|
|
160
|
+
### 扩展数据源类型
|
|
161
|
+
|
|
162
|
+
数据类型的扩展采用面向接口,只需要按`ISource`的约定编写必须实现的方法(init、requestTile)即可。
|
|
163
|
+
|
|
164
|
+
- **constructor(styleSource, path = '')** 构造函数,第一个参数接收数据源配置,第二个参数接收样式路径(如果 style 传入 url 的话),可以用于支持相对路径
|
|
165
|
+
- **init** 初始化数据源
|
|
166
|
+
- **requestTile** 请求瓦片,异步返回 @mapbox/vector-tile 的 VectorTile 类型或者与该类具有一致接口的类型
|
|
167
|
+
- **destroy** 销毁实例,释放内部创建的资源
|
|
168
|
+
|
|
169
|
+
编写扩展类后,通过如下方式注册:
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
import { registerSource } from '@mesh3d/cesium-vectortile-gl'
|
|
173
|
+
|
|
174
|
+
registerSource('sourceType', XXXSource)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
- `第一个参数`为数据源类型名称,如 raster,**必选**
|
|
178
|
+
- `第二个参数`为数据源类,**必选**
|
|
179
|
+
|
|
180
|
+
## 依赖
|
|
181
|
+
|
|
182
|
+
本项目依赖:
|
|
183
|
+
|
|
184
|
+
- [@mapbox/vector-tile](https://github.com/mapbox/vector-tile-js)(BSD-3-Clause)
|
|
185
|
+
- [@maplibre/maplibre-gl-style-spec](https://github.com/maplibre/maplibre-style-spec)(ISC)
|
|
186
|
+
- [@maplibre/vt-pbf](https://github.com/maplibre/vt-pbf)(MIT)
|
|
187
|
+
- [geojson-vt](https://github.com/mapbox/geojson-vt)(ISC)
|
|
188
|
+
- [maplibre-gl](https://github.com/maplibre/maplibre-gl-js)(BSD-3-Clause)
|
|
189
|
+
- [pbf](https://github.com/mapbox/pbf)(BSD-3-Clause)
|
|
190
|
+
|
|
191
|
+
## 相关项目
|
|
192
|
+
|
|
193
|
+
- [CesiumVectorTile](https://github.com/MikesWei/CesiumVectorTile) - 基于 ImageryProvider 的轻量版
|
|
194
|
+
- [Mesh-3D](http://mesh-3d.com) - 企业级 Web3D 引擎(提供商业支持和高级功能)
|
|
195
|
+
|
|
196
|
+
更多技术文章及案例,敬请关注微信公众号【**Mesh-3D**】
|
|
197
|
+

|
|
198
|
+
|
|
199
|
+
## 效果图
|
|
200
|
+
|
|
201
|
+

|
|
202
|
+

|