-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathOrbitOverLife.ts
57 lines (49 loc) · 1.72 KB
/
OrbitOverLife.ts
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
import {Behavior} from './Behavior';
import {Particle} from '../Particle';
import {FunctionValueGenerator, ValueGenerator, ValueGeneratorFromJSON} from '../functions';
import {Quaternion, Vector3} from '../math';
/**
* Orbit particles around an axis over their life.
*/
export class OrbitOverLife implements Behavior {
type = 'OrbitOverLife';
rotation: Quaternion;
temp = new Vector3();
constructor(
public orbitSpeed: FunctionValueGenerator | ValueGenerator,
public axis: Vector3 = new Vector3(0, 1, 0)
) {
this.rotation = new Quaternion();
}
initialize(particle: Particle): void {
this.orbitSpeed.startGen((particle as any).memory);
}
update(particle: Particle, delta: number): void {
this.temp.copy(particle.position).projectOnVector(this.axis);
this.rotation.setFromAxisAngle(
this.axis,
this.orbitSpeed.genValue((particle as any).memory, particle.age / particle.life) * delta
);
particle.position.sub(this.temp);
particle.position.applyQuaternion(this.rotation);
particle.position.add(this.temp);
}
frameUpdate(delta: number): void {}
toJSON(): any {
return {
type: this.type,
orbitSpeed: this.orbitSpeed.toJSON(),
axis: [this.axis.x, this.axis.y, this.axis.z],
};
}
static fromJSON(json: any): Behavior {
return new OrbitOverLife(
ValueGeneratorFromJSON(json.orbitSpeed) as FunctionValueGenerator,
json.axis ? new Vector3(json.axis[0], json.axis[1], json.axis[2]) : undefined
);
}
clone(): Behavior {
return new OrbitOverLife(this.orbitSpeed.clone());
}
reset(): void {}
}