TAARenderPass.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {
  2. WebGLRenderTarget
  3. } from '../../../build/three.module.js';
  4. import { SSAARenderPass } from '../postprocessing/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. var TAARenderPass = function ( scene, camera, clearColor, clearAlpha ) {
  17. if ( SSAARenderPass === undefined ) {
  18. console.error( 'THREE.TAARenderPass relies on SSAARenderPass' );
  19. }
  20. SSAARenderPass.call( this, scene, camera, clearColor, clearAlpha );
  21. this.sampleLevel = 0;
  22. this.accumulate = false;
  23. };
  24. TAARenderPass.JitterVectors = SSAARenderPass.JitterVectors;
  25. TAARenderPass.prototype = Object.assign( Object.create( SSAARenderPass.prototype ), {
  26. constructor: TAARenderPass,
  27. render: function ( renderer, writeBuffer, readBuffer, deltaTime ) {
  28. if ( ! this.accumulate ) {
  29. SSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, deltaTime );
  30. this.accumulateIndex = - 1;
  31. return;
  32. }
  33. var jitterOffsets = TAARenderPass.JitterVectors[ 5 ];
  34. if ( ! this.sampleRenderTarget ) {
  35. this.sampleRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  36. this.sampleRenderTarget.texture.name = 'TAARenderPass.sample';
  37. }
  38. if ( ! this.holdRenderTarget ) {
  39. this.holdRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  40. this.holdRenderTarget.texture.name = 'TAARenderPass.hold';
  41. }
  42. if ( this.accumulate && this.accumulateIndex === - 1 ) {
  43. SSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, deltaTime );
  44. this.accumulateIndex = 0;
  45. }
  46. var autoClear = renderer.autoClear;
  47. renderer.autoClear = false;
  48. var sampleWeight = 1.0 / ( jitterOffsets.length );
  49. if ( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  50. this.copyUniforms[ 'opacity' ].value = sampleWeight;
  51. this.copyUniforms[ 'tDiffuse' ].value = writeBuffer.texture;
  52. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  53. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  54. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  55. var j = this.accumulateIndex;
  56. var jitterOffset = jitterOffsets[ j ];
  57. if ( this.camera.setViewOffset ) {
  58. this.camera.setViewOffset( readBuffer.width, readBuffer.height,
  59. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  60. readBuffer.width, readBuffer.height );
  61. }
  62. renderer.setRenderTarget( writeBuffer );
  63. renderer.clear();
  64. renderer.render( this.scene, this.camera );
  65. renderer.setRenderTarget( this.sampleRenderTarget );
  66. if ( this.accumulateIndex === 0 ) renderer.clear();
  67. this.fsQuad.render( renderer );
  68. this.accumulateIndex ++;
  69. if ( this.accumulateIndex >= jitterOffsets.length ) break;
  70. }
  71. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  72. }
  73. var accumulationWeight = this.accumulateIndex * sampleWeight;
  74. if ( accumulationWeight > 0 ) {
  75. this.copyUniforms[ 'opacity' ].value = 1.0;
  76. this.copyUniforms[ 'tDiffuse' ].value = this.sampleRenderTarget.texture;
  77. renderer.setRenderTarget( writeBuffer );
  78. renderer.clear();
  79. this.fsQuad.render( renderer );
  80. }
  81. if ( accumulationWeight < 1.0 ) {
  82. this.copyUniforms[ 'opacity' ].value = 1.0 - accumulationWeight;
  83. this.copyUniforms[ 'tDiffuse' ].value = this.holdRenderTarget.texture;
  84. renderer.setRenderTarget( writeBuffer );
  85. if ( accumulationWeight === 0 ) renderer.clear();
  86. this.fsQuad.render( renderer );
  87. }
  88. renderer.autoClear = autoClear;
  89. }
  90. } );
  91. export { TAARenderPass };