GlitchPass.js 2.9 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.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. 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 * 3 );
  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 * 3 + 0 ] = val;
  63. data_arr[ i * 3 + 1 ] = val;
  64. data_arr[ i * 3 + 2 ] = val;
  65. }
  66. return new THREE.DataTexture( data_arr, dt_size, dt_size, THREE.RGBFormat, THREE.FloatType );
  67. }
  68. }
  69. THREE.GlitchPass = GlitchPass;
  70. } )();