GlitchPass.js 2.9 KB

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