Skip to content

Commit 6c6dcf9

Browse files
committed
revert the more liberal options input in top-level functions AND use getSphereSurfaceNoiseValue
1 parent 7ed2d65 commit 6c6dcf9

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/mod.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ export function makeCuboid(
2828
height: number,
2929
depth: number,
3030
noise3: Noise3Fn,
31-
options: Options,
31+
{
32+
amplitude = defaultAmplitude,
33+
frequency = defaultFrequency,
34+
octaves = defaultOctaves,
35+
persistence = defaultPersistence,
36+
scale,
37+
}: Partial<Options> = {},
3238
): number[][][] {
39+
const options: Options = { amplitude, frequency, octaves, persistence, scale };
3340
const field: number[][][] = new Array(width);
3441
for (let x = 0; x < width; x++) {
3542
field[x] = new Array(height);
@@ -47,8 +54,15 @@ export function makeCylinderSurface(
4754
circumference: number,
4855
height: number,
4956
noise3: Noise3Fn,
50-
options: Options,
57+
{
58+
amplitude = defaultAmplitude,
59+
frequency = defaultFrequency,
60+
octaves = defaultOctaves,
61+
persistence = defaultPersistence,
62+
scale,
63+
}: Partial<Options> = {},
5164
): number[][] {
65+
const options: Options = { amplitude, frequency, octaves, persistence, scale };
5266
const radius = getCircleRadius(circumference);
5367
const field: number[][] = new Array(circumference);
5468
for (let x = 0; x < circumference; x++) {
@@ -63,8 +77,15 @@ export function makeCylinderSurface(
6377
export function makeLine(
6478
length: number,
6579
noise1: Noise1Fn,
66-
options: Options,
80+
{
81+
amplitude = defaultAmplitude,
82+
frequency = defaultFrequency,
83+
octaves = defaultOctaves,
84+
persistence = defaultPersistence,
85+
scale,
86+
}: Partial<Options> = {},
6787
): number[] {
88+
const options: Options = { amplitude, frequency, octaves, persistence, scale };
6889
const field: number[] = new Array(length);
6990
for (let x = 0; x < length; x++) {
7091
field[x] = getLineNoiseValue(noise1, options, x);
@@ -76,8 +97,15 @@ export function makeRectangle(
7697
width: number,
7798
height: number,
7899
noise2: Noise2Fn,
79-
options: Options,
100+
{
101+
amplitude = defaultAmplitude,
102+
frequency = defaultFrequency,
103+
octaves = defaultOctaves,
104+
persistence = defaultPersistence,
105+
scale,
106+
}: Partial<Options> = {},
80107
): number[][] {
108+
const options: Options = { amplitude, frequency, octaves, persistence, scale };
81109
const field: number[][] = new Array(width);
82110
for (let x = 0; x < width; x++) {
83111
field[x] = new Array(height);
@@ -99,25 +127,13 @@ export function makeSphereSurface(
99127
scale,
100128
}: Partial<Options> = {},
101129
): number[][] {
130+
const options: Options = { amplitude, frequency, octaves, persistence, scale };
131+
const circumferenceSemi = circumference / 2;
102132
const field: number[][] = new Array(circumference);
103133
for (let x = 0; x < circumference; x++) {
104-
const circumferenceSemi = circumference / 2;
105134
field[x] = new Array(circumferenceSemi);
106135
for (let y = 0; y < circumferenceSemi; y++) {
107-
const [nx, ny] = [x / circumference, y / circumferenceSemi];
108-
const [rdx, rdy] = [nx * TWO_PI, ny * Math.PI];
109-
const sinY = Math.sin(rdy + Math.PI);
110-
const a = TWO_PI * Math.sin(rdx) * sinY;
111-
const b = TWO_PI * Math.cos(rdx) * sinY;
112-
const d = TWO_PI * Math.cos(rdy);
113-
let value = 0.0;
114-
for (let octave = 0; octave < octaves; octave++) {
115-
const freq = frequency * Math.pow(2, octave);
116-
value += noise3(a * freq, b * freq, d * freq) *
117-
(amplitude * Math.pow(persistence, octave));
118-
}
119-
field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1));
120-
if (scale) field[x][y] = scale(field[x][y]);
136+
field[x][y] = getSphereSurfaceNoiseValue(noise3, options, circumference, circumferenceSemi, x, y);
121137
}
122138
}
123139
return field;

0 commit comments

Comments
 (0)