From a718bfbc2cb210f241fc6ed27b58f37f93070e9d Mon Sep 17 00:00:00 2001 From: fruchti Date: Sun, 18 Dec 2022 12:49:38 +0100 Subject: [PATCH] Add experimental 2D noise --- lib/cs_noise.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/cs_noise.py diff --git a/lib/cs_noise.py b/lib/cs_noise.py new file mode 100644 index 0000000..386fad5 --- /dev/null +++ b/lib/cs_noise.py @@ -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]