Skip to content

Instantly share code, notes, and snippets.

@eskimoblood
Created August 18, 2011 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eskimoblood/1153776 to your computer and use it in GitHub Desktop.
Save eskimoblood/1153776 to your computer and use it in GitHub Desktop.
port of Noise Wave by Daniel Shiffman from processing to plask. Using Sean McCulloughs perlin noise implentation and convert it to work as commonJS module https://gist.github.com/304522 a
/**
* Created by JetBrains WebStorm.
* User: akoeberle
* Date: 18.08.11
* Time: 10:39
* Ported Sean McCulloughs JavaScript port of perlin noise (https://gist.github.com/304522), to commonJS module
*/
var grad3 = [
[1,1,0],
[-1,1,0],
[1,-1,0],
[-1,-1,0],
[1,0,1],
[-1,0,1],
[1,0,-1],
[-1,0,-1],
[0,1,1],
[0,-1,1],
[0,1,-1],
[0,-1,-1]
],
p = [],
perm = [];
for (var i = 0; i < 256; i++) {
p[i] = Math.floor(Math.random() * 256);
}
// To remove the need for index wrapping, double the permutation table length
for (var i = 0; i < 512; i++) {
perm[i] = p[i & 255];
}
function dot(g, x, y, z) {
return g[0] * x + g[1] * y + g[2] * z;
}
function mix(a, b, t) {
return (1.0 - t) * a + t * b;
}
function fade(t) {
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
}
// Classic Perlin noise, 3D version
exports.noise = function (x, y, z) {
// Find unit grid cell containing point
var X = Math.floor(x);
var Y = Math.floor(y);
var Z = Math.floor(z);
// Get relative xyz coordinates of point within that cell
x = x - X;
y = y - Y;
z = z - Z;
// Wrap the integer cells at 255 (smaller integer period can be introduced here)
X = X & 255;
Y = Y & 255;
Z = Z & 255;
// Calculate a set of eight hashed gradient indices
var gi000 = perm[X + perm[Y + perm[Z]]] % 12;
var gi001 = perm[X + perm[Y + perm[Z + 1]]] % 12;
var gi010 = perm[X + perm[Y + 1 + perm[Z]]] % 12;
var gi011 = perm[X + perm[Y + 1 + perm[Z + 1]]] % 12;
var gi100 = perm[X + 1 + perm[Y + perm[Z]]] % 12;
var gi101 = perm[X + 1 + perm[Y + perm[Z + 1]]] % 12;
var gi110 = perm[X + 1 + perm[Y + 1 + perm[Z]]] % 12;
var gi111 = perm[X + 1 + perm[Y + 1 + perm[Z + 1]]] % 12;
var n000 = dot(grad3[gi000], x, y, z);
var n100 = dot(grad3[gi100], x - 1, y, z);
var n010 = dot(grad3[gi010], x, y - 1, z);
var n110 = dot(grad3[gi110], x - 1, y - 1, z);
var n001 = dot(grad3[gi001], x, y, z - 1);
var n101 = dot(grad3[gi101], x - 1, y, z - 1);
var n011 = dot(grad3[gi011], x, y - 1, z - 1);
var n111 = dot(grad3[gi111], x - 1, y - 1, z - 1);
// Compute the fade curve value for each of x, y, z
var u = fade(x);
var v = fade(y);
var w = fade(z);
// Interpolate along x the contributions from each of the corners
var nx00 = mix(n000, n100, u);
var nx01 = mix(n001, n101, u);
var nx10 = mix(n010, n110, u);
var nx11 = mix(n011, n111, u);
// Interpolate the four results along y
var nxy0 = mix(nx00, nx10, v);
var nxy1 = mix(nx01, nx11, v);
// Interpolate the two last results along z
var nxyz = mix(nxy0, nxy1, w);
return nxyz;
}
/**
* port of Noise Wave
* by Daniel Shiffman from processing to plask
*
* Using Perlin Noise to generate a wave-like pattern.
*/
var plask = require('plask');
var noise = require('./classicalNoise').noise;
var xspacing = 8; // How far apart should each horizontal location be spaced
var w; // Width of entire wave
var yoff = 0; // 2nd dimension of perlin noise
var yvalues = [];
plask.simpleWindow({
settings: {
width: 800,
height: 400
},
init: function() {
var canvas = this.canvas, paint = this.paint;
this.framerate(30);
paint.setStyle(paint.kFillStyle);
paint.setFlags(paint.kAntiAliasFlag);
paint.setColor(80, 0, 0, 50);
w = this.width + 16;
yvalues = new Array(w / xspacing);
},
draw: function() {
this.calcWave();
var canvas = this.canvas, paint = this.paint;
canvas.drawColor(255, 255, 255, 255);
yvalues.forEach(function(value, i) {
canvas.drawCircle(paint, i * xspacing, 200 + value, 16);
});
},
calcWave:function() {
var dx = 0.05;
var dy = 0.01;
var amplitude = 100;
var xoff = yoff += dy;
for (var i = 0; i < yvalues.length; i++) {
yvalues[i] = (2 * noise(xoff, yoff, 0) - 1) * amplitude;
xoff += dx;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment