我正在尝试使用PyQt5和Python实现Simplex Noise来创建Proceduraly Generated Height地图以进行模拟。我选择了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
这是我用来绘制此HeightMap的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?任何帮助将不胜感激!
答案 0 :(得分:0)
我没有测试过,但我的评论似乎就是答案。
https://github.com/lmas/opensimplex#status
它说:
稳定但缓慢。
如果你想要快速的东西,你需要通过像numpy或scipy这样的lib来使用C中的东西...... 因此,要么通过提供更快的代码或等待增强来为项目做出贡献。
或者编写自己的实现。