0

我正在使用单纯形噪声制作高度图。我在调整它以稍微倾向于形成一个岛屿时遇到了问题。在实际渲染生物群落和其他特征之前,我正在努力让这些值正确。我遇到了一个问题,即我的代码应该能够产生这种在中间形成孤岛的趋势,但它似乎只在一个方向上起作用。

如果有什么特别的原因?我处理地形平滑的课程在 x 和 y 方向上做同样的事情,但只有一个有效。

public class MapGenerator{

public double[][] toRender;

int maxHeight = 300;

public MapGenerator() {

    int xResolution = 200;
    int yResolution = 200;

    double[][] result = new double[xResolution][yResolution];

    for (int x = 0; x < xResolution; x++){
        for (int y = 0; y < yResolution; y++){
            result[x][y] = transformPoint(x, y, xResolution, yResolution, SimplexNoise.noise(x, y));
        }
    }

    toRender = result;

}

private double transformPoint(int x, int y, int xSize, int ySize, double point){
    System.out.println();
    System.out.println(point);

    point += 20 * Math.sin(x * Math.PI / xSize);

    point += 20 * Math.sin(y * Math.PI / ySize);

    System.out.println(point);

    return point;
}

}

白噪声的图像:

使用 X 和 Y:

只有 X(Y 被注释掉):

[只有Y(X被注释掉):](类似于X和Y,由于声誉问题,无法发布链接。)

[没有X和Y:](类似于只有X,因为声誉问题不能发布链接。)

4

1 回答 1

1

我不是 100% 你的错误在哪里。它可能在你的日常噪音中。为了调试它,我删除了对 的调用noise并替换为常数值 0.5。然后我得到了其余的工作,所以我在图像的中心看到了一个白色的光晕。然后我添加了noise回电。(注意我在SimplexNoise这里使用我自己的。)

因此,问题出在您的范围内(Math.min( 1.0, point ))或在您的图形显示中(您没有显示)或在您的SimplexNoise(您也没有显示)中。

import SimpleUtils.noise.SimplexNoise;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Brenden Towey
 */
public class MapGenerator
{

   public static void main( String[] args )
   {
      SwingUtilities.invokeLater( new Runnable()
      {
         public void run()
         {
            JFrame frame = new JFrame();

            frame.add( new JLabel( new ImageIcon( new MapGenerator().toImage() )));

            frame.pack();
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
         }
      } );

   }
   public double[][] toRender;

   int maxHeight = 300;
   int xResolution = 200;
   int yResolution = 200;

   public MapGenerator()
   {
      double[][] result = new double[ xResolution ][ yResolution ];
      SimplexNoise noise = new SimplexNoise();
      for( int x = 0; x < xResolution; x++ )
         for( int y = 0; y < yResolution; y++ )
            result[x][y] = transformPoint( x, y, noise.noise(x, y) );

      toRender = result;

   }

   private double transformPoint( int x, int y, double point )
   {
      point += 2 * Math.sin( x * Math.PI / xResolution )/2.0;
      point += 2 * Math.sin( y * Math.PI / yResolution )/2.0;
      return Math.min( 1.0, point);
   }

   public Image toImage()
   {
      BufferedImage image = new BufferedImage( xResolution,
              yResolution, BufferedImage.TYPE_INT_RGB );
      for( int x = 0; x < xResolution; x++ )
         for( int y = 0; y < yResolution; y++ )
            image.setRGB( x, y, greyScale( toRender[x][y] ) );
      return image;
   }

   private int greyScale( double toRender )
   {
      int scale = (int) ( 255 * toRender );
      return scale + (scale << 8) + (scale << 16);
   }
}

我从这段代码的输出

于 2015-05-19T02:17:44.557 回答