@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,117 @@
1
+ import type { Noise2D } from './_types/noise2d.js';
2
+
3
+ const HEADER_LENGTH = 2;
4
+
5
+ function validateBounds(noise: Noise2D, x: number, y: number): void {
6
+ const [width, height] = noise;
7
+ if (!(Number.isInteger(x) && Number.isInteger(y))) throw new RangeError('Invalid x or y');
8
+ if (x < 0 || x >= width || y < 0 || y >= height) throw new RangeError('Invalid bounds');
9
+ }
10
+
11
+ function getIndex(noise: Noise2D, x: number, y: number): number {
12
+ /** @todo - should validation have an opt out? */
13
+ validateBounds(noise, x, y);
14
+ const [width] = noise;
15
+ return y * width + x + HEADER_LENGTH;
16
+ }
17
+
18
+ /**
19
+ * Allocates a zero-filled `width` × `height` map. The caller owns it; every
20
+ * generator returns a map made here and keeps no reference to it.
21
+ *
22
+ * Throws a `RangeError` unless both sizes are positive integers. The sizes are
23
+ * stored in the map's Float32 header, which holds integers exactly only up to
24
+ * 2^24, so larger sizes are not supported.
25
+ */
26
+ export function create(width: number, height: number): Noise2D {
27
+ if (!(Number.isInteger(width) && Number.isInteger(height)))
28
+ throw new RangeError('Invalid width or height');
29
+ if (width <= 0 || height <= 0) throw new RangeError('Invalid width or height');
30
+
31
+ const noise = new Float32Array(width * height + HEADER_LENGTH) as Noise2D;
32
+ noise[0] = width;
33
+ noise[1] = height;
34
+ return noise;
35
+ }
36
+
37
+ /** Number of columns, read from the map's header. */
38
+ export function width(noise: Noise2D): number {
39
+ return noise[0];
40
+ }
41
+
42
+ /** Number of rows, read from the map's header. */
43
+ export function height(noise: Noise2D): number {
44
+ return noise[1];
45
+ }
46
+
47
+ /**
48
+ * The map's cells as a row-major view over the same buffer, without the header:
49
+ * cell (x, y) is at `y * width + x`. Writes through it change the map, and it
50
+ * stays valid as long as the map does. There are no bounds checks, unlike
51
+ * `get`/`set`, so it suits whole-map passes whose loops already stay in range.
52
+ * Each call allocates a small view object, so take it once per pass, not per cell.
53
+ */
54
+ export function getValues(noise: Noise2D): Float32Array {
55
+ return noise.subarray(HEADER_LENGTH);
56
+ }
57
+
58
+ /**
59
+ * Writes `value` to cell (`x`, `y`), rounded to Float32 like every map value.
60
+ * Throws a `RangeError` unless both coordinates are integers inside the map.
61
+ * The check runs on every call; for whole-map passes use {@link getValues}.
62
+ */
63
+ export function set(noise: Noise2D, x: number, y: number, value: number): void {
64
+ noise[getIndex(noise, x, y)] = value;
65
+ }
66
+
67
+ /**
68
+ * Reads cell (`x`, `y`). Throws a `RangeError` unless both coordinates are
69
+ * integers inside the map. The check runs on every call; for whole-map passes
70
+ * use {@link getValues}.
71
+ */
72
+ export function get(noise: Noise2D, x: number, y: number): number {
73
+ return noise[getIndex(noise, x, y)] as number;
74
+ }
75
+
76
+ /**
77
+ * Calls `callback` once per cell in row-major order (x fastest), with the
78
+ * cell's value and coordinates. Writing to the map from the callback is safe:
79
+ * each value is read just before its call. Each read is bounds-checked like
80
+ * {@link get}, so hot loops should use {@link getValues} instead.
81
+ */
82
+ export function forEach(
83
+ noise: Noise2D,
84
+ callback: (value: number, x: number, y: number) => void
85
+ ): void {
86
+ const [width, height] = noise;
87
+ for (let y = 0; y < height; y++) {
88
+ for (let x = 0; x < width; x++) {
89
+ callback(get(noise, x, y), x, y);
90
+ }
91
+ }
92
+ }
93
+
94
+ /**
95
+ * A new greyscale RGBA image of the map: 4 bytes per cell in row-major order,
96
+ * with each channel set to `value * 255` truncated and alpha 255.
97
+ *
98
+ * Expects values in [0, 1], which every generator produces. Values outside it
99
+ * wrap around rather than clamp (1.5 becomes 126, not 255), because `Uint8Array`
100
+ * stores numbers modulo 256. For `ImageData`, wrap the result's bytes in a
101
+ * `Uint8ClampedArray`; no copy is needed.
102
+ */
103
+ export function toPixelBuffer(noise: Noise2D): Uint8Array {
104
+ const [width, height] = noise;
105
+ const pixelBuffer = new Uint8Array(width * height * 4);
106
+ for (let y = 0; y < height; y++) {
107
+ for (let x = 0; x < width; x++) {
108
+ const index = (y * width + x) * 4;
109
+ const value = get(noise, x, y);
110
+ pixelBuffer[index] = value * 255;
111
+ pixelBuffer[index + 1] = value * 255;
112
+ pixelBuffer[index + 2] = value * 255;
113
+ pixelBuffer[index + 3] = 255;
114
+ }
115
+ }
116
+ return pixelBuffer;
117
+ }
@@ -0,0 +1,146 @@
1
+ import type { Noise3D } from './_types/noise3d.js';
2
+
3
+ const HEADER_LENGTH = 3;
4
+
5
+ function validateBounds(noise: Noise3D, x: number, y: number, z: number): void {
6
+ const [width, height, depth] = noise;
7
+ if (!(Number.isInteger(x) && Number.isInteger(y) && Number.isInteger(z)))
8
+ throw new RangeError('Invalid x, y, or z');
9
+ if (x < 0 || x >= width || y < 0 || y >= height || z < 0 || z >= depth)
10
+ throw new RangeError('Invalid bounds');
11
+ }
12
+
13
+ function getIndex(noise: Noise3D, x: number, y: number, z: number): number {
14
+ /** @todo - should validation have an opt out? */
15
+ validateBounds(noise, x, y, z);
16
+ const [width, height] = noise;
17
+ // +3 skips the [width, height, depth] header
18
+ return (z * height + y) * width + x + HEADER_LENGTH;
19
+ }
20
+
21
+ /**
22
+ * Allocates a zero-filled `width` × `height` × `depth` map. The caller owns it;
23
+ * every generator returns a map made here and keeps no reference to it.
24
+ *
25
+ * Throws a `RangeError` unless all three sizes are positive integers. The sizes
26
+ * are stored in the map's Float32 header, which holds integers exactly only up
27
+ * to 2^24, so larger sizes are not supported.
28
+ */
29
+ export function create(width: number, height: number, depth: number): Noise3D {
30
+ if (!(Number.isInteger(width) && Number.isInteger(height) && Number.isInteger(depth)))
31
+ throw new RangeError('Invalid width, height, or depth');
32
+ if (width <= 0 || height <= 0 || depth <= 0)
33
+ throw new RangeError('Invalid width, height, or depth');
34
+
35
+ const noise = new Float32Array(width * height * depth + 3) as Noise3D;
36
+ noise[0] = width;
37
+ noise[1] = height;
38
+ noise[2] = depth;
39
+ return noise;
40
+ }
41
+
42
+ /** Number of columns, read from the map's header. */
43
+ export function width(noise: Noise3D): number {
44
+ return noise[0];
45
+ }
46
+
47
+ /** Number of rows per layer, read from the map's header. */
48
+ export function height(noise: Noise3D): number {
49
+ return noise[1];
50
+ }
51
+
52
+ /** Number of layers, read from the map's header. */
53
+ export function depth(noise: Noise3D): number {
54
+ return noise[2];
55
+ }
56
+
57
+ /**
58
+ * The map's cells as a view over the same buffer, without the header. x varies
59
+ * fastest, then y, then z: cell (x, y, z) is at `(z * height + y) * width + x`.
60
+ * Writes through it change the map, and it stays valid as long as the map does.
61
+ * There are no bounds checks, unlike `get`/`set`, so it suits whole-map passes
62
+ * whose loops already stay in range. Each call allocates a small view object, so
63
+ * take it once per pass, not per cell.
64
+ */
65
+ export function getValues(noise: Noise3D): Float32Array {
66
+ return noise.subarray(HEADER_LENGTH);
67
+ }
68
+
69
+ /**
70
+ * Writes `value` to cell (`x`, `y`, `z`), rounded to Float32 like every map
71
+ * value. Throws a `RangeError` unless all three coordinates are integers inside
72
+ * the map. The check runs on every call; for whole-map passes use
73
+ * {@link getValues}.
74
+ */
75
+ export function set(noise: Noise3D, x: number, y: number, z: number, value: number): void {
76
+ noise[getIndex(noise, x, y, z)] = value;
77
+ }
78
+
79
+ /**
80
+ * Reads cell (`x`, `y`, `z`). Throws a `RangeError` unless all three
81
+ * coordinates are integers inside the map. The check runs on every call; for
82
+ * whole-map passes use {@link getValues}.
83
+ */
84
+ export function get(noise: Noise3D, x: number, y: number, z: number): number {
85
+ return noise[getIndex(noise, x, y, z)] as number;
86
+ }
87
+
88
+ /**
89
+ * Calls `callback` once per cell in storage order (x fastest, then y, then z),
90
+ * with the cell's value and coordinates. Writing to the map from the callback
91
+ * is safe: each value is read just before its call. Each read is bounds-checked
92
+ * like {@link get}, so hot loops should use {@link getValues} instead.
93
+ */
94
+ export function forEach(
95
+ noise: Noise3D,
96
+ callback: (value: number, x: number, y: number, z: number) => void
97
+ ): void {
98
+ const [width, height, depth] = noise;
99
+ for (let z = 0; z < depth; z++) {
100
+ for (let y = 0; y < height; y++) {
101
+ for (let x = 0; x < width; x++) {
102
+ callback(get(noise, x, y, z), x, y, z);
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+ /**
109
+ * A new greyscale RGBA image of layer `layer` (a z index): 4 bytes per cell in
110
+ * row-major order, with each channel set to `value * 255` truncated and alpha
111
+ * 255. Throws a `RangeError` unless `layer` is an integer inside the map.
112
+ *
113
+ * Expects values in [0, 1], which every generator produces. Values outside it
114
+ * wrap around rather than clamp (1.5 becomes 126, not 255), because `Uint8Array`
115
+ * stores numbers modulo 256. For `ImageData`, wrap the result's bytes in a
116
+ * `Uint8ClampedArray`; no copy is needed.
117
+ */
118
+ export function toPixelBuffer(noise: Noise3D, layer: number): Uint8Array {
119
+ const [width, height] = noise;
120
+ const pixelBuffer = new Uint8Array(width * height * 4);
121
+ for (let y = 0; y < height; y++) {
122
+ for (let x = 0; x < width; x++) {
123
+ const index = (y * width + x) * 4;
124
+ const value = get(noise, x, y, layer);
125
+ pixelBuffer[index] = value * 255;
126
+ pixelBuffer[index + 1] = value * 255;
127
+ pixelBuffer[index + 2] = value * 255;
128
+ pixelBuffer[index + 3] = 255;
129
+ }
130
+ }
131
+ return pixelBuffer;
132
+ }
133
+
134
+ /**
135
+ * {@link toPixelBuffer} for every layer, in z order: `depth` separate buffers of
136
+ * `width * height * 4` bytes each.
137
+ */
138
+ export function toPixelBuffers(noise: Noise3D): Uint8Array[] {
139
+ const [, , depth] = noise;
140
+ const pixelBuffers: Uint8Array[] = [];
141
+ for (let z = 0; z < depth; z++) {
142
+ const pixelBuffer = toPixelBuffer(noise, z);
143
+ pixelBuffers.push(pixelBuffer);
144
+ }
145
+ return pixelBuffers;
146
+ }
@@ -0,0 +1,142 @@
1
+ import { Hashes } from '../../hashes/index.js';
2
+ import { Noise2D } from '../../noiseMap/index.js';
3
+ import { DEFAULT_CELLULAR_2D_OPTIONS } from './defaultOptions.js';
4
+ import { floorDiv, floorMod } from './floorDiv.js';
5
+ import { validateOptions2 } from './options.js';
6
+
7
+ import type { Cellular2dOptions } from './_types/index.js';
8
+
9
+ /** Numbers stored per cell in the point grid: the point's x and y within its cell. */
10
+ const CELL_STRIDE = 2;
11
+ /**
12
+ * Extra cells stored on each side of the cells the map covers. The 3×3 search
13
+ * needs one; the second leaves room for a 5×5 search without resizing.
14
+ */
15
+ const CELL_PADDING = 2;
16
+ const CELL_PADDING_TOTAL = CELL_PADDING * 2;
17
+
18
+ /** Scales a 32-bit hash into [0, 1). Exact, since it is a power of two. */
19
+ const INV_2_POW_32 = 2 ** -32;
20
+
21
+ /**
22
+ * Creates a `width` × `height` map of cellular (Worley) noise in [0, 1]. Each
23
+ * value is the distance from its cell to the nearest feature point (F1),
24
+ * divided by the diagonal of one grid cell. The caller owns the returned map.
25
+ *
26
+ * The world is divided into `cellWidth` × `cellHeight` grid cells anchored at
27
+ * world (0, 0), and each grid cell holds one point whose position depends only
28
+ * on the cell's coordinates and the seed. Each value therefore depends only on
29
+ * its world cell (`x + offsetX`, `y + offsetY`) and the other options, never on
30
+ * the map's size, so chunks of any size created with the same options and their
31
+ * world origin as the offset agree bit for bit wherever they overlap.
32
+ *
33
+ * The mapping to [0, 1] is fixed by the cell size rather than stretched per
34
+ * map. The diagonal is the true upper bound but is almost never reached: 256²
35
+ * maps span roughly 0–0.8 and average about 0.3, so the result reads darker
36
+ * than gradient noise centred on 0.5.
37
+ *
38
+ * Distances are measured from the sample's position inside its own grid cell,
39
+ * not from absolute world coordinates, so precision does not fall off far from
40
+ * the origin. The point hash takes 32-bit cell coordinates, so the pattern
41
+ * repeats every 2^32 grid cells along each axis.
42
+ *
43
+ * Known limitation: only the 3×3 block of grid cells around each sample is
44
+ * searched, so the true nearest point is missed when it lies two cells over.
45
+ * For square cells that is rare (about one value in a 256² map), but it grows
46
+ * with the cell's aspect ratio and shows as visible seams: roughly 0.15% of
47
+ * values at 10 × 20, 3% at 5 × 20 and 8% at 3 × 17. Searching
48
+ * `ceil(diagonal / cellSize)` cells along each axis would make it exact.
49
+ *
50
+ * Throws if the size is not a positive integer or an option breaks its contract
51
+ * in {@link Cellular2dOptions}.
52
+ */
53
+ export function create(
54
+ width: number,
55
+ height: number,
56
+ options: Partial<Cellular2dOptions> = {}
57
+ ): Noise2D {
58
+ const noise = Noise2D.create(width, height);
59
+
60
+ const config = { ...DEFAULT_CELLULAR_2D_OPTIONS, ...options };
61
+ validateOptions2(config);
62
+
63
+ const values = Noise2D.getValues(noise);
64
+ const { cellWidth, cellHeight, offsetX, offsetY, seed } = config;
65
+
66
+ // Grid cell holding the map's first column / row, and how far into it that
67
+ // column / row sits. Both are exact for any safe-integer offset.
68
+ const firstCellX = floorDiv(offsetX, cellWidth);
69
+ const firstCellY = floorDiv(offsetY, cellHeight);
70
+ const firstLocalX = floorMod(offsetX, cellWidth);
71
+ const firstLocalY = floorMod(offsetY, cellHeight);
72
+
73
+ const gridSizeX = Math.ceil(width / cellWidth) + CELL_PADDING_TOTAL;
74
+ const gridSizeY = Math.ceil(height / cellHeight) + CELL_PADDING_TOTAL;
75
+
76
+ /**
77
+ * Each grid cell's point, relative to the cell's own corner, interleaved
78
+ * (x, y) and stored row by row. Slot (0, 0) is world grid cell
79
+ * (`firstCellX - CELL_PADDING`, `firstCellY - CELL_PADDING`).
80
+ */
81
+ const cellPoints = new Float64Array(gridSizeX * gridSizeY * CELL_STRIDE);
82
+
83
+ let index = 0;
84
+ for (let slotY = 0; slotY < gridSizeY; slotY++) {
85
+ const cellY = firstCellY - CELL_PADDING + slotY;
86
+ for (let slotX = 0; slotX < gridSizeX; slotX++) {
87
+ const cellX = firstCellX - CELL_PADDING + slotX;
88
+ const hash = Hashes.hash2(cellX, cellY, seed);
89
+ const xHash = Hashes.lowbias32(hash);
90
+ const yHash = Hashes.lowbias32(xHash);
91
+ cellPoints[index++] = xHash * INV_2_POW_32 * cellWidth;
92
+ cellPoints[index++] = yHash * INV_2_POW_32 * cellHeight;
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Distance from (`localX`, `localY`) inside grid slot (`slotX`, `slotY`) to
98
+ * the nearest point in the 3×3 block of slots around it.
99
+ */
100
+ function getNearestDistance(slotX: number, slotY: number, localX: number, localY: number) {
101
+ let minDistanceSq = Number.POSITIVE_INFINITY;
102
+
103
+ for (let dy = -1; dy <= 1; dy++) {
104
+ const rowStart = (slotY + dy) * gridSizeX + slotX;
105
+ const cellOriginY = dy * cellHeight - localY;
106
+ for (let dx = -1; dx <= 1; dx++) {
107
+ const pointIndex = (rowStart + dx) * CELL_STRIDE;
108
+ const distanceX = dx * cellWidth + (cellPoints[pointIndex] as number) - localX;
109
+ const distanceY = cellOriginY + (cellPoints[pointIndex + 1] as number);
110
+ const distanceSq = distanceX * distanceX + distanceY * distanceY;
111
+ if (distanceSq < minDistanceSq) minDistanceSq = distanceSq;
112
+ }
113
+ }
114
+
115
+ return Math.sqrt(minDistanceSq);
116
+ }
117
+
118
+ const invMaxDistance = 1 / Math.sqrt(cellWidth * cellWidth + cellHeight * cellHeight);
119
+
120
+ // Walk the map tracking which slot each sample is in and where inside it,
121
+ // stepping into the next slot when the local position reaches the cell size.
122
+ index = 0;
123
+ let slotY = CELL_PADDING;
124
+ let localY = firstLocalY;
125
+ for (let y = 0; y < height; y++) {
126
+ let slotX = CELL_PADDING;
127
+ let localX = firstLocalX;
128
+ for (let x = 0; x < width; x++) {
129
+ values[index++] = getNearestDistance(slotX, slotY, localX, localY) * invMaxDistance;
130
+ if (++localX === cellWidth) {
131
+ localX = 0;
132
+ slotX++;
133
+ }
134
+ }
135
+ if (++localY === cellHeight) {
136
+ localY = 0;
137
+ slotY++;
138
+ }
139
+ }
140
+
141
+ return noise;
142
+ }
@@ -0,0 +1,171 @@
1
+ import { Hashes } from '../../hashes/index.js';
2
+ import { Noise3D } from '../../noiseMap/index.js';
3
+ import { DEFAULT_CELLULAR_3D_OPTIONS } from './defaultOptions.js';
4
+ import { floorDiv, floorMod } from './floorDiv.js';
5
+ import { validateOptions3 } from './options.js';
6
+
7
+ import type { Cellular3dOptions } from './_types/index.js';
8
+
9
+ /** Numbers stored per cell in the point grid: the point's x, y and z within its cell. */
10
+ const CELL_STRIDE = 3;
11
+ /**
12
+ * Extra cells stored on each side of the cells the map covers. The 3×3×3
13
+ * search needs one; the second leaves room for a 5×5×5 search without resizing.
14
+ */
15
+ const CELL_PADDING = 2;
16
+ const CELL_PADDING_TOTAL = CELL_PADDING * 2;
17
+
18
+ /** Scales a 32-bit hash into [0, 1). Exact, since it is a power of two. */
19
+ const INV_2_POW_32 = 2 ** -32;
20
+
21
+ /**
22
+ * Creates a `width` × `height` × `depth` map of cellular (Worley) noise in
23
+ * [0, 1]. Each value is the distance from its cell to the nearest feature point
24
+ * (F1), divided by the diagonal of one grid cell. The caller owns the returned
25
+ * map.
26
+ *
27
+ * Works like the 2D version with a third axis: grid cells of `cellWidth` ×
28
+ * `cellHeight` × `cellDepth` anchored at world (0, 0, 0), one hashed point per
29
+ * grid cell, and values that depend only on the world cell (`x + offsetX`,
30
+ * `y + offsetY`, `z + offsetZ`), so chunks of any size agree bit for bit where
31
+ * they overlap. Distances are measured from inside the sample's own grid cell,
32
+ * so precision holds far from the origin, and the pattern repeats every 2^32
33
+ * grid cells along each axis.
34
+ *
35
+ * The mapping to [0, 1] is fixed by the cell size, as in 2D. The 3D diagonal is
36
+ * further from the typical nearest distance than the 2D one, so the top of the
37
+ * range is used even less: 64³ maps span roughly 0–0.65 (2D reaches about 0.8)
38
+ * and average about 0.3.
39
+ *
40
+ * Known limitation, as in 2D: only the 3×3×3 block of grid cells around each
41
+ * sample is searched (27 points per value), so the true nearest point is missed
42
+ * when it lies two cells over. Cube-shaped cells are effectively unaffected, but
43
+ * misses grow with the cell's aspect ratio and show as visible seams: roughly
44
+ * 0.05% of values at 10 × 10 × 20 and 2% at 5 × 10 × 20.
45
+ *
46
+ * Throws if the size is not a positive integer or an option breaks its contract
47
+ * in {@link Cellular3dOptions}.
48
+ */
49
+ export function create(
50
+ width: number,
51
+ height: number,
52
+ depth: number,
53
+ options: Partial<Cellular3dOptions> = {}
54
+ ): Noise3D {
55
+ const noise = Noise3D.create(width, height, depth);
56
+
57
+ const config = { ...DEFAULT_CELLULAR_3D_OPTIONS, ...options };
58
+ validateOptions3(config);
59
+
60
+ const values = Noise3D.getValues(noise);
61
+ const { cellWidth, cellHeight, cellDepth, offsetX, offsetY, offsetZ, seed } = config;
62
+
63
+ // Grid cell holding the map's first column / row / layer, and how far into it
64
+ // that column / row / layer sits. Exact for any safe-integer offset.
65
+ const firstCellX = floorDiv(offsetX, cellWidth);
66
+ const firstCellY = floorDiv(offsetY, cellHeight);
67
+ const firstCellZ = floorDiv(offsetZ, cellDepth);
68
+ const firstLocalX = floorMod(offsetX, cellWidth);
69
+ const firstLocalY = floorMod(offsetY, cellHeight);
70
+ const firstLocalZ = floorMod(offsetZ, cellDepth);
71
+
72
+ const gridSizeX = Math.ceil(width / cellWidth) + CELL_PADDING_TOTAL;
73
+ const gridSizeY = Math.ceil(height / cellHeight) + CELL_PADDING_TOTAL;
74
+ const gridSizeZ = Math.ceil(depth / cellDepth) + CELL_PADDING_TOTAL;
75
+ const gridLayerSize = gridSizeX * gridSizeY;
76
+
77
+ /**
78
+ * Each grid cell's point, relative to the cell's own corner, interleaved
79
+ * (x, y, z) and stored row by row, then layer by layer. Slot (0, 0, 0) is
80
+ * world grid cell (`firstCellX - CELL_PADDING`, `firstCellY - CELL_PADDING`,
81
+ * `firstCellZ - CELL_PADDING`).
82
+ */
83
+ const cellPoints = new Float64Array(gridLayerSize * gridSizeZ * CELL_STRIDE);
84
+
85
+ let index = 0;
86
+ for (let slotZ = 0; slotZ < gridSizeZ; slotZ++) {
87
+ const cellZ = firstCellZ - CELL_PADDING + slotZ;
88
+ for (let slotY = 0; slotY < gridSizeY; slotY++) {
89
+ const cellY = firstCellY - CELL_PADDING + slotY;
90
+ for (let slotX = 0; slotX < gridSizeX; slotX++) {
91
+ const cellX = firstCellX - CELL_PADDING + slotX;
92
+ const hash = Hashes.hash3(cellX, cellY, cellZ, seed);
93
+ const xHash = Hashes.lowbias32(hash);
94
+ const yHash = Hashes.lowbias32(xHash);
95
+ const zHash = Hashes.lowbias32(yHash);
96
+ cellPoints[index++] = xHash * INV_2_POW_32 * cellWidth;
97
+ cellPoints[index++] = yHash * INV_2_POW_32 * cellHeight;
98
+ cellPoints[index++] = zHash * INV_2_POW_32 * cellDepth;
99
+ }
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Distance from (`localX`, `localY`, `localZ`) inside grid slot (`slotX`,
105
+ * `slotY`, `slotZ`) to the nearest point in the 3×3×3 block of slots around it.
106
+ */
107
+ function getNearestDistance(
108
+ slotX: number,
109
+ slotY: number,
110
+ slotZ: number,
111
+ localX: number,
112
+ localY: number,
113
+ localZ: number
114
+ ) {
115
+ let minDistanceSq = Number.POSITIVE_INFINITY;
116
+
117
+ for (let dz = -1; dz <= 1; dz++) {
118
+ const layerStart = (slotZ + dz) * gridLayerSize;
119
+ const cellOriginZ = dz * cellDepth - localZ;
120
+ for (let dy = -1; dy <= 1; dy++) {
121
+ const rowStart = layerStart + (slotY + dy) * gridSizeX + slotX;
122
+ const cellOriginY = dy * cellHeight - localY;
123
+ for (let dx = -1; dx <= 1; dx++) {
124
+ const pointIndex = (rowStart + dx) * CELL_STRIDE;
125
+ const distanceX = dx * cellWidth + (cellPoints[pointIndex] as number) - localX;
126
+ const distanceY = cellOriginY + (cellPoints[pointIndex + 1] as number);
127
+ const distanceZ = cellOriginZ + (cellPoints[pointIndex + 2] as number);
128
+ const distanceSq = distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ;
129
+ if (distanceSq < minDistanceSq) minDistanceSq = distanceSq;
130
+ }
131
+ }
132
+ }
133
+
134
+ return Math.sqrt(minDistanceSq);
135
+ }
136
+
137
+ const invMaxDistance =
138
+ 1 / Math.sqrt(cellWidth * cellWidth + cellHeight * cellHeight + cellDepth * cellDepth);
139
+
140
+ // Walk the map tracking which slot each sample is in and where inside it,
141
+ // stepping into the next slot when the local position reaches the cell size.
142
+ index = 0;
143
+ let slotZ = CELL_PADDING;
144
+ let localZ = firstLocalZ;
145
+ for (let z = 0; z < depth; z++) {
146
+ let slotY = CELL_PADDING;
147
+ let localY = firstLocalY;
148
+ for (let y = 0; y < height; y++) {
149
+ let slotX = CELL_PADDING;
150
+ let localX = firstLocalX;
151
+ for (let x = 0; x < width; x++) {
152
+ values[index++] =
153
+ getNearestDistance(slotX, slotY, slotZ, localX, localY, localZ) * invMaxDistance;
154
+ if (++localX === cellWidth) {
155
+ localX = 0;
156
+ slotX++;
157
+ }
158
+ }
159
+ if (++localY === cellHeight) {
160
+ localY = 0;
161
+ slotY++;
162
+ }
163
+ }
164
+ if (++localZ === cellDepth) {
165
+ localZ = 0;
166
+ slotZ++;
167
+ }
168
+ }
169
+
170
+ return noise;
171
+ }
@@ -0,0 +1 @@
1
+ export type { Cellular2dOptions, Cellular3dOptions } from './options.js';
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Options for 2D cellular 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 Cellular2dOptions {
6
+ /**
7
+ * Selects which point each grid cell gets. 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
+ * Width of one grid cell in map cells. Each grid cell holds one point, so this
13
+ * sets the spacing of the pattern. A positive safe integer: whole cells keep
14
+ * the grid's edges on map cells, which lets each sample's position inside its
15
+ * grid cell be tracked exactly, and the lower bound of 1 keeps the number of
16
+ * grid cells a map stores no larger than the map itself.
17
+ */
18
+ cellWidth: number;
19
+ /** Height of one grid cell. Same contract as {@link cellWidth}. */
20
+ cellHeight: number;
21
+ /**
22
+ * World x of the map's first column, in map cells. A safe integer (|v| < 2^53),
23
+ * so `x + offsetX` is exact and a chunk at offset 64 reproduces column 64 of a
24
+ * chunk at offset 0 bit for bit. Larger integers would round when added to.
25
+ */
26
+ offsetX: number;
27
+ /** World y of the map's first row. Same contract as {@link offsetX}. */
28
+ offsetY: number;
29
+ }
30
+
31
+ /** Options for 3D cellular noise: the 2D options plus a depth and a z offset. */
32
+ export interface Cellular3dOptions extends Cellular2dOptions {
33
+ /** Depth of one grid cell. Same contract as {@link Cellular2dOptions.cellWidth}. */
34
+ cellDepth: number;
35
+ /** World z of the map's first layer. Same contract as {@link Cellular2dOptions.offsetX}. */
36
+ offsetZ: number;
37
+ }
@@ -0,0 +1,15 @@
1
+ import type { Cellular2dOptions, Cellular3dOptions } from './_types/index.js';
2
+
3
+ export const DEFAULT_CELLULAR_2D_OPTIONS: Cellular2dOptions = {
4
+ seed: 0,
5
+ cellWidth: 10,
6
+ cellHeight: 10,
7
+ offsetX: 0,
8
+ offsetY: 0,
9
+ };
10
+
11
+ export const DEFAULT_CELLULAR_3D_OPTIONS: Cellular3dOptions = {
12
+ ...DEFAULT_CELLULAR_2D_OPTIONS,
13
+ cellDepth: 10,
14
+ offsetZ: 0,
15
+ };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * `floor(value / divisor)` for a safe-integer `value` and a positive
3
+ * safe-integer `divisor`, exact across the whole safe range.
4
+ *
5
+ * `Math.floor(value / divisor)` is not: the division rounds, so near ±2^53 the
6
+ * floor can land one off, and multiplying the quotient back by `divisor` to
7
+ * check it can leave the safe range and round again. `%` is exact, and
8
+ * `value - remainder` keeps the sign of `value` with a smaller magnitude, so it
9
+ * stays exact and divides evenly.
10
+ */
11
+ export function floorDiv(value: number, divisor: number): number {
12
+ const remainder = value % divisor;
13
+ const quotient = (value - remainder) / divisor;
14
+ return remainder < 0 ? quotient - 1 : quotient;
15
+ }
16
+
17
+ /**
18
+ * `value - floorDiv(value, divisor) * divisor`, in [0, `divisor`), computed
19
+ * without that product so it stays exact across the whole safe range. Same
20
+ * argument contract as {@link floorDiv}.
21
+ */
22
+ export function floorMod(value: number, divisor: number): number {
23
+ const remainder = value % divisor;
24
+ if (remainder < 0) return remainder + divisor;
25
+ // `%` returns -0 for negative multiples of `divisor`; normalise it to 0.
26
+ return remainder === 0 ? 0 : remainder;
27
+ }
@@ -0,0 +1,4 @@
1
+ export * as Cellular2 from './2d.js';
2
+ export * as Cellular3 from './3d.js';
3
+
4
+ export type * from './_types/index.js';
@@ -0,0 +1,20 @@
1
+ import type { Cellular2dOptions, Cellular3dOptions } from './_types/index.js';
2
+
3
+ /** Throws if `options` breaks a contract in {@link Cellular2dOptions}. */
4
+ export function validateOptions2(options: Cellular2dOptions): void {
5
+ if (!Number.isInteger(options.seed)) throw new RangeError('Invalid seed');
6
+ if (!(Number.isSafeInteger(options.cellWidth) && options.cellWidth > 0))
7
+ throw new RangeError('Invalid cellWidth');
8
+ if (!(Number.isSafeInteger(options.cellHeight) && options.cellHeight > 0))
9
+ throw new RangeError('Invalid cellHeight');
10
+ if (!(Number.isSafeInteger(options.offsetX) && Number.isSafeInteger(options.offsetY)))
11
+ throw new RangeError('Invalid offsetX or offsetY');
12
+ }
13
+
14
+ /** Throws if `options` breaks a contract in {@link Cellular3dOptions}. */
15
+ export function validateOptions3(options: Cellular3dOptions): void {
16
+ validateOptions2(options);
17
+ if (!Number.isSafeInteger(options.offsetZ)) throw new RangeError('Invalid offsetZ');
18
+ if (!(Number.isSafeInteger(options.cellDepth) && options.cellDepth > 0))
19
+ throw new RangeError('Invalid cellDepth');
20
+ }