Advertisement
Guest User

Untitled

a guest
Jan 16th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.46 KB | None | 0 0
  1. package com.voxel.engine.core.Noise;
  2.  
  3. /**
  4. * Simplex Noise in 2D, 3D and 4D. Based on the example code of this paper:
  5. * http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  6. *
  7. * @author Stefan Gustavson, Linkping University, Sweden (stegu at itn dot liu
  8. * dot se)
  9. *
  10. * Slight optimizations & restructuring by
  11. * @author Karsten Schmidt (info at toxi dot co dot uk)
  12. *
  13. */
  14. public class SimplexNoise {
  15.  
  16. private static final double SQRT3 = Math.sqrt(3.0);
  17.  
  18. private static final double SQRT5 = Math.sqrt(5.0);
  19.  
  20. private static final double F2 = 0.5 * (SQRT3 - 1.0);
  21. private static final double G2 = (3.0 - SQRT3) / 6.0;
  22. private static final double G22 = G2 * 2.0 - 1.0;
  23. private static final double F3 = 1.0 / 3.0;
  24. private static final double G3 = 1.0 / 6.0;
  25. private static final double G32 = G3 * 2.0;
  26. private static final double G33 = G3 * 3.0 - 1.0;
  27. private static final double F4 = (SQRT5 - 1.0) / 4.0;
  28. private static final double G4 = (5.0 - SQRT5) / 20.0;
  29. private static final double G42 = G4 * 2.0;
  30. private static final double G43 = G4 * 2.0;
  31. private static final double G44 = G4 * 4.0 - 1.0;
  32.  
  33. private static final int grad3[][] = {{1, 1, 0}, {-1, 1, 0},
  34. {1, -1, 0}, {-1, -1, 0}, {1, 0, 1}, {-1, 0, 1},
  35. {1, 0, -1}, {-1, 0, -1}, {0, 1, 1}, {0, -1, 1},
  36. {0, 1, -1}, {0, -1, -1}};
  37.  
  38. private static final int grad4[][] = {{0, 1, 1, 1}, {0, 1, 1, -1},
  39. {0, 1, -1, 1}, {0, 1, -1, -1}, {0, -1, 1, 1},
  40. {0, -1, 1, -1}, {0, -1, -1, 1}, {0, -1, -1, -1},
  41. {1, 0, 1, 1}, {1, 0, 1, -1}, {0, -1, -1, -1},
  42. {-1, 0, 1, 1}, {-1, 0, 1, -1}, {-1, 0, -1, 1},
  43. {-1, 0, -1, -1}, {1, 1, 0, 1}, {1, 1, 0, -1},
  44. {1, -1, 0, 1}, {1, -1, 0, -1}, {-1, 1, 0, 1},
  45. {-1, 1, 0, -1}, {-1, -1, 0, 1}, {-1, -1, 0, -1},
  46. {1, 1, 1, 0}, {1, 1, -1, 0}, {1, -1, 1, 0},
  47. {1, -1, -1, 0}, {-1, 1, 1, 0}, {-1, 1, -1, 0},
  48. {1, -1, -1, 0}, {-1, -1, -1, 0}};
  49.  
  50. private static final int p[] = { 151, 160, 137, 91, 90, 15, 131, 13, 201,
  51. 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37,
  52. 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62,
  53. 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56,
  54. 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139,
  55. 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133,
  56. 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25,
  57. 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200,
  58. 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3,
  59. 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255,
  60. 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
  61. 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153,
  62. 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79,
  63. 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242,
  64. 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249,
  65. 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204,
  66. 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222,
  67. 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180 };
  68.  
  69. private static int perm[] = new int[0x200];
  70. static {
  71. for(int i = 0;i < 0x100;i++){
  72. perm[i] = p[i & 0xff];
  73. }
  74. }
  75.  
  76. private static final int simplex[][] = { { 0, 1, 2, 3 }, { 0, 1, 3, 2 },
  77. { 0, 0, 0, 0 }, { 0, 2, 3, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
  78. { 0, 0, 0, 0 }, { 1, 2, 3, 0 }, { 0, 2, 1, 3 }, { 0, 0, 0, 0 },
  79. { 0, 3, 1, 2 }, { 0, 3, 2, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
  80. { 0, 0, 0, 0 }, { 1, 3, 2, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
  81. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
  82. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 2, 0, 3 }, { 0, 0, 0, 0 },
  83. { 1, 3, 0, 2 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
  84. { 2, 3, 0, 1 }, { 2, 3, 1, 0 }, { 1, 0, 2, 3 }, { 1, 0, 3, 2 },
  85. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 2, 0, 3, 1 },
  86. { 0, 0, 0, 0 }, { 2, 1, 3, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
  87. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
  88. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 2, 0, 1, 3 }, { 0, 0, 0, 0 },
  89. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 3, 0, 1, 2 }, { 3, 0, 2, 1 },
  90. { 0, 0, 0, 0 }, { 3, 1, 2, 0 }, { 2, 1, 0, 3 }, { 0, 0, 0, 0 },
  91. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 3, 1, 0, 2 }, { 0, 0, 0, 0 },
  92. { 3, 2, 0, 1 }, { 3, 2, 1, 0 } };
  93.  
  94. private static final int fastFloor(double x){
  95. return x > 0 ? (int) x : (int) x -1;
  96. }
  97.  
  98. private static double dot(int g[], double x, double y){
  99. return g[0] * x + g[1] * y;
  100. }
  101.  
  102. private static double dot(int g[], double x, double y, double z){
  103. return g[0] * x + g[1] * y + g[2] * z;
  104. }
  105.  
  106. private static double dot(int g[], double x, double y, double z, double w){
  107. return g[0] * x + g[1] * y + g[2] * z + g[3] * w;
  108. }
  109.  
  110. public static double noise(double x, double y){
  111. double n0, n1, n2;
  112. double s = (x + y) * F2;
  113. int i = fastFloor(x + s);
  114. int j = fastFloor(y + s);
  115. double t = (i + j) * G2;
  116.  
  117. double x0 = x - (i - t);
  118. double y0 = y - ( i - t);
  119.  
  120. int i1, j1;
  121.  
  122. if(x0 > y0){
  123. i1 = 1;
  124. j1 = 0;
  125. }else{
  126. i1 = 0;
  127. j1 = 1;
  128. }
  129.  
  130. double x1 = x0 - i1 + G2;
  131. double y1 = y0 - j1 + G2;
  132. double x2 = x0 + G22;
  133. double y2 = y0 + G22;
  134.  
  135. int ii = i & 0xff;
  136. int jj = j & 0xff;
  137.  
  138. double t0 = 0.5 - x0 * x0 - y0 * y0;
  139. if(t0 < 0){
  140. n0 = 0.0;
  141. }else{
  142. t0 *= t0;
  143. int gi0 = perm[ii + perm[jj]] % 12;
  144. n0 = t0 * t0 * dot(grad3[gi0], x0, y0);
  145. }
  146. double t1 = 0.5 - x1 * x1 - y1 * y1;
  147. if(t1 < 0){
  148. n1 = 0.0;
  149. }else{
  150. t1 *= t1;
  151. int gi1 = perm[ii + i1 + perm[jj + j1]] % 12;
  152. n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
  153. }
  154. double t2 = 0.5 - x2 * x2 - y2 * y2;
  155. if(t2 < 0){
  156. n2 = 0.0;
  157. }else{
  158. t2 *= t2;
  159. int gi2 = perm[ii + 1 + perm[jj + 1]] % 12;
  160. n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
  161. }
  162. return 70.0 * (n0 + n1 + n2);
  163. }
  164.  
  165. public static double noise(double x, double y, double z){
  166. double n0, n1, n2, n3;
  167. double s = (x + y + z) * F3;
  168.  
  169. int i = fastFloor(x + s);
  170. int j = fastFloor(y + s);
  171. int k = fastFloor(z + s);
  172.  
  173. double t = (i + j + k) * G3;
  174. double x0 = x - (i - t);
  175. double y0 = y - (j - t);
  176. double z0 = z - (k - t);
  177.  
  178. int i1, j1, k1;
  179. int i2, j2, k2;
  180.  
  181. if(x0 >= y0){
  182. if(y0 >= z0){
  183. i1 = 1;
  184. j1 = 0;
  185. k1 = 0;
  186. i2 = 1;
  187. j2 = 1;
  188. k2 = 0;
  189. }else if(x0 >= z0){
  190. i1 = 0;
  191. j1 = 0;
  192. k1 = 1;
  193. i2 = 1;
  194. j2 = 0;
  195. k2 = 1;
  196. }else{
  197. i1 = 0;
  198. j1 = 0;
  199. k1 = 1;
  200. i2 = 1;
  201. j2 = 0;
  202. k2 = 1;
  203. }
  204. }else{
  205. if(y0 < z0){
  206. i1 = 0;
  207. j1 = 1;
  208. k1 = 0;
  209. i2 = 0;
  210. j2 = 1;
  211. k2 = 1;
  212. }else if(x0 < z0){
  213. i1 = 0;
  214. j1 = 1;
  215. k1 = 0;
  216. i2 = 0;
  217. j2 = 1;
  218. k2 = 1;
  219. }else{
  220. i1 = 0;
  221. j1 = 1;
  222. k1 = 0;
  223. i2 = 1;
  224. j2 = 1;
  225. k2 = 0;
  226. }
  227. }
  228.  
  229. double x1 = x0 - i1 + G3;
  230. double y1 = y0 - j1 + G3;
  231. double z1 = z0 - k1 + G3;
  232. double x2 = x0 - i2 + G3;
  233. double y2 = y0 - j2 + G32;
  234. double z2 = z0 - k2 + G32;
  235. double x3 = x0 + G33;
  236. double y3 = y0 + G33;
  237. double z3 = z0 + G33;
  238.  
  239. int ii = i & 0xff;
  240. int jj = j & 0xff;
  241. int kk = k & 0xff;
  242.  
  243. double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
  244. if(t0 < 0){
  245. n0 = 0.0;
  246. }else{
  247. t0 *= t0;
  248. int gi0 = perm[ii + perm[jj + perm[kk]]] % 12;
  249. n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
  250. }
  251.  
  252. double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
  253. if (t1 < 0){
  254. n1 = 0.0;
  255. }else{
  256. t1 *= t1;
  257. int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]] % 12;
  258. n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
  259. }
  260.  
  261. double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
  262. if (t2 < 0){
  263. n2 = 0.0;
  264. }else{
  265. t2 *= t2;
  266. int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]] % 12;
  267. n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
  268. }
  269.  
  270. double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
  271. if (t3 < 0){
  272. n3 = 0.0;
  273. }else{
  274. t3 *= t3;
  275. int gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 12;
  276. n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
  277. }
  278.  
  279. return 32.0 * (n0 + n1 + n2 + n3);
  280. }
  281.  
  282. public static double noise(double x, double y, double z, double w) {
  283. double n0, n1, n2, n3, n4;
  284.  
  285. double s = (x + y + z + w) * F4;
  286.  
  287. int i = fastFloor(x + s);
  288. int j = fastFloor(y + s);
  289. int k = fastFloor(z + s);
  290. int l = fastFloor(w + s);
  291. double t = (i + j + k + l) * G4;
  292.  
  293. double x0 = x - (i - t);
  294. double y0 = y - (j - t);
  295. double z0 = z - (k - t);
  296. double w0 = w - (l - t);
  297.  
  298. int c = 0;
  299. if (x0 > y0){
  300. c = 0x20;
  301. }
  302. if (x0 > z0){
  303. c |= 0x10;
  304. }
  305. if (y0 > z0){
  306. c |= 0x08;
  307. }
  308. if (x0 > w0){
  309. c |= 0x04;
  310. }
  311. if (y0 > w0){
  312. c |= 0x02;
  313. }
  314. if (z0 > w0){
  315. c |= 0x01;
  316. }
  317.  
  318. int i1, j1, k1, l1;
  319. int i2, j2, k2, l2;
  320. int i3, j3, k3, l3;
  321.  
  322. int[] sc = simplex[c];
  323.  
  324. i1 = sc[0] >= 3 ? 1 : 0;
  325. j1 = sc[1] >= 3 ? 1 : 0;
  326. k1 = sc[2] >= 3 ? 1 : 0;
  327. l1 = sc[3] >= 3 ? 1 : 0;
  328.  
  329. i2 = sc[0] >= 2 ? 1 : 0;
  330. j2 = sc[1] >= 2 ? 1 : 0;
  331. k2 = sc[2] >= 2 ? 1 : 0;
  332. l2 = sc[3] >= 2 ? 1 : 0;
  333.  
  334. i3 = sc[0] >= 1 ? 1 : 0;
  335. j3 = sc[1] >= 1 ? 1 : 0;
  336. k3 = sc[2] >= 1 ? 1 : 0;
  337. l3 = sc[3] >= 1 ? 1 : 0;
  338.  
  339. double x1 = x0 - i1 + G4;
  340.  
  341. double y1 = y0 - j1 + G4;
  342. double z1 = z0 - k1 + G4;
  343. double w1 = w0 - l1 + G4;
  344. double x2 = x0 - i2 + G42;
  345.  
  346. double y2 = y0 - j2 + G42;
  347. double z2 = z0 - k2 + G42;
  348. double w2 = w0 - l2 + G42;
  349. double x3 = x0 - i3 + G43;
  350.  
  351. double y3 = y0 - j3 + G43;
  352. double z3 = z0 - k3 + G43;
  353. double w3 = w0 - l3 + G43;
  354. double x4 = x0 - 1.0 + G44;
  355.  
  356. double y4 = y0 + G44;
  357. double z4 = z0 + G44;
  358. double w4 = w0 + G44;
  359.  
  360. int ii = i & 255;
  361. int jj = j & 255;
  362. int kk = k & 255;
  363. int ll = l & 255;
  364.  
  365. double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
  366. if (t0 < 0){
  367. n0 = 0.0;
  368. }else {
  369. t0 *= t0;
  370. int gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] % 32;
  371. n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
  372. }
  373.  
  374. double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
  375. if (t1 < 0){
  376. n1 = 0.0;
  377. }else {
  378. t1 *= t1;
  379. int gi1 = perm[ii + i1
  380. + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]] % 32;
  381. n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
  382. }
  383.  
  384. double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
  385. if (t2 < 0){
  386. n2 = 0.0;
  387. }else {
  388. t2 *= t2;
  389. int gi2 = perm[ii + i2
  390. + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]] % 32;
  391. n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
  392. }
  393.  
  394. double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
  395. if (t3 < 0){
  396. n3 = 0.0;
  397. }else {
  398. t3 *= t3;
  399. int gi3 = perm[ii + i3
  400. + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]] % 32;
  401. n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
  402. }
  403.  
  404. double t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
  405. if (t4 < 0){
  406. n4 = 0.0;
  407. }else {
  408. t4 *= t4;
  409. int gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32;
  410. n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
  411. }
  412.  
  413. return 27.0 * (n0 + n1 + n2 + n3 + n4);
  414. }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement