-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathChangeEmitDirection.ts
53 lines (46 loc) · 1.73 KB
/
ChangeEmitDirection.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
import {Behavior} from './Behavior';
import {Particle} from '../Particle';
import {ValueGenerator, ValueGeneratorFromJSON} from '../functions/ValueGenerator';
import {Quaternion, Vector3} from '../math';
/**
* Change the emit direction of particles.
*/
export class ChangeEmitDirection implements Behavior {
type = 'ChangeEmitDirection';
_temp: Vector3 = new Vector3();
_q: Quaternion = new Quaternion();
memory = {data: [], dataCount: 0};
constructor(public angle: ValueGenerator) {}
initialize(particle: Particle): void {
const len = particle.velocity.length();
if (len == 0) return;
particle.velocity.normalize();
if (particle.velocity.x === 0 && particle.velocity.y === 0) {
this._temp.set(0, particle.velocity.z, 0);
} else {
this._temp.set(-particle.velocity.y, particle.velocity.x, 0);
}
this.angle.startGen(this.memory);
this._q.setFromAxisAngle(this._temp.normalize(), this.angle.genValue(this.memory));
this._temp.copy(particle.velocity);
particle.velocity.applyQuaternion(this._q);
this._q.setFromAxisAngle(this._temp, Math.random() * Math.PI * 2);
particle.velocity.applyQuaternion(this._q);
particle.velocity.setLength(len);
}
update(particle: Particle, delta: number): void {}
frameUpdate(delta: number): void {}
toJSON(): any {
return {
type: this.type,
angle: this.angle.toJSON(),
};
}
static fromJSON(json: any): Behavior {
return new ChangeEmitDirection(ValueGeneratorFromJSON(json.angle) as ValueGenerator);
}
clone(): Behavior {
return new ChangeEmitDirection(this.angle);
}
reset(): void {}
}