optimise this code, beautify the rendering so its mroe interesting, add more controls

void exit() {
  executor.shutdown();
  super.exit();
}import java.util.concurrent.*;

final int MAX_SIZE = 128;
int currentSize = 96;
boolean[][][] grid = new boolean[MAX_SIZE][MAX_SIZE][MAX_SIZE];
boolean[][][] nextGrid = new boolean[MAX_SIZE][MAX_SIZE][MAX_SIZE];
int[][][] cellAge = new int[MAX_SIZE][MAX_SIZE][MAX_SIZE];

float rotX, rotY;
int cellSize = 3;
boolean paused = false;
int framesPerGeneration = 5;
int frameCount = 0;

PShader cellShader;
ThreadPoolExecutor executor;

// Rule set
int minSurvive = 4;
int maxSurvive = 5;
int birthCount = 5;

void setup() {
  size(800, 800, P3D);
  randomizeGrid();
  
  // Initialize thread pool
  int processors = Runtime.getRuntime().availableProcessors();
  executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(processors);
  
  // Load shader
  cellShader = loadShader("cellFrag.glsl", "cellVert.glsl");
  
  // Initialize cell age
  for (int x = 0; x < MAX_SIZE; x++) {
    for (int y = 0; y < MAX_SIZE; y++) {
      for (int z = 0; z < MAX_SIZE; z++) {
        cellAge[x][y][z] = 0;
      }
    }
  }
}

void draw() {
  background(0);
  translate(width/2, height/2, -currentSize*cellSize/2);
  rotateX(rotX);
  rotateY(rotY);
  translate(-currentSize*cellSize/2, -currentSize*cellSize/2, -currentSize*cellSize/2);

  // Update grid
  if (!paused && frameCount % framesPerGeneration == 0) {
    updateGrid();
  }
  frameCount++;

  // Draw cells using shader
  shader(cellShader);
  for (int x = 0; x < currentSize; x++) {
    for (int y = 0; y < currentSize; y++) {
      for (int z = 0; z < currentSize; z++) {
        if (grid[x][y][z]) {
          pushMatrix();
          translate(x * cellSize, y * cellSize, z * cellSize);
          cellShader.set("cellAge", float(cellAge[x][y][z]) / 255.0);
          box(cellSize);
          popMatrix();
        }
      }
    }
  }
  resetShader();

  // Display controls
  displayControls();
}

void updateGrid() {
  CountDownLatch latch = new CountDownLatch(currentSize);
  for (int x = 0; x < currentSize; x++) {
    final int fx = x;
    executor.execute(() -> {
      for (int y = 0; y < currentSize; y++) {
        for (int z = 0; z < currentSize; z++) {
          int neighbors = countNeighbors(fx, y, z);
          if (grid[fx][y][z]) {
            nextGrid[fx][y][z] = (neighbors >= minSurvive && neighbors <= maxSurvive);
            if (nextGrid[fx][y][z]) {
              cellAge[fx][y][z] = min(cellAge[fx][y][z] + 1, 255);
            }
          } else {
            nextGrid[fx][y][z] = (neighbors == birthCount);
            if (nextGrid[fx][y][z]) {
              cellAge[fx][y][z] = 0;
            }
          }
        }
      }
      latch.countDown();
    });
  }
  try {
    latch.await();
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  boolean[][][] temp = grid;
  grid = nextGrid;
  nextGrid = temp;
}

int countNeighbors(int x, int y, int z) {
  int count = 0;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      for (int dz = -1; dz <= 1; dz++) {
        if (dx == 0 && dy == 0 && dz == 0) continue;
        int nx = (x + dx + currentSize) % currentSize;
        int ny = (y + dy + currentSize) % currentSize;
        int nz = (z + dz + currentSize) % currentSize;
        if (grid[nx][ny][nz]) count++;
      }
    }
  }
  return count;
}

void randomizeGrid() {
  for (int x = 0; x < currentSize; x++) {
    for (int y = 0; y < currentSize; y++) {
      for (int z = 0; z < currentSize; z++) {
        grid[x][y][z] = random(1) < 0.1;
        cellAge[x][y][z] = 0;
      }
    }
  }
}

void mouseDragged() {
  rotY += (mouseX - pmouseX) * 0.01;
  rotX += (mouseY - pmouseY) * 0.01;
}

