TerrainGrid and ImageTileLoader

Hi, I’m trying to change my TerrainQuad original implementation to a TerrainGrid one that will load multiples terrain tiles, etc. So, in the attempt of reusing my original height map (a 512x512 png), I “borrowed” the code from TerrainGridTest.java but I get this error :





INFO: Loading heightmap from file: Scenes/Terrain/world_-1_-1.png

13-Feb-2012 1:30:48 PM com.jme3.terrain.geomipmap.TerrainQuad

WARNING: Heightmap size is larger than the terrain size. Make sure your heightmap image is the same size as the terrain!





I have tried changing some values, but I’m afraid I am completely lost now. Here is what I tried last with no success :



/* snip /

mat_terrain.setFloat(“terrainSize”, 129);



this.terrain = new TerrainGrid(“terrain”, 129, 513, new ImageTileLoader(assetManager, new Namer() {

public String getName(int x, int y) {

return “Scenes/Terrain/world_” + x + “_” + y + “.png”;

}

}));

/
snip */



Can someone help me understand how I can fix this, please?

your heightmap image is too big.



When you create a terrain grid, the 3rd parameter is the visible size of the terrain. Since terrain is a quad tree, and TerrainGrid has 4 TerrainQuads as the top level, the heightmaps need to be half the size of the visible size: 256x256px

You will also want your 2nd parameter to be smaller, or else not much culling or LOD will happen. 65 is a good value. That is the low-level patch size (the actual geometry)

Ok, so I reduced my heightmap tile to 256x256, and set the parameters as





mat_terrain.setFloat(“terrainSize”, 129);

this.terrain = new TerrainGrid(“terrain”, 65, 257, new ImageTileLoader(assetManager, new Namer() {

public String getName(int x, int y) {

return “Scenes/Terrain/world_” + x + “_” + y + “.png”;

}

}));





But all I see is a flat terrain…



Here is my test heightmap :



Do you just have the one heightmap?

The imageTileLoader is going to look for images tiles with the names: world_0_0.png, world_0_1.png, world_1_0.png, world_1_1.png etc.

So you need to have many of these heightmaps that tile up to build your whole world, not just the one.

Look at the sample data here in the ScenesTerrainMountains folder. Each image there is a terrain tile heightmap, they all seam together to form the massive terrain.



If you just want one terrain and not infinite terrain, then just use TerrainQuad.

Alright, I’ve made myself a set of 128x128 tiles (Gray scale PNG), but they seem to load randomly… how should I initialize my TerrainGrid? (The values for the material and the value for the TerrainGrid constructor). I have tried this, but with no luck :





mat_terrain.setFloat(“terrainSize”, 128);

this.terrain = new TerrainGrid(“terrain”, 65, 256, new ImageTileLoader(assetManager, new Namer() {

public String getName(int x, int y) {

return “Scenes/Terrain/world_” + x + “_” + y + “.png”;

}

}));





Also, about the test data, why are the tiles 129x129? Is it because there are an extra row / column that repeats the same heightmap data from it’s right / bottom neighbor?

Ok, I finally resized my map to a power of 2 + 1 (ex: 1025x1025), then made all my tiles to a power of 2 + 1 (ie. 129x129) then used the same settings as the TerrainGridTest.java and it works!

Cool, glad you got it working. I will add some more javadocs to the terrainGrid class to help others out, it doesn’t have much documentation right now and can be a bit confusing.