【问题标题】:Problems with implementation of exponentially ditributed Perlin Noise指数分布的 Perlin 噪声的实现问题
【发布时间】:2020-02-23 08:37:26
【问题描述】:

我一直在尝试在c# 中实现改进的 Perlin 噪声算法codepaper。然而,我得到了非常奇怪的结果——即使我一遍又一遍地查看我的代码版本。 这是我的噪声函数:

public static float sample(float x, float y) {
    float tx = x + 4096;
    int bx0 = ((int) tx) & 255;
    int bx1 = (bx0 + 1) & 255;
    float rx0 = tx - (int) tx;
    float rx1 = rx0 - 1f;

    float ty = y + 4096;
    int by0 = ((int) ty) & 255;
    int by1 = (by0 + 1) & 255;
    float ry0 = ty - (int) ty;
    float ry1 = ry0 - 1f;

    int b00 = p[(p[bx0] + by0) & 255];
    int b10 = p[(p[bx1] + by0) & 255];
    int b01 = p[(p[bx0] + by1) & 255];
    int b11 = p[(p[bx1] + by1) & 255];

    float sx = s_curve(rx0);

    float u1 = m[b00] * (rx0 * g[b00].x + ry0 * g[b00].y);
    float v1 = m[b10] * (rx1 * g[b10].x + ry0 * g[b10].y);
    float a = Mathf.Lerp(sx, u1, v1);

    float u2 = m[b01] * (rx0 * g[b01].x + ry1 * g[b01].y);
    float v2 = m[b11] * (rx1 * g[b11].x + ry1 * g[b11].y);
    float b = Mathf.Lerp(sx, u2, v2);

    float sy = s_curve(ry0);
    return Mathf.Lerp(sy, a, b);
}

其中p 是整数 0 到 255 的随机排序数组,g 是 256 个标准化随机二维向量的数组,m 是集合 1.02^-n 的前 256 个元素。

这些是我得到的结果(没有额外的八度音阶):

有人知道哪里出了问题吗?

【问题讨论】:

    标签: c# algorithm noise perlin-noise


    【解决方案1】:

    实际上我犯了两个错误:

    1. Math.Lerp 的参数顺序调高,例如应该是Mathf.Lerp(sx, u1, v1, sx); 而不是Mathf.Lerp(sx, u1, v1);

    2. 我通过强制转换为整数进行四舍五入 - 但是这会导致负坐标问题。这应该更改为 Mathf.FloorToInt(因为我正在使用 Unity)

    因此,在这些更改之后,这是我的(工作)代码: 公共静态浮动样本(浮动 x,浮动 y){ 浮动交易 = x + 4096; int bx0 = Mathf.FloorToInt(tx) & 255; int bx1 = (bx0 + 1) & 255; 浮动 rx0 = tx - Mathf.FloorToInt(tx); 浮动 rx1 = rx0 - 1f;

    float ty = y + 4096;
    int by0 = Mathf.FloorToInt(ty) & 255;
    int by1 = (by0 + 1) & 255;
    float ry0 = ty - Mathf.FloorToInt(ty);
    float ry1 = ry0 - 1f;
    
    int b00 = p[(p[bx0] + by0) & 255];
    int b10 = p[(p[bx1] + by0) & 255];
    int b01 = p[(p[bx0] + by1) & 255];
    int b11 = p[(p[bx1] + by1) & 255];
    
    float sx = s_curve(rx0);
    
    float u1 = m[b00] * (rx0 * g[b00].x + ry0 * g[b00].y);
    float v1 = m[b10] * (rx1 * g[b10].x + ry0 * g[b10].y);
    float a = Mathf.Lerp(u1, v1, sx);
    
    float u2 = m[b01] * (rx0 * g[b01].x + ry1 * g[b01].y);
    float v2 = m[b11] * (rx1 * g[b11].x + ry1 * g[b11].y);
    float b = Mathf.Lerp(u2, v2, sx);
    
    float sy = s_curve(ry0);
    return Mathf.Lerp(a, b, sy);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2021-06-30
      • 2012-03-21
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      相关资源
      最近更新 更多