TAARenderPass.js 4.3 KB

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