0
我试图平滑我的2D数组。我确信有更好的方法,但我还没有找到它。到目前为止,我正在使用这一点代码来平滑它。平滑数据的问题
您可以在后面看到有一个区域,单个条目在错误的方向上升/下降。它使单个像素看起来比周围的任何物体都更暗/更轻。
我在做什么错误,在我的数据平滑?
for (int i = 0; i < 3; i++) {
for (int y = 0; y < mapChunkSize + 2; y++) {
for (int x = 0; x < mapChunkSize + 2; x++) {
int count = 0;
int dir = 0;
if (x - 1 >= 0 && y - 1 >= 0 && noiseMap [x - 1, y - 1] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x - 1, y - 1] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (x - 1 >= 0 && noiseMap [x - 1, y] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x - 1, y] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (x - 1 >= 0 && y + 1 <= mapChunkSize && noiseMap [x - 1, y + 1] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x - 1, y + 1] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (y - 1 >= 0 && noiseMap [x, y - 1] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x, y - 1] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (y + 1 <= mapChunkSize && noiseMap [x, y + 1] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x, y + 1] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (x + 1 <= mapChunkSize && y - 1 >= 0 && noiseMap [x + 1, y - 1] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x + 1, y - 1] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (x + 1 <= mapChunkSize && noiseMap [x + 1, y] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x + 1, y] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (x + 1 <= mapChunkSize && y + 1 <= mapChunkSize && noiseMap [x + 1, y + 1] != noiseMap [x, y]) {
count += 1;
if (noiseMap [x + 1, y + 1] > noiseMap [x, y]) {
dir += 1;
} else {
dir -= 1;
}
}
if (count > 4) {
if (dir > 0) {
noiseMap [x, y] += stepHeight;
}
if (dir < 0) {
noiseMap [x, y] -= stepHeight;
}
}
}
}
}
前:
后:
一个容易的事情尝试是使用源和目标阵列,让你产生每平滑值仅源自未平滑的值,并且永远不会来自已经平滑的值。 – mcdowella