gpt4 book ai didi

java - 用锋利的边缘发出 Perlin 噪音

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:39 25 4
gpt4 key购买 nike

嗨,我正在使用我发现的算法来生成柏林噪声。我想做的是用更少的曲线创建更锐利的边缘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);
}

噪声低于 0.25 时,该 block 将被分类为砖 block ,任何高于 0.25 的 block 都会被分类为草。

上面是我用来创建噪音并将其转换为 0-1 等级的方法。有什么想法/帮助减少噪音的曲线吗?

最佳答案

查看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

}
}

我不知道它是否看起来不错,这只是我在添加答案时想到的一个变体。

关于java - 用锋利的边缘发出 Perlin 噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42823308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com