Add experimental 2D noise

This commit is contained in:
fruchti 2022-12-18 12:49:38 +01:00
parent 6b64067f0b
commit a718bfbc2c

21
lib/cs_noise.py Normal file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env python
import numpy as np
import scipy.ndimage
def noise2d(width, height, smooth=1.8, filter_mode='wrap', falloff=0.02,
power=3):
rng = np.random.default_rng()
x = np.zeros((1, 1)) # Zero mean
iterations = int(np.ceil(np.log(max(width, height)) / np.log(power)))
falloff *= smooth
contribution = 1
for i in range(iterations):
upscaled = np.zeros((x.shape[0] * power, x.shape[1] * power))
upscaled[power // 2::power, power // 2::power] = x
x = scipy.ndimage.gaussian_filter(upscaled, smooth, mode=filter_mode)
x += rng.normal(scale=contribution, size=x.shape)
contribution *= falloff
return x[:width, :height]