Skip to content

Commit 09d0b03

Browse files
committed
v2.1.0-pre1: Add scale function option
1 parent 358ec8c commit 09d0b03

File tree

4 files changed

+94
-67
lines changed

4 files changed

+94
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.vimrc
12
lib/
23
node_modules/
34
npm-debug.log

deno_lib/mod.ts

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,27 @@ export interface Options {
1515
frequency: number;
1616
octaves: number;
1717
persistence: number;
18+
scale?: (x: number) => number;
1819
}
1920

20-
function processOptions(options: Partial<Options>): Options {
21-
return {
22-
amplitude: typeof options.amplitude === "number" ? options.amplitude : 1.0,
23-
frequency: typeof options.frequency === "number" ? options.frequency : 1.0,
24-
octaves: typeof options.octaves === "number"
25-
? Math.floor(options.octaves)
26-
: 1,
27-
persistence: typeof options.persistence === "number"
28-
? options.persistence
29-
: 0.5,
30-
};
31-
}
21+
const defaultAmplitude = 1.0;
22+
const defaultFrequency = 1.0;
23+
const defaultOctaves = 1;
24+
const defaultPersistence = 0.5;
3225

3326
export function makeCuboid(
3427
width: number,
3528
height: number,
3629
depth: number,
3730
noise3: Noise3Fn,
38-
options: Partial<Options> = {},
31+
{
32+
amplitude = defaultAmplitude,
33+
frequency = defaultFrequency,
34+
octaves = defaultOctaves,
35+
persistence = defaultPersistence,
36+
scale,
37+
}: Partial<Options> = {},
3938
): number[][] {
40-
const { amplitude, frequency, octaves, persistence } = processOptions(
41-
options,
42-
);
4339
const field = new Array(width);
4440
for (let x = 0; x < width; x++) {
4541
field[x] = new Array(height);
@@ -53,6 +49,7 @@ export function makeCuboid(
5349
(amplitude * Math.pow(persistence, octave));
5450
}
5551
field[x][y][z] = value / (2 - 1 / Math.pow(2, octaves - 1));
52+
if (scale) field[x][y][z] = scale(field[x][y][z]);
5653
}
5754
}
5855
}
@@ -63,11 +60,14 @@ export function makeCylinderSurface(
6360
circumference: number,
6461
height: number,
6562
noise3: Noise3Fn,
66-
options: Partial<Options> = {},
63+
{
64+
amplitude = defaultAmplitude,
65+
frequency = defaultFrequency,
66+
octaves = defaultOctaves,
67+
persistence = defaultPersistence,
68+
scale,
69+
}: Partial<Options> = {},
6770
): number[] {
68-
const { amplitude, frequency, octaves, persistence } = processOptions(
69-
options,
70-
);
7171
const radius = circumference / TWO_PI;
7272
const field = new Array(circumference);
7373
for (let x = 0; x < circumference; x++) {
@@ -83,6 +83,7 @@ export function makeCylinderSurface(
8383
(amplitude * Math.pow(persistence, octave));
8484
}
8585
field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1));
86+
if (scale) field[x][y] = scale(field[x][y]);
8687
}
8788
}
8889
return field;
@@ -91,11 +92,14 @@ export function makeCylinderSurface(
9192
export function makeLine(
9293
length: number,
9394
noise1: Noise1Fn,
94-
options: Partial<Options> = {},
95+
{
96+
amplitude = defaultAmplitude,
97+
frequency = defaultFrequency,
98+
octaves = defaultOctaves,
99+
persistence = defaultPersistence,
100+
scale,
101+
}: Partial<Options> = {},
95102
): number[] {
96-
const { amplitude, frequency, octaves, persistence } = processOptions(
97-
options,
98-
);
99103
const field = new Array(length);
100104
for (let x = 0; x < length; x++) {
101105
let value = 0.0;
@@ -104,6 +108,7 @@ export function makeLine(
104108
value += noise1(x * freq) * (amplitude * Math.pow(persistence, octave));
105109
}
106110
field[x] = value / (2 - 1 / Math.pow(2, octaves - 1));
111+
if (scale) field[x] = scale(field[x]);
107112
}
108113
return field;
109114
}
@@ -112,11 +117,14 @@ export function makeRectangle(
112117
width: number,
113118
height: number,
114119
noise2: Noise2Fn,
115-
options: Partial<Options> = {},
120+
{
121+
amplitude = defaultAmplitude,
122+
frequency = defaultFrequency,
123+
octaves = defaultOctaves,
124+
persistence = defaultPersistence,
125+
scale,
126+
}: Partial<Options> = {},
116127
): number[] {
117-
const { amplitude, frequency, octaves, persistence } = processOptions(
118-
options,
119-
);
120128
const field = new Array(width);
121129
for (let x = 0; x < width; x++) {
122130
field[x] = new Array(height);
@@ -128,6 +136,7 @@ export function makeRectangle(
128136
(amplitude * Math.pow(persistence, octave));
129137
}
130138
field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1));
139+
if (scale) field[x][y] = scale(field[x][y]);
131140
}
132141
}
133142
return field;
@@ -136,14 +145,17 @@ export function makeRectangle(
136145
export function makeSphereSurface(
137146
circumference: number,
138147
noise3: Noise3Fn,
139-
options: Partial<Options> = {},
148+
{
149+
amplitude = defaultAmplitude,
150+
frequency = defaultFrequency,
151+
octaves = defaultOctaves,
152+
persistence = defaultPersistence,
153+
scale,
154+
}: Partial<Options> = {},
140155
): number[] {
141-
const { amplitude, frequency, octaves, persistence } = processOptions(
142-
options,
143-
);
144156
const field = new Array(circumference);
145157
for (let x = 0; x < circumference; x++) {
146-
const circumferenceSemi = circumference / 2
158+
const circumferenceSemi = circumference / 2;
147159
field[x] = new Array(circumferenceSemi);
148160
for (let y = 0; y < circumferenceSemi; y++) {
149161
const [nx, ny] = [x / circumference, y / circumferenceSemi];
@@ -159,6 +171,7 @@ export function makeSphereSurface(
159171
(amplitude * Math.pow(persistence, octave));
160172
}
161173
field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1));
174+
if (scale) field[x][y] = scale(field[x][y]);
162175
}
163176
}
164177
return field;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fractal-noise",
3-
"version": "2.0.0",
3+
"version": "2.1.0-pre1",
44
"description": "Fractal noise library",
55
"keywords": [
66
"noise",

src/mod.ts

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,27 @@ export interface Options {
1515
frequency: number;
1616
octaves: number;
1717
persistence: number;
18+
scale?: (x: number) => number;
1819
}
1920

20-
function processOptions(options: Partial<Options>): Options {
21-
return {
22-
amplitude: typeof options.amplitude === "number" ? options.amplitude : 1.0,
23-
frequency: typeof options.frequency === "number" ? options.frequency : 1.0,
24-
octaves: typeof options.octaves === "number"
25-
? Math.floor(options.octaves)
26-
: 1,
27-
persistence: typeof options.persistence === "number"
28-
? options.persistence
29-
: 0.5,
30-
};
31-
}
21+
const defaultAmplitude = 1.0;
22+
const defaultFrequency = 1.0;
23+
const defaultOctaves = 1;
24+
const defaultPersistence = 0.5;
3225

3326
export function makeCuboid(
3427
width: number,
3528
height: number,
3629
depth: number,
3730
noise3: Noise3Fn,
38-
options: Partial<Options> = {},
31+
{
32+
amplitude = defaultAmplitude,
33+
frequency = defaultFrequency,
34+
octaves = defaultOctaves,
35+
persistence = defaultPersistence,
36+
scale,
37+
}: Partial<Options> = {},
3938
): number[][] {
40-
const { amplitude, frequency, octaves, persistence } = processOptions(
41-
options,
42-
);
4339
const field = new Array(width);
4440
for (let x = 0; x < width; x++) {
4541
field[x] = new Array(height);
@@ -53,6 +49,7 @@ export function makeCuboid(
5349
(amplitude * Math.pow(persistence, octave));
5450
}
5551
field[x][y][z] = value / (2 - 1 / Math.pow(2, octaves - 1));
52+
if (scale) field[x][y][z] = scale(field[x][y][z]);
5653
}
5754
}
5855
}
@@ -63,11 +60,14 @@ export function makeCylinderSurface(
6360
circumference: number,
6461
height: number,
6562
noise3: Noise3Fn,
66-
options: Partial<Options> = {},
63+
{
64+
amplitude = defaultAmplitude,
65+
frequency = defaultFrequency,
66+
octaves = defaultOctaves,
67+
persistence = defaultPersistence,
68+
scale,
69+
}: Partial<Options> = {},
6770
): number[] {
68-
const { amplitude, frequency, octaves, persistence } = processOptions(
69-
options,
70-
);
7171
const radius = circumference / TWO_PI;
7272
const field = new Array(circumference);
7373
for (let x = 0; x < circumference; x++) {
@@ -83,6 +83,7 @@ export function makeCylinderSurface(
8383
(amplitude * Math.pow(persistence, octave));
8484
}
8585
field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1));
86+
if (scale) field[x][y] = scale(field[x][y]);
8687
}
8788
}
8889
return field;
@@ -91,11 +92,14 @@ export function makeCylinderSurface(
9192
export function makeLine(
9293
length: number,
9394
noise1: Noise1Fn,
94-
options: Partial<Options> = {},
95+
{
96+
amplitude = defaultAmplitude,
97+
frequency = defaultFrequency,
98+
octaves = defaultOctaves,
99+
persistence = defaultPersistence,
100+
scale,
101+
}: Partial<Options> = {},
95102
): number[] {
96-
const { amplitude, frequency, octaves, persistence } = processOptions(
97-
options,
98-
);
99103
const field = new Array(length);
100104
for (let x = 0; x < length; x++) {
101105
let value = 0.0;
@@ -104,6 +108,7 @@ export function makeLine(
104108
value += noise1(x * freq) * (amplitude * Math.pow(persistence, octave));
105109
}
106110
field[x] = value / (2 - 1 / Math.pow(2, octaves - 1));
111+
if (scale) field[x] = scale(field[x]);
107112
}
108113
return field;
109114
}
@@ -112,11 +117,14 @@ export function makeRectangle(
112117
width: number,
113118
height: number,
114119
noise2: Noise2Fn,
115-
options: Partial<Options> = {},
120+
{
121+
amplitude = defaultAmplitude,
122+
frequency = defaultFrequency,
123+
octaves = defaultOctaves,
124+
persistence = defaultPersistence,
125+
scale,
126+
}: Partial<Options> = {},
116127
): number[] {
117-
const { amplitude, frequency, octaves, persistence } = processOptions(
118-
options,
119-
);
120128
const field = new Array(width);
121129
for (let x = 0; x < width; x++) {
122130
field[x] = new Array(height);
@@ -128,6 +136,7 @@ export function makeRectangle(
128136
(amplitude * Math.pow(persistence, octave));
129137
}
130138
field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1));
139+
if (scale) field[x][y] = scale(field[x][y]);
131140
}
132141
}
133142
return field;
@@ -136,14 +145,17 @@ export function makeRectangle(
136145
export function makeSphereSurface(
137146
circumference: number,
138147
noise3: Noise3Fn,
139-
options: Partial<Options> = {},
148+
{
149+
amplitude = defaultAmplitude,
150+
frequency = defaultFrequency,
151+
octaves = defaultOctaves,
152+
persistence = defaultPersistence,
153+
scale,
154+
}: Partial<Options> = {},
140155
): number[] {
141-
const { amplitude, frequency, octaves, persistence } = processOptions(
142-
options,
143-
);
144156
const field = new Array(circumference);
145157
for (let x = 0; x < circumference; x++) {
146-
const circumferenceSemi = circumference / 2
158+
const circumferenceSemi = circumference / 2;
147159
field[x] = new Array(circumferenceSemi);
148160
for (let y = 0; y < circumferenceSemi; y++) {
149161
const [nx, ny] = [x / circumference, y / circumferenceSemi];
@@ -159,6 +171,7 @@ export function makeSphereSurface(
159171
(amplitude * Math.pow(persistence, octave));
160172
}
161173
field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1));
174+
if (scale) field[x][y] = scale(field[x][y]);
162175
}
163176
}
164177
return field;

0 commit comments

Comments
 (0)