【问题标题】:Generate points from 2D fractal noise从 2D 分形噪声生成点
【发布时间】:2018-10-07 00:34:22
【问题描述】:

我使用来自https://www.redblobgames.com/maps/terrain-from-noise/#trees 的方法使用分层 Perlin 噪声来生成 2D 点。但是,我希望该点的坐标不是ints,而是floats。为此,我实现了一个resolution 变量:

List<Vector2> points = new List<Vector2> ();
int res = 10;
int R = 2;
for (int yc = 0; yc < height*res; yc++) {
    for (int xc = 0; xc < width*res; xc++) {
        float max = 0;
        for (int yn = yc - R; yn <= yc + R; yn++) {
            for (int xn = xc - R; xn <= xc + R; xn++) {
                float e = pointsMap.GetClampedValue((float)xn/res, (float)yn/res);
                if (e > max) { max = e; }
            }
        }
        if (pointsMap.GetClampedValue((float)xc/res, (float)yc/res) == max) {
            points.Add (new Vector2((float)xc/res, (float)yc/res));
        }
    }
}

pointsMap.GetClampedValue() 从 0 到 1 返回一个 float

宽度和高度为 100,分辨率为 10,R = 2,总共执行了 2500 万次for 循环。虽然循环中的代码远未优化,但代码在上述设置的合理时间内执行。但是,随着分辨率、宽度、高度和R 的增加,执行时间会增加太多。

在原函数中,作者留言说有比这更高效的算法。有谁知道一种高效、快速的算法,并且在哪里可以实现这种解决方案?

【问题讨论】:

  • 这可能是相关的:stackoverflow.com/questions/10159318/…,不确定但看看。
  • 您的代码还有分辨率^2 的工作要做,因此即使分辨率或大小略有增加,它的运行速度也会慢几个数量级。了解您使用积分的目的会有所帮助 - 我可以想到一个简单的替代方案,但它不适合某些应用程序
  • @Pikalek 我在程序地形生成器中使用这些点来放置对象。我尝试过使用泊松磁盘采样,但我需要算法在每次访问某个区域时返回相同的点,如果这有意义的话 - 因此我使用了噪声图
  • @Jonan 不,这对我来说没有意义。与 Perlin 噪声一样,如果您对某个区域使用相同的赎金种子,您将获得该区域相同的泊松盘采样。如果你仍然想坚持 Perlin 噪音,我会列出可能有效的修改。

标签: c# unity3d optimization noise procedural-generation


【解决方案1】:

如果你想坚持使用与你链接的教程接近的东西,但你需要浮点坐标而不是十进制坐标,最简单的解决方案是简单地在 [0,1) 范围内添加一个随机值:

Random rng = new Random(regionSeed);
List<Vector2> points = new List<Vector2> ();
for (int yc = 0; yc < height; yc++) {
  for (int xc = 0; xc < width; xc++) {
    double max = 0;
    // there are more efficient algorithms than this
    for (int yn = yc - R; yn <= yc + R; yn++) {
      for (int xn = xc - R; xn <= xc + R; xn++) {
        double e = value[yn][xn];
        if (e > max) { max = e; }
      }
    }
    if (value[yc][xc] == max) {
      list.add( new Vector2(xc+rng.NextDouble(),yc+rng.NextDouble()) );
    }
  }
}

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2014-02-08
    • 1970-01-01
    • 2022-01-02
    • 2017-02-01
    • 2011-06-12
    • 2021-12-15
    • 2013-06-30
    • 1970-01-01
    相关资源
    最近更新 更多