GlitchPass.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import {
  2. DataTexture,
  3. FloatType,
  4. MathUtils,
  5. RedFormat,
  6. ShaderMaterial,
  7. UniformsUtils
  8. } from 'three';
  9. import { Pass, FullScreenQuad } from './Pass.js';
  10. import { DigitalGlitch } from '../shaders/DigitalGlitch.js';
  11. /**
  12. * Pass for creating a glitch effect.
  13. *
  14. * ```js
  15. * const glitchPass = new GlitchPass();
  16. * composer.addPass( glitchPass );
  17. * ```
  18. *
  19. * @augments Pass
  20. */
  21. class GlitchPass extends Pass {
  22. /**
  23. * Constructs a new glitch pass.
  24. *
  25. * @param {number} [dt_size=64] - The size of the displacement texture
  26. * for digital glitch squares.
  27. */
  28. constructor( dt_size = 64 ) {
  29. super();
  30. /**
  31. * The pass uniforms.
  32. *
  33. * @type {Object}
  34. */
  35. this.uniforms = UniformsUtils.clone( DigitalGlitch.uniforms );
  36. /**
  37. * The pass material.
  38. *
  39. * @type {ShaderMaterial}
  40. */
  41. this.material = new ShaderMaterial( {
  42. uniforms: this.uniforms,
  43. vertexShader: DigitalGlitch.vertexShader,
  44. fragmentShader: DigitalGlitch.fragmentShader
  45. } );
  46. /**
  47. * Whether to noticeably increase the effect instensity or not.
  48. *
  49. * @type {boolean}
  50. * @default false
  51. */
  52. this.goWild = false;
  53. // internals
  54. this._heightMap = this._generateHeightmap( dt_size );
  55. this.uniforms[ 'tDisp' ].value = this.heightMap;
  56. this._fsQuad = new FullScreenQuad( this.material );
  57. this._curF = 0;
  58. this._randX = 0;
  59. this._generateTrigger();
  60. }
  61. /**
  62. * Performs the glitch pass.
  63. *
  64. * @param {WebGLRenderer} renderer - The renderer.
  65. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  66. * destination for the pass.
  67. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  68. * previous pass from this buffer.
  69. * @param {number} deltaTime - The delta time in seconds.
  70. * @param {boolean} maskActive - Whether masking is active or not.
  71. */
  72. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  73. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  74. this.uniforms[ 'seed' ].value = Math.random(); // default seeding
  75. this.uniforms[ 'byp' ].value = 0;
  76. if ( this._curF % this._randX == 0 || this.goWild == true ) {
  77. this.uniforms[ 'amount' ].value = Math.random() / 30;
  78. this.uniforms[ 'angle' ].value = MathUtils.randFloat( - Math.PI, Math.PI );
  79. this.uniforms[ 'seed_x' ].value = MathUtils.randFloat( - 1, 1 );
  80. this.uniforms[ 'seed_y' ].value = MathUtils.randFloat( - 1, 1 );
  81. this.uniforms[ 'distortion_x' ].value = MathUtils.randFloat( 0, 1 );
  82. this.uniforms[ 'distortion_y' ].value = MathUtils.randFloat( 0, 1 );
  83. this._curF = 0;
  84. this._generateTrigger();
  85. } else if ( this._curF % this._randX < this._randX / 5 ) {
  86. this.uniforms[ 'amount' ].value = Math.random() / 90;
  87. this.uniforms[ 'angle' ].value = MathUtils.randFloat( - Math.PI, Math.PI );
  88. this.uniforms[ 'distortion_x' ].value = MathUtils.randFloat( 0, 1 );
  89. this.uniforms[ 'distortion_y' ].value = MathUtils.randFloat( 0, 1 );
  90. this.uniforms[ 'seed_x' ].value = MathUtils.randFloat( - 0.3, 0.3 );
  91. this.uniforms[ 'seed_y' ].value = MathUtils.randFloat( - 0.3, 0.3 );
  92. } else if ( this.goWild == false ) {
  93. this.uniforms[ 'byp' ].value = 1;
  94. }
  95. this._curF ++;
  96. if ( this.renderToScreen ) {
  97. renderer.setRenderTarget( null );
  98. this._fsQuad.render( renderer );
  99. } else {
  100. renderer.setRenderTarget( writeBuffer );
  101. if ( this.clear ) renderer.clear();
  102. this._fsQuad.render( renderer );
  103. }
  104. }
  105. /**
  106. * Frees the GPU-related resources allocated by this instance. Call this
  107. * method whenever the pass is no longer used in your app.
  108. */
  109. dispose() {
  110. this.material.dispose();
  111. this.heightMap.dispose();
  112. this._fsQuad.dispose();
  113. }
  114. // internals
  115. _generateTrigger() {
  116. this._randX = MathUtils.randInt( 120, 240 );
  117. }
  118. _generateHeightmap( dt_size ) {
  119. const data_arr = new Float32Array( dt_size * dt_size );
  120. const length = dt_size * dt_size;
  121. for ( let i = 0; i < length; i ++ ) {
  122. const val = MathUtils.randFloat( 0, 1 );
  123. data_arr[ i ] = val;
  124. }
  125. const texture = new DataTexture( data_arr, dt_size, dt_size, RedFormat, FloatType );
  126. texture.needsUpdate = true;
  127. return texture;
  128. }
  129. }
  130. export { GlitchPass };
粤ICP备19079148号