GlitchPass.js 3.1 KB

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