use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class TerrainGrid method getTerrainAt.
/**
* Get the terrain tile at the specified world location, in XZ coordinates.
*/
public Terrain getTerrainAt(Vector3f worldLocation) {
if (worldLocation == null)
return null;
Vector3f tileCell = getTileCell(worldLocation.setY(0));
tileCell = new Vector3f(Math.round(tileCell.x), tileCell.y, Math.round(tileCell.z));
return cache.get(tileCell);
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class TerrainGridLodControl method updateLOD.
@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);
// get the grid index value of where the camera is (ie. 2,1)
Vector3f camCell = terrainGrid.getCamCell(cam);
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);
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method createQuad.
/**
* Quadrants, world coordinates, and heightmap coordinates (Y-up):
*
* -z
* -u |
* -v 1|3
* -x ----+---- x
* 2|4 u
* | v
* z
* <code>createQuad</code> generates four new quads from this quad.
* The heightmap's top left (0,0) coordinate is at the bottom, -x,-z
* coordinate of the terrain, so it grows in the positive x.z direction.
*/
protected void createQuad(int blockSize, float[] heightMap) {
// create 4 terrain quads
int quarterSize = size >> 2;
int split = (size + 1) >> 1;
Vector2f tempOffset = new Vector2f();
offsetAmount += quarterSize;
//if (lodCalculator == null)
// lodCalculator = createDefaultLodCalculator(); // set a default one
// 1 upper left of heightmap, upper left quad
float[] heightBlock1 = createHeightSubBlock(heightMap, 0, 0, split);
Vector3f origin1 = new Vector3f(-quarterSize * stepScale.x, 0, -quarterSize * stepScale.z);
tempOffset.x = offset.x;
tempOffset.y = offset.y;
tempOffset.x += origin1.x;
tempOffset.y += origin1.z;
TerrainQuad quad1 = new TerrainQuad(getName() + "Quad1", blockSize, split, stepScale, heightBlock1, totalSize, tempOffset, offsetAmount);
quad1.setLocalTranslation(origin1);
quad1.quadrant = 1;
this.attachChild(quad1);
// 2 lower left of heightmap, lower left quad
float[] heightBlock2 = createHeightSubBlock(heightMap, 0, split - 1, split);
Vector3f origin2 = new Vector3f(-quarterSize * stepScale.x, 0, quarterSize * stepScale.z);
tempOffset = new Vector2f();
tempOffset.x = offset.x;
tempOffset.y = offset.y;
tempOffset.x += origin2.x;
tempOffset.y += origin2.z;
TerrainQuad quad2 = new TerrainQuad(getName() + "Quad2", blockSize, split, stepScale, heightBlock2, totalSize, tempOffset, offsetAmount);
quad2.setLocalTranslation(origin2);
quad2.quadrant = 2;
this.attachChild(quad2);
// 3 upper right of heightmap, upper right quad
float[] heightBlock3 = createHeightSubBlock(heightMap, split - 1, 0, split);
Vector3f origin3 = new Vector3f(quarterSize * stepScale.x, 0, -quarterSize * stepScale.z);
tempOffset = new Vector2f();
tempOffset.x = offset.x;
tempOffset.y = offset.y;
tempOffset.x += origin3.x;
tempOffset.y += origin3.z;
TerrainQuad quad3 = new TerrainQuad(getName() + "Quad3", blockSize, split, stepScale, heightBlock3, totalSize, tempOffset, offsetAmount);
quad3.setLocalTranslation(origin3);
quad3.quadrant = 3;
this.attachChild(quad3);
// 4 lower right of heightmap, lower right quad
float[] heightBlock4 = createHeightSubBlock(heightMap, split - 1, split - 1, split);
Vector3f origin4 = new Vector3f(quarterSize * stepScale.x, 0, quarterSize * stepScale.z);
tempOffset = new Vector2f();
tempOffset.x = offset.x;
tempOffset.y = offset.y;
tempOffset.x += origin4.x;
tempOffset.y += origin4.z;
TerrainQuad quad4 = new TerrainQuad(getName() + "Quad4", blockSize, split, stepScale, heightBlock4, totalSize, tempOffset, offsetAmount);
quad4.setLocalTranslation(origin4);
quad4.quadrant = 4;
this.attachChild(quad4);
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method findPick.
/**
* Gather the terrain patches that intersect the given ray (toTest).
* This only tests the bounding boxes
* @param toTest
* @param results
*/
public void findPick(Ray toTest, List<TerrainPickData> results) {
if (getWorldBound() != null) {
if (getWorldBound().intersects(toTest)) {
// further checking needed.
for (int i = 0; i < getQuantity(); i++) {
if (children.get(i) instanceof TerrainPatch) {
TerrainPatch tp = (TerrainPatch) children.get(i);
tp.ensurePositiveVolumeBBox();
if (tp.getWorldBound().intersects(toTest)) {
CollisionResults cr = new CollisionResults();
toTest.collideWith(tp.getWorldBound(), cr);
if (cr != null && cr.getClosestCollision() != null) {
cr.getClosestCollision().getDistance();
results.add(new TerrainPickData(tp, cr.getClosestCollision()));
}
}
} else if (children.get(i) instanceof TerrainQuad) {
((TerrainQuad) children.get(i)).findPick(toTest, results);
}
}
}
}
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class AssetTileLoader method getTerrainQuadAt.
public TerrainQuad getTerrainQuadAt(Vector3f location) {
String modelName = assetPath + "/" + name + "_" + Math.round(location.x) + "_" + Math.round(location.y) + "_" + Math.round(location.z) + ".j3o";
Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Load terrain grid tile: {0}", modelName);
TerrainQuad quad = null;
try {
quad = (TerrainQuad) manager.loadModel(modelName);
} catch (Exception e) {
// e.printStackTrace();
}
if (quad == null) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Could not load terrain grid tile: {0}", modelName);
quad = createNewQuad(location);
} else {
Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Loaded terrain grid tile: {0}", modelName);
}
return quad;
}
Aggregations