-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathwebgl.js
More file actions
1864 lines (1678 loc) · 54.8 KB
/
Copy pathwebgl.js
File metadata and controls
1864 lines (1678 loc) · 54.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 { vi, afterEach } from 'vitest';
import { visualSuite, visualTest } from '../visualTest';
visualSuite('WebGL', function() {
visualSuite('Camera', function() {
visualTest('2D objects maintain correct size', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.noStroke();
p5.fill('red');
p5.rectMode(p5.CENTER);
p5.rect(0, 0, p5.width/2, p5.height/2);
screenshot();
});
visualTest('Custom camera before and after resize', function(p5, screenshot) {
p5.createCanvas(25, 50, p5.WEBGL);
const cam = p5.createCamera();
p5.setCamera(cam);
cam.setPosition(-10, -10, 800);
p5.strokeWeight(4);
p5.box(20);
screenshot();
p5.resizeCanvas(50, 25);
p5.box(20);
screenshot();
});
visualTest('Camera settings on framebuffers reset after push/pop', function(p5, screenshot) {
p5.createCanvas(100, 100, p5.WEBGL);
p5.setAttributes({ antialias: true });
const fbo = p5.createFramebuffer();
p5.background(220);
p5.imageMode(p5.CENTER);
fbo.begin();
p5.push();
p5.ortho();
p5.translate(0, -25);
for (let i = -1; i <= 1; i++) {
p5.push();
p5.translate(i * 35, 0);
p5.box(25, 25, 150);
p5.pop();
}
p5.pop();
p5.push();
p5.translate(0, 25);
for (let i = -1; i <= 1; i++) {
p5.push();
p5.translate(i * 35, 0);
p5.box(25, 25, 150);
p5.pop();
}
p5.pop();
fbo.end();
p5.image(fbo, 0, 0);
screenshot();
});
visualTest('Works after perspective() with no args', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(200);
p5.perspective();
p5.noStroke();
p5.lights();
p5.sphere(20);
screenshot();
});
});
visualSuite('filter', function() {
visualTest('On the main canvas', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.noStroke();
p5.fill('red');
p5.circle(0, 0, 20);
p5.filter(p5.GRAY);
screenshot();
});
visualTest('On a framebuffer', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
const fbo = p5.createFramebuffer({ antialias: true });
fbo.begin();
p5.noStroke();
p5.fill('red');
p5.circle(0, 0, 20);
p5.filter(p5.GRAY);
fbo.end();
p5.imageMode(p5.CENTER);
p5.image(fbo, 0, 0);
screenshot();
});
visualTest('On a framebuffer of a different size from the canvas', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
const fbo = p5.createFramebuffer({ antialias: true, width: 25, height: 100 });
fbo.begin();
p5.background('blue');
p5.fill('red');
p5.circle(0, 0, 20);
p5.filter(p5.BLUR, 3);
fbo.end();
p5.imageMode(p5.CENTER);
p5.image(fbo, 0, 0);
screenshot();
});
visualTest(
'On a framebuffer sized differently from the main canvas',
function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
const fbo = p5.createFramebuffer({
width: 26,
height: 26,
antialias: true
});
fbo.begin();
p5.noStroke();
p5.fill('red');
p5.circle(0, 0, 20);
p5.filter(p5.GRAY);
fbo.end();
p5.imageMode(p5.CENTER);
p5.image(fbo, 0, 0);
screenshot();
}
);
for (const mode of ['webgl', '2d']) {
visualSuite(`In ${mode} mode`, function() {
const setupSketch = (p5) => {
p5.createCanvas(50, 50, mode === 'webgl' ? p5.WEBGL : p5.P2D);
if (mode === 'webgl') p5.translate(-p5.width/2, -p5.height/2);
p5.clear();
p5.noStroke();
p5.fill('red');
p5.circle(20, 20, 15);
if (mode === 'webgl') {
p5.beginShape(p5.QUAD_STRIP);
p5.fill('cyan');
p5.vertex(35, 35);
p5.vertex(45, 35);
p5.fill('blue');
p5.vertex(35, 45);
p5.vertex(45, 45);
p5.endShape();
} else {
p5.push();
const grad = p5.drawingContext.createLinearGradient(35, 35, 35, 45);
grad.addColorStop(0, 'cyan');
grad.addColorStop(1, 'blue');
p5.drawingContext.fillStyle = grad;
p5.rect(35, 35, 10, 10);
p5.pop();
}
};
visualTest('It can apply GRAY', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.GRAY);
screenshot();
});
visualTest('It can apply INVERT', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.INVERT);
screenshot();
});
visualTest('It can apply THRESHOLD', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.THRESHOLD);
screenshot();
});
visualTest('It can apply THRESHOLD with a value', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.THRESHOLD, 0.8);
screenshot();
});
visualTest('It can apply POSTERIZE', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.THRESHOLD);
screenshot();
});
visualTest('It can apply POSTERIZE with a value', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.THRESHOLD, 2);
screenshot();
});
visualTest('It can apply BLUR', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.BLUR, 5);
screenshot();
});
visualTest('It can apply BLUR with a value', function(p5, screenshot) {
setupSketch(p5);
p5.filter(p5.BLUR, 10);
screenshot();
});
visualTest('It can apply ERODE (4x)', function(p5, screenshot) {
setupSketch(p5);
for (let i = 0; i < 4; i++) p5.filter(p5.ERODE);
screenshot();
});
visualTest('It can apply DILATE (4x)', function(p5, screenshot) {
setupSketch(p5);
for (let i = 0; i < 4; i++) p5.filter(p5.DILATE);
screenshot();
});
});
}
for (const mode of ['webgl', '2d']) {
visualSuite(`In ${mode} mode`, function() {
visualTest('It can use filter shader hooks', function(p5, screenshot) {
p5.createCanvas(50, 50, mode === 'webgl' ? p5.WEBGL : p5.P2D);
const s = p5.baseFilterShader().modify({
'vec4 getColor': `(FilterInputs inputs, in sampler2D content) {
vec4 c = getTexture(content, inputs.texCoord);
float avg = (c.r + c.g + c.b) / 3.0;
return vec4(avg, avg, avg, c.a);
}`
});
if (mode === 'webgl') p5.translate(-p5.width/2, -p5.height/2);
p5.background(255);
p5.fill('red');
p5.noStroke();
p5.circle(15, 15, 20);
p5.circle(30, 30, 20);
p5.filter(s);
screenshot();
});
});
}
for (const mode of ['webgl', '2d']) {
visualSuite(`In ${mode} mode`, function() {
visualTest('It can combine multiple filter passes', function(p5, screenshot) {
p5.createCanvas(50, 50, mode === 'webgl' ? p5.WEBGL : p5.P2D);
if (mode === 'webgl') p5.translate(-p5.width/2, -p5.height/2);
p5.background(255);
p5.fill(0);
p5.noStroke();
p5.circle(15, 15, 20);
p5.circle(30, 30, 20);
p5.filter(p5.BLUR, 5);
p5.filter(p5.THRESHOLD);
screenshot();
});
});
}
visualTest('On a createGraphics WEBGL buffer', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
const g = p5.createGraphics(50, 50, p5.WEBGL);
g.background(255);
g.noStroke();
g.fill('red');
g.circle(0, 0, 30);
g.filter(p5.INVERT);
p5.imageMode(p5.CENTER);
p5.image(g, 0, 0);
screenshot();
});
for (const mode of ['webgl', '2d']) {
visualTest(`Transparent background colors are correct in ${mode} mode`, function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
const g = p5.createGraphics(50, 50, mode === 'webgl' ? p5.WEBGL : p5.P2D);
if (mode === 'webgl') g.translate(-p5.width/2, -p5.height/2);
g.noStroke();
g.fill(255, 0, 0, 100);
g.rect(10, 10, 30, 30);
g.filter(p5.BLUR, 4);
p5.imageMode(p5.CENTER);
p5.image(g, 0, 0);
screenshot();
});
visualTest(`Multiple filter passes work correctly on a p5.Graphics in ${mode} mode`, function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
const g = p5.createGraphics(50, 50, mode === 'webgl' ? p5.WEBGL : p5.P2D);
if (mode === 'webgl') g.translate(-g.width/2, -g.height/2);
g.background(255);
g.noStroke();
g.fill(0);
g.rect(10, 10, 6, 6);
g.filter(p5.BLUR, 2);
g.rect(30, 30, 6, 6);
g.filter(p5.BLUR, 2);
p5.imageMode(p5.CENTER);
p5.image(g, 0, 0);
screenshot();
});
}
});
visualSuite('Lights', function() {
visualTest('Fill color and default ambient material', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.noStroke();
p5.lights();
p5.fill('red');
p5.sphere(20);
screenshot();
});
});
visualSuite('3DModel', function() {
visualTest('OBJ model with MTL file displays diffuse colors correctly', function(p5, screenshot) {
return new Promise(resolve => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.loadModel('test/unit/assets/octa-color.obj', model => {
p5.background(255);
p5.rotateX(10 * 0.01);
p5.rotateY(10 * 0.01);
model.normalize();
p5.model(model);
screenshot();
resolve();
});
});
});
visualTest('Object with no colors takes on fill color', function(p5, screenshot) {
return new Promise(resolve => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.loadModel('test/unit/assets/cube.obj', model => {
p5.background(255);
p5.fill('blue'); // Setting a fill color
p5.rotateX(p5.frameCount * 0.01);
p5.rotateY(p5.frameCount * 0.01);
model.normalize();
p5.model(model);
screenshot();
resolve();
});
});
});
visualTest(
'Object with different texture coordinates per use of vertex keeps the coordinates intact',
async function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = await p5.loadImage('test/unit/assets/cat.jpg');
const cube = await new Promise(resolve => p5.loadModel('test/unit/assets/cube-textures.obj', resolve));
cube.normalize();
p5.background(255);
p5.texture(tex);
p5.rotateX(p5.PI / 4);
p5.rotateY(p5.PI / 4);
p5.scale(80/400);
p5.model(cube);
screenshot();
}
);
});
visualSuite('vertexProperty', function(){
const vertSrc = `#version 300 es
precision mediump float;
uniform mat4 uProjectionMatrix;
uniform mat4 uModelViewMatrix;
in vec3 aPosition;
in vec3 aCol;
out vec3 vCol;
void main(){
vCol = aCol;
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition, 1.0);
}`;
const fragSrc = `#version 300 es
precision mediump float;
in vec3 vCol;
out vec4 outColor;
void main(){
outColor = vec4(vCol, 1.0);
}`;
visualTest(
'on PATH shape mode', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background('white');
const myShader = p5.createShader(vertSrc, fragSrc);
p5.shader(myShader);
p5.beginShape(p5.PATH);
p5.noStroke();
for (let i = 0; i < 20; i++){
let x = 20 * p5.sin(i/20*p5.TWO_PI);
let y = 20 * p5.cos(i/20*p5.TWO_PI);
p5.vertexProperty('aCol', [x/20, -y/20, 0]);
p5.vertex(x, y);
}
p5.endShape();
screenshot();
}
);
visualTest(
'on QUADS shape mode', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background('white');
const myShader = p5.createShader(vertSrc, fragSrc);
p5.shader(myShader);
p5.beginShape(p5.QUADS);
p5.noStroke();
p5.translate(-25,-25);
for (let i = 0; i < 5; i++){
for (let j = 0; j < 5; j++){
let x1 = i * 10;
let x2 = x1 + 10;
let y1 = j * 10;
let y2 = y1 + 10;
p5.vertexProperty('aCol', [1, 0, 0]);
p5.vertex(x1, y1);
p5.vertexProperty('aCol', [0, 0, 1]);
p5.vertex(x2, y1);
p5.vertexProperty('aCol', [0, 1, 1]);
p5.vertex(x2, y2);
p5.vertexProperty('aCol', [1, 1, 1]);
p5.vertex(x1, y2);
}
}
p5.endShape();
screenshot();
}
);
visualTest(
'on buildGeometry outputs containing 3D primitives', function(p5, screenshot) {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background('white');
const myShader = p5.createShader(vertSrc, fragSrc);
p5.shader(myShader);
const shape = p5.buildGeometry(() => {
p5.push();
p5.translate(15,-10,0);
p5.sphere(5);
p5.pop();
p5.beginShape(p5.TRIANGLES);
p5.vertexProperty('aCol', [1,0,0]);
p5.vertex(-5, 5, 0);
p5.vertexProperty('aCol', [0,1,0]);
p5.vertex(5, 5, 0);
p5.vertexProperty('aCol', [0,0,1]);
p5.vertex(0, -5, 0);
p5.endShape(p5.CLOSE);
p5.push();
p5.translate(-15,10,0);
p5.box(10);
p5.pop();
});
p5.model(shape);
screenshot();
}
);
});
visualSuite('ShaderFunctionality', function() {
visualTest('FillShader', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const img = await p5.loadImage('test/unit/assets/cat.jpg');
const fillShader = p5.createShader(
`
attribute vec3 aPosition;
void main() {
gl_Position = vec4(aPosition, 1.0);
}
`,
`
precision mediump float;
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`
);
p5.shader(fillShader);
p5.lights();
p5.texture(img);
p5.noStroke();
p5.rect(-p5.width / 2, -p5.height / 2, p5.width, p5.height);
screenshot();
});
visualTest('StrokeShader', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
// Create a stroke shader with a fading effect based on distance
const strokeshader = p5.baseStrokeShader().modify({
'Inputs getPixelInputs': `(Inputs inputs) {
float opacity = 1.0 - smoothstep(
0.0,
15.0,
length(inputs.position - inputs.center)
);
inputs.color *= opacity;
return inputs;
}`
});
p5.strokeShader(strokeshader);
p5.strokeWeight(15);
p5.line(
-p5.width / 3,
p5.sin(0.2) * p5.height / 4,
p5.width / 3,
p5.sin(1.2) * p5.height / 4
);
screenshot();
});
visualTest('ImageShader', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const img = await p5.loadImage('test/unit/assets/cat.jpg');
const imgShader = p5.createShader(
`
precision mediump float;
attribute vec3 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
void main() {
vTexCoord = aTexCoord;
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition, 1.0);
}
`,
`
precision mediump float;
varying vec2 vTexCoord;
uniform sampler2D uTexture;
void main() {
vec4 texColor = texture2D(uTexture, vTexCoord);
gl_FragColor = texColor * vec4(1.5, 0.5, 0.5, 1.0);
}
`
);
p5.imageShader(imgShader);
imgShader.setUniform('uTexture', img);
p5.noStroke();
p5.image(img, -p5.width / 2, -p5.height / 2, p5.width, p5.height);
screenshot();
});
visualTest('loadMaterialShader', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const materialShader = await p5.loadMaterialShader('/test/unit/assets/testMaterial.js');
p5.noStroke();
p5.shader(materialShader);
p5.plane(p5.width, p5.height);
screenshot();
});
visualTest('loadFilterShader', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
// Create a scene to filter (red and green stripes)
p5.background(255);
p5.noStroke();
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) {
p5.fill(255, 0, 0); // Red
} else {
p5.fill(0, 255, 0); // Green
}
p5.rect(-p5.width/2 + i * 10, -p5.height/2, 10, p5.height);
}
// Apply the filter shader (should swap red and green channels)
const filterShader = await p5.loadFilterShader('/test/unit/assets/testFilter.js');
p5.filter(filterShader);
screenshot();
});
});
visualSuite('Strokes', function() {
visualTest('Strokes do not cut into fills in ortho mode', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(220);
p5.stroke(8);
p5.ortho();
p5.rotateX(p5.PI/4);
p5.rotateY(p5.PI/4);
p5.box(30);
screenshot();
});
});
visualSuite('Opacity', function() {
visualTest('Basic colors have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(255);
p5.fill(255, 100, 100, 100);
p5.circle(0, 0, 50);
screenshot();
});
visualTest('Colors have opacity applied correctly when lights are used', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(255);
p5.ambientLight(255);
p5.fill(255, 100, 100, 100);
p5.circle(0, 0, 50);
screenshot();
});
visualTest('Colors in shader hooks have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const myShader = p5.baseMaterialShader().modify({
'Inputs getPixelInputs': `(Inputs inputs) {
inputs.color = vec4(1., 0.4, 0.4, 100./255.);
return inputs;
}`
});
p5.background(255);
p5.shader(myShader);
p5.circle(0, 0, 50);
screenshot();
});
visualTest('Colors in textures have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = p5.createFramebuffer();
tex.draw(() => p5.background(255, 100, 100, 100));
p5.background(255);
p5.texture(tex);
p5.circle(0, 0, 50);
screenshot();
});
visualTest('Colors in tinted textures have opacity applied correctly', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = p5.createFramebuffer();
tex.draw(() => p5.background(255, 100, 100, 255));
p5.background(255);
p5.texture(tex);
p5.tint(255, 100);
p5.circle(0, 0, 50);
screenshot();
});
visualTest('noTint() before image() does not throw', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const img = await p5.loadImage('/test/unit/assets/cat.jpg');
p5.noTint();
p5.imageMode(p5.CENTER);
p5.image(img, 0, 0, 50, 50);
screenshot();
});
});
visualSuite('Hooks coordinate spaces', () => {
for (const base of ['baseMaterialShader', 'baseColorShader', 'baseNormalShader']) {
visualSuite(base, () => {
visualTest('Object space', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const myShader = p5[base]().modify({
'Vertex getObjectInputs': `(Vertex inputs) {
inputs.position.x += 0.25;
inputs.normal.x += 0.5 * sin(inputs.position.y * 2.);
inputs.normal = normalize(inputs.normal);
return inputs;
}`
});
p5.background(255);
p5.lights();
p5.fill('red');
p5.noStroke();
p5.rotateY(p5.PI/2);
p5.camera(-800, 0, 0, 0, 0, 0);
p5.shader(myShader);
p5.sphere(20);
screenshot();
});
visualTest('World space', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const myShader = p5[base]().modify({
'Vertex getWorldInputs': `(Vertex inputs) {
inputs.position.x += 10.;
inputs.normal.x += 0.5 * sin(inputs.position.y * 2.);
inputs.normal = normalize(inputs.normal);
return inputs;
}`
});
p5.background(255);
p5.lights();
p5.fill('red');
p5.noStroke();
p5.rotateY(p5.PI/2);
p5.camera(-800, 0, 0, 0, 0, 0);
p5.shader(myShader);
p5.sphere(20);
screenshot();
});
visualTest('Camera space', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const myShader = p5[base]().modify({
'Vertex getCameraInputs': `(Vertex inputs) {
inputs.position.x += 10.;
inputs.normal.x += 0.5 * sin(inputs.position.y * 2.);
inputs.normal = normalize(inputs.normal);
return inputs;
}`
});
p5.background(255);
p5.lights();
p5.fill('red');
p5.noStroke();
p5.rotateY(p5.PI/2);
p5.camera(-800, 0, 0, 0, 0, 0);
p5.shader(myShader);
p5.sphere(20);
screenshot();
});
visualTest('Combined vs split matrices', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
for (const space of ['Object', 'World', 'Camera']) {
const myShader = p5[base]().modify({
[`Vertex get${space}Inputs`]: `(Vertex inputs) {
// No-op
return inputs;
}`
});
p5.background(255);
p5.push();
p5.lights();
p5.fill('red');
p5.noStroke();
p5.translate(20, -10, 5);
p5.rotate(p5.PI * 0.1);
p5.camera(-800, 0, 0, 0, 0, 0);
p5.shader(myShader);
p5.box(30);
p5.pop();
screenshot();
}
});
});
}
});
visualSuite('textToModel', () => {
visualTest('Flat', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const font = await p5.loadFont(
'test/unit/assets/Inconsolata-Bold.ttf'
);
p5.textSize(20);
const geom = font.textToModel('p5*js', 0, 0, {
sampleFactor: 2
});
geom.normalize();
p5.background(255);
p5.normalMaterial();
p5.rotateX(p5.PI*0.1);
p5.rotateY(p5.PI*0.1);
p5.scale(50/200);
p5.model(geom);
screenshot();
});
visualTest('Extruded', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const font = await p5.loadFont(
'test/unit/assets/Inconsolata-Bold.ttf'
);
p5.textSize(20);
const geom = font.textToModel('p5*js', 0, 0, {
extrude: 10,
sampleFactor: 2
});
geom.normalize();
p5.background(255);
p5.normalMaterial();
p5.rotateX(p5.PI*0.1);
p5.rotateY(p5.PI*0.1);
p5.scale(50/200);
p5.model(geom);
screenshot();
});
});
visualSuite('erase()', () => {
visualTest('on the main canvas', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(0);
p5.fill('red');
p5.rect(-20, -20, 40, 40);
p5.erase();
p5.circle(0, 0, 10);
p5.noErase();
screenshot();
});
visualTest('on a framebuffer', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
p5.background(0);
const fbo = p5.createFramebuffer();
fbo.begin();
p5.fill('red');
p5.rect(-20, -20, 40, 40);
p5.erase();
p5.circle(0, 0, 10);
p5.noErase();
fbo.end();
p5.imageMode(p5.CENTER);
p5.image(fbo, 0, 0);
screenshot();
});
});
visualSuite('buildGeometry()', () => {
visualTest('can draw models', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const sphere = p5.buildGeometry(() => {
p5.scale(0.25);
p5.sphere();
});
const geom = p5.buildGeometry(() => {
p5.model(sphere);
});
p5.background(255);
p5.lights();
p5.model(geom);
screenshot();
});
visualTest('only fills set in buildGeometry are kept', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const geom = p5.buildGeometry(() => {
p5.push();
p5.translate(-p5.width*0.2, 0);
p5.scale(0.15);
p5.sphere();
p5.pop();
p5.push();
p5.fill('red');
p5.translate(p5.width*0.2, 0);
p5.scale(0.15);
p5.sphere();
p5.pop();
});
p5.fill('blue');
p5.noStroke();
p5.model(geom);
screenshot();
});
});
visualSuite('font data', () => {
afterEach(() => {
vi.restoreAllMocks();
});
visualTest('glyph resource allocation does not corrupt textures', async (p5, screenshot) => {
p5.createCanvas(100, 100, p5.WEBGL);
vi.spyOn(p5._renderer, 'maxCachedGlyphs').mockReturnValue(6);
const font = await p5.loadFont(
'test/unit/assets/Inconsolata-Bold.ttf'
);
p5.textFont(font);
p5.clear();
p5.textSize(10);
p5.textAlign(p5.LEFT, p5.TOP);
for (let i = 0; i < 100; i++) {
const x = -p5.width/2 + (i % 10) * 10;
const y = -p5.height/2 + p5.floor(i / 10) * 10;
p5.text(String.fromCharCode(33 + i), x, y);
}
screenshot();
});
visualTest('text renders correctly after geometry with many indices', async (p5, screenshot) => {
p5.createCanvas(100, 100, p5.WEBGL);
const font = await p5.loadFont(
'test/unit/assets/Inconsolata-Bold.ttf'
);
p5.background(255);
p5.noStroke();
p5.textFont(font);
p5.textSize(20);
p5.textAlign(p5.CENTER, p5.CENTER);
p5.text('Test 1', 0, -20);
// Draw a sphere which has many more indices than text
p5.fill(200, 200, 255);
p5.sphere(30);
p5.clearDepth();
// Draw text - should bind its own index buffer
p5.fill(0);
p5.text('Test 2', 0, 20);
screenshot();
});
});
visualSuite('texture()', () => {
visualTest('on a rect', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = await p5.loadImage('test/unit/assets/cat.jpg');
p5.texture(tex);
p5.rect(-20, -20, 40, 40);
screenshot();
});
visualTest('on a rect with rounded corners', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = await p5.loadImage('test/unit/assets/cat.jpg');
p5.texture(tex);
p5.rect(-20, -20, 40, 40, 10);
screenshot();
});
});
visualSuite('textures in p5.strands', () => {
visualTest('uniformTexture() works', async (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const tex = await p5.loadImage('test/unit/assets/cat.jpg');
const shader = p5.baseMaterialShader().modify(() => {
const texUniform = p5.uniformTexture(() => tex)
p5.getPixelInputs((inputs) => {
inputs.color = p5.getTexture(texUniform, inputs.texCoord);
return inputs;
});
}, { p5, tex });
p5.shader(shader);
p5.rect(-20, -20, 40, 40);
screenshot();
});
visualTest('getTexture in vertex shaders', (p5, screenshot) => {
p5.createCanvas(50, 50, p5.WEBGL);
const positionData = p5.createFramebuffer({
width: 3,
height: 1,
density: 1,
antialias: false,
format: p5.FLOAT,
textureFiltering: p5.NEAREST
});
positionData.loadPixels();
for (let i = 0; i < 3; i++) {
positionData.pixels[i * 4] = i / 3;
positionData.pixels[i * 4 + 1] = 0;
positionData.pixels[i * 4 + 2] = 0;
positionData.pixels[i * 4 + 3] = 1;
}
positionData.updatePixels();
const sh = p5.baseMaterialShader().modify(() => {
const data = p5.uniformTexture(() => positionData);
p5.getWorldInputs((inputs) => {
const angle = p5.getTexture(data, [p5.instanceIndex/3, 0]).r * p5.TWO_PI;
inputs.position.xy += [p5.cos(angle) * 10, p5.sin(angle) * 10];
return inputs;
});
}, { p5, positionData });
const instance = p5.buildGeometry(() => p5.sphere(3));
p5.noStroke();
p5.fill('red');
p5.shader(sh);
p5.model(instance, 3);
screenshot();
});
});
visualSuite("Image Based Lighting", function () {
const shinesses = [50, 150];
for (const shininess of shinesses) {
visualTest(
`${shininess < 100 ? 'low' : 'high'} shininess`,
async function (p5, screenshot) {
p5.createCanvas(100, 100, p5.WEBGL);
// Load the environment map
const env = await p5.loadImage('test/unit/assets/spheremap.jpg');
p5.clear();