Search in sources :

Example 6 with Grid

use of com.jme3.scene.debug.Grid in project jmonkeyengine by jMonkeyEngine.

the class TerrainGridLodControl method updateLOD.

@Override
protected void updateLOD(List<Vector3f> locations, LodCalculator lodCalculator) {
    TerrainGrid terrainGrid = (TerrainGrid) getSpatial();
    // for now, only the first camera is handled.
    // to accept more, there are two ways:
    // 1: every camera has an associated grid, then the location is not enough to identify which camera location has changed
    // 2: grids are associated with locations, and no incremental update is done, we load new grids for new locations, and unload those that are not needed anymore
    Vector3f cam = locations.isEmpty() ? Vector3f.ZERO.clone() : locations.get(0);
    // get the grid index value of where the camera is (ie. 2,1)
    Vector3f camCell = terrainGrid.getCamCell(cam);
    if (terrainGrid.cellsLoaded > 1) {
        // Check if cells are updated before updating gridoffset.
        terrainGrid.gridOffset[0] = Math.round(camCell.x * (terrainGrid.size / 2));
        terrainGrid.gridOffset[1] = Math.round(camCell.z * (terrainGrid.size / 2));
        terrainGrid.cellsLoaded = 0;
    }
    if (camCell.x != terrainGrid.currentCamCell.x || camCell.z != terrainGrid.currentCamCell.z || !terrainGrid.runOnce) {
        // if the camera has moved into a new cell, load new terrain into the visible 4 center quads
        terrainGrid.updateChildren(camCell);
        for (TerrainGridListener l : terrainGrid.listeners) {
            l.gridMoved(camCell);
        }
    }
    terrainGrid.runOnce = true;
    super.updateLOD(locations, lodCalculator);
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 7 with Grid

use of com.jme3.scene.debug.Grid in project jmonkeyengine by jMonkeyEngine.

the class AssetTileLoader method getTerrainQuadAt.

public TerrainQuad getTerrainQuadAt(Vector3f location) {
    String modelName = assetPath + "/" + name + "_" + Math.round(location.x) + "_" + Math.round(location.y) + "_" + Math.round(location.z) + ".j3o";
    Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Load terrain grid tile: {0}", modelName);
    TerrainQuad quad = null;
    try {
        quad = (TerrainQuad) manager.loadModel(modelName);
    } catch (Exception e) {
    //            e.printStackTrace();
    }
    if (quad == null) {
        Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Could not load terrain grid tile: {0}", modelName);
        quad = createNewQuad(location);
    } else {
        Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Loaded terrain grid tile: {0}", modelName);
    }
    return quad;
}
Also used : TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad) IOException(java.io.IOException)

Example 8 with Grid

use of com.jme3.scene.debug.Grid in project jmonkeyengine by jMonkeyEngine.

the class BresenhamTerrainPicker method getTerrainIntersection.

public Vector3f getTerrainIntersection(Ray worldPick, CollisionResults results) {
    worldPickRay.set(worldPick);
    List<TerrainPickData> pickData = new ArrayList<TerrainPickData>();
    root.findPick(worldPick.clone(), pickData);
    Collections.sort(pickData);
    if (pickData.isEmpty())
        return null;
    workRay.set(worldPick);
    for (TerrainPickData pd : pickData) {
        TerrainPatch patch = pd.targetPatch;
        tracer.getGridSpacing().set(patch.getWorldScale());
        tracer.setGridOrigin(patch.getWorldTranslation());
        workRay.getOrigin().set(worldPick.getDirection()).multLocal(pd.cr.getDistance() - .1f).addLocal(worldPick.getOrigin());
        tracer.startWalk(workRay);
        final Vector3f intersection = new Vector3f();
        final Vector2f loc = tracer.getGridLocation();
        if (tracer.isRayPerpendicularToGrid()) {
            Triangle hit = new Triangle();
            checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit);
            float distance = worldPickRay.origin.distance(intersection);
            CollisionResult cr = new CollisionResult(intersection, distance);
            cr.setGeometry(patch);
            cr.setContactNormal(hit.getNormal());
            results.addCollision(cr);
            return intersection;
        }
        while (loc.x >= -1 && loc.x <= patch.getSize() && loc.y >= -1 && loc.y <= patch.getSize()) {
            //System.out.print(loc.x+","+loc.y+" : ");
            // check the triangles of main square for intersection.
            Triangle hit = new Triangle();
            if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
                // we found an intersection, so return that!
                float distance = worldPickRay.origin.distance(intersection);
                CollisionResult cr = new CollisionResult(intersection, distance);
                cr.setGeometry(patch);
                results.addCollision(cr);
                cr.setContactNormal(hit.getNormal());
                return intersection;
            }
            // because of how we get our height coords, we will
            // sometimes be off by a grid spot, so we check the next
            // grid space up.
            int dx = 0, dz = 0;
            Direction d = tracer.getLastStepDirection();
            switch(d) {
                case PositiveX:
                case NegativeX:
                    dx = 0;
                    dz = 1;
                    break;
                case PositiveZ:
                case NegativeZ:
                    dx = 1;
                    dz = 0;
                    break;
            }
            if (checkTriangles(loc.x + dx, loc.y + dz, workRay, intersection, patch, hit)) {
                // we found an intersection, so return that!
                float distance = worldPickRay.origin.distance(intersection);
                CollisionResult cr = new CollisionResult(intersection, distance);
                results.addCollision(cr);
                cr.setGeometry(patch);
                cr.setContactNormal(hit.getNormal());
                return intersection;
            }
            tracer.next();
        }
    }
    return null;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) ArrayList(java.util.ArrayList) Triangle(com.jme3.math.Triangle) Direction(com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction) TerrainPatch(com.jme3.terrain.geomipmap.TerrainPatch)

