Skip to content

Code improvements of the Perlin noise generator. #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 7 additions & 37 deletions src/math/noise.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,11 @@ var PERLIN_SIZE = 4095;
var perlin_octaves = 4; // default to medium smooth
var perlin_amp_falloff = 0.5; // 50% reduction/octave

// [toxi 031112]
// Maybe we should have a lookup table for speed

var SINCOS_PRECISION = 0.5;
var SINCOS_LENGTH = Math.floor(360 / SINCOS_PRECISION);
var sinLUT = new Array(SINCOS_LENGTH);
var cosLUT = new Array(SINCOS_LENGTH);
var DEG_TO_RAD = Math.PI/180.0;
for (var i = 0; i < SINCOS_LENGTH; i++) {
sinLUT[i] = Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
cosLUT[i] = Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
}

var perlin_PI = SINCOS_LENGTH;
perlin_PI >>= 1;
var scaled_cosine = function(i) {
return 0.5*(1.0-Math.cos(i*Math.PI));
};

var perlin;
var perlin; // will be initialized lazily by noise() or noiseSeed()


/**
Expand Down Expand Up @@ -111,16 +99,10 @@ var perlin;
* </div>
*/
p5.prototype.noise = function(x,y,z) {
// is this legit?
y = y || 0;
z = z || 0;

if (perlin == null) {
// need to deal with seeding?
//if (perlinRandom == null) {
// perlinRandom = new Random();
//}

perlin = new Array(PERLIN_SIZE + 1);
for (var i = 0; i < PERLIN_SIZE + 1; i++) {
perlin[i] = Math.random();
Expand All @@ -142,17 +124,11 @@ p5.prototype.noise = function(x,y,z) {

var n1,n2,n3;

// Is this right do just have this here?
var noise_fsc = function(i) {
// using cosine lookup table
return 0.5*(1.0-cosLUT[Math.floor(i*perlin_PI)%SINCOS_LENGTH]);
};

for (var o=0; o<perlin_octaves; o++) {
var of=xi+(yi<<PERLIN_YWRAPB)+(zi<<PERLIN_ZWRAPB);

rxf= noise_fsc(xf);
ryf= noise_fsc(yf);
rxf = scaled_cosine(xf);
ryf = scaled_cosine(yf);

n1 = perlin[of&PERLIN_SIZE];
n1 += rxf*(perlin[(of+1)&PERLIN_SIZE]-n1);
Expand All @@ -167,7 +143,7 @@ p5.prototype.noise = function(x,y,z) {
n3 += rxf*(perlin[(of+PERLIN_YWRAP+1)&PERLIN_SIZE]-n3);
n2 += ryf*(n3-n2);

n1 += noise_fsc(zf)*(n2-n1);
n1 += scaled_cosine(zf)*(n2-n1);

r += n1*ampl;
ampl *= perlin_amp_falloff;
Expand All @@ -186,12 +162,6 @@ p5.prototype.noise = function(x,y,z) {
};



// [toxi 040903]
// make perlin noise quality user controlled to allow
// for different levels of detail. lower values will produce
// smoother results as higher octaves are suppressed

/**
*
* Adjusts the character and level of detail produced by the Perlin noise
Expand Down