DigitalGlitch.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. console.warn( "THREE.DigitalGlitch: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author felixturner / http://airtight.cc/
  4. *
  5. * RGB Shift Shader
  6. * Shifts red and blue channels from center in opposite directions
  7. * Ported from http://kriss.cx/tom/2009/05/rgb-shift/
  8. * by Tom Butterworth / http://kriss.cx/tom/
  9. *
  10. * amount: shift distance (1 is width of input)
  11. * angle: shift angle in radians
  12. */
  13. THREE.DigitalGlitch = {
  14. uniforms: {
  15. "tDiffuse": { value: null }, //diffuse texture
  16. "tDisp": { value: null }, //displacement texture for digital glitch squares
  17. "byp": { value: 0 }, //apply the glitch ?
  18. "amount": { value: 0.08 },
  19. "angle": { value: 0.02 },
  20. "seed": { value: 0.02 },
  21. "seed_x": { value: 0.02 }, //-1,1
  22. "seed_y": { value: 0.02 }, //-1,1
  23. "distortion_x": { value: 0.5 },
  24. "distortion_y": { value: 0.6 },
  25. "col_s": { value: 0.05 }
  26. },
  27. vertexShader: [
  28. "varying vec2 vUv;",
  29. "void main() {",
  30. " vUv = uv;",
  31. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  32. "}"
  33. ].join( "\n" ),
  34. fragmentShader: [
  35. "uniform int byp;", //should we apply the glitch ?
  36. "uniform sampler2D tDiffuse;",
  37. "uniform sampler2D tDisp;",
  38. "uniform float amount;",
  39. "uniform float angle;",
  40. "uniform float seed;",
  41. "uniform float seed_x;",
  42. "uniform float seed_y;",
  43. "uniform float distortion_x;",
  44. "uniform float distortion_y;",
  45. "uniform float col_s;",
  46. "varying vec2 vUv;",
  47. "float rand(vec2 co){",
  48. " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);",
  49. "}",
  50. "void main() {",
  51. " if(byp<1) {",
  52. " vec2 p = vUv;",
  53. " float xs = floor(gl_FragCoord.x / 0.5);",
  54. " float ys = floor(gl_FragCoord.y / 0.5);",
  55. //based on staffantans glitch shader for unity https://github.com/staffantan/unityglitch
  56. " vec4 normal = texture2D (tDisp, p*seed*seed);",
  57. " if(p.y<distortion_x+col_s && p.y>distortion_x-col_s*seed) {",
  58. " if(seed_x>0.){",
  59. " p.y = 1. - (p.y + distortion_y);",
  60. " }",
  61. " else {",
  62. " p.y = distortion_y;",
  63. " }",
  64. " }",
  65. " if(p.x<distortion_y+col_s && p.x>distortion_y-col_s*seed) {",
  66. " if(seed_y>0.){",
  67. " p.x=distortion_x;",
  68. " }",
  69. " else {",
  70. " p.x = 1. - (p.x + distortion_x);",
  71. " }",
  72. " }",
  73. " p.x+=normal.x*seed_x*(seed/5.);",
  74. " p.y+=normal.y*seed_y*(seed/5.);",
  75. //base from RGB shift shader
  76. " vec2 offset = amount * vec2( cos(angle), sin(angle));",
  77. " vec4 cr = texture2D(tDiffuse, p + offset);",
  78. " vec4 cga = texture2D(tDiffuse, p);",
  79. " vec4 cb = texture2D(tDiffuse, p - offset);",
  80. " gl_FragColor = vec4(cr.r, cga.g, cb.b, cga.a);",
  81. //add noise
  82. " vec4 snow = 200.*amount*vec4(rand(vec2(xs * seed,ys * seed*50.))*0.2);",
  83. " gl_FragColor = gl_FragColor+ snow;",
  84. " }",
  85. " else {",
  86. " gl_FragColor=texture2D (tDiffuse, vUv);",
  87. " }",
  88. "}"
  89. ].join( "\n" )
  90. };