@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.
- package/CHANGELOG.md +29 -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 +78 -4
- 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
|
@@ -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,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,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
|
+
}
|