GlitchPass.js 3.1 KB

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