2012-11-24 68 views
2

我的佩林噪音生成过程中再次出现了一些问题。我已阅读了多篇关于它如何工作的文章。我用下面的代码来实现它:佩林噪音不起作用

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噪声。我刚刚得到这个:

Definitely not Perlin Noise..

这是更blury,混纺噪音比什么都重要。即使添加更多八度和/或增加持续时间,我也会得到非常相似的结果。我使用this文章作为构建代码的参考(也是this之一)。所以如果任何人有任何想法,为什么这不起作用,请评论/回答。谢谢!

+2

为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 –

+0

不,我想我会等待回应。虽然 – CoderTheTyler

+0

或者希望得到回复:D – CoderTheTyler

回答

0

我遇到了这个问题,对于那些刚开始使用Perlin噪声的人来说,这是相当常见的。我相信发生的事情是,你正在采样佩林噪声在相距太远的点上。尝试乘以0.1你的f,看看是否有帮助。另外,首先尝试使用一个八度,它会帮助您调试。

+0

听起来不错。谢谢 – CoderTheTyler

+0

好吗?那有用吗? –