Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PerlinNoiseGenerator.java
1 package org.bukkit.util.noise;
2 
3 import java.util.Random;
4 import org.bukkit.World;
5 
6 /**
7  * Generates noise using the "classic" perlin generator
8  *
9  * @see SimplexNoiseGenerator "Improved" and faster version with slighly
10  * different results
11  */
12 public class PerlinNoiseGenerator extends NoiseGenerator {
13  protected static final int grad3[][] = {{1, 1, 0}, {-1, 1, 0}, {1, -1, 0}, {-1, -1, 0},
14  {1, 0, 1}, {-1, 0, 1}, {1, 0, -1}, {-1, 0, -1},
15  {0, 1, 1}, {0, -1, 1}, {0, 1, -1}, {0, -1, -1}};
16  private static final PerlinNoiseGenerator instance = new PerlinNoiseGenerator();
17 
18  protected PerlinNoiseGenerator() {
19  int p[] = {151, 160, 137, 91, 90, 15, 131, 13, 201,
20  95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37,
21  240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62,
22  94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56,
23  87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139,
24  48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133,
25  230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25,
26  63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200,
27  196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3,
28  64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255,
29  82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
30  223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153,
31  101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79,
32  113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242,
33  193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249,
34  14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204,
35  176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222,
36  114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180};
37 
38  for (int i = 0; i < 512; i++) {
39  perm[i] = p[i & 255];
40  }
41  }
42 
43  /**
44  * Creates a seeded perlin noise generator for the given world
45  *
46  * @param world World to construct this generator for
47  */
48  public PerlinNoiseGenerator(World world) {
49  this(new Random(world.getSeed()));
50  }
51 
52  /**
53  * Creates a seeded perlin noise generator for the given seed
54  *
55  * @param seed Seed to construct this generator for
56  */
57  public PerlinNoiseGenerator(long seed) {
58  this(new Random(seed));
59  }
60 
61  /**
62  * Creates a seeded perlin noise generator with the given Random
63  *
64  * @param rand Random to construct with
65  */
66  public PerlinNoiseGenerator(Random rand) {
67  offsetX = rand.nextDouble() * 256;
68  offsetY = rand.nextDouble() * 256;
69  offsetZ = rand.nextDouble() * 256;
70 
71  for (int i = 0; i < 256; i++) {
72  perm[i] = rand.nextInt(256);
73  }
74 
75  for (int i = 0; i < 256; i++) {
76  int pos = rand.nextInt(256 - i) + i;
77  int old = perm[i];
78 
79  perm[i] = perm[pos];
80  perm[pos] = old;
81  perm[i + 256] = perm[i];
82  }
83  }
84 
85  /**
86  * Computes and returns the 1D unseeded perlin noise for the given
87  * coordinates in 1D space
88  *
89  * @param x X coordinate
90  * @return Noise at given location, from range -1 to 1
91  */
92  public static double getNoise(double x) {
93  return instance.noise(x);
94  }
95 
96  /**
97  * Computes and returns the 2D unseeded perlin noise for the given
98  * coordinates in 2D space
99  *
100  * @param x X coordinate
101  * @param y Y coordinate
102  * @return Noise at given location, from range -1 to 1
103  */
104  public static double getNoise(double x, double y) {
105  return instance.noise(x, y);
106  }
107 
108  /**
109  * Computes and returns the 3D unseeded perlin noise for the given
110  * coordinates in 3D space
111  *
112  * @param x X coordinate
113  * @param y Y coordinate
114  * @param z Z coordinate
115  * @return Noise at given location, from range -1 to 1
116  */
117  public static double getNoise(double x, double y, double z) {
118  return instance.noise(x, y, z);
119  }
120 
121  /**
122  * Gets the singleton unseeded instance of this generator
123  *
124  * @return Singleton
125  */
127  return instance;
128  }
129 
130  @Override
131  public double noise(double x, double y, double z) {
132  x += offsetX;
133  y += offsetY;
134  z += offsetZ;
135 
136  int floorX = floor(x);
137  int floorY = floor(y);
138  int floorZ = floor(z);
139 
140  // Find unit cube containing the point
141  int X = floorX & 255;
142  int Y = floorY & 255;
143  int Z = floorZ & 255;
144 
145  // Get relative xyz coordinates of the point within the cube
146  x -= floorX;
147  y -= floorY;
148  z -= floorZ;
149 
150  // Compute fade curves for xyz
151  double fX = fade(x);
152  double fY = fade(y);
153  double fZ = fade(z);
154 
155  // Hash coordinates of the cube corners
156  int A = perm[X] + Y;
157  int AA = perm[A] + Z;
158  int AB = perm[A + 1] + Z;
159  int B = perm[X + 1] + Y;
160  int BA = perm[B] + Z;
161  int BB = perm[B + 1] + Z;
162 
163  return lerp(fZ, lerp(fY, lerp(fX, grad(perm[AA], x, y, z),
164  grad(perm[BA], x - 1, y, z)),
165  lerp(fX, grad(perm[AB], x, y - 1, z),
166  grad(perm[BB], x - 1, y - 1, z))),
167  lerp(fY, lerp(fX, grad(perm[AA + 1], x, y, z - 1),
168  grad(perm[BA + 1], x - 1, y, z - 1)),
169  lerp(fX, grad(perm[AB + 1], x, y - 1, z - 1),
170  grad(perm[BB + 1], x - 1, y - 1, z - 1))));
171  }
172 
173  /**
174  * Generates noise for the 1D coordinates using the specified number of
175  * octaves and parameters
176  *
177  * @param x X-coordinate
178  * @param octaves Number of octaves to use
179  * @param frequency How much to alter the frequency by each octave
180  * @param amplitude How much to alter the amplitude by each octave
181  * @return Resulting noise
182  */
183  public static double getNoise(double x, int octaves, double frequency, double amplitude) {
184  return instance.noise(x, octaves, frequency, amplitude);
185  }
186 
187  /**
188  * Generates noise for the 2D coordinates using the specified number of
189  * octaves and parameters
190  *
191  * @param x X-coordinate
192  * @param y Y-coordinate
193  * @param octaves Number of octaves to use
194  * @param frequency How much to alter the frequency by each octave
195  * @param amplitude How much to alter the amplitude by each octave
196  * @return Resulting noise
197  */
198  public static double getNoise(double x, double y, int octaves, double frequency, double amplitude) {
199  return instance.noise(x, y, octaves, frequency, amplitude);
200  }
201 
202  /**
203  * Generates noise for the 3D coordinates using the specified number of
204  * octaves and parameters
205  *
206  * @param x X-coordinate
207  * @param y Y-coordinate
208  * @param z Z-coordinate
209  * @param octaves Number of octaves to use
210  * @param frequency How much to alter the frequency by each octave
211  * @param amplitude How much to alter the amplitude by each octave
212  * @return Resulting noise
213  */
214  public static double getNoise(double x, double y, double z, int octaves, double frequency, double amplitude) {
215  return instance.noise(x, y, z, octaves, frequency, amplitude);
216  }
217 }
static double getNoise(double x, int octaves, double frequency, double amplitude)
static double getNoise(double x, double y, double z, int octaves, double frequency, double amplitude)
static double getNoise(double x, double y, double z)
static double getNoise(double x, double y)
static double getNoise(double x, double y, int octaves, double frequency, double amplitude)