DigitalGlitch.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ( function () {
  2. /**
  3. * RGB Shift Shader
  4. * Shifts red and blue channels from center in opposite directions
  5. * Ported from http://kriss.cx/tom/2009/05/rgb-shift/
  6. * by Tom Butterworth / http://kriss.cx/tom/
  7. *
  8. * amount: shift distance (1 is width of input)
  9. * angle: shift angle in radians
  10. */
  11. const DigitalGlitch = {
  12. uniforms: {
  13. 'tDiffuse': {
  14. value: null
  15. },
  16. //diffuse texture
  17. 'tDisp': {
  18. value: null
  19. },
  20. //displacement texture for digital glitch squares
  21. 'byp': {
  22. value: 0
  23. },
  24. //apply the glitch ?
  25. 'amount': {
  26. value: 0.08
  27. },
  28. 'angle': {
  29. value: 0.02
  30. },
  31. 'seed': {
  32. value: 0.02
  33. },
  34. 'seed_x': {
  35. value: 0.02
  36. },
  37. //-1,1
  38. 'seed_y': {
  39. value: 0.02
  40. },
  41. //-1,1
  42. 'distortion_x': {
  43. value: 0.5
  44. },
  45. 'distortion_y': {
  46. value: 0.6
  47. },
  48. 'col_s': {
  49. value: 0.05
  50. }
  51. },
  52. vertexShader: /* glsl */`
  53. varying vec2 vUv;
  54. void main() {
  55. vUv = uv;
  56. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  57. }`,
  58. fragmentShader: /* glsl */`
  59. uniform int byp; //should we apply the glitch ?
  60. uniform sampler2D tDiffuse;
  61. uniform sampler2D tDisp;
  62. uniform float amount;
  63. uniform float angle;
  64. uniform float seed;
  65. uniform float seed_x;
  66. uniform float seed_y;
  67. uniform float distortion_x;
  68. uniform float distortion_y;
  69. uniform float col_s;
  70. varying vec2 vUv;
  71. float rand(vec2 co){
  72. return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  73. }
  74. void main() {
  75. if(byp<1) {
  76. vec2 p = vUv;
  77. float xs = floor(gl_FragCoord.x / 0.5);
  78. float ys = floor(gl_FragCoord.y / 0.5);
  79. //based on staffantans glitch shader for unity https://github.com/staffantan/unityglitch
  80. float disp = texture2D(tDisp, p*seed*seed).r;
  81. if(p.y<distortion_x+col_s && p.y>distortion_x-col_s*seed) {
  82. if(seed_x>0.){
  83. p.y = 1. - (p.y + distortion_y);
  84. }
  85. else {
  86. p.y = distortion_y;
  87. }
  88. }
  89. if(p.x<distortion_y+col_s && p.x>distortion_y-col_s*seed) {
  90. if(seed_y>0.){
  91. p.x=distortion_x;
  92. }
  93. else {
  94. p.x = 1. - (p.x + distortion_x);
  95. }
  96. }
  97. p.x+=disp*seed_x*(seed/5.);
  98. p.y+=disp*seed_y*(seed/5.);
  99. //base from RGB shift shader
  100. vec2 offset = amount * vec2( cos(angle), sin(angle));
  101. vec4 cr = texture2D(tDiffuse, p + offset);
  102. vec4 cga = texture2D(tDiffuse, p);
  103. vec4 cb = texture2D(tDiffuse, p - offset);
  104. gl_FragColor = vec4(cr.r, cga.g, cb.b, cga.a);
  105. //add noise
  106. vec4 snow = 200.*amount*vec4(rand(vec2(xs * seed,ys * seed*50.))*0.2);
  107. gl_FragColor = gl_FragColor+ snow;
  108. }
  109. else {
  110. gl_FragColor=texture2D (tDiffuse, vUv);
  111. }
  112. }`
  113. };
  114. THREE.DigitalGlitch = DigitalGlitch;
  115. } )();