|
@@ -33,6 +33,7 @@ package jme3test.terrain;
|
|
|
|
|
|
|
|
import com.jme3.app.SimpleApplication;
|
|
import com.jme3.app.SimpleApplication;
|
|
|
import com.jme3.bounding.BoundingBox;
|
|
import com.jme3.bounding.BoundingBox;
|
|
|
|
|
+import com.jme3.export.Savable;
|
|
|
import com.jme3.export.binary.BinaryExporter;
|
|
import com.jme3.export.binary.BinaryExporter;
|
|
|
import com.jme3.export.binary.BinaryImporter;
|
|
import com.jme3.export.binary.BinaryImporter;
|
|
|
import com.jme3.font.BitmapText;
|
|
import com.jme3.font.BitmapText;
|
|
@@ -44,6 +45,8 @@ import com.jme3.material.Material;
|
|
|
import com.jme3.math.ColorRGBA;
|
|
import com.jme3.math.ColorRGBA;
|
|
|
import com.jme3.math.Vector3f;
|
|
import com.jme3.math.Vector3f;
|
|
|
import com.jme3.renderer.Camera;
|
|
import com.jme3.renderer.Camera;
|
|
|
|
|
+import com.jme3.scene.Node;
|
|
|
|
|
+import com.jme3.terrain.Terrain;
|
|
|
import com.jme3.terrain.geomipmap.TerrainLodControl;
|
|
import com.jme3.terrain.geomipmap.TerrainLodControl;
|
|
|
import com.jme3.terrain.geomipmap.TerrainQuad;
|
|
import com.jme3.terrain.geomipmap.TerrainQuad;
|
|
|
import com.jme3.terrain.heightmap.AbstractHeightMap;
|
|
import com.jme3.terrain.heightmap.AbstractHeightMap;
|
|
@@ -69,7 +72,7 @@ import jme3tools.converters.ImageToAwt;
|
|
|
*/
|
|
*/
|
|
|
public class TerrainTestReadWrite extends SimpleApplication {
|
|
public class TerrainTestReadWrite extends SimpleApplication {
|
|
|
|
|
|
|
|
- private TerrainQuad terrain;
|
|
|
|
|
|
|
+ private Terrain terrain;
|
|
|
protected BitmapText hintText;
|
|
protected BitmapText hintText;
|
|
|
private float grassScale = 64;
|
|
private float grassScale = 64;
|
|
|
private float dirtScale = 16;
|
|
private float dirtScale = 16;
|
|
@@ -147,8 +150,6 @@ public class TerrainTestReadWrite extends SimpleApplication {
|
|
|
// CREATE HEIGHTMAP
|
|
// CREATE HEIGHTMAP
|
|
|
AbstractHeightMap heightmap = null;
|
|
AbstractHeightMap heightmap = null;
|
|
|
try {
|
|
try {
|
|
|
- //heightmap = new HillHeightMap(1025, 1000, 50, 100, (byte) 3);
|
|
|
|
|
-
|
|
|
|
|
heightmap = new ImageBasedHeightMap(ImageToAwt.convert(heightMapImage.getImage(), false, true, 0), 1f);
|
|
heightmap = new ImageBasedHeightMap(ImageToAwt.convert(heightMapImage.getImage(), false, true, 0), 1f);
|
|
|
heightmap.load();
|
|
heightmap.load();
|
|
|
|
|
|
|
@@ -160,15 +161,17 @@ public class TerrainTestReadWrite extends SimpleApplication {
|
|
|
loadTerrain();
|
|
loadTerrain();
|
|
|
} else {
|
|
} else {
|
|
|
// create the terrain as normal, and give it a control for LOD management
|
|
// create the terrain as normal, and give it a control for LOD management
|
|
|
- terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());//, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
|
|
|
|
|
|
|
+ TerrainQuad terrainQuad = new TerrainQuad("terrain", 65, 129, heightmap.getHeightMap());//, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
|
|
|
List<Camera> cameras = new ArrayList<Camera>();
|
|
List<Camera> cameras = new ArrayList<Camera>();
|
|
|
cameras.add(getCamera());
|
|
cameras.add(getCamera());
|
|
|
- TerrainLodControl control = new TerrainLodControl(terrain, cameras);
|
|
|
|
|
- terrain.addControl(control);
|
|
|
|
|
- terrain.setMaterial(matTerrain);
|
|
|
|
|
- terrain.setLocalTranslation(0, -100, 0);
|
|
|
|
|
- terrain.setLocalScale(2f, 1f, 2f);
|
|
|
|
|
- rootNode.attachChild(terrain);
|
|
|
|
|
|
|
+ TerrainLodControl control = new TerrainLodControl(terrainQuad, cameras);
|
|
|
|
|
+ terrainQuad.addControl(control);
|
|
|
|
|
+ terrainQuad.setMaterial(matTerrain);
|
|
|
|
|
+ terrainQuad.setLocalTranslation(0, -100, 0);
|
|
|
|
|
+ terrainQuad.setLocalScale(4f, 0.25f, 4f);
|
|
|
|
|
+ rootNode.attachChild(terrainQuad);
|
|
|
|
|
+
|
|
|
|
|
+ this.terrain = terrainQuad;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
DirectionalLight light = new DirectionalLight();
|
|
DirectionalLight light = new DirectionalLight();
|
|
@@ -211,7 +214,7 @@ public class TerrainTestReadWrite extends SimpleApplication {
|
|
|
fos = new FileOutputStream(new File("terrainsave.jme"));
|
|
fos = new FileOutputStream(new File("terrainsave.jme"));
|
|
|
|
|
|
|
|
// we just use the exporter and pass in the terrain
|
|
// we just use the exporter and pass in the terrain
|
|
|
- BinaryExporter.getInstance().save(terrain, new BufferedOutputStream(fos));
|
|
|
|
|
|
|
+ BinaryExporter.getInstance().save((Savable)terrain, new BufferedOutputStream(fos));
|
|
|
|
|
|
|
|
fos.flush();
|
|
fos.flush();
|
|
|
float duration = (System.currentTimeMillis() - start) / 1000.0f;
|
|
float duration = (System.currentTimeMillis() - start) / 1000.0f;
|
|
@@ -237,29 +240,28 @@ public class TerrainTestReadWrite extends SimpleApplication {
|
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
|
// remove the existing terrain and detach it from the root node.
|
|
// remove the existing terrain and detach it from the root node.
|
|
|
if (terrain != null) {
|
|
if (terrain != null) {
|
|
|
- terrain.removeFromParent();
|
|
|
|
|
- terrain.removeControl(TerrainLodControl.class);
|
|
|
|
|
- terrain.detachAllChildren();
|
|
|
|
|
|
|
+ Node existingTerrain = (Node)terrain;
|
|
|
|
|
+ existingTerrain.removeFromParent();
|
|
|
|
|
+ existingTerrain.removeControl(TerrainLodControl.class);
|
|
|
|
|
+ existingTerrain.detachAllChildren();
|
|
|
terrain = null;
|
|
terrain = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// import the saved terrain, and attach it back to the root node
|
|
// import the saved terrain, and attach it back to the root node
|
|
|
- fis = new FileInputStream(new File("terrainsave.jme"));
|
|
|
|
|
|
|
+ File f = new File("terrainsave.jme");
|
|
|
|
|
+ fis = new FileInputStream(f);
|
|
|
BinaryImporter imp = BinaryImporter.getInstance();
|
|
BinaryImporter imp = BinaryImporter.getInstance();
|
|
|
imp.setAssetManager(assetManager);
|
|
imp.setAssetManager(assetManager);
|
|
|
terrain = (TerrainQuad) imp.load(new BufferedInputStream(fis));
|
|
terrain = (TerrainQuad) imp.load(new BufferedInputStream(fis));
|
|
|
- rootNode.attachChild(terrain);
|
|
|
|
|
|
|
+ rootNode.attachChild((Node)terrain);
|
|
|
|
|
|
|
|
float duration = (System.currentTimeMillis() - start) / 1000.0f;
|
|
float duration = (System.currentTimeMillis() - start) / 1000.0f;
|
|
|
System.out.println("Load took " + duration + " seconds");
|
|
System.out.println("Load took " + duration + " seconds");
|
|
|
|
|
|
|
|
- // now we have to add back the cameras to the LOD control, since we didn't want to duplicate them on save
|
|
|
|
|
- List<Camera> cameras = new ArrayList<Camera>();
|
|
|
|
|
- cameras.add(getCamera());
|
|
|
|
|
- TerrainLodControl lodControl = terrain.getControl(TerrainLodControl.class);
|
|
|
|
|
- if (lodControl != null) {
|
|
|
|
|
- lodControl.setCameras(cameras);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // now we have to add back the camera to the LOD control
|
|
|
|
|
+ TerrainLodControl lodControl = ((Node)terrain).getControl(TerrainLodControl.class);
|
|
|
|
|
+ if (lodControl != null)
|
|
|
|
|
+ lodControl.setCamera(getCamera());
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
} catch (IOException ex) {
|
|
|
Logger.getLogger(TerrainTestReadWrite.class.getName()).log(Level.SEVERE, null, ex);
|
|
Logger.getLogger(TerrainTestReadWrite.class.getName()).log(Level.SEVERE, null, ex);
|
|
@@ -286,10 +288,10 @@ public class TerrainTestReadWrite extends SimpleApplication {
|
|
|
public void onAction(String name, boolean pressed, float tpf) {
|
|
public void onAction(String name, boolean pressed, float tpf) {
|
|
|
if (name.equals("clone") && !pressed) {
|
|
if (name.equals("clone") && !pressed) {
|
|
|
|
|
|
|
|
- TerrainQuad clone = terrain.clone();
|
|
|
|
|
- terrain.removeFromParent();
|
|
|
|
|
|
|
+ Terrain clone = (Terrain) ((Node)terrain).clone();
|
|
|
|
|
+ ((Node)terrain).removeFromParent();
|
|
|
terrain = clone;
|
|
terrain = clone;
|
|
|
- getRootNode().attachChild(terrain);
|
|
|
|
|
|
|
+ getRootNode().attachChild((Node)terrain);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|