Search in sources :

Example 1 with TerrainPatch

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

the class TerrainPatch method clone.

@Override
public TerrainPatch clone() {
    TerrainPatch clone = new TerrainPatch();
    clone.name = name.toString();
    clone.size = size;
    clone.totalSize = totalSize;
    clone.quadrant = quadrant;
    clone.stepScale = stepScale.clone();
    clone.offset = offset.clone();
    clone.offsetAmount = offsetAmount;
    //clone.lodCalculator = lodCalculator.clone();
    //clone.lodCalculator.setTerrainPatch(clone);
    //clone.setLodCalculator(lodCalculatorFactory.clone());
    clone.geomap = new LODGeomap(size, geomap.getHeightArray());
    clone.setLocalTranslation(getLocalTranslation().clone());
    Mesh m = clone.geomap.createMesh(clone.stepScale, Vector2f.UNIT_XY, clone.offset, clone.offsetAmount, clone.totalSize, false);
    clone.setMesh(m);
    clone.setMaterial(material.clone());
    return clone;
}
Also used : Mesh(com.jme3.scene.Mesh)

Example 2 with TerrainPatch

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

the class TerrainQuad method getMeshNormal.

protected Vector3f getMeshNormal(int x, int z) {
    int quad = findQuadrant(x, z);
    int split = (size + 1) >> 1;
    if (children != null) {
        for (int i = children.size(); --i >= 0; ) {
            Spatial spat = children.get(i);
            int col = x;
            int row = z;
            boolean match = false;
            // get the childs quadrant
            int childQuadrant = 0;
            if (spat instanceof TerrainQuad) {
                childQuadrant = ((TerrainQuad) spat).getQuadrant();
            } else if (spat instanceof TerrainPatch) {
                childQuadrant = ((TerrainPatch) spat).getQuadrant();
            }
            if (childQuadrant == 1 && (quad & 1) != 0) {
                match = true;
            } else if (childQuadrant == 2 && (quad & 2) != 0) {
                row = z - split + 1;
                match = true;
            } else if (childQuadrant == 3 && (quad & 4) != 0) {
                col = x - split + 1;
                match = true;
            } else if (childQuadrant == 4 && (quad & 8) != 0) {
                col = x - split + 1;
                row = z - split + 1;
                match = true;
            }
            if (match) {
                if (spat instanceof TerrainQuad) {
                    return ((TerrainQuad) spat).getMeshNormal(col, row);
                } else if (spat instanceof TerrainPatch) {
                    return ((TerrainPatch) spat).getMeshNormal(col, row);
                }
            }
        }
    }
    return null;
}
Also used : Spatial(com.jme3.scene.Spatial)

Example 3 with TerrainPatch

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

the class TerrainQuad method getHeightMap.

public float[] getHeightMap() {
    float[] hm = null;
    int length = ((size - 1) / 2) + 1;
    int area = size * size;
    hm = new float[area];
    if (getChildren() != null && !getChildren().isEmpty()) {
        float[] ul = null, ur = null, bl = null, br = null;
        // get the child heightmaps
        if (getChild(0) instanceof TerrainPatch) {
            for (Spatial s : getChildren()) {
                if (((TerrainPatch) s).getQuadrant() == 1)
                    ul = ((TerrainPatch) s).getHeightMap();
                else if (((TerrainPatch) s).getQuadrant() == 2)
                    bl = ((TerrainPatch) s).getHeightMap();
                else if (((TerrainPatch) s).getQuadrant() == 3)
                    ur = ((TerrainPatch) s).getHeightMap();
                else if (((TerrainPatch) s).getQuadrant() == 4)
                    br = ((TerrainPatch) s).getHeightMap();
            }
        } else {
            ul = getQuad(1).getHeightMap();
            bl = getQuad(2).getHeightMap();
            ur = getQuad(3).getHeightMap();
            br = getQuad(4).getHeightMap();
        }
        // first upper blocks
        for (int y = 0; y < length; y++) {
            // rows
            for (int x1 = 0; x1 < length; x1++) {
                int row = y * size;
                hm[row + x1] = ul[y * length + x1];
            }
            for (int x2 = 1; x2 < length; x2++) {
                int row = y * size + length;
                hm[row + x2 - 1] = ur[y * length + x2];
            }
        }
        // second lower blocks
        int rowOffset = size * length;
        for (int y = 1; y < length; y++) {
            // rows
            for (int x1 = 0; x1 < length; x1++) {
                int row = (y - 1) * size;
                hm[rowOffset + row + x1] = bl[y * length + x1];
            }
            for (int x2 = 1; x2 < length; x2++) {
                int row = (y - 1) * size + length;
                hm[rowOffset + row + x2 - 1] = br[y * length + x2];
            }
        }
    }
    return hm;
}
Also used : Spatial(com.jme3.scene.Spatial)

Example 4 with TerrainPatch

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

the class DistanceLodCalculator method calculateLod.

