原文链接及内容

实现效果如下视频所示:

示例代码如下:

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
<!-- 官方示例:https://sandcastle.cesium.com/?src=3D%20Models%20Coloring.html&label=All -->
<template>
<CesiumMap />
<!-- 参数调整面板 -->
<div class="panel">
<el-form :model="form" label-suffix=":" label-width="auto">
<el-divider content-position="left">模型</el-divider>
<el-form-item label="模型">
<el-select
v-model="form.model"
placeholder="请选择模型"
@change="handleChangeModel">
<el-option
v-for="item in models"
:key="item.value"
:label="item.label"
:value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="启用阴影">
<el-switch
v-model="form.shadows"
inline-prompt
active-text="开"
inactive-text="关"
@change="handleShadowsChange" />
</el-form-item>
<el-divider content-position="left">模型颜色</el-divider>
<el-form-item label="混合模式">
<el-select
v-model="form.colorBlendMode"
placeholder="请选择模式"
@change="handleColorBlendModeChange">
<el-option
v-for="item in colorBlendModes"
:key="item.value"
:label="item.label"
:value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="混合颜色">
<el-select
v-model="form.color"
placeholder="请选择颜色"
@change="handleColorChange">
<el-option
v-for="item in colors"
:key="item.value"
:label="item.label"
:value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="透明度">
<el-slider
v-model="form.alpha"
show-input
:max="1"
:step="0.01"
:show-input-controls="false"
@change="handleAlphaChange" />
</el-form-item>
<el-form-item label="混合比例">
<el-slider
v-model="form.colorBlendAmount"
:disabled="form.colorBlendMode !== 'Mix'"
show-input
:max="1"
:step="0.01"
:show-input-controls="false"
@change="handleColorBlendAmountChange" />
</el-form-item>
<el-divider content-position="left">模型轮廓</el-divider>
<el-form-item label="轮廓颜色">
<el-select
v-model="form.silhouetteColor"
placeholder="请选择颜色"
@change="handleSilhouetteColorChange">
<el-option
v-for="item in silhouetteColors"
:key="item.value"
:label="item.label"
:value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="轮廓透明度">
<el-slider
v-model="form.silhouetteAlpha"
show-input
:max="1"
:step="0.01"
:show-input-controls="false"
@change="handleSilhouetteAlphaChange" />
</el-form-item>
<el-form-item label="轮廓尺寸">
<el-slider
v-model="form.silhouetteSize"
show-input
:max="10"
:step="0.01"
:show-input-controls="false"
@change="handleSilhouetteSizeChange" />
</el-form-item>
</el-form>
</div>
</template>

<script setup>
import useCSViewerStore from "@/stores/csViewer.js";

//#region --------------------- 定义变量----------------
const cvs = useCSViewerStore();
let viewer, entity;

const form = reactive({
model: "Ground Vehicle",
shadows: true,
/**
* colorBlendMode:默认值为HIGHLIGHT,颜色混合模式,定义目标颜色和基元(primitive)的源颜色之间的混合模式。
* - HIGHLIGHT:其值为0,将源颜色乘以目标颜色
* - REPLACE:其值为1,将源颜色替换为目标颜色
* - MIX:其值为2,将源颜色和目标颜色混合在一起
*/
colorBlendMode: "Highlight",
color: "Red",
alpha: 1.0,
/**
* colorBlendAmount默认值为0.5
* 当上述colorBlendMode 为 MIX 时,用于确定颜色强度的值。
* - 值为 0.0 时,将产生模型的渲染颜色;
* - 而值为 1.0 时,将产生纯色;
* - 介于两者之间的任何值都会导致两者混合。
*/
colorBlendAmount: 0.5,
silhouetteColor: "Red",
silhouetteAlpha: 1.0,
silhouetteSize: 2.0, //轮廓的大小,单位为像素
});

/**
* @type {Array} 要加载的模型列表
*/
const models = [
{ value: "Ground Vehicle", label: "地面车辆" },
{ value: "Aircraft", label: "飞机" },
{ value: "Hot Air Balloon", label: "热气球" },
{ value: "Milk Truck", label: "运奶卡车" },
{ value: "Skinned Character", label: "带有皮肤的角色" },
];

/**
* @type {Array} 颜色混合模式
*/
const colorBlendModes = [
{ value: "Highlight", label: "高亮" },
{ value: "Replace", label: "替换" },
{ value: "Mix", label: "混合" },
];

