GlitchPass.js 3.0 KB

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