Gaps in TerrainGrid

I’m getting small gaps between the tiles in my terrain grid. I’ve done a bit of searching the forums, and it seems to be a common problem but no obvious solution. Here’s my code:

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.terrain.geomipmap.TerrainGrid;
import com.jme3.terrain.geomipmap.TerrainGridLodControl;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.grid.ImageTileLoader;
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
import com.jme3.terrain.heightmap.Namer;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

public class MyTerrainGridTest extends SimpleApplication {

    private Material mat_terrain;
    private TerrainGrid terrain;

    public static void main(final String[] args) {
        MyTerrainGridTest app = new MyTerrainGridTest();
        app.showSettings = false;
        app.start();
    }

    @Override
    public void simpleInitApp() {
        assetManager.registerLocator("assets/", FileLocator.class); // default

        this.flyCam.setMoveSpeed(100f);
        initMaterial();
        initTerrain();
        this.getCamera().setLocation(new Vector3f(0, 200, 0));
        this.getCamera().lookAt(new Vector3f(0,0,0), Vector3f.UNIT_Y);
        this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
    }

    
    @Override
    public void simpleUpdate(final float tpf) {
    }

    
    public void initMaterial() {
        this.mat_terrain = new Material(this.assetManager, "Common/MatDefs/Terrain/HeightBasedTerrain.j3md");
        Texture rock = this.assetManager.loadTexture("Textures/Terrain/Rock2/rock.jpg");
        rock.setWrap(WrapMode.Repeat);
        this.mat_terrain.setTexture("region1ColorMap", rock);
        this.mat_terrain.setVector3("region1", new Vector3f(-10, 0, 64));
    }

    private void initTerrain() {
        int img_size = 256;
        ImageTileLoader itl = new ImageTileLoader(assetManager, new Namer() {
            public String getName(int x, int y) {
                return "Textures/heightmap_hilly_256.png";
                //return "Scenes/TerrainMountains/terrain_1_1.png";

            }
        });
        itl.setHeightScale(.01f);

        this.terrain = new TerrainGrid("terrain", 65, (img_size*2)+1, itl);
        this.terrain.setMaterial(mat_terrain);
        this.terrain.setLocalTranslation(0, 0, 0);
        this.terrain.setLocalScale(3f, .15f, 3f);
        this.rootNode.attachChild(this.terrain);

        TerrainLodControl control = new TerrainGridLodControl(this.terrain, getCamera());
        control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) ); // patch size, and a multiplier
        this.terrain.addControl(control);
    }
}

Any help much appreciated. Thanks.

Edit: In addition, I’m looking to add some decent lighting to the terrain grid, but the directional light doesn’t seem to affect it. Any suggestions?

A common problem, and frequent solution, is to understand that the height-fields must overlap. The first one must be 257 x 257 pixels, it needs to include the same heights as the first pixels in the next tiles on +X and +Z

Do you mean that my heightmap image must be 257 pixels x 257 pixels?

Yes

Thanks, that sort of works. However, it now means that the heightmap doesn’t tile exactly right, so there are small(er) gaps where the edges are slightly different heights. I’m guessing that the heightmap needs to be 257x257, but the 257th row and column of pixels needs to be a duplicate of the 256th row and column to ensure it still tiles. I’ll try that and see if it works.

I think 257th row needs to be duplicate from 1th row in next cell of your map, not 256th of the same cell.

Like prog says.

Yep, that works like a treat. (Bit of a faff though). Thanks for the help everyone.