RenderPass.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import {
  2. Color
  3. } from 'three';
  4. import { Pass } from './Pass.js';
  5. /**
  6. * This class represents a render pass. It takes a camera and a scene and produces
  7. * a beauty pass for subsequent post processing effects.
  8. *
  9. * ```js
  10. * const renderPass = new RenderPass( scene, camera );
  11. * composer.addPass( renderPass );
  12. * ```
  13. *
  14. * @augments Pass
  15. * @three_import import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  16. */
  17. class RenderPass extends Pass {
  18. /**
  19. * Constructs a new render pass.
  20. *
  21. * @param {Scene} scene - The scene to render.
  22. * @param {Camera} camera - The camera.
  23. * @param {?Material} [overrideMaterial=null] - The override material. If set, this material is used
  24. * for all objects in the scene.
  25. * @param {?(number|Color|string)} [clearColor=null] - The clear color of the render pass.
  26. * @param {?number} [clearAlpha=null] - The clear alpha of the render pass.
  27. */
  28. constructor( scene, camera, overrideMaterial = null, clearColor = null, clearAlpha = null ) {
  29. super();
  30. /**
  31. * The scene to render.
  32. *
  33. * @type {Scene}
  34. */
  35. this.scene = scene;
  36. /**
  37. * The camera.
  38. *
  39. * @type {Camera}
  40. */
  41. this.camera = camera;
  42. /**
  43. * The override material. If set, this material is used
  44. * for all objects in the scene.
  45. *
  46. * @type {?Material}
  47. * @default null
  48. */
  49. this.overrideMaterial = overrideMaterial;
  50. /**
  51. * The clear color of the render pass.
  52. *
  53. * @type {?(number|Color|string)}
  54. * @default null
  55. */
  56. this.clearColor = clearColor;
  57. /**
  58. * The clear alpha of the render pass.
  59. *
  60. * @type {?number}
  61. * @default null
  62. */
  63. this.clearAlpha = clearAlpha;
  64. /**
  65. * Overwritten to perform a clear operation by default.
  66. *
  67. * @type {boolean}
  68. * @default true
  69. */
  70. this.clear = true;
  71. /**
  72. * If set to `true`, only the depth can be cleared when `clear` is to `false`.
  73. *
  74. * @type {boolean}
  75. * @default false
  76. */
  77. this.clearDepth = false;
  78. /**
  79. * Overwritten to disable the swap.
  80. *
  81. * @type {boolean}
  82. * @default false
  83. */
  84. this.needsSwap = false;
  85. this._oldClearColor = new Color();
  86. }
  87. /**
  88. * Performs a beauty pass with the configured scene and camera.
  89. *
  90. * @param {WebGLRenderer} renderer - The renderer.
  91. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  92. * destination for the pass.
  93. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  94. * previous pass from this buffer.
  95. * @param {number} deltaTime - The delta time in seconds.
  96. * @param {boolean} maskActive - Whether masking is active or not.
  97. */
  98. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  99. const oldAutoClear = renderer.autoClear;
  100. renderer.autoClear = false;
  101. let oldClearAlpha, oldOverrideMaterial;
  102. if ( this.overrideMaterial !== null ) {
  103. oldOverrideMaterial = this.scene.overrideMaterial;
  104. this.scene.overrideMaterial = this.overrideMaterial;
  105. }
  106. if ( this.clearColor !== null ) {
  107. renderer.getClearColor( this._oldClearColor );
  108. renderer.setClearColor( this.clearColor, renderer.getClearAlpha() );
  109. }
  110. if ( this.clearAlpha !== null ) {
  111. oldClearAlpha = renderer.getClearAlpha();
  112. renderer.setClearAlpha( this.clearAlpha );
  113. }
  114. if ( this.clearDepth == true ) {
  115. renderer.clearDepth();
  116. }
  117. renderer.setRenderTarget( this.renderToScreen ? null : readBuffer );
  118. if ( this.clear === true ) {
  119. // TODO: Avoid using autoClear properties, see https://github.com/mrdoob/three.js/pull/15571#issuecomment-465669600
  120. renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  121. }
  122. renderer.render( this.scene, this.camera );
  123. // restore
  124. if ( this.clearColor !== null ) {
  125. renderer.setClearColor( this._oldClearColor );
  126. }
  127. if ( this.clearAlpha !== null ) {
  128. renderer.setClearAlpha( oldClearAlpha );
  129. }
  130. if ( this.overrideMaterial !== null ) {
  131. this.scene.overrideMaterial = oldOverrideMaterial;
  132. }
  133. renderer.autoClear = oldAutoClear;
  134. }
  135. }
  136. export { RenderPass };