From 9568cf0ea66494b16a658409b08194416fce89c5 Mon Sep 17 00:00:00 2001 From: "bre..ns" Date: Wed, 29 Jun 2011 21:57:46 +0000 Subject: [PATCH] * added the LOD control to terrain in the editor * increased terrain default scale * fixed a bug in the paint tool when terrain scale was not 1 git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7785 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../TerrainEditorController.java | 42 +++++++++++++------ .../TerrainEditorTopComponent.java | 2 + .../tools/LevelTerrainToolAction.java | 2 +- .../tools/PaintTerrainToolAction.java | 16 ++++++- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorController.java b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorController.java index a17253556..92f2f894f 100644 --- a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorController.java +++ b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorController.java @@ -689,7 +689,7 @@ public class TerrainEditorController { { AssetManager manager = SceneApplication.getApplication().getAssetManager(); - TerrainQuad terrain = new TerrainQuad("terrain-"+sceneName, patchSize, totalSize, heightmapData); //TODO make this pluggable for different Terrain implementations + Terrain terrain = new TerrainQuad("terrain-"+sceneName, patchSize, totalSize, heightmapData); //TODO make this pluggable for different Terrain implementations com.jme3.material.Material mat = new com.jme3.material.Material(manager, "Common/MatDefs/Terrain/TerrainLighting.j3md"); String assetFolder = ""; @@ -708,7 +708,7 @@ public class TerrainEditorController { File alphaFolder = new File(assetFolder+"/Textures/terrain-alpha/"); if (!alphaFolder.exists()) alphaFolder.mkdir(); - String alphaBlendFileName = "/Textures/terrain-alpha/"+sceneName+"-"+terrain.getName()+"-alphablend"+i+".png"; + String alphaBlendFileName = "/Textures/terrain-alpha/"+sceneName+"-"+((Node)terrain).getName()+"-alphablend"+i+".png"; File alphaImageFile = new File(assetFolder+alphaBlendFileName); ImageIO.write(alphaBlend, "png", alphaImageFile); Texture tex = manager.loadAsset(new TextureKey(alphaBlendFileName, false)); @@ -727,23 +727,21 @@ public class TerrainEditorController { mat.setFloat("DiffuseMap_0_scale", DEFAULT_TEXTURE_SCALE); mat.setBoolean("WardIso", true); - terrain.setMaterial(mat); - terrain.setModelBound(new BoundingBox()); - terrain.updateModelBound(); - terrain.setLocalTranslation(0, 0, 0); - terrain.setLocalScale(1f, 1f, 1f); + ((Node)terrain).setMaterial(mat); + ((Node)terrain).setModelBound(new BoundingBox()); + ((Node)terrain).updateModelBound(); + ((Node)terrain).setLocalTranslation(0, 0, 0); + ((Node)terrain).setLocalScale(4f, 1f, 4f); // add the lod control - List cameras = new ArrayList(); - cameras.add(SceneApplication.getApplication().getCamera()); - TerrainLodControl control = new TerrainLodControl(terrain, cameras); - //terrain.addControl(control); // removing this until we figure out a way to have it get the cameras when saved/loaded + TerrainLodControl control = new TerrainLodControl(terrain, SceneApplication.getApplication().getCamera()); + ((Node)terrain).addControl(control); - parent.attachChild(terrain); + parent.attachChild((Node)terrain); setNeedsSave(true); - addSpatialUndo(parent, terrain, jmeNodeParent); + addSpatialUndo(parent, (Node)terrain, jmeNodeParent); return terrain; } @@ -1081,6 +1079,24 @@ public class TerrainEditorController { setNeedsSave(true); } + /** + * Re-attach the camera to the LOD control. + * Called when the scene is opened and will only + * update the control if there is already a terrain present in + * the scene. + */ + protected void enableLodControl() { + Terrain terrain = (Terrain) getTerrain(null); + if (terrain == null) + return; + + TerrainQuad t = (TerrainQuad)terrain; + TerrainLodControl control = t.getControl(TerrainLodControl.class); + if (control != null) { + control.setCamera(SceneApplication.getApplication().getCamera()); + } + } + } diff --git a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorTopComponent.java b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorTopComponent.java index 002ee7954..4ae6ab997 100644 --- a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorTopComponent.java +++ b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorTopComponent.java @@ -1022,6 +1022,8 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce terrainDeletedNodeListener = new TerrainNodeListener(); editorController.enableTextureButtons(); + + editorController.enableLodControl(); } // run on GL thread diff --git a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/LevelTerrainToolAction.java b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/LevelTerrainToolAction.java index 96b862abd..90d345420 100644 --- a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/LevelTerrainToolAction.java +++ b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/LevelTerrainToolAction.java @@ -109,7 +109,7 @@ public class LevelTerrainToolAction extends AbstractTerrainToolAction { Vector2f terrainLoc = new Vector2f(locX, locZ); // adjust height based on radius of the tool - float terrainHeightAtLoc = terrain.getHeightmapHeight(terrainLoc)*terrain.getSpatial().getWorldScale().y; + float terrainHeightAtLoc = terrain.getHeightmapHeight(terrainLoc)*((Node)terrain).getWorldScale().y; float radiusWeight = ToolUtils.calculateRadiusPercent(radius, locX-worldLoc.x, locZ-worldLoc.z); float epsilon = 0.1f*height; // rounding error for snapping diff --git a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/PaintTerrainToolAction.java b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/PaintTerrainToolAction.java index 7fb61ae33..f1f445edf 100644 --- a/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/PaintTerrainToolAction.java +++ b/sdk/jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/PaintTerrainToolAction.java @@ -87,10 +87,10 @@ public class PaintTerrainToolAction extends AbstractTerrainToolAction { Texture tex = getAlphaTexture(terrain, alphaIdx); Image image = tex.getImage(); - Vector2f UV = terrain.getPointPercentagePosition(markerLocation.x, markerLocation.z); + Vector2f UV = getPointPercentagePosition(terrain, markerLocation); // get the radius of the brush in pixel-percent - float brushSize = toolRadius/((TerrainQuad)terrain).getTotalSize(); + float brushSize = toolRadius/(terrain.getTerrainSize()*((Node)terrain).getLocalScale().x); int texIndex = selectedTextureIndex - ((selectedTextureIndex/4)*4); // selectedTextureIndex/4 is an int floor, do not simplify the equation boolean erase = toolWeight<0; if (erase) @@ -101,6 +101,18 @@ public class PaintTerrainToolAction extends AbstractTerrainToolAction { tex.getImage().setUpdateNeeded(); } + public Vector2f getPointPercentagePosition(Terrain terrain, Vector3f worldLoc) { + Vector2f uv = new Vector2f(worldLoc.x,worldLoc.z); + float scale = ((Node)terrain).getLocalScale().x; + + uv.subtractLocal(((Node)terrain).getLocalTranslation().x*scale, ((Node)terrain).getLocalTranslation().z*scale); // center it on 0,0 + float scaledSize = terrain.getTerrainSize()*scale; + uv.addLocal(scaledSize/2, scaledSize/2); // shift the bottom left corner up to 0,0 + uv.divideLocal(scaledSize); // get the location as a percentage + + return uv; + } + private Texture getAlphaTexture(Terrain terrain, int alphaLayer) { if (terrain == null) return null;