Search in sources :

Example 11 with TerrainQuad

use of com.jme3.terrain.geomipmap.TerrainQuad 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 12 with TerrainQuad

use of com.jme3.terrain.geomipmap.TerrainQuad in project jmonkeyengine by jMonkeyEngine.

the class TerrainGridSerializationTest method simpleInitApp.

@Override
public void simpleInitApp() {
    File file = new File("TerrainGridTestData.zip");
    if (!file.exists()) {
        assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/TerrainGridTestData.zip", HttpZipLocator.class);
    } else {
        assetManager.registerLocator("TerrainGridTestData.zip", ZipLocator.class);
    }
    this.flyCam.setMoveSpeed(100f);
    ScreenshotAppState state = new ScreenshotAppState();
    this.stateManager.attach(state);
    this.terrain = (TerrainGrid) assetManager.loadModel("TerrainGrid/TerrainGrid.j3o");
    this.rootNode.attachChild(this.terrain);
    TerrainLodControl control = new TerrainGridLodControl(this.terrain, getCamera());
    // patch size, and a multiplier
    control.setLodCalculator(new DistanceLodCalculator(65, 2.7f));
    this.terrain.addControl(control);
    final BulletAppState bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    this.getCamera().setLocation(new Vector3f(0, 256, 0));
    this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
    if (usePhysics) {
        CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(0.5f, 1.8f, 1);
        player3 = new CharacterControl(capsuleShape, 0.5f);
        player3.setJumpSpeed(20);
        player3.setFallSpeed(10);
        player3.setGravity(10);
        player3.setPhysicsLocation(new Vector3f(cam.getLocation().x, 256, cam.getLocation().z));
        bulletAppState.getPhysicsSpace().add(player3);
        terrain.addListener(new TerrainGridListener() {

            public void gridMoved(Vector3f newCenter) {
            }

            public void tileAttached(Vector3f cell, TerrainQuad quad) {
                //workaround for bugged test j3o's
                while (quad.getControl(RigidBodyControl.class) != null) {
                    quad.removeControl(RigidBodyControl.class);
                }
                quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0));
                bulletAppState.getPhysicsSpace().add(quad);
            }

            public void tileDetached(Vector3f cell, TerrainQuad quad) {
                if (quad.getControl(RigidBodyControl.class) != null) {
                    bulletAppState.getPhysicsSpace().remove(quad);
                    quad.removeControl(RigidBodyControl.class);
                }
            }
        });
    }
    this.initKeys();
}
Also used : CharacterControl(com.jme3.bullet.control.CharacterControl) TerrainGridListener(com.jme3.terrain.geomipmap.TerrainGridListener) CapsuleCollisionShape(com.jme3.bullet.collision.shapes.CapsuleCollisionShape) DistanceLodCalculator(com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl) TerrainGridLodControl(com.jme3.terrain.geomipmap.TerrainGridLodControl) ColorRGBA(com.jme3.math.ColorRGBA) BulletAppState(com.jme3.bullet.BulletAppState) Vector3f(com.jme3.math.Vector3f) HeightfieldCollisionShape(com.jme3.bullet.collision.shapes.HeightfieldCollisionShape) TerrainLodControl(com.jme3.terrain.geomipmap.TerrainLodControl) ScreenshotAppState(com.jme3.app.state.ScreenshotAppState) File(java.io.File) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad)

Example 13 with TerrainQuad

use of com.jme3.terrain.geomipmap.TerrainQuad 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 14 with TerrainQuad

use of com.jme3.terrain.geomipmap.TerrainQuad 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 15 with TerrainQuad

use of com.jme3.terrain.geomipmap.TerrainQuad in project jmonkeyengine by jMonkeyEngine.

the class CollisionShapeFactory method createCompoundShape.

private static CompoundCollisionShape createCompoundShape(Node realRootNode, Node rootNode, CompoundCollisionShape shape, boolean meshAccurate, boolean dynamic) {
    for (Spatial spatial : rootNode.getChildren()) {
        if (spatial instanceof TerrainQuad) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            TerrainQuad terrain = (TerrainQuad) spatial;
            Transform trans = getTransform(spatial, realRootNode);
            shape.addChildShape(new HeightfieldCollisionShape(terrain.getHeightMap(), trans.getScale()), trans.getTranslation(), trans.getRotation().toRotationMatrix());
        } else if (spatial instanceof Node) {
            createCompoundShape(realRootNode, (Node) spatial, shape, meshAccurate, dynamic);
        } else if (spatial instanceof TerrainPatch) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            TerrainPatch terrain = (TerrainPatch) spatial;
            Transform trans = getTransform(spatial, realRootNode);
            shape.addChildShape(new HeightfieldCollisionShape(terrain.getHeightMap(), terrain.getLocalScale()), trans.getTranslation(), trans.getRotation().toRotationMatrix());
        } else if (spatial instanceof Geometry) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            if (meshAccurate) {
                CollisionShape childShape = dynamic ? createSingleDynamicMeshShape((Geometry) spatial, realRootNode) : createSingleMeshShape((Geometry) spatial, realRootNode);
                if (childShape != null) {
                    Transform trans = getTransform(spatial, realRootNode);
                    shape.addChildShape(childShape, trans.getTranslation(), trans.getRotation().toRotationMatrix());
                }
            } else {
                Transform trans = getTransform(spatial, realRootNode);
                shape.addChildShape(createSingleBoxShape(spatial, realRootNode), trans.getTranslation(), trans.getRotation().toRotationMatrix());
            }
        }
    }
    return shape;
}
Also used : ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) Transform(com.jme3.math.Transform) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad) TerrainPatch(com.jme3.terrain.geomipmap.TerrainPatch)

Aggregations

TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)24 Material (com.jme3.material.Material)18 Texture (com.jme3.texture.Texture)18 Vector3f (com.jme3.math.Vector3f)16 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)16 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)16 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)15 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)12 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)8 ArrayList (java.util.ArrayList)8 DirectionalLight (com.jme3.light.DirectionalLight)7 Camera (com.jme3.renderer.Camera)7 BulletAppState (com.jme3.bullet.BulletAppState)6 CapsuleCollisionShape (com.jme3.bullet.collision.shapes.CapsuleCollisionShape)5 CharacterControl (com.jme3.bullet.control.CharacterControl)5 ColorRGBA (com.jme3.math.ColorRGBA)5 ScreenshotAppState (com.jme3.app.state.ScreenshotAppState)4 BoundingBox (com.jme3.bounding.BoundingBox)4 HeightfieldCollisionShape (com.jme3.bullet.collision.shapes.HeightfieldCollisionShape)4 Spatial (com.jme3.scene.Spatial)4