Multi-part CZML.html 5.84 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
<!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="A CZML example showing a single entity path split across multiple CZML streams."
    />
    <meta
      name="cesium-sandcastle-labels"
      content="Showcases, DataSources, CZML"
    />
    <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", {
          shouldAnimate: true,
        });

        var statusDisplay = document.createElement("div");
        var fuelDisplay = document.createElement("div");
        var czmlPath = "../../SampleData/";
        var vehicleEntity;

        // Add a blank CzmlDataSource to hold our multi-part entity/entities.
        var dataSource = new Cesium.CzmlDataSource();
        viewer.dataSources.add(dataSource);

        // This demo shows how a single path can be broken up into several CZML streams.
        var partsToLoad = [
          {
            url: "MultipartVehicle_part1.czml",
            range: [0, 1500],
            requested: false,
            loaded: false,
          },
          {
            url: "MultipartVehicle_part2.czml",
            range: [1500, 3000],
            requested: false,
            loaded: false,
          },
          {
            url: "MultipartVehicle_part3.czml",
            range: [3000, 4500],
            requested: false,
            loaded: false,
          },
        ];

        function updateStatusDisplay() {
          var msg = "";
          partsToLoad.forEach(function (part) {
            msg += part.url + " - ";
            if (part.loaded) {
              msg += "Loaded.<br/>";
            } else if (part.requested) {
              msg += "Loading now...<br/>";
            } else {
              msg += "Not needed yet.<br/>";
            }
          });
          statusDisplay.innerHTML = msg;
        }

        // Helper function to mark a part as requested, and process it into the dataSource.
        function processPart(part) {
          part.requested = true;
          updateStatusDisplay();
          dataSource.process(czmlPath + part.url).then(function () {
            part.loaded = true;
            updateStatusDisplay();

            // Follow the vehicle with the camera.
            if (!viewer.trackedEntity) {
              viewer.trackedEntity = vehicleEntity = dataSource.entities.getById(
                "Vehicle"
              );
            }
          });
        }

        // Load the first part up front.
        processPart(partsToLoad[0]);

        // Load a new section before the clock naturally gets there.
        // Note this can't predict when a user may fast-forward to it.
        var preloadTimeInSeconds = 100;

        viewer.clock.onTick.addEventListener(function (clock) {
          // This example uses time offsets from the start to identify which parts need loading.
          var timeOffset = Cesium.JulianDate.secondsDifference(
            clock.currentTime,
            clock.startTime
          );

          // Filter the list of parts to just the ones that need loading right now.
          // Then, process each part that needs loading.
          partsToLoad
            .filter(function (part) {
              return (
                !part.requested &&
                timeOffset >= part.range[0] - preloadTimeInSeconds &&
                timeOffset <= part.range[1]
              );
            })
            .forEach(function (part) {
              processPart(part);
            });

          if (vehicleEntity) {
            var fuel = vehicleEntity.properties.fuel_remaining.getValue(
              clock.currentTime
            );
            if (Cesium.defined(fuel)) {
              fuelDisplay.textContent = "Fuel: " + fuel.toFixed(2) + " gal";
            }
          }
        });

        // Add a reset button, for convenience.
        Sandcastle.addToolbarButton("Reset demo", function () {
          // Put things back to the starting position.
          viewer.clock.currentTime = viewer.clock.startTime;
          viewer.clock.shouldAnimate = true;

          partsToLoad.forEach(function (part) {
            part.requested = false;
            part.loaded = false;
          });

          dataSource.entities.removeAll();
          processPart(partsToLoad[0]);
        });

        // Show the status display below the reset button.
        statusDisplay.style.background = "rgba(42, 42, 42, 0.7)";
        statusDisplay.style.padding = "5px 10px";
        document.getElementById("toolbar").appendChild(statusDisplay);

        // Show a multi-part custom property being read from CZML.
        fuelDisplay.style.background = "rgba(42, 42, 42, 0.7)";
        fuelDisplay.style.padding = "5px 10px";
        fuelDisplay.style.marginTop = "5px";
        document.getElementById("toolbar").appendChild(fuelDisplay);
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>