@tsparticles/simplex-noise 3.0.0-beta.4

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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/browser/Classes/SimplexNoise2D.js +152 -0
  4. package/browser/Classes/SimplexNoise3D.js +528 -0
  5. package/browser/Classes/SimplexNoise4D.js +2867 -0
  6. package/browser/Contributions.js +1 -0
  7. package/browser/SimplexNoise.js +10 -0
  8. package/browser/index.js +1 -0
  9. package/browser/package.json +1 -0
  10. package/browser/utils.js +5 -0
  11. package/cjs/Classes/SimplexNoise2D.js +156 -0
  12. package/cjs/Classes/SimplexNoise3D.js +532 -0
  13. package/cjs/Classes/SimplexNoise4D.js +2871 -0
  14. package/cjs/Contributions.js +2 -0
  15. package/cjs/SimplexNoise.js +14 -0
  16. package/cjs/index.js +5 -0
  17. package/cjs/package.json +1 -0
  18. package/cjs/utils.js +9 -0
  19. package/esm/Classes/SimplexNoise2D.js +152 -0
  20. package/esm/Classes/SimplexNoise3D.js +528 -0
  21. package/esm/Classes/SimplexNoise4D.js +2867 -0
  22. package/esm/Contributions.js +1 -0
  23. package/esm/SimplexNoise.js +10 -0
  24. package/esm/index.js +1 -0
  25. package/esm/package.json +1 -0
  26. package/esm/utils.js +5 -0
  27. package/package.json +105 -0
  28. package/report.html +39 -0
  29. package/tsparticles.simplex.noise.js +457 -0
  30. package/tsparticles.simplex.noise.min.js +2 -0
  31. package/tsparticles.simplex.noise.min.js.LICENSE.txt +1 -0
  32. package/types/Classes/SimplexNoise2D.d.ts +16 -0
  33. package/types/Classes/SimplexNoise3D.d.ts +16 -0
  34. package/types/Classes/SimplexNoise4D.d.ts +16 -0
  35. package/types/Contributions.d.ts +27 -0
  36. package/types/SimplexNoise.d.ts +9 -0
  37. package/types/index.d.ts +1 -0
  38. package/types/utils.d.ts +1 -0
  39. package/umd/Classes/SimplexNoise2D.js +166 -0
  40. package/umd/Classes/SimplexNoise3D.js +542 -0
  41. package/umd/Classes/SimplexNoise4D.js +2881 -0
  42. package/umd/Contributions.js +12 -0
  43. package/umd/SimplexNoise.js +24 -0
  44. package/umd/index.js +15 -0
  45. package/umd/utils.js +19 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Matteo Bruni
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
@@ -0,0 +1,74 @@
1
+ [![banner](https://particles.js.org/images/banner3.png)](https://particles.js.org)
2
+
3
+ # tsParticles Simplex Noise Library
4
+
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/@tsparticles/path-simplex-noise/badge)](https://www.jsdelivr.com/package/npm/@tsparticles/path-simplex-noise)
6
+ [![npmjs](https://badge.fury.io/js/@tsparticles/path-simplex-noise.svg)](https://www.npmjs.com/package/@tsparticles/path-simplex-noise)
7
+ [![npmjs](https://img.shields.io/npm/dt/@tsparticles/path-simplex-noise)](https://www.npmjs.com/package/@tsparticles/path-simplex-noise) [![GitHub Sponsors](https://img.shields.io/github/sponsors/matteobruni)](https://github.com/sponsors/matteobruni)
8
+
9
+ [tsParticles](https://github.com/tsparticles/tsparticles) library for simplex noise movement.
10
+
11
+ ## How to use it
12
+
13
+ ### CDN / Vanilla JS / jQuery
14
+
15
+ The CDN/Vanilla version JS has one required file in vanilla configuration:
16
+
17
+ Including the `tsparticles.simplex.noise.min.js` file will export the function to load the path plugin:
18
+
19
+ ```text
20
+ loadSimplexNoisePath
21
+ ```
22
+
23
+ ### Usage
24
+
25
+ Once the scripts are loaded you can set up `tsParticles` and the path plugin like this:
26
+
27
+ ```javascript
28
+ (async () => {
29
+ await loadSimplexNoisePath(tsParticles);
30
+
31
+ await tsParticles.load({
32
+ id: "tsparticles",
33
+ options: {
34
+ /* options */
35
+ },
36
+ });
37
+ })();
38
+ ```
39
+
40
+ ### ESM / CommonJS
41
+
42
+ This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
43
+
44
+ ```shell
45
+ $ npm install @tsparticles/simplex-noise
46
+ ```
47
+
48
+ or
49
+
50
+ ```shell
51
+ $ yarn add @tsparticles/simplex-noise
52
+ ```
53
+
54
+ Then you need to import it in the app, like this:
55
+
56
+ ```javascript
57
+ const { tsParticles } = require("@tsparticles/engine");
58
+ const { loadSimplexNoisePath } = require("@tsparticles/path-simplex-noise");
59
+
60
+ (async () => {
61
+ await loadSimplexNoisePath(tsParticles);
62
+ })();
63
+ ```
64
+
65
+ or
66
+
67
+ ```javascript
68
+ import { tsParticles } from "@tsparticles/engine";
69
+ import { loadSimplexNoisePath } from "@tsparticles/path-simplex-noise";
70
+
71
+ (async () => {
72
+ await loadSimplexNoisePath(tsParticles);
73
+ })();
74
+ ```
@@ -0,0 +1,152 @@
1
+ import { shuffleSeed } from "../utils.js";
2
+ export class SimplexNoise2D {
3
+ constructor() {
4
+ this._NORM_2D = 1.0 / 47.0;
5
+ this._SQUISH_2D = (Math.sqrt(2 + 1) - 1) / 2;
6
+ this._STRETCH_2D = (1 / Math.sqrt(2 + 1) - 1) / 2;
7
+ this._base2D = [
8
+ [1, 1, 0, 1, 0, 1, 0, 0, 0],
9
+ [1, 1, 0, 1, 0, 1, 2, 1, 1],
10
+ ];
11
+ this._gradients2D = [
12
+ 5,
13
+ 2,
14
+ 2,
15
+ 5,
16
+ -5,
17
+ 2,
18
+ -2,
19
+ 5,
20
+ 5,
21
+ -2,
22
+ 2,
23
+ -5,
24
+ -5,
25
+ -2,
26
+ -2,
27
+ -5,
28
+ ];
29
+ this._lookup = [];
30
+ this._lookupPairs2D = [
31
+ 0,
32
+ 1,
33
+ 1,
34
+ 0,
35
+ 4,
36
+ 1,
37
+ 17,
38
+ 0,
39
+ 20,
40
+ 2,
41
+ 21,
42
+ 2,
43
+ 22,
44
+ 5,
45
+ 23,
46
+ 5,
47
+ 26,
48
+ 4,
49
+ 39,
50
+ 3,
51
+ 42,
52
+ 4,
53
+ 43,
54
+ 3,
55
+ ];
56
+ this._p2D = [
57
+ 0,
58
+ 0,
59
+ 1,
60
+ -1,
61
+ 0,
62
+ 0,
63
+ -1,
64
+ 1,
65
+ 0,
66
+ 2,
67
+ 1,
68
+ 1,
69
+ 1,
70
+ 2,
71
+ 2,
72
+ 0,
73
+ 1,
74
+ 2,
75
+ 0,
76
+ 2,
77
+ 1,
78
+ 0,
79
+ 0,
80
+ 0,
81
+ ];
82
+ this._perm = new Uint8Array(256);
83
+ this._perm2D = new Uint8Array(256);
84
+ }
85
+ noise(x, y) {
86
+ const { _gradients2D, _NORM_2D, _SQUISH_2D, _STRETCH_2D, _lookup, _perm, _perm2D } = this;
87
+ const stretchOffset = (x + y) * _STRETCH_2D, xs = x + stretchOffset, ys = y + stretchOffset, xsb = Math.floor(xs), ysb = Math.floor(ys), squishOffset = (xsb + ysb) * _SQUISH_2D, dx0 = x - (xsb + squishOffset), dy0 = y - (ysb + squishOffset), xins = xs - xsb, yins = ys - ysb, inSum = xins + yins, hash = (xins - yins + 1) | (inSum << 1) | ((inSum + yins) << 2) | ((inSum + xins) << 4);
88
+ let value = 0;
89
+ for (let c = _lookup[hash]; c !== undefined; c = c.next) {
90
+ const dx = dx0 + c.dx, dy = dy0 + c.dy, attn = 2 - dx * dx - dy * dy;
91
+ if (attn > 0) {
92
+ const px = xsb + c.xsb, py = ysb + c.ysb, indexPartA = _perm[px & 0xff], index = _perm2D[(indexPartA + py) & 0xff], valuePart = _gradients2D[index] * dx + _gradients2D[index + 1] * dy;
93
+ value += attn * attn * attn * attn * valuePart;
94
+ }
95
+ }
96
+ return value * _NORM_2D;
97
+ }
98
+ seed(clientSeed) {
99
+ const { _p2D, _base2D, _lookupPairs2D } = this;
100
+ const contributions = [];
101
+ for (let i = 0; i < _p2D.length; i += 4) {
102
+ const baseSet = _base2D[_p2D[i]];
103
+ let previous = null, current = null;
104
+ for (let k = 0; k < baseSet.length; k += 3) {
105
+ current = this._contribution2D(baseSet[k], baseSet[k + 1], baseSet[k + 2]);
106
+ if (previous === null) {
107
+ contributions[i / 4] = current;
108
+ }
109
+ else {
110
+ previous.next = current;
111
+ }
112
+ previous = current;
113
+ }
114
+ if (current) {
115
+ current.next = this._contribution2D(_p2D[i + 1], _p2D[i + 2], _p2D[i + 3]);
116
+ }
117
+ }
118
+ this._lookup = [];
119
+ for (let i = 0; i < _lookupPairs2D.length; i += 2) {
120
+ this._lookup[_lookupPairs2D[i]] = contributions[_lookupPairs2D[i + 1]];
121
+ }
122
+ this._perm = new Uint8Array(256);
123
+ this._perm2D = new Uint8Array(256);
124
+ const source = new Uint8Array(256);
125
+ for (let i = 0; i < 256; i++) {
126
+ source[i] = i;
127
+ }
128
+ let seed = new Uint32Array(1);
129
+ seed[0] = clientSeed;
130
+ seed = shuffleSeed(shuffleSeed(shuffleSeed(seed)));
131
+ for (let i = 255; i >= 0; i--) {
132
+ seed = shuffleSeed(seed);
133
+ const r = new Uint32Array(1);
134
+ r[0] = (seed[0] + 31) % (i + 1);
135
+ if (r[0] < 0) {
136
+ r[0] += i + 1;
137
+ }
138
+ this._perm[i] = source[r[0]];
139
+ this._perm2D[i] = this._perm[i] & 0x0e;
140
+ source[r[0]] = source[i];
141
+ }
142
+ }
143
+ _contribution2D(multiplier, xsb, ysb) {
144
+ const { _SQUISH_2D } = this;
145
+ return {
146
+ dx: -xsb - multiplier * _SQUISH_2D,
147
+ dy: -ysb - multiplier * _SQUISH_2D,
148
+ xsb,
149
+ ysb,
150
+ };
151
+ }
152
+ }