SavePass.js 2.8 KB

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