forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadertoy-template.py
executable file
·68 lines (58 loc) · 2.28 KB
/
shadertoy-template.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
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
""" Shadertoy template to test a shadertoy from www.shadertoy.com """
import datetime
import time
import numpy as np
from glumpy import app, gl, gloo
vertex = """
attribute vec2 position;
void main (void)
{
gl_Position = vec4(position, 0.0, 1.0);
}
"""
fragment = """
uniform vec3 iResolution; // Viewport resolution (in pixels)
uniform float iTime; // Shader playback time (in seconds)
uniform vec4 iMouse; // Mouse pixel coords. xy: current (if MLB down) + zw: click
uniform vec4 iDate; // Date as (year, month, day, time in seconds)
// uniform float iChannelTime[4]; // Channel playback time (in seconds)
// uniform vec3 iChannelResolution[4]; // Channel resolution (in pixels)
// uniform sampler2D iChannel[4]; // Input channel. (XX = 2D or Cube)
void mainImage(out vec4 fragColor, in vec2 fragCoord);
void main(void)
{
mainImage(gl_FragColor, gl_FragCoord.xy);
}
// Put your shadertoy code here
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord.xy / iResolution.xy;
fragColor = vec4(uv,0.5*sin(1.0+iTime),1.0);
}
"""
start_time = time.time()
window = app.Window(width=800, height=800)
@window.event
def on_draw(dt):
window.clear()
program.draw(gl.GL_TRIANGLE_STRIP)
program["iTime"] = start_time - time.time()
today = datetime.datetime.now()
seconds = (today.hour*60*60 + today.minute*60 + today.second)
program["iDate"] = today.year, today.month, today.day, seconds
@window.event
def on_resize(width, height):
program["iResolution"] = width, height, 0
@window.event
def on_mouse_drag(x, y, dx, dy, button):
buttons = {app.window.mouse.NONE : 0, app.window.mouse.LEFT : 1,
app.window.mouse.MIDDLE : 2, app.window.mouse.RIGHT : 3 }
program["iMouse"] = x, y, buttons[button], 0
program = gloo.Program(vertex, fragment, count=4)
program['position'] = [(-1,-1), (-1,+1), (+1,-1), (+1,+1)]
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
app.run(framerate=60)