TAARenderPass.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import {
  2. HalfFloatType,
  3. WebGLRenderTarget
  4. } from 'three';
  5. import { SSAARenderPass } from './SSAARenderPass.js';
  6. /**
  7. *
  8. * Temporal Anti-Aliasing Render Pass.
  9. *
  10. * When there is no motion in the scene, the TAA render pass accumulates jittered camera
  11. * samples across frames to create a high quality anti-aliased result.
  12. *
  13. * Note: This effect uses no reprojection so it is no TRAA implementation.
  14. *
  15. * ```js
  16. * const taaRenderPass = new TAARenderPass( scene, camera );
  17. * taaRenderPass.unbiased = false;
  18. * composer.addPass( taaRenderPass );
  19. * ```
  20. *
  21. * @augments SSAARenderPass
  22. */
  23. class TAARenderPass extends SSAARenderPass {
  24. /**
  25. * Constructs a new TAA render pass.
  26. *
  27. * @param {Scene} scene - The scene to render.
  28. * @param {Camera} camera - The camera.
  29. * @param {?(number|Color|string)} [clearColor=0x000000] - The clear color of the render pass.
  30. * @param {?number} [clearAlpha=0] - The clear alpha of the render pass.
  31. */
  32. constructor( scene, camera, clearColor, clearAlpha ) {
  33. super( scene, camera, clearColor, clearAlpha );
  34. /**
  35. * Overwritten and set to 0 by default.
  36. *
  37. * @type {number}
  38. * @default 0
  39. */
  40. this.sampleLevel = 0;
  41. /**
  42. * Whether to accumulate frames or not. This enables
  43. * the TAA.
  44. *
  45. * @type {boolean}
  46. * @default false
  47. */
  48. this.accumulate = false;
  49. /**
  50. * The accumulation index.
  51. *
  52. * @type {number}
  53. * @default -1
  54. */
  55. this.accumulateIndex = - 1;
  56. // internals
  57. this._sampleRenderTarget = null;
  58. this._holdRenderTarget = null;
  59. }
  60. /**
  61. * Performs the TAA render pass.
  62. *
  63. * @param {WebGLRenderer} renderer - The renderer.
  64. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  65. * destination for the pass.
  66. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  67. * previous pass from this buffer.
  68. * @param {number} deltaTime - The delta time in seconds.
  69. * @param {boolean} maskActive - Whether masking is active or not.
  70. */
  71. render( renderer, writeBuffer, readBuffer, deltaTime/*, maskActive*/ ) {
  72. if ( this.accumulate === false ) {
  73. super.render( renderer, writeBuffer, readBuffer, deltaTime );
  74. this.accumulateIndex = - 1;
  75. return;
  76. }
  77. const jitterOffsets = _JitterVectors[ 5 ];
  78. if ( this._sampleRenderTarget === null ) {
  79. this._sampleRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, { type: HalfFloatType } );
  80. this._sampleRenderTarget.texture.name = 'TAARenderPass.sample';
  81. }
  82. if ( this._holdRenderTarget === null ) {
  83. this._holdRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, { type: HalfFloatType } );
  84. this._holdRenderTarget.texture.name = 'TAARenderPass.hold';
  85. }
  86. if ( this.accumulateIndex === - 1 ) {
  87. super.render( renderer, this._holdRenderTarget, readBuffer, deltaTime );
  88. this.accumulateIndex = 0;
  89. }
  90. const autoClear = renderer.autoClear;
  91. renderer.autoClear = false;
  92. renderer.getClearColor( this._oldClearColor );
  93. const oldClearAlpha = renderer.getClearAlpha();
  94. const sampleWeight = 1.0 / ( jitterOffsets.length );
  95. if ( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  96. this._copyUniforms[ 'opacity' ].value = sampleWeight;
  97. this._copyUniforms[ 'tDiffuse' ].value = writeBuffer.texture;
  98. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  99. const numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  100. for ( let i = 0; i < numSamplesPerFrame; i ++ ) {
  101. const j = this.accumulateIndex;
  102. const jitterOffset = jitterOffsets[ j ];
  103. if ( this.camera.setViewOffset ) {
  104. this.camera.setViewOffset( readBuffer.width, readBuffer.height,
  105. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  106. readBuffer.width, readBuffer.height );
  107. }
  108. renderer.setRenderTarget( writeBuffer );
  109. renderer.setClearColor( this.clearColor, this.clearAlpha );
  110. renderer.clear();
  111. renderer.render( this.scene, this.camera );
  112. renderer.setRenderTarget( this._sampleRenderTarget );
  113. if ( this.accumulateIndex === 0 ) {
  114. renderer.setClearColor( 0x000000, 0.0 );
  115. renderer.clear();
  116. }
  117. this._fsQuad.render( renderer );
  118. this.accumulateIndex ++;
  119. if ( this.accumulateIndex >= jitterOffsets.length ) break;
  120. }
  121. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  122. }
  123. renderer.setClearColor( this.clearColor, this.clearAlpha );
  124. const accumulationWeight = this.accumulateIndex * sampleWeight;
  125. if ( accumulationWeight > 0 ) {
  126. this._copyUniforms[ 'opacity' ].value = 1.0;
  127. this._copyUniforms[ 'tDiffuse' ].value = this._sampleRenderTarget.texture;
  128. renderer.setRenderTarget( writeBuffer );
  129. renderer.clear();
  130. this._fsQuad.render( renderer );
  131. }
  132. if ( accumulationWeight < 1.0 ) {
  133. this._copyUniforms[ 'opacity' ].value = 1.0 - accumulationWeight;
  134. this._copyUniforms[ 'tDiffuse' ].value = this._holdRenderTarget.texture;
  135. renderer.setRenderTarget( writeBuffer );
  136. this._fsQuad.render( renderer );
  137. }
  138. renderer.autoClear = autoClear;
  139. renderer.setClearColor( this._oldClearColor, oldClearAlpha );
  140. }
  141. /**
  142. * Frees the GPU-related resources allocated by this instance. Call this
  143. * method whenever the pass is no longer used in your app.
  144. */
  145. dispose() {
  146. super.dispose();
  147. if ( this._holdRenderTarget ) this._holdRenderTarget.dispose();
  148. }
  149. }
  150. const _JitterVectors = [
  151. [
  152. [ 0, 0 ]
  153. ],
  154. [
  155. [ 4, 4 ], [ - 4, - 4 ]
  156. ],
  157. [
  158. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  159. ],
  160. [
  161. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  162. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  163. ],
  164. [
  165. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  166. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  167. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  168. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  169. ],
  170. [
  171. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  172. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  173. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  174. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  175. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  176. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  177. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  178. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  179. ]
  180. ];
  181. export { TAARenderPass };
粤ICP备19079148号