Picking.html 10.1 KB
Newer Older
Schaaf's avatar
Schaaf committed
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
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <meta
      name="description"
      content="Use the mouse to select and manipulate objects in the scene."
    />
    <meta name="cesium-sandcastle-labels" content="Showcases" />
    <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
        var viewer = new Cesium.Viewer("cesiumContainer", {
          selectionIndicator: false,
          infoBox: false,
        });

        var scene = viewer.scene;
        if (!scene.pickPositionSupported) {
          window.alert("This browser does not support pickPosition.");
        }

        var handler;

        Sandcastle.addDefaultToolbarButton(
          "Show Cartographic Position on Mouse Over",
          function () {
            var entity = viewer.entities.add({
              label: {
                show: false,
                showBackground: true,
                font: "14px monospace",
                horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
                verticalOrigin: Cesium.VerticalOrigin.TOP,
                pixelOffset: new Cesium.Cartesian2(15, 0),
              },
            });

            // Mouse over the globe to see the cartographic position
            handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
            handler.setInputAction(function (movement) {
              var cartesian = viewer.camera.pickEllipsoid(
                movement.endPosition,
                scene.globe.ellipsoid
              );
              if (cartesian) {
                var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                var longitudeString = Cesium.Math.toDegrees(
                  cartographic.longitude
                ).toFixed(2);
                var latitudeString = Cesium.Math.toDegrees(
                  cartographic.latitude
                ).toFixed(2);

                entity.position = cartesian;
                entity.label.show = true;
                entity.label.text =
                  "Lon: " +
                  ("   " + longitudeString).slice(-7) +
                  "\u00B0" +
                  "\nLat: " +
                  ("   " + latitudeString).slice(-7) +
                  "\u00B0";
              } else {
                entity.label.show = false;
              }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
          }
        );

        Sandcastle.addToolbarButton("Pick Entity", function () {
          var entity = viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
            billboard: {
              image: "../images/Cesium_Logo_overlay.png",
            },
          });

          // If the mouse is over the billboard, change its scale and color
          handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
          handler.setInputAction(function (movement) {
            var pickedObject = scene.pick(movement.endPosition);
            if (Cesium.defined(pickedObject) && pickedObject.id === entity) {
              entity.billboard.scale = 2.0;
              entity.billboard.color = Cesium.Color.YELLOW;
            } else {
              entity.billboard.scale = 1.0;
              entity.billboard.color = Cesium.Color.WHITE;
            }
          }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        });

        Sandcastle.addToolbarButton("Drill-Down Picking", function () {
          var pickedEntities = new Cesium.EntityCollection();
          var pickColor = Cesium.Color.YELLOW.withAlpha(0.5);
          function makeProperty(entity, color) {
            var colorProperty = new Cesium.CallbackProperty(function (
              time,
              result
            ) {
              if (pickedEntities.contains(entity)) {
                return pickColor.clone(result);
              }
              return color.clone(result);
            },
            false);

            entity.polygon.material = new Cesium.ColorMaterialProperty(
              colorProperty
            );
          }

          var red = viewer.entities.add({
            polygon: {
              hierarchy: Cesium.Cartesian3.fromDegreesArray([
                -70.0,
                30.0,
                -60.0,
                30.0,
                -60.0,
                40.0,
                -70.0,
                40.0,
              ]),
              height: 0,
            },
          });
          makeProperty(red, Cesium.Color.RED.withAlpha(0.5));

          var blue = viewer.entities.add({
            polygon: {
              hierarchy: Cesium.Cartesian3.fromDegreesArray([
                -75.0,
                34.0,
                -63.0,
                34.0,
                -63.0,
                40.0,
                -75.0,
                40.0,
              ]),
              height: 0,
            },
          });
          makeProperty(blue, Cesium.Color.BLUE.withAlpha(0.5));

          var green = viewer.entities.add({
            polygon: {
              hierarchy: Cesium.Cartesian3.fromDegreesArray([
                -67.0,
                36.0,
                -55.0,
                36.0,
                -55.0,
                30.0,
                -67.0,
                30.0,
              ]),
              height: 0,
            },
          });
          makeProperty(green, Cesium.Color.GREEN.withAlpha(0.5));

          // Move the primitive that the mouse is over to the top.
          handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
          handler.setInputAction(function (movement) {
            // get an array of all primitives at the mouse position
            var pickedObjects = scene.drillPick(movement.endPosition);
            if (Cesium.defined(pickedObjects)) {
              //Update the collection of picked entities.
              pickedEntities.removeAll();
              for (var i = 0; i < pickedObjects.length; ++i) {
                var entity = pickedObjects[i].id;
                pickedEntities.add(entity);
              }
            }
          }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        });

        Sandcastle.addToolbarButton("Pick position", function () {
          var modelEntity = viewer.entities.add({
            name: "milktruck",
            position: Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706),
            model: {
              uri:
                "../../SampleData/models/CesiumMilkTruck/CesiumMilkTruck.glb",
            },
          });
          viewer.zoomTo(modelEntity);

          var labelEntity = viewer.entities.add({
            label: {
              show: false,
              showBackground: true,
              font: "14px monospace",
              horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
              verticalOrigin: Cesium.VerticalOrigin.TOP,
              pixelOffset: new Cesium.Cartesian2(15, 0),
            },
          });

          // Mouse over the globe to see the cartographic position
          handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
          handler.setInputAction(function (movement) {
            var foundPosition = false;

            var scene = viewer.scene;
            if (scene.mode !== Cesium.SceneMode.MORPHING) {
              var pickedObject = scene.pick(movement.endPosition);
              if (
                scene.pickPositionSupported &&
                Cesium.defined(pickedObject) &&
                pickedObject.id === modelEntity
              ) {
                var cartesian = viewer.scene.pickPosition(movement.endPosition);

                if (Cesium.defined(cartesian)) {
                  var cartographic = Cesium.Cartographic.fromCartesian(
                    cartesian
                  );
                  var longitudeString = Cesium.Math.toDegrees(
                    cartographic.longitude
                  ).toFixed(2);
                  var latitudeString = Cesium.Math.toDegrees(
                    cartographic.latitude
                  ).toFixed(2);
                  var heightString = cartographic.height.toFixed(2);

                  labelEntity.position = cartesian;
                  labelEntity.label.show = true;
                  labelEntity.label.text =
                    "Lon: " +
                    ("   " + longitudeString).slice(-7) +
                    "\u00B0" +
                    "\nLat: " +
                    ("   " + latitudeString).slice(-7) +
                    "\u00B0" +
                    "\nAlt: " +
                    ("   " + heightString).slice(-7) +
                    "m";

                  labelEntity.label.eyeOffset = new Cesium.Cartesian3(
                    0.0,
                    0.0,
                    -cartographic.height *
                      (scene.mode === Cesium.SceneMode.SCENE2D ? 1.5 : 1.0)
                  );

                  foundPosition = true;
                }
              }
            }

            if (!foundPosition) {
              labelEntity.label.show = false;
            }
          }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        });

        Sandcastle.reset = function () {
          viewer.entities.removeAll();
          handler = handler && handler.destroy();
        };
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>