回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图用柏林噪声发生器来制作地图的瓷砖,但我注意到我的噪音太刺耳了,我的意思是,它有太多的海拔高度,没有平坦的地方,它们看起来不像山、岛屿、湖泊或任何东西;它们看起来太随机,有很多山峰。在</p>
<p><strong>在问题的末尾,需要进行一些更改,以便解决问题。</strong></p>
<p>问题对于代码来说很重要</p>
<p><strong>1D:</strong></p>
<pre><code>def Noise(self, x): # I wrote this noise function but it seems too random
random.seed(x)
number = random.random()
if number < 0.5:
final = 0 - number * 2
elif number > 0.5:
final = number * 2
return final
def Noise(self, x): # I found this noise function on the internet
x = (x<<13) ^ x
return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)
</code></pre>
<p><strong>2D:</strong></p>
^{pr2}$
<p><strong>我留下了1D和2D柏林噪声的代码,因为可能有人对此感兴趣:</strong>
(我花了很长时间才找到一些代码,所以我想有人会很乐意在这里找到一个示例)。<br/>
您不需要Matplotlib或NumPy来产生噪音;我只是使用它们来制作图形,以便更好地查看结果。在</p>
<pre><code>import random
import matplotlib.pyplot as plt # To make graphs
from mpl_toolkits.mplot3d import Axes3D # To make 3D graphs
import numpy as np # To make graphs
class D(): # Base of classes D1 and D2
def Cubic_Interpolate(self, v0, v1, v2, v3, x):
P = (v3 - v2) - (v0 - v1)
Q = (v0 - v1) - P
R = v2 - v0
S = v1
return P * x**3 + Q * x**2 + R * x + S
class D1(D):
def __init__(self, lenght, octaves):
self.result = self.Perlin(lenght, octaves)
def Noise(self, x): # I wrote this noise function but it seems too random
random.seed(x)
number = random.random()
if number < 0.5:
final = 0 - number * 2
elif number > 0.5:
final = number * 2
return final
def Noise(self, x): # I found this noise function on the internet
x = (x<<13) ^ x
return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)
def Perlin(self, lenght, octaves):
result = []
for x in range(lenght):
value = 0
for y in range(octaves):
frequency = 2 ** y
amplitude = 0.25 ** y
value += self.Interpolate_Noise(x * frequency) * amplitude
result.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(value)
print(f"{x} / {lenght} ({x/lenght*100:.2f}%): {round(x/lenght*10) * '#'} {(10-round(x/lenght*10)) * ' '}. Remaining {lenght-x}.") # I don't use `os.system('cls')` because it slow down the code.
return result
def Smooth_Noise(self, x):
return self.Noise(x) / 2 + self.Noise(x-1) / 4 + self.Noise(x+1) / 4
def Interpolate_Noise(self, x):
round_x = round(x)
frac_x = x - round_x
v0 = self.Smooth_Noise(round_x - 1)
v1 = self.Smooth_Noise(round_x)
v2 = self.Smooth_Noise(round_x + 1)
v3 = self.Smooth_Noise(round_x + 2)
return self.Cubic_Interpolate(v0, v1, v2, v3, frac_x)
def graph(self, *args):
plt.plot(np.array(self.result), '-', label = "Line")
for x in args:
plt.axhline(y=x, color='r', linestyle='-')
plt.xlabel('X')
plt.ylabel('Y')
plt.title("Simple Plot")
plt.legend()
plt.show()
class D2(D):
def __init__(self, lenght, octaves = 1):
self.lenght_axes = round(lenght ** 0.5)
self.lenght = self.lenght_axes ** 2
self.result = self.Perlin(self.lenght, octaves)
def Noise(self, x, y): # I wrote this noise function but it seems too random
n = x + y
random.seed(n)
number = random.random()
if number < 0.5:
final = 0 - number * 2
elif number > 0.5:
final = number * 2
return final
def Noise(self, x, y): # I found this noise function on the internet
n = x + y * 57
n = (n<<13) ^ n
return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)
def Smooth_Noise(self, x, y):
corners = (self.Noise(x - 1, y - 1) + self.Noise(x + 1, y - 1) + self.Noise(x - 1, y + 1) + self.Noise(x + 1, y + 1) ) / 16
sides = (self.Noise(x - 1, y) + self.Noise(x + 1, y) + self.Noise(x, y - 1) + self.Noise(x, y + 1) ) / 8
center = self.Noise(x, y) / 4
return corners + sides + center
def Interpolate_Noise(self, x, y):
round_x = round(x)
frac_x = x - round_x
round_y = round(y)
frac_y = y - round_y
v11 = self.Smooth_Noise(round_x - 1, round_y - 1)
v12 = self.Smooth_Noise(round_x , round_y - 1)
v13 = self.Smooth_Noise(round_x + 1, round_y - 1)
v14 = self.Smooth_Noise(round_x + 2, round_y - 1)
i1 = self.Cubic_Interpolate(v11, v12, v13, v14, frac_x)
v21 = self.Smooth_Noise(round_x - 1, round_y)
v22 = self.Smooth_Noise(round_x , round_y)
v23 = self.Smooth_Noise(round_x + 1, round_y)
v24 = self.Smooth_Noise(round_x + 2, round_y)
i2 = self.Cubic_Interpolate(v21, v22, v23, v24, frac_x)
v31 = self.Smooth_Noise(round_x - 1, round_y + 1)
v32 = self.Smooth_Noise(round_x , round_y + 1)
v33 = self.Smooth_Noise(round_x + 1, round_y + 1)
v34 = self.Smooth_Noise(round_x + 2, round_y + 1)
i3 = self.Cubic_Interpolate(v31, v32, v33, v34, frac_x)
v41 = self.Smooth_Noise(round_x - 1, round_y + 2)
v42 = self.Smooth_Noise(round_x , round_y + 2)
v43 = self.Smooth_Noise(round_x + 1, round_y + 2)
v44 = self.Smooth_Noise(round_x + 2, round_y + 2)
i4 = self.Cubic_Interpolate(v41, v42, v43, v44, frac_x)
return self.Cubic_Interpolate(i1, i2, i3, i4, frac_y)
def Perlin(self, lenght, octaves):
result = []
for x in range(lenght):
value = 0
for y in range(octaves):
frequency = 2 ** y
amplitude = 0.25 ** y
value += self.Interpolate_Noise(x * frequency, x * frequency) * amplitude
result.append(value)
print(f"{x} / {lenght} ({x/lenght*100:.2f}%): {round(x/lenght*10) * '#'} {(10-round(x/lenght*10)) * ' '}. Remaining {lenght-x}.") # I don't use `os.system('cls')` because it slow down the code.
return result
def graph(self, color = 'viridis'):
# Other colors: https://matplotlib.org/examples/color/colormaps_reference.html
fig = plt.figure()
Z = np.array(self.result).reshape(self.lenght_axes, self.lenght_axes)
ax = fig.add_subplot(1, 2, 1, projection='3d')
X = np.arange(self.lenght_axes)
Y = np.arange(self.lenght_axes)
X, Y = np.meshgrid(X, Y)
d3 = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=color, linewidth=0, antialiased=False)
fig.colorbar(d3)
ax = fig.add_subplot(1, 2, 2)
d2 = ax.imshow(Z, cmap=color, interpolation='none')
fig.colorbar(d2)
plt.show()
</code></pre>
<p>问题是输出似乎不适合映射。在</p>
<p>使用以下方法查看此输出:</p>
<pre><code>test = D2(1000, 3)
test.graph()
</code></pre>
<p><a href="https://i.stack.imgur.com/o7FiM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o7FiM.png" alt="enter image description here"/></a></p>
<p>我在找更光滑的。在</p>
<p>也许在2D噪音中很难注意到我所说的,但在1D中则容易得多:</p>
<pre><code>test = D1(1000, 3)
test.graph()
</code></pre>
<p><a href="https://i.stack.imgur.com/WYenJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WYenJ.png" alt="enter image description here"/></a></p>
<p>来自互联网的噪声函数的峰值稍小且频率较低,但仍有太多的峰值。我在找更光滑的。在</p>
<p>可能是这样:<br/>
<a href="https://i.stack.imgur.com/sP3h2.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sP3h2.gif" alt="enter image description here"/></a></p>
<p>或者这样:<br/>
<a href="https://i.stack.imgur.com/MzLPH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MzLPH.png" alt="enter image description here"/></a></p>
<p><strong>p.S:</strong>我是根据<a href="https://web.archive.org/web/20160530124230/http://freespace.virgin.net:80/hugo.elias/models/m_perlin.htm" rel="nofollow noreferrer">this pseudocode</a>制作的。在</p>
<h2><strong>编辑:</strong></h2>
<h3><strong>皮卡莱克:</strong></h3>
<p><a href="https://i.stack.imgur.com/81YUk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/81YUk.png" alt="enter image description here"/></a></p>
<p>即使值较低,也有峰值,没有曲线或平滑/平坦的线条。在</p>
<h3><strong>geza:解决方案</strong></h3>
<p>多亏了<a href="https://stackoverflow.com/a/47839493/7655838">geza's suggestions</a>我找到了解决问题的方法:</p>
<pre><code>def Perlin(self, lenght_axes, octaves, zoom = 0.01, amplitude_base = 0.5):
result = []
for y in range(lenght_axes):
line = []
for x in range(lenght_axes):
value = 0
for o in range(octaves):
frequency = 2 ** o
amplitude = amplitude_base ** o
value += self.Interpolate_Noise(x * frequency * zoom, y * frequency * zoom) * amplitude
line.append(value)
result.append(line)
print(f"{y} / {lenght_axes} ({y/lenght_axes*100:.2f}%): {round(y/lenght_axes*20) * '#'} {(20-round(y/lenght_axes*20)) * ' '}. Remaining {lenght_axes-y}.")
return result
</code></pre>
<p>其他修改包括:</p>
<ul>
<li><code>Z = np.array(self.result)</code>而不是graph函数中的这个{<cd2>}。在</li>
<li>在<code>round_x</code>和<code>round_y</code>变量中使用<code>math.floor()</code>(记住<code>import math</code>)代替<code>Interpolate_Noise</code>函数中的<code>round()</code>。在</li>
<li>将<code>Noise</code>(第二个)中的<code>return</code>行修改为<code>return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0)</code>。
<code>D2(10000, 10)</code>
<a href="https://i.stack.imgur.com/rdj5N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rdj5N.png" alt="enter image description here"/></a>
现在唯一奇怪的是山(黄色)总是靠近同一个地方,但我认为这是改变<code>Noise</code>函数中的数字的问题。在</li>
</ul>