我正在尝试使用PyQt5和Python实现单纯形噪声来创建用于模拟目的的程序生成高度图。我选择了 PyQt5,因为这是我最熟悉的 GUI。这是我使用 OpenSimplex 库生成高度图的代码。
def noise_map(width,height):
height = height
width = width
noise_height_map = [[0 for x in range(width)] for y in range(height)]
for y in range(height):
for x in range(width):
nx = x
ny = y
noise_height_map[y][x] = noise(nx, ny)
return noise_height_map
这是我用来绘制这个高度图的 PyQt5 代码
。class Example(QWidget):
def __init__(self):
super().__init__()
self.size = self.size()
self.map = pickle.load( open( "noise.p", "rb" ) )
self.gen = OpenSimplex()
self.width = 300
self.height = 300
self.initUI()
def initUI(self):
self.setGeometry(300, 300, self.width, self.height)
self.setWindowTitle('Points')
self.show()
def paintEvent(self, e):
start_time = time.time()
qp = QPainter()
qp.begin(self)
#self.draw_random_grid(qp)
self.draw_noise_grid(qp)
#self.draw_noise_grid_v2(qp)
qp.end()
end_time= time.time()
print('Paint Event:',end_time - start_time)
def noise(self, nx, ny):
# Rescale from -1.0:+1.0 to 0.0:1.0
return self.simplex.noise2d(nx, ny)
#/ 2.0 + 0.5
def noise_map(self, width, height):
height = height
width = width
noise_map = [[0 for x in range(width)] for y in range(height)]
for y in range(height):
for x in range(width):
nx = x
ny = y
noise_map[y][x] = self.noise(nx, ny)
return noise_map
def draw_noise_grid(self, qp):
start_time = time.time()
column_index = 0
row_index = 0
col = QColor(0, 0, 0)
for row in range(self.width):
for column in range(self.height):
value =(self.map[row][column] * 255)
col.setHsv(0, 0, value, 255)
qp.fillRect(column_index, row_index, 10, 10, col)
column_index += 10
if column_index >= 1000:
column_index = 0
row_index += 10
if row_index >= 1000:
row_index = 0
end_time = time.time()
print('Draw Grid:',end_time - start_time)
当我对 draw_noise_grid(( 进行计时时,我有将近 1.5 秒的时间让它均匀渲染,一半时间它会破坏 python 并关闭。我的问题我做错了什么导致它渲染得非常慢?还是我需要选择新的 GUI?任何帮助将不胜感激!
我没有测试过它,但我的评论似乎是答案。
https://github.com/lmas/opensimplex#status
它说:
稳定但缓慢。
如果你想要快速的东西,你需要通过像numpy或scipy这样的库在C中使用一些东西......因此,要么通过提供更快的代码来为项目做出贡献,要么等待增强功能。
或者编写自己的实现。