···2233advanced noise generation for the browser and node.js
4455+- written in typescript
66+- seeded noise
77+- based on simplex noise (2D, 3D and 4D)
88+- fractal brownian motion (fbm)
99+- custom gain, lacunarity, scale, shift, power
1010+- billowed, ridged, warped, stepped noise
1111+- seamless tiling noise (1D and 2D)
1212+- most options can be set to own noise instance
1313+514## Install
615716```bash
···2130```javascript
2231const noise = new UberNoise();
23322424-// get noise value at x,y
2525-const value = noise.get(x, y);
2626-2727-// get noise value at x,y,z
3333+// get noise value at x, y, z
2834const value = noise.get(x, y, z);
2929-3030-// get noise value at x,y,z,w
3131-const value = noise.get(x, y, z, w);
3535+// OR
3636+const value = noise.get({x, y, z});
3237```
33383439### Set noise options
···5156 scale: { min: 0.01, max: 0.1, scale: 0.01 },
5257});
5358```
5959+6060+### All options
6161+6262+| Option | Type | Default Value | Description |
6363+|---------------|----------------------------------|-------------------|----------------------------------------------------|
6464+| `seed` | `string \| number` | `Math.random()` | Seed for the noise, defaults to a random value. |
6565+| `min` | `number \| UberNoise \| NoiseOptions` | `-1` | Minimum value of noise. |
6666+| `max` | `number \| UberNoise \| NoiseOptions` | `1` | Maximum value of noise. |
6767+| `scale` | `number \| UberNoise \| NoiseOptions` | `1` | Scale of the noise ("zoom" level). |
6868+| `power` | `number \| UberNoise \| NoiseOptions` | `1` | Power of the noise (1 = linear, 2 = quadratic). |
6969+| `shift` | `number[]` | `[0, 0, 0, 0]` | Move noise in 2D, 3D, or 4D space. |
7070+| `octaves` | `number` | `0` | Number of layers for fbm noise. |
7171+| `gain` | `number \| UberNoise \| NoiseOptions` | `0.5` | Amplitude multiplier per fbm layer. |
7272+| `lacunarity` | `number \| UberNoise \| NoiseOptions` | `2` | Scale multiplier per fbm layer. |
7373+| `amps` | `number[]` | `[]` | Array of custom amplitudes for each fbm layer. |
7474+| `layers` | `NoiseOptions[]` | `[]` | Custom noise options for each fbm layer. |
7575+| `sharpness` | `number \| UberNoise \| NoiseOptions` | `0` | Sharpness of noise (0 = normal, 1 = billowed, -1 = ridged). |
7676+| `steps` | `number \| UberNoise \| NoiseOptions` | `0` | Discretizes the noise into steps. |
7777+| `warp` | `number \| UberNoise \| NoiseOptions` | `0` | Amount to warp the noise. |
7878+| `warpNoise` | `UberNoise` | `undefined` | Custom noise used to warp the noise. |
7979+| `warp2` | `number \| UberNoise \| NoiseOptions` | `0` | Second level of warping, only used if `warp` is used too. |
8080+| `warpNoise2` | `UberNoise` | `undefined` | Second custom noise for additional warping. |
8181+| `invert` | `boolean` | `false` | Invert the noise output. |
8282+| `abs` | `boolean` | `false` | Take the absolute value of the noise. |
8383+| `clamp` | `boolean` | `false` | Clamp the noise between min and max values (otherwise noise might overflow slightly). |
8484+| `tileX` | `boolean` | `false` | Tile the noise in the x direction. |
8585+| `tileY` | `boolean` | `false` | Tile the noise in the y direction. |
8686+| `tile` | `boolean` | `false` | Tile the noise in x and y directions (overwrites `tileX` and `tileY`) |
8787+
···991010import alea from 'alea';
11111212-/**
1313- *
1414- * @property {number} seed - seed for the noise, if not provided, Math.random() will be used,
1515- * currently same results can only be guaranteed for newly created noise objects with same seed
1616- * (as opposed to an old noise object where you changed the seed)
1717- *
1818- * @property {number} min - minimun value of noise
1919- * @property {number} max - maximum value of noise
2020- *
2121- * @property {number} scale - scale of the noise
2222- * @property {number} power - power of the noise (1 = linear, 2 = quadratic, etc)
2323- * @property {Vector} shift - move noise in 2D, 3D or 4D space
2424- *
2525- * @property {number} octaves - number of layers for fbm noise
2626- * @property {number} gain - how much to multiply amplitude per layer
2727- * @property {number} lacunarity - how much to multiply scale per layer
2828- * @property {array} amps - array of amplitudes for each layer
2929- * @property {number} erosion - how much previous layers influence amplitude of later layers
3030- * @property {number} sharpness - billowed or rigded noise (0 = normal, 1 = billowed, -1 = ridged)
3131- * @property {number} steps - will turn noise into steps (integer, number of steps)
3232- *
3333- * @property {number} warp - how much to warp the noise
3434- * @property {number} warpNoise - noise to warp the noise with
3535- * @property {number} warp2 - second warp, can only be used if warp is used too
3636- * @property {number} warpNoise2 - second warp noise
3737- *
3838- * @property {boolean} invert - invert the noise
3939- * @property {boolean} abs - absolute value of the noise
4040- * @property {boolean} clamp - clamp the noise between min and max
4141- * @property {boolean} tileX - tile the noise in x direction
4242- * @property {boolean} tileY - tile the noise in y direction
4343- * @property {boolean} tile - tile the noise in all directions (will override tileX and tileY)
4444- *
4545- * @constructor
4646- */
4747-4812function lerp(a: number, b: number, t: number): number {
4913 return (b - a) * t + a;
5014}
···5620 w?: number;
5721};
58225959-type NoiseParameterInput = number | UberNoise | NoiseOptions;
6023type NoiseParameter = number | UberNoise;
61246225type NoiseOptions = {
6326 /**
6427 * seed for the noise, if not provided, Math.random() will be used,
2828+ *
2929+ * @default Math.random()
6530 */
6631 seed?: string | number;
67326833 /**
6934 * minimun value of noise
3535+ *
3636+ * @default -1
7037 */
7171- min?: NoiseParameterInput;
3838+ min?: number | UberNoise | NoiseOptions;
72397340 /**
7441 * maximum value of noise
4242+ *
4343+ * @default 1
7544 */
7676- max?: NoiseParameterInput;
4545+ max?: number | UberNoise | NoiseOptions;
77467847 /**
7948 * scale of the noise ("zoom" in or out)
4949+ *
5050+ * @default 1
8051 */
8181- scale?: NoiseParameterInput;
5252+ scale?: number | UberNoise | NoiseOptions;
82538354 /**
8455 * power of the noise (1 = linear, 2 = quadratic, etc)
5656+ *
5757+ * @default 1
8558 */
8686- power?: NoiseParameterInput;
5959+ power?: number | UberNoise | NoiseOptions;
87608861 /**
8962 * move noise in 2D, 3D or 4D space
6363+ *
6464+ * @default [0, 0, 0, 0]
9065 */
9166 shift?: number[];
92679368 /**
9469 * number of layers for fbm noise
7070+ *
7171+ * @default 0
9572 */
9673 octaves?: number;
7474+9775 /**
9876 * how much to multiply amplitude per fbm layer
7777+ *
7878+ * @default 0.5
9979 */
100100- gain?: NoiseParameterInput;
8080+ gain?: number | UberNoise | NoiseOptions;
8181+10182 /**
10283 * how much to multiply scale per fbm layer
8484+ *
8585+ * @default 2
10386 */
104104- lacunarity?: NoiseParameterInput;
8787+ lacunarity?: number | UberNoise | NoiseOptions;
1058810689 /**
10790 * array of amplitudes for each fbm layer
9191+ *
9292+ * @default []
10893 */
10994 amps?: number[];
1109511196 /**
11297 * custom noise options for each fbm layer
9898+ *
9999+ * @default []
113100 */
114101 layers?: NoiseOptions[];
115102116103 /**
117104 * billowed or rigded noise (0 = normal, 1 = billowed, -1 = ridged)
105105+ *
106106+ * @default 0
118107 */
119119- sharpness?: NoiseParameterInput;
108108+ sharpness?: number | UberNoise | NoiseOptions;
109109+120110 /**
121111 * will turn noise into discrete steps (integer, number of steps)
112112+ *
113113+ * @default 0
122114 */
123123- steps?: NoiseParameterInput;
115115+ steps?: number | UberNoise | NoiseOptions;
124116125117 /**
126118 * how much to warp the noise
119119+ *
120120+ * @default 0
127121 */
128128- warp?: NoiseParameterInput;
122122+ warp?: number | UberNoise | NoiseOptions;
123123+129124 /**
130125 * custom noise to warp the noise with
126126+ *
127127+ * @default undefined
131128 */
132129 warpNoise?: UberNoise;
130130+133131 /**
134134- * second warp, can only be used if warp is used
135135- * too
132132+ * second warp, can only be used if warp is used too
133133+ *
134134+ * @default 0
136135 */
137137- warp2?: NoiseParameterInput;
136136+ warp2?: number | UberNoise | NoiseOptions;
138137 /**
139138 * second warp noise
139139+ *
140140+ * @default undefined
140141 */
141142 warpNoise2?: UberNoise;
142143143144 /**
144145 * should the noise be inverted
146146+ *
147147+ * @default false
145148 */
146149 invert?: boolean;
150150+147151 /**
148152 * should we take the absolute value of the noise
153153+ *
154154+ * @default false
149155 */
150156 abs?: boolean;
157157+151158 /**
152159 * should we clamp the noise between min and max
160160+ *
161161+ * @default false
153162 */
154163 clamp?: boolean;
155164156165 /**
157166 * tile the noise in x direction
167167+ *
168168+ * @default false
158169 */
159170 tileX?: boolean;
160171 /**
161172 * tile the noise in y direction
173173+ *
174174+ * @default false
162175 */
163176 tileY?: boolean;
164177 /**
165178 * tile the noise in all directions (will override tileX and tileY)
179179+ *
180180+ * @default false
166181 */
167182 tile?: boolean;
168183};
···190205191206 private amps: number[] = [];
192207193193- private _erosion: NoiseParameter = 0;
194208 private _sharpness: NoiseParameter = 0;
195209 private _steps: NoiseParameter = 0;
196210···453467 return norm;
454468 }
455469456456- // same as normalized but returns between min and max
470470+ /**
471471+ * get noise value at position x, y, z, w
472472+ *
473473+ * all transformations will be applied to the noise (shift, warp, power, sharpness, steps, min, max, fmb, etc)
474474+ *
475475+ * either pass in
476476+ * - x, y, z, w as separate arguments
477477+ * - as first argument an object {x, y, z, w}
478478+ * - as first argument an array [x, y, z, w]
479479+ *
480480+ * if y is not provided, it will be set to 0
481481+ * if z is not provided, will use 2D noise
482482+ * if w is not provided, will use 3D noise
483483+ *
484484+ * @param x {number | VectorLikeObject | Array<number>}
485485+ * @param y {number | undefined}
486486+ * @param z {number | undefined}
487487+ * @param w {number | undefined}
488488+ * @returns {number}
489489+ */
457490 get(
458491 x: number | VectorLikeObject | Array<number>,
459492 y: number | undefined = undefined,
···464497 return this.normalizedToMinMax(norm);
465498 }
466499467467- // same as get but returns between -1 and 1
500500+ /**
501501+ * get normalized noise value at position x, y, z, w
502502+ *
503503+ * all transformations will be applied to the noise (shift, warp, power, sharpness, steps, fmb, etc)
504504+ * EXCEPT min and max, noise will be returned as a value between -1 and 1
505505+ *
506506+ * either pass in
507507+ * - x, y, z, w as separate arguments
508508+ * - as first argument an object {x, y, z, w}
509509+ * - as first argument an array [x, y, z, w]
510510+ *
511511+ * if y is not provided, it will be set to 0
512512+ * if z is not provided, will use 2D noise
513513+ * if w is not provided, will use 3D noise
514514+ *
515515+ * @param x {number | VectorLikeObject | Array<number>}
516516+ * @param y {number | undefined}
517517+ * @param z {number | undefined}
518518+ * @param w {number | undefined}
519519+ * @returns {number}
520520+ */
468521 normalized(
469522 x: number | VectorLikeObject | Array<number>,
470523 y: number | undefined = undefined,
···509562 return (value + 1) * 0.5 * (this.max - this.min) + this.min;
510563 }
511564565565+ /**
566566+ *
567567+ * shift the noise in 2D, 3D or 4D space, will be added to the position
568568+ *
569569+ * if called multiple times, the shifts will be added
570570+ *
571571+ * returns the UberNoise object for chaining
572572+ *
573573+ * @param x {number}
574574+ * @param y {number}
575575+ * @param z {number | undefined}
576576+ * @param w {number | undefined}
577577+ * @returns {UberNoise}
578578+ */
512579 move(
513580 x: number,
514581 y: number,
···528595 if (w != undefined) {
529596 this.shift.w = (this.shift.w ?? 0) + w;
530597 }
598598+599599+ return this;
531600 }
532601533533- private checkParameterInput(value: NoiseParameterInput): UberNoise | number {
602602+ private checkParameterInput(
603603+ value: number | UberNoise | NoiseOptions,
604604+ ): UberNoise | number {
534605 if (typeof value === 'object' && !(value instanceof UberNoise)) {
535606 if (value.seed === undefined) {
536607 value.seed = this.pngr();
···551622 get min(): number {
552623 return this.getParameter(this._min);
553624 }
554554- set min(value: NoiseParameterInput) {
625625+ set min(value: number | UberNoise | NoiseOptions) {
555626 this._min = this.checkParameterInput(value);
556627 }
557628 get max(): number {
558629 return this.getParameter(this._max);
559630 }
560560- set max(value: NoiseParameterInput) {
631631+ set max(value: number | UberNoise | NoiseOptions) {
561632 this._max = this.checkParameterInput(value);
562633 }
563634···565636 get scale(): number {
566637 return this.getParameter(this._scale);
567638 }
568568- set scale(value: NoiseParameterInput) {
639639+ set scale(value: number | UberNoise | NoiseOptions) {
569640 this._scale = this.checkParameterInput(value);
570641 }
571642 get power(): number {
572643 return this.getParameter(this._power);
573644 }
574574- set power(value: NoiseParameterInput) {
645645+ set power(value: number | UberNoise | NoiseOptions) {
575646 this._power = this.checkParameterInput(value);
576647 }
577648···579650 get gain(): number {
580651 return this.getParameter(this._gain);
581652 }
582582- set gain(value: NoiseParameterInput) {
653653+ set gain(value: number | UberNoise | NoiseOptions) {
583654 this._gain = this.checkParameterInput(value);
584655 }
585656 get lacunarity(): number {
586657 return this.getParameter(this._lacunarity);
587658 }
588588- set lacunarity(value: NoiseParameterInput) {
659659+ set lacunarity(value: number | UberNoise | NoiseOptions) {
589660 this._lacunarity = this.checkParameterInput(value);
590661 }
591662592592- // getter and setter for erosion, sharpness and steps
593593- get erosion(): number {
594594- return this.getParameter(this._erosion);
595595- }
596596- set erosion(value: NoiseParameterInput) {
597597- this._erosion = this.checkParameterInput(value);
598598- }
599663 get sharpness(): number {
600664 return this.getParameter(this._sharpness);
601665 }
602602- set sharpness(value: NoiseParameterInput) {
666666+ set sharpness(value: number | UberNoise | NoiseOptions) {
603667 this._sharpness = this.checkParameterInput(value);
604668 }
605669 get steps(): number {
606670 return Math.round(this.getParameter(this._steps));
607671 }
608608- set steps(value: NoiseParameterInput) {
672672+ set steps(value: number | UberNoise | NoiseOptions) {
609673 this._steps = this.checkParameterInput(value);
610674 }
611675612612- // getter and setter for warp, warpNoise, warp2 and warpNoise2
613676 get warp(): number {
614677 return this.getParameter(this._warp);
615678 }
616616- set warp(value: NoiseParameterInput) {
679679+ set warp(value: number | UberNoise | NoiseOptions) {
617680 this._warp = this.checkParameterInput(value);
618681 }
619682 get warpNoise(): UberNoise | undefined {
···633696 get warp2(): number {
634697 return this.getParameter(this._warp2);
635698 }
636636- set warp2(value: NoiseParameterInput) {
699699+ set warp2(value: number | UberNoise | NoiseOptions) {
637700 this._warp2 = this.checkParameterInput(value);
638701 }
639702 get warpNoise2(): UberNoise | undefined {
···656719 UberNoise,
657720 type NoiseOptions,
658721 type NoiseParameter,
659659- type NoiseParameterInput,
660722 type VectorLikeObject as VectorObject,
661723};