'use strict';
//TODO(Christof): credit author in some way? (source: https://gist.github.com/KdotJPG/b1270127455a94ac5d19)
var OpenSimplex = function(){
	var STRETCH_CONSTANT_2D = -0.211324865405187;    //(1/Math.sqrt(2+1)-1)/2;
	var SQUISH_CONSTANT_2D = 0.366025403784439;      //(Math.sqrt(2+1)-1)/2;
	var STRETCH_CONSTANT_3D = -1.0 / 6;              //(1/Math.sqrt(3+1)-1)/3;
	var SQUISH_CONSTANT_3D = 1.0 / 3;                //(Math.sqrt(3+1)-1)/3;
	var STRETCH_CONSTANT_4D = -0.138196601125011;    //(1/Math.sqrt(4+1)-1)/4;
	var SQUISH_CONSTANT_4D = 0.309016994374947;      //(Math.sqrt(4+1)-1)/4;

	var NORM_CONSTANT_2D = 47;
	var NORM_CONSTANT_3D = 103;
	var NORM_CONSTANT_4D = 30;

	var DEFAULT_SEED = 0;

	var NewSimplex = {};
	var gradients2D = [
		5,  2,    2,  5,
			-5,  2,   -2,  5,
		5, -2,    2, -5,
			-5, -2,   -2, -5,
	];

	var gradients3D = [
			-11,  4,  4,     -4,  11,  4,    -4,  4,  11,
		11,  4,  4,      4,  11,  4,     4,  4,  11,
			-11, -4,  4,     -4, -11,  4,    -4, -4,  11,
		11, -4,  4,      4, -11,  4,     4, -4,  11,
			-11,  4, -4,     -4,  11, -4,    -4,  4, -11,
		11,  4, -4,      4,  11, -4,     4,  4, -11,
			-11, -4, -4,     -4, -11, -4,    -4, -4, -11,
		11, -4, -4,      4, -11, -4,     4, -4, -11,
	];

	var extrapolate2d = function (xsb,ysb,dx,dy, Simplex)
	{
		var index = Simplex.perm[(Simplex.perm[xsb & 0xFF] + ysb) & 0xFF] & 0x0E;
		var ret = gradients2D[index] * dx
			+ gradients2D[index + 1] * dy;
		if(ret<0)
			var x = 0;

		return ret;
	}

    var extrapolate3d = function (xsb, ysb, zsb, dx, dy, dz, Simplex){
		var index = Simplex.permGradIndex3D[(Simplex.perm[(Simplex.perm[xsb & 0xFF] + ysb) & 0xFF] + zsb) & 0xFF];
		return gradients3D[index] * dx
			+ gradients3D[index + 1] * dy
			+ gradients3D[index + 2] * dz;
	}

	return {
		create : function(seed){
			NewSimplex.noise2D = function(x, y){
				//Place input coordinates onto grid.
				var stretchOffset = (x + y) * STRETCH_CONSTANT_2D;
				var xs = x + stretchOffset;
				var ys = y + stretchOffset;

				//Floor to get grid coordinates of rhombus (stretched square) super-cell origin.
				var xsb = Math.floor(xs);
				var ysb = Math.floor(ys);

				//Skew out to get actual coordinates of rhombus origin. We'll need these later.
				var squishOffset = (xsb + ysb) * SQUISH_CONSTANT_2D;
				var xb = xsb + squishOffset;
				var yb = ysb + squishOffset;

				//Compute grid coordinates relative to rhombus origin.
				var xins = xs - xsb;
				var yins = ys - ysb;

				//Sum those together to get a value that determines which region we're in.
				var inSum = xins + yins;

				//Positions relative to origin povar.
				var dx0 = x - xb;
				var dy0 = y - yb;

				//We'll be defining these inside the next block and using them afterwrads.
				var dx_ext, dy_ext;
				var xsv_ext, ysv_ext;

				var value = 0;

				//Contribution (1,0)
				var dx1 = dx0 - 1 - SQUISH_CONSTANT_2D;
				var dy1 = dy0 - 0 - SQUISH_CONSTANT_2D;
				var attn1 = 2 - dx1 * dx1 - dy1 * dy1;
				if (attn1 > 0) {
					attn1 *= attn1;
					value += attn1 * attn1 * extrapolate2d(xsb + 1, ysb + 0, dx1, dy1, this);
				}

				//Contribution (0,1)
				var dx2 = dx0 - 0 - SQUISH_CONSTANT_2D;
				var dy2 = dy0 - 1 - SQUISH_CONSTANT_2D;
				var attn2 = 2 - dx2 * dx2 - dy2 * dy2;
				if (attn2 > 0) {
					attn2 *= attn2;
					value += attn2 * attn2 * extrapolate2d(xsb + 0, ysb + 1, dx2, dy2, this);
				}

				if (inSum <= 1) { //We're inside the triangle (2-Simplex) at (0,0)
					var zins = 1 - inSum;
					if (zins > xins || zins > yins) { //(0,0) is one of the closest two triangular vertices
						if (xins > yins) {
							xsv_ext = xsb + 1;
							ysv_ext = ysb - 1;
							dx_ext = dx0 - 1;
							dy_ext = dy0 + 1;
						} else {
							xsv_ext = xsb - 1;
							ysv_ext = ysb + 1;
							dx_ext = dx0 + 1;
							dy_ext = dy0 - 1;
						}
					} else { //(1,0) and (0,1) are the closest two vertices.
						xsv_ext = xsb + 1;
						ysv_ext = ysb + 1;
						dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
						dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
					}
				} else { //We're inside the triangle (2-Simplex) at (1,1)
					var zins = 2 - inSum;
					if (zins < xins || zins < yins) { //(0,0) is one of the closest two triangular vertices
						if (xins > yins) {
							xsv_ext = xsb + 2;
							ysv_ext = ysb + 0;
							dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT_2D;
							dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT_2D;
						} else {
							xsv_ext = xsb + 0;
							ysv_ext = ysb + 2;
							dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT_2D;
							dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT_2D;
						}
					} else { //(1,0) and (0,1) are the closest two vertices.
						dx_ext = dx0;
						dy_ext = dy0;
						xsv_ext = xsb;
						ysv_ext = ysb;
					}
					xsb += 1;
					ysb += 1;
					dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
					dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
				}

				//Contribution (0,0) or (1,1)
				var attn0 = 2 - dx0 * dx0 - dy0 * dy0;
				if (attn0 > 0) {
					attn0 *= attn0;
					value += attn0 * attn0 * extrapolate2d(xsb, ysb, dx0, dy0, this);
				}

				//Extra Vertex
				var attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext;
				if (attn_ext > 0) {
					attn_ext *= attn_ext;
					value += attn_ext * attn_ext * extrapolate2d(xsv_ext, ysv_ext, dx_ext, dy_ext, this);
				}

				return value / NORM_CONSTANT_2D;
			};

		    NewSimplex.noise3D = function(x, y, z){

			//Place input coordinates on simplectic honeycomb.
		var stretchOffset = (x + y + z) * STRETCH_CONSTANT_3D;
		var xs = x + stretchOffset;
		var ys = y + stretchOffset;
		var zs = z + stretchOffset;

		//Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
		var xsb = Math.floor(xs);
		var ysb = Math.floor(ys);
		var zsb = Math.floor(zs);

		//Skew out to get actual coordinates of rhombohedron origin. We'll need these later.
		var squishOffset = (xsb + ysb + zsb) * SQUISH_CONSTANT_3D;
		var xb = xsb + squishOffset;
		var yb = ysb + squishOffset;
		var zb = zsb + squishOffset;

		//Compute simplectic honeycomb coordinates relative to rhombohedral origin.
		var xins = xs - xsb;
		var yins = ys - ysb;
		var zins = zs - zsb;

		//Sum those together to get a value that determines which region we're in.
		var inSum = xins + yins + zins;

		//Positions relative to origin povar.
		var dx0 = x - xb;
		var dy0 = y - yb;
		var dz0 = z - zb;

		//We'll be defining these inside the next block and using them afterwards.
		var dx_ext0, dy_ext0, dz_ext0;
		var dx_ext1, dy_ext1, dz_ext1;
		var xsv_ext0, ysv_ext0, zsv_ext0;
		var xsv_ext1, ysv_ext1, zsv_ext1;

		var value = 0;
		if (inSum <= 1) { //We're inside the tetrahedron (3-Simplex) at (0,0,0)

			//Determine which two of (0,0,1), (0,1,0), (1,0,0) are closest.
			var aPovar = 0x01;
			var aScore = xins;
			var bPovar = 0x02;
			var bScore = yins;
			if (aScore >= bScore && zins > bScore) {
				bScore = zins;
				bPovar = 0x04;
			} else if (aScore < bScore && zins > aScore) {
				aScore = zins;
				aPovar = 0x04;
			}

			//Now we determine the two lattice povars not part of the tetrahedron that may contribute.
			//This depends on the closest two tetrahedral vertices, including (0,0,0)
			var wins = 1 - inSum;
			if (wins > aScore || wins > bScore) { //(0,0,0) is one of the closest two tetrahedral vertices.
				var c = (bScore > aScore ? bPovar : aPovar); //Our other closest vertex is the closest out of a and b.

				if ((c & 0x01) == 0) {
					xsv_ext0 = xsb - 1;
					xsv_ext1 = xsb;
					dx_ext0 = dx0 + 1;
					dx_ext1 = dx0;
				} else {
					xsv_ext0 = xsv_ext1 = xsb + 1;
					dx_ext0 = dx_ext1 = dx0 - 1;
				}

				if ((c & 0x02) == 0) {
					ysv_ext0 = ysv_ext1 = ysb;
					dy_ext0 = dy_ext1 = dy0;
					if ((c & 0x01) == 0) {
						ysv_ext1 -= 1;
						dy_ext1 += 1;
					} else {
						ysv_ext0 -= 1;
						dy_ext0 += 1;
					}
				} else {
					ysv_ext0 = ysv_ext1 = ysb + 1;
					dy_ext0 = dy_ext1 = dy0 - 1;
				}

				if ((c & 0x04) == 0) {
					zsv_ext0 = zsb;
					zsv_ext1 = zsb - 1;
					dz_ext0 = dz0;
					dz_ext1 = dz0 + 1;
				} else {
					zsv_ext0 = zsv_ext1 = zsb + 1;
					dz_ext0 = dz_ext1 = dz0 - 1;
				}
			} else { //(0,0,0) is not one of the closest two tetrahedral vertices.
				var c = (aPovar | bPovar)&0xFF; //Our two extra vertices are determined by the closest two.

				if ((c & 0x01) == 0) {
					xsv_ext0 = xsb;
					xsv_ext1 = xsb - 1;
					dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_3D;
					dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D;
				} else {
					xsv_ext0 = xsv_ext1 = xsb + 1;
					dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
					dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
				}

				if ((c & 0x02) == 0) {
					ysv_ext0 = ysb;
					ysv_ext1 = ysb - 1;
					dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_3D;
					dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D;
				} else {
					ysv_ext0 = ysv_ext1 = ysb + 1;
					dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
					dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
				}

				if ((c & 0x04) == 0) {
					zsv_ext0 = zsb;
					zsv_ext1 = zsb - 1;
					dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_3D;
					dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D;
				} else {
					zsv_ext0 = zsv_ext1 = zsb + 1;
					dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
					dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
				}
			}

			//Contribution (0,0,0)
			var attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
			if (attn0 > 0) {
				attn0 *= attn0;
			    value += attn0 * attn0 * extrapolate3d(xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0, this);
			}

			//Contribution (1,0,0)
			var dx1 = dx0 - 1 - SQUISH_CONSTANT_3D;
			var dy1 = dy0 - 0 - SQUISH_CONSTANT_3D;
			var dz1 = dz0 - 0 - SQUISH_CONSTANT_3D;
			var attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
			if (attn1 > 0) {
				attn1 *= attn1;
				value += attn1 * attn1 * extrapolate3d(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1, this);
			}

			//Contribution (0,1,0)
			var dx2 = dx0 - 0 - SQUISH_CONSTANT_3D;
			var dy2 = dy0 - 1 - SQUISH_CONSTANT_3D;
			var dz2 = dz1;
			var attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
			if (attn2 > 0) {
				attn2 *= attn2;
				value += attn2 * attn2 * extrapolate3d(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2, this);
			}

			//Contribution (0,0,1)
			var dx3 = dx2;
			var dy3 = dy1;
			var dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
			var attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
			if (attn3 > 0) {
				attn3 *= attn3;
				value += attn3 * attn3 * extrapolate3d(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3, this);
			}
		} else if (inSum >= 2) { //We're inside the tetrahedron (3-Simplex) at (1,1,1)

			//Determine which two tetrahedral vertices are the closest, out of (1,1,0), (1,0,1), (0,1,1) but not (1,1,1).
			var aPovar = 0x06;
			var aScore = xins;
			var bPovar = 0x05;
			var bScore = yins;
			if (aScore <= bScore && zins < bScore) {
				bScore = zins;
				bPovar = 0x03;
			} else if (aScore > bScore && zins < aScore) {
				aScore = zins;
				aPovar = 0x03;
			}

			//Now we determine the two lattice povars not part of the tetrahedron that may contribute.
			//This depends on the closest two tetrahedral vertices, including (1,1,1)
			var wins = 3 - inSum;
			if (wins < aScore || wins < bScore) { //(1,1,1) is one of the closest two tetrahedral vertices.
				var c = (bScore < aScore ? bPovar : aPovar); //Our other closest vertex is the closest out of a and b.

				if ((c & 0x01) != 0) {
					xsv_ext0 = xsb + 2;
					xsv_ext1 = xsb + 1;
					dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_3D;
					dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
				} else {
					xsv_ext0 = xsv_ext1 = xsb;
					dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_3D;
				}

				if ((c & 0x02) != 0) {
					ysv_ext0 = ysv_ext1 = ysb + 1;
					dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
					if ((c & 0x01) != 0) {
						ysv_ext1 += 1;
						dy_ext1 -= 1;
					} else {
						ysv_ext0 += 1;
						dy_ext0 -= 1;
					}
				} else {
					ysv_ext0 = ysv_ext1 = ysb;
					dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_3D;
				}

				if ((c & 0x04) != 0) {
					zsv_ext0 = zsb + 1;
					zsv_ext1 = zsb + 2;
					dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
					dz_ext1 = dz0 - 2 - 3 * SQUISH_CONSTANT_3D;
				} else {
					zsv_ext0 = zsv_ext1 = zsb;
					dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_3D;
				}
			} else { //(1,1,1) is not one of the closest two tetrahedral vertices.
				var c = (aPovar & bPovar)&0xFF; //Our two extra vertices are determined by the closest two.

				if ((c & 0x01) != 0) {
					xsv_ext0 = xsb + 1;
					xsv_ext1 = xsb + 2;
					dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
					dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D;
				} else {
					xsv_ext0 = xsv_ext1 = xsb;
					dx_ext0 = dx0 - SQUISH_CONSTANT_3D;
					dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
				}

				if ((c & 0x02) != 0) {
					ysv_ext0 = ysb + 1;
					ysv_ext1 = ysb + 2;
					dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
					dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D;
				} else {
					ysv_ext0 = ysv_ext1 = ysb;
					dy_ext0 = dy0 - SQUISH_CONSTANT_3D;
					dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
				}

				if ((c & 0x04) != 0) {
					zsv_ext0 = zsb + 1;
					zsv_ext1 = zsb + 2;
					dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
					dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D;
				} else {
					zsv_ext0 = zsv_ext1 = zsb;
					dz_ext0 = dz0 - SQUISH_CONSTANT_3D;
					dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
				}
			}

			//Contribution (1,1,0)
			var dx3 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
			var dy3 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
			var dz3 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D;
			var attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
			if (attn3 > 0) {
				attn3 *= attn3;
				value += attn3 * attn3 * extrapolate3d(xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3, this);
			}

			//Contribution (1,0,1)
			var dx2 = dx3;
			var dy2 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D;
			var dz2 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
			var attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
			if (attn2 > 0) {
				attn2 *= attn2;
				value += attn2 * attn2 * extrapolate3d(xsb + 1, ysb + 0, zsb + 1, dx2, dy2, dz2, this);
			}

			//Contribution (0,1,1)
			var dx1 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D;
			var dy1 = dy3;
			var dz1 = dz2;
			var attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
			if (attn1 > 0) {
				attn1 *= attn1;
				value += attn1 * attn1 * extrapolate3d(xsb + 0, ysb + 1, zsb + 1, dx1, dy1, dz1, this);
			}

			//Contribution (1,1,1)
			dx0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
			dy0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
			dz0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
			var attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
			if (attn0 > 0) {
				attn0 *= attn0;
				value += attn0 * attn0 * extrapolate3d(xsb + 1, ysb + 1, zsb + 1, dx0, dy0, dz0, this);
			}
		} else { //We're inside the octahedron (Rectified 3-Simplex) in between.
			var aScore;
			var aPovar;
			var aIsFurtherSide;
			var bScore;
			var bPovar;
			var bIsFurtherSide;

			//Decide between povar (0,0,1) and (1,1,0) as closest
			var p1 = xins + yins;
			if (p1 > 1) {
				aScore = p1 - 1;
				aPovar = 0x03;
				aIsFurtherSide = true;
			} else {
				aScore = 1 - p1;
				aPovar = 0x04;
				aIsFurtherSide = false;
			}

			//Decide between povar (0,1,0) and (1,0,1) as closest
			var p2 = xins + zins;
			if (p2 > 1) {
				bScore = p2 - 1;
				bPovar = 0x05;
				bIsFurtherSide = true;
			} else {
				bScore = 1 - p2;
				bPovar = 0x02;
				bIsFurtherSide = false;
			}

			//The closest out of the two (1,0,0) and (0,1,1) will replace the furthest out of the two decided above, if closer.
			var p3 = yins + zins;
			if (p3 > 1) {
				var score = p3 - 1;
				if (aScore <= bScore && aScore < score) {
					aScore = score;
					aPovar = 0x06;
					aIsFurtherSide = true;
				} else if (aScore > bScore && bScore < score) {
					bScore = score;
					bPovar = 0x06;
					bIsFurtherSide = true;
				}
			} else {
				var score = 1 - p3;
				if (aScore <= bScore && aScore < score) {
					aScore = score;
					aPovar = 0x01;
					aIsFurtherSide = false;
				} else if (aScore > bScore && bScore < score) {
					bScore = score;
					bPovar = 0x01;
					bIsFurtherSide = false;
				}
			}

			//Where each of the two closest povars are determines how the extra two vertices are calculated.
			if (aIsFurtherSide == bIsFurtherSide) {
				if (aIsFurtherSide) { //Both closest povars on (1,1,1) side

					//One of the two extra povars is (1,1,1)
					dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D;
					dy_ext0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D;
					dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D;
					xsv_ext0 = xsb + 1;
					ysv_ext0 = ysb + 1;
					zsv_ext0 = zsb + 1;

					//Other extra povar is based on the shared axis.
					var c = (aPovar & bPovar)&0xFF;
					if ((c & 0x01) != 0) {
						dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D;
						dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
						dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
						xsv_ext1 = xsb + 2;
						ysv_ext1 = ysb;
						zsv_ext1 = zsb;
					} else if ((c & 0x02) != 0) {
						dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
						dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D;
						dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
						xsv_ext1 = xsb;
						ysv_ext1 = ysb + 2;
						zsv_ext1 = zsb;
					} else {
						dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
						dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
						dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D;
						xsv_ext1 = xsb;
						ysv_ext1 = ysb;
						zsv_ext1 = zsb + 2;
					}
				} else {//Both closest povars on (0,0,0) side

					//One of the two extra povars is (0,0,0)
					dx_ext0 = dx0;
					dy_ext0 = dy0;
					dz_ext0 = dz0;
					xsv_ext0 = xsb;
					ysv_ext0 = ysb;
					zsv_ext0 = zsb;

					//Other extra povar is based on the omitted axis.
					var c = (aPovar | bPovar)&0xFF;
					if ((c & 0x01) == 0) {
						dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D;
						dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
						dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
						xsv_ext1 = xsb - 1;
						ysv_ext1 = ysb + 1;
						zsv_ext1 = zsb + 1;
					} else if ((c & 0x02) == 0) {
						dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
						dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D;
						dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D;
						xsv_ext1 = xsb + 1;
						ysv_ext1 = ysb - 1;
						zsv_ext1 = zsb + 1;
					} else {
						dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D;
						dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D;
						dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D;
						xsv_ext1 = xsb + 1;
						ysv_ext1 = ysb + 1;
						zsv_ext1 = zsb - 1;
					}
				}
			} else { //One povar on (0,0,0) side, one povar on (1,1,1) side
				var c1, c2;
				if (aIsFurtherSide) {
					c1 = aPovar;
					c2 = bPovar;
				} else {
					c1 = bPovar;
					c2 = aPovar;
				}

				//One contribution is a permutation of (1,1,-1)
				if ((c1 & 0x01) == 0) {
					dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_3D;
					dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
					dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
					xsv_ext0 = xsb - 1;
					ysv_ext0 = ysb + 1;
					zsv_ext0 = zsb + 1;
				} else if ((c1 & 0x02) == 0) {
					dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
					dy_ext0 = dy0 + 1 - SQUISH_CONSTANT_3D;
					dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D;
					xsv_ext0 = xsb + 1;
					ysv_ext0 = ysb - 1;
					zsv_ext0 = zsb + 1;
				} else {
					dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D;
					dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D;
					dz_ext0 = dz0 + 1 - SQUISH_CONSTANT_3D;
					xsv_ext0 = xsb + 1;
					ysv_ext0 = ysb + 1;
					zsv_ext0 = zsb - 1;
				}

				//One contribution is a permutation of (0,0,2)
				dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D;
				dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D;
				dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D;
				xsv_ext1 = xsb;
				ysv_ext1 = ysb;
				zsv_ext1 = zsb;
				if ((c2 & 0x01) != 0) {
					dx_ext1 -= 2;
					xsv_ext1 += 2;
				} else if ((c2 & 0x02) != 0) {
					dy_ext1 -= 2;
					ysv_ext1 += 2;
				} else {
					dz_ext1 -= 2;
					zsv_ext1 += 2;
				}
			}

			//Contribution (1,0,0)
			var dx1 = dx0 - 1 - SQUISH_CONSTANT_3D;
			var dy1 = dy0 - 0 - SQUISH_CONSTANT_3D;
			var dz1 = dz0 - 0 - SQUISH_CONSTANT_3D;
			var attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1;
			if (attn1 > 0) {
				attn1 *= attn1;
				value += attn1 * attn1 * extrapolate3d(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1, this);
			}

			//Contribution (0,1,0)
			var dx2 = dx0 - 0 - SQUISH_CONSTANT_3D;
			var dy2 = dy0 - 1 - SQUISH_CONSTANT_3D;
			var dz2 = dz1;
			var attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2;
			if (attn2 > 0) {
				attn2 *= attn2;
				value += attn2 * attn2 * extrapolate3d(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2, this);
			}

			//Contribution (0,0,1)
			var dx3 = dx2;
			var dy3 = dy1;
			var dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
			var attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
			if (attn3 > 0) {
				attn3 *= attn3;
				value += attn3 * attn3 * extrapolate3d(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3, this);
			}

			//Contribution (1,1,0)
			var dx4 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
			var dy4 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
			var dz4 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D;
			var attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4;
			if (attn4 > 0) {
				attn4 *= attn4;
				value += attn4 * attn4 * extrapolate3d(xsb + 1, ysb + 1, zsb + 0, dx4, dy4, dz4, this);
			}

			//Contribution (1,0,1)
			var dx5 = dx4;
			var dy5 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D;
			var dz5 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D;
			var attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5;
			if (attn5 > 0) {
				attn5 *= attn5;
				value += attn5 * attn5 * extrapolate3d(xsb + 1, ysb + 0, zsb + 1, dx5, dy5, dz5, this);
			}

			//Contribution (0,1,1)
			var dx6 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D;
			var dy6 = dy4;
			var dz6 = dz5;
			var attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6;
			if (attn6 > 0) {
				attn6 *= attn6;
				value += attn6 * attn6 * extrapolate3d(xsb + 0, ysb + 1, zsb + 1, dx6, dy6, dz6, this);
			}
		}

		//First extra vertex
		var attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0;
		if (attn_ext0 > 0)
		{
			attn_ext0 *= attn_ext0;
			value += attn_ext0 * attn_ext0 * extrapolate3d(xsv_ext0, ysv_ext0, zsv_ext0, dx_ext0, dy_ext0, dz_ext0, this);
		}

		//Second extra vertex
		var attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1;
		if (attn_ext1 > 0)
		{
			attn_ext1 *= attn_ext1;
			value += attn_ext1 * attn_ext1 * extrapolate3d(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1, this);
		}

		return value / NORM_CONSTANT_3D;

		    };

			NewSimplex.perm = [];
			NewSimplex.permGradIndex3D = [];
			var source = [];
			for (var i = 0; i < 256; i++)
				source[i] = i;
			var myrng = new Math.seedrandom(seed);
			seed=myrng.int32();
			for (var i = 255; i >= 0; i--) {
				seed=myrng.int32();
				var r = ((seed + 31) % (i + 1));
				if (r < 0)
					r += (i + 1);
				NewSimplex.perm[i] = source[r];
				NewSimplex.permGradIndex3D[i] = ((NewSimplex.perm[i] % (gradients3D.length / 3)) * 3);
				source[r] = source[i];
			}
			return NewSimplex;
		}
	}
}();
