From 6c6dcf9f9d94bf34546f81594f9c212c74612314 Mon Sep 17 00:00:00 2001 From: Luke Winship Date: Fri, 7 Jun 2024 19:49:35 +0100 Subject: [PATCH] revert the more liberal options input in top-level functions AND use getSphereSurfaceNoiseValue --- src/mod.ts | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/mod.ts b/src/mod.ts index 31afc0e..3989f04 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -28,8 +28,15 @@ export function makeCuboid( height: number, depth: number, noise3: Noise3Fn, - options: Options, + { + amplitude = defaultAmplitude, + frequency = defaultFrequency, + octaves = defaultOctaves, + persistence = defaultPersistence, + scale, + }: Partial = {}, ): number[][][] { + const options: Options = { amplitude, frequency, octaves, persistence, scale }; const field: number[][][] = new Array(width); for (let x = 0; x < width; x++) { field[x] = new Array(height); @@ -47,8 +54,15 @@ export function makeCylinderSurface( circumference: number, height: number, noise3: Noise3Fn, - options: Options, + { + amplitude = defaultAmplitude, + frequency = defaultFrequency, + octaves = defaultOctaves, + persistence = defaultPersistence, + scale, + }: Partial = {}, ): number[][] { + const options: Options = { amplitude, frequency, octaves, persistence, scale }; const radius = getCircleRadius(circumference); const field: number[][] = new Array(circumference); for (let x = 0; x < circumference; x++) { @@ -63,8 +77,15 @@ export function makeCylinderSurface( export function makeLine( length: number, noise1: Noise1Fn, - options: Options, + { + amplitude = defaultAmplitude, + frequency = defaultFrequency, + octaves = defaultOctaves, + persistence = defaultPersistence, + scale, + }: Partial = {}, ): number[] { + const options: Options = { amplitude, frequency, octaves, persistence, scale }; const field: number[] = new Array(length); for (let x = 0; x < length; x++) { field[x] = getLineNoiseValue(noise1, options, x); @@ -76,8 +97,15 @@ export function makeRectangle( width: number, height: number, noise2: Noise2Fn, - options: Options, + { + amplitude = defaultAmplitude, + frequency = defaultFrequency, + octaves = defaultOctaves, + persistence = defaultPersistence, + scale, + }: Partial = {}, ): number[][] { + const options: Options = { amplitude, frequency, octaves, persistence, scale }; const field: number[][] = new Array(width); for (let x = 0; x < width; x++) { field[x] = new Array(height); @@ -99,25 +127,13 @@ export function makeSphereSurface( scale, }: Partial = {}, ): number[][] { + const options: Options = { amplitude, frequency, octaves, persistence, scale }; + const circumferenceSemi = circumference / 2; const field: number[][] = new Array(circumference); for (let x = 0; x < circumference; x++) { - const circumferenceSemi = circumference / 2; field[x] = new Array(circumferenceSemi); for (let y = 0; y < circumferenceSemi; y++) { - const [nx, ny] = [x / circumference, y / circumferenceSemi]; - const [rdx, rdy] = [nx * TWO_PI, ny * Math.PI]; - const sinY = Math.sin(rdy + Math.PI); - const a = TWO_PI * Math.sin(rdx) * sinY; - const b = TWO_PI * Math.cos(rdx) * sinY; - const d = TWO_PI * Math.cos(rdy); - let value = 0.0; - for (let octave = 0; octave < octaves; octave++) { - const freq = frequency * Math.pow(2, octave); - value += noise3(a * freq, b * freq, d * freq) * - (amplitude * Math.pow(persistence, octave)); - } - field[x][y] = value / (2 - 1 / Math.pow(2, octaves - 1)); - if (scale) field[x][y] = scale(field[x][y]); + field[x][y] = getSphereSurfaceNoiseValue(noise3, options, circumference, circumferenceSemi, x, y); } } return field;