Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 185 lines 7.3 kB view raw
1package org.bukkit.util.noise; 2 3import org.bukkit.World; 4 5import java.util.Random; 6 7/** 8 * Generates noise using the "classic" perlin generator 9 * 10 * @see SimplexNoiseGenerator "Improved" and faster version with slighly different results 11 */ 12public class PerlinNoiseGenerator extends NoiseGenerator { 13 protected static final int grad3[][] = { { 1, 1, 0 }, { -1, 1, 0 }, { 1, -1, 0 }, { -1, -1, 0 }, { 1, 0, 1 }, { -1, 0, 1 }, { 1, 0, -1 }, { -1, 0, -1 }, { 0, 1, 1 }, { 0, -1, 1 }, { 0, 1, -1 }, { 0, -1, -1 } }; 14 private static final PerlinNoiseGenerator instance = new PerlinNoiseGenerator(); 15 16 protected PerlinNoiseGenerator() { 17 int p[] = { 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 }; 18 19 for (int i = 0; i < 512; i++) { 20 perm[i] = p[i & 255]; 21 } 22 } 23 24 /** 25 * Creates a seeded perlin noise generator for the given world 26 * 27 * @param world World to construct this generator for 28 */ 29 public PerlinNoiseGenerator(World world) { 30 this(new Random(world.getSeed())); 31 } 32 33 /** 34 * Creates a seeded perlin noise generator for the given seed 35 * 36 * @param seed Seed to construct this generator for 37 */ 38 public PerlinNoiseGenerator(long seed) { 39 this(new Random(seed)); 40 } 41 42 /** 43 * Creates a seeded perlin noise generator with the given Random 44 * 45 * @param rand Random to construct with 46 */ 47 public PerlinNoiseGenerator(Random rand) { 48 offsetX = rand.nextDouble() * 256; 49 offsetY = rand.nextDouble() * 256; 50 offsetZ = rand.nextDouble() * 256; 51 52 for (int i = 0; i < 256; i++) { 53 perm[i] = rand.nextInt(256); 54 } 55 56 for (int i = 0; i < 256; i++) { 57 int pos = rand.nextInt(256 - i) + i; 58 int old = perm[i]; 59 60 perm[i] = perm[pos]; 61 perm[pos] = old; 62 perm[i + 256] = perm[i]; 63 } 64 } 65 66 /** 67 * Computes and returns the 1D unseeded perlin noise for the given coordinates in 1D space 68 * 69 * @param x X coordinate 70 * @return Noise at given location, from range -1 to 1 71 */ 72 public static double getNoise(double x) { 73 return instance.noise(x); 74 } 75 76 /** 77 * Computes and returns the 2D unseeded perlin noise for the given coordinates in 2D space 78 * 79 * @param x X coordinate 80 * @param y Y coordinate 81 * @return Noise at given location, from range -1 to 1 82 */ 83 public static double getNoise(double x, double y) { 84 return instance.noise(x, y); 85 } 86 87 /** 88 * Computes and returns the 3D unseeded perlin noise for the given coordinates in 3D space 89 * 90 * @param x X coordinate 91 * @param y Y coordinate 92 * @param z Z coordinate 93 * @return Noise at given location, from range -1 to 1 94 */ 95 public static double getNoise(double x, double y, double z) { 96 return instance.noise(x, y, z); 97 } 98 99 /** 100 * Gets the singleton unseeded instance of this generator 101 * 102 * @return Singleton 103 */ 104 public static PerlinNoiseGenerator getInstance() { 105 return instance; 106 } 107 108 @Override 109 public double noise(double x, double y, double z) { 110 x += offsetX; 111 y += offsetY; 112 z += offsetZ; 113 114 int floorX = floor(x); 115 int floorY = floor(y); 116 int floorZ = floor(z); 117 118 // Find unit cube containing the point 119 int X = floorX & 255; 120 int Y = floorY & 255; 121 int Z = floorZ & 255; 122 123 // Get relative xyz coordinates of the point within the cube 124 x -= floorX; 125 y -= floorY; 126 z -= floorZ; 127 128 // Compute fade curves for xyz 129 double fX = fade(x); 130 double fY = fade(y); 131 double fZ = fade(z); 132 133 // Hash coordinates of the cube corners 134 int A = perm[X] + Y; 135 int AA = perm[A] + Z; 136 int AB = perm[A + 1] + Z; 137 int B = perm[X + 1] + Y; 138 int BA = perm[B] + Z; 139 int BB = perm[B + 1] + Z; 140 141 return lerp(fZ, lerp(fY, lerp(fX, grad(perm[AA], x, y, z), grad(perm[BA], x - 1, y, z)), lerp(fX, grad(perm[AB], x, y - 1, z), grad(perm[BB], x - 1, y - 1, z))), lerp(fY, lerp(fX, grad(perm[AA + 1], x, y, z - 1), grad(perm[BA + 1], x - 1, y, z - 1)), lerp(fX, grad(perm[AB + 1], x, y - 1, z - 1), grad(perm[BB + 1], x - 1, y - 1, z - 1)))); 142 } 143 144 /** 145 * Generates noise for the 1D coordinates using the specified number of octaves and parameters 146 * 147 * @param x X-coordinate 148 * @param octaves Number of octaves to use 149 * @param frequency How much to alter the frequency by each octave 150 * @param amplitude How much to alter the amplitude by each octave 151 * @return Resulting noise 152 */ 153 public static double getNoise(double x, int octaves, double frequency, double amplitude) { 154 return instance.noise(x, octaves, frequency, amplitude); 155 } 156 157 /** 158 * Generates noise for the 2D coordinates using the specified number of octaves and parameters 159 * 160 * @param x X-coordinate 161 * @param y Y-coordinate 162 * @param octaves Number of octaves to use 163 * @param frequency How much to alter the frequency by each octave 164 * @param amplitude How much to alter the amplitude by each octave 165 * @return Resulting noise 166 */ 167 public static double getNoise(double x, double y, int octaves, double frequency, double amplitude) { 168 return instance.noise(x, y, octaves, frequency, amplitude); 169 } 170 171 /** 172 * Generates noise for the 3D coordinates using the specified number of octaves and parameters 173 * 174 * @param x X-coordinate 175 * @param y Y-coordinate 176 * @param z Z-coordinate 177 * @param octaves Number of octaves to use 178 * @param frequency How much to alter the frequency by each octave 179 * @param amplitude How much to alter the amplitude by each octave 180 * @return Resulting noise 181 */ 182 public static double getNoise(double x, double y, double z, int octaves, double frequency, double amplitude) { 183 return instance.noise(x, y, z, octaves, frequency, amplitude); 184 } 185}