|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta |
| 7 | + name="viewport" |
| 8 | + content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" |
| 9 | + /> |
| 10 | + <meta name="description" content="Apply materials to the globe." /> |
| 11 | + <meta name="cesium-sandcastle-labels" content="Showcases" /> |
| 12 | + <title>Cesium Demo</title> |
| 13 | + <script type="text/javascript" src="../Sandcastle-header.js"></script> |
| 14 | + <script |
| 15 | + type="text/javascript" |
| 16 | + src="../../../Build/CesiumUnminified/Cesium.js" |
| 17 | + nomodule |
| 18 | + ></script> |
| 19 | + <script type="module" src="../load-cesium-es6.js"></script> |
| 20 | + </head> |
| 21 | + <body |
| 22 | + class="sandcastle-loading" |
| 23 | + data-sandcastle-bucket="bucket-requirejs.html" |
| 24 | + > |
| 25 | + <style> |
| 26 | + @import url(../templates/bucket.css); |
| 27 | + </style> |
| 28 | + <div id="cesiumContainer" class="fullSize"></div> |
| 29 | + <div id="loadingOverlay"><h1>Loading...</h1></div> |
| 30 | + <div id="toolbar"> |
| 31 | + <div id="zoomButtons"></div> |
| 32 | + </div> |
| 33 | + <script id="cesium_sandcastle_script"> |
| 34 | + window.startup = async function (Cesium) { |
| 35 | + "use strict"; |
| 36 | + //Sandcastle_Begin |
| 37 | + const viewer = new Cesium.Viewer("cesiumContainer", { |
| 38 | + terrain: Cesium.Terrain.fromWorldTerrain({ |
| 39 | + requestVertexNormals: true, // Needed for hillshading |
| 40 | + requestWaterMask: true, // Needed to distinguish land from water |
| 41 | + }), |
| 42 | + timeline: false, |
| 43 | + animation: false, |
| 44 | + }); |
| 45 | + |
| 46 | + // Create a globe material for shading elevation only on land |
| 47 | + const customElevationMaterial = new Cesium.Material({ |
| 48 | + fabric: { |
| 49 | + type: "ElevationLand", |
| 50 | + materials: { |
| 51 | + waterMaskMaterial: { |
| 52 | + type: "WaterMask", |
| 53 | + }, |
| 54 | + elevationRampMaterial: { |
| 55 | + type: "ElevationRamp", |
| 56 | + }, |
| 57 | + }, |
| 58 | + components: { |
| 59 | + diffuse: "elevationRampMaterial.diffuse", |
| 60 | + alpha: "1.0 - waterMaskMaterial.alpha", // We'll need the inverse of the watermask to shade land |
| 61 | + }, |
| 62 | + }, |
| 63 | + translucent: false, |
| 64 | + }); |
| 65 | + |
| 66 | + const minHeight = -414.0; // approximate dead sea elevation |
| 67 | + const maxHeight = 8777.0; // approximate everest elevation |
| 68 | + const elevationRamp = [0.0, 0.045, 0.45, 0.5, 0.55, 1.0]; |
| 69 | + function getColorRamp() { |
| 70 | + const ramp = document.createElement("canvas"); |
| 71 | + ramp.width = 100; |
| 72 | + ramp.height = 1; |
| 73 | + const ctx = ramp.getContext("2d"); |
| 74 | + |
| 75 | + const values = elevationRamp; |
| 76 | + |
| 77 | + const grd = ctx.createLinearGradient(0, 0, 100, 0); |
| 78 | + |
| 79 | + // See https://gis.stackexchange.com/questions/25099/choosing-colour-ramp-to-use-for-elevation |
| 80 | + grd.addColorStop(values[0], "#344f31"); |
| 81 | + grd.addColorStop(values[1], "#5b8742"); |
| 82 | + grd.addColorStop(values[2], "#e6daa5"); |
| 83 | + grd.addColorStop(values[3], "#fdc771"); |
| 84 | + grd.addColorStop(values[4], "#b99d89"); |
| 85 | + grd.addColorStop(values[5], "#f0f0f0"); |
| 86 | + |
| 87 | + ctx.fillStyle = grd; |
| 88 | + ctx.fillRect(0, 0, 100, 1); |
| 89 | + |
| 90 | + return ramp; |
| 91 | + } |
| 92 | + |
| 93 | + const globe = viewer.scene.globe; |
| 94 | + const material = customElevationMaterial; |
| 95 | + const shadingUniforms = |
| 96 | + material.materials.elevationRampMaterial.uniforms; |
| 97 | + shadingUniforms.minimumHeight = minHeight; |
| 98 | + shadingUniforms.maximumHeight = maxHeight; |
| 99 | + shadingUniforms.image = getColorRamp(); |
| 100 | + |
| 101 | + globe.material = material; |
| 102 | + globe.showWaterEffect = false; |
| 103 | + globe.enableLighting = true; |
| 104 | + globe.maximumScreenSpaceError = 0.5; // Load higher resolution tiles for more detailed shading |
| 105 | + |
| 106 | + // Light the scene with a hillshade effect similar to https://pro.arcgis.com/en/pro-app/latest/tool-reference/3d-analyst/how-hillshade-works.htm |
| 107 | + const scene = viewer.scene; |
| 108 | + scene.light = new Cesium.DirectionalLight({ |
| 109 | + direction: new Cesium.Cartesian3(1, 0, 0), // Updated every frame |
| 110 | + }); |
| 111 | + |
| 112 | + // Update the light position base on the camera |
| 113 | + const scratchNormal = new Cesium.Cartesian3(); |
| 114 | + scene.preRender.addEventListener(function (scene, time) { |
| 115 | + const surfaceNormal = globe.ellipsoid.geodeticSurfaceNormal( |
| 116 | + scene.camera.positionWC, |
| 117 | + scratchNormal |
| 118 | + ); |
| 119 | + const negativeNormal = Cesium.Cartesian3.negate( |
| 120 | + surfaceNormal, |
| 121 | + surfaceNormal |
| 122 | + ); |
| 123 | + scene.light.direction = Cesium.Cartesian3.normalize( |
| 124 | + Cesium.Cartesian3.add( |
| 125 | + negativeNormal, |
| 126 | + scene.camera.rightWC, |
| 127 | + surfaceNormal |
| 128 | + ), |
| 129 | + scene.light.direction |
| 130 | + ); |
| 131 | + }); |
| 132 | + //Sandcastle_End |
| 133 | + }; |
| 134 | + if (typeof Cesium !== "undefined") { |
| 135 | + window.startupCalled = true; |
| 136 | + window.startup(Cesium).catch((error) => { |
| 137 | + "use strict"; |
| 138 | + console.error(error); |
| 139 | + }); |
| 140 | + Sandcastle.finishedLoading(); |
| 141 | + } |
| 142 | + </script> |
| 143 | + </body> |
| 144 | +</html> |
0 commit comments