Search in sources :

Example 31 with Terrain

use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.

the class TestMultiPostWater method createTerrain.

private void createTerrain(Node rootNode) {
    matRock = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
    matRock.setBoolean("useTriPlanarMapping", false);
    matRock.setBoolean("WardIso", true);
    matRock.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
    Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/pools.png");
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    matRock.setTexture("DiffuseMap", grass);
    matRock.setFloat("DiffuseMap_0_scale", 64);
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    matRock.setTexture("DiffuseMap_1", dirt);
    matRock.setFloat("DiffuseMap_1_scale", 16);
    Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
    rock.setWrap(WrapMode.Repeat);
    matRock.setTexture("DiffuseMap_2", rock);
    matRock.setFloat("DiffuseMap_2_scale", 128);
    Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
    normalMap0.setWrap(WrapMode.Repeat);
    Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
    normalMap1.setWrap(WrapMode.Repeat);
    Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
    normalMap2.setWrap(WrapMode.Repeat);
    matRock.setTexture("NormalMap", normalMap0);
    matRock.setTexture("NormalMap_1", normalMap2);
    matRock.setTexture("NormalMap_2", normalMap2);
    AbstractHeightMap heightmap = null;
    try {
        heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.25f);
        heightmap.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
    terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
    List<Camera> cameras = new ArrayList<Camera>();
    cameras.add(getCamera());
    terrain.setMaterial(matRock);
    terrain.setLocalScale(new Vector3f(5, 5, 5));
    terrain.setLocalTranslation(new Vector3f(0, -30, 0));
    // unlock it so we can edit the height
    terrain.setLocked(false);
    terrain.setShadowMode(ShadowMode.Receive);
    rootNode.attachChild(terrain);
}
Also used : AbstractHeightMap(com.jme3.terrain.heightmap.AbstractHeightMap) Vector3f(com.jme3.math.Vector3f) ArrayList(java.util.ArrayList) Material(com.jme3.material.Material) Camera(com.jme3.renderer.Camera) Texture(com.jme3.texture.Texture) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad) ImageBasedHeightMap(com.jme3.terrain.heightmap.ImageBasedHeightMap)

Example 32 with Terrain

use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.

the class TerrainTestModifyHeight method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    Vector3f intersection = getWorldIntersection();
    updateHintText(intersection);
    if (raiseTerrain) {
        if (intersection != null) {
            adjustHeight(intersection, 64, tpf * 60);
        }
    } else if (lowerTerrain) {
        if (intersection != null) {
            adjustHeight(intersection, 64, -tpf * 60);
        }
    }
    if (terrain != null && intersection != null) {
        float h = terrain.getHeight(new Vector2f(intersection.x, intersection.z));
        Vector3f tl = terrain.getWorldTranslation();
        marker.setLocalTranslation(tl.add(new Vector3f(intersection.x, h, intersection.z)));
        markerNormal.setLocalTranslation(tl.add(new Vector3f(intersection.x, h, intersection.z)));
        Vector3f normal = terrain.getNormal(new Vector2f(intersection.x, intersection.z));
        ((Arrow) markerNormal.getMesh()).setArrowExtent(normal);
    }
}
Also used : Arrow(com.jme3.scene.debug.Arrow) Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f)

Example 33 with Terrain

use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.

the class TerrainTestReadWrite method loadTerrain.

