Skip to content

Commit

Permalink
rename noiseFunctionND to createNoiseND, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jwagner committed Jul 23, 2022
1 parent d481426 commit 6a374ac
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 106 deletions.
69 changes: 62 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,35 @@ Created something awesome with simplex-noise? Let me know so I can add it to the

```javascript
// import the noise functions you need
import { noiseFunction2D } from 'simplex-noise';
import { createNoise2D } from 'simplex-noise';
```

### CommonJS Require

```javascript
// import the noise functions you need
const { noiseFunction2D } = require('simplex-noise');
const { createNoise2D } = require('simplex-noise');
```

### 2D

```javascript
// initialize the noise function
const noise2D = noiseFunction2D();
const noise2D = createNoise2D();
console.log(noise2D(x, y));
```

### 3D

```javascript
const noise3D = noiseFunction3D();
const noise3D = createNoise3D();
console.log(noise3D(x, y, z));
```

### 4D

```javascript
const noise4D = noiseFunction4D();
const noise4D = createNoise4D();
console.log(noise4D(x, y, z, w));
```

Expand All @@ -65,10 +65,15 @@ console.log(noise4D(x, y, z, w));
By default simplex-noise.js will use Math.random() to seed the noise.
You can pass in a PRNG function to use your own seed value.

```bash
# install the alea prng
npm install -S alea
```

```javascript
import Alea from 'alea';
// create a new random function based on the seed
const alea = new Alea('seed');
const alea = Alea('seed');
// use the seeded random function to initialize the noise function
const noise2D = noiseFunction2D(alea);
console.log(noise2D(x, y));
Expand Down Expand Up @@ -107,8 +112,58 @@ There are some simple unit tests for this library to run them
npm install && npm test
```

## Migrating from 3.x to 4.x

### random initialization
```javascript
// 3.x
import SimplexNoise from 'simplex-noise';
const simplex = new SimplexNoise();
const value2d = simplex.noise2D(x, y);
// 4.x
// import the functions you need
import { createNoise2D } from 'simplex-noise';
const noise2D = createNoise2D();
const value2d = noise2D(x, y);
```

### Initialization with a seed
```javascript
// 3.x
import SimplexNoise from 'simplex-noise';
const simplex = new SimplexNoise('seed');
const value2d = simplex.noise2D(x, y);
// 4.x
// npm install -S alea
import { createNoise2D } from 'simplex-noise';
import Alea from 'alea';
const noise2D = createNoise2D(Alea('seed'));
const value2d = noise2D(x, y);

// IMPORTANT: If you use multiple noise functions (for example 2d and 3d)
// and want compatible output with 3.x you will need to pass a fresh instance
// of alea to each create call. If you reuse the alea instance you will
// get different outputs compared to simplex-noise 3.x.
const seed = 'seed';
const noise2D = createNoise2D(Alea(seed));
const noise3D = createNoise3D(Alea(seed));
```

### Emulating the 3.x and older API
```javascript
const simplex = {
noise2D: createNoise2D(Alea(seed)),
noise3D: createNoise3D(Alea(seed)),
noise4D: createNoise4D(Alea(seed)),
};
```

## Changelog

### 4.0.0
- Reworked the API so you can import the noise functions individually.
When combined with tree-shaking this helps with build sizes.

### 3.0.1
- Include simplex-noise.ts as source file, fixes sourcemap warnings.

