yzt
2023-05-26 de4278af2fd46705a40bac58ec01122db6b7f3d7
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
import { Cartesian2 } from "../../Source/Cesium.js";
import { Cartesian3 } from "../../Source/Cesium.js";
import { Color } from "../../Source/Cesium.js";
import { Particle } from "../../Source/Cesium.js";
 
describe("Scene/Particle", function () {
  it("default constructor", function () {
    var p = new Particle();
    expect(p.mass).toEqual(1.0);
    expect(p.position).toEqual(Cartesian3.ZERO);
    expect(p.velocity).toEqual(Cartesian3.ZERO);
    expect(p.life).toEqual(Number.MAX_VALUE);
    expect(p.image).toBeUndefined();
    expect(p.startColor).toEqual(Color.WHITE);
    expect(p.endColor).toEqual(Color.WHITE);
    expect(p.startScale).toEqual(1.0);
    expect(p.endScale).toEqual(1.0);
    expect(p.imageSize).toEqual(new Cartesian2(1.0, 1.0));
  });
 
  it("constructor", function () {
    var options = {
      mass: 10.0,
      position: new Cartesian3(1.0, 2.0, 3.0),
      velocity: new Cartesian3(4.0, 5.0, 6.0),
      life: 15.0,
      image: "url/to/image",
      startColor: Color.MAGENTA,
      endColor: Color.LIME,
      startScale: 0.5,
      endScale: 20.0,
      imageSize: new Cartesian2(7.0, 8.0),
    };
    var p = new Particle(options);
    expect(p.mass).toEqual(options.mass);
    expect(p.position).toEqual(options.position);
    expect(p.velocity).toEqual(options.velocity);
    expect(p.life).toEqual(options.life);
    expect(p.image).toEqual(options.image);
    expect(p.startColor).toEqual(options.startColor);
    expect(p.endColor).toEqual(options.endColor);
    expect(p.startScale).toEqual(options.startScale);
    expect(p.endScale).toEqual(options.endScale);
    expect(p.imageSize).toEqual(options.imageSize);
  });
 
  it("update without update function", function () {
    var position = new Cartesian3(1.0, 2.0, 3.0);
    var velocity = Cartesian3.normalize(
      new Cartesian3(-1.0, 1.0, 1.0),
      new Cartesian3()
    );
    var p = new Particle({
      life: 15.0,
      position: position,
      velocity: velocity,
    });
 
    var dt = 10.0;
    var expectedPosition = Cartesian3.add(
      p.position,
      Cartesian3.multiplyByScalar(p.velocity, dt, new Cartesian3()),
      new Cartesian3()
    );
 
    expect(p.update(dt)).toEqual(true);
    expect(p.position).toEqual(expectedPosition);
    expect(p.velocity).toEqual(velocity);
    expect(p.age).toEqual(dt);
    expect(p.normalizedAge).toEqual(dt / p.life);
    expect(p.update(dt)).toEqual(false);
  });
 
  it("update with updateFunction", function () {
    var increaseMass = function (particle, dt) {
      particle.mass++;
    };
    var forces = increaseMass;
 
    var position = new Cartesian3(1.0, 2.0, 3.0);
    var velocity = Cartesian3.normalize(
      new Cartesian3(-1.0, 1.0, 1.0),
      new Cartesian3()
    );
    var p = new Particle({
      life: 15.0,
      position: position,
      velocity: velocity,
    });
 
    var dt = 10.0;
    var expectedMass = p.mass + 1;
    var expectedPosition = Cartesian3.add(
      p.position,
      Cartesian3.multiplyByScalar(p.velocity, dt, new Cartesian3()),
      new Cartesian3()
    );
 
    expect(p.update(dt, forces)).toEqual(true);
    expect(p.position).toEqual(expectedPosition);
    expect(p.velocity).toEqual(velocity);
    expect(p.age).toEqual(dt);
    expect(p.normalizedAge).toEqual(dt / p.life);
    expect(p.mass).toEqual(expectedMass);
    expect(p.update(dt)).toEqual(false);
  });
});