DigitalGlitch.js 2.7 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. THREE.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: [
  25. "varying vec2 vUv;",
  26. "void main() {",
  27. " vUv = uv;",
  28. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  29. "}"
  30. ].join( "\n" ),
  31. fragmentShader: [
  32. "uniform int byp;", //should we apply the glitch ?
  33. "uniform sampler2D tDiffuse;",
  34. "uniform sampler2D tDisp;",
  35. "uniform float amount;",
  36. "uniform float angle;",
  37. "uniform float seed;",
  38. "uniform float seed_x;",
  39. "uniform float seed_y;",
  40. "uniform float distortion_x;",
  41. "uniform float distortion_y;",
  42. "uniform float col_s;",
  43. "varying vec2 vUv;",
  44. "float rand(vec2 co){",
  45. " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);",
  46. "}",
  47. "void main() {",
  48. " if(byp<1) {",
  49. " vec2 p = vUv;",
  50. " float xs = floor(gl_FragCoord.x / 0.5);",
  51. " float ys = floor(gl_FragCoord.y / 0.5);",
  52. //based on staffantans glitch shader for unity https://github.com/staffantan/unityglitch
  53. " vec4 normal = texture2D (tDisp, p*seed*seed);",
  54. " if(p.y<distortion_x+col_s && p.y>distortion_x-col_s*seed) {",
  55. " if(seed_x>0.){",
  56. " p.y = 1. - (p.y + distortion_y);",
  57. " }",
  58. " else {",
  59. " p.y = distortion_y;",
  60. " }",
  61. " }",
  62. " if(p.x<distortion_y+col_s && p.x>distortion_y-col_s*seed) {",
  63. " if(seed_y>0.){",
  64. " p.x=distortion_x;",
  65. " }",
  66. " else {",
  67. " p.x = 1. - (p.x + distortion_x);",
  68. " }",
  69. " }",
  70. " p.x+=normal.x*seed_x*(seed/5.);",
  71. " p.y+=normal.y*seed_y*(seed/5.);",
  72. //base from RGB shift shader
  73. " vec2 offset = amount * vec2( cos(angle), sin(angle));",
  74. " vec4 cr = texture2D(tDiffuse, p + offset);",
  75. " vec4 cga = texture2D(tDiffuse, p);",
  76. " vec4 cb = texture2D(tDiffuse, p - offset);",
  77. " gl_FragColor = vec4(cr.r, cga.g, cb.b, cga.a);",
  78. //add noise
  79. " vec4 snow = 200.*amount*vec4(rand(vec2(xs * seed,ys * seed*50.))*0.2);",
  80. " gl_FragColor = gl_FragColor+ snow;",
  81. " }",
  82. " else {",
  83. " gl_FragColor=texture2D (tDiffuse, vUv);",
  84. " }",
  85. "}"
  86. ].join( "\n" )
  87. };