distraction/shaders/eyeblower.py
2020-02-12 13:11:57 +01:00

41 lines
1.1 KiB
Python

import math
from pixel import Pixel
FREQUENCY = 20.0
COLOR_A = (1.0, 0.5, 0.9)
COLOR_B = (0.5, 0.1, 0.3)
def distance(a: tuple, b: tuple) -> float:
if len(a) != len(b):
raise TypeError("Mismatching dimentional vector passed")
dist = 0
for i in range(len(a)):
d = b[i] - a[i]
dist += d * d
return dist ** 0.5
def sign(v: float) -> float:
return 1.0 if v >= 0 else 0.0
def main(pixel: Pixel, resolution: tuple, frag_coord: tuple, time: float): # https://www.shadertoy.com/view/ldX3DN
uv = tuple(-1.0 + 2.0 * x for x in (frag_coord[0] / resolution[0], frag_coord[1] / resolution[1]))
homo_coords = (uv[0], 2.0 * frag_coord[1] / resolution[0])
moving_origin1 = (math.sin(time * 0.7), math.sin(time * 1.7))
moving_origin2 = (-math.cos(time * 2.0), -math.sin(time * 3.0))
wave_point1 = math.sin( distance(moving_origin1, homo_coords) * FREQUENCY )
wave_point2 = math.sin( distance(moving_origin2, homo_coords) * FREQUENCY )
black_or_white1 = sign(wave_point1)
black_or_white2 = sign(wave_point2)
composite = black_or_white1 * black_or_white2
col = tuple(max(COLOR_A[i] * composite, COLOR_B[i]) for i in range(3))
pixel.background_color.set( *col )