Class TerrainGrid
- All Implemented Interfaces:
HasLocalTransform
,CloneableSmartAsset
,Collidable
,Savable
,Terrain
,JmeCloneable
,Cloneable
TerrainGrid itself is an actual TerrainQuad. Its four children are the visible four tiles.
The grid is indexed by cells. Each cell has an integer XZ coordinate originating at 0,0. TerrainGrid will piggyback on the TerrainLodControl so it can use the camera for its updates as well. It does this in the overwritten update() method.
It uses an LRU (Least Recently Used) cache of 16 terrain tiles (full TerrainQuadTrees). The center 4 are the ones that are visible. As the camera moves, it checks what camera cell it is in and will attach the now visible tiles.
The 'quadIndex' variable is a 4x4 array that represents the tiles. The center four (index numbers: 5, 6, 9, 10) are what is visible. Each quadIndex value is an offset vector. The vector contains whole numbers and represents how many tiles in offset this location is from the center of the map. So for example the index 11 [Vector3f(2, 0, 1)] is located 2*terrainSize in X axis and 1*terrainSize in Z axis.
As the camera moves, it tests what cameraCell it is in. Each camera cell covers four quad tiles and is half way inside each one.
+-------+-------+ | 1 | 3 | Four terrainQuads that make up the grid | *..|..* | with the cameraCell in the middle, covering |----|--|--|----| all four quads. | *..|..* | | 2 | 4 | +-------+-------+
This results in the effect of when the camera gets half way across one of the sides of a quad to an empty (non-loaded) area, it will trigger the system to load in the next tiles.
The tile loading is done on a background thread, and once the tile is loaded, then it is attached to the grid quad tree, back on the OGL thread. It will grab the terrain quad from the LRU cache if it exists. If it does not exist, it will load in the new TerrainQuad tile.
The loading of new tiles triggers events for any TerrainGridListeners. The events are:
- tile Attached
- tile Detached
- grid moved.
These allow physics to update, and other operation (often needed for loading the terrain) to occur at the right time.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.jme3.terrain.geomipmap.TerrainQuad
TerrainQuad.LocationHeight
Nested classes/interfaces inherited from class com.jme3.scene.Spatial
Spatial.BatchHint, Spatial.CullHint, Spatial.DFSMode
-
Field Summary
Modifier and TypeFieldDescriptionprotected LRUCache<Vector3f,
TerrainQuad> protected ExecutorService
protected int
protected Vector3f
protected int[]
protected Set<TerrainGridListener>
protected static final Logger
protected Material
protected Vector3f[]
protected int
protected int
protected boolean
Fields inherited from class com.jme3.terrain.geomipmap.TerrainQuad
neighbourFinder, offset, offsetAmount, patchSize, quadrant, size, stepScale, totalSize
Fields inherited from class com.jme3.scene.Spatial
batchHint, controls, cullHint, frustrumIntersects, key, localLights, localOverrides, localTransform, name, parent, queueBucket, queueDistance, refreshFlags, RF_BOUND, RF_CHILD_LIGHTLIST, RF_LIGHTLIST, RF_MATPARAM_OVERRIDE, RF_TRANSFORM, shadowMode, userData, worldBound, worldLights, worldOverrides, worldTransform
-
Constructor Summary
ConstructorDescriptionTerrainGrid
(String name, int patchSize, int maxVisibleSize, Vector3f scale, TerrainGridTileLoader terrainQuadGrid) TerrainGrid
(String name, int patchSize, int maxVisibleSize, Vector3f scale, TerrainGridTileLoader terrainQuadGrid, Vector2f offset, float offsetAmount) TerrainGrid
(String name, int patchSize, int maxVisibleSize, TerrainGridTileLoader terrainQuadGrid) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(TerrainGridListener listener) void
adjustHeight
(List<Vector2f> xz, List<Float> height) Raise/lower the height at many points.protected void
attachQuadAt
(TerrainQuad q, int quadrant, Vector3f quadCell, boolean shifted) Runs on the rendering threadprotected ExecutorService
This will print out any exceptions from the threadgetCamCell
(Vector3f location) Get the location in cell-coordinates of the specified location.protected float
getHeightmapHeight
(int x, int z) This will just get the heightmap value at the supplied point, not an interpolated (actual) height value.getMaterial
(Vector3f worldLocation) Returns the material that this terrain uses.int
protected int
getQuadrant
(int quadIndex) getTerrainAt
(Vector3f worldLocation) Get the terrain tile at the specified world location, in XZ coordinates.getTerrainAtCell
(Vector3f cellCoordinate) Get the terrain tile at the specified XZ cell coordinate (not world coordinate).getTileCell
(Vector3f location) Centered at 0,0.protected boolean
isCenter
(int quadIndex) void
read
(JmeImporter im) void
removeListener
(TerrainGridListener listener) protected void
void
setMaterial
(Material mat) Applies the given material to the Spatial, this will propagate the material down to the geometries in the scene graph.void
setQuadSize
(int quadSize) toCellSpace
(Vector3f worldLocation) Convert the world location into a cell location (integer coordinates)toWorldSpace
(Vector3f cellLocation) Convert the cell coordinate (integer coordinates) into world coordinates.protected void
updateChildren
(Vector3f camCell) Called when the camera has moved into a new cell.void
write
(JmeExporter ex) Methods inherited from class com.jme3.terrain.geomipmap.TerrainQuad
adjustHeight, attachBoundChildren, cacheTerrainTransforms, calculateLod, clearCaches, clone, clone, cloneFields, collideWith, createHeightSubBlock, createQuad, createQuadPatch, findDownPatch, findDownQuad, findLeftPatch, findLeftQuad, findNeighboursLod, findPick, findRightPatch, findRightQuad, findTopPatch, findTopQuad, fixEdges, fixNormalEdges, fixNormals, generateDebugTangents, generateEntropy, getAllTerrainPatches, getAllTerrainPatchesWithTranslation, getHeight, getHeight, getHeightMap, getHeightmapHeight, getMaterial, getMaxLod, getMeshNormal, getNormal, getNormal, getPatch, getPatchSize, getQuad, getQuadrant, getTerrainSize, getTotalSize, isPointOnTerrain, isRootQuad, needToRecalculateNormals, recalculateAllNormals, reIndexPages, resetCachedNeighbours, setHeight, setHeight, setHeight, setHeight, setLocked, setNeedToRecalculateNormals, setNeighbourFinder, setNormalRecalcNeeded, setParent, setQuadrant, setSupportMultipleCollisions, split, updateNormals
Methods inherited from class com.jme3.scene.Node
attachChild, attachChildAt, breadthFirstTraversal, deepClone, depthFirstTraversal, descendantMatches, descendantMatches, descendantMatches, detachAllChildren, detachChild, detachChildAt, detachChildNamed, getChild, getChild, getChildIndex, getChildren, getQuantity, getTriangleCount, getVertexCount, hasChild, oldDeepClone, setLightListRefresh, setLodLevel, setMatParamOverrideRefresh, setModelBound, setTransformRefresh, swapChildren, updateGeometricState, updateLogicalState, updateModelBound, updateWorldBound
Methods inherited from class com.jme3.scene.Spatial
addControl, addControlAt, addLight, addMatParamOverride, breadthFirstTraversal, center, checkCulling, clearMatParamOverrides, depthFirstTraversal, forceRefresh, getBatchHint, getControl, getControl, getCullHint, getKey, getLastFrustumIntersection, getLocalBatchHint, getLocalCullHint, getLocalLightList, getLocalMatParamOverrides, getLocalQueueBucket, getLocalRotation, getLocalScale, getLocalShadowMode, getLocalToWorldMatrix, getLocalTransform, getLocalTranslation, getName, getNumControls, getParent, getQueueBucket, getShadowMode, getUserData, getUserDataKeys, getWorldBound, getWorldLightList, getWorldMatParamOverrides, getWorldRotation, getWorldScale, getWorldTransform, getWorldTranslation, hasAncestor, jmeClone, localToWorld, lookAt, matches, move, move, oldClone, removeControl, removeControl, removeFromParent, removeLight, removeMatParamOverride, rotate, rotate, rotateUpTo, runControlRender, scale, scale, setBatchHint, setBoundRefresh, setCullHint, setKey, setLastFrustumIntersection, setLocalRotation, setLocalRotation, setLocalScale, setLocalScale, setLocalScale, setLocalTransform, setLocalTranslation, setLocalTranslation, setName, setQueueBucket, setRequiresUpdates, setShadowMode, setUserData, toString, updateMatParamOverrides, updateWorldLightList, updateWorldTransforms, worldToLocal
-
Field Details
-
log
-
currentCamCell
-
quarterSize
protected int quarterSize -
quadSize
protected int quadSize -
quadIndex
-
listeners
-
material
-
cache
-
cellsLoaded
protected int cellsLoaded -
gridOffset
protected int[] gridOffset -
runOnce
protected boolean runOnce -
cacheExecutor
-
-
Constructor Details
-
TerrainGrid
public TerrainGrid(String name, int patchSize, int maxVisibleSize, Vector3f scale, TerrainGridTileLoader terrainQuadGrid, Vector2f offset, float offsetAmount) -
TerrainGrid
public TerrainGrid(String name, int patchSize, int maxVisibleSize, Vector3f scale, TerrainGridTileLoader terrainQuadGrid) -
TerrainGrid
public TerrainGrid(String name, int patchSize, int maxVisibleSize, TerrainGridTileLoader terrainQuadGrid) -
TerrainGrid
public TerrainGrid()
-
-
Method Details
-
isCenter
protected boolean isCenter(int quadIndex) -
getQuadrant
protected int getQuadrant(int quadIndex) -
getCamCell
Get the location in cell-coordinates of the specified location. Cell coordinates are integer coordinates, usually with y=0, each representing a cell in the world. For example, moving right in the +X direction: (0,0,0) (1,0,0) (2,0,0), (3,0,0) and then down the -Z direction: (3,0,-1) (3,0,-2) (3,0,-3) -
getTileCell
Centered at 0,0. Get the tile index location in integer form:- Parameters:
location
- world coordinate
-
getGridTileLoader
-
getTerrainAt
Get the terrain tile at the specified world location, in XZ coordinates. -
getTerrainAtCell
Get the terrain tile at the specified XZ cell coordinate (not world coordinate).- Parameters:
cellCoordinate
- integer cell coordinates- Returns:
- the terrain tile at that location
-
toCellSpace
Convert the world location into a cell location (integer coordinates) -
toWorldSpace
Convert the cell coordinate (integer coordinates) into world coordinates. -
removeQuad
-
attachQuadAt
Runs on the rendering thread- Parameters:
shifted
- quads are still attached to the parent and don't need to re-load
-
updateChildren
Called when the camera has moved into a new cell. We need to update what quads are in the scene now. Step 1: touch cache LRU cache is used, so elements that need to remain should be touched. Step 2: load new quads in background thread if the camera has moved into a new cell, we load in new quads- Parameters:
camCell
- the cell the camera is in
-
addListener
-
getCurrentCell
-
removeListener
-
setMaterial
Description copied from class:Spatial
Applies the given material to the Spatial, this will propagate the material down to the geometries in the scene graph.- Overrides:
setMaterial
in classNode
- Parameters:
mat
- The material to set.
-
setQuadSize
public void setQuadSize(int quadSize) -
adjustHeight
Description copied from interface:Terrain
Raise/lower the height at many points. The two lists must be the same size. Each xz coordinate entry matches to a height entry, 1 for 1. So the first coordinate matches to the first height value, the last to the last etc.- Specified by:
adjustHeight
in interfaceTerrain
- Overrides:
adjustHeight
in classTerrainQuad
- Parameters:
xz
- a list of coordinates where the height will be adjustedheight
- +- value to adjust the height by, that matches the xz coordinates
-
getHeightmapHeight
protected float getHeightmapHeight(int x, int z) Description copied from class:TerrainQuad
This will just get the heightmap value at the supplied point, not an interpolated (actual) height value.- Overrides:
getHeightmapHeight
in classTerrainQuad
-
getNumMajorSubdivisions
public int getNumMajorSubdivisions()- Specified by:
getNumMajorSubdivisions
in interfaceTerrain
- Overrides:
getNumMajorSubdivisions
in classTerrainQuad
-
getMaterial
Description copied from interface:Terrain
Returns the material that this terrain uses. Terrain can have different materials in different locations. In general, the TerrainQuad will only have one material. But TerrainGrid will have a different material per tile. It could be possible to pass in null for the location, some Terrain implementations might just have the one material and not care where you are looking. So implementations must handle null being supplied.- Specified by:
getMaterial
in interfaceTerrain
- Overrides:
getMaterial
in classTerrainQuad
- Parameters:
worldLocation
- the location, in world coordinates, of where we are interested in the underlying texture.
-
createExecutorService
This will print out any exceptions from the thread -
read
- Specified by:
read
in interfaceSavable
- Overrides:
read
in classTerrainQuad
- Throws:
IOException
-
write
- Specified by:
write
in interfaceSavable
- Overrides:
write
in classTerrainQuad
- Throws:
IOException
-