2017-03-16 120 views
1

嗨,所以我使用我發現的算法產生珀林噪音。我想要做的是用更少的曲線創建更銳利的邊緣Picture使佩林噪音與鋒利的邊緣

private static final double F2 = 0.5*(Math.sqrt(3.0)-1.0); 
public float[][] generateSimplexNoise(int w, int h, double fre){ 
    float [][]n = new float[w][h]; 
    double f = fre /(float)w; 
    for(int i = 0; i < w ; i++){ 
     for(int j = 0; j < h ; j++){ 
      n[i][j] = (float) noise(i*f,j*f); 
      n[i][j] = (n[i][j]+1)/2; //CONVERTS TO 0-1 SCALE 

     } 
    } 

    return n; 
} 

// 2D simplex noise 
public double noise(double xin, double yin) { 
    double n0, n1, n2; // Noise contributions from the three corners 
    // Skew the input space to determine which simplex cell we're in 
    double s = (xin+yin)*F2; // Hairy factor for 2D 
    int i = fastfloor(xin+s); 
    int j = fastfloor(yin+s); 
    double t = (i+j)*G2; 
    double X0 = i-t; // Unskew the cell origin back to (x,y) space 
    double Y0 = j-t; 
    double x0 = xin-X0; // The x,y distances from the cell origin 
    double y0 = yin-Y0; 
    // For the 2D case, the simplex shape is an equilateral triangle. 
    // Determine which simplex we are in. 
    int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords 
    if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1) 
    else {i1=0; j1=1;}  // upper triangle, YX order: (0,0)->(0,1)->(1,1) 
    // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and 
    // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where 
    // c = (3-sqrt(3))/6 
    double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords 
    double y1 = y0 - j1 + G2; 
    double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords 
    double y2 = y0 - 1.0 + 2.0 * G2; 
    // Work out the hashed gradient indices of the three simplex corners 
    int ii = i & 255; 
    int jj = j & 255; 
    int gi0 = permMod12[ii+perm[jj]]; 
    int gi1 = permMod12[ii+i1+perm[jj+j1]]; 
    int gi2 = permMod12[ii+1+perm[jj+1]]; 
    // Calculate the contribution from the three corners 
    double t0 = 0.5 - x0*x0-y0*y0; 
    if(t0<0) n0 = 0.0; 
    else { 
     t0 *= t0; 
     n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient 
    } 
    double t1 = 0.5 - x1*x1-y1*y1; 
    if(t1<0) n1 = 0.0; 
    else { 
     t1 *= t1; 
     n1 = t1 * t1 * dot(grad3[gi1], x1, y1); 
    } 
    double t2 = 0.5 - x2*x2-y2*y2; 
    if(t2<0) n2 = 0.0; 
    else { 
     t2 *= t2; 
     n2 = t2 * t2 * dot(grad3[gi2], x2, y2); 
    } 
    // Add contributions from each corner to get the final noise value. 
    // The result is scaled to return values in the interval [-1,1]. 
    return 65.0 * (n0 + n1 + n2); 
} 

噪聲低於.25將塊分類爲磚,並將任何高於.25的塊分類爲草。

以上是我用來創建噪聲並將其轉換爲0-1的比例。任何想法/幫助減少噪音的曲線?

+0

從標籤維基:_Perlin噪音是程序生成**梯度**噪音_(強調我的)。漸變是讓外觀流暢的原因,我認爲很難修改它以獲得像塊一樣的外觀。 – pingul

回答

0

查看image您鏈接到的問題最簡單的解決方案是以較低的分辨率進行採樣並使用閾值過濾器來創建所需的對比度。

double squareness = 50.0; // size of blocks values > 1 and in pixels 
double threshold = 4; // number of levels 
double pixVal; 
// then inside the sampling loop 
for(int i = 0; i < w ; i++){ 
    for(int j = 0; j < h ; j++){ 
     pixVal = noise(Math.floor(i/squareness) * squareness * f, 
         Math.floor(j/squareness) * squareness * f); 
     pixVal = pixVal/2.0 + 0.5; // normalize 
     pixVal = Math.floor(pixVal * threshold)/thresholds; 
     n[i][j] = (float) pixVal; // put in array 

    } 
} 

您可能希望使用您用來修改方形第二噪音功能,減少邊界

double sqrMin = 50.0; // min 
double sqrMax = 150.0; // max 
double thresholdSq = 4; 
double threshold = 4; 
double pixVal; 
double square; 
double scale = 1.0/3.0; 
// then inside the sampling loop 
for(int i = 0; i < w ; i++){ 
    for(int j = 0; j < h ; j++){ 
     square = noise(i * f * scale, i * j * scale)/2.0 + 0.5; 
     square = Math.floor(square * thresholdSq)/thresholdSq; 
     square = square * (sqrMax - sqrMin) + sqrMin; 
     pixVal = noise(Math.floor(i/square) * square * f, 
         Math.floor(j/square) * square * f); 
     pixVal = pixVal/2.0 + 0.5; // normalize 
     pixVal = Math.floor(pixVal * threshold)/thresholds; 
     n[i][j] = (float) pixVal; // put in array 

    } 
} 

如果它看起來不錯,我不知道的規律,它只是一個變化我想到在添加答案時。