Java Code Examples for com.jme3.terrain.Terrain#adjustHeight()

The following examples show how to use com.jme3.terrain.Terrain#adjustHeight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LevelTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void resetHeight(Terrain terrain, List<Vector2f> undoLocs, List<Float> undoHeights) {
    List<Float> neg = new ArrayList<Float>();
    for (Float f : undoHeights)
        neg.add( f * -1f );
    
    terrain.adjustHeight(undoLocs, neg);
    ((Node)terrain).updateModelBound();
}
 
Example 2
Source File: SmoothTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void resetHeight(Terrain terrain, List<Vector2f> undoLocs, List<Float> undoHeights) {
    List<Float> neg = new ArrayList<Float>();
    for (Float f : undoHeights)
        neg.add( f * -1f );
    
    terrain.adjustHeight(undoLocs, neg);
    ((Node)terrain).updateModelBound();
}
 
Example 3
Source File: RaiseTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void modifyHeight(Terrain terrain, float radius, float heightDir) {

        int radiusStepsX = (int) (radius / ((Node)terrain).getWorldScale().x);
        int radiusStepsZ = (int) (radius / ((Node)terrain).getWorldScale().z);

        float xStepAmount = ((Node)terrain).getWorldScale().x;
        float zStepAmount = ((Node)terrain).getWorldScale().z;

        List<Vector2f> locs = new ArrayList<Vector2f>();
        List<Float> heights = new ArrayList<Float>();
        
        for (int z=-radiusStepsZ; z<radiusStepsZ; z++) {
            for (int x=-radiusStepsZ; x<radiusStepsX; x++) {

                float locX = worldLoc.x + (x*xStepAmount);
                float locZ = worldLoc.z + (z*zStepAmount);

                // see if it is in the radius of the tool
                if (ToolUtils.isInRadius(locX-worldLoc.x,locZ-worldLoc.z,radius)) {
                    // adjust height based on radius of the tool
                    float h = ToolUtils.calculateHeight(radius, heightDir, locX-worldLoc.x, locZ-worldLoc.z);
                    // increase the height
                    locs.add(new Vector2f(locX, locZ));
                    heights.add(h);
                }
            }
        }

        // do the actual height adjustment
        terrain.adjustHeight(locs, heights);

        ((Node)terrain).updateModelBound(); // or else we won't collide with it where we just edited
    }
 
Example 4
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Perform the actual height modification on the terrain.
 * @param worldLoc the location in the world where the tool was activated
 * @param radius of the tool, terrain in this radius will be affected
 * @param heightFactor the amount to adjust the height by
 */
public void doModifyTerrainHeight(Vector3f worldLoc, float radius, float heightFactor) {

    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return;

    setNeedsSave(true);

    int radiusStepsX = (int) (radius / ((Node)terrain).getLocalScale().x);
    int radiusStepsZ = (int) (radius / ((Node)terrain).getLocalScale().z);

    float xStepAmount = ((Node)terrain).getLocalScale().x;
    float zStepAmount = ((Node)terrain).getLocalScale().z;

    List<Vector2f> locs = new ArrayList<Vector2f>();
    List<Float> heights = new ArrayList<Float>();

    for (int z=-radiusStepsZ; z<radiusStepsZ; z++) {
        for (int x=-radiusStepsZ; x<radiusStepsX; x++) {

            float locX = worldLoc.x + (x*xStepAmount);
            float locZ = worldLoc.z + (z*zStepAmount);

            // see if it is in the radius of the tool
            if (isInRadius(locX-worldLoc.x,locZ-worldLoc.z,radius)) {
                // adjust height based on radius of the tool
                float h = calculateHeight(radius, heightFactor, locX-worldLoc.x, locZ-worldLoc.z);
                // increase the height
                locs.add(new Vector2f(locX, locZ));
                heights.add(h);
            }
        }
    }

    // do the actual height adjustment
    terrain.adjustHeight(locs, heights);

    ((Node)terrain).updateModelBound(); // or else we won't collide with it where we just edited
    
}
 
