Perlin的地形噪声生成

前端之家收集整理的这篇文章主要介绍了Perlin的地形噪声生成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图实现一些源代码我发现 online生成高度地图使用Perlin噪声。我已经成功地设法获得高度地图,使用noise3函数,第三个坐标是随机的“种子”,以允许随机高度图。

我的问题是,生成的地形相当暗,我想要山,我正在滚动草原。我已经做了一些阅读Perlin噪声(大多是here)。由于源代码,我发现显然不是写在可读性和我的弱掌握一般的Perlin噪声的概念,我不知道我需要调整在代码(幅度和频率?)创造更为激烈的地形。

一些更多的信息生成高度地图使用Perlin噪声,Perlin噪声一般甚至一些更加可译码的代码也将受到欢迎。

编辑:我理解(kindof)如何Perlin噪声工作,例如:关于幅度和频率,我只是想知道什么变量在上面链接代码用于这两个方面。

解决方法

Perlin噪声完全由您设置的不同变量控制,即幅度,频率和余辉。八度音量有一点变化,但不大。在我以前写的代码中,我刚刚玩了频率和持续性的数量级,直到我得到了我需要的。如果需要,我可以尝试找到我的旧源。

PerlinNoise.h

  1. #pragma once
  2.  
  3. class PerlinNoise
  4. {
  5. public:
  6.  
  7. // Constructor
  8. PerlinNoise();
  9. PerlinNoise(double _persistence,double _frequency,double _amplitude,int _octaves,int _randomseed);
  10.  
  11. // Get Height
  12. double GetHeight(double x,double y) const;
  13.  
  14. // Get
  15. double Persistence() const { return persistence; }
  16. double Frequency() const { return frequency; }
  17. double Amplitude() const { return amplitude; }
  18. int Octaves() const { return octaves; }
  19. int RandomSeed() const { return randomseed; }
  20.  
  21. // Set
  22. void Set(double _persistence,int _randomseed);
  23.  
  24. void SetPersistence(double _persistence) { persistence = _persistence; }
  25. void SetFrequency( double _frequency) { frequency = _frequency; }
  26. void SetAmplitude( double _amplitude) { amplitude = _amplitude; }
  27. void SetOctaves( int _octaves) { octaves = _octaves; }
  28. void SetRandomSeed( int _randomseed) { randomseed = _randomseed; }
  29.  
  30. private:
  31.  
  32. double Total(double i,double j) const;
  33. double GetValue(double x,double y) const;
  34. double Interpolate(double x,double y,double a) const;
  35. double Noise(int x,int y) const;
  36.  
  37. double persistence,frequency,amplitude;
  38. int octaves,randomseed;
  39. };

PerlinNoise.cpp

  1. #include "PerlinNoise.h"
  2.  
  3. PerlinNoise::PerlinNoise()
  4. {
  5. persistence = 0;
  6. frequency = 0;
  7. amplitude = 0;
  8. octaves = 0;
  9. randomseed = 0;
  10. }
  11.  
  12. PerlinNoise::PerlinNoise(double _persistence,int _randomseed)
  13. {
  14. persistence = _persistence;
  15. frequency = _frequency;
  16. amplitude = _amplitude;
  17. octaves = _octaves;
  18. randomseed = 2 + _randomseed * _randomseed;
  19. }
  20.  
  21. void PerlinNoise::Set(double _persistence,int _randomseed)
  22. {
  23. persistence = _persistence;
  24. frequency = _frequency;
  25. amplitude = _amplitude;
  26. octaves = _octaves;
  27. randomseed = 2 + _randomseed * _randomseed;
  28. }
  29.  
  30. double PerlinNoise::GetHeight(double x,double y) const
  31. {
  32. return amplitude * Total(x,y);
  33. }
  34.  
  35. double PerlinNoise::Total(double i,double j) const
  36. {
  37. //properties of one octave (changing each loop)
  38. double t = 0.0f;
  39. double _amplitude = 1;
  40. double freq = frequency;
  41.  
  42. for(int k = 0; k < octaves; k++)
  43. {
  44. t += GetValue(j * freq + randomseed,i * freq + randomseed) * _amplitude;
  45. _amplitude *= persistence;
  46. freq *= 2;
  47. }
  48.  
  49. return t;
  50. }
  51.  
  52. double PerlinNoise::GetValue(double x,double y) const
  53. {
  54. int Xint = (int)x;
  55. int Yint = (int)y;
  56. double Xfrac = x - Xint;
  57. double Yfrac = y - Yint;
  58.  
  59. //noise values
  60. double n01 = Noise(Xint-1,Yint-1);
  61. double n02 = Noise(Xint+1,Yint-1);
  62. double n03 = Noise(Xint-1,Yint+1);
  63. double n04 = Noise(Xint+1,Yint+1);
  64. double n05 = Noise(Xint-1,Yint);
  65. double n06 = Noise(Xint+1,Yint);
  66. double n07 = Noise(Xint,Yint-1);
  67. double n08 = Noise(Xint,Yint+1);
  68. double n09 = Noise(Xint,Yint);
  69.  
  70. double n12 = Noise(Xint+2,Yint-1);
  71. double n14 = Noise(Xint+2,Yint+1);
  72. double n16 = Noise(Xint+2,Yint);
  73.  
  74. double n23 = Noise(Xint-1,Yint+2);
  75. double n24 = Noise(Xint+1,Yint+2);
  76. double n28 = Noise(Xint,Yint+2);
  77.  
  78. double n34 = Noise(Xint+2,Yint+2);
  79.  
  80. //find the noise values of the four corners
  81. double x0y0 = 0.0625*(n01+n02+n03+n04) + 0.125*(n05+n06+n07+n08) + 0.25*(n09);
  82. double x1y0 = 0.0625*(n07+n12+n08+n14) + 0.125*(n09+n16+n02+n04) + 0.25*(n06);
  83. double x0y1 = 0.0625*(n05+n06+n23+n24) + 0.125*(n03+n04+n09+n28) + 0.25*(n08);
  84. double x1y1 = 0.0625*(n09+n16+n28+n34) + 0.125*(n08+n14+n06+n24) + 0.25*(n04);
  85.  
  86. //interpolate between those values according to the x and y fractions
  87. double v1 = Interpolate(x0y0,x1y0,Xfrac); //interpolate in x direction (y)
  88. double v2 = Interpolate(x0y1,x1y1,Xfrac); //interpolate in x direction (y+1)
  89. double fin = Interpolate(v1,v2,Yfrac); //interpolate in y direction
  90.  
  91. return fin;
  92. }
  93.  
  94. double PerlinNoise::Interpolate(double x,double a) const
  95. {
  96. double negA = 1.0 - a;
  97. double negASqr = negA * negA;
  98. double fac1 = 3.0 * (negASqr) - 2.0 * (negASqr * negA);
  99. double aSqr = a * a;
  100. double fac2 = 3.0 * aSqr - 2.0 * (aSqr * a);
  101.  
  102. return x * fac1 + y * fac2; //add the weighted factors
  103. }
  104.  
  105. double PerlinNoise::Noise(int x,int y) const
  106. {
  107. int n = x + y * 57;
  108. n = (n << 13) ^ n;
  109. int t = (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff;
  110. return 1.0 - double(t) * 0.931322574615478515625e-9;/// 1073741824.0);
  111. }

猜你在找的Perl相关文章