@cult-frog/noise 0.0.0-stage → 0.1.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/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +205 -2
- package/dist/fractal/_types/fractalOptions.d.ts +36 -0
- package/dist/fractal/_types/fractalOptions.d.ts.map +1 -0
- package/dist/fractal/_types/fractalOptions.js +2 -0
- package/dist/fractal/_types/fractalOptions.js.map +1 -0
- package/dist/fractal/_types/index.d.ts +2 -0
- package/dist/fractal/_types/index.d.ts.map +1 -0
- package/dist/fractal/_types/index.js +2 -0
- package/dist/fractal/_types/index.js.map +1 -0
- package/dist/fractal/fractal.d.ts +17 -0
- package/dist/fractal/fractal.d.ts.map +1 -0
- package/dist/fractal/fractal.js +31 -0
- package/dist/fractal/fractal.js.map +1 -0
- package/dist/fractal/index.d.ts +4 -0
- package/dist/fractal/index.d.ts.map +1 -0
- package/dist/fractal/index.js +3 -0
- package/dist/fractal/index.js.map +1 -0
- package/dist/fractal/options.d.ts +14 -0
- package/dist/fractal/options.d.ts.map +1 -0
- package/dist/fractal/options.js +57 -0
- package/dist/fractal/options.js.map +1 -0
- package/dist/gradient/cubeEdge3.d.ts +11 -0
- package/dist/gradient/cubeEdge3.d.ts.map +1 -0
- package/dist/gradient/cubeEdge3.js +39 -0
- package/dist/gradient/cubeEdge3.js.map +1 -0
- package/dist/gradient/index.d.ts +2 -0
- package/dist/gradient/index.d.ts.map +1 -0
- package/dist/gradient/index.js +2 -0
- package/dist/gradient/index.js.map +1 -0
- package/dist/hashes/hash.d.ts +16 -0
- package/dist/hashes/hash.d.ts.map +1 -0
- package/dist/hashes/hash.js +37 -0
- package/dist/hashes/hash.js.map +1 -0
- package/dist/hashes/hashes.d.ts +3 -0
- package/dist/hashes/hashes.d.ts.map +1 -0
- package/dist/hashes/hashes.js +3 -0
- package/dist/hashes/hashes.js.map +1 -0
- package/dist/hashes/index.d.ts +2 -0
- package/dist/hashes/index.d.ts.map +1 -0
- package/dist/hashes/index.js +2 -0
- package/dist/hashes/index.js.map +1 -0
- package/dist/hashes/lowbias.d.ts +12 -0
- package/dist/hashes/lowbias.d.ts.map +1 -0
- package/dist/hashes/lowbias.js +19 -0
- package/dist/hashes/lowbias.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/noiseMap/_types/index.d.ts +3 -0
- package/dist/noiseMap/_types/index.d.ts.map +1 -0
- package/dist/noiseMap/_types/index.js +2 -0
- package/dist/noiseMap/_types/index.js.map +1 -0
- package/dist/noiseMap/_types/noise2d.d.ts +21 -0
- package/dist/noiseMap/_types/noise2d.d.ts.map +1 -0
- package/dist/noiseMap/_types/noise2d.js +2 -0
- package/dist/noiseMap/_types/noise2d.js.map +1 -0
- package/dist/noiseMap/_types/noise3d.d.ts +24 -0
- package/dist/noiseMap/_types/noise3d.d.ts.map +1 -0
- package/dist/noiseMap/_types/noise3d.js +2 -0
- package/dist/noiseMap/_types/noise3d.js.map +1 -0
- package/dist/noiseMap/index.d.ts +6 -0
- package/dist/noiseMap/index.d.ts.map +1 -0
- package/dist/noiseMap/index.js +3 -0
- package/dist/noiseMap/index.js.map +1 -0
- package/dist/noiseMap/noise2d.d.ts +52 -0
- package/dist/noiseMap/noise2d.d.ts.map +1 -0
- package/dist/noiseMap/noise2d.js +105 -0
- package/dist/noiseMap/noise2d.js.map +1 -0
- package/dist/noiseMap/noise3d.d.ts +62 -0
- package/dist/noiseMap/noise3d.d.ts.map +1 -0
- package/dist/noiseMap/noise3d.js +129 -0
- package/dist/noiseMap/noise3d.js.map +1 -0
- package/dist/noiseTypes/cellular/2d.d.ts +36 -0
- package/dist/noiseTypes/cellular/2d.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/2d.js +123 -0
- package/dist/noiseTypes/cellular/2d.js.map +1 -0
- package/dist/noiseTypes/cellular/3d.d.ts +32 -0
- package/dist/noiseTypes/cellular/3d.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/3d.js +143 -0
- package/dist/noiseTypes/cellular/3d.js.map +1 -0
- package/dist/noiseTypes/cellular/_types/index.d.ts +2 -0
- package/dist/noiseTypes/cellular/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/_types/index.js +2 -0
- package/dist/noiseTypes/cellular/_types/index.js.map +1 -0
- package/dist/noiseTypes/cellular/_types/options.d.ts +37 -0
- package/dist/noiseTypes/cellular/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/_types/options.js +2 -0
- package/dist/noiseTypes/cellular/_types/options.js.map +1 -0
- package/dist/noiseTypes/cellular/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/cellular/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/defaultOptions.js +13 -0
- package/dist/noiseTypes/cellular/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/cellular/floorDiv.d.ts +18 -0
- package/dist/noiseTypes/cellular/floorDiv.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/floorDiv.js +28 -0
- package/dist/noiseTypes/cellular/floorDiv.js.map +1 -0
- package/dist/noiseTypes/cellular/index.d.ts +4 -0
- package/dist/noiseTypes/cellular/index.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/index.js +3 -0
- package/dist/noiseTypes/cellular/index.js.map +1 -0
- package/dist/noiseTypes/cellular/options.d.ts +6 -0
- package/dist/noiseTypes/cellular/options.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/options.js +20 -0
- package/dist/noiseTypes/cellular/options.js.map +1 -0
- package/dist/noiseTypes/index.d.ts +5 -0
- package/dist/noiseTypes/index.d.ts.map +1 -0
- package/dist/noiseTypes/index.js +5 -0
- package/dist/noiseTypes/index.js.map +1 -0
- package/dist/noiseTypes/perlin/2d.d.ts +23 -0
- package/dist/noiseTypes/perlin/2d.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/2d.js +64 -0
- package/dist/noiseTypes/perlin/2d.js.map +1 -0
- package/dist/noiseTypes/perlin/3d.d.ts +24 -0
- package/dist/noiseTypes/perlin/3d.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/3d.js +69 -0
- package/dist/noiseTypes/perlin/3d.js.map +1 -0
- package/dist/noiseTypes/perlin/_types/index.d.ts +2 -0
- package/dist/noiseTypes/perlin/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/_types/index.js +2 -0
- package/dist/noiseTypes/perlin/_types/index.js.map +1 -0
- package/dist/noiseTypes/perlin/_types/options.d.ts +22 -0
- package/dist/noiseTypes/perlin/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/_types/options.js +2 -0
- package/dist/noiseTypes/perlin/_types/options.js.map +1 -0
- package/dist/noiseTypes/perlin/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/perlin/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/defaultOptions.js +14 -0
- package/dist/noiseTypes/perlin/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/perlin/index.d.ts +4 -0
- package/dist/noiseTypes/perlin/index.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/index.js +3 -0
- package/dist/noiseTypes/perlin/index.js.map +1 -0
- package/dist/noiseTypes/perlin/options.d.ts +9 -0
- package/dist/noiseTypes/perlin/options.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/options.js +17 -0
- package/dist/noiseTypes/perlin/options.js.map +1 -0
- package/dist/noiseTypes/perlin/sample2.d.ts +25 -0
- package/dist/noiseTypes/perlin/sample2.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/sample2.js +58 -0
- package/dist/noiseTypes/perlin/sample2.js.map +1 -0
- package/dist/noiseTypes/perlin/sample3.d.ts +9 -0
- package/dist/noiseTypes/perlin/sample3.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/sample3.js +38 -0
- package/dist/noiseTypes/perlin/sample3.js.map +1 -0
- package/dist/noiseTypes/simplex/2d.d.ts +23 -0
- package/dist/noiseTypes/simplex/2d.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/2d.js +64 -0
- package/dist/noiseTypes/simplex/2d.js.map +1 -0
- package/dist/noiseTypes/simplex/3d.d.ts +25 -0
- package/dist/noiseTypes/simplex/3d.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/3d.js +70 -0
- package/dist/noiseTypes/simplex/3d.js.map +1 -0
- package/dist/noiseTypes/simplex/_types/index.d.ts +2 -0
- package/dist/noiseTypes/simplex/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/_types/index.js +2 -0
- package/dist/noiseTypes/simplex/_types/index.js.map +1 -0
- package/dist/noiseTypes/simplex/_types/options.d.ts +33 -0
- package/dist/noiseTypes/simplex/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/_types/options.js +2 -0
- package/dist/noiseTypes/simplex/_types/options.js.map +1 -0
- package/dist/noiseTypes/simplex/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/simplex/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/defaultOptions.js +14 -0
- package/dist/noiseTypes/simplex/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/simplex/index.d.ts +4 -0
- package/dist/noiseTypes/simplex/index.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/index.js +3 -0
- package/dist/noiseTypes/simplex/index.js.map +1 -0
- package/dist/noiseTypes/simplex/options.d.ts +9 -0
- package/dist/noiseTypes/simplex/options.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/options.js +17 -0
- package/dist/noiseTypes/simplex/options.js.map +1 -0
- package/dist/noiseTypes/simplex/sample2.d.ts +32 -0
- package/dist/noiseTypes/simplex/sample2.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/sample2.js +94 -0
- package/dist/noiseTypes/simplex/sample2.js.map +1 -0
- package/dist/noiseTypes/simplex/sample3.d.ts +19 -0
- package/dist/noiseTypes/simplex/sample3.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/sample3.js +110 -0
- package/dist/noiseTypes/simplex/sample3.js.map +1 -0
- package/dist/noiseTypes/white/2d.d.ts +18 -0
- package/dist/noiseTypes/white/2d.d.ts.map +1 -0
- package/dist/noiseTypes/white/2d.js +33 -0
- package/dist/noiseTypes/white/2d.js.map +1 -0
- package/dist/noiseTypes/white/3d.d.ts +17 -0
- package/dist/noiseTypes/white/3d.d.ts.map +1 -0
- package/dist/noiseTypes/white/3d.js +34 -0
- package/dist/noiseTypes/white/3d.js.map +1 -0
- package/dist/noiseTypes/white/_types/index.d.ts +2 -0
- package/dist/noiseTypes/white/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/white/_types/index.js +2 -0
- package/dist/noiseTypes/white/_types/index.js.map +1 -0
- package/dist/noiseTypes/white/_types/options.d.ts +28 -0
- package/dist/noiseTypes/white/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/white/_types/options.js +2 -0
- package/dist/noiseTypes/white/_types/options.js.map +1 -0
- package/dist/noiseTypes/white/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/white/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/white/defaultOptions.js +10 -0
- package/dist/noiseTypes/white/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/white/index.d.ts +4 -0
- package/dist/noiseTypes/white/index.d.ts.map +1 -0
- package/dist/noiseTypes/white/index.js +3 -0
- package/dist/noiseTypes/white/index.js.map +1 -0
- package/dist/noiseTypes/white/options.d.ts +6 -0
- package/dist/noiseTypes/white/options.d.ts.map +1 -0
- package/dist/noiseTypes/white/options.js +14 -0
- package/dist/noiseTypes/white/options.js.map +1 -0
- package/package.json +78 -4
- package/src/fractal/_types/fractalOptions.ts +35 -0
- package/src/fractal/_types/index.ts +1 -0
- package/src/fractal/fractal.ts +41 -0
- package/src/fractal/index.ts +4 -0
- package/src/fractal/options.ts +62 -0
- package/src/gradient/cubeEdge3.ts +47 -0
- package/src/gradient/index.ts +1 -0
- package/src/hashes/hash.ts +41 -0
- package/src/hashes/hashes.ts +2 -0
- package/src/hashes/index.ts +1 -0
- package/src/hashes/lowbias.ts +18 -0
- package/src/index.ts +4 -0
- package/src/noiseMap/_types/index.ts +2 -0
- package/src/noiseMap/_types/noise2d.ts +21 -0
- package/src/noiseMap/_types/noise3d.ts +24 -0
- package/src/noiseMap/index.ts +7 -0
- package/src/noiseMap/noise2d.ts +117 -0
- package/src/noiseMap/noise3d.ts +146 -0
- package/src/noiseTypes/cellular/2d.ts +142 -0
- package/src/noiseTypes/cellular/3d.ts +171 -0
- package/src/noiseTypes/cellular/_types/index.ts +1 -0
- package/src/noiseTypes/cellular/_types/options.ts +37 -0
- package/src/noiseTypes/cellular/defaultOptions.ts +15 -0
- package/src/noiseTypes/cellular/floorDiv.ts +27 -0
- package/src/noiseTypes/cellular/index.ts +4 -0
- package/src/noiseTypes/cellular/options.ts +20 -0
- package/src/noiseTypes/index.ts +4 -0
- package/src/noiseTypes/perlin/2d.ts +86 -0
- package/src/noiseTypes/perlin/3d.ts +93 -0
- package/src/noiseTypes/perlin/_types/index.ts +1 -0
- package/src/noiseTypes/perlin/_types/options.ts +23 -0
- package/src/noiseTypes/perlin/defaultOptions.ts +16 -0
- package/src/noiseTypes/perlin/index.ts +4 -0
- package/src/noiseTypes/perlin/options.ts +19 -0
- package/src/noiseTypes/perlin/sample2.ts +62 -0
- package/src/noiseTypes/perlin/sample3.ts +43 -0
- package/src/noiseTypes/simplex/2d.ts +86 -0
- package/src/noiseTypes/simplex/3d.ts +94 -0
- package/src/noiseTypes/simplex/_types/index.ts +1 -0
- package/src/noiseTypes/simplex/_types/options.ts +34 -0
- package/src/noiseTypes/simplex/defaultOptions.ts +16 -0
- package/src/noiseTypes/simplex/index.ts +4 -0
- package/src/noiseTypes/simplex/options.ts +19 -0
- package/src/noiseTypes/simplex/sample2.ts +103 -0
- package/src/noiseTypes/simplex/sample3.ts +128 -0
- package/src/noiseTypes/white/2d.ts +44 -0
- package/src/noiseTypes/white/3d.ts +46 -0
- package/src/noiseTypes/white/_types/index.ts +1 -0
- package/src/noiseTypes/white/_types/options.ts +28 -0
- package/src/noiseTypes/white/defaultOptions.ts +12 -0
- package/src/noiseTypes/white/index.ts +4 -0
- package/src/noiseTypes/white/options.ts +14 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Hashes } from '../../hashes/index.js';
|
|
2
|
+
|
|
3
|
+
/** Skews (x, y) onto the triangle lattice: cell (i, j) is `floor(x + (x + y) * F2)`. */
|
|
4
|
+
const F2 = 0.5 * (Math.sqrt(3) - 1);
|
|
5
|
+
|
|
6
|
+
/** Unskews lattice point (i, j) back to input space: `i - (i + j) * G2`. */
|
|
7
|
+
const G2 = (3 - Math.sqrt(3)) / 6;
|
|
8
|
+
|
|
9
|
+
/** cos 30° and sin 60°, for the gradient directions at 30° steps. */
|
|
10
|
+
const S = Math.sqrt(3) / 2;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Brings the sum inside [-1, 1]. The worst case, with every corner's gradient
|
|
14
|
+
* pointing at the sample, is about 1 / 101.6557 of the raw sum. 101.65 is that
|
|
15
|
+
* reciprocal rounded down, so the result stays within ±1.
|
|
16
|
+
*/
|
|
17
|
+
const SAMPLE2_SCALE = 101.65;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Dot product of the gradient hashed for lattice corner (`ci`, `cj`) with the
|
|
21
|
+
* offset (`dx`, `dy`) from that corner, in input space.
|
|
22
|
+
*
|
|
23
|
+
* The twelve gradients are unit vectors 30° apart. Simplex sums radial kernels,
|
|
24
|
+
* so unequal gradient lengths (Perlin's diagonals are √2) would show up as
|
|
25
|
+
* brighter blobs along those directions; equal lengths keep it isotropic. The
|
|
26
|
+
* √3/2 components are fixed constants, so each dot product is still exact
|
|
27
|
+
* arithmetic and matches across engines. `% 12` relies on the hash being
|
|
28
|
+
* unsigned.
|
|
29
|
+
*/
|
|
30
|
+
export function corner2(ci: number, cj: number, dx: number, dy: number, seed: number): number {
|
|
31
|
+
switch (Hashes.hash2(ci, cj, seed) % 12) {
|
|
32
|
+
case 0:
|
|
33
|
+
return dx;
|
|
34
|
+
case 1:
|
|
35
|
+
return S * dx + 0.5 * dy;
|
|
36
|
+
case 2:
|
|
37
|
+
return 0.5 * dx + S * dy;
|
|
38
|
+
case 3:
|
|
39
|
+
return dy;
|
|
40
|
+
case 4:
|
|
41
|
+
return -0.5 * dx + S * dy;
|
|
42
|
+
case 5:
|
|
43
|
+
return -S * dx + 0.5 * dy;
|
|
44
|
+
case 6:
|
|
45
|
+
return -dx;
|
|
46
|
+
case 7:
|
|
47
|
+
return -S * dx - 0.5 * dy;
|
|
48
|
+
case 8:
|
|
49
|
+
return -0.5 * dx - S * dy;
|
|
50
|
+
case 9:
|
|
51
|
+
return -dy;
|
|
52
|
+
case 10:
|
|
53
|
+
return 0.5 * dx - S * dy;
|
|
54
|
+
default:
|
|
55
|
+
return S * dx - 0.5 * dy;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** One corner's kernel-weighted contribution: (0.5 − d²)⁴ · (g · d), or 0 outside radius √0.5. */
|
|
60
|
+
function contribution(ci: number, cj: number, dx: number, dy: number, seed: number): number {
|
|
61
|
+
const t = 0.5 - dx * dx - dy * dy;
|
|
62
|
+
if (t <= 0) return 0;
|
|
63
|
+
const t2 = t * t;
|
|
64
|
+
return t2 * t2 * corner2(ci, cj, dx, dy, seed);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* One octave of 2D simplex noise at (`x`, `y`), in [-1, 1].
|
|
69
|
+
*
|
|
70
|
+
* Space is split into equilateral triangles and each sample sums kernels from
|
|
71
|
+
* its triangle's three corners, against four corners for 2D Perlin. The kernel
|
|
72
|
+
* radius √0.5 equals the triangle's height, so no corner outside the triangle
|
|
73
|
+
* can reach the sample and the result is continuous with a continuous gradient.
|
|
74
|
+
* Features sit on a triangle lattice with no axis alignment, so the output
|
|
75
|
+
* avoids Perlin's grid-aligned look.
|
|
76
|
+
*
|
|
77
|
+
* Unlike Perlin, the value is not 0 at integer points; it is 0 at the lattice
|
|
78
|
+
* corners, which lie at `(i − (i + j)·G2, j − (i + j)·G2)`. Most values sit well
|
|
79
|
+
* inside ±1, which is only reached with every gradient lined up. Corner indices
|
|
80
|
+
* are truncated to int32 by the hash, so the pattern repeats beyond ±2^31 cells.
|
|
81
|
+
*
|
|
82
|
+
* Seeded output must match across engines, so this may only use exact
|
|
83
|
+
* operations: + − × ÷, floor, sqrt, integer and bitwise ops.
|
|
84
|
+
*/
|
|
85
|
+
export function sample2(x: number, y: number, seed: number): number {
|
|
86
|
+
const skew = (x + y) * F2;
|
|
87
|
+
const i = Math.floor(x + skew);
|
|
88
|
+
const j = Math.floor(y + skew);
|
|
89
|
+
|
|
90
|
+
const unskew = (i + j) * G2;
|
|
91
|
+
const x0 = x - (i - unskew);
|
|
92
|
+
const y0 = y - (j - unskew);
|
|
93
|
+
|
|
94
|
+
// Below the diagonal the middle corner is (i + 1, j), above it (i, j + 1).
|
|
95
|
+
const i1 = x0 > y0 ? 1 : 0;
|
|
96
|
+
const j1 = 1 - i1;
|
|
97
|
+
|
|
98
|
+
const n0 = contribution(i, j, x0, y0, seed);
|
|
99
|
+
const n1 = contribution(i + i1, j + j1, x0 - i1 + G2, y0 - j1 + G2, seed);
|
|
100
|
+
const n2 = contribution(i + 1, j + 1, x0 - 1 + 2 * G2, y0 - 1 + 2 * G2, seed);
|
|
101
|
+
|
|
102
|
+
return (n0 + n1 + n2) * SAMPLE2_SCALE;
|
|
103
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { cubeEdge3 } from '../../gradient/index.js';
|
|
2
|
+
|
|
3
|
+
/** Skews (x, y, z) onto the tetrahedral lattice: cell (i, j, k) is `floor(x + (x + y + z) * F3)`. */
|
|
4
|
+
const F3 = 1 / 3;
|
|
5
|
+
|
|
6
|
+
/** Unskews lattice point (i, j, k) back to input space: `i - (i + j + k) * G3`. */
|
|
7
|
+
const G3 = 1 / 6;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Brings the sum inside [-1, 1]. The worst case, with every corner's gradient
|
|
11
|
+
* pointing at the sample, is about 1 / 76.8808 of the raw sum. 76.88 is that
|
|
12
|
+
* reciprocal rounded down, so the result stays within ±1.
|
|
13
|
+
*/
|
|
14
|
+
const SAMPLE3_SCALE = 76.88;
|
|
15
|
+
|
|
16
|
+
/** One corner's kernel-weighted contribution: (0.5 − d²)⁴ · (g · d), or 0 outside radius √0.5. */
|
|
17
|
+
function contribution(
|
|
18
|
+
ci: number,
|
|
19
|
+
cj: number,
|
|
20
|
+
ck: number,
|
|
21
|
+
dx: number,
|
|
22
|
+
dy: number,
|
|
23
|
+
dz: number,
|
|
24
|
+
seed: number
|
|
25
|
+
): number {
|
|
26
|
+
const t = 0.5 - dx * dx - dy * dy - dz * dz;
|
|
27
|
+
if (t <= 0) return 0;
|
|
28
|
+
const t2 = t * t;
|
|
29
|
+
return t2 * t2 * cubeEdge3(ci, cj, ck, dx, dy, dz, seed);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* One octave of 3D simplex noise at (`x`, `y`, `z`), in [-1, 1].
|
|
34
|
+
*
|
|
35
|
+
* Space is split into tetrahedra and each sample sums kernels from its
|
|
36
|
+
* tetrahedron's four corners, against eight for 3D Perlin, so it costs roughly
|
|
37
|
+
* half as much per octave.
|
|
38
|
+
*
|
|
39
|
+
* The kernel radius is √0.5. The commonly published 0.6 lets corners outside the
|
|
40
|
+
* tetrahedron reach the sample, and since those are never summed the output
|
|
41
|
+
* jumps at tetrahedron faces. At 0.5 no outside corner can reach, so the result
|
|
42
|
+
* is continuous with a continuous gradient.
|
|
43
|
+
*
|
|
44
|
+
* Gradients are the same twelve cube edges as 3D Perlin, all of length √2. The
|
|
45
|
+
* value is 0 at the lattice corners, not at integer points. Corner indices are
|
|
46
|
+
* truncated to int32 by the hash, so the pattern repeats beyond ±2^31 cells.
|
|
47
|
+
* Like `sample2`, this may only use exact operations.
|
|
48
|
+
*/
|
|
49
|
+
export function sample3(x: number, y: number, z: number, seed: number): number {
|
|
50
|
+
const skew = (x + y + z) * F3;
|
|
51
|
+
const i = Math.floor(x + skew);
|
|
52
|
+
const j = Math.floor(y + skew);
|
|
53
|
+
const k = Math.floor(z + skew);
|
|
54
|
+
|
|
55
|
+
const unskew = (i + j + k) * G3;
|
|
56
|
+
const x0 = x - (i - unskew);
|
|
57
|
+
const y0 = y - (j - unskew);
|
|
58
|
+
const z0 = z - (k - unskew);
|
|
59
|
+
|
|
60
|
+
// The tetrahedron is picked by ranking x0, y0, z0: the walk from (i, j, k) to
|
|
61
|
+
// (i + 1, j + 1, k + 1) steps along the largest offset first, then the next.
|
|
62
|
+
let i1: number;
|
|
63
|
+
let j1: number;
|
|
64
|
+
let k1: number;
|
|
65
|
+
let i2: number;
|
|
66
|
+
let j2: number;
|
|
67
|
+
let k2: number;
|
|
68
|
+
if (x0 >= y0) {
|
|
69
|
+
if (y0 >= z0) {
|
|
70
|
+
i1 = 1;
|
|
71
|
+
j1 = 0;
|
|
72
|
+
k1 = 0;
|
|
73
|
+
i2 = 1;
|
|
74
|
+
j2 = 1;
|
|
75
|
+
k2 = 0;
|
|
76
|
+
} else if (x0 >= z0) {
|
|
77
|
+
i1 = 1;
|
|
78
|
+
j1 = 0;
|
|
79
|
+
k1 = 0;
|
|
80
|
+
i2 = 1;
|
|
81
|
+
j2 = 0;
|
|
82
|
+
k2 = 1;
|
|
83
|
+
} else {
|
|
84
|
+
i1 = 0;
|
|
85
|
+
j1 = 0;
|
|
86
|
+
k1 = 1;
|
|
87
|
+
i2 = 1;
|
|
88
|
+
j2 = 0;
|
|
89
|
+
k2 = 1;
|
|
90
|
+
}
|
|
91
|
+
} else if (y0 < z0) {
|
|
92
|
+
i1 = 0;
|
|
93
|
+
j1 = 0;
|
|
94
|
+
k1 = 1;
|
|
95
|
+
i2 = 0;
|
|
96
|
+
j2 = 1;
|
|
97
|
+
k2 = 1;
|
|
98
|
+
} else if (x0 < z0) {
|
|
99
|
+
i1 = 0;
|
|
100
|
+
j1 = 1;
|
|
101
|
+
k1 = 0;
|
|
102
|
+
i2 = 0;
|
|
103
|
+
j2 = 1;
|
|
104
|
+
k2 = 1;
|
|
105
|
+
} else {
|
|
106
|
+
i1 = 0;
|
|
107
|
+
j1 = 1;
|
|
108
|
+
k1 = 0;
|
|
109
|
+
i2 = 1;
|
|
110
|
+
j2 = 1;
|
|
111
|
+
k2 = 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const n0 = contribution(i, j, k, x0, y0, z0, seed);
|
|
115
|
+
const n1 = contribution(i + i1, j + j1, k + k1, x0 - i1 + G3, y0 - j1 + G3, z0 - k1 + G3, seed);
|
|
116
|
+
const n2 = contribution(
|
|
117
|
+
i + i2,
|
|
118
|
+
j + j2,
|
|
119
|
+
k + k2,
|
|
120
|
+
x0 - i2 + 2 * G3,
|
|
121
|
+
y0 - j2 + 2 * G3,
|
|
122
|
+
z0 - k2 + 2 * G3,
|
|
123
|
+
seed
|
|
124
|
+
);
|
|
125
|
+
const n3 = contribution(i + 1, j + 1, k + 1, x0 - 0.5, y0 - 0.5, z0 - 0.5, seed);
|
|
126
|
+
|
|
127
|
+
return (n0 + n1 + n2 + n3) * SAMPLE3_SCALE;
|
|
128
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Hashes } from '../../hashes/index.js';
|
|
2
|
+
import { Noise2D } from '../../noiseMap/index.js';
|
|
3
|
+
import { DEFAULT_WHITE_2D_OPTIONS } from './defaultOptions.js';
|
|
4
|
+
import { validateOptions2 } from './options.js';
|
|
5
|
+
|
|
6
|
+
import type { White2dOptions } from './_types/options.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a `width` × `height` map of white noise in [0, 1): every cell gets an
|
|
10
|
+
* independent, uniformly distributed value with no relation to its neighbours.
|
|
11
|
+
* The caller owns the returned map.
|
|
12
|
+
*
|
|
13
|
+
* Each value is the top 24 bits of the hash of its world cell (`x + offsetX`,
|
|
14
|
+
* `y + offsetY`) and the seed, divided by 2^24. 24 bits is all a Float32 can
|
|
15
|
+
* hold exactly, so values are spaced 2^-24 apart and never reach 1. Values
|
|
16
|
+
* depend only on the world cell and the seed, so chunks of any size agree bit
|
|
17
|
+
* for bit where they overlap.
|
|
18
|
+
*
|
|
19
|
+
* Throws if the size is not a positive integer or an option breaks its contract
|
|
20
|
+
* in {@link White2dOptions}.
|
|
21
|
+
*/
|
|
22
|
+
export function create(
|
|
23
|
+
width: number,
|
|
24
|
+
height: number,
|
|
25
|
+
options: Partial<White2dOptions> = {}
|
|
26
|
+
): Noise2D {
|
|
27
|
+
const noise = Noise2D.create(width, height);
|
|
28
|
+
|
|
29
|
+
const config = { ...DEFAULT_WHITE_2D_OPTIONS, ...options };
|
|
30
|
+
|
|
31
|
+
validateOptions2(config);
|
|
32
|
+
|
|
33
|
+
const seed = config.seed;
|
|
34
|
+
|
|
35
|
+
for (let y = 0; y < height; y++) {
|
|
36
|
+
for (let x = 0; x < width; x++) {
|
|
37
|
+
const hash = Hashes.hash2(x + config.offsetX, y + config.offsetY, seed);
|
|
38
|
+
const value = (hash >>> 8) / 16777216;
|
|
39
|
+
Noise2D.set(noise, x, y, value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return noise;
|
|
44
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Hashes } from '../../hashes/index.js';
|
|
2
|
+
import { Noise3D } from '../../noiseMap/index.js';
|
|
3
|
+
import { DEFAULT_WHITE_3D_OPTIONS } from './defaultOptions.js';
|
|
4
|
+
import { validateOptions3 } from './options.js';
|
|
5
|
+
|
|
6
|
+
import type { White3dOptions } from './_types/options.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a `width` × `height` × `depth` map of white noise in [0, 1): every
|
|
10
|
+
* cell gets an independent, uniformly distributed value. The caller owns the
|
|
11
|
+
* returned map.
|
|
12
|
+
*
|
|
13
|
+
* Works like the 2D version with a third axis: each value is the top 24 bits of
|
|
14
|
+
* the hash of its world cell (`x + offsetX`, `y + offsetY`, `z + offsetZ`) and
|
|
15
|
+
* the seed, divided by 2^24, so chunks of any size agree bit for bit where they
|
|
16
|
+
* overlap.
|
|
17
|
+
*
|
|
18
|
+
* Throws if the size is not a positive integer or an option breaks its contract
|
|
19
|
+
* in {@link White3dOptions}.
|
|
20
|
+
*/
|
|
21
|
+
export function create(
|
|
22
|
+
width: number,
|
|
23
|
+
height: number,
|
|
24
|
+
depth: number,
|
|
25
|
+
options: Partial<White3dOptions> = {}
|
|
26
|
+
): Noise3D {
|
|
27
|
+
const noise = Noise3D.create(width, height, depth);
|
|
28
|
+
|
|
29
|
+
const config = { ...DEFAULT_WHITE_3D_OPTIONS, ...options };
|
|
30
|
+
|
|
31
|
+
validateOptions3(config);
|
|
32
|
+
|
|
33
|
+
const seed = config.seed;
|
|
34
|
+
|
|
35
|
+
for (let y = 0; y < height; y++) {
|
|
36
|
+
for (let x = 0; x < width; x++) {
|
|
37
|
+
for (let z = 0; z < depth; z++) {
|
|
38
|
+
const hash = Hashes.hash3(x + config.offsetX, y + config.offsetY, z + config.offsetZ, seed);
|
|
39
|
+
const value = (hash >>> 8) / 16777216;
|
|
40
|
+
Noise3D.set(noise, x, y, z, value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return noise;
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { White2dOptions, White3dOptions } from './options.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for 2D white noise. A map is a pure function of its size and these
|
|
3
|
+
* options, so the same options reproduce it exactly on any JS engine.
|
|
4
|
+
*/
|
|
5
|
+
export interface White2dOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Selects the value at every world cell. An integer; only its low 32 bits
|
|
8
|
+
* take part, so seeds 2^32 apart give the same map.
|
|
9
|
+
*/
|
|
10
|
+
seed: number;
|
|
11
|
+
/**
|
|
12
|
+
* World x of the map's first column, in map cells. A safe integer (|v| < 2^53),
|
|
13
|
+
* so `x + offsetX` is exact and a chunk at offset 64 reproduces column 64 of a
|
|
14
|
+
* chunk at offset 0 bit for bit. Larger integers would round when added to.
|
|
15
|
+
*
|
|
16
|
+
* The cell hash only sees the low 32 bits of each world coordinate, so the
|
|
17
|
+
* pattern repeats every 2^32 cells along each axis.
|
|
18
|
+
*/
|
|
19
|
+
offsetX: number;
|
|
20
|
+
/** World y of the map's first row. Same contract as {@link offsetX}. */
|
|
21
|
+
offsetY: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Options for 3D white noise: the 2D options plus a z offset. */
|
|
25
|
+
export interface White3dOptions extends White2dOptions {
|
|
26
|
+
/** World z of the map's first layer. Same contract as {@link White2dOptions.offsetX}. */
|
|
27
|
+
offsetZ: number;
|
|
28
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { White2dOptions, White3dOptions } from './_types/options.js';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_WHITE_2D_OPTIONS: White2dOptions = {
|
|
4
|
+
seed: 0,
|
|
5
|
+
offsetX: 0,
|
|
6
|
+
offsetY: 0,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_WHITE_3D_OPTIONS: White3dOptions = {
|
|
10
|
+
...DEFAULT_WHITE_2D_OPTIONS,
|
|
11
|
+
offsetZ: 0,
|
|
12
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { White2dOptions, White3dOptions } from './_types/index.js';
|
|
2
|
+
|
|
3
|
+
/** Throws if `options` breaks a contract in {@link White2dOptions}. */
|
|
4
|
+
export function validateOptions2(options: White2dOptions): void {
|
|
5
|
+
if (!Number.isInteger(options.seed)) throw new RangeError('Invalid seed');
|
|
6
|
+
if (!(Number.isSafeInteger(options.offsetX) && Number.isSafeInteger(options.offsetY)))
|
|
7
|
+
throw new RangeError('Invalid offsetX or offsetY');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Throws if `options` breaks a contract in {@link White3dOptions}. */
|
|
11
|
+
export function validateOptions3(options: White3dOptions): void {
|
|
12
|
+
validateOptions2(options);
|
|
13
|
+
if (!Number.isSafeInteger(options.offsetZ)) throw new RangeError('Invalid offsetZ');
|
|
14
|
+
}
|