Search in sources :

Example 66 with Terrain

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

the class TerrainTestModifyHeight method createTerrainGrid.

private void createTerrainGrid() {
    // TERRAIN TEXTURE material
    matTerrain = new Material(this.assetManager, "Common/MatDefs/Terrain/HeightBasedTerrain.j3md");
    // Parameters to material:
    // regionXColorMap: X = 1..4 the texture that should be appliad to state X
    // regionX: a Vector3f containing the following information:
    //      regionX.x: the start height of the region
    //      regionX.y: the end height of the region
    //      regionX.z: the texture scale for the region
    //  it might not be the most elegant way for storing these 3 values, but it packs the data nicely :)
    // slopeColorMap: the texture to be used for cliffs, and steep mountain sites
    // slopeTileFactor: the texture scale for slopes
    // terrainSize: the total size of the terrain (used for scaling the texture)
    // GRASS texture
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("region1ColorMap", grass);
    matTerrain.setVector3("region1", new Vector3f(88, 200, this.grassScale));
    // DIRT texture
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("region2ColorMap", dirt);
    matTerrain.setVector3("region2", new Vector3f(0, 90, this.dirtScale));
    // ROCK texture
    Texture rock = assetManager.loadTexture("Textures/Terrain/Rock2/rock.jpg");
    rock.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("region3ColorMap", rock);
    matTerrain.setVector3("region3", new Vector3f(198, 260, this.rockScale));
    matTerrain.setTexture("region4ColorMap", rock);
    matTerrain.setVector3("region4", new Vector3f(198, 260, this.rockScale));
    matTerrain.setTexture("slopeColorMap", rock);
    matTerrain.setFloat("slopeTileFactor", 32);
    matTerrain.setFloat("terrainSize", 513);
    FractalSum base = new FractalSum();
    base.setRoughness(0.7f);
    base.setFrequency(1.0f);
    base.setAmplitude(1.0f);
    base.setLacunarity(2.12f);
    base.setOctaves(8);
    base.setScale(0.02125f);
    base.addModulator(new NoiseModulator() {

        @Override
        public float value(float... in) {
            return ShaderUtils.clamp(in[0] * 0.5f + 0.5f, 0, 1);
        }
    });
    FilteredBasis ground = new FilteredBasis(base);
    PerturbFilter perturb = new PerturbFilter();
    perturb.setMagnitude(0.119f);
    OptimizedErode therm = new OptimizedErode();
    therm.setRadius(5);
    therm.setTalus(0.011f);
    SmoothFilter smooth = new SmoothFilter();
    smooth.setRadius(1);
    smooth.setEffect(0.7f);
    IterativeFilter iterate = new IterativeFilter();
    iterate.addPreFilter(perturb);
    iterate.addPostFilter(smooth);
    iterate.setFilter(therm);
    iterate.setIterations(1);
    ground.addPreFilter(iterate);
    this.terrain = new TerrainGrid("terrain", 65, 257, new FractalTileLoader(ground, 256f));
    terrain.setMaterial(matTerrain);
    terrain.setLocalTranslation(0, 0, 0);
    terrain.setLocalScale(2f, 1f, 2f);
    rootNode.attachChild(this.terrain);
    TerrainLodControl control = new TerrainLodControl(this.terrain, getCamera());
    this.terrain.addControl(control);
}
Also used : FractalTileLoader(com.jme3.terrain.geomipmap.grid.FractalTileLoader) PerturbFilter(com.jme3.terrain.noise.filter.PerturbFilter) FilteredBasis(com.jme3.terrain.noise.basis.FilteredBasis) TerrainGrid(com.jme3.terrain.geomipmap.TerrainGrid) IterativeFilter(com.jme3.terrain.noise.filter.IterativeFilter) Material(com.jme3.material.Material) Texture(com.jme3.texture.Texture) OptimizedErode(com.jme3.terrain.noise.filter.OptimizedErode) NoiseModulator(com.jme3.terrain.noise.modulator.NoiseModulator) Vector3f(com.jme3.math.Vector3f) TerrainLodControl(com.jme3.terrain.geomipmap.TerrainLodControl) SmoothFilter(com.jme3.terrain.noise.filter.SmoothFilter) FractalSum(com.jme3.terrain.noise.fractal.FractalSum)

