001package squidpony.squidmath; 002 003import java.io.Serializable; 004 005import static squidpony.squidmath.Noise.fastFloor; 006 007/** 008 * A Noise class that's here for compatibility; it extends {@link SeededNoise} and delegates to it for all methods 009 * except {@link #noiseAlt(double, double)} and {@link #noiseAlt(double, double, double)}. Normally you should use 010 * SeededNoise directly for new code if you expect to mostly use the inner classes in {@link Noise} for special effects, 011 * or {@link FastNoise} if you want the effects all in one place or to use {@code float} instead of {@code double}. 012 * <br> 013 * Created by Tommy Ettinger on 12/14/2016. The technique for point hashing in the "noiseAlt" code is based closely on 014 * <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.140.3594&rep=rep1&type=pdf">this paper</a>, 015 * with credit to Andrew Kensler, Aaron Knoll and Peter Shirley. This technique is good, but it may be periodic in 016 * undesirable ways, and isn't much faster when implemented in Java than {@link IntPointHash}, if at all. 017 */ 018public class WhirlingNoise extends SeededNoise implements Noise.Noise2D, Noise.Noise3D, Noise.Noise4D, Noise.Noise6D, 019 Serializable { 020 021 private static final long serialVersionUID = 6L; 022 public static final WhirlingNoise instance = new WhirlingNoise(); 023 public WhirlingNoise() 024 { 025 this(123456789); 026 } 027 public WhirlingNoise(long seed) { 028 super(seed); 029// System.out.println("{"); 030// for (int i = 0; i < grad3f.length; i++) { 031// System.out.printf("{% 2.15ff, % 2.15ff, % 2.15ff},\n", grad3f[i][0], grad3f[i][1], grad3f[i][2]); 032// } 033// System.out.println("}"); 034 } 035 036 /** 037 * The 32 3D vertices of a rhombic triacontahedron. These were modified from values taken from Vladimir Bulatov's 038 * stellation applet, which has available source but is unlicensed, and is 039 * <a href="http://www.bulatov.org/polyhedra/stellation_applet/index.html">available here</a>, but the vertices are 040 * mathematical constants so copyright isn't an issue. 041 */ 042 protected static final float[][] grad3f = 043 { 044 {-0.448549002408981f, 1.174316525459290f, 0.000000000000001f}, 045 { 0.000000000000001f, 1.069324374198914f, 0.660878777503967f}, 046 { 0.448549002408981f, 1.174316525459290f, 0.000000000000001f}, 047 { 0.000000000000001f, 1.069324374198914f, -0.660878777503967f}, 048 {-0.725767493247986f, 0.725767493247986f, -0.725767493247986f}, 049 {-1.069324374198914f, 0.660878777503967f, 0.000000000000001f}, 050 {-0.725767493247986f, 0.725767493247986f, 0.725767493247986f}, 051 { 0.725767493247986f, 0.725767493247986f, 0.725767493247986f}, 052 { 1.069324374198914f, 0.660878777503967f, 0.000000000000000f}, 053 { 0.725767493247986f, 0.725767493247986f, -0.725767493247986f}, 054 {-0.660878777503967f, 0.000000000000003f, -1.069324374198914f}, 055 {-1.174316525459290f, 0.000000000000003f, -0.448549002408981f}, 056 { 0.000000000000000f, 0.448549002408981f, -1.174316525459290f}, 057 {-0.660878777503967f, 0.000000000000001f, 1.069324374198914f}, 058 { 0.000000000000001f, 0.448549002408981f, 1.174316525459290f}, 059 {-1.174316525459290f, 0.000000000000001f, 0.448549002408981f}, 060 { 0.660878777503967f, 0.000000000000001f, 1.069324374198914f}, 061 { 1.174316525459290f, 0.000000000000001f, 0.448549002408981f}, 062 { 0.660878777503967f, 0.000000000000001f, -1.069324374198914f}, 063 { 1.174316525459290f, 0.000000000000001f, -0.448549002408981f}, 064 {-0.725767493247986f, -0.725767493247986f, -0.725767493247986f}, 065 {-1.069324374198914f, -0.660878777503967f, -0.000000000000001f}, 066 {-0.000000000000001f, -0.448549002408981f, -1.174316525459290f}, 067 {-0.000000000000001f, -0.448549002408981f, 1.174316525459290f}, 068 {-0.725767493247986f, -0.725767493247986f, 0.725767493247986f}, 069 { 0.725767493247986f, -0.725767493247986f, 0.725767493247986f}, 070 { 1.069324374198914f, -0.660878777503967f, 0.000000000000001f}, 071 { 0.725767493247986f, -0.725767493247986f, -0.725767493247986f}, 072 {-0.000000000000004f, -1.069324374198914f, -0.660878777503967f}, 073 {-0.448549002408981f, -1.174316525459290f, -0.000000000000003f}, 074 {-0.000000000000003f, -1.069324374198914f, 0.660878777503967f}, 075 { 0.448549002408981f, -1.174316525459290f, 0.000000000000003f}, 076 }; 077 078 // public static void randomUnitVector4(long seed, final float[] vector) 079// { 080// double mag = 0.0; 081// float t; 082// vector[0] = (t = NumberTools.formCurvedFloat(seed += 0xCB72F6C7)); 083// mag += t * t; 084// vector[1] = (t = NumberTools.formCurvedFloat(seed += 0xCB72F6C7)); 085// mag += t * t; 086// vector[2] = (t = NumberTools.formCurvedFloat(seed += 0xCB72F6C7)); 087// mag += t * t; 088// vector[3] = (t = NumberTools.formCurvedFloat(seed + 0xCB72F6C7)); 089// mag += t * t; 090// 091// if(mag == 0) 092// { 093// vector[0] = 1f; 094// mag = 1.0; 095// } 096// else 097// mag = Math.sqrt(mag); 098// vector[0] /= mag; 099// vector[1] /= mag; 100// vector[2] /= mag; 101// vector[3] /= mag; 102// } 103 104// static { 105// final float len = (float) (1.7861513777574233 / Math.sqrt(3.0)); 106// for (int i = 0; i < 64; i++) { 107//// float x = grad4f[i][0], y = grad4f[i][1], z = grad4f[i][2], w = grad4f[i][3]; 108//// final float len = 1.4142135623730951f / (float)Math.sqrt(x * x + y * y + z * z + w * w); 109// //final float len = 2f / Math.max(Math.abs(x), Math.max(Math.abs(y), Math.abs(z))), len3 = len * 1.5f; 110// grad4f[i][0] *= len; 111// grad4f[i][1] *= len; 112// grad4f[i][2] *= len; 113// grad4f[i][3] *= len; 114// System.out.println("{" + squidpony.StringKit.join(", ", grad4f[i]) + "},"); 115// } 116// } 117 118// protected static final float[][] phiGrad3f = new float[96][3]; 119// 120// static { 121// final float root2 = 1.2599211f; 122// int i = 0; 123// for (; i < 16; i++) { 124// phiGrad3f[i][0] = phiGrad2f[i & 15][0] * root2; 125// phiGrad3f[i][1] = phiGrad2f[i & 15][1] * root2; 126// } 127// for (; i < 32; i++) { 128// phiGrad3f[i][0] = phiGrad2f[i & 15][1] * root2; 129// phiGrad3f[i][1] = phiGrad2f[i & 15][0] * root2; 130// } 131// for (; i < 48; i++) { 132// phiGrad3f[i][0] = phiGrad2f[i & 15][0] * root2; 133// phiGrad3f[i][2] = phiGrad2f[i & 15][1] * root2; 134// } 135// for (; i < 64; i++) { 136// phiGrad3f[i][0] = phiGrad2f[i & 15][1] * root2; 137// phiGrad3f[i][2] = phiGrad2f[i & 15][0] * root2; 138// } 139// for (; i < 80; i++) { 140// phiGrad3f[i][1] = phiGrad2f[i & 15][0] * root2; 141// phiGrad3f[i][2] = phiGrad2f[i & 15][1] * root2; 142// } 143// for (; i < 96; i++) { 144// phiGrad3f[i][1] = phiGrad2f[i & 15][1] * root2; 145// phiGrad3f[i][2] = phiGrad2f[i & 15][0] * root2; 146// } 147// } 148 149// public static final int[] 150// perm_x = {59, 146, 27, 99, 226, 210, 44, 129, 102, 237, 2, 107, 157, 173, 159, 16, 128, 41, 228, 114, 63, 105, 241, 144, 187, 116, 223, 122, 234, 52, 96, 35, 213, 176, 177, 141, 132, 240, 194, 163, 0, 3, 168, 133, 55, 203, 53, 50, 42, 79, 130, 156, 209, 135, 151, 178, 85, 154, 117, 148, 140, 82, 6, 69, 127, 214, 95, 175, 46, 30, 104, 197, 170, 33, 70, 167, 217, 233, 219, 84, 196, 109, 40, 190, 123, 165, 61, 212, 255, 184, 19, 182, 38, 112, 172, 103, 25, 244, 245, 201, 192, 60, 14, 231, 68, 71, 236, 193, 115, 7, 113, 118, 110, 131, 198, 216, 29, 195, 211, 246, 153, 222, 185, 208, 200, 158, 66, 137, 179, 26, 147, 235, 106, 90, 164, 9, 238, 101, 138, 227, 21, 37, 23, 152, 8, 161, 108, 250, 183, 225, 121, 24, 51, 252, 87, 242, 98, 188, 232, 171, 93, 56, 57, 5, 12, 120, 74, 43, 136, 139, 32, 13, 191, 67, 189, 186, 162, 199, 10, 20, 89, 15, 31, 58, 221, 18, 253, 28, 4, 218, 142, 205, 247, 94, 215, 39, 166, 150, 224, 77, 34, 169, 206, 47, 81, 97, 83, 220, 76, 229, 160, 54, 243, 45, 181, 92, 119, 48, 155, 62, 174, 248, 36, 239, 145, 124, 125, 65, 72, 180, 134, 111, 204, 207, 100, 73, 251, 143, 249, 254, 230, 11, 78, 80, 149, 75, 91, 126, 17, 86, 49, 88, 64, 22, 202, 1}, 151// perm_y = {189, 111, 17, 214, 57, 208, 191, 225, 241, 152, 145, 71, 2, 141, 183, 218, 66, 178, 34, 161, 198, 47, 200, 180, 134, 239, 162, 18, 155, 216, 192, 173, 219, 9, 51, 124, 95, 122, 217, 135, 31, 50, 179, 237, 32, 39, 209, 112, 96, 92, 68, 79, 228, 193, 234, 90, 164, 137, 196, 184, 185, 114, 226, 67, 249, 163, 85, 26, 125, 28, 251, 45, 61, 220, 213, 139, 70, 201, 243, 22, 142, 246, 102, 229, 10, 107, 4, 240, 194, 35, 230, 86, 223, 20, 12, 233, 23, 77, 119, 176, 147, 182, 21, 195, 91, 118, 247, 33, 100, 99, 29, 188, 172, 144, 136, 131, 40, 13, 38, 150, 224, 205, 8, 252, 253, 190, 46, 143, 53, 231, 153, 94, 177, 88, 55, 105, 121, 16, 25, 207, 138, 5, 63, 82, 202, 58, 170, 41, 78, 167, 64, 60, 14, 103, 42, 154, 19, 80, 72, 37, 83, 129, 187, 244, 215, 242, 81, 15, 151, 186, 59, 101, 168, 175, 89, 248, 232, 212, 204, 199, 108, 73, 98, 210, 44, 1, 76, 48, 49, 250, 106, 203, 113, 43, 221, 146, 245, 148, 115, 165, 181, 84, 93, 3, 206, 65, 123, 158, 6, 126, 238, 109, 130, 227, 140, 120, 74, 171, 110, 222, 87, 156, 132, 97, 159, 197, 255, 56, 27, 62, 157, 75, 211, 254, 127, 169, 236, 235, 149, 52, 36, 24, 0, 11, 160, 133, 174, 30, 104, 69, 128, 116, 117, 54, 166, 7}, 152// perm_z = {253, 212, 4, 237, 36, 182, 213, 233, 147, 239, 226, 41, 74, 65, 68, 165, 70, 231, 217, 116, 113, 193, 162, 112, 228, 254, 183, 176, 151, 80, 17, 60, 155, 246, 174, 3, 202, 208, 127, 7, 57, 1, 132, 79, 224, 99, 238, 195, 236, 9, 115, 154, 23, 227, 76, 158, 130, 16, 89, 214, 61, 114, 187, 90, 49, 24, 64, 33, 96, 242, 25, 37, 215, 35, 46, 109, 134, 141, 136, 225, 138, 43, 21, 184, 189, 13, 230, 188, 40, 50, 243, 244, 211, 156, 85, 120, 223, 58, 234, 71, 6, 28, 179, 67, 125, 69, 192, 131, 44, 175, 34, 15, 32, 77, 191, 222, 83, 47, 128, 218, 198, 84, 149, 26, 121, 190, 255, 150, 117, 92, 140, 101, 172, 62, 93, 97, 27, 103, 106, 161, 194, 201, 204, 45, 206, 111, 81, 252, 249, 73, 42, 248, 108, 118, 63, 56, 31, 216, 153, 180, 19, 126, 38, 139, 66, 88, 247, 143, 177, 137, 12, 199, 104, 235, 102, 75, 100, 129, 251, 18, 159, 107, 196, 22, 10, 152, 209, 94, 181, 250, 51, 210, 185, 144, 200, 169, 232, 122, 145, 173, 95, 171, 166, 229, 14, 5, 0, 105, 2, 163, 157, 48, 53, 133, 110, 52, 160, 186, 123, 124, 91, 20, 221, 240, 87, 178, 98, 207, 142, 148, 59, 203, 245, 205, 72, 11, 164, 39, 170, 135, 168, 197, 55, 86, 219, 167, 8, 82, 78, 220, 29, 146, 241, 54, 119, 30}, 153// perm_w = {57, 1, 140, 48, 61, 156, 230, 173, 2, 231, 12, 214, 142, 242, 255, 195, 198, 220, 157, 139, 194, 99, 247, 248, 155, 178, 29, 41, 23, 193, 0, 30, 95, 171, 174, 222, 91, 54, 8, 67, 32, 129, 46, 124, 172, 148, 17, 105, 228, 118, 191, 33, 224, 5, 25, 158, 185, 92, 63, 199, 53, 107, 34, 180, 125, 69, 200, 116, 121, 216, 42, 233, 70, 43, 72, 26, 202, 62, 51, 15, 10, 16, 217, 207, 14, 175, 59, 52, 223, 246, 89, 109, 83, 13, 68, 90, 147, 239, 234, 18, 151, 114, 76, 143, 100, 197, 106, 176, 232, 208, 85, 165, 40, 186, 251, 101, 44, 65, 93, 218, 253, 144, 123, 11, 113, 167, 102, 240, 177, 137, 4, 184, 181, 20, 110, 37, 138, 111, 132, 94, 6, 122, 119, 75, 78, 84, 21, 3, 74, 235, 127, 112, 19, 58, 149, 161, 159, 73, 136, 150, 215, 35, 38, 86, 211, 190, 128, 203, 168, 9, 166, 244, 36, 28, 153, 225, 108, 254, 55, 169, 104, 141, 145, 22, 49, 212, 183, 79, 189, 227, 170, 60, 245, 205, 64, 252, 241, 80, 162, 97, 206, 163, 192, 146, 66, 182, 187, 135, 130, 152, 81, 71, 134, 39, 179, 188, 87, 126, 209, 229, 219, 133, 204, 210, 27, 103, 98, 154, 31, 250, 196, 7, 236, 77, 226, 56, 96, 88, 221, 45, 160, 50, 115, 237, 164, 201, 213, 82, 117, 24, 243, 131, 249, 47, 238, 120}, 154// perm_u = {132, 148, 19, 244, 162, 163, 194, 37, 4, 250, 198, 154, 170, 137, 6, 60, 123, 73, 138, 41, 145, 92, 61, 82, 251, 175, 57, 207, 153, 50, 113, 105, 106, 242, 253, 94, 128, 9, 164, 143, 234, 80, 160, 252, 136, 239, 232, 150, 89, 167, 100, 131, 127, 178, 31, 188, 217, 5, 27, 33, 119, 152, 83, 195, 72, 88, 223, 176, 110, 111, 134, 233, 200, 190, 130, 86, 102, 69, 202, 240, 63, 13, 70, 229, 93, 24, 241, 22, 191, 99, 245, 139, 85, 254, 53, 45, 46, 182, 185, 26, 76, 197, 104, 67, 174, 20, 108, 184, 68, 171, 172, 90, 29, 107, 32, 79, 59, 126, 211, 112, 157, 201, 215, 237, 51, 84, 23, 135, 12, 28, 155, 124, 42, 43, 74, 173, 140, 114, 78, 18, 34, 1, 142, 180, 243, 193, 2, 161, 25, 212, 181, 218, 115, 39, 177, 71, 17, 186, 249, 225, 226, 122, 117, 214, 8, 129, 44, 7, 98, 216, 40, 116, 0, 103, 96, 30, 209, 47, 236, 11, 247, 58, 151, 52, 81, 141, 147, 169, 255, 16, 219, 75, 192, 208, 87, 56, 230, 231, 14, 97, 64, 54, 10, 222, 238, 205, 66, 120, 183, 133, 206, 109, 213, 144, 121, 158, 55, 235, 125, 3, 221, 118, 189, 165, 166, 62, 49, 146, 196, 77, 224, 203, 38, 156, 228, 48, 204, 35, 36, 210, 149, 227, 168, 199, 179, 246, 91, 248, 21, 65, 95, 101, 187, 220, 159, 15}, 155// perm_v = {2, 9, 4, 237, 219, 73, 247, 203, 228, 220, 46, 229, 61, 156, 170, 75, 223, 144, 81, 252, 172, 208, 76, 218, 177, 103, 123, 244, 14, 39, 255, 90, 168, 43, 174, 3, 113, 107, 145, 233, 130, 254, 192, 11, 211, 190, 68, 105, 117, 178, 251, 18, 66, 242, 230, 248, 95, 137, 29, 26, 164, 65, 153, 120, 70, 77, 64, 33, 23, 133, 59, 7, 40, 16, 106, 41, 121, 216, 238, 135, 19, 212, 157, 48, 232, 28, 128, 22, 245, 171, 183, 56, 74, 99, 51, 150, 236, 111, 234, 71, 189, 167, 213, 37, 198, 50, 12, 79, 31, 250, 136, 165, 185, 246, 55, 86, 142, 62, 42, 52, 147, 205, 89, 94, 224, 141, 221, 180, 138, 129, 140, 101, 83, 193, 127, 67, 108, 84, 166, 109, 181, 20, 34, 195, 87, 24, 217, 116, 36, 88, 196, 82, 57, 239, 243, 124, 134, 175, 119, 210, 32, 163, 38, 139, 249, 227, 25, 97, 10, 118, 72, 131, 91, 54, 204, 225, 253, 58, 115, 154, 202, 122, 110, 112, 215, 1, 149, 146, 44, 201, 17, 240, 206, 197, 200, 169, 159, 13, 179, 143, 160, 152, 226, 161, 241, 80, 102, 15, 155, 92, 21, 184, 96, 148, 8, 158, 125, 35, 63, 176, 194, 235, 187, 30, 100, 231, 98, 207, 53, 47, 93, 173, 78, 186, 132, 199, 151, 114, 0, 45, 49, 126, 191, 222, 6, 182, 162, 188, 27, 69, 209, 214, 104, 5, 85, 60}; 156 157 // protected static double dot(final float g[], final double x, final double y, final double z) { 158// return g[0] * x + g[1] * y + g[2] * z; 159// } 160// protected static double dot(final float g[], final double x, final double y, final double z, final double w) { 161// return g[0] * x + g[1] * y + g[2] * z + g[3] * w; 162// } 163 164 protected static float dotf(final float[] g, final float x, final float y, final float z) { 165 return g[0] * x + g[1] * y + g[2] * z; 166 } 167 168 /** 169 * Delegates to {@link SeededNoise#noise(double, double, long)} with the initial seed given to this object. 170 * 171 * @param x X input 172 * @param y Y input 173 * @return noise from -1.0 to 1.0, inclusive 174 */ 175 public double getNoise(final double x, final double y) { 176 return noise(x, y, defaultSeed); 177 } 178 179 /** 180 * Delegates to {@link SeededNoise#noise(double, double, long)}. 181 * 182 * @param x X input 183 * @param y Y input 184 * @param seed will completely alter the shape of the noise if changed between calls 185 * @return noise from -1.0 to 1.0, inclusive 186 */ 187 public double getNoiseWithSeed(final double x, final double y, final long seed) { 188 return noise(x, y, seed); 189 } 190 /** 191 * Delegates to {@link SeededNoise#noise(double, double, double, long)} with the initial seed given to this object. 192 * 193 * @param x X input 194 * @param y Y input 195 * @param z Z input 196 * @return noise from -1.0 to 1.0, inclusive 197 */ 198 public double getNoise(final double x, final double y, final double z) { 199 return noise(x, y, z, defaultSeed); 200 } 201 /** 202 * Delegates to {@link SeededNoise#noise(double, double, double, long)}. 203 * 204 * @param x X input 205 * @param y Y input 206 * @param z Z input 207 * @param seed will completely alter the shape of the noise if changed between calls 208 * @return noise from -1.0 to 1.0, inclusive 209 */ 210 public double getNoiseWithSeed(final double x, final double y, final double z, final long seed) { 211 return noise(x, y, z, seed); 212 } 213 214 /** 215 * Delegates to {@link SeededNoise#noise(double, double, double, double, long)} with the initial seed given to this object. 216 * 217 * @param x X input 218 * @param y Y input 219 * @param z Z input 220 * @param w W input (fourth-dimension) 221 * @return noise from -1.0 to 1.0, inclusive 222 */ 223 public double getNoise(final double x, final double y, final double z, final double w) { 224 return noise(x, y, z, w, defaultSeed); 225 } 226 /** 227 * Delegates to {@link SeededNoise#noise(double, double, double, double, long)}. 228 * @param x X input 229 * @param y Y input 230 * @param z Z input 231 * @param w W input (fourth-dimension) 232 * @param seed will completely alter the shape of the noise if changed between calls 233 * @return noise from -1.0 to 1.0, inclusive 234 */ 235 public double getNoiseWithSeed(final double x, final double y, final double z, final double w, final long seed) { 236 return noise(x, y, z, w, seed); 237 } 238 239 240 /** 241 * Delegates to {@link SeededNoise#noise(double, double, long)} with 123456789 as the seed. 242 * 243 * @param xin X input 244 * @param yin Y input 245 * @return noise from -1.0 to 1.0, inclusive 246 */ 247 public static double noise(final double xin, final double yin){ 248 return SeededNoise.noise(xin, yin, 123456789); 249 } 250 251 /** 252 * Delegates to {@link SeededNoise#noise(double, double, long)}. 253 * 254 * @param xin X input 255 * @param yin Y input 256 * @param seed will completely alter the shape of the noise if changed between calls 257 * @return noise from -1.0 to 1.0, inclusive 258 */ 259 public static double noise(final double xin, final double yin, final long seed) { 260 return SeededNoise.noise(xin, yin, seed); 261 } 262 263 264 265 /** 266 * Delegates to {@link SeededNoise#noise(double, double, double, long)} with 123456789 as the seed. 267 * @param xin X input 268 * @param yin Y input 269 * @param zin Z input 270 * @return noise from -1.0 to 1.0, inclusive 271 */ 272 public static double noise(final double xin, final double yin, final double zin){ 273 return SeededNoise.noise(xin, yin, zin, 123456789); 274 } 275 276 /** 277 * Delegates to {@link SeededNoise#noise(double, double, double, long)}. 278 * @param xin X input 279 * @param yin Y input 280 * @param zin Z input 281 * @param seed will completely alter the shape of the noise if changed between calls 282 * @return noise from -1.0 to 1.0, inclusive 283 */ 284 public static double noise(final double xin, final double yin, final double zin, final long seed){ 285 return SeededNoise.noise(xin, yin, zin, seed); 286 } 287 288 /** 289 * Delegates to {@link SeededNoise#noise(double, double, double, double, long)} with 123456789 as the seed. 290 * @param x X input 291 * @param y Y input 292 * @param z Z input 293 * @param w W input (fourth-dimensional) 294 * @return noise from -1.0 to 1.0, inclusive 295 */ 296 public static double noise(final double x, final double y, final double z, final double w) { 297 return SeededNoise.noise(x, y, z, w, 123456789); 298 } 299 300 /** 301 * Delegates to {@link SeededNoise#noise(double, double, double, double, long)}. 302 * @param x X input 303 * @param y Y input 304 * @param z Z input 305 * @param w W input (fourth-dimensional) 306 * @param seed will completely alter the shape of the noise if changed between calls 307 * @return noise from -1.0 to 1.0, inclusive 308 */ 309 public static double noise(final double x, final double y, final double z, final double w, final long seed) { 310 return SeededNoise.noise(x, y, z, w, seed); 311 } 312 313 314// public static double noise(final double x, final double y, final double z, final double w, final long seed) 315// { 316// // The skewing and unskewing factors are hairy again for the 4D case 317// 318// // Skew the (x,y,z,w) space to figure out which cell of 24 simplices 319// // we're in 320// double s = (x + y + z + w) * F4; // Factor for 4D skewing 321// int i = fastFloor(x + s); 322// int j = fastFloor(y + s); 323// int k = fastFloor(z + s); 324// int l = fastFloor(w + s); 325// double t = (i + j + k + l) * G4; // Factor for 4D unskewing 326// double X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space 327// double Y0 = j - t; 328// double Z0 = k - t; 329// double W0 = l - t; 330// double x0 = x - X0; // The x,y,z,w distances from the cell origin 331// double y0 = y - Y0; 332// double z0 = z - Z0; 333// double w0 = w - W0; 334// // For the 4D case, the simplex is a 4D shape I won't even try to 335// // describe. 336// // To find out which of the 24 possible simplices we're in, we need 337// // to figure out the magnitude ordering of x0, y0, z0 and w0. 338// // The method below is a good way of finding the ordering of x,y,z,w 339// // and 340// // then find the correct traversal order for the simplex we’re in. 341// // First, six pair-wise comparisons are performed between each 342// // possible pair 343// // of the four coordinates, and the results are used to add up binary 344// // bits 345// // for an integer index. 346// int c = (x0 > y0 ? 32 : 0) | (x0 > z0 ? 16 : 0) | (y0 > z0 ? 8 : 0) | 347// (x0 > w0 ? 4 : 0) | (y0 > w0 ? 2 : 0) | (z0 > w0 ? 1 : 0); 348// 349// // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some 350// // order. 351// // Many values of c will never occur, since e.g. x>y>z>w makes x<z, 352// // y<w and x<w 353// // impossible. Only the 24 indices which have non-zero entries make 354// // any sense. 355// // We use a thresholding to set the coordinates in turn from the 356// // largest magnitude. 357// // The number 3 in the "simplex" array is at the position of the 358// // largest coordinate. 359// 360// // The integer offsets for the second simplex corner 361// int i1 = simplex[c][0] >= 3 ? 1 : 0; 362// int j1 = simplex[c][1] >= 3 ? 1 : 0; 363// int k1 = simplex[c][2] >= 3 ? 1 : 0; 364// int l1 = simplex[c][3] >= 3 ? 1 : 0; 365// // The number 2 in the "simplex" array is at the second largest 366// // coordinate. 367// 368// // The integer offsets for the third simplex corner 369// int i2 = simplex[c][0] >= 2 ? 1 : 0; 370// int j2 = simplex[c][1] >= 2 ? 1 : 0; 371// int k2 = simplex[c][2] >= 2 ? 1 : 0; 372// int l2 = simplex[c][3] >= 2 ? 1 : 0; 373// // The number 1 in the "simplex" array is at the second smallest 374// // coordinate. 375// 376// // The integer offsets for the fourth simplex corner 377// int i3 = simplex[c][0] >= 1 ? 1 : 0; 378// int j3 = simplex[c][1] >= 1 ? 1 : 0; 379// int k3 = simplex[c][2] >= 1 ? 1 : 0; 380// int l3 = simplex[c][3] >= 1 ? 1 : 0; 381// // The fifth corner has all coordinate offsets = 1, so no need to 382// // look that up. 383// double x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords 384// double y1 = y0 - j1 + G4; 385// double z1 = z0 - k1 + G4; 386// double w1 = w0 - l1 + G4; 387// double x2 = x0 - i2 + 2.0 * G4; // Offsets for third corner in (x,y,z,w) coords 388// double y2 = y0 - j2 + 2.0 * G4; 389// double z2 = z0 - k2 + 2.0 * G4; 390// double w2 = w0 - l2 + 2.0 * G4; 391// double x3 = x0 - i3 + 3.0 * G4; // Offsets for fourth corner in (x,y,z,w) coords 392// double y3 = y0 - j3 + 3.0 * G4; 393// double z3 = z0 - k3 + 3.0 * G4; 394// double w3 = w0 - l3 + 3.0 * G4; 395// double x4 = x0 - 1.0 + 4.0 * G4; // Offsets for last corner in (x,y,z,w) coords 396// double y4 = y0 - 1.0 + 4.0 * G4; 397// double z4 = z0 - 1.0 + 4.0 * G4; 398// double w4 = w0 - 1.0 + 4.0 * G4; 399// 400// final int s0 = (int)(seed & 63), s1 = (int)(seed >>> 6 & 63), s2 = (int)(seed >>> 12 & 63), s3 = (int)(seed >>> 18 & 63); 401// final int gi0 = (perm_x[(i) + s0 & 255] ^ perm_y[(j) + s1 & 255] ^ perm_z[(k) + s2 & 255] ^ perm_w[(l) + s3 & 255]) & 63; 402// final int gi1 = (perm_x[(i + i1) + s0 & 255] ^ perm_y[(j + j1) + s1 & 255] ^ perm_z[(k + k1) + s2 & 255] ^ perm_w[(l + l1) + s3 & 255]) & 63; 403// final int gi2 = (perm_x[(i + i2) + s0 & 255] ^ perm_y[(j + j2) + s1 & 255] ^ perm_z[(k + k2) + s2 & 255] ^ perm_w[(l + l2) + s3 & 255]) & 63; 404// final int gi3 = (perm_x[(i + i3) + s0 & 255] ^ perm_y[(j + j3) + s1 & 255] ^ perm_z[(k + k3) + s2 & 255] ^ perm_w[(l + l3) + s3 & 255]) & 63; 405// final int gi4 = (perm_x[(i + 1) + s0 & 255] ^ perm_y[(j + 1) + s1 & 255] ^ perm_z[(k + 1) + s2 & 255] ^ perm_w[(l + 1) + s3 & 255]) & 63; 406// // Noise contributions from the five corners are n0 to n4 407// 408// // Calculate the contribution from the five corners 409// double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0, n0; 410// if (t0 < 0) { 411// n0 = 0.0; 412// } else { 413// t0 *= t0; 414// n0 = t0 * t0 * dot(grad4f[gi0], x0, y0, z0, w0); 415// } 416// double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1, n1; 417// if (t1 < 0) { 418// n1 = 0.0; 419// } else { 420// t1 *= t1; 421// n1 = t1 * t1 * dot(grad4f[gi1], x1, y1, z1, w1); 422// } 423// double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2, n2; 424// if (t2 < 0) { 425// n2 = 0.0; 426// } else { 427// t2 *= t2; 428// n2 = t2 * t2 * dot(grad4f[gi2], x2, y2, z2, w2); 429// } 430// double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3, n3; 431// if (t3 < 0) { 432// n3 = 0.0; 433// } else { 434// t3 *= t3; 435// n3 = t3 * t3 * dot(grad4f[gi3], x3, y3, z3, w3); 436// } 437// double t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4, n4; 438// if (t4 < 0) { 439// n4 = 0.0; 440// } else { 441// t4 *= t4; 442// n4 = t4 * t4 * dot(grad4f[gi4], x4, y4, z4, w4); 443// } 444// // Sum up and scale the result to cover the range [-1,1] 445// return 17.0 * (n0 + n1 + n2 + n3 + n4); 446// } 447// The 6 permutation tables were created with the code in the commented static block after these definitions. 448public static final int[] 449 perm_x = {59, 146, 27, 99, 226, 210, 44, 129, 102, 237, 2, 107, 157, 173, 159, 16, 128, 41, 228, 114, 63, 105, 241, 144, 187, 116, 223, 122, 234, 52, 96, 35, 213, 176, 177, 141, 132, 240, 194, 163, 0, 3, 168, 133, 55, 203, 53, 50, 42, 79, 130, 156, 209, 135, 151, 178, 85, 154, 117, 148, 140, 82, 6, 69, 127, 214, 95, 175, 46, 30, 104, 197, 170, 33, 70, 167, 217, 233, 219, 84, 196, 109, 40, 190, 123, 165, 61, 212, 255, 184, 19, 182, 38, 112, 172, 103, 25, 244, 245, 201, 192, 60, 14, 231, 68, 71, 236, 193, 115, 7, 113, 118, 110, 131, 198, 216, 29, 195, 211, 246, 153, 222, 185, 208, 200, 158, 66, 137, 179, 26, 147, 235, 106, 90, 164, 9, 238, 101, 138, 227, 21, 37, 23, 152, 8, 161, 108, 250, 183, 225, 121, 24, 51, 252, 87, 242, 98, 188, 232, 171, 93, 56, 57, 5, 12, 120, 74, 43, 136, 139, 32, 13, 191, 67, 189, 186, 162, 199, 10, 20, 89, 15, 31, 58, 221, 18, 253, 28, 4, 218, 142, 205, 247, 94, 215, 39, 166, 150, 224, 77, 34, 169, 206, 47, 81, 97, 83, 220, 76, 229, 160, 54, 243, 45, 181, 92, 119, 48, 155, 62, 174, 248, 36, 239, 145, 124, 125, 65, 72, 180, 134, 111, 204, 207, 100, 73, 251, 143, 249, 254, 230, 11, 78, 80, 149, 75, 91, 126, 17, 86, 49, 88, 64, 22, 202, 1}, 450 perm_y = {189, 111, 17, 214, 57, 208, 191, 225, 241, 152, 145, 71, 2, 141, 183, 218, 66, 178, 34, 161, 198, 47, 200, 180, 134, 239, 162, 18, 155, 216, 192, 173, 219, 9, 51, 124, 95, 122, 217, 135, 31, 50, 179, 237, 32, 39, 209, 112, 96, 92, 68, 79, 228, 193, 234, 90, 164, 137, 196, 184, 185, 114, 226, 67, 249, 163, 85, 26, 125, 28, 251, 45, 61, 220, 213, 139, 70, 201, 243, 22, 142, 246, 102, 229, 10, 107, 4, 240, 194, 35, 230, 86, 223, 20, 12, 233, 23, 77, 119, 176, 147, 182, 21, 195, 91, 118, 247, 33, 100, 99, 29, 188, 172, 144, 136, 131, 40, 13, 38, 150, 224, 205, 8, 252, 253, 190, 46, 143, 53, 231, 153, 94, 177, 88, 55, 105, 121, 16, 25, 207, 138, 5, 63, 82, 202, 58, 170, 41, 78, 167, 64, 60, 14, 103, 42, 154, 19, 80, 72, 37, 83, 129, 187, 244, 215, 242, 81, 15, 151, 186, 59, 101, 168, 175, 89, 248, 232, 212, 204, 199, 108, 73, 98, 210, 44, 1, 76, 48, 49, 250, 106, 203, 113, 43, 221, 146, 245, 148, 115, 165, 181, 84, 93, 3, 206, 65, 123, 158, 6, 126, 238, 109, 130, 227, 140, 120, 74, 171, 110, 222, 87, 156, 132, 97, 159, 197, 255, 56, 27, 62, 157, 75, 211, 254, 127, 169, 236, 235, 149, 52, 36, 24, 0, 11, 160, 133, 174, 30, 104, 69, 128, 116, 117, 54, 166, 7}, 451 perm_z = {253, 212, 4, 237, 36, 182, 213, 233, 147, 239, 226, 41, 74, 65, 68, 165, 70, 231, 217, 116, 113, 193, 162, 112, 228, 254, 183, 176, 151, 80, 17, 60, 155, 246, 174, 3, 202, 208, 127, 7, 57, 1, 132, 79, 224, 99, 238, 195, 236, 9, 115, 154, 23, 227, 76, 158, 130, 16, 89, 214, 61, 114, 187, 90, 49, 24, 64, 33, 96, 242, 25, 37, 215, 35, 46, 109, 134, 141, 136, 225, 138, 43, 21, 184, 189, 13, 230, 188, 40, 50, 243, 244, 211, 156, 85, 120, 223, 58, 234, 71, 6, 28, 179, 67, 125, 69, 192, 131, 44, 175, 34, 15, 32, 77, 191, 222, 83, 47, 128, 218, 198, 84, 149, 26, 121, 190, 255, 150, 117, 92, 140, 101, 172, 62, 93, 97, 27, 103, 106, 161, 194, 201, 204, 45, 206, 111, 81, 252, 249, 73, 42, 248, 108, 118, 63, 56, 31, 216, 153, 180, 19, 126, 38, 139, 66, 88, 247, 143, 177, 137, 12, 199, 104, 235, 102, 75, 100, 129, 251, 18, 159, 107, 196, 22, 10, 152, 209, 94, 181, 250, 51, 210, 185, 144, 200, 169, 232, 122, 145, 173, 95, 171, 166, 229, 14, 5, 0, 105, 2, 163, 157, 48, 53, 133, 110, 52, 160, 186, 123, 124, 91, 20, 221, 240, 87, 178, 98, 207, 142, 148, 59, 203, 245, 205, 72, 11, 164, 39, 170, 135, 168, 197, 55, 86, 219, 167, 8, 82, 78, 220, 29, 146, 241, 54, 119, 30}, 452 perm_w = {57, 1, 140, 48, 61, 156, 230, 173, 2, 231, 12, 214, 142, 242, 255, 195, 198, 220, 157, 139, 194, 99, 247, 248, 155, 178, 29, 41, 23, 193, 0, 30, 95, 171, 174, 222, 91, 54, 8, 67, 32, 129, 46, 124, 172, 148, 17, 105, 228, 118, 191, 33, 224, 5, 25, 158, 185, 92, 63, 199, 53, 107, 34, 180, 125, 69, 200, 116, 121, 216, 42, 233, 70, 43, 72, 26, 202, 62, 51, 15, 10, 16, 217, 207, 14, 175, 59, 52, 223, 246, 89, 109, 83, 13, 68, 90, 147, 239, 234, 18, 151, 114, 76, 143, 100, 197, 106, 176, 232, 208, 85, 165, 40, 186, 251, 101, 44, 65, 93, 218, 253, 144, 123, 11, 113, 167, 102, 240, 177, 137, 4, 184, 181, 20, 110, 37, 138, 111, 132, 94, 6, 122, 119, 75, 78, 84, 21, 3, 74, 235, 127, 112, 19, 58, 149, 161, 159, 73, 136, 150, 215, 35, 38, 86, 211, 190, 128, 203, 168, 9, 166, 244, 36, 28, 153, 225, 108, 254, 55, 169, 104, 141, 145, 22, 49, 212, 183, 79, 189, 227, 170, 60, 245, 205, 64, 252, 241, 80, 162, 97, 206, 163, 192, 146, 66, 182, 187, 135, 130, 152, 81, 71, 134, 39, 179, 188, 87, 126, 209, 229, 219, 133, 204, 210, 27, 103, 98, 154, 31, 250, 196, 7, 236, 77, 226, 56, 96, 88, 221, 45, 160, 50, 115, 237, 164, 201, 213, 82, 117, 24, 243, 131, 249, 47, 238, 120}, 453 perm_u = {132, 148, 19, 244, 162, 163, 194, 37, 4, 250, 198, 154, 170, 137, 6, 60, 123, 73, 138, 41, 145, 92, 61, 82, 251, 175, 57, 207, 153, 50, 113, 105, 106, 242, 253, 94, 128, 9, 164, 143, 234, 80, 160, 252, 136, 239, 232, 150, 89, 167, 100, 131, 127, 178, 31, 188, 217, 5, 27, 33, 119, 152, 83, 195, 72, 88, 223, 176, 110, 111, 134, 233, 200, 190, 130, 86, 102, 69, 202, 240, 63, 13, 70, 229, 93, 24, 241, 22, 191, 99, 245, 139, 85, 254, 53, 45, 46, 182, 185, 26, 76, 197, 104, 67, 174, 20, 108, 184, 68, 171, 172, 90, 29, 107, 32, 79, 59, 126, 211, 112, 157, 201, 215, 237, 51, 84, 23, 135, 12, 28, 155, 124, 42, 43, 74, 173, 140, 114, 78, 18, 34, 1, 142, 180, 243, 193, 2, 161, 25, 212, 181, 218, 115, 39, 177, 71, 17, 186, 249, 225, 226, 122, 117, 214, 8, 129, 44, 7, 98, 216, 40, 116, 0, 103, 96, 30, 209, 47, 236, 11, 247, 58, 151, 52, 81, 141, 147, 169, 255, 16, 219, 75, 192, 208, 87, 56, 230, 231, 14, 97, 64, 54, 10, 222, 238, 205, 66, 120, 183, 133, 206, 109, 213, 144, 121, 158, 55, 235, 125, 3, 221, 118, 189, 165, 166, 62, 49, 146, 196, 77, 224, 203, 38, 156, 228, 48, 204, 35, 36, 210, 149, 227, 168, 199, 179, 246, 91, 248, 21, 65, 95, 101, 187, 220, 159, 15}, 454 perm_v = {2, 9, 4, 237, 219, 73, 247, 203, 228, 220, 46, 229, 61, 156, 170, 75, 223, 144, 81, 252, 172, 208, 76, 218, 177, 103, 123, 244, 14, 39, 255, 90, 168, 43, 174, 3, 113, 107, 145, 233, 130, 254, 192, 11, 211, 190, 68, 105, 117, 178, 251, 18, 66, 242, 230, 248, 95, 137, 29, 26, 164, 65, 153, 120, 70, 77, 64, 33, 23, 133, 59, 7, 40, 16, 106, 41, 121, 216, 238, 135, 19, 212, 157, 48, 232, 28, 128, 22, 245, 171, 183, 56, 74, 99, 51, 150, 236, 111, 234, 71, 189, 167, 213, 37, 198, 50, 12, 79, 31, 250, 136, 165, 185, 246, 55, 86, 142, 62, 42, 52, 147, 205, 89, 94, 224, 141, 221, 180, 138, 129, 140, 101, 83, 193, 127, 67, 108, 84, 166, 109, 181, 20, 34, 195, 87, 24, 217, 116, 36, 88, 196, 82, 57, 239, 243, 124, 134, 175, 119, 210, 32, 163, 38, 139, 249, 227, 25, 97, 10, 118, 72, 131, 91, 54, 204, 225, 253, 58, 115, 154, 202, 122, 110, 112, 215, 1, 149, 146, 44, 201, 17, 240, 206, 197, 200, 169, 159, 13, 179, 143, 160, 152, 226, 161, 241, 80, 102, 15, 155, 92, 21, 184, 96, 148, 8, 158, 125, 35, 63, 176, 194, 235, 187, 30, 100, 231, 98, 207, 53, 47, 93, 173, 78, 186, 132, 199, 151, 114, 0, 45, 49, 126, 191, 222, 6, 182, 162, 188, 27, 69, 209, 214, 104, 5, 85, 60}; 455 456// static { 457// int s = 1; 458// for(int i = 0; i < 256; i++) { 459// System.out.print(((s = s * 53 + 3 & 255) ^ s >>> 4) + ", "); 460// } 461// System.out.println(); 462// s = 7; 463// for(int i = 0; i < 256; i++) { 464// System.out.print(((s = s * 61 + 11 & 255) ^ s >>> 4) + ", "); 465// } 466// System.out.println(); 467// s = 31; 468// for(int i = 0; i < 256; i++) { 469// System.out.print(((s = s * 29 + 111 & 255) ^ s >>> 4) + ", "); 470// } 471// System.out.println(); 472// s = 127; 473// for(int i = 0; i < 256; i++) { 474// System.out.print(((s = s * 101 + 31 & 255) ^ s >>> 4) + ", "); 475// } 476// System.out.println(); 477// s = 15; 478// for(int i = 0; i < 256; i++) { 479// System.out.print(((s = s * 37 + 97 & 255) ^ s >>> 4) + ", "); 480// } 481// System.out.println(); 482// s = 63; 483// for(int i = 0; i < 256; i++) { 484// System.out.print(((s = s * 109 + 47 & 255) ^ s >>> 4) + ", "); 485// } 486// 487// } 488 489 /** 490 * 2D simplex noise returning a float; extremely similar to {@link #noise(double, double)}, but this may be slightly 491 * faster or slightly slower. This uses its parameters verbatim, so you should apply frequency changes yourself. 492 * This also cannot take a seed, while {@link #noise(double, double, long)} can. 493 * @param x x input 494 * @param y y input 495 * @return noise from -1.0 to 1.0, inclusive 496 */ 497 public static float noiseAlt(double x, double y) { 498 //xin *= SCALE; 499 //yin *= SCALE; 500 float noise0, noise1, noise2; // from the three corners 501 float xin = (float)x, yin = (float)y; 502 // Skew the input space to figure out which simplex cell we're in 503 float skew = (xin + yin) * FastNoise.F2f; // Hairy factor for 2D 504 int i = fastFloor(xin + skew); 505 int j = fastFloor(yin + skew); 506 float t = (i + j) * FastNoise.G2f; 507 float X0 = i - t; // Unskew the cell origin back to (x,y) space 508 float Y0 = j - t; 509 float x0 = xin - X0; // The x,y distances from the cell origin 510 float y0 = yin - Y0; 511 // For the 2D case, the simplex shape is an equilateral triangle. 512 // determine which simplex we are in. 513 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) 514 // coords 515 if (x0 > y0) { 516 i1 = 1; 517 j1 = 0; 518 } // lower triangle, XY order: (0,0)->(1,0)->(1,1) 519 else { 520 i1 = 0; 521 j1 = 1; 522 } // upper triangle, YX order: (0,0)->(0,1)->(1,1) 523 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and 524 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), 525 // where: c = (3-sqrt(3))/6 526 float x1 = x0 - i1 + FastNoise.G2f; // Offsets for middle corner in (x,y) 527 // unskewed coords 528 float y1 = y0 - j1 + FastNoise.G2f; 529 float x2 = x0 - 1f + FastNoise.H2f; // Offsets for last corner in (x,y) 530 // unskewed coords 531 float y2 = y0 - 1f + FastNoise.H2f; 532 // Work out the hashed gradient indices of the three simplex corners 533// int gi0 = determine256(i + determine(j)); 534// int gi1 = determine256(i + i1 + determine(j + j1)); 535// int gi2 = determine256(i + 1 + determine(j + 1)); 536 final int gi0 = perm_x[i & 255] ^ perm_y[j & 255]; 537 final int gi1 = perm_x[i + i1 & 255] ^ perm_y[j + j1 & 255]; 538 final int gi2 = perm_x[i + 1 & 255] ^ perm_y[j + 1 & 255]; 539 540 // Calculate the contribution from the three corners 541 float t0 = 0.75f - x0 * x0 - y0 * y0; 542 if (t0 < 0) { 543 noise0 = 0.0f; 544 } else { 545 t0 *= t0; 546 noise0 = t0 * t0 * FastNoise.dotf(FastNoise.phiGrad2f[gi0], x0, y0); 547 // for 2D gradient 548 } 549 float t1 = 0.75f - x1 * x1 - y1 * y1; 550 if (t1 < 0) { 551 noise1 = 0.0f; 552 } else { 553 t1 *= t1; 554 noise1 = t1 * t1 * FastNoise.dotf(FastNoise.phiGrad2f[gi1], x1, y1); 555 } 556 float t2 = 0.75f - x2 * x2 - y2 * y2; 557 if (t2 < 0) { 558 noise2 = 0.0f; 559 } else { 560 t2 *= t2; 561 noise2 = t2 * t2 * FastNoise.dotf(FastNoise.phiGrad2f[gi2], x2, y2); 562 } 563 // Add contributions from each corner to get the final noise value. 564 // The result is clamped to return values in the interval [-1,1]. 565 return Math.max(-1f, Math.min(1f, 9.125f * (noise0 + noise1 + noise2))); 566 } 567 568 /** 569 * 3D simplex noise returning a float; extremely similar to {@link #noise(double, double, double)}, but this may 570 * be slightly faster or slightly slower. This uses its parameters verbatim, so you should apply frequency changes 571 * yourself. This also cannot take a seed, while {@link #noise(double, double, double, long)} can. 572 * 573 * @param x X input 574 * @param y Y input 575 * @param z Z input 576 * @return noise from -1.0 to 1.0, inclusive 577 */ 578 public static float noiseAlt(double x, double y, double z) { 579 //xin *= SCALE; 580 //yin *= SCALE; 581 //zin *= SCALE; 582 float xin = (float)x, yin = (float)y, zin = (float)z; 583 float n0, n1, n2, n3; // Noise contributions from the four corners 584 // Skew the input space to figure out which simplex cell we're in 585 float s = (xin + yin + zin) * FastNoise.F3f; // Very nice and simple skew 586 // factor for 3D 587 int i = fastFloor(xin + s); 588 int j = fastFloor(yin + s); 589 int k = fastFloor(zin + s); 590 float t = (i + j + k) * FastNoise.G3f; 591 float X0 = i - t; // Unskew the cell origin back to (x,y,z) space 592 float Y0 = j - t; 593 float Z0 = k - t; 594 float x0 = xin - X0; // The x,y,z distances from the cell origin 595 float y0 = yin - Y0; 596 float z0 = zin - Z0; 597 // For the 3D case, the simplex shape is a slightly irregular 598 // tetrahedron. 599 // determine which simplex we are in. 600 int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) 601 // coords 602 int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) 603 // coords 604 if (x0 >= y0) { 605 if (y0 >= z0) { 606 i1 = 1; 607 j1 = 0; 608 k1 = 0; 609 i2 = 1; 610 j2 = 1; 611 k2 = 0; 612 } // X Y Z order 613 else if (x0 >= z0) { 614 i1 = 1; 615 j1 = 0; 616 k1 = 0; 617 i2 = 1; 618 j2 = 0; 619 k2 = 1; 620 } // X Z Y order 621 else { 622 i1 = 0; 623 j1 = 0; 624 k1 = 1; 625 i2 = 1; 626 j2 = 0; 627 k2 = 1; 628 } // Z X Y order 629 } else { // x0<y0 630 if (y0 < z0) { 631 i1 = 0; 632 j1 = 0; 633 k1 = 1; 634 i2 = 0; 635 j2 = 1; 636 k2 = 1; 637 } // Z Y X order 638 else if (x0 < z0) { 639 i1 = 0; 640 j1 = 1; 641 k1 = 0; 642 i2 = 0; 643 j2 = 1; 644 k2 = 1; 645 } // Y Z X order 646 else { 647 i1 = 0; 648 j1 = 1; 649 k1 = 0; 650 i2 = 1; 651 j2 = 1; 652 k2 = 0; 653 } // Y X Z order 654 } 655 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in 656 // (x,y,z), 657 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in 658 // (x,y,z), and 659 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in 660 // (x,y,z), where 661 // c = 1/6. 662 float x1 = x0 - i1 + FastNoise.G3f; // Offsets for second corner in (x,y,z) 663 // coords 664 float y1 = y0 - j1 + FastNoise.G3f; 665 float z1 = z0 - k1 + FastNoise.G3f; 666 float x2 = x0 - i2 + FastNoise.F3f; // Offsets for third corner in 667 // (x,y,z) coords 668 float y2 = y0 - j2 + FastNoise.F3f; 669 float z2 = z0 - k2 + FastNoise.F3f; 670 float x3 = x0 - 0.5f; // Offsets for last corner in 671 // (x,y,z) coords 672 float y3 = y0 - 0.5f; 673 float z3 = z0 - 0.5f; 674 // Work out the hashed gradient indices of the four simplex corners 675 676 /* 677 int ii = i & 255; 678 int jj = j & 255; 679 int kk = k & 255; 680 681 int gi0 = perm[ii + perm[jj + perm[kk]]] % 12; 682 int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]] % 12; 683 int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]] % 12; 684 int gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 12; 685 */ 686// int gi0 = determine32(i + determine(j + determine(k))); 687// int gi1 = determine32(i + i1 + determine(j + j1 + determine(k + k1))); 688// int gi2 = determine32(i + i2 + determine(j + j2 + determine(k + k2))); 689// int gi3 = determine32(i + 1 + determine(j + 1 + determine(k + 1))); 690 final int gi0 = (perm_x[(i) & 255] ^ perm_y[(j) & 255] ^ perm_z[(k) + 67 & 255]) & 31; 691 final int gi1 = (perm_x[(i + i1) & 255] ^ perm_y[(j + j1) & 255] ^ perm_z[(k + k1) + 67 & 255]) & 31; 692 final int gi2 = (perm_x[(i + i2) & 255] ^ perm_y[(j + j2) & 255] ^ perm_z[(k + k2) + 67 & 255]) & 31; 693 final int gi3 = (perm_x[(i + 1) & 255] ^ perm_y[(j + 1) & 255] ^ perm_z[(k + 1) + 67 & 255]) & 31; 694 695 696// int gi0 = determineBounded(i + determine(j + determine(k)), 92); 697// int gi1 = determineBounded(i + i1 + determine(j + j1 + determine(k + k1)), 92); 698// int gi2 = determineBounded(i + i2 + determine(j + j2 + determine(k + k2)), 92); 699// int gi3 = determineBounded(i + 1 + determine(j + 1 + determine(k + 1)), 92); 700 701 /* 702 int hash = (int) rawNoise(i + ((j + k * 0x632BE5AB) * 0x9E3779B9), 703 i + i1 + ((j + j1 + (k + k1) * 0x632BE5AB) * 0x9E3779B9), 704 i + i2 + ((j + j2 + (k + k2) * 0x632BE5AB) * 0x9E3779B9), 705 i + 1 + ((j + 1 + ((k + 1) * 0x632BE5AB)) * 0x9E3779B9), 706 seed); 707 int gi0 = (hash >>>= 4) % 12; 708 int gi1 = (hash >>>= 4) % 12; 709 int gi2 = (hash >>>= 4) % 12; 710 int gi3 = (hash >>> 4) % 12; 711 */ 712 713 //int hash = (int) rawNoise(i, j, k, seed); 714 //int gi0 = (hash >>>= 4) % 12, gi1 = (hash >>>= 4) % 12, gi2 = (hash >>>= 4) % 12, gi3 = (hash >>>= 4) % 12; 715 // Calculate the contribution from the four corners 716 float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0; 717 if (t0 < 0) { 718 n0 = 0f; 719 } else { 720 t0 *= t0; 721 n0 = t0 * t0 * dotf(grad3f[gi0], x0, y0, z0); 722 } 723 float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1; 724 if (t1 < 0) { 725 n1 = 0f; 726 } else { 727 t1 *= t1; 728 n1 = t1 * t1 * dotf(grad3f[gi1], x1, y1, z1); 729 } 730 float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2; 731 if (t2 < 0) { 732 n2 = 0f; 733 } else { 734 t2 *= t2; 735 n2 = t2 * t2 * dotf(grad3f[gi2], x2, y2, z2); 736 } 737 float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3; 738 if (t3 < 0) { 739 n3 = 0f; 740 } else { 741 t3 *= t3; 742 n3 = t3 * t3 * dotf(grad3f[gi3], x3, y3, z3); 743 } 744 // Add contributions from each corner to get the final noise value. 745 // The result is clamped to stay just inside [-1,1] 746 return Math.max(-1f, Math.min(1f, 31.5f * (n0 + n1 + n2 + n3))); 747 } 748 749}