void mouseClicked() {
  // Toggle cell at mouse position
  float x = map(mouseX, 0, width, 0, currentSize);
  float y = map(mouseY, 0, height, 0, currentSize);
  int gridX = floor(x);
  int gridY = floor(y);
  int gridZ = currentSize / 2; // Default to middle layer
  
  if (gridX >= 0 && gridX < currentSize && gridY >= 0 && gridY < currentSize) {
    grid[gridX][gridY][gridZ] = !grid[gridX][gridY][gridZ];
    cellAge[gridX][gridY][gridZ] = 0;
  }
}

void keyPressed() {
  switch(key) {
    case ' ': paused = !paused; break;
    case 'r': randomizeGrid(); break;
    case '+': framesPerGeneration = max(1, framesPerGeneration - 1); break;
    case '-': framesPerGeneration++; break;
    case 's': saveState(); break;
    case 'l': loadState(); break;
    case '1': setGridSize(64); break;
    case '2': setGridSize(96); break;
    case '3': setGridSize(128); break;
  }
}

void setGridSize(int newSize) {
  currentSize = newSize;
  randomizeGrid();
}

void saveState() {
  JSONArray json = new JSONArray();
  for (int x = 0; x < currentSize; x++) {
    for (int y = 0; y < currentSize; y++) {
      for (int z = 0; z < currentSize; z++) {
        if (grid[x][y][z]) {
          JSONObject cell = new JSONObject();
          cell.setInt("x", x);
          cell.setInt("y", y);
          cell.setInt("z", z);
          cell.setInt("age", cellAge[x][y][z]);
          json.append(cell);
        }
      }
    }
  }
  saveJSONArray(json, "data/saved_state.json");
}

void loadState() {
  JSONArray json = loadJSONArray("data/saved_state.json");
  if (json == null) return;
  
  // Clear the current grid
  for (int x = 0; x < currentSize; x++) {
    for (int y = 0; y < currentSize; y++) {
      for (int z = 0; z < currentSize; z++) {
        grid[x][y][z] = false;
        cellAge[x][y][z] = 0;
      }
    }
  }
  
  // Load saved cells
  for (int i = 0; i < json.size(); i++) {
    JSONObject cell = json.getJSONObject(i);
    int x = cell.getInt("x");
    int y = cell.getInt("y");
    int z = cell.getInt("z");
    int age = cell.getInt("age");
    if (x < currentSize && y < currentSize && z < currentSize) {
      grid[x][y][z] = true;
      cellAge[x][y][z] = age;
    }
  }
}

void displayControls() {
  camera();
  hint(DISABLE_DEPTH_TEST);
  noLights();
  textAlign(LEFT, TOP);
  fill(255);
  text("Controls:\n" +
       "Drag mouse to rotate\n" +
       "Click to toggle cells\n" +
       "Space: Pause/Resume\n" +
       "R: Randomize grid\n" +
       "+ / -: Adjust speed\n" +
       "S: Save state\n" +
       "L: Load state\n" +
       "1/2/3: Change grid size\n" +
       "\nSpeed: " + (60 / framesPerGeneration) + " gen/sec\n" +
       "Grid size: " + currentSize, 10, 10);
  hint(ENABLE_DEPTH_TEST);
}




cellFrag.glsl:
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform float cellAge;

varying vec4 vertColor;
varying vec2 vertTexCoord;
varying float vAge;

void main() {
  vec3 youngColor = vec3(1.0, 0.0, 0.0);  // Red for young cells
  vec3 oldColor = vec3(0.0, 0.0, 1.0);    // Blue for old cells
  
  vec3 cellColor = mix(youngColor, oldColor, cellAge);
  
  gl_FragColor = vec4(cellColor, 1.0);
}

cellvert.glsl

#define PROCESSING_COLOR_SHADER

uniform mat4 transform;
uniform mat4 texMatrix;

attribute vec4 vertex;
attribute vec4 color;
attribute vec2 texCoord;

varying vec4 vertColor;
varying vec2 vertTexCoord;
varying float vAge;

void main() {
  gl_Position = transform * vertex;
    
  vertColor = color;
  vertTexCoord = (texMatrix * vec4(texCoord, 1.0, 1.0)).xy;
  vAge = color.a;
}