2013-12-13 152 views
0

http://image.kilho.net/?pk=1420781创建自然柏林噪声

我想使用Perlin杂创建地形。

但我总是得到以上的噪音。

http://image.kilho.net/?pk=1420774

我想是最后(第7)图像。

但我的噪声图像看起来像第4或第5图像。

这里是我的代码(JAVA)

int seed; 

public Noise() { 
    Random ran = new Random(); 
    seed = ran.nextInt(); 
} 
/** 
* Brut noise generator using pseudo-random 
*/ 
public double noise(int x,int y) 
{ 
    x=x + y * seed; 
    x=((x<<13)^x); 
    double t=(x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff; 
    return 1-t*0.000000000931322574615478515625; 

} 

/** 
* Smoothed noise generator using 9 brut noise 
*/ 
public double sNoise(int x,int y) 
{ 
    double corners = (noise(x-1, y-1)+noise(x+1, y-1)+noise(x-1, y+1)+noise(x+1, y+1)) * 0.0625; 
    double sides = (noise(x-1, y) +noise(x+1, y) +noise(x, y-1) +noise(x, y+1)) *0.125; 
    double center = noise(x, y) *0.25; 
    return corners + sides + center;   
} 

/** 
* Linear Interpolator 
* 
* @param a value 1 
* @param b value 2 
* @param x interpolator factor 
* 
* @return value interpolated from a to b using x factor by linear interpolation 
*/ 
public double lInterpoleLin(double a,double b,double x) 
{ 
    return a*(1-x) + b*x;  
} 


/** 
* Cosine Interpolator 
* 
* @param a value 1 
* @param b value 2 
* @param x interpolator factor 
* 
* @return value interpolated from a to b using x factor by cosin interpolation 
*/ 
public double lInterpoleCos(double a,double b,double x) 
{ 

    double ft = x * 3.1415927; 
    double f = (1 - Math.cos(ft)) * .5; 
    return a*(1-f) + b*f; 
} 

/** 
* Smooth noise generator with two input 2D 
* <br> 
* You may change the interpolation method : cosin , linear , cubic 
* </br> 
* @param x x parameter 
* @param y y parameter 
* 
* @return value of smoothed noise for 2d value x,y 
*/ 
public double iNoise(double x,double y) 
{ 
    int iX=(int)x; 
    int iY=(int)y; 
    double dX=x-iX; 
    double dY=y-iY; 
    double p1=sNoise(iX,iY); 
    double p2=sNoise(iX+1,iY); 
    double p3=sNoise(iX,iY+1); 
    double p4=sNoise(iX+1,iY+1); 
    double i1=lInterpoleLin(p1,p2,dX); 
    double i2=lInterpoleLin(p3,p4,dX); 
    return lInterpoleLin(i1,i2,dY); 
} 

/** 
* Perlin noise generator for two input 2D 
* 
* @param x x parameter 
* @param y y parameter 
* @param octave maximum octave/harmonic 
* @param persistence noise persitence 
* @return perlin noise value for given entry 
*/ 
public double pNoise(double x,double y,double persistence,int octave) 
{ 
    double result; 
    double amplitude=1; 
    int frequence=1; 
    result=0; 
    for(int n=0;n<octave;n++) 
    { 
     result+=iNoise(x*frequence,y*frequence)*amplitude; 
     frequence<<=1; 
     amplitude*=persistence; 
    } 
    return result; 
} 

}

回答

0

如果调用此代码与持久值小于团结你会过样品的高频率。
您正在使用的分形/ fBm倍频程求和方法是一个八度向下限制器,从最紧张的结果开始,并将其混合到更宽的特征点差中。对于地形生成器,人们通常希望获得具有一些细节的中大特征 - 我听说应该使主通道至少有5个,可能有20个像素宽以获得平滑特征。由于你的代码每个单元只有一个像素,所以白色的“电视静态”噪声可能会压倒你想看到的信号。

我打赌你引用的示例图片库是使用接近统一(或可能更高)的持久性参数来获得最终图像。