-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshadertoy_glsl_mouse_event.py
66 lines (51 loc) · 1.97 KB
/
shadertoy_glsl_mouse_event.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
from wgpu_shadertoy import Shadertoy
# shadertoy source: https://www.shadertoy.com/view/Mss3zH by iq CC-BY-NC-SA-3.0
shader_code = """
// Created by inigo quilez - iq/2013
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org/
// Shows how to use the mouse input (only left button supported):
//
// mouse.xy = mouse position during last button down
// abs(mouse.zw) = mouse position during last button click
// sign(mouze.z) = button is down
// sign(mouze.w) = button is clicked
// See also:
//
// Input - Keyboard : https://www.shadertoy.com/view/lsXGzf
// Input - Microphone : https://www.shadertoy.com/view/llSGDh
// Input - Mouse : https://www.shadertoy.com/view/Mss3zH
// Input - Sound : https://www.shadertoy.com/view/Xds3Rr
// Input - SoundCloud : https://www.shadertoy.com/view/MsdGzn
// Input - Time : https://www.shadertoy.com/view/lsXGz8
// Input - TimeDelta : https://www.shadertoy.com/view/lsKGWV
// Inout - 3D Texture : https://www.shadertoy.com/view/4llcR4
float distanceToSegment( vec2 a, vec2 b, vec2 p )
{
vec2 pa = p - a, ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord / iResolution.x;
vec2 cen = 0.5*iResolution.xy/iResolution.x;
vec4 m = iMouse / iResolution.x;
vec3 col = vec3(0.0);
if( m.z>0.0 ) // button is down
{
float d = distanceToSegment( m.xy, abs(m.zw), p );
col = mix( col, vec3(1.0,1.0,0.0), 1.0-smoothstep(.004,0.008, d) );
}
if( m.w>0.0 ) // button click
{
col = mix( col, vec3(1.0,1.0,1.0), 1.0-smoothstep(0.1,0.105, length(p-cen)) );
}
col = mix( col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.03,0.035, length(p- m.xy )) );
col = mix( col, vec3(0.0,0.0,1.0), 1.0-smoothstep(0.03,0.035, length(p-abs(m.zw))) );
fragColor = vec4( col, 1.0 );
}
"""
shader = Shadertoy(shader_code)
if __name__ == "__main__":
shader.show()