SavePass.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {
  2. HalfFloatType,
  3. NoBlending,
  4. ShaderMaterial,
  5. UniformsUtils,
  6. WebGLRenderTarget
  7. } from 'three';
  8. import { Pass, FullScreenQuad } from './Pass.js';
  9. import { CopyShader } from '../shaders/CopyShader.js';
  10. /**
  11. * A pass that saves the contents of the current read buffer in a render target.
  12. *
  13. * ```js
  14. * const savePass = new SavePass( customRenderTarget );
  15. * composer.addPass( savePass );
  16. * ```
  17. *
  18. * @augments Pass
  19. * @three_import import { SavePass } from 'three/addons/postprocessing/SavePass.js';
  20. */
  21. class SavePass extends Pass {
  22. /**
  23. * Constructs a new save pass.
  24. *
  25. * @param {WebGLRenderTarget} [renderTarget] - The render target for saving the read buffer.
  26. * If not provided, the pass automatically creates a render target.
  27. */
  28. constructor( renderTarget ) {
  29. super();
  30. /**
  31. * The pass uniforms.
  32. *
  33. * @type {Object}
  34. */
  35. this.uniforms = UniformsUtils.clone( CopyShader.uniforms );
  36. /**
  37. * The pass material.
  38. *
  39. * @type {ShaderMaterial}
  40. */
  41. this.material = new ShaderMaterial( {
  42. uniforms: this.uniforms,
  43. vertexShader: CopyShader.vertexShader,
  44. fragmentShader: CopyShader.fragmentShader,
  45. blending: NoBlending
  46. } );
  47. /**
  48. * The render target which is used to save the read buffer.
  49. *
  50. * @type {WebGLRenderTarget}
  51. */
  52. this.renderTarget = renderTarget;
  53. if ( this.renderTarget === undefined ) {
  54. this.renderTarget = new WebGLRenderTarget( 1, 1, { type: HalfFloatType } ); // will be resized later
  55. this.renderTarget.texture.name = 'SavePass.rt';
  56. }
  57. /**
  58. * Overwritten to disable the swap.
  59. *
  60. * @type {boolean}
  61. * @default false
  62. */
  63. this.needsSwap = false;
  64. // internals
  65. this._fsQuad = new FullScreenQuad( this.material );
  66. }
  67. /**
  68. * Performs the save pass.
  69. *
  70. * @param {WebGLRenderer} renderer - The renderer.
  71. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  72. * destination for the pass.
  73. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  74. * previous pass from this buffer.
  75. * @param {number} deltaTime - The delta time in seconds.
  76. * @param {boolean} maskActive - Whether masking is active or not.
  77. */
  78. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive */ ) {
  79. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  80. renderer.setRenderTarget( this.renderTarget );
  81. if ( this.clear ) renderer.clear();
  82. this._fsQuad.render( renderer );
  83. }
  84. /**
  85. * Sets the size of the pass.
  86. *
  87. * @param {number} width - The width to set.
  88. * @param {number} height - The width to set.
  89. */
  90. setSize( width, height ) {
  91. this.renderTarget.setSize( width, height );
  92. }
  93. /**
  94. * Frees the GPU-related resources allocated by this instance. Call this
  95. * method whenever the pass is no longer used in your app.
  96. */
  97. dispose() {
  98. this.renderTarget.dispose();
  99. this.material.dispose();
  100. this._fsQuad.dispose();
  101. }
  102. }
  103. export { SavePass };