DigitalGlitch.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @author felixturner / http://airtight.cc/
  3. *
  4. * RGB Shift Shader
  5. * Shifts red and blue channels from center in opposite directions
  6. * Ported from http://kriss.cx/tom/2009/05/rgb-shift/
  7. * by Tom Butterworth / http://kriss.cx/tom/
  8. *
  9. * amount: shift distance (1 is width of input)
  10. * angle: shift angle in radians
  11. */
  12. THREE.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: [
  27. "varying vec2 vUv;",
  28. "void main() {",
  29. "vUv = uv;",
  30. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  31. "}"
  32. ].join( "\n" ),
  33. fragmentShader: [
  34. "uniform int byp;",//should we apply the glitch ?
  35. "uniform sampler2D tDiffuse;",
  36. "uniform sampler2D tDisp;",
  37. "uniform float amount;",
  38. "uniform float angle;",
  39. "uniform float seed;",
  40. "uniform float seed_x;",
  41. "uniform float seed_y;",
  42. "uniform float distortion_x;",
  43. "uniform float distortion_y;",
  44. "uniform float col_s;",
  45. "varying vec2 vUv;",
  46. "float rand(vec2 co){",
  47. "return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);",
  48. "}",
  49. "void main() {",
  50. "if(byp<1) {",
  51. "vec2 p = vUv;",
  52. "float xs = floor(gl_FragCoord.x / 0.5);",
  53. "float ys = floor(gl_FragCoord.y / 0.5);",
  54. //based on staffantans glitch shader for unity https://github.com/staffantan/unityglitch
  55. "vec4 normal = texture2D (tDisp, p*seed*seed);",
  56. "if(p.y<distortion_x+col_s && p.y>distortion_x-col_s*seed) {",
  57. "if(seed_x>0.){",
  58. "p.y = 1. - (p.y + distortion_y);",
  59. "}",
  60. "else {",
  61. "p.y = distortion_y;",
  62. "}",
  63. "}",
  64. "if(p.x<distortion_y+col_s && p.x>distortion_y-col_s*seed) {",
  65. "if(seed_y>0.){",
  66. "p.x=distortion_x;",
  67. "}",
  68. "else {",
  69. "p.x = 1. - (p.x + distortion_x);",
  70. "}",
  71. "}",
  72. "p.x+=normal.x*seed_x*(seed/5.);",
  73. "p.y+=normal.y*seed_y*(seed/5.);",
  74. //base from RGB shift shader
  75. "vec2 offset = amount * vec2( cos(angle), sin(angle));",
  76. "vec4 cr = texture2D(tDiffuse, p + offset);",
  77. "vec4 cga = texture2D(tDiffuse, p);",
  78. "vec4 cb = texture2D(tDiffuse, p - offset);",
  79. "gl_FragColor = vec4(cr.r, cga.g, cb.b, cga.a);",
  80. //add noise
  81. "vec4 snow = 200.*amount*vec4(rand(vec2(xs * seed,ys * seed*50.))*0.2);",
  82. "gl_FragColor = gl_FragColor+ snow;",
  83. "}",
  84. "else {",
  85. "gl_FragColor=texture2D (tDiffuse, vUv);",
  86. "}",
  87. "}"
  88. ].join( "\n" )
  89. };