quick-noise-js

1.0.2 • Public • Published

quick-noise.js

Fast, public domain implementation of perlin noise.

Works either in the browser or as a common.js module. In the browser it's functions will be available on the quickNoise object.

This started as a port of Sean Barrett's excellent stb_perlin.h library but no longer has much in common with it.

At the time of this writing (Sep 2015) it is the fastest out of the noise modules (on my computer), beating most of the others by at least 10%. However, the optimizations applied in it require that your runtime support Typed Arrays (I do not plan on removing this restriction in the future, since most runtimes support this).

Documentation

function quickNoise.noise(x, y, z, xWrap=0, yWrap=0, zWrap=0)

  • x, y, z are numbers.
  • xWrap, yWrap, and zWrap are integer powers of two between 0 and 256. (0 and 256 are equivalent). If these aren't provided, they default to 0.

This implements Ken Perlin's revised noise function from 2002, in 3D. It computes a random value between -1 and 1 for the coordinate x, y, z, where adjacent values are continuous with a period of 1 (Values at integer points are entirely unrelated).

This function is seeded. That is, it will return the same results when called with the same arguments, across successive program runs. An unseeded version may be created with the quickNoise.create function. The table it is seeded is the one from the stb_perlin.h library.

function quickNoise.create(tableOrRng=Math.random)

tableOrRng must either be:

  • A function that takes 0 arguments and returns a uniformly distributed random number between 0 and 1 (like Math.random).
  • An array of length 256, where the array is generated by shuffling all integers between 0 and 255 (inclusive).

If no argument (or a bad argument) is provided, it defaults to Math.random.

This creates a perlin noise generation function. For more documentation about the function returned by this call, see the documentation for quickNoise.noise, above.

If you provide a function, this will be used to generate the permutation table, and will not be called after this function returns (it won't be used by the function returned by this one, if that makes any sense).

The array argument provided in case you want to provide a specific permutation table.

Usage Example

var UPSCALE = 4;
var canvas = document.getElementsByTagName('canvas')[0];
var ctx = canvas.getContext('2d');
var pixels = null;
var pixBuf = null;
function resize() {
    canvas.style.width = window.innerWidth+"px";
    canvas.style.height = window.innerHeight+"px";
    canvas.height = window.innerHeight/UPSCALE;
    canvas.width = window.innerWidth/UPSCALE;
    pixels = ctx.createImageData(canvas.width, canvas.height);
    pixBuf = new Uint32Array(pixels.data.buffer);
}
 
window.addEventListener('resize', resize);
resize();
var zValue = 0;
function go() {
    var resolution = 10;
    zValue += 1.0 / 60.0;
    var width = ctx.canvas.width;
    var height = ctx.canvas.height;
    var i = 0;
    for (var y = 0; y < height; ++y) {
        for (var x = 0; x < width; ++x) {
            var noiseR = quickNoise.noise(x/resolution, y/resolution, 0+zValue)+1;
            var noiseG = quickNoise.noise(x/resolution, y/resolution, 1+zValue)+1;
            var noiseB = quickNoise.noise(x/resolution, y/resolution, 2+zValue)+1;
            var r = Math.floor(noiseR * 128) & 0xff;
            var g = Math.floor(noiseG * 128) & 0xff;
            var b = Math.floor(noiseB * 128) & 0xff;
            pixBuf[i++] = r | (<< 8) | (<< 16) | 0xff000000;
        }
    }
    ctx.putImageData(pixels, 0, 0);
    requestAnimationFrame(go);
}
go();

License

Public Domain (CC0).

Readme

Keywords

Package Sidebar

Install

npm i quick-noise-js

Weekly Downloads

0

Version

1.0.2

License

CC0-1.0

Last publish

Collaborators

  • zuurr