com.jme3.terrain.Terrain Java Examples

The following examples show how to use com.jme3.terrain.Terrain. 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: TextureLayerSettings.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Get a diffuse normal of the level.
 *
 * @param layer the layer.
 * @return the normal texture or null.
 */
@FromAnyThread
public @Nullable Texture getNormal(final int layer) {

    final Function<Integer, String> layerToNormalName = getLayerToNormalName();
    if (layerToNormalName == null) {
        return null;
    }

    final Terrain terrain = getTerrain();
    final Material material = terrain.getMaterial();
    final MatParam matParam = material.getParam(layerToNormalName.apply(layer));

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }

    return (Texture) matParam.getValue();
}
 
Example #2
Source File: ChangeHeightTerrainToolControl.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Notify about changing height by the point in the terrain.
 *
 * @param terrain the terrain.
 * @param point   the point.
 */
@JmeThread
protected void change(@NotNull final Terrain terrain, @NotNull final Vector2f point) {

    final Node terrainNode = (Node) terrain;
    final Vector3f scale = terrainNode.getWorldScale();

    final int halfSize = terrain.getTerrainSize() / 2;
    final int x = Math.round((point.x / scale.x) + halfSize);
    final int z = Math.round((point.y / scale.z) + halfSize);

    final HeightPoint heightPoint = new HeightPoint(point.getX(), point.getY(), x, z);

    final ObjectDictionary<Terrain, ObjectDictionary<HeightPoint, Float>> originalHeight = getOriginalHeight();
    final ObjectDictionary<HeightPoint, Float> terrainHeights = originalHeight.get(terrain, DICTIONARY_FACTORY);
    if (terrainHeights.containsKey(heightPoint)) {
        return;
    }

    final float height = terrain.getHeightmapHeight(point);
    terrainHeights.put(heightPoint, height);
}
 
Example #3
Source File: ChangeHeightTerrainToolControl.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Start making changes.
 */
@JmeThread
protected void startChange() {

    final ObjectDictionary<Terrain, ObjectDictionary<HeightPoint, Float>> originalHeight = getOriginalHeight();
    originalHeight.forEach(Dictionary::clear);
    originalHeight.clear();

    final Array<Terrain> terrains = getTerrains();
    terrains.clear();

    NodeUtils.visitSpatial(notNull(getPaintedModel()), spatial -> {
        if (spatial instanceof Terrain) {
            terrains.add((Terrain) spatial);
            return false;
        }
        return true;
    });

    final ObjectDictionary<Terrain, Spatial> copiedTerrains = getCopiedTerrains();
    copiedTerrains.clear();

    terrains.forEach(copiedTerrains, (terrain, toStore) ->
            toStore.put(terrain, ((Spatial) terrain).clone()));
}
 
Example #4
Source File: PaintTerrainToolControl.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@JmeThread
private @NotNull Vector2f getPointPercentagePosition(@NotNull final Terrain terrain,
                                                     @NotNull final Vector3f localPoint,
                                                     @NotNull final Vector3f localScale,
                                                     @NotNull final Vector2f result) {
    result.set(localPoint.x, -localPoint.z);

    float scale = localScale.getX();

    // already centered on Terrain's node origin (0,0)
    float scaledSize = terrain.getTerrainSize() * scale;
    result.addLocal(scaledSize / 2, scaledSize / 2); // shift the bottom left corner up to 0,0
    result.divideLocal(scaledSize); // get the location as a percentage

    return result;
}
 
Example #5
Source File: AbstractTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected Terrain getTerrain(Spatial root) {

        // is this the terrain?
        if (root instanceof Terrain && root instanceof Node) {
            return (Terrain)root;
        }

        if (root instanceof Node) {
            Node n = (Node) root;
            for (Spatial c : n.getChildren()) {
                if (c instanceof Node){
                    Terrain res = getTerrain(c);
                    if (res != null)
                        return res;
                }
            }
        }

        return null;
    }
 
Example #6
Source File: PaintTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void paintTexture(Terrain terrain, Vector3f markerLocation, float toolRadius, float toolWeight, int selectedTextureIndex) {
    if (selectedTextureIndex < 0 || markerLocation == null)
        return;
    
    int alphaIdx = selectedTextureIndex/4; // 4 = rgba = 4 textures
    Texture tex = getAlphaTexture(terrain, alphaIdx);
    Image image = tex.getImage();

    Vector2f UV = getPointPercentagePosition(terrain, markerLocation);

    // get the radius of the brush in pixel-percent
    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)
        toolWeight *= -1;

    doPaintAction(texIndex, image, UV, true, brushSize, erase, toolWeight);

    tex.getImage().setUpdateNeeded();
}
 
