AfterimagePass.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author HypnosNova / https://www.threejs.org.cn/gallery/
  3. */
  4. import {
  5. LinearFilter,
  6. MeshBasicMaterial,
  7. NearestFilter,
  8. RGBAFormat,
  9. ShaderMaterial,
  10. UniformsUtils,
  11. WebGLRenderTarget
  12. } from "../../../build/three.module.js";
  13. import { Pass } from "../postprocessing/Pass.js";
  14. import { AfterimageShader } from "../shaders/AfterimageShader.js";
  15. var AfterimagePass = function ( damp ) {
  16. Pass.call( this );
  17. if ( AfterimageShader === undefined )
  18. console.error( "AfterimagePass relies on AfterimageShader" );
  19. this.shader = AfterimageShader;
  20. this.uniforms = UniformsUtils.clone( this.shader.uniforms );
  21. this.uniforms[ "damp" ].value = damp !== undefined ? damp : 0.96;
  22. this.textureComp = new WebGLRenderTarget( window.innerWidth, window.innerHeight, {
  23. minFilter: LinearFilter,
  24. magFilter: NearestFilter,
  25. format: RGBAFormat
  26. } );
  27. this.textureOld = new WebGLRenderTarget( window.innerWidth, window.innerHeight, {
  28. minFilter: LinearFilter,
  29. magFilter: NearestFilter,
  30. format: RGBAFormat
  31. } );
  32. this.shaderMaterial = new ShaderMaterial( {
  33. uniforms: this.uniforms,
  34. vertexShader: this.shader.vertexShader,
  35. fragmentShader: this.shader.fragmentShader
  36. } );
  37. this.compFsQuad = new Pass.FullScreenQuad( this.shaderMaterial );
  38. var material = new MeshBasicMaterial();
  39. this.copyFsQuad = new Pass.FullScreenQuad( material );
  40. };
  41. AfterimagePass.prototype = Object.assign( Object.create( Pass.prototype ), {
  42. constructor: AfterimagePass,
  43. render: function ( renderer, writeBuffer, readBuffer ) {
  44. this.uniforms[ "tOld" ].value = this.textureOld.texture;
  45. this.uniforms[ "tNew" ].value = readBuffer.texture;
  46. renderer.setRenderTarget( this.textureComp );
  47. this.compFsQuad.render( renderer );
  48. this.copyFsQuad.material.map = this.textureComp.texture;
  49. if ( this.renderToScreen ) {
  50. renderer.setRenderTarget( null );
  51. this.copyFsQuad.render( renderer );
  52. } else {
  53. renderer.setRenderTarget( writeBuffer );
  54. if ( this.clear ) renderer.clear();
  55. this.copyFsQuad.render( renderer );
  56. }
  57. // Swap buffers.
  58. var temp = this.textureOld;
  59. this.textureOld = this.textureComp;
  60. this.textureComp = temp;
  61. // Now textureOld contains the latest image, ready for the next frame.
  62. },
  63. setSize: function ( width, height ) {
  64. this.textureComp.setSize( width, height );
  65. this.textureOld.setSize( width, height );
  66. }
  67. } );
  68. export { AfterimagePass };