地形的Perlin噪声生成

时间:2011-01-20 21:52:14

标签: terrain perlin-noise

我正在尝试实现一些我发现online的源代码,以使用Perlin Noise生成高度图。我已成功设法使用noise3函数获取高度图,第三个坐标是随机“种子”,以允许随机高度贴图。

我的问题是产生的地形相当沉闷 - 我想要山脉,我正在滚动草原。我已经完成了一些关于Perlin Noise的阅读(大多数是here)。由于源代码我发现显然没有考虑到可读性和我对Perlin Noise概念的一般掌握,我无法弄清楚我需要在代码中调整什么(幅度和频率?)到创造更加激烈的地形。

有关使用Perlin Noise生成高度贴图的更多信息,一般的Perlin Noise,甚至更难以解密的代码也是受欢迎的。

编辑:我理解(有点)Perlin Noise是如何工作的,例如,关于幅度和频率,我只是想知道在上面链接的代码中要改变哪些变量,哪些是用于这两个方面。

4 个答案:

答案 0 :(得分:55)

Perlin噪声完全由您设置的不同变量控制,即幅度,频率和持久性。八度音程有一点变化,但不多。在我过去编写的代码中,我刚刚使用了频率和持续时间的数量级,直到我得到了我需要的东西。如果需要,我可以尝试找到我的旧资料。

PerlinNoise.h

#pragma once

class PerlinNoise
{
public:

  // Constructor
    PerlinNoise();
    PerlinNoise(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed);

  // Get Height
    double GetHeight(double x, double y) const;

  // Get
  double Persistence() const { return persistence; }
  double Frequency()   const { return frequency;   }
  double Amplitude()   const { return amplitude;   }
  int    Octaves()     const { return octaves;     }
  int    RandomSeed()  const { return randomseed;  }

  // Set
  void Set(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed);

  void SetPersistence(double _persistence) { persistence = _persistence; }
  void SetFrequency(  double _frequency)   { frequency = _frequency;     }
  void SetAmplitude(  double _amplitude)   { amplitude = _amplitude;     }
  void SetOctaves(    int    _octaves)     { octaves = _octaves;         }
  void SetRandomSeed( int    _randomseed)  { randomseed = _randomseed;   }

private:

    double Total(double i, double j) const;
    double GetValue(double x, double y) const;
    double Interpolate(double x, double y, double a) const;
    double Noise(int x, int y) const;

    double persistence, frequency, amplitude;
    int octaves, randomseed;
};

PerlinNoise.cpp

#include "PerlinNoise.h"

PerlinNoise::PerlinNoise()
{
  persistence = 0;
  frequency = 0;
  amplitude  = 0;
  octaves = 0;
  randomseed = 0;
}

PerlinNoise::PerlinNoise(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed)
{
  persistence = _persistence;
  frequency = _frequency;
  amplitude  = _amplitude;
  octaves = _octaves;
  randomseed = 2 + _randomseed * _randomseed;
}

void PerlinNoise::Set(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed)
{
  persistence = _persistence;
  frequency = _frequency;
  amplitude  = _amplitude;
  octaves = _octaves;
  randomseed = 2 + _randomseed * _randomseed;
}

double PerlinNoise::GetHeight(double x, double y) const
{
  return amplitude * Total(x, y);
}

double PerlinNoise::Total(double i, double j) const
{
    //properties of one octave (changing each loop)
    double t = 0.0f;
    double _amplitude = 1;
    double freq = frequency;

    for(int k = 0; k < octaves; k++) 
    {
        t += GetValue(j * freq + randomseed, i * freq + randomseed) * _amplitude;
        _amplitude *= persistence;
        freq *= 2;
    }

    return t;
}

double PerlinNoise::GetValue(double x, double y) const
{
    int Xint = (int)x;
    int Yint = (int)y;
    double Xfrac = x - Xint;
    double Yfrac = y - Yint;

  //noise values
  double n01 = Noise(Xint-1, Yint-1);
  double n02 = Noise(Xint+1, Yint-1);
  double n03 = Noise(Xint-1, Yint+1);
  double n04 = Noise(Xint+1, Yint+1);
  double n05 = Noise(Xint-1, Yint);
  double n06 = Noise(Xint+1, Yint);
  double n07 = Noise(Xint, Yint-1);
  double n08 = Noise(Xint, Yint+1);
  double n09 = Noise(Xint, Yint);

  double n12 = Noise(Xint+2, Yint-1);
  double n14 = Noise(Xint+2, Yint+1);
  double n16 = Noise(Xint+2, Yint);

  double n23 = Noise(Xint-1, Yint+2);
  double n24 = Noise(Xint+1, Yint+2);
  double n28 = Noise(Xint, Yint+2);

  double n34 = Noise(Xint+2, Yint+2);

    //find the noise values of the four corners
    double x0y0 = 0.0625*(n01+n02+n03+n04) + 0.125*(n05+n06+n07+n08) + 0.25*(n09);  
    double x1y0 = 0.0625*(n07+n12+n08+n14) + 0.125*(n09+n16+n02+n04) + 0.25*(n06);  
    double x0y1 = 0.0625*(n05+n06+n23+n24) + 0.125*(n03+n04+n09+n28) + 0.25*(n08);  
    double x1y1 = 0.0625*(n09+n16+n28+n34) + 0.125*(n08+n14+n06+n24) + 0.25*(n04);  

    //interpolate between those values according to the x and y fractions
    double v1 = Interpolate(x0y0, x1y0, Xfrac); //interpolate in x direction (y)
    double v2 = Interpolate(x0y1, x1y1, Xfrac); //interpolate in x direction (y+1)
    double fin = Interpolate(v1, v2, Yfrac);  //interpolate in y direction

    return fin;
}

