@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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +205 -2
- package/dist/fractal/_types/fractalOptions.d.ts +36 -0
- package/dist/fractal/_types/fractalOptions.d.ts.map +1 -0
- package/dist/fractal/_types/fractalOptions.js +2 -0
- package/dist/fractal/_types/fractalOptions.js.map +1 -0
- package/dist/fractal/_types/index.d.ts +2 -0
- package/dist/fractal/_types/index.d.ts.map +1 -0
- package/dist/fractal/_types/index.js +2 -0
- package/dist/fractal/_types/index.js.map +1 -0
- package/dist/fractal/fractal.d.ts +17 -0
- package/dist/fractal/fractal.d.ts.map +1 -0
- package/dist/fractal/fractal.js +31 -0
- package/dist/fractal/fractal.js.map +1 -0
- package/dist/fractal/index.d.ts +4 -0
- package/dist/fractal/index.d.ts.map +1 -0
- package/dist/fractal/index.js +3 -0
- package/dist/fractal/index.js.map +1 -0
- package/dist/fractal/options.d.ts +14 -0
- package/dist/fractal/options.d.ts.map +1 -0
- package/dist/fractal/options.js +57 -0
- package/dist/fractal/options.js.map +1 -0
- package/dist/gradient/cubeEdge3.d.ts +11 -0
- package/dist/gradient/cubeEdge3.d.ts.map +1 -0
- package/dist/gradient/cubeEdge3.js +39 -0
- package/dist/gradient/cubeEdge3.js.map +1 -0
- package/dist/gradient/index.d.ts +2 -0
- package/dist/gradient/index.d.ts.map +1 -0
- package/dist/gradient/index.js +2 -0
- package/dist/gradient/index.js.map +1 -0
- package/dist/hashes/hash.d.ts +16 -0
- package/dist/hashes/hash.d.ts.map +1 -0
- package/dist/hashes/hash.js +37 -0
- package/dist/hashes/hash.js.map +1 -0
- package/dist/hashes/hashes.d.ts +3 -0
- package/dist/hashes/hashes.d.ts.map +1 -0
- package/dist/hashes/hashes.js +3 -0
- package/dist/hashes/hashes.js.map +1 -0
- package/dist/hashes/index.d.ts +2 -0
- package/dist/hashes/index.d.ts.map +1 -0
- package/dist/hashes/index.js +2 -0
- package/dist/hashes/index.js.map +1 -0
- package/dist/hashes/lowbias.d.ts +12 -0
- package/dist/hashes/lowbias.d.ts.map +1 -0
- package/dist/hashes/lowbias.js +19 -0
- package/dist/hashes/lowbias.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/noiseMap/_types/index.d.ts +3 -0
- package/dist/noiseMap/_types/index.d.ts.map +1 -0
- package/dist/noiseMap/_types/index.js +2 -0
- package/dist/noiseMap/_types/index.js.map +1 -0
- package/dist/noiseMap/_types/noise2d.d.ts +21 -0
- package/dist/noiseMap/_types/noise2d.d.ts.map +1 -0
- package/dist/noiseMap/_types/noise2d.js +2 -0
- package/dist/noiseMap/_types/noise2d.js.map +1 -0
- package/dist/noiseMap/_types/noise3d.d.ts +24 -0
- package/dist/noiseMap/_types/noise3d.d.ts.map +1 -0
- package/dist/noiseMap/_types/noise3d.js +2 -0
- package/dist/noiseMap/_types/noise3d.js.map +1 -0
- package/dist/noiseMap/index.d.ts +6 -0
- package/dist/noiseMap/index.d.ts.map +1 -0
- package/dist/noiseMap/index.js +3 -0
- package/dist/noiseMap/index.js.map +1 -0
- package/dist/noiseMap/noise2d.d.ts +52 -0
- package/dist/noiseMap/noise2d.d.ts.map +1 -0
- package/dist/noiseMap/noise2d.js +105 -0
- package/dist/noiseMap/noise2d.js.map +1 -0
- package/dist/noiseMap/noise3d.d.ts +62 -0
- package/dist/noiseMap/noise3d.d.ts.map +1 -0
- package/dist/noiseMap/noise3d.js +129 -0
- package/dist/noiseMap/noise3d.js.map +1 -0
- package/dist/noiseTypes/cellular/2d.d.ts +36 -0
- package/dist/noiseTypes/cellular/2d.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/2d.js +123 -0
- package/dist/noiseTypes/cellular/2d.js.map +1 -0
- package/dist/noiseTypes/cellular/3d.d.ts +32 -0
- package/dist/noiseTypes/cellular/3d.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/3d.js +143 -0
- package/dist/noiseTypes/cellular/3d.js.map +1 -0
- package/dist/noiseTypes/cellular/_types/index.d.ts +2 -0
- package/dist/noiseTypes/cellular/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/_types/index.js +2 -0
- package/dist/noiseTypes/cellular/_types/index.js.map +1 -0
- package/dist/noiseTypes/cellular/_types/options.d.ts +37 -0
- package/dist/noiseTypes/cellular/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/_types/options.js +2 -0
- package/dist/noiseTypes/cellular/_types/options.js.map +1 -0
- package/dist/noiseTypes/cellular/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/cellular/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/defaultOptions.js +13 -0
- package/dist/noiseTypes/cellular/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/cellular/floorDiv.d.ts +18 -0
- package/dist/noiseTypes/cellular/floorDiv.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/floorDiv.js +28 -0
- package/dist/noiseTypes/cellular/floorDiv.js.map +1 -0
- package/dist/noiseTypes/cellular/index.d.ts +4 -0
- package/dist/noiseTypes/cellular/index.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/index.js +3 -0
- package/dist/noiseTypes/cellular/index.js.map +1 -0
- package/dist/noiseTypes/cellular/options.d.ts +6 -0
- package/dist/noiseTypes/cellular/options.d.ts.map +1 -0
- package/dist/noiseTypes/cellular/options.js +20 -0
- package/dist/noiseTypes/cellular/options.js.map +1 -0
- package/dist/noiseTypes/index.d.ts +5 -0
- package/dist/noiseTypes/index.d.ts.map +1 -0
- package/dist/noiseTypes/index.js +5 -0
- package/dist/noiseTypes/index.js.map +1 -0
- package/dist/noiseTypes/perlin/2d.d.ts +23 -0
- package/dist/noiseTypes/perlin/2d.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/2d.js +64 -0
- package/dist/noiseTypes/perlin/2d.js.map +1 -0
- package/dist/noiseTypes/perlin/3d.d.ts +24 -0
- package/dist/noiseTypes/perlin/3d.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/3d.js +69 -0
- package/dist/noiseTypes/perlin/3d.js.map +1 -0
- package/dist/noiseTypes/perlin/_types/index.d.ts +2 -0
- package/dist/noiseTypes/perlin/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/_types/index.js +2 -0
- package/dist/noiseTypes/perlin/_types/index.js.map +1 -0
- package/dist/noiseTypes/perlin/_types/options.d.ts +22 -0
- package/dist/noiseTypes/perlin/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/_types/options.js +2 -0
- package/dist/noiseTypes/perlin/_types/options.js.map +1 -0
- package/dist/noiseTypes/perlin/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/perlin/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/defaultOptions.js +14 -0
- package/dist/noiseTypes/perlin/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/perlin/index.d.ts +4 -0
- package/dist/noiseTypes/perlin/index.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/index.js +3 -0
- package/dist/noiseTypes/perlin/index.js.map +1 -0
- package/dist/noiseTypes/perlin/options.d.ts +9 -0
- package/dist/noiseTypes/perlin/options.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/options.js +17 -0
- package/dist/noiseTypes/perlin/options.js.map +1 -0
- package/dist/noiseTypes/perlin/sample2.d.ts +25 -0
- package/dist/noiseTypes/perlin/sample2.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/sample2.js +58 -0
- package/dist/noiseTypes/perlin/sample2.js.map +1 -0
- package/dist/noiseTypes/perlin/sample3.d.ts +9 -0
- package/dist/noiseTypes/perlin/sample3.d.ts.map +1 -0
- package/dist/noiseTypes/perlin/sample3.js +38 -0
- package/dist/noiseTypes/perlin/sample3.js.map +1 -0
- package/dist/noiseTypes/simplex/2d.d.ts +23 -0
- package/dist/noiseTypes/simplex/2d.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/2d.js +64 -0
- package/dist/noiseTypes/simplex/2d.js.map +1 -0
- package/dist/noiseTypes/simplex/3d.d.ts +25 -0
- package/dist/noiseTypes/simplex/3d.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/3d.js +70 -0
- package/dist/noiseTypes/simplex/3d.js.map +1 -0
- package/dist/noiseTypes/simplex/_types/index.d.ts +2 -0
- package/dist/noiseTypes/simplex/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/_types/index.js +2 -0
- package/dist/noiseTypes/simplex/_types/index.js.map +1 -0
- package/dist/noiseTypes/simplex/_types/options.d.ts +33 -0
- package/dist/noiseTypes/simplex/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/_types/options.js +2 -0
- package/dist/noiseTypes/simplex/_types/options.js.map +1 -0
- package/dist/noiseTypes/simplex/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/simplex/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/defaultOptions.js +14 -0
- package/dist/noiseTypes/simplex/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/simplex/index.d.ts +4 -0
- package/dist/noiseTypes/simplex/index.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/index.js +3 -0
- package/dist/noiseTypes/simplex/index.js.map +1 -0
- package/dist/noiseTypes/simplex/options.d.ts +9 -0
- package/dist/noiseTypes/simplex/options.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/options.js +17 -0
- package/dist/noiseTypes/simplex/options.js.map +1 -0
- package/dist/noiseTypes/simplex/sample2.d.ts +32 -0
- package/dist/noiseTypes/simplex/sample2.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/sample2.js +94 -0
- package/dist/noiseTypes/simplex/sample2.js.map +1 -0
- package/dist/noiseTypes/simplex/sample3.d.ts +19 -0
- package/dist/noiseTypes/simplex/sample3.d.ts.map +1 -0
- package/dist/noiseTypes/simplex/sample3.js +110 -0
- package/dist/noiseTypes/simplex/sample3.js.map +1 -0
- package/dist/noiseTypes/white/2d.d.ts +18 -0
- package/dist/noiseTypes/white/2d.d.ts.map +1 -0
- package/dist/noiseTypes/white/2d.js +33 -0
- package/dist/noiseTypes/white/2d.js.map +1 -0
- package/dist/noiseTypes/white/3d.d.ts +17 -0
- package/dist/noiseTypes/white/3d.d.ts.map +1 -0
- package/dist/noiseTypes/white/3d.js +34 -0
- package/dist/noiseTypes/white/3d.js.map +1 -0
- package/dist/noiseTypes/white/_types/index.d.ts +2 -0
- package/dist/noiseTypes/white/_types/index.d.ts.map +1 -0
- package/dist/noiseTypes/white/_types/index.js +2 -0
- package/dist/noiseTypes/white/_types/index.js.map +1 -0
- package/dist/noiseTypes/white/_types/options.d.ts +28 -0
- package/dist/noiseTypes/white/_types/options.d.ts.map +1 -0
- package/dist/noiseTypes/white/_types/options.js +2 -0
- package/dist/noiseTypes/white/_types/options.js.map +1 -0
- package/dist/noiseTypes/white/defaultOptions.d.ts +4 -0
- package/dist/noiseTypes/white/defaultOptions.d.ts.map +1 -0
- package/dist/noiseTypes/white/defaultOptions.js +10 -0
- package/dist/noiseTypes/white/defaultOptions.js.map +1 -0
- package/dist/noiseTypes/white/index.d.ts +4 -0
- package/dist/noiseTypes/white/index.d.ts.map +1 -0
- package/dist/noiseTypes/white/index.js +3 -0
- package/dist/noiseTypes/white/index.js.map +1 -0
- package/dist/noiseTypes/white/options.d.ts +6 -0
- package/dist/noiseTypes/white/options.d.ts.map +1 -0
- package/dist/noiseTypes/white/options.js +14 -0
- package/dist/noiseTypes/white/options.js.map +1 -0
- package/package.json +74 -3
- package/src/fractal/_types/fractalOptions.ts +35 -0
- package/src/fractal/_types/index.ts +1 -0
- package/src/fractal/fractal.ts +41 -0
- package/src/fractal/index.ts +4 -0
- package/src/fractal/options.ts +62 -0
- package/src/gradient/cubeEdge3.ts +47 -0
- package/src/gradient/index.ts +1 -0
- package/src/hashes/hash.ts +41 -0
- package/src/hashes/hashes.ts +2 -0
- package/src/hashes/index.ts +1 -0
- package/src/hashes/lowbias.ts +18 -0
- package/src/index.ts +4 -0
- package/src/noiseMap/_types/index.ts +2 -0
- package/src/noiseMap/_types/noise2d.ts +21 -0
- package/src/noiseMap/_types/noise3d.ts +24 -0
- package/src/noiseMap/index.ts +7 -0
- package/src/noiseMap/noise2d.ts +117 -0
- package/src/noiseMap/noise3d.ts +146 -0
- package/src/noiseTypes/cellular/2d.ts +142 -0
- package/src/noiseTypes/cellular/3d.ts +171 -0
- package/src/noiseTypes/cellular/_types/index.ts +1 -0
- package/src/noiseTypes/cellular/_types/options.ts +37 -0
- package/src/noiseTypes/cellular/defaultOptions.ts +15 -0
- package/src/noiseTypes/cellular/floorDiv.ts +27 -0
- package/src/noiseTypes/cellular/index.ts +4 -0
- package/src/noiseTypes/cellular/options.ts +20 -0
- package/src/noiseTypes/index.ts +4 -0
- package/src/noiseTypes/perlin/2d.ts +86 -0
- package/src/noiseTypes/perlin/3d.ts +93 -0
- package/src/noiseTypes/perlin/_types/index.ts +1 -0
- package/src/noiseTypes/perlin/_types/options.ts +23 -0
- package/src/noiseTypes/perlin/defaultOptions.ts +16 -0
- package/src/noiseTypes/perlin/index.ts +4 -0
- package/src/noiseTypes/perlin/options.ts +19 -0
- package/src/noiseTypes/perlin/sample2.ts +62 -0
- package/src/noiseTypes/perlin/sample3.ts +43 -0
- package/src/noiseTypes/simplex/2d.ts +86 -0
- package/src/noiseTypes/simplex/3d.ts +94 -0
- package/src/noiseTypes/simplex/_types/index.ts +1 -0
- package/src/noiseTypes/simplex/_types/options.ts +34 -0
- package/src/noiseTypes/simplex/defaultOptions.ts +16 -0
- package/src/noiseTypes/simplex/index.ts +4 -0
- package/src/noiseTypes/simplex/options.ts +19 -0
- package/src/noiseTypes/simplex/sample2.ts +103 -0
- package/src/noiseTypes/simplex/sample3.ts +128 -0
- package/src/noiseTypes/white/2d.ts +44 -0
- package/src/noiseTypes/white/3d.ts +46 -0
- package/src/noiseTypes/white/_types/index.ts +1 -0
- package/src/noiseTypes/white/_types/options.ts +28 -0
- package/src/noiseTypes/white/defaultOptions.ts +12 -0
- package/src/noiseTypes/white/index.ts +4 -0
- package/src/noiseTypes/white/options.ts +14 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this package are documented here. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this package follows [Semantic Versioning](https://semver.org).
|
|
4
|
+
|
|
5
|
+
## [0.1.0] - 4-12-2026
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Noise map types `Noise2D` and `Noise3D`, and the `Noise2D` and `Noise3D` namespaces with `create`, `width`, `height`, `get`, `set`, `forEach`, `getValues` and `toPixelBuffer`, plus `depth` and `toPixelBuffers` on `Noise3D`. A map is a branded `Float32Array` holding its size in a two- or three-value header, then every cell in row-major order with x fastest, so it can be transferred or uploaded without conversion. `get` and `set` throw a `RangeError` for coordinates that are not integers inside the map; `getValues` returns a view of the cells without the header and without bounds checks, sharing the map's memory. `toPixelBuffer` returns a new greyscale RGBA buffer, and values outside [0, 1] wrap rather than clamp. Sizes must be positive integers up to 2^24.
|
|
10
|
+
- White noise: `White2.create` and `White3.create`. Every cell gets an independent, uniformly distributed value in [0, 1), taken from the top 24 bits of the hash of its world cell and the seed.
|
|
11
|
+
- Fractal Perlin noise: `Perlin2.create` and `Perlin3.create`, summing `octaves` layers controlled by `scale`, `lacunarity` and `persistence`. Gradients are picked by integer hash and blended with a smootherstep fade, so the value and its first two derivatives are continuous.
|
|
12
|
+
- Fractal simplex noise: `Simplex2.create` and `Simplex3.create`, with the same options as Perlin. It fills more of [0, 1] than Perlin at the same options and shows no grid alignment. 3D simplex uses a kernel radius of √0.5 rather than the commonly published 0.6, so it has no discontinuities at tetrahedron faces.
|
|
13
|
+
- Cellular (Worley) noise: `Cellular2.create` and `Cellular3.create`. Each value is the distance to the nearest feature point (F1), one point per `cellWidth` × `cellHeight` (× `cellDepth`) grid cell, divided by the cell's diagonal. Only neighbouring grid cells are searched, which is effectively exact for square or cube-shaped cells but leaves visible seams when one side of the cell is much longer than another.
|
|
14
|
+
- Option types `White2dOptions`, `White3dOptions`, `Perlin2dOptions`, `Perlin3dOptions`, `Simplex2dOptions`, `Simplex3dOptions`, `Cellular2dOptions` and `Cellular3dOptions`, and `FractalOptions` for the options Perlin and simplex share. Every option is optional in `create` and falls back to a documented default.
|
|
15
|
+
- Every generator returns values in [0, 1] through a fixed mapping set by the options, never stretched per map, so chunks stay consistent. Values depend only on the world cell (`x + offsetX` and so on) and the options, never on the map's size, so chunks of any size created with their world position as the offset agree bit for bit where they overlap.
|
|
16
|
+
- Seeded output is identical on every JavaScript engine: generators use only operations that ECMAScript defines exactly, never engine-approximated functions such as `sin` or `exp`. Only the low 32 bits of a seed take part, and patterns repeat every 2^32 cells along each axis.
|
|
17
|
+
- Every `create` throws a `RangeError` when the size is not a positive integer or an option breaks its documented contract: a non-integer seed, an offset that is not a safe integer, `octaves` outside 1–32, `persistence` outside [0, 1], a non-positive or non-finite `scale` or `lacunarity`, a cell size that is not a positive safe integer, or a combination of `scale`, `lacunarity` and `octaves` whose finest octave would overflow.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Quin Partridge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,206 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cult-frog/noise
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Seeded procedural noise for games: white, Perlin, simplex and cellular, in 2D and 3D, built for chunked worlds.
|
|
4
|
+
|
|
5
|
+
Every generator returns its map as a single `Float32Array`, so it can be posted to a worker, uploaded to the GPU or read in a tight loop without conversion. Values depend only on the world position and the options, never on the map's size, so neighbouring chunks of a world line up exactly, and the same seed gives the same map on every JavaScript engine.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Noise2D, Simplex2 } from '@cult-frog/noise';
|
|
9
|
+
|
|
10
|
+
const map = Simplex2.create(256, 256, { seed: 42, scale: 64 });
|
|
11
|
+
|
|
12
|
+
const height = Noise2D.get(map, 10, 20); // a value in [0, 1]
|
|
13
|
+
const pixels = Noise2D.toPixelBuffer(map); // greyscale RGBA, ready for ImageData
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- **Four kinds of noise:** white, Perlin, simplex and cellular (Worley), each in 2D and 3D.
|
|
19
|
+
- **Seamless chunks:** maps created with the world position of their corner as the offset agree bit for bit wherever they overlap, at any chunk size.
|
|
20
|
+
- **Deterministic everywhere:** the same seed and options give the same values on every engine. Gradients are picked by integer hash, not by `sin` and `cos`, which engines are allowed to round differently.
|
|
21
|
+
- **One flat buffer per map:** a map is a branded `Float32Array` with its size in a small header, so there is nothing to convert or copy before uploading or transferring it.
|
|
22
|
+
- **Fractal noise built in:** Perlin and simplex sum any number of octaves, with `scale`, `lacunarity` and `persistence` options.
|
|
23
|
+
- **Validated options:** an option outside its documented range throws a `RangeError` instead of quietly producing `NaN`.
|
|
24
|
+
- **Tree-shakeable:** ES modules with no side effects.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
pnpm add @cult-frog/noise
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
npm install @cult-frog/noise
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Requirements
|
|
37
|
+
|
|
38
|
+
- Node.js 20.19 or later, or any modern bundler.
|
|
39
|
+
- The package is published as ES modules only, with bundled type declarations.
|
|
40
|
+
|
|
41
|
+
## Quick start
|
|
42
|
+
|
|
43
|
+
### Generating a map
|
|
44
|
+
|
|
45
|
+
Each generator is a namespace with a single `create` function. Pass the size, then any options you want to change; the rest use their defaults.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { Cellular2, Perlin3, White2 } from '@cult-frog/noise';
|
|
49
|
+
|
|
50
|
+
const grain = White2.create(128, 128, { seed: 7 });
|
|
51
|
+
const cells = Cellular2.create(128, 128, { cellWidth: 16, cellHeight: 16 });
|
|
52
|
+
const caves = Perlin3.create(64, 64, 64, { scale: 24, octaves: 3 });
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Reading values
|
|
56
|
+
|
|
57
|
+
`Noise2D.get` and `Noise3D.get` check their coordinates on every call, which suits occasional lookups. For a pass over the whole map, take the values once with `getValues`, a view of the cells without the header:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { Noise2D, Perlin2 } from '@cult-frog/noise';
|
|
61
|
+
|
|
62
|
+
const map = Perlin2.create(256, 256, { seed: 1 });
|
|
63
|
+
|
|
64
|
+
let water = 0;
|
|
65
|
+
for (const value of Noise2D.getValues(map)) {
|
|
66
|
+
if (value < 0.45) water++;
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The values are in row-major order, so cell (x, y) is at index `y * width + x`. The view shares the map's memory, so writing to it changes the map.
|
|
71
|
+
|
|
72
|
+
### Chunked worlds
|
|
73
|
+
|
|
74
|
+
Use the world position of a chunk's first cell as its offset. Every chunk then samples the same continuous world, and chunks of different sizes still match where they overlap.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { Simplex2 } from '@cult-frog/noise';
|
|
78
|
+
|
|
79
|
+
const CHUNK_SIZE = 64;
|
|
80
|
+
const options = { seed: 1234, scale: 80, octaves: 6 };
|
|
81
|
+
|
|
82
|
+
function createChunk(chunkX: number, chunkY: number) {
|
|
83
|
+
return Simplex2.create(CHUNK_SIZE, CHUNK_SIZE, {
|
|
84
|
+
...options,
|
|
85
|
+
offsetX: chunkX * CHUNK_SIZE,
|
|
86
|
+
offsetY: chunkY * CHUNK_SIZE,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Offsets are whole map cells and must be safe integers, so `x + offset` is always exact.
|
|
92
|
+
|
|
93
|
+
### Drawing a map
|
|
94
|
+
|
|
95
|
+
`toPixelBuffer` returns a new greyscale RGBA buffer. `ImageData` wants a `Uint8ClampedArray`, which can share the same bytes:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { Noise2D, Perlin2 } from '@cult-frog/noise';
|
|
99
|
+
|
|
100
|
+
const map = Perlin2.create(256, 256);
|
|
101
|
+
const pixels = Noise2D.toPixelBuffer(map);
|
|
102
|
+
const image = new ImageData(new Uint8ClampedArray(pixels.buffer), 256, 256);
|
|
103
|
+
context.putImageData(image, 0, 0);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
For a 3D map, `Noise3D.toPixelBuffer(map, z)` draws one layer and `Noise3D.toPixelBuffers(map)` draws them all.
|
|
107
|
+
|
|
108
|
+
## Conventions
|
|
109
|
+
|
|
110
|
+
**Values are in [0, 1].** Every generator maps its output into [0, 1] with a fixed mapping set by the options, never stretched to each map's own minimum and maximum. That is what keeps chunks consistent, at the cost of contrast: summed octaves rarely reach the extremes. If you need contrast more than tiling, stretch the result yourself. Typical spans with the default options:
|
|
111
|
+
|
|
112
|
+
| Generator | Typical span | Average |
|
|
113
|
+
| --- | --- | --- |
|
|
114
|
+
| `White2`, `White3` | the full [0, 1) | 0.5 |
|
|
115
|
+
| `Perlin2` | about 0.2–0.75 | 0.5 |
|
|
116
|
+
| `Perlin3` | about 0.25–0.75 | 0.5 |
|
|
117
|
+
| `Simplex2` | about 0.05–0.95 | 0.5 |
|
|
118
|
+
| `Simplex3` | about 0.1–0.9 | 0.5 |
|
|
119
|
+
| `Cellular2` | about 0–0.8 | 0.3 |
|
|
120
|
+
| `Cellular3` | about 0–0.65 | 0.3 |
|
|
121
|
+
|
|
122
|
+
Measured on 256² maps in 2D and 48³ to 64³ in 3D. A single octave spreads much wider than the default five.
|
|
123
|
+
|
|
124
|
+
A threshold such as a sea level tuned for one generator covers a different share of the map with another.
|
|
125
|
+
|
|
126
|
+
**Maps are row-major.** In 2D, x varies fastest. In 3D, x varies fastest, then y, then z, so each z layer is one contiguous slice.
|
|
127
|
+
|
|
128
|
+
**The caller owns every map.** Generators allocate a new map per call and keep no reference to it. Never write to the header (the first two values in 2D, three in 3D): every helper reads the size from it.
|
|
129
|
+
|
|
130
|
+
**Seeds are integers, and only their low 32 bits count,** so seeds 2^32 apart give the same map.
|
|
131
|
+
|
|
132
|
+
**Patterns repeat every 2^32 cells** along each axis, because cell coordinates are hashed as 32-bit integers. At one cell per metre, that is about 4.3 million kilometres.
|
|
133
|
+
|
|
134
|
+
## API
|
|
135
|
+
|
|
136
|
+
All options are optional; anything left out uses the default shown.
|
|
137
|
+
|
|
138
|
+
### Generators
|
|
139
|
+
|
|
140
|
+
| Namespace | Noise | `create` |
|
|
141
|
+
| --- | --- | --- |
|
|
142
|
+
| `White2`, `White3` | Independent uniform value per cell | `create(width, height, options?)`, `create(width, height, depth, options?)` |
|
|
143
|
+
| `Perlin2`, `Perlin3` | Fractal Perlin noise | same |
|
|
144
|
+
| `Simplex2`, `Simplex3` | Fractal simplex noise | same |
|
|
145
|
+
| `Cellular2`, `Cellular3` | Cellular (Worley) noise: distance to the nearest feature point | same |
|
|
146
|
+
|
|
147
|
+
Sizes must be positive integers. Every `create` throws a `RangeError` for an invalid size or option.
|
|
148
|
+
|
|
149
|
+
### Shared options
|
|
150
|
+
|
|
151
|
+
Every generator takes:
|
|
152
|
+
|
|
153
|
+
| Option | Default | Contract |
|
|
154
|
+
| --- | --- | --- |
|
|
155
|
+
| `seed` | `0` | An integer. |
|
|
156
|
+
| `offsetX`, `offsetY` | `0` | World position of the map's first column and row, in cells. Safe integers. |
|
|
157
|
+
| `offsetZ` (3D only) | `0` | World position of the first layer. A safe integer. |
|
|
158
|
+
|
|
159
|
+
### Fractal options: `Perlin2`, `Perlin3`, `Simplex2`, `Simplex3`
|
|
160
|
+
|
|
161
|
+
These are the `FractalOptions` type, which `Perlin2dOptions` and `Simplex2dOptions` extend.
|
|
162
|
+
|
|
163
|
+
| Option | Default | Contract |
|
|
164
|
+
| --- | --- | --- |
|
|
165
|
+
| `scale` | `20` | Width of one lattice cell of the first octave, in map cells. Larger values zoom in. Finite and above 0. |
|
|
166
|
+
| `octaves` | `5` | Number of layers summed. An integer from 1 to 32. Cost grows linearly with it. |
|
|
167
|
+
| `persistence` | `0.5` | Amplitude multiplier from one octave to the next, from 0 to 1. |
|
|
168
|
+
| `lacunarity` | `2` | Frequency multiplier from one octave to the next. Finite and above 0. |
|
|
169
|
+
|
|
170
|
+
Extreme combinations of `scale`, `lacunarity` and `octaves` whose finest octave would overflow are rejected together. Lacunarity 2 stays far inside the limit even at 32 octaves.
|
|
171
|
+
|
|
172
|
+
Simplex features come out slightly smaller than Perlin's at the same `scale`, because its triangles' edges are about 0.82 × `scale` long. 3D simplex costs about half as much per octave as 3D Perlin.
|
|
173
|
+
|
|
174
|
+
### Cellular options: `Cellular2`, `Cellular3`
|
|
175
|
+
|
|
176
|
+
| Option | Default | Contract |
|
|
177
|
+
| --- | --- | --- |
|
|
178
|
+
| `cellWidth`, `cellHeight` | `10` | Size of one grid cell, which holds one feature point, in map cells. Positive safe integers. |
|
|
179
|
+
| `cellDepth` (3D only) | `10` | Depth of one grid cell. A positive safe integer. |
|
|
180
|
+
|
|
181
|
+
Each value is the distance to the nearest feature point (F1), divided by the diagonal of one grid cell.
|
|
182
|
+
|
|
183
|
+
**Known limitation:** only the neighbouring grid cells are searched for the nearest point. For square or cube-shaped cells this is effectively exact, but cells much longer on one side than another show visible seams: roughly 0.15% of values are wrong at 10 × 20, 3% at 5 × 20 and 8% at 3 × 17.
|
|
184
|
+
|
|
185
|
+
### Noise maps: `Noise2D`, `Noise3D`
|
|
186
|
+
|
|
187
|
+
`Noise2D` and `Noise3D` are both the map types and namespaces of helpers for them.
|
|
188
|
+
|
|
189
|
+
| Function | Description |
|
|
190
|
+
| --- | --- |
|
|
191
|
+
| `create(width, height)`, `create(width, height, depth)` | A zero-filled map. Sizes up to 2^24. |
|
|
192
|
+
| `width`, `height`, `depth` (3D) | The map's size, read from its header. |
|
|
193
|
+
| `get(map, x, y)`, `get(map, x, y, z)` | One cell's value. Throws a `RangeError` outside the map. |
|
|
194
|
+
| `set(map, x, y, value)`, `set(map, x, y, z, value)` | Writes one cell. Throws a `RangeError` outside the map. |
|
|
195
|
+
| `forEach(map, callback)` | Calls `callback(value, x, y)` (or `x, y, z`) for every cell, in storage order. |
|
|
196
|
+
| `getValues(map)` | A view of the cells without the header, with no bounds checks, for whole-map passes. |
|
|
197
|
+
| `toPixelBuffer(map)`, `toPixelBuffer(map, z)` | A new greyscale RGBA buffer. Values outside [0, 1] wrap rather than clamp. |
|
|
198
|
+
| `toPixelBuffers(map)` (3D) | One RGBA buffer per layer. |
|
|
199
|
+
|
|
200
|
+
### Types
|
|
201
|
+
|
|
202
|
+
`Noise2D`, `Noise3D`, `FractalOptions`, `White2dOptions`, `White3dOptions`, `Perlin2dOptions`, `Perlin3dOptions`, `Simplex2dOptions`, `Simplex3dOptions`, `Cellular2dOptions` and `Cellular3dOptions`.
|
|
203
|
+
|
|
204
|
+
## License
|
|
205
|
+
|
|
206
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options shared by every fractal (multi-octave) generator: how octaves are
|
|
3
|
+
* spaced, weighted and seeded. Each generator's own options add its offsets.
|
|
4
|
+
*
|
|
5
|
+
* Besides each field's own contract, `scale`, `lacunarity` and `octaves` are
|
|
6
|
+
* rejected together when the finest octave's frequency is so high that sample
|
|
7
|
+
* coordinates would overflow. That takes extreme values (lacunarity 2 stays far
|
|
8
|
+
* inside the limit even at 32 octaves), and the check does not depend on the
|
|
9
|
+
* offsets, so options that work for one chunk work for every chunk.
|
|
10
|
+
*/
|
|
11
|
+
export interface FractalOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Selects the lattice gradients and each octave's offset. An integer; only its
|
|
14
|
+
* low 32 bits take part, so seeds 2^32 apart give the same map.
|
|
15
|
+
*/
|
|
16
|
+
seed: number;
|
|
17
|
+
/**
|
|
18
|
+
* Width of one lattice cell of the first octave, in map cells. Larger values
|
|
19
|
+
* zoom in. Finite and greater than 0.
|
|
20
|
+
*/
|
|
21
|
+
scale: number;
|
|
22
|
+
/**
|
|
23
|
+
* Frequency multiplier from one octave to the next; 2 halves the feature size
|
|
24
|
+
* each octave. Finite and greater than 0.
|
|
25
|
+
*/
|
|
26
|
+
lacunarity: number;
|
|
27
|
+
/**
|
|
28
|
+
* Amplitude multiplier from one octave to the next, in [0, 1]. Output is scaled
|
|
29
|
+
* by the sum of all amplitudes, so raising this or adding octaves narrows the
|
|
30
|
+
* spread of values around 0.5.
|
|
31
|
+
*/
|
|
32
|
+
persistence: number;
|
|
33
|
+
/** Number of layers summed. Integer in [1, 32]. Cost grows linearly with it. */
|
|
34
|
+
octaves: number;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=fractalOptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractalOptions.d.ts","sourceRoot":"","sources":["../../../src/fractal/_types/fractalOptions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB,gFAAgF;IAChF,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractalOptions.js","sourceRoot":"","sources":["../../../src/fractal/_types/fractalOptions.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/fractal/_types/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/fractal/_types/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FractalOptions } from './_types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Seed-derived per-octave sample offsets (x, y interleaved) and the sum of all
|
|
4
|
+
* octave amplitudes, which bounds the summed noise.
|
|
5
|
+
*
|
|
6
|
+
* The offsets stop every octave sharing the lattice origin. They deliberately
|
|
7
|
+
* exclude `offsetX`/`offsetY`, so every chunk of a world adds the same constants
|
|
8
|
+
* to the same exact integer world position and gets bit-identical samples.
|
|
9
|
+
*
|
|
10
|
+
* The offsets come from `@cult-frog/random`'s sequence, so every Perlin and
|
|
11
|
+
* simplex map depends on it. Random treats a sequence change as breaking, which
|
|
12
|
+
* its semver range here already excludes; moving this package onto a new
|
|
13
|
+
* breaking range of random changes every fractal map, so it is a breaking
|
|
14
|
+
* release of this package too. The golden-value tests fail when that happens.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getOctaveConfig(config: FractalOptions, dimensions: 2 | 3): [octaveOffsets: Float32Array, maxPossibleHeight: number];
|
|
17
|
+
//# sourceMappingURL=fractal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractal.d.ts","sourceRoot":"","sources":["../../src/fractal/fractal.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAIxD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,cAAc,EACtB,UAAU,EAAE,CAAC,GAAG,CAAC,GAChB,CAAC,aAAa,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,CAAC,CAiB1D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Random } from '@cult-frog/random';
|
|
2
|
+
const OFFSET_RANGE = 100_000;
|
|
3
|
+
/**
|
|
4
|
+
* Seed-derived per-octave sample offsets (x, y interleaved) and the sum of all
|
|
5
|
+
* octave amplitudes, which bounds the summed noise.
|
|
6
|
+
*
|
|
7
|
+
* The offsets stop every octave sharing the lattice origin. They deliberately
|
|
8
|
+
* exclude `offsetX`/`offsetY`, so every chunk of a world adds the same constants
|
|
9
|
+
* to the same exact integer world position and gets bit-identical samples.
|
|
10
|
+
*
|
|
11
|
+
* The offsets come from `@cult-frog/random`'s sequence, so every Perlin and
|
|
12
|
+
* simplex map depends on it. Random treats a sequence change as breaking, which
|
|
13
|
+
* its semver range here already excludes; moving this package onto a new
|
|
14
|
+
* breaking range of random changes every fractal map, so it is a breaking
|
|
15
|
+
* release of this package too. The golden-value tests fail when that happens.
|
|
16
|
+
*/
|
|
17
|
+
export function getOctaveConfig(config, dimensions) {
|
|
18
|
+
const octaveOffsets = new Float32Array(config.octaves * dimensions);
|
|
19
|
+
const prng = new Random(config.seed);
|
|
20
|
+
let maxPossibleHeight = 0;
|
|
21
|
+
let amplitude = 1;
|
|
22
|
+
for (let i = 0; i < config.octaves; i++) {
|
|
23
|
+
const index = i * dimensions;
|
|
24
|
+
for (let j = 0; j < dimensions; j++)
|
|
25
|
+
octaveOffsets[index + j] = prng.float(-OFFSET_RANGE, OFFSET_RANGE);
|
|
26
|
+
maxPossibleHeight += amplitude;
|
|
27
|
+
amplitude *= config.persistence;
|
|
28
|
+
}
|
|
29
|
+
return [octaveOffsets, maxPossibleHeight];
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=fractal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fractal.js","sourceRoot":"","sources":["../../src/fractal/fractal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAI3C,MAAM,YAAY,GAAG,OAAO,CAAC;AAE7B;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,eAAe,CAC7B,MAAsB,EACtB,UAAiB;IAEjB,MAAM,aAAa,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,OAAO,GAAG,UAAU,CAAC,CAAC;IACpE,MAAM,IAAI,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAErC,IAAI,iBAAiB,GAAG,CAAC,CAAC;IAC1B,IAAI,SAAS,GAAG,CAAC,CAAC;IAElB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE,CAAC;QACxC,MAAM,KAAK,GAAG,CAAC,GAAG,UAAU,CAAC;QAC7B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,EAAE,CAAC,EAAE;YACjC,aAAa,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;QAErE,iBAAiB,IAAI,SAAS,CAAC;QAC/B,SAAS,IAAI,MAAM,CAAC,WAAW,CAAC;IAClC,CAAC;IAED,OAAO,CAAC,aAAa,EAAE,iBAAiB,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fractal/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAE7B,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/fractal/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FractalOptions } from './_types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Throws if `options` breaks a contract in {@link FractalOptions}. Each fractal
|
|
4
|
+
* generator calls this before checking its own offsets, so every generator
|
|
5
|
+
* accepts and rejects the same fractal options with the same messages.
|
|
6
|
+
*
|
|
7
|
+
* The last check rejects combinations whose individual values are fine but
|
|
8
|
+
* whose highest sample coordinate would overflow to Infinity, which turns every
|
|
9
|
+
* sample into NaN. It assumes the worst-case world position rather than this
|
|
10
|
+
* map's offsets, so options accepted for one chunk are accepted for every chunk
|
|
11
|
+
* of the same world.
|
|
12
|
+
*/
|
|
13
|
+
export declare function validateFractalOptions(options: FractalOptions): void;
|
|
14
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/fractal/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAkCxD;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAgBpE"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Upper bound on `octaves`. At lacunarity 2 the last octave already has 2^31
|
|
3
|
+
* lattice cells per first-octave cell, far finer than one map cell, and at the
|
|
4
|
+
* default persistence its amplitude is below Float32 resolution. Each octave
|
|
5
|
+
* costs a full sample per cell and a slot in the per-call octave table, so the
|
|
6
|
+
* cap also bounds the cost of a single call.
|
|
7
|
+
*/
|
|
8
|
+
const MAX_OCTAVES = 32;
|
|
9
|
+
/**
|
|
10
|
+
* Upper bound on |world coordinate + octave offset| for any map: a safe-integer
|
|
11
|
+
* offset (< 2^53), plus a map extent that cannot exceed 2^53 cells, plus the
|
|
12
|
+
* octave offset range, rounded up with headroom so rounding near the limit
|
|
13
|
+
* cannot matter.
|
|
14
|
+
*/
|
|
15
|
+
const MAX_WORLD_COORDINATE = 2 ** 55;
|
|
16
|
+
/**
|
|
17
|
+
* Highest per-octave frequency, built with the same repeated multiplication the
|
|
18
|
+
* samplers use. With lacunarity below 1 the frequency falls each octave, so the
|
|
19
|
+
* first octave is the highest.
|
|
20
|
+
*/
|
|
21
|
+
function getMaxFrequency(octaves, lacunarity) {
|
|
22
|
+
let frequency = 1;
|
|
23
|
+
let maxFrequency = 1;
|
|
24
|
+
for (let i = 1; i < octaves; i++) {
|
|
25
|
+
frequency *= lacunarity;
|
|
26
|
+
if (frequency > maxFrequency)
|
|
27
|
+
maxFrequency = frequency;
|
|
28
|
+
}
|
|
29
|
+
return maxFrequency;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Throws if `options` breaks a contract in {@link FractalOptions}. Each fractal
|
|
33
|
+
* generator calls this before checking its own offsets, so every generator
|
|
34
|
+
* accepts and rejects the same fractal options with the same messages.
|
|
35
|
+
*
|
|
36
|
+
* The last check rejects combinations whose individual values are fine but
|
|
37
|
+
* whose highest sample coordinate would overflow to Infinity, which turns every
|
|
38
|
+
* sample into NaN. It assumes the worst-case world position rather than this
|
|
39
|
+
* map's offsets, so options accepted for one chunk are accepted for every chunk
|
|
40
|
+
* of the same world.
|
|
41
|
+
*/
|
|
42
|
+
export function validateFractalOptions(options) {
|
|
43
|
+
if (!Number.isInteger(options.seed))
|
|
44
|
+
throw new RangeError('Invalid seed');
|
|
45
|
+
if (!(Number.isInteger(options.octaves) && options.octaves >= 1 && options.octaves <= MAX_OCTAVES))
|
|
46
|
+
throw new RangeError('Invalid octaves');
|
|
47
|
+
if (!(Number.isFinite(options.lacunarity) && options.lacunarity > 0))
|
|
48
|
+
throw new RangeError('Invalid lacunarity');
|
|
49
|
+
if (!(options.persistence >= 0 && options.persistence <= 1))
|
|
50
|
+
throw new RangeError('Invalid persistence');
|
|
51
|
+
if (!(Number.isFinite(options.scale) && options.scale > 0))
|
|
52
|
+
throw new RangeError('Invalid scale');
|
|
53
|
+
const maxScaledFrequency = (1 / options.scale) * getMaxFrequency(options.octaves, options.lacunarity);
|
|
54
|
+
if (!Number.isFinite(MAX_WORLD_COORDINATE * maxScaledFrequency))
|
|
55
|
+
throw new RangeError('Invalid scale, lacunarity, or octaves');
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../src/fractal/options.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,WAAW,GAAG,EAAE,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,oBAAoB,GAAG,CAAC,IAAI,EAAE,CAAC;AAErC;;;;GAIG;AACH,SAAS,eAAe,CAAC,OAAe,EAAE,UAAkB;IAC1D,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,SAAS,IAAI,UAAU,CAAC;QACxB,IAAI,SAAS,GAAG,YAAY;YAAE,YAAY,GAAG,SAAS,CAAC;IACzD,CAAC;IACD,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAuB;IAC5D,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,cAAc,CAAC,CAAC;IAC1E,IACE,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,OAAO,IAAI,CAAC,IAAI,OAAO,CAAC,OAAO,IAAI,WAAW,CAAC;QAE9F,MAAM,IAAI,UAAU,CAAC,iBAAiB,CAAC,CAAC;IAC1C,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,OAAO,CAAC,UAAU,GAAG,CAAC,CAAC;QAClE,MAAM,IAAI,UAAU,CAAC,oBAAoB,CAAC,CAAC;IAC7C,IAAI,CAAC,CAAC,OAAO,CAAC,WAAW,IAAI,CAAC,IAAI,OAAO,CAAC,WAAW,IAAI,CAAC,CAAC;QACzD,MAAM,IAAI,UAAU,CAAC,qBAAqB,CAAC,CAAC;IAC9C,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,eAAe,CAAC,CAAC;IAElG,MAAM,kBAAkB,GACtB,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,eAAe,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAC7E,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,oBAAoB,GAAG,kBAAkB,CAAC;QAC7D,MAAM,IAAI,UAAU,CAAC,uCAAuC,CAAC,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dot product of the gradient hashed for lattice corner (`cx`, `cy`, `cz`) with
|
|
3
|
+
* the offset (`dx`, `dy`, `dz`) from that corner to the sample point.
|
|
4
|
+
*
|
|
5
|
+
* The twelve gradients are the cube's edge directions, (±1, ±1, 0), (±1, 0, ±1)
|
|
6
|
+
* and (0, ±1, ±1), picked by integer hash rather than angle, so each dot product
|
|
7
|
+
* is an exact sum and the same seed gives the same noise in every engine.
|
|
8
|
+
* `Hashes.hash3` is unsigned, which keeps `% 12` in range.
|
|
9
|
+
*/
|
|
10
|
+
export declare function cubeEdge3(cx: number, cy: number, cz: number, dx: number, dy: number, dz: number, seed: number): number;
|
|
11
|
+
//# sourceMappingURL=cubeEdge3.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cubeEdge3.d.ts","sourceRoot":"","sources":["../../src/gradient/cubeEdge3.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CACvB,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,UA4Bb"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Hashes } from '../hashes/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Dot product of the gradient hashed for lattice corner (`cx`, `cy`, `cz`) with
|
|
4
|
+
* the offset (`dx`, `dy`, `dz`) from that corner to the sample point.
|
|
5
|
+
*
|
|
6
|
+
* The twelve gradients are the cube's edge directions, (±1, ±1, 0), (±1, 0, ±1)
|
|
7
|
+
* and (0, ±1, ±1), picked by integer hash rather than angle, so each dot product
|
|
8
|
+
* is an exact sum and the same seed gives the same noise in every engine.
|
|
9
|
+
* `Hashes.hash3` is unsigned, which keeps `% 12` in range.
|
|
10
|
+
*/
|
|
11
|
+
export function cubeEdge3(cx, cy, cz, dx, dy, dz, seed) {
|
|
12
|
+
switch (Hashes.hash3(cx, cy, cz, seed) % 12) {
|
|
13
|
+
case 0:
|
|
14
|
+
return dx + dy;
|
|
15
|
+
case 1:
|
|
16
|
+
return -dx + dy;
|
|
17
|
+
case 2:
|
|
18
|
+
return dx - dy;
|
|
19
|
+
case 3:
|
|
20
|
+
return -dx - dy;
|
|
21
|
+
case 4:
|
|
22
|
+
return dx + dz;
|
|
23
|
+
case 5:
|
|
24
|
+
return -dx + dz;
|
|
25
|
+
case 6:
|
|
26
|
+
return dx - dz;
|
|
27
|
+
case 7:
|
|
28
|
+
return -dx - dz;
|
|
29
|
+
case 8:
|
|
30
|
+
return dy + dz;
|
|
31
|
+
case 9:
|
|
32
|
+
return -dy + dz;
|
|
33
|
+
case 10:
|
|
34
|
+
return dy - dz;
|
|
35
|
+
default:
|
|
36
|
+
return -dy - dz;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=cubeEdge3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cubeEdge3.js","sourceRoot":"","sources":["../../src/gradient/cubeEdge3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CACvB,EAAU,EACV,EAAU,EACV,EAAU,EACV,EAAU,EACV,EAAU,EACV,EAAU,EACV,IAAY;IAEZ,QAAQ,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;QAC5C,KAAK,CAAC;YACJ,OAAO,EAAE,GAAG,EAAE,CAAC;QACjB,KAAK,CAAC;YACJ,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;QAClB,KAAK,CAAC;YACJ,OAAO,EAAE,GAAG,EAAE,CAAC;QACjB,KAAK,CAAC;YACJ,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;QAClB,KAAK,CAAC;YACJ,OAAO,EAAE,GAAG,EAAE,CAAC;QACjB,KAAK,CAAC;YACJ,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;QAClB,KAAK,CAAC;YACJ,OAAO,EAAE,GAAG,EAAE,CAAC;QACjB,KAAK,CAAC;YACJ,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;QAClB,KAAK,CAAC;YACJ,OAAO,EAAE,GAAG,EAAE,CAAC;QACjB,KAAK,CAAC;YACJ,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;QAClB,KAAK,EAAE;YACL,OAAO,EAAE,GAAG,EAAE,CAAC;QACjB;YACE,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;IACpB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/gradient/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/gradient/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unsigned 32-bit hash of an integer lattice point and a seed: the source of
|
|
3
|
+
* every gradient, feature point and white noise value in this package.
|
|
4
|
+
*
|
|
5
|
+
* Each input is truncated to int32 (`| 0`), so non-integers hash like their
|
|
6
|
+
* integer part and coordinates 2^32 apart collide; that is where every
|
|
7
|
+
* generator's 2^32-cell repeat comes from. Each coordinate is mixed in with a
|
|
8
|
+
* full {@link lowbias32} round, so neighbouring points get unrelated hashes.
|
|
9
|
+
*
|
|
10
|
+
* The output is part of the package's compatibility contract: changing it
|
|
11
|
+
* changes every map, and the golden-value tests fail when it does.
|
|
12
|
+
*/
|
|
13
|
+
export declare function hash2(x: number, y: number, seed: number): number;
|
|
14
|
+
/** {@link hash2} with a third coordinate, mixed in the same way. */
|
|
15
|
+
export declare function hash3(x: number, y: number, z: number, seed: number): number;
|
|
16
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hashes/hash.ts"],"names":[],"mappings":"AAcA;;;;;;;;;;;GAWG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAKhE;AAED,oEAAoE;AACpE,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAM3E"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { lowbias32 } from './lowbias.js';
|
|
2
|
+
// A different odd 32-bit multiplier per input, so the hash is not symmetric:
|
|
3
|
+
// (1, 2) and (2, 1) are mixed differently rather than colliding by design.
|
|
4
|
+
// They are xxHash32's primes, chosen for well-mixed bits rather than any
|
|
5
|
+
// property of noise.
|
|
6
|
+
const SEED_MUL = 0xc2b2ae3d;
|
|
7
|
+
const X_MUL = 0x9e3779b1;
|
|
8
|
+
const Y_MUL = 0x85ebca77;
|
|
9
|
+
const Z_MUL = 0x165667b1;
|
|
10
|
+
const INIT = 0x27d4eb2f;
|
|
11
|
+
/**
|
|
12
|
+
* Unsigned 32-bit hash of an integer lattice point and a seed: the source of
|
|
13
|
+
* every gradient, feature point and white noise value in this package.
|
|
14
|
+
*
|
|
15
|
+
* Each input is truncated to int32 (`| 0`), so non-integers hash like their
|
|
16
|
+
* integer part and coordinates 2^32 apart collide; that is where every
|
|
17
|
+
* generator's 2^32-cell repeat comes from. Each coordinate is mixed in with a
|
|
18
|
+
* full {@link lowbias32} round, so neighbouring points get unrelated hashes.
|
|
19
|
+
*
|
|
20
|
+
* The output is part of the package's compatibility contract: changing it
|
|
21
|
+
* changes every map, and the golden-value tests fail when it does.
|
|
22
|
+
*/
|
|
23
|
+
export function hash2(x, y, seed) {
|
|
24
|
+
let h = Math.imul(seed | 0, SEED_MUL) ^ INIT;
|
|
25
|
+
h = lowbias32(h ^ Math.imul(x | 0, X_MUL));
|
|
26
|
+
h = lowbias32(h ^ Math.imul(y | 0, Y_MUL));
|
|
27
|
+
return h;
|
|
28
|
+
}
|
|
29
|
+
/** {@link hash2} with a third coordinate, mixed in the same way. */
|
|
30
|
+
export function hash3(x, y, z, seed) {
|
|
31
|
+
let h = Math.imul(seed | 0, SEED_MUL) ^ INIT;
|
|
32
|
+
h = lowbias32(h ^ Math.imul(x | 0, X_MUL));
|
|
33
|
+
h = lowbias32(h ^ Math.imul(y | 0, Y_MUL));
|
|
34
|
+
h = lowbias32(h ^ Math.imul(z | 0, Z_MUL));
|
|
35
|
+
return h;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=hash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/hashes/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,6EAA6E;AAC7E,2EAA2E;AAC3E,yEAAyE;AACzE,qBAAqB;AACrB,MAAM,QAAQ,GAAG,UAAU,CAAC;AAE5B,MAAM,KAAK,GAAG,UAAU,CAAC;AACzB,MAAM,KAAK,GAAG,UAAU,CAAC;AACzB,MAAM,KAAK,GAAG,UAAU,CAAC;AAEzB,MAAM,IAAI,GAAG,UAAU,CAAC;AAExB;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,KAAK,CAAC,CAAS,EAAE,CAAS,EAAE,IAAY;IACtD,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,IAAI,CAAC;IAC7C,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3C,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3C,OAAO,CAAC,CAAC;AACX,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,KAAK,CAAC,CAAS,EAAE,CAAS,EAAE,CAAS,EAAE,IAAY;IACjE,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,IAAI,CAAC;IAC7C,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3C,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3C,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3C,OAAO,CAAC,CAAC;AACX,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hashes.d.ts","sourceRoot":"","sources":["../../src/hashes/hashes.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hashes.js","sourceRoot":"","sources":["../../src/hashes/hashes.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC"}
|