noise.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //import 'https://cdn.jsdelivr.net/npm/[email protected]/simplex-noise.js';
  2. //import perlin from 'https://cdn.jsdelivr.net/gh/mikechambers/es6-perlin-module/perlin.js';
  3. import perlin from './perlin-noise.js';
  4. import {simplex} from './simplex-noise.js';
  5. import {math} from './math.js';
  6. export const noise = (function() {
  7. class _NoiseGenerator {
  8. constructor(params) {
  9. this._params = params;
  10. this._Init();
  11. }
  12. _Init() {
  13. this._noise = new simplex.SimplexNoise(this._params.seed);
  14. }
  15. Get(x, y, z) {
  16. const G = 2.0 ** (-this._params.persistence);
  17. const xs = x / this._params.scale;
  18. const ys = y / this._params.scale;
  19. const zs = z / this._params.scale;
  20. const noiseFunc = this._noise;
  21. let amplitude = 1.0;
  22. let frequency = 1.0;
  23. let normalization = 0;
  24. let total = 0;
  25. for (let o = 0; o < this._params.octaves; o++) {
  26. const noiseValue = noiseFunc.noise3D(
  27. xs * frequency, ys * frequency, zs * frequency) * 0.5 + 0.5;
  28. total += noiseValue * amplitude;
  29. normalization += amplitude;
  30. amplitude *= G;
  31. frequency *= this._params.lacunarity;
  32. }
  33. total /= normalization;
  34. return Math.pow(
  35. total, this._params.exponentiation) * this._params.height;
  36. }
  37. }
  38. return {
  39. Noise: _NoiseGenerator
  40. }
  41. })();