Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 指数分布柏林噪声的实现问题_C#_Algorithm_Noise_Perlin Noise - Fatal编程技术网

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个元素

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


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

实际上我犯了两个错误:

  • Math.Lerp的参数顺序已切换,它应该是
    Mathf.Lerp(sx,u1,v1,sx)而不是
    Mathf.Lerp(sx,u1,v1)例如

  • 我通过强制转换为整数进行舍入-这会导致负坐标的问题。这应该更改为Mathf.flourtoint(因为我使用的是Unity)

  • 因此,在这些更改之后,这是我的(工作)代码: 公共静态浮动样本(浮动x、浮动y){ 浮动tx=x+4096; int bx0=Mathf.FloorToInt(tx)&255; int bx1=(bx0+1)&255; 浮点数rx0=tx-数学地板点(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);
    

    }实际上我犯了两个错误:

  • Math.Lerp的参数顺序已切换,它应该是
    Mathf.Lerp(sx,u1,v1,sx)而不是
    Mathf.Lerp(sx,u1,v1)例如

  • 我通过强制转换为整数进行舍入-这会导致负坐标的问题。这应该更改为Mathf.flourtoint(因为我使用的是Unity)

  • 因此,在这些更改之后,这是我的(工作)代码: 公共静态浮动样本(浮动x、浮动y){ 浮动tx=x+4096; int bx0=Mathf.FloorToInt(tx)&255; int bx1=(bx0+1)&255; 浮点数rx0=tx-数学地板点(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);
    
    }