C# 指数分布柏林噪声的实现问题
我一直在尝试在C# 指数分布柏林噪声的实现问题,c#,algorithm,noise,perlin-noise,C#,Algorithm,Noise,Perlin Noise,我一直在尝试在c中实现一种改进的柏林噪声算法。然而,我得到了非常奇怪的结果——尽管我已经一遍又一遍地查看了我的代码版本。 这是我的噪音功能: 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; f
c
中实现一种改进的柏林噪声算法。然而,我得到了非常奇怪的结果——尽管我已经一遍又一遍地查看了我的代码版本。
这是我的噪音功能:
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个元素
这些是我得到的结果(没有额外的八度):
有人知道哪里出了问题吗?实际上我犯了两个错误:
Mathf.Lerp(sx,u1,v1,sx)代码>而不是Mathf.Lerp(sx,u1,v1)代码>例如
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);
}实际上我犯了两个错误:
Mathf.Lerp(sx,u1,v1,sx)代码>而不是Mathf.Lerp(sx,u1,v1)代码>例如
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);
}