LocalToFixedFrame.html 9.36 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
<!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 localFrameToFixedFrameGenerator for adequate rotation."
    />
    <meta name="cesium-sandcastle-labels" content="Tutorials" />
    <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">
      <table class="infoPanel">
        <tbody>
          <tr>
            <td>
              Click on the 3D window then use the keyboard to change settings.
            </td>
          </tr>
          <tr>
            <td>Heading: <span id="heading"></span>°</td>
          </tr>
          <tr>
            <td>← to left/→ to right</td>
          </tr>
          <tr>
            <td>Pitch: <span id="pitch"></span>°</td>
          </tr>
          <tr>
            <td>↑ to up/↓ to down</td>
          </tr>
          <tr>
            <td>roll: <span id="roll"></span>°</td>
          </tr>
          <tr>
            <td>← + ⇧ left/→ + ⇧ right</td>
          </tr>
        </tbody>
      </table>
    </div>
    <script id="cesium_sandcastle_script">
      function startup(Cesium) {
        "use strict";
        //Sandcastle_Begin
        var viewer = new Cesium.Viewer("cesiumContainer");
        var canvas = viewer.canvas;
        canvas.setAttribute("tabindex", "0"); // needed to put focus on the canvas
        canvas.addEventListener("click", function () {
          canvas.focus();
        });
        canvas.focus();

        var scene = viewer.scene;
        var camera = viewer.camera;
        var controller = scene.screenSpaceCameraController;
        var r = 0;
        var center = new Cesium.Cartesian3();

        var hpRoll = new Cesium.HeadingPitchRoll();
        var hpRange = new Cesium.HeadingPitchRange();
        var deltaRadians = Cesium.Math.toRadians(1.0);

        var localFrames = [
          {
            pos: Cesium.Cartesian3.fromDegrees(-123.075, 44.045, 5000.0),
            converter: Cesium.Transforms.eastNorthUpToFixedFrame,
            comments: "Classical East North Up\nlocal Frame",
          },
          {
            pos: Cesium.Cartesian3.fromDegrees(-123.075, 44.05, 5500.0),
            converter: Cesium.Transforms.localFrameToFixedFrameGenerator(
              "north",
              "west"
            ),
            comments: "North West Up\nlocal Frame",
          },
          {
            pos: Cesium.Cartesian3.fromDegrees(-123.075, 44.04, 4500.0),
            converter: Cesium.Transforms.localFrameToFixedFrameGenerator(
              "south",
              "up"
            ),
            comments: "South Up West\nlocal Frame",
          },
          {
            pos: Cesium.Cartesian3.fromDegrees(-123.075, 44.05, 4500.0),
            converter: Cesium.Transforms.localFrameToFixedFrameGenerator(
              "up",
              "east"
            ),
            comments: "Up East North\nlocal Frame",
          },
          {
            pos: Cesium.Cartesian3.fromDegrees(-123.075, 44.04, 5500.0),
            converter: Cesium.Transforms.localFrameToFixedFrameGenerator(
              "down",
              "east"
            ),
            comments: "Down East South\nlocal Frame",
          },
        ];

        var primitives = [];
        var hprRollZero = new Cesium.HeadingPitchRoll();

        for (var i = 0; i < localFrames.length; i++) {
          var position = localFrames[i].pos;
          var converter = localFrames[i].converter;
          var comments = localFrames[i].comments;
          var planePrimitive = scene.primitives.add(
            Cesium.Model.fromGltf({
              url: "../../SampleData/models/CesiumAir/Cesium_Air.glb",
              modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
                position,
                hpRoll,
                Cesium.Ellipsoid.WGS84,
                converter
              ),
              minimumPixelSize: 128,
            })
          );

          primitives.push({
            primitive: planePrimitive,
            converter: converter,
            position: position,
          });
          var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
            position,
            hprRollZero,
            Cesium.Ellipsoid.WGS84,
            converter
          );
          scene.primitives.add(
            new Cesium.DebugModelMatrixPrimitive({
              modelMatrix: modelMatrix,
              length: 300.0,
              width: 10.0,
            })
          );

          var positionLabel = position.clone();
          positionLabel.z = position.z + 300.0;
          viewer.entities.add({
            position: positionLabel,
            label: {
              text: comments,
              font: "18px Helvetica",
              fillColor: Cesium.Color.WHITE,
              outlineColor: Cesium.Color.BLACK,
              outlineWidth: 2,
              style: Cesium.LabelStyle.FILL_AND_OUTLINE,
              verticalOrigin: Cesium.VerticalOrigin.CENTER,
              HorizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
            },
          });
        }

        primitives[0].primitive.readyPromise.then(function (model) {
          // Play and loop all animations at half-speed
          model.activeAnimations.addAll({
            multiplier: 0.5,
            loop: Cesium.ModelAnimationLoop.REPEAT,
          });

          // Zoom to model
          r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
          controller.minimumZoomDistance = r * 0.5;
          Cesium.Matrix4.multiplyByPoint(
            model.modelMatrix,
            model.boundingSphere.center,
            center
          );
          var heading = Cesium.Math.toRadians(90.0);
          var pitch = Cesium.Math.toRadians(0.0);
          hpRange.heading = heading;
          hpRange.pitch = pitch;
          hpRange.range = r * 100.0;
          camera.lookAt(center, hpRange);
        });

        document.addEventListener("keydown", function (e) {
          switch (e.keyCode) {
            case 40:
              // pitch down
              hpRoll.pitch -= deltaRadians;
              if (hpRoll.pitch < -Cesium.Math.TWO_PI) {
                hpRoll.pitch += Cesium.Math.TWO_PI;
              }
              break;
            case 38:
              // pitch up
              hpRoll.pitch += deltaRadians;
              if (hpRoll.pitch > Cesium.Math.TWO_PI) {
                hpRoll.pitch -= Cesium.Math.TWO_PI;
              }
              break;
            case 39:
              if (e.shiftKey) {
                // roll right
                hpRoll.roll += deltaRadians;
                if (hpRoll.roll > Cesium.Math.TWO_PI) {
                  hpRoll.roll -= Cesium.Math.TWO_PI;
                }
              } else {
                // turn right
                hpRoll.heading += deltaRadians;
                if (hpRoll.heading > Cesium.Math.TWO_PI) {
                  hpRoll.heading -= Cesium.Math.TWO_PI;
                }
              }
              break;
            case 37:
              if (e.shiftKey) {
                // roll left until
                hpRoll.roll -= deltaRadians;
                if (hpRoll.roll < 0.0) {
                  hpRoll.roll += Cesium.Math.TWO_PI;
                }
              } else {
                // turn left
                hpRoll.heading -= deltaRadians;
                if (hpRoll.heading < 0.0) {
                  hpRoll.heading += Cesium.Math.TWO_PI;
                }
              }
              break;
            default:
          }
        });

        var headingSpan = document.getElementById("heading");
        var pitchSpan = document.getElementById("pitch");
        var rollSpan = document.getElementById("roll");

        viewer.scene.preUpdate.addEventListener(function (scene, time) {
          for (var i = 0; i < primitives.length; i++) {
            var primitive = primitives[i].primitive;
            var converter = primitives[i].converter;
            var position = primitives[i].position;
            Cesium.Transforms.headingPitchRollToFixedFrame(
              position,
              hpRoll,
              Cesium.Ellipsoid.WGS84,
              converter,
              primitive.modelMatrix
            );
          }
        });

        viewer.scene.preRender.addEventListener(function (scene, time) {
          headingSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.heading).toFixed(
            1
          );
          pitchSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.pitch).toFixed(1);
          rollSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.roll).toFixed(1);
        });
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>