Search in sources :

Example 16 with ImageBasedHeightMap

use of com.jme3.terrain.heightmap.ImageBasedHeightMap in project jmonkeyengine by jMonkeyEngine.

the class TerrainTestCollision method simpleInitApp.

@Override
public void simpleInitApp() {
    bulletAppState = new BulletAppState();
    bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
    stateManager.attach(bulletAppState);
    setupKeys();
    matRock = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");
    matRock.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
    Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    matRock.setTexture("Tex1", grass);
    matRock.setFloat("Tex1Scale", 64f);
    Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
    dirt.setWrap(WrapMode.Repeat);
    matRock.setTexture("Tex2", dirt);
    matRock.setFloat("Tex2Scale", 32f);
    Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
    rock.setWrap(WrapMode.Repeat);
    matRock.setTexture("Tex3", rock);
    matRock.setFloat("Tex3Scale", 128f);
    matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matWire.getAdditionalRenderState().setWireframe(true);
    matWire.setColor("Color", ColorRGBA.Green);
    AbstractHeightMap heightmap = null;
    try {
        heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.25f);
        heightmap.load();
    } catch (Exception e) {
    }
    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(matRock);
    terrain.setLocalScale(new Vector3f(2, 2, 2));
    // unlock it so we can edit the height
    terrain.setLocked(false);
    rootNode.attachChild(terrain);
    /**
         * Create PhysicsRigidBodyControl for collision
         */
    terrain.addControl(new RigidBodyControl(0));
    bulletAppState.getPhysicsSpace().addAll(terrain);
    // let them drop from the sky
    for (int i = 0; i < 5; i++) {
        float r = (float) (8 * Math.random());
        Geometry sphere = new Geometry("cannonball", new Sphere(10, 10, r));
        sphere.setMaterial(matWire);
        // random position
        float x = (float) (20 * Math.random()) - 40;
        // random position
        float y = (float) (20 * Math.random()) - 40;
        // random position
        float z = (float) (20 * Math.random()) - 40;
        sphere.setLocalTranslation(new Vector3f(x, 100 + y, z));
        sphere.addControl(new RigidBodyControl(new SphereCollisionShape(r), 2));
        rootNode.attachChild(sphere);
        bulletAppState.getPhysicsSpace().add(sphere);
    }
    collisionBox = new Geometry("collisionBox", new Box(2, 2, 2));
    collisionBox.setModelBound(new BoundingBox());
    collisionBox.setLocalTranslation(new Vector3f(20, 95, 30));
    collisionBox.setMaterial(matWire);
    rootNode.attachChild(collisionBox);
    selectedCollisionObject = collisionBox;
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(1, -0.5f, -0.1f).normalizeLocal());
    dl.setColor(new ColorRGBA(0.50f, 0.40f, 0.50f, 1.0f));
    rootNode.addLight(dl);
    cam.setLocation(new Vector3f(0, 25, -10));
    cam.lookAtDirection(new Vector3f(0, -1, 0).normalizeLocal(), Vector3f.UNIT_Y);
}
Also used : SphereCollisionShape(com.jme3.bullet.collision.shapes.SphereCollisionShape) Material(com.jme3.material.Material) BoundingBox(com.jme3.bounding.BoundingBox) Box(com.jme3.scene.shape.Box) Texture(com.jme3.texture.Texture) DistanceLodCalculator(com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl) ImageBasedHeightMap(com.jme3.terrain.heightmap.ImageBasedHeightMap) Geometry(com.jme3.scene.Geometry) Sphere(com.jme3.scene.shape.Sphere) AbstractHeightMap(com.jme3.terrain.heightmap.AbstractHeightMap) ColorRGBA(com.jme3.math.ColorRGBA) BulletAppState(com.jme3.bullet.BulletAppState) Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox) DirectionalLight(com.jme3.light.DirectionalLight) TerrainLodControl(com.jme3.terrain.geomipmap.TerrainLodControl) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad)

Aggregations

Texture (com.jme3.texture.Texture)16 Material (com.jme3.material.Material)15 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)15 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)15 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)15 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)11 Vector3f (com.jme3.math.Vector3f)10 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)8 Camera (com.jme3.renderer.Camera)7 ArrayList (java.util.ArrayList)7 DirectionalLight (com.jme3.light.DirectionalLight)5 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)4 BoundingBox (com.jme3.bounding.BoundingBox)3 TextureKey (com.jme3.asset.TextureKey)2 BulletAppState (com.jme3.bullet.BulletAppState)2 ChaseCamera (com.jme3.input.ChaseCamera)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 CapsuleCollisionShape (com.jme3.bullet.collision.shapes.CapsuleCollisionShape)1 SphereCollisionShape (com.jme3.bullet.collision.shapes.SphereCollisionShape)1 CharacterControl (com.jme3.bullet.control.CharacterControl)1