DigitalGlitch.js 2.5 KB

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