diff --git a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java index b71dc0cdb..c162769aa 100644 --- a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java +++ b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java @@ -31,6 +31,7 @@ */ package com.jme3.terrain.geomipmap; +import com.jme3.bounding.BoundingBox; import com.jme3.export.JmeExporter; import com.jme3.export.JmeImporter; import com.jme3.scene.control.UpdateControl; @@ -50,6 +51,7 @@ import com.jme3.material.Material; import com.jme3.math.FastMath; import com.jme3.math.Vector2f; import com.jme3.math.Vector3f; +import com.jme3.scene.Spatial; import com.jme3.terrain.Terrain; import com.jme3.terrain.geomipmap.lodcalc.LodCalculator; import com.jme3.terrain.heightmap.HeightMapGrid; @@ -118,6 +120,7 @@ public class TerrainGrid extends TerrainQuad { protected PhysicsSpace space; private int cellsLoaded = 0; private int[] gridOffset; + private boolean runOnce = false; protected class UpdateQuadCache implements Runnable { @@ -166,7 +169,7 @@ public class TerrainGrid extends TerrainQuad { public Object call() throws Exception { attachQuadAt(newQuad, quadrant, quadCell); - newQuad.resetCachedNeighbours(); + //newQuad.resetCachedNeighbours(); return null; } }); @@ -269,6 +272,9 @@ public class TerrainGrid extends TerrainQuad { } + /** + * @deprecated not needed to be called any more, handled automatically + */ public void initialize(Vector3f location) { if (this.material == null) { throw new RuntimeException("Material must be set prior to call of initialize"); @@ -293,13 +299,14 @@ public class TerrainGrid extends TerrainQuad { gridOffset[1] = Math.round(camCell.z * (size / 2)); cellsLoaded = 0; } - if (camCell.x != this.currentCamCell.x || camCell.z != currentCamCell.z) { + if (camCell.x != this.currentCamCell.x || camCell.z != currentCamCell.z || !runOnce) { // if the camera has moved into a new cell, load new terrain into the visible 4 center quads this.updateChildren(camCell); for (TerrainGridListener l : this.listeners) { l.gridMoved(camCell); } } + runOnce = true; super.update(locations, lodCalculator); } @@ -353,6 +360,14 @@ public class TerrainGrid extends TerrainQuad { l.tileAttached(quadCell, q); } updateModelBound(); + + for (Spatial s : getChildren()) { + if (s instanceof TerrainQuad) { + TerrainQuad tq = (TerrainQuad)s; + tq.resetCachedNeighbours(); + tq.fixNormalEdges(new BoundingBox(tq.getWorldTranslation(), totalSize*2, Float.MAX_VALUE, totalSize*2)); + } + } } @Deprecated diff --git a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java index 8f03eaf3d..80d956d23 100644 --- a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java +++ b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java @@ -584,7 +584,7 @@ public class TerrainQuad extends Node implements Terrain { TerrainPatch patch = (TerrainPatch) child; UpdatedTerrainPatch utp = updated.get(patch.getName()); - if(utp.lodChanged()) { + if(utp != null && utp.lodChanged()) { if (!patch.searchedForNeighboursAlready) { // set the references to the neighbours patch.rightNeighbour = findRightPatch(patch); @@ -975,6 +975,13 @@ public class TerrainQuad extends Node implements Terrain { } return false; } + + /** + * This will cause all normals for this terrain quad to be recalculated + */ + protected void setNeedToRecalculateNormals() { + affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), size*2, Float.MAX_VALUE, size*2); + } public float getHeightmapHeight(Vector2f xz) { // offset diff --git a/engine/src/test/jme3test/terrain/TerrainFractalGridTest.java b/engine/src/test/jme3test/terrain/TerrainFractalGridTest.java index a253fe197..9a2b503bc 100644 --- a/engine/src/test/jme3test/terrain/TerrainFractalGridTest.java +++ b/engine/src/test/jme3test/terrain/TerrainFractalGridTest.java @@ -138,7 +138,7 @@ public class TerrainFractalGridTest extends SimpleApplication { this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f)); - this.terrain.initialize(cam.getLocation()); + } @Override diff --git a/engine/src/test/jme3test/terrain/TerrainGridAlphaMapTest.java b/engine/src/test/jme3test/terrain/TerrainGridAlphaMapTest.java index 6491b97c2..505652832 100644 --- a/engine/src/test/jme3test/terrain/TerrainGridAlphaMapTest.java +++ b/engine/src/test/jme3test/terrain/TerrainGridAlphaMapTest.java @@ -92,6 +92,7 @@ public class TerrainGridAlphaMapTest extends SimpleApplication { material = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md"); material.setBoolean("useTriPlanarMapping", false); //material.setBoolean("isTerrainGrid", true); + material.setFloat("Shininess", 0.0f); // GRASS texture Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg"); @@ -192,11 +193,11 @@ public class TerrainGridAlphaMapTest extends SimpleApplication { public void tileAttached(Vector3f cell, TerrainQuad quad) { Texture alpha = null; - try { - alpha = assetManager.loadTexture("TerrainAlphaTest/alpha_" + (int)cell.x+ "_" + (int)cell.z + ".png"); - } catch (Exception e) { + //try { + // alpha = assetManager.loadTexture("TerrainAlphaTest/alpha_" + (int)cell.x+ "_" + (int)cell.z + ".png"); + //} catch (Exception e) { alpha = assetManager.loadTexture("TerrainAlphaTest/alpha_default.png"); - } + //} quad.getMaterial().setTexture("AlphaMap", alpha); if (usePhysics) { quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0)); @@ -213,7 +214,7 @@ public class TerrainGridAlphaMapTest extends SimpleApplication { updateMarkerElevations(); } }); - this.terrain.initialize(cam.getLocation()); + this.initKeys(); markers = new Node(); diff --git a/engine/src/test/jme3test/terrain/TerrainGridSerializationTest.java b/engine/src/test/jme3test/terrain/TerrainGridSerializationTest.java index f0a8e889f..ec07e7afe 100644 --- a/engine/src/test/jme3test/terrain/TerrainGridSerializationTest.java +++ b/engine/src/test/jme3test/terrain/TerrainGridSerializationTest.java @@ -97,7 +97,7 @@ public class TerrainGridSerializationTest extends SimpleApplication { }); } - this.terrain.initialize(cam.getLocation()); + this.initKeys(); } diff --git a/engine/src/test/jme3test/terrain/TerrainGridTest.java b/engine/src/test/jme3test/terrain/TerrainGridTest.java index 54f90981a..deca43f63 100644 --- a/engine/src/test/jme3test/terrain/TerrainGridTest.java +++ b/engine/src/test/jme3test/terrain/TerrainGridTest.java @@ -158,7 +158,7 @@ public class TerrainGridTest extends SimpleApplication { }); } - this.terrain.initialize(cam.getLocation()); + this.initKeys(); } diff --git a/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java b/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java index fa6d5354b..86fbbf988 100644 --- a/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java +++ b/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java @@ -154,7 +154,7 @@ public class TerrainGridTileLoaderTest extends SimpleApplication { }); } - this.terrain.initialize(cam.getLocation()); + this.initKeys(); } diff --git a/engine/src/test/jme3test/terrain/TerrainTestAdvanced.java b/engine/src/test/jme3test/terrain/TerrainTestAdvanced.java index 533dce40e..f69796a9f 100644 --- a/engine/src/test/jme3test/terrain/TerrainTestAdvanced.java +++ b/engine/src/test/jme3test/terrain/TerrainTestAdvanced.java @@ -97,6 +97,7 @@ public class TerrainTestAdvanced extends SimpleApplication { // 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")); diff --git a/engine/src/test/jme3test/terrain/TerrainTestModifyHeight.java b/engine/src/test/jme3test/terrain/TerrainTestModifyHeight.java index 0fe4a0cec..14fdbdb76 100644 --- a/engine/src/test/jme3test/terrain/TerrainTestModifyHeight.java +++ b/engine/src/test/jme3test/terrain/TerrainTestModifyHeight.java @@ -408,7 +408,7 @@ public class TerrainTestModifyHeight extends SimpleApplication { terrain.setMaterial(matTerrain); terrain.setLocalTranslation(0, 0, 0); terrain.setLocalScale(2f, 1f, 2f); - ((TerrainGrid)terrain).initialize(Vector3f.ZERO); + rootNode.attachChild(this.terrain); TerrainLodControl control = new TerrainLodControl(this.terrain, getCamera());