GlitchPass.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = Object.assign( {}, 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.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  17. this.scene = new THREE.Scene();
  18. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  19. this.scene.add( this.quad );
  20. this.goWild = false;
  21. this.curF = 0;
  22. this.generateTrigger();
  23. };
  24. THREE.GlitchPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  25. constructor: THREE.GlitchPass,
  26. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  27. this.uniforms[ "tDiffuse" ].value = readBuffer.texture;
  28. this.uniforms[ 'seed' ].value = Math.random();//default seeding
  29. this.uniforms[ 'byp' ].value = 0;
  30. if ( this.curF % this.randX == 0 || this.goWild == true ) {
  31. this.uniforms[ 'amount' ].value = Math.random() / 30;
  32. this.uniforms[ 'angle' ].value = THREE.Math.randFloat( - Math.PI, Math.PI );
  33. this.uniforms[ 'seed_x' ].value = THREE.Math.randFloat( - 1, 1 );
  34. this.uniforms[ 'seed_y' ].value = THREE.Math.randFloat( - 1, 1 );
  35. this.uniforms[ 'distortion_x' ].value = THREE.Math.randFloat( 0, 1 );
  36. this.uniforms[ 'distortion_y' ].value = THREE.Math.randFloat( 0, 1 );
  37. this.curF = 0;
  38. this.generateTrigger();
  39. } else if ( this.curF % this.randX < this.randX / 5 ) {
  40. this.uniforms[ 'amount' ].value = Math.random() / 90;
  41. this.uniforms[ 'angle' ].value = THREE.Math.randFloat( - Math.PI, Math.PI );
  42. this.uniforms[ 'distortion_x' ].value = THREE.Math.randFloat( 0, 1 );
  43. this.uniforms[ 'distortion_y' ].value = THREE.Math.randFloat( 0, 1 );
  44. this.uniforms[ 'seed_x' ].value = THREE.Math.randFloat( - 0.3, 0.3 );
  45. this.uniforms[ 'seed_y' ].value = THREE.Math.randFloat( - 0.3, 0.3 );
  46. } else if ( this.goWild == false ) {
  47. this.uniforms[ 'byp' ].value = 1;
  48. }
  49. this.curF ++;
  50. this.quad.material = this.material;
  51. if ( this.renderToScreen ) {
  52. renderer.render( this.scene, this.camera );
  53. } else {
  54. renderer.render( this.scene, this.camera, writeBuffer, this.clear );
  55. }
  56. },
  57. generateTrigger: function() {
  58. this.randX = THREE.Math.randInt( 120, 240 );
  59. },
  60. generateHeightmap: function( dt_size ) {
  61. var data_arr = new Float32Array( dt_size * dt_size * 3 );
  62. var length = dt_size * dt_size;
  63. for ( var i = 0; i < length; i ++ ) {
  64. var val = THREE.Math.randFloat( 0, 1 );
  65. data_arr[ i * 3 + 0 ] = val;
  66. data_arr[ i * 3 + 1 ] = val;
  67. data_arr[ i * 3 + 2 ] = val;
  68. }
  69. var texture = new THREE.DataTexture( data_arr, dt_size, dt_size, THREE.RGBFormat, THREE.FloatType );
  70. texture.needsUpdate = true;
  71. return texture;
  72. }
  73. } );