GlitchPass.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.uniforms[ 'tDisp' ].value = this.generateHeightmap( dt_size );
  19. this.material = new ShaderMaterial( {
  20. uniforms: this.uniforms,
  21. vertexShader: shader.vertexShader,
  22. fragmentShader: shader.fragmentShader
  23. } );
  24. this.fsQuad = new FullScreenQuad( this.material );
  25. this.goWild = false;
  26. this.curF = 0;
  27. this.generateTrigger();
  28. }
  29. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  30. if ( renderer.capabilities.isWebGL2 === false ) this.uniforms[ 'tDisp' ].value.format = LuminanceFormat;
  31. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  32. this.uniforms[ 'seed' ].value = Math.random();//default seeding
  33. this.uniforms[ 'byp' ].value = 0;
  34. if ( this.curF % this.randX == 0 || this.goWild == true ) {
  35. this.uniforms[ 'amount' ].value = Math.random() / 30;
  36. this.uniforms[ 'angle' ].value = MathUtils.randFloat( - Math.PI, Math.PI );
  37. this.uniforms[ 'seed_x' ].value = MathUtils.randFloat( - 1, 1 );
  38. this.uniforms[ 'seed_y' ].value = MathUtils.randFloat( - 1, 1 );
  39. this.uniforms[ 'distortion_x' ].value = MathUtils.randFloat( 0, 1 );
  40. this.uniforms[ 'distortion_y' ].value = MathUtils.randFloat( 0, 1 );
  41. this.curF = 0;
  42. this.generateTrigger();
  43. } else if ( this.curF % this.randX < this.randX / 5 ) {
  44. this.uniforms[ 'amount' ].value = Math.random() / 90;
  45. this.uniforms[ 'angle' ].value = MathUtils.randFloat( - Math.PI, Math.PI );
  46. this.uniforms[ 'distortion_x' ].value = MathUtils.randFloat( 0, 1 );
  47. this.uniforms[ 'distortion_y' ].value = MathUtils.randFloat( 0, 1 );
  48. this.uniforms[ 'seed_x' ].value = MathUtils.randFloat( - 0.3, 0.3 );
  49. this.uniforms[ 'seed_y' ].value = MathUtils.randFloat( - 0.3, 0.3 );
  50. } else if ( this.goWild == false ) {
  51. this.uniforms[ 'byp' ].value = 1;
  52. }
  53. this.curF ++;
  54. if ( this.renderToScreen ) {
  55. renderer.setRenderTarget( null );
  56. this.fsQuad.render( renderer );
  57. } else {
  58. renderer.setRenderTarget( writeBuffer );
  59. if ( this.clear ) renderer.clear();
  60. this.fsQuad.render( renderer );
  61. }
  62. }
  63. generateTrigger() {
  64. this.randX = MathUtils.randInt( 120, 240 );
  65. }
  66. generateHeightmap( dt_size ) {
  67. const data_arr = new Float32Array( dt_size * dt_size );
  68. const length = dt_size * dt_size;
  69. for ( let i = 0; i < length; i ++ ) {
  70. const val = MathUtils.randFloat( 0, 1 );
  71. data_arr[ i ] = val;
  72. }
  73. const texture = new DataTexture( data_arr, dt_size, dt_size, RedFormat, FloatType );
  74. texture.needsUpdate = true;
  75. return texture;
  76. }
  77. }
  78. export { GlitchPass };