TAARenderPass.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 samples across frames to create a high quality anti-aliased result.
  11. *
  12. * References:
  13. *
  14. * TODO: Add support for motion vector pas so that accumulation of samples across frames can occur on dynamics scenes.
  15. *
  16. */
  17. class TAARenderPass extends SSAARenderPass {
  18. constructor( scene, camera, clearColor, clearAlpha ) {
  19. super( scene, camera, clearColor, clearAlpha );
  20. this.sampleLevel = 0;
  21. this.accumulate = false;
  22. }
  23. render( renderer, writeBuffer, readBuffer, deltaTime ) {
  24. if ( this.accumulate === false ) {
  25. super.render( renderer, writeBuffer, readBuffer, deltaTime );
  26. this.accumulateIndex = - 1;
  27. return;
  28. }
  29. const jitterOffsets = _JitterVectors[ 5 ];
  30. if ( this.sampleRenderTarget === undefined ) {
  31. this.sampleRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, { type: HalfFloatType } );
  32. this.sampleRenderTarget.texture.name = 'TAARenderPass.sample';
  33. }
  34. if ( this.holdRenderTarget === undefined ) {
  35. this.holdRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, { type: HalfFloatType } );
  36. this.holdRenderTarget.texture.name = 'TAARenderPass.hold';
  37. }
  38. if ( this.accumulateIndex === - 1 ) {
  39. super.render( renderer, this.holdRenderTarget, readBuffer, deltaTime );
  40. this.accumulateIndex = 0;
  41. }
  42. const autoClear = renderer.autoClear;
  43. renderer.autoClear = false;
  44. renderer.getClearColor( this._oldClearColor );
  45. const oldClearAlpha = renderer.getClearAlpha();
  46. const sampleWeight = 1.0 / ( jitterOffsets.length );
  47. if ( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  48. this.copyUniforms[ 'opacity' ].value = sampleWeight;
  49. this.copyUniforms[ 'tDiffuse' ].value = writeBuffer.texture;
  50. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  51. const numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  52. for ( let i = 0; i < numSamplesPerFrame; i ++ ) {
  53. const j = this.accumulateIndex;
  54. const jitterOffset = jitterOffsets[ j ];
  55. if ( this.camera.setViewOffset ) {
  56. this.camera.setViewOffset( readBuffer.width, readBuffer.height,
  57. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  58. readBuffer.width, readBuffer.height );
  59. }
  60. renderer.setRenderTarget( writeBuffer );
  61. renderer.setClearColor( this.clearColor, this.clearAlpha );
  62. renderer.clear();
  63. renderer.render( this.scene, this.camera );
  64. renderer.setRenderTarget( this.sampleRenderTarget );
  65. if ( this.accumulateIndex === 0 ) {
  66. renderer.setClearColor( 0x000000, 0.0 );
  67. renderer.clear();
  68. }
  69. this.fsQuad.render( renderer );
  70. this.accumulateIndex ++;
  71. if ( this.accumulateIndex >= jitterOffsets.length ) break;
  72. }
  73. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  74. }
  75. renderer.setClearColor( this.clearColor, this.clearAlpha );
  76. const accumulationWeight = this.accumulateIndex * sampleWeight;
  77. if ( accumulationWeight > 0 ) {
  78. this.copyUniforms[ 'opacity' ].value = 1.0;
  79. this.copyUniforms[ 'tDiffuse' ].value = this.sampleRenderTarget.texture;
  80. renderer.setRenderTarget( writeBuffer );
  81. renderer.clear();
  82. this.fsQuad.render( renderer );
  83. }
  84. if ( accumulationWeight < 1.0 ) {
  85. this.copyUniforms[ 'opacity' ].value = 1.0 - accumulationWeight;
  86. this.copyUniforms[ 'tDiffuse' ].value = this.holdRenderTarget.texture;
  87. renderer.setRenderTarget( writeBuffer );
  88. this.fsQuad.render( renderer );
  89. }
  90. renderer.autoClear = autoClear;
  91. renderer.setClearColor( this._oldClearColor, oldClearAlpha );
  92. }
  93. dispose() {
  94. super.dispose();
  95. if ( this.sampleRenderTarget !== undefined ) this.sampleRenderTarget.dispose();
  96. if ( this.holdRenderTarget !== undefined ) this.holdRenderTarget.dispose();
  97. }
  98. }
  99. const _JitterVectors = [
  100. [
  101. [ 0, 0 ]
  102. ],
  103. [
  104. [ 4, 4 ], [ - 4, - 4 ]
  105. ],
  106. [
  107. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  108. ],
  109. [
  110. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  111. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  112. ],
  113. [
  114. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  115. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  116. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  117. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  118. ],
  119. [
  120. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  121. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  122. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  123. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  124. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  125. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  126. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  127. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  128. ]
  129. ];
  130. export { TAARenderPass };