Advertisement
itblanco

3d CA

Nov 26th, 2020 (edited)
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. int w;
  2. int h;
  3. int t;
  4. int currentT;
  5. float randomFillPercent;
  6. int[][] map;
  7. int[][][] map2;
  8. float sizeX, sizeY;
  9.  
  10. float nextTime = 0;
  11. float timeOffset = 0.02;
  12.  
  13. void setup() {
  14.   size(1000, 1000, P3D);
  15.  
  16.   w = 50;
  17.   h = 50;
  18.   t = 200;
  19.   currentT = 0;
  20.   resetMap2();
  21.   sizeX = 5;
  22.   sizeY = 5;
  23.   randomFillPercent = 0.2 ;
  24.   generateMap();
  25.  
  26.   surface.setLocation(0, 0);
  27.   ortho(-width/2, width/2, -height/2, height/2, 1, 10000);
  28. }
  29.  
  30. void draw() {
  31.   background(20);
  32.   translate(width/2, height/2);
  33.   rotateX(PI*0.25);
  34.   rotateZ(-PI*0.75);
  35.   translate(0, 0, -500);
  36.   rotateZ(millis()*0.0005);
  37.   noStroke();
  38.   //stroke(0);
  39.   lights();
  40.   fill(255);
  41.   for (int x = 0; x < w; x++) {
  42.     for (int y = 0; y < h; y++) {
  43.       for (int z = 0; z < t; z++) {
  44.         //fill(map[x][y] == 0 ? 0 : 255);
  45.         push();
  46.         //rect(x * sizeX - (w*sizeX)/2.0, y * sizeY - (h*sizeY)/2.0, sizeX, sizeY);
  47.         translate(x * sizeX - (w*sizeX)/2.0, y * sizeY - (h*sizeY)/2.0, z * sizeY);
  48.         if (map2[x][y][z] == 1) box(sizeX);
  49.         pop();
  50.       }
  51.     }
  52.   }
  53.  
  54.   drawAxes();
  55.  
  56.   if (nextTime < millis()*0.001) {
  57.     smoothMap();
  58.     nextTime = millis()*0.001 + timeOffset;
  59.   }
  60. }
  61.  
  62. void drawAxes() {
  63.   float axisLenght = 100;
  64.   stroke(255, 0, 0);
  65.   line(0, 0, 0, axisLenght, 0, 0);
  66.   stroke(0, 255, 0);
  67.   line(0, 0, 0, 0, axisLenght, 0);
  68.   stroke(0, 0, 255);
  69.   line(0, 0, 0, 0, 0, axisLenght);
  70.   stroke(255, 255, 0);
  71.   line(0, 0, 0, 0, 0, -axisLenght);
  72. }
  73.  
  74. void keyPressed() {
  75.   if (key == ' ') {
  76.     randomFillPercent = map(mouseX, 0, width, 0.1, 0.9);
  77.     generateMap();
  78.   }
  79.   if (key == 's') {
  80.     smoothMap();
  81.   }
  82. }
  83.  
  84. /////////////////////////////////////////
  85. void generateMap() {
  86.   resetMap2();
  87.   map = new int[w][h];
  88.   randomFillMap();
  89.   smoothMap();
  90. }
  91.  
  92. void randomFillMap() {
  93.   for (int x = 0; x < w; x++) {
  94.     for (int y = 0; y < h; y++) {
  95.       if (x == 0 || x == w || y == 0 || y == h) {
  96.         map[x][y] = 1;
  97.       } else {
  98.         map[x][y] = random(1) < randomFillPercent ? 1 : 0;
  99.       }
  100.     }
  101.   }
  102. }
  103.  
  104.  
  105. void smoothMap() {
  106.   int[][] newMap = new int[w][h];
  107.  
  108.   for (int x = 0; x < w; x++) {
  109.     for (int y = 0; y < h; y++) {
  110.       int neighbourWallTiles = getSurroundingWallCount(x, y);
  111.       int currentCell = map[x][y];
  112.       //if(neighbourWallTiles > 4) newMap[x][y] = 1;
  113.       //else if(neighbourWallTiles < 4) newMap[x][y] = 0;
  114.       if (currentCell == 1 && (neighbourWallTiles == 2 || neighbourWallTiles == 3)) newMap[x][y] = currentCell;
  115.       else if (currentCell == 0 && neighbourWallTiles == 3) newMap[x][y] = 1;
  116.       else newMap[x][y] = 0;
  117.     }
  118.   }
  119.   map = newMap;
  120.   copyMap();
  121. }
  122.  
  123. int getSurroundingWallCount(int gridX, int gridY) {
  124.   int wallCount = 0;
  125.  
  126.   for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX++) {
  127.     for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY++) {
  128.       if (neighbourX >= 0 && neighbourX < w && neighbourY >= 0 && neighbourY < h) {
  129.         if (neighbourX != gridX || neighbourY != gridY) {
  130.           wallCount += map[neighbourX][neighbourY];
  131.         }
  132.       } else {
  133.         wallCount++;
  134.       }
  135.     }
  136.   }
  137.  
  138.   return wallCount;
  139. }
  140.  
  141. void copyMap() {
  142.   if (currentT < t) {
  143.     for (int x = 0; x < w; x++) {
  144.       for (int y = 0; y < h; y++) {
  145.         map2[x][y][currentT] = map[x][y];
  146.       }
  147.     }
  148.     currentT++;
  149.   }
  150. }
  151.  
  152. void resetMap2() {
  153.   map2 = new int[w][h][t];
  154.   for (int x = 0; x < w; x++) {
  155.     for (int y = 0; y < h; y++) {
  156.       for (int z = 0; z < t; z++) {
  157.         map2[x][y][z] = 0;
  158.       }
  159.     }
  160.   }
  161.   currentT = 0;
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement