1 package org.bukkit.util.noise;
3 import java.util.Random;
4 import org.bukkit.World;
13 protected static final double SQRT_3 = Math.sqrt(3);
14 protected static final double SQRT_5 = Math.sqrt(5);
15 protected static final double F2 = 0.5 * (
SQRT_3 - 1);
16 protected static final double G2 = (3 -
SQRT_3) / 6;
17 protected static final double G22 =
G2 * 2.0 - 1;
18 protected static final double F3 = 1.0 / 3.0;
19 protected static final double G3 = 1.0 / 6.0;
20 protected static final double F4 = (
SQRT_5 - 1.0) / 4.0;
21 protected static final double G4 = (5.0 -
SQRT_5) / 20.0;
22 protected static final double G42 =
G4 * 2.0;
23 protected static final double G43 =
G4 * 3.0;
24 protected static final double G44 =
G4 * 4.0 - 1.0;
25 protected static final int grad4[][] = {{0, 1, 1, 1}, {0, 1, 1, -1}, {0, 1, -1, 1}, {0, 1, -1, -1},
26 {0, -1, 1, 1}, {0, -1, 1, -1}, {0, -1, -1, 1}, {0, -1, -1, -1},
27 {1, 0, 1, 1}, {1, 0, 1, -1}, {1, 0, -1, 1}, {1, 0, -1, -1},
28 {-1, 0, 1, 1}, {-1, 0, 1, -1}, {-1, 0, -1, 1}, {-1, 0, -1, -1},
29 {1, 1, 0, 1}, {1, 1, 0, -1}, {1, -1, 0, 1}, {1, -1, 0, -1},
30 {-1, 1, 0, 1}, {-1, 1, 0, -1}, {-1, -1, 0, 1}, {-1, -1, 0, -1},
31 {1, 1, 1, 0}, {1, 1, -1, 0}, {1, -1, 1, 0}, {1, -1, -1, 0},
32 {-1, 1, 1, 0}, {-1, 1, -1, 0}, {-1, -1, 1, 0}, {-1, -1, -1, 0}};
33 protected static final int simplex[][] = {
34 {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0},
35 {0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0},
36 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
37 {1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0},
38 {1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0},
39 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
40 {2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0},
41 {2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0}};
55 this(
new Random(world.
getSeed()));
64 this(
new Random(seed));
74 offsetW = rand.nextDouble() * 256;
77 protected static double dot(
int g[],
double x,
double y) {
78 return g[0] * x + g[1] * y;
81 protected static double dot(
int g[],
double x,
double y,
double z) {
82 return g[0] * x + g[1] * y + g[2] * z;
85 protected static double dot(
int g[],
double x,
double y,
double z,
double w) {
86 return g[0] * x + g[1] * y + g[2] * z + g[3] * w;
96 return instance.
noise(xin);
106 public static double getNoise(
double xin,
double yin) {
107 return instance.
noise(xin, yin);
118 public static double getNoise(
double xin,
double yin,
double zin) {
119 return instance.
noise(xin, yin, zin);
131 public static double getNoise(
double x,
double y,
double z,
double w) {
132 return instance.
noise(x, y, z, w);
136 public double noise(
double xin,
double yin,
double zin) {
141 double n0, n1, n2, n3;
144 double s = (xin + yin + zin) *
F3;
145 int i =
floor(xin + s);
146 int j =
floor(yin + s);
147 int k =
floor(zin + s);
148 double t = (i + j + k) *
G3;
152 double x0 = xin - X0;
153 double y0 = yin - Y0;
154 double z0 = zin - Z0;
217 double x1 = x0 - i1 +
G3;
218 double y1 = y0 - j1 +
G3;
219 double z1 = z0 - k1 +
G3;
220 double x2 = x0 - i2 + 2.0 *
G3;
221 double y2 = y0 - j2 + 2.0 *
G3;
222 double z2 = z0 - k2 + 2.0 *
G3;
223 double x3 = x0 - 1.0 + 3.0 *
G3;
224 double y3 = y0 - 1.0 + 3.0 *
G3;
225 double z3 = z0 - 1.0 + 3.0 *
G3;
232 int gi1 =
perm[ii + i1 +
perm[jj + j1 +
perm[kk + k1]]] % 12;
233 int gi2 =
perm[ii + i2 +
perm[jj + j2 +
perm[kk + k2]]] % 12;
234 int gi3 =
perm[ii + 1 +
perm[jj + 1 +
perm[kk + 1]]] % 12;
237 double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
242 n0 = t0 * t0 *
dot(
grad3[gi0], x0, y0, z0);
245 double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
250 n1 = t1 * t1 *
dot(
grad3[gi1], x1, y1, z1);
253 double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
258 n2 = t2 * t2 *
dot(
grad3[gi2], x2, y2, z2);
261 double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
266 n3 = t3 * t3 *
dot(
grad3[gi3], x3, y3, z3);
271 return 32.0 * (n0 + n1 + n2 + n3);
275 public double noise(
double xin,
double yin) {
282 double s = (xin + yin) *
F2;
283 int i =
floor(xin + s);
284 int j =
floor(yin + s);
285 double t = (i + j) *
G2;
288 double x0 = xin - X0;
289 double y0 = yin - Y0;
308 double x1 = x0 - i1 +
G2;
309 double y1 = y0 - j1 +
G2;
310 double x2 = x0 +
G22;
311 double y2 = y0 +
G22;
317 int gi1 =
perm[ii + i1 +
perm[jj + j1]] % 12;
318 int gi2 =
perm[ii + 1 +
perm[jj + 1]] % 12;
321 double t0 = 0.5 - x0 * x0 - y0 * y0;
326 n0 = t0 * t0 *
dot(
grad3[gi0], x0, y0);
329 double t1 = 0.5 - x1 * x1 - y1 * y1;
334 n1 = t1 * t1 *
dot(
grad3[gi1], x1, y1);
337 double t2 = 0.5 - x2 * x2 - y2 * y2;
342 n2 = t2 * t2 *
dot(
grad3[gi2], x2, y2);
347 return 70.0 * (n0 + n1 + n2);
359 public double noise(
double x,
double y,
double z,
double w) {
365 double n0, n1, n2, n3, n4;
368 double s = (x + y + z + w) *
F4;
369 int i =
floor(x + s);
370 int j =
floor(y + s);
371 int k =
floor(z + s);
372 int l =
floor(w + s);
374 double t = (i + j + k + l) *
G4;
392 int c1 = (x0 > y0) ? 32 : 0;
393 int c2 = (x0 > z0) ? 16 : 0;
394 int c3 = (y0 > z0) ? 8 : 0;
395 int c4 = (x0 > w0) ? 4 : 0;
396 int c5 = (y0 > w0) ? 2 : 0;
397 int c6 = (z0 > w0) ? 1 : 0;
398 int c = c1 + c2 + c3 + c4 + c5 + c6;
409 i1 =
simplex[c][0] >= 3 ? 1 : 0;
410 j1 =
simplex[c][1] >= 3 ? 1 : 0;
411 k1 =
simplex[c][2] >= 3 ? 1 : 0;
412 l1 =
simplex[c][3] >= 3 ? 1 : 0;
415 i2 =
simplex[c][0] >= 2 ? 1 : 0;
416 j2 =
simplex[c][1] >= 2 ? 1 : 0;
417 k2 =
simplex[c][2] >= 2 ? 1 : 0;
418 l2 =
simplex[c][3] >= 2 ? 1 : 0;
421 i3 =
simplex[c][0] >= 1 ? 1 : 0;
422 j3 =
simplex[c][1] >= 1 ? 1 : 0;
423 k3 =
simplex[c][2] >= 1 ? 1 : 0;
424 l3 =
simplex[c][3] >= 1 ? 1 : 0;
428 double x1 = x0 - i1 +
G4;
429 double y1 = y0 - j1 +
G4;
430 double z1 = z0 - k1 +
G4;
431 double w1 = w0 - l1 +
G4;
433 double x2 = x0 - i2 +
G42;
434 double y2 = y0 - j2 +
G42;
435 double z2 = z0 - k2 +
G42;
436 double w2 = w0 - l2 +
G42;
438 double x3 = x0 - i3 +
G43;
439 double y3 = y0 - j3 +
G43;
440 double z3 = z0 - k3 +
G43;
441 double w3 = w0 - l3 +
G43;
443 double x4 = x0 +
G44;
444 double y4 = y0 +
G44;
445 double z4 = z0 +
G44;
446 double w4 = w0 +
G44;
455 int gi1 =
perm[ii + i1 +
perm[jj + j1 +
perm[kk + k1 +
perm[ll + l1]]]] % 32;
456 int gi2 =
perm[ii + i2 +
perm[jj + j2 +
perm[kk + k2 +
perm[ll + l2]]]] % 32;
457 int gi3 =
perm[ii + i3 +
perm[jj + j3 +
perm[kk + k3 +
perm[ll + l3]]]] % 32;
461 double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
466 n0 = t0 * t0 *
dot(
grad4[gi0], x0, y0, z0, w0);
469 double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
474 n1 = t1 * t1 *
dot(
grad4[gi1], x1, y1, z1, w1);
477 double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
482 n2 = t2 * t2 *
dot(
grad4[gi2], x2, y2, z2, w2);
485 double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
490 n3 = t3 * t3 *
dot(
grad4[gi3], x3, y3, z3, w3);
493 double t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
498 n4 = t4 * t4 *
dot(
grad4[gi4], x4, y4, z4, w4);
502 return 27.0 * (n0 + n1 + n2 + n3 + n4);