首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指数分布Perlin噪声的实现问题

指数分布Perlin噪声的实现问题
EN

Stack Overflow用户
提问于 2019-10-28 09:33:49
回答 1查看 75关注 0票数 1

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

代码语言:javascript
复制
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个归一化随机2D向量的数组,m是集合1.02^-n的前256个元素。

以下是我得到的结果类型(没有额外的八度):

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

EN

回答 1

Stack Overflow用户

发布于 2019-10-29 00:56:15

实际上我犯了两个错误:

  1. Math.Lerp的参数顺序被调高了,它应该是Mathf.Lerp(sx, u1, v1, sx);而不是Mathf.Lerp(sx, u1, v1);例如
  2. 我通过强制转换为整数来进行舍入-这会导致负坐标的问题。这应该更改为Mathf.FloorToInt (因为我正在使用Unity)

因此,在这些更改之后,这是我的(工作)代码: public static float sample(float x,float y) { float tx =x+ 4096;int bx0 = Mathf.FloorToInt( tx ) & 255;int bx1 = (bx0 + 1) & 255;float rx0 =tx- Mathf.FloorToInt(tx);float rx1 = rx0 - 1f;

代码语言:javascript
复制
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);

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58584831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档