TAARenderPass.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. var TAARenderPass = function ( scene, camera, clearColor, clearAlpha ) {
  14. if ( THREE.SSAARenderPass === undefined ) {
  15. console.error( 'THREE.TAARenderPass relies on THREE.SSAARenderPass' );
  16. }
  17. THREE.SSAARenderPass.call( this, scene, camera, clearColor, clearAlpha );
  18. this.sampleLevel = 0;
  19. this.accumulate = false;
  20. };
  21. TAARenderPass.JitterVectors = THREE.SSAARenderPass.JitterVectors;
  22. TAARenderPass.prototype = Object.assign( Object.create( THREE.SSAARenderPass.prototype ), {
  23. constructor: TAARenderPass,
  24. render: function ( renderer, writeBuffer, readBuffer, deltaTime ) {
  25. if ( ! this.accumulate ) {
  26. THREE.SSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, deltaTime );
  27. this.accumulateIndex = - 1;
  28. return;
  29. }
  30. var jitterOffsets = TAARenderPass.JitterVectors[ 5 ];
  31. if ( ! this.sampleRenderTarget ) {
  32. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  33. this.sampleRenderTarget.texture.name = 'TAARenderPass.sample';
  34. }
  35. if ( ! this.holdRenderTarget ) {
  36. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  37. this.holdRenderTarget.texture.name = 'TAARenderPass.hold';
  38. }
  39. if ( this.accumulate && this.accumulateIndex === - 1 ) {
  40. THREE.SSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, deltaTime );
  41. this.accumulateIndex = 0;
  42. }
  43. var autoClear = renderer.autoClear;
  44. renderer.autoClear = false;
  45. var sampleWeight = 1.0 / jitterOffsets.length;
  46. if ( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  47. this.copyUniforms[ 'opacity' ].value = sampleWeight;
  48. this.copyUniforms[ 'tDiffuse' ].value = writeBuffer.texture; // 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, 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. var 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. THREE.TAARenderPass = TAARenderPass;
  87. } )();