@newkrok/three-particles 0.4.1 → 0.5.0

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/README.md CHANGED
@@ -18,4 +18,4 @@ Install with npm
18
18
  `npm i @newkrok/three-particles`
19
19
 
20
20
  Add as a package.json dependency
21
- `"dependencies": { ... "@newkrok/three-particles": "0.4.0" ... }, `
21
+ `"dependencies": { ... "@newkrok/three-particles": "0.5.0" ... }, `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newkrok/three-particles",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Particle system for ThreeJS",
5
5
  "main": "src/js/three-particles.js",
6
6
  "bin": {
@@ -0,0 +1,36 @@
1
+ const nCr = (n, k) => {
2
+ let z = 1;
3
+ for (let i = 1; i <= k; i++) z *= (n + 1 - i) / i;
4
+ return z;
5
+ };
6
+
7
+ export const createBezierCurveFunction = (bezierPoints) => (percentage) => {
8
+ if (percentage < 0) return bezierPoints[0];
9
+ if (percentage > 1) return bezierPoints[bezierPoints.length - 1];
10
+
11
+ let start = 0;
12
+ let stop = bezierPoints.length - 1;
13
+
14
+ bezierPoints.find((point, index) => {
15
+ const result = percentage < point.percentage;
16
+ if (result) stop = index;
17
+ else if (point.percentage !== undefined) start = index;
18
+ return result;
19
+ });
20
+
21
+ const n = stop - start;
22
+ const calculatedPercentage =
23
+ (percentage - bezierPoints[start].percentage) /
24
+ (bezierPoints[stop].percentage - bezierPoints[start].percentage);
25
+
26
+ let value = 0;
27
+ for (let i = 0; i <= n; i++) {
28
+ const p = bezierPoints[start + i];
29
+ const c =
30
+ nCr(n, i) *
31
+ Math.pow(1 - calculatedPercentage, n - i) *
32
+ Math.pow(calculatedPercentage, i);
33
+ value += c * p.y;
34
+ }
35
+ return value;
36
+ };
@@ -1,6 +1,7 @@
1
1
  import Easing from "easing-functions";
2
2
 
3
3
  export const CurveFunction = {
4
+ BEZIER: "BEZIER",
4
5
  LINEAR: "LINEAR",
5
6
  QUADRATIC_IN: "QUADRATIC_IN",
6
7
  QUADRATIC_OUT: "QUADRATIC_OUT",
@@ -1,7 +1,6 @@
1
1
  import * as THREE from "three/build/three.module.js";
2
2
 
3
3
  import { getCurveFunction } from "./three-particles-curves.js";
4
- import { size } from "lodash";
5
4
 
6
5
  const noiseInput = new THREE.Vector3(0, 0, 0);
7
6
 
@@ -8,7 +8,12 @@ export const patchObject = (
8
8
  const result = {};
9
9
  Object.keys(objectA).forEach((key) => {
10
10
  if (!config.skippedProperties || !config.skippedProperties.includes(key)) {
11
- if (typeof objectA[key] === "object" && objectA[key] && objectB[key]) {
11
+ if (
12
+ typeof objectA[key] === "object" &&
13
+ objectA[key] &&
14
+ objectB[key] &&
15
+ !Array.isArray(objectA[key])
16
+ ) {
12
17
  result[key] = patchObject(objectA[key], objectB[key], config);
13
18
  } else {
14
19
  result[key] =
@@ -13,6 +13,7 @@ import { FBM } from "three-noise/build/three-noise.module.js";
13
13
  import ParticleSystemFragmentShader from "./three-particles/shaders/particle-system-fragment-shader.glsl.js";
14
14
  import ParticleSystemVertexShader from "./three-particles/shaders/particle-system-vertex-shader.glsl.js";
15
15
  import { applyModifiers } from "./three-particles/three-particles-modifiers.js";
16
+ import { createBezierCurveFunction } from "@newkrok/three-particles/src/js/effects/three-particles/three-particles-bezier";
16
17
 
17
18
  let createdParticleSystems = [];
18
19
 
@@ -115,7 +116,11 @@ const DEFAULT_PARTICLE_SYSTEM_CONFIG = {
115
116
  },
116
117
  sizeOverLifetime: {
117
118
  isActive: false,
118
- curveFunction: CurveFunction.LINEAR,
119
+ curveFunction: CurveFunction.BEZIER,
120
+ bezierPoints: [
121
+ { x: 0, y: 0, percentage: 0 },
122
+ { x: 1, y: 1, percentage: 1 },
123
+ ],
119
124
  },
120
125
  /* colorOverLifetime: {
121
126
  isActive: false,
@@ -123,7 +128,11 @@ const DEFAULT_PARTICLE_SYSTEM_CONFIG = {
123
128
  }, */
124
129
  opacityOverLifetime: {
125
130
  isActive: false,
126
- curveFunction: CurveFunction.LINEAR,
131
+ curveFunction: CurveFunction.BEZIER,
132
+ bezierPoints: [
133
+ { x: 0, y: 0, percentage: 0 },
134
+ { x: 1, y: 1, percentage: 1 },
135
+ ],
127
136
  },
128
137
  rotationOverLifetime: {
129
138
  isActive: false,
@@ -249,6 +258,22 @@ export const createParticleSystem = (
249
258
  };
250
259
 
251
260
  const normalizedConfig = patchObject(DEFAULT_PARTICLE_SYSTEM_CONFIG, config);
261
+
262
+ const bezierCompatibleProperties = [
263
+ "sizeOverLifetime",
264
+ "opacityOverLifetime",
265
+ ];
266
+ bezierCompatibleProperties.forEach((key) => {
267
+ if (
268
+ normalizedConfig[key].isActive &&
269
+ normalizedConfig[key].curveFunction === CurveFunction.BEZIER &&
270
+ normalizedConfig[key].bezierPoints
271
+ )
272
+ normalizedConfig[key].curveFunction = createBezierCurveFunction(
273
+ normalizedConfig[key].bezierPoints
274
+ );
275
+ });
276
+
252
277
  const {
253
278
  transform,
254
279
  duration,
@@ -600,12 +625,13 @@ export const updateParticleSystems = ({ now, delta, elapsed }) => {
600
625
  particleSystem.material.uniforms.elapsed.value = elapsed;
601
626
 
602
627
  particleSystem.getWorldPosition(currentWorldPosition);
603
- if (lastWorldPosition.x !== -99999)
628
+ if (lastWorldPosition.x !== -99999) {
604
629
  worldPositionChange.set(
605
630
  currentWorldPosition.x - lastWorldPosition.x,
606
631
  currentWorldPosition.y - lastWorldPosition.y,
607
632
  currentWorldPosition.z - lastWorldPosition.z
608
633
  );
634
+ }
609
635
  generalData.distanceFromLastEmitByDistance += worldPositionChange.length();
610
636
  particleSystem.getWorldPosition(lastWorldPosition);
611
637
 
@@ -648,11 +674,15 @@ export const updateParticleSystems = ({ now, delta, elapsed }) => {
648
674
  gravity !== 0 ||
649
675
  velocity.x !== 0 ||
650
676
  velocity.y !== 0 ||
651
- velocity.z !== 0
677
+ velocity.z !== 0 ||
678
+ worldPositionChange.x !== 0 ||
679
+ worldPositionChange.y !== 0 ||
680
+ worldPositionChange.z !== 0
652
681
  ) {
653
682
  const positionIndex = index * 3;
654
683
  const positionArr =
655
684
  particleSystem.geometry.attributes.position.array;
685
+
656
686
  if (simulationSpace === SimulationSpace.WORLD) {
657
687
  positionArr[positionIndex] -= worldPositionChange.x;
658
688
  positionArr[positionIndex + 1] -= worldPositionChange.y;