/**
* @type {Array} 模型的颜色值列表
*/
const colors = [
{ value: "White", label: "白色" },
{ value: "Red", label: "红色" },
{ value: "Green", label: "绿色" },
{ value: "Blue", label: "蓝色" },
{ value: "Yellow", label: "黄色" },
{ value: "Gray", label: "灰色" },
];

/**
* @type {Array} 轮廓的颜色值列表
*/
const silhouetteColors = colors.slice(1);
//#endregion

onMounted(() => {
viewer = cvs.viewer;
init();
});

//#region --------------------- 方法区域----------------
function init() {
handleChangeModel(form.model);
handleShadowsChange(form.shadows);
}

function handleChangeModel(val) {
switch (val) {
case "Aircraft":
createModel("./SampleData/models/CesiumAir/Cesium_Air.glb", 5000.0);
break;
case "Ground Vehicle":
createModel("./SampleData/models/GroundVehicle/GroundVehicle.glb", 0);
break;
case "Hot Air Balloon":
createModel(
"./SampleData/models/CesiumBalloon/CesiumBalloon.glb",
1000.0
);
break;
case "Milk Truck":
createModel("./SampleData/models/CesiumMilkTruck/CesiumMilkTruck.glb", 0);
break;
case "Skinned Character":
createModel("./SampleData/models/CesiumMan/Cesium_Man.glb", 0);
break;
default:
break;
}
}

function createModel(url, height) {
viewer.entities.removeAll();

const position = Cesium.Cartesian3.fromDegrees(
-77.0744619,
44.0503706,
height
);
const heading = Cesium.Math.toRadians(135);
const pitch = 0;
const roll = 0;
const hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
const orientation = Cesium.Transforms.headingPitchRollQuaternion(
position,
hpr
);

entity = viewer.entities.add({
name: url,
position: position,
orientation: orientation,
model: {
uri: url,
minimumPixelSize: 128,
maximumScale: 20000,
color: getColor(form.color, form.alpha),
colorBlendMode: getColorBlendMode(form.colorBlendMode),
colorBlendAmount: parseFloat(form.colorBlendAmount),
silhouetteColor: getColor(form.silhouetteColor, form.silhouetteAlpha),
silhouetteSize: parseFloat(form.silhouetteSize),
},
});
viewer.trackedEntity = entity;
}

function handleShadowsChange(checked) {
viewer.shadows = checked;
}

function handleColorBlendModeChange(val) {
const colorBlendMode = getColorBlendMode(val);
entity.model.colorBlendMode = colorBlendMode;
}

function handleColorChange(val) {
entity.model.color = getColor(val, form.alpha);
}

function handleAlphaChange(val) {
entity.model.color = getColor(form.color, val);
}

function handleColorBlendAmountChange(val) {
entity.model.colorBlendAmount = parseFloat(val);
}

function handleSilhouetteColorChange(val) {
entity.model.silhouetteColor = getColor(val, form.silhouetteAlpha);
}

function handleSilhouetteAlphaChange(val) {
entity.model.silhouetteColor = getColor(form.silhouetteColor, val);
}

function handleSilhouetteSizeChange(val) {
entity.model.silhouetteSize = parseFloat(val);
}
//-------------------通用方法--------------------

/**
* 通过颜色名称和透明度获取Cesium颜色值
* @param colorName 颜色名称
* @param alpha 透明度
*/
function getColor(colorName, alpha) {
const color = Cesium.Color[colorName.toUpperCase()];
return Cesium.Color.fromAlpha(color, parseFloat(alpha));
}

/**
* 获取颜色混合模式
* @param colorBlendMode 颜色混合模式,定义目标颜色和基元(primitive)的源颜色之间的混合模式。
*/
function getColorBlendMode(colorBlendMode) {
return Cesium.ColorBlendMode[colorBlendMode.toUpperCase()];
}
//#endregion
</script>

<style lang="scss" scoped>
.panel {
position: absolute;
width: 360px;
top: 10px;
right: 10px;
background-color: white;
border-radius: 4px;
z-index: 2;
padding: 10px;
opacity: 0.96;
box-shadow: rgba(195, 191, 188, 0.7) 0px 1px 2px 0px,
rgba(195, 191, 188, 0.85) 0px 2px 4px 2px;
}

//自定义分割线上边距
:deep(.el-divider--horizontal) {
margin-top: 15px;
}

//分割线上的文字样式
:deep(.el-divider__text) {
font-size: 15px;
font-weight: bold;
color: #409eff;
}

//滑块右侧的输入框的长度
:deep(.el-input-number) {
width: 75px;
}

//滑块右侧输入框
:deep(.el-slider__runway.show-input) {
margin-right: 13px;
margin-left: 5px;
}
</style>