DigitalGlitch.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ( function () {
  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. var DigitalGlitch = {
  12. uniforms: {
  13. 'tDiffuse': {
  14. value: null
  15. },
  16. //diffuse texture
  17. 'tDisp': {
  18. value: null
  19. },
  20. //displacement texture for digital glitch squares
  21. 'byp': {
  22. value: 0
  23. },
  24. //apply the glitch ?
  25. 'amount': {
  26. value: 0.08
  27. },
  28. 'angle': {
  29. value: 0.02
  30. },
  31. 'seed': {
  32. value: 0.02
  33. },
  34. 'seed_x': {
  35. value: 0.02
  36. },
  37. //-1,1
  38. 'seed_y': {
  39. value: 0.02
  40. },
  41. //-1,1
  42. 'distortion_x': {
  43. value: 0.5
  44. },
  45. 'distortion_y': {
  46. value: 0.6
  47. },
  48. 'col_s': {
  49. value: 0.05
  50. }
  51. },
  52. vertexShader: [ 'varying vec2 vUv;', 'void main() {', ' vUv = uv;', ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', '}' ].join( '\n' ),
  53. fragmentShader: [ 'uniform int byp;', //should we apply the glitch ?
  54. 'uniform sampler2D tDiffuse;', 'uniform sampler2D tDisp;', 'uniform float amount;', 'uniform float angle;', 'uniform float seed;', 'uniform float seed_x;', 'uniform float seed_y;', 'uniform float distortion_x;', 'uniform float distortion_y;', 'uniform float col_s;', 'varying vec2 vUv;', 'float rand(vec2 co){', ' return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);', '}', 'void main() {', ' if(byp<1) {', ' vec2 p = vUv;', ' float xs = floor(gl_FragCoord.x / 0.5);', ' float ys = floor(gl_FragCoord.y / 0.5);', //based on staffantans glitch shader for unity https://github.com/staffantan/unityglitch
  55. ' vec4 normal = texture2D (tDisp, p*seed*seed);', ' if(p.y<distortion_x+col_s && p.y>distortion_x-col_s*seed) {', ' if(seed_x>0.){', ' p.y = 1. - (p.y + distortion_y);', ' }', ' else {', ' p.y = distortion_y;', ' }', ' }', ' if(p.x<distortion_y+col_s && p.x>distortion_y-col_s*seed) {', ' if(seed_y>0.){', ' p.x=distortion_x;', ' }', ' else {', ' p.x = 1. - (p.x + distortion_x);', ' }', ' }', ' p.x+=normal.x*seed_x*(seed/5.);', ' p.y+=normal.y*seed_y*(seed/5.);', //base from RGB shift shader
  56. ' vec2 offset = amount * vec2( cos(angle), sin(angle));', ' vec4 cr = texture2D(tDiffuse, p + offset);', ' vec4 cga = texture2D(tDiffuse, p);', ' vec4 cb = texture2D(tDiffuse, p - offset);', ' gl_FragColor = vec4(cr.r, cga.g, cb.b, cga.a);', //add noise
  57. ' vec4 snow = 200.*amount*vec4(rand(vec2(xs * seed,ys * seed*50.))*0.2);', ' gl_FragColor = gl_FragColor+ snow;', ' }', ' else {', ' gl_FragColor=texture2D (tDiffuse, vUv);', ' }', '}' ].join( '\n' )
  58. };
  59. THREE.DigitalGlitch = DigitalGlitch;
  60. } )();