-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
272 lines (226 loc) · 8.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import math
from collections import namedtuple
from functools import lru_cache, wraps
from random import randint
from time import time
import numpy as np
import scipy.misc as smp
# Settings
enable_lru = True
enable_color = False
enable_strange_shade = False
enable_gradient_shade = True
repeat = 0 # not sure really what this does. it was in the tutorial. it breaks color.
p = [151, 160, 137, 91, 90, 15,
131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180]
p.extend(p)
Vector = namedtuple('Vector', 'x y z')
def use_lru(func):
if not enable_lru:
return func
@lru_cache(maxsize=None)
def decorator(*args):
return func(*args)
return decorator
def truncate(f, n):
'''Truncates/pads a float f to n decimal places without rounding'''
s = '{}'.format(f)
if 'e' in s or 'E' in s:
return '{0:.{1}f}'.format(f, n)
i, p, d = s.partition('.')
return float('.'.join([i, (d + '0' * n)[:n]]))
def truncate_args(digits):
def decorator(func):
@wraps(func)
def wrapper(*args):
args = (truncate(x, digits) for x in args)
return func(*args)
return wrapper
return decorator
@use_lru
def fade(t):
return 6*t**5 - 15*t**4 + 10*t**3
@use_lru
def inc(num):
num += 1
if repeat > 0:
num %= repeat
return num
@use_lru
def hash_row(x, y, z):
return p[p[p[x] + y] + z]
def grad_slow(hash, x, y, z):
h = hash & 15
u = x if h < 8 else y
if h < 4:
v = y
elif h == 12 or h == 14:
v = x
else:
v = z
return (u if (h & 1) == 0 else -u) + (v if (h & 2) == 0 else -v)
def grad(hash, x, y, z):
switch = hash & 0xF
if switch == 0x0:
return x + y
elif switch == 0x1:
return -x + y
elif switch == 0x2:
return x - y
elif switch == 0x3:
return -x - y
elif switch == 0x4:
return x + z
elif switch == 0x5:
return -x + z
elif switch == 0x6:
return x - z
elif switch == 0x7:
return -x - z
elif switch == 0x8:
return y + z
elif switch == 0x9:
return -y + z
elif switch == 0xA:
return y - z
elif switch == 0xB:
return -y - z
elif switch == 0xC:
return y + x
elif switch == 0xD:
return -y + z
elif switch == 0xE:
return y - x
elif switch == 0xF:
return -y - z
def lerp(a, b, x):
return a + x * (b - a)
def perlin(x, y, z):
arglist = Vector(x, y, z)
if repeat > 0:
arglist = Vector(*map(lambda x: x % repeat, arglist))
float_args, int_args = [Vector(*l) for l in zip(*list(map(math.modf, arglist)))]
int_args = Vector(*map(int, int_args))
fade_vector = Vector(*map(fade, float_args))
aaa = hash_row(*int_args)
aba = hash_row(int_args.x, inc(int_args.y), int_args.z)
aab = hash_row(int_args.x, int_args.y, inc(int_args.z))
abb = hash_row(int_args.x, inc(int_args.y), inc(int_args.z))
baa = hash_row(inc(int_args.x), int_args.y, int_args.z)
bba = hash_row(inc(int_args.x), inc(int_args.y), int_args.z)
bab = hash_row(inc(int_args.x), int_args.y, inc(int_args.z))
bbb = hash_row(*map(inc, int_args))
x1 = lerp(grad(aaa, *float_args), grad(baa, float_args.x - 1, float_args.y, float_args.z), fade_vector.x)
x2 = lerp(grad(aba, float_args.x, float_args.y - 1, float_args.z),
grad(bba, float_args.x - 1, float_args.y - 1, float_args.z), fade_vector.x)
y1 = lerp(x1, x2, fade_vector.y)
x1 = lerp(grad(aab, float_args.x, float_args.y, float_args.z - 1),
grad(bab, float_args.x - 1, float_args.y, float_args.z - 1), fade_vector.x)
x2 = lerp(grad(abb, float_args.x, float_args.y - 1, float_args.z - 1),
grad(bbb, float_args.x - 1, float_args.y - 1, float_args.z - 1), fade_vector.x)
y2 = lerp(x1, x2, fade_vector.y)
return (lerp(y1, y2, fade_vector.z) + 1) / 2
def octave_perlin(x, y, z, octaves, persistence):
total = 0
frequency = 1
amplitude = 1
maxValue = 0
for i in range(octaves):
total += perlin(x * frequency, y * frequency, z * frequency) * amplitude
maxValue += amplitude
amplitude += persistence
frequency *= 2
return total / maxValue
def fbm(x,y,z):
v = 0.0
a = 0.5
shift = np.array([100.0, 0.0])
# Rotate to reduce axial bias
rot = np.array([[math.cos(0.5), math.sin(0.5)], [-1*math.sin(0.5), math.cos(0.5)]])
for i in range(0, 5):
v += a * perlin(x,y,z)
x,y,z = rot * np.array([x,y,z]) * 2.0 + shift
a *= 0.5
return v
def HueToRGB(h, s=1, v=1):
h = float(h)
s = float(s)
v = float(v)
h60 = h / 60.0
h60f = math.floor(h60)
hi = int(h60f) % 6
f = h60 - h60f
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
r, g, b = 0, 0, 0
if hi == 0:
r, g, b = v, t, p
elif hi == 1:
r, g, b = q, v, p
elif hi == 2:
r, g, b = p, v, t
elif hi == 3:
r, g, b = p, q, v
elif hi == 4:
r, g, b = t, p, v
elif hi == 5:
r, g, b = v, p, q
r, g, b = int(r * 255), int(g * 255), int(b * 255)
return r, g, b
# Size of the screen
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 300
# how fine the noise is. lower => finer features
UNIT_CUBE = 128
def f(x,y,z):
p = np.array([x,y,z])
return fbm(*(p[:] + fbm(*(p[:] +fbm(*p)))))
def main():
starttime = time()
data = np.zeros((SCREEN_WIDTH, SCREEN_HEIGHT, 3), dtype=np.uint8)
# choose a random z-slice to get a random image back. otherwise perlin() always returns the same map (z=0)
z = randint(1, UNIT_CUBE)
colorseed = randint(1, UNIT_CUBE), randint(1, UNIT_CUBE), randint(1, UNIT_CUBE)
for x in range(SCREEN_WIDTH):
for y in range(SCREEN_HEIGHT):
value = perlin(x // UNIT_CUBE + (x % UNIT_CUBE) / UNIT_CUBE, y // UNIT_CUBE + (y % UNIT_CUBE) / UNIT_CUBE,
z)
if enable_color:
r = perlin(x // UNIT_CUBE + (x % UNIT_CUBE) / UNIT_CUBE, y // UNIT_CUBE + (y % UNIT_CUBE) / UNIT_CUBE,
colorseed[0])
g = perlin(x // UNIT_CUBE + (x % UNIT_CUBE) / UNIT_CUBE, y // UNIT_CUBE + (y % UNIT_CUBE) / UNIT_CUBE,
colorseed[1])
b = perlin(x // UNIT_CUBE + (x % UNIT_CUBE) / UNIT_CUBE, y // UNIT_CUBE + (y % UNIT_CUBE) / UNIT_CUBE,
colorseed[2])
elif enable_strange_shade:
r, g, b = HueToRGB(value)
elif enable_gradient_shade:
r, g, b = HueToRGB(value * 360)
value = 1/255 # dirty, but we want to remove the scaling from when we add the color to the bitmap
else:
r, g, b = 1, 1, 1
data[x, y] = list(map(int, (255 * r * value, 255 * g * value, 255 * b * value)))
img = smp.toimage(data)
img.show()
img.save('noise.bmp')
print("Time elapsed: " + str(time() - starttime))
lrulist = [fade, hash_row, inc, grad, lerp, perlin]
for lru in lrulist:
try:
print(lru.__name__, lru.cache_info())
except AttributeError:
continue
if __name__ == "__main__":
main()