private void updateFromMailbox(final Renderer renderer) {
if (updateTimer > updateThreashold) {
final List<Region> regionList = mailBox.switchAndGet();
if (!regionList.isEmpty()) {
for (int unit = validLevels - 1; unit >= 0; unit--) {
final LevelData levelData = levelDataList.get(unit);
// final int pow = (int) Math.pow(2, unit);
final int sX = levelData.x - textureSize / 2;
final int sY = levelData.y - textureSize / 2;
levelData.clipRegion.setX(sX);
levelData.clipRegion.setY(sY);
}
for (int i = regionList.size() - 1; i >= 0; i--) {
final Region region = regionList.get(i);
final Region clipRegion = levelDataList.get(region.getLevel()).clipRegion;
if (clipRegion.intersects(region)) {
clipRegion.intersection(region);
} else {
regionList.remove(i);
}
}
Collections.sort(regionList, regionSorter);
final int start = regionList.size() - 1;
for (int i = start; i >= 0; i--) {
final Region region = regionList.get(i);
recursiveAddUpdates(regionList, region.getLevel(), region.getX(), region.getY(), region.getWidth(),
region.getHeight());
}
for (int i = regionList.size() - 1; i >= 0; i--) {
final Region region = regionList.get(i);
final Region clipRegion = levelDataList.get(region.getLevel()).clipRegion;
if (clipRegion.intersects(region)) {
clipRegion.intersection(region);
} else {
regionList.remove(i);
}
}
Collections.sort(regionList, regionSorter);
final Set<Integer> affectedUnits = Sets.newHashSet();
for (int i = regionList.size() - 1; i >= 0; i--) {
final Region region = regionList.get(i);
final int unit = region.getLevel();
affectedUnits.add(unit);
final LevelData levelData = levelDataList.get(unit);
final TextureCache cache = cacheList.get(unit);
final ByteBuffer imageDestination = levelData.sliceData;
final int sX = region.getX();
final int sY = region.getY();
int dX = region.getX() + textureSize / 2;
int dY = region.getY() + textureSize / 2;
dX = MathUtils.moduloPositive(dX, textureSize);
dY = MathUtils.moduloPositive(dY, textureSize);
cache.updateRegion(imageDestination, sX, sY, dX + 1, dY + 1, region.getWidth(), region.getHeight());
}
for (final int unit : affectedUnits) {
final LevelData levelData = levelDataList.get(unit);
final ByteBuffer imageDestination = levelData.sliceData;
// TODO: only update subpart
imageDestination.rewind();
renderer.updateTexture3DSubImage(textureClipmap, 0, 0, unit, textureSize, textureSize, 1,