@newkrok/three-particles 0.7.0 → 0.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newkrok/three-particles",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Particle system for ThreeJS",
5
5
  "main": "src/js/three-particles.js",
6
6
  "bin": {
@@ -28,7 +28,8 @@
28
28
  "dependencies": {
29
29
  "easing-functions": "1.0.1",
30
30
  "three": "0.137.0",
31
- "three-noise": "^1.1.1"
31
+ "three-noise": "^1.1.1",
32
+ "@newkrok/three-utils": "0.1.0"
32
33
  },
33
34
  "scripts": {
34
35
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -2,37 +2,9 @@ import * as THREE from "three";
2
2
 
3
3
  import { EmitFrom } from "../three-particles.js";
4
4
 
5
- export const patchObject = (
6
- objectA,
7
- objectB,
8
- config = { skippedProperties: [], applyToFirstObject: false }
9
- ) => {
10
- const result = {};
11
- Object.keys(objectA).forEach((key) => {
12
- if (!config.skippedProperties || !config.skippedProperties.includes(key)) {
13
- if (
14
- typeof objectA[key] === "object" &&
15
- objectA[key] &&
16
- objectB[key] &&
17
- !Array.isArray(objectA[key])
18
- ) {
19
- result[key] = patchObject(objectA[key], objectB[key], config);
20
- } else {
21
- result[key] =
22
- objectB[key] === 0
23
- ? 0
24
- : objectB[key] === false
25
- ? false
26
- : objectB[key] || objectA[key];
27
- if (config.applyToFirstObject) objectA[key] = result[key];
28
- }
29
- }
30
- });
31
- return result;
32
- };
33
-
34
5
  export const calculateRandomPositionAndVelocityOnSphere = (
35
6
  position,
7
+ quaternion,
36
8
  velocity,
37
9
  startSpeed,
38
10
  { radius, radiusThickness, arc }
@@ -59,6 +31,8 @@ export const calculateRandomPositionAndVelocityOnSphere = (
59
31
  radius * normalizedThickness * zDirection +
60
32
  radius * radiusThickness * randomizedDistanceRatio * zDirection;
61
33
 
34
+ position.applyQuaternion(quaternion);
35
+
62
36
  const randomizedSpeed = THREE.MathUtils.randFloat(
63
37
  startSpeed.min,
64
38
  startSpeed.max
@@ -73,6 +47,7 @@ export const calculateRandomPositionAndVelocityOnSphere = (
73
47
 
74
48
  export const calculateRandomPositionAndVelocityOnCone = (
75
49
  position,
50
+ quaternion,
76
51
  velocity,
77
52
  startSpeed,
78
53
  { radius, radiusThickness, arc, angle = 90 }
@@ -92,6 +67,8 @@ export const calculateRandomPositionAndVelocityOnCone = (
92
67
  radius * radiusThickness * randomizedDistanceRatio * yDirection;
93
68
  position.z = 0;
94
69
 
70
+ position.applyQuaternion(quaternion);
71
+
95
72
  const positionLength = position.length();
96
73
  const normalizedAngle = Math.abs(
97
74
  (positionLength / radius) * THREE.Math.degToRad(angle)
@@ -118,6 +95,7 @@ export const calculateRandomPositionAndVelocityOnCone = (
118
95
 
119
96
  export const calculateRandomPositionAndVelocityOnBox = (
120
97
  position,
98
+ quaternion,
121
99
  velocity,
122
100
  startSpeed,
123
101
  { scale, emitFrom }
@@ -157,6 +135,8 @@ export const calculateRandomPositionAndVelocityOnBox = (
157
135
  break;
158
136
  }
159
137
 
138
+ position.applyQuaternion(quaternion);
139
+
160
140
  const randomizedSpeed = THREE.MathUtils.randFloat(
161
141
  startSpeed.min,
162
142
  startSpeed.max
@@ -166,6 +146,7 @@ export const calculateRandomPositionAndVelocityOnBox = (
166
146
 
167
147
  export const calculateRandomPositionAndVelocityOnCircle = (
168
148
  position,
149
+ quaternion,
169
150
  velocity,
170
151
  startSpeed,
171
152
  { radius, radiusThickness, arc }
@@ -185,6 +166,8 @@ export const calculateRandomPositionAndVelocityOnCircle = (
185
166
  radius * radiusThickness * randomizedDistanceRatio * yDirection;
186
167
  position.z = 0;
187
168
 
169
+ position.applyQuaternion(quaternion);
170
+
188
171
  const randomizedSpeed = THREE.MathUtils.randFloat(
189
172
  startSpeed.min,
190
173
  startSpeed.max
@@ -201,6 +184,7 @@ export const calculateRandomPositionAndVelocityOnCircle = (
201
184
 
202
185
  export const calculateRandomPositionAndVelocityOnRectangle = (
203
186
  position,
187
+ quaternion,
204
188
  velocity,
205
189
  startSpeed,
206
190
  { rotation, scale }
@@ -213,6 +197,8 @@ export const calculateRandomPositionAndVelocityOnRectangle = (
213
197
  position.y = yOffset * Math.cos(rotationX);
214
198
  position.z = xOffset * Math.sin(rotationY) - yOffset * Math.sin(rotationX);
215
199
 
200
+ position.applyQuaternion(quaternion);
201
+
216
202
  const randomizedSpeed = THREE.MathUtils.randFloat(
217
203
  startSpeed.min,
218
204
  startSpeed.max
@@ -6,15 +6,16 @@ import {
6
6
  calculateRandomPositionAndVelocityOnCone,
7
7
  calculateRandomPositionAndVelocityOnRectangle,
8
8
  calculateRandomPositionAndVelocityOnSphere,
9
- patchObject,
10
9
  } from "./three-particles/three-particles-utils.js";
11
10
 
12
11
  import { CurveFunction } from "./three-particles/three-particles-curves.js";
13
12
  import { FBM } from "three-noise/build/three-noise.module.js";
13
+ import { Gyroscope } from "three/examples/jsm/misc/Gyroscope";
14
14
  import ParticleSystemFragmentShader from "./three-particles/shaders/particle-system-fragment-shader.glsl.js";
15
15
  import ParticleSystemVertexShader from "./three-particles/shaders/particle-system-vertex-shader.glsl.js";
16
16
  import { applyModifiers } from "./three-particles/three-particles-modifiers.js";
17
17
  import { createBezierCurveFunction } from "./three-particles/three-particles-bezier";
18
+ import { patchObject } from "@newkrok/three-utils/src/js/newkrok/three-utils/object-utils.js";
18
19
 
19
20
  let createdParticleSystems = [];
20
21
 
@@ -195,6 +196,7 @@ const calculatePositionAndVelocity = (
195
196
  { shape, sphere, cone, circle, rectangle, box },
196
197
  startSpeed,
197
198
  position,
199
+ quaternion,
198
200
  velocity,
199
201
  velocityOverLifetime
200
202
  ) => {
@@ -202,6 +204,7 @@ const calculatePositionAndVelocity = (
202
204
  case Shape.SPHERE:
203
205
  calculateRandomPositionAndVelocityOnSphere(
204
206
  position,
207
+ quaternion,
205
208
  velocity,
206
209
  startSpeed,
207
210
  sphere
@@ -211,6 +214,7 @@ const calculatePositionAndVelocity = (
211
214
  case Shape.CONE:
212
215
  calculateRandomPositionAndVelocityOnCone(
213
216
  position,
217
+ quaternion,
214
218
  velocity,
215
219
  startSpeed,
216
220
  cone
@@ -220,6 +224,7 @@ const calculatePositionAndVelocity = (
220
224
  case Shape.CIRCLE:
221
225
  calculateRandomPositionAndVelocityOnCircle(
222
226
  position,
227
+ quaternion,
223
228
  velocity,
224
229
  startSpeed,
225
230
  circle
@@ -229,6 +234,7 @@ const calculatePositionAndVelocity = (
229
234
  case Shape.RECTANGLE:
230
235
  calculateRandomPositionAndVelocityOnRectangle(
231
236
  position,
237
+ quaternion,
232
238
  velocity,
233
239
  startSpeed,
234
240
  rectangle
@@ -238,6 +244,7 @@ const calculatePositionAndVelocity = (
238
244
  case Shape.BOX:
239
245
  calculateRandomPositionAndVelocityOnBox(
240
246
  position,
247
+ quaternion,
241
248
  velocity,
242
249
  startSpeed,
243
250
  box
@@ -271,6 +278,7 @@ export const createParticleSystem = (
271
278
  currentWorldPosition: new THREE.Vector3(-99999),
272
279
  worldPositionChange: new THREE.Vector3(),
273
280
  worldQuaternion: new THREE.Quaternion(),
281
+ wrapperQuaternion: new THREE.Quaternion(),
274
282
  lastWorldQuaternion: new THREE.Quaternion(-99999),
275
283
  worldEuler: new THREE.Euler(),
276
284
  gravityVelocity: new THREE.Vector3(0, 0, 0),
@@ -419,6 +427,7 @@ export const createParticleSystem = (
419
427
  shape,
420
428
  startSpeed,
421
429
  startPositions[i],
430
+ generalData.wrapperQuaternion,
422
431
  velocities[i],
423
432
  velocityOverLifetime
424
433
  );
@@ -550,6 +559,7 @@ export const createParticleSystem = (
550
559
  shape,
551
560
  startSpeed,
552
561
  startPositions[particleIndex],
562
+ generalData.wrapperQuaternion,
553
563
  velocities[particleIndex],
554
564
  velocityOverLifetime
555
565
  );
@@ -579,7 +589,7 @@ export const createParticleSystem = (
579
589
  });
580
590
  };
581
591
 
582
- const particleSystem = new THREE.Points(geometry, material);
592
+ let particleSystem = new THREE.Points(geometry, material);
583
593
  particleSystem.sortParticles = true;
584
594
 
585
595
  particleSystem.position.copy(transform.position);
@@ -591,8 +601,15 @@ export const createParticleSystem = (
591
601
  const calculatedCreationTime =
592
602
  now + THREE.MathUtils.randFloat(startDelay.min, startDelay.max) * 1000;
593
603
 
604
+ let wrapper;
605
+ if (normalizedConfig.simulationSpace === SimulationSpace.WORLD) {
606
+ wrapper = new Gyroscope();
607
+ wrapper.add(particleSystem);
608
+ }
609
+
594
610
  createdParticleSystems.push({
595
611
  particleSystem,
612
+ wrapper,
596
613
  generalData,
597
614
  onUpdate,
598
615
  onComplete,
@@ -609,18 +626,26 @@ export const createParticleSystem = (
609
626
  deactivateParticle,
610
627
  activateParticle,
611
628
  });
612
- return particleSystem;
629
+
630
+ return wrapper || particleSystem;
613
631
  };
614
632
 
615
633
  export const destroyParticleSystem = (particleSystem) => {
616
634
  createdParticleSystems = createdParticleSystems.filter(
617
- ({ particleSystem: savedParticleSystem }) =>
618
- savedParticleSystem !== particleSystem
619
- );
635
+ ({ particleSystem: savedParticleSystem, wrapper }) => {
636
+ if (
637
+ savedParticleSystem !== particleSystem &&
638
+ wrapper !== particleSystem
639
+ ) {
640
+ return true;
641
+ }
620
642
 
621
- particleSystem.geometry.dispose();
622
- particleSystem.material.dispose();
623
- particleSystem.parent.remove(particleSystem);
643
+ savedParticleSystem.geometry.dispose();
644
+ savedParticleSystem.material.dispose();
645
+ savedParticleSystem.parent.remove(savedParticleSystem);
646
+ return false;
647
+ }
648
+ );
624
649
  };
625
650
 
626
651
  export const updateParticleSystems = ({ now, delta, elapsed }) => {
@@ -630,6 +655,7 @@ export const updateParticleSystems = ({ now, delta, elapsed }) => {
630
655
  generalData,
631
656
  onComplete,
632
657
  particleSystem,
658
+ wrapper,
633
659
  creationTime,
634
660
  lastEmissionTime,
635
661
  duration,
@@ -654,6 +680,8 @@ export const updateParticleSystems = ({ now, delta, elapsed }) => {
654
680
  gravityVelocity,
655
681
  } = generalData;
656
682
 
683
+ if (wrapper) generalData.wrapperQuaternion.copy(wrapper.parent.quaternion);
684
+
657
685
  const lastWorldPositionSnapshot = { ...lastWorldPosition };
658
686
 
659
687
  const lifetime = now - creationTime;