Reset Perlin's cache from time to time.

This commit is contained in:
Dominik Moritz Roth 2023-07-12 20:23:03 +02:00
parent dc2ab472e9
commit ee39fab49a

View File

@ -99,6 +99,7 @@ class Perlin_Noise():
self.magic = 3.141592653589 # Axis offset, should be (kinda) irrational self.magic = 3.141592653589 # Axis offset, should be (kinda) irrational
# We want to genrate samples, that approx ~N(0,1) # We want to genrate samples, that approx ~N(0,1)
self.normal_factor = 14/99 self.normal_factor = 14/99
self.clear_cache_every = 1024
self.reset() self.reset()
def __call__(self, shape=None): def __call__(self, shape=None):
@ -107,6 +108,8 @@ class Perlin_Noise():
self.index += 1 self.index += 1
noise = [self.noise([self.index*self.scale, self.magic*a]) / self.normal_factor noise = [self.noise([self.index*self.scale, self.magic*a]) / self.normal_factor
for a in range(shape[-1])] for a in range(shape[-1])]
if self.index % self.clear_cache_every == 0:
self.noise.cache = {}
return th.Tensor(noise) return th.Tensor(noise)
def reset(self): def reset(self):
@ -126,12 +129,16 @@ class Harmonic_Perlin_Noise():
octaves_arr += [1/(int_octaves+2)*(octaves-int_octaves)] octaves_arr += [1/(int_octaves+2)*(octaves-int_octaves)]
octaves_arr = np.array(octaves_arr) octaves_arr = np.array(octaves_arr)
self.octaves = octaves_arr / np.linalg.norm(octaves_arr) self.octaves = octaves_arr / np.linalg.norm(octaves_arr)
self.clear_cache_every = 1024
self.reset() self.reset()
def __call__(self, shape=None): def __call__(self, shape=None):
if shape == None: if shape == None:
shape = self.known_shape shape = self.known_shape
harmonics = [noise(shape)*self.octaves[i] for i, noise in enumerate(self.noises)] harmonics = [noise(shape)*self.octaves[i] for i, noise in enumerate(self.noises)]
if self.index % self.clear_cache_every == 0:
for i, noise in enumerate(self.noises):
noise.cache = {}
return sum(harmonics) return sum(harmonics)
def reset(self): def reset(self):