├── LICENSE ├── README.md └── perlin_noise ├── graph_noise.py └── perlin.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Supreme Sector 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Perlin-Noise 2 | An instantiation of the Python Perlin class allows you to generate Perlin Noise 3 | 4 | ## Video 5 | https://youtu.be/QHdU1XRB9uw 6 | -------------------------------------------------------------------------------- /perlin_noise/graph_noise.py: -------------------------------------------------------------------------------- 1 | import perlin 2 | import matplotlib.pyplot as plt 3 | 4 | noise=perlin.Perlin() 5 | 6 | time=[i*0.01 for i in range(600)] 7 | values=[noise.valueAt(i) for i in time] 8 | 9 | plt.title("Perlin Noise") 10 | plt.xlabel("Time") 11 | plt.ylabel("Value") 12 | plt.plot(time, values) 13 | plt.show() 14 | 15 | noise.discard(2.2) 16 | values = [noise.valueAt(i) if noise.valueAt(i) else 0 for i in time] 17 | plt.title("Perlin Noise - Discard") 18 | plt.xlabel("Time") 19 | plt.ylabel("Value") 20 | plt.plot(time, values) 21 | plt.show() 22 | -------------------------------------------------------------------------------- /perlin_noise/perlin.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | class Perlin: 4 | def __init__(self): 5 | self.gradients = [] 6 | self.lowerBound = 0 7 | 8 | 9 | def valueAt(self, t): 10 | if(t= len(self.gradients)-1+self.lowerBound: 15 | self.gradients.append(random.uniform(-1, 1)) 16 | 17 | discarded = int(self.lowerBound) # getting number of gradients that have been discarded 18 | # Compute products between surrounding gradients and distances from them 19 | d1 = (t-t//1) 20 | d2 = d1-1 21 | a1 = self.gradients[(int)(t//1)-discarded]*d1 22 | a2 = self.gradients[(int)(t//1+1)-discarded]*d2 23 | 24 | amt = self.__ease(d1) 25 | 26 | return self.__lerp(a1,a2,amt) 27 | 28 | def discard(self, amount): 29 | gradientsToDiscard = int(amount+self.lowerBound%1) 30 | self.gradients = self.gradients[gradientsToDiscard:] 31 | self.lowerBound += amount 32 | 33 | def __ease(self, x): 34 | return 6*x**5-15*x**4+10*x**3 35 | 36 | 37 | def __lerp(self, start, stop, amt): 38 | return amt*(stop-start)+start 39 | --------------------------------------------------------------------------------