GlitchPass.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. */
  3. THREE.GlitchPass = function ( dt_size ) {
  4. if ( THREE.DigitalGlitch === undefined ) console.error( "THREE.GlitchPass relies on THREE.DigitalGlitch" );
  5. var shader = THREE.DigitalGlitch;
  6. this.uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  7. if(dt_size==undefined) dt_size=64;
  8. this.uniforms[ "tDisp"].value=this.generateHeightmap(dt_size);
  9. this.material = new THREE.ShaderMaterial({
  10. uniforms: this.uniforms,
  11. vertexShader: shader.vertexShader,
  12. fragmentShader: shader.fragmentShader
  13. });
  14. console.log(this.material);
  15. this.enabled = true;
  16. this.renderToScreen = false;
  17. this.needsSwap = true;
  18. this.camera = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  19. this.scene = new THREE.Scene();
  20. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  21. this.scene.add( this.quad );
  22. this.goWild=false;
  23. this.curF=0;
  24. this.generateTrigger();
  25. };
  26. THREE.GlitchPass.prototype = {
  27. render: function ( renderer, writeBuffer, readBuffer, delta )
  28. {
  29. this.uniforms[ "tDiffuse" ].value = readBuffer;
  30. this.uniforms[ 'seed' ].value=Math.random();//default seeding
  31. this.uniforms[ 'byp' ].value=0;
  32. if(this.curF % this.randX ==0 || this.goWild==true)
  33. {
  34. this.uniforms[ 'amount' ].value=Math.random()/30;
  35. this.uniforms[ 'angle' ].value=THREE.Math.randFloat(-Math.PI,Math.PI);
  36. this.uniforms[ 'seed_x' ].value=THREE.Math.randFloat(-1,1);
  37. this.uniforms[ 'seed_y' ].value=THREE.Math.randFloat(-1,1);
  38. this.uniforms[ 'distortion_x' ].value=THREE.Math.randFloat(0,1);
  39. this.uniforms[ 'distortion_y' ].value=THREE.Math.randFloat(0,1);
  40. this.curF=0;
  41. this.generateTrigger();
  42. }
  43. else if(this.curF % this.randX <this.randX/5)
  44. {
  45. this.uniforms[ 'amount' ].value=Math.random()/90;
  46. this.uniforms[ 'angle' ].value=THREE.Math.randFloat(-Math.PI,Math.PI);
  47. this.uniforms[ 'distortion_x' ].value=THREE.Math.randFloat(0,1);
  48. this.uniforms[ 'distortion_y' ].value=THREE.Math.randFloat(0,1);
  49. this.uniforms[ 'seed_x' ].value=THREE.Math.randFloat(-0.3,0.3);
  50. this.uniforms[ 'seed_y' ].value=THREE.Math.randFloat(-0.3,0.3);
  51. }
  52. else if(this.goWild==false)
  53. {
  54. this.uniforms[ 'byp' ].value=1;
  55. }
  56. this.curF++;
  57. this.quad.material = this.material;
  58. if ( this.renderToScreen )
  59. {
  60. renderer.render( this.scene, this.camera );
  61. }
  62. else
  63. {
  64. renderer.render( this.scene, this.camera, writeBuffer, false );
  65. }
  66. },
  67. generateTrigger:function()
  68. {
  69. this.randX=THREE.Math.randInt(120,240);
  70. },
  71. generateHeightmap:function(dt_size)
  72. {
  73. var data_arr = new Float32Array( dt_size*dt_size * 3 );
  74. console.log(dt_size);
  75. var length=dt_size*dt_size;
  76. for ( var i = 0; i < length; i++)
  77. {
  78. var val=THREE.Math.randFloat(0,1);
  79. data_arr[ i*3 + 0 ] = val;
  80. data_arr[ i*3 + 1 ] = val;
  81. data_arr[ i*3 + 2 ] = val;
  82. }
  83. var texture = new THREE.DataTexture( data_arr, dt_size, dt_size, THREE.RGBFormat, THREE.FloatType );
  84. console.log(texture);
  85. console.log(dt_size);
  86. texture.minFilter = THREE.NearestFilter;
  87. texture.magFilter = THREE.NearestFilter;
  88. texture.needsUpdate = true;
  89. texture.flipY = false;
  90. return texture;
  91. }
  92. };