DigitalGlitch.js 2.5 KB

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