软件 Perlin 噪声实现

发布于 2024-11-28 11:20:13 字数 1917 浏览 4 评论 0原文

我根据此处的信息编写了 2D Perlin 噪声实现, 此处此处,以及 此处。但是,输出看起来像这个

public static double Perlin(double X, double XScale, double Y, double YScale, double Persistance, double Octaves) {
    double total=0.0;
    for(int i=0;i<Octaves;i++){
        int frq = (int) Math.Pow(2,i);
        int amp = (int) Math.Pow(Persistance,i);
        total += InterpolatedSmoothNoise((X / XScale) * frq, (Y / YScale) * frq) * amp;
    }
return total;
}

private static double InterpolatedSmoothNoise (double X, double Y) {
int ix = (int) Math.Floor(X);
double fx = X-ix;
int iy = (int) Math.Floor(Y);
double fy = Y-iy;

double v1 = SmoothPerlin(ix,iy); //          --
double v2 = SmoothPerlin(ix+1,iy); //        +-
double v3 = SmoothPerlin(ix,iy+1);//         -+
double v4 = SmoothPerlin(ix+1,iy+1);//       ++

double i1 = Interpolate(v1,v2,fx);
double i2 = Interpolate(v3,v4,fx);

    return Interpolate(i1,i2,fy);
}

private static double SmoothPerlin (int X, int Y) {
    double sides=(Noise(X-1,Y,Z)+Noise(X+1,Y,Z)+Noise(X,Y-1,Z)+Noise(X,Y+1,Z)+Noise(X,Y,Z-1)+Noise(X,Y,Z+1))/12.0;
    double center=Noise(X,Y,Z)/2.0;
    return sides + center;
}

private static double Noise (int X, int Y) {
uint m_z = (uint) (36969 * (X & 65535) + (X >> 16));
uint m_w = (uint) (18000 * (Y & 65535) + (Y >> 16));
uint ou = (m_z << 16) + m_w;
return ((ou + 1.0) * 2.328306435454494e-10);
}

任何有关错误的意见都会受到赞赏。

编辑:我找到了解决此问题的方法:我使用加载时生成的双精度数组来解决此问题。不过,任何实现良好随机数生成器的方法都是值得赞赏的。

I have written a 2D Perlin noise implementation based on information from here, here, here, and here. However, the output looks like this.

public static double Perlin(double X, double XScale, double Y, double YScale, double Persistance, double Octaves) {
    double total=0.0;
    for(int i=0;i<Octaves;i++){
        int frq = (int) Math.Pow(2,i);
        int amp = (int) Math.Pow(Persistance,i);
        total += InterpolatedSmoothNoise((X / XScale) * frq, (Y / YScale) * frq) * amp;
    }
return total;
}

private static double InterpolatedSmoothNoise (double X, double Y) {
int ix = (int) Math.Floor(X);
double fx = X-ix;
int iy = (int) Math.Floor(Y);
double fy = Y-iy;

double v1 = SmoothPerlin(ix,iy); //          --
double v2 = SmoothPerlin(ix+1,iy); //        +-
double v3 = SmoothPerlin(ix,iy+1);//         -+
double v4 = SmoothPerlin(ix+1,iy+1);//       ++

double i1 = Interpolate(v1,v2,fx);
double i2 = Interpolate(v3,v4,fx);

    return Interpolate(i1,i2,fy);
}

private static double SmoothPerlin (int X, int Y) {
    double sides=(Noise(X-1,Y,Z)+Noise(X+1,Y,Z)+Noise(X,Y-1,Z)+Noise(X,Y+1,Z)+Noise(X,Y,Z-1)+Noise(X,Y,Z+1))/12.0;
    double center=Noise(X,Y,Z)/2.0;
    return sides + center;
}

private static double Noise (int X, int Y) {
uint m_z = (uint) (36969 * (X & 65535) + (X >> 16));
uint m_w = (uint) (18000 * (Y & 65535) + (Y >> 16));
uint ou = (m_z << 16) + m_w;
return ((ou + 1.0) * 2.328306435454494e-10);
}

Any input on what is wrong is appreciated.

EDIT: I found a way to solve this: I used an array of doubles generated at load to fix this. Any way to implement a good random number generator is appreciated though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-12-05 11:20:14

我用 C 语言重建了你的代码,并遵循@Howard 的建议,这段代码对我来说效果很好。我不确定您使用的是哪个插值函数。我在代码中使用了线性插值。我使用了以下噪声函数:

static double Noise2(int x, int y) {
    int n = x + y * 57;
    n = (n<<13) ^ n;

    return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);  
}

I reconstructed your code in C and following suggestion from @Howard and this code is working well for me. I am not sure which Interpolate function you used. I used a linear interpolation in my code. I used following noise function:

static double Noise2(int x, int y) {
    int n = x + y * 57;
    n = (n<<13) ^ n;

    return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);  
}
心如狂蝶 2024-12-05 11:20:13

我想这种效果是由于你的噪声函数造成的(所有其他代码看起来都不错)。

该函数的

private static double Noise (int X, int Y) {
    uint m_z = (uint) (36969 * (X & 65535) + (X >> 16));
    uint m_w = (uint) (18000 * (Y & 65535) + (Y >> 16));
    uint ou = (m_z << 16) + m_w;
    return ((ou + 1.0) * 2.328306435454494e-10);
}

噪音不是很大,但与您的输入 XY 变量密切相关。尝试使用您输入的任何其他伪随机函数。

I suppose this effect is due to your noise function (all other code looks ok).

The function

private static double Noise (int X, int Y) {
    uint m_z = (uint) (36969 * (X & 65535) + (X >> 16));
    uint m_w = (uint) (18000 * (Y & 65535) + (Y >> 16));
    uint ou = (m_z << 16) + m_w;
    return ((ou + 1.0) * 2.328306435454494e-10);
}

isn't very noisy but strongly correlated with your input X and Y variables. Try using any other pseudo-random function which you seed with you input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文