Skip to content

Instantly share code, notes, and snippets.

@loganhasson
Forked from KdotJPG/OpenSimplex2S.java
Created November 1, 2015 22:26

Revisions

  1. @KdotJPG KdotJPG revised this gist Jan 5, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    * OpenSimplex (Simplectic) Noise in Java.
    * OpenSimplex Noise in Java.
    * by Kurt Spencer
    *
    * v1.1 (October 5, 2014)
    @@ -69,7 +69,7 @@ public OpenSimplexNoise(long seed) {
    }
    }

    //2D OpenSimplex (Simplectic) Noise.
    //2D OpenSimplex Noise.
    public double eval(double x, double y) {

    //Place input coordinates onto grid.
    @@ -184,7 +184,7 @@ public double eval(double x, double y) {
    return value / NORM_CONSTANT_2D;
    }

    //3D OpenSimplex (Simplectic) Noise.
    //3D OpenSimplex Noise.
    public double eval(double x, double y, double z) {

    //Place input coordinates on simplectic honeycomb.
    @@ -745,7 +745,7 @@ public double eval(double x, double y, double z) {
    return value / NORM_CONSTANT_3D;
    }

    //4D OpenSimplex (Simplectic) Noise.
    //4D OpenSimplex Noise.
    public double eval(double x, double y, double z, double w) {

    //Place input coordinates on simplectic honeycomb.
  2. @KdotJPG KdotJPG revised this gist Oct 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -768,7 +768,7 @@ public double eval(double x, double y, double z, double w) {
    double zb = zsb + squishOffset;
    double wb = wsb + squishOffset;

    //Compute simplectic honeycomb coordinates relative to rhombohedral origin.
    //Compute simplectic honeycomb coordinates relative to rhombo-hypercube origin.
    double xins = xs - xsb;
    double yins = ys - ysb;
    double zins = zs - zsb;
  3. @KdotJPG KdotJPG revised this gist Oct 6, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -1277,7 +1277,7 @@ public double eval(double x, double y, double z, double w) {
    aIsBiggerSide = false;
    }

    //Where each of the two closest points are determines how the extra two or three vertices are calculated.
    //Where each of the two closest points are determines how the extra three vertices are calculated.
    if (aIsBiggerSide == bIsBiggerSide) {
    if (aIsBiggerSide) { //Both closest points on the bigger side
    byte c1 = (byte)(aPoint | bPoint);
    @@ -1706,7 +1706,7 @@ public double eval(double x, double y, double z, double w) {
    aIsBiggerSide = false;
    }

    //Where each of the two closest points are determines how the extra two or three vertices are calculated.
    //Where each of the two closest points are determines how the extra three vertices are calculated.
    if (aIsBiggerSide == bIsBiggerSide) {
    if (aIsBiggerSide) { //Both closest points on the bigger side
    byte c1 = (byte)(aPoint & bPoint);
  4. @KdotJPG KdotJPG revised this gist Oct 6, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,9 @@
    * rhyme and reason behind it.
    * - Removed default permutation array in favor of
    * default seed.
    * - Changed seed-based constructor to be independent
    * of any particular randomization library, so results
    * will be the same when ported to other languages.
    */

    public class OpenSimplexNoise {
  5. @KdotJPG KdotJPG revised this gist Oct 6, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -13,12 +13,12 @@

    public class OpenSimplexNoise {

    private static final double STRETCH_CONSTANT_2D = -0.211324865405187; //(1/Math.sqrt(2+1)-1)/2;
    private static final double SQUISH_CONSTANT_2D = 0.366025403784439; //(Math.sqrt(2+1)-1)/2;
    private static final double STRETCH_CONSTANT_3D = -1.0 / 6; //(1/Math.sqrt(3+1)-1)/3;
    private static final double SQUISH_CONSTANT_3D = 1.0 / 3; //(Math.sqrt(3+1)-1)/3;
    private static final double STRETCH_CONSTANT_4D = -0.138196601125011; //(1/Math.sqrt(4+1)-1)/4;
    private static final double SQUISH_CONSTANT_4D = 0.309016994374947; //(Math.sqrt(4+1)-1)/4;
    private static final double STRETCH_CONSTANT_2D = -0.211324865405187; //(1/Math.sqrt(2+1)-1)/2;
    private static final double SQUISH_CONSTANT_2D = 0.366025403784439; //(Math.sqrt(2+1)-1)/2;
    private static final double STRETCH_CONSTANT_3D = -1.0 / 6; //(1/Math.sqrt(3+1)-1)/3;
    private static final double SQUISH_CONSTANT_3D = 1.0 / 3; //(Math.sqrt(3+1)-1)/3;
    private static final double STRETCH_CONSTANT_4D = -0.138196601125011; //(1/Math.sqrt(4+1)-1)/4;
    private static final double SQUISH_CONSTANT_4D = 0.309016994374947; //(Math.sqrt(4+1)-1)/4;

    private static final double NORM_CONSTANT_2D = 47;
    private static final double NORM_CONSTANT_3D = 103;
    @@ -38,7 +38,7 @@ public OpenSimplexNoise(short[] perm) {
    permGradIndex3D = new short[256];

    for (int i = 0; i < 256; i++) {
    //Since 3D has 24 gradients, simple bitmask won't work, so precompute modulo array.
    //Since 3D has 24 gradients, simple bitmask won't work, so precompute modulo array.
    permGradIndex3D[i] = (short)((perm[i] % (gradients3D.length / 3)) * 3);
    }
    }
  6. @KdotJPG KdotJPG revised this gist Oct 6, 2014. 1 changed file with 1563 additions and 86 deletions.
    1,649 changes: 1,563 additions & 86 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -1,56 +1,196 @@
    /*
    * OpenSimplex (Simplectic) Noise in Java.
    * (v1.0.1 With new gradient set and corresponding normalization factor, 9/19/14)
    * by Kurt Spencer
    *
    * v1.1 (October 5, 2014)
    * - Added 2D and 4D implementations.
    * - Proper gradient sets for all dimensions, from a
    * dimensionally-generalizable scheme with an actual
    * rhyme and reason behind it.
    * - Removed default permutation array in favor of
    * default seed.
    */

    public class OpenSimplexNoise {

    private static final double STRETCH_CONSTANT_3D = -1.0 / 6;
    private static final double SQUISH_CONSTANT_3D = 1.0 / 3;
    private static final double STRETCH_CONSTANT_2D = -0.211324865405187; //(1/Math.sqrt(2+1)-1)/2;
    private static final double SQUISH_CONSTANT_2D = 0.366025403784439; //(Math.sqrt(2+1)-1)/2;
    private static final double STRETCH_CONSTANT_3D = -1.0 / 6; //(1/Math.sqrt(3+1)-1)/3;
    private static final double SQUISH_CONSTANT_3D = 1.0 / 3; //(Math.sqrt(3+1)-1)/3;
    private static final double STRETCH_CONSTANT_4D = -0.138196601125011; //(1/Math.sqrt(4+1)-1)/4;
    private static final double SQUISH_CONSTANT_4D = 0.309016994374947; //(Math.sqrt(4+1)-1)/4;

    private static final double NORM_CONSTANT_2D = 47;
    private static final double NORM_CONSTANT_3D = 103;
    private static final double NORM_CONSTANT_4D = 30;

    private static final long DEFAULT_SEED = 0;

    private short[] perm;
    private short[] permGradIndex3D;

    public OpenSimplexNoise() {
    this(PERM_DEFAULT);
    this(DEFAULT_SEED);
    }

    public OpenSimplexNoise(short[] perm) {
    this.perm = perm;
    permGradIndex3D = new short[256];

    for (int i = 0; i < 256; i++) {
    //Since 3D has 24 gradients, simple bitmask won't work, so precompute modulo array.
    permGradIndex3D[i] = (short)((perm[i] % (gradients3D.length / 3)) * 3);
    }
    }

    //Initializes the class using a permutation array generated from a 64-bit seed.
    //Generates a proper permutation (i.e. doesn't merely perform N successive pair swaps on a base array)
    //Uses java.util.Random
    //Uses a simple 64-bit LCG.
    public OpenSimplexNoise(long seed) {
    perm = new short[256];
    permGradIndex3D = new short[256];
    short[] source = new short[256];
    for (short i = 0; i < 256; i++)
    source[i] = i;
    java.util.Random random = new java.util.Random(seed);
    seed = seed * 6364136223846793005l + 1442695040888963407l;
    seed = seed * 6364136223846793005l + 1442695040888963407l;
    seed = seed * 6364136223846793005l + 1442695040888963407l;
    for (int i = 255; i >= 0; i--) {
    int r = random.nextInt(i + 1);
    seed = seed * 6364136223846793005l + 1442695040888963407l;
    int r = (int)((seed + 31) % (i + 1));
    if (r < 0)
    r += (i + 1);
    perm[i] = source[r];
    permGradIndex3D[i] = (short)((perm[i] % (gradients3D.length / 3)) * 3);
    source[r] = source[i];
    }
    }

    //2D OpenSimplex (Simplectic) Noise.
    public double eval(double x, double y) {

    //Place input coordinates onto grid.
    double stretchOffset = (x + y) * STRETCH_CONSTANT_2D;
    double xs = x + stretchOffset;
    double ys = y + stretchOffset;

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

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

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

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

    //Positions relative to origin point.
    double dx0 = x - xb;
    double dy0 = y - yb;

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

    double value = 0;

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

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

    if (inSum <= 1) { //We're inside the triangle (2-Simplex) at (0,0)
    double 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)
    double 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)
    double attn0 = 2 - dx0 * dx0 - dy0 * dy0;
    if (attn0 > 0) {
    attn0 *= attn0;
    value += attn0 * attn0 * extrapolate(xsb, ysb, dx0, dy0);
    }

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

    return value / NORM_CONSTANT_2D;
    }

    //3D OpenSimplex (Simplectic) Noise.
    public double eval(double x, double y, double z) {

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

    //Floor to get simplectic lattice coordinates of rhombohedron (stretched cube) super-cell origin.
    //Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
    int xsb = fastFloor(xs);
    int ysb = fastFloor(ys);
    int zsb = fastFloor(zs);
    @@ -61,12 +201,12 @@ public double eval(double x, double y, double z) {
    double yb = ysb + squishOffset;
    double zb = zsb + squishOffset;

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

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

    //Positions relative to origin point.
    @@ -81,7 +221,7 @@ public double eval(double x, double y, double z) {
    int xsv_ext1, ysv_ext1, zsv_ext1;

    double value = 0;
    if (inSum <= 1) { //We're inside the Tetrahedron (3-Simplex) at (0,0,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.
    byte aPoint = 0x01;
    @@ -137,7 +277,7 @@ public double eval(double x, double y, double z) {
    dz_ext0 = dz_ext1 = dz0 - 1;
    }
    } else { //(0,0,0) is not one of the closest two tetrahedral vertices.
    byte c = (byte)(aPoint | bPoint); //Our two extra vertices are determined by the closest two.
    byte c = (byte)(aPoint | bPoint); //Our two extra vertices are determined by the closest two.

    if ((c & 0x01) == 0) {
    xsv_ext0 = xsb;
    @@ -177,10 +317,10 @@ public double eval(double x, double y, double z) {
    double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
    if (attn0 > 0) {
    attn0 *= attn0;
    value = attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0);
    value += attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0);
    }

    //Contribution (0,0,1)
    //Contribution (1,0,0)
    double dx1 = dx0 - 1 - SQUISH_CONSTANT_3D;
    double dy1 = dy0 - 0 - SQUISH_CONSTANT_3D;
    double dz1 = dz0 - 0 - SQUISH_CONSTANT_3D;
    @@ -200,7 +340,7 @@ public double eval(double x, double y, double z) {
    value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2);
    }

    //Contribution (1,0,0)
    //Contribution (0,0,1)
    double dx3 = dx2;
    double dy3 = dy1;
    double dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
    @@ -209,7 +349,7 @@ public double eval(double x, double y, double z) {
    attn3 *= attn3;
    value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3);
    }
    } else if (inSum >= 2) { //We're inside the Tetrahedron (3-Simplex) at (1,1,1)
    } 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).
    byte aPoint = 0x06;
    @@ -265,7 +405,7 @@ public double eval(double x, double y, double z) {
    dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_3D;
    }
    } else { //(1,1,1) is not one of the closest two tetrahedral vertices.
    byte c = (byte)(aPoint & bPoint); //Our two extra vertices are determined by the closest two.
    byte c = (byte)(aPoint & bPoint); //Our two extra vertices are determined by the closest two.

    if ((c & 0x01) != 0) {
    xsv_ext0 = xsb + 1;
    @@ -308,7 +448,7 @@ public double eval(double x, double y, double z) {
    double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
    if (attn3 > 0) {
    attn3 *= attn3;
    value = attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3);
    value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3);
    }

    //Contribution (1,0,1)
    @@ -340,15 +480,15 @@ public double eval(double x, double y, double z) {
    attn0 *= attn0;
    value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, dx0, dy0, dz0);
    }
    } else { //We're inside the Octahedron (Rectified 3-Simplex) in between.
    } else { //We're inside the octahedron (Rectified 3-Simplex) in between.
    double aScore;
    byte aPoint;
    boolean aIsFurtherSide;
    double bScore;
    byte bPoint;
    boolean bIsFurtherSide;

    //Decide between point (1,0,0) and (0,1,1) as closest
    //Decide between point (0,0,1) and (1,1,0) as closest
    double p1 = xins + yins;
    if (p1 > 1) {
    aScore = p1 - 1;
    @@ -372,7 +512,7 @@ public double eval(double x, double y, double z) {
    bIsFurtherSide = false;
    }

    //The closest out of the two (0,0,1) and (1,1,0) will replace the furthest out of the two decided above, if closer.
    //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.
    double p3 = yins + zins;
    if (p3 > 1) {
    double score = p3 - 1;
    @@ -504,38 +644,32 @@ public double eval(double x, double y, double z) {
    }

    //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 = 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;
    dx_ext1 -= 2;
    xsv_ext1 += 2;
    } else if ((c2 & 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;
    dy_ext1 -= 2;
    ysv_ext1 += 2;
    } 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;
    dz_ext1 -= 2;
    zsv_ext1 += 2;
    }
    }

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

    //Contribution (0,1,0)
    @@ -548,7 +682,7 @@ public double eval(double x, double y, double z) {
    value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2);
    }

    //Contribution (1,0,0)
    //Contribution (0,0,1)
    double dx3 = dx2;
    double dy3 = dy1;
    double dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
    @@ -604,54 +738,1397 @@ public double eval(double x, double y, double z) {
    attn_ext1 *= attn_ext1;
    value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1);
    }

    //Normalization constant tested using over 4 billion evaluations to bound range within [-1,1].
    //This is a safe upper bound. Actual min/max values found over the course of the 4 billion
    //evaluations were -28.12974224468639 (min) and 28.134269887817773 (max).
    return value / 28.25;

    return value / NORM_CONSTANT_3D;
    }

    private double extrapolate(int xsb, int ysb, int zsb, double dx, double dy, double dz)
    {
    short index = permGradIndex3D[(perm[(perm[xsb & 0xFF] + ysb) & 0xFF] + zsb) & 0xFF];
    return gradients3D[index] * dx
    + gradients3D[index + 1] * dy
    + gradients3D[index + 2] * dz;
    }
    //4D OpenSimplex (Simplectic) Noise.
    public double eval(double x, double y, double z, double w) {

    private static int fastFloor(double x) {
    int xi = (int)x;
    return x < xi ? xi - 1 : xi;
    }
    //Place input coordinates on simplectic honeycomb.
    double stretchOffset = (x + y + z + w) * STRETCH_CONSTANT_4D;
    double xs = x + stretchOffset;
    double ys = y + stretchOffset;
    double zs = z + stretchOffset;
    double ws = w + stretchOffset;

    //Floor to get simplectic honeycomb coordinates of rhombo-hypercube super-cell origin.
    int xsb = fastFloor(xs);
    int ysb = fastFloor(ys);
    int zsb = fastFloor(zs);
    int wsb = fastFloor(ws);

    //Skew out to get actual coordinates of stretched rhombo-hypercube origin. We'll need these later.
    double squishOffset = (xsb + ysb + zsb + wsb) * SQUISH_CONSTANT_4D;
    double xb = xsb + squishOffset;
    double yb = ysb + squishOffset;
    double zb = zsb + squishOffset;
    double wb = wsb + squishOffset;

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

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

    //Positions relative to origin point.
    double dx0 = x - xb;
    double dy0 = y - yb;
    double dz0 = z - zb;
    double dw0 = w - wb;

    //We'll be defining these inside the next block and using them afterwards.
    double dx_ext0, dy_ext0, dz_ext0, dw_ext0;
    double dx_ext1, dy_ext1, dz_ext1, dw_ext1;
    double dx_ext2, dy_ext2, dz_ext2, dw_ext2;
    int xsv_ext0, ysv_ext0, zsv_ext0, wsv_ext0;
    int xsv_ext1, ysv_ext1, zsv_ext1, wsv_ext1;
    int xsv_ext2, ysv_ext2, zsv_ext2, wsv_ext2;

    double value = 0;
    if (inSum <= 1) { //We're inside the pentachoron (4-Simplex) at (0,0,0,0)

    //Determine which two of (0,0,0,1), (0,0,1,0), (0,1,0,0), (1,0,0,0) are closest.
    byte aPoint = 0x01;
    double aScore = xins;
    byte bPoint = 0x02;
    double bScore = yins;
    if (aScore >= bScore && zins > bScore) {
    bScore = zins;
    bPoint = 0x04;
    } else if (aScore < bScore && zins > aScore) {
    aScore = zins;
    aPoint = 0x04;
    }
    if (aScore >= bScore && wins > bScore) {
    bScore = wins;
    bPoint = 0x08;
    } else if (aScore < bScore && wins > aScore) {
    aScore = wins;
    aPoint = 0x08;
    }

    //Now we determine the three lattice points not part of the pentachoron that may contribute.
    //This depends on the closest two pentachoron vertices, including (0,0,0,0)
    double uins = 1 - inSum;
    if (uins > aScore || uins > bScore) { //(0,0,0,0) is one of the closest two pentachoron vertices.
    byte c = (bScore > aScore ? bPoint : aPoint); //Our other closest vertex is the closest out of a and b.
    if ((c & 0x01) == 0) {
    xsv_ext0 = xsb - 1;
    xsv_ext1 = xsv_ext2 = xsb;
    dx_ext0 = dx0 + 1;
    dx_ext1 = dx_ext2 = dx0;
    } else {
    xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb + 1;
    dx_ext0 = dx_ext1 = dx_ext2 = dx0 - 1;
    }

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

    if ((c & 0x04) == 0) {
    zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb;
    dz_ext0 = dz_ext1 = dz_ext2 = dz0;
    if ((c & 0x03) != 0) {
    if ((c & 0x03) == 0x03) {
    zsv_ext0 -= 1;
    dz_ext0 += 1;
    } else {
    zsv_ext1 -= 1;
    dz_ext1 += 1;
    }
    } else {
    zsv_ext2 -= 1;
    dz_ext2 += 1;
    }
    } else {
    zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1;
    dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 1;
    }

    if ((c & 0x08) == 0) {
    wsv_ext0 = wsv_ext1 = wsb;
    wsv_ext2 = wsb - 1;
    dw_ext0 = dw_ext1 = dw0;
    dw_ext2 = dw0 + 1;
    } else {
    wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb + 1;
    dw_ext0 = dw_ext1 = dw_ext2 = dw0 - 1;
    }
    } else { //(0,0,0,0) is not one of the closest two pentachoron vertices.
    byte c = (byte)(aPoint | bPoint); //Our three extra vertices are determined by the closest two.

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

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

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

    if ((c & 0x08) == 0) {
    wsv_ext0 = wsv_ext1 = wsb;
    wsv_ext2 = wsb - 1;
    dw_ext0 = dw0 - 2 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 - SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 + 1 - SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb + 1;
    dw_ext0 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw_ext2 = dw0 - 1 - SQUISH_CONSTANT_4D;
    }
    }

    //Contribution (0,0,0,0)
    double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0 - dw0 * dw0;
    if (attn0 > 0) {
    attn0 *= attn0;
    value += attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 0, dx0, dy0, dz0, dw0);
    }

    //Contribution (1,0,0,0)
    double dx1 = dx0 - 1 - SQUISH_CONSTANT_4D;
    double dy1 = dy0 - 0 - SQUISH_CONSTANT_4D;
    double dz1 = dz0 - 0 - SQUISH_CONSTANT_4D;
    double dw1 = dw0 - 0 - SQUISH_CONSTANT_4D;
    double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1;
    if (attn1 > 0) {
    attn1 *= attn1;
    value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 0, dx1, dy1, dz1, dw1);
    }

    //Contribution (0,1,0,0)
    double dx2 = dx0 - 0 - SQUISH_CONSTANT_4D;
    double dy2 = dy0 - 1 - SQUISH_CONSTANT_4D;
    double dz2 = dz1;
    double dw2 = dw1;
    double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2;
    if (attn2 > 0) {
    attn2 *= attn2;
    value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 0, dx2, dy2, dz2, dw2);
    }

    //Contribution (0,0,1,0)
    double dx3 = dx2;
    double dy3 = dy1;
    double dz3 = dz0 - 1 - SQUISH_CONSTANT_4D;
    double dw3 = dw1;
    double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3;
    if (attn3 > 0) {
    attn3 *= attn3;
    value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 0, dx3, dy3, dz3, dw3);
    }

    //Contribution (0,0,0,1)
    double dx4 = dx2;
    double dy4 = dy1;
    double dz4 = dz1;
    double dw4 = dw0 - 1 - SQUISH_CONSTANT_4D;
    double attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4;
    if (attn4 > 0) {
    attn4 *= attn4;
    value += attn4 * attn4 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 1, dx4, dy4, dz4, dw4);
    }
    } else if (inSum >= 3) { //We're inside the pentachoron (4-Simplex) at (1,1,1,1)
    //Determine which two of (1,1,1,0), (1,1,0,1), (1,0,1,1), (0,1,1,1) are closest.
    byte aPoint = 0x0E;
    double aScore = xins;
    byte bPoint = 0x0D;
    double bScore = yins;
    if (aScore <= bScore && zins < bScore) {
    bScore = zins;
    bPoint = 0x0B;
    } else if (aScore > bScore && zins < aScore) {
    aScore = zins;
    aPoint = 0x0B;
    }
    if (aScore <= bScore && wins < bScore) {
    bScore = wins;
    bPoint = 0x07;
    } else if (aScore > bScore && wins < aScore) {
    aScore = wins;
    aPoint = 0x07;
    }

    //Now we determine the three lattice points not part of the pentachoron that may contribute.
    //This depends on the closest two pentachoron vertices, including (0,0,0,0)
    double uins = 4 - inSum;
    if (uins < aScore || uins < bScore) { //(1,1,1,1) is one of the closest two pentachoron vertices.
    byte c = (bScore < aScore ? bPoint : aPoint); //Our other closest vertex is the closest out of a and b.

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

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

    if ((c & 0x04) != 0) {
    zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1;
    dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D;
    if ((c & 0x03) != 0x03) {
    if ((c & 0x03) == 0) {
    zsv_ext0 += 1;
    dz_ext0 -= 1;
    } else {
    zsv_ext1 += 1;
    dz_ext1 -= 1;
    }
    } else {
    zsv_ext2 += 1;
    dz_ext2 -= 1;
    }
    } else {
    zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb;
    dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 4 * SQUISH_CONSTANT_4D;
    }

    if ((c & 0x08) != 0) {
    wsv_ext0 = wsv_ext1 = wsb + 1;
    wsv_ext2 = wsb + 2;
    dw_ext0 = dw_ext1 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 - 2 - 4 * SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb;
    dw_ext0 = dw_ext1 = dw_ext2 = dw0 - 4 * SQUISH_CONSTANT_4D;
    }
    } else { //(1,1,1,1) is not one of the closest two pentachoron vertices.
    byte c = (byte)(aPoint & bPoint); //Our three extra vertices are determined by the closest two.

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

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

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

    if ((c & 0x08) != 0) {
    wsv_ext0 = wsv_ext1 = wsb + 1;
    wsv_ext2 = wsb + 2;
    dw_ext0 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb;
    dw_ext0 = dw0 - 2 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw_ext2 = dw0 - 3 * SQUISH_CONSTANT_4D;
    }
    }

    //Contribution (1,1,1,0)
    double dx4 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double dy4 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double dz4 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double dw4 = dw0 - 3 * SQUISH_CONSTANT_4D;
    double attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4;
    if (attn4 > 0) {
    attn4 *= attn4;
    value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 0, dx4, dy4, dz4, dw4);
    }

    //Contribution (1,1,0,1)
    double dx3 = dx4;
    double dy3 = dy4;
    double dz3 = dz0 - 3 * SQUISH_CONSTANT_4D;
    double dw3 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3;
    if (attn3 > 0) {
    attn3 *= attn3;
    value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 1, dx3, dy3, dz3, dw3);
    }

    //Contribution (1,0,1,1)
    double dx2 = dx4;
    double dy2 = dy0 - 3 * SQUISH_CONSTANT_4D;
    double dz2 = dz4;
    double dw2 = dw3;
    double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2;
    if (attn2 > 0) {
    attn2 *= attn2;
    value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 1, dx2, dy2, dz2, dw2);
    }

    //Contribution (0,1,1,1)
    double dx1 = dx0 - 3 * SQUISH_CONSTANT_4D;
    double dz1 = dz4;
    double dy1 = dy4;
    double dw1 = dw3;
    double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1;
    if (attn1 > 0) {
    attn1 *= attn1;
    value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 1, dx1, dy1, dz1, dw1);
    }

    //Contribution (1,1,1,1)
    dx0 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D;
    dy0 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D;
    dz0 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D;
    dw0 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D;
    double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0 - dw0 * dw0;
    if (attn0 > 0) {
    attn0 *= attn0;
    value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 1, dx0, dy0, dz0, dw0);
    }
    } else if (inSum <= 2) { //We're inside the first dispentachoron (Rectified 4-Simplex)
    double aScore;
    byte aPoint;
    boolean aIsBiggerSide = true;
    double bScore;
    byte bPoint;
    boolean bIsBiggerSide = true;

    //Decide between (1,1,0,0) and (0,0,1,1)
    if (xins + yins > zins + wins) {
    aScore = xins + yins;
    aPoint = 0x03;
    } else {
    aScore = zins + wins;
    aPoint = 0x0C;
    }

    //Decide between (1,0,1,0) and (0,1,0,1)
    if (xins + zins > yins + wins) {
    bScore = xins + zins;
    bPoint = 0x05;
    } else {
    bScore = yins + wins;
    bPoint = 0x0A;
    }

    //Closer between (1,0,0,1) and (0,1,1,0) will replace the further of a and b, if closer.
    if (xins + wins > yins + zins) {
    double score = xins + wins;
    if (aScore >= bScore && score > bScore) {
    bScore = score;
    bPoint = 0x09;
    } else if (aScore < bScore && score > aScore) {
    aScore = score;
    aPoint = 0x09;
    }
    } else {
    double score = yins + zins;
    if (aScore >= bScore && score > bScore) {
    bScore = score;
    bPoint = 0x06;
    } else if (aScore < bScore && score > aScore) {
    aScore = score;
    aPoint = 0x06;
    }
    }

    //Decide if (1,0,0,0) is closer.
    double p1 = 2 - inSum + xins;
    if (aScore >= bScore && p1 > bScore) {
    bScore = p1;
    bPoint = 0x01;
    bIsBiggerSide = false;
    } else if (aScore < bScore && p1 > aScore) {
    aScore = p1;
    aPoint = 0x01;
    aIsBiggerSide = false;
    }

    //Decide if (0,1,0,0) is closer.
    double p2 = 2 - inSum + yins;
    if (aScore >= bScore && p2 > bScore) {
    bScore = p2;
    bPoint = 0x02;
    bIsBiggerSide = false;
    } else if (aScore < bScore && p2 > aScore) {
    aScore = p2;
    aPoint = 0x02;
    aIsBiggerSide = false;
    }

    //Decide if (0,0,1,0) is closer.
    double p3 = 2 - inSum + zins;
    if (aScore >= bScore && p3 > bScore) {
    bScore = p3;
    bPoint = 0x04;
    bIsBiggerSide = false;
    } else if (aScore < bScore && p3 > aScore) {
    aScore = p3;
    aPoint = 0x04;
    aIsBiggerSide = false;
    }

    //Decide if (0,0,0,1) is closer.
    double p4 = 2 - inSum + wins;
    if (aScore >= bScore && p4 > bScore) {
    bScore = p4;
    bPoint = 0x08;
    bIsBiggerSide = false;
    } else if (aScore < bScore && p4 > aScore) {
    aScore = p4;
    aPoint = 0x08;
    aIsBiggerSide = false;
    }

    //Where each of the two closest points are determines how the extra two or three vertices are calculated.
    if (aIsBiggerSide == bIsBiggerSide) {
    if (aIsBiggerSide) { //Both closest points on the bigger side
    byte c1 = (byte)(aPoint | bPoint);
    byte c2 = (byte)(aPoint & bPoint);
    if ((c1 & 0x01) == 0) {
    xsv_ext0 = xsb;
    xsv_ext1 = xsb - 1;
    dx_ext0 = dx0 - 3 * SQUISH_CONSTANT_4D;
    dx_ext1 = dx0 + 1 - 2 * SQUISH_CONSTANT_4D;
    } else {
    xsv_ext0 = xsv_ext1 = xsb + 1;
    dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D;
    dx_ext1 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    }

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

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

    if ((c1 & 0x08) == 0) {
    wsv_ext0 = wsb;
    wsv_ext1 = wsb - 1;
    dw_ext0 = dw0 - 3 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 + 1 - 2 * SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsb + 1;
    dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    }

    //One combination is a permutation of (0,0,0,2) based on c2
    xsv_ext2 = xsb;
    ysv_ext2 = ysb;
    zsv_ext2 = zsb;
    wsv_ext2 = wsb;
    dx_ext2 = dx0 - 2 * SQUISH_CONSTANT_4D;
    dy_ext2 = dy0 - 2 * SQUISH_CONSTANT_4D;
    dz_ext2 = dz0 - 2 * SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 - 2 * SQUISH_CONSTANT_4D;
    if ((c2 & 0x01) != 0) {
    xsv_ext2 += 2;
    dx_ext2 -= 2;
    } else if ((c2 & 0x02) != 0) {
    ysv_ext2 += 2;
    dy_ext2 -= 2;
    } else if ((c2 & 0x04) != 0) {
    zsv_ext2 += 2;
    dz_ext2 -= 2;
    } else {
    wsv_ext2 += 2;
    dw_ext2 -= 2;
    }

    } else { //Both closest points on the smaller side
    //One of the two extra points is (0,0,0,0)
    xsv_ext2 = xsb;
    ysv_ext2 = ysb;
    zsv_ext2 = zsb;
    wsv_ext2 = wsb;
    dx_ext2 = dx0;
    dy_ext2 = dy0;
    dz_ext2 = dz0;
    dw_ext2 = dw0;

    //Other two points are based on the omitted axes.
    byte c = (byte)(aPoint | bPoint);

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

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

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

    if ((c & 0x08) == 0)
    {
    wsv_ext0 = wsb;
    wsv_ext1 = wsb - 1;
    dw_ext0 = dw0 - SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 + 1 - SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsb + 1;
    dw_ext0 = dw_ext1 = dw0 - 1 - SQUISH_CONSTANT_4D;
    }

    }
    } else { //One point on each "side"
    byte c1, c2;
    if (aIsBiggerSide) {
    c1 = aPoint;
    c2 = bPoint;
    } else {
    c1 = bPoint;
    c2 = aPoint;
    }

    //Two contributions are the bigger-sided point with each 0 replaced with -1.
    if ((c1 & 0x01) == 0) {
    xsv_ext0 = xsb - 1;
    xsv_ext1 = xsb;
    dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_4D;
    dx_ext1 = dx0 - SQUISH_CONSTANT_4D;
    } else {
    xsv_ext0 = xsv_ext1 = xsb + 1;
    dx_ext0 = dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_4D;
    }

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

    if ((c1 & 0x04) == 0) {
    zsv_ext0 = zsv_ext1 = zsb;
    dz_ext0 = dz_ext1 = dz0 - SQUISH_CONSTANT_4D;
    if ((c1 & 0x03) == 0x03) {
    zsv_ext0 -= 1;
    dz_ext0 += 1;
    } else {
    zsv_ext1 -= 1;
    dz_ext1 += 1;
    }
    } else {
    zsv_ext0 = zsv_ext1 = zsb + 1;
    dz_ext0 = dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_4D;
    }

    if ((c1 & 0x08) == 0) {
    wsv_ext0 = wsb;
    wsv_ext1 = wsb - 1;
    dw_ext0 = dw0 - SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 + 1 - SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsb + 1;
    dw_ext0 = dw_ext1 = dw0 - 1 - SQUISH_CONSTANT_4D;
    }

    //One contribution is a permutation of (0,0,0,2) based on the smaller-sided point
    xsv_ext2 = xsb;
    ysv_ext2 = ysb;
    zsv_ext2 = zsb;
    wsv_ext2 = wsb;
    dx_ext2 = dx0 - 2 * SQUISH_CONSTANT_4D;
    dy_ext2 = dy0 - 2 * SQUISH_CONSTANT_4D;
    dz_ext2 = dz0 - 2 * SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 - 2 * SQUISH_CONSTANT_4D;
    if ((c2 & 0x01) != 0) {
    xsv_ext2 += 2;
    dx_ext2 -= 2;
    } else if ((c2 & 0x02) != 0) {
    ysv_ext2 += 2;
    dy_ext2 -= 2;
    } else if ((c2 & 0x04) != 0) {
    zsv_ext2 += 2;
    dz_ext2 -= 2;
    } else {
    wsv_ext2 += 2;
    dw_ext2 -= 2;
    }
    }

    //Contribution (1,0,0,0)
    double dx1 = dx0 - 1 - SQUISH_CONSTANT_4D;
    double dy1 = dy0 - 0 - SQUISH_CONSTANT_4D;
    double dz1 = dz0 - 0 - SQUISH_CONSTANT_4D;
    double dw1 = dw0 - 0 - SQUISH_CONSTANT_4D;
    double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1;
    if (attn1 > 0) {
    attn1 *= attn1;
    value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 0, dx1, dy1, dz1, dw1);
    }

    //Contribution (0,1,0,0)
    double dx2 = dx0 - 0 - SQUISH_CONSTANT_4D;
    double dy2 = dy0 - 1 - SQUISH_CONSTANT_4D;
    double dz2 = dz1;
    double dw2 = dw1;
    double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2;
    if (attn2 > 0) {
    attn2 *= attn2;
    value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 0, dx2, dy2, dz2, dw2);
    }

    //Contribution (0,0,1,0)
    double dx3 = dx2;
    double dy3 = dy1;
    double dz3 = dz0 - 1 - SQUISH_CONSTANT_4D;
    double dw3 = dw1;
    double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3;
    if (attn3 > 0) {
    attn3 *= attn3;
    value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 0, dx3, dy3, dz3, dw3);
    }

    //Contribution (0,0,0,1)
    double dx4 = dx2;
    double dy4 = dy1;
    double dz4 = dz1;
    double dw4 = dw0 - 1 - SQUISH_CONSTANT_4D;
    double attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4;
    if (attn4 > 0) {
    attn4 *= attn4;
    value += attn4 * attn4 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 1, dx4, dy4, dz4, dw4);
    }

    //Contribution (1,1,0,0)
    double dx5 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dy5 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dz5 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dw5 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5 - dw5 * dw5;
    if (attn5 > 0) {
    attn5 *= attn5;
    value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 0, dx5, dy5, dz5, dw5);
    }

    //Contribution (1,0,1,0)
    double dx6 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dy6 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dz6 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dw6 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6 - dw6 * dw6;
    if (attn6 > 0) {
    attn6 *= attn6;
    value += attn6 * attn6 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 0, dx6, dy6, dz6, dw6);
    }

    //Contribution (1,0,0,1)
    double dx7 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dy7 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dz7 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dw7 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double attn7 = 2 - dx7 * dx7 - dy7 * dy7 - dz7 * dz7 - dw7 * dw7;
    if (attn7 > 0) {
    attn7 *= attn7;
    value += attn7 * attn7 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 1, dx7, dy7, dz7, dw7);
    }

    //Contribution (0,1,1,0)
    double dx8 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dy8 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dz8 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dw8 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double attn8 = 2 - dx8 * dx8 - dy8 * dy8 - dz8 * dz8 - dw8 * dw8;
    if (attn8 > 0) {
    attn8 *= attn8;
    value += attn8 * attn8 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 0, dx8, dy8, dz8, dw8);
    }

    //Contribution (0,1,0,1)
    double dx9 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dy9 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dz9 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dw9 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double attn9 = 2 - dx9 * dx9 - dy9 * dy9 - dz9 * dz9 - dw9 * dw9;
    if (attn9 > 0) {
    attn9 *= attn9;
    value += attn9 * attn9 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 1, dx9, dy9, dz9, dw9);
    }

    //Contribution (0,0,1,1)
    double dx10 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dy10 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dz10 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dw10 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double attn10 = 2 - dx10 * dx10 - dy10 * dy10 - dz10 * dz10 - dw10 * dw10;
    if (attn10 > 0) {
    attn10 *= attn10;
    value += attn10 * attn10 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 1, dx10, dy10, dz10, dw10);
    }
    } else { //We're inside the second dispentachoron (Rectified 4-Simplex)
    double aScore;
    byte aPoint;
    boolean aIsBiggerSide = true;
    double bScore;
    byte bPoint;
    boolean bIsBiggerSide = true;

    //Decide between (0,0,1,1) and (1,1,0,0)
    if (xins + yins < zins + wins) {
    aScore = xins + yins;
    aPoint = 0x0C;
    } else {
    aScore = zins + wins;
    aPoint = 0x03;
    }

    //Decide between (0,1,0,1) and (1,0,1,0)
    if (xins + zins < yins + wins) {
    bScore = xins + zins;
    bPoint = 0x0A;
    } else {
    bScore = yins + wins;
    bPoint = 0x05;
    }

    //Closer between (0,1,1,0) and (1,0,0,1) will replace the further of a and b, if closer.
    if (xins + wins < yins + zins) {
    double score = xins + wins;
    if (aScore <= bScore && score < bScore) {
    bScore = score;
    bPoint = 0x06;
    } else if (aScore > bScore && score < aScore) {
    aScore = score;
    aPoint = 0x06;
    }
    } else {
    double score = yins + zins;
    if (aScore <= bScore && score < bScore) {
    bScore = score;
    bPoint = 0x09;
    } else if (aScore > bScore && score < aScore) {
    aScore = score;
    aPoint = 0x09;
    }
    }

    //Decide if (0,1,1,1) is closer.
    double p1 = 3 - inSum + xins;
    if (aScore <= bScore && p1 < bScore) {
    bScore = p1;
    bPoint = 0x0E;
    bIsBiggerSide = false;
    } else if (aScore > bScore && p1 < aScore) {
    aScore = p1;
    aPoint = 0x0E;
    aIsBiggerSide = false;
    }

    //Decide if (1,0,1,1) is closer.
    double p2 = 3 - inSum + yins;
    if (aScore <= bScore && p2 < bScore) {
    bScore = p2;
    bPoint = 0x0D;
    bIsBiggerSide = false;
    } else if (aScore > bScore && p2 < aScore) {
    aScore = p2;
    aPoint = 0x0D;
    aIsBiggerSide = false;
    }

    //Decide if (1,1,0,1) is closer.
    double p3 = 3 - inSum + zins;
    if (aScore <= bScore && p3 < bScore) {
    bScore = p3;
    bPoint = 0x0B;
    bIsBiggerSide = false;
    } else if (aScore > bScore && p3 < aScore) {
    aScore = p3;
    aPoint = 0x0B;
    aIsBiggerSide = false;
    }

    //Decide if (1,1,1,0) is closer.
    double p4 = 3 - inSum + wins;
    if (aScore <= bScore && p4 < bScore) {
    bScore = p4;
    bPoint = 0x07;
    bIsBiggerSide = false;
    } else if (aScore > bScore && p4 < aScore) {
    aScore = p4;
    aPoint = 0x07;
    aIsBiggerSide = false;
    }

    //Where each of the two closest points are determines how the extra two or three vertices are calculated.
    if (aIsBiggerSide == bIsBiggerSide) {
    if (aIsBiggerSide) { //Both closest points on the bigger side
    byte c1 = (byte)(aPoint & bPoint);
    byte c2 = (byte)(aPoint | bPoint);

    //Two contributions are permutations of (0,0,0,1) and (0,0,0,2) based on c1
    xsv_ext0 = xsv_ext1 = xsb;
    ysv_ext0 = ysv_ext1 = ysb;
    zsv_ext0 = zsv_ext1 = zsb;
    wsv_ext0 = wsv_ext1 = wsb;
    dx_ext0 = dx0 - SQUISH_CONSTANT_4D;
    dy_ext0 = dy0 - SQUISH_CONSTANT_4D;
    dz_ext0 = dz0 - SQUISH_CONSTANT_4D;
    dw_ext0 = dw0 - SQUISH_CONSTANT_4D;
    dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_4D;
    dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_4D;
    dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 - 2 * SQUISH_CONSTANT_4D;
    if ((c1 & 0x01) != 0) {
    xsv_ext0 += 1;
    dx_ext0 -= 1;
    xsv_ext1 += 2;
    dx_ext1 -= 2;
    } else if ((c1 & 0x02) != 0) {
    ysv_ext0 += 1;
    dy_ext0 -= 1;
    ysv_ext1 += 2;
    dy_ext1 -= 2;
    } else if ((c1 & 0x04) != 0) {
    zsv_ext0 += 1;
    dz_ext0 -= 1;
    zsv_ext1 += 2;
    dz_ext1 -= 2;
    } else {
    wsv_ext0 += 1;
    dw_ext0 -= 1;
    wsv_ext1 += 2;
    dw_ext1 -= 2;
    }

    //One contribution is a permutation of (1,1,1,-1) based on c2
    xsv_ext2 = xsb + 1;
    ysv_ext2 = ysb + 1;
    zsv_ext2 = zsb + 1;
    wsv_ext2 = wsb + 1;
    dx_ext2 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dy_ext2 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dz_ext2 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    if ((c2 & 0x01) == 0) {
    xsv_ext2 -= 2;
    dx_ext2 += 2;
    } else if ((c2 & 0x02) == 0) {
    ysv_ext2 -= 2;
    dy_ext2 += 2;
    } else if ((c2 & 0x04) == 0) {
    zsv_ext2 -= 2;
    dz_ext2 += 2;
    } else {
    wsv_ext2 -= 2;
    dw_ext2 += 2;
    }
    } else { //Both closest points on the smaller side
    //One of the two extra points is (1,1,1,1)
    xsv_ext2 = xsb + 1;
    ysv_ext2 = ysb + 1;
    zsv_ext2 = zsb + 1;
    wsv_ext2 = wsb + 1;
    dx_ext2 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D;
    dy_ext2 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D;
    dz_ext2 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D;

    //Other two points are based on the shared axes.
    byte c = (byte)(aPoint & bPoint);

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

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

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

    if ((c & 0x08) != 0)
    {
    wsv_ext0 = wsb + 1;
    wsv_ext1 = wsb + 2;
    dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsb;
    dw_ext0 = dw_ext1 = dw0 - 3 * SQUISH_CONSTANT_4D;
    }
    }
    } else { //One point on each "side"
    byte c1, c2;
    if (aIsBiggerSide) {
    c1 = aPoint;
    c2 = bPoint;
    } else {
    c1 = bPoint;
    c2 = aPoint;
    }

    //Two contributions are the bigger-sided point with each 1 replaced with 2.
    if ((c1 & 0x01) != 0) {
    xsv_ext0 = xsb + 2;
    xsv_ext1 = xsb + 1;
    dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_4D;
    dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D;
    } else {
    xsv_ext0 = xsv_ext1 = xsb;
    dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_4D;
    }

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

    if ((c1 & 0x04) != 0) {
    zsv_ext0 = zsv_ext1 = zsb + 1;
    dz_ext0 = dz_ext1 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D;
    if ((c1 & 0x03) == 0) {
    zsv_ext0 += 1;
    dz_ext0 -= 1;
    } else {
    zsv_ext1 += 1;
    dz_ext1 -= 1;
    }
    } else {
    zsv_ext0 = zsv_ext1 = zsb;
    dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_4D;
    }

    if ((c1 & 0x08) != 0) {
    wsv_ext0 = wsb + 1;
    wsv_ext1 = wsb + 2;
    dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D;
    dw_ext1 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D;
    } else {
    wsv_ext0 = wsv_ext1 = wsb;
    dw_ext0 = dw_ext1 = dw0 - 3 * SQUISH_CONSTANT_4D;
    }

    //One contribution is a permutation of (1,1,1,-1) based on the smaller-sided point
    xsv_ext2 = xsb + 1;
    ysv_ext2 = ysb + 1;
    zsv_ext2 = zsb + 1;
    wsv_ext2 = wsb + 1;
    dx_ext2 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dy_ext2 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dz_ext2 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    dw_ext2 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    if ((c2 & 0x01) == 0) {
    xsv_ext2 -= 2;
    dx_ext2 += 2;
    } else if ((c2 & 0x02) == 0) {
    ysv_ext2 -= 2;
    dy_ext2 += 2;
    } else if ((c2 & 0x04) == 0) {
    zsv_ext2 -= 2;
    dz_ext2 += 2;
    } else {
    wsv_ext2 -= 2;
    dw_ext2 += 2;
    }
    }

    //Contribution (1,1,1,0)
    double dx4 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double dy4 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double dz4 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double dw4 = dw0 - 3 * SQUISH_CONSTANT_4D;
    double attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4;
    if (attn4 > 0) {
    attn4 *= attn4;
    value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 0, dx4, dy4, dz4, dw4);
    }

    //Contribution (1,1,0,1)
    double dx3 = dx4;
    double dy3 = dy4;
    double dz3 = dz0 - 3 * SQUISH_CONSTANT_4D;
    double dw3 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D;
    double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3;
    if (attn3 > 0) {
    attn3 *= attn3;
    value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 1, dx3, dy3, dz3, dw3);
    }

    //Contribution (1,0,1,1)
    double dx2 = dx4;
    double dy2 = dy0 - 3 * SQUISH_CONSTANT_4D;
    double dz2 = dz4;
    double dw2 = dw3;
    double attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2;
    if (attn2 > 0) {
    attn2 *= attn2;
    value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 1, dx2, dy2, dz2, dw2);
    }

    //Contribution (0,1,1,1)
    double dx1 = dx0 - 3 * SQUISH_CONSTANT_4D;
    double dz1 = dz4;
    double dy1 = dy4;
    double dw1 = dw3;
    double attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1;
    if (attn1 > 0) {
    attn1 *= attn1;
    value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 1, dx1, dy1, dz1, dw1);
    }

    //Contribution (1,1,0,0)
    double dx5 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dy5 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dz5 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dw5 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5 - dw5 * dw5;
    if (attn5 > 0) {
    attn5 *= attn5;
    value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 0, dx5, dy5, dz5, dw5);
    }

    //Contribution (1,0,1,0)
    double dx6 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dy6 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dz6 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dw6 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6 - dw6 * dw6;
    if (attn6 > 0) {
    attn6 *= attn6;
    value += attn6 * attn6 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 0, dx6, dy6, dz6, dw6);
    }

    //Contribution (1,0,0,1)
    double dx7 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dy7 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dz7 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dw7 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double attn7 = 2 - dx7 * dx7 - dy7 * dy7 - dz7 * dz7 - dw7 * dw7;
    if (attn7 > 0) {
    attn7 *= attn7;
    value += attn7 * attn7 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 1, dx7, dy7, dz7, dw7);
    }

    //Contribution (0,1,1,0)
    double dx8 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dy8 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dz8 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dw8 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double attn8 = 2 - dx8 * dx8 - dy8 * dy8 - dz8 * dz8 - dw8 * dw8;
    if (attn8 > 0) {
    attn8 *= attn8;
    value += attn8 * attn8 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 0, dx8, dy8, dz8, dw8);
    }

    //Contribution (0,1,0,1)
    double dx9 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dy9 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dz9 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dw9 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double attn9 = 2 - dx9 * dx9 - dy9 * dy9 - dz9 * dz9 - dw9 * dw9;
    if (attn9 > 0) {
    attn9 *= attn9;
    value += attn9 * attn9 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 1, dx9, dy9, dz9, dw9);
    }

    //Contribution (0,0,1,1)
    double dx10 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dy10 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D;
    double dz10 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double dw10 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D;
    double attn10 = 2 - dx10 * dx10 - dy10 * dy10 - dz10 * dz10 - dw10 * dw10;
    if (attn10 > 0) {
    attn10 *= attn10;
    value += attn10 * attn10 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 1, dx10, dy10, dz10, dw10);
    }
    }

    //First extra vertex
    double attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0 - dw_ext0 * dw_ext0;
    if (attn_ext0 > 0)
    {
    attn_ext0 *= attn_ext0;
    value += attn_ext0 * attn_ext0 * extrapolate(xsv_ext0, ysv_ext0, zsv_ext0, wsv_ext0, dx_ext0, dy_ext0, dz_ext0, dw_ext0);
    }

    //Second extra vertex
    double attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1 - dw_ext1 * dw_ext1;
    if (attn_ext1 > 0)
    {
    attn_ext1 *= attn_ext1;
    value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, wsv_ext1, dx_ext1, dy_ext1, dz_ext1, dw_ext1);
    }

    //Third extra vertex
    double attn_ext2 = 2 - dx_ext2 * dx_ext2 - dy_ext2 * dy_ext2 - dz_ext2 * dz_ext2 - dw_ext2 * dw_ext2;
    if (attn_ext2 > 0)
    {
    attn_ext2 *= attn_ext2;
    value += attn_ext2 * attn_ext2 * extrapolate(xsv_ext2, ysv_ext2, zsv_ext2, wsv_ext2, dx_ext2, dy_ext2, dz_ext2, dw_ext2);
    }

    return value / NORM_CONSTANT_4D;
    }

    private double extrapolate(int xsb, int ysb, double dx, double dy)
    {
    int index = perm[(perm[xsb & 0xFF] + ysb) & 0xFF] & 0x0E;
    return gradients2D[index] * dx
    + gradients2D[index + 1] * dy;
    }

    private double extrapolate(int xsb, int ysb, int zsb, double dx, double dy, double dz)
    {
    int index = permGradIndex3D[(perm[(perm[xsb & 0xFF] + ysb) & 0xFF] + zsb) & 0xFF];
    return gradients3D[index] * dx
    + gradients3D[index + 1] * dy
    + gradients3D[index + 2] * dz;
    }

    private double extrapolate(int xsb, int ysb, int zsb, int wsb, double dx, double dy, double dz, double dw)
    {
    int index = perm[(perm[(perm[(perm[xsb & 0xFF] + ysb) & 0xFF] + zsb) & 0xFF] + wsb) & 0xFF] & 0xFC;
    return gradients4D[index] * dx
    + gradients4D[index + 1] * dy
    + gradients4D[index + 2] * dz
    + gradients4D[index + 3] * dw;
    }

    private static int fastFloor(double x) {
    int xi = (int)x;
    return x < xi ? xi - 1 : xi;
    }

    //Gradients for 2D. They approximate the directions to the
    //vertices of an octagon from the center.
    private static byte[] gradients2D = new byte[] {
    5, 2, 2, 5,
    -5, 2, -2, 5,
    5, -2, 2, -5,
    -5, -2, -2, -5,
    };

    //Array of gradient values for 3D.
    //(New gradient set 9/19/14)
    //Gradients for 3D. They approximate the directions to the
    //vertices of a rhombicuboctahedron from the center, skewed so
    //that the triangular and square facets can be inscribed inside
    //circles of the same radius.
    private static byte[] gradients3D = new byte[] {
    0, 3, 2, 0, 2, 3, 3, 0, 2, 2, 0, 3, 3, 2, 0, 2, 3, 0,
    0,-3, 2, 0, 2,-3, -3, 0, 2, 2, 0,-3, -3, 2, 0, 2,-3, 0,
    0, 3,-2, 0,-2, 3, 3, 0,-2, -2, 0, 3, 3,-2, 0, -2, 3, 0,
    0,-3,-2, 0,-2,-3, -3, 0,-2, -2, 0,-3, -3,-2, 0, -2,-3, 0,
    -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,
    };

    //The standardized permutation order as used in Ken Perlin's "Improved Noise"
    //(and basically every noise implementation on the Internet).
    //Also note that there's no reason this can't be a byte array other than that this is Java.
    public static final short[] PERM_DEFAULT = new short[] {
    151,160,137, 91, 90, 15,131, 13,201, 95, 96, 53,194,233, 7,225,
    140, 36,103, 30, 69,142, 8, 99, 37,240, 21, 10, 23,190, 6,148,
    247,120,234, 75, 0, 26,197, 62, 94,252,219,203,117, 35, 11, 32,
    57,177, 33, 88,237,149, 56, 87,174, 20,125,136,171,168, 68,175,
    74,165, 71,134,139, 48, 27,166, 77,146,158,231, 83,111,229,122,
    60,211,133,230,220,105, 92, 41, 55, 46,245, 40,244,102,143, 54,
    65, 25, 63,161, 1,216, 80, 73,209, 76,132,187,208, 89, 18,169,
    200,196,135,130,116,188,159, 86,164,100,109,198,173,186, 3, 64,
    52,217,226,250,124,123, 5,202, 38,147,118,126,255, 82, 85,212,
    207,206, 59,227, 47, 16, 58, 17,182,189, 28, 42,223,183,170,213,
    119,248,152, 2, 44,154,163, 70,221,153,101,155,167, 43,172, 9,
    129, 22, 39,253, 19, 98,108,110, 79,113,224,232,178,185,112,104,
    218,246, 97,228,251, 34,242,193,238,210,144, 12,191,179,162,241,
    81, 51,145,235,249, 14,239,107, 49,192,214, 31,181,199,106,157,
    184, 84,204,176,115,121, 50, 45,127, 4,150,254,138,236,205, 93,
    222,114, 67, 29, 24, 72,243,141,128,195, 78, 66,215, 61,156,180
    //Gradients for 4D. They approximate the directions to the
    //vertices of a disprismatotesseractihexadecachoron from the center,
    //skewed so that the tetrahedral and cubic facets can be inscribed inside
    //spheres of the same radius.
    private static byte[] gradients4D = new byte[] {
    3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3,
    -3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3, 1, -1, 1, 1, 3,
    3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3,
    -3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3,
    3, 1, -1, 1, 1, 3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3,
    -3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3,
    3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3,
    -3, -1, -1, 1, -1, -3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3,
    3, 1, 1, -1, 1, 3, 1, -1, 1, 1, 3, -1, 1, 1, 1, -3,
    -3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3,
    3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3,
    -3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3,
    3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3,
    -3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3,
    3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3, -1, 1, -1, -1, -3,
    -3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3,
    };
    }
  7. KdotJPG revised this gist Sep 20, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion OpenSimplexNoise
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    .
  8. KdotJPG revised this gist Sep 20, 2014. 5 changed files with 24 additions and 219 deletions.
    201 changes: 0 additions & 201 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -1,201 +0,0 @@
    Apache License
    Version 2.0, January 2004
    http://www.apache.org/licenses/

    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

    1. Definitions.

    "License" shall mean the terms and conditions for use, reproduction,
    and distribution as defined by Sections 1 through 9 of this document.

    "Licensor" shall mean the copyright owner or entity authorized by
    the copyright owner that is granting the License.

    "Legal Entity" shall mean the union of the acting entity and all
    other entities that control, are controlled by, or are under common
    control with that entity. For the purposes of this definition,
    "control" means (i) the power, direct or indirect, to cause the
    direction or management of such entity, whether by contract or
    otherwise, or (ii) ownership of fifty percent (50%) or more of the
    outstanding shares, or (iii) beneficial ownership of such entity.

    "You" (or "Your") shall mean an individual or Legal Entity
    exercising permissions granted by this License.

    "Source" form shall mean the preferred form for making modifications,
    including but not limited to software source code, documentation
    source, and configuration files.

    "Object" form shall mean any form resulting from mechanical
    transformation or translation of a Source form, including but
    not limited to compiled object code, generated documentation,
    and conversions to other media types.

    "Work" shall mean the work of authorship, whether in Source or
    Object form, made available under the License, as indicated by a
    copyright notice that is included in or attached to the work
    (an example is provided in the Appendix below).

    "Derivative Works" shall mean any work, whether in Source or Object
    form, that is based on (or derived from) the Work and for which the
    editorial revisions, annotations, elaborations, or other modifications
    represent, as a whole, an original work of authorship. For the purposes
    of this License, Derivative Works shall not include works that remain
    separable from, or merely link (or bind by name) to the interfaces of,
    the Work and Derivative Works thereof.

    "Contribution" shall mean any work of authorship, including
    the original version of the Work and any modifications or additions
    to that Work or Derivative Works thereof, that is intentionally
    submitted to Licensor for inclusion in the Work by the copyright owner
    or by an individual or Legal Entity authorized to submit on behalf of
    the copyright owner. For the purposes of this definition, "submitted"
    means any form of electronic, verbal, or written communication sent
    to the Licensor or its representatives, including but not limited to
    communication on electronic mailing lists, source code control systems,
    and issue tracking systems that are managed by, or on behalf of, the
    Licensor for the purpose of discussing and improving the Work, but
    excluding communication that is conspicuously marked or otherwise
    designated in writing by the copyright owner as "Not a Contribution."

    "Contributor" shall mean Licensor and any individual or Legal Entity
    on behalf of whom a Contribution has been received by Licensor and
    subsequently incorporated within the Work.

    2. Grant of Copyright License. Subject to the terms and conditions of
    this License, each Contributor hereby grants to You a perpetual,
    worldwide, non-exclusive, no-charge, royalty-free, irrevocable
    copyright license to reproduce, prepare Derivative Works of,
    publicly display, publicly perform, sublicense, and distribute the
    Work and such Derivative Works in Source or Object form.

    3. Grant of Patent License. Subject to the terms and conditions of
    this License, each Contributor hereby grants to You a perpetual,
    worldwide, non-exclusive, no-charge, royalty-free, irrevocable
    (except as stated in this section) patent license to make, have made,
    use, offer to sell, sell, import, and otherwise transfer the Work,
    where such license applies only to those patent claims licensable
    by such Contributor that are necessarily infringed by their
    Contribution(s) alone or by combination of their Contribution(s)
    with the Work to which such Contribution(s) was submitted. If You
    institute patent litigation against any entity (including a
    cross-claim or counterclaim in a lawsuit) alleging that the Work
    or a Contribution incorporated within the Work constitutes direct
    or contributory patent infringement, then any patent licenses
    granted to You under this License for that Work shall terminate
    as of the date such litigation is filed.

    4. Redistribution. You may reproduce and distribute copies of the
    Work or Derivative Works thereof in any medium, with or without
    modifications, and in Source or Object form, provided that You
    meet the following conditions:

    (a) You must give any other recipients of the Work or
    Derivative Works a copy of this License; and

    (b) You must cause any modified files to carry prominent notices
    stating that You changed the files; and

    (c) You must retain, in the Source form of any Derivative Works
    that You distribute, all copyright, patent, trademark, and
    attribution notices from the Source form of the Work,
    excluding those notices that do not pertain to any part of
    the Derivative Works; and

    (d) If the Work includes a "NOTICE" text file as part of its
    distribution, then any Derivative Works that You distribute must
    include a readable copy of the attribution notices contained
    within such NOTICE file, excluding those notices that do not
    pertain to any part of the Derivative Works, in at least one
    of the following places: within a NOTICE text file distributed
    as part of the Derivative Works; within the Source form or
    documentation, if provided along with the Derivative Works; or,
    within a display generated by the Derivative Works, if and
    wherever such third-party notices normally appear. The contents
    of the NOTICE file are for informational purposes only and
    do not modify the License. You may add Your own attribution
    notices within Derivative Works that You distribute, alongside
    or as an addendum to the NOTICE text from the Work, provided
    that such additional attribution notices cannot be construed
    as modifying the License.

    You may add Your own copyright statement to Your modifications and
    may provide additional or different license terms and conditions
    for use, reproduction, or distribution of Your modifications, or
    for any such Derivative Works as a whole, provided Your use,
    reproduction, and distribution of the Work otherwise complies with
    the conditions stated in this License.

    5. Submission of Contributions. Unless You explicitly state otherwise,
    any Contribution intentionally submitted for inclusion in the Work
    by You to the Licensor shall be under the terms and conditions of
    this License, without any additional terms or conditions.
    Notwithstanding the above, nothing herein shall supersede or modify
    the terms of any separate license agreement you may have executed
    with Licensor regarding such Contributions.

    6. Trademarks. This License does not grant permission to use the trade
    names, trademarks, service marks, or product names of the Licensor,
    except as required for reasonable and customary use in describing the
    origin of the Work and reproducing the content of the NOTICE file.

    7. Disclaimer of Warranty. Unless required by applicable law or
    agreed to in writing, Licensor provides the Work (and each
    Contributor provides its Contributions) on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    implied, including, without limitation, any warranties or conditions
    of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
    PARTICULAR PURPOSE. You are solely responsible for determining the
    appropriateness of using or redistributing the Work and assume any
    risks associated with Your exercise of permissions under this License.

    8. Limitation of Liability. In no event and under no legal theory,
    whether in tort (including negligence), contract, or otherwise,
    unless required by applicable law (such as deliberate and grossly
    negligent acts) or agreed to in writing, shall any Contributor be
    liable to You for damages, including any direct, indirect, special,
    incidental, or consequential damages of any character arising as a
    result of this License or out of the use or inability to use the
    Work (including but not limited to damages for loss of goodwill,
    work stoppage, computer failure or malfunction, or any and all
    other commercial damages or losses), even if such Contributor
    has been advised of the possibility of such damages.

    9. Accepting Warranty or Additional Liability. While redistributing
    the Work or Derivative Works thereof, You may choose to offer,
    and charge a fee for, acceptance of support, warranty, indemnity,
    or other liability obligations and/or rights consistent with this
    License. However, in accepting such obligations, You may act only
    on Your own behalf and on Your sole responsibility, not on behalf
    of any other Contributor, and only if You agree to indemnify,
    defend, and hold each Contributor harmless for any liability
    incurred by, or claims asserted against, such Contributor by reason
    of your accepting any such warranty or additional liability.

    END OF TERMS AND CONDITIONS

    APPENDIX: How to apply the Apache License to your work.

    To apply the Apache License to your work, attach the following
    boilerplate notice, with the fields enclosed by brackets "[]"
    replaced with your own identifying information. (Don't include
    the brackets!) The text should be enclosed in the appropriate
    comment syntax for the file format. We also recommend that a
    file or class name and description of purpose be included on the
    same "printed page" as the copyright notice for easier
    identification within third-party archives.

    Copyright [yyyy] [name of copyright owner]

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    File renamed without changes.
    18 changes: 2 additions & 16 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,6 @@
    /*
    * OpenSimplex (Simplectic) Noise in Java
    * Copyright 2014 Kurt Spencer
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    *
    * (v0.0.1 with new gradient set and corresponding normalization factor, 9/19/14)
    * OpenSimplex (Simplectic) Noise in Java.
    * (v1.0.1 With new gradient set and corresponding normalization factor, 9/19/14)
    */

    public class OpenSimplexNoise {
    2 changes: 0 additions & 2 deletions OpenSimplexNoiseTest.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    /*
    * OpenSimplex Noise sample class.
    *
    * Public Domain
    */

    import java.awt.image.BufferedImage;
    22 changes: 22 additions & 0 deletions UNLICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    This is free and unencumbered software released into the public domain.

    Anyone is free to copy, modify, publish, use, compile, sell, or
    distribute this software, either in source code form or as a compiled
    binary, for any purpose, commercial or non-commercial, and by any
    means.

    In jurisdictions that recognize copyright laws, the author or authors
    of this software dedicate any and all copyright interest in the
    software to the public domain. We make this dedication for the benefit
    of the public at large and to the detriment of our heirs and
    successors. We intend this dedication to be an overt act of
    relinquishment in perpetuity of all present and future rights to this
    software under copyright law.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.
  9. KdotJPG revised this gist Sep 20, 2014. 2 changed files with 1 addition and 1 deletion.
    1 change: 1 addition & 0 deletions OpenSimplexNoise
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    .
    1 change: 0 additions & 1 deletion LICENSE
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    Apache License
    Version 2.0, January 2004
    http://www.apache.org/licenses/
  10. KdotJPG revised this gist Sep 19, 2014. 3 changed files with 238 additions and 25 deletions.
    202 changes: 202 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,202 @@

    Apache License
    Version 2.0, January 2004
    http://www.apache.org/licenses/

    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

    1. Definitions.

    "License" shall mean the terms and conditions for use, reproduction,
    and distribution as defined by Sections 1 through 9 of this document.

    "Licensor" shall mean the copyright owner or entity authorized by
    the copyright owner that is granting the License.

    "Legal Entity" shall mean the union of the acting entity and all
    other entities that control, are controlled by, or are under common
    control with that entity. For the purposes of this definition,
    "control" means (i) the power, direct or indirect, to cause the
    direction or management of such entity, whether by contract or
    otherwise, or (ii) ownership of fifty percent (50%) or more of the
    outstanding shares, or (iii) beneficial ownership of such entity.

    "You" (or "Your") shall mean an individual or Legal Entity
    exercising permissions granted by this License.

    "Source" form shall mean the preferred form for making modifications,
    including but not limited to software source code, documentation
    source, and configuration files.

    "Object" form shall mean any form resulting from mechanical
    transformation or translation of a Source form, including but
    not limited to compiled object code, generated documentation,
    and conversions to other media types.

    "Work" shall mean the work of authorship, whether in Source or
    Object form, made available under the License, as indicated by a
    copyright notice that is included in or attached to the work
    (an example is provided in the Appendix below).

    "Derivative Works" shall mean any work, whether in Source or Object
    form, that is based on (or derived from) the Work and for which the
    editorial revisions, annotations, elaborations, or other modifications
    represent, as a whole, an original work of authorship. For the purposes
    of this License, Derivative Works shall not include works that remain
    separable from, or merely link (or bind by name) to the interfaces of,
    the Work and Derivative Works thereof.

    "Contribution" shall mean any work of authorship, including
    the original version of the Work and any modifications or additions
    to that Work or Derivative Works thereof, that is intentionally
    submitted to Licensor for inclusion in the Work by the copyright owner
    or by an individual or Legal Entity authorized to submit on behalf of
    the copyright owner. For the purposes of this definition, "submitted"
    means any form of electronic, verbal, or written communication sent
    to the Licensor or its representatives, including but not limited to
    communication on electronic mailing lists, source code control systems,
    and issue tracking systems that are managed by, or on behalf of, the
    Licensor for the purpose of discussing and improving the Work, but
    excluding communication that is conspicuously marked or otherwise
    designated in writing by the copyright owner as "Not a Contribution."

    "Contributor" shall mean Licensor and any individual or Legal Entity
    on behalf of whom a Contribution has been received by Licensor and
    subsequently incorporated within the Work.

    2. Grant of Copyright License. Subject to the terms and conditions of
    this License, each Contributor hereby grants to You a perpetual,
    worldwide, non-exclusive, no-charge, royalty-free, irrevocable
    copyright license to reproduce, prepare Derivative Works of,
    publicly display, publicly perform, sublicense, and distribute the
    Work and such Derivative Works in Source or Object form.

    3. Grant of Patent License. Subject to the terms and conditions of
    this License, each Contributor hereby grants to You a perpetual,
    worldwide, non-exclusive, no-charge, royalty-free, irrevocable
    (except as stated in this section) patent license to make, have made,
    use, offer to sell, sell, import, and otherwise transfer the Work,
    where such license applies only to those patent claims licensable
    by such Contributor that are necessarily infringed by their
    Contribution(s) alone or by combination of their Contribution(s)
    with the Work to which such Contribution(s) was submitted. If You
    institute patent litigation against any entity (including a
    cross-claim or counterclaim in a lawsuit) alleging that the Work
    or a Contribution incorporated within the Work constitutes direct
    or contributory patent infringement, then any patent licenses
    granted to You under this License for that Work shall terminate
    as of the date such litigation is filed.

    4. Redistribution. You may reproduce and distribute copies of the
    Work or Derivative Works thereof in any medium, with or without
    modifications, and in Source or Object form, provided that You
    meet the following conditions:

    (a) You must give any other recipients of the Work or
    Derivative Works a copy of this License; and

    (b) You must cause any modified files to carry prominent notices
    stating that You changed the files; and

    (c) You must retain, in the Source form of any Derivative Works
    that You distribute, all copyright, patent, trademark, and
    attribution notices from the Source form of the Work,
    excluding those notices that do not pertain to any part of
    the Derivative Works; and

    (d) If the Work includes a "NOTICE" text file as part of its
    distribution, then any Derivative Works that You distribute must
    include a readable copy of the attribution notices contained
    within such NOTICE file, excluding those notices that do not
    pertain to any part of the Derivative Works, in at least one
    of the following places: within a NOTICE text file distributed
    as part of the Derivative Works; within the Source form or
    documentation, if provided along with the Derivative Works; or,
    within a display generated by the Derivative Works, if and
    wherever such third-party notices normally appear. The contents
    of the NOTICE file are for informational purposes only and
    do not modify the License. You may add Your own attribution
    notices within Derivative Works that You distribute, alongside
    or as an addendum to the NOTICE text from the Work, provided
    that such additional attribution notices cannot be construed
    as modifying the License.

    You may add Your own copyright statement to Your modifications and
    may provide additional or different license terms and conditions
    for use, reproduction, or distribution of Your modifications, or
    for any such Derivative Works as a whole, provided Your use,
    reproduction, and distribution of the Work otherwise complies with
    the conditions stated in this License.

    5. Submission of Contributions. Unless You explicitly state otherwise,
    any Contribution intentionally submitted for inclusion in the Work
    by You to the Licensor shall be under the terms and conditions of
    this License, without any additional terms or conditions.
    Notwithstanding the above, nothing herein shall supersede or modify
    the terms of any separate license agreement you may have executed
    with Licensor regarding such Contributions.

    6. Trademarks. This License does not grant permission to use the trade
    names, trademarks, service marks, or product names of the Licensor,
    except as required for reasonable and customary use in describing the
    origin of the Work and reproducing the content of the NOTICE file.

    7. Disclaimer of Warranty. Unless required by applicable law or
    agreed to in writing, Licensor provides the Work (and each
    Contributor provides its Contributions) on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    implied, including, without limitation, any warranties or conditions
    of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
    PARTICULAR PURPOSE. You are solely responsible for determining the
    appropriateness of using or redistributing the Work and assume any
    risks associated with Your exercise of permissions under this License.

    8. Limitation of Liability. In no event and under no legal theory,
    whether in tort (including negligence), contract, or otherwise,
    unless required by applicable law (such as deliberate and grossly
    negligent acts) or agreed to in writing, shall any Contributor be
    liable to You for damages, including any direct, indirect, special,
    incidental, or consequential damages of any character arising as a
    result of this License or out of the use or inability to use the
    Work (including but not limited to damages for loss of goodwill,
    work stoppage, computer failure or malfunction, or any and all
    other commercial damages or losses), even if such Contributor
    has been advised of the possibility of such damages.

    9. Accepting Warranty or Additional Liability. While redistributing
    the Work or Derivative Works thereof, You may choose to offer,
    and charge a fee for, acceptance of support, warranty, indemnity,
    or other liability obligations and/or rights consistent with this
    License. However, in accepting such obligations, You may act only
    on Your own behalf and on Your sole responsibility, not on behalf
    of any other Contributor, and only if You agree to indemnify,
    defend, and hold each Contributor harmless for any liability
    incurred by, or claims asserted against, such Contributor by reason
    of your accepting any such warranty or additional liability.

    END OF TERMS AND CONDITIONS

    APPENDIX: How to apply the Apache License to your work.

    To apply the Apache License to your work, attach the following
    boilerplate notice, with the fields enclosed by brackets "[]"
    replaced with your own identifying information. (Don't include
    the brackets!) The text should be enclosed in the appropriate
    comment syntax for the file format. We also recommend that a
    file or class name and description of purpose be included on the
    same "printed page" as the copyright notice for easier
    identification within third-party archives.

    Copyright [yyyy] [name of copyright owner]

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    54 changes: 30 additions & 24 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,20 @@
    /*
    * OpenSimplex (Simplectic) Noise for 3D in Java
    * (Preliminary Release)
    * OpenSimplex (Simplectic) Noise in Java
    * Copyright 2014 Kurt Spencer
    *
    * KdotJPG
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    *
    * (v0.0.1 with new gradient set and corresponding normalization factor, 9/19/14)
    */

    public class OpenSimplexNoise {
    @@ -14,7 +26,7 @@ public class OpenSimplexNoise {
    private short[] permGradIndex3D;

    public OpenSimplexNoise() {
    this(perm_default);
    this(PERM_DEFAULT);
    }

    public OpenSimplexNoise(short[] perm) {
    @@ -607,7 +619,10 @@ public double eval(double x, double y, double z) {
    value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1);
    }

    return value / 18;
    //Normalization constant tested using over 4 billion evaluations to bound range within [-1,1].
    //This is a safe upper bound. Actual min/max values found over the course of the 4 billion
    //evaluations were -28.12974224468639 (min) and 28.134269887817773 (max).
    return value / 28.25;
    }

    private double extrapolate(int xsb, int ysb, int zsb, double dx, double dy, double dz)
    @@ -623,20 +638,19 @@ private static int fastFloor(double x) {
    return x < xi ? xi - 1 : xi;
    }

    //Array of gradient values.
    //I may release an updated version of this with a better set if I figure one out. This works though.
    //Note: bytes in Java are signed. Keep this in mind if porting to another language.
    //Array of gradient values for 3D.
    //(New gradient set 9/19/14)
    private static byte[] gradients3D = new byte[] {
    0,1,2, 0,2,1, 1,0,2, 2,0,1, 1,2,0, 2,1,0,
    0,-1,2, 0,2,-1, -1,0,2, 2,0,-1, -1,2,0, 2,-1,0,
    0,1,-2, 0,-2,1, 1,0,-2, -2,0,1, 1,-2,0, -2,1,0,
    0,-1,-2, 0,-2,-1, -1,0,-2, -2,0,-1, -1,-2,0, -2,-1,0,
    0, 3, 2, 0, 2, 3, 3, 0, 2, 2, 0, 3, 3, 2, 0, 2, 3, 0,
    0,-3, 2, 0, 2,-3, -3, 0, 2, 2, 0,-3, -3, 2, 0, 2,-3, 0,
    0, 3,-2, 0,-2, 3, 3, 0,-2, -2, 0, 3, 3,-2, 0, -2, 3, 0,
    0,-3,-2, 0,-2,-3, -3, 0,-2, -2, 0,-3, -3,-2, 0, -2,-3, 0,
    };

    //The standardized permutation order as used in Ken Perlin's "Improved Noise" 2002,
    //(and basically every noise implementation on the Internet)
    //The standardized permutation order as used in Ken Perlin's "Improved Noise"
    //(and basically every noise implementation on the Internet).
    //Also note that there's no reason this can't be a byte array other than that this is Java.
    private static short[] perm_default = new short[] {
    public static final short[] PERM_DEFAULT = new short[] {
    151,160,137, 91, 90, 15,131, 13,201, 95, 96, 53,194,233, 7,225,
    140, 36,103, 30, 69,142, 8, 99, 37,240, 21, 10, 23,190, 6,148,
    247,120,234, 75, 0, 26,197, 62, 94,252,219,203,117, 35, 11, 32,
    @@ -654,12 +668,4 @@ private static int fastFloor(double x) {
    184, 84,204,176,115,121, 50, 45,127, 4,150,254,138,236,205, 93,
    222,114, 67, 29, 24, 72,243,141,128,195, 78, 66,215, 61,156,180
    };
    }

    /*
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
    * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */
    }
    7 changes: 6 additions & 1 deletion OpenSimplexNoiseTest.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    /*
    * OpenSimplex Noise sample class.
    *
    * Public Domain
    */

    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.*;
    @@ -23,6 +29,5 @@ public static void main(String[] args)
    }
    }
    ImageIO.write(image, "png", new File("noise.png"));

    }
    }
  11. KdotJPG revised this gist Sep 19, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -267,7 +267,7 @@ public double eval(double x, double y, double z) {
    dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_3D;
    }
    } else { //(1,1,1) is not one of the closest two tetrahedral vertices.
    byte c = (byte)(aPoint & bPoint); //Our two extra vertices are determined by the closest two.
    byte c = (byte)(aPoint & bPoint); //Our two extra vertices are determined by the closest two.

    if ((c & 0x01) != 0) {
    xsv_ext0 = xsb + 1;
  12. KdotJPG created this gist Sep 19, 2014.
    665 changes: 665 additions & 0 deletions OpenSimplexNoise.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,665 @@
    /*
    * OpenSimplex (Simplectic) Noise for 3D in Java
    * (Preliminary Release)
    *
    * KdotJPG
    */

    public class OpenSimplexNoise {

    private static final double STRETCH_CONSTANT_3D = -1.0 / 6;
    private static final double SQUISH_CONSTANT_3D = 1.0 / 3;

    private short[] perm;
    private short[] permGradIndex3D;

    public OpenSimplexNoise() {
    this(perm_default);
    }

    public OpenSimplexNoise(short[] perm) {
    this.perm = perm;
    permGradIndex3D = new short[256];
    for (int i = 0; i < 256; i++) {
    permGradIndex3D[i] = (short)((perm[i] % (gradients3D.length / 3)) * 3);
    }
    }

    //Initializes the class using a permutation array generated from a 64-bit seed.
    //Generates a proper permutation (i.e. doesn't merely perform N successive pair swaps on a base array)
    //Uses java.util.Random
    public OpenSimplexNoise(long seed) {
    perm = new short[256];
    permGradIndex3D = new short[256];
    short[] source = new short[256];
    for (short i = 0; i < 256; i++)
    source[i] = i;
    java.util.Random random = new java.util.Random(seed);
    for (int i = 255; i >= 0; i--) {
    int r = random.nextInt(i + 1);
    perm[i] = source[r];
    permGradIndex3D[i] = (short)((perm[i] % (gradients3D.length / 3)) * 3);
    source[r] = source[i];
    }
    }

    //3D OpenSimplex (Simplectic) Noise.
    public double eval(double x, double y, double z) {

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

    //Floor to get simplectic lattice coordinates of rhombohedron (stretched cube) super-cell origin.
    int xsb = fastFloor(xs);
    int ysb = fastFloor(ys);
    int zsb = fastFloor(zs);

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

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

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

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

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

    double 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.
    byte aPoint = 0x01;
    double aScore = xins;
    byte bPoint = 0x02;
    double bScore = yins;
    if (aScore >= bScore && zins > bScore) {
    bScore = zins;
    bPoint = 0x04;
    } else if (aScore < bScore && zins > aScore) {
    aScore = zins;
    aPoint = 0x04;
    }

    //Now we determine the two lattice points not part of the tetrahedron that may contribute.
    //This depends on the closest two tetrahedral vertices, including (0,0,0)
    double wins = 1 - inSum;
    if (wins > aScore || wins > bScore) { //(0,0,0) is one of the closest two tetrahedral vertices.
    byte c = (bScore > aScore ? bPoint : aPoint); //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.
    byte c = (byte)(aPoint | bPoint); //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)
    double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
    if (attn0 > 0) {
    attn0 *= attn0;
    value = attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0);
    }

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

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

    //Contribution (1,0,0)
    double dx3 = dx2;
    double dy3 = dy1;
    double dz3 = dz0 - 1 - SQUISH_CONSTANT_3D;
    double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
    if (attn3 > 0) {
    attn3 *= attn3;
    value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3);
    }
    } 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).
    byte aPoint = 0x06;
    double aScore = xins;
    byte bPoint = 0x05;
    double bScore = yins;
    if (aScore <= bScore && zins < bScore) {
    bScore = zins;
    bPoint = 0x03;
    } else if (aScore > bScore && zins < aScore) {
    aScore = zins;
    aPoint = 0x03;
    }

    //Now we determine the two lattice points not part of the tetrahedron that may contribute.
    //This depends on the closest two tetrahedral vertices, including (1,1,1)
    double wins = 3 - inSum;
    if (wins < aScore || wins < bScore) { //(1,1,1) is one of the closest two tetrahedral vertices.
    byte c = (bScore < aScore ? bPoint : aPoint); //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.
    byte c = (byte)(aPoint & bPoint); //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)
    double dx3 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D;
    double dy3 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D;
    double dz3 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D;
    double attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3;
    if (attn3 > 0) {
    attn3 *= attn3;
    value = attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3);
    }

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

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

    //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;
    double attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0;
    if (attn0 > 0) {
    attn0 *= attn0;
    value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, dx0, dy0, dz0);
    }
    } else { //We're inside the Octahedron (Rectified 3-Simplex) in between.
    double aScore;
    byte aPoint;
    boolean aIsFurtherSide;
    double bScore;
    byte bPoint;
    boolean bIsFurtherSide;

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

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

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

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

    //One of the two extra points 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 point is based on the shared axis.
    byte c = (byte)(aPoint & bPoint);
    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 points on (0,0,0) side

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

    //Other extra point is based on the omitted axis.
    byte c = (byte)(aPoint | bPoint);
    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 point on (0,0,0) side, one point on (1,1,1) side
    byte c1, c2;
    if (aIsFurtherSide) {
    c1 = aPoint;
    c2 = bPoint;
    } else {
    c1 = bPoint;
    c2 = aPoint;
    }

    //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)
    if ((c2 & 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 ((c2 & 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;
    }
    }

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

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

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

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

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

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

    //First extra vertex
    double 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 * extrapolate(xsv_ext0, ysv_ext0, zsv_ext0, dx_ext0, dy_ext0, dz_ext0);
    }

    //Second extra vertex
    double 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 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1);
    }

    return value / 18;
    }

    private double extrapolate(int xsb, int ysb, int zsb, double dx, double dy, double dz)
    {
    short index = permGradIndex3D[(perm[(perm[xsb & 0xFF] + ysb) & 0xFF] + zsb) & 0xFF];
    return gradients3D[index] * dx
    + gradients3D[index + 1] * dy
    + gradients3D[index + 2] * dz;
    }

    private static int fastFloor(double x) {
    int xi = (int)x;
    return x < xi ? xi - 1 : xi;
    }

    //Array of gradient values.
    //I may release an updated version of this with a better set if I figure one out. This works though.
    //Note: bytes in Java are signed. Keep this in mind if porting to another language.
    private static byte[] gradients3D = new byte[] {
    0,1,2, 0,2,1, 1,0,2, 2,0,1, 1,2,0, 2,1,0,
    0,-1,2, 0,2,-1, -1,0,2, 2,0,-1, -1,2,0, 2,-1,0,
    0,1,-2, 0,-2,1, 1,0,-2, -2,0,1, 1,-2,0, -2,1,0,
    0,-1,-2, 0,-2,-1, -1,0,-2, -2,0,-1, -1,-2,0, -2,-1,0,
    };

    //The standardized permutation order as used in Ken Perlin's "Improved Noise" 2002,
    //(and basically every noise implementation on the Internet)
    //Also note that there's no reason this can't be a byte array other than that this is Java.
    private static short[] perm_default = new short[] {
    151,160,137, 91, 90, 15,131, 13,201, 95, 96, 53,194,233, 7,225,
    140, 36,103, 30, 69,142, 8, 99, 37,240, 21, 10, 23,190, 6,148,
    247,120,234, 75, 0, 26,197, 62, 94,252,219,203,117, 35, 11, 32,
    57,177, 33, 88,237,149, 56, 87,174, 20,125,136,171,168, 68,175,
    74,165, 71,134,139, 48, 27,166, 77,146,158,231, 83,111,229,122,
    60,211,133,230,220,105, 92, 41, 55, 46,245, 40,244,102,143, 54,
    65, 25, 63,161, 1,216, 80, 73,209, 76,132,187,208, 89, 18,169,
    200,196,135,130,116,188,159, 86,164,100,109,198,173,186, 3, 64,
    52,217,226,250,124,123, 5,202, 38,147,118,126,255, 82, 85,212,
    207,206, 59,227, 47, 16, 58, 17,182,189, 28, 42,223,183,170,213,
    119,248,152, 2, 44,154,163, 70,221,153,101,155,167, 43,172, 9,
    129, 22, 39,253, 19, 98,108,110, 79,113,224,232,178,185,112,104,
    218,246, 97,228,251, 34,242,193,238,210,144, 12,191,179,162,241,
    81, 51,145,235,249, 14,239,107, 49,192,214, 31,181,199,106,157,
    184, 84,204,176,115,121, 50, 45,127, 4,150,254,138,236,205, 93,
    222,114, 67, 29, 24, 72,243,141,128,195, 78, 66,215, 61,156,180
    };
    }

    /*
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
    * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */
    28 changes: 28 additions & 0 deletions OpenSimplexNoiseTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.*;

    public class OpenSimplexNoiseTest
    {
    private static final int WIDTH = 512;
    private static final int HEIGHT = 512;
    private static final double FEATURE_SIZE = 24;

    public static void main(String[] args)
    throws IOException {

    OpenSimplexNoise noise = new OpenSimplexNoise();
    BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < HEIGHT; y++)
    {
    for (int x = 0; x < WIDTH; x++)
    {
    double value = noise.eval(x / FEATURE_SIZE, y / FEATURE_SIZE, 0.0);
    int rgb = 0x010101 * (int)((value + 1) * 127.5);
    image.setRGB(x, y, rgb);
    }
    }
    ImageIO.write(image, "png", new File("noise.png"));

    }
    }