我的 Perlin Noise 生成器(再次)有点问题。我已经阅读了多篇关于它如何工作的文章。我用以下代码实现了它:
package PerlinNoise;
import java.util.Random;
public class PerlinNoise {
private long seed;
private Random rand;
private float f;
public PerlinNoise(long seed, float f) {
this.seed = seed;
this.f = f;
rand = new Random();
}
//interpolates generated noise
public float getInterpolatedNoise(float x, float y) {
float a = (int) Math.floor((double) x / f);
float A = a + 1;
float b = (int) Math.floor((double) y / f); //<-- define the points around the point
float B = b + 1;
return cosineInterpolate(
cosineInterpolate((float) getNoise(a, b), (float) getNoise(A, b), (float) (x - a * f) / f),
cosineInterpolate((float) getNoise(a, B), (float) getNoise(A, B), (float) (x - a * f) / f),
(float) (y - b * f) / f); //<-- interpolates everything
}
//cosine interpolation
private float cosineInterpolate(float a, float b, float x) {
float f = (float) ((1f - Math.cos(x * Math.PI)) * .5f);
return a * (1f - f) + b * f;
}
//generates random noise value between -1.0 and 1.0
private float getNoise(float x, float y) {
if(y < 0) {
rand.setSeed((long) (332423 * (Math.sin(Math.cos(x) * x) + Math.cos(Math.sin(y) * y) + Math.tan(seed))));
}else{
rand.setSeed((long) (432423 * (Math.sin(x) + Math.cos(y) + Math.tan(seed))));
}
float n = (float)(((float)rand.nextInt(255) - (float)rand.nextInt(255)) / 255.0f);
return n;
}
这是我将所有八度音阶加在一起的时候:
//performs perlin noise function
public static float[][] getPerlinNoise(int octaves, long seed) {
float[][] noise = new float[Main.csX][Main.csY];
for(int z = 0; z < octaves; z++) {
float f = (float)Math.pow(2, z);
PerlinNoise oct = new PerlinNoise(seed, f);
for(int y = 0; y < Main.csY; y++) {
for(int x = 0; x < Main.csX; x++) {
noise[x][y] = (noise[x][y] + oct.getInterpolatedNoise(x * f, y * f)) * (float)Math.pow(p, z); //<- pumps out numbers between -1.0 and 1.0
}
}
}
return noise;
}
抱歉那里有巨大的代码转储。当我运行它时,代码一切正常,但我没有得到 Perlin 噪音。我只是得到这个:
这比其他任何东西都更模糊,混合的噪音。即使我添加更多八度音阶和/或增加持久性,我也会得到非常相似的结果。我使用这篇文章作为构建代码的参考(也是这篇文章)。因此,如果有人对为什么这不起作用有任何想法,请发表评论/回答。谢谢!