improved heightmap smooth algorithm

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8066 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
bre..ns 13 years ago
parent ccb31e646b
commit 14cc4eb3b2
  1. 36
      engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java

@ -435,16 +435,44 @@ public abstract class AbstractHeightMap implements HeightMap {
* Value of 1 will ignore the node old height. * Value of 1 will ignore the node old height.
*/ */
public void smooth(float np) { public void smooth(float np) {
smooth(np, 1);
}
/**
* Smooth the terrain. For each node, its X(determined by radius) neighbors heights
* are averaged and will participate in the node new height
* by a factor <code>np</code> between 0 and 1
*
* @param np
* The factor to what extend the neighbors average has an influence.
* Value of 0 will ignore neighbors (no smoothing)
* Value of 1 will ignore the node old height.
*/
public void smooth(float np, int radius) {
if (np < 0 || np > 1) { if (np < 0 || np > 1) {
return; return;
} }
int[] dxs = new int[]{-1, 0, 1, 1, 1, 0, -1, -1}; if (radius == 0)
int[] dys = new int[]{-1, -1, -1, 0, 1, 1, 1, 0}; radius = 1;
//int[] dxs = new int[]{-1, 0, 1, 1, 1, 0, -1, -1};
//int[] dys = new int[]{-1, -1, -1, 0, 1, 1, 1, 0};
for (int x = 0; x < size; x++) { for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {
int neighNumber = 0; int neighNumber = 0;
float neighAverage = 0; float neighAverage = 0;
for (int d = 0; d < 8; d++) { for (int rx = -radius; rx <= radius; rx++) {
for (int ry = -radius; ry <= radius; ry++) {
if (x+rx < 0 || x+rx >= size) {
continue;
}
if (y+ry < 0 || y+ry >= size) {
continue;
}
neighNumber++;
neighAverage += heightData[(x+rx) + (y+ry) * size];
}
}
/*for (int d = 0; d < 8; d++) {
int i = x + dxs[d]; int i = x + dxs[d];
int j = y + dys[d]; int j = y + dys[d];
if (i < 0 || i >= size) { if (i < 0 || i >= size) {
@ -455,7 +483,7 @@ public abstract class AbstractHeightMap implements HeightMap {
} }
neighNumber++; neighNumber++;
neighAverage += heightData[i + j * size]; neighAverage += heightData[i + j * size];
} }*/
neighAverage /= neighNumber; neighAverage /= neighNumber;
float cp = 1 - np; float cp = 1 - np;

Loading…
Cancel
Save