math 0.0.0 → 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 (201) hide show
  1. package/API.md +1605 -0
  2. package/LICENSE +21 -0
  3. package/README.md +197 -0
  4. package/dist/color/color.js +136 -0
  5. package/dist/color/color.js.map +1 -0
  6. package/dist/color/colorspace.js +43 -0
  7. package/dist/color/colorspace.js.map +1 -0
  8. package/dist/color/hsl.js +123 -0
  9. package/dist/color/hsl.js.map +1 -0
  10. package/dist/color/index.js +7 -0
  11. package/dist/color/index.js.map +1 -0
  12. package/dist/color/parse.js +276 -0
  13. package/dist/color/parse.js.map +1 -0
  14. package/dist/core/angle.js +43 -0
  15. package/dist/core/angle.js.map +1 -0
  16. package/dist/core/euler.js +212 -0
  17. package/dist/core/euler.js.map +1 -0
  18. package/dist/core/mat2.js +385 -0
  19. package/dist/core/mat2.js.map +1 -0
  20. package/dist/core/mat2d.js +422 -0
  21. package/dist/core/mat2d.js.map +1 -0
  22. package/dist/core/mat3.js +749 -0
  23. package/dist/core/mat3.js.map +1 -0
  24. package/dist/core/mat4.js +2197 -0
  25. package/dist/core/mat4.js.map +1 -0
  26. package/dist/core/polar.js +200 -0
  27. package/dist/core/polar.js.map +1 -0
  28. package/dist/core/quat.js +730 -0
  29. package/dist/core/quat.js.map +1 -0
  30. package/dist/core/quat2.js +883 -0
  31. package/dist/core/quat2.js.map +1 -0
  32. package/dist/core/scalar.js +112 -0
  33. package/dist/core/scalar.js.map +1 -0
  34. package/dist/core/spherical.js +248 -0
  35. package/dist/core/spherical.js.map +1 -0
  36. package/dist/core/vec2.js +597 -0
  37. package/dist/core/vec2.js.map +1 -0
  38. package/dist/core/vec3.js +916 -0
  39. package/dist/core/vec3.js.map +1 -0
  40. package/dist/core/vec4.js +573 -0
  41. package/dist/core/vec4.js.map +1 -0
  42. package/dist/geometry/circumcircle.js +39 -0
  43. package/dist/geometry/circumcircle.js.map +1 -0
  44. package/dist/geometry/index.js +6 -0
  45. package/dist/geometry/index.js.map +1 -0
  46. package/dist/geometry/polygon2-decompose.js +411 -0
  47. package/dist/geometry/polygon2-decompose.js.map +1 -0
  48. package/dist/geometry/polygon2-triangulate.js +222 -0
  49. package/dist/geometry/polygon2-triangulate.js.map +1 -0
  50. package/dist/geometry/quickhull2.js +123 -0
  51. package/dist/geometry/quickhull2.js.map +1 -0
  52. package/dist/geometry/quickhull3.js +605 -0
  53. package/dist/geometry/quickhull3.js.map +1 -0
  54. package/dist/ik/fabrik2.js +675 -0
  55. package/dist/ik/fabrik2.js.map +1 -0
  56. package/dist/ik/fabrik3.js +912 -0
  57. package/dist/ik/fabrik3.js.map +1 -0
  58. package/dist/ik/index.js +5 -0
  59. package/dist/ik/index.js.map +1 -0
  60. package/dist/index.js +27 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/noise/fractal.js +185 -0
  63. package/dist/noise/fractal.js.map +1 -0
  64. package/dist/noise/index.js +16 -0
  65. package/dist/noise/index.js.map +1 -0
  66. package/dist/noise/perlin2d.js +50 -0
  67. package/dist/noise/perlin2d.js.map +1 -0
  68. package/dist/noise/perlin3d.js +72 -0
  69. package/dist/noise/perlin3d.js.map +1 -0
  70. package/dist/noise/permutation.js +120 -0
  71. package/dist/noise/permutation.js.map +1 -0
  72. package/dist/noise/simplex2d.js +84 -0
  73. package/dist/noise/simplex2d.js.map +1 -0
  74. package/dist/noise/simplex3d.js +147 -0
  75. package/dist/noise/simplex3d.js.map +1 -0
  76. package/dist/noise/simplex4d.js +149 -0
  77. package/dist/noise/simplex4d.js.map +1 -0
  78. package/dist/noise/worley2d.js +50 -0
  79. package/dist/noise/worley2d.js.map +1 -0
  80. package/dist/noise/worley3d.js +61 -0
  81. package/dist/noise/worley3d.js.map +1 -0
  82. package/dist/random/index.js +9 -0
  83. package/dist/random/index.js.map +1 -0
  84. package/dist/random/isaac32.js +164 -0
  85. package/dist/random/isaac32.js.map +1 -0
  86. package/dist/random/isaac64.js +215 -0
  87. package/dist/random/isaac64.js.map +1 -0
  88. package/dist/random/mulberry32.js +48 -0
  89. package/dist/random/mulberry32.js.map +1 -0
  90. package/dist/random/random.js +120 -0
  91. package/dist/random/random.js.map +1 -0
  92. package/dist/shapes/box2.js +300 -0
  93. package/dist/shapes/box2.js.map +1 -0
  94. package/dist/shapes/box3.js +550 -0
  95. package/dist/shapes/box3.js.map +1 -0
  96. package/dist/shapes/circle.js +6 -0
  97. package/dist/shapes/circle.js.map +1 -0
  98. package/dist/shapes/frustum.js +662 -0
  99. package/dist/shapes/frustum.js.map +1 -0
  100. package/dist/shapes/index.js +25 -0
  101. package/dist/shapes/index.js.map +1 -0
  102. package/dist/shapes/obb3.js +557 -0
  103. package/dist/shapes/obb3.js.map +1 -0
  104. package/dist/shapes/plane3.js +267 -0
  105. package/dist/shapes/plane3.js.map +1 -0
  106. package/dist/shapes/polygon2.js +451 -0
  107. package/dist/shapes/polygon2.js.map +1 -0
  108. package/dist/shapes/raycast3.js +164 -0
  109. package/dist/shapes/raycast3.js.map +1 -0
  110. package/dist/shapes/segment2.js +82 -0
  111. package/dist/shapes/segment2.js.map +1 -0
  112. package/dist/shapes/sphere.js +23 -0
  113. package/dist/shapes/sphere.js.map +1 -0
  114. package/dist/shapes/triangle2.js +79 -0
  115. package/dist/shapes/triangle2.js.map +1 -0
  116. package/dist/shapes/triangle3.js +60 -0
  117. package/dist/shapes/triangle3.js.map +1 -0
  118. package/dist/src/color/color.d.ts +50 -0
  119. package/dist/src/color/colorspace.d.ts +15 -0
  120. package/dist/src/color/hsl.d.ts +28 -0
  121. package/dist/src/color/index.d.ts +5 -0
  122. package/dist/src/color/parse.d.ts +15 -0
  123. package/dist/src/core/angle.d.ts +29 -0
  124. package/dist/src/core/arrays.d.ts +4 -0
  125. package/dist/src/core/euler.d.ts +78 -0
  126. package/dist/src/core/index.d.ts +27 -0
  127. package/dist/src/core/mat2.d.ts +217 -0
  128. package/dist/src/core/mat2d.d.ts +218 -0
  129. package/dist/src/core/mat3.d.ts +295 -0
  130. package/dist/src/core/mat4.d.ts +667 -0
  131. package/dist/src/core/polar.d.ts +151 -0
  132. package/dist/src/core/quat.d.ts +370 -0
  133. package/dist/src/core/quat2.d.ts +344 -0
  134. package/dist/src/core/scalar.d.ts +75 -0
  135. package/dist/src/core/spherical.d.ts +165 -0
  136. package/dist/src/core/vec2.d.ts +403 -0
  137. package/dist/src/core/vec3.d.ts +493 -0
  138. package/dist/src/core/vec4.d.ts +336 -0
  139. package/dist/src/geometry/circumcircle.d.ts +9 -0
  140. package/dist/src/geometry/index.d.ts +5 -0
  141. package/dist/src/geometry/polygon2-decompose.d.ts +25 -0
  142. package/dist/src/geometry/polygon2-triangulate.d.ts +15 -0
  143. package/dist/src/geometry/quickhull2.d.ts +10 -0
  144. package/dist/src/geometry/quickhull3.d.ts +27 -0
  145. package/dist/src/ik/fabrik2.d.ts +314 -0
  146. package/dist/src/ik/fabrik3.d.ts +371 -0
  147. package/dist/src/ik/index.d.ts +2 -0
  148. package/dist/src/index.d.ts +1 -0
  149. package/dist/src/noise/fractal.d.ts +95 -0
  150. package/dist/src/noise/index.d.ts +9 -0
  151. package/dist/src/noise/perlin2d.d.ts +19 -0
  152. package/dist/src/noise/perlin3d.d.ts +20 -0
  153. package/dist/src/noise/permutation.d.ts +22 -0
  154. package/dist/src/noise/simplex2d.d.ts +19 -0
  155. package/dist/src/noise/simplex3d.d.ts +20 -0
  156. package/dist/src/noise/simplex4d.d.ts +24 -0
  157. package/dist/src/noise/worley2d.d.ts +22 -0
  158. package/dist/src/noise/worley3d.d.ts +23 -0
  159. package/dist/src/random/index.d.ts +8 -0
  160. package/dist/src/random/isaac32.d.ts +62 -0
  161. package/dist/src/random/isaac64.d.ts +75 -0
  162. package/dist/src/random/mulberry32.d.ts +43 -0
  163. package/dist/src/random/random.d.ts +67 -0
  164. package/dist/src/shapes/box2.d.ts +174 -0
  165. package/dist/src/shapes/box3.d.ts +213 -0
  166. package/dist/src/shapes/circle.d.ts +7 -0
  167. package/dist/src/shapes/frustum.d.ts +147 -0
  168. package/dist/src/shapes/index.d.ts +20 -0
  169. package/dist/src/shapes/obb3.d.ts +105 -0
  170. package/dist/src/shapes/plane3.d.ts +130 -0
  171. package/dist/src/shapes/polygon2.d.ts +155 -0
  172. package/dist/src/shapes/raycast3.d.ts +42 -0
  173. package/dist/src/shapes/segment2.d.ts +34 -0
  174. package/dist/src/shapes/sphere.d.ts +19 -0
  175. package/dist/src/shapes/triangle2.d.ts +53 -0
  176. package/dist/src/shapes/triangle3.d.ts +29 -0
  177. package/dist/src/time/easing.d.ts +21 -0
  178. package/dist/src/time/index.d.ts +6 -0
  179. package/dist/src/time/spring-core.d.ts +15 -0
  180. package/dist/src/time/spring.d.ts +36 -0
  181. package/dist/src/time/spring2.d.ts +11 -0
  182. package/dist/src/time/spring3.d.ts +11 -0
  183. package/dist/src/time/spring4.d.ts +11 -0
  184. package/dist/time/easing.js +24 -0
  185. package/dist/time/easing.js.map +1 -0
  186. package/dist/time/index.js +11 -0
  187. package/dist/time/index.js.map +1 -0
  188. package/dist/time/spring-core.js +48 -0
  189. package/dist/time/spring-core.js.map +1 -0
  190. package/dist/time/spring.js +62 -0
  191. package/dist/time/spring.js.map +1 -0
  192. package/dist/time/spring2.js +31 -0
  193. package/dist/time/spring2.js.map +1 -0
  194. package/dist/time/spring3.js +32 -0
  195. package/dist/time/spring3.js.map +1 -0
  196. package/dist/time/spring4.js +32 -0
  197. package/dist/time/spring4.js.map +1 -0
  198. package/package.json +90 -12
  199. package/skills/math/SKILL.md +140 -0
  200. package/math.coffee +0 -36
  201. package/math.js +0 -67
