ClearPass.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @three_import import { ClearPass } from 'three/addons/postprocessing/ClearPass.js';
  16. */
  17. class ClearPass extends Pass {
  18. /**
  19. * Constructs a new clear pass.
  20. *
  21. * @param {(number|Color|string)} [clearColor=0x000000] - The clear color.
  22. * @param {number} [clearAlpha=0] - The clear alpha.
  23. */
  24. constructor( clearColor = 0x000000, clearAlpha = 0 ) {
  25. super();
  26. /**
  27. * Overwritten to disable the swap.
  28. *
  29. * @type {boolean}
  30. * @default false
  31. */
  32. this.needsSwap = false;
  33. /**
  34. * The clear color.
  35. *
  36. * @type {(number|Color|string)}
  37. * @default 0x000000
  38. */
  39. this.clearColor = clearColor;
  40. /**
  41. * The clear alpha.
  42. *
  43. * @type {number}
  44. * @default 0
  45. */
  46. this.clearAlpha = clearAlpha;
  47. // internals
  48. this._oldClearColor = new Color();
  49. }
  50. /**
  51. * Performs the clear operation. This affects the current read or the default framebuffer.
  52. *
  53. * @param {WebGLRenderer} renderer - The renderer.
  54. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  55. * destination for the pass.
  56. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  57. * previous pass from this buffer.
  58. * @param {number} deltaTime - The delta time in seconds.
  59. * @param {boolean} maskActive - Whether masking is active or not.
  60. */
  61. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  62. let oldClearAlpha;
  63. if ( this.clearColor ) {
  64. renderer.getClearColor( this._oldClearColor );
  65. oldClearAlpha = renderer.getClearAlpha();
  66. renderer.setClearColor( this.clearColor, this.clearAlpha );
  67. }
  68. renderer.setRenderTarget( this.renderToScreen ? null : readBuffer );
  69. renderer.clear();
  70. if ( this.clearColor ) {
  71. renderer.setClearColor( this._oldClearColor, oldClearAlpha );
  72. }
  73. }
  74. }
  75. export { ClearPass };