Example #7
Source File: PaintTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Texture getAlphaTexture(Terrain terrain, int alphaLayer) {
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (alphaLayer == 0)
        matParam = terrain.getMaterial().getParam("AlphaMap");
    else if(alphaLayer == 1)
        matParam = terrain.getMaterial().getParam("AlphaMap_1");
    else if(alphaLayer == 2)
        matParam = terrain.getMaterial().getParam("AlphaMap_2");
    
    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example #8
Source File: TextureLayerSettings.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Get a diffuse texture of the level.
 *
 * @param layer the layer.
 * @return the diffuse texture or null.
 */
@FromAnyThread
public @Nullable Texture getDiffuse(final int layer) {

    final Function<Integer, String> layerToDiffuseName = getLayerToDiffuseName();
    if (layerToDiffuseName == null) {
        return null;
    }

    final Terrain terrain = getTerrain();
    final Material material = terrain.getMaterial();
    final MatParam matParam = material.getParam(layerToDiffuseName.apply(layer));

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }

    return (Texture) matParam.getValue();
}
 
Example #9
Source File: TerrainUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected static Node findTerrain(Spatial root) {
   
    // is this the terrain?
    if (root instanceof Terrain && root instanceof Node) {
        return (Node)root;
    }

    if (root instanceof Node) {
        Node n = (Node) root;
        for (Spatial c : n.getChildren()) {
            if (c instanceof Node){
                Node res = findTerrain(c);
                if (res != null)
                    return res;
            }
        }
    }

    return null;
}
 
Example #10
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private int doGetNumUsedTextures() {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return 0;

    int count = 0;

    for (int i=0; i<MAX_TEXTURES; i++) {
        Texture tex = doGetDiffuseTexture(i);
        if (tex != null)
            count++;
        tex = doGetNormalMap(i);
        if (tex != null)
            count++;
    }
    return count;
}
 
Example #11
Source File: TextureLayerSettings.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Get a alpha texture of the level.
 *
 * @param layer the layer.
 * @return the alpha texture or null.
 */
@FromAnyThread
public @Nullable Texture getAlpha(final int layer) {

    final Function<Integer, String> layerToAlphaName = getLayerToAlphaName();
    if (layerToAlphaName == null) {
        return null;
    }

    final Terrain terrain = getTerrain();
    final Material material = terrain.getMaterial();
    final MatParam matParam = material.getParam(layerToAlphaName.apply(layer));

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }

    return (Texture) matParam.getValue();
}
 
Example #12
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void doSetNormalMap(int layer, Texture tex) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return;
    if (tex == null) {
        // remove the texture if it is null
        if (layer == 0)
            terrain.getMaterial().clearParam("NormalMap");
        else
            terrain.getMaterial().clearParam("NormalMap_"+layer);
        return;
    }

    tex.setWrap(WrapMode.Repeat);
    
    if (layer == 0)
        terrain.getMaterial().setTexture("NormalMap", tex);
    else
        terrain.getMaterial().setTexture("NormalMap_"+layer, tex);

    setNeedsSave(true);
}
 
Example #13
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void doSetNormalMap(int layer, String texturePath) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return;

    if (texturePath == null) {
        // remove the texture if it is null
        if (layer == 0)
            terrain.getMaterial().clearParam("NormalMap");
        else
            terrain.getMaterial().clearParam("NormalMap_"+layer);
    } else {
        Texture tex = SceneApplication.getApplication().getAssetManager().loadTexture(texturePath);
        tex.setWrap(WrapMode.Repeat);

        if (layer == 0)
            terrain.getMaterial().setTexture("NormalMap", tex);
        else
            terrain.getMaterial().setTexture("NormalMap_"+layer, tex);
    }
    enableTextureButtons();
    setNeedsSave(true);
}
 
Example #14
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get the normal map texture at the specified layer.
 * Run this on the GL thread!
 */
