【问题标题】:Perlin Noise not working柏林噪音不起作用
【发布时间】:2023-03-04 23:21:01
【问题描述】:

我的 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 噪音。我只是得到这个:

这比其他任何东西都更像是模糊的混合噪点。即使我添加更多八度音阶和/或增加持久性,我也会得到非常相似的结果。我使用this 文章作为构建代码的参考(还有this 一个)。因此,如果有人对为什么这不起作用有任何想法,请发表评论/回答。谢谢!

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 不.. 我想我会等待回复。不过谢谢
  • 或者希望得到回应:D
  • 不是一个答案吗?一定有人对 Perlin Noise 有所了解!
  • 再次声明,为了尽快获得更好的帮助,请发布 SSCCE。

标签: java random blur noise perlin-noise


【解决方案1】:

我遇到过这个问题,这对于刚开始使用 Perlin Noise 的人来说相当普遍。我相信正在发生的事情是您在相距太远的点对柏林噪声进行采样。尝试将 f 乘以 0.1,看看是否有帮助。另外,尝试一开始只使用一个八度,它会帮助您调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 2012-06-25
    • 1970-01-01
    • 2014-07-26
    • 2012-07-22
    • 2011-11-22
    • 1970-01-01
    • 2013-07-17
    相关资源
    最近更新 更多