ClearPass.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {
  2. Color
  3. } from 'three';
  4. import { Pass } from './Pass.js';
  5. /**
  6. * This class can be used to force a clear operation for the current read or
  7. * default framebuffer (when rendering to screen).
  8. *
  9. * ```js
  10. * const clearPass = new ClearPass();
  11. * composer.addPass( clearPass );
  12. * ```
  13. *
  14. * @augments Pass
  15. */
  16. class ClearPass extends Pass {
  17. /**
  18. * Constructs a new clear pass.
  19. *
  20. * @param {(number|Color|string)} [clearColor=0x000000] - The clear color.
  21. * @param {number} [clearAlpha=0] - The clear alpha.
  22. */
  23. constructor( clearColor = 0x000000, clearAlpha = 0 ) {
  24. super();
  25. /**
  26. * Overwritten to disable the swap.
  27. *
  28. * @type {boolean}
  29. * @default false
  30. */
  31. this.needsSwap = false;
  32. /**
  33. * The clear color.
  34. *
  35. * @type {(number|Color|string)}
  36. * @default 0x000000
  37. */
  38. this.clearColor = clearColor;
  39. /**
  40. * The clear alpha.
  41. *
  42. * @type {number}
  43. * @default 0
  44. */
  45. this.clearAlpha = clearAlpha;
  46. // internals
  47. this._oldClearColor = new Color();
  48. }
  49. /**
  50. * Performs the clear operation. This affects the current read or the default framebuffer.
  51. *
  52. * @param {WebGLRenderer} renderer - The renderer.
  53. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  54. * destination for the pass.
  55. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  56. * previous pass from this buffer.
  57. * @param {number} deltaTime - The delta time in seconds.
  58. * @param {boolean} maskActive - Whether masking is active or not.
  59. */
  60. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  61. let oldClearAlpha;
  62. if ( this.clearColor ) {
  63. renderer.getClearColor( this._oldClearColor );
  64. oldClearAlpha = renderer.getClearAlpha();
  65. renderer.setClearColor( this.clearColor, this.clearAlpha );
  66. }
  67. renderer.setRenderTarget( this.renderToScreen ? null : readBuffer );
  68. renderer.clear();
  69. if ( this.clearColor ) {
  70. renderer.setClearColor( this._oldClearColor, oldClearAlpha );
  71. }
  72. }
  73. }
  74. export { ClearPass };
粤ICP备19079148号