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
|
const viewer = new Cesium.Viewer("cesiumContainer", { geocoder: false, sceneModePicker: false, homeButton: false, navigationHelpButton: false, baseLayerPicker: false, navigationInstructionsInitiallyVisible: false, animation: false, timeline: false, fullscreenButton: false, selectionIndicator: false, skyBox: false, shouldAnimate: true, });
viewer.cesiumWidget.creditContainer.style.display = "none";
viewer.scene.debugShowFramesPerSecond = true;
const scene = viewer.scene;
viewer.camera.flyTo({ duration: 0, destination: new Cesium.Cartesian3( 762079.3157173397, -28363749.882652905, 19814354.842565004 ), orientation: { direction: new Cesium.Cartesian3( -0.022007098944236157, 0.819079900508189, -0.5732571885110153 ), up: new Cesium.Cartesian3( -0.015396759850986286, 0.5730503851893346, 0.8193754913471885 ), }, easingFunction: Cesium.EasingFunction.QUADRATIC_IN_OUT, });
let tileset; try {
tileset = await Cesium.Cesium3DTileset.fromIonAssetId(1208297, { maximumScreenSpaceError: 4, }); scene.primitives.add(tileset); } catch (error) { console.log(`tileset加载出错: ${error}`); }
const style = new Cesium.Cesium3DTileStyle({ defines: { LandCoverColor: "rgb(${color}[0], ${color}[1], ${color}[2])", }, color: "${LandCoverColor} === vec4(1.0) ? rgb(254, 254, 254) : ${LandCoverColor}", });
const customShader = new Cesium.CustomShader({ uniforms: { u_time: { type: Cesium.UniformType.FLOAT, value: 0, }, }, fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { int featureId = fsInput.featureIds.featureId_0; // 使用笛卡尔坐标,但比例大致为 [-1, 1] vec3 positionWC = fsInput.attributes.positionWC / 6.3e6; if (featureId == 60) { // 就像调频(FM)合成,产生不规则间隔的波 float wave = sin(14.0 * positionWC.z - u_time); wave = 0.5 + 0.5 * sin(10.0 * wave * positionWC.z - u_time); // 混合一个过度饱和的漫反射版本以产生闪烁的色带 material.diffuse = mix(material.diffuse, material.diffuse * 3.0, wave); } } `, });
const startTime = performance.now(); const customShaderUpdate = function () { const elapsedTimeSeconds = (performance.now() - startTime) / 1000; customShader.setUniform("u_time", elapsedTimeSeconds); };
viewer.scene.postUpdate.addEventListener(function () { customShaderUpdate(); });
let enablePicking = true; const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
const metadataOverlay = document.createElement("div"); viewer.container.appendChild(metadataOverlay); metadataOverlay.className = "backdrop"; metadataOverlay.style.display = "none"; metadataOverlay.style.position = "absolute"; metadataOverlay.style.bottom = "0"; metadataOverlay.style.left = "0"; metadataOverlay.style["pointer-events"] = "none"; metadataOverlay.style.padding = "4px"; metadataOverlay.style.backgroundColor = "#303030"; metadataOverlay.style.whiteSpace = "pre-line"; metadataOverlay.style.fontSize = "16px"; metadataOverlay.style.borderRadius = "4px";
let tableHtmlScratch; let i;
handler.setInputAction(function (movement) { if (enablePicking) { const feature = scene.pick(movement.endPosition); if (feature instanceof Cesium.Cesium3DTileFeature) { metadataOverlay.style.display = "block"; metadataOverlay.style.bottom = `${ viewer.canvas.clientHeight - movement.endPosition.y }px`; metadataOverlay.style.left = `${movement.endPosition.x}px`;
tableHtmlScratch = "<table><thead><tr><th><tt>字段</tt></th><th><tt>值</tt></th></tr></thead><tbody>";
const propertyIds = feature.getPropertyIds(); const length = propertyIds.length; for (let i = 0; i < length; ++i) { const propertyId = propertyIds[i]; const propertyValue = feature.getProperty(propertyId); tableHtmlScratch += `<tr><td><tt>${propertyId}</tt></td><td><tt>${propertyValue}</tt></td></tr>`; } tableHtmlScratch += "</tbody></table>"; metadataOverlay.innerHTML = tableHtmlScratch; } else { metadataOverlay.style.display = "none"; } } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
const modes = [ { text: "全球视角", onselect: function () { tileset.customShader = undefined; tileset.debugShowBoundingVolume = false; tileset.style = undefined; }, },
{ text: "显示 S2 边界体", onselect: function () { tileset.customShader = undefined; tileset.debugShowBoundingVolume = true; tileset.style = undefined; }, }, { text: "应用自定义样式", onselect: function () { tileset.customShader = undefined; tileset.debugShowBoundingVolume = false; tileset.style = style; }, }, { text: "应用自定义Shader", onselect: function () { tileset.customShader = customShader; tileset.debugShowBoundingVolume = false; tileset.style = undefined; }, }, ];
Sandcastle.addToolbarMenu(modes); Sandcastle.addToggleButton("启用拾取", enablePicking, function (checked) { if (enablePicking) { metadataOverlay.style.display = "none"; }
enablePicking = checked; });
|