double PerlinNoise::Interpolate(double x, double y, double a) const
{
    double negA = 1.0 - a;
  double negASqr = negA * negA;
    double fac1 = 3.0 * (negASqr) - 2.0 * (negASqr * negA);
  double aSqr = a * a;
    double fac2 = 3.0 * aSqr - 2.0 * (aSqr * a);

    return x * fac1 + y * fac2; //add the weighted factors
}

double PerlinNoise::Noise(int x, int y) const
{
    int n = x + y * 57;
    n = (n << 13) ^ n;
  int t = (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff;
    return 1.0 - double(t) * 0.931322574615478515625e-9;/// 1073741824.0);
}

答案 1 :(得分:20)

一位朋友刚刚把我与这个问题联系起来,我想我会尝试清理一些在接受的答案中没有解决的事情。

利亚&#39;有趣且有用的文章使用&#34;重视噪音&#34;不是&#34;柏林噪音&#34;。值噪声涉及随机点的曲线拟合。梯度噪声(其中Perlin噪声是一个主要示例)创建一个0值点的点阵,并为每个点提供随机梯度。他们经常互相混淆!

http://en.wikipedia.org/wiki/Gradient_noise

其次,使用第三个值作为种子是昂贵的。如果您想要随机地形,请考虑将您的原点翻译为随机数量。 3D通话比2D通话更昂贵。而您所做的就是使用z值来选择特定的二维噪声切片。

第三,直接函数调用将返回相当平滑且整体滚动的值,而不是像真实地形那样崎岖,因为它的随机性仅限于单个频率。为了获得更加崎岖的地形,一种好的技术是将在不同频率上通过噪声空间进行的多个呼叫相加在一起,通常设置一个分形&#34;值。

因此,例如,将noise(x, y) + (1/2)(noise(x*2, y*2) + (1/4)(noise(x*4, y*4) ...

加在一起

结果总和可能经常在-1到1的范围之外,因此您必须在值有用之前对结果进行标准化。我建议设置因子系列(1,1 / 2,1 / 4等),以确保您保持在[-1,1]范围内,这可以通过渐进式加权来完成,具体取决于有多少&#39; octaves&#39;你用。 (但我不知道这是否真的是最有效的方法。)

四个八度音程示例:(1/15)(noise(x, y) + (2/15)(noise(2x, 2y) + (4/15)(noise(4x, 4y) + (8/15)(noise(8x, 8y)

然后,使用&#34;湍流噪音&#34;取值和归一化的归一化= |sum| (即,使用abs功能)。这将使地形具有明确的角谷脊,而不是平滑地滚动。

我正在使用SimplexNoise可视化工具,希望最终在GitHub上开源,作为Java项目。可以通过java-gaming.org上的这篇文章找到并运行可视化器的初稿: http://www.java-gaming.org/topics/simplex-noise-experiments-towards-procedural-generation/27163/view.html 对初稿的重点是更多教程,使用生成的代码示例(但它们是Java)。

关于SimplexNoise如何工作的伟大文章(以及Perlin vs Gradient背景): http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf

Stefan Gustavson做得非常好!

答案 2 :(得分:5)

振幅控制地形的高低,流动频率,低频率流动。

因此,如果你想要一个锯齿状的山地景观,你需要两者兼得。

答案 3 :(得分:2)

这是我刚才在JavaScript中使用3D Perlin Noise编写的表面生成示例。由于表面体素存在或不存在,我只需在计算Perlin Noise立方体后应用阈值。在该示例中,噪声概率对于所有维度是相等的。当您将随机值增加到地面并将其减少到天空时,您可以获得更逼真的景观。

http://kirox.de/test/Surface.html

必须启用WebGL。在撰写本文时,我建议使用Chrome以获得最佳性能。