@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.
Files changed (264) hide show
  1. package/CHANGELOG.md +29 -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 +78 -4
  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,4 @@
1
+ export * from './cellular/index.js';
2
+ export * from './perlin/index.js';
3
+ export * from './simplex/index.js';
4
+ export * from './white/index.js';
@@ -0,0 +1,86 @@
1
+ import { getOctaveConfig } from '../../fractal/index.js';
2
+ import { Noise2D } from '../../noiseMap/index.js';
3
+ import { DEFAULT_PERLIN_2D_OPTIONS } from './defaultOptions.js';
4
+ import { validateOptions2 } from './options.js';
5
+ import { sample2 } from './sample2.js';
6
+
7
+ import type { Perlin2dOptions } from './_types/options.js';
8
+
9
+ /**
10
+ * Summed octaves at an integer world cell, in ±`maxPossibleHeight`. Assumes
11
+ * `config` is validated: `octaveOffsets` then holds exactly `config.octaves`
12
+ * pairs, so the indexed reads below are always in bounds.
13
+ */
14
+ function samplePoint(
15
+ worldX: number,
16
+ worldY: number,
17
+ config: Perlin2dOptions,
18
+ octaveOffsets: Float32Array,
19
+ invScale: number
20
+ ): number {
21
+ let amplitude = 1;
22
+ let frequency = 1;
23
+ let noiseHeight = 0;
24
+
25
+ for (let i = 0; i < config.octaves; i++) {
26
+ const index = i * 2;
27
+ const scaledFrequency = invScale * frequency;
28
+ const sampleX = (worldX + (octaveOffsets[index] as number)) * scaledFrequency;
29
+ const sampleY = (worldY + (octaveOffsets[index + 1] as number)) * scaledFrequency;
30
+
31
+ noiseHeight += sample2(sampleX, sampleY, config.seed) * amplitude;
32
+
33
+ amplitude *= config.persistence;
34
+ frequency *= config.lacunarity;
35
+ }
36
+
37
+ return noiseHeight;
38
+ }
39
+
40
+ /**
41
+ * Creates a `width` × `height` map of fractal (multi-octave) Perlin noise in
42
+ * [0, 1]. The caller owns the returned map.
43
+ *
44
+ * Each value depends only on its world cell (`x + offsetX`, `y + offsetY`) and
45
+ * the other options, never on the map's size. Chunks of any size, created with
46
+ * the same options and their world origin as the offset, agree bit for bit
47
+ * wherever they overlap.
48
+ *
49
+ * The mapping to [0, 1] is fixed by the options, not stretched to each map's own
50
+ * minimum and maximum; that is what keeps chunks consistent. The cost is
51
+ * contrast: the fixed bound is the sum of octave amplitudes, which summed noise
52
+ * rarely approaches. With the defaults (5 octaves, persistence 0.5) a 256 × 256
53
+ * map spans roughly 0.2–0.8, while a single octave uses nearly all of [0, 1].
54
+ * Stretch the result yourself if you need contrast more than tiling.
55
+ *
56
+ * Throws if the size is not a positive integer or an option breaks its contract
57
+ * in {@link Perlin2dOptions}.
58
+ */
59
+ export function create(
60
+ width: number,
61
+ height: number,
62
+ options: Partial<Perlin2dOptions> = {}
63
+ ): Noise2D {
64
+ const noise = Noise2D.create(width, height);
65
+
66
+ const config = { ...DEFAULT_PERLIN_2D_OPTIONS, ...options };
67
+ validateOptions2(config);
68
+
69
+ const values = Noise2D.getValues(noise);
70
+ const invScale = 1 / config.scale;
71
+
72
+ const [octaveOffsets, maxPossibleHeight] = getOctaveConfig(config, 2);
73
+ const invRange = 0.5 / maxPossibleHeight;
74
+
75
+ let index = 0;
76
+ for (let y = 0; y < height; y++) {
77
+ const worldY = y + config.offsetY;
78
+ for (let x = 0; x < width; x++) {
79
+ const worldX = x + config.offsetX;
80
+ const value = samplePoint(worldX, worldY, config, octaveOffsets, invScale);
81
+ values[index++] = value * invRange + 0.5;
82
+ }
83
+ }
84
+
85
+ return noise;
86
+ }
@@ -0,0 +1,93 @@
1
+ import { getOctaveConfig } from '../../fractal/index.js';
2
+ import { Noise3D } from '../../noiseMap/index.js';
3
+ import { DEFAULT_PERLIN_3D_OPTIONS } from './defaultOptions.js';
4
+ import { validateOptions3 } from './options.js';
5
+ import { sample3 } from './sample3.js';
6
+
7
+ import type { Perlin3dOptions } from './_types/options.js';
8
+
9
+ /**
10
+ * Summed octaves at an integer world cell, in ±`maxPossibleHeight`. Assumes
11
+ * `config` is validated: `octaveOffsets` then holds exactly `config.octaves`
12
+ * triples, so the indexed reads below are always in bounds.
13
+ */
14
+ function samplePoint(
15
+ worldX: number,
16
+ worldY: number,
17
+ worldZ: number,
18
+ config: Perlin3dOptions,
19
+ octaveOffsets: Float32Array,
20
+ invScale: number
21
+ ): number {
22
+ let amplitude = 1;
23
+ let frequency = 1;
24
+ let noiseHeight = 0;
25
+
26
+ for (let i = 0; i < config.octaves; i++) {
27
+ const index = i * 3;
28
+ const scaledFrequency = invScale * frequency;
29
+ const sampleX = (worldX + (octaveOffsets[index] as number)) * scaledFrequency;
30
+ const sampleY = (worldY + (octaveOffsets[index + 1] as number)) * scaledFrequency;
31
+ const sampleZ = (worldZ + (octaveOffsets[index + 2] as number)) * scaledFrequency;
32
+
33
+ noiseHeight += sample3(sampleX, sampleY, sampleZ, config.seed) * amplitude;
34
+
35
+ amplitude *= config.persistence;
36
+ frequency *= config.lacunarity;
37
+ }
38
+
39
+ return noiseHeight;
40
+ }
41
+
42
+ /**
43
+ * Creates a `width` × `height` × `depth` map of fractal (multi-octave) Perlin
44
+ * noise in [0, 1]. The caller owns the returned map.
45
+ *
46
+ * Each value depends only on its world cell (`x + offsetX`, `y + offsetY`,
47
+ * `z + offsetZ`) and the other options, never on the map's size. Chunks of any
48
+ * size, created with the same options and their world origin as the offset,
49
+ * agree bit for bit wherever they overlap.
50
+ *
51
+ * As in 2D, the mapping to [0, 1] is fixed by the options rather than stretched
52
+ * per map, which keeps chunks consistent at the cost of contrast. 3D noise
53
+ * spreads less than 2D: with the defaults a 48³ map spans roughly 0.23–0.78, and
54
+ * a single octave roughly 0.15–0.88.
55
+ *
56
+ * Each cell costs `octaves` calls to `sample3`, which blends eight corners, so a
57
+ * cell costs about twice a 2D one and the total grows with the volume.
58
+ *
59
+ * Throws if the size is not a positive integer or an option breaks its contract
60
+ * in {@link Perlin3dOptions}.
61
+ */
62
+ export function create(
63
+ width: number,
64
+ height: number,
65
+ depth: number,
66
+ options: Partial<Perlin3dOptions> = {}
67
+ ): Noise3D {
68
+ const noise = Noise3D.create(width, height, depth);
69
+
70
+ const config = { ...DEFAULT_PERLIN_3D_OPTIONS, ...options };
71
+ validateOptions3(config);
72
+
73
+ const values = Noise3D.getValues(noise);
74
+ const invScale = 1 / config.scale;
75
+
76
+ const [octaveOffsets, maxPossibleHeight] = getOctaveConfig(config, 3);
77
+ const invRange = 0.5 / maxPossibleHeight;
78
+
79
+ let index = 0;
80
+ for (let z = 0; z < depth; z++) {
81
+ const worldZ = z + config.offsetZ;
82
+ for (let y = 0; y < height; y++) {
83
+ const worldY = y + config.offsetY;
84
+ for (let x = 0; x < width; x++) {
85
+ const worldX = x + config.offsetX;
86
+ const value = samplePoint(worldX, worldY, worldZ, config, octaveOffsets, invScale);
87
+ values[index++] = value * invRange + 0.5;
88
+ }
89
+ }
90
+ }
91
+
92
+ return noise;
93
+ }
@@ -0,0 +1 @@
1
+ export type { Perlin2dOptions, Perlin3dOptions } from './options.js';
@@ -0,0 +1,23 @@
1
+ import type { FractalOptions } from '../../../fractal/index.js';
2
+
3
+ /**
4
+ * Options for 2D Perlin noise: the shared {@link FractalOptions} plus the map's
5
+ * world offset. A map is a pure function of its size and these options, so the
6
+ * same options reproduce it exactly on any JS engine.
7
+ */
8
+ export interface Perlin2dOptions extends FractalOptions {
9
+ /**
10
+ * World x of the map's first column, in map cells. A safe integer (|v| < 2^53),
11
+ * so `x + offsetX` is exact and a chunk at offset 64 reproduces column 64 of a
12
+ * chunk at offset 0 bit for bit. Larger integers would round when added to.
13
+ */
14
+ offsetX: number;
15
+ /** World y of the map's first row. Same contract as {@link offsetX}. */
16
+ offsetY: number;
17
+ }
18
+
19
+ /** Options for 3D Perlin noise: the 2D options plus a z offset. */
20
+ export interface Perlin3dOptions extends Perlin2dOptions {
21
+ /** World z of the map's first layer. Same contract as {@link Perlin2dOptions.offsetX}. */
22
+ offsetZ: number;
23
+ }
@@ -0,0 +1,16 @@
1
+ import type { Perlin2dOptions, Perlin3dOptions } from './_types/options.js';
2
+
3
+ export const DEFAULT_PERLIN_2D_OPTIONS: Perlin2dOptions = {
4
+ seed: 0,
5
+ offsetX: 0,
6
+ offsetY: 0,
7
+ scale: 20,
8
+ lacunarity: 2,
9
+ persistence: 0.5,
10
+ octaves: 5,
11
+ };
12
+
13
+ export const DEFAULT_PERLIN_3D_OPTIONS: Perlin3dOptions = {
14
+ ...DEFAULT_PERLIN_2D_OPTIONS,
15
+ offsetZ: 0,
16
+ };
@@ -0,0 +1,4 @@
1
+ export * as Perlin2 from './2d.js';
2
+ export * as Perlin3 from './3d.js';
3
+
4
+ export type * from './_types/index.js';
@@ -0,0 +1,19 @@
1
+ import { validateFractalOptions } from '../../fractal/index.js';
2
+
3
+ import type { Perlin2dOptions, Perlin3dOptions } from './_types/options.js';
4
+
5
+ /**
6
+ * Throws if `options` breaks a contract in {@link Perlin2dOptions}. The fractal
7
+ * options are checked first, by the shared fractal validation.
8
+ */
9
+ export function validateOptions2(options: Perlin2dOptions): void {
10
+ validateFractalOptions(options);
11
+ if (!(Number.isSafeInteger(options.offsetX) && Number.isSafeInteger(options.offsetY)))
12
+ throw new RangeError('Invalid offsetX or offsetY');
13
+ }
14
+
15
+ /** Throws if `options` breaks a contract in {@link Perlin3dOptions}. */
16
+ export function validateOptions3(options: Perlin3dOptions): void {
17
+ validateOptions2(options);
18
+ if (!Number.isSafeInteger(options.offsetZ)) throw new RangeError('Invalid offsetZ');
19
+ }
@@ -0,0 +1,62 @@
1
+ import { Easing, lerp } from '@cult-frog/math';
2
+
3
+ import { Hashes } from '../../hashes/index.js';
4
+
5
+ /**
6
+ * Dot product of the gradient hashed for lattice corner (`cx`, `cy`) with the
7
+ * offset (`dx`, `dy`) from that corner to the sample point.
8
+ *
9
+ * The eight gradients are (±1, 0), (0, ±1) and (±1, ±1), picked by integer hash
10
+ * and a switch instead of an angle. ECMAScript lets engines approximate trig
11
+ * functions differently, so angle-based gradients would make the same seed give
12
+ * different noise in different engines; this uses only exact operations.
13
+ */
14
+ export function corner2(cx: number, cy: number, dx: number, dy: number, seed: number) {
15
+ switch (Hashes.hash2(cx, cy, seed) & 7) {
16
+ case 0:
17
+ return dx;
18
+ case 1:
19
+ return -dx;
20
+ case 2:
21
+ return dy;
22
+ case 3:
23
+ return -dy;
24
+ case 4:
25
+ return dx + dy;
26
+ case 5:
27
+ return dx - dy;
28
+ case 6:
29
+ return -dx + dy;
30
+ default:
31
+ return -dx - dy;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * One octave of 2D Perlin noise at (`x`, `y`) in lattice units, in [-1, 1].
37
+ *
38
+ * Exactly 0 at every integer point. The ±1 bound is only reached near cell
39
+ * centres where diagonal gradients line up, so most values sit well inside it.
40
+ * The smootherstep fade keeps the first and second derivatives continuous across
41
+ * cell borders. Corner coordinates are truncated to int32 by the hash, so the
42
+ * pattern repeats beyond ±2^31.
43
+ *
44
+ * Seeded output must match across engines, so this may only use exact
45
+ * operations: + − × ÷, floor, sqrt, integer and bitwise ops. Engine-approximated
46
+ * functions such as sin, cos, exp and pow break that.
47
+ */
48
+ export function sample2(x: number, y: number, seed: number) {
49
+ const x0 = Math.floor(x);
50
+ const y0 = Math.floor(y);
51
+ const dx = x - x0;
52
+ const dy = y - y0;
53
+
54
+ const n00 = corner2(x0, y0, dx, dy, seed);
55
+ const n10 = corner2(x0 + 1, y0, dx - 1, dy, seed);
56
+ const n01 = corner2(x0, y0 + 1, dx, dy - 1, seed);
57
+ const n11 = corner2(x0 + 1, y0 + 1, dx - 1, dy - 1, seed);
58
+
59
+ const u = Easing.Smooth.smootherstep(dx);
60
+ const v = Easing.Smooth.smootherstep(dy);
61
+ return lerp(lerp(n00, n10, u), lerp(n01, n11, u), v);
62
+ }
@@ -0,0 +1,43 @@
1
+ import { Easing, lerp } from '@cult-frog/math';
2
+
3
+ import { cubeEdge3 } from '../../gradient/index.js';
4
+
5
+ /**
6
+ * Brings the 3D sum inside [-1, 1]. With these gradients, eight corners can
7
+ * reach about 1.0364 off-centre (near (0.48, 0.36, 0.5) in a cell), unlike 2D,
8
+ * whose bound is exactly 1 at the centre. 1 / 1.03635 ≈ 0.96492, rounded down.
9
+ */
10
+ const SAMPLE3_SCALE = 0.9649;
11
+
12
+ /**
13
+ * One octave of 3D Perlin noise at (`x`, `y`, `z`) in lattice units, in [-1, 1].
14
+ *
15
+ * Exactly 0 at every integer point. Corner coordinates are truncated to int32 by
16
+ * the hash, so the pattern repeats beyond ±2^31. Like `sample2`, this may only use
17
+ * exact operations, so seeded output matches across engines.
18
+ */
19
+ export function sample3(x: number, y: number, z: number, seed: number) {
20
+ const x0 = Math.floor(x);
21
+ const y0 = Math.floor(y);
22
+ const z0 = Math.floor(z);
23
+ const dx = x - x0;
24
+ const dy = y - y0;
25
+ const dz = z - z0;
26
+
27
+ const n000 = cubeEdge3(x0, y0, z0, dx, dy, dz, seed);
28
+ const n100 = cubeEdge3(x0 + 1, y0, z0, dx - 1, dy, dz, seed);
29
+ const n010 = cubeEdge3(x0, y0 + 1, z0, dx, dy - 1, dz, seed);
30
+ const n110 = cubeEdge3(x0 + 1, y0 + 1, z0, dx - 1, dy - 1, dz, seed);
31
+ const n001 = cubeEdge3(x0, y0, z0 + 1, dx, dy, dz - 1, seed);
32
+ const n101 = cubeEdge3(x0 + 1, y0, z0 + 1, dx - 1, dy, dz - 1, seed);
33
+ const n011 = cubeEdge3(x0, y0 + 1, z0 + 1, dx, dy - 1, dz - 1, seed);
34
+ const n111 = cubeEdge3(x0 + 1, y0 + 1, z0 + 1, dx - 1, dy - 1, dz - 1, seed);
35
+
36
+ const u = Easing.Smooth.smootherstep(dx);
37
+ const v = Easing.Smooth.smootherstep(dy);
38
+ const w = Easing.Smooth.smootherstep(dz);
39
+
40
+ const near = lerp(lerp(n000, n100, u), lerp(n010, n110, u), v);
41
+ const far = lerp(lerp(n001, n101, u), lerp(n011, n111, u), v);
42
+ return lerp(near, far, w) * SAMPLE3_SCALE;
43
+ }
@@ -0,0 +1,86 @@
1
+ import { getOctaveConfig } from '../../fractal/index.js';
2
+ import { Noise2D } from '../../noiseMap/index.js';
3
+ import { DEFAULT_SIMPLEX_2D_OPTIONS } from './defaultOptions.js';
4
+ import { validateOptions2 } from './options.js';
5
+ import { sample2 } from './sample2.js';
6
+
7
+ import type { Simplex2dOptions } from './_types/index.js';
8
+
9
+ /**
10
+ * Creates a `width` × `height` map of fractal (multi-octave) simplex noise in
11
+ * [0, 1]. The caller owns the returned map.
12
+ *
13
+ * Each value depends only on its world cell (`x + offsetX`, `y + offsetY`) and
14
+ * the other options, never on the map's size. Chunks of any size, created with
15
+ * the same options and their world origin as the offset, agree bit for bit
16
+ * wherever they overlap.
17
+ *
18
+ * The mapping to [0, 1] is fixed by the options, not stretched to each map's own
19
+ * minimum and maximum, which keeps chunks consistent. Simplex fills that range
20
+ * far more than Perlin does: with the defaults a 256 × 256 map spans roughly
21
+ * 0.05–0.96 (Perlin roughly 0.2–0.8), and a single octave reaches almost exactly
22
+ * 0 and 1. A threshold tuned for one, such as a sea level, covers a different
23
+ * share of the map with the other at the same options.
24
+ *
25
+ * Throws if the size is not a positive integer or an option breaks its contract
26
+ * in {@link Simplex2dOptions}.
27
+ */
28
+ export function create(
29
+ width: number,
30
+ height: number,
31
+ options: Partial<Simplex2dOptions> = {}
32
+ ): Noise2D {
33
+ const noise = Noise2D.create(width, height);
34
+
35
+ const config = { ...DEFAULT_SIMPLEX_2D_OPTIONS, ...options };
36
+ validateOptions2(config);
37
+
38
+ const values = Noise2D.getValues(noise);
39
+ const invScale = 1 / config.scale;
40
+
41
+ const [octaveOffsets, maxPossibleHeight] = getOctaveConfig(config, 2);
42
+ const invRange = 0.5 / maxPossibleHeight;
43
+
44
+ let index = 0;
45
+ for (let y = 0; y < height; y++) {
46
+ const worldY = y + config.offsetY;
47
+ for (let x = 0; x < width; x++) {
48
+ const worldX = x + config.offsetX;
49
+ const value = samplePoint(worldX, worldY, config, octaveOffsets, invScale);
50
+ values[index++] = value * invRange + 0.5;
51
+ }
52
+ }
53
+
54
+ return noise;
55
+ }
56
+
57
+ /**
58
+ * Summed octaves at an integer world cell, in ±`maxPossibleHeight`. Assumes
59
+ * `config` is validated: `octaveOffsets` then holds exactly `config.octaves`
60
+ * pairs, so the indexed reads below are always in bounds.
61
+ */
62
+ function samplePoint(
63
+ worldX: number,
64
+ worldY: number,
65
+ config: Simplex2dOptions,
66
+ octaveOffsets: Float32Array,
67
+ invScale: number
68
+ ): number {
69
+ let amplitude = 1;
70
+ let frequency = 1;
71
+ let noiseHeight = 0;
72
+
73
+ for (let i = 0; i < config.octaves; i++) {
74
+ const index = i * 2;
75
+ const scaledFrequency = invScale * frequency;
76
+ const sampleX = (worldX + (octaveOffsets[index] as number)) * scaledFrequency;
77
+ const sampleY = (worldY + (octaveOffsets[index + 1] as number)) * scaledFrequency;
78
+
79
+ noiseHeight += sample2(sampleX, sampleY, config.seed) * amplitude;
80
+
81
+ amplitude *= config.persistence;
82
+ frequency *= config.lacunarity;
83
+ }
84
+
85
+ return noiseHeight;
86
+ }
@@ -0,0 +1,94 @@
1
+ import { getOctaveConfig } from '../../fractal/index.js';
2
+ import { Noise3D } from '../../noiseMap/index.js';
3
+ import { DEFAULT_SIMPLEX_3D_OPTIONS } from './defaultOptions.js';
4
+ import { validateOptions3 } from './options.js';
5
+ import { sample3 } from './sample3.js';
6
+
7
+ import type { Simplex3dOptions } from './_types/index.js';
8
+
9
+ /**
10
+ * Creates a `width` × `height` × `depth` map of fractal (multi-octave) simplex
11
+ * noise in [0, 1]. The caller owns the returned map.
12
+ *
13
+ * Each value depends only on its world cell (`x + offsetX`, `y + offsetY`,
14
+ * `z + offsetZ`) and the other options, never on the map's size. Chunks of any
15
+ * size, created with the same options and their world origin as the offset,
16
+ * agree bit for bit wherever they overlap.
17
+ *
18
+ * As in 2D, the mapping to [0, 1] is fixed by the options rather than stretched
19
+ * per map, and simplex fills more of it than Perlin: with the defaults a 48³ map
20
+ * spans roughly 0.07–0.93 (Perlin roughly 0.25–0.75), and a single octave
21
+ * reaches almost exactly 0 and 1.
22
+ *
23
+ * Each cell costs `octaves` calls to `sample3`, which sums four corners against
24
+ * eight for 3D Perlin, so a map costs about half as much as the same 3D Perlin
25
+ * map. The total still grows with the volume.
26
+ *
27
+ * Throws if the size is not a positive integer or an option breaks its contract
28
+ * in {@link Simplex3dOptions}.
29
+ */
30
+ export function create(
31
+ width: number,
32
+ height: number,
33
+ depth: number,
34
+ options: Partial<Simplex3dOptions> = {}
35
+ ): Noise3D {
36
+ const noise = Noise3D.create(width, height, depth);
37
+
38
+ const config = { ...DEFAULT_SIMPLEX_3D_OPTIONS, ...options };
39
+ validateOptions3(config);
40
+
41
+ const values = Noise3D.getValues(noise);
42
+ const invScale = 1 / config.scale;
43
+
44
+ const [octaveOffsets, maxPossibleHeight] = getOctaveConfig(config, 3);
45
+ const invRange = 0.5 / maxPossibleHeight;
46
+
47
+ let index = 0;
48
+ for (let z = 0; z < depth; z++) {
49
+ const worldZ = z + config.offsetZ;
50
+ for (let y = 0; y < height; y++) {
51
+ const worldY = y + config.offsetY;
52
+ for (let x = 0; x < width; x++) {
53
+ const worldX = x + config.offsetX;
54
+ const value = samplePoint(worldX, worldY, worldZ, config, octaveOffsets, invScale);
55
+ values[index++] = value * invRange + 0.5;
56
+ }
57
+ }
58
+ }
59
+
60
+ return noise;
61
+ }
62
+
63
+ /**
64
+ * Summed octaves at an integer world cell, in ±`maxPossibleHeight`. Assumes
65
+ * `config` is validated: `octaveOffsets` then holds exactly `config.octaves`
66
+ * triples, so the indexed reads below are always in bounds.
67
+ */
68
+ function samplePoint(
69
+ worldX: number,
70
+ worldY: number,
71
+ worldZ: number,
72
+ config: Simplex3dOptions,
73
+ octaveOffsets: Float32Array,
74
+ invScale: number
75
+ ): number {
76
+ let amplitude = 1;
77
+ let frequency = 1;
78
+ let noiseHeight = 0;
79
+
80
+ for (let i = 0; i < config.octaves; i++) {
81
+ const index = i * 3;
82
+ const scaledFrequency = invScale * frequency;
83
+ const sampleX = (worldX + (octaveOffsets[index] as number)) * scaledFrequency;
84
+ const sampleY = (worldY + (octaveOffsets[index + 1] as number)) * scaledFrequency;
85
+ const sampleZ = (worldZ + (octaveOffsets[index + 2] as number)) * scaledFrequency;
86
+
87
+ noiseHeight += sample3(sampleX, sampleY, sampleZ, config.seed) * amplitude;
88
+
89
+ amplitude *= config.persistence;
90
+ frequency *= config.lacunarity;
91
+ }
92
+
93
+ return noiseHeight;
94
+ }
@@ -0,0 +1 @@
1
+ export type { Simplex2dOptions, Simplex3dOptions } from './options.js';
@@ -0,0 +1,34 @@
1
+ import type { FractalOptions } from '../../../fractal/index.js';
2
+
3
+ /**
4
+ * Options for 2D simplex noise: the shared {@link FractalOptions} plus the map's
5
+ * world offset. A map is a pure function of its size and these options, so the
6
+ * same options reproduce it exactly on any JS engine.
7
+ */
8
+ export interface Simplex2dOptions extends FractalOptions {
9
+ /**
10
+ * Size of one lattice cell of the first octave, in map cells. Larger values
11
+ * zoom in. Finite and greater than 0.
12
+ *
13
+ * The lattice is skewed into triangles whose edges are about 0.82 × `scale`
14
+ * long, so features come out slightly smaller than Perlin's at the same scale.
15
+ */
16
+ scale: number;
17
+ /**
18
+ * World x of the map's first column, in map cells. A safe integer (|v| < 2^53),
19
+ * so `x + offsetX` is exact and a chunk at offset 64 reproduces column 64 of a
20
+ * chunk at offset 0 bit for bit. Larger integers would round when added to.
21
+ */
22
+ offsetX: number;
23
+ /** World y of the map's first row. Same contract as {@link offsetX}. */
24
+ offsetY: number;
25
+ }
26
+
27
+ /**
28
+ * Options for 3D simplex noise: the 2D options plus a z offset. In 3D the
29
+ * tetrahedra's edges are 0.87–1 × `scale` long.
30
+ */
31
+ export interface Simplex3dOptions extends Simplex2dOptions {
32
+ /** World z of the map's first layer. Same contract as {@link Simplex2dOptions.offsetX}. */
33
+ offsetZ: number;
34
+ }
@@ -0,0 +1,16 @@
1
+ import type { Simplex2dOptions, Simplex3dOptions } from './_types/options.js';
2
+
3
+ export const DEFAULT_SIMPLEX_2D_OPTIONS: Simplex2dOptions = {
4
+ seed: 0,
5
+ offsetX: 0,
6
+ offsetY: 0,
7
+ scale: 20,
8
+ lacunarity: 2,
9
+ persistence: 0.5,
10
+ octaves: 5,
11
+ };
12
+
13
+ export const DEFAULT_SIMPLEX_3D_OPTIONS: Simplex3dOptions = {
14
+ ...DEFAULT_SIMPLEX_2D_OPTIONS,
15
+ offsetZ: 0,
16
+ };
@@ -0,0 +1,4 @@
1
+ export * as Simplex2 from './2d.js';
2
+ export * as Simplex3 from './3d.js';
3
+
4
+ export type * from './_types/index.js';
@@ -0,0 +1,19 @@
1
+ import { validateFractalOptions } from '../../fractal/index.js';
2
+
3
+ import type { Simplex2dOptions, Simplex3dOptions } from './_types/options.js';
4
+
5
+ /**
6
+ * Throws if `options` breaks a contract in {@link Simplex2dOptions}. The fractal
7
+ * options are checked first, by the shared fractal validation.
8
+ */
9
+ export function validateOptions2(options: Simplex2dOptions): void {
10
+ validateFractalOptions(options);
11
+ if (!(Number.isSafeInteger(options.offsetX) && Number.isSafeInteger(options.offsetY)))
12
+ throw new RangeError('Invalid offsetX or offsetY');
13
+ }
14
+
15
+ /** Throws if `options` breaks a contract in {@link Simplex3dOptions}. */
16
+ export function validateOptions3(options: Simplex3dOptions): void {
17
+ validateOptions2(options);
18
+ if (!Number.isSafeInteger(options.offsetZ)) throw new RangeError('Invalid offsetZ');
19
+ }