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
| <!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Cesium 近地天空盒</title> <script src="https://cdn.jsdelivr.net/npm/cesium@1.133.0/Build/Cesium/Cesium.js"></script> <link href="https://cdn.jsdelivr.net/npm/cesium@1.133.0/Build/Cesium/Widgets/widgets.css" rel="stylesheet" /> <style> html, body, #cesiumContainer { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; background: #000; font-family: "Microsoft YaHei", "PingFang SC", sans-serif; } .control-panel { position: absolute; top: 12px; left: 12px; z-index: 10; width: 280px; padding: 14px 16px; color: #e8eef8; background: rgba(13, 19, 33, 0.82); border: 1px solid rgba(255, 255, 255, 0.12); border-radius: 8px; backdrop-filter: blur(10px); } button { height: 32px; color: #f8fbff; background: rgba(45, 99, 201, 0.86); border: 1px solid rgba(255, 255, 255, 0.16); border-radius: 6px; cursor: pointer; font-size: 12px; } button:hover { background: rgba(58, 119, 232, 0.95); } </style> </head> <body> <div id="cesiumContainer"></div> <div class="control-panel"> <div class="control-title">近地天空盒 SkyBoxOnGround</div> <p class="control-desc">相机高度低于阈值时切换为自定义近地天空盒,高于阈值时恢复 Cesium 默认天空和大气。</p> <div class="status-row"> <span class="status-label">当前相机高度</span> <span id="cameraHeight" class="status-value">-- m</span> </div> <div class="status-row"> <span class="status-label">天空盒状态</span> <span id="skyboxStatus" class="status-value">--</span> </div> <div class="status-row"> <span class="status-label">切换阈值</span> <span id="heightLimit" class="status-value">120000 m</span> </div> <div class="button-row"> <button id="nearBtn" type="button">低空视角</button> <button id="highBtn" type="button">高空视角</button> </div> </div>
<script> window.CESIUM_BASE_URL = "https://cdn.jsdelivr.net/npm/cesium@1.133.0/Build/Cesium/";
const BoxGeometry = Cesium.BoxGeometry; const Cartesian3 = Cesium.Cartesian3; const defaultValue = Cesium.defaultValue; const defined = Cesium.defined; const destroyObject = Cesium.destroyObject; const DeveloperError = Cesium.DeveloperError; const GeometryPipeline = Cesium.GeometryPipeline; const Matrix3 = Cesium.Matrix3; const Matrix4 = Cesium.Matrix4; const Transforms = Cesium.Transforms; const VertexFormat = Cesium.VertexFormat; const BufferUsage = Cesium.BufferUsage; const CubeMap = Cesium.CubeMap; const DrawCommand = Cesium.DrawCommand; const loadCubeMap = Cesium.loadCubeMap; const RenderState = Cesium.RenderState; const VertexArray = Cesium.VertexArray; const BlendingState = Cesium.BlendingState; const SceneMode = Cesium.SceneMode; const ShaderProgram = Cesium.ShaderProgram; const ShaderSource = Cesium.ShaderSource; const skyboxMatrix3 = new Matrix3();
class SkyBoxOnGround { constructor(options) { if (!Cesium.defined(Cesium.Matrix4.getRotation)) { Cesium.Matrix4.getRotation = Cesium.Matrix4.getMatrix3; } this.sources = options.sources; this._sources = undefined; this.show = defaultValue(options.show, true); this._command = new DrawCommand({ modelMatrix: Matrix4.clone(Matrix4.IDENTITY), owner: this, }); this._cubeMap = undefined; this._attributeLocations = undefined; this._useHdr = undefined; }
update(frameState, useHdr) { const that = this; const SkyBoxFS = `#version 300 es precision highp float; uniform samplerCube u_cubeMap; in vec3 v_texCoord; out vec4 fragColor; void main() { vec4 color = texture(u_cubeMap, normalize(v_texCoord)); fragColor = vec4(czm_gammaCorrect(color).rgb, czm_morphTime); }`;
const SkyBoxVS = `#version 300 es precision highp float; in vec3 position; out vec3 v_texCoord; uniform mat3 u_rotateMatrix; void main() { vec3 p = czm_viewRotation * u_rotateMatrix * (czm_temeToPseudoFixed * (czm_entireFrustum.y * position)); gl_Position = czm_projection * vec4(p, 1.0); v_texCoord = position; }`;
if (!this.show) return undefined; if (frameState.mode !== SceneMode.SCENE3D && frameState.mode !== SceneMode.MORPHING) return undefined; if (!frameState.passes.render) return undefined;
const context = frameState.context;
if (this._sources !== this.sources) { this._sources = this.sources; const sources = this.sources; if (typeof sources.positiveX === "string") { loadCubeMap(context, this._sources).then(function (cubeMap) { that._cubeMap = that._cubeMap && that._cubeMap.destroy(); that._cubeMap = cubeMap; }); } }
const command = this._command; command.modelMatrix = Transforms.eastNorthUpToFixedFrame(frameState.camera._positionWC);
if (!defined(command.vertexArray)) { command.uniformMap = { u_cubeMap: function () { return that._cubeMap; }, u_rotateMatrix: function () { return Matrix4.getRotation(command.modelMatrix, skyboxMatrix3); }, }; const geometry = BoxGeometry.createGeometry( BoxGeometry.fromDimensions({ dimensions: new Cartesian3(2.0, 2.0, 2.0), vertexFormat: VertexFormat.POSITION_ONLY, }) ); const attributeLocations = (this._attributeLocations = GeometryPipeline.createAttributeLocations(geometry)); command.vertexArray = VertexArray.fromGeometry({ context: context, geometry: geometry, attributeLocations: attributeLocations, bufferUsage: BufferUsage.STATIC_DRAW || BufferUsage._DRAW, }); command.renderState = RenderState.fromCache({ blending: BlendingState.ALPHA_BLEND, }); }
if (!defined(command.shaderProgram) || this._useHdr !== useHdr) { const fs = new ShaderSource({ defines: [useHdr ? "HDR" : ""], sources: [SkyBoxFS], }); command.shaderProgram = ShaderProgram.fromCache({ context: context, vertexShaderSource: SkyBoxVS, fragmentShaderSource: fs, attributeLocations: this._attributeLocations, }); this._useHdr = useHdr; }
if (!defined(this._cubeMap)) return undefined; return command; }
isDestroyed() { return false; } destroy() { const command = this._command; command.vertexArray = command.vertexArray && command.vertexArray.destroy(); command.shaderProgram = command.shaderProgram && command.shaderProgram.destroy(); this._cubeMap = this._cubeMap && this._cubeMap.destroy(); return destroyObject(this); } }
class SkyBox { constructor(viewer) { this.viewer = viewer; this.sources = {}; }
SkyBoxOnGround(options) { const _this = this; const height = !options.height ? "5705" : options.height; const defaultSkyBox = _this.viewer.scene.skyBox; const currentSkyBox = new SkyBoxOnGround({ sources: options.sources });
_this.viewer.scene.postRender.addEventListener(() => { const e = _this.viewer.camera.position; if (Cesium.Cartographic.fromCartesian(e).height < height) { _this.viewer.scene.skyBox = currentSkyBox; _this.viewer.scene.skyAtmosphere.show = false; } else { _this.viewer.scene.skyBox = defaultSkyBox; _this.viewer.scene.skyAtmosphere.show = true; } }); } }
function createSkyBoxSources() { return { positiveX: "./skyboxImage/px.png", negativeX: "./skyboxImage/nx.png", positiveY: "./skyboxImage/pz.png", negativeY: "./skyboxImage/nz.png", positiveZ: "./skyboxImage/py.png", negativeZ: "./skyboxImage/ny.png", }; }
const heightLimit = 120000; document.getElementById("heightLimit").textContent = `${heightLimit} m`;
const viewer = new Cesium.Viewer("cesiumContainer", { baseLayer: false, animation: false, timeline: false, fullscreenButton: false, infoBox: false, geocoder: false, homeButton: false, sceneModePicker: false, baseLayerPicker: false, navigationHelpButton: false, selectionIndicator: false, shouldAnimate: true, });
viewer.imageryLayers.addImageryProvider( new Cesium.UrlTemplateImageryProvider({ url: "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}", maximumLevel: 18, }) );
viewer.scene.globe.enableLighting = false; viewer.scene.fog.enabled = true; viewer.scene.fog.density = 0.00018; viewer.scene.skyAtmosphere.show = true; viewer.scene.debugShowFramesPerSecond = true; viewer._cesiumWidget._creditContainer.style.display = "none";
const skyBox = new SkyBox(viewer); skyBox.SkyBoxOnGround({ height: heightLimit, sources: createSkyBoxSources(), });
const cameraHeightEl = document.getElementById("cameraHeight"); const skyboxStatusEl = document.getElementById("skyboxStatus");
viewer.scene.postRender.addEventListener(() => { const height = Cesium.Cartographic.fromCartesian(viewer.camera.position).height; cameraHeightEl.textContent = `${height.toFixed(0)} m`; skyboxStatusEl.textContent = height < heightLimit ? "近地天空盒" : "默认天空盒"; });
function flyToNear() { viewer.camera.flyTo({ destination: Cesium.Cartesian3.fromDegrees(116.3913, 39.9075, 5200), orientation: { heading: Cesium.Math.toRadians(18), pitch: Cesium.Math.toRadians(-12), roll: 0, }, duration: 1.4, }); }
function flyToHigh() { viewer.camera.flyTo({ destination: Cesium.Cartesian3.fromDegrees(116.3913, 39.9075, 280000), orientation: { heading: Cesium.Math.toRadians(0), pitch: Cesium.Math.toRadians(-80), roll: 0, }, duration: 1.4, }); }
document.getElementById("nearBtn").addEventListener("click", flyToNear); document.getElementById("highBtn").addEventListener("click", flyToHigh);
flyToNear(); window.viewer = viewer; </script> </body> </html>
|