2D 柏林噪声
我已经完全掌握了 3D 柏林噪声的艺术,现在我正在尝试将相同的实现用于 2D 算法。 问题似乎在于选择我的渐变方向。在 3D 中,我在均匀分布的方向上使用 16 个渐变,效果非常好。 在 2D 中我想我会使用 8 个渐变。上、下、左、右和四个对角线方向。
这是我得到的:
噪声的总体外观始终是正确的,但方块的边缘不太相符。 我也尝试过使用其他渐变或更少的渐变,但得到了类似的结果。 在另一个示例中,您可以看到边缘有时确实匹配,并且该区域的结果很好 -
当我不使用渐变而只是在四个角中随机选取的值之间进行插值时,我会得到正确的结果,这让我认为是渐变部分搞乱了它。
这是我的代码:
//8 different gradient directions
private Point[] grads = new Point[] {
new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(1, -1),
new Point(0, -1), new Point(-1, -1), new Point(-1, 0), new Point(-1, 1),};
//takes the dot product of a gradient and (x, y)
private float dot2D(int i, float x, float y)
{
return
grads[i].X * x + grads[i].Y * y;
}
public float Noise2D(float x, float y)
{
int
ix = (int)(x),
iy = (int)(y);
x = x - ix;
y = y - iy;
float
fx = fade(x),
fy = fade(y);
ix &= 255;
iy &= 255;
// here is where i get the index to look up in the list of
// different gradients.
// hashTable is my array of 0-255 in random order
int
g00 = hashTable[ix + hashTable[iy ]],
g10 = hashTable[ix + 1 + hashTable[iy ]],
g01 = hashTable[ix + hashTable[iy + 1]],
g11 = hashTable[ix + 1 + hashTable[iy + 1]];
// this takes the dot product to find the values to interpolate between
float
n00 = dot2D(g00 & 7, x, y),
n10 = dot2D(g10 & 7, x, y),
n01 = dot2D(g01 & 7, x, y),
n11 = dot2D(g11 & 7, x, y);
// lerp() is just normal linear interpolation
float
y1 = lerp(fx, n00, n10),
y2 = lerp(fx, n01, n11);
return
lerp(fy, y1, y2);
}
I have fully mastered the art of Perlin Noise in 3D, and now I'm trying to use my same implementation for a 2D algorithm.
The problem seems to be in picking my gradient directions. In 3D I use 16 gradients in evenly distributed directions and this works great.
In 2D I figured I'd use 8 gradients. up, down, left, right, and the four diagonal directions.
Here is what I get:
The general look of the noise is always correct, but the edges of the squares don't quite match up.
I have also tried using other gradients or fewer gradients but get similar results.
Here in another example you can see that the edges do match up sometimes and the results are fine in that area -
When I don't use gradients and instead just interpolate between a value picked randomly at each of the 4 corners I get the right results, which is what makes me think it is the gradient part that is messing it up.
Here is my code:
//8 different gradient directions
private Point[] grads = new Point[] {
new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(1, -1),
new Point(0, -1), new Point(-1, -1), new Point(-1, 0), new Point(-1, 1),};
//takes the dot product of a gradient and (x, y)
private float dot2D(int i, float x, float y)
{
return
grads[i].X * x + grads[i].Y * y;
}
public float Noise2D(float x, float y)
{
int
ix = (int)(x),
iy = (int)(y);
x = x - ix;
y = y - iy;
float
fx = fade(x),
fy = fade(y);
ix &= 255;
iy &= 255;
// here is where i get the index to look up in the list of
// different gradients.
// hashTable is my array of 0-255 in random order
int
g00 = hashTable[ix + hashTable[iy ]],
g10 = hashTable[ix + 1 + hashTable[iy ]],
g01 = hashTable[ix + hashTable[iy + 1]],
g11 = hashTable[ix + 1 + hashTable[iy + 1]];
// this takes the dot product to find the values to interpolate between
float
n00 = dot2D(g00 & 7, x, y),
n10 = dot2D(g10 & 7, x, y),
n01 = dot2D(g01 & 7, x, y),
n11 = dot2D(g11 & 7, x, y);
// lerp() is just normal linear interpolation
float
y1 = lerp(fx, n00, n10),
y2 = lerp(fx, n01, n11);
return
lerp(fy, y1, y2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有点着急,但这可能会有所帮助。我将 Perlin 的参考实现改编为 C#。对于 2D,只需使用具有固定 z 参数的 3D Noise() 函数。 (
public static float Noise(float x, float y, float z)
在课程结束时。)更新
好的,我成功创建了一个可用的 2D 版本。这是该类:
像这样调用它:
请注意,我使用了几个 XNA 结构(Vector2 和 Texture2D),但它们的作用应该非常清楚。
如果您想要具有较少八度音程的更高频率(更多“噪音”)内容,请增加八度音程循环中使用的初始频率值。
此实现使用“改进的”Perlin 噪声,它应该比标准版本快一点。您还可以看看单纯形噪声,它在更高维度上速度要快得多。
I'm in a bit of a rush, but this might be helpful. I adapted Perlin's reference implementation to C#. For 2D, just use the 3D Noise() function with a fixed z parameter. (
public static float Noise(float x, float y, float z)
towards the end of the class.)Update
Okay, I managed to create a working 2D version. Here's the class:
Call it like this:
Note that I've used a couple of XNA structures (Vector2 and Texture2D), but it should be pretty clear what they do.
If you want higher frequency (more "noisy") content with fewer octaves, increase the initial frequency value that used in the octave loop.
This implementation uses "improved" Perlin noise, which should be a bit faster than the standard version. You might also have a look at Simplex noise, which is quite a bit faster at higher dimensions.
我必须将其更改
为
:基本上只需在需要的地方从 x 和 y 中减去 1 即可。
I had to change this:
to this:
Basically just subtracting 1 from the x and y where needed.
如果您将
z
的零值插入到 3D 方程中,然后简单地进行数学运算,删除项,您会发现最终得到一个更简单的方程。不过,您的实现看起来与我正在使用的有点不同。
以下是我正在使用的 3D 和 2D 函数(在 JavaScript 中)的比较:
2D 版本涉及较少的计算。
If you plug in a zero value for
z
into your 3D equation and simply follow the math through, removing terms, you'll see that you end up with a simpler equation in the end.Your implementation looks kind of different to the one I'm using though.
Here's a comparison of a 3D and 2D function I'm using (in JavaScript):
The 2D version involves fewer computations.