DigitalGlitch.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/#manual/en/introduction/Installation." );
  2. /**
  3. * RGB Shift Shader
  4. * Shifts red and blue channels from center in opposite directions
  5. * Ported from http://kriss.cx/tom/2009/05/rgb-shift/
  6. * by Tom Butterworth / http://kriss.cx/tom/
  7. *
  8. * amount: shift distance (1 is width of input)
  9. * angle: shift angle in radians
  10. */
  11. THREE.DigitalGlitch = {
  12. uniforms: {
  13. "tDiffuse": { value: null }, //diffuse texture
  14. "tDisp": { value: null }, //displacement texture for digital glitch squares
  15. "byp": { value: 0 }, //apply the glitch ?
  16. "amount": { value: 0.08 },
  17. "angle": { value: 0.02 },
  18. "seed": { value: 0.02 },
  19. "seed_x": { value: 0.02 }, //-1,1
  20. "seed_y": { value: 0.02 }, //-1,1
  21. "distortion_x": { value: 0.5 },
  22. "distortion_y": { value: 0.6 },
  23. "col_s": { value: 0.05 }
  24. },
  25. vertexShader: [
  26. "varying vec2 vUv;",
  27. "void main() {",
  28. " vUv = uv;",
  29. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  30. "}"
  31. ].join( "\n" ),
  32. fragmentShader: [
  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. " vec4 normal = texture2D (tDisp, p*seed*seed);",
  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+=normal.x*seed_x*(seed/5.);",
  72. " p.y+=normal.y*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. ].join( "\n" )
  88. };