public boolean calculateLod(TerrainPatch terrainPatch, List<Vector3f> locations, HashMap<String, UpdatedTerrainPatch> updates) {
    if (locations == null || locations.isEmpty())
        // no camera yet
        return false;
    float distance = getCenterLocation(terrainPatch).distance(locations.get(0));
    if (turnOffLod) {
        // set to full detail
        int prevLOD = terrainPatch.getLod();
        UpdatedTerrainPatch utp = updates.get(terrainPatch.getName());
        if (utp == null) {
            utp = new UpdatedTerrainPatch(terrainPatch);
            updates.put(utp.getName(), utp);
        }
        utp.setNewLod(0);
        utp.setPreviousLod(prevLOD);
        //utp.setReIndexNeeded(true);
        return true;
    }
    // go through each lod level to find the one we are in
    for (int i = 0; i <= terrainPatch.getMaxLod(); i++) {
        if (distance < getLodDistanceThreshold() * (i + 1) * terrainPatch.getWorldScaleCached().x || i == terrainPatch.getMaxLod()) {
            boolean reIndexNeeded = false;
            if (i != terrainPatch.getLod()) {
                reIndexNeeded = true;
            //System.out.println("lod change: "+lod+" > "+i+"    dist: "+distance);
            }
            int prevLOD = terrainPatch.getLod();
            UpdatedTerrainPatch utp = updates.get(terrainPatch.getName());
            if (utp == null) {
                //save in here, do not update actual variables
                utp = new UpdatedTerrainPatch(terrainPatch);
                updates.put(utp.getName(), utp);
            }
            utp.setNewLod(i);
            utp.setPreviousLod(prevLOD);
            return reIndexNeeded;
        }
    }
    return false;
}
Also used : UpdatedTerrainPatch(com.jme3.terrain.geomipmap.UpdatedTerrainPatch)

Example 5 with TerrainPatch

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

the class PerspectiveLodCalculator method calculateLod.

public boolean calculateLod(TerrainPatch patch, List<Vector3f> locations, HashMap<String, UpdatedTerrainPatch> updates) {
    if (turnOffLod) {
        // set to full detail
        int prevLOD = patch.getLod();
        UpdatedTerrainPatch utp = updates.get(patch.getName());
        if (utp == null) {
            utp = new UpdatedTerrainPatch(patch);
            updates.put(utp.getName(), utp);
        }
        utp.setNewLod(0);
        utp.setPreviousLod(prevLOD);
        //utp.setReIndexNeeded(true);
        return true;
    }
    float[] lodEntropies = patch.getLodEntropies();
    float cameraConstant = getCameraConstant(cam, pixelError);
    Vector3f patchPos = getCenterLocation(patch);
    // vector from camera to patch
    //Vector3f toPatchDir = locations.get(0).subtract(patchPos).normalizeLocal();
    //float facing = cam.getDirection().dot(toPatchDir);
    float distance = patchPos.distance(locations.get(0));
    // go through each lod level to find the one we are in
    for (int i = 0; i <= patch.getMaxLod(); i++) {
        if (distance < lodEntropies[i] * cameraConstant || i == patch.getMaxLod()) {
            boolean reIndexNeeded = false;
            if (i != patch.getLod()) {
                reIndexNeeded = true;
            //                    System.out.println("lod change: "+lod+" > "+i+"    dist: "+distance);
            }
            int prevLOD = patch.getLod();
            UpdatedTerrainPatch utp = updates.get(patch.getName());
            if (utp == null) {
                //save in here, do not update actual variables
                utp = new UpdatedTerrainPatch(patch);
                updates.put(utp.getName(), utp);
            }
            utp.setNewLod(i);
            utp.setPreviousLod(prevLOD);
            //utp.setReIndexNeeded(reIndexNeeded);
            return reIndexNeeded;
        }
    }
    return false;
}
Also used : UpdatedTerrainPatch(com.jme3.terrain.geomipmap.UpdatedTerrainPatch) Vector3f(com.jme3.math.Vector3f)

Aggregations

Vector3f (com.jme3.math.Vector3f)5 Spatial (com.jme3.scene.Spatial)5 Vector2f (com.jme3.math.Vector2f)2 TerrainPatch (com.jme3.terrain.geomipmap.TerrainPatch)2 UpdatedTerrainPatch (com.jme3.terrain.geomipmap.UpdatedTerrainPatch)2 ArrayList (java.util.ArrayList)2 BoundingBox (com.jme3.bounding.BoundingBox)1 ChildCollisionShape (com.jme3.bullet.collision.shapes.infos.ChildCollisionShape)1 CollisionResult (com.jme3.collision.CollisionResult)1 CollisionResults (com.jme3.collision.CollisionResults)1 Transform (com.jme3.math.Transform)1 Triangle (com.jme3.math.Triangle)1 Mesh (com.jme3.scene.Mesh)1 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)1 Direction (com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction)1 TerrainPickData (com.jme3.terrain.geomipmap.picking.TerrainPickData)1