forked from RyoleBg/PE-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaders.hx
1339 lines (1006 loc) · 35.5 KB
/
Shaders.hx
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
package;
// STOLEN FROM HAXEFLIXEL DEMO AND FROM PSYCH ENGINE 0.5.1 WITH SHADERS LOL
import flixel.system.FlxAssets.FlxShader;
import openfl.display.BitmapData;
import openfl.display.Shader;
import openfl.display.ShaderInput;
import openfl.utils.Assets;
import flixel.FlxG;
import openfl.Lib;
using StringTools;
typedef ShaderEffect = {
var shader:Dynamic;
}
class BuildingEffect {
public var shader:BuildingShader = new BuildingShader();
public function new(){
shader.alphaShit.value = [0];
}
public function addAlpha(alpha:Float){
trace(shader.alphaShit.value[0]);
shader.alphaShit.value[0]+=alpha;
}
public function setAlpha(alpha:Float){
shader.alphaShit.value[0]=alpha;
}
}
class BuildingShader extends FlxShader
{
@:glFragmentSource('
#pragma header
uniform float alphaShit;
void main()
{
vec4 color = flixel_texture2D(bitmap,openfl_TextureCoordv);
if (color.a > 0.0)
color-=alphaShit;
gl_FragColor = color;
}
')
public function new()
{
super();
}
}
class ChromaticAberrationShader extends FlxShader
{
@:glFragmentSource('
#pragma header
uniform float rOffset;
uniform float gOffset;
uniform float bOffset;
void main()
{
vec4 col1 = texture2D(bitmap, openfl_TextureCoordv.st - vec2(rOffset, 0.0));
vec4 col2 = texture2D(bitmap, openfl_TextureCoordv.st - vec2(gOffset, 0.0));
vec4 col3 = texture2D(bitmap, openfl_TextureCoordv.st - vec2(bOffset, 0.0));
vec4 toUse = texture2D(bitmap, openfl_TextureCoordv);
toUse.r = col1.r;
toUse.g = col2.g;
toUse.b = col3.b;
//float someshit = col4.r + col4.g + col4.b;
gl_FragColor = toUse;
}')
public function new()
{
super();
}
}
class ChromaticAberrationEffect extends Effect
{
public var shader:ChromaticAberrationShader;
public function new(offset:Float = 0.00){
shader = new ChromaticAberrationShader();
shader.rOffset.value = [offset];
shader.gOffset.value = [0.0];
shader.bOffset.value = [-offset];
}
public function setChrome(chromeOffset:Float):Void
{
shader.rOffset.value = [chromeOffset];
shader.gOffset.value = [0.0];
shader.bOffset.value = [chromeOffset * -1];
}
}
class ScanlineEffect extends Effect
{
public var shader:Scanline;
public function new (lockAlpha){
shader = new Scanline();
shader.lockAlpha.value = [lockAlpha];
}
}
class Scanline extends FlxShader
{
@:glFragmentSource('
#pragma header
const float scale = 1.0;
uniform bool lockAlpha = false;
void main()
{
if (mod(floor(openfl_TextureCoordv.y * openfl_TextureSize.y / scale), 2.0) == 0.0 ){
float bitch = 1.0;
vec4 texColor = texture2D(bitmap, openfl_TextureCoordv);
if (lockAlpha) bitch = texColor.a;
gl_FragColor = vec4(0.0, 0.0, 0.0, bitch);
}else{
gl_FragColor = texture2D(bitmap, openfl_TextureCoordv);
}
}')
public function new()
{
super();
}
}
class TiltshiftEffect extends Effect{
public var shader:Tiltshift;
public function new (blurAmount:Float, center:Float){
shader = new Tiltshift();
shader.bluramount.value = [blurAmount];
shader.center.value = [center];
}
}
class Tiltshift extends FlxShader
{
@:glFragmentSource('
#pragma header
// Modified version of a tilt shift shader from Martin Jonasson (http://grapefrukt.com/)
// Read http://notes.underscorediscovery.com/ for context on shaders and this file
// License : MIT
/*
Take note that blurring in a single pass (the two for loops below) is more expensive than separating
the x and the y blur into different passes. This was used where bleeding edge performance
was not crucial and is to illustrate a point.
The reason two passes is cheaper?
texture2D is a fairly high cost call, sampling a texture.
So, in a single pass, like below, there are 3 steps, per x and y.
That means a total of 9 "taps", it touches the texture to sample 9 times.
Now imagine we apply this to some geometry, that is equal to 16 pixels on screen (tiny)
(16 * 16) * 9 = 2304 samples taken, for width * height number of pixels, * 9 taps
Now, if you split them up, it becomes 3 for x, and 3 for y, a total of 6 taps
(16 * 16) * 6 = 1536 samples
That\'s on a *tiny* sprite, let\'s scale that up to 128x128 sprite...
(128 * 128) * 9 = 147,456
(128 * 128) * 6 = 98,304
That\'s 33.33..% cheaper for splitting them up.
That\'s with 3 steps, with higher steps (more taps per pass...)
A really smooth, 6 steps, 6*6 = 36 taps for one pass, 12 taps for two pass
You will notice, the curve is not linear, at 12 steps it\'s 144 vs 24 taps
It becomes orders of magnitude slower to do single pass!
Therefore, you split them up into two passes, one for x, one for y.
*/
// I am hardcoding the constants like a jerk
uniform float bluramount = 1.0;
uniform float center = 1.0;
const float stepSize = 0.004;
const float steps = 3.0;
const float minOffs = (float(steps-1.0)) / -2.0;
const float maxOffs = (float(steps-1.0)) / +2.0;
void main() {
float amount;
vec4 blurred;
// Work out how much to blur based on the mid point
amount = pow((openfl_TextureCoordv.y * center) * 2.0 - 1.0, 2.0) * bluramount;
// This is the accumulation of color from the surrounding pixels in the texture
blurred = vec4(0.0, 0.0, 0.0, 1.0);
// From minimum offset to maximum offset
for (float offsX = minOffs; offsX <= maxOffs; ++offsX) {
for (float offsY = minOffs; offsY <= maxOffs; ++offsY) {
// copy the coord so we can mess with it
vec2 temp_tcoord = openfl_TextureCoordv.xy;
//work out which uv we want to sample now
temp_tcoord.x += offsX * amount * stepSize;
temp_tcoord.y += offsY * amount * stepSize;
// accumulate the sample
blurred += texture2D(bitmap, temp_tcoord);
}
}
// because we are doing an average, we divide by the amount (x AND y, hence steps * steps)
blurred /= float(steps * steps);
// return the final blurred color
gl_FragColor = blurred;
}')
public function new()
{
super();
}
}
class GreyscaleEffect extends Effect{
public var shader:GreyscaleShader = new GreyscaleShader();
public function new(){
}
}
class GreyscaleShader extends FlxShader{
@:glFragmentSource('
#pragma header
void main() {
vec4 color = texture2D(bitmap, openfl_TextureCoordv);
float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4(vec3(gray), color.a);
}
')
public function new(){
super();
}
}
class GrainEffect extends Effect {
public var shader:Grain;
public function new (grainsize, lumamount,lockAlpha){
shader = new Grain();
shader.lumamount.value = [lumamount];
shader.grainsize.value = [grainsize];
shader.lockAlpha.value = [lockAlpha];
shader.uTime.value = [FlxG.random.float(0,8)];
PlayState.instance.shaderUpdates.push(update);
}
public function update(elapsed){
shader.uTime.value[0] += elapsed;
}
}
class Grain extends FlxShader
{
@:glFragmentSource('
#pragma header
/*
Film Grain post-process shader v1.1
Martins Upitis (martinsh) devlog-martinsh.blogspot.com
2013
--------------------------
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
So you are free to share, modify and adapt it for your needs, and even use it for commercial use.
I would also love to hear about a project you are using it.
Have fun,
Martins
--------------------------
Perlin noise shader by toneburst:
http://machinesdontcare.wordpress.com/2009/06/25/3d-perlin-noise-sphere-vertex-shader-sourcecode/
*/
uniform float uTime;
const float permTexUnit = 1.0/256.0; // Perm texture texel-size
const float permTexUnitHalf = 0.5/256.0; // Half perm texture texel-size
float width = openfl_TextureSize.x;
float height = openfl_TextureSize.y;
const float grainamount = 0.05; //grain amount
bool colored = false; //colored noise?
uniform float coloramount = 0.6;
uniform float grainsize = 1.6; //grain particle size (1.5 - 2.5)
uniform float lumamount = 1.0; //
uniform bool lockAlpha = false;
//a random texture generator, but you can also use a pre-computed perturbation texture
vec4 rnm(in vec2 tc)
{
float noise = sin(dot(tc + vec2(uTime,uTime),vec2(12.9898,78.233))) * 43758.5453;
float noiseR = fract(noise)*2.0-1.0;
float noiseG = fract(noise*1.2154)*2.0-1.0;
float noiseB = fract(noise * 1.3453) * 2.0 - 1.0;
float noiseA = (fract(noise * 1.3647) * 2.0 - 1.0);
return vec4(noiseR,noiseG,noiseB,noiseA);
}
float fade(in float t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
float pnoise3D(in vec3 p)
{
vec3 pi = permTexUnit*floor(p)+permTexUnitHalf; // Integer part, scaled so +1 moves permTexUnit texel
// and offset 1/2 texel to sample texel centers
vec3 pf = fract(p); // Fractional part for interpolation
// Noise contributions from (x=0, y=0), z=0 and z=1
float perm00 = rnm(pi.xy).a ;
vec3 grad000 = rnm(vec2(perm00, pi.z)).rgb * 4.0 - 1.0;
float n000 = dot(grad000, pf);
vec3 grad001 = rnm(vec2(perm00, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
float n001 = dot(grad001, pf - vec3(0.0, 0.0, 1.0));
// Noise contributions from (x=0, y=1), z=0 and z=1
float perm01 = rnm(pi.xy + vec2(0.0, permTexUnit)).a ;
vec3 grad010 = rnm(vec2(perm01, pi.z)).rgb * 4.0 - 1.0;
float n010 = dot(grad010, pf - vec3(0.0, 1.0, 0.0));
vec3 grad011 = rnm(vec2(perm01, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
float n011 = dot(grad011, pf - vec3(0.0, 1.0, 1.0));
// Noise contributions from (x=1, y=0), z=0 and z=1
float perm10 = rnm(pi.xy + vec2(permTexUnit, 0.0)).a ;
vec3 grad100 = rnm(vec2(perm10, pi.z)).rgb * 4.0 - 1.0;
float n100 = dot(grad100, pf - vec3(1.0, 0.0, 0.0));
vec3 grad101 = rnm(vec2(perm10, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
float n101 = dot(grad101, pf - vec3(1.0, 0.0, 1.0));
// Noise contributions from (x=1, y=1), z=0 and z=1
float perm11 = rnm(pi.xy + vec2(permTexUnit, permTexUnit)).a ;
vec3 grad110 = rnm(vec2(perm11, pi.z)).rgb * 4.0 - 1.0;
float n110 = dot(grad110, pf - vec3(1.0, 1.0, 0.0));
vec3 grad111 = rnm(vec2(perm11, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
float n111 = dot(grad111, pf - vec3(1.0, 1.0, 1.0));
// Blend contributions along x
vec4 n_x = mix(vec4(n000, n001, n010, n011), vec4(n100, n101, n110, n111), fade(pf.x));
// Blend contributions along y
vec2 n_xy = mix(n_x.xy, n_x.zw, fade(pf.y));
// Blend contributions along z
float n_xyz = mix(n_xy.x, n_xy.y, fade(pf.z));
// We are done, return the final noise value.
return n_xyz;
}
//2d coordinate orientation thing
vec2 coordRot(in vec2 tc, in float angle)
{
float aspect = width/height;
float rotX = ((tc.x*2.0-1.0)*aspect*cos(angle)) - ((tc.y*2.0-1.0)*sin(angle));
float rotY = ((tc.y*2.0-1.0)*cos(angle)) + ((tc.x*2.0-1.0)*aspect*sin(angle));
rotX = ((rotX/aspect)*0.5+0.5);
rotY = rotY*0.5+0.5;
return vec2(rotX,rotY);
}
void main()
{
vec2 texCoord = openfl_TextureCoordv.st;
vec3 rotOffset = vec3(1.425,3.892,5.835); //rotation offset values
vec2 rotCoordsR = coordRot(texCoord, uTime + rotOffset.x);
vec3 noise = vec3(pnoise3D(vec3(rotCoordsR*vec2(width/grainsize,height/grainsize),0.0)));
if (colored)
{
vec2 rotCoordsG = coordRot(texCoord, uTime + rotOffset.y);
vec2 rotCoordsB = coordRot(texCoord, uTime + rotOffset.z);
noise.g = mix(noise.r,pnoise3D(vec3(rotCoordsG*vec2(width/grainsize,height/grainsize),1.0)),coloramount);
noise.b = mix(noise.r,pnoise3D(vec3(rotCoordsB*vec2(width/grainsize,height/grainsize),2.0)),coloramount);
}
vec3 col = texture2D(bitmap, openfl_TextureCoordv).rgb;
//noisiness response curve based on scene luminance
vec3 lumcoeff = vec3(0.299,0.587,0.114);
float luminance = mix(0.0,dot(col, lumcoeff),lumamount);
float lum = smoothstep(0.2,0.0,luminance);
lum += luminance;
noise = mix(noise,vec3(0.0),pow(lum,4.0));
col = col+noise*grainamount;
float bitch = 1.0;
vec4 texColor = texture2D(bitmap, openfl_TextureCoordv);
if (lockAlpha) bitch = texColor.a;
gl_FragColor = vec4(col,bitch);
}')
public function new()
{
super();
}
}
class VCRDistortionEffect extends Effect
{
public var shader:VCRDistortionShader = new VCRDistortionShader();
public function new(glitchFactor:Float,distortion:Bool=true,perspectiveOn:Bool=true,vignetteMoving:Bool=true){
shader.iTime.value = [0];
shader.vignetteOn.value = [true];
shader.perspectiveOn.value = [perspectiveOn];
shader.distortionOn.value = [distortion];
shader.scanlinesOn.value = [true];
shader.vignetteMoving.value = [vignetteMoving];
shader.glitchModifier.value = [glitchFactor];
shader.iResolution.value = [Lib.current.stage.stageWidth,Lib.current.stage.stageHeight];
// var noise = Assets.getBitmapData(Paths.image("noise2"));
// shader.noiseTex.input = noise;
PlayState.instance.shaderUpdates.push(update);
}
public function update(elapsed:Float){
shader.iTime.value[0] += elapsed;
shader.iResolution.value = [Lib.current.stage.stageWidth,Lib.current.stage.stageHeight];
}
public function setVignette(state:Bool){
shader.vignetteOn.value[0] = state;
}
public function setPerspective(state:Bool){
shader.perspectiveOn.value[0] = state;
}
public function setGlitchModifier(modifier:Float){
shader.glitchModifier.value[0] = modifier;
}
public function setDistortion(state:Bool){
shader.distortionOn.value[0] = state;
}
public function setScanlines(state:Bool){
shader.scanlinesOn.value[0] = state;
}
public function setVignetteMoving(state:Bool){
shader.vignetteMoving.value[0] = state;
}
}
class VCRDistortionShader extends FlxShader // https://www.shadertoy.com/view/ldjGzV and https://www.shadertoy.com/view/Ms23DR and https://www.shadertoy.com/view/MsXGD4 and https://www.shadertoy.com/view/Xtccz4
{
@:glFragmentSource('
#pragma header
uniform float iTime;
uniform bool vignetteOn;
uniform bool perspectiveOn;
uniform bool distortionOn;
uniform bool scanlinesOn;
uniform bool vignetteMoving;
// uniform sampler2D noiseTex;
uniform float glitchModifier;
uniform vec3 iResolution;
float onOff(float a, float b, float c)
{
return step(c, sin(iTime + a*cos(iTime*b)));
}
float ramp(float y, float start, float end)
{
float inside = step(start,y) - step(end,y);
float fact = (y-start)/(end-start)*inside;
return (1.-fact) * inside;
}
vec4 getVideo(vec2 uv)
{
vec2 look = uv;
if(distortionOn){
float window = 1./(1.+20.*(look.y-mod(iTime/4.,1.))*(look.y-mod(iTime/4.,1.)));
look.x = look.x + (sin(look.y*10. + iTime)/50.*onOff(4.,4.,.3)*(1.+cos(iTime*80.))*window)*(glitchModifier*2);
float vShift = 0.4*onOff(2.,3.,.9)*(sin(iTime)*sin(iTime*20.) +
(0.5 + 0.1*sin(iTime*200.)*cos(iTime)));
look.y = mod(look.y + vShift*glitchModifier, 1.);
}
vec4 video = flixel_texture2D(bitmap,look);
return video;
}
vec2 screenDistort(vec2 uv)
{
if(perspectiveOn){
uv = (uv - 0.5) * 2.0;
uv *= 1.1;
uv.x *= 1.0 + pow((abs(uv.y) / 5.0), 2.0);
uv.y *= 1.0 + pow((abs(uv.x) / 4.0), 2.0);
uv = (uv / 2.0) + 0.5;
uv = uv *0.92 + 0.04;
return uv;
}
return uv;
}
float random(vec2 uv)
{
return fract(sin(dot(uv, vec2(15.5151, 42.2561))) * 12341.14122 * sin(iTime * 0.03));
}
float noise(vec2 uv)
{
vec2 i = floor(uv);
vec2 f = fract(uv);
float a = random(i);
float b = random(i + vec2(1.,0.));
float c = random(i + vec2(0., 1.));
float d = random(i + vec2(1.));
vec2 u = smoothstep(0., 1., f);
return mix(a,b, u.x) + (c - a) * u.y * (1. - u.x) + (d - b) * u.x * u.y;
}
vec2 scandistort(vec2 uv) {
float scan1 = clamp(cos(uv.y * 2.0 + iTime), 0.0, 1.0);
float scan2 = clamp(cos(uv.y * 2.0 + iTime + 4.0) * 10.0, 0.0, 1.0) ;
float amount = scan1 * scan2 * uv.x;
//uv.x -= 0.05 * mix(flixel_texture2D(noiseTex, vec2(uv.x, amount)).r * amount, amount, 0.9);
return uv;
}
void main()
{
vec2 uv = openfl_TextureCoordv;
vec2 curUV = screenDistort(uv);
uv = scandistort(curUV);
vec4 video = getVideo(uv);
float vigAmt = 1.0;
float x = 0.;
video.r = getVideo(vec2(x+uv.x+0.001,uv.y+0.001)).x+0.05;
video.g = getVideo(vec2(x+uv.x+0.000,uv.y-0.002)).y+0.05;
video.b = getVideo(vec2(x+uv.x-0.002,uv.y+0.000)).z+0.05;
video.r += 0.08*getVideo(0.75*vec2(x+0.025, -0.027)+vec2(uv.x+0.001,uv.y+0.001)).x;
video.g += 0.05*getVideo(0.75*vec2(x+-0.022, -0.02)+vec2(uv.x+0.000,uv.y-0.002)).y;
video.b += 0.08*getVideo(0.75*vec2(x+-0.02, -0.018)+vec2(uv.x-0.002,uv.y+0.000)).z;
video = clamp(video*0.6+0.4*video*video*1.0,0.0,1.0);
if(vignetteMoving)
vigAmt = 3.+.3*sin(iTime + 5.*cos(iTime*5.));
float vignette = (1.-vigAmt*(uv.y-.5)*(uv.y-.5))*(1.-vigAmt*(uv.x-.5)*(uv.x-.5));
if(vignetteOn)
video *= vignette;
gl_FragColor = mix(video,vec4(noise(uv * 75.)),.05);
if(curUV.x<0 || curUV.x>1 || curUV.y<0 || curUV.y>1){
gl_FragColor = vec4(0,0,0,0);
}
}
')
public function new()
{
super();
}
}
class ThreeDEffect extends Effect{
public var shader:ThreeDShader = new ThreeDShader();
public function new(xrotation:Float=0,yrotation:Float=0,zrotation:Float=0,depth:Float=0){
shader.xrot.value = [xrotation];
shader.yrot.value = [yrotation];
shader.zrot.value = [zrotation];
shader.dept.value = [depth];
}
}
//coding is like hitting on women, you never start with the number
// -naether
class ThreeDShader extends FlxShader{
@:glFragmentSource('
#pragma header
uniform float xrot = 0.0;
uniform float yrot = 0.0;
uniform float zrot = 0.0;
uniform float dept = 0.0;
float alph = 0;
float plane( in vec3 norm, in vec3 po, in vec3 ro, in vec3 rd ) {
float de = dot(norm, rd);
de = sign(de)*max( abs(de), 0.001);
return dot(norm, po-ro)/de;
}
vec2 raytraceTexturedQuad(in vec3 rayOrigin, in vec3 rayDirection, in vec3 quadCenter, in vec3 quadRotation, in vec2 quadDimensions) {
//Rotations ------------------
float a = sin(quadRotation.x); float b = cos(quadRotation.x);
float c = sin(quadRotation.y); float d = cos(quadRotation.y);
float e = sin(quadRotation.z); float f = cos(quadRotation.z);
float ac = a*c; float bc = b*c;
mat3 RotationMatrix =
mat3( d*f, d*e, -c,
ac*f-b*e, ac*e+b*f, a*d,
bc*f+a*e, bc*e-a*f, b*d );
//--------------------------------------
vec3 right = RotationMatrix * vec3(quadDimensions.x, 0.0, 0.0);
vec3 up = RotationMatrix * vec3(0, quadDimensions.y, 0);
vec3 normal = cross(right, up);
normal /= length(normal);
//Find the plane hit point in space
vec3 pos = (rayDirection * plane(normal, quadCenter, rayOrigin, rayDirection)) - quadCenter;
//Find the texture UV by projecting the hit point along the plane dirs
return vec2(dot(pos, right) / dot(right, right),
dot(pos, up) / dot(up, up)) + 0.5;
}
void main() {
vec4 texColor = texture2D(bitmap, openfl_TextureCoordv);
//Screen UV goes from 0 - 1 along each axis
vec2 screenUV = openfl_TextureCoordv;
vec2 p = (2.0 * screenUV) - 1.0;
float screenAspect = 1280/720;
p.x *= screenAspect;
//Normalized Ray Dir
vec3 dir = vec3(p.x, p.y, 1.0);
dir /= length(dir);
//Define the plane
vec3 planePosition = vec3(0.0, 0.0, dept);
vec3 planeRotation = vec3(xrot, yrot, zrot);//this the shit you needa change
vec2 planeDimension = vec2(-screenAspect, 1.0);
vec2 uv = raytraceTexturedQuad(vec3(0), dir, planePosition, planeRotation, planeDimension);
//If we hit the rectangle, sample the texture
if (abs(uv.x - 0.5) < 0.5 && abs(uv.y - 0.5) < 0.5) {
vec3 tex = flixel_texture2D(bitmap, uv).xyz;
float bitch = 1.0;
if (tex.z == 0.0){
bitch = 0.0;
}
gl_FragColor = vec4(flixel_texture2D(bitmap, uv).xyz, bitch);
}
}
')
public function new(){
super();
}
}
//Boing! by ThaeHan
class FuckingTriangleEffect extends Effect{
public var shader:FuckingTriangle = new FuckingTriangle();
public function new(rotx:Float, roty:Float){
shader.rotX.value = [rotx];
shader.rotY.value = [roty];
}
}
class FuckingTriangle extends FlxShader{
@:glFragmentSource('
#pragma header
const vec3 vertices[18] = vec3[18] (
vec3(-0.5, 0.0, -0.5),
vec3( 0.5, 0.0, -0.5),
vec3(-0.5, 0.0, 0.5),
vec3(-0.5, 0.0, 0.5),
vec3( 0.5, 0.0, -0.5),
vec3( 0.5, 0.0, 0.5),
vec3(-0.5, 0.0, -0.5),
vec3( 0.5, 0.0, -0.5),
vec3( 0.0, 1.0, 0.0),
vec3(-0.5, 0.0, 0.5),
vec3( 0.5, 0.0, 0.5),
vec3( 0.0, 1.0, 0.0),
vec3(-0.5, 0.0, -0.5),
vec3(-0.5, 0.0, 0.5),
vec3( 0.0, 1.0, 0.0),
vec3( 0.5, 0.0, -0.5),
vec3( 0.5, 0.0, 0.5),
vec3( 0.0, 1.0, 0.0)
);
const vec2 texCoords[18] = vec2[18] (
vec2(0., 1.),
vec2(1., 1.),
vec2(0., 0.),
vec2(0., 0.),
vec2(1., 1.),
vec2(1., 0.),
vec2(0., 1.),
vec2(1., 1.),
vec2(.5, 0.),
vec2(0., 1.),
vec2(1., 1.),
vec2(.5, 0.),
vec2(0., 1.),
vec2(1., 1.),
vec2(.5, 0.),
vec2(0., 1.),
vec2(1., 1.),
vec2(.5, 0.)
);
vec4 vertexShader(in vec3 vertex, in mat4 transform) {
return transform * vec4(vertex, 1.);
}
vec4 fragmentShader(in vec2 uv) {
return flixel_texture2D(bitmap, uv);
}
const float fov = 70.0;
const float near = 0.1;
const float far = 10.;
const vec3 cameraPos = vec3(0., 0.3, 2.);
uniform float rotX = -25.;
uniform float rotY = 45.;
vec4 pixel(in vec2 ndc, in float aspect, inout float depth, in int vertexIndex) {
mat4 proj = perspective(fov, aspect, near, far);
mat4 view = translate(-cameraPos);
mat4 model = rotateX(rotX) * rotateY(rotY);
mat4 mvp = proj * view * model;
vec4 v0 = vertexShader(vertices[vertexIndex ], mvp);
vec4 v1 = vertexShader(vertices[vertexIndex+1], mvp);
vec4 v2 = vertexShader(vertices[vertexIndex+2], mvp);
vec2 t0 = texCoords[vertexIndex ] / v0.w; float oow0 = 1. / v0.w;
vec2 t1 = texCoords[vertexIndex+1] / v1.w; float oow1 = 1. / v1.w;
vec2 t2 = texCoords[vertexIndex+2] / v2.w; float oow2 = 1. / v2.w;
v0 /= v0.w;
v1 /= v1.w;
v2 /= v2.w;
vec3 tri = bary(v0.xy, v1.xy, v2.xy, ndc);
if(tri.x < 0. || tri.x > 1. || tri.y < 0. || tri.y > 1. || tri.z < 0. || tri.z > 1.) {
return vec4(0.);
}
float triDepth = baryLerp(v0.z, v1.z, v2.z, tri);
if(triDepth > depth || triDepth < -1. || triDepth > 1.) {
return vec4(0.);
}
depth = triDepth;
float oneOverW = baryLerp(oow0, oow1, oow2, tri);
vec2 uv = uvLerp(t0, t1, t2, tri) / oneOverW;
return fragmentShader(uv);
}
void main()
{
vec2 ndc = ((gl_FragCoord.xy * 2.) / openfl_TextureSize.xy) - vec2(1.);
float aspect = openfl_TextureSize.x / openfl_TextureSize.y;
vec3 outColor = vec3(.4,.6,.9);
float depth = 1.0;
for(int i = 0; i < 18; i += 3) {
vec4 tri = pixel(ndc, aspect, depth, i);
outColor = mix(outColor.rgb, tri.rgb, tri.a);
}
gl_FragColor = vec4(outColor, 1.);
}
')
public function new(){
super();
}
}
class BloomEffect extends Effect{
public var shader:BloomShader = new BloomShader();
public function new(blurSize:Float, intensity:Float){
shader.blurSize.value = [blurSize];
shader.intensity.value = [intensity];
}
}
class BloomShader extends FlxShader{
@:glFragmentSource('
#pragma header
uniform float intensity = 0.35;
uniform float blurSize = 1.0/512.0;
void main()
{
vec4 sum = vec4(0);
vec2 texcoord = openfl_TextureCoordv;
int j;
int i;
//thank you! http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/ for the
//blur tutorial
// blur in y (vertical)
// take nine samples, with the distance blurSize between them
sum += flixel_texture2D(bitmap, vec2(texcoord.x - 4.0*blurSize, texcoord.y)) * 0.05;
sum += flixel_texture2D(bitmap, vec2(texcoord.x - 3.0*blurSize, texcoord.y)) * 0.09;
sum += flixel_texture2D(bitmap, vec2(texcoord.x - 2.0*blurSize, texcoord.y)) * 0.12;
sum += flixel_texture2D(bitmap, vec2(texcoord.x - blurSize, texcoord.y)) * 0.15;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y)) * 0.16;
sum += flixel_texture2D(bitmap, vec2(texcoord.x + blurSize, texcoord.y)) * 0.15;
sum += flixel_texture2D(bitmap, vec2(texcoord.x + 2.0*blurSize, texcoord.y)) * 0.12;
sum += flixel_texture2D(bitmap, vec2(texcoord.x + 3.0*blurSize, texcoord.y)) * 0.09;
sum += flixel_texture2D(bitmap, vec2(texcoord.x + 4.0*blurSize, texcoord.y)) * 0.05;
// blur in y (vertical)
// take nine samples, with the distance blurSize between them
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y - 4.0*blurSize)) * 0.05;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y - 3.0*blurSize)) * 0.09;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y - 2.0*blurSize)) * 0.12;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y - blurSize)) * 0.15;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y)) * 0.16;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y + blurSize)) * 0.15;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y + 2.0*blurSize)) * 0.12;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y + 3.0*blurSize)) * 0.09;
sum += flixel_texture2D(bitmap, vec2(texcoord.x, texcoord.y + 4.0*blurSize)) * 0.05;
//increase blur with intensity!
gl_FragColor = sum*intensity + flixel_texture2D(bitmap, texcoord);
// if(sin(iTime) > 0.0)
// fragColor = sum * sin(iTime)+ texture(iChannel0, texcoord);
// else
// fragColor = sum * -sin(iTime)+ texture(iChannel0, texcoord);
}
')
public function new(){
super();
}
}
/*STOLE FROM DAVE AND BAMBI
I LOVE BANUUU I LOVE BANUUU
________
/ \
_/__________\_
|| o|| o||
|//-- --//|
\____O___/
| |
|______|
| | |
|___|__|
*/
class GlitchEffect extends Effect
{
public var shader:GlitchShader = new GlitchShader();
public var waveSpeed(default, set):Float = 0;
public var waveFrequency(default, set):Float = 0;
public var waveAmplitude(default, set):Float = 0;
public function new(waveSpeed:Float,waveFrequency:Float,waveAmplitude:Float):Void
{
shader.uTime.value = [0];
this.waveSpeed = waveSpeed;
this.waveFrequency = waveFrequency;