RenderPass.js 3.9 KB

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