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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
| const viewer = new Cesium.Viewer("cesiumContainer", { geocoder: false, sceneModePicker: false, homeButton: false, navigationHelpButton: false, baseLayerPicker: false, navigationInstructionsInitiallyVisible: false, fullscreenButton: false, selectionIndicator: false, skyBox: false, timeline: false, animation: false, shouldAnimate: true,
orderIndependentTranslucency: false, });
viewer.cesiumWidget.creditContainer.style.display = "none";
viewer.clock.currentTime = Cesium.JulianDate.fromIso8601( "2021-11-09T20:27:37.016064475348684937Z", );
const position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 0); const hpr = new Cesium.HeadingPitchRoll(0, 0, 0); const fixedFrameTransform = Cesium.Transforms.localFrameToFixedFrameGenerator( "north", "west", );
const expandModelShader = new Cesium.CustomShader({ uniforms: {
u_drag: { type: Cesium.UniformType.VEC2, value: new Cesium.Cartesian2(0.0, 0.0), }, }, vertexShaderText: ` // 如果鼠标向右拖动,模型就会增大;如果鼠标向左拖动,模型就会缩小 void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput) { vsOutput.positionMC += 0.01 * u_drag.x * vsInput.attributes.normalMC; } `, });
const textureUniformShader = new Cesium.CustomShader({ uniforms: { u_time: { type: Cesium.UniformType.FLOAT, value: 0, }, u_stripes: { type: Cesium.UniformType.SAMPLER_2D, value: new Cesium.TextureUniform({ url: "../SampleData/cesium_stripes.png", }), }, }, fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { vec2 texCoord = fsInput.attributes.texCoord_0 + 0.1 * vec2(u_time, 0.0); material.diffuse = texture(u_stripes, texCoord).rgb; } `, });
function makeCheckerboardTexture(textureSize) { const checkerboard = new Uint8Array(4 * textureSize * textureSize);
const maxDiagonal = 2 * (textureSize - 1); for (let i = 0; i < textureSize; i++) { for (let j = 0; j < textureSize; j++) { const index = i * textureSize + j; const diagonal = i + j; if (diagonal % 2 === 0) { checkerboard[4 * index] = 255; }
checkerboard[4 * index + 3] = (255 * diagonal) / maxDiagonal; } } return new Cesium.TextureUniform({ typedArray: checkerboard, width: textureSize, height: textureSize, minificationFilter: Cesium.TextureMinificationFilter.NEAREST, magnificationFilter: Cesium.TextureMagnificationFilter.NEAREST, }); } const checkerboardTexture = makeCheckerboardTexture(8);
const checkerboardMaskShader = new Cesium.CustomShader({ uniforms: { u_checkerboard: { type: Cesium.UniformType.SAMPLER_2D, value: checkerboardTexture, }, }, fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { vec2 texCoord = fsInput.attributes.texCoord_0; vec4 checkerboard = texture(u_checkerboard, texCoord); material.diffuse = mix(material.diffuse, vec3(0.0), checkerboard.r); } `, });
const checkerboardAlphaShader = new Cesium.CustomShader({ uniforms: { u_checkerboard: { type: Cesium.UniformType.SAMPLER_2D, value: checkerboardTexture, }, }, translucencyMode: Cesium.CustomShaderTranslucencyMode.TRANSLUCENT, fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { vec2 texCoord = fsInput.attributes.texCoord_0; vec4 checkerboard = texture(u_checkerboard, texCoord); material.diffuse = checkerboard.rgb; material.alpha = checkerboard.a; } `, });
const checkerboardHolesShader = new Cesium.CustomShader({ uniforms: { u_checkerboard: { type: Cesium.UniformType.SAMPLER_2D, value: checkerboardTexture, }, }, fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { vec2 texCoord = fsInput.attributes.texCoord_0; vec4 checkerboard = texture(u_checkerboard, texCoord); if (checkerboard.r > 0.0) { discard; } } `, });
function makeGradientTexture() { const size = 256; const typedArray = new Uint8Array(3 * size * size); for (let i = 0; i < size; i++) { for (let j = 0; j < size; j++) { const index = i * size + j; typedArray[3 * index + 0] = j; typedArray[3 * index + 1] = i; } }
return new Cesium.TextureUniform({ typedArray: typedArray, width: size, height: size, pixelFormat: Cesium.PixelFormat.RGB, }); } const gradientTexture = makeGradientTexture();
const gradientShader = new Cesium.CustomShader({ uniforms: { u_gradient: { type: Cesium.UniformType.SAMPLER_2D, value: gradientTexture, }, }, fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { material.diffuse = texture(u_gradient, fsInput.attributes.texCoord_0).rgb; } `, });
const modifyPbrShader = new Cesium.CustomShader({ uniforms: { u_drag: { type: Cesium.UniformType.VEC2, value: new Cesium.Cartesian2(0.0, 0.0), }, }, fragmentShaderText: ` // 单击并拖动以改变 PBR 值 void fragmentMain(FragmentInput vsInput, inout czm_modelMaterial material) { float dragDistance = length(u_drag); float variation = smoothstep(0.0, 300.0, dragDistance); // variation添加了金色的色调到高光 material.specular = mix(material.specular, vec3(0.8, 0.5, 0.1), variation); // variation使材料更具光泽 material.roughness = clamp(1.0 - variation, 0.01, 1.0); // variation将一些红色混合到漫反射颜色中 material.diffuse += vec3(0.5, 0.0, 0.0) * variation; } `, });
const pointCloudWaveShader = new Cesium.CustomShader({ uniforms: { u_time: { type: Cesium.UniformType.FLOAT, value: 0, }, }, vertexShaderText: ` void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput) { // 该模型的 x 和 y 坐标范围在[0, 1]之间,这恰好也可用作 UV 坐标。 vec2 uv = vsInput.attributes.positionMC.xy; // 使点云以复杂的波浪形式波动,使其在空间和时间上都发生变化。振幅基于点云的原始形状(其本身就是一个波浪形表面)。波浪是相对于模型中心计算的,因此变换范围从 [0, 1] -> [-1, 1] -> [0, 1] float amplitude = 2.0 * vsInput.attributes.positionMC.z - 1.0; float wave = amplitude * sin(2.0 * czm_pi * uv.x - 2.0 * u_time) * sin(u_time); vsOutput.positionMC.z = 0.5 + 0.5 * wave; // 通过改变点的大小使其产生脉冲(pulse)效果 vsOutput.pointSize = 10.0 + 5.0 * sin(u_time); } `, fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { // 将点设为圆形而不是方形 float distance = length(gl_PointCoord - 0.5); if (distance > 0.5) { discard; } // 创建一个正弦波色板,使其大致沿波浪方向移动,但速度不同。系数是任意选择的。 vec2 uv = fsInput.attributes.positionMC.xy; material.diffuse = 0.2 * fsInput.attributes.color_0.rgb; material.diffuse += vec3(0.2, 0.3, 0.4) + vec3(0.2, 0.3, 0.4) * sin(2.0 * czm_pi * vec3(3.0, 2.0, 1.0) * uv.x - 3.0 * u_time); } `, });
const models = { balloon: "../SampleData/models/CesiumBalloon/CesiumBalloon.glb", drone: "../SampleData/models/CesiumDrone/CesiumDrone.glb", pawns: "../SampleData/models/CesiumDrone/Pawns.glb", milkTruck: "../SampleData/models/CesiumMilkTruck/CesiumMilkTruck.glb", groundVehicle: "../SampleData/models/GroundVehicle/GroundVehicle.glb", pointCloudWave: "../SampleData/models/PointCloudWave/PointCloudWave.glb", };
let needsDrag = false; const demos = [ { text: "自定义纹理", onselect: function () { selectModel(models.groundVehicle, textureUniformShader); needsDrag = false; }, }, { text: "程序化纹理", onselect: function () { selectModel(models.balloon, checkerboardMaskShader); needsDrag = false; }, }, { text: "半透明材质", onselect: function () { selectModel(models.balloon, checkerboardAlphaShader); needsDrag = false; }, }, { text: "使用纹理作为遮罩(Mask)", onselect: function () { selectModel(models.balloon, checkerboardHolesShader); needsDrag = false; }, }, { text: "程序化渐变纹理", onselect: function () { selectModel(models.balloon, gradientShader); needsDrag = false; }, }, { text: "通过鼠标拖动修改 PBR 值", onselect: function () { selectModel(models.groundVehicle, modifyPbrShader); needsDrag = true; }, }, { text: "通过鼠标拖动扩展(Expand)模型", onselect: function () { selectModel(models.milkTruck, expandModelShader); needsDrag = true; }, }, { text: "动画(移动的)点云", onselect: function () { selectModel(models.pointCloudWave, pointCloudWaveShader); needsDrag = false; }, }, ];
async function selectModel(url, customShader) { viewer.scene.primitives.removeAll(); try { const model = viewer.scene.primitives.add( await Cesium.Model.fromGltfAsync({ url: url, customShader: customShader, modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame( position, hpr, Cesium.Ellipsoid.WGS84, fixedFrameTransform, ), }), );
const removeListener = model.readyEvent.addEventListener(() => { viewer.camera.flyToBoundingSphere(model.boundingSphere, { duration: 0.0, });
removeListener(); }); } catch (error) { console.log(`模型加载出错: ${error}`); } } Sandcastle.addToolbarMenu(demos);
const startTime = performance.now(); viewer.scene.postUpdate.addEventListener(function () { const elapsedTimeSeconds = (performance.now() - startTime) / 1000; textureUniformShader.setUniform("u_time", elapsedTimeSeconds); pointCloudWaveShader.setUniform("u_time", elapsedTimeSeconds); });
let dragActive = false; const dragCenter = new Cesium.Cartesian2();
viewer.screenSpaceEventHandler.setInputAction(function (movement) { if (!needsDrag) { return; }
const pickedFeature = viewer.scene.pick(movement.position); if (!Cesium.defined(pickedFeature)) { return; }
viewer.scene.screenSpaceCameraController.enableInputs = false;
dragActive = true; movement.position.clone(dragCenter); }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
const scratchDrag = new Cesium.Cartesian2(); viewer.screenSpaceEventHandler.setInputAction(function (movement) { if (!needsDrag) { return; }
if (dragActive) { const drag = Cesium.Cartesian3.subtract( movement.endPosition, dragCenter, scratchDrag, );
expandModelShader.setUniform("u_drag", drag); modifyPbrShader.setUniform("u_drag", drag); } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
viewer.screenSpaceEventHandler.setInputAction(function (movement) { if (!needsDrag) { return; }
viewer.scene.screenSpaceCameraController.enableInputs = true;
dragActive = false; }, Cesium.ScreenSpaceEventType.LEFT_UP);
|