public double calcDensity(int x, int y, int z) {
    double height = calcBaseTerrain(x, z);
    double ocean = calcOceanTerrain(x, z);
    double river = calcRiverTerrain(x, z);

    float temp = biomeProvider.getTemperatureAt(x, z);
    float humidity = biomeProvider.getHumidityAt(x, z);

    Vector2f distanceToMountainBiome = new Vector2f(temp - 0.25f, humidity - 0.35f);

    double mIntens = TeraMath.clamp(1.0 - distanceToMountainBiome.length() * 3.0);
    double densityMountains = calcMountainDensity(x, y, z) * mIntens;
    double densityHills = calcHillDensity(x, y, z) * (1.0 - mIntens);

    int plateauArea = (int) (Chunk.SIZE_Y * 0.10);
    double flatten = TeraMath.clamp(((Chunk.SIZE_Y - 16) - y) / plateauArea);

    return -y
        + (((32.0 + height * 32.0) * TeraMath.clamp(river + 0.25) * TeraMath.clamp(ocean + 0.25))
                + densityMountains * 1024.0
                + densityHills * 128.0)
            * flatten;
  }
  @Override
  public void generateChunk(Chunk c) {
    double[][][] densityMap = new double[Chunk.SIZE_X + 1][Chunk.SIZE_Y + 1][Chunk.SIZE_Z + 1];

    /*
     * Create the density map at a lower sample rate.
     */
    for (int x = 0; x <= Chunk.SIZE_X; x += SAMPLE_RATE_3D_HOR) {
      for (int z = 0; z <= Chunk.SIZE_Z; z += SAMPLE_RATE_3D_HOR) {
        for (int y = 0; y <= Chunk.SIZE_Y; y += SAMPLE_RATE_3D_VERT) {
          densityMap[x][y][z] = calcDensity(c.getBlockWorldPosX(x), y, c.getBlockWorldPosZ(z));
        }
      }
    }

    /*
     * Trilinear interpolate the missing values.
     */
    triLerpDensityMap(densityMap);

    /*
     * Generate the chunk from the density map.
     */
    for (int x = 0; x < Chunk.SIZE_X; x++) {
      for (int z = 0; z < Chunk.SIZE_Z; z++) {
        WorldBiomeProvider.Biome type =
            biomeProvider.getBiomeAt(c.getBlockWorldPosX(x), c.getBlockWorldPosZ(z));
        int firstBlockHeight = -1;

        for (int y = Chunk.SIZE_Y; y >= 0; y--) {

          if (y == 0) { // The very deepest layer of the world is an indestructible mantle
            c.setBlock(x, y, z, BlockManager.getInstance().getBlock("MantleStone"));
            break;
          }

          if (y <= 32 && y > 0) { // Ocean
            c.setBlock(x, y, z, BlockManager.getInstance().getBlock("Water"));
            c.setLiquid(x, y, z, new LiquidData(LiquidType.WATER, Chunk.MAX_LIQUID_DEPTH));

            if (y == 32) {
              // Ice layer
              if (type == WorldBiomeProvider.Biome.SNOW)
                c.setBlock(x, y, z, BlockManager.getInstance().getBlock("Ice"));
            }
          }

          double dens = densityMap[x][y][z];

          if ((dens >= 0 && dens < 32)) {

            // Some block was set...
            if (firstBlockHeight == -1) firstBlockHeight = y;

            if (calcCaveDensity(c.getBlockWorldPosX(x), y, c.getBlockWorldPosZ(z)) > -0.7)
              GenerateOuterLayer(x, y, z, firstBlockHeight, c, type);
            else c.setBlock(x, y, z, air);

            continue;
          } else if (dens >= 32) {

            // Some block was set...
            if (firstBlockHeight == -1) firstBlockHeight = y;

            if (calcCaveDensity(c.getBlockWorldPosX(x), y, c.getBlockWorldPosZ(z)) > -0.6)
              GenerateInnerLayer(x, y, z, c, type);
            else c.setBlock(x, y, z, air);

            continue;
          }

          // Nothing was set!
          firstBlockHeight = -1;
        }
      }
    }
  }