-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathp5.Shader.js
More file actions
1232 lines (1072 loc) · 46.8 KB
/
p5.Shader.js
File metadata and controls
1232 lines (1072 loc) · 46.8 KB
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
import p5 from '../../../src/app.js';
import rendererWebGPU from "../../../src/webgpu/p5.RendererWebGPU";
p5.registerAddon(rendererWebGPU);
suite('WebGPU p5.Shader', function() {
var myp5;
beforeAll(function() {
window.IS_MINIFIED = true;
myp5 = new p5(function(p) {
p.setup = function() {
p.createCanvas(100, 100, 'webgpu');
p.pointLight(250, 250, 250, 100, 100, 0);
p.ambientMaterial(250);
};
});
});
afterAll(function() {
myp5.remove();
});
suite('p5.strands', () => {
test('does not break when arrays are in uniform callbacks', async () => {
await myp5.createCanvas(5, 5, myp5.WEBGPU);
const myShader = myp5.baseMaterialShader().modify(() => {
const size = myp5.uniformVector2(() => [myp5.width, myp5.height]);
myp5.getPixelInputs(inputs => {
inputs.color = [
size / 1000,
0,
1
];
return inputs;
});
}, { myp5 });
expect(() => {
myp5.shader(myShader);
myp5.plane(myp5.width, myp5.height);
}).not.toThrowError();
});
suite('if statement conditionals', () => {
test('handle simple if statement with true condition', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const condition = myp5.uniformFloat(() => 1.0); // true condition
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.5); // initial gray
if (condition > 0.5) {
color = myp5.float(1.0); // set to white in if branch
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is white (condition was true)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 255, 5); // Red channel should be 255 (white)
assert.approximately(pixelColor[1], 255, 5); // Green channel should be 255
assert.approximately(pixelColor[2], 255, 5); // Blue channel should be 255
});
test('handle simple if statement with simpler assignment', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const condition = myp5.uniformFloat(() => 1.0); // true condition
myp5.getPixelInputs(inputs => {
let color = 1; // initial gray
if (condition > 0.5) {
color = 1; // set to white in if branch
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is white (condition was true)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 255, 5); // Red channel should be 255 (white)
assert.approximately(pixelColor[1], 255, 5); // Green channel should be 255
assert.approximately(pixelColor[2], 255, 5); // Blue channel should be 255
});
test('handle simple if statement with false condition', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const condition = myp5.uniformFloat(() => 0.0); // false condition
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.5); // initial gray
if (condition > 0.5) {
color = myp5.float(1.0); // set to white in if branch
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is gray (condition was false, original value kept)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // Red channel should be ~127 (gray)
assert.approximately(pixelColor[1], 127, 5); // Green channel should be ~127
assert.approximately(pixelColor[2], 127, 5); // Blue channel should be ~127
});
test('handle if-else statement', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const condition = myp5.uniformFloat(() => 0.0); // false condition
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.5); // initial gray
if (condition > 0.5) {
color = myp5.float(1.0); // white for true
} else {
color = myp5.float(0.0); // black for false
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is black (else branch executed)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 0, 5); // Red channel should be ~0 (black)
assert.approximately(pixelColor[1], 0, 5); // Green channel should be ~0
assert.approximately(pixelColor[2], 0, 5); // Blue channel should be ~0
});
test('handle multiple variable assignments in if statement', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const condition = myp5.uniformFloat(() => 1.0); // true condition
myp5.getPixelInputs(inputs => {
let red = myp5.float(0.0);
let green = myp5.float(0.0);
let blue = myp5.float(0.0);
if (condition > 0.5) {
red = myp5.float(1.0);
green = myp5.float(0.5);
blue = myp5.float(0.0);
}
inputs.color = [red, green, blue, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel has the expected color (red=1.0, green=0.5, blue=0.0)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 255, 5); // Red channel should be 255
assert.approximately(pixelColor[1], 127, 5); // Green channel should be ~127
assert.approximately(pixelColor[2], 0, 5); // Blue channel should be ~0
});
test('handle modifications after if statement', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const condition = myp5.uniformFloat(() => 1.0); // true condition
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.0); // start with black
if (condition > 0.5) {
color = myp5.float(1.0); // set to white in if branch
} else {
color = myp5.float(0.5); // set to gray in else branch
}
// Modify the color after the if statement
color = color * 0.5; // Should result in 0.5 * 1.0 = 0.5 (gray)
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is gray (white * 0.5 = gray)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // Red channel should be ~127 (gray)
assert.approximately(pixelColor[1], 127, 5); // Green channel should be ~127
assert.approximately(pixelColor[2], 127, 5); // Blue channel should be ~127
});
test('handle modifications after if statement in both branches', async () => {
await myp5.createCanvas(100, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
const uv = inputs.texCoord;
const condition = uv.x > 0.5; // left half false, right half true
let color = myp5.float(0.0);
if (condition) {
color = myp5.float(1.0); // white on right side
} else {
color = myp5.float(0.8); // light gray on left side
}
// Multiply by 0.5 after the if statement
color = color * 0.5;
// Right side: 1.0 * 0.5 = 0.5 (medium gray)
// Left side: 0.8 * 0.5 = 0.4 (darker gray)
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check left side (false condition)
const leftPixel = await myp5.get(25, 25)
const rightPixel = await myp5.get(75, 25)
assert.approximately(leftPixel[0], 102, 5); // 0.4 * 255 ≈ 102
// Check right side (true condition)
assert.approximately(rightPixel[0], 127, 5); // 0.5 * 255 ≈ 127
});
test('handle if-else-if chains', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const value = myp5.uniformFloat(() => 0.5); // middle value
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.0);
if (value > 0.8) {
color = myp5.float(1.0); // white for high values
} else if (value > 0.3) {
color = myp5.float(0.5); // gray for medium values
} else {
color = myp5.float(0.0); // black for low values
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is gray (medium condition was true)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // Red channel should be ~127 (gray)
assert.approximately(pixelColor[1], 127, 5); // Green channel should be ~127
assert.approximately(pixelColor[2], 127, 5); // Blue channel should be ~127
});
test('handle if-else-if chains in the else branch', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const value = myp5.uniformFloat(() => 0.2); // middle value
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.0);
if (value > 0.8) {
color = myp5.float(1.0); // white for high values
} else if (value > 0.3) {
color = myp5.float(0.5); // gray for medium values
} else {
color = myp5.float(0.0); // black for low values
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is black (else condition was true)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 0, 5);
assert.approximately(pixelColor[1], 0, 5);
assert.approximately(pixelColor[2], 0, 5);
});
test('handle conditional assignment in if-else-if chains', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const val = myp5.uniformFloat(() => Math.PI * 8);
myp5.getPixelInputs(inputs => {
let shininess = 0
let color = 0
if (val > 5) {
const elevation = myp5.sin(val)
if (elevation > 0.4) {
shininess = 0;
} else if (elevation > 0.25) {
shininess = 30;
} else {
color = 1;
shininess = 100;
}
} else {
shininess += 25;
}
inputs.shininess = shininess;
inputs.color = [color, color, color, 1];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is 255 (hit nested else statement)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 255, 5);
assert.approximately(pixelColor[1], 255, 5);
assert.approximately(pixelColor[2], 255, 5);
});
test('handle nested if statements', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const outerCondition = myp5.uniformFloat(() => 1.0); // true
const innerCondition = myp5.uniformFloat(() => 1.0); // true
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.0);
if (outerCondition > 0.5) {
if (innerCondition > 0.5) {
color = myp5.float(1.0); // white for both conditions true
} else {
color = myp5.float(0.5); // gray for outer true, inner false
}
} else {
color = myp5.float(0.0); // black for outer false
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is white (both conditions were true)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 255, 5); // Red channel should be 255 (white)
assert.approximately(pixelColor[1], 255, 5); // Green channel should be 255
assert.approximately(pixelColor[2], 255, 5); // Blue channel should be 255
});
// Keep one direct API test for completeness
test('handle direct StrandsIf API usage', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const conditionValue = myp5.uniformFloat(() => 1.0); // true condition
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.5); // initial gray
const assignments = myp5.strandsIf(
conditionValue.greaterThan(0),
() => {
let tmp = color.copy();
tmp = myp5.float(1.0); // set to white in if branch
return { color: tmp };
}
).Else(() => {
return { color: color }; // keep original in else branch
});
color = assignments.color;
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is white (condition was true)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 255, 5); // Red channel should be 255 (white)
assert.approximately(pixelColor[1], 255, 5); // Green channel should be 255
assert.approximately(pixelColor[2], 255, 5); // Blue channel should be 255
});
test('handle direct StrandsIf ElseIf API usage', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const value = myp5.uniformFloat(() => 0.5); // middle value
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.0); // initial black
const assignments = myp5.strandsIf(
value.greaterThan(0.8),
() => {
let tmp = color.copy();
tmp = myp5.float(1.0); // white for high values
return { color: tmp };
}
).ElseIf(
value.greaterThan(0.3),
() => {
let tmp = color.copy();
tmp = myp5.float(0.5); // gray for medium values
return { color: tmp };
}
).Else(() => {
let tmp = color.copy();
tmp = myp5.float(0.0); // black for low values
return { color: tmp };
});
color = assignments.color;
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Check that the center pixel is gray (medium condition was true)
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // Red channel should be ~127 (gray)
assert.approximately(pixelColor[1], 127, 5); // Green channel should be ~127
assert.approximately(pixelColor[2], 127, 5); // Blue channel should be ~127
});
test('using boolean intermediate variables', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseFilterShader().modify(() => {
myp5.getColor((inputs, canvasContent) => {
let value = 1;
let condition = 1 > 2;
if (value < 0.5) {
condition = 0.5 < 2;
}
if (condition) {
return [1, 0, 0, 1]
}
return [0.4, 0, 0, 1];
});
}, { myp5 });
myp5.background(255, 255, 255);
myp5.filter(testShader);
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 0.4 * 255, 5);
assert.approximately(pixelColor[1], 0, 5);
assert.approximately(pixelColor[2], 0, 5);
});
test('using boolean intermediate variables in functions', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseFilterShader().modify(() => {
const conditionMet = () => {
let condition = 1 > 2;
let value = 1;
if (value < 0.5) {
condition = 0.5 < 2;
}
return !condition
}
myp5.getColor((inputs, canvasContent) => {
if (conditionMet()) {
return [1, 0, 0, 1]
}
return [0.4, 0, 0, 1];
});
}, { myp5 });
myp5.background(255, 255, 255);
myp5.filter(testShader);
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 255, 5);
assert.approximately(pixelColor[1], 0, 5);
assert.approximately(pixelColor[2], 0, 5);
});
test('using boolean intermediate variables in functions with early returns', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseFilterShader().modify(() => {
const conditionMet = () => {
let value = 1;
if (value < 0.5) {
return true
}
return false
}
myp5.getColor((inputs, canvasContent) => {
if (conditionMet()) {
return [1, 0, 0, 1]
}
return [0.4, 0, 0, 1];
});
}, { myp5 });
myp5.background(255, 255, 255);
myp5.filter(testShader);
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 102, 5);
assert.approximately(pixelColor[1], 0, 5);
assert.approximately(pixelColor[2], 0, 5);
});
});
suite('ternary expressions', () => {
test('ternary changes color based on left/right side of canvas', async () => {
await myp5.createCanvas(50, 25, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
inputs.color = inputs.texCoord.x > 0.5 ? [1, 0, 0, 1] : [0, 0, 1, 1];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
const leftPixel = await myp5.get(12, 12);
assert.approximately(leftPixel[0], 0, 5);
assert.approximately(leftPixel[1], 0, 5);
assert.approximately(leftPixel[2], 255, 5);
const rightPixel = await myp5.get(37, 12);
assert.approximately(rightPixel[0], 255, 5);
assert.approximately(rightPixel[1], 0, 5);
assert.approximately(rightPixel[2], 0, 5);
});
test('ternary with scalar values', async () => {
await myp5.createCanvas(50, 25, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
const brightness = inputs.texCoord.x > 0.5 ? 1.0 : 0.0;
inputs.color = [brightness, brightness, brightness, 1];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
const leftPixel = await myp5.get(12, 12);
assert.approximately(leftPixel[0], 0, 5);
assert.approximately(leftPixel[1], 0, 5);
assert.approximately(leftPixel[2], 0, 5);
const rightPixel = await myp5.get(37, 12);
assert.approximately(rightPixel[0], 255, 5);
assert.approximately(rightPixel[1], 255, 5);
assert.approximately(rightPixel[2], 255, 5);
});
});
suite('for loop statements', () => {
test('handle simple for loop with known iteration count', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let color = myp5.float(0.0);
for (let i = 0; i < 3; i++) {
color = color + 0.1;
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should loop 3 times: 0.0 + 0.1 + 0.1 + 0.1 = 0.3
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 77, 5); // 0.3 * 255 ≈ 77
assert.approximately(pixelColor[1], 77, 5);
assert.approximately(pixelColor[2], 77, 5);
});
test('handle swizzle assignments in loops', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let color = [0, 0, 0, 1];
for (let i = 0; i < 3; i++) {
color.rgb += 0.1;
}
inputs.color = color;
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should loop 3 times: 0.0 + 0.1 + 0.1 + 0.1 = 0.3
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 77, 5); // 0.3 * 255 ≈ 77
assert.approximately(pixelColor[1], 77, 5);
assert.approximately(pixelColor[2], 77, 5);
});
test('handle for loop with variable as loop bound', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
const maxIterations = myp5.uniformInt(() => 2);
myp5.getPixelInputs(inputs => {
let result = myp5.float(0.0);
for (let i = 0; i < maxIterations; i++) {
result = result + 0.25;
}
inputs.color = [result, result, result, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should loop 2 times: 0.0 + 0.25 + 0.25 = 0.5
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // 0.5 * 255 ≈ 127
assert.approximately(pixelColor[1], 127, 5);
assert.approximately(pixelColor[2], 127, 5);
});
test('handle for loop modifying multiple variables', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let red = myp5.float(0.0);
let green = myp5.float(0.0);
for (let i = 0; i < 4; i++) {
red = red + 0.125; // 4 * 0.125 = 0.5
green = green + 0.25; // 4 * 0.25 = 1.0
}
inputs.color = [red, green, 0.0, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // 0.5 * 255 ≈ 127
assert.approximately(pixelColor[1], 255, 5); // 1.0 * 255 = 255
assert.approximately(pixelColor[2], 0, 5); // 0.0 * 255 = 0
});
test('handle for loop with conditional inside', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let sum = myp5.float(0.0);
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) {
sum = sum + 0.1; // Add on even iterations: 0, 2, 4
}
}
inputs.color = [sum, sum, sum, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should add 0.1 three times (iterations 0, 2, 4): 3 * 0.1 = 0.3
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 77, 5); // 0.3 * 255 ≈ 77
assert.approximately(pixelColor[1], 77, 5);
assert.approximately(pixelColor[2], 77, 5);
});
test('handle nested for loops', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let total = myp5.float(0.0);
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 3; j++) {
total = total + 0.05; // 2 * 3 = 6 iterations
}
}
inputs.color = [total, total, total, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should run 6 times: 6 * 0.05 = 0.3
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 77, 5); // 0.3 * 255 ≈ 77
assert.approximately(pixelColor[1], 77, 5);
assert.approximately(pixelColor[2], 77, 5);
});
test('handle complex nested for loops with multiple phi assignments', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let outerSum = myp5.float(0.0);
let globalCounter = myp5.float(0.0);
// Outer for loop modifying multiple variables
for (let i = 0; i < 2; i++) {
let innerSum = myp5.float(0.0);
let localCounter = myp5.float(0.0);
// Inner for loop also modifying multiple variables
for (let j = 0; j < 2; j++) {
innerSum = innerSum + 0.1;
localCounter = localCounter + 1.0;
globalCounter = globalCounter + 0.5; // This modifies outer scope
}
// Complex state modification between loops involving all variables
innerSum = innerSum * localCounter; // 0.2 * 2.0 = 0.4
outerSum = outerSum + innerSum; // Add to outer sum
globalCounter = globalCounter * 0.5; // Modify global again
}
// Final result should be: 2 iterations * 0.4 = 0.8 for outerSum
// globalCounter: ((0 + 2*0.5)*0.5 + 2*0.5)*0.5 = ((1)*0.5 + 1)*0.5 = 1.5*0.5 = 0.75
inputs.color = [outerSum, globalCounter, 0.0, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 204, 5); // 0.8 * 255 ≈ 204
assert.approximately(pixelColor[1], 191, 5); // 0.75 * 255 ≈ 191
assert.approximately(pixelColor[2], 0, 5);
});
test('handle nested for loops with state modification between loops', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let total = myp5.float(0.0);
// Outer for loop
for (let i = 0; i < 2; i++) {
let innerSum = myp5.float(0.0);
// Inner for loop
for (let j = 0; j < 3; j++) {
innerSum = innerSum + 0.1; // 3 * 0.1 = 0.3 per outer iteration
}
// State modification between inner and outer loop
innerSum = innerSum * 0.5; // Multiply by 0.5: 0.3 * 0.5 = 0.15
total = total + innerSum; // Add to total: 2 * 0.15 = 0.3
}
inputs.color = [total, total, total, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should be: 2 iterations * (3 * 0.1 * 0.5) = 2 * 0.15 = 0.3
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 77, 5); // 0.3 * 255 ≈ 77
assert.approximately(pixelColor[1], 77, 5);
assert.approximately(pixelColor[2], 77, 5);
});
test('handle for loop using loop variable in calculations', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let sum = myp5.float(0.0);
for (let i = 1; i <= 3; i++) {
sum = sum + (i * 0.1); // 1*0.1 + 2*0.1 + 3*0.1 = 0.6
}
inputs.color = [sum, sum, sum, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should be: 0.1 + 0.2 + 0.3 = 0.6
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 153, 5); // 0.6 * 255 ≈ 153
assert.approximately(pixelColor[1], 153, 5);
assert.approximately(pixelColor[2], 153, 5);
});
// Keep one direct API test for completeness
test('handle direct StrandsFor API usage', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let accumulator = myp5.float(0.0);
const loopResult = myp5.strandsFor(
() => 0,
(loopVar) => loopVar < 4,
(loopVar) => loopVar + 1,
(loopVar, vars) => {
let newValue = vars.accumulator.copy();
newValue = newValue + 0.125;
return { accumulator: newValue };
},
{ accumulator: accumulator.copy() },
);
accumulator = loopResult.accumulator;
inputs.color = [accumulator, accumulator, accumulator, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should loop 4 times: 4 * 0.125 = 0.5
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // 0.5 * 255 ≈ 127
assert.approximately(pixelColor[1], 127, 5);
assert.approximately(pixelColor[2], 127, 5);
});
test('handle for loop with break statement', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
const testShader = myp5.baseMaterialShader().modify(() => {
myp5.getPixelInputs(inputs => {
let color = 0;
let maxIterations = 5;
for (let i = 0; i < 100; i++) {
if (i >= maxIterations) {
break;
}
color = color + 0.1;
}
inputs.color = [color, color, color, 1.0];
return inputs;
});
}, { myp5 });
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// Should break after 5 iterations: 5 * 0.1 = 0.5
const pixelColor = await myp5.get(25, 25);
assert.approximately(pixelColor[0], 127, 5); // 0.5 * 255 ≈ 127
});
});
suite('passing data between shaders', () => {
test('handle passing a value from a vertex hook to a fragment hook', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
myp5.pixelDensity(1);
const testShader = myp5.baseMaterialShader().modify(() => {
let worldPos = myp5.varyingVec3();
myp5.getWorldInputs((inputs) => {
worldPos = inputs.position.xyz;
return inputs;
});
myp5.getFinalColor((c) => {
return [myp5.abs(worldPos / 25), 1];
});
}, { myp5 });
myp5.background(0, 0, 255); // Make the background blue to tell it apart
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
// The middle should have position 0,0 which translates to black
await myp5.loadPixels();
const midColor = myp5.pixels.slice((25 * myp5.width + 25) * 4, (25 * myp5.width + 25) * 4 + 4);
assert.approximately(midColor[0], 0, 5);
assert.approximately(midColor[1], 0, 5);
assert.approximately(midColor[2], 0, 5);
// The corner should have position 1,1 which translates to yellow
const cornerColor = myp5.pixels.slice((0 * myp5.width + 0) * 4, (0 * myp5.width + 0) * 4 + 4);
assert.approximately(cornerColor[0], 255, 5);
assert.approximately(cornerColor[1], 255, 5);
assert.approximately(cornerColor[2], 0, 5);
});
test('handle passing a value from a vertex hook to a fragment hook with swizzle assignment', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
myp5.pixelDensity(1);
const testShader = myp5.baseMaterialShader().modify(() => {
let worldPos = myp5.varyingVec3();
myp5.getWorldInputs((inputs) => {
worldPos.xyz = inputs.position.xyz;
return inputs;
});
myp5.getFinalColor((c) => {
return [myp5.abs(worldPos / 25), 1];
});
}, { myp5 });
myp5.background(0, 0, 255); // Make the background blue to tell it apart
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
await myp5.loadPixels();
// The middle should have position 0,0 which translates to black
const midColor = await myp5.pixels.slice((25 * myp5.width + 25) * 4, (25 * myp5.width + 25) * 4 + 4);
assert.approximately(midColor[0], 0, 5);
assert.approximately(midColor[1], 0, 5);
assert.approximately(midColor[2], 0, 5);
// The corner should have position 1,1 which translates to yellow
const cornerColor = await myp5.pixels.slice((0 * myp5.width + 0) * 4, (0 * myp5.width + 0) * 4 + 4);
assert.approximately(cornerColor[0], 255, 5);
assert.approximately(cornerColor[1], 255, 5);
assert.approximately(cornerColor[2], 0, 5);
});
test('handle passing a value from a vertex hook to a fragment hook as part of hook output', async () => {
await myp5.createCanvas(50, 50, myp5.WEBGPU);
myp5.pixelDensity(1);
const testShader = myp5.baseMaterialShader().modify(() => {
let worldPos = myp5.varyingVec3();
myp5.getWorldInputs((inputs) => {
worldPos = inputs.position.xyz;
inputs.position.xyz = worldPos + [25, 25, 0];
return inputs;
});
myp5.getFinalColor((c) => {
return [myp5.abs(worldPos / 25), 1];
});
}, { myp5 });
myp5.background(0, 0, 255); // Make the background blue to tell it apart
myp5.noStroke();
myp5.shader(testShader);
myp5.plane(myp5.width, myp5.height);
await myp5.loadPixels();
// The middle (shifted +25,25) should have position 0,0 which translates to black
const midColor = myp5.pixels.slice((49 * myp5.width + 49) * 4, (49 * myp5.width + 49) * 4 + 4);
assert.approximately(midColor[0], 0, 5);
assert.approximately(midColor[1], 0, 5);
assert.approximately(midColor[2], 0, 5);
// The corner (shifted +25,25) should have position 1,1 which translates to yellow
const cornerColor = myp5.pixels.slice((25 * myp5.width + 25) * 4, (25 * myp5.width + 25) * 4 + 4);
assert.approximately(cornerColor[0], 255, 5);
assert.approximately(cornerColor[1], 255, 5);
assert.approximately(cornerColor[2], 0, 5);
});