@newkrok/three-particles 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Istvan Krisztian Somoracz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@newkrok/three-particles",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Particle system for ThreeJS",
|
|
5
|
+
"main": "src/js/three-particles.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"three-particles": "src/js/three-particles.js"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/NewKrok/three-particles.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"threejs",
|
|
15
|
+
"particles",
|
|
16
|
+
"3d",
|
|
17
|
+
"lib"
|
|
18
|
+
],
|
|
19
|
+
"author": "Istvan Krisztian Somoracz",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/NewKrok/three-particles/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/NewKrok/three-particles#readme",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const ParticleSystemFragmentShader = `
|
|
2
|
+
uniform sampler2D map;
|
|
3
|
+
uniform float elapsed;
|
|
4
|
+
uniform float fps;
|
|
5
|
+
uniform vec2 tiles;
|
|
6
|
+
|
|
7
|
+
varying vec4 vColor;
|
|
8
|
+
varying float vLifeTime;
|
|
9
|
+
varying float vRotation;
|
|
10
|
+
|
|
11
|
+
void main()
|
|
12
|
+
{
|
|
13
|
+
gl_FragColor = vColor;
|
|
14
|
+
float mid = 0.5;
|
|
15
|
+
|
|
16
|
+
float frameIndex = max(min(vLifeTime / 1000.0 * fps, tiles.x * tiles.y - 1.0), 0.0);
|
|
17
|
+
float spriteXIndex = floor(mod(frameIndex, tiles.x));
|
|
18
|
+
float spriteYIndex = floor(mod(frameIndex / tiles.y, tiles.y));
|
|
19
|
+
|
|
20
|
+
vec2 frameUV = vec2(
|
|
21
|
+
gl_PointCoord.x / tiles.x + spriteXIndex / tiles.x,
|
|
22
|
+
gl_PointCoord.y / tiles.y + spriteYIndex / tiles.y);
|
|
23
|
+
|
|
24
|
+
vec2 center = vec2(0.5, 0.5);
|
|
25
|
+
vec2 centeredPoint = gl_PointCoord - center;
|
|
26
|
+
|
|
27
|
+
mat2 rotation = mat2(
|
|
28
|
+
cos(vRotation), sin(vRotation),
|
|
29
|
+
-sin(vRotation), cos(vRotation)
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
centeredPoint = rotation * centeredPoint;
|
|
33
|
+
vec2 centeredMiddlePoint = vec2(
|
|
34
|
+
centeredPoint.x + center.x,
|
|
35
|
+
centeredPoint.y + center.y
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
float dist = distance(centeredMiddlePoint, center);
|
|
39
|
+
if (dist > 0.5) discard;
|
|
40
|
+
|
|
41
|
+
vec2 uvPoint = vec2(
|
|
42
|
+
centeredMiddlePoint.x / tiles.x + spriteXIndex / tiles.x,
|
|
43
|
+
centeredMiddlePoint.y / tiles.y + spriteYIndex / tiles.y
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
vec4 rotatedTexture = texture2D(map, uvPoint);
|
|
47
|
+
|
|
48
|
+
gl_FragColor = gl_FragColor * rotatedTexture;
|
|
49
|
+
}
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
export default ParticleSystemFragmentShader;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const ParticleSystemVertexShader = `
|
|
2
|
+
attribute float startSize;
|
|
3
|
+
attribute float colorR;
|
|
4
|
+
attribute float colorG;
|
|
5
|
+
attribute float colorB;
|
|
6
|
+
attribute float colorA;
|
|
7
|
+
attribute float lifeTime;
|
|
8
|
+
attribute float rotation;
|
|
9
|
+
|
|
10
|
+
varying mat4 vPosition;
|
|
11
|
+
varying vec4 vColor;
|
|
12
|
+
varying float vLifeTime;
|
|
13
|
+
varying float vRotation;
|
|
14
|
+
|
|
15
|
+
void main()
|
|
16
|
+
{
|
|
17
|
+
vColor = vec4(colorR, colorG, colorB, colorA);
|
|
18
|
+
vLifeTime = lifeTime;
|
|
19
|
+
vRotation = rotation;
|
|
20
|
+
|
|
21
|
+
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
22
|
+
gl_PointSize = startSize * (100.0 / length(mvPosition.xyz));
|
|
23
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
24
|
+
}
|
|
25
|
+
`
|
|
26
|
+
|
|
27
|
+
export default ParticleSystemVertexShader
|
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
import * as THREE from "three/build/three.module.js";
|
|
2
|
+
|
|
3
|
+
import ParticleSystemFragmentShader from "./shaders/particle-system-fragment-shader.glsl.js";
|
|
4
|
+
import ParticleSystemVertexShader from "./shaders/particle-system-vertex-shader.glsl.js";
|
|
5
|
+
|
|
6
|
+
// Float32Array is not enough accurate when we are storing timestamp in it so we just remove unnecessary time
|
|
7
|
+
const float32Helper = 1638200000000;
|
|
8
|
+
|
|
9
|
+
let createdParticleSystems = [];
|
|
10
|
+
|
|
11
|
+
export const SimulationSpace = {
|
|
12
|
+
LOCAL: "LOCAL",
|
|
13
|
+
WORLD: "WORLD",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const Shape = {
|
|
17
|
+
SPHERE: "SPHERE",
|
|
18
|
+
CONE: "CONE",
|
|
19
|
+
BOX: "BOX",
|
|
20
|
+
CIRCLE: "CIRCLE",
|
|
21
|
+
RECTANGLE: "RECTANGLE",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const defaultTextureSheetAnimation = {
|
|
25
|
+
tiles: new THREE.Vector2(1.0, 1.0),
|
|
26
|
+
fps: 30.0,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const createFloat32Attributes = ({
|
|
30
|
+
geometry,
|
|
31
|
+
propertyName,
|
|
32
|
+
maxParticles,
|
|
33
|
+
factory,
|
|
34
|
+
}) => {
|
|
35
|
+
geometry.setAttribute(
|
|
36
|
+
propertyName,
|
|
37
|
+
new THREE.BufferAttribute(
|
|
38
|
+
new Float32Array(
|
|
39
|
+
Array.from(
|
|
40
|
+
{ length: maxParticles },
|
|
41
|
+
typeof factory === "function" ? factory : () => factory
|
|
42
|
+
)
|
|
43
|
+
),
|
|
44
|
+
1
|
|
45
|
+
)
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const getRandomStartPositionByShape = ({ shape, radius, radiusThickness }) => {
|
|
50
|
+
const position = new THREE.Vector3();
|
|
51
|
+
switch (shape) {
|
|
52
|
+
case Shape.SPHERE: {
|
|
53
|
+
const u = Math.random();
|
|
54
|
+
const v = Math.random();
|
|
55
|
+
const randomizedDistanceRatio = Math.random();
|
|
56
|
+
const theta = 2 * Math.PI * u;
|
|
57
|
+
const phi = Math.acos(2 * v - 1);
|
|
58
|
+
const xDirection = Math.sin(phi) * Math.cos(theta);
|
|
59
|
+
const yDirection = Math.sin(phi) * Math.sin(theta);
|
|
60
|
+
const zDirection = Math.cos(phi);
|
|
61
|
+
const normalizedThickness = 1 - radiusThickness;
|
|
62
|
+
|
|
63
|
+
position.x =
|
|
64
|
+
radius * normalizedThickness * xDirection +
|
|
65
|
+
radius * radiusThickness * randomizedDistanceRatio * xDirection;
|
|
66
|
+
position.y =
|
|
67
|
+
radius * normalizedThickness * yDirection +
|
|
68
|
+
radius * radiusThickness * randomizedDistanceRatio * yDirection;
|
|
69
|
+
position.z =
|
|
70
|
+
radius * normalizedThickness * zDirection +
|
|
71
|
+
radius * radiusThickness * randomizedDistanceRatio * zDirection;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return position;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const createParticleSystem = ({
|
|
80
|
+
duration = 5.0,
|
|
81
|
+
looping = true,
|
|
82
|
+
startDelay = { min: 0.0, max: 0.0 },
|
|
83
|
+
startLifeTime = { min: 5.0, max: 5.0 },
|
|
84
|
+
startSpeed = { min: 5.0, max: 5.0 },
|
|
85
|
+
startSize = { min: 1.0, max: 1.0 },
|
|
86
|
+
startRotation = { min: 0.0, max: 0.0 },
|
|
87
|
+
startColor = {
|
|
88
|
+
min: { r: 1.0, g: 1.0, b: 1.0 },
|
|
89
|
+
max: { r: 1.0, g: 1.0, b: 1.0 },
|
|
90
|
+
},
|
|
91
|
+
startOpacity = { min: 1.0, max: 1.0 },
|
|
92
|
+
gravity = 0.0,
|
|
93
|
+
simulationSpace = SimulationSpace.LOCAL,
|
|
94
|
+
maxParticles = 100,
|
|
95
|
+
emission = {
|
|
96
|
+
rateOverTime: 10.0,
|
|
97
|
+
rateOverDistance: 0.0,
|
|
98
|
+
},
|
|
99
|
+
shape = {
|
|
100
|
+
shape: Shape.SPHERE,
|
|
101
|
+
radius: 1.0,
|
|
102
|
+
radiusThickness: 1.0,
|
|
103
|
+
arc: 360.0,
|
|
104
|
+
},
|
|
105
|
+
map,
|
|
106
|
+
onUpdate = null,
|
|
107
|
+
onComplete = null,
|
|
108
|
+
textureSheetAnimation = defaultTextureSheetAnimation,
|
|
109
|
+
}) => {
|
|
110
|
+
const now = Date.now();
|
|
111
|
+
const lastWorldPosition = new THREE.Vector3(-99999, -99999, -99999);
|
|
112
|
+
const worldPositionChange = new THREE.Vector3();
|
|
113
|
+
const generalData = { distanceFromLastEmitByDistance: 0 };
|
|
114
|
+
|
|
115
|
+
const normalizedStartDelay = { min: 0.0, max: 0.0, ...startDelay };
|
|
116
|
+
const normalizedStartLifeTime = { min: 5.0, max: 5.0, ...startLifeTime };
|
|
117
|
+
const normalizedStartSpeed = { min: 5.0, max: 5.0, ...startSpeed };
|
|
118
|
+
const normalizedStartSize = { min: 1.0, max: 1.0, ...startSize };
|
|
119
|
+
const normalizedStartRotation = { min: 0.0, max: 0.0, ...startRotation };
|
|
120
|
+
const normalizedStartColor = {
|
|
121
|
+
min: { r: 1.0, g: 1.0, b: 1.0, ...startColor.min },
|
|
122
|
+
max: { r: 1.0, g: 1.0, b: 1.0, ...startColor.max },
|
|
123
|
+
};
|
|
124
|
+
const normalizedStartOpacity = { min: 0.0, max: 0.0, ...startOpacity };
|
|
125
|
+
const normalizedEmission = {
|
|
126
|
+
rateOverTime: 10.0,
|
|
127
|
+
rateOverDistance: 0.0,
|
|
128
|
+
...emission,
|
|
129
|
+
};
|
|
130
|
+
const normalizedShape = {
|
|
131
|
+
shape: Shape.SPHERE,
|
|
132
|
+
radius: 1.0,
|
|
133
|
+
radiusThickness: 1.0,
|
|
134
|
+
arc: 360.0,
|
|
135
|
+
...shape,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const rawUniforms = {
|
|
139
|
+
...defaultTextureSheetAnimation,
|
|
140
|
+
...textureSheetAnimation,
|
|
141
|
+
};
|
|
142
|
+
rawUniforms.tiles = rawUniforms.tiles
|
|
143
|
+
? rawUniforms.tiles
|
|
144
|
+
: defaultTextureSheetAnimation.tiles;
|
|
145
|
+
|
|
146
|
+
const uniforms = Object.keys(rawUniforms).reduce(
|
|
147
|
+
(prev, key) => ({
|
|
148
|
+
...prev,
|
|
149
|
+
[key]: { value: rawUniforms[key] },
|
|
150
|
+
}),
|
|
151
|
+
{}
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const material = new THREE.ShaderMaterial({
|
|
155
|
+
uniforms: {
|
|
156
|
+
elapsed: {
|
|
157
|
+
value: 0.0,
|
|
158
|
+
},
|
|
159
|
+
map: {
|
|
160
|
+
value: map,
|
|
161
|
+
},
|
|
162
|
+
...uniforms,
|
|
163
|
+
},
|
|
164
|
+
vertexShader: ParticleSystemVertexShader,
|
|
165
|
+
fragmentShader: ParticleSystemFragmentShader,
|
|
166
|
+
transparent: true,
|
|
167
|
+
blending: THREE.AdditiveBlending,
|
|
168
|
+
depthTest: true,
|
|
169
|
+
depthWrite: false,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const geometry = new THREE.BufferGeometry();
|
|
173
|
+
|
|
174
|
+
const startPosition = getRandomStartPositionByShape(normalizedShape);
|
|
175
|
+
geometry.setFromPoints(
|
|
176
|
+
Array.from({ length: maxParticles }, () => ({ ...startPosition }))
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
const createFloat32AttributesRequest = (propertyName, factory) => {
|
|
180
|
+
createFloat32Attributes({
|
|
181
|
+
geometry,
|
|
182
|
+
propertyName,
|
|
183
|
+
maxParticles,
|
|
184
|
+
factory,
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
createFloat32AttributesRequest("isActive", false);
|
|
189
|
+
createFloat32AttributesRequest("creationTime", 0);
|
|
190
|
+
createFloat32AttributesRequest("lifeTime", 0);
|
|
191
|
+
createFloat32AttributesRequest("startLifeTime", () =>
|
|
192
|
+
THREE.MathUtils.randFloat(
|
|
193
|
+
normalizedStartLifeTime.min,
|
|
194
|
+
normalizedStartLifeTime.max
|
|
195
|
+
)
|
|
196
|
+
);
|
|
197
|
+
const randomizedSpeed = THREE.MathUtils.randFloat(
|
|
198
|
+
normalizedStartSpeed.min,
|
|
199
|
+
normalizedStartSpeed.max
|
|
200
|
+
);
|
|
201
|
+
const speedMultiplierByPosition = startPosition.length() / 1;
|
|
202
|
+
createFloat32AttributesRequest(
|
|
203
|
+
"velocityX",
|
|
204
|
+
() => startPosition.x * speedMultiplierByPosition * randomizedSpeed
|
|
205
|
+
);
|
|
206
|
+
createFloat32AttributesRequest(
|
|
207
|
+
"velocityY",
|
|
208
|
+
() => startPosition.y * speedMultiplierByPosition * randomizedSpeed
|
|
209
|
+
);
|
|
210
|
+
createFloat32AttributesRequest(
|
|
211
|
+
"velocityZ",
|
|
212
|
+
() => startPosition.z * speedMultiplierByPosition * randomizedSpeed
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
createFloat32AttributesRequest("opacity", 0);
|
|
216
|
+
|
|
217
|
+
createFloat32Attributes({
|
|
218
|
+
geometry,
|
|
219
|
+
propertyName: "rotation",
|
|
220
|
+
maxParticles,
|
|
221
|
+
factory: () =>
|
|
222
|
+
THREE.Math.degToRad(
|
|
223
|
+
THREE.MathUtils.randFloat(
|
|
224
|
+
normalizedStartRotation.min,
|
|
225
|
+
normalizedStartRotation.max
|
|
226
|
+
)
|
|
227
|
+
),
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
createFloat32Attributes({
|
|
231
|
+
geometry,
|
|
232
|
+
propertyName: "startSize",
|
|
233
|
+
maxParticles,
|
|
234
|
+
factory: () =>
|
|
235
|
+
THREE.MathUtils.randFloat(
|
|
236
|
+
normalizedStartSize.min,
|
|
237
|
+
normalizedStartSize.max
|
|
238
|
+
),
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
createFloat32AttributesRequest("rotation", 0);
|
|
242
|
+
|
|
243
|
+
const colorRandomRatio = Math.random();
|
|
244
|
+
createFloat32AttributesRequest(
|
|
245
|
+
"colorR",
|
|
246
|
+
() =>
|
|
247
|
+
normalizedStartColor.min.r +
|
|
248
|
+
colorRandomRatio *
|
|
249
|
+
(normalizedStartColor.max.r - normalizedStartColor.min.r)
|
|
250
|
+
);
|
|
251
|
+
createFloat32AttributesRequest(
|
|
252
|
+
"colorG",
|
|
253
|
+
() =>
|
|
254
|
+
normalizedStartColor.min.g +
|
|
255
|
+
colorRandomRatio *
|
|
256
|
+
(normalizedStartColor.max.g - normalizedStartColor.min.g)
|
|
257
|
+
);
|
|
258
|
+
createFloat32AttributesRequest(
|
|
259
|
+
"colorB",
|
|
260
|
+
() =>
|
|
261
|
+
normalizedStartColor.min.b +
|
|
262
|
+
colorRandomRatio *
|
|
263
|
+
(normalizedStartColor.max.b - normalizedStartColor.min.b)
|
|
264
|
+
);
|
|
265
|
+
createFloat32AttributesRequest("colorA", 0);
|
|
266
|
+
|
|
267
|
+
const deactivateParticle = (particleIndex) => {
|
|
268
|
+
geometry.attributes.isActive.array[particleIndex] = false;
|
|
269
|
+
geometry.attributes.lifeTime.array[particleIndex] = 0;
|
|
270
|
+
geometry.attributes.lifeTime.needsUpdate = true;
|
|
271
|
+
geometry.attributes.colorA.array[particleIndex] = 0;
|
|
272
|
+
geometry.attributes.colorA.needsUpdate = true;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const activateParticle = ({ particleIndex, activationTime }) => {
|
|
276
|
+
geometry.attributes.isActive.array[particleIndex] = true;
|
|
277
|
+
geometry.attributes.creationTime.array[particleIndex] =
|
|
278
|
+
activationTime - float32Helper;
|
|
279
|
+
|
|
280
|
+
const colorRandomRatio = Math.random();
|
|
281
|
+
|
|
282
|
+
geometry.attributes.colorR.array[particleIndex] =
|
|
283
|
+
normalizedStartColor.min.r +
|
|
284
|
+
colorRandomRatio *
|
|
285
|
+
(normalizedStartColor.max.r - normalizedStartColor.min.r);
|
|
286
|
+
geometry.attributes.colorR.needsUpdate = true;
|
|
287
|
+
|
|
288
|
+
geometry.attributes.colorG.array[particleIndex] =
|
|
289
|
+
normalizedStartColor.min.g +
|
|
290
|
+
colorRandomRatio *
|
|
291
|
+
(normalizedStartColor.max.g - normalizedStartColor.min.g);
|
|
292
|
+
geometry.attributes.colorG.needsUpdate = true;
|
|
293
|
+
|
|
294
|
+
geometry.attributes.colorB.array[particleIndex] =
|
|
295
|
+
normalizedStartColor.min.b +
|
|
296
|
+
colorRandomRatio *
|
|
297
|
+
(normalizedStartColor.max.b - normalizedStartColor.min.b);
|
|
298
|
+
geometry.attributes.colorB.needsUpdate = true;
|
|
299
|
+
|
|
300
|
+
geometry.attributes.colorA.array[particleIndex] = THREE.MathUtils.randFloat(
|
|
301
|
+
normalizedStartOpacity.min,
|
|
302
|
+
normalizedStartOpacity.max
|
|
303
|
+
);
|
|
304
|
+
geometry.attributes.colorA.needsUpdate = true;
|
|
305
|
+
|
|
306
|
+
geometry.attributes.startLifeTime.array[particleIndex] =
|
|
307
|
+
THREE.MathUtils.randFloat(
|
|
308
|
+
normalizedStartLifeTime.min,
|
|
309
|
+
normalizedStartLifeTime.max
|
|
310
|
+
) * 1000;
|
|
311
|
+
geometry.attributes.startLifeTime.needsUpdate = true;
|
|
312
|
+
|
|
313
|
+
geometry.attributes.startSize.array[particleIndex] =
|
|
314
|
+
THREE.MathUtils.randFloat(
|
|
315
|
+
normalizedStartSize.min,
|
|
316
|
+
normalizedStartSize.max
|
|
317
|
+
);
|
|
318
|
+
geometry.attributes.startSize.needsUpdate = true;
|
|
319
|
+
|
|
320
|
+
geometry.attributes.rotation.array[particleIndex] = THREE.Math.degToRad(
|
|
321
|
+
THREE.MathUtils.randFloat(
|
|
322
|
+
normalizedStartRotation.min,
|
|
323
|
+
normalizedStartRotation.max
|
|
324
|
+
)
|
|
325
|
+
);
|
|
326
|
+
geometry.attributes.rotation.needsUpdate = true;
|
|
327
|
+
|
|
328
|
+
const startPosition = getRandomStartPositionByShape(normalizedShape);
|
|
329
|
+
geometry.attributes.position.array[Math.floor(particleIndex * 3)] =
|
|
330
|
+
startPosition.x;
|
|
331
|
+
geometry.attributes.position.array[Math.floor(particleIndex * 3) + 1] =
|
|
332
|
+
startPosition.y;
|
|
333
|
+
geometry.attributes.position.array[Math.floor(particleIndex * 3) + 2] =
|
|
334
|
+
startPosition.z;
|
|
335
|
+
particleSystem.geometry.attributes.position.needsUpdate = true;
|
|
336
|
+
|
|
337
|
+
const randomizedSpeed = THREE.MathUtils.randFloat(
|
|
338
|
+
normalizedStartSpeed.min,
|
|
339
|
+
normalizedStartSpeed.max
|
|
340
|
+
);
|
|
341
|
+
const speedMultiplierByPosition = 1 / startPosition.length();
|
|
342
|
+
geometry.attributes.velocityX.array[particleIndex] =
|
|
343
|
+
startPosition.x * speedMultiplierByPosition * randomizedSpeed;
|
|
344
|
+
geometry.attributes.velocityY.array[particleIndex] =
|
|
345
|
+
startPosition.y * speedMultiplierByPosition * randomizedSpeed;
|
|
346
|
+
geometry.attributes.velocityZ.array[particleIndex] =
|
|
347
|
+
startPosition.z * speedMultiplierByPosition * randomizedSpeed;
|
|
348
|
+
|
|
349
|
+
geometry.attributes.lifeTime.array[particleIndex] = 0;
|
|
350
|
+
geometry.attributes.lifeTime.needsUpdate = true;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
const particleSystem = new THREE.Points(geometry, material);
|
|
354
|
+
particleSystem.sortParticles = true;
|
|
355
|
+
|
|
356
|
+
const calculatedCreationTime =
|
|
357
|
+
now +
|
|
358
|
+
THREE.MathUtils.randFloat(
|
|
359
|
+
normalizedStartDelay.min,
|
|
360
|
+
normalizedStartDelay.max
|
|
361
|
+
) *
|
|
362
|
+
1000;
|
|
363
|
+
|
|
364
|
+
createdParticleSystems.push({
|
|
365
|
+
particleSystem,
|
|
366
|
+
generalData,
|
|
367
|
+
lastWorldPosition,
|
|
368
|
+
worldPositionChange,
|
|
369
|
+
onUpdate,
|
|
370
|
+
onComplete,
|
|
371
|
+
creationTime: calculatedCreationTime,
|
|
372
|
+
lastEmissionTime: calculatedCreationTime,
|
|
373
|
+
duration,
|
|
374
|
+
looping,
|
|
375
|
+
simulationSpace,
|
|
376
|
+
gravity,
|
|
377
|
+
emission: normalizedEmission,
|
|
378
|
+
iterationCount: 0,
|
|
379
|
+
deactivateParticle,
|
|
380
|
+
activateParticle,
|
|
381
|
+
});
|
|
382
|
+
return particleSystem;
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
export const destroyParticleSystem = (particleSystem) => {
|
|
386
|
+
createdParticleSystems = createdParticleSystems.filter(
|
|
387
|
+
({ particleSystem: savedParticleSystem }) =>
|
|
388
|
+
savedParticleSystem !== particleSystem
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
particleSystem.geometry.dispose();
|
|
392
|
+
particleSystem.material.dispose();
|
|
393
|
+
particleSystem.parent.remove(particleSystem);
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
export const updateParticleSystems = ({ delta, elapsed }) => {
|
|
397
|
+
const now = Date.now();
|
|
398
|
+
createdParticleSystems.forEach((props) => {
|
|
399
|
+
const {
|
|
400
|
+
onUpdate,
|
|
401
|
+
generalData,
|
|
402
|
+
lastWorldPosition,
|
|
403
|
+
worldPositionChange,
|
|
404
|
+
onComplete,
|
|
405
|
+
particleSystem,
|
|
406
|
+
creationTime,
|
|
407
|
+
lastEmissionTime,
|
|
408
|
+
duration,
|
|
409
|
+
looping,
|
|
410
|
+
emission,
|
|
411
|
+
iterationCount,
|
|
412
|
+
deactivateParticle,
|
|
413
|
+
activateParticle,
|
|
414
|
+
simulationSpace,
|
|
415
|
+
gravity,
|
|
416
|
+
} = props;
|
|
417
|
+
const lifeTime = now - creationTime;
|
|
418
|
+
particleSystem.material.uniforms.elapsed.value = elapsed;
|
|
419
|
+
|
|
420
|
+
if (
|
|
421
|
+
lastWorldPosition.x !== -99999 &&
|
|
422
|
+
lastWorldPosition.y !== -99999 &&
|
|
423
|
+
lastWorldPosition.z !== -99999
|
|
424
|
+
)
|
|
425
|
+
worldPositionChange.set(
|
|
426
|
+
particleSystem.position.x - lastWorldPosition.x,
|
|
427
|
+
particleSystem.position.y - lastWorldPosition.y,
|
|
428
|
+
particleSystem.position.z - lastWorldPosition.z
|
|
429
|
+
);
|
|
430
|
+
generalData.distanceFromLastEmitByDistance += worldPositionChange.length();
|
|
431
|
+
lastWorldPosition.copy(particleSystem.position);
|
|
432
|
+
|
|
433
|
+
particleSystem.geometry.attributes.creationTime.array.forEach(
|
|
434
|
+
(entry, index) => {
|
|
435
|
+
if (particleSystem.geometry.attributes.isActive.array[index]) {
|
|
436
|
+
const particleLifeTime = now - float32Helper - entry;
|
|
437
|
+
if (
|
|
438
|
+
particleLifeTime >
|
|
439
|
+
particleSystem.geometry.attributes.startLifeTime.array[index]
|
|
440
|
+
)
|
|
441
|
+
deactivateParticle(index);
|
|
442
|
+
else {
|
|
443
|
+
const accelerationX =
|
|
444
|
+
particleSystem.geometry.attributes.velocityX.array[index];
|
|
445
|
+
|
|
446
|
+
const accelerationY =
|
|
447
|
+
particleSystem.geometry.attributes.velocityY.array[index] -
|
|
448
|
+
gravity;
|
|
449
|
+
particleSystem.geometry.attributes.velocityY.array[index] =
|
|
450
|
+
accelerationY;
|
|
451
|
+
|
|
452
|
+
const accelerationZ =
|
|
453
|
+
particleSystem.geometry.attributes.velocityZ.array[index];
|
|
454
|
+
|
|
455
|
+
if (gravity !== 0 || accelerationX !== 0 || accelerationY !== 0) {
|
|
456
|
+
const positionArr =
|
|
457
|
+
particleSystem.geometry.attributes.position.array;
|
|
458
|
+
if (simulationSpace === SimulationSpace.WORLD) {
|
|
459
|
+
positionArr[index * 3] -= worldPositionChange.x;
|
|
460
|
+
positionArr[index * 3 + 1] -= worldPositionChange.y;
|
|
461
|
+
positionArr[index * 3 + 2] -= worldPositionChange.z;
|
|
462
|
+
}
|
|
463
|
+
positionArr[index * 3] += accelerationX * delta;
|
|
464
|
+
positionArr[index * 3 + 1] += accelerationY * delta;
|
|
465
|
+
positionArr[index * 3 + 2] += accelerationZ * delta;
|
|
466
|
+
particleSystem.geometry.attributes.position.needsUpdate = true;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
particleSystem.geometry.attributes.lifeTime.array[index] =
|
|
470
|
+
particleLifeTime;
|
|
471
|
+
particleSystem.geometry.attributes.lifeTime.needsUpdate = true;
|
|
472
|
+
|
|
473
|
+
// TEMP
|
|
474
|
+
particleSystem.geometry.attributes.colorA.array[index] =
|
|
475
|
+
1 -
|
|
476
|
+
particleLifeTime /
|
|
477
|
+
particleSystem.geometry.attributes.startLifeTime.array[index];
|
|
478
|
+
particleSystem.geometry.attributes.colorA.needsUpdate = true;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
if (looping || lifeTime < duration * 1000) {
|
|
485
|
+
const emissionDelta = now - lastEmissionTime;
|
|
486
|
+
const neededParticlesByTime = Math.floor(
|
|
487
|
+
emission.rateOverTime * (emissionDelta / 1000)
|
|
488
|
+
);
|
|
489
|
+
const neededParticlesByDistance =
|
|
490
|
+
emission.rateOverDistance > 0 &&
|
|
491
|
+
generalData.distanceFromLastEmitByDistance > 0
|
|
492
|
+
? Math.floor(
|
|
493
|
+
generalData.distanceFromLastEmitByDistance /
|
|
494
|
+
(1 / emission.rateOverDistance)
|
|
495
|
+
)
|
|
496
|
+
: 0;
|
|
497
|
+
const neededParticles = neededParticlesByTime + neededParticlesByDistance;
|
|
498
|
+
|
|
499
|
+
if (emission.rateOverDistance > 0 && neededParticlesByDistance >= 1)
|
|
500
|
+
generalData.distanceFromLastEmitByDistance = 0;
|
|
501
|
+
|
|
502
|
+
if (neededParticles > 0) {
|
|
503
|
+
for (let i = 0; i < neededParticles; i++) {
|
|
504
|
+
let particleIndex = -1;
|
|
505
|
+
particleSystem.geometry.attributes.isActive.array.find(
|
|
506
|
+
(isActive, index) => {
|
|
507
|
+
if (!isActive) {
|
|
508
|
+
particleIndex = index;
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
if (
|
|
517
|
+
particleIndex !== -1 &&
|
|
518
|
+
particleIndex <
|
|
519
|
+
particleSystem.geometry.attributes.isActive.array.length
|
|
520
|
+
) {
|
|
521
|
+
activateParticle({ particleIndex, activationTime: now });
|
|
522
|
+
props.lastEmissionTime = now;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (onUpdate)
|
|
528
|
+
onUpdate({
|
|
529
|
+
particleSystem,
|
|
530
|
+
delta,
|
|
531
|
+
elapsed,
|
|
532
|
+
lifeTime,
|
|
533
|
+
iterationCount: iterationCount + 1,
|
|
534
|
+
});
|
|
535
|
+
} else if (onComplete)
|
|
536
|
+
onComplete({
|
|
537
|
+
particleSystem,
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
};
|