Debian Codegraph @

demos/gtk-demo/cogs.glsl

raw
1 // Originally from: https://www.shadertoy.com/view/3ljyDD
2 // License CC0: Hexagonal tiling + cog wheels
3 // Nothing fancy, just hexagonal tiling + cog wheels
4
5 #define PI 3.141592654
6 #define TAU (2.0*PI)
7 #define MROT(a) mat2(cos(a), sin(a), -sin(a), cos(a))
8
9 float hash(in vec2 co) {
10 return fract(sin(dot(co.xy ,vec2(12.9898,58.233))) * 13758.5453);
11 }
12
13 float pcos(float a) {
14 return 0.5 + 0.5*cos(a);
15 }
16
17 void rot(inout vec2 p, float a) {
18 float c = cos(a);
19 float s = sin(a);
20 p = vec2(c*p.x + s*p.y, -s*p.x + c*p.y);
21 }
22
23 float modPolar(inout vec2 p, float repetitions) {
24 float angle = 2.0*PI/repetitions;
25 float a = atan(p.y, p.x) + angle/2.;
26 float r = length(p);
27 float c = floor(a/angle);
28 a = mod(a,angle) - angle/2.;
29 p = vec2(cos(a), sin(a))*r;
30 // For an odd number of repetitions, fix cell index of the cell in -x direction
31 // (cell index would be e.g. -5 and 5 in the two halves of the cell):
32 if (abs(c) >= (repetitions/2.0)) c = abs(c);
33 return c;
34 }
35
36 float pmin(float a, float b, float k) {
37 float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
38 return mix( b, a, h ) - k*h*(1.0-h);
39 }
40
41 const vec2 sz = vec2(1.0, sqrt(3.0));
42 const vec2 hsz = 0.5*sz;
43 const float smallCount = 16.0;
44
45 vec2 hextile(inout vec2 p) {
46 // See Art of Code: Hexagonal Tiling Explained!
47 // https://www.youtube.com/watch?v=VmrIDyYiJBA
48
49 vec2 p1 = mod(p, sz)-hsz;
50 vec2 p2 = mod(p - hsz*1.0, sz)-hsz;
51 vec2 p3 = mix(p2, p1, vec2(length(p1) < length(p2)));
52 vec2 n = p3 - p;
53 p = p3;
54
55 return n;
56 }
57
58 float circle(vec2 p, float r) {
59 return length(p) - r;
60 }
61
62 float box(vec2 p, vec2 b) {
63 vec2 d = abs(p)-b;
64 return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
65 }
66
67 float unevenCapsule(vec2 p, float r1, float r2, float h) {
68 p.x = abs(p.x);
69 float b = (r1-r2)/h;
70 float a = sqrt(1.0-b*b);
71 float k = dot(p,vec2(-b,a));
72 if( k < 0.0 ) return length(p) - r1;
73 if( k > a*h ) return length(p-vec2(0.0,h)) - r2;
74 return dot(p, vec2(a,b) ) - r1;
75 }
76
77 float cogwheel(vec2 p, float innerRadius, float outerRadius, float cogs, float holes) {
78 float cogWidth = 0.25*innerRadius*TAU/cogs;
79
80 float d0 = circle(p, innerRadius);
81
82 vec2 icp = p;
83 modPolar(icp, holes);
84 icp -= vec2(innerRadius*0.55, 0.0);
85 float d1 = circle(icp, innerRadius*0.25);
86
87 vec2 cp = p;
88 modPolar(cp, cogs);
89 cp -= vec2(innerRadius, 0.0);
90 float d2 = unevenCapsule(cp.yx, cogWidth, cogWidth*0.75, (outerRadius-innerRadius));
91
92 float d3 = circle(p, innerRadius*0.20);
93
94 float d = 1E6;
95 d = min(d, d0);
96 d = pmin(d, d2, 0.5*cogWidth);
97 d = min(d, d2);
98 d = max(d, -d1);
99 d = max(d, -d3);
100
101 return d;
102 }
103
104 float ccell1(vec2 p, float r) {
105 float d = 1E6;
106 const float bigCount = 60.0;
107
108 vec2 cp0 = p;
109 rot(cp0, -iTime*TAU/bigCount);
110 float d0 = cogwheel(cp0, 0.36, 0.38, bigCount, 5.0);
111
112 vec2 cp1 = p;
113 float nm = modPolar(cp1, 6.0);
114
115 cp1 -= vec2(0.5, 0.0);
116 rot(cp1, 0.2+TAU*nm/2.0 + iTime*TAU/smallCount);
117 float d1 = cogwheel(cp1, 0.11, 0.125, smallCount, 5.0);
118
119 d = min(d, d0);
120 d = min(d, d1);
121 return d;
122 }
123
124 float ccell2(vec2 p, float r) {
125 float d = 1E6;
126 vec2 cp0 = p;
127 float nm = modPolar(cp0, 6.0);
128 vec2 cp1 = cp0;
129 const float off = 0.275;
130 const float count = smallCount + 2.0;
131 cp0 -= vec2(off, 0.0);
132 rot(cp0, 0.+TAU*nm/2.0 - iTime*TAU/count);
133 float d0 = cogwheel(cp0, 0.09, 0.105, count, 5.0);
134
135
136 cp1 -= vec2(0.5, 0.0);
137 rot(cp1, 0.2+TAU*nm/2.0 + iTime*TAU/smallCount);
138 float d1 = cogwheel(cp1, 0.11, 0.125, smallCount, 5.0);
139
140 float l = length(p);
141 float d2 = l - (off+0.055);
142 float d3 = d2 + 0.020;;
143
144 vec2 tp0 = p;
145 modPolar(tp0, 60.0);
146 tp0.x -= off;
147 float d4 = box(tp0, vec2(0.0125, 0.005));
148
149 float ctime = -(iTime*0.05 + r)*TAU;
150
151 vec2 tp1 = p;
152 rot(tp1, ctime*12.0);
153 tp1.x -= 0.13;
154 float d5 = box(tp1, vec2(0.125, 0.005));
155
156 vec2 tp2 = p;
157 rot(tp2, ctime);
158 tp2.x -= 0.13*0.5;
159 float d6 = box(tp2, vec2(0.125*0.5, 0.0075));
160
161 float d7 = l - 0.025;
162 float d8 = l - 0.0125;
163
164 d = min(d, d0);
165 d = min(d, d1);
166 d = min(d, d2);
167 d = max(d, -d3);
168 d = min(d, d4);
169 d = min(d, d5);
170 d = min(d, d6);
171 d = min(d, d7);
172 d = max(d, -d8);
173
174 return d;
175 }
176
177 float df(vec2 p, float scale, inout vec2 nn) {
178 p /= scale;
179 nn = hextile(p);
180 nn = floor(nn + 0.5);
181 float r = hash(nn);
182
183 float d;;
184
185 if (r < 0.5) {
186 d = ccell1(p, r);
187 } else {
188 d = ccell2(p, r);
189 }
190
191 return d*scale;
192 }
193
194 vec3 postProcess(vec3 col, vec2 q) {
195 //col = saturate(col);
196 col=pow(clamp(col,0.0,1.0),vec3(0.75));
197 col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast
198 col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation
199 col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting
200 return col;
201 }
202
203 void mainImage(out vec4 fragColor, vec2 fragCoord) {
204 vec2 q = fragCoord/iResolution.xy;
205 vec2 p = -1.0 + 2.0*q;
206 p.x *= iResolution.x/iResolution.y;
207 float tm = iTime*0.1;
208 p += vec2(cos(tm), sin(tm*sqrt(0.5)));
209 float z = mix(0.5, 1.0, pcos(tm*sqrt(0.3)));
210 float aa = 4.0 / iResolution.y;
211
212 vec2 nn = vec2(0.0);
213 float d = df(p, z, nn);
214
215 vec3 col = vec3(160.0)/vec3(255.0);
216 vec3 baseCol = vec3(0.3);
217 vec4 logoCol = vec4(baseCol, 1.0)*smoothstep(-aa, 0.0, -d);
218 col = mix(col, logoCol.xyz, pow(logoCol.w, 8.0));
219 col += 0.4*pow(abs(sin(20.0*d)), 0.6);
220
221 col = postProcess(col, q);
222
223 fragColor = vec4(col, 1.0);
224 }