Example 9 with Grid

use of com.jme3.scene.debug.Grid in project jmonkeyengine by jMonkeyEngine.

the class BresenhamYUpGridTracer method startWalk.

public void startWalk(final Ray walkRay) {
    // store ray
    this.walkRay.set(walkRay);
    // simplify access to direction
    Vector3f direction = this.walkRay.getDirection();
    // Move start point to grid space
    Vector3f start = this.walkRay.getOrigin().subtract(gridOrigin);
    gridLocation.x = (int) (start.x / gridSpacing.x);
    gridLocation.y = (int) (start.z / gridSpacing.z);
    Vector3f ooDirection = new Vector3f(1.0f / direction.x, 1, 1.0f / direction.z);
    // Check which direction on the X world axis we are moving.
    if (direction.x > TOLERANCE) {
        distToNextXIntersection = ((gridLocation.x + 1) * gridSpacing.x - start.x) * ooDirection.x;
        distBetweenXIntersections = gridSpacing.x * ooDirection.x;
        stepXDirection = 1;
    } else if (direction.x < -TOLERANCE) {
        distToNextXIntersection = (start.x - (gridLocation.x * gridSpacing.x)) * -direction.x;
        distBetweenXIntersections = -gridSpacing.x * ooDirection.x;
        stepXDirection = -1;
    } else {
        distToNextXIntersection = Float.MAX_VALUE;
        distBetweenXIntersections = Float.MAX_VALUE;
        stepXDirection = 0;
    }
    // Check which direction on the Z world axis we are moving.
    if (direction.z > TOLERANCE) {
        distToNextZIntersection = ((gridLocation.y + 1) * gridSpacing.z - start.z) * ooDirection.z;
        distBetweenZIntersections = gridSpacing.z * ooDirection.z;
        stepZDirection = 1;
    } else if (direction.z < -TOLERANCE) {
        distToNextZIntersection = (start.z - (gridLocation.y * gridSpacing.z)) * -direction.z;
        distBetweenZIntersections = -gridSpacing.z * ooDirection.z;
        stepZDirection = -1;
    } else {
        distToNextZIntersection = Float.MAX_VALUE;
        distBetweenZIntersections = Float.MAX_VALUE;
        stepZDirection = 0;
    }
    // Reset some variables
    rayLocation.set(start);
    rayLength = 0.0f;
    stepDirection = Direction.None;
}
Also used : Vector3f(com.jme3.math.Vector3f)

Aggregations

Vector3f (com.jme3.math.Vector3f)6 Material (com.jme3.material.Material)3 ActionListener (com.jme3.input.controls.ActionListener)2 KeyTrigger (com.jme3.input.controls.KeyTrigger)2 AmbientLight (com.jme3.light.AmbientLight)2 DirectionalLight (com.jme3.light.DirectionalLight)2 SpotLight (com.jme3.light.SpotLight)2 ColorRGBA (com.jme3.math.ColorRGBA)2 Triangle (com.jme3.math.Triangle)2 Geometry (com.jme3.scene.Geometry)2 LightNode (com.jme3.scene.LightNode)2 Node (com.jme3.scene.Node)2 Box (com.jme3.scene.shape.Box)2 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)2 Texture (com.jme3.texture.Texture)2 IOException (java.io.IOException)2 BasicProfilerState (com.jme3.app.BasicProfilerState)1 ChaseCameraAppState (com.jme3.app.ChaseCameraAppState)1 ScreenshotAppState (com.jme3.app.state.ScreenshotAppState)1 AssetInfo (com.jme3.asset.AssetInfo)1