2017-02-25 163 views
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; 
         } 
        } 
       } 
      } 
     } 

前:

enter image description here

后:

enter image description here

+1

一个容易的事情尝试是使用源和目标阵列,让你产生每平滑值仅源自未平滑的值,并且永远不会来自已经平滑的值。 – mcdowella

回答

0

该代码看起来像一场噩梦调试。话虽如此,我已经看到过像这样的图像文物,它看起来像mcdowella所说的那样:你正在分配2D阵列,同时读取它,从而导致你的检查不准确。虽然很难破译。

我确定你可以通过简单地增加单元格邻居的平均值来获得相同的行为。下面的例子应该有希望做的伎俩(其未经检验,但希望传达的想法):

//Assumes all height values in the 2D array are normalized between 0-1 
float[,] RampAverage(float[,] values, int rampSteps){ 
    int valuesWidth = values.GetLength(0); 
    int valuesHeight = values.GetLength(1); 
    float[,] tempValues = new float[valuesWidth,valuesHeight]; 

    for (int x = 0; x < valuesWidth; x ++){ 
     for(int y = 0; y < valuesHeight; y++){ 
      Vector2 key = new Vector2(x,y); 
      float neighborAverage = GetNeighborAverage(values,key); 
      tempValues[x,y] = Ramp(neighborAverage,rampSteps); 
     } 
    } 

    return tempValues; 
} 

// Assumes all values are between 0-1 
float Ramp(float value, int rampSteps){ 
    float step = 1/rampSteps; 
    int d = Mathf.FloorToInt(value/step); 
    return d*step; 
} 

float GetNeighborAverage(float[,] values, Vector2 key){ 
    int contributors = 0; 
    float value = 0; 

    int maxX = values.GetLength(0)-1; 
    int maxY = valuse.GetLength(1)-1; 

    for (int y = -1; y <= 1; y++) 
    { 
     for (int x = -1; x <= 1; x++) 
     { 
      int xIndex = x + (int)key.x; 
      int yIndex = y + (int)key.y; 
      if (xIndex >= 0 && yIndex >= 0 && xIndex <= maxX-1 && yIndex <= maxY-1) 
      { 
       value += values[xIndex,yIndex]; 
       contributors ++; 
      } 
     } 
    } 
    return value/contributors; 
}