GlitchPass.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.GlitchPass = function ( dt_size ) {
  5. THREE.Pass.call( this );
  6. if ( THREE.DigitalGlitch === undefined ) console.error( "THREE.GlitchPass relies on THREE.DigitalGlitch" );
  7. var shader = THREE.DigitalGlitch;
  8. this.uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  9. if ( dt_size == undefined ) dt_size = 64;
  10. this.uniforms[ "tDisp" ].value = this.generateHeightmap( dt_size );
  11. this.material = new THREE.ShaderMaterial( {
  12. uniforms: this.uniforms,
  13. vertexShader: shader.vertexShader,
  14. fragmentShader: shader.fragmentShader
  15. } );
  16. this.fsQuad = new THREE.Pass.FullScreenQuad( this.material );
  17. this.goWild = false;
  18. this.curF = 0;
  19. this.generateTrigger();
  20. };
  21. THREE.GlitchPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  22. constructor: THREE.GlitchPass,
  23. render: function ( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  24. this.uniforms[ "tDiffuse" ].value = readBuffer.texture;
  25. this.uniforms[ 'seed' ].value = Math.random();//default seeding
  26. this.uniforms[ 'byp' ].value = 0;
  27. if ( this.curF % this.randX == 0 || this.goWild == true ) {
  28. this.uniforms[ 'amount' ].value = Math.random() / 30;
  29. this.uniforms[ 'angle' ].value = THREE.MathUtils.randFloat( - Math.PI, Math.PI );
  30. this.uniforms[ 'seed_x' ].value = THREE.MathUtils.randFloat( - 1, 1 );
  31. this.uniforms[ 'seed_y' ].value = THREE.MathUtils.randFloat( - 1, 1 );
  32. this.uniforms[ 'distortion_x' ].value = THREE.MathUtils.randFloat( 0, 1 );
  33. this.uniforms[ 'distortion_y' ].value = THREE.MathUtils.randFloat( 0, 1 );
  34. this.curF = 0;
  35. this.generateTrigger();
  36. } else if ( this.curF % this.randX < this.randX / 5 ) {
  37. this.uniforms[ 'amount' ].value = Math.random() / 90;
  38. this.uniforms[ 'angle' ].value = THREE.MathUtils.randFloat( - Math.PI, Math.PI );
  39. this.uniforms[ 'distortion_x' ].value = THREE.MathUtils.randFloat( 0, 1 );
  40. this.uniforms[ 'distortion_y' ].value = THREE.MathUtils.randFloat( 0, 1 );
  41. this.uniforms[ 'seed_x' ].value = THREE.MathUtils.randFloat( - 0.3, 0.3 );
  42. this.uniforms[ 'seed_y' ].value = THREE.MathUtils.randFloat( - 0.3, 0.3 );
  43. } else if ( this.goWild == false ) {
  44. this.uniforms[ 'byp' ].value = 1;
  45. }
  46. this.curF ++;
  47. if ( this.renderToScreen ) {
  48. renderer.setRenderTarget( null );
  49. this.fsQuad.render( renderer );
  50. } else {
  51. renderer.setRenderTarget( writeBuffer );
  52. if ( this.clear ) renderer.clear();
  53. this.fsQuad.render( renderer );
  54. }
  55. },
  56. generateTrigger: function () {
  57. this.randX = THREE.MathUtils.randInt( 120, 240 );
  58. },
  59. generateHeightmap: function ( dt_size ) {
  60. var data_arr = new Float32Array( dt_size * dt_size * 3 );
  61. var length = dt_size * dt_size;
  62. for ( var i = 0; i < length; i ++ ) {
  63. var val = THREE.MathUtils.randFloat( 0, 1 );
  64. data_arr[ i * 3 + 0 ] = val;
  65. data_arr[ i * 3 + 1 ] = val;
  66. data_arr[ i * 3 + 2 ] = val;
  67. }
  68. return new THREE.DataTexture( data_arr, dt_size, dt_size, THREE.RGBFormat, THREE.FloatType );
  69. }
  70. } );