Example 67 with Terrain

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

the class TerrainTestReadWrite method testHeightmapBuilding.

// no junit tests, so this has to be hand-tested:
private static void testHeightmapBuilding() {
    int s = 9;
    int b = 3;
    float[] hm = new float[s * s];
    for (int i = 0; i < s; i++) {
        for (int j = 0; j < s; j++) {
            hm[(i * s) + j] = i * j;
        }
    }
    for (int i = 0; i < s; i++) {
        for (int j = 0; j < s; j++) {
            System.out.print(hm[i * s + j] + " ");
        }
        System.out.println("");
    }
    TerrainQuad terrain = new TerrainQuad("terrain", b, s, hm);
    float[] hm2 = terrain.getHeightMap();
    boolean failed = false;
    for (int i = 0; i < s * s; i++) {
        if (hm[i] != hm2[i]) {
            failed = true;
        }
    }
    System.out.println("");
    if (failed) {
        System.out.println("Terrain heightmap building FAILED!!!");
        for (int i = 0; i < s; i++) {
            for (int j = 0; j < s; j++) {
                System.out.print(hm2[i * s + j] + " ");
            }
            System.out.println("");
        }
    } else {
        System.out.println("Terrain heightmap building PASSED");
    }
}
Also used : TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad)

Example 68 with Terrain

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

the class TestTransparentSSAO method simpleInitApp.

public void simpleInitApp() {
    renderManager.setAlphaToCoverage(true);
    cam.setLocation(new Vector3f(0.14914267f, 0.58147097f, 4.7686534f));
    cam.setRotation(new Quaternion(-0.0044764364f, 0.9767943f, 0.21314798f, 0.020512417f));
    //        cam.setLocation(new Vector3f(2.0606942f, 3.20342f, 6.7860126f));
    //        cam.setRotation(new Quaternion(-0.017481906f, 0.98241085f, -0.12393151f, -0.13857932f));
    viewPort.setBackgroundColor(ColorRGBA.DarkGray);
    Quad q = new Quad(20, 20);
    q.scaleTextureCoordinates(Vector2f.UNIT_XY.mult(5));
    Geometry geom = new Geometry("floor", q);
    Material mat = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
    geom.setMaterial(mat);
    geom.rotate(-FastMath.HALF_PI, 0, 0);
    geom.center();
    geom.setShadowMode(ShadowMode.Receive);
    TangentBinormalGenerator.generate(geom);
    rootNode.attachChild(geom);
    // create the geometry and attach it
    Spatial teaGeom = assetManager.loadModel("Models/Tree/Tree.mesh.j3o");
    teaGeom.setQueueBucket(Bucket.Transparent);
    teaGeom.setShadowMode(ShadowMode.Cast);
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(2));
    rootNode.addLight(al);
    DirectionalLight dl1 = new DirectionalLight();
    dl1.setDirection(new Vector3f(1, -1, 1).normalizeLocal());
    dl1.setColor(new ColorRGBA(0.965f, 0.949f, 0.772f, 1f).mult(0.7f));
    rootNode.addLight(dl1);
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
    dl.setColor(new ColorRGBA(0.965f, 0.949f, 0.772f, 1f).mult(0.7f));
    rootNode.addLight(dl);
    rootNode.attachChild(teaGeom);
    FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    //0.49997783f, 42.598858f, 35.999966f, 0.39299846f
    SSAOFilter ssao = new SSAOFilter();
    fpp.addFilter(ssao);
    SSAOUI ui = new SSAOUI(inputManager, ssao);
    viewPort.addProcessor(fpp);
}
Also used : Geometry(com.jme3.scene.Geometry) SSAOFilter(com.jme3.post.ssao.SSAOFilter) Quad(com.jme3.scene.shape.Quad) Spatial(com.jme3.scene.Spatial) DirectionalLight(com.jme3.light.DirectionalLight) Material(com.jme3.material.Material) FilterPostProcessor(com.jme3.post.FilterPostProcessor) AmbientLight(com.jme3.light.AmbientLight)