Example 5
Source File: LevelTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void modifyHeight(Terrain terrain, float radius, float height) {
    if (levelTerrainLocation == null)
        return;

    float desiredHeight = levelTerrainLocation.y;

    int radiusStepsX = (int)(radius / ((Node)terrain).getLocalScale().x);
    int radiusStepsZ = (int)(radius / ((Node)terrain).getLocalScale().z);

    float xStepAmount = ((Node)terrain).getLocalScale().x;
    float zStepAmount = ((Node)terrain).getLocalScale().z;

    List<Vector2f> locs = new ArrayList<Vector2f>();
    List<Float> heights = new ArrayList<Float>();

    for (int z=-radiusStepsZ; z<radiusStepsZ; z++) {
        for (int x=-radiusStepsZ; x<radiusStepsX; x++) {

            float locX = worldLoc.x + (x*xStepAmount);
            float locZ = worldLoc.z + (z*zStepAmount);
            
            // see if it is in the radius of the tool
            if (ToolUtils.isInRadius(locX-worldLoc.x,locZ-worldLoc.z,radius)) {

                Vector2f terrainLoc = new Vector2f(locX, locZ);
                // adjust height based on radius of the tool
                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
                
                float adj = 0;
                if (terrainHeightAtLoc < desiredHeight)
                    adj = 1;
                else if (terrainHeightAtLoc > desiredHeight)
                    adj = -1;
                        
                adj *= radiusWeight * height;

                // test if adjusting too far and then cap it
                if (adj > 0 && ToolUtils.floatGreaterThan((terrainHeightAtLoc + adj), desiredHeight, epsilon))
                    adj = desiredHeight - terrainHeightAtLoc;
                else if (adj < 0 && ToolUtils.floatLessThan((terrainHeightAtLoc + adj), desiredHeight, epsilon))
                    adj = terrainHeightAtLoc - desiredHeight;
  
                if (!ToolUtils.floatEquals(adj, 0, 0.001f)) {
                    locs.add(terrainLoc);
                    heights.add(adj);
                }
                
            }
        }
    }
    undoLocs = locs;
    undoHeights = heights;
    
    // do the actual height adjustment
    terrain.adjustHeight(locs, heights);
    
    ((Node)terrain).updateModelBound(); // or else we won't collide with it where we just edited

}
 
Example 6
Source File: SmoothTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void modifyHeight(Terrain terrain, float radius, float height) {
    
    int radiusStepsX = (int)(radius / ((Node)terrain).getLocalScale().x);
    int radiusStepsZ = (int)(radius / ((Node)terrain).getLocalScale().z);

    float xStepAmount = ((Node)terrain).getLocalScale().x;
    float zStepAmount = ((Node)terrain).getLocalScale().z;

    List<Vector2f> locs = new ArrayList<Vector2f>();
    List<Float> heights = new ArrayList<Float>();

    for (int z=-radiusStepsZ; z<radiusStepsZ; z++) {
        for (int x=-radiusStepsZ; x<radiusStepsX; x++) {

            float locX = worldLoc.x + (x*xStepAmount);
            float locZ = worldLoc.z + (z*zStepAmount);

            // see if it is in the radius of the tool
            if (ToolUtils.isInRadius(locX-worldLoc.x,locZ-worldLoc.z,radius)) {

                Vector2f terrainLoc = new Vector2f(locX, locZ);
                // adjust height based on radius of the tool
                float center = terrain.getHeightmapHeight(terrainLoc);
                float left = terrain.getHeightmapHeight(new Vector2f(terrainLoc.x-1, terrainLoc.y));
                float right = terrain.getHeightmapHeight(new Vector2f(terrainLoc.x+1, terrainLoc.y));
                float up = terrain.getHeightmapHeight(new Vector2f(terrainLoc.x, terrainLoc.y+1));
                float down = terrain.getHeightmapHeight(new Vector2f(terrainLoc.x, terrainLoc.y-1));
                int count = 1;
                float amount = center;
                if (left != Float.NaN) {
                    amount += left;
                    count++;
                }
                if (right != Float.NaN) {
                    amount += right;
                    count++;
                }
                if (up != Float.NaN) {
                    amount += up;
                    count++;
                }
                if (down != Float.NaN) {
                    amount += down;
                    count++;
                }

                amount /= count; // take average

                // weigh it
                float diff = amount-center;
                diff *= height;
                    
                locs.add(terrainLoc);
                heights.add(diff);
            }
        }
    }
    
    undoLocs = locs;
    undoHeights = heights;
    
    // do the actual height adjustment
    terrain.adjustHeight(locs, heights);

    ((Node)terrain).updateModelBound(); // or else we won't collide with it where we just edited
}