Files
forge-invar/src/forge_invar/glsl_funcs.py
T

122 lines
2.3 KiB
Python

from math import cos, floor, sin
# glsl base
PI = 3.1415927
def mod(x: float, y: float) -> float:
return x % y
def fract(x: float) -> float:
return mod(x, 1.0)
def mix(x: float, y: float, a: float) -> float:
if a <= 0.0:
return x
if a >= 1.0:
return y
return x + (y - x) * a
def step(edge: float, x: float) -> float:
if x < edge:
return 0.0
return 1.0
# inc_rand.glsl
RAND_C = 43758.5453
def rand(seed: float) -> float:
return fract(sin(seed) * RAND_C)
# inc_time.glsl
def rand_time(itime: float, ibeats: float, seed: float, beats: int = 4) -> float:
return rand(seed + floor(ibeats / beats))
def divider(x: float) -> float:
if x < 0.1:
return 0.0
if x < 0.2:
return 0.125
if x < 0.3:
return 0.25
if x < 0.4:
return 0.5
if x < 0.6:
return 1.0
if x < 0.8:
return 2.0
return 4.0
def mod_time(ibeats: float, k: float, k2: float = 1.0) -> float:
return mod(divider(k) * ibeats * k2 * 0.25, 1.0)
def cos_time(ibeats: float, k: float) -> float:
return cos(mod_time(ibeats, k, 5.0) * 2.0 * PI)
# inc_magic.glsl
def magic_f(
itime: float,
ibeats: float,
f: tuple[float, float, float],
b: tuple[float, float, float],
i: float,
) -> tuple[float, float]:
return (
mix(f[0], rand_time(itime, ibeats, i + 1.0), min(1.0, b[2])),
mix(f[1], rand_time(itime, ibeats, i + 2.0), min(1.0, b[2])),
)
def magic_b(
itime: float, ibeats: float, b: tuple[float, float, float], i: float
) -> tuple[float, float, float]:
return (
mix(b[0], step(0.2, rand_time(itime, ibeats, i + 3.0)), min(1.0, b[2])),
mix(b[1], step(0.5, rand_time(itime, ibeats, i + 4.0)), min(1.0, b[2])),
min(1.0, b[2]),
)
def magic_trigger(
itime: float, ibeats: float, b: tuple[float, float, float], i: float
) -> bool:
return magic_b(itime, ibeats, b, i)[0] > 0.0
def magic(
itime: float,
ibeats: float,
f: tuple[float, float, float],
b: tuple[float, float, float],
i: float,
) -> float:
mf = magic_f(itime, ibeats, f, b, i)
mb = magic_b(itime, ibeats, b, i)
return mix(
0.0,
mf[0]
* mix(
1.0 - mod_time(ibeats, mf[1]),
cos_time(ibeats, mf[1]) * 0.5 + 0.5,
mb[1],
),
mb[0],
)