Example 69 with Terrain

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

the class TestInconsistentCompareDetection method simpleInitApp.

@Override
public void simpleInitApp() {
    cam.setLocation(new Vector3f(-11.674385f, 7.892636f, 33.133106f));
    cam.setRotation(new Quaternion(0.06426433f, 0.90940624f, -0.15329266f, 0.38125014f));
    Material m = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    m.setColor("Color", ColorRGBA.White);
    t1 = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg");
    t2 = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
    Box b = new Box(1, 1, 1);
    for (int x = 0; x < 12; x++) {
        for (int y = 0; y < 12; y++) {
            Geometry g = new Geometry("g_" + x + "_" + y, b);
            Node monkey = new Node("n_" + x + "_" + y);
            monkey.attachChild(g);
            monkey.move(x * 2, 0, y * 2);
            Material newMat = m.clone();
            g.setMaterial(newMat);
            if (FastMath.rand.nextBoolean()) {
                newMat.setTexture("ColorMap", t1);
            } else {
                newMat.setTexture("ColorMap", t2);
            }
            rootNode.attachChild(monkey);
        }
    }
    Thread evilThread = new Thread(new Runnable() {

        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            // begin randomly changing textures after 1 sec.
            while (true) {
                for (Spatial child : rootNode.getChildren()) {
                    Geometry g = (Geometry) (((Node) child).getChild(0));
                    Material m = g.getMaterial();
                    Texture curTex = m.getTextureParam("ColorMap").getTextureValue();
                    if (curTex == t1) {
                        m.setTexture("ColorMap", t2);
                    } else {
                        m.setTexture("ColorMap", t1);
                    }
                }
            }
        }
    });
    evilThread.setDaemon(true);
    evilThread.start();
}
Also used : Geometry(com.jme3.scene.Geometry) Quaternion(com.jme3.math.Quaternion) Spatial(com.jme3.scene.Spatial) Vector3f(com.jme3.math.Vector3f) Node(com.jme3.scene.Node) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box) Texture(com.jme3.texture.Texture)

Example 70 with Terrain

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

the class TerrainTestAdvanced method simpleInitApp.

