GlitchPass.js 3.1 KB

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