使Perlin产生尖锐的边缘噪音

时间:2017-03-16 00:39:33

标签: java game-physics perlin-noise

嗨所以我使用我发现的算法产生perlin噪音。我想要做的是创建更清晰的边缘,使用更少的曲线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比例的内容。任何想法/帮助减少噪音?

1 个答案:

答案 0 :(得分: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

    }
}

如果看起来不错,我不知道,这只是我在添加答案时想到的变化。