[Solved] Terrain Stitching

A while back I started making a custom paging grid for my project as a more customizable alternative to the terrain grid. Right now I’m having some trouble getting the neighboring terrains to stitch together nicely on the edges. Here’s the code I’m calling on each terrain vertex that neighbors another terrain:

Vector2f localHeight = new Vector2f(), neighborHeight = new Vector2f();
    for(int x = 0; x < tileWidth + 8;x++){
        for(int z = 0; z < tileWidth + 8; z++){  
        int xLocation =  ((x-4)-(tileWidth/2) + xCenterIndex); 
        int zLocation =  ((z-4)-(tileWidth/2) + zCenterIndex);
        localHeight.set(xLocation, zLocation-1);
        if(botTerrain != null && z == 0){ // z==0 is the bottom row of the center hieghtmap
            neighborHeight.set(xLocation, zLocation+511); // z==511 is the top row of the heightmap below
                      float neighborY = botTerrain.getHeightmapHeight(neighborHeight); 
                      float localY = localTerrain.getHeightmapHeight(localHeight); 
                      if(neighborY !=Float.nan && localY != Float.nan){
                            float avg = (localY + neighborY)/2;
                            if(avg > 0){
                                terrain.setHeight(localHeight, avg);
                                botTerrain.setHeight(neighborHeight, avg);

The method Terrain.getHeightMapHeight() returns Float.NaN if you reference a point outside of the heightmap, so I think that’s my issue. But I’m not sure where to start fixing that because the locations that return NaN don’t seem to have any pattern: each terrain has a few random spots that consistently don’t want to get stitched together. Additionally, the Vector2f is a world coordinate, not a local coordinate, so even though each terrain is 512x512, I had to do some math to account for the different world translation of each terrain

In the top picture I set the height to 30 for each vertex to show how it seems to sporadically miss small chunks along the edges.

The bottom picture is how it looks if I set the height to the average so it smooths out, and I also covered up the gap on the left with a rock for now. If all else fails, I can always sculpt my terrain to match as much as possible and cover up the bad spots with rocks and plants, but I’d much rather figure out how to get this to stitch in every spot. Thanks :sweat_smile:.

Why does your terrain have gaps?

If it’s LOD related then you are transitioning LODs Waaaay too close.

Is this JME’s terrain or your own custom terrain?

JME chose to go with the really complicated and totally unnecessary terrain stitching. In reality, the easiest way to transition from one LOD to another is by dropping skirts with the same texture coordinates. Seamless terrain tile transitions essentially for free.

It will still be a bit noticeable in your case but your gaps look like they are a data/algorithm problem and not an LOD problem.

I’m using a normal JME terrain that i created using the Spaceshift editor and SDK editor.

I’m assembling a non-dynamic world out of a handful of scenes that each get loaded in from their j3o file and placed in a grid formation. Each scene is a parent node for its scene objects like plants and rocks, as well as a 512x512 terrain, so the gaps are appearing along the edges where different scene nodes meet.

I think I’m doing this a bit different from how I’ve seen other pagers work, but I had trouble implementing them so I decided to start with this simpler approach that pages a grid of scenes, and just looks at the terrain as a scene object.

I also tried removing the code where I add LODControls to each terrain and its still looking the same, so it must be something I’m doing wrong. I’m going to take a break for now and look at everything with a fresh mindset tomorrow.

JME terrain has to be used with JME controls, etc… because otherwise you get big gaps between the tiles from where the LOD stitching would normally happen.

If you aren’t going to use the JME controls, etc… then you are better off generating your own mesh, I guess.

jME terrain tiles must have a heightmap that is 2^x+1 i.e. 513x513 values. The last column and row must be the same as the neighbouring tiles. That is, the last column must be the same values as the first column on the ‘right’ and th elast row must be the same values as the tile ‘below’. You need to overlap the tiles so they can be stitched.

1 Like

I took that into account and redid the math for my code, and that’s definitely where I was going wrong. I wasn’t accounting for the +1 for a terrain size at all and was just getting lucky that it was adding up to work in some places at all.

I appreciate the help, thanks! :slight_smile:

1 Like