我一直在尝试在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个归一化随机2D向量的数组,m是集合1.02^-n的前256个元素。
以下是我得到的结果类型(没有额外的八度):

有人知道哪里出了问题吗?
发布于 2019-10-29 00:56:15
实际上我犯了两个错误:
Mathf.Lerp(sx, u1, v1, sx);而不是Mathf.Lerp(sx, u1, v1);例如因此,在这些更改之后,这是我的(工作)代码: 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;
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);}
https://stackoverflow.com/questions/58584831
复制相似问题