TAARenderPass.js 6.3 KB

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