|
| 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 name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> |
| 7 | + <meta name="description" content="Use Viewer to start building new applications or easily embed Cesium into existing applications."> |
| 8 | + <meta name="cesium-sandcastle-labels" content="Beginner, Showcases"> |
| 9 | + <title>Cesium Demo</title> |
| 10 | + <script type="text/javascript" src="../Sandcastle-header.js"></script> |
| 11 | + <script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script> |
| 12 | + <script type="text/javascript"> |
| 13 | + if(typeof require === "function") { |
| 14 | + require.config({ |
| 15 | + baseUrl : '../../../Source', |
| 16 | + waitSeconds : 120 |
| 17 | + }); |
| 18 | + } |
| 19 | + </script> |
| 20 | +</head> |
| 21 | +<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html"> |
| 22 | +<style> |
| 23 | + @import url(../templates/bucket.css); |
| 24 | +</style> |
| 25 | +<div id="cesiumContainer" class="fullSize"></div> |
| 26 | +<div id="loadingOverlay"><h1>Loading...</h1></div> |
| 27 | +<div id="toolbar"> |
| 28 | + <table><tbody> |
| 29 | + <tr> |
| 30 | + <td>Distance</td> |
| 31 | + <td> |
| 32 | + <input type="range" min="-100.0" max="100.0" step="1.0" data-bind="value: cylinderRadius, valueUpdate: 'input'"> |
| 33 | + <input type="text" size="5" data-bind="value: cylinderRadius"> |
| 34 | + </td> |
| 35 | + <td>Plane Count</td> |
| 36 | + <td> |
| 37 | + <input type="range" min="1" max="128" step="1" data-bind="value: planeCount, valueUpdate: 'input'"> |
| 38 | + <input type="text" size="5" data-bind="value: planeCount"> |
| 39 | + </td> |
| 40 | + </tr> |
| 41 | + </tbody></table> |
| 42 | + <select data-bind="options: exampleTypes, value: currentExampleType"></select> |
| 43 | +</div> |
| 44 | +<script id="cesium_sandcastle_script"> |
| 45 | +function startup(Cesium) { |
| 46 | + 'use strict'; |
| 47 | +//Sandcastle_Begin |
| 48 | +var viewer = new Cesium.Viewer('cesiumContainer', { |
| 49 | + infoBox: false, |
| 50 | + selectionIndicator: false, |
| 51 | + shouldAnimate : true, |
| 52 | + projectionPicker : true |
| 53 | +}); |
| 54 | + |
| 55 | +viewer.terrainProvider = new Cesium.CesiumTerrainProvider({ |
| 56 | + url : 'https://assets.agi.com/stk-terrain/v1/tilesets/world/tiles', |
| 57 | + requestWaterMask : true, |
| 58 | + requestVertexNormals : true |
| 59 | +}); |
| 60 | +viewer.extend(Cesium.viewerCesium3DTilesInspectorMixin); |
| 61 | + |
| 62 | +var globe = viewer.scene.globe; |
| 63 | +globe.depthTestAgainstTerrain = true; |
| 64 | + |
| 65 | +var cylinderRadius = -20.0; |
| 66 | +var radiusMultiplier = 1.0; |
| 67 | + |
| 68 | +var steps = 32; |
| 69 | +var clippingPlanes = []; |
| 70 | +var modelEntityClippingPlanes; |
| 71 | +var clippingModeUnion = false; |
| 72 | +var enabled = true; |
| 73 | + |
| 74 | +var clipObjects = ['model', 'b3dm', 'pnts', 'i3dm', 'terrain']; |
| 75 | +var viewModel = { |
| 76 | + cylinderRadius : cylinderRadius, |
| 77 | + exampleTypes : clipObjects, |
| 78 | + currentExampleType : clipObjects[0], |
| 79 | + planeCount : steps |
| 80 | +}; |
| 81 | + |
| 82 | +Cesium.knockout.track(viewModel); |
| 83 | + |
| 84 | +var toolbar = document.getElementById('toolbar'); |
| 85 | +Cesium.knockout.applyBindings(viewModel, toolbar); |
| 86 | + |
| 87 | +Cesium.knockout.getObservable(viewModel, 'cylinderRadius').subscribe( |
| 88 | + function(newValue) { |
| 89 | + cylinderRadius = parseFloat(viewModel.cylinderRadius); |
| 90 | + updatePlanes(); |
| 91 | + } |
| 92 | +); |
| 93 | + |
| 94 | +Cesium.knockout.getObservable(viewModel, 'planeCount').subscribe( |
| 95 | + function(newValue) { |
| 96 | + var newSteps = parseFloat(viewModel.planeCount); |
| 97 | + if (newSteps !== steps) { |
| 98 | + steps = newSteps; |
| 99 | + modelEntityClippingPlanes.removeAll(); |
| 100 | + computePlanes(); |
| 101 | + } |
| 102 | + } |
| 103 | +); |
| 104 | + |
| 105 | +var scene = viewer.scene; |
| 106 | +var planeEntities = []; |
| 107 | +var selectedPlane; |
| 108 | + |
| 109 | +function updatePlanes() { |
| 110 | + for (var i = 0; i < clippingPlanes.length; i++) { |
| 111 | + var plane = clippingPlanes[i]; |
| 112 | + plane.distance = cylinderRadius * radiusMultiplier; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function computePlanes() { |
| 117 | + var stepDegrees = 6.28319 / steps; |
| 118 | + clippingPlanes = []; |
| 119 | + |
| 120 | + for (var i = 0; i < steps; i++) { |
| 121 | + var angle = i * stepDegrees; |
| 122 | + var dir = new Cesium.Cartesian3(); |
| 123 | + dir.x = 1.0; |
| 124 | + dir.y = Math.tan(angle); |
| 125 | + if (angle > 1.57079632679) { |
| 126 | + dir.x = -1.0; |
| 127 | + dir.y *= -1.0; |
| 128 | + } |
| 129 | + if (angle > 3.14159265359) { |
| 130 | + dir.x = -1.0; |
| 131 | + dir.y = dir.y; |
| 132 | + } |
| 133 | + if (angle > 4.71238898038) { |
| 134 | + dir.x = 1.0; |
| 135 | + dir.y = -dir.y; |
| 136 | + } |
| 137 | + Cesium.Cartesian3.normalize(dir, dir); |
| 138 | + var newPlane = new Cesium.ClippingPlane(dir, cylinderRadius * radiusMultiplier); |
| 139 | + modelEntityClippingPlanes.add(newPlane); |
| 140 | + clippingPlanes.push(newPlane); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +function createClippingPlanes(modelMatrix) { |
| 145 | + modelEntityClippingPlanes = new Cesium.ClippingPlaneCollection({ |
| 146 | + modelMatrix : Cesium.defined(modelMatrix) ? modelMatrix : Cesium.Matrix4.IDENTITY, |
| 147 | + edgeWidth: 2.0, |
| 148 | + edgeColor: Cesium.Color.WHITE, |
| 149 | + unionClippingRegions : clippingModeUnion, |
| 150 | + enabled : enabled |
| 151 | + }); |
| 152 | + computePlanes(); |
| 153 | +} |
| 154 | + |
| 155 | +function updateClippingPlanes() { |
| 156 | + return modelEntityClippingPlanes; |
| 157 | +} |
| 158 | + |
| 159 | +var modelUrl = '../../SampleData/models/CesiumAir/Cesium_Air.glb'; |
| 160 | +var agiHqUrl = Cesium.IonResource.fromAssetId(3836); |
| 161 | +var instancedUrl = Cesium.IonResource.fromAssetId(3876); |
| 162 | +var pointCloudUrl = Cesium.IonResource.fromAssetId(3844); |
| 163 | + |
| 164 | +function loadModel(url) { |
| 165 | + createClippingPlanes(); |
| 166 | + var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 300.0); |
| 167 | + var heading = 0.0; |
| 168 | + var pitch = 0.0; |
| 169 | + var roll = 0.0; |
| 170 | + var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll); |
| 171 | + var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr); |
| 172 | + var entity = viewer.entities.add({ |
| 173 | + name : url, |
| 174 | + position : position, |
| 175 | + orientation : orientation, |
| 176 | + model : { |
| 177 | + uri : url, |
| 178 | + scale : 20, |
| 179 | + minimumPixelSize : 100.0, |
| 180 | + clippingPlanes : new Cesium.CallbackProperty(updateClippingPlanes, false) |
| 181 | + } |
| 182 | + }); |
| 183 | + viewer.trackedEntity = entity; |
| 184 | +} |
| 185 | + |
| 186 | +var tileset; |
| 187 | +function loadTileset(url) { |
| 188 | + createClippingPlanes(); |
| 189 | + tileset = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({ |
| 190 | + url : url, |
| 191 | + clippingPlanes : modelEntityClippingPlanes |
| 192 | + })); |
| 193 | + |
| 194 | + return tileset.readyPromise.then(function() { |
| 195 | + var boundingSphere = tileset.boundingSphere; |
| 196 | + |
| 197 | + var cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center); |
| 198 | + var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0); |
| 199 | + var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 100.0); |
| 200 | + var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3()); |
| 201 | + tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation); |
| 202 | + |
| 203 | + var radius = boundingSphere.radius; |
| 204 | + viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.5, -0.2, radius * 4.0)); |
| 205 | + viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY); |
| 206 | + }).otherwise(function(error) { |
| 207 | + throw(error); |
| 208 | + }); |
| 209 | +} |
| 210 | + |
| 211 | +loadModel(modelUrl); |
| 212 | + |
| 213 | +Cesium.knockout.getObservable(viewModel, 'currentExampleType').subscribe(function(newValue) { |
| 214 | + reset(); |
| 215 | + |
| 216 | + if (newValue === clipObjects[0]) { |
| 217 | + // Model |
| 218 | + loadModel(modelUrl); |
| 219 | + } else if (newValue === clipObjects[1]) { |
| 220 | + // B3dm photogrammetry |
| 221 | + agiHqUrl.then(function(resource) { |
| 222 | + return loadTileset(resource); |
| 223 | + }); |
| 224 | + } else if (newValue === clipObjects[2]) { |
| 225 | + // Point clouds |
| 226 | + radiusMultiplier = 20.0; |
| 227 | + pointCloudUrl.then(function(resource) { |
| 228 | + return loadTileset(resource); |
| 229 | + }).then(function() { |
| 230 | + tileset.pointCloudShading.attenuation = true; |
| 231 | + }); |
| 232 | + } else if (newValue === clipObjects[3]) { |
| 233 | + // i3dm |
| 234 | + instancedUrl.then(function(resource) { |
| 235 | + return loadTileset(resource); |
| 236 | + }).then(function() { |
| 237 | + tileset.clippingPlanes.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(tileset.boundingSphere.center); |
| 238 | + }); |
| 239 | + } else if (newValue === clipObjects[4]) { |
| 240 | + // Terrain |
| 241 | + var position = Cesium.Cartesian3.fromRadians(-2.0872979473351286, 0.6596620013036164, 2380.0); |
| 242 | + var entity = viewer.entities.add({ |
| 243 | + position : position, |
| 244 | + model : { |
| 245 | + uri : '../../SampleData/models/CesiumMan/Cesium_Man.glb', |
| 246 | + minimumPixelSize : 128, |
| 247 | + scale : 40 |
| 248 | + } |
| 249 | + }); |
| 250 | + viewer.trackedEntity = entity; |
| 251 | + createClippingPlanes(entity.computeModelMatrix(Cesium.JulianDate.now())); |
| 252 | + globe.clippingPlanes = modelEntityClippingPlanes; |
| 253 | + } |
| 254 | + updatePlanes(); |
| 255 | +}); |
| 256 | + |
| 257 | +function reset() { |
| 258 | + radiusMultiplier = 1.0; |
| 259 | + viewModel.cylinderRadius = cylinderRadius; |
| 260 | + viewer.entities.removeAll(); |
| 261 | + viewer.scene.primitives.removeAll(); |
| 262 | + globe.clippingPlanes = undefined; // destroy Globe clipping planes, if any |
| 263 | + modelEntityClippingPlanes = undefined; |
| 264 | +} |
| 265 | + |
| 266 | +Sandcastle.addToggleButton('union', clippingModeUnion, function(checked) { |
| 267 | + clippingModeUnion = checked; |
| 268 | + modelEntityClippingPlanes.unionClippingRegions = clippingModeUnion; |
| 269 | +}); |
| 270 | + |
| 271 | +Sandcastle.addToggleButton('enabled', enabled, function(checked) { |
| 272 | + enabled = checked; |
| 273 | + modelEntityClippingPlanes.enabled = enabled; |
| 274 | +}); |
| 275 | + |
| 276 | +//Sandcastle_End |
| 277 | + Sandcastle.finishedLoading(); |
| 278 | +} |
| 279 | +if (typeof Cesium !== "undefined") { |
| 280 | + startup(Cesium); |
| 281 | +} else if (typeof require === "function") { |
| 282 | + require(["Cesium"], startup); |
| 283 | +} |
| 284 | +</script> |
| 285 | +</body> |
| 286 | +</html> |
0 commit comments