simplex-noise.js

repository·main·Indexed 23 days ago

https://github.com/jwagner/simplex-noise.js

A high-performance, dependency-free implementation of Simplex noise for JavaScript and TypeScript. It supports 2D, 3D, and 4D noise generation and works in both browser and Node.js environments. Version 4.0.3 utilizes a functional factory approach via createNoise2D, createNoise3D, and createNoise4D.

Tokens
1.2K
Snippets
8
Records
8
Agent score
34%

What's inside simplex-noise

  1. Use a seed value with a PRNG

    main

    By default, simplex-noise.js uses Math.random(). To use a specific seed, you must provide a Pseudo-Random Number Generator (PRNG) function to the creation functions. It is recommended to use the alea package for this purpose.

    1. Install alea: npm install -S alea
    2. Create a PRNG instance with your seed.
    3. Pass the PRNG instance to createNoise2D, createNoise3D, or createNoise4D.

    Note: If you are creating multiple noise functions (e.g., 2D and 3D) and want them to be independent/compatible with 3.x behavior, pass a fresh instance of alea to each call.

    import alea from 'alea';
    import { createNoise2D } from 'simplex-noise';
    
    const prng = alea('seed');
    const noise2D = createNoise2D(prng);
    console.log(noise2D(x, y));
    import alea from 'alea';
    // create a new random function based on the seed
    const prng = alea('seed');
    // use the seeded random function to initialize the noise function
    const noise2D = createNoise2D(prng);
    console.log(noise2D(x, y));
  2. Migrate from 3.x to 4.x

    main

    Version 4.x introduced breaking changes to the API and output.

    API Changes

    • 3.x: Used a class-based approach: new SimplexNoise().noise2D(x, y).
    • 4.x: Uses functional factory approach: createNoise2D()(x, y).

    Seeding Changes

    • 3.x: Allowed passing a seed string directly to the constructor: new SimplexNoise('seed').
    • 4.x: Requires an external PRNG function (like alea) passed to the factory: createNoise2D(alea('seed')).

    Emulating the 3.x API

    If you need to maintain a similar interface, you can wrap the new functions in an object:

    const simplex = {
      noise2D: createNoise2D(alea(seed)),
      noise3D: createNoise3D(alea(seed)),
      noise4D: createNoise4D(alea(seed)),
    };
    // 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);
  3. Generate 4D noise

    main

    Initialize a 4D noise function using createNoise4D(). The resulting function accepts four coordinates (x, y, z, w) and returns a value between -1 and 1.

    const noise4D = createNoise4D();
    const value = noise4D(x, y, z, w);
    const noise4D = createNoise4D();
    console.log(noise4D(x, y, z, w));
  4. Import noise functions in CommonJS

    main

    In a CommonJS environment (like Node.js), use require to import the noise creation functions.

    const { createNoise2D } = require('simplex-noise');
    // import the noise functions you need
    const { createNoise2D } = require('simplex-noise');
  5. Generate 2D noise

    main

    Initialize a 2D noise function using createNoise2D(). The resulting function accepts two coordinates (x, y) and returns a value between -1 and 1.

    const noise2D = createNoise2D();
    const value = noise2D(x, y);
    // initialize the noise function
    const noise2D = createNoise2D();
    // returns a value between -1 and 1
    console.log(noise2D(x, y));
  6. Import noise functions in ES Modules

    main

    In an ES Module environment, import only the specific noise creation functions you need to enable tree shaking and reduce bundle size.

    import { createNoise2D } from 'simplex-noise';
    // import the noise functions you need
    import { createNoise2D } from 'simplex-noise';
  7. Generate 3D noise

    main

    Initialize a 3D noise function using createNoise3D(). The resulting function accepts three coordinates (x, y, z) and returns a value between -1 and 1.

    const noise3D = createNoise3D();
    const value = noise3D(x, y, z);
    const noise3D = createNoise3D();
    console.log(noise3D(x, y, z));