use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class TerrainLodControl method write.
@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
OutputCapsule oc = ex.getCapsule(this);
oc.write((Node) terrain, "terrain", null);
oc.write(lodCalculator, "lodCalculator", null);
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class EntropyComputeUtil method computeLodEntropy.
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices) {
// Bounding box for the terrain block
BoundingBox bbox = (BoundingBox) terrainBlock.getBound();
// Vertex positions for the block
FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);
// Prepare to cast rays
Vector3f pos = new Vector3f();
Vector3f dir = new Vector3f(0, -1, 0);
Ray ray = new Ray(pos, dir);
// Prepare collision results
CollisionResults results = new CollisionResults();
// Set the LOD indices on the block
VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);
terrainBlock.clearBuffer(Type.Index);
if (lodIndices instanceof IntBuffer)
terrainBlock.setBuffer(Type.Index, 3, (IntBuffer) lodIndices);
else if (lodIndices instanceof ShortBuffer) {
terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
}
// Recalculate collision mesh
terrainBlock.createCollisionData();
float entropy = 0;
for (int i = 0; i < positions.limit() / 3; i++) {
BufferUtils.populateFromBuffer(pos, positions, i);
float realHeight = pos.y;
pos.addLocal(0, bbox.getYExtent(), 0);
ray.setOrigin(pos);
results.clear();
terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);
if (results.size() > 0) {
Vector3f contactPoint = results.getClosestCollision().getContactPoint();
float delta = Math.abs(realHeight - contactPoint.y);
entropy = Math.max(delta, entropy);
}
}
// Restore original indices
terrainBlock.clearBuffer(Type.Index);
terrainBlock.setBuffer(originalIndices);
return entropy;
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class ImageTileLoader method getHeightMapAt.
/**
* Lets you specify the type of images that are being loaded. All images
* must be the same type.
* @param imageType eg. BufferedImage.TYPE_USHORT_GRAY
*/
/*public void setImageType(int imageType) {
this.imageType = imageType;
}*/
/**
* The ImageHeightmap that will parse the image type that you
* specify with setImageType().
* @param customImageHeightmap must extend AbstractHeightmap
*/
/*public void setCustomImageHeightmap(ImageHeightmap customImageHeightmap) {
if (!(customImageHeightmap instanceof AbstractHeightMap)) {
throw new IllegalArgumentException("customImageHeightmap must be an AbstractHeightMap!");
}
this.customImageHeightmap = customImageHeightmap;
}*/
private HeightMap getHeightMapAt(Vector3f location) {
// HEIGHTMAP image (for the terrain heightmap)
int x = (int) location.x;
int z = (int) location.z;
AbstractHeightMap heightmap = null;
//BufferedImage im = null;
String name = null;
try {
name = namer.getName(x, z);
logger.log(Level.FINE, "Loading heightmap from file: {0}", name);
final Texture texture = assetManager.loadTexture(new TextureKey(name));
heightmap = new ImageBasedHeightMap(texture.getImage());
/*if (assetInfo != null){
InputStream in = assetInfo.openStream();
im = ImageIO.read(in);
} else {
im = new BufferedImage(patchSize, patchSize, imageType);
logger.log(Level.WARNING, "File: {0} not found, loading zero heightmap instead", name);
}*/
// CREATE HEIGHTMAP
/*if (imageType == BufferedImage.TYPE_USHORT_GRAY) {
heightmap = new Grayscale16BitHeightMap(im);
} else if (imageType == BufferedImage.TYPE_3BYTE_BGR) {
heightmap = new ImageBasedHeightMap(im);
} else if (customImageHeightmap != null && customImageHeightmap instanceof AbstractHeightMap) {
// If it gets here, it means you have specified a different image type, and you must
// then also supply a custom image heightmap class that can parse that image into
// a heightmap.
customImageHeightmap.setImage(im);
heightmap = (AbstractHeightMap) customImageHeightmap;
} else {
// error, no supported image format and no custom image heightmap specified
if (customImageHeightmap == null)
logger.log(Level.SEVERE, "Custom image type specified [{0}] but no customImageHeightmap declared! Use setCustomImageHeightmap()",imageType);
if (!(customImageHeightmap instanceof AbstractHeightMap))
logger.severe("customImageHeightmap must be an AbstractHeightMap!");
return null;
}*/
heightmap.setHeightScale(1);
heightmap.load();
//} catch (IOException e) {
// e.printStackTrace();
} catch (AssetNotFoundException e) {
logger.log(Level.WARNING, "Asset {0} not found, loading zero heightmap instead", name);
}
return heightmap;
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class TerrainLodControl method read.
@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule ic = im.getCapsule(this);
terrain = (Terrain) ic.readSavable("terrain", null);
lodCalculator = (LodCalculator) ic.readSavable("lodCalculator", new DistanceLodCalculator());
}
use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.
the class TestEverything method setupFloor.
public void setupFloor() {
Material mat = assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m");
Box floor = new Box(50, 1f, 50);
TangentBinormalGenerator.generate(floor);
floor.scaleTextureCoordinates(new Vector2f(5, 5));
Geometry floorGeom = new Geometry("Floor", floor);
floorGeom.setMaterial(mat);
floorGeom.setShadowMode(ShadowMode.Receive);
rootNode.attachChild(floorGeom);
}
Aggregations