@Override protected void updateLOD(SafeArrayList<Vector3f> locations, LodCalculator lodCalculator) { TerrainGrid terrainGrid = (TerrainGrid)getSpatial(); // for now, only the first camera is handled. // to accept more, there are two ways: // 1: every camera has an associated grid, then the location is not enough to identify which camera location has changed // 2: grids are associated with locations, and no incremental update is done, we load new grids for new locations, and unload those that are not needed anymore Vector3f cam = locations.isEmpty() ? Vector3f.ZERO.clone() : locations.get(0); Vector3f camCell = terrainGrid.getCamCell(cam); // get the grid index value of where the camera is (ie. 2,1) if (terrainGrid.cellsLoaded > 1) { // Check if cells are updated before updating gridoffset. terrainGrid.gridOffset[0] = Math.round(camCell.x * (terrainGrid.size / 2)); terrainGrid.gridOffset[1] = Math.round(camCell.z * (terrainGrid.size / 2)); terrainGrid.cellsLoaded = 0; } if (camCell.x != terrainGrid.currentCamCell.x || camCell.z != terrainGrid.currentCamCell.z || !terrainGrid.runOnce) { // if the camera has moved into a new cell, load new terrain into the visible 4 center quads terrainGrid.updateChildren(camCell); for (TerrainGridListener l : terrainGrid.listeners) { l.gridMoved(camCell); } } terrainGrid.runOnce = true; super.updateLOD(locations, lodCalculator); } }
/** * Runs on the rendering thread * @param shifted quads are still attached to the parent and don't need to re-load */ protected void attachQuadAt(TerrainQuad q, int quadrant, Vector3f quadCell, boolean shifted) { q.setQuadrant((short) quadrant); if (!shifted) this.attachChild(q); Vector3f loc = quadCell.mult(this.quadSize - 1).subtract(quarterSize, 0, quarterSize);// quadrant location handled TerrainQuad automatically now q.setLocalTranslation(loc); if (!shifted) { for (TerrainGridListener l : listeners) { l.tileAttached(quadCell, q); } } updateModelBound(); }
protected void removeQuad(TerrainQuad q) { if (q != null && ( (q.getQuadrant() > 0 && q.getQuadrant()<5) || q.getParent() != null) ) { for (TerrainGridListener l : listeners) { l.tileDetached(getTileCell(q.getWorldTranslation()), q); } q.setQuadrant((short)0); this.detachChild(q); cellsLoaded++; // For gridoffset calc., maybe the run() method is a better location for this. } }
protected void removeQuad(TerrainQuad q) { if (q != null && ( (q.getQuadrant() > 0 && q.getQuadrant()<5) || q.getParent() != null) ) { for (TerrainGridListener l : listeners) { l.tileDetached(getTileCell(q.getWorldTranslation()), q); } q.setQuadrant((short)0); this.detachChild(q); cellsLoaded++; // For gridoffset calc., maybe the run() method is a better location for this. } }
/** * Runs on the rendering thread * @param shifted quads are still attached to the parent and don't need to re-load */ protected void attachQuadAt(TerrainQuad q, int quadrant, Vector3f quadCell, boolean shifted) { q.setQuadrant((short) quadrant); if (!shifted) this.attachChild(q); Vector3f loc = quadCell.mult(this.quadSize - 1).subtract(quarterSize, 0, quarterSize);// quadrant location handled TerrainQuad automatically now q.setLocalTranslation(loc); if (!shifted) { for (TerrainGridListener l : listeners) { l.tileAttached(quadCell, q); } } updateModelBound(); }
@Override protected void updateLOD(List<Vector3f> locations, LodCalculator lodCalculator) { TerrainGrid terrainGrid = (TerrainGrid)getSpatial(); // for now, only the first camera is handled. // to accept more, there are two ways: // 1: every camera has an associated grid, then the location is not enough to identify which camera location has changed // 2: grids are associated with locations, and no incremental update is done, we load new grids for new locations, and unload those that are not needed anymore Vector3f cam = locations.isEmpty() ? Vector3f.ZERO.clone() : locations.get(0); Vector3f camCell = terrainGrid.getCamCell(cam); // get the grid index value of where the camera is (ie. 2,1) if (terrainGrid.cellsLoaded > 1) { // Check if cells are updated before updating gridoffset. terrainGrid.gridOffset[0] = Math.round(camCell.x * (terrainGrid.size / 2)); terrainGrid.gridOffset[1] = Math.round(camCell.z * (terrainGrid.size / 2)); terrainGrid.cellsLoaded = 0; } if (camCell.x != terrainGrid.currentCamCell.x || camCell.z != terrainGrid.currentCamCell.z || !terrainGrid.runOnce) { // if the camera has moved into a new cell, load new terrain into the visible 4 center quads terrainGrid.updateChildren(camCell); for (TerrainGridListener l : terrainGrid.listeners) { l.gridMoved(camCell); } } terrainGrid.runOnce = true; super.updateLOD(locations, lodCalculator); } }