Guest User

Olsen Noise Algorithm

a guest
Sep 19th, 2014
375
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * @author Tat
  3. */
  4.  
  5. public class OlsenNoise2D {
  6.  
  7. public int[][] olsennoise(int x, int y, int width, int height) {
  8. int maxiterations = 7;
  9. int cx, cy;
  10. int cxh, cyh;
  11. int cwidth, cheight;
  12. int xoff, yoff;
  13. int nwidth, nheight;
  14. int nx, ny;
  15. int nxh, nyh;
  16. int[][] field = null;
  17.  
  18. for (int iteration = 0; iteration < maxiterations; iteration++) {
  19. nx = x;
  20. ny = y;
  21.  
  22. nxh = x + width;
  23. nyh = y + width;
  24.  
  25. for (int i = 1, n = maxiterations - iteration; i < n; i++) {
  26. nx = (nx / 2) - 1;
  27. ny = (ny / 2) - 1;
  28. nxh = 1 -(-nxh/2);
  29. nyh = 1 -(-nyh/2);
  30. }
  31.  
  32. xoff = -2*((nx/2)) + nx + 1;
  33. yoff = -2*((ny/2)) + ny + 1;
  34.  
  35. cx = (nx / 2) - 1;
  36. cy = (ny / 2) - 1;
  37. cxh = 1 -(-nxh/2);
  38. cyh = 1 -(-nyh/2);
  39.  
  40. nwidth = nxh - nx;
  41. nheight = nyh - ny;
  42.  
  43. cwidth = cxh - cx;
  44. cheight = cyh - cy;
  45.  
  46. if (field == null) field = new int[cwidth][cheight];
  47. for (int j = 0, m = field.length; j < m; j++) {
  48. for (int k = 0, n = field[j].length; k < n; k++) {
  49. field[j][k] += (hashrandom(cx + j, cy + k, iteration) & (1 << (7 - iteration)));
  50. }
  51. }
  52. int[][] upsampled = new int[field.length * 2][field[0].length * 2];
  53. for (int j = 0, m = upsampled.length; j < m; j++) {
  54. for (int k = 0, n = upsampled[0].length; k < n; k++) {
  55. upsampled[j][k] = field[j / 2][k / 2];
  56. }
  57. }
  58. field = upsampled;
  59.  
  60. int[][] blurfield = new int[field.length - 2][field[0].length - 2];
  61. for (int j = 0, m = blurfield.length; j < m; j++) {
  62. for (int k = 0, n = blurfield[0].length; k < n; k++) {
  63. for (int h = 0; h < 9; h++) {
  64. blurfield[j][k] += field[j + (h % 3)][k + (h / 3)];
  65. }
  66. blurfield[j][k] /= 9;
  67. }
  68. }
  69. field = blurfield;
  70.  
  71.  
  72. int[][] trimfield = new int[nwidth][nheight];
  73.  
  74. for (int j = 0, m = trimfield.length; j < m; j++) {
  75. for (int k = 0, n = trimfield[0].length; k < n; k++) {
  76. trimfield[j][k] = field[j + xoff][k + yoff];
  77. }
  78. }
  79. field = trimfield;
  80. }
  81. return field;
  82. }
  83.  
  84. public static int hashrandom(int... elements) {
  85. long hash = 0;
  86.  
  87. for (int i = 0; i < elements.length; i++) {
  88. hash ^= elements[i];
  89. hash = hash(hash);
  90. }
  91. return (int) hash;
  92. }
  93.  
  94. public static long hash(long v) {
  95. long hash = v;
  96. long h = hash;
  97.  
  98. switch ((int) hash & 3) {
  99. case 3:
  100. hash += h;
  101. hash ^= hash << 32;
  102. hash ^= h << 36;
  103. hash += hash >> 22;
  104. break;
  105. case 2:
  106. hash += h;
  107. hash ^= hash << 22;
  108. hash += hash >> 34;
  109. break;
  110. case 1:
  111. hash += h;
  112. hash ^= hash << 20;
  113. hash += hash >> 2;
  114. }
  115. hash ^= hash << 6;
  116. hash += hash >> 10;
  117. hash ^= hash << 8;
  118. hash += hash >> 34;
  119. hash ^= hash << 50;
  120. hash += hash >> 12;
  121. return hash;
  122. }
  123.  
  124. }
RAW Paste Data