Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * @author Tat
- */
- public class OlsenNoise2D {
- public int[][] olsennoise(int x, int y, int width, int height) {
- int maxiterations = 7;
- int cx, cy;
- int cxh, cyh;
- int cwidth, cheight;
- int xoff, yoff;
- int nwidth, nheight;
- int nx, ny;
- int nxh, nyh;
- int[][] field = null;
- for (int iteration = 0; iteration < maxiterations; iteration++) {
- nx = x;
- ny = y;
- nxh = x + width;
- nyh = y + width;
- for (int i = 1, n = maxiterations - iteration; i < n; i++) {
- nx = (nx / 2) - 1;
- ny = (ny / 2) - 1;
- nxh = 1 -(-nxh/2);
- nyh = 1 -(-nyh/2);
- }
- xoff = -2*((nx/2)) + nx + 1;
- yoff = -2*((ny/2)) + ny + 1;
- cx = (nx / 2) - 1;
- cy = (ny / 2) - 1;
- cxh = 1 -(-nxh/2);
- cyh = 1 -(-nyh/2);
- nwidth = nxh - nx;
- nheight = nyh - ny;
- cwidth = cxh - cx;
- cheight = cyh - cy;
- if (field == null) field = new int[cwidth][cheight];
- for (int j = 0, m = field.length; j < m; j++) {
- for (int k = 0, n = field[j].length; k < n; k++) {
- field[j][k] += (hashrandom(cx + j, cy + k, iteration) & (1 << (7 - iteration)));
- }
- }
- int[][] upsampled = new int[field.length * 2][field[0].length * 2];
- for (int j = 0, m = upsampled.length; j < m; j++) {
- for (int k = 0, n = upsampled[0].length; k < n; k++) {
- upsampled[j][k] = field[j / 2][k / 2];
- }
- }
- field = upsampled;
- int[][] blurfield = new int[field.length - 2][field[0].length - 2];
- for (int j = 0, m = blurfield.length; j < m; j++) {
- for (int k = 0, n = blurfield[0].length; k < n; k++) {
- for (int h = 0; h < 9; h++) {
- blurfield[j][k] += field[j + (h % 3)][k + (h / 3)];
- }
- blurfield[j][k] /= 9;
- }
- }
- field = blurfield;
- int[][] trimfield = new int[nwidth][nheight];
- for (int j = 0, m = trimfield.length; j < m; j++) {
- for (int k = 0, n = trimfield[0].length; k < n; k++) {
- trimfield[j][k] = field[j + xoff][k + yoff];
- }
- }
- field = trimfield;
- }
- return field;
- }
- public static int hashrandom(int... elements) {
- long hash = 0;
- for (int i = 0; i < elements.length; i++) {
- hash ^= elements[i];
- hash = hash(hash);
- }
- return (int) hash;
- }
- public static long hash(long v) {
- long hash = v;
- long h = hash;
- switch ((int) hash & 3) {
- case 3:
- hash += h;
- hash ^= hash << 32;
- hash ^= h << 36;
- hash += hash >> 22;
- break;
- case 2:
- hash += h;
- hash ^= hash << 22;
- hash += hash >> 34;
- break;
- case 1:
- hash += h;
- hash ^= hash << 20;
- hash += hash >> 2;
- }
- hash ^= hash << 6;
- hash += hash >> 10;
- hash ^= hash << 8;
- hash += hash >> 34;
- hash ^= hash << 50;
- hash += hash >> 12;
- return hash;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement