原文链接及内容

效果如下视频所示:

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<style>
@import url(../templates/bucket.css);
#toolbar {
background: rgba(42, 42, 42, 0.8);
padding: 4px;
border-radius: 4px;
}
#toolbar input {
vertical-align: middle;
padding-top: 2px;
padding-bottom: 2px;
}
#toolbar .header {
font-weight: bold;
}
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>加载中...</h1></div>
<div id="toolbar">
<table>
<tbody>
<tr>
<td class="header">分类类型</td>
</tr>
<tr>
<td>地面</td>
<td><input type="checkbox" data-bind="checked: ground" /></td>
</tr>
<tr>
<td>低矮植被</td>
<td>
<input type="checkbox" data-bind="checked: low_vegetation" />
</td>
</tr>
<tr>
<td>中等(高度)植被</td>
<td>
<input type="checkbox" data-bind="checked: medium_vegetation" />
</td>
</tr>
<tr>
<td>高大植被</td>
<td>
<input type="checkbox" data-bind="checked: high_vegetation" />
</td>
</tr>
<tr>
<td>建筑物</td>
<td><input type="checkbox" data-bind="checked: buildings" /></td>
</tr>
<tr>
<td>其他/未分类</td>
<td><input type="checkbox" data-bind="checked: other" /></td>
</tr>
</tbody>
</table>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
const viewer = new Cesium.Viewer("cesiumContainer", {
geocoder: false,
homeButton: false,
sceneModePicker: false,
navigationHelpButton: false,
navigationInstructionsInitiallyVisible: false,
animation: false,
timeline: false,
fullscreenButton: false,
skyBox: false,
shouldAnimate: true,
baseLayerPicker: false,
terrain: Cesium.Terrain.fromWorldTerrain(),
});
viewer.cesiumWidget.creditContainer.style.display = "none";

viewer.camera.flyTo({
destination: new Cesium.Cartesian3(
1223285.2286828577,
-4319476.080312792,
4562579.020145769,
),
orientation: {
direction: new Cesium.Cartesian3(
0.63053223097472,
0.47519958296727743,
-0.6136892226931869,
),
up: new Cesium.Cartesian3(
0.7699959023135587,
-0.4824455703743441,
0.41755548379407276,
),
},
duration: 0,
});

//在蒙特利尔周围添加存储视图。您可以通过捕获 camera.position、camera.direction 和 camera.up 来添加到此列表中。
Sandcastle.addToolbarMenu([
{
text: "(视角)概览",
onselect: function () {
viewer.camera.flyTo({
destination: new Cesium.Cartesian3(
1268112.9336926902,
-4347432.089579957,
4539129.813606778,
),
orientation: {
direction: new Cesium.Cartesian3(
-0.23288147105081208,
0.9376599248561527,
-0.25799241415197466,
),
up: new Cesium.Cartesian3(
-0.015748156073159988,
0.2616156268422992,
0.9650436567182887,
),
},
easingFunction: Cesium.EasingFunction.QUADRATIC_IN_OUT,
});
},
},
{
text: "高速公路",
onselect: function () {
viewer.camera.flyTo({
destination: new Cesium.Cartesian3(
1266560.143870489,
-4278126.842199712,
4542690.264566619,
),
orientation: {
direction: new Cesium.Cartesian3(
-0.3402460635871598,
-0.46669052711538217,
-0.8163532128400116,
),
up: new Cesium.Cartesian3(
0.08964012922691329,
-0.8802940231336787,
0.46588311846138497,
),
},
easingFunction: Cesium.EasingFunction.QUADRATIC_IN_OUT,
});
},
},
{
text: "奥林匹克体育场",
onselect: function () {
viewer.camera.flyTo({
destination: new Cesium.Cartesian3(
1267081.619536883,
-4290744.917138439,
4530941.041519919,
),
orientation: {
direction: new Cesium.Cartesian3(
-0.735813047510908,
0.6294547560338262,
0.24973159435503312,
),
up: new Cesium.Cartesian3(
-0.09796934684423217,
-0.4638476756625683,
0.88048131204549,
),
},
easingFunction: Cesium.EasingFunction.QUADRATIC_IN_OUT,
});
},
},
{
text: "生物圈(Biosphere)博物馆",
onselect: function () {
viewer.camera.flyTo({
destination: new Cesium.Cartesian3(
1269319.8408991008,
-4293301.826913256,
4527724.561372451,
),
orientation: {
direction: new Cesium.Cartesian3(
-0.742505030107832,
-0.3413204607149223,
-0.5763563336703441,
),
up: new Cesium.Cartesian3(
-0.04655102331027917,
-0.8320643756800384,
0.5527222421370013,
),
},
easingFunction: Cesium.EasingFunction.QUADRATIC_IN_OUT,
});
},
},
{
text: "皇家山圣约瑟夫大教堂",
onselect: function () {
viewer.camera.flyTo({
destination: new Cesium.Cartesian3(
1263148.6745904868,
-4297262.506644816,
4525958.844284831,
),
orientation: {
direction: new Cesium.Cartesian3(
0.6550952540993403,
0.7551122393690295,
0.025606913355780074,
),
up: new Cesium.Cartesian3(
0.46670450470847263,
-0.4310758971098583,
0.7722437932516845,
),
},
easingFunction: Cesium.EasingFunction.QUADRATIC_IN_OUT,
});
},
},
]);

