Browse Source

terrain editor : fix issue where brushes seems to be offset from where the mouse is, caused by terrain not in world center

Dokthar 9 years ago
parent
commit
e76b6e9d1d

+ 2 - 0
jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainToolController.java

@@ -42,6 +42,8 @@ import com.jme3.gde.terraineditor.tools.TerrainTool;
 import com.jme3.input.event.KeyInputEvent;
 import com.jme3.math.Vector3f;
 import com.jme3.scene.Node;
+import com.jme3.scene.Spatial;
+import com.jme3.terrain.Terrain;
 import java.util.concurrent.Callable;
 import org.openide.loaders.DataObject;
 

+ 29 - 1
jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/AbstractTerrainToolAction.java

@@ -46,7 +46,8 @@ import org.openide.loaders.DataObject;
 public abstract class AbstractTerrainToolAction extends AbstractStatefulGLToolAction {
 
     private Terrain terrain;
-    
+    private Node terrainNode;
+
     protected Terrain getTerrain(Spatial root) {
 
         if (terrain != null)
@@ -55,6 +56,7 @@ public abstract class AbstractTerrainToolAction extends AbstractStatefulGLToolAc
         // is this the terrain?
         if (root instanceof Terrain && root instanceof Node) {
             terrain = (Terrain)root;
+            terrainNode = (Node)root;
             return terrain;
         }
 
@@ -72,6 +74,32 @@ public abstract class AbstractTerrainToolAction extends AbstractStatefulGLToolAc
         return null;
     }
     
+    protected Node getTerrainNode(Spatial root) {
+
+        if (terrainNode != null)
+            return terrainNode;
+        
+        // is this the terrain?
+        if (root instanceof Terrain && root instanceof Node) {
+            terrainNode = (Node)root;
+            terrain = (Terrain)root;
+            return terrainNode;
+        }
+
+        if (root instanceof Node) {
+            Node n = (Node) root;
+            for (Spatial c : n.getChildren()) {
+                if (c instanceof Node){
+                    Node res = getTerrainNode(c);
+                    if (res != null)
+                        return res;
+                }
+            }
+        }
+
+        return null;
+    }
+     
     @Override
     protected void setModified(final AbstractSceneExplorerNode rootNode, final DataObject dataObject) {
         if (dataObject.isModified())

+ 2 - 0
jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/LevelTerrainToolAction.java

@@ -75,6 +75,8 @@ public class LevelTerrainToolAction extends AbstractTerrainToolAction {
         Terrain terrain = getTerrain(rootNode.getLookup().lookup(Node.class));
         if (terrain == null)
             return null;
+        Node terrainNode = getTerrainNode(rootNode.getLookup().lookup(Node.class));
+        worldLoc.subtractLocal(terrainNode.getWorldTranslation());
         modifyHeight(terrain, levelTerrainLocation, worldLoc, radius, height, precision, mesh);
         return terrain;
     }

+ 7 - 4
jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/PaintTerrainToolAction.java

@@ -73,6 +73,8 @@ public class PaintTerrainToolAction extends AbstractTerrainToolAction {
         Terrain terrain = getTerrain(rootNode.getLookup().lookup(Node.class));
         if (terrain == null)
             return null;
+        Node terrainNode = getTerrainNode(rootNode.getLookup().lookup(Node.class));
+        worldLoc.subtractLocal(terrainNode.getWorldTranslation());
         paintTexture(terrain, worldLoc, radius, weight, selectedTextureIndex);
         return terrain;
     }
@@ -95,7 +97,7 @@ public class PaintTerrainToolAction extends AbstractTerrainToolAction {
         Vector2f UV = getPointPercentagePosition(terrain, markerLocation);
 
         // get the radius of the brush in pixel-percent
-        float brushSize = toolRadius/(terrain.getTerrainSize()*((Node)terrain).getLocalScale().x);
+        float brushSize = toolRadius/(terrain.getTerrainSize()*((Node)terrain).getWorldScale().x);
         int texIndex = selectedTextureIndex - ((selectedTextureIndex/4)*4); // selectedTextureIndex/4 is an int floor, do not simplify the equation
         boolean erase = toolWeight<0;
         if (erase)
@@ -108,9 +110,10 @@ public class PaintTerrainToolAction extends AbstractTerrainToolAction {
     
     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).getWorldTranslation().x*scale, ((Node)terrain).getWorldTranslation().z*scale); // center it on 0,0
+        float scale = ((Node)terrain).getWorldScale().x;
+
+        // already centered on Terrain's node origin (0,0)
+        //uv.subtractLocal(((Node)terrain).getWorldTranslation().x*scale, ((Node)terrain).getWorldTranslation().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

+ 2 - 0
jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/RaiseTerrainToolAction.java

@@ -70,6 +70,8 @@ public class RaiseTerrainToolAction extends AbstractTerrainToolAction {
         Terrain terrain = getTerrain(rootNode.getLookup().lookup(Node.class));
         if (terrain == null)
             return null;
+        Node terrainNode = getTerrainNode(rootNode.getLookup().lookup(Node.class));
+        worldLoc.subtractLocal(terrainNode.getWorldTranslation());
         modifyHeight(terrain, worldLoc, radius, height, mesh);
         return terrain;
     }

+ 2 - 0
jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/RoughTerrainToolAction.java

@@ -77,6 +77,8 @@ public class RoughTerrainToolAction extends AbstractTerrainToolAction {
         Terrain terrain = getTerrain(rootNode.getLookup().lookup(Node.class));
         if (terrain == null)
             return null;
+        Node terrainNode = getTerrainNode(rootNode.getLookup().lookup(Node.class));
+        worldLoc.subtractLocal(terrainNode.getWorldTranslation());
         roughen(terrain, radius, weight, params);
         return terrain;
     }

+ 4 - 1
jme3-terrain-editor/src/com/jme3/gde/terraineditor/tools/SlopeTerrainToolAction.java

@@ -75,7 +75,10 @@ public class SlopeTerrainToolAction extends AbstractTerrainToolAction {
         Terrain terrain = getTerrain(rootNode.getLookup().lookup(Node.class));
         if (terrain == null)
             return null;
-
+        Node terrainNode = getTerrainNode(rootNode.getLookup().lookup(Node.class));
+        point1.subtractLocal(terrainNode.getWorldTranslation());
+        point2.subtractLocal(terrainNode.getWorldTranslation());
+        current.subtractLocal(terrainNode.getWorldTranslation());
         modifyHeight(terrain, point1, point2, current, radius, weight, precise, lock, mesh);
 
         return terrain;