TAARenderPass.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. *
  3. * Temporal Anti-Aliasing Render Pass
  4. *
  5. * 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.
  6. *
  7. * References:
  8. *
  9. * TODO: Add support for motion vector pas so that accumulation of samples across frames can occur on dynamics scenes.
  10. *
  11. */
  12. THREE.TAARenderPass = function ( scene, camera, clearColor, clearAlpha ) {
  13. if ( THREE.SSAARenderPass === undefined ) {
  14. console.error( 'THREE.TAARenderPass relies on THREE.SSAARenderPass' );
  15. }
  16. THREE.SSAARenderPass.call( this, scene, camera, clearColor, clearAlpha );
  17. this.sampleLevel = 0;
  18. this.accumulate = false;
  19. };
  20. THREE.TAARenderPass.JitterVectors = THREE.SSAARenderPass.JitterVectors;
  21. THREE.TAARenderPass.prototype = Object.assign( Object.create( THREE.SSAARenderPass.prototype ), {
  22. constructor: THREE.TAARenderPass,
  23. render: function ( renderer, writeBuffer, readBuffer, deltaTime ) {
  24. if ( ! this.accumulate ) {
  25. THREE.SSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, deltaTime );
  26. this.accumulateIndex = - 1;
  27. return;
  28. }
  29. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 5 ];
  30. if ( ! this.sampleRenderTarget ) {
  31. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  32. this.sampleRenderTarget.texture.name = 'TAARenderPass.sample';
  33. }
  34. if ( ! this.holdRenderTarget ) {
  35. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  36. this.holdRenderTarget.texture.name = 'TAARenderPass.hold';
  37. }
  38. if ( this.accumulate && this.accumulateIndex === - 1 ) {
  39. THREE.SSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, deltaTime );
  40. this.accumulateIndex = 0;
  41. }
  42. var autoClear = renderer.autoClear;
  43. renderer.autoClear = false;
  44. var 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. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  50. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  51. var j = this.accumulateIndex;
  52. var 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. var 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. } );