Terrain Smoothing Code

Here is a simple method that will help you smooth out loaded height maps so they look nice.

MapWidth should be the width of the map in pixels, 512, 1024…

Passes should be 1 or more, 10 will do a decent job. But set it as high as you want



[java]

private static final int Increment = 2;



public static float[] Smooth(float[] HeightMap, int MapWidth, int Passes)

{

int Alternator = 0;

for (int p = 0; p < Passes; p++)

{

for (int i = MapWidth + 1 + Alternator; i < HeightMap.length - MapWidth - 1 - Alternator; i += Increment)

{

// Each middle value equals the average point between the points around it

HeightMap = ((HeightMap + HeightMap +

HeightMap + HeightMap +

HeightMap + HeightMap +

HeightMap +

HeightMap) / 8f);

}



if (Alternator == 0)

Alternator = 1;

else

Alternator = 0;

}



return HeightMap;

}[/java]

3 Likes

Can this be working on JME platform ?

Yeah it works well, although it flattens some hills :slight_smile:

It’s very agressive so start off with 1 pass.



To use this, you will have to copy this code into your project and call this “Smooth” function after you have loaded your height map. You pass in your heightmap data into this function before creating a terrain spatial from it. Here is an example from one of my projects:



[java]

// First load the height map

Texture heightMapImage = Global.AssetManager.loadTexture(Name+"/mountains512.png");

ImageBasedHeightMap heightmap = new ImageBasedHeightMap(ImageToAwt.convert(heightMapImage.getImage(), true, true, 0));

heightmap.load();

// Then smooth it out

float[] mapData = TerrainSmoother.Smooth(heightmap.getHeightMap(), 1024, 50);

// Then create the terrain spatial

Terrain = new TerrainQuad(“Terrain”, 65, 1025, mapData);[/java]

The terrain editor could have a “smooth” brush that does some kind of blur on the area

1 Like

That would be cool, I’m still using one of the older alphas sans terrain editor. Using paint to make my maps, then I smooth the terrain out with that function :slight_smile:

Maybe I should upgrade.

According to this post http://hub.jmonkeyengine.org/groups/terramonkey/forum/topic/question-about-imagebasedheightmap/#post-124931 i was going to implement it myself , but iceczd you seems to know the subject better than me so i propose that we could work on it together , also i sent you a private message

This is awesome!!! Thank you! Makes my terrain look much better :smiley: Also makes a physics car game with the level being made of a heightmap possible :slight_smile: Wish I had it for my project that I had to do last quarter (it was a car game that used terrain as the level… It wasn’t very good). I have added the snippet to it now and it works MUCH better :slight_smile:

@Danath: Use my code however you need to get the job done, I can help out on occasion if you ever need me to review code or need suggestions, I’m just busy with work all the time so I’m not always quick to respond, but happy to help a little when I can. Will check my messages right now :slight_smile:



@nomnom: Glad the smooth terrain is helping :slight_smile: