TAARenderPass.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. const sampleWeight = 1.0 / ( jitterOffsets.length );
  45. if ( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  46. this.copyUniforms[ 'opacity' ].value = sampleWeight;
  47. this.copyUniforms[ 'tDiffuse' ].value = writeBuffer.texture;
  48. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  49. const numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  50. for ( let i = 0; i < numSamplesPerFrame; i ++ ) {
  51. const j = this.accumulateIndex;
  52. const jitterOffset = jitterOffsets[ j ];
  53. if ( this.camera.setViewOffset ) {
  54. this.camera.setViewOffset( readBuffer.width, readBuffer.height,
  55. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  56. readBuffer.width, readBuffer.height );
  57. }
  58. renderer.setRenderTarget( writeBuffer );
  59. renderer.clear();
  60. renderer.render( this.scene, this.camera );
  61. renderer.setRenderTarget( this.sampleRenderTarget );
  62. if ( this.accumulateIndex === 0 ) renderer.clear();
  63. this.fsQuad.render( renderer );
  64. this.accumulateIndex ++;
  65. if ( this.accumulateIndex >= jitterOffsets.length ) break;
  66. }
  67. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  68. }
  69. const accumulationWeight = this.accumulateIndex * sampleWeight;
  70. if ( accumulationWeight > 0 ) {
  71. this.copyUniforms[ 'opacity' ].value = 1.0;
  72. this.copyUniforms[ 'tDiffuse' ].value = this.sampleRenderTarget.texture;
  73. renderer.setRenderTarget( writeBuffer );
  74. renderer.clear();
  75. this.fsQuad.render( renderer );
  76. }
  77. if ( accumulationWeight < 1.0 ) {
  78. this.copyUniforms[ 'opacity' ].value = 1.0 - accumulationWeight;
  79. this.copyUniforms[ 'tDiffuse' ].value = this.holdRenderTarget.texture;
  80. renderer.setRenderTarget( writeBuffer );
  81. if ( accumulationWeight === 0 ) renderer.clear();
  82. this.fsQuad.render( renderer );
  83. }
  84. renderer.autoClear = autoClear;
  85. }
  86. dispose() {
  87. super.dispose();
  88. if ( this.sampleRenderTarget !== undefined ) this.sampleRenderTarget.dispose();
  89. if ( this.holdRenderTarget !== undefined ) this.holdRenderTarget.dispose();
  90. }
  91. }
  92. const _JitterVectors = [
  93. [
  94. [ 0, 0 ]
  95. ],
  96. [
  97. [ 4, 4 ], [ - 4, - 4 ]
  98. ],
  99. [
  100. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  101. ],
  102. [
  103. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  104. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  105. ],
  106. [
  107. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  108. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  109. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  110. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  111. ],
  112. [
  113. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  114. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  115. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  116. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  117. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  118. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  119. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  120. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  121. ]
  122. ];
  123. export { TAARenderPass };