-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
1544 lines (1337 loc) · 52.8 KB
/
index.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
ZzArt - Abstract Art Evolution
© Frank Force 2019
code credits
- download.js v4.21, by dandavis; 2008-2018. [MIT] see http://danml.com/download.html for tests/usage
- Smooth HSV by iq - https://www.shadertoy.com/view/MsS3Wc
- github-corners by Tim Holman - https://github.com/tholman/github-corners
todo
- poster option
- resolution
- 2x3 resolution
- background color
- margin
- line around image
- dpi
- color change button
- black and white
- scale variation button
-->
<!doctype html>
<html>
<head>
<style>
body,select { font-size: 20px; font-family: monospace; color: #FFF; }
input { font-size: 20px; }
a { color:#5AF; }
a:visited { color:#A5A; }
canvas { background-color: #FFF; }
div.satellite
{
display:none;
z-index:100;
position:relative;
-webkit-text-fill-color: white;
-webkit-text-stroke-width: 1px;
-webkit-text-stroke-color: black;
}
select
{
background-color: #000;
}
button
{
font-size: 30px;
vertical-align: middle;
text-align: center;
background-color: #EEE;
margin: 10px;
}
button.small
{
border-radius: 10px;
height:45px;
}
button.satellite
{
background-color: #FFF;
-webkit-text-fill-color: black;
-webkit-text-stroke-width: 1px;
color: #000;
font-size: 30px;
margin-top: 0px;
margin-right: 0px;
margin-left: 0px;
padding: 2px;
width:45px;
height:45px;
border-radius: 10px;
}
input.advanced
{
width:80px;
height:20px;
}
</style>
<title>ZzArt - Abstract Art Evolution</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.png"/>
<meta name="author" content="Frank Force">
<meta charset="utf-8">
</head>
<body bgcolor="#222">
<tag autocomplete=off autocorrect=off autocapitalize=off spellcheck=false/>
<div id=div_satellite class=satellite>
<button id=button_satelliteHelp class=satellite onclick=ButtonSatelliteHelp() title="Help">📡</button>
<button id=button_satelliteSave class=satellite onclick=ButtonSave() title="Save HD Image [S]">💾</button>
<font size=10><b>𝓩𝔃𝓐𝓻𝓽</b> - <span id=span_generationsSatellite>0</span></font><br></div>
<center>
<div id=div_title><font size=5><b>𝓩𝔃𝓐𝓻𝓽</b></font> - <span id=span_generations>0</span></div>
<div id=buttons_top>
<button class=small id=button_preview disabled onclick=ButtonTogglePreview() title="Toggle Preview [Spacebar]">🔍</button>
<button class=small id=button_back disabled onclick=ChangeMemoryLocation(-1) title="Undo [Z]">◄</button>
<button class=small id=button_forward disabled onclick=ChangeMemoryLocation(1) title="Redo [X]">►</button>
<button class=small id=button_save onclick=ButtonSave() title="Save HD Image [S]">💾</button>
<button class=small style="display:none;" id=button_saveFolder onclick=DisplaySaveListPage() title="Show Next Save Folder Page">📁</button>
<button class=small id=button_randomize onclick=ButtonRandomize() title="Randomize [R]">🎲</button>
<button class=small id=button_share onclick=ButtonShare() title="Copy Link To Clipboard">🔗</button>
<button class=small style="display:none;" id=button_seed onclick=ButtonSeed() title="Enter Seed">🌱</button>
<button class=small id=button_openSatellite onclick=ButtonSatellite() title="Open Satellite Preview">📡</button>
<button class=small id=button_advanced onclick=ButtonAdvanced() title="Advanced Controls">🔧</button>
<button class=small id=button_help onclick=ButtonHelp() title="Help">❓</button>
<button class=small style="display:none;" id=button_delete onclick=DeleteSelectedSave() title="Delete Selected Save">␡</button>
<br></div>
<canvas id=canvas_main width=1920 height=1080 style="width:1280px; height:720px; border:2px solid black;"></canvas>
<canvas id=canvas_shader hidden style="width:1280px; height:720px; border:2px solid black;"></canvas>
<canvas id=canvas_save hidden style="width:1280px; height:720px; border:2px solid black;"></canvas>
<br>
<div id=div_credit><font size=3>ZzArt © <a href="http://www.frankforce.com" target="_blank">Frank Force</a> 2019 ☮♥☻␌</font></div>
<div id=div_advanced style="display:none;"><hr><table><tr>
<td>
<center>Shadertoy Compatible GLSL Code
<br><textarea disabled id=textarea_code rows=10 cols=80></textarea>
<br><textarea disabled hidden id=textarea_debug style="color:#F00;" rows=4 cols=160></textarea>
<br>Base 64 JSON
<br><textarea disabled id=textarea_json rows=5 cols=80></textarea>
</center></td>
<td><center>
Saved Shaders
<br><select id=select_saveList onchange=SelectSavedShader(select_saveList.selectedIndex) style="width:350px;overflow:scrollY" size=10></select>
</center></td>
<td style='font-size: 20px; line-height: 40px;'><center>
<input id=checkbox_showWatermark onclick=UpdateUI() type=checkbox title="Toggle Watermark">Show Watermark
<br>Save Scale: <input type=number id=input_saveScale class=advanced value=2>
<br>Grid Size: <input type=number onchange=SetGridSize(parseInt(input_gridSize.value)) id=input_gridSize class=advanced value=5>
<br>Start Iterations: <input type=number id=input_startIterations class=advanced value=1>
<br>Randomize Length: <input type=number id=input_randomizeLength class=advanced value=15>
<br><button onclick=OpenCapJS()>Open in CapJS</button>
<br><button onclick=ExportSaveList()>Export Saves</button>
<br><button onclick=input_importFile.click()>Import Saves</button><input id=input_importFile type="file" style="display:none">
<br><button onclick=DeleteSelectedSave()>Delete Selected</button>
</center></td></tr></table></div>
</center>
<a href="https://github.com/KilledByAPixel/ZzArt" target="_blank" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#5AF; color:#222; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<script>
"use strict"; // strict mode
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// global variables
let canvasContext_main = canvas_main.getContext('2d');
let canvasContext_save = canvas_save.getContext('2d');
let canvasContext_shader = canvas_shader.getContext('webgl');
let defaultCanvasWidth = canvas_main.width;
let defaultCanvasHeight = canvas_main.height;
let shaderMemory = [];
let shaderGrid = [];
let gridSize = 5;
let favoriteShader = 0;
let shaderMemoryLocation = 0;
let showPreview = 0;
let advancedMode = 0;
let rotateCanvas = 0;
let dataVersion = 3;
let itchMode = 0;
let satelliteMode = 0;
let startIterations = 1;
let uniqueID = 0;
let isInit = 0;
let maxIterations = 9;
function Init()
{
isInit = 1;
uniqueID = RandSeeded();
InitWebgl();
shaderMemory.push(new ShaderObject());
if (!itchMode)
LoadFromURL();
LoadLocalStorage();
LoadSavedShaderList();
for(let i=0; i<gridSize; i++)
shaderGrid[i] = [];
if (satelliteMode)
InitSatelliteMode();
else
{
if (favoriteShader)
shaderMemory[shaderMemoryLocation] = favoriteShader;
SetFavoriteFromMemory();
if (!itchMode && IsMobile())
TryToRotate();
DrawShaders();
UpdateUI();
}
isInit = 0;
}
function InitSatelliteMode()
{
// redraw the favorite shader
canvas_shader.width = canvas_main.width;
canvas_shader.height = canvas_main.height;
favoriteShader.randSeed = 0;
// set ui for satellite mode
canvas_shader.style = 'position:absolute; left:0px; top:0px;width:100%;height:100%'
canvas_shader.style.zIndex = 10;
canvas_main.hidden = 1;
canvas_shader.hidden = 0;
buttons_top.style.display = 'none';
div_credit.style.display = 'none';
div_title.style.display = 'none';
div_title.style.display = 'none';
div_satellite.style.display = 'inline';
favoriteShader.Render();
setInterval(UpdateSatelliteMode, 100);
}
function UpdateSatelliteMode()
{
// continuous poll favorite from local storage
let localStorageItem = localStorage.saveData;
if (localStorageItem)
{
let saveData = JSON.parse(localStorage.saveData);
let rawObject = saveData.favorite;
if (rawObject.uniqueID != favoriteShader.uniqueID)
{
// redraw new favorite
favoriteShader = Object.assign(new ShaderObject(), rawObject).Clone();
favoriteShader.Render();
span_generationsSatellite.innerHTML = favoriteShader.GetGenerationString();
}
}
}
function TryToRotate()
{
// rotate canvas if window is more vertical then horizontal
let newRotateCanvas = window.innerHeight > window.innerWidth;
if (rotateCanvas == newRotateCanvas)
return;
rotateCanvas = newRotateCanvas;
if (rotateCanvas)
{
canvas_main.width = defaultCanvasHeight;
canvas_main.height = defaultCanvasWidth;
}
else
{
canvas_main.width = defaultCanvasWidth;
canvas_main.height = defaultCanvasHeight;
}
}
function RandomizeShaders()
{
saveListIndex = 0;
favoriteShader = new ShaderObject();
for(let i=9;i--;) Rand(); // warm up random number generator
for(let X=0; X<gridSize; X++)
for(let Y=0; Y<gridSize; Y++)
{
let shader = shaderGrid[X][Y] = favoriteShader.Clone();
shader.SetGridPos(X,Y);
shader.Randomize();
}
}
function DrawShaders()
{
let x = canvasContext_main;
let c = canvas_main;
c.width|=0;
let gap = 10;
let G = gridSize;
let SX = (c.width-gap)/G;
let SY = (c.height-gap)/G;
let W = (c.width - gap*(gridSize+1))/G;
let H = (c.height - gap*(gridSize+1))/G;
// use small hight for previews
canvas_shader.width = W;
canvas_shader.height = H;
for(let X=0; X<G; X++)
for(let Y=0; Y<G; Y++)
{
let shader = shaderGrid[X][Y];
shader.Render();
let posX = gap+SX*X;
let posY = gap+SY*Y;
x.drawImage(canvas_shader, posX, posY, W, H);
x.beginPath()
x.rect(posX,posY,W,H);
x.lineWidth=2;
x.strokeStyle='#000';
x.stroke();
}
if (favoriteShader.gridPosX >=0 && favoriteShader.gridPosY >=0)
{
let posX = gap+SX*favoriteShader.gridPosX;
let posY = gap+SY*favoriteShader.gridPosY;
x.beginPath()
x.rect(posX-gap/2,posY-gap/2,W+gap,H+gap);
x.lineWidth=7;
x.strokeStyle='#f00';
x.stroke();
}
}
function SetBest(bestX, bestY)
{
favoriteShader = shaderGrid[bestX][bestY];
favoriteShader.randSeed = randSeed;
++favoriteShader.subGeneration;
if (favoriteShader.generation == 0)
++favoriteShader.generation;
++shaderMemoryLocation;
shaderMemory.length = shaderMemoryLocation;
shaderMemory.push(favoriteShader.Clone());
MakeVariations(bestX, bestY);
DrawShaders();
UpdateUI();
}
function MakeVariations(skipX, skipY)
{
saveListIndex = 0;
textarea_code.value = favoriteShader.GetCode();
textarea_json.value = btoa(JSON.stringify(favoriteShader));
for(let X=0; X<gridSize; X++)
for(let Y=0; Y<gridSize; Y++)
{
let shader = shaderGrid[X][Y] = favoriteShader.Clone();
shader.SetGridPos(X,Y);
if (X!=skipX || Y!=skipY)
shader.Mutate();
}
}
function SetFavoriteFromMemory()
{
if (!favoriteShader)
{
RandomizeShaders();
return;
}
favoriteShader = shaderMemory[shaderMemoryLocation];
randSeed = favoriteShader.randSeed;
randSeedString = favoriteShader.randSeedString;
if (favoriteShader.IsVariation())
{
if (favoriteShader.gridPosX >= gridSize || favoriteShader.gridPosY >= gridSize)
favoriteShader.gridPosX = favoriteShader.gridPosY = 0;
let X = favoriteShader.gridPosX;
let Y = favoriteShader.gridPosY;
shaderGrid[X][Y] = favoriteShader.Clone();
MakeVariations(X, Y);
}
else if (favoriteShader.IsSaveList())
{
saveListIndex = favoriteShader.saveListIndex;
DisplaySaveListPage(0);
}
else
RandomizeShaders();
randSeedString = '';
}
///////////////////////////////////////////////////////////////////////////////
// SHADER OBJECT
class ShaderObject
{
constructor()
{
this.shaderStatements = [];
this.randSeed = randSeed;
this.randSeedString = randSeedString.length? randSeedString : ''+randSeed;
this.iterationCount = 1;
this.gridPosX = -1;
this.gridPosY = -1;
this.generation = 0;
this.subGeneration = 0;
this.hueOffset = 0;
this.hueScale = 1;
this.saturationScale = 1;
this.uvOffsetX = 0;
this.uvOffsetY = 0;
this.uvScaleX = 1;
this.uvScaleY = 1;
this.rotate = 0;
this.usePalette = 0;
this.paletteColors = [new Vector3(), new Vector3(), new Vector3(), new Vector3()];
this.saveListIndex = -1;
this.uniqueID=++uniqueID;
}
SetGridPos(X,Y) { this.gridPosX = X; this.gridPosY = Y; }
IsVariation() { return this.gridPosX >= 0; }
IsSaveList() { return this.saveListIndex >= 0; };
MakeAllObjectFloatsFixed(object, digits=3)
{
// keep floats small
for (let key in object)
{
let subObject = object[key];
if (typeof subObject === 'object')
this.MakeAllObjectFloatsFixed(subObject)
else if (typeof subObject === 'number')
object[key] = parseFloat(subObject.toFixed(digits));
}
}
Randomize()
{
let statementCount = parseInt(input_randomizeLength.value);
if (statementCount <0)
statementCount = 0;
this.usePalette = 1;
this.hueOffset = Rand();
this.hueScale = RandBetween(-1,1);
this.saturationScale = Rand();
this.generation = 0;
this.subGeneration = 0;
this.shaderStatements = [];
this.uvOffsetX = RandBetween(-1,1);
this.uvOffsetY = RandBetween(-1,1);
this.uvScaleX = RandBetween(-1,1);
this.uvScaleY = RandBetween(-1,1);
this.iterationCount = startIterations;
for(let i=statementCount; i--;)
{
let statement = new ShaderStatement();
statement.Randomize();
this.shaderStatements.push(statement);
}
for(let color of this.paletteColors)
color.Randomize(0,1);
this.MakeAllObjectFloatsFixed(this);
}
Clone()
{
let clone = Object.assign(new ShaderObject(), this);
clone.shaderStatements = [];
for(let statement of this.shaderStatements)
clone.shaderStatements.push(Object.assign(new ShaderStatement(), statement));
clone.paletteColors = [];
for(let color of this.paletteColors)
clone.paletteColors.push(Object.assign(new Vector3(), color));
// fix old shaders that had too high iterations
clone.iterationCount = Clamp(parseInt(clone.iterationCount),1,maxIterations);
return clone;
}
Mutate()
{
this.uniqueID = RandSeeded();
this.subGeneration = 0;
++this.generation;
if (this.shaderStatements.length <= 2)
{
this.Randomize();
return;
}
if (Rand() < .3)
{
// remove statement
let r = RandInt(this.shaderStatements.length);
let s = this.shaderStatements[r];
this.shaderStatements.splice(r, 1);
}
if (Rand() < .5)
{
// change order or a statement
let r = RandInt(this.shaderStatements.length);
let s = this.shaderStatements[r];
this.shaderStatements.splice(r, 1);
r = RandInt(this.shaderStatements.length+1);
this.shaderStatements.splice(r, 0, s);
}
for(let i=RandInt(2); i--;)
{
// mutate statements
let r = RandInt(this.shaderStatements.length);
this.shaderStatements[r].Mutate();
}
if (Rand() < .2)
{
// insert random statement
let statement = new ShaderStatement();
statement.Randomize();
let r = RandInt(this.shaderStatements.length+1);
this.shaderStatements.splice(r, 0, statement);
}
// mutate colors
this.hueOffset += RandBetween(-.1,.1)
if (Rand() < .2)
this.hueOffset = Rand();
if (Rand() < .2)
this.saturationScale = Rand();
if (Rand() < .2)
this.hueScale = RandBetween(-1,1);
if (Rand() < .4)
{
for(let color of this.paletteColors)
color.Randomize(0,1);
}
// mutate position
if (Rand() < .1)
{
this.uvOffsetX = RandBetween(-1,1);
this.uvOffsetY = RandBetween(-1,1);
this.uvScaleX = RandBetween(-1,1);
this.uvScaleY = RandBetween(-1,1);
}
else
{
this.uvOffsetX += RandBetween(-.1,.1);
this.uvOffsetY += RandBetween(-.1,.1);
this.uvScaleX += RandBetween(-.1,.1);
this.uvScaleY += RandBetween(-.1,.1);
}
if (Rand() < .1)
this.rotate = !this.rotate;
if (Rand() < .1)
this.iterationCount += RandInt(3)-1;
this.MakeAllObjectFloatsFixed(this);
}
GetCode()
{
let s = 10;
let uvsx = (s*this.uvScaleX).toFixed(3);
let uvsy = (s*this.uvScaleY).toFixed(3);
let uvox = (s*this.uvOffsetX).toFixed(3);
let uvoy = (s*this.uvOffsetY).toFixed(3);
let code = ``;
code += `// ZzArt - ${this.GetGenerationString()}\n\n`;
code += `const float PI=3.141592653589793;\n`;
if (this.usePalette)
code += `vec3 CosinePalette( float t, vec3 a, vec3 b, vec3 c, vec3 d ) { return a + b*cos( PI*2.*(c*t+d)); }\n`;
else
code += `vec3 SmoothHSV(vec3 c) { vec3 rgb = clamp(abs(mod(c.x*6.+vec3(0,4,2),6.)-3.)-1.,0.,1.); return c.z * mix( vec3(1), rgb*rgb*(3.-2.*rgb), c.y); }\n`
code += `vec4 lengthA(vec4 a) { return vec4(length(a)); }\n`;
code += `vec4 asinA(vec4 a) { return asin(clamp(a,-1.,1.)); }\n`;
code += `vec4 acosA(vec4 a) { return acos(clamp(a,-1.,1.)); }\n`;
code += `vec4 logA(vec4 a) { return log(abs(a)); }\n`;
code += `vec4 log2A(vec4 a) { return log2(abs(a)); }\n`;
code += `vec4 sqrtA(vec4 a) { return sqrt(abs(a)); }\n`;
code += `vec4 inversesqrtA(vec4 a) { return inversesqrt(abs(a)); }\n`;
code += `vec4 pow2(vec4 a) { return a*a; }\n`;
code += `vec4 pow3(vec4 a) { return a*a*a; }\n\n`;
code += `void mainImage(out vec4 a, in vec2 p)\n{\n`;
let rotateSwizzle = rotateCanvas^this.rotate? 'yxyx' : 'xyxy';
code += `a=p.${rotateSwizzle}/iResolution.${rotateSwizzle};\n`;
code += `a.xywz *= vec2(${uvsx}, ${uvsy}).xyxy;\n`;
code += `a.xywz += vec2(${uvox}, ${uvoy}).xyxy;\n`;
code += `vec4 b = a;\n\n`;
code += `// Generated Code - Line Count: ${this.shaderStatements.length}\n`
if (this.shaderStatements.length == 0)
code += `b=a=vec4(0.0);\n`;
else
{
if (this.iterationCount > 1)
code += `for (int i = 0; i < ${this.iterationCount}; ++i)\n{\n`
for(let statement of this.shaderStatements)
code += statement.GetString() + '\n';
if (this.iterationCount > 1)
code += `}\n`
}
// use hsl color
if (this.usePalette)
{
code += `\n// Cosine palettes by iq\n`
code += `a.x = a.x * ${(this.hueScale*.1).toFixed(3)}+${this.hueOffset.toFixed(3)};\n`
code += `a.xyz = b.x * CosinePalette(a.x`
for(let color of this.paletteColors)
code += `,\n ${color.GetShaderCode()}`;
code += `);\n`;
}
else
{
code += `\n// Smooth HSV by iq\n`
code += `a.x = a.x * ${this.hueScale.toFixed(3)}+${this.hueOffset.toFixed(3)};\n`;
code += `a.y *= ${this.saturationScale.toFixed(3)};\n`;
code += `a.xyz = SmoothHSV(a.xyz);\n`;
}
code += `}`;
return code;
}
Render()
{
let code = this.GetCode();
RenderShader(code);
}
GetGenerationString(shorten)
{
let string = ''
if (this.IsSaveList())
{
let page = 1+this.saveListIndex / (gridSize*gridSize)|0;
return 'Favorites Page: ' + page;
}
let seed = this.randSeedString?this.randSeedString:this.randSeed;
if (shorten)
string += `${seed}-`;
else
string += this.IsVariation()? 'Generation: ' : 'Seed: '
if (!this.IsVariation())
return string + seed;
if (this.subGeneration <= 1)
string += this.generation;
else
string += this.generation + '-' + (this.subGeneration>27?this.subGeneration:String.fromCharCode(65+this.subGeneration-2));
if (!shorten && this.IsVariation())
string += ` (${seed})`
return string;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// SHADER STUFF
class ShaderStatement
{
constructor()
{
this.output = 'a';
this.outputSwizzle = 'xyzw';
this.assignmentOperator = '=';
this.functionName = '';
this.parameter = 'a';
this.valueX = 1;
this.valueY = 1;
this.valueZ = 1;
this.valueW = 1;
this.parameterSwizzle = 'xyzw';
}
Randomize()
{
this.output = shaderRandomizer.Output();
this.assignmentOperator = shaderRandomizer.AssignmentOperator();
this.functionName = shaderRandomizer.FunctionName();
this.parameter = shaderRandomizer.Parameter();
this.valueX = shaderRandomizer.Value();
this.valueY = shaderRandomizer.Value();
this.valueZ = shaderRandomizer.Value();
this.valueW = shaderRandomizer.Value();
this.outputSwizzle = shaderRandomizer.Swizzle(1);
this.parameterSwizzle = shaderRandomizer.Swizzle();
}
Mutate()
{
let r = RandInt(10);
switch (r)
{
case 0: this.output = shaderRandomizer.Output(); break;
case 1: this.assignmentOperator = shaderRandomizer.AssignmentOperator(); break;
case 2: this.functionName = shaderRandomizer.FunctionName(); break;
case 3: this.outputSwizzle = shaderRandomizer.Swizzle(1); break;
case 4: this.parameter = shaderRandomizer.Parameter(); break;
case 5: this.parameterSwizzle = shaderRandomizer.Swizzle(); break;
case 6: this.valueX = shaderRandomizer.Value(); break;
case 7: this.valueY = shaderRandomizer.Value(); break;
case 8: this.valueZ = shaderRandomizer.Value(); break;
case 9: this.valueW = shaderRandomizer.Value(); break;
}
this.valueX += 0.05*RandBetween(-1,1);
this.valueY += 0.05*RandBetween(-1,1);
this.valueZ += 0.05*RandBetween(-1,1);
this.valueW += 0.05*RandBetween(-1,1);
}
GetString()
{
let parameter = '' + this.parameter;
if (parameter == '')
parameter = `vec4(${this.valueX.toFixed(3)}, ${this.valueY.toFixed(3)}, ${this.valueZ.toFixed(3)}, ${this.valueW.toFixed(3)})`;
let code;
code = this.output + '.' + this.outputSwizzle;
code += ' ' + this.assignmentOperator + ' ' + this.functionName
code += '(' + parameter + ')'
code += '.' + this.parameterSwizzle;
code += ';';
return code;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// SHADER RANDOMIZER
let shaderRandomizer =
{
AssignmentOperator: function()
{
let f = ['=','+=','-=','*=','/='];
return f[RandInt(f.length)];
},
FunctionName: function()
{
if (Rand() < .5)
return '';
let f =
[
'sin','cos','normalize','lengthA',
'tan','asinA','acosA','atan',
'logA','log2A','exp','exp2',
'sqrtA','inversesqrtA','fract',
'abs','sign','floor','ceil',
'pow2','pow3'
];
return f[RandInt(f.length)];
},
Output: function()
{
let f = ['a','b'];
return f[RandInt(f.length)];
},
Value: function()
{
let v = RandBetween(0,1);
v = v * v;
v *= 10;
return RandInt(2)? v : -v;
},
Parameter: function()
{
let f = ['a','b','']
return f[RandInt(f.length)];
},
Swizzle: function(noDuplicates)
{
if (noDuplicates)
{
let s = ['x','y','z','w'];
s = ShuffleArray(s);
return s.join('');
}
let s = ['x','y','z','w'];
return s[RandInt(s.length)] + s[RandInt(s.length)] + s[RandInt(s.length)] + s[RandInt(s.length)];
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// USER INTERFACE
function SetGridSize(newGridSize)
{
gridSize = Clamp(newGridSize,2,10);
shaderGrid=[];
for(let i=0; i<gridSize; i++)
shaderGrid[i] = [];
SetFavoriteFromMemory();
DrawShaders();
UpdateUI();
}
function UpdateUI(resetSaveListSelection=1)
{
if (satelliteMode)
return;
if (!isInit)
{
// clear url
let url = new URL(window.location.href);
url.search= "";
window.history.pushState(null,null,url.toString());
}
if (resetSaveListSelection)
select_saveList.selectedIndex=-1;
startIterations = Clamp(parseInt(input_startIterations.value), 1, maxIterations);
button_back.disabled = shaderMemoryLocation <= 0;
button_forward.disabled = shaderMemoryLocation >= shaderMemory.length-1;
button_preview.disabled = !favoriteShader || favoriteShader.gridPosX<0;
canvas_main.hidden = showPreview;
canvas_shader.hidden = !showPreview;
div_advanced.style.display = advancedMode? 'inline' : 'none';
button_delete.style.display = advancedMode? 'inline' : 'none';
button_help.style.display = advancedMode? 'none' : 'inline';
textarea_debug.hidden = textarea_debug.value == '';
button_saveFolder.style.display = saveList.length==0? 'none' : 'inline';
let s = shaderMemory[shaderMemoryLocation];
span_generations.innerHTML = s.GetGenerationString();
let isMobile = IsMobile();
if (isMobile || itchMode)
{
if (!itchMode)
button_help.style.display = 'none';
button_seed.style.display = 'inline';
button_advanced.style.display = 'none';
button_share.style.display = 'none';
div_credit.style.display = 'none';
button_openSatellite.style.display = 'none';
}
// set title
let shader = shaderMemory[shaderMemoryLocation];
document.title = `ZzArt - ` + shader.GetGenerationString();
// resize canvas to fit window
let a = canvas_main.width / canvas_main.height;
let w = window.innerWidth - (isMobile||itchMode?20:100);
let h = window.innerHeight - (itchMode?120:150);
let wa = w/h;
if (rotateCanvas)
wa = 1/wa;
if (wa > a)
w = h * a;
else
h = w / a;
canvas_main.style.width=w+'px';
canvas_main.style.height=h+'px';
canvas_shader.style.width=w+'px';
canvas_shader.style.height=h+'px';
SaveLocalStorage();
}
function ChangeMemoryLocation(direction)
{
if (shaderMemoryLocation + direction < 0 || shaderMemoryLocation + direction >= shaderMemory.length)
return;
if (showPreview)
{
showPreview = 0;
UpdateUI();
return;
}
shaderMemoryLocation+=direction;
SetFavoriteFromMemory();
DrawShaders();
UpdateUI();
}
function ButtonSatellite()
{
let url = new URL(window.location.href);
url.search = 'satellite=1';
window.open(url);
}
function ButtonSatelliteHelp()
{
window.alert(
`Welcome to 𝓩𝔃𝓐𝓻𝓽 ~ Abstract Art Evolution
This satellite 📡 mode allows you to view a full screen preview of our current favorite on a second monitor while browsing!`
);
}
function ButtonHelp()
{
window.alert(
`Welcome to 𝓩𝔃𝓐𝓻𝓽 ~ Abstract Art Evolution
To get started, click 🎲 a few times generate random seeds.
When you like something, just click it see more variations.
You can click 🔍 or press space to see a large preview.
Use 📡 to view open the large preview in a separate window.
Click 💾 to save your art as a 4K png image file.`
);
}
function Randomize(seed)
{
randSeed = seed;
showPreview = 0;
++shaderMemoryLocation;
shaderMemory.length=shaderMemoryLocation;
shaderMemory.push(new ShaderObject());
RandomizeShaders();
DrawShaders();
UpdateUI();
}
function ButtonRandomize()
{
let seed = Math.abs(Date.now() % 1e9);
Randomize(seed);
}
function ButtonSeed()
{
let seedString = window.prompt('Enter a ZzArt seed to use for randomization:', '');
if (seedString === null)
return;
seedString = String(seedString);
let seed = parseInt(seedString);
if (!Number.isInteger(seed))
seed = HashString(seedString);
if (seed==0)
seed = 1; // prevent it from being black
randSeedString = seedString;
Randomize(seed);
randSeedString = '';
}
function ButtonAdvanced()
{
advancedMode = !advancedMode;
UpdateUI();
}
function ButtonTogglePreview()
{
showPreview = !showPreview;
if (showPreview)
{
// redraw the favorite shader
canvas_shader.width = canvas_main.width;
canvas_shader.height = canvas_main.height;
if (favoriteShader && favoriteShader.IsVariation())
favoriteShader.Render();
}
UpdateUI();
}
function ButtonSave()
{
if (!favoriteShader)
return;
AddToSaveList(favoriteShader);
// save large
let saveScale = parseInt(input_saveScale.value);
if (saveScale <= 0)
return;
canvas_shader.width = saveScale*defaultCanvasWidth;
canvas_shader.height = saveScale*defaultCanvasHeight;
favoriteShader.Render();
let canvas = canvas_shader;