TAARenderPass.js 4.2 KB

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