-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.umd.js
3839 lines (3485 loc) · 119 KB
/
index.umd.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.kampos = {}));
})(this, (function (exports) { 'use strict';
/**
* Exposes the `u_resolution` uniform for use inside fragment shaders.
*
* @function resolution
* @param {Object} [params]
* @param {number} [params.width] initial canvas width. Defaults to `window.innerWidth`.
* @param {number} [params.height] initial canvas height. Defaults to `window.innerHeight`.
* @returns {resolutionUtility}
*
* @example resolution({width: 1600, height: 900})
*/
function resolution({
width = window.innerWidth,
height = window.innerHeight,
} = {}) {
/**
* @typedef {Object} resolutionUtility
* @property {{width: number?, height: number?}} resolution
*
* @example
* mouse.resolution = {width: 854, height: 480};
*/
return {
fragment: {
uniform: {
u_resolution: 'vec2',
},
},
get resolution() {
const [x, y] = this.uniforms[0].data;
return { x, y };
},
set resolution({ width: x, height: y }) {
if (typeof x !== 'undefined') this.uniforms[0].data[0] = x;
if (typeof y !== 'undefined') this.uniforms[0].data[1] = y;
},
uniforms: [
{
name: 'u_resolution',
type: 'f',
data: [width || window.innerWidth, height || window.innerHeight],
},
],
};
}
/**
* Exposes the `u_mouse` uniform for use inside fragment shaders.
*
* @function mouse
* @param {Object} [params]
* @param {{x: number?, y: number?}} [params.initial] initial mouse position. Defaults to `{x: 0, y: 0}`.
* @returns {mouseUtility}
*
* @example mouse({initial: {x: 0.5, y: 0.5}})
*/
function mouse({
initial = { x: 0, y: 0 },
} = {}) {
/**
* @typedef {Object} mouseUtility
* @property {{x: number?, y: number?}} position
*
* @example
* mouse.position = {x: 0.4, y: 0.2};
*/
return {
fragment: {
uniform: {
u_mouse: 'vec2',
},
},
get position() {
const [x, y] = this.uniforms[0].data;
return { x, y };
},
set position({ x, y }) {
if (typeof x !== 'undefined') this.uniforms[0].data[0] = x;
if (typeof y !== 'undefined') this.uniforms[0].data[1] = y;
},
uniforms: [
{
name: 'u_mouse',
type: 'f',
data: [initial.x || 0, initial.y || 0],
},
],
};
}
/**
* Exposes the `circle` function to be used by effects.
* This function takes a point, radius, and spread, and returns a value between 0 and 1.
*
* @function circle
* @returns {circleUtility}
*
* @example circle()
*/
function circle() {
/**
* @typedef {Object} circleUtility
*
* @example
* float aspectRatio = u_resolution.x / u_resolution.y;
* vec2 st_ = gl_FragCoord.xy / u_resolution;
* float circle_ = circle(
* vec2(st_.x * aspectRatio, st_.y),
* vec2(u_mouse.x * aspectRatio, u_mouse.y),
* 0.35,
* 0.1
* );
*/
return {
fragment: {
constant: `
float circle(vec2 _point1, vec2 _point2, float _radius, float _spread){
vec2 dist = _point1 - _point2;
return 1.0 - smoothstep(_radius - _spread, _radius + _spread, sqrt(dot(dist, dist)) / _radius);
}`
},
};
}
/**
* @function alphaMask
* @param {Object} [params]
* @param {boolean} [params.isLuminance=false] whether to use luminance when reading mask values
* @returns {alphaMaskEffect}
*
* @example alphaMask()
*/
function alphaMask ({ isLuminance = false } = {}) {
/**
* @typedef {Object} alphaMaskEffect
* @property {ArrayBufferView|ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|ImageBitmap} mask
* @property {boolean} disabled
* @property {boolean} isLuminance
*
* @description Multiplies `alpha` value with values read from `mask` media source.
*
* @example
* const img = new Image();
* img.src = 'picture.png';
* effect.mask = img;
* effect.disabled = true;
*/
return {
vertex: {
attribute: {
a_alphaMaskTexCoord: 'vec2',
},
main: `
v_alphaMaskTexCoord = a_alphaMaskTexCoord;`,
},
fragment: {
uniform: {
u_alphaMaskEnabled: 'bool',
u_alphaMaskIsLuminance: 'bool',
u_mask: 'sampler2D',
},
main: `
if (u_alphaMaskEnabled) {
vec4 alphaMaskPixel = texture2D(u_mask, v_alphaMaskTexCoord);
if (u_alphaMaskIsLuminance) {
alpha *= dot(lumcoeff, alphaMaskPixel.rgb) * alphaMaskPixel.a;
}
else {
alpha *= alphaMaskPixel.a;
}
}`,
},
get disabled() {
return !this.uniforms[0].data[0];
},
set disabled(b) {
this.uniforms[0].data[0] = +!b;
},
get mask() {
return this.textures[0].data;
},
set mask(img) {
this.textures[0].data = img;
},
get isLuminance() {
return !!this.uniforms[2].data[0];
},
set isLuminance(toggle) {
this.uniforms[2].data[0] = +toggle;
this.textures[0].format = toggle ? 'RGBA' : 'ALPHA';
},
varying: {
v_alphaMaskTexCoord: 'vec2',
},
uniforms: [
{
name: 'u_alphaMaskEnabled',
type: 'i',
data: [1],
},
{
name: 'u_mask',
type: 'i',
data: [1],
},
{
name: 'u_alphaMaskIsLuminance',
type: 'i',
data: [+!!isLuminance],
},
],
attributes: [
{
name: 'a_alphaMaskTexCoord',
extends: 'a_texCoord',
},
],
textures: [
{
format: isLuminance ? 'RGBA' : 'ALPHA',
},
],
};
}
/**
* Depends on the `resolution` utility.
* Depends on the `mouse` utility.
*
* @function deformation
* @param {Object} [params]
* @param {number} [params.radius] initial radius to use for circle of effect boundaries. Defaults to 0 which means no effect.
* @param {string} [params.wrap] wrapping method to use. Defaults to `deformation.CLAMP`.
* @param {string} [params.deformation] deformation method to use within the radius. Defaults to `deformation.NONE`.
* @returns {deformationEffect}
*
* @example deformation({radius: 0.1, wrap: deformation.CLAMP, deformation: deformation.TUNNEL})
*/
function deformation({
radius,
wrap = WRAP_METHODS$1.WRAP,
deformation = DEFORMATION_METHODS.NONE,
} = {}) {
const dataRadius = radius || 0;
/**
* @typedef {Object} deformationEffect
* @property {boolean} disabled
* @property {number} radius
*
* @example
* effect.disabled = true;
* effect.radius = 0.253;
*/
return {
fragment: {
uniform: {
u_deformationEnabled: 'bool',
u_radius: 'float',
},
source: `
float _aspectRatio = u_resolution.x / u_resolution.y;
vec2 _position = u_mouse;
vec2 diff = sourceCoord - _position;
float dist = diff.x * diff.x * _aspectRatio * _aspectRatio + diff.y * diff.y;
float r = sqrt(dist);
bool isInsideDeformation = dist < u_radius * u_radius;
if (u_deformationEnabled) {
if (isInsideDeformation) {
vec2 dispVec = diff;
float a = atan(diff.y, diff.x);
${deformation}
dispVec = dispVec + _position;
${wrap}
sourceCoord = dispVec;
}
}`,
// main: `
// if (isInsideDeformation) {
// color = mix(color, texture2D(u_source, v_texCoord).rgb, vec3(pow(r / u_radius, 4.0)));
// }`,
},
get disabled() {
return !this.uniforms[0].data[0];
},
set disabled(b) {
this.uniforms[0].data[0] = +!b;
},
get radius() {
return this.uniforms[1].data[0];
},
set radius(r) {
if (typeof r !== 'undefined') this.uniforms[1].data[0] = r;
},
uniforms: [
{
name: 'u_deformationEnabled',
type: 'i',
data: [1],
},
{
name: 'u_radius',
type: 'f',
data: [dataRadius],
},
],
};
}
const WRAP_METHODS$1 = {
CLAMP: `dispVec = clamp(dispVec, 0.0, 1.0);`,
DISCARD: `if (dispVec.x < 0.0 || dispVec.x > 1.0 || dispVec.y > 1.0 || dispVec.y < 0.0) { discard; }`,
WRAP: `dispVec = mod(dispVec, 1.0);`,
};
deformation.CLAMP = WRAP_METHODS$1.CLAMP;
deformation.DISCARD = WRAP_METHODS$1.DISCARD;
deformation.WRAP = WRAP_METHODS$1.WRAP;
const DEFORMATION_METHODS = {
NONE: ``,
TUNNEL: `dispVec = vec2(dispVec.x * cos(r + r) - dispVec.y * sin(r + r), dispVec.y * cos(r + r) + dispVec.x * sin(r + r));`,
SOMETHING: `dispVec = vec2(0.3 / (10.0 * r + dispVec.x), 0.5 * a / PI);`,
SOMETHING2: `dispVec = vec2(0.02 * dispVec.y + 0.03 * cos(a) / r, 0.02 * dispVec.x + 0.03 * sin(a) / r);`,
INVERT: `dispVec = dispVec * -1.0;`,
SCALE: `dispVec = dispVec * 0.75;`,
MAGNIFY: `dispVec = dispVec * (pow(2.0, r / u_radius) - 1.0);`,
UNMAGNIFY: `dispVec = dispVec * (pow(2.0, min(u_radius / r, 4.0)));`,
};
deformation.NONE = DEFORMATION_METHODS.NONE;
deformation.TUNNEL = DEFORMATION_METHODS.TUNNEL;
deformation.SOMETHING = DEFORMATION_METHODS.SOMETHING;
deformation.SOMETHING2 = DEFORMATION_METHODS.SOMETHING2;
deformation.INVERT = DEFORMATION_METHODS.INVERT;
deformation.SCALE = DEFORMATION_METHODS.SCALE;
deformation.MAGNIFY = DEFORMATION_METHODS.MAGNIFY;
deformation.UNMAGNIFY = DEFORMATION_METHODS.UNMAGNIFY;
const MODES_AUX = {
blend_luminosity: `float blend_luminosity (vec3 c) {
return dot(c, blendLum);
}`,
blend_saturation: `float blend_saturation (vec3 c) {
return max(max(c.r, c.g), c.b) - min(min(c.r, c.g), c.b);
}`,
blend_set_luminosity: `vec3 blend_clip_color (vec3 c) {
float l = blend_luminosity(c);
float cMin = min(min(c.r, c.g), c.b);
float cMax = max(max(c.r, c.g), c.b);
if (cMin < 0.0)
return l + (((c - l) * l) / (l - cMin));
if (cMax > 1.0)
return l + (((c - l) * (1.0 - l)) / (cMax - l));
return c;
}
vec3 blend_set_luminosity (vec3 c, float l) {
vec3 delta = vec3(l - blend_luminosity(c));
return blend_clip_color(vec3(c.rgb + delta.rgb));
}`,
blend_set_saturation: `
float getBlendMid (vec3 c) {
float bigger = max(c.r, c.g);
if (bigger < c.b) {
return bigger;
}
float smaller = min(c.r, c.g);
if (c.b < smaller) {
return smaller;
}
return c.b;
}
vec3 blend_set_saturation (vec3 c, float s) {
if (s == 0.0) return vec3(0.0);
float cMax = max(max(c.r, c.g), c.b);
float cMid = getBlendMid(c);
float cMin = min(min(c.r, c.g), c.b);
float r, g, b;
cMid = (((cMid - cMin) * s) / (cMax - cMin));
cMax = s;
cMin = 0.0;
if (c.r > c.g) {
// r > g
if (c.b > c.r) {
// g < r < b
g = cMin;
r = cMid;
b = cMax;
}
else if (c.g > c.b) {
// b < g < r
b = cMin;
g = cMid;
r = cMax;
}
else {
// g < b < r
g = cMin;
b = cMid;
r = cMax;
}
}
// g > r
else if (c.g > c.b) {
// g > b
if (c.b > c.r) {
// r < b < g
r = cMin;
b = cMid;
g = cMax;
}
else {
// b < r < g
b = cMin;
r = cMid;
g = cMax;
}
}
else {
// r < g < b
r = cMin;
g = cMid;
b = cMax;
}
return vec3(r, g, b);
}`,
};
const MODES_CONSTANT = {
normal: '',
multiply: '',
screen: '',
overlay: `float blend_overlay (float b, float c) {
if (b <= 0.5)
return 2.0 * b * c;
else
return 1.0 - 2.0 * ((1.0 - b) * (1.0 - c));
}`,
darken: '',
lighten: '',
colorDodge: `float blend_colorDodge (float b, float c) {
if (b == 0.0)
return 0.0;
else if (c == 1.0)
return 1.0;
else
return min(1.0, b / (1.0 - c));
}`,
colorBurn: `float blend_colorBurn (float b, float c) {
if (b == 1.0) {
return 1.0;
}
else if (c == 0.0) {
return 0.0;
}
else {
return 1.0 - min(1.0, (1.0 - b) / c);
}
}`,
hardLight: `float blend_hardLight (float b, float c) {
if (c <= 0.5) {
return 2.0 * b * c;
}
else {
return 1.0 - 2.0 * ((1.0 - b) * (1.0 - c));
}
}`,
softLight: `float blend_softLight (float b, float c) {
if (c <= 0.5) {
return b - (1.0 - 2.0 * c) * b * (1.0 - b);
}
else {
float d;
if (b <= 0.25) {
d = ((16.0 * b - 12.0) * b + 4.0) * b;
}
else {
d = sqrt(b);
}
return b + (2.0 * c - 1.0) * (d - b);
}
}`,
difference: `float blend_difference (float b, float c) {
return abs(b - c);
}`,
exclusion: `float blend_exclusion (float b, float c) {
return b + c - 2.0 * b * c;
}`,
hue: `${MODES_AUX.blend_luminosity}
${MODES_AUX.blend_saturation}
${MODES_AUX.blend_set_saturation}
${MODES_AUX.blend_set_luminosity}`,
saturation: `${MODES_AUX.blend_luminosity}
${MODES_AUX.blend_saturation}
${MODES_AUX.blend_set_saturation}
${MODES_AUX.blend_set_luminosity}`,
color: `${MODES_AUX.blend_luminosity}
${MODES_AUX.blend_set_luminosity}`,
luminosity: `${MODES_AUX.blend_luminosity}
${MODES_AUX.blend_set_luminosity}`,
};
function generateBlendVector(name) {
return `vec3(${name}(backdrop.r, source.r), ${name}(backdrop.g, source.g), ${name}(backdrop.b, source.b))`;
}
const MODES_MAIN = {
normal: 'source',
multiply: 'source * backdrop',
screen: 'backdrop + source - backdrop * source',
overlay: generateBlendVector('blend_overlay'),
darken: generateBlendVector('min'),
lighten: generateBlendVector('max'),
colorDodge: generateBlendVector('blend_colorDodge'),
colorBurn: generateBlendVector('blend_colorBurn'),
hardLight: generateBlendVector('blend_hardLight'),
softLight: generateBlendVector('blend_softLight'),
difference: generateBlendVector('blend_difference'),
exclusion: generateBlendVector('blend_exclusion'),
hue: 'blend_set_luminosity(blend_set_saturation(source, blend_saturation(backdrop)), blend_luminosity(backdrop))',
saturation:
'blend_set_luminosity(blend_set_saturation(backdrop, blend_saturation(source)), blend_luminosity(backdrop))',
color: 'blend_set_luminosity(source, blend_luminosity(backdrop))',
luminosity: 'blend_set_luminosity(backdrop, blend_luminosity(source))',
};
/**
* @function blend
* @param {Object} [params]
* @param {'normal'|'multiply'|'screen'|'overlay'|'darken'|'lighten'|'color-dodge'|'color-burn'|'hard-light'|'soft-light'|'difference'|'exclusion'|'hue'|'saturation'|'color'|'luminosity'} [params.mode='normal'] blend mode to use
* @param {number[]} [params.color=[0, 0, 0, 1]] Initial color to use when blending to a solid color
* @returns {blendEffect}
* @example blend('colorBurn')
*/
function blend ({
mode = 'normal',
color = [0.0, 0.0, 0.0, 1.0],
} = {}) {
/**
* @typedef {Object} blendEffect
* @property {number[]} color backdrop solid color as Array of 4 numbers, normalized (0.0 - 1.0)
* @property {ArrayBufferView|ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|ImageBitmap} image to use as backdrop
* @property {boolean} disabled
*
* @example
* const img = new Image();
* img.src = 'picture.png';
* effect.color = [0.3, 0.55, 0.8, 1.0];
* effect.image = img;
*/
return {
vertex: {
attribute: {
a_blendImageTexCoord: 'vec2',
},
main: `
v_blendImageTexCoord = a_blendImageTexCoord;`,
},
fragment: {
uniform: {
u_blendEnabled: 'bool',
u_blendColorEnabled: 'bool',
u_blendImageEnabled: 'bool',
u_blendColor: 'vec4',
u_blendImage: 'sampler2D',
},
constant: `const vec3 blendLum = vec3(0.3, 0.59, 0.11);
${MODES_CONSTANT[mode]}`,
main: `
if (u_blendEnabled) {
vec3 backdrop = vec3(0.0);
float backdropAlpha = 1.0;
if (u_blendColorEnabled) {
backdrop = u_blendColor.rgb;
backdropAlpha = u_blendColor.a;
}
if (u_blendImageEnabled) {
vec4 blendBackdropPixel = texture2D(u_blendImage, v_blendImageTexCoord);
if (u_blendColorEnabled) {
vec3 source = blendBackdropPixel.rgb;
float sourceAlpha = blendBackdropPixel.a;
backdrop = (1.0 - backdropAlpha) * source + backdropAlpha * clamp(${MODES_MAIN[mode]}, 0.0, 1.0);
backdropAlpha = sourceAlpha + backdropAlpha * (1.0 - sourceAlpha);
}
else {
backdrop = blendBackdropPixel.rgb;
backdropAlpha = blendBackdropPixel.a;
}
}
vec3 source = vec3(color.rgb);
color = (1.0 - backdropAlpha) * source + backdropAlpha * clamp(${MODES_MAIN[mode]}, 0.0, 1.0);
alpha = alpha + backdropAlpha * (1.0 - alpha);
}`,
},
get color() {
return this.uniforms[1].data.slice(0);
},
set color(l) {
if (!l || !l.length) {
this.uniforms[2].data[0] = 0;
} else {
this.uniforms[2].data[0] = 1;
l.forEach((c, i) => {
if (!Number.isNaN(c)) {
this.uniforms[1].data[i] = c;
}
});
}
},
get image() {
return this.textures[0].data;
},
set image(img) {
if (img) {
this.uniforms[4].data[0] = 1;
this.textures[0].data = img;
} else {
this.uniforms[4].data[0] = 0;
}
},
get disabled() {
return !this.uniforms[0].data[0];
},
set disabled(b) {
this.uniforms[0].data[0] = +!b;
},
varying: {
v_blendImageTexCoord: 'vec2',
},
uniforms: [
{
name: 'u_blendEnabled',
type: 'i',
data: [1],
},
{
name: 'u_blendColor',
type: 'f',
data: color,
},
{
name: 'u_blendColorEnabled',
type: 'i',
data: [1],
},
{
name: 'u_blendImage',
type: 'i',
data: [1],
},
{
name: 'u_blendImageEnabled',
type: 'i',
data: [0],
},
],
attributes: [
{
name: 'a_blendImageTexCoord',
extends: 'a_texCoord',
},
],
textures: [
{
format: 'RGBA',
},
],
};
}
/**
* @function brightnessContrast
* @property {number} brightness
* @property {number} contrast
* @param {Object} [params]
* @param {number} [params.brightness=1.0] initial brightness to use.
* @param {number} [params.contrast=1.0] initial contrast to use.
* @returns {brightnessContrastEffect}
*
* @example brightnessContrast({brightness: 1.5, contrast: 0.8})
*/
function brightnessContrast ({ brightness = 1.0, contrast = 1.0 } = {}) {
/**
* @typedef {Object} brightnessContrastEffect
* @property {number} brightness
* @property {number} contrast
* @property {boolean} brightnessDisabled
* @property {boolean} contrastDisabled
*
* @example
* effect.brightness = 1.5;
* effect.contrast = 0.9;
* effect.contrastDisabled = true;
*/
return {
fragment: {
uniform: {
u_brEnabled: 'bool',
u_ctEnabled: 'bool',
u_contrast: 'float',
u_brightness: 'float',
},
constant: 'const vec3 half3 = vec3(0.5);',
main: `
if (u_brEnabled) {
color *= u_brightness;
}
if (u_ctEnabled) {
color = (color - half3) * u_contrast + half3;
}
color = clamp(color, 0.0, 1.0);`,
},
get brightness() {
return this.uniforms[2].data[0];
},
set brightness(value) {
this.uniforms[2].data[0] = parseFloat(Math.max(0, value));
},
get contrast() {
return this.uniforms[3].data[0];
},
set contrast(value) {
this.uniforms[3].data[0] = parseFloat(Math.max(0, value));
},
get brightnessDisabled() {
return !this.uniforms[0].data[0];
},
set brightnessDisabled(toggle) {
this.uniforms[0].data[0] = +!toggle;
},
get contrastDisabled() {
return !this.uniforms[1].data[0];
},
set contrastDisabled(toggle) {
this.uniforms[1].data[0] = +!toggle;
},
uniforms: [
{
name: 'u_brEnabled',
type: 'i',
data: [1],
},
{
name: 'u_ctEnabled',
type: 'i',
data: [1],
},
{
name: 'u_brightness',
type: 'f',
data: [brightness],
},
{
name: 'u_contrast',
type: 'f',
data: [contrast],
},
],
};
}
/**
* @function hueSaturation
* @property {number} hue rotation in degrees
* @property {number} saturation
* @param {Object} [params]
* @param {number} [params.hue=0.0] initial hue value
* @param {number} [params.saturation=1.0] initial saturation value
* @returns {hueSaturationEffect}
* @example hueSaturation({hue: 45, saturation: 1.3})
*/
function hueSaturation ({ hue = 0.0, saturation = 1.0 } = {}) {
/**
* @typedef {Object} hueSaturationEffect
* @property {number} hue
* @property {number} saturation
* @property {boolean} hueDisabled
* @property {boolean} saturationDisabled
*
* @example
* effect.hue = 45;
* effect.saturation = 0.8;
*/
return {
vertex: {
uniform: {
u_hue: 'float',
u_saturation: 'float',
},
// for implementation see: https://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement
constant: `
const mat3 lummat = mat3(
lumcoeff,
lumcoeff,
lumcoeff
);
const mat3 cosmat = mat3(
vec3(0.787, -0.715, -0.072),
vec3(-0.213, 0.285, -0.072),
vec3(-0.213, -0.715, 0.928)
);
const mat3 sinmat = mat3(
vec3(-0.213, -0.715, 0.928),
vec3(0.143, 0.140, -0.283),
vec3(-0.787, 0.715, 0.072)
);
const mat3 satmat = mat3(
vec3(0.787, -0.715, -0.072),
vec3(-0.213, 0.285, -0.072),
vec3(-0.213, -0.715, 0.928)
);`,
main: `
float angle = (u_hue / 180.0) * 3.14159265358979323846264;
v_hueRotation = lummat + cos(angle) * cosmat + sin(angle) * sinmat;
v_saturation = lummat + satmat * u_saturation;`,
},
fragment: {
uniform: {
u_hueEnabled: 'bool',
u_satEnabled: 'bool',
u_hue: 'float',
u_saturation: 'float',
},
main: `
if (u_hueEnabled) {
color = vec3(
dot(color, v_hueRotation[0]),
dot(color, v_hueRotation[1]),
dot(color, v_hueRotation[2])
);
}
if (u_satEnabled) {
color = vec3(
dot(color, v_saturation[0]),
dot(color, v_saturation[1]),
dot(color, v_saturation[2])
);
}
color = clamp(color, 0.0, 1.0);`,
},
varying: {
v_hueRotation: 'mat3',
v_saturation: 'mat3',
},
get hue() {
return this.uniforms[2].data[0];
},
set hue(h) {
this.uniforms[2].data[0] = parseFloat(h);
},
get saturation() {
return this.uniforms[3].data[0];
},
set saturation(s) {
this.uniforms[3].data[0] = parseFloat(Math.max(0, s));
},
get hueDisabled() {
return !this.uniforms[0].data[0];
},
set hueDisabled(b) {
this.uniforms[0].data[0] = +!b;
},
get saturationDisabled() {
return !this.uniforms[1].data[0];
},
set saturationDisabled(b) {
this.uniforms[1].data[0] = +!b;
},
uniforms: [
{
name: 'u_hueEnabled',
type: 'i',
data: [1],
},
{
name: 'u_satEnabled',
type: 'i',
data: [1],
},
{
name: 'u_hue',
type: 'f',
data: [hue],
},
{
name: 'u_saturation',
type: 'f',
data: [saturation],
},
],
};
}
/**
* @function duotone
* @param {Object} [params]
* @param {number[]} [params.dark=[0.741, 0.0431, 0.568, 1]] initial dark color to use.
* @param {number[]} [params.light=[0.988, 0.733, 0.051, 1]] initial light color to use.
* @returns {duotoneEffect}
*
* @example duotone({dark: [0.2, 0.11, 0.33, 1], light: [0.88, 0.78, 0.43, 1]})
*/
function duotone ({
dark = [0.7411764706, 0.0431372549, 0.568627451, 1],
light = [0.9882352941, 0.7333333333, 0.05098039216, 1],
} = {}) {
/**
* @typedef {Object} duotoneEffect
* @property {number[]} light Array of 4 numbers, normalized (0.0 - 1.0)
* @property {number[]} dark Array of 4 numbers, normalized (0.0 - 1.0)
* @property {boolean} disabled
*
* @example
* effect.light = [1.0, 1.0, 0.8];
* effect.dark = [0.2, 0.6, 0.33];
*/
return {
fragment: {
uniform: {
u_duotoneEnabled: 'bool',
u_light: 'vec4',
u_dark: 'vec4',
},
main: `
if (u_duotoneEnabled) {
vec3 gray = vec3(dot(lumcoeff, color));
color = mix(u_dark.rgb, u_light.rgb, gray);
}`,
},
get light() {
return this.uniforms[1].data.slice(0);
},
set light(l) {
l.forEach((c, i) => {
if (!Number.isNaN(c)) {
this.uniforms[1].data[i] = c;
}
});
},
get dark() {
return this.uniforms[2].data.slice(0);
},
set dark(d) {
d.forEach((c, i) => {
if (!Number.isNaN(c)) {
this.uniforms[2].data[i] = c;
}
});
},
get disabled() {
return !this.uniforms[0].data[0];
},
set disabled(b) {
this.uniforms[0].data[0] = +!b;
},
uniforms: [
{
name: 'u_duotoneEnabled',
type: 'i',
data: [1],
},
{
name: 'u_light',
type: 'f',
data: light,
},
{
name: 'u_dark',
type: 'f',
data: dark,
},