private Texture doGetNormalMap(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (layer == 0)
        matParam = terrain.getMaterial().getParam("NormalMap");
    else
        matParam = terrain.getMaterial().getParam("NormalMap_"+layer);

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example #15
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Texture doGetAlphaTexture(Terrain terrain, int alphaLayer) {
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (alphaLayer == 0)
        matParam = terrain.getMaterial().getParam("AlphaMap");
    else if(alphaLayer == 1)
        matParam = terrain.getMaterial().getParam("AlphaMap_1");
    else if(alphaLayer == 2)
        matParam = terrain.getMaterial().getParam("AlphaMap_2");
    
    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example #16
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get the diffuse texture at the specified layer.
 * Run this on the GL thread!
 */
private Texture doGetDiffuseTexture(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (layer == 0)
        matParam = terrain.getMaterial().getParam("DiffuseMap");
    else
        matParam = terrain.getMaterial().getParam("DiffuseMap_"+layer);

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();

    return tex;
}
 
Example #17
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public JmeNode findJmeTerrain(JmeNode root) {
    if (root == null)
        root = (JmeNode) jmeRootNode;
    
    Node node = root.getLookup().lookup(Node.class);
    if (node != null && node instanceof Terrain && node instanceof Node) {
        return root;
    }
    
    if (node != null) {
        if (root.getChildren() != null) {
            for (org.openide.nodes.Node child : root.getChildren().getNodes() ) {
                if (child instanceof JmeNode) {
                    JmeNode res = findJmeTerrain((JmeNode)child);
                    if (res != null)
                        return res;
                }
            }
        }
    }
    
    return null;
}
 
Example #18
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 #19
Source File: LevelTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void doUndoTool(AbstractSceneExplorerNode rootNode, Object undoObject) {
    if (undoObject == null)
        return;
    if (undoLocs == null || undoHeights == null)
        return;
    resetHeight((Terrain)undoObject, undoLocs, undoHeights);
}
 
Example #20
Source File: PaintTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 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;
}
 
Example #21
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void doSetTextureScale(int layer, float scale) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return;
    terrain.getMaterial().setFloat("DiffuseMap_"+layer+"_scale", scale);
    setNeedsSave(true);
}
 
Example #22
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Float doGetTextureScale(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return 1f;
    MatParam matParam = null;
    matParam = terrain.getMaterial().getParam("DiffuseMap_"+layer+"_scale");
    return (Float) matParam.getValue();
}
 
Example #23
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void doGenerateEntropies(ProgressMonitor progressMonitor) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return;

    terrain.generateEntropy(progressMonitor);
}
 
Example #24
Source File: RaiseTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected Object doApplyTool(AbstractSceneExplorerNode rootNode) {
    Terrain terrain = getTerrain(rootNode.getLookup().lookup(Node.class));
    if (terrain == null)
        return null;
    modifyHeight(terrain, radius, height);
    return terrain;
}
 
Example #25
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void save() throws IOException {
    SceneApplication.getApplication().enqueue(new Callable() {

        public Object call() throws Exception {
            currentFileObject.saveAsset();
            //TerrainSaveCookie sc = currentFileObject.getCookie(TerrainSaveCookie.class);
            //if (sc != null) {
                Node root = rootNode.getLookup().lookup(Node.class);
                doSaveAlphaImages((Terrain)getTerrain(root));
            //}
            return null;
        }
    });
}
 
Example #26
Source File: TerrainGridTileLoaderTest.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onAction(final String name, final boolean keyPressed, final float tpf) {
    if (name.equals("Lefts")) {
        if (keyPressed) {
            TerrainGridTileLoaderTest.this.left = true;
        } else {
            TerrainGridTileLoaderTest.this.left = false;
        }
    } else if (name.equals("Rights")) {
        if (keyPressed) {
            TerrainGridTileLoaderTest.this.right = true;
        } else {
            TerrainGridTileLoaderTest.this.right = false;
        }
    } else if (name.equals("Ups")) {
        if (keyPressed) {
            TerrainGridTileLoaderTest.this.up = true;
        } else {
            TerrainGridTileLoaderTest.this.up = false;
        }
    } else if (name.equals("Downs")) {
        if (keyPressed) {
            TerrainGridTileLoaderTest.this.down = true;
        } else {
            TerrainGridTileLoaderTest.this.down = false;
        }
    } else if (name.equals("Jumps")) {
        TerrainGridTileLoaderTest.this.player3.jump();
    } else if (name.equals("pick") && keyPressed) {
        //Terrain picked = terrain.getTerrainAt(player3.getPhysicsLocation());
        Terrain picked = terrain.getTerrainAtCell(terrain.getCurrentCell());
        System.out.println("** cell "+player3.getPhysicsLocation()+" picked terrain: "+picked);
    }
}
 
Example #27
Source File: TerrainTestReadWrite.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onAction(String name, boolean pressed, float tpf) {
    if (name.equals("clone") && !pressed) {

        Terrain clone = (Terrain) ((Node)terrain).clone();
        ((Node)terrain).removeFromParent();
        terrain = clone;
        getRootNode().attachChild((Node)terrain);
    }
}
 
Example #28
Source File: TerrainLodControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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());
}
 
Example #29
Source File: TerrainLodControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void setSpatial(Spatial spatial) {
    super.setSpatial(spatial);
    if (spatial instanceof Terrain) {
        this.terrain = (Terrain) spatial;
    }
}
 
Example #30
Source File: TerrainEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void doRemoveNormalMap(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return;
    if (layer == 0)
        terrain.getMaterial().clearParam("NormalMap");
    else
        terrain.getMaterial().clearParam("NormalMap_"+layer);

    setNeedsSave(true);
}