/**
   * Batches a collection of Geometries so that all with the same material get combined.
   *
   * @param geometries The Geometries to combine
   * @return A List of newly created Geometries, each with a distinct material
   */
  public static List<Geometry> makeBatches(Collection<Geometry> geometries, boolean useLods) {
    ArrayList<Geometry> retVal = new ArrayList<Geometry>();
    HashMap<Material, List<Geometry>> matToGeom = new HashMap<Material, List<Geometry>>();

    for (Geometry geom : geometries) {
      List<Geometry> outList = matToGeom.get(geom.getMaterial());
      if (outList == null) {
        // trying to compare materials with the contentEquals method
        for (Material mat : matToGeom.keySet()) {
          if (geom.getMaterial().contentEquals(mat)) {
            outList = matToGeom.get(mat);
          }
        }
      }
      if (outList == null) {
        outList = new ArrayList<Geometry>();
        matToGeom.put(geom.getMaterial(), outList);
      }
      if (geom.getCullHint() != CullHint.Always) {
        outList.add(geom);
      }
    }

    int batchNum = 0;
    for (Map.Entry<Material, List<Geometry>> entry : matToGeom.entrySet()) {
      Material mat = entry.getKey();
      List<Geometry> geomsForMat = entry.getValue();
      Mesh mesh = new Mesh();
      mergeGeometries(geomsForMat, mesh);
      // lods
      if (useLods) {
        makeLods(geomsForMat, mesh);
      }
      mesh.updateCounts();

      Geometry out = new Geometry("batch[" + (batchNum++) + "]", mesh);
      out.setMaterial(mat);
      out.updateModelBound();
      retVal.add(out);
    }

    return retVal;
  }