Perlin Noise显示单位正方形之间的线条

时间:2018-03-26 18:02:48

标签: java algorithm noise perlin-noise

如标题中所述,我的问题是我的perlin噪音在单位正方形之间显示清晰的线条而不是流畅地流动。我的代码非常基本,如下所示。它基于本网站的简单解释:http://flafla2.github.io/2014/08/09/perlinnoise.html

int grads[][] = {
            {1,0},{0,1},{-1,0},{0,-1},
            {1,1},{-1,1},{1,-1},{-1,-1}
    };
    public double perlin (double x, double y) {

        int unitX = (int)Math.floor(x) & 255; // decide unit square
        int unitY = (int)Math.floor(y) & 255; // decide unit square

        double relX = x-Math.floor(x); // relative x position
        double relY = y-Math.floor(y); // relative y position

        //System.out.print(unitX + " " + unitY + " ");

        // bad pseudorandom gradient
        int units = unitX+unitY;
        //System.out.println(units + " added ");
        int[] gradTL = grads[(units)%8];
        int[] gradTR = grads[(units+1)%8];
        int[] gradBL = grads[(units+1)%8];
        int[] gradBR = grads[(units+2)%8];

        // distance from edges to point, relative x and y inside the unit square
        double[] vecTL = {relX,relY};
        double[] vecTR = {1-relX,relY};
        double[] vecBL = {relX,1-relY};
        double[] vecBR = {1-relX,1-relY};

        double tl = dot(gradTL,vecTL);
        double tr = dot(gradTR,vecTR);
        double bl = dot(gradBL,vecBL);
        double br = dot(gradBR,vecBR);

        double u = fade(relX);
        double v = fade(relY);

        double x1 = lerp(tl,tr,u);
        double y1 = lerp(bl,br,u);

        return lerp(x1,y1,v);
    }
    public double dot(int[] grad, double[] dist) {
        return (grad[0]*dist[0]) + (grad[1]*dist[1]);
    }
    public double lerp(double start, double end, double rate){
        return start+rate*(end-start);
    }
    public double fade(double t) {
        return t*t*t*(t*(t*6-15)+10);
    }

我正在使用流场来显示噪音,我知道这可能只是过于复杂,但这是该项目的最终目标。 Example正如您所看到的,有明显的线条显示了柏林噪音的网格。

以下是我输入函数的内容。

double val = p.perlin((x/width)*2, (y/height)*2);

我想尝试在单位正方形之间平滑地产生噪音。同样是的,我理解单声道噪声被认为是更好的,但我不是试图产生perlin噪声,而不是单纯噪声。

1 个答案:

答案 0 :(得分:0)

这部分对我来说不对:

    // distance from edges to point, relative x and y inside the unit square
    double[] vecTL = {relX,relY};
    double[] vecTR = {1-relX,relY};
    double[] vecBL = {relX,1-relY};
    double[] vecBR = {1-relX,1-relY};

我想你想要

    double[] vecTL = {relX,relY};
    double[] vecTR = {relX-1,relY};
    double[] vecBL = {relX,relY-1};
    double[] vecBR = {relX-1,relY-1};