project/generation_heightmap.py

40 lines
1.5 KiB
Python

import numpy as np
from opensimplex import OpenSimplex
class HeightmapGenerator:
def __init__(self, width=200, height=200, seed=None):
self.width = width
self.height = height
self.seed = seed if seed is not None else np.random.randint(0, 1000000)
self.noise = OpenSimplex(seed=self.seed)
def generate_heightmap(self, land_water_map, roughness=0.5):
heightmap = np.zeros((self.height, self.width))
scale = 30 / (roughness + 0.1)
base_noise = np.zeros((self.height, self.width))
for y in range(self.height):
for x in range(self.width):
nx = x / scale
ny = y / scale
base_noise[y,x] = self.noise.noise2(nx, ny)
base_noise = (base_noise - base_noise.min()) / (base_noise.max() - base_noise.min())
detail_scale = scale * 0.3
detail_noise = np.zeros((self.height, self.width))
for y in range(self.height):
for x in range(self.width):
nx = x / detail_scale
ny = y / detail_scale
detail_noise[y,x] = self.noise.noise2(nx, ny) * 0.3
combined = base_noise + detail_noise
combined = np.where(land_water_map == 1, combined, 0)
combined = (combined - combined.min()) / (combined.max() - combined.min())
heightmap = np.where(land_water_map == 1,
np.round(combined * 9 + 1) / 10,
0)
return heightmap