DigitalGlitch.js 2.3 KB

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