// 设置复选框以开启/关闭不同的分类设置。
const viewModel = {
ground: true,
other: true,
buildings: true,
low_vegetation: true,
medium_vegetation: true,
high_vegetation: true,
};

// 为每种分类类型分配颜色。
const pointStyles = {
unclassified: {
color: "color('#808080')",
show: true,
},
not_awarded: {
color: "color('#FFDEAD')",
show: true,
},
ground: {
color: "color('#FFDEAD')",
show: true,
},
low_vegetation: {
color: "color('#63FF7E')",
show: true,
},
medium_vegetation: {
color: "color('#63FF7E')",
show: true,
},
high_vegetation: {
color: "color('#22B33A')",
show: true,
},
buildings: {
color: "color('#efefef')",
show: true,
},
low_point: {
color: "color('#808080')",
show: true,
},
reserved_city_diffusion: {
color: "color('#808080')",
show: true,
},
};

//分类字典
const classificationDictionary = {
not_awarded: 1,
ground: 2,
low_vegetation: 3,
medium_vegetation: 4,
high_vegetation: 5,
buildings: 6,
low_point: 7,
reserved_city_diffusion: 8,
unclassified: -1,
};

//这是一个辅助函数(helper function),用于在每次更新 UI/复选框时重新应用样式。
function applyStyle(tileset, styles) {
const styleObject = {};
const styleKeys = Object.keys(styles);

styleObject.color = {
conditions: [],
};
styleObject.show = {
conditions: [],
};

let finalCondition;

for (let i = 0; i < styleKeys.length; ++i) {
const key = styleKeys[i];
const id = classificationDictionary[key];

const colorCondition = [`\${Classification} === ${id}`, styles[key].color];
const showCondition = [`\${Classification} === ${id}`, styles[key].show];

if (id === -1) {
colorCondition[0] = true;
showCondition[0] = true;

finalCondition = {
colorCondition: colorCondition,
showCondition: showCondition,
};
} else {
styleObject.color.conditions.push(colorCondition);
styleObject.show.conditions.push(showCondition);
}
}

if (Cesium.defined(finalCondition)) {
styleObject.color.conditions.push(finalCondition.colorCondition);
styleObject.show.conditions.push(finalCondition.showCondition);
}

tileset.style = new Cesium.Cesium3DTileStyle(styleObject);
}

let tileset;
try {
/**
* 加拿大蒙特利尔市约 100 亿个点的 3D Tileset 数据集,2015 年采集,
* 分辨率达 20 厘米。由 Cesium ion 进行切片处理并托管。
*/
tileset = await Cesium.Cesium3DTileset.fromIonAssetId(28945, {
pointCloudShading: {
attenuation: true,
maximumAttenuation: 2,
},
});
viewer.scene.primitives.add(tileset);

// 应用一个初始样式
applyStyle(tileset, pointStyles);
} catch (error) {
console.log(`加载瓦片集出错: ${error}`);
}

Cesium.knockout.track(viewModel);

const toolbar = document.getElementById("toolbar");
Cesium.knockout.applyBindings(viewModel, toolbar);

// ---------以下代码均为复选框change事件
Cesium.knockout.getObservable(viewModel, "ground").subscribe(function (show) {
pointStyles.ground.show = show;
pointStyles.not_awarded.show = show;

applyStyle(tileset, pointStyles);
});

Cesium.knockout
.getObservable(viewModel, "low_vegetation")
.subscribe(function (show) {
pointStyles.low_vegetation.show = show;

applyStyle(tileset, pointStyles);
});

Cesium.knockout
.getObservable(viewModel, "medium_vegetation")
.subscribe(function (show) {
pointStyles.medium_vegetation.show = show;

applyStyle(tileset, pointStyles);
});

Cesium.knockout
.getObservable(viewModel, "high_vegetation")
.subscribe(function (show) {
pointStyles.high_vegetation.show = show;

applyStyle(tileset, pointStyles);
});

Cesium.knockout.getObservable(viewModel, "buildings").subscribe(function (show) {
pointStyles.buildings.show = show;

applyStyle(tileset, pointStyles);
});

Cesium.knockout.getObservable(viewModel, "other").subscribe(function (show) {
pointStyles.low_point.show = show;
pointStyles.reserved_city_diffusion.show = show;
pointStyles.unclassified.show = show;

applyStyle(tileset, pointStyles);
});