EffectComposer.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import {
  2. Clock,
  3. HalfFloatType,
  4. NoBlending,
  5. Vector2,
  6. WebGLRenderTarget
  7. } from 'three';
  8. import { CopyShader } from '../shaders/CopyShader.js';
  9. import { ShaderPass } from './ShaderPass.js';
  10. import { ClearMaskPass, MaskPass } from './MaskPass.js';
  11. /**
  12. * Used to implement post-processing effects in three.js.
  13. * The class manages a chain of post-processing passes to produce the final visual result.
  14. * Post-processing passes are executed in order of their addition/insertion.
  15. * The last pass is automatically rendered to screen.
  16. *
  17. * This module can only be used with {@link WebGLRenderer}.
  18. *
  19. * ```js
  20. * const composer = new EffectComposer( renderer );
  21. *
  22. * // adding some passes
  23. * const renderPass = new RenderPass( scene, camera );
  24. * composer.addPass( renderPass );
  25. *
  26. * const glitchPass = new GlitchPass();
  27. * composer.addPass( glitchPass );
  28. *
  29. * const outputPass = new OutputPass()
  30. * composer.addPass( outputPass );
  31. *
  32. * function animate() {
  33. *
  34. * composer.render(); // instead of renderer.render()
  35. *
  36. * }
  37. * ```
  38. */
  39. class EffectComposer {
  40. /**
  41. * Constructs a new effect composer.
  42. *
  43. * @param {WebGLRenderer} renderer - The renderer.
  44. * @param {WebGLRenderTarget} [renderTarget] - This render target and a clone will
  45. * be used as the internal read and write buffers. If not given, the composer creates
  46. * the buffers automatically.
  47. */
  48. constructor( renderer, renderTarget ) {
  49. /**
  50. * The renderer.
  51. *
  52. * @type {WebGLRenderer}
  53. */
  54. this.renderer = renderer;
  55. this._pixelRatio = renderer.getPixelRatio();
  56. if ( renderTarget === undefined ) {
  57. const size = renderer.getSize( new Vector2() );
  58. this._width = size.width;
  59. this._height = size.height;
  60. renderTarget = new WebGLRenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } );
  61. renderTarget.texture.name = 'EffectComposer.rt1';
  62. } else {
  63. this._width = renderTarget.width;
  64. this._height = renderTarget.height;
  65. }
  66. this.renderTarget1 = renderTarget;
  67. this.renderTarget2 = renderTarget.clone();
  68. this.renderTarget2.texture.name = 'EffectComposer.rt2';
  69. /**
  70. * A reference to the internal write buffer. Passes usually write
  71. * their result into this buffer.
  72. *
  73. * @type {WebGLRenderTarget}
  74. */
  75. this.writeBuffer = this.renderTarget1;
  76. /**
  77. * A reference to the internal read buffer. Passes usually read
  78. * the previous render result from this buffer.
  79. *
  80. * @type {WebGLRenderTarget}
  81. */
  82. this.readBuffer = this.renderTarget2;
  83. /**
  84. * Whether the final pass is rendered to the screen (default framebuffer) or not.
  85. *
  86. * @type {boolean}
  87. * @default true
  88. */
  89. this.renderToScreen = true;
  90. /**
  91. * An array representing the (ordered) chain of post-processing passes.
  92. *
  93. * @type {Array<Pass>}
  94. */
  95. this.passes = [];
  96. /**
  97. * A copy pass used for internal swap operations.
  98. *
  99. * @private
  100. * @type {ShaderPass}
  101. */
  102. this.copyPass = new ShaderPass( CopyShader );
  103. this.copyPass.material.blending = NoBlending;
  104. /**
  105. * The internal clock for managing time data.
  106. *
  107. * @private
  108. * @type {Clock}
  109. */
  110. this.clock = new Clock();
  111. }
  112. /**
  113. * Swaps the internal read/write buffers.
  114. */
  115. swapBuffers() {
  116. const tmp = this.readBuffer;
  117. this.readBuffer = this.writeBuffer;
  118. this.writeBuffer = tmp;
  119. }
  120. /**
  121. * Adds the given pass to the pass chain.
  122. *
  123. * @param {Pass} pass - The pass to add.
  124. */
  125. addPass( pass ) {
  126. this.passes.push( pass );
  127. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  128. }
  129. /**
  130. * Inserts the given pass at a given index.
  131. *
  132. * @param {Pass} pass - The pass to insert.
  133. * @param {number} index - The index into the pass chain.
  134. */
  135. insertPass( pass, index ) {
  136. this.passes.splice( index, 0, pass );
  137. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  138. }
  139. /**
  140. * Removes the given pass from the pass chain.
  141. *
  142. * @param {Pass} pass - The pass to remove.
  143. */
  144. removePass( pass ) {
  145. const index = this.passes.indexOf( pass );
  146. if ( index !== - 1 ) {
  147. this.passes.splice( index, 1 );
  148. }
  149. }
  150. /**
  151. * Returns `true` if the pass for the given index is the last enabled pass in the pass chain.
  152. *
  153. * @param {number} passIndex - The pass index.
  154. * @return {boolean} Whether the the pass for the given index is the last pass in the pass chain.
  155. */
  156. isLastEnabledPass( passIndex ) {
  157. for ( let i = passIndex + 1; i < this.passes.length; i ++ ) {
  158. if ( this.passes[ i ].enabled ) {
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. /**
  165. * Executes all enabled post-processing passes in order to produce the final frame.
  166. *
  167. * @param {number} deltaTime - The delta time in seconds. If not given, the composer computes
  168. * its own time delta value.
  169. */
  170. render( deltaTime ) {
  171. // deltaTime value is in seconds
  172. if ( deltaTime === undefined ) {
  173. deltaTime = this.clock.getDelta();
  174. }
  175. const currentRenderTarget = this.renderer.getRenderTarget();
  176. let maskActive = false;
  177. for ( let i = 0, il = this.passes.length; i < il; i ++ ) {
  178. const pass = this.passes[ i ];
  179. if ( pass.enabled === false ) continue;
  180. pass.renderToScreen = ( this.renderToScreen && this.isLastEnabledPass( i ) );
  181. pass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime, maskActive );
  182. if ( pass.needsSwap ) {
  183. if ( maskActive ) {
  184. const context = this.renderer.getContext();
  185. const stencil = this.renderer.state.buffers.stencil;
  186. //context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  187. stencil.setFunc( context.NOTEQUAL, 1, 0xffffffff );
  188. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime );
  189. //context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  190. stencil.setFunc( context.EQUAL, 1, 0xffffffff );
  191. }
  192. this.swapBuffers();
  193. }
  194. if ( MaskPass !== undefined ) {
  195. if ( pass instanceof MaskPass ) {
  196. maskActive = true;
  197. } else if ( pass instanceof ClearMaskPass ) {
  198. maskActive = false;
  199. }
  200. }
  201. }
  202. this.renderer.setRenderTarget( currentRenderTarget );
  203. }
  204. /**
  205. * Resets the internal state of the EffectComposer.
  206. *
  207. * @param {WebGLRenderTarget} [renderTarget] - This render target has the same purpose like
  208. * the one from the constructor. If set, it is used to setup the read and write buffers.
  209. */
  210. reset( renderTarget ) {
  211. if ( renderTarget === undefined ) {
  212. const size = this.renderer.getSize( new Vector2() );
  213. this._pixelRatio = this.renderer.getPixelRatio();
  214. this._width = size.width;
  215. this._height = size.height;
  216. renderTarget = this.renderTarget1.clone();
  217. renderTarget.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  218. }
  219. this.renderTarget1.dispose();
  220. this.renderTarget2.dispose();
  221. this.renderTarget1 = renderTarget;
  222. this.renderTarget2 = renderTarget.clone();
  223. this.writeBuffer = this.renderTarget1;
  224. this.readBuffer = this.renderTarget2;
  225. }
  226. /**
  227. * Resizes the internal read and write buffers as well as all passes. Similar to {@link WebGLRenderer#setSize},
  228. * this method honors the current pixel ration.
  229. *
  230. * @param {number} width - The width in logical pixels.
  231. * @param {number} height - The height in logical pixels.
  232. */
  233. setSize( width, height ) {
  234. this._width = width;
  235. this._height = height;
  236. const effectiveWidth = this._width * this._pixelRatio;
  237. const effectiveHeight = this._height * this._pixelRatio;
  238. this.renderTarget1.setSize( effectiveWidth, effectiveHeight );
  239. this.renderTarget2.setSize( effectiveWidth, effectiveHeight );
  240. for ( let i = 0; i < this.passes.length; i ++ ) {
  241. this.passes[ i ].setSize( effectiveWidth, effectiveHeight );
  242. }
  243. }
  244. /**
  245. * Sets device pixel ratio. This is usually used for HiDPI device to prevent blurring output.
  246. * Setting the pixel ratio will automatically resize the composer.
  247. *
  248. * @param {number} pixelRatio - The pixel ratio to set.
  249. */
  250. setPixelRatio( pixelRatio ) {
  251. this._pixelRatio = pixelRatio;
  252. this.setSize( this._width, this._height );
  253. }
  254. /**
  255. * Frees the GPU-related resources allocated by this instance. Call this
  256. * method whenever the composer is no longer used in your app.
  257. */
  258. dispose() {
  259. this.renderTarget1.dispose();
  260. this.renderTarget2.dispose();
  261. this.copyPass.dispose();
  262. }
  263. }
  264. export { EffectComposer };
粤ICP备19079148号