/* * ------------------------------------------------------- * Class: PerlinNoiseClass.as * ------------------------------------------------------- * Version: 1.0 * ------------------------------------------------------- * Latest update: August 1, 2003 * ------------------------------------------------------- * Purpose: Object and associated methods for Perlin style noise creation. * ------------------------------------------------------- * Adaptation for Flash MX by Richard Wright - 2003 [wisolutions2002@shaw.ca] * ------------------------------------------------------- * Copyright: A. Phelps - 2002. Rochester Institute of Technology. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of this software nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ------------------------------------------------------ * Note: The pseudocode to this number generator first appeared on * Hugo Elias's wonderful web site article about Perlin noise, * and was republished in Special Effects Game Programming with * Direct X by Mason McCuskey [2]. Unfortunately, the original * author is unknown. See associated documentation for links and * references. * ----------------------------------------------------- * Functions: * 1. Noise1D(x) * 2. Noise2D(x,y) * 3. Noise3D(x,y,z) * 4. SmoothNoise2D(x,y) * 5. Interpolate(a,b,x) * 6. InterpolatedNoise2D(x,y) * ----------------------------------------------------- * Updates may be available at: * http://members.shaw.ca/flashmath101/as/ * ----------------------------------------------------- */ // check if this file has already been included if (!_global._PERLINCLASS_AS) { // create the variable that keeps track of whether or not this file has been included _global._PERLINCLASS_AS = true; perlin = new Object(); perlin.maxH = 0; perlin.maxV = 0; // 1. Noise1D perlin.Noise1D = function(x) { x = Number(x); x = (x<<13)^x; return (1.000-((x*(x*x*15731+789221)+1376312589)&0x7FFFFFFF)/1073741824.0); }; // 2. Noise2D perlin.Noise2D = function(x,y) { var n; x = Number(x); y = Number(y); n = x+y*17; n = (n<<13)^n; return (1.000-((n*(n*n*15731+789221)+1376312589)&0x7FFFFFFF)/1073741824.0); }; // 3. perlin.Noise3D = function(x,y,z) { var n; n = Math.floor(x+y*57+z*131); n = (n<<13)^n; return (1.000-((n*(n*n*15731+789221)+1376312589)&0x7FFFFFFF)*0.000000000931322574615478515625); }; // 4. perlin.SmoothNoise2D = function(x,y) { var corners,sides,center,x_minus1,y_minus1,x_plus1,y_plus1; /* // uncomment for non-wrapped clamp corners = (this.Noise2D(x-1,y-1)+this.Noise2D(x+1,y-1)+this.Noise2D(x-1,y+1)+this.Noise2D(x+1,y+1))/16; sides = (this.Noise2D(x-1,y) +this.Noise2D(x+1,y)+this.Noise2D(x,y-1)+this.Noise2D(x,y+1))/8; center = this.Noise2D(x,y)/4; return corners+sides+center; */ x_minus1 = x-1; if (x_minus1<0) x = this.maxH; y_minus1 = y-1; if (y_minus1<0) y = this.maxV; x_plus1 = x+1; if (x_plus1>this.maxH) x_plus1 = 0; y_plus1 = y+1; if (y_plus1>this.maxV) y_plus1 = 0; corners = (this.Noise2D(x_minus1,y_minus1)+this.Noise2D(x_plus1,y_minus1)+this.Noise2D(x_minus1,y_plus1)+this.Noise2D(x_plus1,y_plus1))/16; sides = (this.Noise2D(x_minus1,y)+this.Noise2D(x_plus1,y)+this.Noise2D(x,y_minus1)+this.Noise2D(x,y_plus1))/8; center = this.Noise2D(x,y)/4; return corners+sides+center; }; // 5. perlin.Interpolate = function(a,b,x) { var ft,f; ft = x*Math.PI; f = (1-Math.cos(ft))*.5; return a*(1-f)+b*f; }; // 6. perlin.InterpolatedNoise2D = function(x,y) { var integer_X,fractional_X,integer_Y,fractional_Y,v1,v2,v3,v4,i1,i2; if (x>this.maxH) x = 0; if (y>this.maxV) y = 0; if (x<0) x = this.maxH; if (y<0) x = this.maxV; integer_X = int(x); fractional_X = x-integer_X; integer_Y = int(y); fractional_Y = y-integer_Y; v1 = this.SmoothNoise2D(integer_X,integer_Y); v2 = this.SmoothNoise2D(integer_X+1,integer_Y); v3 = this.SmoothNoise2D(integer_X,integer_Y+1); v4 = this.SmoothNoise2D(integer_X+1,integer_Y+1); i1 = this.Interpolate(v1,v2,fractional_X); i2 = this.Interpolate(v3,v4,fractional_X); return this.Interpolate(i1,i2,fractional_Y); }; } // closes the 'if' statement that checked if this file had already been included