private void loadTerrain() {
    FileInputStream fis = null;
    try {
        long start = System.currentTimeMillis();
        // remove the existing terrain and detach it from the root node.
        if (terrain != null) {
            Node existingTerrain = (Node) terrain;
            existingTerrain.removeFromParent();
            existingTerrain.removeControl(TerrainLodControl.class);
            existingTerrain.detachAllChildren();
            terrain = null;
        }
        // import the saved terrain, and attach it back to the root node
        File f = new File("terrainsave.jme");
        fis = new FileInputStream(f);
        BinaryImporter imp = BinaryImporter.getInstance();
        imp.setAssetManager(assetManager);
        terrain = (TerrainQuad) imp.load(new BufferedInputStream(fis));
        rootNode.attachChild((Node) terrain);
        float duration = (System.currentTimeMillis() - start) / 1000.0f;
        System.out.println("Load took " + duration + " seconds");
        // 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) {
        Logger.getLogger(TerrainTestReadWrite.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(TerrainTestReadWrite.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
Also used : BinaryImporter(com.jme3.export.binary.BinaryImporter) Node(com.jme3.scene.Node) TerrainLodControl(com.jme3.terrain.geomipmap.TerrainLodControl)

Example 34 with Terrain

use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.

the class TerrainTestReadWrite method createMap.

private void createMap() {
    matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
    matTerrain.setBoolean("useTriPlanarMapping", false);
    matTerrain.setBoolean("WardIso", true);
    // ALPHA map (for splat textures)
    matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
    // HEIGHTMAP image (for the terrain heightmap)
    Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
    // GRASS texture
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap", grass);
    matTerrain.setFloat("DiffuseMap_0_scale", grassScale);
    // DIRT texture
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_1", dirt);
    matTerrain.setFloat("DiffuseMap_1_scale", dirtScale);
    // ROCK texture
    Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
    rock.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_2", rock);
    matTerrain.setFloat("DiffuseMap_2_scale", rockScale);
    Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
    normalMap0.setWrap(WrapMode.Repeat);
    Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
    normalMap1.setWrap(WrapMode.Repeat);
    Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
    normalMap2.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("NormalMap", normalMap0);
    matTerrain.setTexture("NormalMap_1", normalMap2);
    matTerrain.setTexture("NormalMap_2", normalMap2);
    matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matWire.getAdditionalRenderState().setWireframe(true);
    matWire.setColor("Color", ColorRGBA.Green);
    // CREATE HEIGHTMAP
    AbstractHeightMap heightmap = null;
    try {
        heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 1f);
        heightmap.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (new File("terrainsave.jme").exists()) {
        loadTerrain();
    } else {
        // create the terrain as normal, and give it a control for LOD management
        //, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
        TerrainQuad terrainQuad = new TerrainQuad("terrain", 65, 129, heightmap.getHeightMap());
        TerrainLodControl control = new TerrainLodControl(terrainQuad, getCamera());
        // patch size, and a multiplier
        control.setLodCalculator(new DistanceLodCalculator(65, 2.7f));
        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();
    light.setDirection((new Vector3f(-0.5f, -1f, -0.5f)).normalize());
    rootNode.addLight(light);
}
Also used : AbstractHeightMap(com.jme3.terrain.heightmap.AbstractHeightMap) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) TerrainLodControl(com.jme3.terrain.geomipmap.TerrainLodControl) Material(com.jme3.material.Material) Texture(com.jme3.texture.Texture) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad) DistanceLodCalculator(com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator) ImageBasedHeightMap(com.jme3.terrain.heightmap.ImageBasedHeightMap)

Example 35 with Terrain

use of com.jme3.terrain.Terrain in project jmonkeyengine by jMonkeyEngine.

the class TestAbsoluteLocators method main.

public static void main(String[] args) {
    AssetManager am = JmeSystem.newAssetManager();
    am.registerLoader(AWTLoader.class, "jpg");
    am.registerLoader(WAVLoader.class, "wav");
    // register absolute locator
    am.registerLocator("/", ClasspathLocator.class);
    // find a sound
    AudioData audio = am.loadAudio("Sound/Effects/Gun.wav");
    // find a texture
    Texture tex = am.loadTexture("Textures/Terrain/Pond/Pond.jpg");
    if (audio == null)
        throw new RuntimeException("Cannot find audio!");
    else
        System.out.println("Audio loaded from Sounds/Effects/Gun.wav");
    if (tex == null)
        throw new RuntimeException("Cannot find texture!");
    else
        System.out.println("Texture loaded from Textures/Terrain/Pond/Pond.jpg");
    System.out.println("Success!");
}
Also used : AudioData(com.jme3.audio.AudioData) AssetManager(com.jme3.asset.AssetManager) Texture(com.jme3.texture.Texture)

Aggregations

Material (com.jme3.material.Material)54 Vector3f (com.jme3.math.Vector3f)43 Texture (com.jme3.texture.Texture)38 Geometry (com.jme3.scene.Geometry)32 DirectionalLight (com.jme3.light.DirectionalLight)23 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)21 Box (com.jme3.scene.shape.Box)20 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)17 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)15 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)15 AmbientLight (com.jme3.light.AmbientLight)14 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)13 Node (com.jme3.scene.Node)12 Sphere (com.jme3.scene.shape.Sphere)11 Vector2f (com.jme3.math.Vector2f)10 Spatial (com.jme3.scene.Spatial)10 ArrayList (java.util.ArrayList)10 TextureKey (com.jme3.asset.TextureKey)9 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)9 ColorRGBA (com.jme3.math.ColorRGBA)8