-
Notifications
You must be signed in to change notification settings - Fork 4
/
shadama.js
executable file
·5801 lines (4890 loc) · 193 KB
/
shadama.js
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
/* globals CodeMirror THREE Papa ohm
setTestParams
grammarUnitTests
symTableUnitTests
translateTests
*/
/* SPECTOR */
// import {join} from "./croquet.js";
export function ShadamaFactory(frame, optDimension, parent, optDefaultProgName, optDOMTools) {
let threeRenderer = frame ? frame.renderer : null;
let TEXTURE_SIZE = 2048;
let FIELD_WIDTH = 512;
let FIELD_HEIGHT = 512;
let VOXEL_STEP = 8;
let VOXEL_WIDTH = 512;
let VOXEL_HEIGHT = 512;
let VOXEL_DEPTH = 512;
let VOXEL_TEXTURE_WIDTH = 512; // sqrt(512 * 512 * 512 / 8 / 8 / 8);
let VOXEL_TEXTURE_HEIGHT = 512; // sqrt(512 * 512 * 512 / 8 / 8 / 8);
let T = TEXTURE_SIZE;
let FW = FIELD_WIDTH;
let FH = FIELD_HEIGHT;
let VS = VOXEL_STEP;
let VW = VOXEL_WIDTH;
let VH = VOXEL_HEIGHT;
let VD = VOXEL_DEPTH;
let VTW = VOXEL_TEXTURE_WIDTH;
let VTH = VOXEL_TEXTURE_HEIGHT;
let N = "_new_";
let dimension = optDimension || 3; // 2 | 3;
// need to change things around here so that you can have different Shadma instances with different sizes
let breedVAO;
let patchVAO;
let programs = {}; // {name: {prog: shader, vao: VAO, uniLocations: uniformLocs}}
let renderer;
let gl;
let state;
let audioContext;
let renderRequests = [];
let targetTexture; // THREE.js texture, not WebGL texture
let debugCanvas1;
let debugArray;
let debugArray1;
let debugArray2;
let debugTextureBreed;
let debugTexturePatch;
let framebufferDBreed; // for debugging u8rgba texture
let framebufferDPatch; // for debugging u8rgba texture
let framebufferBreed;
let framebufferPatch;
let framebufferDiffuse;
let framebufferU8RGBA; // for three js u8rgba texture
let readFramebufferBreed;
let readFramebufferPatch;
let writeFramebufferBreed;
let writeFramebufferPatch;
let editor = null;
let editorType = null;
let parseErrorWidget = null;
let domTools = false;
let readout;
let watcherList; // DOM
let watcherElements = []; // [DOM]
let envList; // DOM
let shadamaCanvas;
let fullScreenScale = 1;
let keepGoing = true;
let animationRequested = false;
let times = [];
let withThreeJS;
let runTests;
let showAllEnv;
let degaussdemo;
let climatedemo;
let useCroquet;
let croquetView;
let moveVector;
let euler;
let shaders = {
"drawBreed.vert":
`#version 300 es
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform vec2 u_resolution;
uniform vec2 u_half;
uniform float u_pointSize;
uniform sampler2D u_x;
uniform sampler2D u_y;
uniform sampler2D u_r;
uniform sampler2D u_g;
uniform sampler2D u_b;
uniform sampler2D u_a;
out vec4 v_color;
void main(void) {
ivec2 fc = ivec2(a_index);
float x = texelFetch(u_x, fc, 0).r;
float y = texelFetch(u_y, fc, 0).r;
vec2 dPos = vec2(x, y); // (0-resolution, 0-resolution)
vec2 normPos = dPos / u_resolution; // (0-1.0, 0-1.0)
vec2 clipPos = (normPos + u_half) * 2.0 - 1.0; // (-1.0-1.0, -1.0-1.0)
gl_Position = vec4(clipPos, 0, 1.0);
float r = texelFetch(u_r, fc, 0).r;
float g = texelFetch(u_g, fc, 0).r;
float b = texelFetch(u_b, fc, 0).r;
float a = texelFetch(u_a, fc, 0).r;
v_color = vec4(r, g, b, a);
gl_PointSize = u_pointSize;
}`,
"drawBreed.frag":
`#version 300 es
precision highp float;
in vec4 v_color;
out vec4 fragColor;
void main(void) {
fragColor = v_color;
}`,
"drawPatch.vert":
`#version 300 es
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform vec2 u_resolution;
uniform vec2 u_half;
uniform sampler2D u_r;
uniform sampler2D u_g;
uniform sampler2D u_b;
uniform sampler2D u_a;
out vec4 v_color;
void main(void) {
vec2 clipPos = (b_index + u_half) * 2.0 - 1.0; // (-1.0-1.0, -1.0-1.0)
gl_Position = vec4(clipPos, 0, 1.0);
gl_PointSize = 1.0;
ivec2 fc = ivec2(a_index);
float r = texelFetch(u_r, fc, 0).r;
float g = texelFetch(u_g, fc, 0).r;
float b = texelFetch(u_b, fc, 0).r;
float a = texelFetch(u_a, fc, 0).r;
v_color = vec4(r, g, b, a);
}`,
"drawPatch.frag":
`#version 300 es
precision highp float;
in vec4 v_color;
out vec4 fragColor;
void main(void) {
fragColor = v_color;
}`,
"debug.vert":
`#version 300 es
precision highp float;
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform sampler2D u_value;
uniform vec2 u_half;
out vec4 v_color;
void main(void) {
vec2 clipPos = (b_index + u_half) * 2.0 - 1.0; // (-1.0-1.0, -1.0-1.0)
gl_Position = vec4(clipPos, 0, 1.0);
gl_PointSize = 1.0;
ivec2 fc = ivec2(a_index);
v_color = texelFetch(u_value, fc, 0);
}`,
"debug.frag":
`#version 300 es
precision highp float;
in vec4 v_color;
out vec4 fragColor;
void main(void) {
fragColor = v_color;
}`,
"renderBreed.vert":
`#version 300 es
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform mat4 mvMatrix;
uniform mat4 pMatrix;
uniform vec3 u_resolution;
uniform vec3 u_half;
uniform sampler2D u_x;
uniform sampler2D u_y;
uniform sampler2D u_z;
uniform sampler2D u_r;
uniform sampler2D u_g;
uniform sampler2D u_b;
uniform sampler2D u_a;
uniform sampler2D u_d;
uniform float u_dotSize;
uniform bool u_use_vector;
out vec4 v_color;
void main(void) {
ivec2 fc = ivec2(a_index);
float x = texelFetch(u_x, fc, 0).r;
float y = texelFetch(u_y, fc, 0).r;
float z = texelFetch(u_z, fc, 0).r;
vec3 dPos = vec3(x, y, z);
vec3 normPos = dPos / u_resolution;
vec3 clipPos = ((normPos + u_half) * 2.0 - 1.0) * (u_resolution.x / 2.0);
vec4 mvPos = mvMatrix * vec4(clipPos, 1.0);
gl_Position = pMatrix * mvPos;
float r = texelFetch(u_r, fc, 0).r;
float g = texelFetch(u_g, fc, 0).r;
float b = texelFetch(u_b, fc, 0).r;
float a = texelFetch(u_a, fc, 0).r;
v_color = vec4(r, g, b, a);
gl_PointSize = ((u_use_vector ? texelFetch(u_d, fc, 0).r : u_dotSize) / -mvPos.z);
}`,
"renderBreed.frag":
`#version 300 es
precision highp float;
in vec4 v_color;
out vec4 fragColor;
void main(void) {
fragColor = v_color;
}`,
"renderPatch.vert":
`#version 300 es
precision highp float;
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform mat4 mvMatrix;
uniform mat4 pMatrix;
uniform vec3 u_resolution;
uniform vec3 v_resolution;
uniform int v_step;
uniform vec3 u_half;
uniform sampler2D u_r;
uniform sampler2D u_g;
uniform sampler2D u_b;
uniform sampler2D u_a;
out vec4 v_color;
void main(void) {
ivec2 fc = ivec2(a_index);
ivec3 iv_resolution = ivec3(v_resolution);
// the framebuffer will be 512^512, which is square of cube root of 64 * 64 * 64
// fc varies over this.
int index = int(a_index.y) * int(u_resolution.x) + int(a_index.x);
int z = index / (iv_resolution.x * iv_resolution.y);
int xy = index % (iv_resolution.x * iv_resolution.y);
int x = xy % iv_resolution.x;
int y = xy / iv_resolution.x;
x = x * v_step;
y = y * v_step;
z = z * v_step;
vec3 dPos = vec3(x, y, z);
vec3 normPos = dPos / u_resolution;
vec3 clipPos = ((normPos + u_half) * 2.0 - 1.0) * (u_resolution.x / 2.0);
vec4 mvPos = mvMatrix * vec4(clipPos, 1.0);
gl_Position = pMatrix * mvPos;
gl_PointSize = 24.0 * ( 24.0 / -mvPos.z );
float r = texelFetch(u_r, fc, 0).r;
float g = texelFetch(u_g, fc, 0).r;
float b = texelFetch(u_b, fc, 0).r;
float a = texelFetch(u_a, fc, 0).r;
v_color = vec4(r, g, b, a);
}`,
"renderPatch.frag":
`#version 300 es
precision highp float;
in vec4 v_color;
out vec4 fragColor;
void main(void) {
fragColor = v_color;
}`,
"diffusePatch.vert":
`#version 300 es
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform vec2 u_half;
uniform sampler2D u_value;
out float v_value;
const float weight[9] = float[9](
0.077847, 0.123317, 0.077847,
0.123317, 0.195346, 0.123317,
0.077847, 0.123317, 0.077847
);
void main(void) {
vec2 clipPos = (b_index + u_half) * 2.0 - 1.0; // (-1.0-1.0, -1.0-1.0)
gl_Position = vec4(clipPos, 0, 1.0);
gl_PointSize = 1.0;
ivec2 fc = ivec2(a_index);
float v = 0.0;
v += texelFetch(u_value, fc + ivec2(-1, -1), 0).r * weight[0];
v += texelFetch(u_value, fc + ivec2(-1, 0), 0).r * weight[1];
v += texelFetch(u_value, fc + ivec2(-1, 1), 0).r * weight[2];
v += texelFetch(u_value, fc + ivec2( 0, -1), 0).r * weight[3];
v += texelFetch(u_value, fc + ivec2( 0, 0), 0).r * weight[4];
v += texelFetch(u_value, fc + ivec2( 0, 1), 0).r * weight[5];
v += texelFetch(u_value, fc + ivec2( 1, -1), 0).r * weight[6];
v += texelFetch(u_value, fc + ivec2( 1, 0), 0).r * weight[7];
v += texelFetch(u_value, fc + ivec2( 1, 1), 0).r * weight[8];
v = v <= (1.0/256.0) ? 0.0 : v;
v_value = v;
}`,
"diffusePatch.frag":
`#version 300 es
precision highp float;
in float v_value;
layout (location = 0) out float fragColor;
void main(void) {
fragColor = v_value;
}`,
"increasePatch.vert":
`#version 300 es
precision highp float;
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform vec2 u_resolution;
uniform vec2 u_half;
uniform sampler2D u_that_x;
uniform sampler2D u_that_y;
uniform bool u_use_vector;
uniform sampler2D u_texture;
uniform float u_value;
out float v_value;
void main() {
float _x = texelFetch(u_that_x, ivec2(a_index), 0).r;
float _y = texelFetch(u_that_y, ivec2(a_index), 0).r;
vec2 _pos = vec2(_x, _y);
vec2 oneToOne = ((_pos / u_resolution) + u_half) * 2.0 - 1.0;
gl_Position = vec4(oneToOne, 0.0, 1.0);
gl_PointSize = 1.0;
v_value = u_use_vector ? texelFetch(u_texture, ivec2(a_index), 0).r : u_value;
}`,
"increasePatch.frag":
`#version 300 es
precision highp float;
in float v_value;
layout (location = 0) out float fragColor;
void main() {
fragColor = v_value;
}`,
"increaseVoxel.vert":
`#version 300 es
precision highp float;
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform vec2 u_resolution;
uniform vec3 v_resolution;
uniform float v_step;
uniform vec2 u_half;
uniform sampler2D u_that_x;
uniform sampler2D u_that_y;
uniform sampler2D u_that_z;
uniform bool u_use_vector;
uniform sampler2D u_texture;
uniform float u_value;
out float v_value;
void main() {
ivec2 fc = ivec2(a_index);
float _x = texelFetch(u_that_x, fc, 0).r;
float _y = texelFetch(u_that_y, fc, 0).r;
float _z = texelFetch(u_that_z, fc, 0).r;
_x = floor(_x / v_step); // 8 // [0..64), if originally within [0..512)
_y = floor(_y / v_step); // 8
_z = floor(_z / v_step); // 8
int index = int(_z * v_resolution.x * v_resolution.y + _y * v_resolution.x + _x);
vec2 _pos = vec2(index % int(u_resolution.x), index / int(u_resolution.x));
vec2 oneToOne = ((_pos / u_resolution) + u_half) * 2.0 - 1.0;
gl_Position = vec4(oneToOne, 0.0, 1.0);
gl_PointSize = 1.0;
v_value = u_use_vector ? texelFetch(u_texture, ivec2(a_index), 0).r : u_value;
}`,
"increaseVoxel.frag":
`#version 300 es
precision highp float;
in float v_value;
layout (location = 0) out float fragColor;
void main() {
fragColor = v_value;
}`,
"copyRGBA.vert":
`#version 300 es
layout (location = 0) in vec2 a_index;
layout (location = 1) in vec2 b_index;
uniform vec2 u_resolution;
uniform vec2 u_half;
uniform ivec2 u_videoExtent;
uniform sampler2D u_videoTexture;
out vec4 v_value;
void main(void) {
ivec2 fc = ivec2(a_index);
int w = int(u_resolution.x);
int i = fc.y * w + fc.x;
ivec2 vp = ivec2(i % w, i / u_videoExtent.x);
vp.y = u_videoExtent.y - vp.y;
vec2 clipPos = (b_index + u_half) * 2.0 - 1.0; // (-1.0-1.0, -1.0-1.0)
gl_Position = vec4(clipPos, 0.0, 1.0);
gl_PointSize = 1.0;
v_value = texelFetch(u_videoTexture, vp, 0);
}`,
"copyRGBA.frag":
`#version 300 es
precision highp float;
in vec4 v_value;
layout (location = 0) out float o_r;
layout (location = 1) out float o_g;
layout (location = 2) out float o_b;
layout (location = 3) out float o_a;
void main(void) {
o_r = v_value.r;
o_g = v_value.g;
o_b = v_value.b;
o_a = v_value.a;
}`,
};
function initBreedVAO() {
let allIndices = new Array(T * T * 2);
let divIndices = new Array(T * T * 2);
for (let j = 0; j < T; j++) {
for (let i = 0; i < T; i++) {
let ind = ((j * T) + i) * 2;
allIndices[ind + 0] = i;
allIndices[ind + 1] = j;
divIndices[ind + 0] = i / T;
divIndices[ind + 1] = j / T;
}
}
breedVAO = gl.createVertexArray();
gl.bindVertexArray(breedVAO);
let aBuffer = gl.createBuffer();
let bBuffer = gl.createBuffer();
let attrLocations = new Array(2);
attrLocations[0] = 0; // gl.getAttribLocation(prog, 'a_index'); a_index has layout location spec
attrLocations[1] = 1; // gl.getAttribLocation(prog, 'b_index'); b_index has layout location spec
let attrStrides = new Array(2);
attrStrides[0] = 2;
attrStrides[1] = 2;
setBufferAttribute([aBuffer, bBuffer], [allIndices, divIndices], attrLocations, attrStrides);
gl.bindVertexArray(null);
}
function initPatchVAO() {
let w;
let h;
if (dimension === 2) {
w = FW;
h = FH;
} else {
w = VTW;
h = VTH;
}
let allIndices = new Array(w * h * 2);
let divIndices = new Array(w * h * 2);
for (let j = 0; j < h; j++) {
for (let i = 0; i < w; i++) {
let ind = ((j * w) + i) * 2;
allIndices[ind + 0] = i;
allIndices[ind + 1] = j;
divIndices[ind + 0] = i / w;
divIndices[ind + 1] = j / h;
}
}
patchVAO = gl.createVertexArray();
gl.bindVertexArray(patchVAO);
let aBuffer = gl.createBuffer();
let bBuffer = gl.createBuffer();
let attrLocations = [0, 1];
attrLocations[0] = 0; // gl.getAttribLocation(prog, 'a_index'); a_index has layout location spec
attrLocations[1] = 1; // gl.getAttribLocation(prog, 'b_index'); b_index has layout location spec
let attrStrides = [2, 2];
setBufferAttribute([aBuffer, bBuffer], [allIndices, divIndices], attrLocations, attrStrides);
gl.bindVertexArray(null);
}
function makePrimitive(name, uniforms, vao) {
let vs = createShader(name + ".vert", shaders[name + '.vert']);
let fs = createShader(name + ".frag", shaders[name + '.frag']);
let prog = createProgram(vs, fs);
let uniLocations = {};
uniforms.forEach((n) => {
uniLocations[n] = gl.getUniformLocation(prog, n);
});
return {program: prog, uniLocations, vao};
}
function drawBreedProgram() {
return makePrimitive("drawBreed", ["u_resolution", "u_half", "u_pointSize", "u_x", "u_y", "u_r", "u_g", "u_b", "u_a"], breedVAO);
}
function drawPatchProgram() {
return makePrimitive("drawPatch", ["u_resolution", "u_half", "u_a", "u_r", "u_g", "u_b"], patchVAO);
}
function debugBreedProgram() {
return makePrimitive("debug", ["u_value", "u_half"], breedVAO);
}
function debugPatchProgram() {
return makePrimitive("debug", ["u_value", "u_half"], patchVAO);
}
function renderBreedProgram() {
return makePrimitive("renderBreed", ["mvMatrix", "pMatrix", "u_resolution", "u_half", "u_x", "u_y", "u_z", "u_r", "u_g", "u_b", "u_a", "u_d", "u_dotSize", "u_use_vector"], breedVAO);
}
function renderPatchProgram() {
return makePrimitive("renderPatch", ["mvMatrix", "pMatrix", "u_resolution", "u_half", "v_resolution", "v_step", "u_r", "u_g", "u_b", "u_a"], patchVAO);
}
function diffusePatchProgram() {
return makePrimitive("diffusePatch", ["u_value", "u_half",], patchVAO);
}
function increasePatchProgram() {
return makePrimitive("increasePatch", ["u_resolution", "u_half", "u_that_x", "u_that_y", "u_use_vector", "u_texture", "u_value"], breedVAO);
}
function increaseVoxelProgram() {
return makePrimitive("increaseVoxel", ["u_resolution", "u_half", "v_resolution", "v_step", "u_that_x", "u_that_y", "u_that_z", "u_use_vector", "u_texture", "u_value"], breedVAO);
}
function copyRGBAProgram() {
return makePrimitive("copyRGBA", ["u_resolution", "u_half", "u_videoExtent", "u_videoTexture"], breedVAO);
}
function createShader(id, source) {
let type;
if (id.endsWith(".vert")) {
type = gl.VERTEX_SHADER;
} else if (id.endsWith(".frag")) {
type = gl.FRAGMENT_SHADER;
}
let shader = gl.createShader(type);
if (!source) {
let scriptElement = document.getElementById(id);
if(!scriptElement){return null;}
source = scriptElement.text;
}
gl.shaderSource(shader, source);
gl.compileShader(shader);
let success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
}
console.log(source);
console.log(gl.getShaderInfoLog(shader));
alert(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
function createProgram(vertexShader, fragmentShader) {
let program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
let success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success) {
return program;
}
console.log(gl.getProgramInfoLog(program));
// alert(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
return null;
}
function createTexture(data, type, width, height) {
if (!type) {
type = gl.UNSIGNED_BYTE;
}
if (!width) {
width = T;
}
if (!height) {
height = T;
}
let tex = gl.createTexture();
state.bindTexture(gl.TEXTURE_2D, tex);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, false);
gl.pixelStorei(gl.UNPACK_ROW_LENGTH, width);
gl.pixelStorei(gl.UNPACK_IMAGE_HEIGHT, height);
gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);
gl.pixelStorei(gl.UNPACK_SKIP_IMAGES, 0);
if (type === gl.UNSIGNED_BYTE) {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, type, data, 0);
} else if (type === gl.R32F) {
gl.texImage2D(gl.TEXTURE_2D, 0, type, width, height, 0, gl.RED, gl.FLOAT, data, 0);
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, width, height, 0, gl.RGBA, type, data, 0);
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
state.bindTexture(gl.TEXTURE_2D, null);
return tex;
}
function makeFramebuffer(format, width, height) {
if (!format) {
format = gl.UNSIGNED_BYTE;
}
if (!width) {
width = T;
}
if (!height) {
height = T;
}
/*
let tex;
if (format === gl.FLOAT) {
tex = createTexture(new Float32Array(width * height * 4), format, width, height);
}
if (format === gl.R32F) {
tex = createTexture(new Float32Array(width * height), format, width, height);
}
if (format === gl.UNSIGNED_BYTE) {
tex = createTexture(new Uint8Array(width * height * 4), format, width, height);
}
*/
let buffer = gl.createFramebuffer();
/*
state.bindFramebuffer(gl.FRAMEBUFFER, buffer);
state.bindTexture(gl.TEXTURE_2D, tex);
if (format === gl.R32F) {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.R32F, width, height, 0, gl.RED, gl.FLOAT, null);
} else if (format === gl.UNSIGNED_BYTE) {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, format, null);
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, width, height, 0, gl.RGBA, format, null);
}
state.bindTexture(gl.TEXTURE_2D, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
*/
if (withThreeJS) {
let target = new THREE.WebGLRenderTarget(width, height);
renderer.properties.get(target).__webglFramebuffer = buffer;
//gl.deleteTexture(tex); // has to be revisited
return target;
}
return buffer;
}
function setTargetBuffer(buffer, tex) {
renderer.setRenderTarget(buffer);
if (buffer) {
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
}
}
function setTargetBuffers(buffer, tex) {
if (!buffer) {
renderer.setRenderTarget(null, gl.DRAW_FRAMEBUFFER);
return;
}
let list = [];
renderer.setRenderTarget(buffer, gl.DRAW_FRAMEBUFFER);
for (let i = 0; i < tex.length; i++) {
let val = gl.COLOR_ATTACHMENT0 + i;
gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, val, gl.TEXTURE_2D, tex[i], 0);
list.push(val);
}
gl.drawBuffers(list);
}
function setBufferAttribute(buffers, data, attrL, attrS) {
for (let i in buffers) {
gl.bindBuffer(gl.ARRAY_BUFFER, buffers[i]);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data[i]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(attrL[i]);
gl.vertexAttribPointer(attrL[i], attrS[i], gl.FLOAT, false, 0, 0);
}
}
function webglTexture() {
return withThreeJS ? (targetTexture && renderer.properties.get(targetTexture).__webglTexture || null) : null;
}
function noBlend() {
if (withThreeJS) {
state.setCullFace(THREE.CullFaceNone);
state.setBlending(THREE.NoBlending);
} else {
gl.disable(gl.BLEND);
}
}
function normalBlend() {
if (withThreeJS) {
state.setBlending(THREE.NormalBlending);
} else {
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
}
}
function oneBlend() {
if (withThreeJS) {
state.setBlending(THREE.CustomBlending, THREE.AddEquation, THREE.OneFactor, THREE.OneFactor);
} else {
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE);
}
}
function textureCopy(obj, src, dst) {
let w;
let h;
let readbuffer;
let writebuffer;
if (obj.constructor === Breed) {
w = T;
h = T;
readbuffer = readFramebufferBreed;
writebuffer = writeFramebufferBreed;
} else if (obj.constructor === Patch) {
w = FW;
h = FH;
readbuffer = readFramebufferPatch;
writebuffer = writeFramebufferPatch;
}
renderer.setRenderTarget(readbuffer, gl.READ_FRAMEBUFFER);
gl.framebufferTexture2D(gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, src, 0);
renderer.setRenderTarget(writebuffer, gl.DRAW_FRAMEBUFFER);
gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, dst, 0);
gl.blitFramebuffer(0, 0, w, h, 0, 0, w, h, gl.COLOR_BUFFER_BIT, gl.NEAREST);
setTargetBuffer(null, null);
}
function updateOwnVariable(obj, name, optData) {
let width;
let height;
if (obj.constructor === Breed) {
width = T;
height = T;
} else if (obj.constructor === Patch) {
width = FW;
height = FH;
} else {
width = VTW;
height = VTH;
}
let ary = optData || new Float32Array(width * height);
if (obj[name]) {
gl.deleteTexture(obj[name]);
}
if (obj[N + name]) {
gl.deleteTexture(obj[N + name]);
}
obj.own[name] = name;
obj[name] = createTexture(ary, gl.R32F, width, height);
obj[N + name] = createTexture(ary, gl.R32F, width, height);
}
function removeOwnVariable(obj, name) {
delete obj.own[name];
if (obj[name]) {
gl.deleteTexture(obj[name]);
delete obj[name];
}
if (obj[N + name]) {
gl.deleteTexture(obj[N + name]);
delete obj[N + name];
}
}
function update(Cls, name, fields, env) {
let stringify = (obj) => {
let type = Object.prototype.toString.call(obj);
if (type === "[object Object]") {
let pairs = [];
for (let k in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, k)) continue;
pairs.push([k, stringify(obj[k])]);
}
pairs.sort((a, b) => a[0] < b[0] ? -1 : 1);
pairs = pairs.map(v => '"' + v[0] + '":' + v[1]);
return "{" + pairs + "}";
}
if (type === "[object Array]") {
return "[" + obj.map(v => stringify(v)) + "]";
}
return JSON.stringify(obj);
};
let obj = env[name];
if (!obj) {
obj = new Cls();
for (let i = 0; i < fields.length; i++) {
updateOwnVariable(obj, fields[i]);
}
env[name] = obj;
return true;
}
let oldOwn = obj.own;
let toBeDeleted = []; // [<str>]
let toBeCreated = []; // [<str>]
let newOwn = {};
// common case: when the existing own and fields are the same
for (let i = 0; i < fields.length; i++) {
let k = fields[i];
newOwn[k] = k;
}
if (stringify(newOwn) === stringify(oldOwn)) {
return false;
}
// other case: get things into toBeDeleted and toBeCreated, and toBeMoved
for (let k in oldOwn) {
if (fields.indexOf(k) < 0) {
toBeDeleted.push(k);
}
}
for (let i = 0; i < fields.length; i++) {
let k = fields[i];
if (!oldOwn[k]) {
toBeCreated.push(k);
}
}
toBeCreated.forEach((k) => updateOwnVariable(obj, k));
toBeDeleted.forEach((k) => removeOwnVariable(obj, k));
return true;
}
function programFromTable(table, vert, frag, name) {
let debugName = name;
if (debugName === "move") {
}
let prog = createProgram(
createShader(name + ".vert", vert),
createShader(name + ".frag", frag));
let vao = breedVAO;
let uniLocations = {};
let forBreed = table.forBreed;
let viewportW = forBreed ? T : FW;
let viewportH = forBreed ? T : FH;
let hasPatchInput = table.hasPatchInput;
table.defaultUniforms.forEach((n) => {
uniLocations[n] = gl.getUniformLocation(prog, n);
});
table.uniformTable.keysAndValuesDo((key, entry) => {
let uni = table.uniform(entry);
uniLocations[uni] = gl.getUniformLocation(prog, uni);
});