-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathPointEmitter.ts
57 lines (47 loc) · 1.53 KB
/
PointEmitter.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 {EmitterShape, ShapeJSON} from './EmitterUtil';
import {Particle} from '../Particle';
import {IParticleSystem} from '../IParticleSystem';
import { Matrix4, Quaternion } from '../math';
import { UP_VEC3, ZERO_VEC3 } from '../util/MathUtil';
/**
* A point emitter emits particles from a single point.
*/
export class PointEmitter implements EmitterShape {
type = 'point';
_m1: Matrix4;
constructor() {
this._m1 = new Matrix4();
}
update(system: IParticleSystem, delta: number): void {}
initialize(p: Particle) {
const u = Math.random();
const v = Math.random();
const theta = u * Math.PI * 2;
const phi = Math.acos(2.0 * v - 1.0);
const r = Math.cbrt(Math.random());
const sinTheta = Math.sin(theta);
const cosTheta = Math.cos(theta);
const sinPhi = Math.sin(phi);
const cosPhi = Math.cos(phi);
p.velocity.x = r * sinPhi * cosTheta;
p.velocity.y = r * sinPhi * sinTheta;
p.velocity.z = r * cosPhi;
p.velocity.multiplyScalar(p.startSpeed);
p.position.setScalar(0);
if (p.rotation instanceof Quaternion) {
this._m1.lookAt(ZERO_VEC3, p.position, UP_VEC3);
p.rotation.setFromRotationMatrix(this._m1);
}
}
toJSON(): ShapeJSON {
return {
type: 'point',
};
}
static fromJSON(json: any): PointEmitter {
return new PointEmitter();
}
clone(): EmitterShape {
return new PointEmitter();
}
}