Expand Down Expand Up @@ -159,7 +214,7 @@ you will need to use a polyfill like [typedarray.js](http://www.calormen.com/pol


## License
Copyright (c) 2021 Jonas Wagner, licensed under the MIT License (enclosed)
Copyright (c) 2022 Jonas Wagner, licensed under the MIT License (enclosed)

## Credits
This is mostly a direct javascript port of the [Java implementation](http://webstaff.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java)
Expand Down
19 changes: 0 additions & 19 deletions alea.md

This file was deleted.

6 changes: 3 additions & 3 deletions simplex-noise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type NoiseFunction2D = (x: number, y: number) => number;
* @param random the random function that will be used to build the permutation table
* @returns {NoiseFunction2D}
*/
export function noiseFunction2D(random: RandomFn = Math.random): NoiseFunction2D {
export function createNoise2D(random: RandomFn = Math.random): NoiseFunction2D {
const perm = buildPermutationTable(random);
const permMod12 = perm.map(v => v % 12);
return function noise2D(x: number, y: number): number {
Expand Down Expand Up @@ -153,7 +153,7 @@ export type NoiseFunction3D = (x: number, y: number, z: number) => number;
* @param random the random function that will be used to build the permutation table
* @returns {NoiseFunction3D}
*/
export function noiseFunction3D(random: RandomFn = Math.random): NoiseFunction3D {
export function createNoise3D(random: RandomFn = Math.random): NoiseFunction3D {
const perm = buildPermutationTable(random);
const permMod12 = perm.map(v => v % 12);

Expand Down Expand Up @@ -294,7 +294,7 @@ export type NoiseFunction4D = (x: number, y: number, z: number, w: number) => nu
* @param random the random function that will be used to build the permutation table
* @returns {NoiseFunction3D}
*/
export function noiseFunction4D(random: RandomFn = Math.random) {
export function createNoise4D(random: RandomFn = Math.random) {
const perm = buildPermutationTable(random);
return function noise4D(x: number, y: number, z: number, w: number): number {
let n0, n1, n2, n3, n4; // Noise contributions from the five corners
Expand Down
6 changes: 3 additions & 3 deletions test/module-compatibility/commonjs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const noiseFunction2D = require('simplex-noise').noiseFunction2D;
/* eslint-disable @typescript-eslint/no-var-requires */
const createNoise2D = require('simplex-noise').createNoise2D;
console.log(require('simplex-noise'));
console.log(noiseFunction2D()(1, 2));
console.log(createNoise2D()(1, 2));
4 changes: 2 additions & 2 deletions test/module-compatibility/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import {noiseFunction2D} from 'simplex-noise';
console.log(noiseFunction2D()(1, 2));
import {createNoise2D} from 'simplex-noise';
console.log(createNoise2D()(1, 2));
26 changes: 13 additions & 13 deletions test/simplex-noise-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noiseFunction2D, noiseFunction3D, noiseFunction4D } from '../simplex-noise';
import { createNoise2D, createNoise3D, createNoise4D } from '../simplex-noise';
import { buildPermutationTable } from '../simplex-noise';
import alea from 'alea';
import { assert } from 'chai';
Expand Down Expand Up @@ -38,8 +38,8 @@ describe('buildPermutationTable', function () {
});
});

describe('noiseFunction2D', () => {
const noise2D = noiseFunction2D(getRandom());
describe('createNoise2D', () => {
const noise2D = createNoise2D(getRandom());
describe('noise2D', () => {
it('should return the same value for the same input', function () {
assert.equal(noise2D(0.1, 0.2), noise2D(0.1, 0.2));
Expand All @@ -48,11 +48,11 @@ describe('noiseFunction2D', () => {
assert.notEqual(noise2D(0.1, 0.2), noise2D(0.101, 0.202));
});
it('should return the same output with the same seed', function () {
const noise2D2 = noiseFunction2D(getRandom());
const noise2D2 = createNoise2D(getRandom());
assert.equal(noise2D(0.1, 0.2), noise2D2(0.1, 0.2));
});
it('should return a different output with a different seed', function () {
const noise2D2 = noiseFunction2D(getRandom('other seed'));
const noise2D2 = createNoise2D(getRandom('other seed'));
assert.notEqual(noise2D(0.1, 0.2), noise2D2(0.1, 0.2));
});
it('should return values between -1 and 1', function () {
Expand All @@ -73,8 +73,8 @@ describe('noiseFunction2D', () => {
});
});

describe('noiseFunction3D', () => {
const noise3D = noiseFunction3D(getRandom());
describe('createNoise3D', () => {
const noise3D = createNoise3D(getRandom());
describe('noise3D', () => {
it('should return the same value for the same input', function () {
assert.equal(noise3D(0.1, 0.2, 0.3), noise3D(0.1, 0.2, 0.3));
Expand All @@ -83,11 +83,11 @@ describe('noiseFunction3D', () => {
assert.notEqual(noise3D(0.1, 0.2, 0.3), noise3D(0.101, 0.202, 0.303));
});
it('should return the same output with the same seed', function () {
const noise3D2 = noiseFunction3D(getRandom());
const noise3D2 = createNoise3D(getRandom());
assert.equal(noise3D(0.1, 0.2, 0.3), noise3D2(0.1, 0.2, 0.3));
});
it('should return a different output with a different seed', function () {
const noise3D2 = noiseFunction3D(getRandom('other seed'));
const noise3D2 = createNoise3D(getRandom('other seed'));
assert.notEqual(noise3D(0.1, 0.2, 0.3), noise3D2(0.1, 0.2, 0.3));
});
it('should return values between -1 and 1', function () {
Expand All @@ -108,8 +108,8 @@ describe('noiseFunction3D', () => {
});
});

describe('noiseFunction4D', () => {
const noise4D = noiseFunction4D(getRandom());
describe('createNoise4D', () => {
const noise4D = createNoise4D(getRandom());
describe('noise4D', () => {
it('should return the same value for the same input', function () {
assert.equal(noise4D(0.1, 0.2, 0.3, 0.4), noise4D(0.1, 0.2, 0.3, 0.4));
Expand All @@ -118,11 +118,11 @@ describe('noiseFunction4D', () => {
assert.notEqual(noise4D(0.1, 0.2, 0.3, 0.4), noise4D(0.101, 0.202, 0.303, 0.404));
});
it('should return the same output with the same seed', function () {
const noise4D2 = noiseFunction4D(getRandom());
const noise4D2 = createNoise4D(getRandom());
assert.equal(noise4D(0.1, 0.2, 0.3, 0.4), noise4D2(0.1, 0.2, 0.3, 0.4));
});
it('should return a different output with a different seed', function () {
const noise4D2 = noiseFunction4D(getRandom('other seed'));
const noise4D2 = createNoise4D(getRandom('other seed'));
assert.notEqual(noise4D(0.1, 0.2, 0.3, 0.4), noise4D2(0.1, 0.2, 0.3, 0.4));
});
it('should return values between -1 and 1', function () {
Expand Down
Loading

0 comments on commit 6a374ac

Please sign in to comment.