From 14cc4eb3b2a181f08d2cb5d0e12d5512b599d739 Mon Sep 17 00:00:00 2001 From: "bre..ns" Date: Tue, 23 Aug 2011 20:19:34 +0000 Subject: [PATCH] improved heightmap smooth algorithm git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8066 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../terrain/heightmap/AbstractHeightMap.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java b/engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java index 99be15b3f..97e48532b 100644 --- a/engine/src/terrain/com/jme3/terrain/heightmap/AbstractHeightMap.java +++ b/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. */ 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 np 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) { return; } - int[] dxs = new int[]{-1, 0, 1, 1, 1, 0, -1, -1}; - int[] dys = new int[]{-1, -1, -1, 0, 1, 1, 1, 0}; + if (radius == 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 y = 0; y < size; y++) { int neighNumber = 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 j = y + dys[d]; if (i < 0 || i >= size) { @@ -455,7 +483,7 @@ public abstract class AbstractHeightMap implements HeightMap { } neighNumber++; neighAverage += heightData[i + j * size]; - } + }*/ neighAverage /= neighNumber; float cp = 1 - np;