Excalibur Perlin Noise
This is a small module that implements the original perlin noise implementation in Excalibur given the original Siggraph paper
You may want to use noise if you need "smooth" randomness from point to point. It's super useful for generating "natural" looking structures randomly.
Usage
In your excalibur project run
npm install @excaliburjs/plugin-perlin
import { PerlinGenerator } from '@excaliburjs/plugin-perlin';
const generator = new PerlinGenerator({
seed: 515,
octaves: 2,
frequency: 24,
amplitude: 0.91,
persistance: 0.95
});
You can use the generator to sample perlin noise with the given configuration.
generator.noise(x)
- Sample 1D noise, outputs a value [0, 1]generator.noise(x, y)
- Sample 2D noise, outputs a value [0, 1]generator.noise(x, y, z)
- Sample 3D noise, , outputs a value [0, 1]generator.sequence(length, optionalStep)
- Generates a list of 1D noise starting at 0 and ending at 1 of continuous perlin noise, by default the step is 1/length;generator.grid(width, height, optionalStep)
- Generates a 2D grid as a packed array of perlin noise given a step value packed into a 1D array i = (x + y*width), by default the step will 1/(min(dimension))
Perlin Drawing
If you want to draw perlin noise directly there is a helper PerlinDrawer2D
which can be used to draw to a Canvas
or output an HTML Image.
const drawer = new PerlinDrawer2D(generator, (value: number) => {
const rainbow = [
ex.Color.Red,
ex.Color.Orange,
ex.Color.Yellow,
ex.Color.Green,
ex.Color.Blue,
ex.Color.Violet
]
const colorIndex = Math.floor((val * rainbow.length));
return rainbow[colorIndex];
});
const canvas = new ex.Canvas({
width: 800,
height: 600,
cache: true,
draw: (ctx) => {
drawer.draw(ctx, 0, 0, 800, 600);
}
});
const actor = new ex.Actor({x: 0, y: 0, width: 800, height: 600});
actor.graphics.use(canvas);
actor.graphics.anchor = ex.Vector.Zero;
game.add(actor);
Here is an example of HTML image output, this can be used as an upload to an Excalibur Material
or other ImageSource
.
const drawer = new PerlinDrawer2D(generator, (value: number) => {
const rainbow = [
ex.Color.Red,
ex.Color.Orange,
ex.Color.Yellow,
ex.Color.Green,
ex.Color.Blue,
ex.Color.Violet
]
const colorIndex = Math.floor((val * rainbow.length));
return rainbow[colorIndex];
});
const image = drawer.image(200, 200);
document.body.appendChild(image);