eco*_*utu 37 terrain perlin-noise
我正在尝试实现我在网上找到的一些源代码,使用Perlin Noise生成高度图.我已成功设法使用noise3函数获取高度图,第三个坐标是随机"种子",以允许随机高度贴图.
我的问题是产生的地形相当沉闷 - 我想要山脉,我正在滚动草原.我已经完成了对Perlin Noise的阅读(主要是在这里).由于源代码我发现显然没有考虑到可读性和我对Perlin Noise概念的一般掌握,我无法弄清楚我需要在代码中调整什么(幅度和频率?)到创造更加激烈的地形.
有关使用Perlin Noise,Perlin Noise,甚至更多可解码代码生成高度贴图的更多信息也将受到欢迎.
编辑:我理解(有点)Perlin Noise的工作原理,例如关于振幅和频率,我只是想知道在上面链接的代码中要改变哪些变量,这些变量用于这两个方面.
Nic*_*nks 55
Perlin噪声完全由您设置的不同变量控制,即振幅,频率和持久性.八度音程有一点变化,但不多.在我过去编写的代码中,我刚刚使用了频率和持续时间的数量级,直到我得到了我需要的东西.如果需要,我可以尝试找到我的旧资料.
#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;
};
Run Code Online (Sandbox Code Playgroud)
#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);
}
Run Code Online (Sandbox Code Playgroud)
Phi*_*ner 20
一位朋友刚刚把我与这个问题联系起来,我想我会尝试清理一些在接受的答案中没有解决的问题.
Elias的有趣且有用的文章使用"价值噪音"而不是"Perlin噪音".值噪声涉及随机点的曲线拟合.梯度噪声(其中Perlin噪声是一个主要示例)创建一个0值点的点阵,并为每个点提供随机梯度.他们经常互相混淆!
http://en.wikipedia.org/wiki/Gradient_noise
其次,使用第三个值作为种子是昂贵的.如果您想要随机地形,请考虑将您的原点翻译为随机数量.3D通话比2D通话更昂贵.而您所做的就是使用z值来选择特定的二维噪声切片.
第三,直接函数调用将返回相当平滑且整体滚动的值,而不是像真实地形那样崎岖,因为它的随机性仅限于单个频率.为了获得更加崎岖的地形,一种好的技术是将在不同频率上通过噪声空间前进的多个呼叫相加在一起,通常设置"分形"值.
因此,例如,总结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]范围内,这可以通过渐进式加权来完成,具体取决于多少'octaves'你用.(但我不知道这是否真的是最有效的方法.)
四个八度音程示例: (1/15)(noise(x, y) + (2/15)(noise(2x, 2y) + (4/15)(noise(4x, 4y) + (8/15)(noise(8x, 8y)
然后,使用"湍流噪声"归一化取和和制作它= |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做得非常好!