This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
FBM.js
100 lines (87 loc) · 3.13 KB
/
FBM.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as THREE from "three";
import { Perlin } from "./Perlin.js";
/**
* This class is an implimentaiton of a Fractal Brownian Motion
* function using Perlin Nosie.
*/
export class FBM {
/**
* Create an instance of the FBM class.
* Use this instance to generate fBm noise.
*
* @param {Object} options Options for fBm generaiton.
* @param {number} options.seed Seed for Perlin Noise
* @param {number} options.scale What distance to view the noisemap
* @param {number} options.persistance How much each octave contributes to the overall shape
* @param {number} options.lacunarity How much detail is added or removed at each octave
* @param {number} options.octaves Levels of detail you want you perlin noise to have
* @param {number} options.redistribution Level of flatness within the valleys
*/
constructor(options) {
const { seed, scale, persistance, lacunarity, octaves, redistribution } =
options;
this._noise = new Perlin(seed);
this._scale = scale || 1;
this._persistance = persistance || 0.5;
this._lacunarity = lacunarity || 2;
this._octaves = octaves || 6;
this._redistribution = redistribution || 1;
}
/**
* Sample 2D Perlin Noise with fBm at given
* coordinates. The function will use <code>Perlin_get2</code> or <code>Perlin_get3</code>
* depending on the input vector's type.
*
* @param {(THREE.Vector2 | THREE.Vector3)} input Coordinates to sample noise at.
* @returns {number} Normalized noise in the range [0, 1]
*/
get2(input) {
let result = 0;
let amplitude = 1;
let frequency = 1;
let max = amplitude;
let noiseFunction = this._noise.get2.bind(this._noise);
for (let i = 0; i < this._octaves; i++) {
const position = new THREE.Vector2(
input.x * this._scale * frequency,
input.y * this._scale * frequency
);
const noiseVal = noiseFunction(position);
result += noiseVal * amplitude;
frequency *= this._lacunarity;
amplitude *= this._persistance;
max += amplitude;
}
const redistributed = Math.pow(result, this._redistribution);
return redistributed / max;
}
/**
* Sample 3D Perlin Noise with fBm at given
* coordinates. The function will use <code>Perlin_get2</code> or <code>Perlin_get3</code>
* depending on the input vector's type.
*
* @param {THREE.Vector3} input Coordinates to sample noise at.
* @returns {number} Normalized noise in the range [0, 1]
*/
get3(input) {
let result = 0;
let amplitude = 1;
let frequency = 1;
let max = amplitude;
let noiseFunction = this._noise.get3.bind(this._noise);
for (let i = 0; i < this._octaves; i++) {
const position = new THREE.Vector2(
input.x * this._scale * frequency,
input.y * this._scale * frequency,
input.z * this._scale * frequency
);
const noiseVal = noiseFunction(position);
result += noiseVal * amplitude;
frequency *= this._lacunarity;
amplitude *= this._persistance;
max += amplitude;
}
const redistributed = Math.pow(result, this._redistribution);
return redistributed / max;
}
}