]> 4ch.mooo.com Git - plz.git/blob - plzpart-gl/plz.glsl
wwww
[plz.git] / plzpart-gl / plz.glsl
1 precision mediump float;
2
3 uniform vec3    iResolution;            // viewport resolution (in pixels)
4 uniform float   iGlobalTime;            // shader playback time (in seconds)
5
6 #define PI 3.1415926535
7 #define DPII (PI*2.0)
8
9 int GetPerPlasmaTime(int t)
10 {
11         // Get the time since this plasma started using the total time.
12         // Times for each plasma are (from the timetable array): 723, 1491,
13         // 1875, 2259, 2278.  (64 is added to each time because it takes 64
14         // frames to drop the current plasma offscreen before switching to
15         // the new one.)
16
17         if (t < 723+64) {
18
19         } else if (t < 1491+64) {
20                 t -= 723+64;
21         } else if (t < 1875+64) {
22                 t -= 1491+64;
23         } else if (t < 2259+64) {
24                 t -= 1875+64;
25         } else if (t < 2278+64) {
26                 t -= 2259+64;
27         } else {
28                 t = 0;
29         }
30
31         return t;
32 }
33
34 int GetPlasmaIndex(int t)
35 {
36         int nPlasmaIndex = 0;
37
38         if (t < 723+64) {
39                 nPlasmaIndex = 0;
40         } else if (t < 1491+64) {
41                 nPlasmaIndex = 1;
42         } else if (t < 1875+64) {
43                 nPlasmaIndex = 2;
44         }
45
46         return nPlasmaIndex;
47 }
48
49 int GetYOffset(int t)
50 {
51         int nResult = 60;
52         bool bDrop = false;
53
54         if ( (t >= 723) && (t < (723+64)) ) {
55                 t -= 723;
56                 bDrop = true;
57         } else if ( (t >= 1491) && (t < (1491+64)) ) {
58                 t -= 1491;
59                 bDrop = true;
60         } else if ( (t >= 1875) && (t < (1875+64)) ) {
61                 t -= 1875;
62                 bDrop = true;
63         } else if ( (t >= 2259) && (t < (2259+64)) ) {
64                 t -= 2259;
65                 bDrop = true;
66         }
67
68         if (bDrop) {
69                 nResult = t * t / 4 * 43 / 128 + 60;
70         }
71
72         return nResult;
73 }
74
75 float FadeColourComponent(float c, int t)
76 {
77         float fResult = c;
78         float fProportion = 0.0;
79
80         if ((t >= 0) && (t < 128)) {
81                 // For plasma 1, fade from white to the palette.
82                 fProportion = float(t) / 127.0;
83                 fResult = (fProportion * c) + ((1.0 - fProportion) * 1.0);
84         } else if ( (t >= (723+64)) && (t < (723+64+32)) ) {
85                 // For plasma 2, fade from black to the palette.
86                 fProportion = float(t-723-64) / 31.0;
87                 fResult = (fProportion * c) + ((1.0 - fProportion) * 0.0);
88         } else if ( (t >= (1491+64)) && (t < (1491+64+32)) ) {
89                 // For plasma 3, fade from black to the palette.
90                 fProportion = float(t-1491-64) / 31.0;
91                 fResult = (fProportion * c) + ((1.0 - fProportion) * 0.0);
92         }
93
94         return fResult;
95 }
96
97 int GetInitialL1(int nPlasmaIndex)
98 {
99         if (nPlasmaIndex == 0) {
100                 return 1000;
101         } else if (nPlasmaIndex == 1) {
102                 return 1000;
103         } else /* if (nPlasmaIndex == 2) */ {
104                 return 3500;
105         }
106 }
107 int GetInitialL2(int nPlasmaIndex)
108 {
109         if (nPlasmaIndex == 0) {
110                 return 2000;
111         } else if (nPlasmaIndex == 1) {
112                 return 2000;
113         } else /* if (nPlasmaIndex == 2) */ {
114                 return 1000;
115         }
116 }
117
118 int GetInitialL3(int nPlasmaIndex)
119 {
120         if (nPlasmaIndex == 0) {
121                 return 3000;
122         } else if (nPlasmaIndex == 1) {
123                 return 4000;
124         } else /* if (nPlasmaIndex == 2) */ {
125                 return 3000;
126         }
127 }
128
129 int GetInitialL4(int nPlasmaIndex)
130 {
131         if (nPlasmaIndex == 0) {
132                 return 4000;
133         } else if (nPlasmaIndex == 1) {
134                 return 4000;
135         } else /* if (nPlasmaIndex == 2) */ {
136                 return 1000;
137         }
138 }
139
140 int GetInitialK1(int nPlasmaIndex)
141 {
142         if (nPlasmaIndex == 0) {
143                 return 3500;
144         } else if (nPlasmaIndex == 1) {
145                 return 1500;
146         } else /* if (nPlasmaIndex == 2) */ {
147                 return 3500;
148         }
149 }
150
151 int GetInitialK2(int nPlasmaIndex)
152 {
153         if (nPlasmaIndex == 0) {
154                 return 2300;
155         } else if (nPlasmaIndex == 1) {
156                 return 2300;
157         } else /* if (nPlasmaIndex == 2) */ {
158                 return 3300;
159         }
160 }
161
162 int GetInitialK3(int nPlasmaIndex)
163 {
164         if (nPlasmaIndex == 0) {
165                 return 3900;
166         } else if (nPlasmaIndex == 1) {
167                 return 3900;
168         } else /* if (nPlasmaIndex == 2) */ {
169                 return 2900;
170         }
171 }
172
173 int GetInitialK4(int nPlasmaIndex)
174 {
175         if (nPlasmaIndex == 0) {
176                 return 3670;
177         } else if (nPlasmaIndex == 1) {
178                 return 1670;
179         } else /* if (nPlasmaIndex == 2) */ {
180                 return 2670;
181         }
182 }
183
184 int GetC1(int nXOffset0or1, int t)
185 {
186         int nPlasmaIndex = GetPlasmaIndex(t);
187         t = GetPerPlasmaTime(t);
188
189         int nResult = 0;
190         if (nXOffset0or1 == 0) {
191                 nResult = GetInitialK1(nPlasmaIndex) + (-3 * t);
192         } else {
193                 nResult = GetInitialL1(nPlasmaIndex) + (-1 * t);
194         }
195         
196         float fResult = float(nResult);
197         fResult = mod(fResult, 4096.0);
198
199         if (fResult < 0.0) {
200                 fResult += 4096.0;
201         }
202
203         return int(fResult);
204 }
205
206 int GetC2(int nXOffset0or1, int t)
207 {
208         int nPlasmaIndex = GetPlasmaIndex(t);
209         t = GetPerPlasmaTime(t);
210
211         int nResult = 0;
212         if (nXOffset0or1 == 0) {
213                 nResult = GetInitialK2(nPlasmaIndex) + (-2 * t);
214         } else {
215                 nResult = GetInitialL2(nPlasmaIndex) + (-2 * t);
216         }
217         
218         float fResult = float(nResult);
219         fResult = mod(fResult, 4096.0);
220
221         if (fResult < 0.0) {
222                 fResult += 4096.0;
223         }
224
225         return int(fResult);
226 }
227
228 int GetC3(int nXOffset0or1, int t)
229 {
230         int nPlasmaIndex = GetPlasmaIndex(t);
231         t = GetPerPlasmaTime(t);
232
233         int nResult = 0;
234         if (nXOffset0or1 == 0) {
235                 nResult = GetInitialK3(nPlasmaIndex) + (1 * t);
236         } else {
237                 nResult = GetInitialL3(nPlasmaIndex) + (2 * t);
238         }
239         
240         float fResult = float(nResult);
241         fResult = mod(fResult, 4096.0);
242
243         if (fResult < 0.0) {
244                 fResult += 4096.0;
245         }
246
247         return int(fResult);
248 }
249
250 int GetC4(int nXOffset0or1, int t)
251 {
252         int nPlasmaIndex = GetPlasmaIndex(t);
253         t = GetPerPlasmaTime(t);
254
255         int nResult = 0;
256         if (nXOffset0or1 == 0) {
257                 nResult = GetInitialK4(nPlasmaIndex) + (2 * t);
258         } else {
259                 nResult = GetInitialL4(nPlasmaIndex) + (3 * t);
260         }
261         
262         float fResult = float(nResult);
263         fResult = mod(fResult, 4096.0);
264
265         if (fResult < 0.0) {
266                 fResult += 4096.0;
267         }
268
269         return int(fResult);
270 }
271
272 float Palette0_GetRed(float nIndex)
273 {
274         float a = 0.0;
275
276         if ((nIndex >= 0.0) && (nIndex < 64.0)) {
277                 a = nIndex;
278         } else if ((nIndex >= 64.0) && (nIndex < 128.0)) {
279                 a = 63.0 - (nIndex - 64.0);
280         } else if ((nIndex >= 128.0) && (nIndex < 192.0)) {
281                 a = 0.0;
282         } else {
283                 a = nIndex - 192.0;
284         }
285
286         return (cos(a*DPII/128.0+PI)*31.0+32.0) / 63.0;
287 }
288
289 float Palette0_GetGreen(float nIndex)
290 {
291         return 0.0;
292 }
293
294 float Palette0_GetBlue(float nIndex)
295 {
296         float a = 0.0;
297
298         if ((nIndex >= 0.0) && (nIndex < 128.0)) {
299                 a = 0.0;
300         } else if ((nIndex >= 128.0) && (nIndex < 192.0)) {
301                 a = nIndex - 128.0;
302         } else {
303                 a = 63.0 - (nIndex - 192.0);
304         }
305
306         return (cos(a*DPII/128.0+PI)*31.0+32.0) / 63.0;
307 }
308
309 float Palette1_GetGreen(float nIndex)
310 {
311         float a = 0.0;
312
313         if ((nIndex >= 0.0) && (nIndex < 128.0)) {
314                 a = 0.0;
315         } else if ((nIndex >= 128.0) && (nIndex < 192.0)) {
316                 a = nIndex - 128.0;
317         } else {
318                 a = 63.0;
319         }
320
321         return (cos(a*DPII/128.0+PI)*31.0+32.0) / 63.0;
322 }
323
324 float Palette1_GetBlue(float nIndex)
325 {
326         float a = 0.0;
327
328         if ((nIndex >= 0.0) && (nIndex < 64.0)) {
329                 a = 0.0;
330         } else if ((nIndex >= 64.0) && (nIndex < 128.0)) {
331                 a = nIndex - 64.0;
332         } else if ((nIndex >= 128.0) && (nIndex < 192.0)) {
333                 a = 63.0 - (nIndex - 128.0);
334         } else {
335                 a = nIndex - 192.0;
336         }
337
338         return (cos(a*DPII/128.0+PI)*31.0+32.0) / 63.0;
339 }
340
341 float Palette2_GetRed(float nIndex)
342 {
343         float a = 0.0;
344
345         if ((nIndex >= 0.0) && (nIndex < 64.0)) {
346                 a = 0.0;
347         } else if ((nIndex >= 64.0) && (nIndex < 128.0)) {
348                 a = nIndex - 64.0;
349         } else if ((nIndex >= 128.0) && (nIndex < 192.0)) {
350                 a = 63.0 - (nIndex - 128.0);
351         } else {
352                 a = 0.0;
353         }
354
355         return (cos(a*DPII/128.0+PI)*31.0+32.0) / 63.0 / 2.0;
356 }
357
358 float Palette_GetRed(float nIndex, int t)
359 {
360         int nPlasmaIndex = GetPlasmaIndex(t);
361         t = GetPerPlasmaTime(t);
362         float fResult = 0.0;
363
364         if (nPlasmaIndex == 0) {
365                 fResult = Palette0_GetRed(nIndex);
366         } else if (nPlasmaIndex == 1) {
367                 fResult = Palette0_GetRed(nIndex);
368         } else if (nPlasmaIndex == 2) {
369                 fResult = Palette2_GetRed(nIndex);
370         }
371
372         return fResult;
373 }
374
375 float Palette_GetGreen(float nIndex, int t)
376 {
377         int nPlasmaIndex = GetPlasmaIndex(t);
378         t = GetPerPlasmaTime(t);
379         float fResult = 0.0;
380
381         if (nPlasmaIndex == 0) {
382                 fResult = Palette0_GetGreen(nIndex);
383         } else if (nPlasmaIndex == 1) {
384                 fResult = Palette1_GetGreen(nIndex);
385         } else if (nPlasmaIndex == 2) {
386                 fResult = Palette2_GetRed(nIndex);
387         }
388
389         return fResult;
390 }
391
392 float Palette_GetBlue(float nIndex, int t)
393 {
394         int nPlasmaIndex = GetPlasmaIndex(t);
395         t = GetPerPlasmaTime(t);
396         float fResult = 0.0;
397
398         if (nPlasmaIndex == 0) {
399                 fResult = Palette0_GetBlue(nIndex);
400         } else if (nPlasmaIndex == 1) {
401                 fResult = Palette1_GetBlue(nIndex);
402         } else if (nPlasmaIndex == 2) {
403                 fResult = Palette2_GetRed(nIndex);
404         }
405
406         return fResult;
407 }
408
409 void main(void)
410 {
411         // t is an integer frame count.
412         int t = int(iGlobalTime * 60.0);
413
414         // Wrap t when it exceeds 2323, so that the plasmas will repeat.
415         float fT = float(t);
416         t = int(mod(fT, 1939.0));
417
418         // fPlasmaX is a float from 0 to 319,
419         // fPlasmaY is a float from 0 to 399.
420         float fPlasmaX = (gl_FragCoord.x / iResolution.x) * 319.0;
421         float fPlasmaY = (gl_FragCoord.y / iResolution.y) * 399.0;
422         fPlasmaY = 399.0 - fPlasmaY;
423
424         fPlasmaY -= float(GetYOffset(t));
425         /* fPlasmaX += float(GetXOffset(t)); */
426
427         float fRed = 0.0;
428         float fGreen = 0.0;
429         float fBlue = 0.0;
430
431         if ( (fPlasmaY >= 0.0) && (fPlasmaY < 280.0) ) {
432                 float fScreenX = gl_FragCoord.x;
433                 float fScreenY = gl_FragCoord.y;
434                 float ah = 0.0;
435                 float ccc = fPlasmaX / 4.0;
436                 float fOffset = 0.0;
437                 float bx = 0.0;
438
439                 int nXOffset0or1 = int(mod(fScreenX, 2.0));
440
441                 // On every odd row, toggle the x offset. This causes different
442                 // plasma constants to be read for these pixels, achieving a
443                 // chequered overlay of two plasmas.
444
445                 int xor = int(mod(fScreenY, 2.0));
446                 if (xor == 1) {
447                         if (nXOffset0or1 == 0) {
448                                 nXOffset0or1 = 1;
449                         } else {
450                                 nXOffset0or1 = 0;
451                         }
452                 }
453
454                 float c1 = float(GetC1(nXOffset0or1, t));
455                 float c2 = float(GetC2(nXOffset0or1, t));
456                 float c3 = float(GetC3(nXOffset0or1, t));
457                 float c4 = float(GetC4(nXOffset0or1, t));
458
459                 fOffset = ((fPlasmaY * 2.0) + (c2 * 2.0) - (ccc * 8.0) + (80.0 * 8.0)) / 2.0;
460                 bx = ((sin(fOffset*DPII/4096.0)*55.0+sin(fOffset*DPII/4096.0*4.0)*5.0+sin(fOffset*DPII/4096.0*17.0)*3.0+64.0)*16.0);
461
462                 fOffset = bx + c1 + (ccc * 8.0);
463                 ah = (sin(fOffset*DPII/4096.0)*55.0+sin(fOffset*DPII/4096.0*6.0)*5.0+sin(fOffset*DPII/4096.0*21.0)*4.0+64.0);
464
465                 fOffset = ((fPlasmaY * 2.0) + (c4 * 2.0) + (ccc * 32.0)) / 2.0;
466                 bx = ((sin(fOffset*DPII/4096.0)*55.0+sin(fOffset*DPII/4096.0*5.0)*8.0+sin(fOffset*DPII/4096.0*15.0)*2.0+64.0)*8.0);
467
468                 fOffset = bx + (fPlasmaY * 2.0) + c3 - (ccc * 4.0) + (80.0 * 4.0);
469                 ah += (sin(fOffset*DPII/4096.0)*55.0+sin(fOffset*DPII/4096.0*6.0)*5.0+sin(fOffset*DPII/4096.0*21.0)*4.0+64.0);
470
471                 fRed = Palette_GetRed(ah, t);
472                 fGreen = Palette_GetGreen(ah, t);
473                 fBlue = Palette_GetBlue(ah, t);
474         }
475
476         fRed = FadeColourComponent(fRed, t);
477         fGreen = FadeColourComponent(fGreen, t);
478         fBlue = FadeColourComponent(fBlue, t);
479
480         gl_FragColor = vec4(fRed, fGreen, fBlue, 1.0);
481 }
482