@cult-frog/noise 0.0.0-stage → 0.1.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.
Files changed (264) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/README.md +205 -2
  4. package/dist/fractal/_types/fractalOptions.d.ts +36 -0
  5. package/dist/fractal/_types/fractalOptions.d.ts.map +1 -0
  6. package/dist/fractal/_types/fractalOptions.js +2 -0
  7. package/dist/fractal/_types/fractalOptions.js.map +1 -0
  8. package/dist/fractal/_types/index.d.ts +2 -0
  9. package/dist/fractal/_types/index.d.ts.map +1 -0
  10. package/dist/fractal/_types/index.js +2 -0
  11. package/dist/fractal/_types/index.js.map +1 -0
  12. package/dist/fractal/fractal.d.ts +17 -0
  13. package/dist/fractal/fractal.d.ts.map +1 -0
  14. package/dist/fractal/fractal.js +31 -0
  15. package/dist/fractal/fractal.js.map +1 -0
  16. package/dist/fractal/index.d.ts +4 -0
  17. package/dist/fractal/index.d.ts.map +1 -0
  18. package/dist/fractal/index.js +3 -0
  19. package/dist/fractal/index.js.map +1 -0
  20. package/dist/fractal/options.d.ts +14 -0
  21. package/dist/fractal/options.d.ts.map +1 -0
  22. package/dist/fractal/options.js +57 -0
  23. package/dist/fractal/options.js.map +1 -0
  24. package/dist/gradient/cubeEdge3.d.ts +11 -0
  25. package/dist/gradient/cubeEdge3.d.ts.map +1 -0
  26. package/dist/gradient/cubeEdge3.js +39 -0
  27. package/dist/gradient/cubeEdge3.js.map +1 -0
  28. package/dist/gradient/index.d.ts +2 -0
  29. package/dist/gradient/index.d.ts.map +1 -0
  30. package/dist/gradient/index.js +2 -0
  31. package/dist/gradient/index.js.map +1 -0
  32. package/dist/hashes/hash.d.ts +16 -0
  33. package/dist/hashes/hash.d.ts.map +1 -0
  34. package/dist/hashes/hash.js +37 -0
  35. package/dist/hashes/hash.js.map +1 -0
  36. package/dist/hashes/hashes.d.ts +3 -0
  37. package/dist/hashes/hashes.d.ts.map +1 -0
  38. package/dist/hashes/hashes.js +3 -0
  39. package/dist/hashes/hashes.js.map +1 -0
  40. package/dist/hashes/index.d.ts +2 -0
  41. package/dist/hashes/index.d.ts.map +1 -0
  42. package/dist/hashes/index.js +2 -0
  43. package/dist/hashes/index.js.map +1 -0
  44. package/dist/hashes/lowbias.d.ts +12 -0
  45. package/dist/hashes/lowbias.d.ts.map +1 -0
  46. package/dist/hashes/lowbias.js +19 -0
  47. package/dist/hashes/lowbias.js.map +1 -0
  48. package/dist/index.d.ts +4 -0
  49. package/dist/index.d.ts.map +1 -0
  50. package/dist/index.js +3 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/noiseMap/_types/index.d.ts +3 -0
  53. package/dist/noiseMap/_types/index.d.ts.map +1 -0
  54. package/dist/noiseMap/_types/index.js +2 -0
  55. package/dist/noiseMap/_types/index.js.map +1 -0
  56. package/dist/noiseMap/_types/noise2d.d.ts +21 -0
  57. package/dist/noiseMap/_types/noise2d.d.ts.map +1 -0
  58. package/dist/noiseMap/_types/noise2d.js +2 -0
  59. package/dist/noiseMap/_types/noise2d.js.map +1 -0
  60. package/dist/noiseMap/_types/noise3d.d.ts +24 -0
  61. package/dist/noiseMap/_types/noise3d.d.ts.map +1 -0
  62. package/dist/noiseMap/_types/noise3d.js +2 -0
  63. package/dist/noiseMap/_types/noise3d.js.map +1 -0
  64. package/dist/noiseMap/index.d.ts +6 -0
  65. package/dist/noiseMap/index.d.ts.map +1 -0
  66. package/dist/noiseMap/index.js +3 -0
  67. package/dist/noiseMap/index.js.map +1 -0
  68. package/dist/noiseMap/noise2d.d.ts +52 -0
  69. package/dist/noiseMap/noise2d.d.ts.map +1 -0
  70. package/dist/noiseMap/noise2d.js +105 -0
  71. package/dist/noiseMap/noise2d.js.map +1 -0
  72. package/dist/noiseMap/noise3d.d.ts +62 -0
  73. package/dist/noiseMap/noise3d.d.ts.map +1 -0
  74. package/dist/noiseMap/noise3d.js +129 -0
  75. package/dist/noiseMap/noise3d.js.map +1 -0
  76. package/dist/noiseTypes/cellular/2d.d.ts +36 -0
  77. package/dist/noiseTypes/cellular/2d.d.ts.map +1 -0
  78. package/dist/noiseTypes/cellular/2d.js +123 -0
  79. package/dist/noiseTypes/cellular/2d.js.map +1 -0
  80. package/dist/noiseTypes/cellular/3d.d.ts +32 -0
  81. package/dist/noiseTypes/cellular/3d.d.ts.map +1 -0
  82. package/dist/noiseTypes/cellular/3d.js +143 -0
  83. package/dist/noiseTypes/cellular/3d.js.map +1 -0
  84. package/dist/noiseTypes/cellular/_types/index.d.ts +2 -0
  85. package/dist/noiseTypes/cellular/_types/index.d.ts.map +1 -0
  86. package/dist/noiseTypes/cellular/_types/index.js +2 -0
  87. package/dist/noiseTypes/cellular/_types/index.js.map +1 -0
  88. package/dist/noiseTypes/cellular/_types/options.d.ts +37 -0
  89. package/dist/noiseTypes/cellular/_types/options.d.ts.map +1 -0
  90. package/dist/noiseTypes/cellular/_types/options.js +2 -0
  91. package/dist/noiseTypes/cellular/_types/options.js.map +1 -0
  92. package/dist/noiseTypes/cellular/defaultOptions.d.ts +4 -0
  93. package/dist/noiseTypes/cellular/defaultOptions.d.ts.map +1 -0
  94. package/dist/noiseTypes/cellular/defaultOptions.js +13 -0
  95. package/dist/noiseTypes/cellular/defaultOptions.js.map +1 -0
  96. package/dist/noiseTypes/cellular/floorDiv.d.ts +18 -0
  97. package/dist/noiseTypes/cellular/floorDiv.d.ts.map +1 -0
  98. package/dist/noiseTypes/cellular/floorDiv.js +28 -0
  99. package/dist/noiseTypes/cellular/floorDiv.js.map +1 -0
  100. package/dist/noiseTypes/cellular/index.d.ts +4 -0
  101. package/dist/noiseTypes/cellular/index.d.ts.map +1 -0
  102. package/dist/noiseTypes/cellular/index.js +3 -0
  103. package/dist/noiseTypes/cellular/index.js.map +1 -0
  104. package/dist/noiseTypes/cellular/options.d.ts +6 -0
  105. package/dist/noiseTypes/cellular/options.d.ts.map +1 -0
  106. package/dist/noiseTypes/cellular/options.js +20 -0
  107. package/dist/noiseTypes/cellular/options.js.map +1 -0
  108. package/dist/noiseTypes/index.d.ts +5 -0
  109. package/dist/noiseTypes/index.d.ts.map +1 -0
  110. package/dist/noiseTypes/index.js +5 -0
  111. package/dist/noiseTypes/index.js.map +1 -0
  112. package/dist/noiseTypes/perlin/2d.d.ts +23 -0
  113. package/dist/noiseTypes/perlin/2d.d.ts.map +1 -0
  114. package/dist/noiseTypes/perlin/2d.js +64 -0
  115. package/dist/noiseTypes/perlin/2d.js.map +1 -0
  116. package/dist/noiseTypes/perlin/3d.d.ts +24 -0
  117. package/dist/noiseTypes/perlin/3d.d.ts.map +1 -0
  118. package/dist/noiseTypes/perlin/3d.js +69 -0
  119. package/dist/noiseTypes/perlin/3d.js.map +1 -0
  120. package/dist/noiseTypes/perlin/_types/index.d.ts +2 -0
  121. package/dist/noiseTypes/perlin/_types/index.d.ts.map +1 -0
  122. package/dist/noiseTypes/perlin/_types/index.js +2 -0
  123. package/dist/noiseTypes/perlin/_types/index.js.map +1 -0
  124. package/dist/noiseTypes/perlin/_types/options.d.ts +22 -0
  125. package/dist/noiseTypes/perlin/_types/options.d.ts.map +1 -0
  126. package/dist/noiseTypes/perlin/_types/options.js +2 -0
  127. package/dist/noiseTypes/perlin/_types/options.js.map +1 -0
  128. package/dist/noiseTypes/perlin/defaultOptions.d.ts +4 -0
  129. package/dist/noiseTypes/perlin/defaultOptions.d.ts.map +1 -0
  130. package/dist/noiseTypes/perlin/defaultOptions.js +14 -0
  131. package/dist/noiseTypes/perlin/defaultOptions.js.map +1 -0
  132. package/dist/noiseTypes/perlin/index.d.ts +4 -0
  133. package/dist/noiseTypes/perlin/index.d.ts.map +1 -0
  134. package/dist/noiseTypes/perlin/index.js +3 -0
  135. package/dist/noiseTypes/perlin/index.js.map +1 -0
  136. package/dist/noiseTypes/perlin/options.d.ts +9 -0
  137. package/dist/noiseTypes/perlin/options.d.ts.map +1 -0
  138. package/dist/noiseTypes/perlin/options.js +17 -0
  139. package/dist/noiseTypes/perlin/options.js.map +1 -0
  140. package/dist/noiseTypes/perlin/sample2.d.ts +25 -0
  141. package/dist/noiseTypes/perlin/sample2.d.ts.map +1 -0
  142. package/dist/noiseTypes/perlin/sample2.js +58 -0
  143. package/dist/noiseTypes/perlin/sample2.js.map +1 -0
  144. package/dist/noiseTypes/perlin/sample3.d.ts +9 -0
  145. package/dist/noiseTypes/perlin/sample3.d.ts.map +1 -0
  146. package/dist/noiseTypes/perlin/sample3.js +38 -0
  147. package/dist/noiseTypes/perlin/sample3.js.map +1 -0
  148. package/dist/noiseTypes/simplex/2d.d.ts +23 -0
  149. package/dist/noiseTypes/simplex/2d.d.ts.map +1 -0
  150. package/dist/noiseTypes/simplex/2d.js +64 -0
  151. package/dist/noiseTypes/simplex/2d.js.map +1 -0
  152. package/dist/noiseTypes/simplex/3d.d.ts +25 -0
  153. package/dist/noiseTypes/simplex/3d.d.ts.map +1 -0
  154. package/dist/noiseTypes/simplex/3d.js +70 -0
  155. package/dist/noiseTypes/simplex/3d.js.map +1 -0
  156. package/dist/noiseTypes/simplex/_types/index.d.ts +2 -0
  157. package/dist/noiseTypes/simplex/_types/index.d.ts.map +1 -0
  158. package/dist/noiseTypes/simplex/_types/index.js +2 -0
  159. package/dist/noiseTypes/simplex/_types/index.js.map +1 -0
  160. package/dist/noiseTypes/simplex/_types/options.d.ts +33 -0
  161. package/dist/noiseTypes/simplex/_types/options.d.ts.map +1 -0
  162. package/dist/noiseTypes/simplex/_types/options.js +2 -0
  163. package/dist/noiseTypes/simplex/_types/options.js.map +1 -0
  164. package/dist/noiseTypes/simplex/defaultOptions.d.ts +4 -0
  165. package/dist/noiseTypes/simplex/defaultOptions.d.ts.map +1 -0
  166. package/dist/noiseTypes/simplex/defaultOptions.js +14 -0
  167. package/dist/noiseTypes/simplex/defaultOptions.js.map +1 -0
  168. package/dist/noiseTypes/simplex/index.d.ts +4 -0
  169. package/dist/noiseTypes/simplex/index.d.ts.map +1 -0
  170. package/dist/noiseTypes/simplex/index.js +3 -0
  171. package/dist/noiseTypes/simplex/index.js.map +1 -0
  172. package/dist/noiseTypes/simplex/options.d.ts +9 -0
  173. package/dist/noiseTypes/simplex/options.d.ts.map +1 -0
  174. package/dist/noiseTypes/simplex/options.js +17 -0
  175. package/dist/noiseTypes/simplex/options.js.map +1 -0
  176. package/dist/noiseTypes/simplex/sample2.d.ts +32 -0
  177. package/dist/noiseTypes/simplex/sample2.d.ts.map +1 -0
  178. package/dist/noiseTypes/simplex/sample2.js +94 -0
  179. package/dist/noiseTypes/simplex/sample2.js.map +1 -0
  180. package/dist/noiseTypes/simplex/sample3.d.ts +19 -0
  181. package/dist/noiseTypes/simplex/sample3.d.ts.map +1 -0
  182. package/dist/noiseTypes/simplex/sample3.js +110 -0
  183. package/dist/noiseTypes/simplex/sample3.js.map +1 -0
  184. package/dist/noiseTypes/white/2d.d.ts +18 -0
  185. package/dist/noiseTypes/white/2d.d.ts.map +1 -0
  186. package/dist/noiseTypes/white/2d.js +33 -0
  187. package/dist/noiseTypes/white/2d.js.map +1 -0
  188. package/dist/noiseTypes/white/3d.d.ts +17 -0
  189. package/dist/noiseTypes/white/3d.d.ts.map +1 -0
  190. package/dist/noiseTypes/white/3d.js +34 -0
  191. package/dist/noiseTypes/white/3d.js.map +1 -0
  192. package/dist/noiseTypes/white/_types/index.d.ts +2 -0
  193. package/dist/noiseTypes/white/_types/index.d.ts.map +1 -0
  194. package/dist/noiseTypes/white/_types/index.js +2 -0
  195. package/dist/noiseTypes/white/_types/index.js.map +1 -0
  196. package/dist/noiseTypes/white/_types/options.d.ts +28 -0
  197. package/dist/noiseTypes/white/_types/options.d.ts.map +1 -0
  198. package/dist/noiseTypes/white/_types/options.js +2 -0
  199. package/dist/noiseTypes/white/_types/options.js.map +1 -0
  200. package/dist/noiseTypes/white/defaultOptions.d.ts +4 -0
  201. package/dist/noiseTypes/white/defaultOptions.d.ts.map +1 -0
  202. package/dist/noiseTypes/white/defaultOptions.js +10 -0
  203. package/dist/noiseTypes/white/defaultOptions.js.map +1 -0
  204. package/dist/noiseTypes/white/index.d.ts +4 -0
  205. package/dist/noiseTypes/white/index.d.ts.map +1 -0
  206. package/dist/noiseTypes/white/index.js +3 -0
  207. package/dist/noiseTypes/white/index.js.map +1 -0
  208. package/dist/noiseTypes/white/options.d.ts +6 -0
  209. package/dist/noiseTypes/white/options.d.ts.map +1 -0
  210. package/dist/noiseTypes/white/options.js +14 -0
  211. package/dist/noiseTypes/white/options.js.map +1 -0
  212. package/package.json +74 -3
  213. package/src/fractal/_types/fractalOptions.ts +35 -0
  214. package/src/fractal/_types/index.ts +1 -0
  215. package/src/fractal/fractal.ts +41 -0
  216. package/src/fractal/index.ts +4 -0
  217. package/src/fractal/options.ts +62 -0
  218. package/src/gradient/cubeEdge3.ts +47 -0
  219. package/src/gradient/index.ts +1 -0
  220. package/src/hashes/hash.ts +41 -0
  221. package/src/hashes/hashes.ts +2 -0
  222. package/src/hashes/index.ts +1 -0
  223. package/src/hashes/lowbias.ts +18 -0
  224. package/src/index.ts +4 -0
  225. package/src/noiseMap/_types/index.ts +2 -0
  226. package/src/noiseMap/_types/noise2d.ts +21 -0
  227. package/src/noiseMap/_types/noise3d.ts +24 -0
  228. package/src/noiseMap/index.ts +7 -0
  229. package/src/noiseMap/noise2d.ts +117 -0
  230. package/src/noiseMap/noise3d.ts +146 -0
  231. package/src/noiseTypes/cellular/2d.ts +142 -0
  232. package/src/noiseTypes/cellular/3d.ts +171 -0
  233. package/src/noiseTypes/cellular/_types/index.ts +1 -0
  234. package/src/noiseTypes/cellular/_types/options.ts +37 -0
  235. package/src/noiseTypes/cellular/defaultOptions.ts +15 -0
  236. package/src/noiseTypes/cellular/floorDiv.ts +27 -0
  237. package/src/noiseTypes/cellular/index.ts +4 -0
  238. package/src/noiseTypes/cellular/options.ts +20 -0
  239. package/src/noiseTypes/index.ts +4 -0
  240. package/src/noiseTypes/perlin/2d.ts +86 -0
  241. package/src/noiseTypes/perlin/3d.ts +93 -0
  242. package/src/noiseTypes/perlin/_types/index.ts +1 -0
  243. package/src/noiseTypes/perlin/_types/options.ts +23 -0
  244. package/src/noiseTypes/perlin/defaultOptions.ts +16 -0
  245. package/src/noiseTypes/perlin/index.ts +4 -0
  246. package/src/noiseTypes/perlin/options.ts +19 -0
  247. package/src/noiseTypes/perlin/sample2.ts +62 -0
  248. package/src/noiseTypes/perlin/sample3.ts +43 -0
  249. package/src/noiseTypes/simplex/2d.ts +86 -0
  250. package/src/noiseTypes/simplex/3d.ts +94 -0
  251. package/src/noiseTypes/simplex/_types/index.ts +1 -0
  252. package/src/noiseTypes/simplex/_types/options.ts +34 -0
  253. package/src/noiseTypes/simplex/defaultOptions.ts +16 -0
  254. package/src/noiseTypes/simplex/index.ts +4 -0
  255. package/src/noiseTypes/simplex/options.ts +19 -0
  256. package/src/noiseTypes/simplex/sample2.ts +103 -0
  257. package/src/noiseTypes/simplex/sample3.ts +128 -0
  258. package/src/noiseTypes/white/2d.ts +44 -0
  259. package/src/noiseTypes/white/3d.ts +46 -0
  260. package/src/noiseTypes/white/_types/index.ts +1 -0
  261. package/src/noiseTypes/white/_types/options.ts +28 -0
  262. package/src/noiseTypes/white/defaultOptions.ts +12 -0
  263. package/src/noiseTypes/white/index.ts +4 -0
  264. 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,4 @@
1
+ export * as White2 from './2d.js';
2
+ export * as White3 from './3d.js';
3
+
4
+ export type * from './_types/index.js';
@@ -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
+ }