@@ -0,0 +1,2 @@
1
+ export * as fabrik2 from './fabrik2.js';
2
+ export * as fabrik3 from './fabrik3.js';
@@ -0,0 +1 @@
1
+ export * from './core/index.js';
@@ -0,0 +1,95 @@
1
+ import type { Vec2 } from '../core/vec2.js';
2
+ import type { Vec3 } from '../core/vec3.js';
3
+ /**
4
+ * Fractional Brownian motion: sums octaves of a noise source at increasing
5
+ * frequency and decreasing amplitude - the workhorse for natural-looking
6
+ * heightmaps and clouds. Given a source in [-1, 1], returns a value in [-1, 1].
7
+ *
8
+ * @param sample receives an octave frequency, returns the base noise scaled by it
9
+ * @param octaves number of octaves to sum
10
+ * @param lacunarity frequency multiplier between octaves
11
+ * @param gain amplitude multiplier between octaves
12
+ * @returns the summed, normalized noise value
13
+ */
14
+ export declare function fbm(sample: (frequency: number) => number, octaves: number, lacunarity: number, gain: number): number;
15
+ /**
16
+ * Ridged multifractal: like {@link fbm}, but each octave is folded to
17
+ * `1 - abs(noise)`, filling the field with sharp ridges - the classic mountain
18
+ * range / canyon look. Given a source in [-1, 1], returns a value in [0, 1].
19
+ *
20
+ * @param sample receives an octave frequency, returns the base noise scaled by it
21
+ * @param octaves number of octaves to sum
22
+ * @param lacunarity frequency multiplier between octaves
23
+ * @param gain amplitude multiplier between octaves
24
+ * @returns the summed, normalized ridged value
25
+ */
26
+ export declare function ridged(sample: (frequency: number) => number, octaves: number, lacunarity: number, gain: number): number;
27
+ /**
28
+ * Billow noise: like {@link fbm}, but each octave is folded to `2*abs(noise) - 1`,
29
+ * giving puffy, rounded, cloud-like lobes. Given a source in [-1, 1], returns a
30
+ * value in [-1, 1].
31
+ *
32
+ * @param sample receives an octave frequency, returns the base noise scaled by it
33
+ * @param octaves number of octaves to sum
34
+ * @param lacunarity frequency multiplier between octaves
35
+ * @param gain amplitude multiplier between octaves
36
+ * @returns the summed, normalized billow value
37
+ */
38
+ export declare function billow(sample: (frequency: number) => number, octaves: number, lacunarity: number, gain: number): number;
39
+ /**
40
+ * Domain warping (2D): offsets a point by a noise-derived vector, so feeding the
41
+ * result back into a noise source bends and swirls it - marble, meandering
42
+ * terrain, organic distortion. The two axes sample the warp noise at
43
+ * decorrelated offsets so they don't move in lockstep.
44
+ *
45
+ * @param out the receiving Vec2 (the warped point)
46
+ * @param sample the warp noise source, sampled at (x, y)
47
+ * @param x x coordinate
48
+ * @param y y coordinate
49
+ * @param amount displacement scale (default 1)
50
+ * @returns out
51
+ */
52
+ export declare function domainWarp2(out: Vec2, sample: (x: number, y: number) => number, x: number, y: number, amount?: number): Vec2;
53
+ /**
54
+ * Domain warping (3D): offsets a point by a noise-derived vector so a noise
55
+ * source sampled at the result is bent and swirled. See {@link domainWarp2}.
56
+ *
57
+ * @param out the receiving Vec3 (the warped point)
58
+ * @param sample the warp noise source, sampled at (x, y, z)
59
+ * @param x x coordinate
60
+ * @param y y coordinate
61
+ * @param z z coordinate
62
+ * @param amount displacement scale (default 1)
63
+ * @returns out
64
+ */
65
+ export declare function domainWarp3(out: Vec3, sample: (x: number, y: number, z: number) => number, x: number, y: number, z: number, amount?: number): Vec3;
66
+ /**
67
+ * Curl of a 2D scalar noise potential - a divergence-free (incompressible) flow
68
+ * field. Particles advected through it swirl like a fluid and never converge to
69
+ * sinks or fly apart, which is why it's the cheap go-to for procedural smoke /
70
+ * flow. Given a scalar potential psi(x, y), the flow is (d psi/dy, -d psi/dx),
71
+ * with the derivatives taken by central differences of step `eps`.
72
+ *
73
+ * @param out the receiving Vec2 (the flow velocity)
74
+ * @param sample the scalar potential noise, sampled at (x, y)
75
+ * @param x x coordinate
76
+ * @param y y coordinate
77
+ * @param eps finite-difference step (default 1e-4)
78
+ * @returns out
79
+ */
80
+ export declare function curl2(out: Vec2, sample: (x: number, y: number) => number, x: number, y: number, eps?: number): Vec2;
81
+ /**
82
+ * Curl of a 3D noise vector potential - a divergence-free 3D flow field for
83
+ * volumetric smoke / fluid motion. The three potential components reuse a single
84
+ * `sample` at large, decorrelating offsets, and the flow is their curl,
85
+ * `∇ × psi`, with derivatives taken by central differences of step `eps`.
86
+ *
87
+ * @param out the receiving Vec3 (the flow velocity)
88
+ * @param sample the scalar potential noise, sampled at (x, y, z)
89
+ * @param x x coordinate
90
+ * @param y y coordinate
91
+ * @param z z coordinate
92
+ * @param eps finite-difference step (default 1e-4)
93
+ * @returns out
94
+ */
95
+ export declare function curl3(out: Vec3, sample: (x: number, y: number, z: number) => number, x: number, y: number, z: number, eps?: number): Vec3;
@@ -0,0 +1,9 @@
1
+ export * from './fractal.js';
2
+ export * as perlin2d from './perlin2d.js';
3
+ export * as perlin3d from './perlin3d.js';
4
+ export type { Permutation } from './permutation.js';
5
+ export * as simplex2d from './simplex2d.js';
6
+ export * as simplex3d from './simplex3d.js';
7
+ export * as simplex4d from './simplex4d.js';
8
+ export * as worley2d from './worley2d.js';
9
+ export * as worley3d from './worley3d.js';
@@ -0,0 +1,19 @@
1
+ import { type Permutation } from './permutation.js';
2
+ /** A seeded 2D Perlin noise generator. Create one with {@link create}. */
3
+ export type Perlin2DGenerator = Permutation;
4
+ /**
5
+ * Creates a 2D Perlin noise generator with the given seed.
6
+ *
7
+ * @param seed The seed value for the noise generator
8
+ * @returns A generator to pass to {@link sample}
9
+ */
10
+ export declare function create(seed: number): Perlin2DGenerator;
11
+ /**
12
+ * Samples 2D Perlin noise.
13
+ *
14
+ * @param generator A generator created with {@link create}
15
+ * @param x X coordinate
16
+ * @param y Y coordinate
17
+ * @returns The noise value at (x, y)
18
+ */
19
+ export declare function sample({ perm, grad3 }: Perlin2DGenerator, x: number, y: number): number;
@@ -0,0 +1,20 @@
1
+ import { type Permutation } from './permutation.js';
2
+ /** A seeded 3D Perlin noise generator. Create one with {@link create}. */
3
+ export type Perlin3DGenerator = Permutation;
4
+ /**
5
+ * Creates a 3D Perlin noise generator with the given seed.
6
+ *
7
+ * @param seed The seed value for the noise generator
8
+ * @returns A generator to pass to {@link sample}
9
+ */
10
+ export declare function create(seed: number): Perlin3DGenerator;
11
+ /**
12
+ * Samples 3D Perlin noise.
13
+ *
14
+ * @param generator A generator created with {@link create}
15
+ * @param x X coordinate
16
+ * @param y Y coordinate
17
+ * @param z Z coordinate
18
+ * @returns The noise value at (x, y, z)
19
+ */
20
+ export declare function sample({ perm, grad3 }: Perlin3DGenerator, x: number, y: number, z: number): number;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Seeded permutation and gradient tables that back a noise generator.
3
+ *
4
+ * All noise variants (simplex/perlin, 2D/3D/4D) share this same table shape, so
5
+ * each generator type is a structural alias of this. `perm` is the doubled
6
+ * (512 entry) permutation table. `grad3` holds the 3D gradient for each
7
+ * permutation index as flat xyz triples (used by 2D and 3D noise), and `grad4`
8
+ * holds the 4D gradient for each index as flat xyzw quads. Flat typed arrays
9
+ * keep sampling free of nested array loads and allocation.
10
+ */
11
+ export type Permutation = {
12
+ perm: Uint8Array;
13
+ grad3: Float64Array;
14
+ grad4: Float64Array;
15
+ };
16
+ /**
17
+ * Creates the seeded permutation and gradient tables for a noise generator.
18
+ *
19
+ * @param seed The seed value for the noise generator
20
+ * @returns The permutation tables derived from the seed
21
+ */
22
+ export declare function createPermutation(seed: number): Permutation;
@@ -0,0 +1,19 @@
1
+ import { type Permutation } from './permutation.js';
2
+ /** A seeded 2D simplex noise generator. Create one with {@link create}. */
3
+ export type Simplex2DGenerator = Permutation;
4
+ /**
5
+ * Creates a 2D simplex noise generator with the given seed.
6
+ *
7
+ * @param seed The seed value for the noise generator
8
+ * @returns A generator to pass to {@link sample}
9
+ */
10
+ export declare function create(seed: number): Simplex2DGenerator;
11
+ /**
12
+ * Samples 2D simplex noise, returning a value in the interval [-1, 1].
13
+ *
14
+ * @param generator A generator created with {@link create}
15
+ * @param x X coordinate
16
+ * @param y Y coordinate
17
+ * @returns The noise value at (x, y)
18
+ */
19
+ export declare function sample({ perm, grad3 }: Simplex2DGenerator, x: number, y: number): number;
@@ -0,0 +1,20 @@
1
+ import { type Permutation } from './permutation.js';
2
+ /** A seeded 3D simplex noise generator. Create one with {@link create}. */
3
+ export type Simplex3DGenerator = Permutation;
4
+ /**
5
+ * Creates a 3D simplex noise generator with the given seed.
6
+ *
7
+ * @param seed The seed value for the noise generator
8
+ * @returns A generator to pass to {@link sample}
9
+ */
10
+ export declare function create(seed: number): Simplex3DGenerator;
11
+ /**
12
+ * Samples 3D simplex noise, returning a value in the interval [-1, 1].
13
+ *
14
+ * @param generator A generator created with {@link create}
15
+ * @param x X coordinate
16
+ * @param y Y coordinate
17
+ * @param z Z coordinate
18
+ * @returns The noise value at (x, y, z)
19
+ */
20
+ export declare function sample({ perm, grad3 }: Simplex3DGenerator, x: number, y: number, z: number): number;
@@ -0,0 +1,24 @@
1
+ import { type Permutation } from './permutation.js';
2
+ /** A seeded 4D simplex noise generator. Create one with {@link create}. */
3
+ export type Simplex4DGenerator = Permutation;
4
+ /**
5
+ * Creates a 4D simplex noise generator with the given seed.
6
+ *
7
+ * @param seed The seed value for the noise generator
8
+ * @returns A generator to pass to {@link sample}
9
+ */
10
+ export declare function create(seed: number): Simplex4DGenerator;
11
+ /**
12
+ * Samples 4D simplex noise, returning a value in the interval [-1, 1].
13
+ *
14
+ * The fourth axis is commonly used as time (for animated 3D noise) or as a wrap
15
+ * dimension (map a coordinate around a circle in w for seamless looping).
16
+ *
17
+ * @param generator A generator created with {@link create}
18
+ * @param x X coordinate
19
+ * @param y Y coordinate
20
+ * @param z Z coordinate
21
+ * @param w W coordinate
22
+ * @returns The noise value at (x, y, z, w)
23
+ */
24
+ export declare function sample({ perm, grad4 }: Simplex4DGenerator, x: number, y: number, z: number, w: number): number;
@@ -0,0 +1,22 @@
1
+ import { type Permutation } from './permutation.js';
2
+ /** A seeded 2D Worley (cellular) noise generator. Create one with {@link create}. */
3
+ export type Worley2DGenerator = Permutation;
4
+ /**
5
+ * Creates a 2D Worley noise generator with the given seed.
6
+ *
7
+ * @param seed The seed value for the noise generator
8
+ * @returns A generator to pass to {@link sample}
9
+ */
10
+ export declare function create(seed: number): Worley2DGenerator;
11
+ /**
12
+ * Samples 2D Worley (cellular) noise: the Euclidean distance to the nearest of a
13
+ * set of feature points, one scattered per unit cell. Gives organic cell / scale
14
+ * / caustic patterns; values are roughly in [0, 1] (0 right at a feature point,
15
+ * rising between them). Threshold it for cracks, cells, or region masks.
16
+ *
17
+ * @param generator A generator created with {@link create}
18
+ * @param x X coordinate
19
+ * @param y Y coordinate
20
+ * @returns The distance to the nearest feature point (F1)
21
+ */
22
+ export declare function sample({ perm }: Worley2DGenerator, x: number, y: number): number;
@@ -0,0 +1,23 @@
1
+ import { type Permutation } from './permutation.js';
2
+ /** A seeded 3D Worley (cellular) noise generator. Create one with {@link create}. */
3
+ export type Worley3DGenerator = Permutation;
4
+ /**
5
+ * Creates a 3D Worley noise generator with the given seed.
6
+ *
7
+ * @param seed The seed value for the noise generator
8
+ * @returns A generator to pass to {@link sample}
9
+ */
10
+ export declare function create(seed: number): Worley3DGenerator;
11
+ /**
12
+ * Samples 3D Worley (cellular) noise: the Euclidean distance to the nearest of a
13
+ * set of feature points, one scattered per unit cell. Values are roughly in
14
+ * [0, 1] (0 right at a feature point). Threshold a 3D field for cave networks,
15
+ * ore pockets, or volumetric cell structures.
16
+ *
17
+ * @param generator A generator created with {@link create}
18
+ * @param x X coordinate
19
+ * @param y Y coordinate
20
+ * @param z Z coordinate
21
+ * @returns The distance to the nearest feature point (F1)
22
+ */
23
+ export declare function sample({ perm }: Worley3DGenerator, x: number, y: number, z: number): number;
@@ -0,0 +1,8 @@
1
+ export * as isaac32 from './isaac32.js';
2
+ export type { Isaac32 } from './isaac32.js';
3
+ export * as isaac64 from './isaac64.js';
4
+ export type { Isaac64 } from './isaac64.js';
5
+ export * as mulberry32 from './mulberry32.js';
6
+ export type { Mulberry32 } from './mulberry32.js';
7
+ export type { RandomGenerator } from './random.js';
8
+ export * as random from './random.js';
@@ -0,0 +1,62 @@
1
+ /**
2
+ * State of an ISAAC-32 PRNG: two 256-word arrays plus three accumulators and a
3
+ * cursor into the current batch of results. Create one with {@link create}.
4
+ *
5
+ * ISAAC (Indirection, Shift, Accumulate, Add, Count) generates 256 words per
6
+ * round and hands them out one at a time; {@link next}/{@link sample} refill the
7
+ * batch automatically when it runs dry.
8
+ *
9
+ * The state is a plain object, so it can be inspected,
10
+ * cloned (to fork a sequence), or serialised. `m`/`r` are typed arrays, so a
11
+ * structural clone (e.g. `structuredClone`) forks correctly; a shallow `{...}`
12
+ * copy shares them and does not.
13
+ */
14
+ export type Isaac32 = {
15
+ /** internal state ("mem"), 256 words */
16
+ m: Uint32Array;
17
+ /** current batch of results, 256 words */
18
+ r: Uint32Array;
19
+ /** accumulator */
20
+ a: number;
21
+ /** previous result */
22
+ b: number;
23
+ /** counter, incremented once per batch */
24
+ c: number;
25
+ /** cursor into `r`; a value of 256 means the batch is spent */
26
+ i: number;
27
+ };
28
+ /**
29
+ * Creates ISAAC-32 PRNG state seeded with `seed`.
30
+ *
31
+ * ISAAC is a fast, high-quality generator: its cycle length is at least 2^40 and
32
+ * ~2^8295 on average, and its output passes stringent statistical tests. Bob
33
+ * Jenkins designed it to resist prediction, but it is not founded on
34
+ * cryptographic theory, so **do not rely on it for cryptography** — use the Web
35
+ * Crypto API for that.
36
+ *
37
+ * A `seed` of 0 reproduces the reference implementation's unseeded output. For
38
+ * the full-strength 64-bit variant see {@link Isaac64}.
39
+ *
40
+ * @param seed the seed value (32-bit integer), defaults to 0
41
+ * @returns state to pass to {@link sample} or {@link next}
42
+ */
43
+ export declare function create(seed?: number): Isaac32;
44
+ /**
45
+ * Advances `state` and returns the next raw 32-bit unsigned integer.
46
+ *
47
+ * @param state PRNG state created with {@link create}, mutated in place
48
+ * @returns an integer in the range [0, 2^32)
49
+ */
50
+ export declare function next(state: Isaac32): number;
51
+ /**
52
+ * Advances `state` and returns the next number in the range [0, 1).
53
+ *
54
+ * @param state PRNG state created with {@link create}, mutated in place
55
+ * @returns a number in the range [0, 1)
56
+ */
57
+ export declare function sample(state: Isaac32): number;
58
+ /**
59
+ * Generates a random 32-bit unsigned integer seed, suitable for use with
60
+ * {@link create}.
61
+ */
62
+ export declare function seed(): number;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * State of an ISAAC64 PRNG. Create one with {@link create}.
3
+ *
4
+ * ISAAC64 is the 64-bit variant of ISAAC (Indirection, Shift, Accumulate, Add,
5
+ * Count): same design as {@link Isaac32} but on 64-bit words, giving a larger
6
+ * state and 64-bit output. JavaScript has no native 64-bit integers, so each
7
+ * 64-bit word is split into two 32-bit lanes — `*Hi` (bits 63..32) and `*Lo`
8
+ * (bits 31..0) — and the hot loop does 64-bit math with explicit carries in
9
+ * plain `number`s. That keeps the whole generator in fast Word32 arithmetic
10
+ * (see the note on {@link Isaac32}'s typed arrays); a `bigint` implementation is
11
+ * ~6x slower. `next` reassembles a `bigint`; `sample` skips `bigint` entirely.
12
+ *
13
+ * The state is a plain object, so it can be inspected, cloned (to fork a
14
+ * sequence), or serialised. The lanes are typed arrays, so a structural clone
15
+ * (e.g. `structuredClone`) forks correctly; a shallow `{...}` copy shares them.
16
+ */
17
+ export type Isaac64 = {
18
+ /** internal state ("mem") high/low lanes, 256 words */
19
+ mHi: Uint32Array;
20
+ mLo: Uint32Array;
21
+ /** current batch of results, high/low lanes, 256 words */
22
+ rHi: Uint32Array;
23
+ rLo: Uint32Array;
24
+ /** accumulator */
25
+ aHi: number;
26
+ aLo: number;
27
+ /** previous result */
28
+ bHi: number;
29
+ bLo: number;
30
+ /** counter, incremented once per batch */
31
+ cHi: number;
32
+ cLo: number;
33
+ /** cursor into `r`; a value of 256 means the batch is spent */
34
+ i: number;
35
+ };
36
+ /**
37
+ * Creates ISAAC64 PRNG state seeded with `seed`.
38
+ *
39
+ * ISAAC64 is a fast, high-quality generator with an enormous cycle length
40
+ * (~2^8295 on average). Bob Jenkins designed it to resist prediction, but it is
41
+ * not founded on cryptographic theory, so **do not rely on it for
42
+ * cryptography** — use the Web Crypto API for that.
43
+ *
44
+ * A `seed` of 0 reproduces the reference implementation's unseeded output.
45
+ *
46
+ * @param seed the seed value (64-bit integer), defaults to 0n
47
+ * @returns state to pass to {@link sample} or {@link next}
48
+ */
49
+ export declare function create(seed?: bigint): Isaac64;
50
+ /**
51
+ * Advances `state` and returns the next raw 64-bit unsigned integer.
52
+ *
53
+ * This reassembles a `bigint` from the two lanes, so it allocates; for a value
54
+ * in [0, 1) prefer {@link sample}, which stays in `number` arithmetic.
55
+ *
56
+ * @param state PRNG state created with {@link create}, mutated in place
57
+ * @returns an integer in the range [0, 2^64)
58
+ */
59
+ export declare function next(state: Isaac64): bigint;
60
+ /**
61
+ * Advances `state` and returns the next number in the range [0, 1).
62
+ *
63
+ * The result carries 53 bits of randomness (the width of a double's mantissa),
64
+ * taken from the high bits of a 64-bit word — read straight from the lanes, so
65
+ * no `bigint` is allocated.
66
+ *
67
+ * @param state PRNG state created with {@link create}, mutated in place
68
+ * @returns a number in the range [0, 1)
69
+ */
70
+ export declare function sample(state: Isaac64): number;
71
+ /**
72
+ * Generates a random 64-bit unsigned integer seed, suitable for use with
73
+ * {@link create}.
74
+ */
75
+ export declare function seed(): bigint;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * State of a Mulberry32 PRNG: a single 32-bit accumulator that {@link sample}
3
+ * advances on each call. Create one with {@link create}.
4
+ *
5
+ * Unlike a closure-based generator, the state is a plain object — so it can be
6
+ * inspected, cloned (to fork a sequence), or serialised.
7
+ */
8
+ export type Mulberry32 = {
9
+ a: number;
10
+ };
11
+ /**
12
+ * Creates Mulberry32 PRNG state seeded with `seed`.
13
+ *
14
+ * Mulberry32 is a simple, fast, and effective PRNG that passes statistical tests
15
+ * and has good distribution properties.
16
+ *
17
+ * @param seed the seed value (32-bit integer)
18
+ * @returns state to pass to {@link sample}
19
+ */
20
+ export declare function create(seed: number): Mulberry32;
21
+ /**
22
+ * Advances `state` and returns the next raw 32-bit unsigned integer.
23
+ *
24
+ * The accumulator is kept to 32 bits with `| 0`; without it, `state.a` would grow
25
+ * as an unbounded float and lose integer precision past 2^53 (~5M draws),
26
+ * corrupting the sequence.
27
+ *
28
+ * @param state PRNG state created with {@link create}, mutated in place
29
+ * @returns an integer in the range [0, 2^32)
30
+ */
31
+ export declare function next(state: Mulberry32): number;
32
+ /**
33
+ * Advances `state` and returns the next number in the range [0, 1).
34
+ *
35
+ * @param state PRNG state created with {@link create}, mutated in place
36
+ * @returns a number in the range [0, 1)
37
+ */
38
+ export declare function sample(state: Mulberry32): number;
39
+ /**
40
+ * Generates a random 32-bit unsigned integer seed, suitable for use with
41
+ * {@link create}.
42
+ */
43
+ export declare function seed(): number;
@@ -0,0 +1,67 @@
1
+ import type { Quat } from '../core/quat.js';
2
+ import type { Vec2 } from '../core/vec2.js';
3
+ import type { Vec3 } from '../core/vec3.js';
4
+ import type { Vec4 } from '../core/vec4.js';
5
+ /** A function that returns a random number in the range [0, 1). */
6
+ export type RandomGenerator = () => number;
7
+ /**
8
+ * Returns a random float in the range [min, max).
9
+ * @param random the random generator to use
10
+ * @param min the minimum value (inclusive)
11
+ * @param max the maximum value (exclusive)
12
+ */
13
+ export declare function float(random: RandomGenerator, min: number, max: number): number;
14
+ /**
15
+ * Returns a random integer in the range [min, max] (inclusive).
16
+ * @param random the random generator to use
17
+ * @param min the minimum value (inclusive)
18
+ * @param max the maximum value (inclusive)
19
+ */
20
+ export declare function int(random: RandomGenerator, min: number, max: number): number;
21
+ /**
22
+ * Returns a random boolean.
23
+ * @param random the random generator to use
24
+ * @param chance the probability of returning true, in the range [0, 1]. Defaults to 0.5.
25
+ */
26
+ export declare function bool(random: RandomGenerator, chance?: number): boolean;
27
+ /**
28
+ * Returns a random sign, either 1 or -1.
29
+ * @param random the random generator to use
30
+ * @param plusChance the probability of returning 1, in the range [0, 1]. Defaults to 0.5.
31
+ */
32
+ export declare function sign(random: RandomGenerator, plusChance?: number): number;
33
+ /**
34
+ * Returns a random item from an array.
35
+ * @param random the random generator to use
36
+ * @param items the array to choose from
37
+ * @throws if the array is empty
38
+ */
39
+ export declare function choice<T>(random: RandomGenerator, items: T[]): T;
40
+ /**
41
+ * Writes a random unit-length Vec2 into out.
42
+ * @param out the receiving vector
43
+ * @param random the random generator to use
44
+ * @returns out
45
+ */
46
+ export declare function vec2(out: Vec2, random: RandomGenerator): Vec2;
47
+ /**
48
+ * Writes a random unit-length Vec3 into out.
49
+ * @param out the receiving vector
50
+ * @param random the random generator to use
51
+ * @returns out
52
+ */
53
+ export declare function vec3(out: Vec3, random: RandomGenerator): Vec3;
54
+ /**
55
+ * Writes a random unit-length Vec4 into out.
56
+ * @param out the receiving vector
57
+ * @param random the random generator to use
58
+ * @returns out
59
+ */
60
+ export declare function vec4(out: Vec4, random: RandomGenerator): Vec4;
61
+ /**
62
+ * Writes a random unit quaternion into out.
63
+ * @param out the receiving quaternion
64
+ * @param random the random generator to use
65
+ * @returns out
66
+ */
67
+ export declare function quat(out: Quat, random: RandomGenerator): Quat;