yzt
2023-05-05 634ab285812bcc3eb802cacb9ec54f489bc2728f
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- Use Chrome Frame in IE -->
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <meta name="description" content="Pick features in a 3D Tiles tileset." />
    <meta name="cesium-sandcastle-labels" content="Showcases, 3D Tiles" />
    <title>Cesium Demo</title>
    <script type="text/javascript" src="../Sandcastle-header.js"></script>
    <script
      type="text/javascript"
      src="../../../Build/CesiumUnminified/Cesium.js"
      nomodule
    ></script>
    <script type="module" src="../load-cesium-es6.js"></script>
  </head>
  <body
    class="sandcastle-loading"
    data-sandcastle-bucket="bucket-requirejs.html"
  >
    <style>
      @import url(../templates/bucket.css);
    </style>
    <div id="cesiumContainer" class="fullSize"></div>
    <div id="loadingOverlay"><h1>Loading...</h1></div>
    <div id="toolbar"></div>
    <script id="cesium_sandcastle_script">
      function startup(Cesium) {
        "use strict";
        //Sandcastle_Begin
        // A simple demo of 3D Tiles feature picking with hover and select behavior
        // Building data courtesy of NYC OpenData portal: http://www1.nyc.gov/site/doitt/initiatives/3d-building.page
        var viewer = new Cesium.Viewer("cesiumContainer", {
          terrainProvider: Cesium.createWorldTerrain(),
        });
 
        viewer.scene.globe.depthTestAgainstTerrain = true;
 
        // Set the initial camera view to look at Manhattan
        var initialPosition = Cesium.Cartesian3.fromDegrees(
          -74.01881302800248,
          40.69114333714821,
          753
        );
        var initialOrientation = new Cesium.HeadingPitchRoll.fromDegrees(
          21.27879878293835,
          -21.34390550872461,
          0.0716951918898415
        );
        viewer.scene.camera.setView({
          destination: initialPosition,
          orientation: initialOrientation,
          endTransform: Cesium.Matrix4.IDENTITY,
        });
 
        // Load the NYC buildings tileset
        var tileset = new Cesium.Cesium3DTileset({
          url: Cesium.IonResource.fromAssetId(75343),
        });
        viewer.scene.primitives.add(tileset);
 
        // HTML overlay for showing feature name on mouseover
        var nameOverlay = document.createElement("div");
        viewer.container.appendChild(nameOverlay);
        nameOverlay.className = "backdrop";
        nameOverlay.style.display = "none";
        nameOverlay.style.position = "absolute";
        nameOverlay.style.bottom = "0";
        nameOverlay.style.left = "0";
        nameOverlay.style["pointer-events"] = "none";
        nameOverlay.style.padding = "4px";
        nameOverlay.style.backgroundColor = "black";
 
        // Information about the currently selected feature
        var selected = {
          feature: undefined,
          originalColor: new Cesium.Color(),
        };
 
        // An entity object which will hold info about the currently selected feature for infobox display
        var selectedEntity = new Cesium.Entity();
 
        // Get default left click handler for when a feature is not picked on left click
        var clickHandler = viewer.screenSpaceEventHandler.getInputAction(
          Cesium.ScreenSpaceEventType.LEFT_CLICK
        );
 
        // If silhouettes are supported, silhouette features in blue on mouse over and silhouette green on mouse click.
        // If silhouettes are not supported, change the feature color to yellow on mouse over and green on mouse click.
        if (
          Cesium.PostProcessStageLibrary.isSilhouetteSupported(viewer.scene)
        ) {
          // Silhouettes are supported
          var silhouetteBlue = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
          silhouetteBlue.uniforms.color = Cesium.Color.BLUE;
          silhouetteBlue.uniforms.length = 0.01;
          silhouetteBlue.selected = [];
 
          var silhouetteGreen = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
          silhouetteGreen.uniforms.color = Cesium.Color.LIME;
          silhouetteGreen.uniforms.length = 0.01;
          silhouetteGreen.selected = [];
 
          viewer.scene.postProcessStages.add(
            Cesium.PostProcessStageLibrary.createSilhouetteStage([
              silhouetteBlue,
              silhouetteGreen,
            ])
          );
 
          // Silhouette a feature blue on hover.
          viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(
            movement
          ) {
            // If a feature was previously highlighted, undo the highlight
            silhouetteBlue.selected = [];
 
            // Pick a new feature
            var pickedFeature = viewer.scene.pick(movement.endPosition);
            if (!Cesium.defined(pickedFeature)) {
              nameOverlay.style.display = "none";
              return;
            }
 
            // A feature was picked, so show it's overlay content
            nameOverlay.style.display = "block";
            nameOverlay.style.bottom =
              viewer.canvas.clientHeight - movement.endPosition.y + "px";
            nameOverlay.style.left = movement.endPosition.x + "px";
            var name = pickedFeature.getProperty("BIN");
            nameOverlay.textContent = name;
 
            // Highlight the feature if it's not already selected.
            if (pickedFeature !== selected.feature) {
              silhouetteBlue.selected = [pickedFeature];
            }
          },
          Cesium.ScreenSpaceEventType.MOUSE_MOVE);
 
          // Silhouette a feature on selection and show metadata in the InfoBox.
          viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
            movement
          ) {
            // If a feature was previously selected, undo the highlight
            silhouetteGreen.selected = [];
 
            // Pick a new feature
            var pickedFeature = viewer.scene.pick(movement.position);
            if (!Cesium.defined(pickedFeature)) {
              clickHandler(movement);
              return;
            }
 
            // Select the feature if it's not already selected
            if (silhouetteGreen.selected[0] === pickedFeature) {
              return;
            }
 
            // Save the selected feature's original color
            var highlightedFeature = silhouetteBlue.selected[0];
            if (pickedFeature === highlightedFeature) {
              silhouetteBlue.selected = [];
            }
 
            // Highlight newly selected feature
            silhouetteGreen.selected = [pickedFeature];
 
            // Set feature infobox description
            var featureName = pickedFeature.getProperty("name");
            selectedEntity.name = featureName;
            selectedEntity.description =
              'Loading <div class="cesium-infoBox-loading"></div>';
            viewer.selectedEntity = selectedEntity;
            selectedEntity.description =
              '<table class="cesium-infoBox-defaultTable"><tbody>' +
              "<tr><th>BIN</th><td>" +
              pickedFeature.getProperty("BIN") +
              "</td></tr>" +
              "<tr><th>DOITT ID</th><td>" +
              pickedFeature.getProperty("DOITT_ID") +
              "</td></tr>" +
              "<tr><th>SOURCE ID</th><td>" +
              pickedFeature.getProperty("SOURCE_ID") +
              "</td></tr>" +
              "</tbody></table>";
          },
          Cesium.ScreenSpaceEventType.LEFT_CLICK);
        } else {
          // Silhouettes are not supported. Instead, change the feature color.
 
          // Information about the currently highlighted feature
          var highlighted = {
            feature: undefined,
            originalColor: new Cesium.Color(),
          };
 
          // Color a feature yellow on hover.
          viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(
            movement
          ) {
            // If a feature was previously highlighted, undo the highlight
            if (Cesium.defined(highlighted.feature)) {
              highlighted.feature.color = highlighted.originalColor;
              highlighted.feature = undefined;
            }
            // Pick a new feature
            var pickedFeature = viewer.scene.pick(movement.endPosition);
            if (!Cesium.defined(pickedFeature)) {
              nameOverlay.style.display = "none";
              return;
            }
            // A feature was picked, so show it's overlay content
            nameOverlay.style.display = "block";
            nameOverlay.style.bottom =
              viewer.canvas.clientHeight - movement.endPosition.y + "px";
            nameOverlay.style.left = movement.endPosition.x + "px";
            var name = pickedFeature.getProperty("name");
            if (!Cesium.defined(name)) {
              name = pickedFeature.getProperty("id");
            }
            nameOverlay.textContent = name;
            // Highlight the feature if it's not already selected.
            if (pickedFeature !== selected.feature) {
              highlighted.feature = pickedFeature;
              Cesium.Color.clone(
                pickedFeature.color,
                highlighted.originalColor
              );
              pickedFeature.color = Cesium.Color.YELLOW;
            }
          },
          Cesium.ScreenSpaceEventType.MOUSE_MOVE);
 
          // Color a feature on selection and show metadata in the InfoBox.
          viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
            movement
          ) {
            // If a feature was previously selected, undo the highlight
            if (Cesium.defined(selected.feature)) {
              selected.feature.color = selected.originalColor;
              selected.feature = undefined;
            }
            // Pick a new feature
            var pickedFeature = viewer.scene.pick(movement.position);
            if (!Cesium.defined(pickedFeature)) {
              clickHandler(movement);
              return;
            }
            // Select the feature if it's not already selected
            if (selected.feature === pickedFeature) {
              return;
            }
            selected.feature = pickedFeature;
            // Save the selected feature's original color
            if (pickedFeature === highlighted.feature) {
              Cesium.Color.clone(
                highlighted.originalColor,
                selected.originalColor
              );
              highlighted.feature = undefined;
            } else {
              Cesium.Color.clone(pickedFeature.color, selected.originalColor);
            }
            // Highlight newly selected feature
            pickedFeature.color = Cesium.Color.LIME;
            // Set feature infobox description
            var featureName = pickedFeature.getProperty("name");
            selectedEntity.name = featureName;
            selectedEntity.description =
              'Loading <div class="cesium-infoBox-loading"></div>';
            viewer.selectedEntity = selectedEntity;
            selectedEntity.description =
              '<table class="cesium-infoBox-defaultTable"><tbody>' +
              "<tr><th>BIN</th><td>" +
              pickedFeature.getProperty("BIN") +
              "</td></tr>" +
              "<tr><th>DOITT ID</th><td>" +
              pickedFeature.getProperty("DOITT_ID") +
              "</td></tr>" +
              "<tr><th>SOURCE ID</th><td>" +
              pickedFeature.getProperty("SOURCE_ID") +
              "</td></tr>" +
              "<tr><th>Longitude</th><td>" +
              pickedFeature.getProperty("longitude") +
              "</td></tr>" +
              "<tr><th>Latitude</th><td>" +
              pickedFeature.getProperty("latitude") +
              "</td></tr>" +
              "<tr><th>Height</th><td>" +
              pickedFeature.getProperty("height") +
              "</td></tr>" +
              "<tr><th>Terrain Height (Ellipsoid)</th><td>" +
              pickedFeature.getProperty("TerrainHeight") +
              "</td></tr>" +
              "</tbody></table>";
          },
          Cesium.ScreenSpaceEventType.LEFT_CLICK);
        }
 
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>