@Override
public void simpleInitApp() {
    setupKeys();
    // First, we load up our textures and the heightmap texture for the terrain
    // TERRAIN TEXTURE material
    matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
    matTerrain.setBoolean("useTriPlanarMapping", false);
    matTerrain.setFloat("Shininess", 0.0f);
    // ALPHA map (for splat textures)
    matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alpha1.png"));
    matTerrain.setTexture("AlphaMap_1", assetManager.loadTexture("Textures/Terrain/splat/alpha2.png"));
    // this material also supports 'AlphaMap_2', so you can get up to 12 diffuse textures
    // HEIGHTMAP image (for the terrain heightmap)
    TextureKey hmKey = new TextureKey("Textures/Terrain/splat/mountains512.png", false);
    Texture heightMapImage = assetManager.loadTexture(hmKey);
    // DIRT texture, Diffuse textures 0 to 3 use the first AlphaMap
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap", dirt);
    matTerrain.setFloat("DiffuseMap_0_scale", dirtScale);
    // DARK ROCK texture
    Texture darkRock = assetManager.loadTexture("Textures/Terrain/Rock2/rock.jpg");
    darkRock.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_1", darkRock);
    matTerrain.setFloat("DiffuseMap_1_scale", darkRockScale);
    // PINK ROCK texture
    Texture pinkRock = assetManager.loadTexture("Textures/Terrain/Rock/Rock.PNG");
    pinkRock.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_2", pinkRock);
    matTerrain.setFloat("DiffuseMap_2_scale", pinkRockScale);
    // RIVER ROCK texture, this texture will use the next alphaMap: AlphaMap_1
    Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
    riverRock.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_3", riverRock);
    matTerrain.setFloat("DiffuseMap_3_scale", riverRockScale);
    // GRASS texture
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_4", grass);
    matTerrain.setFloat("DiffuseMap_4_scale", grassScale);
    // BRICK texture
    Texture brick = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg");
    brick.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_5", brick);
    matTerrain.setFloat("DiffuseMap_5_scale", brickScale);
    // ROAD texture
    Texture road = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
    road.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("DiffuseMap_6", road);
    matTerrain.setFloat("DiffuseMap_6_scale", roadScale);
    // diffuse textures 0 to 3 use AlphaMap
    // diffuse textures 4 to 7 use AlphaMap_1
    // diffuse textures 8 to 11 use AlphaMap_2
    // NORMAL MAPS
    Texture normalMapDirt = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
    normalMapDirt.setWrap(WrapMode.Repeat);
    Texture normalMapPinkRock = assetManager.loadTexture("Textures/Terrain/Rock/Rock_normal.png");
    normalMapPinkRock.setWrap(WrapMode.Repeat);
    Texture normalMapGrass = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
    normalMapGrass.setWrap(WrapMode.Repeat);
    Texture normalMapRoad = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
    normalMapRoad.setWrap(WrapMode.Repeat);
    matTerrain.setTexture("NormalMap", normalMapDirt);
    matTerrain.setTexture("NormalMap_1", normalMapPinkRock);
    matTerrain.setTexture("NormalMap_2", normalMapPinkRock);
    matTerrain.setTexture("NormalMap_4", normalMapGrass);
    matTerrain.setTexture("NormalMap_6", normalMapRoad);
    // WIREFRAME material (used to debug the terrain, only useful for this test case)
    matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matWire.getAdditionalRenderState().setWireframe(true);
    matWire.setColor("Color", ColorRGBA.Green);
    createSky();
    // CREATE HEIGHTMAP
    AbstractHeightMap heightmap = null;
    try {
        heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.3f);
        heightmap.load();
        heightmap.smooth(0.9f, 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /*
         * Here we create the actual terrain. The tiles will be 65x65, and the total size of the
         * terrain will be 513x513. It uses the heightmap we created to generate the height values.
         */
    /**
         * Optimal terrain patch size is 65 (64x64).
         * The total size is up to you. At 1025 it ran fine for me (200+FPS), however at
         * size=2049 it got really slow. But that is a jump from 2 million to 8 million triangles...
         */
    //, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
    terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
    TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
    // patch size, and a multiplier
    control.setLodCalculator(new DistanceLodCalculator(65, 2.7f));
    terrain.addControl(control);
    terrain.setMaterial(matTerrain);
    terrain.setModelBound(new BoundingBox());
    terrain.updateModelBound();
    terrain.setLocalTranslation(0, -100, 0);
    terrain.setLocalScale(1f, 1f, 1f);
    rootNode.attachChild(terrain);
    //Material debugMat = assetManager.loadMaterial("Common/Materials/VertexColor.j3m");
    //terrain.generateDebugTangents(debugMat);
    DirectionalLight light = new DirectionalLight();
    light.setDirection((new Vector3f(-0.1f, -0.1f, -0.1f)).normalize());
    rootNode.addLight(light);
    cam.setLocation(new Vector3f(0, 10, -10));
    cam.lookAtDirection(new Vector3f(0, -1.5f, -1).normalizeLocal(), Vector3f.UNIT_Y);
    flyCam.setMoveSpeed(400);
    rootNode.attachChild(createAxisMarker(20));
}
Also used : TextureKey(com.jme3.asset.TextureKey) AbstractHeightMap(com.jme3.terrain.heightmap.AbstractHeightMap) BoundingBox(com.jme3.bounding.BoundingBox) 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)

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