-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSphereEmitter.ts
135 lines (122 loc) · 4.1 KB
/
SphereEmitter.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
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
import {EmitterMode, EmitterShape, getValueFromEmitterMode, ShapeJSON} from './EmitterUtil';
import {Particle} from '../Particle';
import {MathUtils, Matrix3, Matrix4, Quaternion, Vector3} from '../math';
import {
ConstantValue,
FunctionValueGenerator,
GeneratorMemory,
ValueGenerator,
ValueGeneratorFromJSON,
} from '../functions';
import {EmissionState, IParticleSystem} from '../IParticleSystem';
import { UP_VEC3, ZERO_VEC3 } from '../util/MathUtil';
/**
* Interface representing the parameters for a sphere emitter.
*/
export interface SphereEmitterParameters {
/**
* The radius of the sphere.
*/
radius?: number;
/**
* The arc of the sphere.
*/
arc?: number;
/**
* The thickness of the sphere. 1 is a full sphere, 0 is a thin shell.
*/
thickness?: number;
/**
* The mode of the emitter.
* {@link EmitterMode}
*/
mode?: EmitterMode;
/**
* The length of the segment at which the emitter point converges at the start and end, when mode is EmitterMode.Loop or EmitterMode.PingPong.
* {@link EmitterMode}
*/
spread?: number;
/**
* The speed of the emitter start point when mode is EmitterMode.Loop or EmitterMode.PingPong.
* {@link EmitterMode}
*/
speed?: ValueGenerator | FunctionValueGenerator;
}
export class SphereEmitter implements EmitterShape {
type = 'sphere';
radius: number;
arc: number;
thickness: number; //[0, 1]
mode: EmitterMode;
spread: number;
speed: ValueGenerator | FunctionValueGenerator;
memory: GeneratorMemory;
_m1: Matrix4;
constructor(parameters: SphereEmitterParameters = {}) {
this.radius = parameters.radius ?? 10;
this.arc = parameters.arc ?? 2.0 * Math.PI;
this.thickness = parameters.thickness ?? 1;
this.mode = parameters.mode ?? EmitterMode.Random;
this.spread = parameters.spread ?? 0;
this.speed = parameters.speed ?? new ConstantValue(1);
this.memory = [];
this._m1 = new Matrix4();
}
private currentValue = 0;
update(system: IParticleSystem, delta: number): void {
if (EmitterMode.Random != this.mode) {
this.currentValue += this.speed.genValue(this.memory, system.emissionState.time / system.duration) * delta;
}
}
initialize(p: Particle, emissionState: EmissionState) {
const u = getValueFromEmitterMode(this.mode, this.currentValue, this.spread, emissionState);
const v = Math.random();
const rand = MathUtils.lerp(1 - this.thickness, 1, Math.random());
const theta = u * this.arc;
const phi = Math.acos(2.0 * v - 1.0);
const sinTheta = Math.sin(theta);
const cosTheta = Math.cos(theta);
const sinPhi = Math.sin(phi);
const cosPhi = Math.cos(phi);
p.position.x = sinPhi * cosTheta;
p.position.y = sinPhi * sinTheta;
p.position.z = cosPhi;
p.velocity.copy(p.position).multiplyScalar(p.startSpeed);
p.position.multiplyScalar(this.radius * rand);
if (p.rotation instanceof Quaternion) {
this._m1.lookAt(ZERO_VEC3, p.position, UP_VEC3);
p.rotation.setFromRotationMatrix(this._m1);
}
}
toJSON(): ShapeJSON {
return {
type: 'sphere',
radius: this.radius,
arc: this.arc,
thickness: this.thickness,
mode: this.mode,
spread: this.spread,
speed: this.speed.toJSON(),
};
}
static fromJSON(json: any): SphereEmitter {
return new SphereEmitter({
radius: json.radius,
arc: json.arc,
thickness: json.thickness,
mode: json.mode,
speed: json.speed ? ValueGeneratorFromJSON(json.speed) : undefined,
spread: json.spread,
});
}
clone(): EmitterShape {
return new SphereEmitter({
radius: this.radius,
arc: this.arc,
thickness: this.thickness,
mode: this.mode,
speed: this.speed.clone(),
spread: this.spread,
});
}
}