Skip to content

Commit 0453eb9

Browse files
Add files via upload
1 parent 51afd2a commit 0453eb9

File tree

1 file changed

+308
-0
lines changed

1 file changed

+308
-0
lines changed

colored perlin noise effect.py

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
import pygame
2+
import random
3+
4+
import numpy as np
5+
from math import floor
6+
from ctypes import c_int64
7+
import random
8+
9+
import numba
10+
11+
GRADIENTS2 = np.array([
12+
5, 2, 2, 5,
13+
-5, 2, -2, 5,
14+
5, -2, 2, -5,
15+
-5, -2, -2, -5,
16+
], dtype=np.int64)
17+
18+
GRADIENTS3 = np.array([
19+
-11, 4, 4, -4, 11, 4, -4, 4, 11,
20+
11, 4, 4, 4, 11, 4, 4, 4, 11,
21+
-11, -4, 4, -4, -11, 4, -4, -4, 11,
22+
11, -4, 4, 4, -11, 4, 4, -4, 11,
23+
-11, 4, -4, -4, 11, -4, -4, 4, -11,
24+
11, 4, -4, 4, 11, -4, 4, 4, -11,
25+
-11, -4, -4, -4, -11, -4, -4, -4, -11,
26+
11, -4, -4, 4, -11, -4, 4, -4, -11,
27+
], dtype=np.int64)
28+
29+
GRADIENTS4 = np.array([
30+
3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3,
31+
-3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3, 1, -1, 1, 1, 3,
32+
3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3, 1, 1, -1, 1, 3,
33+
-3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3,
34+
3, 1, -1, 1, 1, 3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3,
35+
-3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3,
36+
3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3,
37+
-3, -1, -1, 1, -1, -3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3,
38+
3, 1, 1, -1, 1, 3, 1, -1, 1, 1, 3, -1, 1, 1, 1, -3,
39+
-3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3,
40+
3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3,
41+
-3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3,
42+
3, 1, -1, -1, 1, 3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3,
43+
-3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3,
44+
3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3, -1, 1, -1, -1, -3,
45+
-3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3,
46+
], dtype=np.int64)
47+
48+
STRETCH_CONSTANT2 = -0.211324865405187
49+
SQUISH_CONSTANT2 = 0.366025403784439
50+
STRETCH_CONSTANT3 = -1.0 / 6
51+
SQUISH_CONSTANT3 = 1.0 / 3
52+
STRETCH_CONSTANT4 = -0.138196601125011
53+
SQUISH_CONSTANT4 = 0.309016994374947
54+
55+
NORM_CONSTANT2 = 47
56+
NORM_CONSTANT3 = 103
57+
NORM_CONSTANT4 = 30
58+
59+
@numba.njit(fastmath=True, parallel=True)
60+
def extrapolate2(perm, xsb, ysb, dx, dy):
61+
index = perm[(perm[xsb & 0xFF] + ysb) & 0xFF] & 0x0E
62+
g1, g2 = GRADIENTS2[index : index + 2]
63+
return g1 * dx + g2 * dy
64+
65+
@numba.njit(fastmath=True, parallel=True)
66+
def generatekey(x, y, perm):
67+
stretch_offset = (x + y) * STRETCH_CONSTANT2
68+
69+
xs = x + stretch_offset
70+
ys = y + stretch_offset
71+
72+
xsb = floor(xs)
73+
ysb = floor(ys)
74+
75+
squish_offset = (xsb + ysb) * SQUISH_CONSTANT2
76+
xb = xsb + squish_offset
77+
yb = ysb + squish_offset
78+
79+
xins = xs - xsb
80+
yins = ys - ysb
81+
82+
in_sum = xins + yins
83+
84+
dx0 = x - xb
85+
dy0 = y - yb
86+
87+
value = 0
88+
89+
dx1 = dx0 - 1 - SQUISH_CONSTANT2
90+
dy1 = dy0 - 0 - SQUISH_CONSTANT2
91+
attn1 = 2 - dx1 * dx1 - dy1 * dy1
92+
if attn1 > 0:
93+
attn1 *= attn1
94+
value += attn1 * attn1 * extrapolate2(perm, xsb + 1, ysb + 0, dx1, dy1)
95+
96+
dx2 = dx0 - 0 - SQUISH_CONSTANT2
97+
dy2 = dy0 - 1 - SQUISH_CONSTANT2
98+
attn2 = 2 - dx2 * dx2 - dy2 * dy2
99+
if attn2 > 0:
100+
attn2 *= attn2
101+
value += attn2 * attn2 * extrapolate2(perm, xsb + 0, ysb + 1, dx2, dy2)
102+
103+
if in_sum <= 1:
104+
zins = 1 - in_sum
105+
if zins > xins or zins > yins:
106+
if xins > yins:
107+
xsv_ext = xsb + 1
108+
ysv_ext = ysb - 1
109+
dx_ext = dx0 - 1
110+
dy_ext = dy0 + 1
111+
else:
112+
xsv_ext = xsb - 1
113+
ysv_ext = ysb + 1
114+
dx_ext = dx0 + 1
115+
dy_ext = dy0 - 1
116+
else:
117+
xsv_ext = xsb + 1
118+
ysv_ext = ysb + 1
119+
dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT2
120+
dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT2
121+
else:
122+
zins = 2 - in_sum
123+
if zins < xins or zins < yins:
124+
if xins > yins:
125+
xsv_ext = xsb + 2
126+
ysv_ext = ysb + 0
127+
dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT2
128+
dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT2
129+
else:
130+
xsv_ext = xsb + 0
131+
ysv_ext = ysb + 2
132+
dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT2
133+
dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT2
134+
else:
135+
dx_ext = dx0
136+
dy_ext = dy0
137+
xsv_ext = xsb
138+
ysv_ext = ysb
139+
xsb += 1
140+
ysb += 1
141+
dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT2
142+
dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT2
143+
144+
attn0 = 2 - dx0 * dx0 - dy0 * dy0
145+
if attn0 > 0:
146+
attn0 *= attn0
147+
value += attn0 * attn0 * extrapolate2(perm, xsb, ysb, dx0, dy0)
148+
149+
attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext
150+
if attn_ext > 0:
151+
attn_ext *= attn_ext
152+
value += attn_ext * attn_ext * extrapolate2(perm, xsv_ext, ysv_ext, dx_ext, dy_ext)
153+
154+
return value / NORM_CONSTANT2
155+
156+
def overflow(x):
157+
return c_int64(x).value
158+
159+
def getseed(seed):
160+
perm = np.zeros(256, dtype=np.int64)
161+
perm_grad_index3 = np.zeros(256, dtype=np.int64)
162+
source = np.arange(256)
163+
164+
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
165+
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
166+
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
167+
for i in range(255, -1, -1):
168+
seed = overflow(seed * 6364136223846793005 + 1442695040888963407)
169+
r = int((seed + 31) % (i + 1))
170+
if r < 0:
171+
r += i + 1
172+
perm[i] = source[r]
173+
174+
perm_grad_index3[i] = int((perm[i] % (len(GRADIENTS3) / 3)) * 3)
175+
source[r] = source[i]
176+
177+
return perm
178+
179+
pygame.init()
180+
181+
def pilImageToSurface(pilImage):
182+
return pygame.image.fromstring(
183+
pilImage.tobytes(),
184+
pilImage.size,
185+
pilImage.mode)
186+
187+
def gradientRect( window, left_colour, right_colour, target_rect, c7 ):
188+
""" Draw a horizontal-gradient filled rectangle covering <target_rect> """
189+
colour_rect = pygame.Surface( ( 2, 2 ) ) # tiny! 2x2 bitmap
190+
pygame.draw.line( colour_rect, left_colour, ( 0,0 ), ( 0,1 ) ) # left colour line
191+
pygame.draw.line( colour_rect, right_colour, ( 1,0 ), ( 1,1 ) ) # right colour line
192+
colour_rect = pygame.transform.smoothscale( colour_rect, ( target_rect.width, target_rect.height ) ) # stretch!
193+
new_rect = pygame.transform.rotate(colour_rect, c7)
194+
window.blit( new_rect, target_rect )
195+
196+
clock = pygame.time.Clock()
197+
198+
pygame.display.set_caption("Its EASTER!!!!")
199+
200+
i = 0
201+
202+
c1_seed = getseed(int(random.random()*200))
203+
c2_seed = getseed(int(random.random()*200))
204+
c3_seed = getseed(int(random.random()*200))
205+
206+
c4_seed = getseed(int(random.random()*200))
207+
c5_seed = getseed(int(random.random()*200))
208+
c6_seed = getseed(int(random.random()*200))
209+
210+
c7_seed = getseed(int(random.random()*200))
211+
212+
c8_seed = getseed(int(random.random()*200))
213+
c9_seed = getseed(int(random.random()*200))
214+
215+
c1, c2, c3 = generatekey(i, 0, c1_seed), generatekey(i, 0, c2_seed), generatekey(i, 0, c3_seed)
216+
c4, c5, c6 = generatekey(i, 0, c4_seed), generatekey(i, 0, c5_seed), generatekey(i, 0, c6_seed)
217+
218+
flags = pygame.DOUBLEBUF | pygame.FULLSCREEN
219+
display = pygame.display.set_mode((1920, 1080), flags, 16)
220+
221+
c7 = generatekey(i, 0, c7_seed)
222+
while True:
223+
#gradientRect(display, [int(c1), int(c2), int(c3)], [int(c4), int(c5), int(c6)], rect, c7)
224+
pygame.display.set_caption(f"Its EASTER!!!! {clock.get_fps()}")
225+
226+
for event in pygame.event.get():
227+
if event.type == pygame.QUIT:
228+
pygame.quit()
229+
quit()
230+
231+
if event.type == pygame.KEYDOWN:
232+
if event.key == pygame.K_ESCAPE:
233+
pygame.quit()
234+
quit()
235+
236+
c1 += generatekey(i, 0, c1_seed)*random.random()*5
237+
c2 += generatekey(i, 0, c2_seed)*random.random()*5
238+
c3 += generatekey(i, 0, c3_seed)*random.random()*5
239+
240+
'''c4 += generatekey(i, 0, c4_seed)*random.random()*5
241+
c5 += generatekey(i, 0, c5_seed)*random.random()*5
242+
c6 += generatekey(i, 0, c6_seed)*random.random()*5
243+
244+
c7 += generatekey(i, 0, c7_seed)*random.random()*5'''
245+
246+
if c1 > 255:
247+
c1 = 255
248+
elif c1 < 100:
249+
c1 = 100
250+
251+
if c2 > 255:
252+
c2 = 255
253+
elif c2 < 0:
254+
c2 = 0
255+
256+
if c3 > 255:
257+
c3 = 255
258+
elif c3 < 0:
259+
c3 = 0
260+
261+
if c4 > 255:
262+
c4 = 255
263+
elif c4 < 100:
264+
c4 = 100
265+
266+
if c5 > 255:
267+
c5 = 255
268+
elif c5 < 0:
269+
c5 = 0
270+
271+
if c6 > 255:
272+
c6 = 255
273+
elif c6 < 0:
274+
c6 = 0
275+
276+
res = 8
277+
for x in range(int(display.get_width()/res)):
278+
for y in range(int(display.get_height()/res)):
279+
g = generatekey(x/30, y/30+i, c8_seed)
280+
if g > 0:
281+
size = res
282+
rect = pygame.Rect(x*size-size/2, y*size-size/2, size, size)
283+
cq_1 = int(c1/(1-g))
284+
cq_2 = int(c2/(1-g))
285+
cq_3 = int(c3/(1-g))
286+
287+
if cq_1 > 255:
288+
cq_1 = 255
289+
elif cq_1 < 0:
290+
cq_1 = 0
291+
292+
if cq_2 > 255:
293+
cq_2 = 255
294+
elif cq_2 < 0:
295+
cq_2 = 0
296+
297+
if cq_3 > 255:
298+
cq_3 = 255
299+
elif cq_3 < 0:
300+
cq_3 = 0
301+
bdr = 0
302+
pygame.draw.rect(display, [cq_1, cq_2, cq_3], rect, border_radius=bdr)
303+
304+
#display.blit(image, (0, 0))
305+
306+
pygame.display.flip()
307+
clock.tick(60)
308+
i += 0.007 #0.002

0 commit comments

Comments
 (0)