我的佩林噪音生成過程中再次出現了一些問題。我已閱讀了多篇關於它如何工作的文章。我用下面的代碼來實現它:佩林噪音不起作用
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噪聲。我剛剛得到這個:
這是更blury,混紡噪音比什麼都重要。即使添加更多八度和/或增加持續時間,我也會得到非常相似的結果。我使用this文章作爲構建代碼的參考(也是this之一)。所以如果任何人有任何想法,爲什麼這不起作用,請評論/回答。謝謝!
爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –
不,我想我會等待迴應。雖然 – CoderTheTyler
或者希望得到回覆:D – CoderTheTyler