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
| 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, infoBox: false, });
viewer.cesiumWidget.creditContainer.style.display = "none";
viewer.scene.camera.setView({ destination: new Cesium.Cartesian3( 1216356.033078094, -4736402.278325668, 4081270.375520902, ), orientation: new Cesium.HeadingPitchRoll( 0.08033365594766728, -0.29519015695063455, 0.00027759141518046704, ), endTransform: Cesium.Matrix4.IDENTITY, });
if (!viewer.scene.context.depthTexture) { window.alert("此浏览器不支持雾后期处理(post process)。"); }
const fragmentShaderSource = ` float getDistance(sampler2D depthTexture, vec2 texCoords) { float depth = czm_unpackDepth(texture(depthTexture, texCoords)); if (depth == 0.0) { return czm_infinity; } vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, depth); return -eyeCoordinate.z / eyeCoordinate.w; } float interpolateByDistance(vec4 nearFarScalar, float distance) { float startDistance = nearFarScalar.x; float startValue = nearFarScalar.y; float endDistance = nearFarScalar.z; float endValue = nearFarScalar.w; float t = clamp((distance - startDistance) / (endDistance - startDistance), 0.0, 1.0); return mix(startValue, endValue, t); } vec4 alphaBlend(vec4 sourceColor, vec4 destinationColor) { return sourceColor * vec4(sourceColor.aaa, 1.0) + destinationColor * (1.0 - sourceColor.a); } uniform sampler2D colorTexture; uniform sampler2D depthTexture; uniform vec4 fogByDistance; uniform vec4 fogColor; in vec2 v_textureCoordinates; void main(void) { float distance = getDistance(depthTexture, v_textureCoordinates); vec4 sceneColor = texture(colorTexture, v_textureCoordinates); float blendAmount = interpolateByDistance(fogByDistance, distance); vec4 finalFogColor = vec4(fogColor.rgb, fogColor.a * blendAmount); out_FragColor = alphaBlend(finalFogColor, sceneColor); } `;
const ellipsoid = viewer.scene.globe.ellipsoid;
const postProcessStage = viewer.scene.postProcessStages.add( new Cesium.PostProcessStage({
fragmentShader: fragmentShaderSource, uniforms: {
fogByDistance: new Cesium.Cartesian4(10, 0.0, 200, 1.0), fogColor: Cesium.Color.BLACK, }, }), );
try { const tileset = await Cesium.Cesium3DTileset.fromIonAssetId(40866); viewer.scene.primitives.add(tileset); } catch (error) { console.log(`瓦片数据加载出错: ${error}`); }
|