Search in sources :

Example 6 with TerrainGridListener

use of com.jme3.terrain.geomipmap.TerrainGridListener 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);
}
Also used : Vector3f(com.jme3.math.Vector3f)

Aggregations

Vector3f (com.jme3.math.Vector3f)6 ScreenshotAppState (com.jme3.app.state.ScreenshotAppState)4 BulletAppState (com.jme3.bullet.BulletAppState)4 CapsuleCollisionShape (com.jme3.bullet.collision.shapes.CapsuleCollisionShape)4 HeightfieldCollisionShape (com.jme3.bullet.collision.shapes.HeightfieldCollisionShape)4 CharacterControl (com.jme3.bullet.control.CharacterControl)4 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)4 ColorRGBA (com.jme3.math.ColorRGBA)4 TerrainGridListener (com.jme3.terrain.geomipmap.TerrainGridListener)4 TerrainGridLodControl (com.jme3.terrain.geomipmap.TerrainGridLodControl)4 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)4 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)4 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)4 File (java.io.File)4 Material (com.jme3.material.Material)3 TerrainGrid (com.jme3.terrain.geomipmap.TerrainGrid)3 Texture (com.jme3.texture.Texture)3 DirectionalLight (com.jme3.light.DirectionalLight)2 AmbientLight (com.jme3.light.AmbientLight)1 Node (com.jme3.scene.Node)1