Debian Codegraph @

screensaver.shadertoy/resources/shaders/pixellated.frag.glsl

raw
1 // from https://www.shadertoy.com/view/4tjGWy
2
3 // sin() that returns 0-1
4 float nsin(float a)
5 {
6 return (sin(a)+1.)/2.;
7 }
8 float ncos(float a)
9 {
10 return (cos(a)+1.)/2.;
11 }
12
13
14 // return 0-1
15 float plasma_a(vec2 uv, float t, vec2 lbound, vec2 ubound)
16 {
17 vec2 p1 = vec2(nsin(t * 1.3), nsin(t * 1.9));
18 vec2 p2 = vec2(nsin(t * 1.2), nsin(t * 2.2));
19 p1 = (p1 * (ubound - lbound)) + lbound;
20 p2 = (p2 * (ubound - lbound)) + lbound;
21
22 return
23 (nsin(length(p1 - uv))
24 + nsin(length(p2 - uv))
25 + nsin(uv.x / 3.)
26 + nsin(uv.y / 2.)
27 ) / 4.
28 ;
29 }
30
31 // like smootherstep, but returns 0.0 at both edges, and 1.0 in the center (instead of a ramp, it's a hill)
32 float tri_step(float lbound, float ubound, float val)
33 {
34 float halfRange = (ubound - lbound) / 2.0;
35 val -= lbound;// shift down to 0.0 - range
36 val -= halfRange;// shift down to -halfrange - halfrange
37 val = abs(val);// make inverted triangle
38 val = halfRange - val;// invert it so it's the kind of triangle we want (0 at the ends)
39 val = val / halfRange;// scale triangle to 0-1
40 val = clamp(val, 0.0, 1.0);
41 return val;
42 }
43
44
45 // convert a 1D value to color, mixing channels
46 vec3 a_to_color(float a)
47 {
48 return vec3(
49 tri_step(0.,0.75, 1.-a),
50 tri_step(0.12,0.95, 1.-a),
51 tri_step(0.4,1.0, 1.-a)
52 );
53 }
54
55
56 vec2 getuv_centerX(vec2 fragCoord, vec2 newTL, vec2 newSize, out float vignetteAmt)
57 {
58 vec2 ret = vec2(fragCoord.x / iResolution.x, (iResolution.y - fragCoord.y) / iResolution.y);// ret is now 0-1 in both dimensions
59
60 // vignette. return 0-1
61 vec2 vignetteCenter = vec2(0.5, 0.5);// only makes sense 0-1 values here
62 if(iMouse.z > 0.)
63 vignetteCenter = vec2(iMouse.x / iResolution.x, (iResolution.y - iMouse.y) / iResolution.y);// ret is now 0-1 in both dimensions;
64 vignetteAmt = 1.0 - distance(ret, vignetteCenter);
65 // vignetteAmt = pow(vignetteAmt, 1.);
66
67 ret *= newSize;// scale up to new dimensions
68 float aspect = iResolution.x / iResolution.y;
69 ret.x *= aspect;// orig aspect ratio
70 float newWidth = newSize.x * aspect;
71 return ret + vec2(newTL.x - (newWidth - newSize.x) / 2.0, newTL.y);
72 }
73
74
75
76 void mainImage( out vec4 fragColor, in vec2 fragCoord )
77 {
78 vec2 lbound = vec2(0., 0.);
79 vec2 ubound = vec2(10., 10.);
80 float vignetteAmt;
81 vec2 uv = getuv_centerX(fragCoord, vec2(0.), vec2(10.), vignetteAmt);
82
83 // background
84 fragColor = vec4(1.,1.,1.,1.);
85
86 // pixellate effect
87 //const float pixelCount = 18.;
88 float pixelSize = 44./iResolution.x * ubound.x;// pixels wide always.
89
90 if(iMouse.x > 0.)
91 pixelSize = (iMouse.x+12.)/iResolution.x * ubound.x;// pixels wide always.
92
93 vec2 plasma_uv = floor((uv / pixelSize) + 0.5) * pixelSize;// pixellated uv coords
94
95 // plasma
96 float a = plasma_a(plasma_uv, iTime, lbound, ubound);
97 fragColor = vec4(a_to_color(a), 1.0);
98
99 // distance to pixel center
100 const float pi2 = 3.14159 * 2.0;
101 float pixelBorderFX = ncos(uv.x / pixelSize * pi2);
102 pixelBorderFX = min(pixelBorderFX, ncos(uv.y / pixelSize * pi2));
103 pixelBorderFX = pow(pixelBorderFX, 0.1);
104 fragColor.rgb *= pixelBorderFX;
105
106 // apply vignette
107 fragColor.rgb *= ((vignetteAmt + 0.1) * 2.) - 0.3;
108 fragColor.rgb = clamp(fragColor.rgb, 0.,1.);
109 }