GlitchPass.js 3.2 KB

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