2013-12-13 115 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個像素寬以獲得平滑特徵。由於你的代碼每個單元只有一個像素,所以白色的「電視靜態」噪聲可能會壓倒你想看到的信號。

我打賭你引用的示例圖片庫是使用接近統一(或可能更高)的持久性參數來獲得最終圖像。