Particle System.html 13.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!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="Particle systems." />
    <meta name="cesium-sandcastle-labels" content="Beginner, 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);

      #toolbar {
        background: rgba(42, 42, 42, 0.8);
        padding: 4px;
        border-radius: 4px;
      }

      #toolbar input {
        vertical-align: middle;
        padding-top: 2px;
        padding-bottom: 2px;
      }

      #toolbar .header {
        font-weight: bold;
      }
    </style>
    <div id="cesiumContainer" class="fullSize"></div>
    <div id="loadingOverlay"><h1>Loading...</h1></div>
    <div id="toolbar">
      <table>
        <tbody>
          <tr>
            <td>Rate</td>
            <td>
              <input
                type="range"
                min="0.0"
                max="100.0"
                step="1"
                data-bind="value: emissionRate, valueUpdate: 'input'"
              />
              <input type="text" size="5" data-bind="value: emissionRate" />
            </td>
          </tr>

          <tr>
            <td>Size</td>
            <td>
              <input
                type="range"
                min="2"
                max="60.0"
                step="1"
                data-bind="value: particleSize, valueUpdate: 'input'"
              />
              <input type="text" size="5" data-bind="value: particleSize" />
            </td>
          </tr>

          <tr>
            <td>Min Life</td>
            <td>
              <input
                type="range"
                min="0.1"
                max="30.0"
                step="1"
                data-bind="value: minimumParticleLife, valueUpdate: 'input'"
              />
              <input
                type="text"
                size="5"
                data-bind="value: minimumParticleLife"
              />
            </td>
          </tr>

          <tr>
            <td>Max Life</td>
            <td>
              <input
                type="range"
                min="0.1"
                max="30.0"
                step="1"
                data-bind="value: maximumParticleLife, valueUpdate: 'input'"
              />
              <input
                type="text"
                size="5"
                data-bind="value: maximumParticleLife"
              />
            </td>
          </tr>

          <tr>
            <td>Min Speed</td>
            <td>
              <input
                type="range"
                min="0.0"
                max="30.0"
                step="1"
                data-bind="value: minimumSpeed, valueUpdate: 'input'"
              />
              <input type="text" size="5" data-bind="value: minimumSpeed" />
            </td>
          </tr>

          <tr>
            <td>Max Speed</td>
            <td>
              <input
                type="range"
                min="0.0"
                max="30.0"
                step="1"
                data-bind="value: maximumSpeed, valueUpdate: 'input'"
              />
              <input type="text" size="5" data-bind="value: maximumSpeed" />
            </td>
          </tr>

          <tr>
            <td>Start Scale</td>
            <td>
              <input
                type="range"
                min="0.0"
                max="10.0"
                step="1"
                data-bind="value: startScale, valueUpdate: 'input'"
              />
              <input type="text" size="5" data-bind="value: startScale" />
            </td>
          </tr>

          <tr>
            <td>End Scale</td>
            <td>
              <input
                type="range"
                min="0.0"
                max="10.0"
                step="1"
                data-bind="value: endScale, valueUpdate: 'input'"
              />
              <input type="text" size="5" data-bind="value: endScale" />
            </td>
          </tr>

          <tr>
            <td>Gravity</td>
            <td>
              <input
                type="range"
                min="-20.0"
                max="20.0"
                step="1"
                data-bind="value: gravity, valueUpdate: 'input'"
              />
              <input type="text" size="5" data-bind="value: gravity" />
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <script id="cesium_sandcastle_script">
      function startup(Cesium) {
        "use strict";
        //Sandcastle_Begin
        var viewer = new Cesium.Viewer("cesiumContainer");

        //Set the random number seed for consistent results.
        Cesium.Math.setRandomNumberSeed(3);

        //Set bounds of our simulation time
        var start = Cesium.JulianDate.fromDate(new Date(2015, 2, 25, 16));
        var stop = Cesium.JulianDate.addSeconds(
          start,
          120,
          new Cesium.JulianDate()
        );

        //Make sure viewer is at the desired time.
        viewer.clock.startTime = start.clone();
        viewer.clock.stopTime = stop.clone();
        viewer.clock.currentTime = start.clone();
        viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; //Loop at the end
        viewer.clock.multiplier = 1;
        viewer.clock.shouldAnimate = true;

        //Set timeline to simulation bounds
        viewer.timeline.zoomTo(start, stop);

        var viewModel = {
          emissionRate: 5.0,
          gravity: 0.0,
          minimumParticleLife: 1.2,
          maximumParticleLife: 1.2,
          minimumSpeed: 1.0,
          maximumSpeed: 4.0,
          startScale: 1.0,
          endScale: 5.0,
          particleSize: 25.0,
        };

        Cesium.knockout.track(viewModel);
        var toolbar = document.getElementById("toolbar");
        Cesium.knockout.applyBindings(viewModel, toolbar);

        var entityPosition = new Cesium.Cartesian3();
        var entityOrientation = new Cesium.Quaternion();
        var rotationMatrix = new Cesium.Matrix3();
        var modelMatrix = new Cesium.Matrix4();

        function computeModelMatrix(entity, time) {
          return entity.computeModelMatrix(time, new Cesium.Matrix4());
        }

        var emitterModelMatrix = new Cesium.Matrix4();
        var translation = new Cesium.Cartesian3();
        var rotation = new Cesium.Quaternion();
        var hpr = new Cesium.HeadingPitchRoll();
        var trs = new Cesium.TranslationRotationScale();

        function computeEmitterModelMatrix() {
          hpr = Cesium.HeadingPitchRoll.fromDegrees(0.0, 0.0, 0.0, hpr);
          trs.translation = Cesium.Cartesian3.fromElements(
            -4.0,
            0.0,
            1.4,
            translation
          );
          trs.rotation = Cesium.Quaternion.fromHeadingPitchRoll(hpr, rotation);

          return Cesium.Matrix4.fromTranslationRotationScale(
            trs,
            emitterModelMatrix
          );
        }

        var pos1 = Cesium.Cartesian3.fromDegrees(
          -75.15787310614596,
          39.97862668312678
        );
        var pos2 = Cesium.Cartesian3.fromDegrees(
          -75.1633691390455,
          39.95355089912078
        );
        var position = new Cesium.SampledPositionProperty();

        position.addSample(start, pos1);
        position.addSample(stop, pos2);

        var entity = viewer.entities.add({
          availability: new Cesium.TimeIntervalCollection([
            new Cesium.TimeInterval({
              start: start,
              stop: stop,
            }),
          ]),
          model: {
            uri: "../../SampleData/models/CesiumMilkTruck/CesiumMilkTruck.glb",
            minimumPixelSize: 64,
          },
          viewFrom: new Cesium.Cartesian3(-100.0, 0.0, 100.0),
          position: position,
          orientation: new Cesium.VelocityOrientationProperty(position),
        });
        viewer.trackedEntity = entity;

        var scene = viewer.scene;
        var particleSystem = scene.primitives.add(
          new Cesium.ParticleSystem({
            image: "../../SampleData/smoke.png",

            startColor: Cesium.Color.LIGHTSEAGREEN.withAlpha(0.7),
            endColor: Cesium.Color.WHITE.withAlpha(0.0),

            startScale: viewModel.startScale,
            endScale: viewModel.endScale,

            minimumParticleLife: viewModel.minimumParticleLife,
            maximumParticleLife: viewModel.maximumParticleLife,

            minimumSpeed: viewModel.minimumSpeed,
            maximumSpeed: viewModel.maximumSpeed,

            imageSize: new Cesium.Cartesian2(
              viewModel.particleSize,
              viewModel.particleSize
            ),

            emissionRate: viewModel.emissionRate,

            bursts: [
              // these burst will occasionally sync to create a multicolored effect
              new Cesium.ParticleBurst({
                time: 5.0,
                minimum: 10,
                maximum: 100,
              }),
              new Cesium.ParticleBurst({
                time: 10.0,
                minimum: 50,
                maximum: 100,
              }),
              new Cesium.ParticleBurst({
                time: 15.0,
                minimum: 200,
                maximum: 300,
              }),
            ],

            lifetime: 16.0,

            emitter: new Cesium.CircleEmitter(2.0),

            emitterModelMatrix: computeEmitterModelMatrix(),

            updateCallback: applyGravity,
          })
        );

        var gravityScratch = new Cesium.Cartesian3();

        function applyGravity(p, dt) {
          // We need to compute a local up vector for each particle in geocentric space.
          var position = p.position;

          Cesium.Cartesian3.normalize(position, gravityScratch);
          Cesium.Cartesian3.multiplyByScalar(
            gravityScratch,
            viewModel.gravity * dt,
            gravityScratch
          );

          p.velocity = Cesium.Cartesian3.add(
            p.velocity,
            gravityScratch,
            p.velocity
          );
        }

        viewer.scene.preUpdate.addEventListener(function (scene, time) {
          particleSystem.modelMatrix = computeModelMatrix(entity, time);

          // Account for any changes to the emitter model matrix.
          particleSystem.emitterModelMatrix = computeEmitterModelMatrix();

          // Spin the emitter if enabled.
          if (viewModel.spin) {
            viewModel.heading += 1.0;
            viewModel.pitch += 1.0;
            viewModel.roll += 1.0;
          }
        });

        Cesium.knockout
          .getObservable(viewModel, "emissionRate")
          .subscribe(function (newValue) {
            particleSystem.emissionRate = parseFloat(newValue);
          });

        Cesium.knockout
          .getObservable(viewModel, "particleSize")
          .subscribe(function (newValue) {
            var particleSize = parseFloat(newValue);
            particleSystem.minimumImageSize.x = particleSize;
            particleSystem.minimumImageSize.y = particleSize;
            particleSystem.maximumImageSize.x = particleSize;
            particleSystem.maximumImageSize.y = particleSize;
          });

        Cesium.knockout
          .getObservable(viewModel, "minimumParticleLife")
          .subscribe(function (newValue) {
            particleSystem.minimumParticleLife = parseFloat(newValue);
          });

        Cesium.knockout
          .getObservable(viewModel, "maximumParticleLife")
          .subscribe(function (newValue) {
            particleSystem.maximumParticleLife = parseFloat(newValue);
          });

        Cesium.knockout
          .getObservable(viewModel, "minimumSpeed")
          .subscribe(function (newValue) {
            particleSystem.minimumSpeed = parseFloat(newValue);
          });

        Cesium.knockout
          .getObservable(viewModel, "maximumSpeed")
          .subscribe(function (newValue) {
            particleSystem.maximumSpeed = parseFloat(newValue);
          });

        Cesium.knockout
          .getObservable(viewModel, "startScale")
          .subscribe(function (newValue) {
            particleSystem.startScale = parseFloat(newValue);
          });

        Cesium.knockout
          .getObservable(viewModel, "endScale")
          .subscribe(function (newValue) {
            particleSystem.endScale = parseFloat(newValue);
          });

        var options = [
          {
            text: "Circle Emitter",
            onselect: function () {
              particleSystem.emitter = new Cesium.CircleEmitter(2.0);
            },
          },
          {
            text: "Sphere Emitter",
            onselect: function () {
              particleSystem.emitter = new Cesium.SphereEmitter(2.5);
            },
          },
          {
            text: "Cone Emitter",
            onselect: function () {
              particleSystem.emitter = new Cesium.ConeEmitter(
                Cesium.Math.toRadians(45.0)
              );
            },
          },
          {
            text: "Box Emitter",
            onselect: function () {
              particleSystem.emitter = new Cesium.BoxEmitter(
                new Cesium.Cartesian3(10.0, 10.0, 10.0)
              );
            },
          },
        ];

        Sandcastle.addToolbarMenu(options);
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>